@serfab/cadre-host 0.11.0 → 0.13.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 (46) hide show
  1. package/README.md +406 -296
  2. package/dist/bin/host.d.ts +11 -7
  3. package/dist/bin/host.js +40 -12
  4. package/dist/bin/host.js.map +1 -1
  5. package/dist/installer/config.d.ts +4 -4
  6. package/dist/installer/config.js +5 -39
  7. package/dist/installer/config.js.map +1 -1
  8. package/dist/installer/identity.d.ts +8 -1
  9. package/dist/installer/identity.js +8 -1
  10. package/dist/installer/identity.js.map +1 -1
  11. package/dist/orchestrator/host-process-orchestrator.d.ts +2 -2
  12. package/dist/orchestrator/host-process-orchestrator.js +14 -4
  13. package/dist/orchestrator/host-process-orchestrator.js.map +1 -1
  14. package/dist/server/routes/bootstrap-node-validation.d.ts +96 -0
  15. package/dist/server/routes/bootstrap-node-validation.js +178 -0
  16. package/dist/server/routes/bootstrap-node-validation.js.map +1 -0
  17. package/dist/server/routes/grants.js +8 -25
  18. package/dist/server/routes/grants.js.map +1 -1
  19. package/dist/server/routes/provision-request-validation.d.ts +45 -0
  20. package/dist/server/routes/provision-request-validation.js +97 -0
  21. package/dist/server/routes/provision-request-validation.js.map +1 -0
  22. package/dist/server/settings-store.d.ts +1 -1
  23. package/dist/server/settings-store.js +5 -1
  24. package/dist/server/settings-store.js.map +1 -1
  25. package/package.json +5 -4
  26. package/src/__tests__/cli.smoke.test.ts +8 -3
  27. package/src/__tests__/global-setup.ts +9 -4
  28. package/src/__tests__/orchestrator-node-identity.test.ts +204 -204
  29. package/src/__tests__/orchestrator.test.ts +951 -951
  30. package/src/bin/__tests__/cli-reference.test.ts +167 -0
  31. package/src/bin/host.ts +1326 -1299
  32. package/src/donation/__tests__/donation-service.test.ts +1068 -1068
  33. package/src/donation/__tests__/donation-supervisor.test.ts +510 -510
  34. package/src/donation/__tests__/fake-orchestrator.test.ts +168 -168
  35. package/src/installer/__tests__/config.test.ts +134 -112
  36. package/src/installer/config.ts +5 -43
  37. package/src/installer/identity.ts +8 -1
  38. package/src/nat/__tests__/nat-service.test.ts +515 -497
  39. package/src/orchestrator/host-process-orchestrator.ts +1143 -1133
  40. package/src/server/__tests__/bootstrap-node-validation.test.ts +134 -0
  41. package/src/server/__tests__/grants-route.test.ts +326 -263
  42. package/src/server/__tests__/settings-route.test.ts +8 -7
  43. package/src/server/routes/bootstrap-node-validation.ts +187 -0
  44. package/src/server/routes/grants.ts +9 -26
  45. package/src/server/routes/provision-request-validation.ts +105 -0
  46. package/src/server/settings-store.ts +41 -37
