keypin 0.3.0 → 0.4.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.
package/README.md CHANGED
@@ -92,7 +92,7 @@ keypin rotate <key-id> # replace a Cloudflare API token, then revoke the o
92
92
  ```
93
93
 
94
94
  `publish` and `rotate` show a plan and wait for you to type `yes`. `--yes` skips that only for routine updates. It
95
- never approves a first write, a changed account or vault item, or a revoke.
95
+ never approves a first write, a changed account or vault item, or a revoke. `keypin --help` lists the exit codes.
96
96
 
97
97
  ## Docs
98
98
 
@@ -2,8 +2,9 @@
2
2
  import {
3
3
  messageOf,
4
4
  errorCode,
5
- installProblem
6
- } from "./chunk-8agfxsqt.js";
5
+ installProblem,
6
+ selfCommand
7
+ } from "./chunk-j6kpw8ge.js";
7
8
 
8
9
  // src/start.ts
9
10
  import { homedir as homedir2 } from "os";
@@ -2406,7 +2407,7 @@ async function init(args, deps) {
2406
2407
  import { parseArgs as parseArgs3 } from "util";
2407
2408
 
2408
2409
  // src/publishing.ts
2409
- var NEEDS_APPROVAL = 2;
2410
+ var NEEDS_APPROVAL = 3;
2410
2411
  async function locked(deps, body) {
2411
2412
  const path = statePath(deps);
2412
2413
  const lockFile = `${path}.lock`;
@@ -2674,6 +2675,7 @@ async function confirmed(deps, needsApproval, request) {
2674
2675
  }
2675
2676
 
2676
2677
  // src/commands/rotate.ts
2678
+ import { join as join6 } from "path";
2677
2679
  import { parseArgs as parseArgs4 } from "util";
2678
2680
  var GRACE_DAYS = DUE_SOON_DAYS;
2679
2681
  async function rotate(args, deps) {
@@ -2707,6 +2709,7 @@ async function rotate(args, deps) {
2707
2709
  `);
