keypin 0.5.0 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -21,6 +21,15 @@ class Secret {
21
21
  reveal() {
22
22
  return this.#value;
23
23
  }
24
+ equals(other) {
25
+ return this.#value === other.#value;
26
+ }
27
+ isEmpty() {
28
+ return !this.#value;
29
+ }
30
+ includes(text) {
31
+ return this.#value.includes(text);
32
+ }
24
33
  toString() {
25
34
  return HIDDEN;
26
35
  }
@@ -151,6 +160,9 @@ function receiptSecretKey(receipt) {
151
160
  function placeId(target) {
152
161
  return target.to === "cloudflare-worker" ? `cloudflare-worker:${target.worker}` : `github-actions:${target.repo}`;
153
162
  }
163
+ function isVariableName(name) {
164
+ return VARIABLE_NAME.test(name);
165
+ }
154
166
  function parseMap(text, file) {
155
167
  let data;
156
168
  try {
@@ -164,6 +176,7 @@ function parseMap(text, file) {
164
176
  return validateMap(data, file);
165
177
  }
166
178
  var ENVIRONMENT_NAME = /^[a-z][a-z0-9-]*$/;
179
+ var TARGET_NAME = ENVIRONMENT_NAME;
167
180
  var KEY_FIELDS = ["rotate_every"];
168
181
  var VARIABLE_NAME = /^[A-Za-z_][A-Za-z0-9_]*$/;
169
182
  var REPO = /^[A-Za-z0-9-]+\/[A-Za-z0-9._-]+$/;
@@ -174,9 +187,10 @@ function validateMap(data, file) {
174
187
  const fail = (where, message) => errors.push(`${file}: ${where}${message}`);
175
188
  if (!isRecord(data))
176
189
  return { ok: false, errors: [`${file}: expected a table at the top level`] };
177
- unknownFields(data, ["environments", "1password", "keys"], (field) => fail("", field === "vaults" ? `[vaults] is from keypin 0.4; use [1password] and op:// references (${OLD_FORMAT})` : `unknown top-level field "${field}"`));
190
+ unknownFields(data, ["environments", "1password", "targets", "keys"], (field) => fail("", field === "vaults" ? `[vaults] is from keypin 0.4; use [1password] and op:// references (${OLD_FORMAT})` : `unknown top-level field "${field}"`));
178
191
  const environments = validateEnvironments(data.environments, (message) => fail("", message));
179
192
  const account = validateAccount(data["1password"], (message) => fail("[1password]: ", message));
193
+ const targets = validateTargets(data.targets, (where, message) => fail(where, message));
180
194
  const keys = [];
181
195
  const rawKeys = data.keys ?? {};
182
196
  if (!isRecord(rawKeys))
@@ -184,11 +198,15 @@ function validateMap(data, file) {
184
198
  else {
185
199
  Object.entries(rawKeys).forEach(([id, raw], index) => {
186
200
  const label = VARIABLE_NAME.test(id) ? `key "${id}"` : `keys[${index}]`;
187
- const entry = validateKey(id, raw, account, environments, (where, message) => fail(`${label}${where}: `, message));
201
+ const entry = validateKey(id, raw, account, environments, targets, (where, message) => fail(`${label}${where}: `, message));
188
202
  if (entry)
189
203
  keys.push(entry);
190
204
  });
191
205
  }
206
+ for (const name of targets.defined) {
207
+ if (!targets.used.has(name))
208
+ fail(`target "${name}": `, "no key publishes to it; use it in a publish list or delete it");
209
+ }
192
210
  sharedDestinations(keys, environments, (where, message) => fail(where, message));
193
211
  return errors.length ? { ok: false, errors } : { ok: true, map: { file, account: account ?? null, environments, keys } };
194
212
  }
@@ -202,6 +220,42 @@ function validateAccount(raw, fail) {
202
220
  unknownFields(raw, ["account"], (field) => fail(`unknown field "${field}"`));
203
221
  return requiredString(raw, "account", fail);
204
222
  }
223
+ function validateTargets(raw, fail) {
224
+ const targets = { defined: [], valid: new Map, used: new Set };
225
+ if (raw === undefined)
226
+ return targets;
227
+ if (!isRecord(raw)) {
228
+ fail("", `"targets" must be a table of named destinations, such as [targets.ci]`);
229
+ return targets;
230
+ }
231
+ Object.entries(raw).forEach(([name, table], index) => {
232
+ if (!TARGET_NAME.test(name)) {
233
+ fail(`targets[${index}]: `, "a target name starts with a lowercase letter and uses only lowercase letters, digits and dashes");
234
+ return;
235
+ }
236
+ targets.defined.push(name);
237
+ const target = validateTarget(table, (m) => fail(`target "${name}": `, m));
238
+ if (target)
239
+ targets.valid.set(name, target);
240
+ });
241
+ return targets;
242
+ }
243
+ function resolveTarget(raw, targets, fail) {
244
+ if (isRecord(raw))
245
+ return validateTarget(raw, fail);
246
+ if (typeof raw !== "string") {
247
+ fail("must be a target name or a table");
248
+ return;
249
+ }
250
+ const name = targets.defined.find((defined) => defined === raw);
251
+ if (name === undefined) {
252
+ fail(targets.defined.length ? `names no target in [targets]; targets are ${targets.defined.map((n) => `"${n}"`).join(", ")}` : "names a target, but the map has no [targets]");
253
+ return;
254
+ }
255
+ targets.used.add(name);
256
+ const target = targets.valid.get(name);
257
+ return target ? { ...target } : undefined;
258
+ }
205
259
  function validateEnvironments(raw, fail) {
206
260
  if (raw === undefined)
207
261
  return DEFAULT_ENVIRONMENTS;
@@ -236,7 +290,7 @@ function sharedDestinations(keys, environments, fail) {
236
290
  }
237
291
  }
238
292
  }
239
- function validateKey(id, raw, account, environmentNames, fail) {
293
+ function validateKey(id, raw, account, environmentNames, targets, fail) {
240
294
  let failures = 0;
241
295
  const err = (where, message) => {
242
296
  failures++;
@@ -256,7 +310,7 @@ function validateKey(id, raw, account, environmentNames, fail) {
256
310
  const environments = Object.create(null);
257
311
  const declared = environmentNames.filter((name) => Object.hasOwn(raw, name));
258
312
  for (const name of declared) {
259
- const entry = validateEnv(raw[name], account, (m) => err(` ${name}`, m));
313
+ const entry = validateEnv(raw[name], account, targets, (m) => err(` ${name}`, m));
260
314
  if (entry)
261
315
  environments[name] = entry;
262
316
  }
@@ -273,7 +327,7 @@ function validateKey(id, raw, account, environmentNames, fail) {
273
327
  return;
274
328
  return { name: id, rotateEvery: rotateEvery ?? null, environments };
275
329
  }
276
- function validateEnv(given, account, fail) {
330
+ function validateEnv(given, account, targets, fail) {
277
331
  const raw = typeof given === "string" ? { from: given } : given;
278
332
  if (!isRecord(raw)) {
279
333
  fail(`must be an op:// reference, or a table with "from"`);
@@ -284,7 +338,7 @@ function validateEnv(given, account, fail) {
284
338
  failed = true;
285
339
  fail(message);
286
340
  };
287
- unknownFields(raw, ["from", "publish", "issue"], (field) => err(field === "vault" || field === "item" ? `"vault" and "item" are from keypin 0.4; use from = "op://<vault>/<item>/<field>" (${OLD_FORMAT})` : `unknown field "${field}"`));
341
+ unknownFields(raw, ["from", "publish", "rotate"], (field) => err(field === "vault" || field === "item" ? `"vault" and "item" are from keypin 0.4; use from = "op://<vault>/<item>/<field>" (${OLD_FORMAT})` : field === "issue" ? `the "issue" section is from keypin 0.5; rename it to "rotate" (${OLD_FORMAT})` : `unknown field "${field}"`));
288
342
  const ref = reference(raw, "from", account, err);
289
343
  const publish = [];
290
344
  const firstAt = new Map;
@@ -293,7 +347,7 @@ function validateEnv(given, account, fail) {
293
347
  err(`field "publish" must be a list`);
294
348
  else {
295
349
  raw.publish.forEach((t, i) => {
296
- const target = validateTarget(t, (m) => err(`publish[${i}]: ${m}`));
350
+ const target = resolveTarget(t, targets, (m) => err(`publish[${i}]: ${m}`));
297
351
  if (!target)
298
352
  return;
299
353
  const same = firstAt.get(placeId(target));
@@ -306,10 +360,10 @@ function validateEnv(given, account, fail) {
306
360
  });
307
361
  }
308
362
  }
309
- const issue = raw.issue === undefined || !ref ? undefined : validateIssue(raw.issue, ref, account, (m) => err(`issue: ${m}`));
363
+ const rotate = raw.rotate === undefined || !ref ? undefined : validateRotate(raw.rotate, ref, account, (m) => err(`rotate: ${m}`));
310
364
  if (failed || !ref)
311
365
  return;
312
- return { ref, publish, ...issue ? { issue } : {} };
366
+ return { ref, publish, ...rotate ? { rotate } : {} };
313
367
  }
314
368
  function reference(table, field, account, fail) {
315
369
  const text = requiredString(table, field, fail);
@@ -327,7 +381,7 @@ function reference(table, field, account, fail) {
327
381
  return { kind: "1password", account, ...parsed };
328
382
  }
329
383
  var ACCOUNT_ID = /^[0-9a-f]{32}$/;
330
- function validateIssue(raw, own, vaultAccount, fail) {
384
+ function validateRotate(raw, own, vaultAccount, fail) {
331
385
  if (!isRecord(raw)) {
332
386
  fail("must be a table");
333
387
  return;
@@ -689,7 +743,7 @@ function isRotation(r) {
689
743
  return isRecord(r) && [r.id, r.keyId, r.environment, r.provider, r.account, r.startedAt, r.updatedAt].every(isText2) && isTextOrNull2(r.mapFile) && ROTATION_STAGES.some((stage) => stage === r.stage) && storedRefWithPasswordDefault(r.item) !== null && isTokenRecord(r.old) && isTokenRecord(r.new);
690
744
  }
691
745
  // ../../packages/core/src/status.ts
692
- var NOTICES = ["due-soon", "authority-unknown", "rotation-unfinished"];
746
+ var NOTICES = ["due-soon", "never-published", "rotation-unfinished"];
693
747
  function buildStatus(input) {
694
748
  if (!isDay(input.today))
695
749
  throw new Error(`today must be YYYY-MM-DD, got "${input.today}"`);
@@ -736,24 +790,28 @@ function buildStatus(input) {
736
790
  const rotateEvery = key.rotateEvery;
737
791
  const rotated = metadata?.rotated ?? null;
738
792
  const due = rotationDue({ label, metadata, rotated, rotateEvery, today: input.today, add });
739
- add("authority-unknown", `${label}: what this key grants is unknown, because keypin didn't create it`);
740
793
  const current = input.generation?.(key.name, environment);
794
+ const published = input.receipts.some((r) => ownedBy(r, owner));
795
+ const unpublished = [];
796
+ const publish = `keypin publish ${key.name}${envFlag(environment, publishEnvironment(result.map))}`;
741
797
  const destinations = entry.publish.map((target) => {
742
798
  const id = destinationId(target, key.name);
743
799
  const last = latest.get(secretKey(target, key.name));
744
800
  const ours = last && (ownedBy(last, owner) || last.source !== null && sameRef(last.source, entry.ref));
745
801
  const receipt = ours ? last : undefined;
746
802
  if (!receipt) {
747
- add("missing-receipt", `${label}: no receipt for ${id}, so what it holds is unknown`, {
748
- destinationId: id
749
- });
803
+ if (last) {
804
+ add("missing-receipt", `${label}: ${id}'s last receipt isn't from this project or this key's item, so what it holds is unknown`, { destinationId: id });
805
+ } else if (published) {
806
+ add("missing-receipt", `${label}: no receipt for ${id}, though this project published this key before, so what it holds is unknown; ${publish} writes it again`, { destinationId: id });
807
+ } else {
808
+ unpublished.push(id);
809
+ }
750
810
  return { destinationId: id, account: target.account, receipt: null, stale: null };
751
811
  }
752
812
  const stale = current === undefined ? null : receipt.generation !== current;
753
813
  if (stale) {
754
- add("stale-receipt", `${label}: ${id} last got generation ${receipt.generation}, and the value is now ${current}`, {
755
- destinationId: id
756
- });
814
+ add("stale-receipt", `${label}: ${id} last got generation ${receipt.generation}, and the value is now ${current}`, { destinationId: id });
757
815
  }
758
816
  const { generation, at } = receipt;
759
817
  return {
@@ -763,6 +821,9 @@ function buildStatus(input) {
763
821
  stale
764
822
  };
765
823
  });
824
+ if (unpublished.length) {
825
+ add("never-published", `${label}: not published yet to ${unpublished.join(", ")}; run ${publish}`);
826
+ }
766
827
  const rotation = unfinishedRotation({ version: ROTATIONS_VERSION, rotations: [...input.rotations ?? []] }, owner, entry.ref);
767
828
  if (rotation) {
768
829
  add("rotation-unfinished", `${label}: a rotation started ${rotation.startedAt} is at stage "${rotation.stage}"; run ${rotateCommand(key.name, environment, publishEnvironment(result.map))} to continue`);
@@ -773,7 +834,6 @@ function buildStatus(input) {
773
834
  rotated,
774
835
  rotateEvery,
775
836
  due,
776
- authority: "unknown",
777
837
  destinations
778
838
  });
779
839
  }
@@ -847,13 +907,26 @@ var WRANGLER_FILES = [
847
907
  ];
848
908
  var DOTENV_LINE = /^\s*(?:export\s+)?([\w.-]+)(?:\s*=\s*?|:\s+?)(\s*'(?:\\'|[^'])*'|\s*"(?:\\"|[^"])*"|\s*`(?:\\`|[^`])*`|[^#\r\n]+)?\s*(?:#.*)?$/gm;
849
909
  function envNames(text) {
850
- const names = new Set;
910
+ return [...envValues(text).keys()];
911
+ }
912
+ function envValues(text) {
913
+ const values = new Map;
851
914
  for (const match of text.replace(/\r\n?/g, `
852
915
  `).matchAll(DOTENV_LINE)) {
853
916
  if (match[1])
854
- names.add(match[1]);
917
+ values.set(match[1], new Secret(dotenvValue(match[2] ?? "")));
855
918
  }
856
- return [...names];
919
+ return values;
920
+ }
921
+ function dotenvValue(raw) {
922
+ const value = raw.trim();
923
+ const unquoted = value.replace(/^(['"`])([\s\S]*)\1$/, "$2");
924
+ return value.startsWith('"') ? unquoted.replace(/\\n/g, `
925
+ `).replace(/\\r/g, "\r") : unquoted;
926
+ }
927
+ function draftItemName(project, environment, name) {
928
+ const folder = project.replace(/[^A-Za-z0-9 ._-]/g, "-");
929
+ return `${folder} ${environment} ${name.toLowerCase().replaceAll("_", " ").trim()}`;
857
930
  }
858
931
  var CONTEXT_REF = /(?<![\w.])(secrets|vars)\s*(?:\.\s*([A-Za-z_][A-Za-z0-9_]*)|\[\s*'([A-Za-z_][A-Za-z0-9_]*)'\s*\])/g;
859
932
  function workflowRefs(text) {
@@ -1132,10 +1205,9 @@ function draftMap(names, options) {
1132
1205
  const runName = runEnvironment(options);
1133
1206
  const publishName = publishEnvironment(options);
1134
1207
  const vault = options.map?.vault ?? "<your vault>";
1135
- const project = options.project.replace(/[^A-Za-z0-9 ._-]/g, "-");
1136
- const from = (environment, what) => str(formatReference({
1208
+ const from = (environment, name) => str(formatReference({
1137
1209
  vault,
1138
- item: `${project} ${environment} ${what}`,
1210
+ item: draftItemName(options.project, environment, name),
1139
1211
  section: null,
1140
1212
  field: "password"
1141
1213
  }));
@@ -1168,16 +1240,15 @@ function draftMap(names, options) {
1168
1240
  });
1169
1241
  if (!local && !publish.length)
1170
1242
  continue;
1171
- if (!/^[A-Za-z_][A-Za-z0-9_]*$/.test(row.name))
1243
+ if (!isVariableName(row.name))
1172
1244
  continue;
1173
1245
  const name = row.name;
1174
- const what = name.toLowerCase().replaceAll("_", " ").trim();
1175
1246
  lines.push("", `[keys.${name}]`, `rotate_every = "90d"`);
1176
1247
  if (local && !(publish.length && runName === publishName)) {
1177
- lines.push(`${runName} = ${from(runName, what)}`);
1248
+ lines.push(`${runName} = ${from(runName, name)}`);
1178
1249
  }
1179
1250
  if (publish.length) {
1180
- lines.push("", `[keys.${name}.${publishName}]`, `from = ${from(publishName, what)}`);
1251
+ lines.push("", `[keys.${name}.${publishName}]`, `from = ${from(publishName, name)}`);
1181
1252
  for (const target of publish) {
1182
1253
  lines.push("", `[[keys.${name}.${publishName}.publish]]`, ...target);
1183
1254
  }
@@ -1187,8 +1258,8 @@ function draftMap(names, options) {
1187
1258
  return null;
1188
1259
  const account = options.map?.account ?? "<your 1Password account>";
1189
1260
  const header = [
1190
- "# Draft from keypin discover; nothing was written. The op:// references are placeholders.",
1191
- !options.map?.account ? "# Fill in your account and vault, then save this as keypin.toml." : options.map.vault === null ? "# Fill in the vault, then copy only the [keys.*] tables into this project's keypin.toml." : "# The vault is the one this project's keypin.toml uses; copy only the [keys.*] tables.",
1261
+ options.written ? "# Written by keypin discover. Each op:// reference is a placeholder: replace it with the field's secret reference." : "# Draft from keypin discover; nothing was written. The op:// references are placeholders.",
1262
+ options.written ? `# ${options.map?.account ? "Fill in the vault" : "Fill in your 1Password account and vault"}, then check each destination.` : !options.map?.account ? "# Fill in your account and vault, then save this as keypin.toml." : options.map.vault === null ? "# Fill in the vault, then copy only the [keys.*] tables into this project's keypin.toml." : "# The vault is the one this project's keypin.toml uses; copy only the [keys.*] tables.",
1192
1263
  ...options.environments.join() === DEFAULT_ENVIRONMENTS.join() ? [] : ["", `environments = [${options.environments.map(str).join(", ")}]`],
1193
1264
  "",
1194
1265
  "[1password]",
@@ -1943,6 +2014,9 @@ function onePasswordBackend(connect = desktopConnect) {
1943
2014
  const sdk = await client(ref.account);
1944
2015
  const id = await vaultId(sdk, ref);
1945
2016
  const found = await find(sdk, id, ref);
2017
+ if (found && options.newItem) {
2018
+ throw new Error(`${where(ref)}: the item already exists; keypin doesn't overwrite it`);
2019
+ }
1946
2020
  if (!found) {
1947
2021
  if (ref.field !== VALUE_FIELD || ref.section !== null) {
1948
2022
  throw new Error(`${where(ref)}: no such item; create it in 1Password first`);
@@ -2049,8 +2123,8 @@ function messageOf2(error) {
2049
2123
  return error instanceof Error ? error.message : String(error);
2050
2124
  }
2051
2125
  // src/commands/discover.ts
2052
- import { basename, join as join3, relative, resolve } from "path";
2053
- import { parseArgs } from "util";
2126
+ import { basename, join as join5, relative, resolve as resolve2 } from "path";
2127
+ import { parseArgs as parseArgs2 } from "util";
2054
2128
 
2055
2129
  // src/deps.ts
2056
2130
  class UsageError extends Error {
@@ -2086,18 +2160,101 @@ function pickEnvironment(map, requested, fallback) {
2086
2160
  throw new UsageError(`--env is one of ${names} in ${map.file}, got "${requested}"`);
2087
2161
  }
2088
2162
 
2163
+ // src/commands/init.ts
2164
+ import { join as join4, resolve } from "path";
2165
+ import { parseArgs } from "util";
2166
+
2167
+ // src/paths.ts
2168
+ import { dirname as dirname2, join as join3 } from "path";
2169
+ function registryPath(deps) {
2170
+ return join3(base(deps.env.XDG_CONFIG_HOME, deps.home, ".config"), "keypin", "registry.json");
2171
+ }
2172
+ function statePath(deps) {
2173
+ return join3(base(deps.env.XDG_STATE_HOME, deps.home, ".local/state"), "keypin", "state.json");
2174
+ }
2175
+ function base(xdg, home, fallback) {
2176
+ return xdg?.startsWith("/") ? xdg : join3(home, fallback);
2177
+ }
2178
+ function projectCount(registry, path) {
2179
+ const count = registry.projects.length;
2180
+ return `${count} ${count === 1 ? "project" : "projects"} in ${path}`;
2181
+ }
2182
+ function rotationsPath(deps) {
2183
+ return join3(dirname2(statePath(deps)), "rotations.json");
2184
+ }
2185
+
2186
+ // src/commands/init.ts
2187
+ var TEMPLATE = `# keypin.toml: this project's keys. It holds names and places, never values.
2188
+ # Uncomment and edit. Every field is checked; an unknown one is an error.
2189
+ #
2190
+ # The 1Password account the keys below read from: its sign-in address or account ID.
2191
+ # [1password]
2192
+ # account = "my.1password.com"
2193
+ #
2194
+ # A key, named after the variable your program reads.
2195
+ # [keys.EXAMPLE_API_KEY]
2196
+ # rotate_every = "180d" # <n>d, <n>w, <n>m or never
2197
+ # dev = "op://Private/my-project dev example api key/password" # what keypin run reads
2198
+ #
2199
+ # What keypin publish reads, and every place it writes it.
2200
+ # [keys.EXAMPLE_API_KEY.prod]
2201
+ # from = "op://Private/my-project prod example api key/password"
2202
+ #
2203
+ # [[keys.EXAMPLE_API_KEY.prod.publish]]
2204
+ # to = "cloudflare-worker"
2205
+ # account = "<cloudflare account ID>"
2206
+ # worker = "my-worker"
2207
+ #
2208
+ # [[keys.EXAMPLE_API_KEY.prod.publish]]
2209
+ # to = "github-actions"
2210
+ # repo = "owner/repo"
2211
+ `;
2212
+ async function init(args, deps) {
2213
+ const { positionals } = parseArgs({ args, options: {}, allowPositionals: true });
2214
+ if (positionals.length > 1)
2215
+ throw new UsageError("keypin init takes at most one folder");
2216
+ const folder = resolve(deps.cwd, positionals[0] ?? ".");
2217
+ if (!await deps.isFolder(folder))
2218
+ throw new Error(`${folder} isn't a folder`);
2219
+ const file = join4(folder, MAP_FILE);
2220
+ const created = await deps.createFile(file, TEMPLATE);
2221
+ if (created === "link")
2222
+ throw new Error(`${file} is a symbolic link; keypin init doesn't follow one`);
2223
+ deps.stderr.write(created === "created" ? `keypin: wrote ${file}
2224
+ ` : `keypin: ${file} exists; left it as it is
2225
+ `);
2226
+ await registerProject(deps, folder);
2227
+ return 0;
2228
+ }
2229
+ async function registerProject(deps, folder) {
2230
+ const path = registryPath(deps);
2231
+ const registry = await register(deps.files, path, folder);
2232
+ deps.stderr.write(`keypin: ${folder} is registered (${projectCount(registry, path)})
2233
+ `);
2234
+ }
2235
+
2089
2236
  // src/commands/discover.ts
2090
2237
  async function discover(args, deps) {
2091
- const { values, positionals } = parseArgs({
2238
+ const { values, positionals } = parseArgs2({
2092
2239
  args,
2093
- options: { json: { type: "boolean", default: false } },
2240
+ options: {
2241
+ json: { type: "boolean", default: false },
2242
+ write: { type: "boolean", default: false }
2243
+ },
2094
2244
  allowPositionals: true
2095
2245
  });
2096
2246
  if (positionals.length > 1)
2097
2247
  throw new UsageError("keypin discover takes at most one folder");
2098
- const folder = resolve(deps.cwd, positionals[0] ?? ".");
2248
+ const folder = resolve2(deps.cwd, positionals[0] ?? ".");
2099
2249
  if (!await deps.isFolder(folder))
2100
2250
  throw new Error(`${folder} isn't a folder`);
2251
+ const mapFile = join5(folder, MAP_FILE);
2252
+ const mapText = await deps.files.read(mapFile);
2253
+ if (values.write && mapText !== null && mapText !== TEMPLATE) {
2254
+ deps.stderr.write(`keypin discover: ${mapFile} already has a map, and --write doesn't change one. Run keypin discover without --write and copy the [keys.*] tables from its draft by hand.
2255
+ `);
2256
+ return 1;
2257
+ }
2101
2258
  const unknowns = [];
2102
2259
  const scan = await walk(deps, folder);
2103
2260
  const tracked = await trackedFiles(deps, folder, scan.envFiles);
@@ -2106,7 +2263,7 @@ async function discover(args, deps) {
2106
2263
  const kind = envFileKind(basename(path));
2107
2264
  if (!kind)
2108
2265
  continue;
2109
- const text = await deps.files.read(join3(folder, path));
2266
+ const text = await deps.files.read(join5(folder, path));
2110
2267
  if (text === null)
2111
2268
  continue;
2112
2269
  envFiles.push({ path, kind, names: envNames(text), tracked: tracked?.has(path) ?? null });
@@ -2122,7 +2279,7 @@ async function discover(args, deps) {
2122
2279
  const workflows = await readWorkflows(deps, folder, top, unknowns);
2123
2280
  const workers = [];
2124
2281
  for (const config of scan.wrangler) {
2125
- const text = await deps.files.read(join3(folder, config));
2282
+ const text = await deps.files.read(join5(folder, config));
2126
2283
  if (text === null)
2127
2284
  continue;
2128
2285
  const parsed = wranglerWorkers(text, config);
@@ -2165,7 +2322,6 @@ async function discover(args, deps) {
2165
2322
  read: read.ok ? read : { ok: false, reason: `as ${login}: ${read.reason}` }
2166
2323
  };
2167
2324
  }
2168
- const mapText = await deps.files.read(join3(folder, MAP_FILE));
2169
2325
  let map = null;
2170
2326
  if (mapText !== null) {
2171
2327
  const parsed = parseMap(mapText, MAP_FILE);
@@ -2183,13 +2339,20 @@ async function discover(args, deps) {
2183
2339
  project: basename(folder),
2184
2340
  githubAccount: login,
2185
2341
  map: map ? { account: map.account, vault: firstVault(map) } : null,
2186
- environments: map?.environments ?? DEFAULT_ENVIRONMENTS
2342
+ environments: map?.environments ?? DEFAULT_ENVIRONMENTS,
2343
+ written: values.write
2187
2344
  });
2188
2345
  if (draft !== null) {
2189
2346
  const check = parseMap(draft, "draft");
2190
2347
  if (!check.ok)
2191
2348
  throw new Error(`the draft doesn't parse: ${check.errors.join("; ")}`);
2192
2349
  }
2350
+ if (values.write && draft === null) {
2351
+ deps.stderr.write(`keypin discover: found no unmapped name set locally or in the cloud, so there's no draft; nothing was written. Run keypin init for a template.
2352
+ `);
2353
+ return 1;
2354
+ }
2355
+ const written = values.write && draft !== null ? await writeDraft(deps, mapFile, mapText, draft) : null;
2193
2356
  const report = {
2194
2357
  folder,
2195
2358
  repository: github?.repo ?? null,
@@ -2201,12 +2364,26 @@ async function discover(args, deps) {
2201
2364
  },
2202
2365
  nestedRepositories: scan.nested,
2203
2366
  ...discovery,
2204
- draft
2367
+ draft,
2368
+ written
2205
2369
  };
2206
2370
  deps.stdout.write(values.json ? `${JSON.stringify(report, null, 2)}
2207
2371
  ` : render(report, discovery));
2372
+ if (written !== null) {
2373
+ deps.stderr.write(`keypin: wrote the draft to ${written}. Its op:// references are placeholders: replace each with the field's secret reference from 1Password before you run or publish.
2374
+ `);
2375
+ await registerProject(deps, folder);
2376
+ }
2208
2377
  return 0;
2209
2378
  }
2379
+ async function writeDraft(deps, file, found, draft) {
2380
+ const result = found === null ? await deps.createFile(file, draft) : await deps.replaceFile(file, TEMPLATE, draft);
2381
+ if (result === "link")
2382
+ throw new Error(`${file} is a symbolic link; keypin discover doesn't follow one`);
2383
+ if (result === "exists" || result === "changed")
2384
+ throw new Error(`${file} changed while discover ran; nothing was written. Run it again.`);
2385
+ return file;
2386
+ }
2210
2387
  async function walk(deps, root) {
2211
2388
  const scan = { envFiles: [], wrangler: [], nested: [] };
2212
2389
  const visit = async (folder) => {
@@ -2216,7 +2393,7 @@ async function walk(deps, root) {
2216
2393
  return;
2217
2394
  }
2218
2395
  for (const entry of entries) {
2219
- const path = join3(folder, entry.name);
2396
+ const path = join5(folder, entry.name);
2220
2397
  if (entry.kind === "folder") {
2221
2398
  if (!SKIPPED_FOLDERS.has(entry.name))
2222
2399
  await visit(path);
@@ -2240,7 +2417,7 @@ async function trackedFiles(deps, folder, paths) {
2240
2417
  return new Set(out.split("\x00").filter(Boolean));
2241
2418
  }
2242
2419
  async function readWorkflows(deps, folder, top, unknowns) {
2243
- const dir = join3(top ?? folder, ".github", "workflows");
2420
+ const dir = join5(top ?? folder, ".github", "workflows");
2244
2421
  let entries;
2245
2422
  try {
2246
2423
  entries = await deps.listFolder(dir);
@@ -2258,10 +2435,10 @@ async function readWorkflows(deps, folder, top, unknowns) {
2258
2435
  for (const entry of entries.toSorted((a, b) => a.name.localeCompare(b.name))) {
2259
2436
  if (entry.kind !== "file" || !/\.ya?ml$/.test(entry.name))
2260
2437
  continue;
2261
- const text = await deps.files.read(join3(dir, entry.name));
2438
+ const text = await deps.files.read(join5(dir, entry.name));
2262
2439
  if (text === null)
2263
2440
  continue;
2264
- const path = relative(folder, join3(dir, entry.name));
2441
+ const path = relative(folder, join5(dir, entry.name));
2265
2442
  const refs = workflowRefs(text);
2266
2443
  if (refs)
2267
2444
  workflows.push({ path, refs });
@@ -2380,6 +2557,8 @@ function render(report, discovery) {
2380
2557
  lines.push("");
2381
2558
  if (report.draft === null)
2382
2559
  lines.push("No draft: every name set locally or in the cloud is already mapped.");
2560
+ else if (report.written !== null)
2561
+ lines.push(`Draft keypin.toml for the unmapped names, written to ${report.written}:`, "", report.draft);
2383
2562
  else
2384
2563
  lines.push("Draft keypin.toml for the unmapped names (not written):", "", report.draft);
2385
2564
  return `${lines.join(`
@@ -2390,77 +2569,8 @@ function count(n, noun) {
2390
2569
  return `${n} ${noun}${n === 1 ? "" : "s"}`;
2391
2570
  }
2392
2571
 
2393
- // src/commands/init.ts
2394
- import { join as join5, resolve as resolve2 } from "path";
2395
- import { parseArgs as parseArgs2 } from "util";
2396
-
2397
- // src/paths.ts
2398
- import { dirname as dirname2, join as join4 } from "path";
2399
- function registryPath(deps) {
2400
- return join4(base(deps.env.XDG_CONFIG_HOME, deps.home, ".config"), "keypin", "registry.json");
2401
- }
2402
- function statePath(deps) {
2403
- return join4(base(deps.env.XDG_STATE_HOME, deps.home, ".local/state"), "keypin", "state.json");
2404
- }
2405
- function base(xdg, home, fallback) {
2406
- return xdg?.startsWith("/") ? xdg : join4(home, fallback);
2407
- }
2408
- function projectCount(registry, path) {
2409
- const count = registry.projects.length;
2410
- return `${count} ${count === 1 ? "project" : "projects"} in ${path}`;
2411
- }
2412
- function rotationsPath(deps) {
2413
- return join4(dirname2(statePath(deps)), "rotations.json");
2414
- }
2415
-
2416
- // src/commands/init.ts
2417
- var TEMPLATE = `# keypin.toml: this project's keys. It holds names and places, never values.
2418
- # Uncomment and edit. Every field is checked; an unknown one is an error.
2419
- #
2420
- # The 1Password account the keys below read from: its sign-in address or account ID.
2421
- # [1password]
2422
- # account = "my.1password.com"
2423
- #
2424
- # A key, named after the variable your program reads.
2425
- # [keys.EXAMPLE_API_KEY]
2426
- # rotate_every = "180d" # <n>d, <n>w, <n>m or never
2427
- # dev = "op://Private/my-project dev example api key/password" # what keypin run reads
2428
- #
2429
- # What keypin publish reads, and every place it writes it.
2430
- # [keys.EXAMPLE_API_KEY.prod]
2431
- # from = "op://Private/my-project prod example api key/password"
2432
- #
2433
- # [[keys.EXAMPLE_API_KEY.prod.publish]]
2434
- # to = "cloudflare-worker"
2435
- # account = "<cloudflare account ID>"
2436
- # worker = "my-worker"
2437
- #
2438
- # [[keys.EXAMPLE_API_KEY.prod.publish]]
2439
- # to = "github-actions"
2440
- # repo = "owner/repo"
2441
- `;
2442
- async function init(args, deps) {
2443
- const { positionals } = parseArgs2({ args, options: {}, allowPositionals: true });
2444
- if (positionals.length > 1)
2445
- throw new UsageError("keypin init takes at most one folder");
2446
- const folder = resolve2(deps.cwd, positionals[0] ?? ".");
2447
- if (!await deps.isFolder(folder))
2448
- throw new Error(`${folder} isn't a folder`);
2449
- const file = join5(folder, MAP_FILE);
2450
- const created = await deps.createFile(file, TEMPLATE);
2451
- if (created === "link")
2452
- throw new Error(`${file} is a symbolic link; keypin init doesn't follow one`);
2453
- deps.stderr.write(created === "created" ? `keypin: wrote ${file}
2454
- ` : `keypin: ${file} exists; left it as it is
2455
- `);
2456
- const path = registryPath(deps);
2457
- const registry = await register(deps.files, path, folder);
2458
- deps.stderr.write(`keypin: ${folder} is registered (${projectCount(registry, path)})
2459
- `);
2460
- return 0;
2461
- }
2462
-
2463
- // src/commands/publish.ts
2572
+ // src/commands/import.ts
2573
+ import { basename as basename2, join as join6, resolve as resolve3 } from "path";
2464
2574
  import { parseArgs as parseArgs3 } from "util";
2465
2575
 
2466
2576
  // src/publishing.ts
@@ -2489,9 +2599,9 @@ function lockedMessage(file, holder) {
2489
2599
  }
2490
2600
  return `keypin: another publish or rotate (process ${holder.pid}) holds ${file} and is still running. Wait for it to finish.`;
2491
2601
  }
2492
- async function checkAccounts(key, entry, deps, say) {
2602
+ async function checkAccounts(key, entry, deps, say, heading = "Account checks") {
2493
2603
  say(`
2494
- Account checks:`);
2604
+ ${heading}:`);
2495
2605
  const checked = new Map;
2496
2606
  let failed = false;
2497
2607
  for (const target of entry.publish) {
@@ -2567,10 +2677,22 @@ function finishAll(state, owner) {
2567
2677
  return unfinishedOperations(state, owner).reduce((s, op) => finishOperation(s, op.id), state);
2568
2678
  }
2569
2679
  function rerun(deps, command, args) {
2570
- const words = [...deps.self, command, ...args].map(shellWord).join(" ");
2680
+ const end = args.includes("--") ? args.indexOf("--") : args.length;
2681
+ const approve = [...args.slice(0, end).filter((arg) => arg !== "--plan"), ...args.slice(end)];
2682
+ const words = [...deps.self, command, ...approve].map(shellWord).join(" ");
2571
2683
  return `To approve, run this in a terminal:
2572
2684
  cd ${shellWord(deps.cwd)} && ${words}`;
2573
2685
  }
2686
+ function planOnly(deps, command, args, needsApproval) {
2687
+ const say = (text) => deps.stderr.write(`${text}
2688
+ `);
2689
+ say(needsApproval ? `
2690
+ This plan needs yes typed in a terminal; --yes can't approve it.` : `
2691
+ This plan is routine: --yes can approve it.`);
2692
+ say("Nothing was written: --plan only shows the plan.");
2693
+ say(rerun(deps, command, args));
2694
+ return NEEDS_APPROVAL;
2695
+ }
2574
2696
  function shellWord(word) {
2575
2697
  return /^[\w@%+=:,./-]+$/.test(word) ? word : `'${word.replaceAll("'", `'\\''`)}'`;
2576
2698
  }
@@ -2581,6 +2703,272 @@ function describeTarget(target, secretName) {
2581
2703
  return `${destinationId(target, secretName)} (account ${target.account})`;
2582
2704
  }
2583
2705
 
2706
+ // src/commands/import.ts
2707
+ var ROTATE_EVERY = "90d";
2708
+ async function importEnv(args, deps) {
2709
+ const request = parseRequest(args);
2710
+ const say = (text) => deps.stderr.write(`${text}
2711
+ `);
2712
+ const envPath = resolve3(deps.cwd, request.envFile);
2713
+ if (envFileKind(basename2(envPath)) === "expected")
2714
+ throw new UsageError(`${envPath} is an example file; import the file that sets the values`);
2715
+ const text = await deps.files.read(envPath);
2716
+ if (text === null)
2717
+ throw new Error(`${envPath} doesn't exist`);
2718
+ const target = await readTarget(deps, request);
2719
+ const environment = runEnvironment(target.map ?? { environments: DEFAULT_ENVIRONMENTS });
2720
+ const project = basename2(deps.cwd);
2721
+ const values = envValues(text);
2722
+ if (!values.size)
2723
+ throw new Error(`${envPath} sets no variables; nothing to import`);
2724
+ const planned = [];
2725
+ const skips = [];
2726
+ const items = new Map;
2727
+ for (const [name, value] of values) {
2728
+ const item = draftItemName(project, environment, name);
2729
+ const sameItem = items.get(item.toLowerCase());
2730
+ const mapped = target.map?.keys.find((key) => key.name === name);
2731
+ if (!isVariableName(name))
2732
+ skips.push({
2733
+ name,
2734
+ reason: "not a variable name (letters, digits and _, not a digit first)",
2735
+ onlyInFile: true
2736
+ });
2737
+ else if (value.isEmpty())
2738
+ skips.push({ name, reason: "the value is empty", onlyInFile: false });
2739
+ else if (mapped?.environments[environment])
2740
+ skips.push({
2741
+ name,
2742
+ reason: `already mapped in ${target.file}; left as it is`,
2743
+ onlyInFile: false
2744
+ });
2745
+ else if (mapped)
2746
+ skips.push({
2747
+ name,
2748
+ reason: `mapped in ${target.file} without ${environment}; left as it is, so add its ${environment} reference by hand`,
2749
+ onlyInFile: true
2750
+ });
2751
+ else if (value.includes("$"))
2752
+ skips.push({
2753
+ name,
2754
+ reason: "the value has a $, which some loaders expand to another variable's value",
2755
+ onlyInFile: true
2756
+ });
2757
+ else if (sameItem !== undefined)
2758
+ skips.push({
2759
+ name,
2760
+ reason: `its item would be "${item}", the same as ${sameItem}'s`,
2761
+ onlyInFile: true
2762
+ });
2763
+ else {
2764
+ items.set(item.toLowerCase(), name);
2765
+ planned.push({ name, value, ref: refFor(target.account, request.vault, item) });
2766
+ }
2767
+ }
2768
+ let imports = planned;
2769
+ const stored = [];
2770
+ if (planned.length) {
2771
+ if (await deps.isLink(target.file))
2772
+ throw new Error(`${target.file} is a symbolic link; keypin import doesn't follow one`);
2773
+ const check = parseMap(mapText(target, planned, environment), target.file);
2774
+ if (!check.ok)
2775
+ throw new Error(`adding the keys to ${target.file} wouldn't parse, so nothing was created: ${check.errors.join("; ")}`);
2776
+ const scope = { kind: "1password", account: target.account, vault: request.vault };
2777
+ say(`keypin: listing the items in vault ${request.vault} of 1Password account ${target.account}`);
2778
+ const titles = new Set((await deps.vault().list(scope)).map((i) => i.title.toLowerCase()));
2779
+ imports = [];
2780
+ for (const entry of planned) {
2781
+ if (!titles.has(entry.ref.item.toLowerCase())) {
2782
+ imports.push(entry);
2783
+ continue;
2784
+ }
2785
+ const problem = await existingProblem(deps, entry);
2786
+ if (problem === null)
2787
+ stored.push(entry);
2788
+ else
2789
+ skips.push({
2790
+ name: entry.name,
2791
+ reason: `"${entry.ref.item}" already exists in vault ${request.vault} ${problem}; keypin doesn't overwrite it`,
2792
+ onlyInFile: true,
2793
+ exists: true
2794
+ });
2795
+ }
2796
+ }
2797
+ const today = localDay(deps.now());
2798
+ say(`Import ${envPath} into 1Password account ${target.account}, vault ${request.vault}:`);
2799
+ for (const { name, ref } of imports)
2800
+ say(` create "${ref.item}" (password, rotated ${today}), and map ${name} to ${formatReference(ref)}`);
2801
+ if (!imports.length)
2802
+ say(" nothing to create");
2803
+ for (const { name, ref } of stored)
2804
+ say(` map ${name} to ${formatReference(ref)}, an item that exists with the same value`);
2805
+ if (skips.length) {
2806
+ say("Skipped:");
2807
+ for (const skip of skips)
2808
+ say(` ${skip.name}: ${skip.reason}`);
2809
+ }
2810
+ const existed = skips.some((skip) => skip.exists);
2811
+ if (!imports.length && !stored.length)
2812
+ return existed ? 1 : 0;
2813
+ say(target.map ? `Map: add ${count2(imports.length + stored.length, "key")} at the end of ${target.file}` : `Map: write ${target.file} and register ${deps.cwd}`);
2814
+ if (imports.length && !await confirmed(deps, request, imports.length))
2815
+ return NEEDS_APPROVAL;
2816
+ const created = [];
2817
+ for (const entry of imports) {
2818
+ try {
2819
+ await deps.vault().write(entry.ref, entry.value, { rotated: today, newItem: true });
2820
+ created.push(entry);
2821
+ say(` created "${entry.ref.item}"`);
2822
+ } catch (error) {
2823
+ say(` FAILED "${entry.ref.item}": ${messageOf(error)}`);
2824
+ break;
2825
+ }
2826
+ }
2827
+ const failed = imports.filter((entry) => !created.includes(entry)).map((i) => i.name);
2828
+ if (failed.length)
2829
+ say(`Stopped. Created ${names(created.map((i) => i.name))}; not created: ${names(failed)}.`);
2830
+ const mapped = planned.filter((entry) => stored.includes(entry) || created.includes(entry));
2831
+ if (!mapped.length)
2832
+ return 1;
2833
+ try {
2834
+ await writeMap(deps, target, mapped, environment);
2835
+ } catch (error) {
2836
+ say(`keypin: ${messageOf(error)}`);
2837
+ say(target.map ? `The items are in 1Password. Add this at the end of ${target.file} by hand:` : `The items are in 1Password. Save this as ${target.file} by hand:`);
2838
+ deps.stdout.write(mapAddition(target, mapped, environment));
2839
+ return 1;
2840
+ }
2841
+ say(`keypin: mapped ${names(mapped.map((i) => i.name))} in ${target.file}`);
2842
+ if (!target.map)
2843
+ await registerProject(deps, deps.cwd);
2844
+ const left = [...skips.filter((skip) => skip.onlyInFile).map((skip) => skip.name), ...failed];
2845
+ say(`The imported values are now in 1Password, and keypin run -- <command> provides them. keypin left ${envPath} as it is.`);
2846
+ say(left.length ? `${names(left)} ${left.length === 1 ? "is" : "are"} still only in that file. Move ${left.length === 1 ? "it" : "them"}, then delete the file and stop loading it (for example, wrangler dev reads .dev.vars).` : "Delete it and stop loading it (for example, wrangler dev reads .dev.vars).");
2847
+ if (failed.length)
2848
+ say("Run keypin import again to create the rest. It maps an item it already stored, since that item holds the same value.");
2849
+ return failed.length || existed ? 1 : 0;
2850
+ }
2851
+ async function existingProblem(deps, entry) {
2852
+ try {
2853
+ const value = await deps.vault().read(entry.ref);
2854
+ return value.equals(entry.value) ? null : "with another value";
2855
+ } catch (error) {
2856
+ return `and couldn't be read (${messageOf(error)})`;
2857
+ }
2858
+ }
2859
+ function names(list) {
2860
+ return list.length ? list.join(", ") : "none";
2861
+ }
2862
+ function parseRequest(args) {
2863
+ const { values, positionals } = parseArgs3({
2864
+ args,
2865
+ options: {
2866
+ vault: { type: "string" },
2867
+ account: { type: "string" },
2868
+ yes: { type: "boolean", default: false }
2869
+ },
2870
+ allowPositionals: true
2871
+ });
2872
+ const [envFile, ...extra] = positionals;
2873
+ if (envFile === undefined || extra.length)
2874
+ throw new UsageError("keypin import takes one env file, such as .env or .dev.vars");
2875
+ const vault = values.vault?.trim();
2876
+ if (!vault)
2877
+ throw new UsageError("--vault is required: the 1Password vault to create the items in");
2878
+ if (vault.includes("/"))
2879
+ throw new UsageError("--vault is a vault name, without /");
2880
+ const account = values.account?.trim();
2881
+ if (values.account !== undefined && !account)
2882
+ throw new UsageError("--account is your 1Password sign-in address, such as my.1password.com");
2883
+ return { envFile, vault, account, yes: values.yes, args };
2884
+ }
2885
+ async function readTarget(deps, request) {
2886
+ const file = join6(deps.cwd, MAP_FILE);
2887
+ const found = await deps.files.read(file);
2888
+ if (found === null || found === TEMPLATE) {
2889
+ if (!request.account)
2890
+ throw new UsageError(`--account is required: ${file} doesn't name a 1Password account yet, so give its sign-in address, such as my.1password.com`);
2891
+ return { file, found, map: null, account: request.account };
2892
+ }
2893
+ const parsed = parseMap(found, file);
2894
+ if (!parsed.ok)
2895
+ throw new Error(`fix ${file} first: ${parsed.errors.join("; ")}`);
2896
+ const account = parsed.map.account ?? request.account;
2897
+ if (!account)
2898
+ throw new UsageError(`--account is required: ${file} has no [1password] account, so give its sign-in address, such as my.1password.com`);
2899
+ if (request.account && request.account !== account)
2900
+ throw new UsageError(`${file} uses 1Password account ${account}, not ${request.account}; drop --account`);
2901
+ return { file, found, map: parsed.map, account };
2902
+ }
2903
+ function refFor(account, vault, item) {
2904
+ return { kind: "1password", account, vault, item, section: null, field: "password" };
2905
+ }
2906
+ function keyTables(imports, environment) {
2907
+ return imports.map(({ name, ref }) => `
2908
+ [keys.${name}]
2909
+ rotate_every = ${str2(ROTATE_EVERY)}
2910
+ ${environment} = ${str2(formatReference(ref))}
2911
+ `).join("");
2912
+ }
2913
+ function mapBase(target) {
2914
+ if (!target.map || target.found === null)
2915
+ return "";
2916
+ return target.found.endsWith(`
2917
+ `) || !target.found ? target.found : `${target.found}
2918
+ `;
2919
+ }
2920
+ function mapAddition(target, imports, environment) {
2921
+ const account = `[1password]
2922
+ account = ${str2(target.account)}
2923
+ `;
2924
+ const tables = keyTables(imports, environment);
2925
+ if (!target.map)
2926
+ return `# Written by keypin import. The values are in 1Password; this file holds only references.
2927
+
2928
+ ${account}${tables}`;
2929
+ return `${target.map.account ? "" : `
2930
+ ${account}`}${tables}`;
2931
+ }
2932
+ function mapText(target, imports, environment) {
2933
+ return `${mapBase(target)}${mapAddition(target, imports, environment)}`;
2934
+ }
2935
+ async function writeMap(deps, target, imports, environment) {
2936
+ const text = mapText(target, imports, environment);
2937
+ const check = parseMap(text, target.file);
2938
+ if (!check.ok)
2939
+ throw new Error(`the new ${target.file} doesn't parse: ${check.errors.join("; ")}`);
2940
+ const result = target.found === null ? await deps.createFile(target.file, text) : await deps.replaceFile(target.file, target.found, text);
2941
+ if (result === "link")
2942
+ throw new Error(`${target.file} is a symbolic link; keypin import doesn't follow one`);
2943
+ if (result === "exists" || result === "changed")
2944
+ throw new Error(`${target.file} changed while import ran, so it wasn't written`);
2945
+ }
2946
+ async function confirmed(deps, request, items) {
2947
+ const say = (text) => deps.stderr.write(`${text}
2948
+ `);
2949
+ if (request.yes)
2950
+ return true;
2951
+ if (deps.isTerminal) {
2952
+ const answer = await deps.ask(`Type yes to create ${count2(items, "item")} in 1Password: `);
2953
+ if (answer.trim() === "yes")
2954
+ return true;
2955
+ say("Not confirmed. Nothing was created.");
2956
+ return false;
2957
+ }
2958
+ say("Stopped: no terminal to confirm on. Nothing was created. Confirm in a terminal, or add --yes.");
2959
+ say(rerun(deps, "import", request.args));
2960
+ return false;
2961
+ }
2962
+ function count2(n, noun) {
2963
+ return `${n} ${noun}${n === 1 ? "" : "s"}`;
2964
+ }
2965
+ function str2(value) {
2966
+ return JSON.stringify(value);
2967
+ }
2968
+
2969
+ // src/commands/publish.ts
2970
+ import { parseArgs as parseArgs4 } from "util";
2971
+
2584
2972
  // src/vault.ts
2585
2973
  function announce(stderr, what, refs) {
2586
2974
  const groups = new Map;
@@ -2604,129 +2992,185 @@ var refKey = (ref) => JSON.stringify([ref.kind, ref.account, ref.vault, ref.item
2604
2992
 
2605
2993
  // src/commands/publish.ts
2606
2994
  async function publish(args, deps) {
2607
- const { keyId, env, yes } = parseRequest(args);
2995
+ const { keyId, env, yes, plan } = parseRequest2(args);
2608
2996
  const map = await findMap(deps.files, deps.cwd);
2609
2997
  const environment = pickEnvironment(map, env, publishEnvironment(map));
2610
- const request = { keyId, environment, yes, args };
2998
+ const request = { keyId, environment, yes, plan, args };
2611
2999
  return locked(deps, (path) => publishLocked(request, map, path, deps));
2612
3000
  }
2613
- function parseRequest(args) {
2614
- const { values, positionals } = parseArgs3({
3001
+ function parseRequest2(args) {
3002
+ const { values, positionals } = parseArgs4({
2615
3003
  args,
2616
3004
  options: {
2617
3005
  env: { type: "string" },
2618
- yes: { type: "boolean", default: false }
3006
+ yes: { type: "boolean", default: false },
3007
+ all: { type: "boolean", default: false },
3008
+ plan: { type: "boolean", default: false }
2619
3009
  },
2620
3010
  allowPositionals: true
2621
3011
  });
2622
3012
  const [keyId, ...extra] = positionals;
3013
+ if (values.all) {
3014
+ if (positionals.length)
3015
+ throw new UsageError("keypin publish takes a variable name or --all, not both");
3016
+ return { keyId: null, env: values.env, yes: values.yes, plan: values.plan };
3017
+ }
2623
3018
  if (keyId === undefined || extra.length)
2624
- throw new UsageError("keypin publish takes one key: the variable name");
2625
- return { keyId, env: values.env, yes: values.yes };
3019
+ throw new UsageError("keypin publish takes one key: the variable name, or --all");
3020
+ return { keyId, env: values.env, yes: values.yes, plan: values.plan };
2626
3021
  }
2627
- async function publishLocked(request, map, path, deps) {
3022
+ function jobsFor(request, map, say) {
2628
3023
  const { keyId, environment } = request;
2629
- const key = map.keys.find((k) => k.name === keyId);
2630
- if (!key)
2631
- throw new Error(`${map.file} has no key "${keyId}"`);
2632
- const entry = key.environments[environment];
2633
- if (!entry)
2634
- throw new Error(`key "${keyId}" has no ${environment} entry in ${map.file}`);
3024
+ const job = (key, entry) => ({
3025
+ key,
3026
+ entry,
3027
+ owner: { keyId: key.name, environment, mapFile: map.file }
3028
+ });
3029
+ if (keyId !== null) {
3030
+ const key = map.keys.find((k) => k.name === keyId);
3031
+ if (!key)
3032
+ throw new Error(`${map.file} has no key "${keyId}"`);
3033
+ const entry = key.environments[environment];
3034
+ if (!entry)
3035
+ throw new Error(`key "${keyId}" has no ${environment} entry in ${map.file}`);
3036
+ return [job(key, entry)];
3037
+ }
3038
+ const jobs = map.keys.flatMap((key) => {
3039
+ const entry = key.environments[environment];
3040
+ return entry?.publish.length ? [job(key, entry)] : [];
3041
+ });
3042
+ const skipped = map.keys.filter((key) => !jobs.some((j) => j.key === key));
3043
+ if (skipped.length)
3044
+ say(`Not published, no ${environment} destinations: ${skipped.map((k) => k.name).join(", ")}`);
3045
+ return jobs;
3046
+ }
3047
+ async function publishLocked(request, map, path, deps) {
3048
+ const { environment } = request;
3049
+ const many = request.keyId === null;
2635
3050
  const say = (text) => deps.stderr.write(`${text}
2636
3051
  `);
2637
- say(`Publishing ${keyId} (${environment}) from ${formatReference(entry.ref)}`);
2638
- say(` 1Password account: ${entry.ref.account}`);
2639
- for (const target of entry.publish)
2640
- say(` to: ${describeTarget(target, key.name)}`);
2641
- if (!entry.publish.length) {
2642
- say(`The map lists no destinations for ${keyId} (${environment}); nothing to publish.`);
3052
+ const jobs = jobsFor(request, map, say);
3053
+ if (many && !jobs.length) {
3054
+ say(`No key in ${map.file} has ${environment} destinations; nothing to publish.`);
2643
3055
  return 1;
2644
3056
  }
2645
- const state = await loadState(deps.files, path);
2646
- const owner = { keyId, environment, mapFile: map.file };
2647
- for (const op of unfinishedOperations(state, owner)) {
2648
- say(`An earlier publish (started ${op.startedAt}, generation ${op.generation}) didn't finish: wrote ${op.done.length} of ${op.planned.length} (${op.done.join(", ") || "none"}). This run plans again from the receipts.`);
3057
+ for (const { key, entry } of jobs) {
3058
+ say(`Publishing ${key.name} (${environment}) from ${formatReference(entry.ref)}`);
3059
+ say(` 1Password account: ${entry.ref.account}`);
3060
+ for (const target of entry.publish)
3061
+ say(` to: ${describeTarget(target, key.name)}`);
3062
+ if (!entry.publish.length) {
3063
+ say(`The map lists no destinations for ${key.name} (${environment}); nothing to publish.`);
3064
+ return 1;
3065
+ }
2649
3066
  }
2650
- const checked = await checkAccounts(key, entry, deps, say);
2651
- if (!checked) {
3067
+ let state = await loadState(deps.files, path);
3068
+ for (const { owner } of jobs) {
3069
+ for (const op of unfinishedOperations(state, owner)) {
3070
+ say(`An earlier publish (started ${op.startedAt}, generation ${op.generation}) didn't finish: wrote ${op.done.length} of ${op.planned.length} (${op.done.join(", ") || "none"}). This run plans again from the receipts.`);
3071
+ }
3072
+ }
3073
+ const checks = [];
3074
+ for (const job of jobs) {
3075
+ const heading = many ? `Account checks for ${job.key.name}` : "Account checks";
3076
+ const checked = await checkAccounts(job.key, job.entry, deps, say, heading);
3077
+ if (checked)
3078
+ checks.push({ ...job, checked });
3079
+ }
3080
+ if (checks.length < jobs.length) {
2652
3081
  say("Stopped: an account check failed. Nothing was read and nothing was written.");
2653
3082
  return 1;
2654
3083
  }
2655
- const approvals = approvalsFor({
2656
- key,
2657
- environment,
2658
- mapFile: map.file,
2659
- receipts: state.receipts
2660
- });
2661
- if (approvals.length && !deps.isTerminal) {
3084
+ const approvals = jobs.flatMap(({ key, owner }) => approvalsFor({ key, environment, mapFile: owner.mapFile, receipts: state.receipts }));
3085
+ if (approvals.length && !deps.isTerminal && !request.plan) {
2662
3086
  say(`
2663
3087
  Needs approval:`);
2664
3088
  for (const approval of approvals)
2665
3089
  say(` ${approval.approval}`);
2666
- say(`Stopped before reading the value: this publish needs approval, and --yes can't give it. Nothing was read and nothing was written.`);
3090
+ say(`Stopped before reading ${many ? "any value" : "the value"}: this publish needs approval, and --yes can't give it. Nothing was read and nothing was written.`);
2667
3091
  say(rerun(deps, "publish", request.args));
2668
3092
  return NEEDS_APPROVAL;
2669
3093
  }
2670
- announce(deps.stderr, "the value to publish", [entry.ref]);
2671
- let secret;
2672
- try {
2673
- secret = await deps.vault().read(entry.ref);
2674
- } catch (error) {
2675
- say(`Stopped: couldn't read ${keyId} (${formatReference(entry.ref)}): ${messageOf(error)}`);
2676
- return 1;
2677
- }
2678
- const generation = await generationOf(secret);
2679
- const plan = planPublish({
2680
- key,
2681
- environment,
2682
- mapFile: map.file,
2683
- receipts: state.receipts,
2684
- generation
2685
- });
2686
- say(`
3094
+ announce(deps.stderr, many ? "the values to publish" : "the value to publish", jobs.map((job) => job.entry.ref));
3095
+ const planned = [];
3096
+ let needsApproval = false;
3097
+ for (const job of checks) {
3098
+ const { key, entry, owner, checked } = job;
3099
+ let secret;
3100
+ try {
3101
+ secret = await deps.vault().read(entry.ref);
3102
+ } catch (error) {
3103
+ say(`Stopped: couldn't read ${key.name} (${formatReference(entry.ref)}): ${messageOf(error)}`);
3104
+ return 1;
3105
+ }
3106
+ const generation = await generationOf(secret);
3107
+ const plan = planPublish({
3108
+ key,
3109
+ environment,
3110
+ mapFile: owner.mapFile,
3111
+ receipts: state.receipts,
3112
+ generation
3113
+ });
3114
+ const writes = writesFor(plan.steps, checked);
3115
+ say(many ? `
3116
+ Plan for ${key.name}, generation ${generation}:` : `
2687
3117
  Plan for generation ${generation}:`);
2688
- const writes = writesFor(plan.steps, checked);
2689
- for (const step of plan.steps) {
2690
- const found = checked.get(step.destinationId);
2691
- say(` ${step.action} ${describeTarget(step.target, key.name)}: ${step.reason}`);
2692
- if (found)
2693
- say(` ${accountFacts(found.check)}`);
2694
- if (step.approval)
2695
- say(` needs approval: ${step.approval}`);
3118
+ for (const step of plan.steps) {
3119
+ const found = checked.get(step.destinationId);
3120
+ say(` ${step.action} ${describeTarget(step.target, key.name)}: ${step.reason}`);
3121
+ if (found)
3122
+ say(` ${accountFacts(found.check)}`);
3123
+ if (step.approval)
3124
+ say(` needs approval: ${step.approval}`);
3125
+ }
3126
+ for (const removed of plan.removed) {
3127
+ say(` not in the map any more: ${removed.destinationId} (last got ${removed.lastReceipt.generation} at ${removed.lastReceipt.at}). keypin doesn't delete it.`);
3128
+ }
3129
+ needsApproval ||= plan.needsApproval;
3130
+ planned.push({ ...job, secret, generation, writes });
2696
3131
  }
2697
- for (const removed of plan.removed) {
2698
- say(` not in the map any more: ${removed.destinationId} (last got ${removed.lastReceipt.generation} at ${removed.lastReceipt.at}). keypin doesn't delete it.`);
3132
+ const finished = planned.filter((job) => !job.writes.length && unfinishedOperations(state, job.owner).length).reduce((s, job) => finishAll(s, job.owner), state);
3133
+ if (finished !== state && !request.plan) {
3134
+ state = finished;
3135
+ await saveState(deps.files, path, state);
2699
3136
  }
2700
- if (!writes.length) {
3137
+ const pending = planned.filter((job) => job.writes.length);
3138
+ if (!pending.length) {
2701
3139
  say("Nothing to write: every destination already has this generation.");
2702
- if (unfinishedOperations(state, owner).length) {
2703
- await saveState(deps.files, path, finishAll(state, owner));
2704
- }
2705
3140
  return 0;
2706
3141
  }
2707
- if (!await confirmed(deps, plan.needsApproval, request))
3142
+ if (request.plan)
3143
+ return planOnly(deps, "publish", request.args, needsApproval);
3144
+ if (!await confirmed2(deps, needsApproval, request))
2708
3145
  return NEEDS_APPROVAL;
2709
- const written = await writeAll({
2710
- key,
2711
- environment,
2712
- mapFile: map.file,
2713
- entry,
2714
- secret,
2715
- generation,
2716
- writes,
2717
- state,
2718
- path,
2719
- deps,
2720
- say
2721
- });
2722
- if (!written.ok) {
2723
- say("Stopped. Finished writes keep their receipts; run keypin publish again to continue.");
2724
- return 1;
3146
+ for (const [index, job] of pending.entries()) {
3147
+ const written = await writeAll({
3148
+ key: job.key,
3149
+ environment,
3150
+ mapFile: job.owner.mapFile,
3151
+ entry: job.entry,
3152
+ secret: job.secret,
3153
+ generation: job.generation,
3154
+ writes: job.writes,
3155
+ state,
3156
+ path,
3157
+ deps,
3158
+ say
3159
+ });
3160
+ state = written.state;
3161
+ if (!written.ok) {
3162
+ if (many) {
3163
+ const left = pending.slice(index).map((j) => j.key.name);
3164
+ say(`Not finished: ${left.join(", ")}.`);
3165
+ }
3166
+ say("Stopped. Finished writes keep their receipts; run keypin publish again to continue.");
3167
+ return 1;
3168
+ }
3169
+ say(`Published ${job.key.name} (${environment}) to ${job.writes.length} destination(s).`);
2725
3170
  }
2726
- say(`Published ${keyId} (${environment}) to ${writes.length} destination(s).`);
2727
3171
  return 0;
2728
3172
  }
2729
- async function confirmed(deps, needsApproval, request) {
3173
+ async function confirmed2(deps, needsApproval, request) {
2730
3174
  const say = (text) => deps.stderr.write(`${text}
2731
3175
  `);
2732
3176
  if (request.yes && !needsApproval)
@@ -2744,15 +3188,16 @@ async function confirmed(deps, needsApproval, request) {
2744
3188
  }
2745
3189
 
2746
3190
  // src/commands/rotate.ts
2747
- import { join as join6 } from "path";
2748
- import { parseArgs as parseArgs4 } from "util";
3191
+ import { join as join7 } from "path";
3192
+ import { parseArgs as parseArgs5 } from "util";
2749
3193
  var GRACE_DAYS = DUE_SOON_DAYS;
2750
3194
  async function rotate(args, deps) {
2751
- const { values, positionals } = parseArgs4({
3195
+ const { values, positionals } = parseArgs5({
2752
3196
  args,
2753
3197
  options: {
2754
3198
  env: { type: "string" },
2755
- yes: { type: "boolean", default: false }
3199
+ yes: { type: "boolean", default: false },
3200
+ plan: { type: "boolean", default: false }
2756
3201
  },
2757
3202
  allowPositionals: true
2758
3203
  });
@@ -2767,9 +3212,9 @@ async function rotate(args, deps) {
2767
3212
  const entry = key.environments[environment];
2768
3213
  if (!entry)
2769
3214
  throw new Error(`key "${keyId}" has no ${environment} entry in ${map.file}`);
2770
- const issue = entry.issue;
2771
- if (!issue) {
2772
- throw new Error(`key "${keyId}" has no [keys.${keyId}.${environment}.issue] section in ${map.file}, so keypin can't make a new value`);
3215
+ const spec = entry.rotate;
3216
+ if (!spec) {
3217
+ throw new Error(`key "${keyId}" has no [keys.${keyId}.${environment}.rotate] section in ${map.file}, so keypin can't make a new value`);
2773
3218
  }
2774
3219
  const say = (text) => deps.stderr.write(`${text}
2775
3220
  `);
@@ -2778,9 +3223,10 @@ async function rotate(args, deps) {
2778
3223
  map,
2779
3224
  key,
2780
3225
  entry,
2781
- issue,
3226
+ spec,
2782
3227
  environment,
2783
3228
  yes: values.yes,
3229
+ plan: values.plan,
2784
3230
  args,
2785
3231
  statePath,
2786
3232
  rotationsPath: rotationsPath(deps),
@@ -2792,8 +3238,8 @@ async function rotate(args, deps) {
2792
3238
  if (!open)
2793
3239
  return start(run, data);
2794
3240
  say(`An unfinished rotation of ${key.name} (${environment}), started ${open.startedAt}, is at stage "${open.stage}".`);
2795
- if (open.provider !== issue.provider || open.account !== issue.account) {
2796
- say(`Stopped: that rotation made its token as ${open.provider} in account ${open.account}, and the map now says ${issue.provider} in account ${issue.account}. Put the map back to finish it. Nothing was changed.`);
3241
+ if (open.provider !== spec.provider || open.account !== spec.account) {
3242
+ say(`Stopped: that rotation made its token as ${open.provider} in account ${open.account}, and the map now says ${spec.provider} in account ${spec.account}. Put the map back to finish it. Nothing was changed.`);
2797
3243
  return 1;
2798
3244
  }
2799
3245
  switch (open.stage) {
@@ -2810,10 +3256,10 @@ async function rotate(args, deps) {
2810
3256
  });
2811
3257
  }
2812
3258
  async function start(run, data) {
2813
- const { key, entry, issue, environment, deps, say } = run;
3259
+ const { key, entry, spec, environment, deps, say } = run;
2814
3260
  say(`Rotating ${key.name} (${environment}) from ${formatReference(entry.ref)}`);
2815
3261
  say(` 1Password account: ${entry.ref.account}`);
2816
- say(` new token: ${issue.provider} in account ${issue.account}, made with the admin token in ${formatReference(issue.admin)}`);
3262
+ say(` new token: ${spec.provider} in account ${spec.account}, made with the admin token in ${formatReference(spec.admin)}`);
2817
3263
  for (const target of entry.publish)
2818
3264
  say(` to: ${describeTarget(target, key.name)}`);
2819
3265
  if (!entry.publish.length) {
@@ -2833,29 +3279,29 @@ async function start(run, data) {
2833
3279
  return 1;
2834
3280
  }
2835
3281
  const approvals = approvalsFor({ ...planFor(run), receipts: state.receipts });
2836
- if (!deps.isTerminal && (approvals.length || !run.yes)) {
3282
+ if (!deps.isTerminal && !run.plan && (approvals.length || !run.yes)) {
2837
3283
  for (const approval of approvals)
2838
3284
  say(` needs approval: ${approval.approval}`);
2839
3285
  say(approvals.length ? "Stopped before reading anything: this rotation needs approval, and --yes can't give it. Nothing was changed." : "Stopped before reading anything: no terminal to confirm on. Nothing was changed. Add --yes, or confirm in a terminal.");
2840
3286
  say(rerun(deps, "rotate", run.args));
2841
3287
  return NEEDS_APPROVAL;
2842
3288
  }
2843
- announce(deps.stderr, "the current value and the admin token", [entry.ref, issue.admin]);
3289
+ announce(deps.stderr, "the current value and the admin token", [entry.ref, spec.admin]);
2844
3290
  const vault = deps.vault();
2845
3291
  let current;
2846
3292
  let admin;
2847
3293
  try {
2848
3294
  current = await vault.read(entry.ref);
2849
- admin = await vault.read(issue.admin);
3295
+ admin = await vault.read(spec.admin);
2850
3296
  } catch (error) {
2851
3297
  say(`Stopped: ${messageOf(error)}. Nothing was changed.`);
2852
3298
  return 1;
2853
3299
  }
2854
- if (current.reveal() === admin.reveal()) {
2855
- say(`Stopped: ${formatReference(issue.admin)} holds the same value as ${formatReference(entry.ref)}. The admin token needs its own field, or rotating would overwrite it. Nothing was changed.`);
3300
+ if (current.equals(admin)) {
3301
+ say(`Stopped: ${formatReference(spec.admin)} holds the same value as ${formatReference(entry.ref)}. The admin token needs its own field, or rotating would overwrite it. Nothing was changed.`);
2856
3302
  return 1;
2857
3303
  }
2858
- const provider = deps.provider(issue, admin);
3304
+ const provider = deps.provider(spec, admin);
2859
3305
  const found = await provider.verify(current);
2860
3306
  if (found.status !== "active") {
2861
3307
  say(`Stopped: the current value is token ${found.id}, and it's ${found.status}. Nothing was changed.`);
@@ -2885,7 +3331,9 @@ Plan:`);
2885
3331
  for (const approval of approvals)
2886
3332
  say(` needs approval: ${approval.approval}`);
2887
3333
  say(` then stop: the old token stays active until you run ${again(run)} again.`);
2888
- if (!await confirmed2(run, approvals.length > 0, "make the new token"))
3334
+ if (run.plan)
3335
+ return planOnly(deps, "rotate", run.args, approvals.length > 0);
3336
+ if (!await confirmed3(run, approvals.length > 0, "make the new token"))
2889
3337
  return NEEDS_APPROVAL;
2890
3338
  const now = deps.now().toISOString();
2891
3339
  let rotation = {
@@ -2894,8 +3342,8 @@ Plan:`);
2894
3342
  environment,
2895
3343
  mapFile: run.map.file,
2896
3344
  item: entry.ref,
2897
- provider: issue.provider,
2898
- account: issue.account,
3345
+ provider: spec.provider,
3346
+ account: spec.account,
2899
3347
  stage: "issuing",
2900
3348
  startedAt: now,
2901
3349
  updatedAt: now,
@@ -2965,14 +3413,15 @@ async function publishNew(run, data, rotation, fresh) {
2965
3413
  return 1;
2966
3414
  }
2967
3415
  checked = found;
2968
- if (!deps.isTerminal && approvalsFor({ ...planFor(run), receipts: state.receipts }).length) {
3416
+ const approvals = approvalsFor({ ...planFor(run), receipts: state.receipts });
3417
+ if (!deps.isTerminal && !run.plan && approvals.length) {
2969
3418
  say("Stopped before reading the value: publishing needs approval, and --yes can't give it.");
2970
3419
  say(rerun(deps, "rotate", run.args));
2971
3420
  return NEEDS_APPROVAL;
2972
3421
  }
2973
3422
  announce(deps.stderr, "the value to publish and the admin token, to check the new token", [
2974
3423
  entry.ref,
2975
- run.issue.admin
3424
+ run.spec.admin
2976
3425
  ]);
2977
3426
  value = await deps.vault().read(entry.ref);
2978
3427
  const generation = await generationOf(value);
@@ -2997,7 +3446,9 @@ Publishing generation ${generation}:`);
2997
3446
  say(` needs approval: ${step.approval}`);
2998
3447
  }
2999
3448
  const writes = writesFor(plan.steps, checked);
3000
- if (resuming && writes.length && !await confirmed2(run, plan.needsApproval, "publish")) {
3449
+ if (run.plan)
3450
+ return planOnly(deps, "rotate", run.args, plan.needsApproval);
3451
+ if (resuming && writes.length && !await confirmed3(run, plan.needsApproval, "publish")) {
3001
3452
  return NEEDS_APPROVAL;
3002
3453
  }
3003
3454
  const written = await writeAll({
@@ -3032,7 +3483,7 @@ Published. The old token ${rotation.old.id} "${rotation.old.name}" is still acti
3032
3483
  return 0;
3033
3484
  }
3034
3485
  async function revokeOld(run, data, rotation) {
3035
- const { key, issue, environment, deps, say } = run;
3486
+ const { key, spec, environment, deps, say } = run;
3036
3487
  const generation = rotation.new.generation;
3037
3488
  if (!generation || !rotation.new.id || !rotation.old.id) {
3038
3489
  throw new Error(`rotation ${rotation.id} has no new generation or token IDs`);
@@ -3062,21 +3513,21 @@ async function revokeOld(run, data, rotation) {
3062
3513
  }
3063
3514
  return 1;
3064
3515
  }
3065
- if (!deps.isTerminal) {
3516
+ if (!deps.isTerminal && !run.plan) {
3066
3517
  say("Stopped: revoking needs yes typed in a terminal, and --yes can't give it. Nothing was revoked.");
3067
3518
  say(rerun(deps, "rotate", run.args));
3068
3519
  return NEEDS_APPROVAL;
3069
3520
  }
3070
3521
  announce(deps.stderr, "the current value, to check it's the new token, and the admin token", [
3071
3522
  run.entry.ref,
3072
- issue.admin
3523
+ spec.admin
3073
3524
  ]);
3074
3525
  const holds = await generationOf(await deps.vault().read(run.entry.ref));
3075
3526
  if (holds !== generation) {
3076
3527
  say(`Stopped: ${formatReference(run.entry.ref)} holds generation ${holds}, not the new ${generation}. Something else changed it, and revoking would leave it with a dead token. Nothing was revoked.`);
3077
3528
  return 1;
3078
3529
  }
3079
- const provider = deps.provider(issue, await deps.vault().read(issue.admin));
3530
+ const provider = deps.provider(spec, await deps.vault().read(spec.admin));
3080
3531
  const current = await provider.details(rotation.new.id);
3081
3532
  if (current?.status !== "active") {
3082
3533
  say(`Stopped: the new token ${rotation.new.id} is ${current ? current.status : "gone"}. Nothing was revoked.`);
@@ -3085,12 +3536,17 @@ async function revokeOld(run, data, rotation) {
3085
3536
  const old = await provider.details(rotation.old.id);
3086
3537
  if (!old) {
3087
3538
  say(`The old token ${rotation.old.id} is already gone.`);
3088
- return finish(run, data, rotation);
3539
+ if (!run.plan)
3540
+ return finish(run, data, rotation);
3541
+ say("The next run records this rotation as finished, and asks nothing.");
3542
+ return planOnly(deps, "rotate", run.args, true);
3089
3543
  }
3090
3544
  say(`
3091
3545
  Old token: ${old.id} "${old.name}", ${old.status}, expires ${old.expiresOn ?? "never"}`);
3092
3546
  say(` last used: ${old.lastUsedOn ?? "not reported"}`);
3093
3547
  say(`New token: ${current.id} "${current.name}", ${current.status}, expires ${current.expiresOn ?? "never"}`);
3548
+ if (run.plan)
3549
+ return planOnly(deps, "rotate", run.args, true);
3094
3550
  const answer = await deps.ask(`Type yes to revoke the old token ${old.id}: `);
3095
3551
  if (answer.trim() !== "yes") {
3096
3552
  say("Not confirmed. Nothing was revoked.");
@@ -3106,7 +3562,7 @@ async function finish(run, data, rotation) {
3106
3562
  return 0;
3107
3563
  }
3108
3564
  async function abandon(run, data, rotation) {
3109
- const { entry, issue, deps, say } = run;
3565
+ const { entry, spec, deps, say } = run;
3110
3566
  if (rotation.new.generation) {
3111
3567
  announce(deps.stderr, "the current value, to see which token it holds", [entry.ref]);
3112
3568
  const holds = await generationOf(await deps.vault().read(entry.ref));
@@ -3117,7 +3573,8 @@ async function abandon(run, data, rotation) {
3117
3573
  updatedAt: deps.now().toISOString()
3118
3574
  };
3119
3575
  const next = putRotation(data, stored);
3120
- await saveRotations(deps.files, run.rotationsPath, next);
3576
+ if (!run.plan)
3577
+ await saveRotations(deps.files, run.rotationsPath, next);
3121
3578
  say(`${formatReference(entry.ref)} already holds the new value (generation ${holds}).`);
3122
3579
  return publishNew(run, next, stored, null);
3123
3580
  }
@@ -3142,6 +3599,10 @@ async function abandon(run, data, rotation) {
3142
3599
  }
3143
3600
  }
3144
3601
  say(`It stopped before the new value was stored, so the old token ${rotation.old.id} still works everywhere.`);
3602
+ if (run.plan) {
3603
+ say(rotation.new.id ? `The next run deletes the new token ${rotation.new.id} and forgets this rotation.` : `Cloudflare may have made a token named "${rotation.new.name}". The next run can't delete it: find it under Account API tokens and delete it, then the next run forgets this rotation.`);
3604
+ return planOnly(deps, "rotate", run.args, true);
3605
+ }
3145
3606
  if (!deps.isTerminal) {
3146
3607
  say("Stopped: cleaning it up needs yes typed in a terminal. Nothing was changed.");
3147
3608
  say(rerun(deps, "rotate", run.args));
@@ -3155,8 +3616,8 @@ async function abandon(run, data, rotation) {
3155
3616
  return NEEDS_APPROVAL;
3156
3617
  }
3157
3618
  } else {
3158
- announce(deps.stderr, "the admin token", [issue.admin]);
3159
- const provider = deps.provider(issue, await deps.vault().read(issue.admin));
3619
+ announce(deps.stderr, "the admin token", [spec.admin]);
3620
+ const provider = deps.provider(spec, await deps.vault().read(spec.admin));
3160
3621
  const made = await provider.details(id);
3161
3622
  if (made) {
3162
3623
  say(`New token: ${made.id} "${made.name}", ${made.status}, its value was never stored`);
@@ -3191,7 +3652,7 @@ async function deleteUnchecked(run, data, rotation, provider) {
3191
3652
  async function recheck(run, rotation, value) {
3192
3653
  if (!rotation.new.id || !rotation.old.id)
3193
3654
  return "the rotation has no token IDs";
3194
- const provider = run.deps.provider(run.issue, await run.deps.vault().read(run.issue.admin));
3655
+ const provider = run.deps.provider(run.spec, await run.deps.vault().read(run.spec.admin));
3195
3656
  const old = await provider.details(rotation.old.id);
3196
3657
  if (!old)
3197
3658
  return `the old token ${rotation.old.id} is gone, so its policies can't be compared`;
@@ -3233,7 +3694,7 @@ async function listedOwners(run) {
3233
3694
  list(run.map, null);
3234
3695
  const registry = await loadRegistry(deps.files, registryPath(deps));
3235
3696
  for (const project of registry.projects) {
3236
- const file = join6(project, MAP_FILE);
3697
+ const file = join7(project, MAP_FILE);
3237
3698
  if (file === run.map.file)
3238
3699
  continue;
3239
3700
  const text = await deps.files.read(file);
@@ -3286,7 +3747,7 @@ async function checkNew(provider, id, value, old) {
3286
3747
  return messageOf(error);
3287
3748
  }
3288
3749
  }
3289
- async function confirmed2(run, needsApproval, what) {
3750
+ async function confirmed3(run, needsApproval, what) {
3290
3751
  const { deps, say } = run;
3291
3752
  if (run.yes && !needsApproval)
3292
3753
  return true;
@@ -3335,9 +3796,10 @@ function describeAccess(token) {
3335
3796
  }
3336
3797
 
3337
3798
  // src/commands/run.ts
3338
- import { parseArgs as parseArgs5 } from "util";
3799
+ import { dirname as dirname3, join as join8, relative as relative2 } from "path";
3800
+ import { parseArgs as parseArgs6 } from "util";
3339
3801
  async function run(args, deps) {
3340
- const { values, positionals } = parseArgs5({
3802
+ const { values, positionals } = parseArgs6({
3341
3803
  args,
3342
3804
  options: { env: { type: "string" } },
3343
3805
  allowPositionals: true
@@ -3387,6 +3849,7 @@ async function run(args, deps) {
3387
3849
  `);
3388
3850
  return 1;
3389
3851
  }
3852
+ await warnAboutEnvFiles(deps, [...new Set([dirname3(map.file), deps.cwd])], keys.map((k) => k.key.name));
3390
3853
  let child;
3391
3854
  try {
3392
3855
  child = deps.launch(positionals, env);
@@ -3402,12 +3865,38 @@ async function run(args, deps) {
3402
3865
  stop();
3403
3866
  }
3404
3867
  }
3868
+ async function warnAboutEnvFiles(deps, folders, mapped) {
3869
+ if (!mapped.length)
3870
+ return;
3871
+ for (const folder of folders) {
3872
+ const entries = await deps.listFolder(folder).catch(() => []);
3873
+ for (const entry of entries.toSorted((a, b) => a.name.localeCompare(b.name))) {
3874
+ if (entry.kind !== "file" || envFileKind(entry.name) !== "set")
3875
+ continue;
3876
+ const path = join8(folder, entry.name);
3877
+ const text = await deps.files.read(path).catch(() => null);
3878
+ if (text === null)
3879
+ continue;
3880
+ const names = new Set(envNames(text));
3881
+ const held = mapped.filter((name) => names.has(name));
3882
+ if (held.length)
3883
+ deps.stderr.write(envFileWarning(entry.name, relative2(deps.cwd, path), held));
3884
+ }
3885
+ }
3886
+ }
3887
+ function envFileWarning(name, file, keys) {
3888
+ const list = keys.join(", ");
3889
+ const it = keys.length === 1 ? "it" : "them";
3890
+ const effect = name.startsWith(".dev.vars") ? `still sets ${list}; wrangler dev uses the file's value over keypin's` : `still holds a plaintext copy of ${list}; tools differ on which value wins`;
3891
+ return `keypin: ${file} ${effect}. Move ${it} out of the file.
3892
+ `;
3893
+ }
3405
3894
 
3406
3895
  // src/commands/status.ts
3407
- import { join as join7 } from "path";
3408
- import { parseArgs as parseArgs6 } from "util";
3896
+ import { join as join9 } from "path";
3897
+ import { parseArgs as parseArgs7 } from "util";
3409
3898
  async function status(args, deps) {
3410
- const { values, positionals } = parseArgs6({
3899
+ const { values, positionals } = parseArgs7({
3411
3900
  args,
3412
3901
  options: { json: { type: "boolean", default: false } },
3413
3902
  allowPositionals: true
@@ -3419,7 +3908,7 @@ async function status(args, deps) {
3419
3908
  const { rotations } = await loadRotations(deps.files, rotationsPath(deps));
3420
3909
  const maps = new Map;
3421
3910
  for (const project of registry.projects) {
3422
- const file = join7(project, MAP_FILE);
3911
+ const file = join9(project, MAP_FILE);
3423
3912
  try {
3424
3913
  const text = await deps.files.read(file);
3425
3914
  if (text !== null)
@@ -3495,7 +3984,7 @@ function render2(result, projects) {
3495
3984
  ["problem", "Problems"],
3496
3985
  ["notice", "Notices"]
3497
3986
  ]) {
3498
- const found = shown(result.problems.filter((p) => p.severity === severity));
3987
+ const found = result.problems.filter((p) => p.severity === severity);
3499
3988
  if (!found.length)
3500
3989
  continue;
3501
3990
  lines.push("", `${title} (${found.length}):`);
@@ -3508,30 +3997,15 @@ function render2(result, projects) {
3508
3997
  `)}
3509
3998
  `;
3510
3999
  }
3511
- function shown(problems) {
3512
- const unknown = problems.filter((p) => p.kind === "authority-unknown");
3513
- const rest = problems.filter((p) => p.kind !== "authority-unknown");
3514
- if (!unknown.length)
3515
- return rest;
3516
- const keys = [...new Set(unknown.map((p) => p.keyId ?? p.project))];
3517
- const subject = keys.length === 1 ? "1 key" : `${keys.length} keys`;
3518
- return [
3519
- ...rest,
3520
- {
3521
- kind: "authority-unknown",
3522
- message: `${subject} (${keys.join(", ")}): what each grants is unknown, because keypin didn't create it`
3523
- }
3524
- ];
3525
- }
3526
4000
 
3527
4001
  // src/commands/unregister.ts
3528
- import { resolve as resolve3 } from "path";
3529
- import { parseArgs as parseArgs7 } from "util";
4002
+ import { resolve as resolve4 } from "path";
4003
+ import { parseArgs as parseArgs8 } from "util";
3530
4004
  async function unregister2(args, deps) {
3531
- const { positionals } = parseArgs7({ args, options: {}, allowPositionals: true });
4005
+ const { positionals } = parseArgs8({ args, options: {}, allowPositionals: true });
3532
4006
  if (positionals.length > 1)
3533
4007
  throw new UsageError("keypin unregister takes at most one folder");
3534
- const folder = resolve3(deps.cwd, positionals[0] ?? ".");
4008
+ const folder = resolve4(deps.cwd, positionals[0] ?? ".");
3535
4009
  const path = registryPath(deps);
3536
4010
  const before = await loadRegistry(deps.files, path);
3537
4011
  const registry = await unregister(deps.files, path, folder);
@@ -3547,7 +4021,7 @@ async function unregister2(args, deps) {
3547
4021
  return 0;
3548
4022
  }
3549
4023
  // ../../package.json
3550
- var version = "0.5.0";
4024
+ var version = "0.7.0";
3551
4025
 
3552
4026
  // src/cli.ts
3553
4027
  var USAGE = `Usage:
@@ -3555,23 +4029,34 @@ var USAGE = `Usage:
3555
4029
  keypin unregister [folder]
3556
4030
  stop listing a project; its keypin.toml stays
3557
4031
  keypin status [--json] every key in every registered project, with problems
3558
- keypin discover [folder] [--json]
3559
- every key name a project uses, local and in the cloud, and a draft map
4032
+ keypin discover [folder] [--json] [--write]
4033
+ every key name a project uses, local and in the cloud, and a draft map;
4034
+ --write saves the draft as keypin.toml (only when there's none, or the
4035
+ untouched init template) and registers the project
4036
+ keypin import <env-file> --vault <vault> [--account <sign-in address>] [--yes]
4037
+ move an env file's values into new 1Password items and map them; the
4038
+ account is the map's, or --account when there's no map yet. It never
4039
+ overwrites an item, so --yes is enough to approve it. The file stays.
3560
4040
  keypin run [--env <name>] -- <command>
3561
4041
  run a command with one environment's keys (default: the first, dev)
3562
- keypin publish <VARIABLE> [--env <name>] [--yes]
4042
+ keypin publish <VARIABLE> [--env <name>] [--yes | --plan]
3563
4043
  check accounts, plan, confirm, then write the key to every destination
3564
- (default environment: the last, prod)
3565
- keypin rotate <VARIABLE> [--env <name>] [--yes]
4044
+ (default environment: the last, prod); --plan reads the value, prints
4045
+ the plan and the command to approve it, and writes nothing
4046
+ keypin publish --all [--env <name>] [--yes | --plan]
4047
+ the same for every key with destinations, with one plan and one approval
4048
+ keypin rotate <VARIABLE> [--env <name>] [--yes | --plan]
3566
4049
  replace a token with a new one that has the same permissions, publish it,
3567
- and revoke the old one on the next run
4050
+ and revoke the old one on the next run; --plan reads and checks the tokens,
4051
+ prints this run's plan and the command to approve it, and changes nothing
3568
4052
  keypin --version print the version
3569
4053
 
3570
4054
  Exit codes:
3571
4055
  0 done
3572
4056
  1 failed, or status found a problem
3573
4057
  2 usage error
3574
- 3 stopped for approval: run the printed command in a terminal and type yes
4058
+ 3 stopped for approval: run the printed command in a terminal and type yes;
4059
+ --plan exits 3 whenever the plan would write (routine or not), 0 when there's nothing to write
3575
4060
  keypin run exits 1 if it can't start the command, then with the command's own code.
3576
4061
  `;
3577
4062
  var COMMANDS = new Map([
@@ -3579,6 +4064,7 @@ var COMMANDS = new Map([
3579
4064
  ["unregister", unregister2],
3580
4065
  ["status", status],
3581
4066
  ["discover", discover],
4067
+ ["import", importEnv],
3582
4068
  ["run", run],
3583
4069
  ["publish", publish],
3584
4070
  ["rotate", rotate]
@@ -3622,8 +4108,8 @@ ${USAGE}`);
3622
4108
  // src/disk.ts
3623
4109
  import { constants, rmSync } from "fs";
3624
4110
  import { execFile as execFile3 } from "child_process";
3625
- import { chmod, lstat, mkdir, open, readdir, readFile as readFile2, rename, rm, stat } from "fs/promises";
3626
- import { dirname as dirname3 } from "path";
4111
+ import { chmod, lstat, mkdir, open, readdir, rename, rm, stat } from "fs/promises";
4112
+ import { dirname as dirname4 } from "path";
3627
4113
 
3628
4114
  // src/launch.ts
3629
4115
  var launch = (command, env) => {
@@ -3643,16 +4129,24 @@ var onSignals = (handler) => {
3643
4129
  // src/disk.ts
3644
4130
  var diskFiles = {
3645
4131
  async read(path) {
4132
+ let handle;
3646
4133
  try {
3647
- return await readFile2(path, "utf8");
4134
+ handle = await open(path, constants.O_RDONLY | constants.O_NONBLOCK);
3648
4135
  } catch (error) {
3649
4136
  if (errorCode(error) === "ENOENT")
3650
4137
  return null;
3651
4138
  throw error;
3652
4139
  }
4140
+ try {
4141
+ if (!(await handle.stat()).isFile())
4142
+ throw new Error(`${path} isn't a regular file`);
4143
+ return await handle.readFile("utf8");
4144
+ } finally {
4145
+ await handle.close();
4146
+ }
3653
4147
  },
3654
4148
  async write(path, text) {
3655
- await privateFolder(dirname3(path));
4149
+ await privateFolder(dirname4(path));
3656
4150
  const handle = await open(path, PRIVATE_WRITE, 384);
3657
4151
  try {
3658
4152
  await handle.chmod(384);
@@ -3686,6 +4180,78 @@ var createFile = async (path, text) => {
3686
4180
  return now === "missing" ? "exists" : now;
3687
4181
  }
3688
4182
  };
4183
+ function replaceFileWith(beforeRecheck) {
4184
+ return async (path, expected, text) => {
4185
+ const first = await readExpected(path, expected);
4186
+ if (typeof first === "string")
4187
+ return first;
4188
+ const temporary = `${path}.${crypto.randomUUID()}.tmp`;
4189
+ try {
4190
+ const handle = await open(temporary, "wx", 384);
4191
+ try {
4192
+ await handle.chmod(Number(first.mode) & 4095);
4193
+ await handle.writeFile(text);
4194
+ await handle.sync();
4195
+ } finally {
4196
+ await handle.close();
4197
+ }
4198
+ await beforeRecheck();
4199
+ const now = await readExpected(path, expected);
4200
+ if (typeof now === "string")
4201
+ return now;
4202
+ if (!sameFile(first, now))
4203
+ return "changed";
4204
+ await rename(temporary, path);
4205
+ return "replaced";
4206
+ } finally {
4207
+ await rm(temporary, { force: true });
4208
+ }
4209
+ };
4210
+ }
4211
+ var replaceFile = replaceFileWith(async () => {});
4212
+ function sameFile(a, b) {
4213
+ return a.dev === b.dev && a.ino === b.ino && a.size === b.size && a.mtimeNs === b.mtimeNs;
4214
+ }
4215
+ async function readExpected(path, expected) {
4216
+ if (await kindOf(path) === "link")
4217
+ return "link";
4218
+ let handle;
4219
+ try {
4220
+ handle = await open(path, constants.O_RDONLY | constants.O_NOFOLLOW);
4221
+ } catch (error) {
4222
+ if (errorCode(error) === "ELOOP")
4223
+ return "link";
4224
+ if (errorCode(error) === "ENOENT")
4225
+ return "changed";
4226
+ throw error;
4227
+ }
4228
+ try {
4229
+ const opened = await handle.stat({ bigint: true });
4230
+ if (await handle.readFile("utf8") !== expected)
4231
+ return "changed";
4232
+ const found = await lstat(path, { bigint: true }).catch((error) => {
4233
+ if (errorCode(error) === "ENOENT")
4234
+ return null;
4235
+ throw error;
4236
+ });
4237
+ if (found === null)
4238
+ return "changed";
4239
+ if (found.isSymbolicLink())
4240
+ return "link";
4241
+ return sameFile(opened, found) ? opened : "changed";
4242
+ } finally {
4243
+ await handle.close();
4244
+ }
4245
+ }
4246
+ async function isLink(path) {
4247
+ try {
4248
+ return (await lstat(path)).isSymbolicLink();
4249
+ } catch (error) {
4250
+ if (errorCode(error) === "ENOENT")
4251
+ return false;
4252
+ throw error;
4253
+ }
4254
+ }
3689
4255
  async function kindOf(path) {
3690
4256
  try {
3691
4257
  const found = await lstat(path);
@@ -3722,7 +4288,7 @@ function git(folder, args) {
3722
4288
  });
3723
4289
  }
3724
4290
  var diskLock = async (path) => {
3725
- await privateFolder(dirname3(path));
4291
+ await privateFolder(dirname4(path));
3726
4292
  if (await take(path))
3727
4293
  return { held: true, lock: held(path) };
3728
4294
  const pid = await holderOf(path);
@@ -3783,12 +4349,12 @@ function destination(target) {
3783
4349
  }
3784
4350
  }
3785
4351
  }
3786
- function provider(issue, admin) {
3787
- switch (issue.provider) {
4352
+ function provider(spec, admin) {
4353
+ switch (spec.provider) {
3788
4354
  case "cloudflare-account-token":
3789
- return new CloudflareAccountTokens({ account: issue.account, admin });
4355
+ return new CloudflareAccountTokens({ account: spec.account, admin });
3790
4356
  default: {
3791
- const unknown = issue.provider;
4357
+ const unknown = spec.provider;
3792
4358
  throw new Error(`unknown provider ${JSON.stringify(unknown)}`);
3793
4359
  }
3794
4360
  }
@@ -3820,6 +4386,8 @@ function main() {
3820
4386
  token: () => currentWranglerToken()
3821
4387
  })).listWorkerSecrets(account, worker),
3822
4388
  createFile,
4389
+ replaceFile,
4390
+ isLink,
3823
4391
  vault: () => vault ??= onePasswordBackend(connect),
3824
4392
  now: () => new Date,
3825
4393
  stdout: process.stdout,