@coffre/conformance 0.9.0 → 0.10.1

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/main.js CHANGED
@@ -154,6 +154,10 @@ function place(path) {
154
154
  key
155
155
  };
156
156
  }
157
+ /** `?revision=57.3f9a0c2e81b4d7a6`, or nothing for a change asked without its preview. */
158
+ function asPreviewed(revision) {
159
+ return revision === void 0 ? "" : `?revision=${encodeURIComponent(revision)}`;
160
+ }
157
161
  function createClient(options) {
158
162
  const origin = options.url.replace(/\/+$/, "");
159
163
  const transport = options.transport ?? (async (request) => {
@@ -219,6 +223,12 @@ function createClient(options) {
219
223
  /** As a project's: its `name` defaults to the slug. `from` forks a sibling. */
220
224
  create: (path, input = {}) => call("PUT /projects/:project/:environment", place(path), input),
221
225
  update: (path, patch) => call("PATCH /projects/:project/:environment", place(path), patch),
226
+ /**
227
+ * Protected, a person holds their role's protected column there
228
+ * (docs/design/roles.md); whoever manages the project's environments
229
+ * protects one, and only an Owner of the whole instance unprotects it.
230
+ */
231
+ protect: (path, on = true) => call("PATCH /projects/:project/:environment", place(path), { protected: on }),
222
232
  /** What deleting an archived environment would erase and revoke; changes nothing. */
223
233
  previewDelete: (path) => {
224
234
  const [method, route] = address("DELETE /projects/:project/:environment", place(path));
@@ -306,7 +316,7 @@ function createClient(options) {
306
316
  })
307
317
  },
308
318
  /**
309
- * Handles: stand-ins a person's app holds in place of an environment's
319
+ * Handles: placeholders a person's app holds in place of an environment's
310
320
  * values, which only the credential proxy (`/use`) takes. The value is
311
321
  * in the answer, and only there; it is revoked as a token is.
312
322
  */
@@ -403,6 +413,39 @@ set: (member, access) => call("PATCH /access/:member", { member }, access) },
403
413
  list: () => call("GET /apps", {}),
404
414
  disconnect: (id) => call("DELETE /apps/:id", { id })
405
415
  },
416
+ /**
417
+ * The instance's roles, in two columns, an environment's that is not
418
+ * protected and one's that is (docs/design/roles.md). Any member lists
419
+ * them; an Owner of the whole instance edits them. `instance` is about
420
+ * instance roles, a person's across every project in their scope.
421
+ */
422
+ roles: {
423
+ list: () => call("GET /roles", {}),
424
+ /** What defining the role so would do to everyone it reaches; changes nothing. */
425
+ preview: (role, definition, { instance = false } = {}) => {
426
+ const [method, route] = instance ? address("PUT /instance-roles/:role", { role }) : address("PUT /roles/:role", { role });
427
+ return send(method, `${route}?dryRun=1`, definition);
428
+ },
429
+ /**
430
+ * A built-in role edited, or one an Owner makes. With its preview's
431
+ * `revision`, made as previewed, or refused (409 `policy_changed`)
432
+ * if the policy changed since: preview it again.
433
+ */
434
+ set: (role, definition, { instance = false, revision } = {}) => {
435
+ const [method, route] = instance ? address("PUT /instance-roles/:role", { role }) : address("PUT /roles/:role", { role });
436
+ return send(method, `${route}${asPreviewed(revision)}`, definition);
437
+ },
438
+ /** What putting it back as it ships, or deleting one an Owner made, would do; changes nothing. */
439
+ previewReset: (role, { instance = false } = {}) => {
440
+ const [method, route] = instance ? address("DELETE /instance-roles/:role", { role }) : address("DELETE /roles/:role", { role });
441
+ return send(method, `${route}?dryRun=1`, void 0);
442
+ },
443
+ /** A built-in role back as it ships; one an Owner made, deleted, while no grant names it. With its preview's `revision`, as `set`. */
444
+ reset: (role, { instance = false, revision } = {}) => {
445
+ const [method, route] = instance ? address("DELETE /instance-roles/:role", { role }) : address("DELETE /roles/:role", { role });
446
+ return send(method, `${route}${asPreviewed(revision)}`, void 0);
447
+ }
448
+ },
406
449
  /** The instance's settings, for those who run it: where people set up service accounts themselves. */
407
450
  settings: {
408
451
  get: () => call("GET /settings", {}),
@@ -410,7 +453,25 @@ set: (member, access) => call("PATCH /access/:member", { member }, access) },
410
453
  },
