Skip to content

Commit ed45eb9

Browse files
fix(cli): an os migrate audit-* run stops when a registered host signer or sink fails to resolve (#34569)
<!-- claude-projects-attribution: {"github_login":"os-steve"} --> _Requested by **Steve** · [project thread](https://claude.ai/code/project/chan_01FdL1XeUyvVNxLreh3F4Qi9?thread=cmsg_01FdL1XeUyvVNxLreh3F4Qi96xkbCcbB1nF3xM9vBtrWwE)_ Fixes #34519 Clause-②: no Metadata-protocol: none ## 维护者速读 - **改了什么:** `os migrate audit-*` 四个改写命令在改写已封存的行之前要先签一份修订;签名时取宿主的签名服务和导出服务。现在和封存器(#34280)一样分三种情况:没注册就用本机钥(不变);注册了能取到就用;注册了却取不到就整次运行报错退出,不改任何行。 - **为什么改:** 以前「取不到」被当成「没注册」,修订会悄悄改用本机钥签名,导出也悄悄丢掉;工厂方式注册的签名器也取不到。这和 #34280 修掉的封存器问题同一形状。 - **风险与回滚:** 只影响注册了签名器或导出服务、且注册已经坏掉的宿主:从悄悄降级变成报错退出(exit 1)。配置正常的宿主无变化。回滚即还原本 PR。 - **先看:** `packages/cli/src/utils/audit-redaction-seal.ts` 末尾的 `hostSeam`;四个命令里 `await redaction.seal(...)` 一行。 - **两分钟验证:** `cd packages/cli && npx vitest run src/utils/audit-redaction-seal.host-seam-34519.test.ts`,预期 4 条全绿。 - **需要你做的:** 无,席位落地。 ## What changes - `packages/cli/src/utils/audit-redaction-seal.ts`: `RedactionSealRun.seal()` is now async and resolves `audit.checkpointSigner` and `audit.checkpointSink` through `hostSeam()`: the kernel's `getServiceAsync`, absorbing only the branded "never registered" rejection (`isServiceNotRegisteredError`, `@objectstack/core`); every other rejection becomes `Audit redaction seal: the '<slot>' service is registered but could not be resolved (<cause>) — no sealed row is amended or rewritten; the run does not <fallback> in place of a service the host registered. …`, with the cause attached. A kernel with no async accessor keeps the sync read, where "not registered" is the only fault. This is the rule and the wording of PR #34495 (the sealer, #34280). - The four commands (`audit-key-material`, `audit-mail-bodies`, `audit-metadata-bodies`, `audit-side-copies`): `const seal = await redaction.seal(stack.kernel, engine);`. The call already sat inside the try whose catch prints the error (or the `--json` error object) and exits 1, before `underAuditSealLock`, so a rejection stops the run before any row is amended or rewritten. - `content/docs/deployment/cli.mdx` (Sealed audit rows): one paragraph says who signs the amendments (the host signer when registered, the local key when none) and that a signer or sink registered but not resolvable stops the run with exit 1, never falling back to the local key. ## Tests - New `src/utils/audit-redaction-seal.host-seam-34519.test.ts` (real `ObjectKernel` service registry; the crypto composition is stubbed to "no provider", which a run survives): 1. nothing registered: `signer` and `sink` are `undefined`, so the amendment writer signs with the local key, as before; 2. a signer factory that throws: `seal()` rejects with the slot name, the cause and "does not sign with the local key"; 3. a sink factory that throws: `seal()` rejects naming `audit.checkpointSink`; 4. a signer that resolves, as an instance and as a factory, is handed over. - `audit-redaction-seal.contract.test.ts`: one new pin per command: `seal()` is awaited exactly once, before the `underAuditSealLock(` hold. - Ablation (fix reverted, tests kept): 7 red (cases 2–4 and the four await pins), case 1 green. - The new test imports `@objectstack/service-settings` at module top, as `check:test-source-alias` asks (the util's static reach names it through the mocked `one-shot-settings.js`). - No existing assertion changed. `packages/cli`: `pnpm typecheck` (incl. `check:test-typecheck`) green; `src/commands/migrate/` + the new file: 75 files, 606 passed / 8 skipped. ## Acceptance notes - The card names the signer; the sink rides the same helper and the same rule, as in PR #34495, so it is covered here too. - `Clause-②: no`: no key, flag or config value an operator writes is refused; a run on a host whose signer or sink registration already fails now exits 1 instead of amending under the local key. ## Verification Gates per `node scripts/pm/dispatch-gates.mjs --commands` after the last commit: see the `os-dev-report` comment on #34519. <!-- signed: repo:objectstack#2 · os-steve · session_01ADHkmRPxhTmqbUbMrqygh4 --> --------- Co-authored-by: Claude <noreply@anthropic.com>
1 parent ad7331a commit ed45eb9

9 files changed

Lines changed: 174 additions & 16 deletions
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
'@objectstack/cli': patch
3+
---
4+
5+
fix(cli): an `os migrate audit-*` run stops when the host's checkpoint signer or sink is registered but fails to resolve, instead of amending under the local key
6+
7+
Clause-②: no
8+
Metadata-protocol: none
9+
10+
The four `os migrate audit-*` ledger-copy rewrites amend a sealed row before they rewrite it (ADR-0154 D7), signing the amendment with the host's `audit.checkpointSigner` when one is registered and the local key otherwise. The CLI read that slot, and `audit.checkpointSink`, through a helper that turned every resolution error into "not registered". A host signer whose factory threw (a KMS client that cannot start, a missing credential) was therefore replaced by the local key, and a failing sink dropped the export, with nothing said. A signer registered as a factory was never built at all.
11+
12+
The run now follows the rule the sealer follows since #34280. No host signer registered still amends under the local key, and no sink exports nothing, as before. A registered signer or sink that resolves is used, including one registered as a factory. One that is registered and fails to resolve stops the run before any row is amended or rewritten: it prints `Audit redaction seal: the '<slot>' service is registered but could not be resolved (<cause>)` and exits 1. A working host sees no change.

‎content/docs/deployment/cli.mdx‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2210,6 +2210,13 @@ when:
22102210
`modified`;
22112211
- the rewrite would change a column the object seals in clear.
22122212
2213+
The amendments are signed by the host's checkpoint signer (`audit.checkpointSigner`) when the
2214+
host registers one, and by the deployment's local key when it registers none; a host checkpoint
2215+
sink (`audit.checkpointSink`) is handed each amendment. A signer or sink that is registered but
2216+
cannot be resolved stops the run before any row is amended or rewritten: it prints
2217+
`Audit redaction seal: the '<slot>' service is registered but could not be resolved (<cause>)`
2218+
and exits `1`. It never falls back to the local key in place of the host's signer.
2219+
22132220
The dry run decides each row the same way and writes nothing. Re-run where the
22142221
crypto provider `os serve` seals with is available.
22152222

‎packages/cli/src/commands/migrate/audit-key-material.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ export default class MigrateAuditKeyMaterial extends Command {
228228
update: (object, data, options) => engine.update(object, data, options),
229229
};
230230

231-
const seal = redaction.seal(stack.kernel, engine);
231+
const seal = await redaction.seal(stack.kernel, engine);
232232
// [ADR-0154 D3 amended] An --apply run holds the audit seal lock from before its first rewrite to after its
233233
// last, archive runs included, so no sealer pass seals a row it rewrites; where the database offers no session
234234
// lock, a run on a sealed object needs --sealer-stopped (seat ruling 6885005594, E + B).

‎packages/cli/src/commands/migrate/audit-mail-bodies.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ export default class MigrateAuditMailBodies extends Command {
236236
update: (object, data, options) => engine.update(object, data, options),
237237
};
238238

239-
const seal = redaction.seal(stack.kernel, engine);
239+
const seal = await redaction.seal(stack.kernel, engine);
240240
// [ADR-0154 D3 amended] An --apply run holds the audit seal lock from before its first rewrite to after its
241241
// last, archive runs included, so no sealer pass seals a row it rewrites; where the database offers no session
242242
// lock, a run on a sealed object needs --sealer-stopped (seat ruling 6885005594, E + B).

‎packages/cli/src/commands/migrate/audit-metadata-bodies.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ export default class MigrateAuditMetadataBodies extends Command {
209209
update: (object, data, options) => engine.update(object, data, options),
210210
};
211211

212-
const seal = redaction.seal(stack.kernel, engine);
212+
const seal = await redaction.seal(stack.kernel, engine);
213213
// [ADR-0154 D3 amended] An --apply run holds the audit seal lock from before its first rewrite to after its
214214
// last, archive runs included, so no sealer pass seals a row it rewrites; where the database offers no session
215215
// lock, a run on a sealed object needs --sealer-stopped (seat ruling 6885005594, E + B).

‎packages/cli/src/commands/migrate/audit-redaction-seal.contract.test.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@ describe('os migrate audit-* amend a sealed row first (ADR-0154 D7)', () => {
7878
}
7979
});
8080

81+
it(`${file}: awaits the seal before the lock hold, so a host signer that cannot be resolved fails the run first (#34519)`, () => {
82+
expect(source.match(/const seal = await redaction\.seal\(stack\.kernel, engine\);/g)).toHaveLength(1);
83+
expect(source.indexOf('await redaction.seal(')).toBeLessThan(source.indexOf('underAuditSealLock('));
84+
});
85+
8186
it(`${file}: exits non-zero when a sealed row was refused, or the seal lock was lost, as for a failure`, () => {
8287
expect(source.match(/failures > 0 \|\| (report\.)?refused > 0 \|\| lostLock\) this\.exit\(1\)/g)).toHaveLength(2);
8388
});

