@wtfalch/keys 0.4.2 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. package/README.md +160 -5
  2. package/dist/bin/copy.d.ts +8 -4
  3. package/dist/bin/copy.js +8 -4
  4. package/dist/bin/migrations.js +21 -7
  5. package/dist/held/schema.js +1 -1
  6. package/dist/held/seal.d.ts +22 -0
  7. package/dist/held/seal.js +48 -0
  8. package/dist/held/shred.d.ts +1 -1
  9. package/dist/held/shred.js +1 -1
  10. package/dist/held-edge/index.d.ts +50 -0
  11. package/dist/held-edge/index.js +175 -0
  12. package/dist/host/audit-endpoint.d.ts +15 -0
  13. package/dist/host/audit-endpoint.js +117 -0
  14. package/dist/host/events.d.ts +26 -0
  15. package/dist/host/events.js +31 -0
  16. package/dist/host/index.d.ts +11 -0
  17. package/dist/host/index.js +11 -0
  18. package/dist/host/schema.d.ts +153 -0
  19. package/dist/host/schema.js +20 -0
  20. package/dist/issued/index.js +2 -2
  21. package/dist/issued/ratelimit.d.ts +1 -1
  22. package/dist/issued/ratelimit.js +1 -1
  23. package/dist/issued/resign.js +1 -1
  24. package/dist/issued/tables.d.ts +4 -4
  25. package/dist/issued/tables.js +4 -4
  26. package/dist/migrations/{0003_keys_shred.sql → held/0002_keys_shred.sql} +26 -31
  27. package/dist/migrations/{0004_keys_security.sql → held/0003_keys_security.sql} +36 -70
  28. package/dist/migrations/host/0001_keys_host_events.sql +73 -0
  29. package/dist/migrations/issued/0002_keys_environment.sql +29 -0
  30. package/dist/migrations/{0006_keys_usage.sql → issued/0003_keys_usage.sql} +3 -2
  31. package/dist/migrations/{0008_keys_environment_default.sql → issued/0005_keys_environment_default.sql} +1 -1
  32. package/dist/migrations/issued/0006_keys_issued_guard.sql +47 -0
  33. package/dist/migrations-dir.d.ts +19 -0
  34. package/dist/migrations-dir.js +20 -0
  35. package/dist/testing/index.d.ts +9 -0
  36. package/dist/testing/index.js +9 -0
  37. package/dist/testing/worker-stub.d.ts +55 -0
  38. package/dist/testing/worker-stub.js +180 -0
  39. package/package.json +21 -4
  40. package/dist/migrations/0005_keys_environment.sql +0 -52
  41. package/dist/migrations/0009_keys_issued_guard.sql +0 -46
  42. /package/dist/migrations/{0002_keys_held.sql → held/0001_keys_held.sql} +0 -0
  43. /package/dist/migrations/{0001_keys_issued.sql → issued/0001_keys_issued.sql} +0 -0
  44. /package/dist/migrations/{0007_keys_rate_limit.sql → issued/0004_keys_rate_limit.sql} +0 -0
@@ -1,38 +1,44 @@
1
1
  -- Security review K01/K07. Additive upgrade; do not edit copied migrations.
2
- -- Explicit relation qualification defeats caller-owned temporary relations.
3
- -- The runtime role must not own these objects or have CREATE on public.
4
- create or replace function public.keys_shred_expired()
2
+ -- Explicit relation qualification defeats caller-owned temporary relations:
3
+ -- every function here pins its search_path to the schema this file runs in,
4
+ -- followed by pg_catalog, with pg_temp last. The first statement builds that
5
+ -- path for this transaction; `set search_path from current` then records it
6
+ -- on each function. The runtime role must not own these objects or have
7
+ -- CREATE on the schema; the host's `ensureRuntimeRole` never grants it.
8
+ select set_config('search_path', quote_ident(current_schema()) || ', pg_catalog, pg_temp', true);
9
+
10
+ create or replace function keys_shred_expired()
5
11
  returns integer
