@coffre/vault 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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@coffre/vault",
3
- "version": "0.9.0",
3
+ "version": "0.10.1",
4
4
  "description": "coffre's vault: keys, grants and members, in the database the app uses, as a Cloudflare Worker or on Node.",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -36,8 +36,8 @@
36
36
  },
37
37
  "dependencies": {
38
38
  "drizzle-orm": "0.45.2",
39
- "@coffre/core": "0.9.0",
40
- "@coffre/db": "0.9.0"
39
+ "@coffre/core": "0.10.1",
40
+ "@coffre/db": "0.10.1"
41
41
  },
42
42
  "devDependencies": {
43
43
  "@types/node": "26.1.1",
package/src/accounting.ts CHANGED
@@ -7,19 +7,48 @@ type Item = { item: number; secretId: string; version: number; subject: string }
7
7
  type ParsedIntent = { intentId: string; operation: string; expiresAt: number; keys: Item[] };
8
8
  /** An outcome entry: one item of its intent, or several, for a read logged once per place. */
9
9
  type ParsedOutcome = { intentId: string; relatedSeq: bigint; items: Item[] };
10
- type Intent = ParsedIntent & { entry: StoredEntry; seen: Set<number> };
10
+ type Intent = ParsedIntent & { seq: bigint; actor: string; seen: Set<number> };
11
11
  type Accounting = { ok: true; pending: number } | Extract<LogVerification, { ok: false }>;
12
+ type Broken = Extract<Accounting, { ok: false }>;
13
+
14
+ /** A key batch still under way, as a checkpoint keeps it: its intent's entry, and the items whose outcomes are logged. */
15
+ export type OpenIntent = { seq: number; intentId: string; operation: string; expiresAt: number; actor: string; keys: Item[]; seen: number[] };
16
+
17
+ /** Where the accounting stands, as a checkpoint keeps it: the batches still open, and the first fault, once found. */
18
+ export type AccountingState = { intents: OpenIntent[]; fault: { failedAtSeq: number; reason: string } | null };
12
19
 
13
20
  /**
14
21
  * Every key operation's intent and its outcomes, entry by entry, oldest
15
- * first, as the full check reads the vault's entries (`add`); then what is
16
- * still open (`result`). The chain can hold while a process died between an
22
+ * first, as a walk reads the vault's entries (`add`); then what is still
23
+ * open (`result`). The chain can hold while a process died between an
17
24
  * intent and its outcomes: only a missed deadline is a fault.
25
+ *
26
+ * A walk resumes from a checkpoint's `state()`: the batches open there, and
27
+ * nothing of those that closed. So an intent's identity is checked against
28
+ * those still open and those since, not every one the log ever held: a
29
+ * closed batch takes no more outcomes in any case, since each names its
30
+ * intent's entry.
18
31
  */
19
32
  export class KeyAccounting {
20
33
  readonly #intents = new Map<bigint, Intent>();
21
34
  readonly #ids = new Set<string>();
22
- #fault: Extract<Accounting, { ok: false }> | null = null;
35
+ #fault: Broken | null = null;
36
+
37
+ constructor(from: AccountingState = { intents: [], fault: null }) {
38
+ for (const { seq, seen, ...open } of from.intents) {
39
+ this.#intents.set(BigInt(seq), { ...open, seq: BigInt(seq), seen: new Set(seen) });
40
+ this.#ids.add(open.intentId);
41
+ }
42
+ this.#fault = from.fault === null ? null : { ok: false, ...from.fault };
43
+ }
44
+
45
+ /** Where it stands, to resume from: the batches open, oldest first. */
46
+ state(): AccountingState {
47
+ const intents = [...this.#intents.values()]
48
+ .sort((a, b) => (a.seq < b.seq ? -1 : 1))
49
+ .map(({ seq, seen, ...open }) => ({ ...open, seq: Number(seq), seen: [...seen].sort((a, b) => a - b) }));
50
+ return { intents, fault: this.#fault === null ? null : { failedAtSeq: this.#fault.failedAtSeq!, reason: this.#fault.reason } };
51
+ }
23
52
 
24
53
  add(entry: StoredEntry): void {
25
54
  if (this.#fault !== null || entry.author !== 'vault') return;
@@ -31,7 +60,7 @@ export class KeyAccounting {
31
60
  if (typeof parsed === 'string') return broken(parsed);
32
61
  if (this.#ids.has(parsed.intentId)) return broken('key intent repeats an operation identity');
33
62
  this.#ids.add(parsed.intentId);
34
- if (parsed.keys.length > 0) this.#intents.set(entry.seq, { ...parsed, entry, seen: new Set() });
63
+ if (parsed.keys.length > 0) this.#intents.set(entry.seq, { ...parsed, seq: entry.seq, actor: entry.actor, seen: new Set() });
35
64
  } else if (KEY_ACTIONS.has(entry.action) && entry.relatedSeq !== null) {
36
65
  // An outcome names its intent; a key released with no service to call has none.
37
66
  const parsed = parseOutcome(entry);
@@ -42,7 +71,7 @@ export class KeyAccounting {
42
71
  if (intent === undefined || key === undefined || intent.seen.has(outcome.item)) {
43
72
  return broken('key outcome does not identify one item of its intent');
44
73
  }
45
- if (parsed.intentId !== intent.intentId || entry.actor !== intent.entry.actor || entry.action !== intent.operation) {
74
+ if (parsed.intentId !== intent.intentId || entry.actor !== intent.actor || entry.action !== intent.operation) {
46
75
  return broken('key outcome belongs to another operation');
47
76
  }
48
77
  if (outcome.secretId !== key.secretId || outcome.version !== key.version || outcome.subject !== key.subject) {
@@ -51,7 +80,7 @@ export class KeyAccounting {
51
80
  intent.seen.add(outcome.item);
52
81
  }
53
82
  // Completed batches need no state, and another outcome for one is a duplicate.
54
- if (intent !== undefined && intent.seen.size === intent.keys.length) this.#intents.delete(intent.entry.seq);
83
+ if (intent !== undefined && intent.seen.size === intent.keys.length) this.#intents.delete(intent.seq);
55
84
  }
56
85
  }
57
86
 
@@ -65,7 +94,7 @@ export class KeyAccounting {
65
94
  const missing = intent.keys.length - intent.seen.size;
66
95
  return `key intent ${intent.intentId} is overdue: ${missing} of ${intent.keys.length} outcomes missing`;
67
96
  }).join('; ');
68
- return { ok: false, failedAtSeq: Number(overdue[0]!.entry.seq), reason };
97
+ return { ok: false, failedAtSeq: Number(overdue[0]!.seq), reason };
69
98
  }
70
99
  }
71
100
 
package/src/cloudflare.ts CHANGED
@@ -23,6 +23,7 @@ import type {
23
23
  RewrapInput,
24
24
  SetAccessInput,
25
25
  SetSettingsInput,
26
+ ChangePolicyInput,
26
27
  UnwrapInput,
27
28
  Vault,
28
29
  VerifyLogInput,
@@ -94,6 +95,8 @@ export class VaultEntrypoint extends WorkerEntrypoint implements Vault {
94
95
  async remove(input: RemoveInput) { return this.#call((vault) => vault.remove(input)); }
95
96
  async settings() { return this.#call((vault) => vault.settings()); }
96
97
  async setSettings(input: SetSettingsInput) { return this.#call((vault) => vault.setSettings(input)); }
98
+ async policy() { return this.#call((vault) => vault.policy()); }
99
+ async changePolicy(input: ChangePolicyInput) { return this.#call((vault) => vault.changePolicy(input)); }
97
100
  async checkpoint() { return this.#call((vault) => vault.checkpoint()); }
98
101
  async about() { return this.#call((vault) => vault.about()); }
99
102
  async keyChecks() { return this.#call((vault) => vault.keyChecks()); }
package/src/config.ts CHANGED
@@ -28,20 +28,8 @@ export type ResolvedVaultConfig = {
28
28
  */
29
29
  signingKeys: readonly Uint8Array[];
30
30
  bulkLimit: BulkLimit;
31
- /** `values` when left out. */
32
- people?: People;
33
31
  };
34
32
 
35
- /**
36
- * What a person's reads open (docs/design/credential-proxy.md): `values`,
37
- * as any member's; or `stand-ins`, where a person, on any credential of
38
- * theirs (a session, a personal token, an MCP connection), opens values
39
- * only for coffre to use for them, never to be handed back: the credential
40
- * proxy injects them, and the deployment's `expose` list names the few it
41
- * may pass on. Service accounts read values either way.
42
- */
43
- export type People = 'values' | 'stand-ins';
44
-
45
33
  /**
46
34
  * A key-encryption key: 32 random bytes, base64, and the id envelopes record
47
35
  * it under; or one a key service holds, such as `awsKms({ keyArn, … })`.
@@ -76,8 +64,12 @@ export type VaultConfig = {
76
64
  signingKey?: string;
77
65
  /** At most `count` data keys unwrapped per principal in any `windowMinutes`; 1000 in 15 unless set. */
78
66
  bulkLimit?: { count: number; windowMinutes: number };
79
- /** What a person's reads open (`People`): `values` unless set. */
80
- people?: People;
67
+ /**
68
+ * @deprecated Gone in 0.10, and refused: each key says who sees it now. Mark
69
+ * those no person may read sensitive (`coffre mark <env> sensitive`), and
70
+ * give people read through their roles where they should see values.
71
+ */
72
+ people?: 'gone in 0.10: mark keys sensitive (coffre mark <env> sensitive), and let roles say who reads the rest';
81
73
  } & Owners;
82
74
 
83
75
  /**
@@ -146,6 +138,12 @@ function kek(entry: Kek): { provider: KekProvider; signingKey: Buffer | null } {
146
138
 
147
139
  /** Check a deployment's vault configuration, failing on the first problem. */
148
140
  export function resolveVaultConfig(config: VaultConfig): ResolvedVaultConfig {
141
+ if ('people' in config && config.people !== undefined) {
142
+ throw new Error(
143
+ `people is gone in 0.10 (it was ${JSON.stringify(config.people)}): each key says who sees it now. Remove people, mark the keys no person may read sensitive ` +
144
+ '(coffre mark <env> sensitive, or coffre set <env>/<KEY> --sensitive), and let roles say who reads the rest (docs/design/credential-proxy.md)',
145
+ );
146
+ }
149
147
  const keks = [config.kek, ...(config.previousKeks ?? [])].map(kek);
150
148
  const [current, ...previous] = keks.map((entry) => entry.provider);
151
149
  const refs = [current, ...previous].map(({ provider, keyId }) => `${provider}:${keyId}`);
@@ -156,7 +154,6 @@ export function resolveVaultConfig(config: VaultConfig): ResolvedVaultConfig {
156
154
  owners: checkOwners(config.owners ?? config.bootstrapOwners ?? config.rootAdmins ?? []),
157
155
  signingKeys: signingKeys(config.signingKey, keks),
158
156
  bulkLimit: config.bulkLimit === undefined ? DEFAULT_BULK_LIMIT : checkBulkLimit(config.bulkLimit),
159
- people: checkPeople(config.people ?? 'values'),
160
157
  };
161
158
  }
162
159
 
@@ -184,10 +181,6 @@ function signingKeys(given: string | undefined, keks: { provider: KekProvider; s
184
181
  return [signing, ...derived].filter((key, i, all) => all.findIndex((other) => other.equals(key)) === i);
185
182
  }
186
183
 
187
- function checkPeople(people: People): People {
188
- if (people !== 'values' && people !== 'stand-ins') throw new Error(`people is 'values' or 'stand-ins'; got ${JSON.stringify(people)}`);
189
- return people;
190
- }
191
184
 
192
185
  function checkBulkLimit({ count, windowMinutes }: { count: number; windowMinutes: number }): BulkLimit {
193
186
  if (!Number.isInteger(count) || count < 1 || !(windowMinutes > 0)) {
package/src/index.ts CHANGED
@@ -3,6 +3,6 @@ export { checkpointMessage, verifyCheckpoint } from '@coffre/core/vault';
3
3
  export type { SecretContext } from '@coffre/core/envelope';
4
4
  export { awsKms, KekBadClaimError, KekUnavailableError } from '@coffre/core/kek';
5
5
  export type { AwsCredentials, AwsKmsOptions, KekProvider, WrappedDek } from '@coffre/core/kek';
6
- export type { Kek, People, VaultConfig } from './config.ts';
6
+ export type { Kek, VaultConfig } from './config.ts';
7
7
  /** The signing key a local KEK stands for, for tools that check the vault's records from outside. */
8
8
  export { derivedSigningKey } from './config.ts';
package/src/local.ts CHANGED
@@ -17,6 +17,8 @@ export const METHODS = [
17
17
  'remove',
18
18
  'settings',
19
19
  'setSettings',
20
+ 'policy',
21
+ 'changePolicy',
20
22
  'checkpoint',
21
23
  'about',
22
24
  'keyChecks',
package/src/log.ts CHANGED
@@ -2,7 +2,8 @@ import { deriveLogKey, GENESIS_HASH, verifyEntries, type LogKey, type StoredEntr
2
2
  import type { LogHead, LogVerification } from '@coffre/core/vault';
3
3
  import type { Queryable } from '@coffre/db';
4
4
 
5
- import { entriesFrom, hashAt } from './store.ts';
5
+ import { entriesFrom, hashAt, vaultEntriesAfter } from './store.ts';
6
+ import type { Walk } from './tally.ts';
6
7
 
7
8
  /**
8
9
  * The vault's half of the one audit log: its entries are the ones it
@@ -21,83 +22,113 @@ export function vaultLogKey(signingKey: Uint8Array): LogKey {
21
22
  return deriveLogKey('vault', signingKey);
22
23
  }
23
24
 
24
- /**
25
- * How far this process has verified the chain: every entry before
26
- * `nextSeq`, the last of which has `hash`, with `vaultEntries` of the
27
- * vault's among them, written under `vaultKeys`, in the order the log moved
28
- * through them (`forward`). In memory only: the database cannot vouch for
29
- * itself.
30
- */
31
- export type Anchor = { nextSeq: bigint; hash: Buffer; vaultEntries: number; vaultKeys: readonly string[] };
25
+ /** Where a walk of the log stands: the last entry it covered, -1 before the first, and that entry's hash. */
26
+ export type Position = { seq: bigint; hash: Buffer };
32
27
 
33
- /** Before the first entry: nothing verified yet. */
34
- export const UNVERIFIED: Anchor = { nextSeq: 0n, hash: GENESIS_HASH, vaultEntries: 0, vaultKeys: [] };
28
+ /** Before the first entry. */
29
+ export const START: Position = { seq: -1n, hash: GENESIS_HASH };
35
30
 
36
- /** The further of two anchors, when two calls verified at once. */
37
- export function further(a: Anchor, b: Anchor): Anchor {
38
- return b.nextSeq > a.nextSeq ? b : a;
31
+ /** The further of two positions, when two calls walked at once. */
32
+ export function further(a: Position, b: Position): Position {
33
+ return b.seq > a.seq ? b : a;
39
34
  }
40
35
 
41
36
  /** Entries read at once, at most, and no more than `BATCH_BYTES` of what they say: well within a Worker's memory, and a fifth of the round trips of 1000. */
42
37
  export const VERIFY_BATCH = 5000;
43
38
 
44
- type Verified = { verification: LogVerification; anchor: Anchor };
39
+ type Broken = Extract<LogVerification, { ok: false }>;
45
40
 
46
41
  /**
47
- * Check the chain, a batch in memory at a time, and return where it is now
48
- * verified to. Three parts:
42
+ * Walk the chain on from `from`, a batch in memory at a time, through `to`
43
+ * or the log's last entry, and no more than `limit` entries: every link and
44
+ * hash recomputed, each of the vault's entries by its MAC, its keys moving
45
+ * only forward, and each fed to `walk`. Where it got to, and whether that
46
+ * is as far as it was to go: a walk cut short by `limit` goes on from there
47
+ * another time.
49
48
  *
50
- * - `shown`, the page a reader is looking at: each entry against its own
51
- * hash and MAC;
52
- * - `anchor`, the head at the last check: still there, unchanged. A rewrite
53
- * of anything before it, chained again to hide, changes its hash;
54
- * - every entry after the anchor, or from the first.
55
- *
56
- * So a view rehashes only what is new since the last one. What it leaves
57
- * out is an entry before the anchor edited in place, not chained again, and
58
- * not on the page: a full check, which starts from `UNVERIFIED`, finds that,
59
- * as does the first view after a start, which has no anchor. `onBatch` sees
60
- * each batch once it has verified, for checks that read the same entries.
49
+ * It takes `from` as given: whether the log still holds it is the caller's
50
+ * to check, with the other heads it must hold, in one read.
61
51
  */
62
- export async function verifyChain(
52
+ export async function rehash(
63
53
  db: Queryable,
64
54
  logKeys: readonly LogKey[],
65
- shown: readonly StoredEntry[],
66
- anchor: Anchor,
67
- onBatch: (batch: readonly StoredEntry[]) => void = () => {},
68
- ): Promise<Verified> {
69
- const broken = (failedAtSeq: bigint, reason: string): Verified => ({
70
- verification: { ok: false, failedAtSeq: Number(failedAtSeq), reason: withCause(reason) },
71
- anchor,
72
- });
73
- const keys = { keys: logKeys, chainOnly: ['app' as const] };
74
-
75
- for (const row of [...shown].sort((a, b) => (a.seq < b.seq ? -1 : 1))) {
76
- const result = verifyEntries([row], { ...keys, startSeq: row.seq, startPrevHash: row.prevHash });
77
- if (!result.ok) return broken(result.failedAtSeq, result.reason);
55
+ from: Position,
56
+ walk: Walk,
57
+ { to, limit = Infinity }: { to?: bigint; limit?: number },
58
+ ): Promise<{ ok: true; at: Position; done: boolean } | Broken> {
59
+ let at = from;
60
+ for (let left = limit; ; ) {
61
+ if (to !== undefined && at.seq >= to) return { ok: true, at, done: true };
62
+ // Out of entries to walk, it is done if there are none left to: a walk that ends at its budget's last read one more.
63
+ if (left <= 0) return { ok: true, at, done: !(await entriesFrom(db, at.seq + 1n, 1)).some((entry) => to === undefined || entry.seq <= to) };
64
+ let batch = await entriesFrom(db, at.seq + 1n, Math.min(VERIFY_BATCH, left));
65
+ if (to !== undefined) batch = batch.filter((entry) => entry.seq <= to);
66
+ if (batch.length === 0) return { ok: true, at, done: to === undefined };
67
+ const result = verifyEntries(batch, { keys: logKeys, chainOnly: ['app'], startSeq: at.seq + 1n, startPrevHash: at.hash });
68
+ if (!result.ok) return { ok: false, failedAtSeq: Number(result.failedAtSeq), reason: withCause(result.reason) };
69
+ for (const entry of batch) {
70
+ const moved = entry.author === 'vault' ? walk.add(entry, logKeys[0]!.keyId) : null;
71
+ if (moved !== null) return { ok: false, failedAtSeq: Number(moved.failedAtSeq), reason: moved.reason };
72
+ }
73
+ at = { seq: result.nextSeq - 1n, hash: result.head };
74
+ left -= batch.length;
78
75
  }
76
+ }
79
77
 
80
- if (anchor.nextSeq > 0n && !(await hashAt(db, anchor.nextSeq - 1n))?.equals(anchor.hash)) {
81
- return broken(anchor.nextSeq - 1n, 'changed since the vault last verified it');
78
+ /**
79
+ * The chain again from `from`, through `to` and no more than `limit`
80
+ * entries: each hash recomputed from its entry and each link checked, and
81
+ * nothing else, so a stretch costs a hash an entry. What a checkpoint does
82
+ * in the background to the prefixes signed before (`#scrub` in vault.ts):
83
+ * each entry's MAC was checked when it was signed over, and its hash pins
84
+ * it since, so all that can change behind a signed prefix and keep the
85
+ * chain is an entry edited in place, its stored hash left, which its hash
86
+ * finds.
87
+ */
88
+ export async function relink(db: Queryable, from: Position, to: bigint, limit: number): Promise<{ ok: true; at: Position } | Broken> {
89
+ let at = from;
90
+ while (at.seq < to && limit > 0) {
91
+ const batch = (await entriesFrom(db, at.seq + 1n, Math.min(VERIFY_BATCH, limit))).filter((entry) => entry.seq <= to);
92
+ if (batch.length === 0) break;
93
+ const result = verifyEntries(batch, { keys: [], chainOnly: ['app', 'vault'], startSeq: at.seq + 1n, startPrevHash: at.hash });
94
+ if (!result.ok) return { ok: false, failedAtSeq: Number(result.failedAtSeq), reason: result.reason };
95
+ at = { seq: result.nextSeq - 1n, hash: result.head };
96
+ limit -= batch.length;
82
97
  }
98
+ return { ok: true, at };
99
+ }
83
100
 
84
- let verified = anchor;
85
- for (;;) {
86
- const batch = await entriesFrom(db, verified.nextSeq, VERIFY_BATCH);
87
- if (batch.length === 0) break;
88
- const result = verifyEntries(batch, { ...keys, startSeq: verified.nextSeq, startPrevHash: verified.hash });
89
- if (!result.ok) return broken(result.failedAtSeq, result.reason);
90
- const moved = forward(batch, verified.vaultKeys, logKeys[0].keyId);
91
- if ('failedAtSeq' in moved) return broken(moved.failedAtSeq, moved.reason);
92
- onBatch(batch);
93
- verified = {
94
- nextSeq: result.nextSeq,
95
- hash: result.head,
96
- vaultEntries: verified.vaultEntries + result.authenticated,
97
- vaultKeys: moved.keys,
98
- };
101
+ /**
102
+ * The vault's own entries after `after`, through `to` or its last, no more
103
+ * than `limit` of them, oldest first: each against its own hash, and by its
104
+ * MAC, over the link it names, its keys moving only forward, each fed to
105
+ * `walk`. The links between entries are the caller's to recompute, as the
106
+ * app does over the same stretch. The last it read, and whether that is as
107
+ * far as it was to go.
108
+ */
109
+ export async function ownEntries(
110
+ db: Queryable,
111
+ logKeys: readonly LogKey[],
112
+ after: bigint,
113
+ walk: Walk,
114
+ { to, limit = Infinity }: { to?: bigint; limit?: number },
115
+ ): Promise<{ ok: true; at: bigint; done: boolean } | Broken> {
116
+ for (let left = limit; ; ) {
117
+ if (left <= 0) return { ok: true, at: after, done: !(await vaultEntriesAfter(db, after, 1)).some((entry) => to === undefined || entry.seq <= to) };
118
+ let batch = await vaultEntriesAfter(db, after, Math.min(VERIFY_BATCH, left));
119
+ if (to !== undefined) batch = batch.filter((entry) => entry.seq <= to);
120
+ if (batch.length === 0) return { ok: true, at: after, done: true };
121
+ for (const entry of batch) {
122
+ const result = verifyEntries([entry], { startSeq: entry.seq, startPrevHash: entry.prevHash, keys: logKeys });
123
+ if (!result.ok) return { ok: false, failedAtSeq: Number(result.failedAtSeq), reason: withCause(result.reason) };
124
+ }
125
+ for (const entry of batch) {
126
+ const moved = walk.add(entry, logKeys[0]!.keyId);
127
+ if (moved !== null) return { ok: false, failedAtSeq: Number(moved.failedAtSeq), reason: moved.reason };
128
+ }
129
+ after = batch[batch.length - 1]!.seq;
130
+ left -= batch.length;
99
131
  }
100
- return { verification: { ok: true, entries: verified.vaultEntries }, anchor: verified };
101
132
  }
102
133
 
103
134
  /**
package/src/replay.ts CHANGED
@@ -141,7 +141,7 @@ function placeOf(row: StoredEntry, detail: Record<string, unknown>): Place {
141
141
  }
142
142
 
143
143
  /** A grant's place: its environment, or its project when it has none, or every project's path. */
144
- function placeKey(place: Place): string {
144
+ export function placeKey(place: Place): string {
145
145
  return place.environmentId ?? place.projectId ?? (place.environmentSlug === null ? '*' : `*/${place.environmentSlug}`);
146
146
  }
147
147