@coffre/conformance 0.15.0 → 0.16.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.
@@ -1,40 +1,47 @@
1
1
  // What is written down: every value opened, by the vault that opened it;
2
- // every version stored, naming the vault's wrap of its key; no value without
3
- // its entry; and a log changed behind coffre's back is caught.
4
- import { createHash, randomBytes } from 'node:crypto';
5
-
6
- import type { AuditEntryView, CoffreClient } from '@coffre/client';
2
+ // every version stored, naming the vault's wrap of its key; and no value
3
+ // without its entry; and each entry the audit page lists proven against the
4
+ // head the vault signed. 0.14's checks that a log changed behind coffre's
5
+ // back is caught asked `GET /audit/verification`, which 0.16 drops: the
6
+ // page's proofs and `coffre verify --full` read the log now
7
+ // (docs/design/0.16.md, "The server").
8
+ import { judgeProofs, type AuditEntryView, type CoffreClient, type ProofJudgment } from '@coffre/client';
7
9
 
8
10
  import { using } from '../database.ts';
9
11
  import type { Deployment } from '../harness.ts';
10
- import { expect, until } from '../report.ts';
11
- import { appendOnlyLifted, entryFields, insertRow, LOG_REFUSAL, logRefuses, query } from './storage.ts';
12
- import { DEV, PROJECT, valuesIn, type Canaries, type People } from './people.ts';
12
+ import { expect, until, PUBLISHED_SECONDS } from '../report.ts';
13
+ import { LOG_REFUSAL, logBodies, logRefuses, query, type Body } from './storage.ts';
14
+ import { canary, DEV, PROJECT, replaced, valuesIn, type Canaries, type People } from './people.ts';
13
15
 
14
16
  /**
15
17
  * Every returned value read, committed by the vault under the response's
16
18
  * operation: one entry for the place, which lists each secret of a read of
17
- * several, and which a look for any one of them finds. With 0.15's log, each
18
- * value read is its use (`usesAudited`).
19
+ * several, and which a look for any one of them finds.
19
20
  */