6
12
  language plpgsql
7
13
  security definer
8
- set search_path = pg_catalog, public, pg_temp
14
+ set search_path from current
9
15
  as $$
10
16
  declare
11
17
  shredded integer;
12
18
  begin
13
- update public.keys_held_versions kv
19
+ update keys_held_versions kv
14
20
  set wrapped_key = null
15
- from public.keys_held_entries ke
21
+ from keys_held_entries ke
16
22
  where kv.tenant_id = ke.tenant_id
17
23
  and kv.entry_id = ke.entry_id
18
24
  and kv.wrapped_key is not null
19
25
  and (
20
26
  (kv.retired_at is not null
21
- and now() >= kv.retired_at + (public.keys_shred_delay_days() || ' days')::interval)
27
+ and now() >= kv.retired_at + (keys_shred_delay_days() || ' days')::interval)
22
28
  or (ke.revoked_at is not null
23
- and now() >= ke.revoked_at + (public.keys_shred_delay_days() || ' days')::interval)
29
+ and now() >= ke.revoked_at + (keys_shred_delay_days() || ' days')::interval)
24
30
  or (ke.tenant_archived_at is not null
25
- and now() >= ke.tenant_archived_at + (public.keys_shred_delay_days() || ' days')::interval)
31
+ and now() >= ke.tenant_archived_at + (keys_shred_delay_days() || ' days')::interval)
26
32
  );
27
33
  get diagnostics shredded = row_count;
28
34
  return shredded;
29
35
  end
30
36
  $$;
31
- revoke all on function public.keys_shred_expired() from public;
37
+ revoke all on function keys_shred_expired() from public;
32
38
 
33
- create or replace function public.keys_held_versions_guard() returns trigger
39
+ create or replace function keys_held_versions_guard() returns trigger
34
40
  language plpgsql
35
- set search_path = pg_catalog, public, pg_temp
41
+ set search_path from current
36
42
  as $$
37
43
  declare
38
44
  is_rewrap boolean;
@@ -101,7 +107,7 @@ begin
101
107
  -- with the same shape -- the trigger cannot and does not distinguish
102
108
  -- the two): the shape alone is not enough -- this row's own stored
103
109
  -- clocks, which can no longer be forged (see the file header), must
104
- -- already show public.keys_shred_delay_days() elapsed, the same predicate
110
+ -- already show keys_shred_delay_days() elapsed, the same predicate
105
111
  -- keys_shred_expired()'s WHERE clause applies.
106
112
  is_shred := old.wrapped_key is not null
107
113
  and new.wrapped_key is null
@@ -109,22 +115,22 @@ begin
109
115
  and new.retired_at is not distinct from old.retired_at
110
116
  and (
111
117
  (new.retired_at is not null
112
- and now() >= new.retired_at + (public.keys_shred_delay_days() || ' days')::interval)
118
+ and now() >= new.retired_at + (keys_shred_delay_days() || ' days')::interval)
113
119
  or exists (
114
- select 1 from public.keys_held_entries ke
120
+ select 1 from keys_held_entries ke
115
121
  where ke.tenant_id = new.tenant_id
116
122
  and ke.entry_id = new.entry_id
117
123
  and (
118
124
  (ke.revoked_at is not null
119
- and now() >= ke.revoked_at + (public.keys_shred_delay_days() || ' days')::interval)
125
+ and now() >= ke.revoked_at + (keys_shred_delay_days() || ' days')::interval)
120
126
  or (ke.tenant_archived_at is not null
121
- and now() >= ke.tenant_archived_at + (public.keys_shred_delay_days() || ' days')::interval)
127
+ and now() >= ke.tenant_archived_at + (keys_shred_delay_days() || ' days')::interval)
122
128
  )
123
129
  )
124
130
  );
125
131
 
126
132
  if not (is_rewrap or is_retire or is_shred) then
