@serfab/cadre-host 1.9.0 → 1.11.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.
@@ -1,203 +1,203 @@
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
- import { removeAllNodes } from './orchestrator-teardown.js';
23
-
24
- // Writes the --identity-file argument (as seen on its own command line) next
25
- // to the startup token, then behaves like a minimal long-lived node.
26
- const FAKE_CLI = `
27
- import fs from 'node:fs';
28
- import path from 'node:path';
29
- const args = process.argv.slice(2);
30
- const get = (flag) => { const i = args.indexOf(flag); return i >= 0 ? args[i + 1] : undefined; };
31
- const tokenPath = get('--startup-token-file');
32
- const token = process.env.CADRE_STARTUP_TOKEN ?? '';
33
- if (tokenPath) {
34
- const dir = path.dirname(tokenPath);
35
- try { fs.writeFileSync(path.join(dir, 'identity-arg.txt'), get('--identity-file') ?? '', 'utf8'); } catch (e) { console.error(e); }
36
- if (token) { try { fs.writeFileSync(tokenPath, token, 'utf8'); } catch (e) { console.error(e); } }
37
- }
38
- process.on('SIGTERM', () => process.exit(0));
39
- setInterval(() => {}, 1 << 30);
40
- `;
41
-
42
- let tmpRoot: string;
43
- let scriptPath: string;
44
- const orchestrators: HostProcessOrchestrator[] = [];
45
-
46
- beforeEach(() => {
47
- tmpRoot = mkdtempSync(join(tmpdir(), 'cadre-host-nodeid-'));
48
- scriptPath = join(tmpRoot, 'fake-cli.mjs');
49
- writeFileSync(scriptPath, FAKE_CLI, 'utf8');
50
- });
51
-
52
- afterEach(async () => {
53
- try {
54
- await removeAllNodes(orchestrators);
55
- } finally {
56
- await sleep(50);
57
- try { rmSync(tmpRoot, { recursive: true, force: true }); } catch { /* ignore */ }
58
- }
59
- });
60
-
61
- function makeOrchestrator(
62
- rootDir: string,
63
- portRange: { start: number; end: number } = { start: 17500, end: 17999 },
64
- ): HostProcessOrchestrator {
65
- mkdirSync(rootDir, { recursive: true });
66
- const orch = new HostProcessOrchestrator({
67
- rootDir,
68
- portRange,
69
- stopTimeoutMs: 1500,
70
- spawn: { entrypoint: scriptPath },
71
- });
72
- orchestrators.push(orch);
73
- return orch;
74
- }
75
-
76
- async function waitForFile(path: string, timeoutMs = 5000): Promise<string> {
77
- const deadline = Date.now() + timeoutMs;
78
- while (Date.now() < deadline) {
79
- if (existsSync(path)) return readFileSync(path, 'utf8');
80
- await sleep(50);
81
- }
82
- throw new Error(`file never appeared: ${path}`);
83
- }
84
-
85
- function sleep(ms: number): Promise<void> {
86
- return new Promise((res) => setTimeout(res, ms));
87
- }
88
-
89
- describe('HostProcessOrchestrator node identity', () => {
90
- it('passes --identity-file pointing at a real key inside the node workdir', async () => {
91
- const rootDir = join(tmpRoot, 'a');
92
- const orch = makeOrchestrator(rootDir);
93
- await orch.init();
94
-
95
- await orch.createContainer({
96
- containerId: 'donated-1',
97
- partyId: 'foreign-P',
98
- bootstrapNodes: [],
99
- profile: 'storage',
100
- pinnedOwnerKeys: ['key-a'],
101
- });
102
-
103
- const workdir = join(rootDir, 'donated-1');
104
- const arg = await waitForFile(join(workdir, 'identity-arg.txt'));
105
- expect(arg).toBe(join(workdir, 'identity.key'));
106
- // Decodes as a libp2p protobuf private key → the child gets a usable identity.
107
- expect(loadIdentity(arg).peerId).toMatch(/^12D3Koo/);
108
- });
109
-
110
- it('reuses the same identity when the same containerId is spawned again', async () => {
111
- const rootDir = join(tmpRoot, 'b');
112
- const orch = makeOrchestrator(rootDir);
113
- await orch.init();
114
-
115
- const first = await orch.createContainer({
116
- containerId: 'donated-2',
117
- partyId: 'foreign-P',
118
- bootstrapNodes: [],
119
- profile: 'storage',
120
- pinnedOwnerKeys: ['key-a'],
121
- });
122
- const workdir = join(rootDir, 'donated-2');
123
- await waitForFile(join(workdir, 'identity-arg.txt'));
124
- const before = loadIdentity(join(workdir, 'identity.key')).peerId;
125
-
126
- // Stop (not remove — removal deletes the workdir) and re-spawn, as a restart
127
- // of a live loan would.
128
- await orch.stopContainer(first.dockerId);
129
- await orch.createContainer({
130
- containerId: 'donated-2',
131
- partyId: 'foreign-P',
132
- bootstrapNodes: [],
133
- profile: 'storage',
134
- pinnedOwnerKeys: ['key-a'],
135
- });
136
-
137
- const after = loadIdentity(join(workdir, 'identity.key')).peerId;
138
- expect(after).toBe(before);
139
- });
140
-
141
- it('destroys the identity with the workdir when the loan is terminated', async () => {
142
- const rootDir = join(tmpRoot, 'c');
143
- const orch = makeOrchestrator(rootDir);
144
- await orch.init();
145
-
146
- const created = await orch.createContainer({
147
- containerId: 'donated-3',
148
- partyId: 'foreign-P',
149
- bootstrapNodes: [],
150
- profile: 'storage',
151
- pinnedOwnerKeys: ['key-a'],
152
- });
153
- const identityPath = join(rootDir, 'donated-3', 'identity.key');
154
- await waitForFile(identityPath);
155
-
156
- await orch.removeContainer(created.dockerId);
157
- expect(existsSync(identityPath)).toBe(false);
158
- expect(existsSync(join(rootDir, 'donated-3'))).toBe(false);
159
- });
160
-
161
- // The failure the identity step can actually produce is a damaged key file,
162
- // and `DonationService.provision` turns every such throw into an `error`
163
- // record and lets the grantee retry. If the spawn path reserved its ports
164
- // before the step that throws, each retry would burn five more ports out of a
165
- // bounded range until provisioning stopped working altogether.
166
- it('reserves no ports when the identity step fails', async () => {
167
- const rootDir = join(tmpRoot, 'd');
168
- // Exactly one node's worth of ports (health, metrics, p2p, admin, ws).
169
- const orch = makeOrchestrator(rootDir, { start: 18100, end: 18104 });
170
- await orch.init();
171
-
172
- mkdirSync(join(rootDir, 'donated-bad'), { recursive: true });
173
- writeFileSync(join(rootDir, 'donated-bad', 'identity.key'), 'not a protobuf private key', 'utf8');
174
-
175
- const request = {
176
- partyId: 'foreign-P',
177
- bootstrapNodes: [],
178
- profile: 'storage' as const,
179
- pinnedOwnerKeys: ['key-a'],
180
- };
181
- await expect(orch.createContainer({ ...request, containerId: 'donated-bad' })).rejects.toThrow();
182
-
183
- // The whole range is still free, so a healthy container still gets its five.
184
- const ok = await orch.createContainer({ ...request, containerId: 'donated-good' });
185
- expect(ok.p2pPort).toBeGreaterThanOrEqual(18100);
186
- });
187
-
188
- // The owner node carries the HOST's installer identity from <dataDir>, not a
189
- // per-workdir key — a second key in its workdir would mean the host's own
190
- // cadre node quietly changed peer id.
191
- it('leaves the owner node on its own installer identity', async () => {
192
- const rootDir = join(tmpRoot, 'e');
193
- const orch = makeOrchestrator(rootDir);
194
- await orch.init();
195
-
196
- const hostIdentity = join(tmpRoot, 'host-identity.key');
197
- await orch.ensureOwnerNode({ identityPath: hostIdentity, partyId: 'host-P', libp2pPort: 4101 });
198
-
199
- const ownerWorkdir = join(rootDir, OWNER_CONTAINER_ID);
200
- expect(await waitForFile(join(ownerWorkdir, 'identity-arg.txt'))).toBe(hostIdentity);
201
- expect(existsSync(join(ownerWorkdir, 'identity.key'))).toBe(false);
202
- });
203
- });
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
+ import { removeAllNodes } from './orchestrator-teardown.js';
23
+
24
+ // Writes the --identity-file argument (as seen on its own command line) next
25
+ // to the startup token, then behaves like a minimal long-lived node.
26
+ const FAKE_CLI = `
27
+ import fs from 'node:fs';
28
+ import path from 'node:path';
29
+ const args = process.argv.slice(2);
30
+ const get = (flag) => { const i = args.indexOf(flag); return i >= 0 ? args[i + 1] : undefined; };
31
+ const tokenPath = get('--startup-token-file');
32
+ const token = process.env.CADRE_STARTUP_TOKEN ?? '';
33
+ if (tokenPath) {
34
+ const dir = path.dirname(tokenPath);
35
+ try { fs.writeFileSync(path.join(dir, 'identity-arg.txt'), get('--identity-file') ?? '', 'utf8'); } catch (e) { console.error(e); }
36
+ if (token) { try { fs.writeFileSync(tokenPath, token, 'utf8'); } catch (e) { console.error(e); } }
37
+ }
38
+ process.on('SIGTERM', () => process.exit(0));
39
+ setInterval(() => {}, 1 << 30);
40
+ `;
41
+
42
+ let tmpRoot: string;
43
+ let scriptPath: string;
44
+ const orchestrators: HostProcessOrchestrator[] = [];
45
+
46
+ beforeEach(() => {
47
+ tmpRoot = mkdtempSync(join(tmpdir(), 'cadre-host-nodeid-'));
48
+ scriptPath = join(tmpRoot, 'fake-cli.mjs');
49
+ writeFileSync(scriptPath, FAKE_CLI, 'utf8');
50
+ });
51
+
52
+ afterEach(async () => {
53
+ try {
54
+ await removeAllNodes(orchestrators);
55
+ } finally {
56
+ await sleep(50);
57
+ try { rmSync(tmpRoot, { recursive: true, force: true }); } catch { /* ignore */ }
58
+ }
59
+ });
60
+
61
+ function makeOrchestrator(
62
+ rootDir: string,
63
+ portRange: { start: number; end: number } = { start: 17500, end: 17999 },
64
+ ): HostProcessOrchestrator {
65
+ mkdirSync(rootDir, { recursive: true });
66
+ const orch = new HostProcessOrchestrator({
67
+ rootDir,
68
+ portRange,
69
+ stopTimeoutMs: 1500,
70
+ spawn: { entrypoint: scriptPath },
71
+ });
72
+ orchestrators.push(orch);
73
+ return orch;
74
+ }
75
+
76
+ async function waitForFile(path: string, timeoutMs = 5000): Promise<string> {
77
+ const deadline = Date.now() + timeoutMs;
78
+ while (Date.now() < deadline) {
79
+ if (existsSync(path)) return readFileSync(path, 'utf8');
80
+ await sleep(50);
81
+ }
82
+ throw new Error(`file never appeared: ${path}`);
83
+ }
84
+
85
+ function sleep(ms: number): Promise<void> {
86
+ return new Promise((res) => setTimeout(res, ms));
87
+ }
88
+
89
+ describe('HostProcessOrchestrator node identity', () => {
90
+ it('passes --identity-file pointing at a real key inside the node workdir', async () => {
91
+ const rootDir = join(tmpRoot, 'a');
92
+ const orch = makeOrchestrator(rootDir);
93
+ await orch.init();
94
+
95
+ await orch.createContainer({
96
+ containerId: 'donated-1',
97
+ partyId: 'foreign-P',
98
+ bootstrapNodes: [],
99
+ profile: 'storage',
100
+ pinnedOwnerKeys: ['key-a'],
101
+ });
102
+
103
+ const workdir = join(rootDir, 'donated-1');
104
+ const arg = await waitForFile(join(workdir, 'identity-arg.txt'));
105
+ expect(arg).toBe(join(workdir, 'identity.key'));
106
+ // Decodes as a libp2p protobuf private key → the child gets a usable identity.
107
+ expect(loadIdentity(arg).peerId).toMatch(/^12D3Koo/);
108
+ });
109
+
110
+ it('reuses the same identity when the same containerId is spawned again', async () => {
111
+ const rootDir = join(tmpRoot, 'b');
112
+ const orch = makeOrchestrator(rootDir);
113
+ await orch.init();
114
+
115
+ const first = await orch.createContainer({
116
+ containerId: 'donated-2',
117
+ partyId: 'foreign-P',
118
+ bootstrapNodes: [],
119
+ profile: 'storage',
120
+ pinnedOwnerKeys: ['key-a'],
121
+ });
122
+ const workdir = join(rootDir, 'donated-2');
123
+ await waitForFile(join(workdir, 'identity-arg.txt'));
124
+ const before = loadIdentity(join(workdir, 'identity.key')).peerId;
125
+
126
+ // Stop (not remove — removal deletes the workdir) and re-spawn, as a restart
127
+ // of a live loan would.
128
+ await orch.stopContainer(first.dockerId);
129
+ await orch.createContainer({
130
+ containerId: 'donated-2',
131
+ partyId: 'foreign-P',
132
+ bootstrapNodes: [],
133
+ profile: 'storage',
134
+ pinnedOwnerKeys: ['key-a'],
135
+ });
136
+
137
+ const after = loadIdentity(join(workdir, 'identity.key')).peerId;
138
+ expect(after).toBe(before);
139
+ });
140
+
141
+ it('destroys the identity with the workdir when the loan is terminated', async () => {
142
+ const rootDir = join(tmpRoot, 'c');
143
+ const orch = makeOrchestrator(rootDir);
144
+ await orch.init();
145
+
146
+ const created = await orch.createContainer({
147
+ containerId: 'donated-3',
148
+ partyId: 'foreign-P',
149
+ bootstrapNodes: [],
150
+ profile: 'storage',
151
+ pinnedOwnerKeys: ['key-a'],
152
+ });
153
+ const identityPath = join(rootDir, 'donated-3', 'identity.key');
154
+ await waitForFile(identityPath);
155
+
156
+ await orch.removeContainer(created.dockerId);
157
+ expect(existsSync(identityPath)).toBe(false);
158
+ expect(existsSync(join(rootDir, 'donated-3'))).toBe(false);
159
+ });
160
+
161
+ // The failure the identity step can actually produce is a damaged key file,
162
+ // and `DonationService.provision` turns every such throw into an `error`
163
+ // record and lets the grantee retry. If the spawn path reserved its ports
164
+ // before the step that throws, each retry would burn five more ports out of a
165
+ // bounded range until provisioning stopped working altogether.
166
+ it('reserves no ports when the identity step fails', async () => {
167
+ const rootDir = join(tmpRoot, 'd');
168
+ // Exactly one node's worth of ports (health, metrics, p2p, admin, ws).
169
+ const orch = makeOrchestrator(rootDir, { start: 18100, end: 18104 });
170
+ await orch.init();
171
+
172
+ mkdirSync(join(rootDir, 'donated-bad'), { recursive: true });
173
+ writeFileSync(join(rootDir, 'donated-bad', 'identity.key'), 'not a protobuf private key', 'utf8');
174
+
175
+ const request = {
176
+ partyId: 'foreign-P',
177
+ bootstrapNodes: [],
178
+ profile: 'storage' as const,
179
+ pinnedOwnerKeys: ['key-a'],
180
+ };
181
+ await expect(orch.createContainer({ ...request, containerId: 'donated-bad' })).rejects.toThrow();
182
+
183
+ // The whole range is still free, so a healthy container still gets its five.
184
+ const ok = await orch.createContainer({ ...request, containerId: 'donated-good' });
185
+ expect(ok.p2pPort).toBeGreaterThanOrEqual(18100);
186
+ });
187
+
188
+ // The owner node carries the HOST's installer identity from <dataDir>, not a
189
+ // per-workdir key — a second key in its workdir would mean the host's own
190
+ // cadre node quietly changed peer id.
191
+ it('leaves the owner node on its own installer identity', async () => {
192
+ const rootDir = join(tmpRoot, 'e');
193
+ const orch = makeOrchestrator(rootDir);
194
+ await orch.init();
195
+
196
+ const hostIdentity = join(tmpRoot, 'host-identity.key');
197
+ await orch.ensureOwnerNode({ identityPath: hostIdentity, partyId: 'host-P', libp2pPort: 4101 });
198
+
199
+ const ownerWorkdir = join(rootDir, OWNER_CONTAINER_ID);
200
+ expect(await waitForFile(join(ownerWorkdir, 'identity-arg.txt'))).toBe(hostIdentity);
201
+ expect(existsSync(join(ownerWorkdir, 'identity.key'))).toBe(false);
202
+ });
203
+ });