@serfab/cadre-host 0.8.1 → 0.9.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.
- package/dist/auth/trust-circle.d.ts +5 -5
- package/dist/auth/trust-circle.js +10 -10
- package/dist/auth/trust-circle.js.map +1 -1
- package/dist/auth/types.d.ts +2 -2
- package/dist/bin/host.js +255 -54
- package/dist/bin/host.js.map +1 -1
- package/dist/donation/donation-service.d.ts +136 -0
- package/dist/donation/donation-service.js +306 -0
- package/dist/donation/donation-service.js.map +1 -0
- package/dist/donation/donation-store.d.ts +42 -0
- package/dist/donation/donation-store.js +141 -0
- package/dist/donation/donation-store.js.map +1 -0
- package/dist/donation/grant-service.d.ts +71 -0
- package/dist/donation/grant-service.js +163 -0
- package/dist/donation/grant-service.js.map +1 -0
- package/dist/donation/grant-store.d.ts +38 -0
- package/dist/donation/grant-store.js +135 -0
- package/dist/donation/grant-store.js.map +1 -0
- package/dist/donation/index.d.ts +18 -0
- package/dist/donation/index.js +16 -0
- package/dist/donation/index.js.map +1 -0
- package/dist/donation/types.d.ts +175 -0
- package/dist/donation/types.js +40 -0
- package/dist/donation/types.js.map +1 -0
- package/dist/index.d.ts +6 -4
- package/dist/index.js +4 -2
- package/dist/index.js.map +1 -1
- package/dist/installer/config.d.ts +26 -0
- package/dist/installer/config.js +13 -0
- package/dist/installer/config.js.map +1 -1
- package/dist/installer/index.d.ts +2 -0
- package/dist/installer/index.js +3 -0
- package/dist/installer/index.js.map +1 -1
- package/dist/installer/wizard.d.ts +4 -0
- package/dist/installer/wizard.js +5 -1
- package/dist/installer/wizard.js.map +1 -1
- package/dist/nat/nat-service.d.ts +5 -5
- package/dist/nat/nat-service.js +9 -9
- package/dist/nat/nat-service.js.map +1 -1
- package/dist/nat/types.d.ts +1 -1
- package/dist/orchestrator/host-process-orchestrator.d.ts +35 -27
- package/dist/orchestrator/host-process-orchestrator.js +79 -53
- package/dist/orchestrator/host-process-orchestrator.js.map +1 -1
- package/dist/orchestrator/index.d.ts +3 -3
- package/dist/orchestrator/index.js +1 -1
- package/dist/orchestrator/index.js.map +1 -1
- package/dist/orchestrator/state-store.d.ts +5 -5
- package/dist/orchestrator/state-store.js +2 -2
- package/dist/orchestrator/state-store.js.map +1 -1
- package/dist/orchestrator/types.d.ts +8 -8
- package/dist/owner/index.d.ts +11 -0
- package/dist/owner/index.js +11 -0
- package/dist/owner/index.js.map +1 -0
- package/dist/{authority/authority-node-client.d.ts → owner/owner-node-client.d.ts} +32 -14
- package/dist/{authority/authority-node-client.js → owner/owner-node-client.js} +39 -15
- package/dist/owner/owner-node-client.js.map +1 -0
- package/dist/server/error-handler.d.ts +15 -0
- package/dist/server/error-handler.js +41 -0
- package/dist/server/error-handler.js.map +1 -1
- package/dist/server/index.d.ts +26 -2
- package/dist/server/index.js +37 -16
- package/dist/server/index.js.map +1 -1
- package/dist/server/routes/grants-admin.d.ts +17 -0
- package/dist/server/routes/grants-admin.js +32 -0
- package/dist/server/routes/grants-admin.js.map +1 -0
- package/dist/server/routes/grants.d.ts +28 -0
- package/dist/server/routes/grants.js +163 -0
- package/dist/server/routes/grants.js.map +1 -0
- package/dist/server/routes/nodes.d.ts +5 -4
- package/dist/server/routes/nodes.js +19 -18
- package/dist/server/routes/nodes.js.map +1 -1
- package/dist/server/routes/settings.d.ts +6 -1
- package/dist/server/routes/settings.js +5 -2
- package/dist/server/routes/settings.js.map +1 -1
- package/dist/server/routes/status.d.ts +8 -4
- package/dist/server/routes/status.js +9 -4
- package/dist/server/routes/status.js.map +1 -1
- package/dist/update/release-key.js +7 -5
- package/dist/update/release-key.js.map +1 -1
- package/package.json +4 -4
- package/src/__tests__/{host-authority.smoke.test.ts → host-owner.smoke.test.ts} +7 -7
- package/src/__tests__/{orchestrator-authority.test.ts → orchestrator-owner.test.ts} +33 -33
- package/src/__tests__/orchestrator-pin-keys.test.ts +153 -0
- package/src/__tests__/orchestrator-push.test.ts +10 -10
- package/src/auth/__tests__/trust-circle-integration.test.ts +11 -11
- package/src/auth/__tests__/trust-circle.test.ts +10 -10
- package/src/auth/trust-circle.ts +10 -10
- package/src/auth/types.ts +2 -2
- package/src/bin/host.ts +273 -53
- package/src/donation/__tests__/donation-service.test.ts +290 -0
- package/src/donation/__tests__/grant-service.test.ts +213 -0
- package/src/donation/__tests__/grant-store.test.ts +113 -0
- package/src/donation/donation-service.ts +392 -0
- package/src/donation/donation-store.ts +164 -0
- package/src/donation/grant-service.ts +200 -0
- package/src/donation/grant-store.ts +152 -0
- package/src/donation/index.ts +49 -0
- package/src/donation/types.ts +220 -0
- package/src/index.ts +37 -5
- package/src/installer/__tests__/config.test.ts +27 -1
- package/src/installer/__tests__/installer.smoke.test.ts +31 -1
- package/src/installer/__tests__/wizard.test.ts +5 -1
- package/src/installer/config.ts +34 -0
- package/src/installer/index.ts +5 -0
- package/src/installer/wizard.ts +12 -1
- package/src/nat/__tests__/nat-service.test.ts +5 -5
- package/src/nat/nat-service.ts +10 -10
- package/src/nat/types.ts +1 -1
- package/src/orchestrator/host-process-orchestrator.ts +88 -60
- package/src/orchestrator/index.ts +3 -3
- package/src/orchestrator/state-store.ts +7 -7
- package/src/orchestrator/types.ts +8 -8
- package/src/{authority/__tests__/authority-node-client.test.ts → owner/__tests__/owner-node-client.test.ts} +45 -17
- package/src/owner/index.ts +12 -0
- package/src/{authority/authority-node-client.ts → owner/owner-node-client.ts} +55 -21
- package/src/server/__tests__/error-handler.test.ts +22 -0
- package/src/server/__tests__/grants-admin-route.test.ts +73 -0
- package/src/server/__tests__/grants-route.test.ts +189 -0
- package/src/server/__tests__/nodes-route.test.ts +25 -25
- package/src/server/__tests__/server.smoke.test.ts +75 -0
- package/src/server/__tests__/settings-route.test.ts +17 -0
- package/src/server/__tests__/status-route.test.ts +21 -0
- package/src/server/error-handler.ts +43 -0
- package/src/server/index.ts +62 -17
- package/src/server/routes/grants-admin.ts +41 -0
- package/src/server/routes/grants.ts +186 -0
- package/src/server/routes/nodes.ts +19 -18
- package/src/server/routes/settings.ts +11 -3
- package/src/server/routes/status.ts +18 -8
- package/src/update/__tests__/release-key.test.ts +16 -3
- package/src/update/release-key.ts +7 -5
- package/dist/authority/authority-node-client.js.map +0 -1
- package/dist/authority/index.d.ts +0 -11
- package/dist/authority/index.js +0 -11
- package/dist/authority/index.js.map +0 -1
- package/src/authority/index.ts +0 -12
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* HostProcessOrchestrator
|
|
2
|
+
* HostProcessOrchestrator owner-node lifecycle tests.
|
|
3
3
|
*
|
|
4
4
|
* Uses the `spawn.entrypoint` hook with a fake CLI that writes its startup
|
|
5
5
|
* token and binds a minimal `/admin/identity` endpoint on the admin port —
|
|
6
6
|
* enough to validate spawn, idempotency, admin-port allocation/persistence,
|
|
7
|
-
* the end-to-end
|
|
7
|
+
* the end-to-end OwnerNodeClient round-trip, and init() re-attach.
|
|
8
8
|
*/
|
|
9
9
|
|
|
10
10
|
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
|
|
@@ -12,13 +12,13 @@ import { mkdtempSync, mkdirSync, rmSync, writeFileSync } from 'node:fs';
|
|
|
12
12
|
import { tmpdir } from 'node:os';
|
|
13
13
|
import { join } from 'node:path';
|
|
14
14
|
|
|
15
|
-
import { HostProcessOrchestrator,
|
|
15
|
+
import { HostProcessOrchestrator, OWNER_CONTAINER_ID } from '../orchestrator/host-process-orchestrator.js';
|
|
16
16
|
import { StateStore } from '../orchestrator/state-store.js';
|
|
17
17
|
import { isPidAlive } from '../orchestrator/pid-liveness.js';
|
|
18
18
|
import { decodeDockerId } from '../orchestrator/types.js';
|
|
19
|
-
import {
|
|
19
|
+
import { OwnerNodeClient } from '../owner/owner-node-client.js';
|
|
20
20
|
|
|
21
|
-
const
|
|
21
|
+
const FAKE_OWNER_CLI = `
|
|
22
22
|
import fs from 'node:fs';
|
|
23
23
|
import http from 'node:http';
|
|
24
24
|
|
|
@@ -55,7 +55,7 @@ if (adminPort && token) {
|
|
|
55
55
|
}
|
|
56
56
|
|
|
57
57
|
process.on('SIGTERM', () => process.exit(0));
|
|
58
|
-
console.log('fake
|
|
58
|
+
console.log('fake owner up');
|
|
59
59
|
setInterval(() => {}, 1 << 30);
|
|
60
60
|
`;
|
|
61
61
|
|
|
@@ -65,13 +65,13 @@ const orchestrators: HostProcessOrchestrator[] = [];
|
|
|
65
65
|
|
|
66
66
|
beforeEach(() => {
|
|
67
67
|
tmpRoot = mkdtempSync(join(tmpdir(), 'cadre-host-auth-'));
|
|
68
|
-
scriptPath = join(tmpRoot, 'fake-
|
|
69
|
-
writeFileSync(scriptPath,
|
|
68
|
+
scriptPath = join(tmpRoot, 'fake-owner.mjs');
|
|
69
|
+
writeFileSync(scriptPath, FAKE_OWNER_CLI, 'utf8');
|
|
70
70
|
});
|
|
71
71
|
|
|
72
72
|
afterEach(async () => {
|
|
73
73
|
for (const orch of orchestrators) {
|
|
74
|
-
try { await orch.
|
|
74
|
+
try { await orch.stopOwnerNode(); } catch { /* ignore */ }
|
|
75
75
|
for (const dockerId of listDockerIds(orch)) {
|
|
76
76
|
try { await orch.removeContainer(dockerId); } catch { /* ignore */ }
|
|
77
77
|
}
|
|
@@ -113,66 +113,66 @@ function sleep(ms: number): Promise<void> {
|
|
|
113
113
|
|
|
114
114
|
const CFG = { identityPath: 'C:/fake/identity.key', partyId: 'install-id-123', libp2pPort: 4555 };
|
|
115
115
|
|
|
116
|
-
describe('HostProcessOrchestrator.
|
|
117
|
-
it('spawns the
|
|
116
|
+
describe('HostProcessOrchestrator.ensureOwnerNode', () => {
|
|
117
|
+
it('spawns the owner node, allocates+persists an admin port', async () => {
|
|
118
118
|
const orch = makeOrchestrator(join(tmpRoot, 'a'));
|
|
119
119
|
await orch.init();
|
|
120
|
-
const node = await orch.
|
|
120
|
+
const node = await orch.ensureOwnerNode(CFG);
|
|
121
121
|
|
|
122
|
-
expect(node.id).toBe(
|
|
123
|
-
expect(node.
|
|
122
|
+
expect(node.id).toBe(OWNER_CONTAINER_ID);
|
|
123
|
+
expect(node.owner).toBe(true);
|
|
124
124
|
expect(node.ports.admin).toBeGreaterThanOrEqual(18500);
|
|
125
125
|
expect(node.ports.p2p).toBe(4555);
|
|
126
126
|
|
|
127
|
-
const endpoint = orch.
|
|
127
|
+
const endpoint = orch.getOwnerAdminEndpoint();
|
|
128
128
|
expect(endpoint).toBeDefined();
|
|
129
129
|
expect(endpoint!.baseUrl).toBe(`http://127.0.0.1:${node.ports.admin}`);
|
|
130
130
|
|
|
131
|
-
// Persisted with admin port +
|
|
131
|
+
// Persisted with admin port + owner flag + ownerConfig.
|
|
132
132
|
const persisted = new StateStore(join(tmpRoot, 'a')).load();
|
|
133
|
-
expect(persisted.
|
|
134
|
-
const ph = persisted.handles.find((h) => h.containerId ===
|
|
135
|
-
expect(ph?.
|
|
133
|
+
expect(persisted.ownerConfig).toMatchObject({ partyId: 'install-id-123', libp2pPort: 4555 });
|
|
134
|
+
const ph = persisted.handles.find((h) => h.containerId === OWNER_CONTAINER_ID);
|
|
135
|
+
expect(ph?.owner).toBe(true);
|
|
136
136
|
expect(typeof ph?.ports.admin).toBe('number');
|
|
137
137
|
});
|
|
138
138
|
|
|
139
139
|
it('is idempotent — a second call does not spawn a second child', async () => {
|
|
140
140
|
const orch = makeOrchestrator(join(tmpRoot, 'b'));
|
|
141
141
|
await orch.init();
|
|
142
|
-
const first = await orch.
|
|
142
|
+
const first = await orch.ensureOwnerNode(CFG);
|
|
143
143
|
await waitFor(() => orch.isRunning(first.dockerId));
|
|
144
144
|
|
|
145
|
-
const second = await orch.
|
|
145
|
+
const second = await orch.ensureOwnerNode(CFG);
|
|
146
146
|
expect(second.dockerId).toBe(first.dockerId);
|
|
147
147
|
expect(orch.listNodes()).toHaveLength(1);
|
|
148
148
|
});
|
|
149
149
|
|
|
150
|
-
it('the
|
|
150
|
+
it('the OwnerNodeClient reaches the spawned admin channel', async () => {
|
|
151
151
|
const orch = makeOrchestrator(join(tmpRoot, 'c'));
|
|
152
152
|
await orch.init();
|
|
153
|
-
const node = await orch.
|
|
153
|
+
const node = await orch.ensureOwnerNode(CFG);
|
|
154
154
|
await waitFor(() => orch.isRunning(node.dockerId));
|
|
155
155
|
|
|
156
|
-
const client = new
|
|
156
|
+
const client = new OwnerNodeClient(() => orch.getOwnerAdminEndpoint());
|
|
157
157
|
await waitFor(async () => {
|
|
158
158
|
try { return (await client.getPeerId()) === 'FAKEPEER'; } catch { return false; }
|
|
159
159
|
});
|
|
160
160
|
expect(await client.getPeerId()).toBe('FAKEPEER');
|
|
161
161
|
});
|
|
162
162
|
|
|
163
|
-
it('
|
|
163
|
+
it('getOwnerAdminEndpoint() survives an init() re-attach', async () => {
|
|
164
164
|
const rootDir = join(tmpRoot, 'd');
|
|
165
165
|
const a = makeOrchestrator(rootDir);
|
|
166
166
|
await a.init();
|
|
167
|
-
const node = await a.
|
|
167
|
+
const node = await a.ensureOwnerNode(CFG);
|
|
168
168
|
await waitFor(() => a.isRunning(node.dockerId));
|
|
169
|
-
const before = a.
|
|
169
|
+
const before = a.getOwnerAdminEndpoint();
|
|
170
170
|
|
|
171
171
|
const b = makeOrchestrator(rootDir);
|
|
172
172
|
await b.init();
|
|
173
|
-
expect(b.
|
|
174
|
-
expect(b.
|
|
175
|
-
const after = b.
|
|
173
|
+
expect(b.isOwnerNode(OWNER_CONTAINER_ID)).toBe(true);
|
|
174
|
+
expect(b.hasOwnerConfig()).toBe(true);
|
|
175
|
+
const after = b.getOwnerAdminEndpoint();
|
|
176
176
|
expect(after).toEqual(before);
|
|
177
177
|
expect(await b.isRunning(node.dockerId)).toBe(true);
|
|
178
178
|
|
|
@@ -180,14 +180,14 @@ describe('HostProcessOrchestrator.ensureAuthorityNode', () => {
|
|
|
180
180
|
expect(isPidAlive(pid)).toBe(true);
|
|
181
181
|
});
|
|
182
182
|
|
|
183
|
-
it('
|
|
183
|
+
it('restartOwnerNode preserves the workdir and re-spawns', async () => {
|
|
184
184
|
const orch = makeOrchestrator(join(tmpRoot, 'e'));
|
|
185
185
|
await orch.init();
|
|
186
|
-
const first = await orch.
|
|
186
|
+
const first = await orch.ensureOwnerNode(CFG);
|
|
187
187
|
await waitFor(() => orch.isRunning(first.dockerId));
|
|
188
188
|
const { pid: firstPid } = decodeDockerId(first.dockerId);
|
|
189
189
|
|
|
190
|
-
const restarted = await orch.
|
|
190
|
+
const restarted = await orch.restartOwnerNode();
|
|
191
191
|
await waitFor(() => orch.isRunning(restarted.dockerId));
|
|
192
192
|
|
|
193
193
|
// New child (different pid/token), still exactly one node, workdir reused.
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* HostProcessOrchestrator pinned-owner-key tests — the load-bearing wiring for
|
|
3
|
+
* the node-donation flow.
|
|
4
|
+
*
|
|
5
|
+
* A donated node joins a FOREIGN cadre and must accept a requester-signed seed,
|
|
6
|
+
* so the orchestrator threads `request.pinnedOwnerKeys` into the child as the
|
|
7
|
+
* comma-separated `CADRE_OWNER_KEYS` env var (which `cadre-cli start` turns into
|
|
8
|
+
* a cold-start pinned-key trust policy). The same signal marks the node as
|
|
9
|
+
* foreign-party, which suppresses the host's own push credentials (meaningless
|
|
10
|
+
* to another cadre's app).
|
|
11
|
+
*
|
|
12
|
+
* A fake CLI records `CADRE_OWNER_KEYS` from its environment to a file so the
|
|
13
|
+
* env threading is observable without a real cadre node.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
|
|
17
|
+
import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
|
|
18
|
+
import { tmpdir } from 'node:os';
|
|
19
|
+
import { join } from 'node:path';
|
|
20
|
+
|
|
21
|
+
import type { PushCredentials } from '@serfab/cadre-core';
|
|
22
|
+
import { HostProcessOrchestrator } from '../orchestrator/host-process-orchestrator.js';
|
|
23
|
+
|
|
24
|
+
// Writes CADRE_OWNER_KEYS (as seen in the child env) next to the startup token,
|
|
25
|
+
// 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, 'owner-keys.txt'), process.env.CADRE_OWNER_KEYS ?? '', '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
|
+
const FCM = { projectId: 'proj', clientEmail: 'svc@proj.iam', privateKey: 'FCM-SECRET-PEM' };
|
|
43
|
+
|
|
44
|
+
let tmpRoot: string;
|
|
45
|
+
let scriptPath: string;
|
|
46
|
+
const orchestrators: HostProcessOrchestrator[] = [];
|
|
47
|
+
|
|
48
|
+
beforeEach(() => {
|
|
49
|
+
tmpRoot = mkdtempSync(join(tmpdir(), 'cadre-host-pin-'));
|
|
50
|
+
scriptPath = join(tmpRoot, 'fake-cli.mjs');
|
|
51
|
+
writeFileSync(scriptPath, FAKE_CLI, 'utf8');
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
afterEach(async () => {
|
|
55
|
+
for (const orch of orchestrators) {
|
|
56
|
+
for (const n of orch.listNodes()) {
|
|
57
|
+
try { await orch.removeContainer(n.dockerId); } catch { /* ignore */ }
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
orchestrators.length = 0;
|
|
61
|
+
await sleep(50);
|
|
62
|
+
try { rmSync(tmpRoot, { recursive: true, force: true }); } catch { /* ignore */ }
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
function makeOrchestrator(rootDir: string, pushResolver?: () => Promise<PushCredentials | undefined>): HostProcessOrchestrator {
|
|
66
|
+
mkdirSync(rootDir, { recursive: true });
|
|
67
|
+
const orch = new HostProcessOrchestrator({
|
|
68
|
+
rootDir,
|
|
69
|
+
portRange: { start: 18500, end: 18999 },
|
|
70
|
+
stopTimeoutMs: 1500,
|
|
71
|
+
spawn: { entrypoint: scriptPath },
|
|
72
|
+
...(pushResolver ? { pushResolver } : {}),
|
|
73
|
+
});
|
|
74
|
+
orchestrators.push(orch);
|
|
75
|
+
return orch;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function readChildConfig(rootDir: string, containerId: string): Record<string, unknown> {
|
|
79
|
+
return JSON.parse(readFileSync(join(rootDir, containerId, 'cadre.json'), 'utf8')) as Record<string, unknown>;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
async function waitForFile(path: string, timeoutMs = 5000): Promise<string> {
|
|
83
|
+
const deadline = Date.now() + timeoutMs;
|
|
84
|
+
while (Date.now() < deadline) {
|
|
85
|
+
if (existsSync(path)) return readFileSync(path, 'utf8');
|
|
86
|
+
await sleep(50);
|
|
87
|
+
}
|
|
88
|
+
throw new Error(`file never appeared: ${path}`);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function sleep(ms: number): Promise<void> {
|
|
92
|
+
return new Promise((res) => setTimeout(res, ms));
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
describe('HostProcessOrchestrator pinned owner keys', () => {
|
|
96
|
+
it('threads pinnedOwnerKeys into the child as comma-separated CADRE_OWNER_KEYS', async () => {
|
|
97
|
+
const rootDir = join(tmpRoot, 'a');
|
|
98
|
+
const orch = makeOrchestrator(rootDir);
|
|
99
|
+
await orch.init();
|
|
100
|
+
|
|
101
|
+
await orch.createContainer({
|
|
102
|
+
containerId: 'donated-1',
|
|
103
|
+
partyId: 'foreign-P',
|
|
104
|
+
bootstrapNodes: [],
|
|
105
|
+
profile: 'storage',
|
|
106
|
+
pinnedOwnerKeys: ['key-a', 'key-b'],
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
const keys = await waitForFile(join(rootDir, 'donated-1', 'owner-keys.txt'));
|
|
110
|
+
expect(keys).toBe('key-a,key-b');
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
it('suppresses host push for a foreign-party (pinned-key) node even on storage profile', async () => {
|
|
114
|
+
const rootDir = join(tmpRoot, 'b');
|
|
115
|
+
const orch = makeOrchestrator(rootDir, async () => ({ fcm: FCM }));
|
|
116
|
+
await orch.init();
|
|
117
|
+
|
|
118
|
+
// Foreign-party donated node: pinned keys present → no host push injected.
|
|
119
|
+
await orch.createContainer({
|
|
120
|
+
containerId: 'donated-2',
|
|
121
|
+
partyId: 'foreign-P',
|
|
122
|
+
bootstrapNodes: [],
|
|
123
|
+
profile: 'storage',
|
|
124
|
+
pinnedOwnerKeys: ['key-a'],
|
|
125
|
+
});
|
|
126
|
+
expect(readChildConfig(rootDir, 'donated-2').push).toBeUndefined();
|
|
127
|
+
|
|
128
|
+
// Control: a host-own storage node (no pinned keys) DOES get push.
|
|
129
|
+
await orch.createContainer({
|
|
130
|
+
containerId: 'own-1',
|
|
131
|
+
partyId: 'host-P',
|
|
132
|
+
bootstrapNodes: [],
|
|
133
|
+
profile: 'storage',
|
|
134
|
+
});
|
|
135
|
+
expect(readChildConfig(rootDir, 'own-1').push).toEqual({ fcm: FCM });
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
it('omits CADRE_OWNER_KEYS when no pinned keys are supplied', async () => {
|
|
139
|
+
const rootDir = join(tmpRoot, 'c');
|
|
140
|
+
const orch = makeOrchestrator(rootDir);
|
|
141
|
+
await orch.init();
|
|
142
|
+
|
|
143
|
+
await orch.createContainer({
|
|
144
|
+
containerId: 'own-2',
|
|
145
|
+
partyId: 'host-P',
|
|
146
|
+
bootstrapNodes: [],
|
|
147
|
+
profile: 'storage',
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
const keys = await waitForFile(join(rootDir, 'own-2', 'owner-keys.txt'));
|
|
151
|
+
expect(keys).toBe(''); // env var absent → child reads empty string
|
|
152
|
+
});
|
|
153
|
+
});
|
|
@@ -13,7 +13,7 @@ import { tmpdir } from 'node:os';
|
|
|
13
13
|
import { join } from 'node:path';
|
|
14
14
|
|
|
15
15
|
import type { PushCredentials } from '@serfab/cadre-core';
|
|
16
|
-
import { HostProcessOrchestrator,
|
|
16
|
+
import { HostProcessOrchestrator, OWNER_CONTAINER_ID } from '../orchestrator/host-process-orchestrator.js';
|
|
17
17
|
|
|
18
18
|
const FAKE_CLI = `
|
|
19
19
|
import fs from 'node:fs';
|
|
@@ -42,7 +42,7 @@ beforeEach(() => {
|
|
|
42
42
|
|
|
43
43
|
afterEach(async () => {
|
|
44
44
|
for (const orch of orchestrators) {
|
|
45
|
-
try { await orch.
|
|
45
|
+
try { await orch.stopOwnerNode(); } catch { /* ignore */ }
|
|
46
46
|
}
|
|
47
47
|
orchestrators.length = 0;
|
|
48
48
|
await sleep(50);
|
|
@@ -62,7 +62,7 @@ function makeOrchestrator(rootDir: string, pushResolver?: () => Promise<PushCred
|
|
|
62
62
|
return orch;
|
|
63
63
|
}
|
|
64
64
|
|
|
65
|
-
function readChildConfig(rootDir: string, containerId =
|
|
65
|
+
function readChildConfig(rootDir: string, containerId = OWNER_CONTAINER_ID): Record<string, unknown> {
|
|
66
66
|
const raw = readFileSync(join(rootDir, containerId, 'cadre.json'), 'utf8');
|
|
67
67
|
return JSON.parse(raw) as Record<string, unknown>;
|
|
68
68
|
}
|
|
@@ -76,7 +76,7 @@ describe('HostProcessOrchestrator push injection', () => {
|
|
|
76
76
|
const rootDir = join(tmpRoot, 'a');
|
|
77
77
|
const orch = makeOrchestrator(rootDir, async () => ({ fcm: FCM, apns: APNS, cooldownMs: 1000 }));
|
|
78
78
|
await orch.init();
|
|
79
|
-
await orch.
|
|
79
|
+
await orch.ensureOwnerNode(CFG);
|
|
80
80
|
|
|
81
81
|
const cfg = readChildConfig(rootDir);
|
|
82
82
|
expect(cfg.push).toEqual({ fcm: FCM, apns: APNS, cooldownMs: 1000 });
|
|
@@ -86,7 +86,7 @@ describe('HostProcessOrchestrator push injection', () => {
|
|
|
86
86
|
const rootDir = join(tmpRoot, 'b');
|
|
87
87
|
const orch = makeOrchestrator(rootDir);
|
|
88
88
|
await orch.init();
|
|
89
|
-
await orch.
|
|
89
|
+
await orch.ensureOwnerNode(CFG);
|
|
90
90
|
|
|
91
91
|
const cfg = readChildConfig(rootDir);
|
|
92
92
|
expect(cfg.push).toBeUndefined();
|
|
@@ -96,7 +96,7 @@ describe('HostProcessOrchestrator push injection', () => {
|
|
|
96
96
|
const rootDir = join(tmpRoot, 'c');
|
|
97
97
|
const orch = makeOrchestrator(rootDir, async () => undefined);
|
|
98
98
|
await orch.init();
|
|
99
|
-
await orch.
|
|
99
|
+
await orch.ensureOwnerNode(CFG);
|
|
100
100
|
|
|
101
101
|
expect(readChildConfig(rootDir).push).toBeUndefined();
|
|
102
102
|
});
|
|
@@ -106,8 +106,8 @@ describe('HostProcessOrchestrator push injection', () => {
|
|
|
106
106
|
const orch = makeOrchestrator(rootDir, async () => { throw new Error('partial creds'); });
|
|
107
107
|
await orch.init();
|
|
108
108
|
// Spawn still succeeds.
|
|
109
|
-
const node = await orch.
|
|
110
|
-
expect(node.id).toBe(
|
|
109
|
+
const node = await orch.ensureOwnerNode(CFG);
|
|
110
|
+
expect(node.id).toBe(OWNER_CONTAINER_ID);
|
|
111
111
|
expect(readChildConfig(rootDir).push).toBeUndefined();
|
|
112
112
|
});
|
|
113
113
|
|
|
@@ -116,7 +116,7 @@ describe('HostProcessOrchestrator push injection', () => {
|
|
|
116
116
|
let current: PushCredentials | undefined = { fcm: FCM };
|
|
117
117
|
const orch = makeOrchestrator(rootDir, async () => current);
|
|
118
118
|
await orch.init();
|
|
119
|
-
await orch.
|
|
119
|
+
await orch.ensureOwnerNode(CFG);
|
|
120
120
|
expect(readChildConfig(rootDir).push).toEqual({ fcm: FCM });
|
|
121
121
|
|
|
122
122
|
// state.json must not carry the raw key — only re-resolvable references.
|
|
@@ -125,7 +125,7 @@ describe('HostProcessOrchestrator push injection', () => {
|
|
|
125
125
|
|
|
126
126
|
// Rotate the resolver's answer, then restart — the new spawn must pick it up.
|
|
127
127
|
current = { apns: APNS };
|
|
128
|
-
await orch.
|
|
128
|
+
await orch.restartOwnerNode();
|
|
129
129
|
const cfg2 = readChildConfig(rootDir);
|
|
130
130
|
expect(cfg2.push).toEqual({ apns: APNS });
|
|
131
131
|
expect((cfg2.push as PushCredentials).fcm).toBeUndefined();
|
|
@@ -4,7 +4,7 @@ import { tmpdir } from 'node:os';
|
|
|
4
4
|
import { join } from 'node:path';
|
|
5
5
|
import { generateKeyPair } from '@libp2p/crypto/keys';
|
|
6
6
|
import { peerIdFromPrivateKey } from '@libp2p/peer-id';
|
|
7
|
-
import { CadreNode,
|
|
7
|
+
import { CadreNode, ed25519KeyPairFromLibp2p } from '@serfab/cadre-core';
|
|
8
8
|
|
|
9
9
|
import { TrustCircleService } from '../trust-circle.js';
|
|
10
10
|
import { TrustCircleStore } from '../trust-circle-store.js';
|
|
@@ -29,19 +29,19 @@ describe('TrustCircleService — real CadreNode integration', () => {
|
|
|
29
29
|
let host: CadreNode;
|
|
30
30
|
let store: TrustCircleStore;
|
|
31
31
|
let service: TrustCircleService;
|
|
32
|
-
let
|
|
33
|
-
let
|
|
32
|
+
let hostOwnerPrivateKey: string;
|
|
33
|
+
let hostOwnerPublicKey: string;
|
|
34
34
|
|
|
35
35
|
beforeEach(async () => {
|
|
36
36
|
tmpRoot = mkdtempSync(join(tmpdir(), 'cadre-host-tc-int-'));
|
|
37
37
|
|
|
38
|
-
// Own-
|
|
39
|
-
// (the cadre-cli `--
|
|
38
|
+
// Own-owner host: the node's libp2p identity key IS its owner key
|
|
39
|
+
// (the cadre-cli `--owner` shape), so registerSelf can owner-sign the
|
|
40
40
|
// INSERT of its own CadrePeer row.
|
|
41
41
|
const hostKey = await generateKeyPair('Ed25519');
|
|
42
|
-
const
|
|
43
|
-
|
|
44
|
-
|
|
42
|
+
const owner = ed25519KeyPairFromLibp2p(hostKey);
|
|
43
|
+
hostOwnerPrivateKey = owner.privateKeyB64;
|
|
44
|
+
hostOwnerPublicKey = owner.publicKeyB64;
|
|
45
45
|
|
|
46
46
|
const baseId = Math.random().toString(36).slice(2);
|
|
47
47
|
|
|
@@ -55,11 +55,11 @@ describe('TrustCircleService — real CadreNode integration', () => {
|
|
|
55
55
|
|
|
56
56
|
const db = host.getControlDatabase();
|
|
57
57
|
expect(db).not.toBeNull();
|
|
58
|
-
await db!.
|
|
58
|
+
await db!.insertOwnerKey(hostOwnerPublicKey);
|
|
59
59
|
|
|
60
|
-
host.initializeSeedBootstrap(
|
|
60
|
+
host.initializeSeedBootstrap(hostOwnerPrivateKey);
|
|
61
61
|
|
|
62
|
-
// The
|
|
62
|
+
// The owner writes its own CadrePeer row up-front (the implement ticket's
|
|
63
63
|
// CLI change). After this the host's own peerId is a member, so it shows up
|
|
64
64
|
// alongside any redeemed peers in the trust-circle listing.
|
|
65
65
|
await host.registerSelf();
|
|
@@ -9,10 +9,10 @@ import { TrustCircleService, createTrustCircleHandlers } from '../trust-circle.j
|
|
|
9
9
|
import { TrustCircleStore } from '../trust-circle-store.js';
|
|
10
10
|
import { TrustCircleError } from '../types.js';
|
|
11
11
|
import type { CadreNodeLike } from '../trust-circle.js';
|
|
12
|
-
import {
|
|
12
|
+
import { OwnerNodeUnavailableError } from '../../owner/owner-node-client.js';
|
|
13
13
|
|
|
14
14
|
/**
|
|
15
|
-
* Minimal in-memory mock of the
|
|
15
|
+
* Minimal in-memory mock of the owner-node channel adapter. Records calls
|
|
16
16
|
* so assertions can verify the auth + redemption flow without standing up a
|
|
17
17
|
* real libp2p control network or admin channel.
|
|
18
18
|
*/
|
|
@@ -24,14 +24,14 @@ class MockCadreNode implements CadreNodeLike {
|
|
|
24
24
|
|
|
25
25
|
/** Toggle to make acceptPhone reject. */
|
|
26
26
|
acceptShouldThrow: Error | null = null;
|
|
27
|
-
/** When false, membership calls throw
|
|
27
|
+
/** When false, membership calls throw OwnerNodeUnavailableError. */
|
|
28
28
|
nodeAvailable = true;
|
|
29
29
|
|
|
30
30
|
async createInvite(token?: string, expiresIn?: number) {
|
|
31
31
|
this.issuedInvites.push({ token, expiresIn });
|
|
32
32
|
const invite: CadreInvite = {
|
|
33
33
|
partyId: 'party-test',
|
|
34
|
-
|
|
34
|
+
ownerAddrs: ['/ip4/127.0.0.1/tcp/4001'],
|
|
35
35
|
token,
|
|
36
36
|
createdAt: Date.now(),
|
|
37
37
|
...(expiresIn ? { expiresAt: Date.now() + expiresIn } : {}),
|
|
@@ -55,12 +55,12 @@ class MockCadreNode implements CadreNodeLike {
|
|
|
55
55
|
}
|
|
56
56
|
|
|
57
57
|
async listMembers(): Promise<Array<{ peerId: string; multiaddr: string | null }>> {
|
|
58
|
-
if (!this.nodeAvailable) throw new
|
|
58
|
+
if (!this.nodeAvailable) throw new OwnerNodeUnavailableError('node down');
|
|
59
59
|
return [...this.authorizedPeers].map((peerId) => ({ peerId, multiaddr: null }));
|
|
60
60
|
}
|
|
61
61
|
|
|
62
62
|
async isMember(peerId: string): Promise<boolean> {
|
|
63
|
-
if (!this.nodeAvailable) throw new
|
|
63
|
+
if (!this.nodeAvailable) throw new OwnerNodeUnavailableError('node down');
|
|
64
64
|
return this.authorizedPeers.has(peerId);
|
|
65
65
|
}
|
|
66
66
|
}
|
|
@@ -201,7 +201,7 @@ describe('TrustCircleService.redeemInvite', () => {
|
|
|
201
201
|
|
|
202
202
|
it('preserves the pending token when acceptPhone fails with node_unavailable', async () => {
|
|
203
203
|
const { token } = await service.issueInvite({ label: 'Transient' });
|
|
204
|
-
cadreNode.acceptShouldThrow = new
|
|
204
|
+
cadreNode.acceptShouldThrow = new OwnerNodeUnavailableError('node down');
|
|
205
205
|
|
|
206
206
|
await expect(service.redeemInvite({ token, peerId: 'pTrans' }))
|
|
207
207
|
.rejects.toMatchObject({ code: 'node_unavailable' });
|
|
@@ -225,7 +225,7 @@ describe('TrustCircleService.redeemInvite', () => {
|
|
|
225
225
|
// first redeem hits node_unavailable. The second redeem should still
|
|
226
226
|
// bounce synchronously while the gate is held.
|
|
227
227
|
let release!: () => void;
|
|
228
|
-
const gate = new Promise<void>((_, reject) => { release = () => reject(new
|
|
228
|
+
const gate = new Promise<void>((_, reject) => { release = () => reject(new OwnerNodeUnavailableError('node down')); });
|
|
229
229
|
cadreNode.acceptPhone = async () => {
|
|
230
230
|
await gate;
|
|
231
231
|
};
|
|
@@ -349,7 +349,7 @@ describe('TrustCircleService.list', () => {
|
|
|
349
349
|
expect(store.getMember('orphan')).toBeUndefined();
|
|
350
350
|
});
|
|
351
351
|
|
|
352
|
-
it('falls back to local labels when the
|
|
352
|
+
it('falls back to local labels when the owner node is unavailable', async () => {
|
|
353
353
|
store.addMember({ peerId: 'pZ', label: 'Zoe', addedAt: 't' });
|
|
354
354
|
cadreNode.nodeAvailable = false;
|
|
355
355
|
|
|
@@ -361,7 +361,7 @@ describe('TrustCircleService.list', () => {
|
|
|
361
361
|
|
|
362
362
|
it('issueInvite surfaces node_unavailable when the channel is down', async () => {
|
|
363
363
|
const downNode = new MockCadreNode();
|
|
364
|
-
downNode.createInvite = async () => { throw new
|
|
364
|
+
downNode.createInvite = async () => { throw new OwnerNodeUnavailableError('refused'); };
|
|
365
365
|
const svc = new TrustCircleService({ cadreNode: downNode, store });
|
|
366
366
|
await expect(svc.issueInvite({ label: 'Nina' }))
|
|
367
367
|
.rejects.toMatchObject({ code: 'node_unavailable' });
|
package/src/auth/trust-circle.ts
CHANGED
|
@@ -3,7 +3,7 @@ import debug from 'debug';
|
|
|
3
3
|
|
|
4
4
|
import type { CadreInvite } from '@serfab/cadre-core';
|
|
5
5
|
|
|
6
|
-
import {
|
|
6
|
+
import { OwnerNodeUnavailableError } from '../owner/owner-node-client.js';
|
|
7
7
|
|
|
8
8
|
import type { TrustCircleStore } from './trust-circle-store.js';
|
|
9
9
|
import type {
|
|
@@ -53,9 +53,9 @@ export interface CadreNodeLike {
|
|
|
53
53
|
* membership management.
|
|
54
54
|
*
|
|
55
55
|
* Membership is canonical in the cadre control DB (`CadrePeer` table),
|
|
56
|
-
* reached over the
|
|
56
|
+
* reached over the owner node's admin channel; labels and pending tokens
|
|
57
57
|
* live in the host-local `TrustCircleStore`. When the node is unreachable,
|
|
58
|
-
*
|
|
58
|
+
* owner operations surface `node_unavailable` (→ 503) and `list()`
|
|
59
59
|
* degrades to the local labels file.
|
|
60
60
|
*/
|
|
61
61
|
export class TrustCircleService {
|
|
@@ -78,7 +78,7 @@ export class TrustCircleService {
|
|
|
78
78
|
|
|
79
79
|
/**
|
|
80
80
|
* Issue a new invite. Generates a token, persists a pending row, and
|
|
81
|
-
* delegates to cadre-core to embed the host's
|
|
81
|
+
* delegates to cadre-core to embed the host's owner addresses.
|
|
82
82
|
*/
|
|
83
83
|
async issueInvite(opts: { label: string; ttlMs?: number }): Promise<{
|
|
84
84
|
encodedInvite: string;
|
|
@@ -127,7 +127,7 @@ export class TrustCircleService {
|
|
|
127
127
|
*
|
|
128
128
|
* Crash-safety tradeoff: if the host crashes between `acceptPhone` success
|
|
129
129
|
* and the durable `removePending`, a retry will dial in fine but the
|
|
130
|
-
*
|
|
130
|
+
* owner node's `acceptPhone` will reject the second `CadrePeer` insert
|
|
131
131
|
* with a PK constraint error (cadre-core does not upsert). That surfaces to
|
|
132
132
|
* the redeemer as a non-`node_unavailable` failure; the admin can revoke
|
|
133
133
|
* the lingering pending row. This is a narrower window than the prior
|
|
@@ -167,7 +167,7 @@ export class TrustCircleService {
|
|
|
167
167
|
// matches by construction) and re-checks expiration.
|
|
168
168
|
const reconstructed: CadreInvite = {
|
|
169
169
|
partyId: '',
|
|
170
|
-
|
|
170
|
+
ownerAddrs: [],
|
|
171
171
|
token: pending.token,
|
|
172
172
|
createdAt: new Date(pending.createdAt).getTime(),
|
|
173
173
|
...(pending.expiresAt ? { expiresAt: new Date(pending.expiresAt).getTime() } : {}),
|
|
@@ -242,7 +242,7 @@ export class TrustCircleService {
|
|
|
242
242
|
/**
|
|
243
243
|
* UI snapshot. Joins the canonical CadrePeer list (fetched over the admin
|
|
244
244
|
* channel) with local labels; prunes any orphan labels (peer is no longer
|
|
245
|
-
* authorised). When the
|
|
245
|
+
* authorised). When the owner node is unreachable, degrades to the
|
|
246
246
|
* local labels file so listing keeps working while the node is down.
|
|
247
247
|
*/
|
|
248
248
|
async list(): Promise<TrustCircleSnapshot> {
|
|
@@ -260,7 +260,7 @@ export class TrustCircleService {
|
|
|
260
260
|
try {
|
|
261
261
|
controlMembers = await this.cadreNode.listMembers();
|
|
262
262
|
} catch (err) {
|
|
263
|
-
if (!(err instanceof
|
|
263
|
+
if (!(err instanceof OwnerNodeUnavailableError)) throw err;
|
|
264
264
|
controlMembers = null;
|
|
265
265
|
}
|
|
266
266
|
|
|
@@ -306,8 +306,8 @@ export class TrustCircleService {
|
|
|
306
306
|
* `never` so callers can `.catch(err => this.toDomainError(err))`.
|
|
307
307
|
*/
|
|
308
308
|
private toDomainError(err: unknown): never {
|
|
309
|
-
if (err instanceof
|
|
310
|
-
throw new TrustCircleError('node_unavailable', `
|
|
309
|
+
if (err instanceof OwnerNodeUnavailableError) {
|
|
310
|
+
throw new TrustCircleError('node_unavailable', `Owner node unavailable: ${err.message}`);
|
|
311
311
|
}
|
|
312
312
|
throw err instanceof Error ? err : new Error(String(err));
|
|
313
313
|
}
|
package/src/auth/types.ts
CHANGED
|
@@ -62,7 +62,7 @@ export interface TrustCircleSnapshot {
|
|
|
62
62
|
* - `not_found` → 404 (unknown token / unknown peer)
|
|
63
63
|
* - `expired` → 410 (invite past expiresAt)
|
|
64
64
|
* - `already_redeemed` → 409 (token was used and is gone)
|
|
65
|
-
* - `node_unavailable` → 503 (
|
|
65
|
+
* - `node_unavailable` → 503 (owner node admin channel down/not ready)
|
|
66
66
|
* - default → 500
|
|
67
67
|
*/
|
|
68
68
|
export interface TrustCircleHandlers {
|
|
@@ -84,7 +84,7 @@ export type TrustCircleErrorCode =
|
|
|
84
84
|
| 'invalid_label'
|
|
85
85
|
| 'invalid_token'
|
|
86
86
|
| 'storage_error'
|
|
87
|
-
/** The
|
|
87
|
+
/** The owner node's admin channel is unreachable / not ready. */
|
|
88
88
|
| 'node_unavailable';
|
|
89
89
|
|
|
90
90
|
/** Typed error carrying a stable `code` for HTTP mapping. */
|