@@ -0,0 +1 @@
1
+ {"version":3,"file":"provision-request-validation.js","sourceRoot":"","sources":["../../../src/server/routes/provision-request-validation.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AAGH,OAAO,EAAE,sBAAsB,EAAE,MAAM,gCAAgC,CAAC;AAExE,wEAAwE;AACxE,SAAS,eAAe,CAAC,KAAc;IACrC,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,CAAC,IAAI,EAAE,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QAC3D,OAAO,EAAE,KAAK,EAAE,qBAAqB,EAAE,CAAC;IAC1C,CAAC;IACD,OAAO,EAAE,OAAO,EAAE,KAAK,CAAC,IAAI,EAAE,EAAE,CAAC;AACnC,CAAC;AAED;;;GAGG;AACH,SAAS,iBAAiB,CAAC,KAAc;IACvC,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,IAAI,KAAK,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QAChD,OAAO,EAAE,KAAK,EAAE,uBAAuB,EAAE,CAAC;IAC5C,CAAC;IACD,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,GAAG,EAAiB,EAAE,CAAC,OAAO,GAAG,KAAK,QAAQ,CAAC,EAAE,CAAC;QAClE,OAAO,EAAE,KAAK,EAAE,uCAAuC,EAAE,CAAC;IAC5D,CAAC;IACD,OAAO,EAAE,SAAS,EAAE,KAAK,EAAE,CAAC;AAC9B,CAAC;AAED,6EAA6E;AAC7E,SAAS,eAAe,CAAC,KAAc;IACrC,IAAI,KAAK,KAAK,SAAS;QAAE,OAAO,EAAE,CAAC;IACnC,IAAI,KAAK,KAAK,SAAS,IAAI,KAAK,KAAK,aAAa,EAAE,CAAC;QACnD,OAAO,EAAE,KAAK,EAAE,4CAA4C,EAAE,CAAC;IACjE,CAAC;IACD,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,CAAC;AAC5B,CAAC;AAED;;;;;;;;;;GAUG;AACH,MAAM,UAAU,wBAAwB,CACtC,IAAa,EACb,UAAkB;IAElB,IAAI,OAAO,IAAI,KAAK,QAAQ,IAAI,IAAI,KAAK,IAAI,IAAI,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE,CAAC;QACrE,OAAO,EAAE,KAAK,EAAE,oCAAoC,EAAE,CAAC;IACzD,CAAC;IACD,MAAM,MAAM,GAAG,IAA+B,CAAC;IAE/C,MAAM,OAAO,GAAG,eAAe,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;IAChD,IAAI,OAAO,IAAI,OAAO;QAAE,OAAO,OAAO,CAAC;IAEvC,MAAM,SAAS,GAAG,sBAAsB,CAAC,MAAM,CAAC,cAAc,CAAC,CAAC;IAChE,IAAI,OAAO,IAAI,SAAS;QAAE,OAAO,SAAS,CAAC;IAE3C,MAAM,SAAS,GAAG,iBAAiB,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC;IACtD,IAAI,OAAO,IAAI,SAAS;QAAE,OAAO,SAAS,CAAC;IAE3C,MAAM,OAAO,GAAG,eAAe,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;IAChD,IAAI,OAAO,IAAI,OAAO;QAAE,OAAO,OAAO,CAAC;IAEvC,OAAO;QACL,OAAO,EAAE;YACP,UAAU;YACV,OAAO,EAAE,OAAO,CAAC,OAAO;YACxB,cAAc,EAAE,SAAS,CAAC,KAAK;YAC/B,SAAS,EAAE,SAAS,CAAC,SAAS;YAC9B,GAAG,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,OAAO,EAAE,OAAO,CAAC,OAAO,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;SACzD;KACF,CAAC;AACJ,CAAC"}
@@ -17,7 +17,7 @@ export interface HostSettingsStoreOptions {
17
17
  export declare class HostSettingsStore {
18
18
  private readonly configPath;
19
19
  constructor(opts: HostSettingsStoreOptions);
20
- /** Read the current host.config.json (runs v1→v2 migration on first read). */
20
+ /** Read the current host.config.json. Throws if absent, malformed, or not version 2. */
21
21
  read(): HostConfigFile;
22
22
  /** Patch and persist. */
23
23
  update(patch: Partial<Omit<HostConfigFile, 'version'>>): HostConfigFile;
@@ -15,7 +15,11 @@ export class HostSettingsStore {
15
15
  constructor(opts) {
16
16
  this.configPath = configPath(opts.dataDir);
17
17
  }
18
- /** Read the current host.config.json (runs v1→v2 migration on first read). */
18
+ // NOTE: a bad config (absent, malformed, wrong version) throws a plain Error, so
19
+ // /api/settings answers 500 code "internal" with the read message passed through.
20
+ // Fine while the UI just surfaces the text; if it ever needs to tell "your config
21
+ // file is unreadable" apart from a genuine server bug, give this a typed error.
22
+ /** Read the current host.config.json. Throws if absent, malformed, or not version 2. */
19
23
  read() {
20
24
  return readHostConfig(this.configPath);
21
25
  }
@@ -1 +1 @@
1
- {"version":3,"file":"settings-store.js","sourceRoot":"","sources":["../../src/server/settings-store.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAEH,OAAO,EAAE,UAAU,EAAE,MAAM,uBAAuB,CAAC;AACnD,OAAO,EAAE,cAAc,EAAE,gBAAgB,EAAuB,MAAM,wBAAwB,CAAC;AAO/F,MAAM,OAAO,iBAAiB;IAG5B,YAAY,IAA8B;QACxC,IAAI,CAAC,UAAU,GAAG,UAAU,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;IAC7C,CAAC;IAED,8EAA8E;IAC9E,IAAI;QACF,OAAO,cAAc,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC;IACzC,CAAC;IAED,yBAAyB;IACzB,MAAM,CAAC,KAA+C;QACpD,OAAO,gBAAgB,CAAC,IAAI,CAAC,UAAU,EAAE,KAAK,CAAC,CAAC;IAClD,CAAC;CACF"}
1
+ {"version":3,"file":"settings-store.js","sourceRoot":"","sources":["../../src/server/settings-store.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAEH,OAAO,EAAE,UAAU,EAAE,MAAM,uBAAuB,CAAC;AACnD,OAAO,EAAE,cAAc,EAAE,gBAAgB,EAAuB,MAAM,wBAAwB,CAAC;AAO/F,MAAM,OAAO,iBAAiB;IAG5B,YAAY,IAA8B;QACxC,IAAI,CAAC,UAAU,GAAG,UAAU,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;IAC7C,CAAC;IAED,iFAAiF;IACjF,kFAAkF;IAClF,kFAAkF;IAClF,gFAAgF;IAChF,wFAAwF;IACxF,IAAI;QACF,OAAO,cAAc,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC;IACzC,CAAC;IAED,yBAAyB;IACzB,MAAM,CAAC,KAA+C;QACpD,OAAO,gBAAgB,CAAC,IAAI,CAAC,UAAU,EAAE,KAAK,CAAC,CAAC;IAClD,CAAC;CACF"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@serfab/cadre-host",
3
- "version": "0.11.0",
3
+ "version": "0.13.0",
4
4
  "type": "module",
5
5
  "description": "Self-hosted cadre node manager for basement-PC deployments",
6
6
  "main": "dist/index.js",
@@ -64,9 +64,10 @@
64
64
  "@libp2p/crypto": "^5.1.13",
65
65
  "@libp2p/interface": "^3.1.0",
66
66
  "@libp2p/peer-id": "^6.0.4",
67
- "@serfab/cadre-cli": "^0.11.0",
68
- "@serfab/cadre-core": "^0.11.0",
69
- "@serfab/cadre-provider": "^0.11.0",
67
+ "@multiformats/multiaddr": "^12.5.1",
68
+ "@serfab/cadre-cli": "^0.13.0",
69
+ "@serfab/cadre-core": "^0.13.0",
70
+ "@serfab/cadre-provider": "^0.13.0",
70
71
  "commander": "^14.0.2",
71
72
  "debug": "^4.4.3",
72
73
  "fastify": "^5.7.1",
@@ -4,7 +4,7 @@
4
4
  */
5
5
 
6
6
  import { execFileSync } from 'node:child_process';
7
- import { existsSync } from 'node:fs';
7
+ import { existsSync, readFileSync } from 'node:fs';
8
8
  import { fileURLToPath } from 'node:url';
9
9
  import { dirname, resolve } from 'node:path';
10
10
  import { describe, it, expect, vi } from 'vitest';
@@ -33,7 +33,7 @@ describe('cadre-host CLI smoke test', () => {
33
33
  }
34
34
  });
35
35
 
36
- it('prints version', () => {
36
+ it('prints the version from package.json', () => {
37
37
  if (!existsSync(binPath)) {
38
38
  throw new Error(`cadre-host bin not built at ${binPath}; run \`yarn build\` first`);
39
39
  }
@@ -41,6 +41,11 @@ describe('cadre-host CLI smoke test', () => {
41
41
  const stdout = execFileSync(process.execPath, [binPath, '--version'], {
42
42
  encoding: 'utf8',
43
43
  });
44
- expect(stdout.trim().length).toBeGreaterThan(0);
44
+ // The built CLI reads package.json at runtime, so this holds even against a
45
+ // stale dist — it catches the version falling back to '0.0.0-unknown'.
46
+ const pkg = JSON.parse(
47
+ readFileSync(resolve(here, '../../package.json'), 'utf8'),
48
+ ) as { version: string };
49
+ expect(stdout.trim()).toBe(pkg.version);
45
50
  });
46
51
  });
@@ -14,10 +14,15 @@
14
14
  * build is invisible here: the suite runs the previous build and reports green
15
15
  * about code it never executed. Fail the run up front instead.
16
16
  *
17
- * This does not cover cadre-host's *own* `src` going stale relative to its own
18
- * `dist` — the CLI smoke tests spawn `dist/bin/host.js` as a child process, which
19
- * is a same-package staleness concern, not a cross-package one `BuildTarget` is
20
- * meant to check (see `test-harness/build-freshness.ts`'s module comment).
17
+ * NOTE: accepted tradeoff — this does not cover cadre-host's *own* `src` going
18
+ * stale relative to its own `dist`. The CLI smoke tests spawn `dist/bin/host.js`
19
+ * as a child process, which is a same-package staleness concern, not the
20
+ * cross-package one `BuildTarget` is meant to check (see
21
+ * `test-harness/build-freshness.ts`'s module comment). Consequence: a `src`-only
22
+ * edit to `bin/host.ts` is checked by `bin/__tests__/cli-reference.test.ts`
23
+ * (which reads `src`) but not by the smoke tests (which read `dist`), so run
24
+ * `yarn workspace @serfab/cadre-host build:server` before the suite. Revisit if
25
+ * a stale `dist` ever produces a false green rather than merely a weaker check.
21
26
  *
22
27
  * The guard itself lives at the repo root (`test-harness/build-freshness.ts`),
23
28
  * shared with the other suites that call it; the list of packages below is this
@@ -1,204 +1,204 @@
1
- /**
2
- * HostProcessOrchestrator per-node identity tests.
3
- *
4
- * A donated node must keep the peer id its host cadre approved across restarts,
5
- * and needs a durable on-disk home for its node-local stores (`cadre-cli start`
6
- * opens the file-backed bootstrap-peer / trusted-owner stores only when a
7
- * protobuf identity key file is configured). The orchestrator therefore writes
8
- * `<workdir>/identity.key` once per container and passes it as
9
- * `--identity-protobuf`.
10
- *
11
- * A fake CLI records the `--identity-protobuf` argument it was launched with, so
12
- * the spawn wiring is observable without a real cadre node.
13
- */
14
-
15
- import { afterEach, beforeEach, describe, expect, it } from 'vitest';
16
- import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
17
- import { tmpdir } from 'node:os';
18
- import { join } from 'node:path';
19
-
20
- import { HostProcessOrchestrator, OWNER_CONTAINER_ID } from '../orchestrator/host-process-orchestrator.js';
21
- import { loadIdentity } from '../installer/identity.js';
22
-
23
- // Writes the --identity-protobuf argument (as seen on its own command line) next
24
- // to the startup token, then behaves like a minimal long-lived node.
25
- const FAKE_CLI = `
26
- import fs from 'node:fs';
27
- import path from 'node:path';
28
- const args = process.argv.slice(2);
29
- const get = (flag) => { const i = args.indexOf(flag); return i >= 0 ? args[i + 1] : undefined; };
30
- const tokenPath = get('--startup-token-file');
31
- const token = process.env.CADRE_STARTUP_TOKEN ?? '';
32
- if (tokenPath) {
33
- const dir = path.dirname(tokenPath);
34
- try { fs.writeFileSync(path.join(dir, 'identity-arg.txt'), get('--identity-protobuf') ?? '', 'utf8'); } catch (e) { console.error(e); }
35
- if (token) { try { fs.writeFileSync(tokenPath, token, 'utf8'); } catch (e) { console.error(e); } }
36
- }
37
- process.on('SIGTERM', () => process.exit(0));
38
- setInterval(() => {}, 1 << 30);
39
- `;
40
-
41
- let tmpRoot: string;
42
- let scriptPath: string;
43
- const orchestrators: HostProcessOrchestrator[] = [];
44
-
45
- beforeEach(() => {
46
- tmpRoot = mkdtempSync(join(tmpdir(), 'cadre-host-nodeid-'));
47
- scriptPath = join(tmpRoot, 'fake-cli.mjs');
48
- writeFileSync(scriptPath, FAKE_CLI, 'utf8');
49
- });
50
-
51
- afterEach(async () => {
52
- for (const orch of orchestrators) {
53
- for (const n of orch.listNodes()) {
54
- try { await orch.removeContainer(n.dockerId); } catch { /* ignore */ }
55
- }
56
- }
57
- orchestrators.length = 0;
58
- await sleep(50);
59
- try { rmSync(tmpRoot, { recursive: true, force: true }); } catch { /* ignore */ }
60
- });
61
-
62
- function makeOrchestrator(
63
- rootDir: string,
64
- portRange: { start: number; end: number } = { start: 17500, end: 17999 },
65
- ): HostProcessOrchestrator {
66
- mkdirSync(rootDir, { recursive: true });
67
- const orch = new HostProcessOrchestrator({
68
- rootDir,
69
- portRange,
70
- stopTimeoutMs: 1500,
71
- spawn: { entrypoint: scriptPath },
72
- });
73
- orchestrators.push(orch);
74
- return orch;
75
- }
76
-
77
- async function waitForFile(path: string, timeoutMs = 5000): Promise<string> {
78
- const deadline = Date.now() + timeoutMs;
79
- while (Date.now() < deadline) {
80
- if (existsSync(path)) return readFileSync(path, 'utf8');
81
- await sleep(50);
82
- }
83
- throw new Error(`file never appeared: ${path}`);
84
- }
85
-
86
- function sleep(ms: number): Promise<void> {
87
- return new Promise((res) => setTimeout(res, ms));
88
- }
89
-
90
- describe('HostProcessOrchestrator node identity', () => {
91
- it('passes --identity-protobuf pointing at a real key inside the node workdir', async () => {
92
- const rootDir = join(tmpRoot, 'a');
93
- const orch = makeOrchestrator(rootDir);
94
- await orch.init();
95
-
96
- await orch.createContainer({
97
- containerId: 'donated-1',
98
- partyId: 'foreign-P',
99
- bootstrapNodes: [],
100
- profile: 'storage',
101
- pinnedOwnerKeys: ['key-a'],
102
- });
103
-
104
- const workdir = join(rootDir, 'donated-1');
105
- const arg = await waitForFile(join(workdir, 'identity-arg.txt'));
106
- expect(arg).toBe(join(workdir, 'identity.key'));
107
- // Decodes as a libp2p protobuf private key → the child gets a usable identity.
108
- expect(loadIdentity(arg).peerId).toMatch(/^12D3Koo/);
109
- });
110
-
111
- it('reuses the same identity when the same containerId is spawned again', async () => {
112
- const rootDir = join(tmpRoot, 'b');
113
- const orch = makeOrchestrator(rootDir);
114
- await orch.init();
115
-
116
- const first = await orch.createContainer({
117
- containerId: 'donated-2',
118
- partyId: 'foreign-P',
119
- bootstrapNodes: [],
120
- profile: 'storage',
121
- pinnedOwnerKeys: ['key-a'],
122
- });
123
- const workdir = join(rootDir, 'donated-2');
124
- await waitForFile(join(workdir, 'identity-arg.txt'));
125
- const before = loadIdentity(join(workdir, 'identity.key')).peerId;
126
-
127
- // Stop (not remove — removal deletes the workdir) and re-spawn, as a restart
128
- // of a live loan would.
129
- await orch.stopContainer(first.dockerId);
130
- await orch.createContainer({
131
- containerId: 'donated-2',
132
- partyId: 'foreign-P',
133
- bootstrapNodes: [],
134
- profile: 'storage',
135
- pinnedOwnerKeys: ['key-a'],
136
- });
137
-
138
- const after = loadIdentity(join(workdir, 'identity.key')).peerId;
139
- expect(after).toBe(before);
140
- });
141
-
142
- it('destroys the identity with the workdir when the loan is terminated', async () => {
143
- const rootDir = join(tmpRoot, 'c');
144
- const orch = makeOrchestrator(rootDir);
145
- await orch.init();
146
-
147
- const created = await orch.createContainer({
148
- containerId: 'donated-3',
149
- partyId: 'foreign-P',
150
- bootstrapNodes: [],
151
- profile: 'storage',
152
- pinnedOwnerKeys: ['key-a'],
153
- });
154
- const identityPath = join(rootDir, 'donated-3', 'identity.key');
155
- await waitForFile(identityPath);
156
-
157
- await orch.removeContainer(created.dockerId);
158
- expect(existsSync(identityPath)).toBe(false);
159
- expect(existsSync(join(rootDir, 'donated-3'))).toBe(false);
160
- });
161
-
162
- // The failure the identity step can actually produce is a damaged key file,
163
- // and `DonationService.provision` turns every such throw into an `error`
164
- // record and lets the grantee retry. If the spawn path reserved its ports
165
- // before the step that throws, each retry would burn four more ports out of a
166
- // bounded range until provisioning stopped working altogether.
167
- it('reserves no ports when the identity step fails', async () => {
168
- const rootDir = join(tmpRoot, 'd');
169
- // Exactly one node's worth of ports (health, metrics, p2p, admin).
170
- const orch = makeOrchestrator(rootDir, { start: 18100, end: 18103 });
171
- await orch.init();
172
-
173
- mkdirSync(join(rootDir, 'donated-bad'), { recursive: true });
174
- writeFileSync(join(rootDir, 'donated-bad', 'identity.key'), 'not a protobuf private key', 'utf8');
175
-
176
- const request = {
177
- partyId: 'foreign-P',
178
- bootstrapNodes: [],
179
- profile: 'storage' as const,
180
- pinnedOwnerKeys: ['key-a'],
181
- };
182
- await expect(orch.createContainer({ ...request, containerId: 'donated-bad' })).rejects.toThrow();
183
-
184
- // The whole range is still free, so a healthy container still gets its four.
185
- const ok = await orch.createContainer({ ...request, containerId: 'donated-good' });
186
- expect(ok.p2pPort).toBeGreaterThanOrEqual(18100);
187
- });
188
-
189
- // The owner node carries the HOST's installer identity from <dataDir>, not a
190
- // per-workdir key — a second key in its workdir would mean the host's own
191
- // cadre node quietly changed peer id.
192
- it('leaves the owner node on its own installer identity', async () => {
193
- const rootDir = join(tmpRoot, 'e');
194
- const orch = makeOrchestrator(rootDir);
195
- await orch.init();
196
-
197
- const hostIdentity = join(tmpRoot, 'host-identity.key');
198
- await orch.ensureOwnerNode({ identityPath: hostIdentity, partyId: 'host-P', libp2pPort: 4101 });
199
-
200
- const ownerWorkdir = join(rootDir, OWNER_CONTAINER_ID);
201
- expect(await waitForFile(join(ownerWorkdir, 'identity-arg.txt'))).toBe(hostIdentity);
202
- expect(existsSync(join(ownerWorkdir, 'identity.key'))).toBe(false);
203
- });
204
- });
1
+ /**
2
+ * HostProcessOrchestrator per-node identity tests.
3
+ *
4
+ * A donated node must keep the peer id its host cadre approved across restarts,
5
+ * and needs a durable on-disk home for its node-local stores (`cadre-cli start`
6
+ * opens the file-backed bootstrap-peer / trusted-owner stores only when a
7
+ * protobuf identity key file is configured). The orchestrator therefore writes
8
+ * `<workdir>/identity.key` once per container and passes it as
9
+ * `--identity-file`.
10
+ *
11
+ * A fake CLI records the `--identity-file` argument it was launched with, so
12
+ * the spawn wiring is observable without a real cadre node.
13
+ */
14
+
15
+ import { afterEach, beforeEach, describe, expect, it } from 'vitest';
16
+ import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
17
+ import { tmpdir } from 'node:os';
18
+ import { join } from 'node:path';
19
+
20
+ import { HostProcessOrchestrator, OWNER_CONTAINER_ID } from '../orchestrator/host-process-orchestrator.js';
21
+ import { loadIdentity } from '../installer/identity.js';
22
+
23
+ // Writes the --identity-file argument (as seen on its own command line) next
24
+ // to the startup token, then behaves like a minimal long-lived node.
25
+ const FAKE_CLI = `
26
+ import fs from 'node:fs';
27
+ import path from 'node:path';
28
+ const args = process.argv.slice(2);
29
+ const get = (flag) => { const i = args.indexOf(flag); return i >= 0 ? args[i + 1] : undefined; };
30
+ const tokenPath = get('--startup-token-file');
31
+ const token = process.env.CADRE_STARTUP_TOKEN ?? '';
32
+ if (tokenPath) {
33
+ const dir = path.dirname(tokenPath);
34
+ try { fs.writeFileSync(path.join(dir, 'identity-arg.txt'), get('--identity-file') ?? '', 'utf8'); } catch (e) { console.error(e); }
35
+ if (token) { try { fs.writeFileSync(tokenPath, token, 'utf8'); } catch (e) { console.error(e); } }
36
+ }
37
+ process.on('SIGTERM', () => process.exit(0));
38
+ setInterval(() => {}, 1 << 30);
39
+ `;
40
+
41
+ let tmpRoot: string;
42
+ let scriptPath: string;
43
+ const orchestrators: HostProcessOrchestrator[] = [];
44
+
45
+ beforeEach(() => {
46
+ tmpRoot = mkdtempSync(join(tmpdir(), 'cadre-host-nodeid-'));
47
+ scriptPath = join(tmpRoot, 'fake-cli.mjs');
48
+ writeFileSync(scriptPath, FAKE_CLI, 'utf8');
49
+ });
50
+
51
+ afterEach(async () => {
52
+ for (const orch of orchestrators) {
53
+ for (const n of orch.listNodes()) {
54
+ try { await orch.removeContainer(n.dockerId); } catch { /* ignore */ }
55
+ }
56
+ }
57
+ orchestrators.length = 0;
58
+ await sleep(50);
59
+ try { rmSync(tmpRoot, { recursive: true, force: true }); } catch { /* ignore */ }
60
+ });
61
+
62
+ function makeOrchestrator(
63
+ rootDir: string,
64
+ portRange: { start: number; end: number } = { start: 17500, end: 17999 },
65
+ ): HostProcessOrchestrator {
66
+ mkdirSync(rootDir, { recursive: true });
67
+ const orch = new HostProcessOrchestrator({
68
+ rootDir,
69
+ portRange,
70
+ stopTimeoutMs: 1500,
71
+ spawn: { entrypoint: scriptPath },
72
+ });
73
+ orchestrators.push(orch);
74
+ return orch;
75
+ }
76
+
77
+ async function waitForFile(path: string, timeoutMs = 5000): Promise<string> {
78
+ const deadline = Date.now() + timeoutMs;
79
+ while (Date.now() < deadline) {
80
+ if (existsSync(path)) return readFileSync(path, 'utf8');
81
+ await sleep(50);
82
+ }
83
+ throw new Error(`file never appeared: ${path}`);
84
+ }
85
+
86
+ function sleep(ms: number): Promise<void> {
87
+ return new Promise((res) => setTimeout(res, ms));
88
+ }
89
+
90
+ describe('HostProcessOrchestrator node identity', () => {
91
+ it('passes --identity-file pointing at a real key inside the node workdir', async () => {
92
+ const rootDir = join(tmpRoot, 'a');
93
+ const orch = makeOrchestrator(rootDir);
94
+ await orch.init();
95
+
96
+ await orch.createContainer({
97
+ containerId: 'donated-1',
98
+ partyId: 'foreign-P',
99
+ bootstrapNodes: [],
100
+ profile: 'storage',
101
+ pinnedOwnerKeys: ['key-a'],
102
+ });
103
+
104
+ const workdir = join(rootDir, 'donated-1');
105
+ const arg = await waitForFile(join(workdir, 'identity-arg.txt'));
106
+ expect(arg).toBe(join(workdir, 'identity.key'));
107
+ // Decodes as a libp2p protobuf private key → the child gets a usable identity.
108
+ expect(loadIdentity(arg).peerId).toMatch(/^12D3Koo/);
109
+ });
110
+
111
+ it('reuses the same identity when the same containerId is spawned again', async () => {
112
+ const rootDir = join(tmpRoot, 'b');
113
+ const orch = makeOrchestrator(rootDir);
114
+ await orch.init();
115
+
116
+ const first = await orch.createContainer({
117
+ containerId: 'donated-2',
118
+ partyId: 'foreign-P',
119
+ bootstrapNodes: [],
120
+ profile: 'storage',
121
+ pinnedOwnerKeys: ['key-a'],
122
+ });
123
+ const workdir = join(rootDir, 'donated-2');
124
+ await waitForFile(join(workdir, 'identity-arg.txt'));
125
+ const before = loadIdentity(join(workdir, 'identity.key')).peerId;
126
+
127
+ // Stop (not remove — removal deletes the workdir) and re-spawn, as a restart
128
+ // of a live loan would.
129
+ await orch.stopContainer(first.dockerId);
130
+ await orch.createContainer({
131
+ containerId: 'donated-2',
132
+ partyId: 'foreign-P',
133
+ bootstrapNodes: [],
134
+ profile: 'storage',
135
+ pinnedOwnerKeys: ['key-a'],
136
+ });
137
+
138
+ const after = loadIdentity(join(workdir, 'identity.key')).peerId;
139
+ expect(after).toBe(before);
140
+ });
141
+
142
+ it('destroys the identity with the workdir when the loan is terminated', async () => {
143
+ const rootDir = join(tmpRoot, 'c');
144
+ const orch = makeOrchestrator(rootDir);
145
+ await orch.init();
146
+
147
+ const created = await orch.createContainer({
148
+ containerId: 'donated-3',
149
+ partyId: 'foreign-P',
150
+ bootstrapNodes: [],
151
+ profile: 'storage',
152
+ pinnedOwnerKeys: ['key-a'],
153
+ });
154
+ const identityPath = join(rootDir, 'donated-3', 'identity.key');
155
+ await waitForFile(identityPath);
156
+
157
+ await orch.removeContainer(created.dockerId);
158
+ expect(existsSync(identityPath)).toBe(false);
159
+ expect(existsSync(join(rootDir, 'donated-3'))).toBe(false);
160
+ });
161
+
162
+ // The failure the identity step can actually produce is a damaged key file,
163
+ // and `DonationService.provision` turns every such throw into an `error`
164
+ // record and lets the grantee retry. If the spawn path reserved its ports
165
+ // before the step that throws, each retry would burn four more ports out of a
166
+ // bounded range until provisioning stopped working altogether.
167
+ it('reserves no ports when the identity step fails', async () => {
168
+ const rootDir = join(tmpRoot, 'd');
169
+ // Exactly one node's worth of ports (health, metrics, p2p, admin).
170
+ const orch = makeOrchestrator(rootDir, { start: 18100, end: 18103 });
171
+ await orch.init();
172
+
173
+ mkdirSync(join(rootDir, 'donated-bad'), { recursive: true });
174
+ writeFileSync(join(rootDir, 'donated-bad', 'identity.key'), 'not a protobuf private key', 'utf8');
175
+
176
+ const request = {
177
+ partyId: 'foreign-P',
178
+ bootstrapNodes: [],
179
+ profile: 'storage' as const,
180
+ pinnedOwnerKeys: ['key-a'],
181
+ };
182
+ await expect(orch.createContainer({ ...request, containerId: 'donated-bad' })).rejects.toThrow();
183
+
184
+ // The whole range is still free, so a healthy container still gets its four.
185
+ const ok = await orch.createContainer({ ...request, containerId: 'donated-good' });
186
+ expect(ok.p2pPort).toBeGreaterThanOrEqual(18100);
187
+ });
188
+
189
+ // The owner node carries the HOST's installer identity from <dataDir>, not a
190
+ // per-workdir key — a second key in its workdir would mean the host's own
191
+ // cadre node quietly changed peer id.
192
+ it('leaves the owner node on its own installer identity', async () => {
193
+ const rootDir = join(tmpRoot, 'e');
194
+ const orch = makeOrchestrator(rootDir);
195
+ await orch.init();
196
+
197
+ const hostIdentity = join(tmpRoot, 'host-identity.key');
198
+ await orch.ensureOwnerNode({ identityPath: hostIdentity, partyId: 'host-P', libp2pPort: 4101 });
199
+
200
+ const ownerWorkdir = join(rootDir, OWNER_CONTAINER_ID);
201
+ expect(await waitForFile(join(ownerWorkdir, 'identity-arg.txt'))).toBe(hostIdentity);
202
+ expect(existsSync(join(ownerWorkdir, 'identity.key'))).toBe(false);
203
+ });
204
+ });