127
- raise exception 'keys_held_versions is append-only: an update must be exactly a rewrap (wrapped_key and kek_id together), a retire (retired_at NULL to non-null, once), or a shred of a row genuinely eligible for public.keys_shred_delay_days() -- refused, including un-retiring, un-shredding, shredding early, and any change to ciphertext or iv';
133
+ raise exception 'keys_held_versions is append-only: an update must be exactly a rewrap (wrapped_key and kek_id together), a retire (retired_at NULL to non-null, once), or a shred of a row genuinely eligible for keys_shred_delay_days() -- refused, including un-retiring, un-shredding, shredding early, and any change to ciphertext or iv';
128
134
  end if;
129
135
  end if;
130
136
  return new;
@@ -134,37 +140,37 @@ $$;
134
140
  -- Serialize publication before INSERT takes a snapshot of other versions.
135
141
  -- Reservations may finish out of order; current_version remains the reservation
136
142
  -- counter, not a promise that that version was successfully published.
137
- create or replace function public.keys_held_versions_publish_guard() returns trigger
143
+ create or replace function keys_held_versions_publish_guard() returns trigger
138
144
  language plpgsql
139
145
  security definer
140
- set search_path = pg_catalog, public, pg_temp
146
+ set search_path from current
141
147
  as $$
142
148
  declare
143
149
  revoked timestamptz;
144
150
  begin
145
- select revoked_at into revoked from public.keys_held_entries
151
+ select revoked_at into revoked from keys_held_entries
146
152
  where tenant_id = new.tenant_id and entry_id = new.entry_id for update;
147
153
  if revoked is not null then
148
154
  raise exception 'held entry is revoked';
149
155
  end if;