20
21
  export async function revealAudited(deployment: Deployment, { admin, reader }: People, canaries: Canaries, purpose: 'reveal' | 'run'): Promise<string> {
21
22
  const path = purpose === 'reveal' ? `${DEV}/API_KEY` : DEV;
22
- const asked = Date.now();
23
+ // 0.16 logs the first use of a version in a minute, by credential and purpose (docs/design/0.16.md, "What it
24
+ // promises"): a version the reader read a moment ago gets no new entry. Each value read is written afresh first.
25
+ const fresh = Object.fromEntries(Object.keys(purpose === 'reveal' ? { API_KEY: '' } : valuesIn(canaries, DEV)).map((key) => [key, canary()]));
26
+ await admin.api.secrets.set(DEV, fresh);
27
+ for (const [key, value] of Object.entries(fresh)) replaced(canaries, `${DEV}/${key}`, value);
23
28
  const { operationId, values } = await reader.api.secrets.reveal(path);
24
29
  expect(typeof operationId === 'string' && operationId.length > 0, 'the read returned no operation id');
25
30
  const expected = purpose === 'reveal' ? { API_KEY: canaries[`${DEV}/API_KEY`] } : valuesIn(canaries, DEV);
26
31
  expect(JSON.stringify(Object.entries(values).sort()) === JSON.stringify(Object.entries(expected).sort()), 'the read returned different values', values);
27
- // With 0.15's log, each value read is its use, listed once the log has sealed it and the vault's projector has gone
28
- // that far (T166).
29
- if ((await admin.api.audit.verifyTrees()).kept) return usesAudited(deployment, { admin, reader }, Object.keys(expected), purpose, asked);
30
- const entries = await everyAuditEntry(admin.api);
31
- const bundle = entries.filter((entry) => entry.operationId === operationId);
32
+ // 0.16 answers a change or a read with its request's id as its operation, and its entry names that request. The page
33
+ // lists what the signer has published, within its minute.
34
+ let bundle: AuditEntryView[] = [];
35
+ await until('the read on the audit page', async () => {
36
+ bundle = (await everyAuditEntry(admin.api)).filter((entry) => entry.requestId === operationId);
37
+ return bundle.length > 0;
38
+ }, PUBLISHED_SECONDS).catch(() => undefined);
32
39
  expect(bundle.length === 1, 'the read of one place is not logged as one entry', bundle);
33
40
  const [entry] = bundle as [AuditEntryView];
34
41
  const read = entry.secrets ?? [{ key: entry.key, version: entry.version }];
35
42
  expect(JSON.stringify(read.map((secret) => secret.key).sort()) === JSON.stringify(Object.keys(expected).sort()), 'the read does not list each value once', entry);
36
43
  expect(entry.author === 'vault' && entry.action === 'secret.read' && entry.decision === 'allow', "the read's entry is not the vault's allowed read", entry);
37
- expect(entry.metadata.purpose === purpose && entry.requestId !== null, 'the read does not name its request and its purpose', entry);
44
+ expect(entry.metadata.purpose === purpose, 'the read does not name its purpose', entry);
38
45
  const { keys: listed } = await admin.api.secrets.list(DEV);
39
46
  for (const secret of read) {
40
47
  expect(secret.version === listed.find((key) => key.key === secret.key)?.version, `the entry for ${String(secret.key)} names another version`, entry);
@@ -43,16 +50,14 @@ export async function revealAudited(deployment: Deployment, { admin, reader }: P
43
50
  expect(own.entries.some((found) => found.seq === entry.seq), `the read is not in ${String(secret.key)}'s log`, own.entries.slice(0, 5));
44
51
  }
45
52
  await using(deployment.database(), async (sql) => {
46
- const rows = await query<{ author: string; action: string; decision: string; request_id: string | null; secret_id: string | null; metadata: string }>(sql,
47
- 'SELECT author, action, decision, request_id, secret_id, metadata FROM audit_log WHERE operation_id = $1', [operationId]);
53
+ const rows = (await logBodies(sql)).filter((body) => body.request === operationId);
48
54
  expect(rows.length === 1, 'the committed read is not one entry', rows);
49
- const [row] = rows as [(typeof rows)[number]];
50
- expect(row.author === 'vault' && row.action === 'secret.read' && row.decision === 'allow' && row.request_id !== null, 'the committed read is not the vault release', row);
51
- const metadata = JSON.parse(row.metadata) as { purpose: string; version?: number; secrets?: { secretId: string; version: number }[] };
52
- const committed = metadata.secrets ?? [{ secretId: row.secret_id!, version: metadata.version! }];
53
- expect(metadata.purpose === purpose && committed.length === Object.keys(expected).length, 'the committed read does not name its purpose and each secret', row);
55
+ const [row] = rows as [Body];
56
+ const committed = (row.detail.keys ?? []) as { secret: string; version: number }[];
57
+ expect(row.action === 'secret.read' && row.detail.refused === undefined, 'the committed read is not the vault release', row);
58
+ expect(row.detail.purpose === purpose && committed.length === Object.keys(expected).length, 'the committed read does not name its purpose and each secret', row);
54
59
  for (const secret of committed) {
55
- const [current] = await query<{ current_version: number }>(sql, 'SELECT current_version FROM secrets WHERE id = $1', [secret.secretId]);
60
+ const [current] = await query<{ current_version: number }>(sql, 'SELECT current_version FROM secrets WHERE id = $1', [secret.secret]);
56
61
  expect(current?.current_version === secret.version, 'the committed read names another version', row);
57
62
  }
58
63
  });
@@ -60,323 +65,212 @@ export async function revealAudited(deployment: Deployment, { admin, reader }: P
60
65
  }
61
66
 
62
67
  /**
63
- * How long before a read 0.15's log may hold the use it rode on: a repeat
64
- * within a minute rides on the first one's entry (a cover, R53), and the
65
- * deployment's clock may stand a little off the harness's.
68
+ * Each entry the audit page lists is held to the head the vault vouched for
69
+ * in the page's own call (docs/design/0.16.md, "The log's head, when
70
+ * asked"): every entry the head numbers proves against it, only those
71
+ * past it are not proven, and the page's stretch lacks none of either tree.
72
+ * What 0.14 asked of `GET /audit/verification`, which 0.16 drops; judged as
73
+ * `coffre verify instance` judges it (`judgeProofs`). The signer signs on
74
+ * its own, so a head stands within its minute.
66
75
  */
67
- const COVERED_MS = 90_000;
68
-
69
- /** How long a read may wait to be listed: the checkpointer's tick seals it, then the projector's lists it, each a tick or so. */
70
- const LISTED_WITHIN_MS = 20_000;
76
+ export async function auditProven({ admin }: People): Promise<string> {
77
+ let judged: ProofJudgment | undefined;
78
+ // A batch the signer publishes, or a pruning committed, between the vault
79
+ // reading its head and the app reading the log can leave a page honestly
80
+ // racing (`judgeProofs`): asked again until it is not, and judged as it
81
+ // last read if it still is.
82
+ await until('a page past the race between its head and its log', async () => {
83
+ judged = judgeProofs(await admin.api.audit.list({ detail: '1', limit: 200 }));
84
+ return !judged.racing;
85
+ }, PUBLISHED_SECONDS).catch((error: unknown) => {
86
+ if (judged === undefined) throw error;
87
+ });
88
+ const { head, proven, failed, misplaced, missing } = judged!;
89
+ expect(head !== null, 'the audit page has no head');
90
+ expect(failed.length === 0, 'entries the head numbers do not prove against it', failed.slice(0, 5));
91
+ expect(misplaced.length === 0, 'an entry the head numbers is not proven, or one past it is', { head, misplaced: misplaced.slice(0, 5) });
92
+ expect(missing !== null && missing.length === 0, "the page's stretch of the log lacks entries, or the page cannot say", { head, missing: missing?.slice(0, 5) });
93
+ expect(proven > 0, 'no entry on the page is proven', { head });
94
+ return `${proven} entries proven against the head of ${head.size}, signed at ${head.signedAt}, and none missing`;
95
+ }
71
96
 
72
97
  /**
73
- * Whether `probe` holds within `LISTED_WITHIN_MS`, asked every quarter of a
74
- * second: what a check of 0.15's reads waits for, as a read is listed once
75
- * the log has sealed it and the vault's projector has gone that far (T166).
98
+ * The audit page is sent with what its proofs found, rendered by the
99
+ * server, never a verdict to come: each entry's row says its proof in
100
+ * `data-proof` (`proven`, `unproven` or `failed`), which the page's port
101
+ * writes; at least one row proves, none fails, and a log that proves is
102
+ * not shown as one coffre could not check.
76
103
  */
77
- export async function listed(probe: () => Promise<boolean>): Promise<boolean> {
78
- for (const deadline = Date.now() + LISTED_WITHIN_MS; ;) {
79
- if (await probe()) return true;
80
- if (Date.now() >= deadline) return false;
81
- await new Promise((resolve) => setTimeout(resolve, 250));
82
- }
104
+ export async function auditPageProofs({ admin }: People): Promise<string> {
105
+ let status = 0;
106
+ let html = '';
107
+ let proofs: string[] = [];
108
+ // The race `auditProven` waits out, in the page's own call: a batch signed
109
+ // between its head and its rows can leave every row shown honestly
110
+ // unproven. So a page of rows none of which proves is asked again; an
111
+ // empty one, a failed proof or an error is final.
112
+ await until('an audit page with a row its head numbers', async () => {
113
+ const page = await admin.browser.fetch('/audit');
114
+ status = page.status;
115
+ html = await page.text();
116
+ proofs = [...html.matchAll(/data-proof="([a-z]+)"/g)].map((match) => match[1]);
117
+ return status !== 200 || proofs.length === 0 || proofs.some((proof) => proof !== 'unproven');
118
+ }, PUBLISHED_SECONDS);
119
+ expect(status === 200 && !html.includes('Verifying'), 'the audit page was not sent with its proofs', { status });
120
+ const proven = proofs.filter((proof) => proof === 'proven').length;
121
+ expect(proven > 0, "the audit page's rows say no proof, or none proven", { status, proofs: proofs.slice(0, 10) });
122
+ expect(!proofs.includes('failed'), 'the audit page shows an entry whose proof failed', { proofs: proofs.slice(0, 10) });
123
+ expect(!html.includes('could not check the log'), 'the audit page says coffre could not check the log', { status });
124
+ return `the audit page sent with its proofs, rendered by the server: ${proven} of ${proofs.length} rows proven`;
83
125
  }
84
126
 
127
+ /** A row of `log` as the database holds it: its bigints as text, put back as they were. */
128
+ type LogRow = { seq: string; kept: boolean; leaf: string; body: string };
129
+
85
130
  /**
86
- * Every value read, with 0.15's log, where a read is a use of each key,
87
- * which names no operation (R53, T166): the reader's newest read of each,
88
- * for the purpose, made by this read or by one within the minute before it
89
- * that this rode on, listed within `LISTED_WITHIN_MS`, the vault's and
90
- * vouched for, of the version the key holds, in the key's own log, and the
91
- * use the vault wrote, as its projector listed it.
131
+ * The newest signed entry of a tree cut out of the log, by the database's
132
+ * owner (docs/design/0.16.md, "What the minute's run checks of the log"):
133
+ * the deployed signer's next check, within its minute, says `cut` of that
134
+ * tree in its alert, in the processes' output, and the audit page proves
135
+ * the entry nowhere, the gap a hole in its stretch. Put back, the page
136
+ * proves it again. The change cut first, then the use, whose finding says
137
+ * the alarm ran a later check. Each tree is cut once in a run, so no
138
+ * earlier finding can stand for it. That a finding put right is let go and
139
+ * said again is the vault's own tests' (signer-host.test.ts). An entry cut
140
+ * further back, or the newest once another is signed after it, is the
141
+ * re-check's, which reads the log a day after its last walk (D222): not
142
+ * within a run (docs/conformance.md).
92
143
  */
93
- async function usesAudited(
94
- deployment: Deployment, { admin, reader }: Pick<People, 'admin' | 'reader'>, keys: string[], purpose: 'reveal' | 'run', asked: number,
95
- ): Promise<string> {
96
- const { keys: held } = await admin.api.secrets.list(DEV);
97
- // Of the key at dev itself: not a read of another place's key of that name, as through dev's reference into prod.
98
- const readOf = (entries: AuditEntryView[], key: string) => entries.find((found) => found.log === '0.15' && found.action === 'secret.read' && found.actorId === reader.email && found.key === key
99
- && `${found.project}/${found.environment}` === DEV && found.metadata.via === undefined && found.metadata.purpose === purpose && Date.parse(found.occurredAt) >= asked - COVERED_MS);
100
- let entries = await everyAuditEntry(admin.api);
101
- await listed(async () => keys.every((key) => readOf(entries, key) !== undefined) || ((entries = await everyAuditEntry(admin.api)), false));
102
- const read = keys.map((key) => {
103
- const entry = readOf(entries, key);
104
- expect(entry !== undefined, `the read of ${key} is not listed in 0.15's log within ${LISTED_WITHIN_MS / 1000} s`, entries.slice(0, 5));
105
- expect(entry.author === 'vault' && entry.decision === 'allow' && entry.forged !== true, `the read of ${key} is not the vault's allowed read`, entry);
106
- expect(entry.version === held.find((found) => found.key === key)?.version, `the entry for ${key} names another version`, entry);
107
- return entry;
108
- });
109
- // On the reader's own page, whole: the read may ride on a cover from the minute before. Not on the key's: 0.15 lists
110
- // reads on the whole log and by member, never by place (D160), and the key's page says so.
111
- const theirs = await everyAuditEntry(admin.api, { actor: reader.member });
112
- for (const entry of read) {
113
- expect(theirs.some((found) => found.log === '0.15' && found.seq === entry.seq), `the read of ${String(entry.key)} is not on ${reader.email}'s page`, { read: entry, newest: theirs.slice(0, 5) });
114
- const page = await admin.api.audit.list({ path: `${DEV}/${String(entry.key)}`, sealed: '1', limit: 1 });
115
- expect(page.readsOmitted === 'path', `${String(entry.key)}'s page does not say where its reads are listed`, page.readsOmitted);
116
- }
144
+ export async function logCut(deployment: Deployment, { admin }: People): Promise<string> {
145
+ const said = (tree: string) => deployment.output().split(`- cut ${tree}:`).length - 1;
146
+ const out: LogRow[] = [];
147
+ const seqs = new Set<number>();
117
148
  await using(deployment.database(), async (sql) => {
118
- for (const entry of read) {
119
- const rows = await query<{ secret: string }>(sql, 'SELECT secret FROM log_uses WHERE number = $1', [entry.seq]);
120
- expect(rows.length === 1 && String(rows[0]!.secret) === entry.metadata.secretId, 'the read is not the use the vault wrote', { entry, rows });
121
- const projected = await query<{ secret: string; principal: string }>(sql, 'SELECT secret, principal FROM log_reads WHERE number = $1', [entry.seq]);
122
- expect(projected.length === 1 && String(projected[0]!.secret) === entry.metadata.secretId && projected[0]!.principal === `user:${reader.email}`, "the read is not the use as the vault's projector listed it", { entry, projected });
149
+ const cut = async (kept: boolean): Promise<LogRow> => {
150
+ // Nothing waits: an entry signed after the cut would end its tree again, and leave the cut to the walk.
151
+ await until('nothing waiting to be signed', async () => (await query(sql, 'SELECT 1 FROM log_pending LIMIT 1')).length === 0, PUBLISHED_SECONDS);
152
+ const tree = kept ? 'kept' : 'aging';
153
+ expect(said(tree) === 0, `the signer named a cut of the ${tree} tree before one was made`);
154
+ const [row] = await query<LogRow>(sql, 'DELETE FROM log WHERE kept = $1 AND leaf = (SELECT max(leaf) FROM log WHERE kept = $1) RETURNING seq::text, kept, leaf::text, body', [kept]);
155
+ expect(row !== undefined, `the log has no ${tree} entry to cut`);
156
+ out.push(row);
157
+ await until(`the signer's alert naming - cut ${tree}:`, async () => said(tree) > 0, PUBLISHED_SECONDS);
158
+ // The page's stretch lacks it, by its tree's count, and lists it nowhere as proven.
159
+ const page = await admin.api.audit.list({ detail: '1', limit: 200 });
160
+ const seq = Number(row.seq);
161
+ seqs.add(seq);
162
+ const hole = page.holes?.some((gap) => (gap.above === null || gap.above > seq) && (gap.below === null || gap.below < seq) && gap[tree] > 0);
163
+ expect(page.entries.every((entry) => entry.seq !== seq || entry.proof !== 'proven'), 'the audit page proves an entry cut out of the log', { seq });
164
+ expect(hole === true, "the audit page's stretch does not lack the entry cut out of the log", { seq, holes: page.holes });
165
+ return row;
166
+ };
167
+ const putBack = async (row: LogRow) => {
168
+ await query(sql, 'INSERT INTO log (seq, kept, leaf, body) VALUES ($1, $2, $3, $4) ON CONFLICT DO NOTHING', [row.seq, row.kept, row.leaf, row.body]);
169
+ out.splice(out.indexOf(row), 1);
170
+ };
171
+ try {
172
+ await putBack(await cut(true));
173
+ await putBack(await cut(false));
174
+ } finally {
175
+ for (const row of [...out]) await putBack(row);
123
176
  }
124
177
  });
125
- return `${read.length} values read by the vault for ${purpose}, each its use in 0.15's log`;
126
- }
127
-
128
- /**
129
- * Verification must reach the stored head, not just some intact prefix:
130
- * the page's, from the newest checkpoint, and the walk from the first
131
- * entry, a stretch a request, which ends where it does. And the audit page
132
- * is sent with its verdict, rendered by the server, never a badge to come.
133
- */
134
- export async function verification(deployment: Deployment, { admin }: People): Promise<string> {
135
- const anchored = await admin.api.audit.verify();
136
- expect(anchored.ok && anchored.remaining === 0, 'the log does not verify from its newest checkpoint', anchored);
137
- const result = await admin.api.audit.verifyWhole();
138
- expect(result.ok && result.remaining === 0, 'the full log does not verify to its end', result);
139
- expect(result.through !== null && anchored.through !== null && result.through >= anchored.through, 'the walk from the first entry stopped short', { anchored, result });
140
- await using(deployment.database(), async (sql) => {
141
- const [head] = await sql.query<{ next_seq: string | number; head_hash: Uint8Array }>('SELECT next_seq, head_hash FROM audit_chain_head');
142
- const [last] = await sql.query<{ seq: string | number; hash: Uint8Array }>('SELECT seq, hash FROM audit_log ORDER BY seq DESC LIMIT 1');
143
- expect(head !== undefined && last !== undefined, 'the head or newest entry is missing');
144
- expect(BigInt(head.next_seq) === BigInt(last.seq) + 1n, 'the head does not name the newest entry');
145
- expect(Buffer.from(head.head_hash).equals(Buffer.from(last.hash)), 'the head has another hash');
146
- expect(result.through !== null && BigInt(result.through) === BigInt(last.seq), 'verification stopped before the head', { result, head: head.next_seq });
147
- expect(BigInt(result.entries) === BigInt(head.next_seq), 'verification did not count every entry', result);
178
+ // Every entry back, and proven against the head again.
179
+ let judged: ProofJudgment | undefined;
180
+ let listed: AuditEntryView[] = [];
181
+ await until('a page past the race between its head and its log', async () => {
182
+ const page = await admin.api.audit.list({ detail: '1', limit: 200 });
183
+ [judged, listed] = [judgeProofs(page), page.entries];
184
+ return !judged.racing;
185
+ }, PUBLISHED_SECONDS).catch((error: unknown) => {
186
+ if (judged === undefined) throw error;
148
187
  });
149
- // 0.15's log, where the deployment keeps it (plan PR 8): its newest checkpoint and every entry since, as the audit
150
- // page checks it. A deployment of 0.14's log alone has none (`kept` false).
151
- const trees = await admin.api.audit.verifyTrees();
152
- expect(!trees.kept || trees.problems.length === 0, "0.15's log does not verify from its newest checkpoint", trees);
153
- // Not checked to its head, as 0.14's checkpoints catching up above: a checkpointer behind, no break of the log, and
154
- // no pass either (review r4 of W240).
155
- expect(!trees.kept || !trees.capped, "0.15's log is not checked to its head: more entries wait above its newest checkpoint than a check reads, the checkpointer behind", trees);
156
- // And the reads the pages list, held to the uses it sealed, from the first, as `coffre verify log` holds them (T166).
157
- const reads = await admin.api.audit.verifyReads();
158
- expect(!reads.kept || reads.ok, "the reads listed are not the uses 0.15's log sealed", reads);
159
- const page = await admin.browser.fetch('/audit');
160
- const html = await page.text();
161
- expect(page.status === 200 && html.includes('chain-seal') && !html.includes('Verifying'), 'the audit page was not sent with its verification result', { status: page.status });
162
- const sealed = !trees.kept ? '' : trees.checkpoint === null ? "; 0.15's log, nothing sealed yet" : `; 0.15's log from its checkpoint up to ${trees.checkpoint.upto}`;
163
- return `${result.entries} entries verified through the head at ${result.through}${sealed}, and the audit page sent with its result`;
188
+ const { failed, misplaced, missing } = judged!;
189
+ const unproven = [...seqs].filter((seq) => !listed.some((entry) => entry.seq === seq && entry.proof === 'proven'));
190
+ expect(unproven.length === 0 && failed.length === 0 && misplaced.length === 0 && missing !== null && missing.length === 0, 'the entries put back are not proven again', { unproven, failed: failed.slice(0, 5), misplaced: misplaced.slice(0, 5), missing });
191
+ return "the newest change and use cut out of the log, each named in the signer's alert and proven nowhere on the audit page; put back, proven again";
164
192
  }
165
193
 
166
194
  /**
167
- * Each heartbeat has the vault sign the log up to its last entry, in an
168
- * entry of its own, after checking that the prefix it signed last is still
169
- * there. Two, so the second covers the first; then the log is verified from
170
- * its first entry, every checkpoint with it.
195
+ * Every version stored is the vault's write: one entry of the log names
196
+ * it, its secret and its number, among the keys the change wrote: a
197
+ * `secret.write` or `secret.loosen`, or the `environment.create` of a fork
198
+ * that copied it, naming who wrote it and the request. 0.14's app and vault each logged their
199
+ * half of a write, the app's naming the vault's seal of its key; in 0.16
200
+ * the vault writes the version and its one entry together, and the app
201
+ * writes nothing (docs/design/0.16.md, "Four rules").
171
202
  */
172
- export async function checkpoints(deployment: Deployment, { admin }: People): Promise<string> {
173
- const before = await admin.api.audit.verify();
174
- expect(before.ok, 'the log does not verify', before);
175
- await deployment.scheduled();
176
- await deployment.scheduled();
177
- const after = await admin.api.audit.verify();
178
- expect(after.ok && after.checkpoint !== null, 'the audit log is not verified and checkpointed', after);
179
- expect(after.checkpoint.seq > (before.through ?? -1), 'the checkpoint does not cover what was written before it', { before, after });
180
- const { entries } = await admin.api.audit.list({ detail: '1', limit: 20 });
181
- const signed = entries.filter((entry) => entry.action === 'audit.checkpoint' && entry.decision === 'allow');
182
- expect(
183
- signed.length >= 2 && signed.every((entry) => entry.author === 'vault' && entry.detail),
184
- "the checkpoints are not the vault's entries, hidden as detail",
185
- signed,
186
- );
187
- const [newest, previous] = signed as [AuditEntryView, AuditEntryView];
188
- expect(newest.metadata.seq === after.checkpoint.seq, 'the newest checkpoint entry is not the one verified', { signed, after });
189
- expect(previous.seq <= after.checkpoint.seq, 'the newest checkpoint does not cover the one before it', signed);
190
- return `${after.entries} entries verified through ${after.through}, checkpointed at ${after.checkpoint.seq}`;
191
- }
203
+ const WRITES = new Set(['secret.write', 'secret.loosen', 'environment.create']);
192
204
 
193
- /**
194
- * Every version the app says it stored names the vault's entry for the key
195
- * it was sealed with, `key.wrap` for a write and `key.rewrap` for a restore,
196
- * a mark and a loosening:
197
- * the same member, request and operation, the same secret and version. And
198
- * every value read is the vault's to log: the app keeps no reads of its own.
199
- */
200
205
  export async function writesAgree(deployment: Deployment): Promise<string> {
201
206
  return using(deployment.database(), async (sql) => {
202
- const entries = await sql.query<{
203
- seq: number | string; author: string; action: string; decision: string; actor: string; metadata: string;
204
- project_id: string | null; environment_id: string | null; secret_id: string | null;
205
- request_id: string | null; operation_id: string | null; related_seq: string | number | null;
206
- }>('SELECT * FROM audit_log ORDER BY seq');
207
- const bySeq = new Map(entries.map((entry) => [String(entry.seq), entry]));
208
- const writes = entries.filter((entry) => entry.author === 'app' && entry.decision === 'allow'
209
- && (entry.action === 'secret.write' || entry.action === 'secret.restore' || entry.action === 'secret.loosen'));
210
- for (const entry of entries.filter((row) => row.author === 'app' && row.decision === 'allow')) {
211
- expect(entry.action !== 'secret.read', 'the app logged an allowed read of a value', entry);
212
- }
213
- for (const entry of writes) {
214
- // A write of a new value wraps a new key; a restore, a mark (which says the version it `kept`) and a loosening rewrap one.
215
- const seal = entry.action === 'secret.write' && JSON.parse(entry.metadata).kept === undefined ? 'key.wrap' : 'key.rewrap';
216
- const key = entry.related_seq === null ? undefined : bySeq.get(String(entry.related_seq));
217
- expect(key !== undefined && key.author === 'vault' && key.action === seal && key.decision === 'allow',
218
- `a ${entry.action} names no ${seal} of the vault's`, { entry, key });
219
- for (const field of ['actor', 'request_id', 'operation_id', 'project_id', 'environment_id'] as const) {
220
- expect(entry[field] === key[field], `the write and its seal have different ${field}`, { entry, key });
207
+ const written = new Map<string, number>();
208
+ for (const body of await logBodies(sql)) {
209
+ if (!WRITES.has(body.action) || body.detail.refused !== undefined) continue;
210
+ // Each is someone's, through one request the server passed the vault (docs/design/0.16.md, "The server").
211
+ const named = (field: unknown) => typeof field === 'string' && field !== '';
212
+ expect(named(body.actor) && named(body.request), `a committed ${body.action} names no actor or no request`, body);
213
+ for (const line of (body.detail.keys ?? []) as { secret?: string; version?: number }[]) {
214
+ if (line.version === undefined) continue;
215
+ const name = `${line.secret} ${line.version}`;
216
+ written.set(name, (written.get(name) ?? 0) + 1);
221
217
  }
222
- expect(entry.operation_id !== null && entry.request_id !== null, 'the committed write has no operation or request', entry);
223
- const metadata = JSON.parse(entry.metadata) as { version: number; key: string };
224
- const sealed = JSON.parse(key.metadata) as { secretId?: string; version: number; subject: string };
225
- expect((sealed.secretId ?? key.secret_id) === entry.secret_id && sealed.version === metadata.version
226
- && sealed.subject.split('/').at(-1) === metadata.key, 'the write names a seal of another secret or version', { entry, key });
227
218
  }
228
219
  const versions = await query<{ secret_id: string; version: number }>(sql,
229
220
  `SELECT v.secret_id, v.version FROM secret_versions v JOIN secrets s ON s.id = v.secret_id
230
221
  JOIN projects p ON p.id = s.project_id WHERE p.slug = $1`, [PROJECT]);
231
222
  expect(versions.length > 0, 'no stored versions were inspected');
232
223
  for (const version of versions) {
233
- const matching = writes.filter((entry) => entry.secret_id === version.secret_id
234
- && (JSON.parse(entry.metadata) as { version: number }).version === version.version);
235
- expect(matching.length === 1, 'a stored version has no unique app write entry', { version, matching });
224
+ const entries = written.get(`${version.secret_id} ${version.version}`) ?? 0;
225
+ expect(entries === 1, 'a stored version has no unique write entry', { version, entries });
236
226
  }
237
- return `${versions.length} stored versions, each with one app write naming the vault's seal in the same operation`;
227
+ return `${versions.length} stored versions, each written by one entry of the vault's, naming its actor and request`;
238
228
  });
239
229
  }
240
230
 
241
231
  /**
242
- * No value leaves without its entry: with 0.15's log refusing uses (a
243
- * trigger, as a full disk or a lost connection would), the vault's use,
244
- * which writes its row in the statement that decides it, fails, so a
245
- * reveal fails and carries nothing. Of a key no one has read yet: a key's
246
- * use within a minute of the last is that one's, its isolate's cover, and
247
- * writes nothing again.
232
+ * No value leaves without its entry: with the audit log refusing writes (a
233
+ * trigger, as a full disk or a lost connection would), the vault cannot log
234
+ * the read, so a reveal fails and carries nothing. The vault logs a
235
+ * credential's read of a version once a minute (docs/design/0.16.md, "What
236
+ * it promises"), so the reveal asks for a key written just before, which no
237
+ * entry has said yet; it is archived after.
248
238
  */
239
+ const UNLOGGED = 'NO_AUDIT_NO_VALUE';
240
+
249
241
  export async function noAuditNoValue(deployment: Deployment, { admin }: People, canaries: Canaries): Promise<string> {
250
242
  let status = 0;
251
- const key = `UNLOGGED_${randomBytes(4).toString('hex').toUpperCase()}`;
252
- const value = `coffre-canary-${randomBytes(10).toString('hex')}`;
253
- await admin.api.secrets.set(DEV, { [key]: value });
243
+ // Not a place's value for the checks after (it is gone at the end), but one for the scans to look for.
244
+ const value = canary();
245
+ canaries[`unlogged ${DEV}/${UNLOGGED}`] = value;
246
+ await admin.api.secrets.set(DEV, { [UNLOGGED]: value });
254
247
  await using(deployment.database(), (sql) => logRefuses(sql, async () => {
255
248
  const before = deployment.output().split(LOG_REFUSAL).length;
256
- const response = await admin.browser.send('POST', '/api/reveals', { path: `${DEV}/${key}` });
249
+ const response = await admin.browser.send('POST', '/api/reveals', { path: DEV });
257
250
  const text = await response.text();
258
251
  status = response.status;
259
252
  const body = JSON.parse(text) as { error: string; message: string; reason?: string };
260
253
  expect(status === 500 && body.error === 'internal_error' && body.reason === undefined,
261
254
  'the unlogged reveal did not fail as internal_error', { status, body });
262
255
  expect(body.message === 'something went wrong; see the server log', 'the audit failure exposed another reason', body);
263
- expect(![value, ...Object.values(canaries)].some((canary) => text.includes(canary)), 'a reveal that could not be logged carried a value', text);
256
+ expect(!text.includes(value), 'a reveal that could not be logged carried the value it could not log', text);
257
+ expect(!Object.values(canaries).some((known) => text.includes(known)), 'a reveal that could not be logged carried a value', text);
264
258
  // A lost vault gives the same public error; the trigger must be its cause.
265
259
  await until('the injected audit refusal in the processes\' output', async () => deployment.output().split(LOG_REFUSAL).length > before, 5);
266
- }, undefined, 'log_uses'));
267
- const after = await admin.api.secrets.reveal(`${DEV}/${key}`);
268
- expect(after.values[key] === value, 'reveals did not come back once the log took writes again');
269
- expect((await admin.api.secrets.reveal(DEV)).values.API_KEY === canaries[`${DEV}/API_KEY`], 'reveals did not come back once the log took writes again');
270
- return `a reveal the log would not take: ${status} internal_error, its injected cause in the log, and no value`;
271
- }
272
-
273
- /** A publicly chained entry still needs the vault's MAC. */
274
- export async function forgedVaultEntry(deployment: Deployment, { admin }: People): Promise<string> {
275
- const verify = () => admin.api.audit.verify();
276
- expect((await verify()).ok, 'the log does not verify before the forged entry');
277
- await using(deployment.database(), async (sql) => {
278
- // An entry in the vault's name, chained after the last, with a MAC made up.
279
- const [head] = await sql.query<{ next_seq: number | string; head_hash: Uint8Array }>(
280
- 'SELECT next_seq, head_hash FROM audit_chain_head',
281
- );
282
- const [writer] = await sql.query<{ key_id: string }>("SELECT key_id FROM audit_log WHERE author = 'vault' ORDER BY seq DESC LIMIT 1");
283
- expect(writer !== undefined, 'no vault key id to copy');
284
- const seq = BigInt(head.next_seq);
285
- const forged = {
286
- seq, author: 'vault', key_id: writer.key_id, occurred_at: Date.now(), actor: 'user:forger@conformance.example',
287
- action: 'secret.read', decision: 'allow', metadata: '{}',
288
- };
289
- const prevHash = Buffer.from(head.head_hash);
290
- const mac = Buffer.alloc(32, 0x41);
291
- const hash = createHash('sha256').update('coffre.audit.chain.v2').update(prevHash).update(entryFields(forged)).update(mac).digest();
292
- await insertRow(sql, 'audit_log', { ...forged, prev_hash: prevHash, mac, hash });
293
- await query(sql, 'UPDATE audit_chain_head SET next_seq = $1, head_hash = $2', [seq + 1n, hash]);
294
- let inserted;
295
- try {
296
- inserted = await verify();
297
- } finally {
298
- await appendOnlyLifted(sql, () => query(sql, 'DELETE FROM audit_log WHERE seq = $1', [seq]));
299
- await query(sql, 'UPDATE audit_chain_head SET next_seq = $1, head_hash = $2', [seq, prevHash]);
300
- }
301
- expect(!inserted.ok && inserted.author === 'vault' && inserted.failedAtSeq === Number(seq), "an entry in the vault's name, chained but not by the vault, verifies", inserted);
302
- const removed = await verify();
303
- expect(removed.ok, 'the log did not verify once the entry was gone', removed);
304
- });
305
- return 'a publicly chained vault entry with a made-up MAC was refused at its sequence';
306
- }
307
-
308
- /**
309
- * Rewrite either author's first entry in place and require a fault at that
310
- * exact sequence: from the walk from the first entry, since the page's
311
- * check goes on from the newest checkpoint and reads nothing before it.
312
- */
313
- export async function rewrittenEntry(deployment: Deployment, { admin }: People, author: 'app' | 'vault'): Promise<string> {
314
- expect((await admin.api.audit.verifyWhole()).ok, 'the log does not verify before the rewrite');
315
- let seq = 0;
316
- await using(deployment.database(), (sql) => appendOnlyLifted(sql, async () => {
317
- const [row] = await query<{ seq: string | number; actor: string }>(sql,
318
- 'SELECT seq, actor FROM audit_log WHERE author = $1 ORDER BY seq LIMIT 1', [author]);
319
- expect(row !== undefined, `no ${author} entry to rewrite`);
320
- seq = Number(row.seq);
321
- await query(sql, 'UPDATE audit_log SET actor = $1 WHERE seq = $2', ['user:nobody@conformance.example', row.seq]);
322
- try {
323
- const result = await admin.api.audit.verifyWhole();
324
- expect(!result.ok && result.failedAtSeq === Number(row.seq), 'a rewritten entry was not caught at its sequence', result);
325
- } finally {
326
- await query(sql, 'UPDATE audit_log SET actor = $1 WHERE seq = $2', [row.actor, row.seq]);
327
- }
328
- }));
329
- expect((await admin.api.audit.verifyWhole()).ok, 'the restored entry did not verify');
330
- return `entry ${seq}, the ${author}'s, rewritten: caught there, then put back`;
331
- }
332
-
333
- /** Remove an unreferenced entry, then put its exact bytes back. */
334
- export async function missingEntry(deployment: Deployment, { admin }: People, position: 'middle' | 'first' | 'batch'): Promise<string> {
335
- expect((await admin.api.audit.verify()).ok, 'the log does not verify before deletion');
336
- let seq = 0;
337
- await using(deployment.database(), (sql) => appendOnlyLifted(sql, async () => {
338
- const [row] = await sql.query(`SELECT * FROM audit_log WHERE ${position !== 'middle' ? `seq = ${position === 'first' ? 0 : 1000}` :
339
- "seq > 0 AND action = 'audit.heartbeat' AND NOT EXISTS (SELECT 1 FROM audit_log linked WHERE linked.related_seq = audit_log.seq)"} ORDER BY seq LIMIT 1`);
340
- expect(row !== undefined, 'no unreferenced entry to delete');
341
- seq = Number(row.seq);
342
- const linked = await query(sql, 'SELECT * FROM audit_log WHERE related_seq = $1', [row.seq]);
343
- await query(sql, 'UPDATE audit_log SET related_seq = NULL WHERE related_seq = $1', [row.seq]);
344
- await query(sql, 'DELETE FROM audit_log WHERE seq = $1', [row.seq]);
345
- try {
346
- const result = await admin.api.audit.verify();
347
- expect(!result.ok, 'a log with an entry missing verifies', result);
348
- if (position !== 'middle') expect(result.reason.includes(`sequence gap: expected seq ${position === 'first' ? 0 : 1000}`), 'the sequence gap was not reported', result);
349
- } finally {
350
- await insertRow(sql, 'audit_log', row);
351
- for (const entry of linked) await query(sql, 'UPDATE audit_log SET related_seq = $1 WHERE seq = $2', [entry.related_seq, entry.seq]);
352
- }
353
- }));
354
- expect((await admin.api.audit.verify()).ok, 'the restored log did not verify');
355
- const which = { first: 'the first', batch: 'the first past a page of 1,000', middle: 'a heartbeat' }[position];
356
- return `entry ${seq}, ${which}, deleted: caught, then put back`;
357
- }
358
-
359
- /** Last: keep the head, delete its newest entries, and require verification to notice. */
360
- export async function deletedTail(deployment: Deployment, { admin }: People): Promise<string> {
361
- const intact = await admin.api.audit.verify();
362
- expect(intact.ok && intact.checkpoint !== null, 'the log is not intact and checkpointed');
363
- const signed = intact.checkpoint.seq;
364
- await using(deployment.database(), (sql) => appendOnlyLifted(sql, async () => {
365
- await query(sql, 'UPDATE audit_log SET related_seq = NULL WHERE seq >= $1', [signed]);
366
- await query(sql, 'DELETE FROM audit_log WHERE seq >= $1', [signed]);
367
- }));
368
- const result = await admin.api.audit.verify();
369
- expect(!result.ok && result.author === 'app' && result.reason.includes('removed from the end'), 'the log verifies with its newest entries deleted', result);
370
- return 'the newest entries deleted, with the head left alone, were caught';
260
+ }, 'secret.read'));
261
+ const after = await admin.api.secrets.reveal(DEV);
262
+ expect(after.values.API_KEY === canaries[`${DEV}/API_KEY`] && after.values[UNLOGGED] === value, 'reveals did not come back once the log took writes again');
263
+ await admin.api.secrets.set(DEV, { [UNLOGGED]: null });
264
+ return `a reveal the audit log would not take: ${status} internal_error, its injected cause in the log, and no value`;
371
265
  }
372
266
 
373
- /** The whole log, detail included, newest first: 0.14's entries, and the sign-ins 0.15's records. */
374
- export async function everyAuditEntry(api: CoffreClient, query: { actor?: string } = {}): Promise<AuditEntryView[]> {
267
+ /** The whole log, detail included, newest first. */
268
+ export async function everyAuditEntry(api: CoffreClient): Promise<AuditEntryView[]> {
375
269
  const all: AuditEntryView[] = [];
376
- for (let before: string | undefined; ; ) {
377
- const { entries, next } = await api.audit.list({ before, limit: 500, detail: '1', sealed: '1', ...query });
270
+ for (let before: number | undefined; ; ) {
271
+ const { entries, more } = await api.audit.list({ before, limit: 500, detail: '1' });
378
272
  all.push(...entries);
379
- if (next === null) return all;
380
- before = next;
273
+ if (!more || entries.length === 0) return all;
274
+ before = entries.at(-1)!.seq;
381
275
  }
382
276
  }