2708
2710
  return locked(deps, async (statePath) => {
2709
2711
  const run = {
2712
+ map,
2710
2713
  key,
2711
2714
  entry,
2712
2715
  issue,
@@ -2979,16 +2982,34 @@ async function revokeOld(run, data, rotation) {
2979
2982
  say(`Stopped: run keypin publish ${key.id}${environment === "prod" ? "" : ` --env ${environment}`} first. Nothing was revoked.`);
2980
2983
  return 1;
2981
2984
  }
2982
- say(`Every destination's receipt has generation ${generation}.`);
2985
+ say(`Every destination of ${key.id} (${environment}) has generation ${generation}.`);
2983
2986
  for (const removed of retired) {
2984
2987
  say(` ${removed.destinationId} left the map and its receipt has the old token (generation ${removed.lastReceipt.generation}); revoking breaks it unless that secret is gone`);
2985
2988
  }
2989
+ const others = await otherHolders(run, state.receipts, rotation.old.generation, retired);
2990
+ if (others === null)
2991
+ return 1;
2992
+ if (others.length) {
2993
+ say(`Stopped: these destinations still have the old token. Put the new value in each one's vault item, then run its keypin publish. Nothing was revoked.`);
2994
+ for (const { receipt, owner } of others) {
2995
+ say(` ${describeHolder(receipt)}: ${publishCommand(owner)}`);
2996
+ }
2997
+ return 1;
2998
+ }
2986
2999
  if (!deps.isTerminal) {
2987
3000
  say("Stopped: revoking needs yes typed in a terminal, and --yes can't give it. Nothing was revoked.");
2988
3001
  say(rerun(deps, "rotate", run.args));
2989
3002
  return NEEDS_APPROVAL;
2990
3003
  }
2991
- announce(deps.stderr, "the admin token", [issue.admin]);
3004
+ announce(deps.stderr, "the current value, to check it's the new token, and the admin token", [
3005
+ run.entry.ref,
3006
+ issue.admin
3007
+ ]);
3008
+ const holds = await generationOf(await deps.vault().read(run.entry.ref));
3009
+ if (holds !== generation) {
3010
+ say(`Stopped: item "${run.entry.ref.item}" holds generation ${holds}, not the new ${generation}. Something else changed it, and revoking would leave it with a dead token. Nothing was revoked.`);
3011
+ return 1;
3012
+ }
2992
3013
  const provider = deps.provider(issue, await deps.vault().read(issue.admin));
2993
3014
  const current = await provider.details(rotation.new.id);
2994
3015
  if (current?.status !== "active") {
@@ -3039,6 +3060,21 @@ async function abandon(run, data, rotation) {
3039
3060
  return 1;
3040
3061
  }
3041
3062
  }
3063
+ if (rotation.new.generation) {
3064
+ const state = await loadState(deps.files, run.statePath);
3065
+ const holders = holdersOf(state.receipts, rotation.new.generation);
3066
+ if (holders.length) {
3067
+ say(`Stopped: the new token ${rotation.new.id} was published, so keypin won't delete it. Nothing was changed. Either put the new value back in item "${entry.ref.item}" and run ${again(run)} again to finish, or publish the old value to each destination that has the new one:`);
3068
+ const owners = await listedOwners(run);
3069
+ if (owners === null)
3070
+ return 1;
3071
+ for (const r of holders) {
3072
+ const owner = ownerOf(r, owners);
3073
+ say(` ${describeHolder(r)}: ${owner ? publishCommand(owner) : "no registered map lists it; set that secret to the old value by hand"}`);
3074
+ }
3075
+ return 1;
3076
+ }
3077
+ }
3042
3078
  say(`It stopped before the new value was stored, so the old token ${rotation.old.id} still works everywhere.`);
3043
3079
  if (!deps.isTerminal) {
3044
3080
  say("Stopped: cleaning it up needs yes typed in a terminal. Nothing was changed.");
@@ -3095,6 +3131,69 @@ async function recheck(run, rotation, value) {
3095
3131
  return `the old token ${rotation.old.id} is gone, so its policies can't be compared`;
3096
3132
  return checkNew(provider, rotation.new.id, value, old);
3097
3133
  }
3134
+ function holdersOf(receipts, generation) {
3135
+ return latestPerSecret(receipts).filter((r) => r.generation === generation);
3136
+ }
3137
+ function describeHolder(receipt) {
3138
+ return `${receipt.destinationId} (${receipt.keyId}, ${receipt.environment})`;
3139
+ }
3140
+ function publishCommand(owner) {
3141
+ const env = owner.environment === "prod" ? "" : ` --env ${owner.environment}`;
3142
+ const where = owner.project === null ? "" : `cd ${shellWord(owner.project)} && `;
3143
+ return `${where}keypin publish ${owner.keyId}${env}`;
3144
+ }
3145
+ async function listedOwners(run) {
3146
+ const { deps, say } = run;
3147
+ const owners = new Map;
3148
+ const list = (map, project) => {
3149
+ for (const k of map.keys) {
3150
+ for (const [name, entry] of Object.entries(k.environments)) {
3151
+ for (const target of entry.publish) {
3152
+ const secret = secretKey(target, k.env);
3153
+ owners.set(secret, [
3154
+ ...owners.get(secret) ?? [],
3155
+ { project, keyId: k.id, environment: name }
3156
+ ]);
3157
+ }
3158
+ }
3159
+ }
3160
+ };
3161
+ list(run.map, null);
3162
+ const registry = await loadRegistry(deps.files, registryPath(deps));
3163
+ for (const project of registry.projects) {
3164
+ const file = join6(project, MAP_FILE);
3165
+ if (file === run.map.file)
3166
+ continue;
3167
+ const text = await deps.files.read(file);
3168
+ const result = text === null ? null : parseMap(text, file);
3169
+ if (!result?.ok) {
3170
+ say(`Stopped: ${file} ${result ? "has errors" : "is missing"}, so keypin can't tell which keys use these destinations. Fix it or run keypin unregister ${shellWord(project)}. Nothing was changed.`);
3171
+ return null;
3172
+ }
3173
+ list(result.map, project);
3174
+ }
3175
+ return owners;
3176
+ }
3177
+ function ownerOf(receipt, owners) {
3178
+ const found = owners.get(receiptSecretKey(receipt)) ?? [];
3179
+ return found.find((o) => o.keyId === receipt.keyId && o.environment === receipt.environment) ?? found[0];
3180
+ }
3181
+ var UNLISTED = "no registered map lists it any more; revoking breaks it unless that secret is gone";
3182
+ async function otherHolders(run, receipts, generation, retired) {
3183
+ const owners = await listedOwners(run);
3184
+ if (owners === null)
3185
+ return null;
3186
+ const shown = new Set(retired.map((r) => r.destinationId));
3187
+ return holdersOf(receipts, generation).flatMap((receipt) => {
3188
+ const owner = ownerOf(receipt, owners);
3189
+ if (owner)
3190
+ return [{ receipt, owner }];
3191
+ if (!shown.has(receipt.destinationId)) {
3192
+ run.say(` ${describeHolder(receipt)} has the old token, and ${UNLISTED}`);
3193
+ }
3194
+ return [];
3195
+ });
3196
+ }
3098
3197
  function again(run) {
3099
3198
  return rotateCommand(run.key.id, run.environment);
3100
3199
  }
@@ -3226,7 +3325,7 @@ async function run(args, deps) {
3226
3325
  }
3227
3326
 
3228
3327
  // src/commands/status.ts
3229
- import { join as join6 } from "path";
3328
+ import { join as join7 } from "path";
3230
3329
  import { parseArgs as parseArgs6 } from "util";
3231
3330
  async function status(args, deps) {
3232
3331
  const { values, positionals } = parseArgs6({
@@ -3241,7 +3340,7 @@ async function status(args, deps) {
3241
3340
  const { rotations } = await loadRotations(deps.files, rotationsPath(deps));
3242
3341
  const maps = new Map;
3243
3342
  for (const project of registry.projects) {
3244
- const file = join6(project, MAP_FILE);
3343
+ const file = join7(project, MAP_FILE);
3245
3344
  try {
3246
3345
  const text = await deps.files.read(file);
3247
3346
  if (text !== null)
@@ -3371,7 +3470,7 @@ async function unregister2(args, deps) {
3371
3470
  return 0;
3372
3471
  }
3373
3472
  // ../../package.json
3374
- var version = "0.3.0";
3473
+ var version = "0.4.0";
3375
3474
 
3376
3475
  // src/cli.ts
3377
3476
  var USAGE = `Usage:
@@ -3388,6 +3487,13 @@ var USAGE = `Usage:
3388
3487
  replace a token with a new one that has the same permissions, publish it,
3389
3488
  and revoke the old one on the next run
3390
3489
  keypin --version print the version
3490
+
3491
+ Exit codes:
3492
+ 0 done
3493
+ 1 failed, or status found a problem
3494
+ 2 usage error
3495
+ 3 stopped for approval: run the printed command in a terminal and type yes
3496
+ keypin run exits 1 if it can't start the command, then with the command's own code.
3391
3497
  `;
3392
3498
  var COMMANDS = new Map([
3393
3499
  ["init", init],
@@ -3415,6 +3521,11 @@ async function cli(argv, deps) {
3415
3521
  ${USAGE}`);
3416
3522
  return 2;
3417
3523
  }
3524
+ const own = args.includes("--") ? args.slice(0, args.indexOf("--")) : args;
3525
+ if (own.includes("--help") || own.includes("-h")) {
3526
+ deps.stdout.write(USAGE);
3527
+ return 0;
3528
+ }
3418
3529
  try {
3419
3530
  return await command(args, deps);
3420
3531
  } catch (error) {
@@ -3618,7 +3729,7 @@ async function ask(question) {
3618
3729
  function main() {
3619
3730
  return cli(process.argv.slice(2), {
3620
3731
  cwd: process.cwd(),
3621
- self: process.argv.slice(0, 2),
3732
+ self: selfCommand(process.argv[0] ?? "bun", process.argv[1] ?? "keypin"),
3622
3733
  env: process.env,
3623
3734
  home: homedir2(),
3624
3735
  files: diskFiles,
@@ -0,0 +1,65 @@
1
+ // @bun
2
+ // src/install.ts
3
+ import { fileURLToPath } from "url";
4
+
5
+ // src/errors.ts
6
+ function messageOf(error) {
7
+ return error instanceof Error ? error.message : String(error);
8
+ }
9
+ function errorCode(error) {
10
+ if (error instanceof Error && "code" in error && typeof error.code === "string")
11
+ return error.code;
12
+ return;
13
+ }
14
+
15
+ // src/install.ts
16
+ var INSTALL = "bunx --package vite-plus@1.5.1 vp install";
17
+ var REINSTALL = "bun add -g keypin";
18
+ function checkout() {
19
+ if (!import.meta.url.endsWith("/apps/cli/src/install.ts"))
20
+ return null;
21
+ return fileURLToPath(new URL("../../..", import.meta.url)).replace(/\/$/, "");
22
+ }
23
+ function installProblem(error, repo = checkout()) {
24
+ if (errorCode(error) !== "ERR_MODULE_NOT_FOUND")
25
+ return null;
26
+ const name = /'([^']+)'/.exec(messageOf(error))?.[1];
27
+ const fix = repo === null ? `reinstall keypin with "${REINSTALL}"` : `run "${INSTALL}" in ${repo}`;
28
+ return `${name ? `the package ${name}` : "a package"} isn't installed; ${fix}`;
29
+ }
30
+
31
+ // src/runtime.ts
32
+ var MIN_BUN = "1.4.4";
33
+ var NO_ENV_FILE = "--no-env-file";
34
+ var ENV_FILE = "--env-file";
35
+ function selfCommand(bun, script) {
36
+ return [bun, NO_ENV_FILE, script];
37
+ }
38
+ function runtimeProblem(bun, script, execArgv) {
39
+ if (bun === undefined)
40
+ return `keypin runs on Bun ${MIN_BUN} or newer, not Node.`;
41
+ if (olderThan(bun, MIN_BUN)) {
42
+ return `keypin needs Bun ${MIN_BUN} or newer, because older Bun can read a keypin.toml wrong, and this is Bun ${bun}. Upgrade Bun, or run it with "bunx bun@${MIN_BUN} ${NO_ENV_FILE} ${script}".`;
43
+ }
44
+ if (!execArgv.includes(NO_ENV_FILE)) {
45
+ return `keypin needs Bun started with ${NO_ENV_FILE}, or Bun adds this folder's .env values to every command keypin runs. Run it as "keypin", or as "bun ${NO_ENV_FILE} ${script}".`;
46
+ }
47
+ if (execArgv.some((arg) => arg === ENV_FILE || arg.startsWith(`${ENV_FILE}=`))) {
48
+ return `keypin can't run with Bun's ${ENV_FILE}: Bun would add that file's values to every command keypin runs. Remove ${ENV_FILE} from the command and from BUN_OPTIONS.`;
49
+ }
50
+ return null;
51
+ }
52
+ function olderThan(version, minimum) {
53
+ const parts = (v) => v.split(/[.+-]/).slice(0, 3).map(Number);
54
+ const a = parts(version);
55
+ const b = parts(minimum);
56
+ for (let i = 0;i < 3; i++) {
57
+ const left = a[i] ?? 0;
58
+ const right = b[i] ?? 0;
59
+ if (left !== right)
60
+ return left < right;
61
+ }
62
+ return false;
63
+ }
64
+
65
+ export { messageOf, errorCode, installProblem, selfCommand, runtimeProblem };
package/main.js CHANGED
@@ -1,34 +1,12 @@
1
- #!/usr/bin/env bun
1
+ #!/usr/bin/env -S bun --no-env-file
2
2
  // @bun
3
3
  import {
4
- installProblem
5
- } from "./chunk-8agfxsqt.js";
6
-
7
- // src/runtime.ts
8
- var MIN_BUN = "1.4.4";
9
- function runtimeProblem(bun, script) {
10
- if (bun === undefined)
11
- return `keypin runs on Bun ${MIN_BUN} or newer, not Node.`;
12
- if (olderThan(bun, MIN_BUN)) {
13
- return `keypin needs Bun ${MIN_BUN} or newer, because older Bun can read a keypin.toml wrong, and this is Bun ${bun}. Upgrade Bun, or run it with "bunx bun@${MIN_BUN} ${script}".`;
14
- }
15
- return null;
16
- }
17
- function olderThan(version, minimum) {
18
- const parts = (v) => v.split(/[.+-]/).slice(0, 3).map(Number);
19
- const a = parts(version);
20
- const b = parts(minimum);
21
- for (let i = 0;i < 3; i++) {
22
- const left = a[i] ?? 0;
23
- const right = b[i] ?? 0;
24
- if (left !== right)
25
- return left < right;
26
- }
27
- return false;
28
- }
4
+ installProblem,
5
+ runtimeProblem
6
+ } from "./chunk-j6kpw8ge.js";
29
7
 
30
8
  // src/main.ts
31
- var problem = runtimeProblem(typeof Bun === "undefined" ? undefined : Bun.version, process.argv[1] ?? "keypin");
9
+ var problem = runtimeProblem(typeof Bun === "undefined" ? undefined : Bun.version, process.argv[1] ?? "keypin", process.execArgv);
32
10
  if (problem) {
33
11
  process.stderr.write(`keypin: ${problem}
34
12
  `);
@@ -36,7 +14,7 @@ if (problem) {
36
14
  }
37
15
  var start;
38
16
  try {
39
- start = await import("./chunk-p51dggc1.js");
17
+ start = await import("./chunk-dfr5e2r6.js");
40
18
  } catch (error) {
41
19
  const hint = installProblem(error);
42
20
  if (!hint)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "keypin",
3
- "version": "0.3.0",
3
+ "version": "0.4.0",
4
4
  "description": "A map of every app key across your projects, published to the account it's pinned to. Values stay in your vault.",
5
5
  "license": "MIT",
6
6
  "homepage": "https://github.com/caiopizzol/keypin#readme",
package/chunk-8agfxsqt.js DELETED
@@ -1,31 +0,0 @@
1
- // @bun
2
- // src/install.ts
3
- import { fileURLToPath } from "url";
4
-
5
- // src/errors.ts
6
- function messageOf(error) {
7
- return error instanceof Error ? error.message : String(error);
8
- }
9
- function errorCode(error) {
10
- if (error instanceof Error && "code" in error && typeof error.code === "string")
11
- return error.code;
12
- return;
13
- }
14
-
15
- // src/install.ts
16
- var INSTALL = "bunx --package vite-plus@1.5.1 vp install";
17
- var REINSTALL = "bun add -g keypin";
18
- function checkout() {
19
- if (!import.meta.url.endsWith("/apps/cli/src/install.ts"))
20
- return null;
21
- return fileURLToPath(new URL("../../..", import.meta.url)).replace(/\/$/, "");
22
- }
23
- function installProblem(error, repo = checkout()) {
24
- if (errorCode(error) !== "ERR_MODULE_NOT_FOUND")
25
- return null;
26
- const name = /'([^']+)'/.exec(messageOf(error))?.[1];
27
- const fix = repo === null ? `reinstall keypin with "${REINSTALL}"` : `run "${INSTALL}" in ${repo}`;
28
- return `${name ? `the package ${name}` : "a package"} isn't installed; ${fix}`;
29
- }
30
-
31
- export { messageOf, errorCode, installProblem };