150
- if exists (select 1 from public.keys_held_versions
156
+ if exists (select 1 from keys_held_versions
151
157
  where tenant_id = new.tenant_id and entry_id = new.entry_id and version > new.version) then
152
158
  new.retired_at := clock_timestamp();
153
159
  end if;
154
160
  return new;
155
161
  end
156
162
  $$;
157
- drop trigger if exists keys_held_versions_publish on public.keys_held_versions;
158
- create trigger keys_held_versions_publish before insert on public.keys_held_versions
159
- for each row execute function public.keys_held_versions_publish_guard();
163
+ drop trigger if exists keys_held_versions_publish on keys_held_versions;
164
+ create trigger keys_held_versions_publish before insert on keys_held_versions
165
+ for each row execute function keys_held_versions_publish_guard();
160
166
 
161
- create or replace function public.keys_held_versions_retire_predecessors() returns trigger
167
+ create or replace function keys_held_versions_retire_predecessors() returns trigger
162
168
  language plpgsql
163
169
  security definer
164
- set search_path = pg_catalog, public, pg_temp
170
+ set search_path from current
165
171
  as $$
166
172
  begin
167
- update public.keys_held_versions set retired_at = clock_timestamp()
173
+ update keys_held_versions set retired_at = clock_timestamp()
168
174
  where tenant_id = new.tenant_id and entry_id = new.entry_id
169
175
  and version < new.version and retired_at is null;
170
176
  return null;
@@ -173,9 +179,9 @@ $$;
173
179
 
174
180
  -- A transaction's start time is caller-controlled by holding it open. Stamp
175
181
  -- clocks at the actual write, so long transactions cannot backdate eligibility.
176
- create or replace function public.keys_held_entries_clock_guard() returns trigger
182
+ create or replace function keys_held_entries_clock_guard() returns trigger
177
183
  language plpgsql
178
- set search_path = pg_catalog, public, pg_temp
184
+ set search_path from current
179
185
  as $$
180
186
  begin
181
187
  if tg_op = 'INSERT' then
@@ -204,43 +210,3 @@ begin
204
210
  return new;
205
211
  end
206
212
  $$;
207
-
208
- -- K03: keep issued revocations irreversible under ordinary table privileges.
209
- -- Retaining IDs also prevents deleting and reinserting a revoked signed row.
210
- create or replace function public.keys_issued_guard() returns trigger
211
- language plpgsql
212
- set search_path = pg_catalog, public, pg_temp
213
- as $$
214
- begin
215
- if tg_op in ('DELETE', 'TRUNCATE') then
216
- raise exception 'keys_issued_credentials: delete and truncate refused';
217
- end if;
218
- if tg_op = 'UPDATE' then
219
- if new.id is distinct from old.id or new.issued_by_id is distinct from old.issued_by_id
220
- or new.key_prefix is distinct from old.key_prefix then
221
- raise exception 'keys_issued_credentials: identity and lineage are immutable';
222
- end if;
223
- if old.revoked_at is not null and new.revoked_at is distinct from old.revoked_at then
224
- raise exception 'keys_issued_credentials: revocation is permanent';
225
- end if;
226
- end if;
227
- return new;
228
- end
229
- $$;
230
- drop trigger if exists keys_issued_guard_update on public.keys_issued_credentials;
231
- create trigger keys_issued_guard_update before update or delete on public.keys_issued_credentials
232
- for each row execute function public.keys_issued_guard();
233
- drop trigger if exists keys_issued_guard_truncate on public.keys_issued_credentials;
234
- create trigger keys_issued_guard_truncate before truncate on public.keys_issued_credentials
235
- for each statement execute function public.keys_issued_guard();
236
-
237
- do $$
238
- declare rt text := current_database() || '_rt';
239
- begin
240
- revoke create on schema public from public;
241
- if exists (select 1 from pg_roles where rolname = rt) then
242
- execute format('revoke create on schema public from %I', rt);
243
- execute format('revoke delete, truncate on public.keys_issued_credentials from %I', rt);
244
- end if;
245
- end
246
- $$;
@@ -0,0 +1,73 @@
1
+ -- @wtfalch/keys: the ./host entry's event table. Mirrors src/host/schema.ts.
2
+ --
3
+ -- One row per thing @wtfalch/keys did for this host: a caller key
4
+ -- authenticating (key.used, ./held's KeyUsedEvent) or this host minting,
5
+ -- rotating or revoking one of its own issued credentials (credential.minted /
6
+ -- .rotated / .revoked, ./issued's AuditEvent). Not audit_events -- a host
7
+ -- picking this table over its own audit ledger is the host's call, made
8
+ -- for the same reasons every existing host copy of this table already
9
+ -- recorded (a core `credential.*` namespace a host's own audit vocabulary
10
+ -- may refuse, and a held-key `key.used` row carrying no tenant Access to
11
+ -- sign an audit_events row with). Which columns apply depends on kind: a
12
+ -- key.used row carries request_id, tenant_id, entry_id and version; a
13
+ -- credential.* row carries request_id (absent on credential.revoked, which
14
+ -- never calls the Worker) and credential_id; all four are nullable for that
15
+ -- reason.
16
+ --
17
+ -- `./host`'s `auditEndpoint` reads only the three kinds that carry a
18
+ -- request_id (`key.used`, `credential.minted`, `credential.rotated`) --
19
+ -- `packages/reconcile`'s own contract (`packages/reconcile/README.md`,
20
+ -- "Host audit contract"): `credential.revoked` has no Worker call to pair
21
+ -- against and is excluded there, not here -- this table still records it,
22
+ -- for a host's own audit trail.
23
+ --
24
+ -- Append-only via the guard trigger below: every UPDATE, DELETE and
25
+ -- TRUNCATE is refused outright -- this table has no in-place edit, so
26
+ -- there is no narrower shape to allow. The trigger is the first wall; the
27
+ -- second is the host listing this table under `appendOnly` in
28
+ -- `ensureRuntimeRole` (README), which leaves its runtime role INSERT and
29
+ -- SELECT only. Roles stay host-owned: this migration owns none.
30
+ --
31
+ -- Applied by the host's own migrate script after the host copies this file
32
+ -- into its own migrations directory as the next number (keys-migrations);
33
+ -- never edited there. Every statement is idempotent.
34
+
35
+ create table if not exists keys_host_events (
36
+ id bigint generated always as identity primary key,
37
+ kind text not null,
38
+ request_id text,
39
+ credential_id text,
40
+ tenant_id text,
41
+ entry_id text,
42
+ version integer,
43
+ at timestamptz not null default now(),
44
+ constraint keys_host_events_kind_check
45
+ check (kind in ('key.used', 'credential.minted', 'credential.rotated', 'credential.revoked'))
46
+ );
47
+ create index if not exists keys_host_events_at_idx on keys_host_events (at);
48
+
49
+ create or replace function keys_host_events_guard() returns trigger
50
+ language plpgsql as $$
51
+ begin
52
+ if tg_op = 'UPDATE' then
53
+ raise exception 'keys_host_events is append-only: update refused';
54
+ elsif tg_op = 'DELETE' then
55
+ raise exception 'keys_host_events is append-only: delete refused';
56
+ elsif tg_op = 'TRUNCATE' then
57
+ raise exception 'keys_host_events is append-only: truncate refused';
58
+ end if;
59
+ return null;
60
+ end
61
+ $$;
62
+ drop trigger if exists keys_host_events_no_update on keys_host_events;
63
+ create trigger keys_host_events_no_update
64
+ before update on keys_host_events
65
+ for each row execute function keys_host_events_guard();
66
+ drop trigger if exists keys_host_events_no_delete on keys_host_events;
67
+ create trigger keys_host_events_no_delete
68
+ before delete on keys_host_events
69
+ for each row execute function keys_host_events_guard();
70
+ drop trigger if exists keys_host_events_no_truncate on keys_host_events;
71
+ create trigger keys_host_events_no_truncate
72
+ before truncate on keys_host_events
73
+ for each statement execute function keys_host_events_guard();
@@ -0,0 +1,29 @@
1
+ -- #47: a built-in `environment` on every issued credential, so a host's
2
+ -- sandbox/live split is a structured field rather than a convention the host
3
+ -- invents on top of `key_prefix`. Additive; do not edit copied migrations.
4
+ --
5
+ -- Not part of SignableRow (worker-contract.ts): the Worker's signature never
6
+ -- covers it, same footing as idempotency_key and created_at. What keeps it
7
+ -- trustworthy despite that is keys_issued_guard() (0006_keys_issued_guard.sql),
8
+ -- which refuses any UPDATE that changes it -- set once at issue(), for that row's life.
9
+ --
10
+ -- Backfilled 'live' on existing rows: every row minted before this migration
11
+ -- was minted under a single, unqualified prefix, which today's default
12
+ -- environment ('live') matches.
13
+ alter table keys_issued_credentials
14
+ add column if not exists environment text not null default 'live';
15
+ alter table keys_issued_credentials alter column environment drop default;
16
+
17
+ do $$
18
+ begin
19
+ if not exists (
20
+ select 1 from pg_constraint
21
+ where conname = 'keys_issued_credentials_environment_check'
22
+ and conrelid = 'keys_issued_credentials'::regclass
23
+ ) then
24
+ alter table keys_issued_credentials
25
+ add constraint keys_issued_credentials_environment_check
26
+ check (environment in ('live', 'test'));
27
+ end if;
28
+ end
29
+ $$;
@@ -3,8 +3,8 @@
3
3
  -- not edit copied migrations.
4
4
  --
5
5
  -- Neither column is part of SignableRow (worker-contract.ts) -- unsigned,
6
- -- same footing as environment/idempotency_key/created_at (0005) -- and, per
7
- -- keys_issued_guard() below, is NOT frozen the way those are: check() writes
6
+ -- same footing as environment/idempotency_key/created_at (0002) -- and, per
7
+ -- keys_issued_guard() (0006_keys_issued_guard.sql), is NOT frozen the way those are: check() writes
8
8
  -- both on every successful verification, so they must stay ordinarily
9
9
  -- updatable rather than joining the identity/lineage/environment immutable
10
10
  -- set.
@@ -17,6 +17,7 @@ begin
17
17
  if not exists (
18
18
  select 1 from pg_constraint
19
19
  where conname = 'keys_issued_credentials_use_count_check'
20
+ and conrelid = 'keys_issued_credentials'::regclass
20
21
  ) then
21
22
  alter table keys_issued_credentials
22
23
  add constraint keys_issued_credentials_use_count_check
@@ -1,4 +1,4 @@
1
- -- Restores the `environment` default that 0005_keys_environment.sql dropped.
1
+ -- Restores the `environment` default that 0002_keys_environment.sql dropped.
2
2
  -- Additive; do not edit copied migrations.
3
3
  --
4
4
  -- Without it, a host that runs its migrations before redeploying breaks every
@@ -0,0 +1,47 @@
1
+ -- The issued-credential guard (security review K03): revocation stays
2
+ -- permanent, identity, lineage and environment are immutable, and a row can
3
+ -- be neither deleted nor truncated. Additive; do not edit copied migrations.
4
+ --
5
+ -- Part of the issued chain, so a host that uses only the issued module (no
6
+ -- held tables) gets the guard: without it a revoked credential could be
7
+ -- un-revoked, or deleted and inserted again. The function body freezes
8
+ -- `environment` too (0002_keys_environment.sql).
9
+ --
10
+ -- The function pins its search_path to the schema this file runs in,
11
+ -- followed by pg_catalog, with pg_temp last, so a caller-owned temporary
12
+ -- relation cannot shadow one of this schema's. The first statement builds that path
13
+ -- for this transaction; `set search_path from current` then records it on
14
+ -- the function.
15
+ --
16
+ -- The runtime role must be able to update but never delete or truncate this
17
+ -- table: the host lists it under `noDelete` in `ensureRuntimeRole` (README).
18
+ -- The triggers below refuse both whatever the role holds.
19
+ select set_config('search_path', quote_ident(current_schema()) || ', pg_catalog, pg_temp', true);
20
+
21
+ create or replace function keys_issued_guard() returns trigger
22
+ language plpgsql
23
+ set search_path from current
24
+ as $$
25
+ begin
26
+ if tg_op in ('DELETE', 'TRUNCATE') then
27
+ raise exception 'keys_issued_credentials: delete and truncate refused';
28
+ end if;
29
+ if tg_op = 'UPDATE' then
30
+ if new.id is distinct from old.id or new.issued_by_id is distinct from old.issued_by_id
31
+ or new.key_prefix is distinct from old.key_prefix
32
+ or new.environment is distinct from old.environment then
33
+ raise exception 'keys_issued_credentials: identity, lineage and environment are immutable';
34
+ end if;
35
+ if old.revoked_at is not null and new.revoked_at is distinct from old.revoked_at then
36
+ raise exception 'keys_issued_credentials: revocation is permanent';
37
+ end if;
38
+ end if;
39
+ return new;
40
+ end
41
+ $$;
42
+ drop trigger if exists keys_issued_guard_update on keys_issued_credentials;
43
+ create trigger keys_issued_guard_update before update or delete on keys_issued_credentials
44
+ for each row execute function keys_issued_guard();
45
+ drop trigger if exists keys_issued_guard_truncate on keys_issued_credentials;
46
+ create trigger keys_issued_guard_truncate before truncate on keys_issued_credentials
47
+ for each statement execute function keys_issued_guard();
@@ -0,0 +1,19 @@
1
+ /**
2
+ * Absolute paths of the numbered `.sql` migrations this package ships, one
3
+ * directory per chain. Each is a complete, ordered chain for its own part, so
4
+ * a host applies only the parts it uses, as `runMigrationSources` sources:
5
+ *
6
+ * { name: 'keys-issued', dir: issuedMigrationsDir } // ./issued
7
+ * { name: 'keys-held', dir: heldMigrationsDir } // ./held
8
+ * { name: 'keys-host', dir: hostMigrationsDir } // ./host
9
+ *
10
+ * An issued-only host passes the first and gets no held tables. The files
11
+ * name no schema: the host's `schema` decides where they land.
12
+ *
13
+ * Its own subpath (`@wtfalch/keys/migrations-dir`) and no other imports: a
14
+ * migration image imports this without the Drizzle the entries pull in, and a
15
+ * bundler never meets this file's `import.meta.url` through them.
16
+ */
17
+ export declare const issuedMigrationsDir: string;
18
+ export declare const heldMigrationsDir: string;
19
+ export declare const hostMigrationsDir: string;
@@ -0,0 +1,20 @@
1
+ import { fileURLToPath } from 'node:url';
2
+ /**
3
+ * Absolute paths of the numbered `.sql` migrations this package ships, one
4
+ * directory per chain. Each is a complete, ordered chain for its own part, so
5
+ * a host applies only the parts it uses, as `runMigrationSources` sources:
6
+ *
7
+ * { name: 'keys-issued', dir: issuedMigrationsDir } // ./issued
8
+ * { name: 'keys-held', dir: heldMigrationsDir } // ./held
9
+ * { name: 'keys-host', dir: hostMigrationsDir } // ./host
10
+ *
11
+ * An issued-only host passes the first and gets no held tables. The files
12
+ * name no schema: the host's `schema` decides where they land.
13
+ *
14
+ * Its own subpath (`@wtfalch/keys/migrations-dir`) and no other imports: a
15
+ * migration image imports this without the Drizzle the entries pull in, and a
16
+ * bundler never meets this file's `import.meta.url` through them.
17
+ */
18
+ export const issuedMigrationsDir = fileURLToPath(new URL('./migrations/issued/', import.meta.url));
19
+ export const heldMigrationsDir = fileURLToPath(new URL('./migrations/held/', import.meta.url));
20
+ export const hostMigrationsDir = fileURLToPath(new URL('./migrations/host/', import.meta.url));
@@ -0,0 +1,9 @@
1
+ /**
2
+ * `./testing`: an in-process Worker stub built on the package's own signing
3
+ * and AAD code, for a host's tests that need a `WorkerClient` without a
4
+ * live Worker (issue #83). `canonicalJson`, `encodeSigned` and
5
+ * `pickSignable` are re-exported here -- not from `./issued` or the
6
+ * public root -- because they exist for building test fixtures, not for a
7
+ * host's runtime code, which never needs to construct signed bytes itself.
8
+ */
9
+ export { createKeysWorkerStub, TEST_CREDENTIAL, canonicalJson, encodeSigned, pickSignable, type CreateKeysWorkerStubOptions, type KeysWorkerStub, } from './worker-stub.js';
@@ -0,0 +1,9 @@
1
+ /**
2
+ * `./testing`: an in-process Worker stub built on the package's own signing
3
+ * and AAD code, for a host's tests that need a `WorkerClient` without a
4
+ * live Worker (issue #83). `canonicalJson`, `encodeSigned` and
5
+ * `pickSignable` are re-exported here -- not from `./issued` or the
6
+ * public root -- because they exist for building test fixtures, not for a
7
+ * host's runtime code, which never needs to construct signed bytes itself.
8
+ */
9
+ export { createKeysWorkerStub, TEST_CREDENTIAL, canonicalJson, encodeSigned, pickSignable, } from './worker-stub.js';
@@ -0,0 +1,55 @@
1
+ import { canonicalJson, encodeSigned, pickSignable } from '../issued/encoding.js';
2
+ import type { VerifyKeys, WorkerClient, WorkerErrorCode } from '../worker-contract.js';
3
+ /**
4
+ * An in-process stand-in for the unwrap Worker (`packages/worker`), for
5
+ * every host test that needs a `WorkerClient<TGrant>` without a live
6
+ * Worker. Implements the full contract -- `wrap`/`unwrap`/`rewrap` for
7
+ * `./held`, `signRow`/`resign`/`recordEviction` for `./issued` -- with the
8
+ * package's own real signing, AAD and sealing code, not a reimplementation:
9
+ * it imports `encodeSigned`/`canonicalJson`/`pickSignable` from `../issued/
10
+ * encoding.js`, `encodeAad` from `../held/aad.js`, and `aesGcmSeal`/
11
+ * `aesGcmOpen` from `../held/seal.js` -- the same module
12
+ * `packages/worker/src/crypto.ts` imports for its own AES-256-GCM layout
13
+ * (#86: before this, the two copies could drift silently) -- rather than
14
+ * duplicating any of them, so a signature or a sealed key this stub
15
+ * produces is verified the same way a real Worker's would be -- proven by
16
+ * `worker-stub.test.ts`, which runs `check()` (`../issued`) and `open()`
17
+ * (`../held`) against rows this stub signed and sealed, and by
18
+ * `../../../worker/src/crypto.test.ts`, which opens a value sealed through
19
+ * this same module with the Worker's own re-exported `aesGcmOpen`, and the
20
+ * reverse.
21
+ *
22
+ * This is what every host's own `tests/keys-worker-stub.ts` re-implemented
23
+ * by hand (issue #83, "checked by hand against dist/issued/index.d.ts") --
24
+ * see the package README's "Adopting ./testing".
25
+ *
26
+ * Signing uses `node:crypto`'s classic, synchronous `generateKeyPairSync`/
27
+ * `sign`/`verify` rather than `webcrypto.subtle`: both produce and check
28
+ * the same RFC 8032 Ed25519 signatures -- `worker-stub.test.ts` proves it
29
+ * by running `../issued/verify.js`'s own `verifyIssuedRowSignature`
30
+ * (which uses `webcrypto.subtle.verify`) against a signature this stub
31
+ * produced -- but being synchronous, it lets `verifyKeys`/`verifyKeysEnv`
32
+ * be plain properties instead of a promise a caller would have to await
33
+ * before building `KEYS_VERIFY_KEYS`.
34
+ */
35
+ /** Every credential this stub accepts. Anything else is `credential_invalid`, exactly as a real bad bearer token would be. */
36
+ export declare const TEST_CREDENTIAL = "stub-test-credential";
37
+ type Method = keyof WorkerClient<unknown>;
38
+ export interface CreateKeysWorkerStubOptions {
39
+ /** Unix milliseconds. Defaults to `Date.now`; a test passes a fake clock to check `minter_expired`. */
40
+ readonly now?: () => number;
41
+ }
42
+ export interface KeysWorkerStub<TGrant> extends WorkerClient<TGrant> {
43
+ /** This stub's one Ed25519 public key, for `createCredentialIssuer`'s `verifyKeys` option. */
44
+ readonly verifyKeys: VerifyKeys;
45
+ /** The same key, base64url, in the comma-separated form `KEYS_VERIFY_KEYS` takes. */
46
+ readonly verifyKeysEnv: string;
47
+ /** Calls received so far, per method -- for asserting a code path called the Worker exactly once, or not at all. */
48
+ readonly callCounts: Record<Method, number>;
49
+ /** Every call to `method` (or `'all'`) fails with `code` until `clearFailure()`. */
50
+ failAs(method: Method | 'all', code: WorkerErrorCode, retryAfterMs?: number): void;
51
+ clearFailure(): void;
52
+ }
53
+ /** `canonicalJson`, `encodeSigned` and `pickSignable` are the package's own -- re-exported by `./testing/index.js`, not reimplemented here. */
54
+ export declare function createKeysWorkerStub<TGrant = unknown>(options?: CreateKeysWorkerStubOptions): KeysWorkerStub<TGrant>;
55
+ export { canonicalJson, encodeSigned, pickSignable };