@keyboxed/cli 0.3.3 → 0.3.4

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/dist/keybox.js CHANGED
@@ -389,6 +389,12 @@ var init_config = __esm({
389
389
  PROFILE = /^[a-z0-9][a-z0-9-]{0,30}$/;
390
390
  PIN_FILE = ".keybox.json";
391
391
  UserError = class extends Error {
392
+ /** Process exit code: 1 by default; 3 still waiting for approval, 4 denied or expired. */
393
+ constructor(message, exitCode = 1) {
394
+ super(message);
395
+ this.exitCode = exitCode;
396
+ }
397
+ exitCode;
392
398
  };
393
399
  configPath = () => join(configDir(), "config.json");
394
400
  sameUrl = (a, b) => a.replace(/\/+$/, "").toLowerCase() === b.replace(/\/+$/, "").toLowerCase();
@@ -445,13 +451,135 @@ async function api(method, path, body, config = requireConfig()) {
445
451
  }
446
452
  const data = await res.json().catch(() => null);
447
453
  if (!res.ok) throw new UserError(data?.error?.message ?? `Keybox returned HTTP ${res.status}.`);
454
+ if (res.status === 202 && data && typeof data.approval === "string") return waitForApproval(data, config);
448
455
  return data;
449
456
  }
457
+ function setApprovalWait(on) {
458
+ wait = on;
459
+ }
460
+ async function waitForApproval(p, config) {
461
+ const out2 = process.stderr;
462
+ out2.write(`Needs approval on your phone: ${p.summary.text}
463
+ `);
464
+ for (const f of p.summary.flags ?? []) out2.write(` ! ${f}
465
+ `);
466
+ if (!wait) throw new UserError(`Waiting for approval: ${p.approval}. Check with \`keybox approvals show ${p.approval}\`.`, STILL_PENDING);
467
+ out2.write(`Waiting for ${p.approval}\u2026 (Ctrl-C leaves it pending)
468
+ `);
469
+ const until = Math.min(p.expires_at, Date.now() + 5 * 6e4);
470
+ while (Date.now() < until + 2e3) {
471
+ await sleep(2e3);
472
+ let v;
473
+ try {
474
+ v = await api("GET", `/approvals/${encodeURIComponent(p.approval)}`, void 0, config);
475
+ } catch {
476
+ continue;
477
+ }
478
+ if (v.status === "executed") {
479
+ out2.write(`Approved by ${v.decided_by ?? "an approver"}.
480
+ `);
481
+ return v.result;
482
+ }
483
+ if (v.status === "failed") throw new UserError(`Approved, but it did not run: ${v.note ?? `HTTP ${v.result_status}`}`);
484
+ if (["denied", "cancelled", "expired"].includes(v.status)) throw new UserError(`Not done: the request was ${v.status}.`, NOT_APPROVED);
485
+ }
486
+ throw new UserError(`Still waiting for approval: ${p.approval}. Check with \`keybox approvals show ${p.approval}\`.`, STILL_PENDING);
487
+ }
488
+ var wait, STILL_PENDING, NOT_APPROVED, sleep;
450
489
  var init_api = __esm({
451
490
  "src/api.ts"() {
452
491
  "use strict";
453
492
  init_config();
454
493
  init_device();
494
+ wait = true;
495
+ STILL_PENDING = 3;
496
+ NOT_APPROVED = 4;
497
+ sleep = (ms) => new Promise((r) => setTimeout(r, ms));
498
+ }
499
+ });
500
+
501
+ // src/approvals.ts
502
+ async function approvalsList(all) {
503
+ const list2 = await api("GET", `/approvals${all ? "" : "?status=pending"}`);
504
+ if (!list2.length) {
505
+ note(all ? "No approval requests yet." : "Nothing is waiting for approval. `keybox approvals list --all` shows past requests.");
506
+ return;
507
+ }
508
+ process.stdout.write(
509
+ headed([
510
+ ["ID", "STATUS", "WHAT", "FROM", "WHEN"],
511
+ ...list2.map((a) => [a.id, statusOf(a), a.summary.text, who(a), ago(a.created_at)])
512
+ ])
513
+ );
514
+ if (list2.some((a) => a.status === "pending")) note("Approve or deny in the Keybox app on your phone. `keybox approvals cancel ID` withdraws your own request.");
515
+ }
516
+ async function approvalsShow(id) {
517
+ const a = await api("GET", `/approvals/${encodeURIComponent(id)}`);
518
+ const row = (k, v) => `${c.dim(k.padEnd(10))} ${v}
519
+ `;
520
+ let out2 = row("request", a.summary.text) + row("status", statusOf(a)) + row("call", `${a.method} ${a.path}`) + row("from", `${who(a)}${a.ip ? ` ${a.ip}` : ""}${a.country ? ` (${a.country})` : ""}`) + row("asked", when(a.created_at)) + (a.status === "pending" ? row("expires", when(a.expires_at)) : "") + (a.decided_by ? row("decided", `${a.decided_by}, ${a.decided_at ? when(a.decided_at) : ""}`) : "") + (a.summary.fingerprint ? row("key", `fingerprint ${a.summary.fingerprint}`) : "") + (a.note ? row("note", a.note) : "");
521
+ for (const r of a.summary.rules ?? []) out2 += row("rule", r);
522
+ for (const f of a.summary.flags ?? []) out2 += row("warning", c.warn(f));
523
+ process.stdout.write(out2);
524
+ if (a.result !== void 0) note("This request ran. Its result was handed to the command that asked for it, once; it is not shown again.");
525
+ }
526
+ async function approvalsCancel(id) {
527
+ await api("DELETE", `/approvals/${encodeURIComponent(id)}`);
528
+ ok(`Cancelled ${id}. Nothing was changed.`);
529
+ }
530
+ function statusOf(a) {
531
+ if (a.status === "pending") return c.accent("waiting");
532
+ if (a.status === "executed") return "done";
533
+ if (a.status === "failed") return c.warn("failed");
534
+ return c.dim(a.status);
535
+ }
536
+ function who(a) {
537
+ return [a.requester.member, a.requester.device].filter(Boolean).join(" / ") || a.requester.via;
538
+ }
539
+ async function approversList() {
540
+ const list2 = await api("GET", "/approvers");
541
+ if (!list2.length) {
542
+ note("No approvers yet. In the Keybox app on your phone, open Approvals and add this phone. That also turns approvals on.");
543
+ return;
544
+ }
545
+ process.stdout.write(headed([["ID", "NAME", "PERSON", "ADDED"], ...list2.map((a) => [a.id, a.name, a.member ?? "owner", when(a.created_at)])]));
546
+ if (list2.length === 1) note("One approver. Add a second phone or a spare, so losing this one does not lock you out of changes.");
547
+ }
548
+ async function approversRm(id) {
549
+ const r = await api("DELETE", `/approvers/${encodeURIComponent(id)}`);
550
+ if (!r.removed) throw new UserError(`No approver ${id}.`);
551
+ ok(`Removed approver ${id}.`);
552
+ }
553
+ async function stepupShow() {
554
+ const s = await api("GET", "/stepup");
555
+ const row = (k, v) => `${c.dim(k.padEnd(10))} ${v}
556
+ `;
557
+ process.stdout.write(
558
+ row("approvals", s.on ? c.accent("on") : "off") + row("approvers", s.approvers) + row("expire", `${s.ttl_minutes} minutes`) + (s.break_glass ? row("warning", c.warn("KEYBOX_STEPUP_OFF is set on the Worker, so approvals are off regardless.")) : "")
559
+ );
560
+ if (!s.on) {
561
+ step("With approvals off, anything holding this machine\u2019s admin credential can rebind a key to another host and so disclose it.");
562
+ note(s.approvers ? "Turn them on with `keybox stepup on`." : "Add your phone as an approver in the Keybox app; that turns them on.");
563
+ }
564
+ }
565
+ async function stepupSet(on) {
566
+ const s = await api("PUT", "/stepup", { on });
567
+ ok(s.on ? "Approvals are on. Adding access or moving a key now needs your phone." : "Approvals are off.");
568
+ }
569
+ async function stepupLine() {
570
+ try {
571
+ const s = await api("GET", "/stepup");
572
+ return s.on ? `on (${s.approvers} approver${s.approvers === 1 ? "" : "s"})` : c.warn("off (keybox stepup)");
573
+ } catch {
574
+ return null;
575
+ }
576
+ }
577
+ var init_approvals = __esm({
578
+ "src/approvals.ts"() {
579
+ "use strict";
580
+ init_api();
581
+ init_config();
582
+ init_ui();
455
583
  }
456
584
  });
457
585
 
@@ -2504,7 +2632,7 @@ var VERSION;
2504
2632
  var init_version = __esm({
2505
2633
  "src/version.ts"() {
2506
2634
  "use strict";
2507
- VERSION = true ? "0.3.3" : "0.0.0-dev";
2635
+ VERSION = true ? "0.3.4" : "0.0.0-dev";
2508
2636
  }
2509
2637
  });
2510
2638
 
@@ -2911,11 +3039,11 @@ ${c.accent("\u25A0")} This updates the Worker's code. Your stored keys, agents a
2911
3039
  async function status() {
2912
3040
  const cfg = requireConfig();
2913
3041
  const health = await (await fetch(`${cfg.url}/_keybox/health`)).json();
2914
- const [secrets, agents] = await Promise.all([api("GET", "/secrets"), api("GET", "/agents")]);
3042
+ const [secrets, agents, approvals] = await Promise.all([api("GET", "/secrets"), api("GET", "/agents"), stepupLine()]);
2915
3043
  const row = (k, v) => `${c.dim(k.padEnd(11))} ${v}
2916
3044
  `;
2917
3045
  process.stdout.write(
2918
- row("url", cfg.url) + row("dashboard", `${cfg.url.replace(/\/$/, "")}/_keybox/ui ${c.dim("(open with keybox ui)")}`) + (cfg.accountId ? row("account", cfg.accountId) : "") + row("worker", `v${health.version}${health.version === shippedWorkerVersion() ? "" : c.warn(` (this CLI ships v${shippedWorkerVersion()}; run keybox update)`)}`) + row("configured", health.configured) + row("secrets", secrets.length) + row("agents", agents.length)
3046
+ row("url", cfg.url) + row("dashboard", `${cfg.url.replace(/\/$/, "")}/_keybox/ui ${c.dim("(open with keybox ui)")}`) + (cfg.accountId ? row("account", cfg.accountId) : "") + row("worker", `v${health.version}${health.version === shippedWorkerVersion() ? "" : c.warn(` (this CLI ships v${shippedWorkerVersion()}; run keybox update)`)}`) + row("configured", health.configured) + row("secrets", secrets.length) + row("agents", agents.length) + (approvals ? row("approvals", approvals) : "")
2919
3047
  );
2920
3048
  if (process.stdout.isTTY) step(`Run ${c.bold("keybox verify")} to check the deployed code against this CLI.`);
2921
3049
  }
@@ -2923,6 +3051,7 @@ var COMPAT_DATE, asset, sha, LATER;
2923
3051
  var init_deploy = __esm({
2924
3052
  "src/deploy.ts"() {
2925
3053
  "use strict";
3054
+ init_approvals();
2926
3055
  init_api();
2927
3056
  init_cloudflare();
2928
3057
  init_config();
@@ -5115,19 +5244,19 @@ async function membersAdd(name, opts) {
5115
5244
  `);
5116
5245
  note("Valid 24 hours, works once. Their machine then waits for you: `keybox devices` shows it, `keybox devices approve ID` lets it in.");
5117
5246
  }
5118
- async function membersRole(who, role) {
5247
+ async function membersRole(who2, role) {
5119
5248
  if (role !== "admin" && role !== "member") throw new UserError("Usage: keybox members role NAME admin|member");
5120
5249
  const list2 = await api("GET", "/members");
5121
- const m = list2.find((x) => x.id === who || x.name.toLowerCase() === who.toLowerCase());
5122
- if (!m) throw new UserError(`No member called ${who}. See \`keybox members\`.`);
5250
+ const m = list2.find((x) => x.id === who2 || x.name.toLowerCase() === who2.toLowerCase());
5251
+ if (!m) throw new UserError(`No member called ${who2}. See \`keybox members\`.`);
5123
5252
  await api("PATCH", `/members/${encodeURIComponent(m.id)}`, { role });
5124
5253
  ok(`${m.name} is now ${role === "admin" ? "an admin: can add and change keys and policy" : "a member: can run their own agents and tokens, not change keys"}.`);
5125
5254
  note("Their open dashboard sessions ended; they sign in again with `keybox ui`.");
5126
5255
  }
5127
- async function membersRm(who) {
5256
+ async function membersRm(who2) {
5128
5257
  const list2 = await api("GET", "/members");
5129
- const m = list2.find((x) => x.id === who || x.name.toLowerCase() === who.toLowerCase());
5130
- if (!m) throw new UserError(`No member called ${who}. See \`keybox members\`.`);
5258
+ const m = list2.find((x) => x.id === who2 || x.name.toLowerCase() === who2.toLowerCase());
5259
+ if (!m) throw new UserError(`No member called ${who2}. See \`keybox members\`.`);
5131
5260
  await api("DELETE", `/members/${encodeURIComponent(m.id)}`);
5132
5261
  ok(`Removed ${m.name}. Their machines and dashboard sessions are revoked.`);
5133
5262
  }
@@ -5175,6 +5304,8 @@ var init_extra = __esm({
5175
5304
  });
5176
5305
 
5177
5306
  // src/index.ts
5307
+ init_api();
5308
+ init_approvals();
5178
5309
  init_commands();
5179
5310
  init_config();
5180
5311
  init_deploy();
@@ -5621,6 +5752,11 @@ var HELP_BODY = `Setup
5621
5752
  keybox devices [list|add|approve ID|revoke ID]
5622
5753
  Machines that can manage this Keybox; add one with a one-time code
5623
5754
  keybox token off [--yes] Turn the shared admin token off; only device keys work after
5755
+ keybox stepup [on|off] Approvals: adding access or moving a key needs your phone
5756
+ keybox approvals [list [--all] | show ID | cancel ID]
5757
+ Requests waiting for your phone
5758
+ keybox approvers [list | rm ID] Phones that can approve (add one in the Keybox app)
5759
+ (any change) --no-wait Don't wait for the phone; exit 3 with the approval id
5624
5760
  keybox update [--check] [--yes] Update the CLI, then the deployed Worker, then verify
5625
5761
  keybox verify [--json] Check Cloudflare is running exactly this CLI's code
5626
5762
  keybox deploy [--yes] Replace the deployed Worker with this CLI's version
@@ -5748,7 +5884,9 @@ async function main(argv) {
5748
5884
  profile: { type: "string", short: "p" },
5749
5885
  as: { type: "string" },
5750
5886
  here: { type: "boolean", default: false },
5751
- help: { type: "boolean", short: "h", default: false }
5887
+ help: { type: "boolean", short: "h", default: false },
5888
+ "no-wait": { type: "boolean", default: false },
5889
+ all: { type: "boolean", default: false }
5752
5890
  }
5753
5891
  });
5754
5892
  const [group, sub, arg] = p;
@@ -5759,8 +5897,9 @@ async function main(argv) {
5759
5897
  if (o.version) return void process.stdout.write(VERSION + "\n");
5760
5898
  useProfile(o.profile);
5761
5899
  if (o.verbose) process.env.KEYBOX_VERBOSE = "1";
5900
+ if (o["no-wait"]) setApprovalWait(false);
5762
5901
  if (o.help || !group) return void process.stdout.write(help());
5763
- switch (`${group} ${["secrets", "agents", "tokens", "access", "skill", "devices", "token", "members", "profiles"].includes(group) ? sub ?? "" : ""}`.trim()) {
5902
+ switch (`${group} ${["secrets", "agents", "tokens", "access", "skill", "devices", "token", "members", "profiles", "approvals", "approvers", "stepup"].includes(group) ? sub ?? "" : ""}`.trim()) {
5764
5903
  case "init":
5765
5904
  return init({ name: o.name, url: o.url, account: o.account, yes: o.yes, as: o.as });
5766
5905
  case "verify":
@@ -5802,6 +5941,24 @@ async function main(argv) {
5802
5941
  return devicesRevoke(need(arg, "device id"));
5803
5942
  case "token off":
5804
5943
  return tokenOff({ yes: o.yes });
5944
+ case "approvals":
5945
+ case "approvals list":
5946
+ return approvalsList(o.all);
5947
+ case "approvals show":
5948
+ return approvalsShow(need(arg, "approval id"));
5949
+ case "approvals cancel":
5950
+ return approvalsCancel(need(arg, "approval id"));
5951
+ case "approvers":
5952
+ case "approvers list":
5953
+ return approversList();
5954
+ case "approvers rm":
5955
+ return approversRm(need(arg, "approver id"));
5956
+ case "stepup":
5957
+ return stepupShow();
5958
+ case "stepup on":
5959
+ return stepupSet(true);
5960
+ case "stepup off":
5961
+ return stepupSet(false);
5805
5962
  case "status":
5806
5963
  return status();
5807
5964
  case "ui":
@@ -5887,5 +6044,5 @@ main(process.argv.slice(2)).catch((err) => {
5887
6044
  ${err instanceof Error ? err.stack : String(err)}
5888
6045
  `);
5889
6046
  }
5890
- process.exitCode = 1;
6047
+ process.exitCode = err instanceof UserError ? err.exitCode : 1;
5891
6048
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@keyboxed/cli",
3
- "version": "0.3.3",
3
+ "version": "0.3.4",
4
4
  "description": "Give AI agents access to your APIs without giving them your keys.",
5
5
  "homepage": "https://keybox.space",
6
6
  "license": "FSL-1.1-ALv2",