‎packages/cli/src/commands/migrate/audit-side-copies.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ export default class MigrateAuditSideCopies extends Command {
254254
update: (object, data, options) => engine.update(object, data, options),
255255
};
256256

257-
const seal = redaction.seal(stack.kernel, engine);
257+
const seal = await redaction.seal(stack.kernel, engine);
258258
// [ADR-0154 D3 amended] An --apply run holds the audit seal lock from before its first rewrite to after its
259259
// last, archive runs included, on the copy tables it rewrites (`--object` narrows them), so no sealer pass
260260
// seals a row it rewrites; where the database offers no session lock, a run on a sealed object needs
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.
2+
3+
// check:test-source-alias: the util's reach names `@objectstack/service-settings` (through the mocked
4+
// `one-shot-settings.js`); its load is paid here, at collection, never inside a clocked case.
5+
import '@objectstack/service-settings';
6+
import { ObjectKernel } from '@objectstack/core';
7+
import { AUDIT_CHECKPOINT_SIGNER_SERVICE, AUDIT_CHECKPOINT_SINK_SERVICE } from '@objectstack/spec/contracts';
8+
import { describe, expect, it, vi } from 'vitest';
9+
10+
// The crypto composition is not what this file judges: it is stubbed to "no
11+
// crypto provider", which a run survives (the module doc of the util under test).
12+
vi.mock('./one-shot-settings.js', () => ({
13+
oneShotCrypto: async () => {
14+
throw new Error('no host config in this test');
15+
},
16+
}));
17+
18+
const { composeRedactionSeal } = await import('./audit-redaction-seal.js');
19+
20+
/**
21+
* #34519 — the `os migrate audit-*` redaction seal reads the host's
22+
* `audit.checkpointSigner` and `audit.checkpointSink` in three states, the
23+
* rule #34280 set for the sealer. Only an ABSENT signer leaves the run to the
24+
* local key (the amendment writer's default); one that is registered and
25+
* resolves is handed over, a factory included; one that is registered and
26+
* cannot be resolved rejects `seal()`, naming the slot. Each of the four
27+
* commands awaits `seal()` inside the try whose catch prints the error and
28+
* exits 1 (`audit-redaction-seal.contract.test.ts` pins the await), so that
29+
* rejection fails the command before any row is amended or rewritten.
30+
*/
31+
32+
/**
33+
* A real kernel's service registry, with no plugins booted: what `seal()`
34+
* reads off the boot is the registry alone (`getServiceAsync`, `getService`).
35+
*/
36+
function kernelWith(register: (kernel: ObjectKernel) => void = () => {}): ObjectKernel {
37+
const kernel = new ObjectKernel({ logger: { level: 'silent' } });
38+
register(kernel);
39+
return kernel;
40+
}
41+
42+
const hostSigner = {
43+
async sign() {
44+
return { keyId: 'host-key', signature: 'c2ln' };
45+
},
46+
async trustedPublicKey() {
47+
return null;
48+
},
49+
};
50+
51+
describe('the redaction seal resolves the host signer and sink in three states (#34519)', () => {
52+
it('no host signer or sink registered: neither is handed over, so the run amends under the local key', async () => {
53+
const seal = await (await composeRedactionSeal()).seal(kernelWith(), {});
54+
expect(seal.signer).toBeUndefined();
55+
expect(seal.sink).toBeUndefined();
56+
});
57+
58+
it('a registered host signer whose resolution throws: seal() rejects, naming the slot and the fallback it refuses', async () => {
59+
const kernel = kernelWith((k) =>
60+
k.registerServiceFactory(AUDIT_CHECKPOINT_SIGNER_SERVICE, () => {
61+
throw new Error('KMS client cannot start: credential missing');
62+
}),
63+
);
64+
const run = await composeRedactionSeal();
65+
await expect(run.seal(kernel, {})).rejects.toThrow(
66+
`Audit redaction seal: the '${AUDIT_CHECKPOINT_SIGNER_SERVICE}' service is registered but could not be resolved ` +
67+
'(KMS client cannot start: credential missing)',
68+
);
69+
await expect(run.seal(kernel, {})).rejects.toThrow('the run does not sign with the local key');
70+
});
71+
72+
it('a registered sink whose resolution throws: seal() rejects, naming the slot', async () => {
73+
const kernel = kernelWith((k) =>
74+
k.registerServiceFactory(AUDIT_CHECKPOINT_SINK_SERVICE, () => {
75+
throw new Error('SIEM client cannot start');
76+
}),
77+
);
78+
await expect((await composeRedactionSeal()).seal(kernel, {})).rejects.toThrow(
79+
`the '${AUDIT_CHECKPOINT_SINK_SERVICE}' service is registered but could not be resolved (SIEM client cannot start)`,
80+
);
81+
});
82+
83+
it('a registered host signer that resolves is handed over — as an instance, and as a factory', async () => {
84+
for (const how of ['instance', 'factory'] as const) {
85+
const kernel = kernelWith((k) => {
86+
if (how === 'instance') k.registerService(AUDIT_CHECKPOINT_SIGNER_SERVICE, hostSigner);
87+
else k.registerServiceFactory(AUDIT_CHECKPOINT_SIGNER_SERVICE, () => hostSigner);
88+
});
89+
const seal = await (await composeRedactionSeal()).seal(kernel, {});
90+
expect(seal.signer, how).toBe(hostSigner);
91+
}
92+
});
93+
});