411
454
  audit: {
412
455
  list: (query = {}) => call("GET /audit", {}, query),
413
- verify: () => call("GET /audit/verification", {}),
456
+ /**
457
+ * Whether the log holds, from the newest checkpoint; or, with `from`, a
458
+ * stretch of the walk from the first entry: `start`, then each `next`
459
+ * until it is null (`verifyWhole`).
460
+ */
461
+ verify: (query = {}) => call("GET /audit/verification", {}, query),
462
+ /**
463
+ * The walk from the first entry to the head, a stretch a request, each
464
+ * as long as the server walks in one: the first stretch that fails, or
465
+ * the last. `onStretch` sees each verdict as it comes.
466
+ */
467
+ verifyWhole: async (onStretch = () => {}) => {
468
+ for (let from = "start";;) {
469
+ const verdict = await call("GET /audit/verification", {}, { from });
470
+ onStretch(verdict);
471
+ if (!verdict.ok || verdict.next === void 0 || verdict.next === null) return verdict;
472
+ from = verdict.next;
473
+ }
474
+ },
414
475
  /** What an escrowed key is checked against: public material, for admins and owners of the whole instance. */
415
476
  keys: () => call("GET /audit/keys", {})
416
477
  }
@@ -586,6 +647,16 @@ async function boot(kind, at, options) {
586
647
  children.push(entry);
587
648
  return entry;
588
649
  }
650
+ /** Stop one of the daemons on purpose: its exit fails nothing. */
651
+ async function halt(entry) {
652
+ entry.daemon = false;
653
+ if (entry.exited) return;
654
+ const exited = new Promise((done) => entry.child.once("exit", done));
655
+ process.kill(-entry.child.pid, "SIGTERM");
656
+ const killed = setTimeout(() => process.kill(-entry.child.pid, "SIGKILL"), 1e4);
657
+ await exited;
658
+ clearTimeout(killed);
659
+ }
589
660
  async function run(command, args, env = {}, input) {
590
661
  const entry = start(args[0] ?? command, command, args, env, false, input);
591
662
  if (await new Promise((done) => {
@@ -646,7 +717,7 @@ async function boot(kind, at, options) {
646
717
  ], {}, `${owner}\n`);
647
718
  await run(bin("vite"), ["build", "app"]);
648
719
  const state = join(scratch, "state");
649
- start("wrangler", bin("wrangler"), [
720
+ const startWrangler = () => start("wrangler", bin("wrangler"), [
650
721
  "dev",
651
722
  ...[
652
723
  "-c",
@@ -686,6 +757,7 @@ async function boot(kind, at, options) {
686
757
  CLOUDFLARE_HYPERDRIVE_LOCAL_CONNECTION_STRING_VAULT_HYPERDRIVE: vaultRuntime,
687
758
  WRANGLER_SEND_METRICS: "false"
688
759
  });
760
+ let wrangler = startWrangler();
689
761
  return {
690
762
  kind,
691
763
  origin,
@@ -696,6 +768,12 @@ async function boot(kind, at, options) {
696
768
  const response = await fetch(`${origin}/cdn-cgi/local/scheduled?cron=*/5+*+*+*+*`);
697
769
  if (!response.ok && !options.allowFailure) throw new Failure(`the scheduled handler answered ${response.status}`, `${await response.text()}\n${logs()}`);
698
770
  },
771
+ async restart() {
772
+ await halt(wrangler);
773
+ wrangler = startWrangler();
774
+ await until(`${origin}/livez`, async () => (await fetch(`${origin}/livez`)).ok, 120, alive);
775
+ },
776
+ serving: () => ({ wrangler: wrangler.child.pid }),
699
777
  database: () => postgres(owner),
700
778
  runtime: () => postgres(runtime),
701
779
  vaultRuntime: () => postgres(vaultRuntime),
@@ -709,14 +787,19 @@ async function boot(kind, at, options) {
709
787
  const database = join(scratch, "coffre.db");
710
788
  const socket = join(scratch, "vault.sock");
711
789
  await run(bin("coffre-server"), ["migrate", `file:${database}`]);
712
- start("vault", process.execPath, ["src/vault.ts"], {
713
- VAULT_SOCKET: socket,
714
- DATABASE_URL: `file:${database}`,
715
- VAULT_KEY_ID: KEYS.VAULT_KEY_ID,
716
- VAULT_KEY: KEYS.VAULT_KEY,
717
- ...OWNER_VARS
718
- });
719
- await until("the vault socket", async () => existsSync(socket), 30, alive);
790
+ const startVault = async () => {
791
+ rmSync(socket, { force: true });
792
+ const started = start("vault", process.execPath, ["src/vault.ts"], {
793
+ VAULT_SOCKET: socket,
794
+ DATABASE_URL: `file:${database}`,
795
+ VAULT_KEY_ID: KEYS.VAULT_KEY_ID,
796
+ VAULT_KEY: KEYS.VAULT_KEY,
797
+ ...OWNER_VARS
798
+ });
799
+ await until("the vault socket", async () => existsSync(socket), 30, alive);
800
+ return started;
801
+ };
802
+ let vault = await startVault();
720
803
  await run(bin("vite"), ["build", "app"]);
721
804
  const startServer = () => start("server", bin("srvx"), [
722
805
  "--prod",
@@ -739,13 +822,21 @@ async function boot(kind, at, options) {
739
822
  owner: OWNER,
740
823
  beforeFirstBeat: false,
741
824
  async scheduled() {
742
- const exited = new Promise((done) => server.child.once("exit", done));
743
- server.daemon = false;
744
- process.kill(-server.child.pid, "SIGTERM");
745
- await exited;
825
+ await halt(server);
826
+ server = startServer();
827
+ await until("the restarted server", async () => (await fetch(`${origin}/livez`)).ok, 30, alive);
828
+ },
829
+ async restart() {
830
+ await halt(server);
831
+ await halt(vault);
832
+ vault = await startVault();
746
833
  server = startServer();
747
834
  await until("the restarted server", async () => (await fetch(`${origin}/livez`)).ok, 30, alive);
748
835
  },
836
+ serving: () => ({
837
+ vault: vault.child.pid,
838
+ server: server.child.pid
839
+ }),
749
840
  database: async () => sqlite(database),
750
841
  runtime: null,
751
842
  vaultRuntime: null,
@@ -2343,10 +2434,21 @@ async function revealAudited(deployment, { admin, reader }, canaries, purpose) {
2343
2434
  });
2344
2435
  return `${read.length} values read by the vault for ${purpose}, in one entry under ${operationId}`;
2345
2436
  }
2346
- /** Full verification must reach the stored head, not just some intact prefix. */
2437
+ /**
2438
+ * Verification must reach the stored head, not just some intact prefix:
2439
+ * the page's, from the newest checkpoint, and the walk from the first
2440
+ * entry, a stretch a request, which ends where it does. And the audit page
2441
+ * is sent with its verdict, rendered by the server, never a badge to come.
2442
+ */
2347
2443
  async function verification(deployment, { admin }) {
2348
- const result = await admin.api.audit.verify();
2349
- expect(result.ok, "the full log does not verify", result);
2444
+ const anchored = await admin.api.audit.verify();
2445
+ expect(anchored.ok && anchored.remaining === 0, "the log does not verify from its newest checkpoint", anchored);
2446
+ const result = await admin.api.audit.verifyWhole();
2447
+ expect(result.ok && result.remaining === 0, "the full log does not verify to its end", result);
2448
+ expect(result.through !== null && anchored.through !== null && result.through >= anchored.through, "the walk from the first entry stopped short", {
2449
+ anchored,
2450
+ result
2451
+ });
2350
2452
  await using(deployment.database(), async (sql) => {
2351
2453
  const [head] = await sql.query("SELECT next_seq, head_hash FROM audit_chain_head");
2352
2454
  const [last] = await sql.query("SELECT seq, hash FROM audit_log ORDER BY seq DESC LIMIT 1");
@@ -2359,7 +2461,10 @@ async function verification(deployment, { admin }) {
2359
2461
  });
2360
2462
  expect(BigInt(result.entries) === BigInt(head.next_seq), "verification did not count every entry", result);
2361
2463
  });
2362
- return `${result.entries} entries verified through the head at ${result.through}`;
2464
+ const page = await admin.browser.fetch("/audit");
2465
+ const html = await page.text();
2466
+ expect(page.status === 200 && html.includes("chain-seal") && !html.includes("Verifying"), "the audit page was not sent with its verification result", { status: page.status });
2467
+ return `${result.entries} entries verified through the head at ${result.through}, and the audit page sent with its result`;
2363
2468
  }
2364
2469
  /**
2365
2470
  * Each heartbeat has the vault sign the log up to its last entry, in an
@@ -2508,9 +2613,13 @@ async function forgedVaultEntry(deployment, { admin }) {
2508
2613
  });
2509
2614
  return "a publicly chained vault entry with a made-up MAC was refused at its sequence";
2510
2615
  }
2511
- /** Rewrite either author's entry and require a fault at that exact sequence. */
2616
+ /**
2617
+ * Rewrite either author's first entry in place and require a fault at that
2618
+ * exact sequence: from the walk from the first entry, since the page's
2619
+ * check goes on from the newest checkpoint and reads nothing before it.
2620
+ */
2512
2621
  async function rewrittenEntry(deployment, { admin }, author) {
2513
- expect((await admin.api.audit.verify()).ok, "the log does not verify before the rewrite");
2622
+ expect((await admin.api.audit.verifyWhole()).ok, "the log does not verify before the rewrite");
2514
2623
  let seq = 0;
2515
2624
  await using(deployment.database(), (sql) => appendOnlyLifted(sql, async () => {
2516
2625
  const [row] = await query(sql, "SELECT seq, actor FROM audit_log WHERE author = $1 ORDER BY seq LIMIT 1", [author]);
@@ -2518,13 +2627,13 @@ async function rewrittenEntry(deployment, { admin }, author) {
2518
2627
  seq = Number(row.seq);
2519
2628
  await query(sql, "UPDATE audit_log SET actor = $1 WHERE seq = $2", ["user:nobody@conformance.example", row.seq]);
2520
2629
  try {
2521
- const result = await admin.api.audit.verify();
2630
+ const result = await admin.api.audit.verifyWhole();
2522
2631
  expect(!result.ok && result.failedAtSeq === Number(row.seq), "a rewritten entry was not caught at its sequence", result);
2523
2632
  } finally {
2524
2633
  await query(sql, "UPDATE audit_log SET actor = $1 WHERE seq = $2", [row.actor, row.seq]);
2525
2634
  }
2526
2635
  }));
2527
- expect((await admin.api.audit.verify()).ok, "the restored entry did not verify");
2636
+ expect((await admin.api.audit.verifyWhole()).ok, "the restored entry did not verify");
2528
2637
  return `entry ${seq}, the ${author}'s, rewritten: caught there, then put back`;
2529
2638
  }
2530
2639
  /** Remove an unreferenced entry, then put its exact bytes back. */
@@ -2586,7 +2695,7 @@ async function everyAuditEntry(api) {
2586
2695
  //#region src/checks/checkpoints.ts
2587
2696
  /** Leave one old signature invalid, reseal the chain, and make later checkpoints valid. */
2588
2697
  async function earlierCheckpoint(deployment, { admin }) {
2589
- expect((await admin.api.audit.verify()).ok, "the log does not verify before checkpoint tampering");
2698
+ expect((await admin.api.audit.verifyWhole()).ok, "the log does not verify before checkpoint tampering");
2590
2699
  await using(deployment.database(), (sql) => appendOnlyLifted(sql, async () => {
2591
2700
  const rows = await sql.query("SELECT * FROM audit_log ORDER BY seq");
2592
2701
  const signed = rows.filter((row) => row.author === "vault" && row.action === "audit.checkpoint" && row.decision === "allow");
@@ -2630,7 +2739,7 @@ async function earlierCheckpoint(deployment, { admin }) {
2630
2739
  head_hash: previous
2631
2740
  }, "only_row");
2632
2741
  });
2633
- const result = await admin.api.audit.verify();
2742
+ const result = await admin.api.audit.verifyWhole();
2634
2743
  expect(!result.ok && result.author === "vault" && result.failedAtSeq === Number(signed[0].seq) && result.reason === "a checkpoint the vault did not sign", "a valid later checkpoint hid an invalid earlier one", result);
2635
2744
  } finally {
2636
2745
  await transaction(sql, async () => {
@@ -2642,7 +2751,7 @@ async function earlierCheckpoint(deployment, { admin }) {
2642
2751
  seed.fill(0);
2643
2752
  }
2644
2753
  }));
2645
- expect((await admin.api.audit.verify()).ok, "restoring the signed prefixes did not restore verification");
2754
+ expect((await admin.api.audit.verifyWhole()).ok, "restoring the signed prefixes did not restore verification");
2646
2755
  return "an invalid earlier checkpoint was caught despite valid MACs and a valid later checkpoint";
2647
2756
  }
2648
2757
  //#endregion
@@ -3154,12 +3263,13 @@ async function middleCut(deployment, { admin }) {
3154
3263
  const [heartbeat] = await query(sql, `SELECT h.seq FROM audit_log h JOIN audit_log c ON c.seq = h.seq + 1
3155
3264
  WHERE h.action = 'audit.heartbeat' AND h.seq > 0 AND c.author = 'vault' AND c.action = 'audit.checkpoint'
3156
3265
  AND c.decision = 'allow' AND c.seq < $1 AND NOT EXISTS
3157
- (SELECT 1 FROM audit_log linked WHERE linked.related_seq IN (h.seq, c.seq)) ORDER BY h.seq LIMIT 1`, [JSON.parse(latest.metadata).seq]);
3266
+ (SELECT 1 FROM audit_log linked WHERE linked.related_seq IN (h.seq, c.seq) AND linked.seq <> c.seq) ORDER BY h.seq LIMIT 1`, [JSON.parse(latest.metadata).seq]);
3158
3267
  expect(heartbeat !== void 0, "no older heartbeat and checkpoint to cut");
3159
3268
  seq = Number(heartbeat.seq);
3160
3269
  const kept = await query(sql, "SELECT * FROM audit_log WHERE seq >= $1 AND seq <= $2 ORDER BY seq", [seq, seq + 1]);
3161
3270
  expect(kept.length === 2, "the middle cut needs two adjacent entries");
3162
3271
  await appendOnlyLifted(sql, async () => {
3272
+ await query(sql, "UPDATE audit_log SET related_seq = NULL WHERE seq = $1", [seq + 1]);
3163
3273
  await query(sql, "DELETE FROM audit_log WHERE seq >= $1 AND seq <= $2", [seq, seq + 1]);
3164
3274
  try {
3165
3275
  await deployment.scheduled({ allowFailure: true });
@@ -3217,6 +3327,7 @@ function getCalls({ places, secrets, members, services }) {
3217
3327
  input: { github: "acme/api" }
3218
3328
  }],
3219
3329
  "GET /workloads/repositories": [{ params: {} }],
3330
+ "GET /roles": [{ params: {} }],
3220
3331
  "GET /settings": [{ params: {} }],
3221
3332
  "GET /sessions": [{ params: {} }],
3222
3333
  "GET /identities": [{ params: {} }],
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@coffre/conformance",
3
- "version": "0.9.0",
3
+ "version": "0.10.1",
4
4
  "description": "Checks a coffre deployment from outside: what it must never do, whatever code it runs.",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -31,8 +31,8 @@
31
31
  "dependencies": {
32
32
  "jose": "6.2.3",
33
33
  "pg": "8.16.3",
34
- "@coffre/db": "0.9.0",
35
- "@coffre/cli": "0.9.0"
34
+ "@coffre/cli": "0.10.1",
35
+ "@coffre/db": "0.10.1"
36
36
  },
37
37
  "devDependencies": {
38
38
  "@types/node": "26.1.1",
@@ -41,7 +41,7 @@
41
41
  "tsdown": "0.23.0",
42
42
  "typescript": "5.9.3",
43
43
  "vite": "8.1.5",
44
- "@coffre/client": "0.9.0"
44
+ "@coffre/client": "0.10.1"
45
45
  },
46
46
  "scripts": {
47
47
  "build": "tsdown",
@@ -54,10 +54,18 @@ export async function revealAudited(deployment: Deployment, { admin, reader }: P
54
54
  return `${read.length} values read by the vault for ${purpose}, in one entry under ${operationId}`;
55
55
  }
56
56
 
57
- /** Full verification must reach the stored head, not just some intact prefix. */
57
+ /**
58
+ * Verification must reach the stored head, not just some intact prefix:
59
+ * the page's, from the newest checkpoint, and the walk from the first
60
+ * entry, a stretch a request, which ends where it does. And the audit page
61
+ * is sent with its verdict, rendered by the server, never a badge to come.
62
+ */
58
63
  export async function verification(deployment: Deployment, { admin }: People): Promise<string> {
59
- const result = await admin.api.audit.verify();
60
- expect(result.ok, 'the full log does not verify', result);
64
+ const anchored = await admin.api.audit.verify();
65
+ expect(anchored.ok && anchored.remaining === 0, 'the log does not verify from its newest checkpoint', anchored);
66
+ const result = await admin.api.audit.verifyWhole();
67
+ expect(result.ok && result.remaining === 0, 'the full log does not verify to its end', result);
68
+ expect(result.through !== null && anchored.through !== null && result.through >= anchored.through, 'the walk from the first entry stopped short', { anchored, result });
61
69
  await using(deployment.database(), async (sql) => {
62
70
  const [head] = await sql.query<{ next_seq: string | number; head_hash: Uint8Array }>('SELECT next_seq, head_hash FROM audit_chain_head');
63
71
  const [last] = await sql.query<{ seq: string | number; hash: Uint8Array }>('SELECT seq, hash FROM audit_log ORDER BY seq DESC LIMIT 1');
@@ -67,7 +75,10 @@ export async function verification(deployment: Deployment, { admin }: People): P
67
75
  expect(result.through !== null && BigInt(result.through) === BigInt(last.seq), 'verification stopped before the head', { result, head: head.next_seq });
68
76
  expect(BigInt(result.entries) === BigInt(head.next_seq), 'verification did not count every entry', result);
69
77
  });
70
- return `${result.entries} entries verified through the head at ${result.through}`;
78
+ const page = await admin.browser.fetch('/audit');
79
+ const html = await page.text();
80
+ expect(page.status === 200 && html.includes('chain-seal') && !html.includes('Verifying'), 'the audit page was not sent with its verification result', { status: page.status });
81
+ return `${result.entries} entries verified through the head at ${result.through}, and the audit page sent with its result`;
71
82
  }
72
83
 
73
84
  /**
@@ -203,9 +214,13 @@ export async function forgedVaultEntry(deployment: Deployment, { admin }: People
203
214
  return 'a publicly chained vault entry with a made-up MAC was refused at its sequence';
204
215
  }
205
216
 
206
- /** Rewrite either author's entry and require a fault at that exact sequence. */
217
+ /**
218
+ * Rewrite either author's first entry in place and require a fault at that
219
+ * exact sequence: from the walk from the first entry, since the page's
220
+ * check goes on from the newest checkpoint and reads nothing before it.
221
+ */
207
222
  export async function rewrittenEntry(deployment: Deployment, { admin }: People, author: 'app' | 'vault'): Promise<string> {
208
- expect((await admin.api.audit.verify()).ok, 'the log does not verify before the rewrite');
223
+ expect((await admin.api.audit.verifyWhole()).ok, 'the log does not verify before the rewrite');
209
224
  let seq = 0;
210
225
  await using(deployment.database(), (sql) => appendOnlyLifted(sql, async () => {
211
226
  const [row] = await query<{ seq: string | number; actor: string }>(sql,
@@ -214,13 +229,13 @@ export async function rewrittenEntry(deployment: Deployment, { admin }: People,
214
229
  seq = Number(row.seq);
215
230
  await query(sql, 'UPDATE audit_log SET actor = $1 WHERE seq = $2', ['user:nobody@conformance.example', row.seq]);
216
231
  try {
217
- const result = await admin.api.audit.verify();
232
+ const result = await admin.api.audit.verifyWhole();
218
233
  expect(!result.ok && result.failedAtSeq === Number(row.seq), 'a rewritten entry was not caught at its sequence', result);
219
234
  } finally {
220
235
  await query(sql, 'UPDATE audit_log SET actor = $1 WHERE seq = $2', [row.actor, row.seq]);
221
236
  }
222
237
  }));
223
- expect((await admin.api.audit.verify()).ok, 'the restored entry did not verify');
238
+ expect((await admin.api.audit.verifyWhole()).ok, 'the restored entry did not verify');
224
239
  return `entry ${seq}, the ${author}'s, rewritten: caught there, then put back`;
225
240
  }
226
241
 
@@ -11,7 +11,7 @@ import { appendOnlyLifted, entryFields, restoreRow, transaction } from './storag
11
11
 
12
12
  /** Leave one old signature invalid, reseal the chain, and make later checkpoints valid. */
13
13
  export async function earlierCheckpoint(deployment: Deployment, { admin }: People): Promise<string> {
14
- expect((await admin.api.audit.verify()).ok, 'the log does not verify before checkpoint tampering');
14
+ expect((await admin.api.audit.verifyWhole()).ok, 'the log does not verify before checkpoint tampering');
15
15
  await using(deployment.database(), (sql) => appendOnlyLifted(sql, async () => {
16
16
  const rows = await sql.query('SELECT * FROM audit_log ORDER BY seq');
17
17
  const signed = rows.filter((row) => row.author === 'vault' && row.action === 'audit.checkpoint' && row.decision === 'allow');
@@ -49,7 +49,8 @@ export async function earlierCheckpoint(deployment: Deployment, { admin }: Peopl
49
49
  }
50
50
  await restoreRow(sql, 'audit_chain_head', { ...head, head_hash: previous }, 'only_row');
51
51
  });
52
- const result = await admin.api.audit.verify();
52
+ // The page's check goes on from the newest checkpoint, valid: the walk from the first entry reads them all.
53
+ const result = await admin.api.audit.verifyWhole();
53
54
  expect(!result.ok && result.author === 'vault' && result.failedAtSeq === Number(signed[0].seq)
54
55
  && result.reason === 'a checkpoint the vault did not sign', 'a valid later checkpoint hid an invalid earlier one', result);
55
56
  } finally {
@@ -60,6 +61,6 @@ export async function earlierCheckpoint(deployment: Deployment, { admin }: Peopl
60
61
  keys.app.fill(0); keys.vault.fill(0); seed.fill(0);
61
62
  }
62
63
  }));
63
- expect((await admin.api.audit.verify()).ok, 'restoring the signed prefixes did not restore verification');
64
+ expect((await admin.api.audit.verifyWhole()).ok, 'restoring the signed prefixes did not restore verification');
64
65
  return 'an invalid earlier checkpoint was caught despite valid MACs and a valid later checkpoint';
65
66
  }
@@ -55,16 +55,18 @@ export async function middleCut(deployment: Deployment, { admin }: People): Prom
55
55
  );
56
56
  expect(latest !== undefined, 'no signed prefix to cut from');
57
57
  const signed = JSON.parse(latest.metadata) as { seq: number };
58
- // An old heartbeat and its checkpoint carry no access change or linked write.
58
+ // An old heartbeat and its checkpoint carry no access change or linked write: nothing names them but the
59
+ // checkpoint, which names the heartbeat as the last entry it signs.
59
60
  const [heartbeat] = await query<{ seq: string | number }>(sql, `SELECT h.seq FROM audit_log h JOIN audit_log c ON c.seq = h.seq + 1
60
61
  WHERE h.action = 'audit.heartbeat' AND h.seq > 0 AND c.author = 'vault' AND c.action = 'audit.checkpoint'
61
62
  AND c.decision = 'allow' AND c.seq < $1 AND NOT EXISTS
62
- (SELECT 1 FROM audit_log linked WHERE linked.related_seq IN (h.seq, c.seq)) ORDER BY h.seq LIMIT 1`, [signed.seq]);
63
+ (SELECT 1 FROM audit_log linked WHERE linked.related_seq IN (h.seq, c.seq) AND linked.seq <> c.seq) ORDER BY h.seq LIMIT 1`, [signed.seq]);
63
64
  expect(heartbeat !== undefined, 'no older heartbeat and checkpoint to cut');
64
65
  seq = Number(heartbeat.seq);
65
66
  const kept = await query(sql, 'SELECT * FROM audit_log WHERE seq >= $1 AND seq <= $2 ORDER BY seq', [seq, seq + 1]);
66
67
  expect(kept.length === 2, 'the middle cut needs two adjacent entries');
67
68
  await appendOnlyLifted(sql, async () => {
69
+ await query(sql, 'UPDATE audit_log SET related_seq = NULL WHERE seq = $1', [seq + 1]);
68
70
  await query(sql, 'DELETE FROM audit_log WHERE seq >= $1 AND seq <= $2', [seq, seq + 1]);
69
71
  try {
70
72
  // Do not call full verification first: the scheduled job must find the hole itself.
package/src/harness.ts CHANGED
@@ -46,6 +46,10 @@ export type Deployment = {
46
46
  beforeFirstBeat: boolean;
47
47
  /** What the Cron trigger does every five minutes. */
48
48
  scheduled(options?: { allowFailure?: boolean }): Promise<void>;
49
+ /** Stop what serves the deployment and start it again on the same data: new isolates on Workers, new processes on Node. */
50
+ restart(): Promise<void>;
51
+ /** The process group of each process serving it, by name, for whoever measures them: `wrangler` on Workers; `vault` and `server` on Node. */
52
+ serving(): Record<string, number>;
49
53
  /** The app's database, as its owner. */
50
54
  database(): Promise<Sql>;
51
55
  /** The app's database as the login the app runs as, on an engine that has logins. */
@@ -143,6 +147,17 @@ export async function boot(kind: Kind, at: string, options: HarnessOptions): Pro
143
147
  return entry;
144
148
  }
145
149
 
150
+ /** Stop one of the daemons on purpose: its exit fails nothing. */
151
+ async function halt(entry: Child): Promise<void> {
152
+ entry.daemon = false;
153
+ if (entry.exited) return;
154
+ const exited = new Promise((done) => entry.child.once('exit', done));
155
+ process.kill(-entry.child.pid!, 'SIGTERM');
156
+ const killed = setTimeout(() => process.kill(-entry.child.pid!, 'SIGKILL'), 10_000);
157
+ await exited;
158
+ clearTimeout(killed);
159
+ }
160
+
146
161
  async function run(command: string, args: string[], env: Record<string, string> = {}, input?: string): Promise<void> {
147
162
  const entry = start(args[0] ?? command, command, args, env, false, input);
148
163
  const code = await new Promise((done) => {
@@ -216,7 +231,7 @@ export async function boot(kind: Kind, at: string, options: HarnessOptions): Pro
216
231
  // The app as it deploys: its Start app, built by its own Vite, which wrangler runs unbundled.
217
232
  await run(bin('vite'), ['build', 'app']);
218
233
  const state = join(scratch, 'state');
219
- start('wrangler', bin('wrangler'), [
234
+ const startWrangler = () => start('wrangler', bin('wrangler'), [
220
235
  'dev',
221
236
  ...['-c', drainingConfig(join(dir, 'app/dist/server')), '-c', 'vault/wrangler.jsonc'],
222
237
  ...['--ip', '127.0.0.1', '--port', String(port), '--inspector-port', String(port + 2)],
@@ -239,6 +254,7 @@ export async function boot(kind: Kind, at: string, options: HarnessOptions): Pro
239
254
  CLOUDFLARE_HYPERDRIVE_LOCAL_CONNECTION_STRING_VAULT_HYPERDRIVE: vaultRuntime,
240
255
  WRANGLER_SEND_METRICS: 'false',
241
256
  });
257
+ let wrangler = startWrangler();
242
258
  return {
243
259
  kind,
244
260
  origin,
@@ -249,6 +265,12 @@ export async function boot(kind: Kind, at: string, options: HarnessOptions): Pro
249
265
  const response = await fetch(`${origin}/cdn-cgi/local/scheduled?cron=*/5+*+*+*+*`);
250
266
  if (!response.ok && !options.allowFailure) throw new Failure(`the scheduled handler answered ${response.status}`, `${await response.text()}\n${logs()}`);
251
267
  },
268
+ async restart() {
269
+ await halt(wrangler);
270
+ wrangler = startWrangler();
271
+ await until(`${origin}/livez`, async () => (await fetch(`${origin}/livez`)).ok, 120, alive);
272
+ },
273
+ serving: () => ({ wrangler: wrangler.child.pid! }),
252
274
  database: () => postgres(owner),
253
275
  runtime: () => postgres(runtime),
254
276
  vaultRuntime: () => postgres(vaultRuntime),
@@ -263,14 +285,19 @@ export async function boot(kind: Kind, at: string, options: HarnessOptions): Pro
263
285
  const database = join(scratch, 'coffre.db');
264
286
  const socket = join(scratch, 'vault.sock');
265
287
  await run(bin('coffre-server'), ['migrate', `file:${database}`]);
266
- start('vault', process.execPath, ['src/vault.ts'], {
267
- VAULT_SOCKET: socket,
268
- DATABASE_URL: `file:${database}`,
269
- VAULT_KEY_ID: KEYS.VAULT_KEY_ID,
270
- VAULT_KEY: KEYS.VAULT_KEY,
271
- ...OWNER_VARS,
272
- });
273
- await until('the vault socket', async () => existsSync(socket), 30, alive);
288
+ const startVault = async () => {
289
+ rmSync(socket, { force: true });
290
+ const started = start('vault', process.execPath, ['src/vault.ts'], {
291
+ VAULT_SOCKET: socket,
292
+ DATABASE_URL: `file:${database}`,
293
+ VAULT_KEY_ID: KEYS.VAULT_KEY_ID,
294
+ VAULT_KEY: KEYS.VAULT_KEY,
295
+ ...OWNER_VARS,
296
+ });
297
+ await until('the vault socket', async () => existsSync(socket), 30, alive);
298
+ return started;
299
+ };
300
+ let vault = await startVault();
274
301
  // The pages as they run: the deployment's Start app, built by its own Vite.
275
302
  await run(bin('vite'), ['build', 'app']);
276
303
  // As `pnpm start` runs it: the app's build, under srvx.
@@ -294,13 +321,18 @@ export async function boot(kind: Kind, at: string, options: HarnessOptions): Pro
294
321
  // `serve` runs the job as it starts, and every five minutes after:
295
322
  // so restart the server.
296
323
  async scheduled() {
297
- const exited = new Promise((done) => server.child.once('exit', done));
298
- server.daemon = false;
299
- process.kill(-server.child.pid!, 'SIGTERM');
300
- await exited;
324
+ await halt(server);
325
+ server = startServer();
326
+ await until('the restarted server', async () => (await fetch(`${origin}/livez`)).ok, 30, alive);
327
+ },
328
+ async restart() {
329
+ await halt(server);
330
+ await halt(vault);
331
+ vault = await startVault();
301
332
  server = startServer();
302
333
  await until('the restarted server', async () => (await fetch(`${origin}/livez`)).ok, 30, alive);
303
334
  },
335
+ serving: () => ({ vault: vault.child.pid!, server: server.child.pid! }),
304
336
  database: async () => sqlite(database),
305
337
  runtime: null,
306
338
  vaultRuntime: null,