‎packages/cli/src/utils/audit-redaction-seal.ts‎

Lines changed: 53 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.
22

3+
import { isServiceNotRegisteredError } from '@objectstack/core';
34
import type { KeyMaterialMigrationOptions } from '@objectstack/plugin-audit';
45
import { AUDIT_CHECKPOINT_SIGNER_SERVICE, AUDIT_CHECKPOINT_SINK_SERVICE } from '@objectstack/spec/contracts';
56
import { oneShotCrypto, type OneShotCrypto } from './one-shot-settings.js';
@@ -23,11 +24,21 @@ import { oneShotCrypto, type OneShotCrypto } from './one-shot-settings.js';
2324
/** The `seal` option the three rewrites take. */
2425
export type RedactionSeal = NonNullable<KeyMaterialMigrationOptions['seal']>;
2526

27+
/** The boot's kernel, as far as {@link RedactionSealRun.seal} reads it. */
28+
export interface RedactionSealKernel {
29+
getService(name: string): unknown;
30+
getServiceAsync?(name: string): Promise<unknown>;
31+
}
32+
2633
export interface RedactionSealRun {
2734
/** Composed into the boot beside the data-migration plugins. */
2835
readonly plugins: readonly unknown[];
29-
/** The rewrite's `seal`, once the boot is up. */
30-
seal(kernel: { getService(name: string): unknown }, engine: unknown): RedactionSeal;
36+
/**
37+
* The rewrite's `seal`, once the boot is up. Rejects when the host registers
38+
* a checkpoint signer or sink that cannot be resolved (#34519): only an
39+
* ABSENT signer leaves the run to the local key.
40+
*/
41+
seal(kernel: RedactionSealKernel, engine: unknown): Promise<RedactionSeal>;
3142
/** The line a run that refused `refused` sealed rows ends with: why, and the fix. */
3243
refusedLine(refused: number): string;
3344
}
@@ -42,7 +53,7 @@ export async function composeRedactionSeal(): Promise<RedactionSealRun> {
4253
}
4354
return {
4455
plugins: crypto?.plugins ?? [],
45-
seal(kernel, engine) {
56+
async seal(kernel, engine) {
4657
let provider: RedactionSeal['crypto'];
4758
if (crypto) {
4859
try {
@@ -54,18 +65,11 @@ export async function composeRedactionSeal(): Promise<RedactionSealRun> {
5465
unavailable = error instanceof Error ? error.message : String(error);
5566
}
5667
}
57-
const service = <T>(name: string): T | undefined => {
58-
try {
59-
return (kernel.getService(name) as T | undefined) ?? undefined;
60-
} catch {
61-
return undefined;
62-
}
63-
};
6468
return {
6569
engine: engine as RedactionSeal['engine'],
6670
...(provider ? { crypto: provider } : {}),
67-
signer: service<RedactionSeal['signer']>(AUDIT_CHECKPOINT_SIGNER_SERVICE),
68-
sink: service<RedactionSeal['sink']>(AUDIT_CHECKPOINT_SINK_SERVICE),
71+
signer: await hostSeam<RedactionSeal['signer']>(kernel, AUDIT_CHECKPOINT_SIGNER_SERVICE, 'sign with the local key'),
72+
sink: await hostSeam<RedactionSeal['sink']>(kernel, AUDIT_CHECKPOINT_SINK_SERVICE, 'amend without the off-host export'),
6973
};
7074
},
7175
refusedLine(refused) {
@@ -79,3 +83,40 @@ export async function composeRedactionSeal(): Promise<RedactionSealRun> {
7983
},
8084
};
8185
}
86+
87+
/**
88+
* #34519: a host seam the redaction seal reads (`audit.checkpointSigner`,
89+
* `audit.checkpointSink`), in three states — the rule the sealer follows
90+
* (#34280). Never registered ⇒ `undefined`, and the run keeps its stand-in
91+
* (the local signer, no export). Registered and resolved ⇒ the service.
92+
* Registered and NOT resolvable — a factory that throws, a KMS client that
93+
* cannot start — ⇒ the run stops with an error naming the slot, instead of
94+
* amending sealed rows under the local key where the host chose its own.
95+
*
96+
* The kernel's async accessor tells the first and third apart with the
97+
* branded "never registered" rejection (`isServiceNotRegisteredError`), ⛔ not
98+
* message text, and builds a factory-registered seam the sync accessor
99+
* refuses. A kernel with no async accessor has no factories, so "not
100+
* registered" is the only fault its sync accessor reports.
101+
*/
102+
async function hostSeam<T>(kernel: RedactionSealKernel, slot: string, fallback: string): Promise<T | undefined> {
103+
if (typeof kernel.getServiceAsync === 'function') {
104+
try {
105+
return ((await kernel.getServiceAsync(slot)) as T | undefined) ?? undefined;
106+
} catch (error) {
107+
if (isServiceNotRegisteredError(error)) return undefined;
108+
const fault = error instanceof Error ? error.message : String(error);
109+
throw new Error(
110+
`Audit redaction seal: the '${slot}' service is registered but could not be resolved (${fault}) — no sealed row is amended or ` +
111+
`rewritten; the run does not ${fallback} in place of a service the host registered. Fix the registration of ` +
112+
'the service named here, then run again.',
113+
{ cause: error },
114+
);
115+
}
116+
}
117+
try {
118+
return (kernel.getService(slot) as T | undefined) ?? undefined;
119+
} catch {
120+
return undefined;
121+
}
122+
}

0 commit comments

Comments
 (0)