@serfab/cadre-host 0.7.1 → 0.8.1
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/README.md +296 -101
- package/dist/auth/trust-circle.d.ts +44 -6
- package/dist/auth/trust-circle.js +115 -58
- package/dist/auth/trust-circle.js.map +1 -1
- package/dist/auth/types.d.ts +4 -1
- package/dist/auth/types.js.map +1 -1
- package/dist/authority/authority-node-client.d.ts +77 -0
- package/dist/authority/authority-node-client.js +143 -0
- package/dist/authority/authority-node-client.js.map +1 -0
- package/dist/authority/index.d.ts +11 -0
- package/dist/authority/index.js +11 -0
- package/dist/authority/index.js.map +1 -0
- package/dist/bin/host.js +210 -97
- package/dist/bin/host.js.map +1 -1
- package/dist/index.d.ts +10 -6
- package/dist/index.js +6 -3
- package/dist/index.js.map +1 -1
- package/dist/installer/browser.js +16 -17
- package/dist/installer/browser.js.map +1 -1
- package/dist/installer/config.d.ts +25 -0
- package/dist/installer/config.js +1 -1
- package/dist/installer/config.js.map +1 -1
- package/dist/installer/index.d.ts +5 -2
- package/dist/installer/index.js +14 -10
- package/dist/installer/index.js.map +1 -1
- package/dist/installer/service-host/launchd.js +1 -1
- package/dist/installer/service-host/launchd.js.map +1 -1
- package/dist/installer/service-host/systemd.js +1 -1
- package/dist/installer/service-host/systemd.js.map +1 -1
- package/dist/nat/index.d.ts +1 -1
- package/dist/nat/nat-service.d.ts +45 -3
- package/dist/nat/nat-service.js +116 -3
- package/dist/nat/nat-service.js.map +1 -1
- package/dist/nat/secrets/index.d.ts +7 -0
- package/dist/nat/secrets/index.js +28 -2
- package/dist/nat/secrets/index.js.map +1 -1
- package/dist/nat/types.d.ts +3 -1
- package/dist/nat/types.js.map +1 -1
- package/dist/orchestrator/host-process-orchestrator.d.ts +60 -1
- package/dist/orchestrator/host-process-orchestrator.js +246 -39
- package/dist/orchestrator/host-process-orchestrator.js.map +1 -1
- package/dist/orchestrator/index.d.ts +3 -2
- package/dist/orchestrator/index.js +1 -1
- package/dist/orchestrator/index.js.map +1 -1
- package/dist/orchestrator/state-store.d.ts +6 -5
- package/dist/orchestrator/state-store.js +10 -2
- package/dist/orchestrator/state-store.js.map +1 -1
- package/dist/orchestrator/types.d.ts +45 -10
- package/dist/orchestrator/types.js.map +1 -1
- package/dist/push/index.d.ts +67 -0
- package/dist/push/index.js +101 -0
- package/dist/push/index.js.map +1 -0
- package/dist/server/error-handler.d.ts +2 -0
- package/dist/server/error-handler.js +4 -0
- package/dist/server/error-handler.js.map +1 -1
- package/dist/server/routes/nodes.d.ts +4 -3
- package/dist/server/routes/nodes.js +38 -24
- package/dist/server/routes/nodes.js.map +1 -1
- package/dist/server/static.d.ts +12 -0
- package/dist/server/static.js +24 -7
- package/dist/server/static.js.map +1 -1
- package/dist/ui/assets/index-BlMXQPSw.js +14 -0
- package/dist/ui/assets/{index-C9uChk2P.js.map → index-BlMXQPSw.js.map} +1 -1
- package/dist/ui/index.html +1 -1
- package/dist/update/apply.d.ts +23 -1
- package/dist/update/apply.js +46 -4
- package/dist/update/apply.js.map +1 -1
- package/dist/update/index.d.ts +14 -3
- package/dist/update/index.js +35 -2
- package/dist/update/index.js.map +1 -1
- package/dist/update/manifest.d.ts +28 -9
- package/dist/update/manifest.js +56 -29
- package/dist/update/manifest.js.map +1 -1
- package/dist/update/release-key.d.ts +18 -7
- package/dist/update/release-key.js +28 -15
- package/dist/update/release-key.js.map +1 -1
- package/dist/update/sign.d.ts +46 -0
- package/dist/update/sign.js +53 -0
- package/dist/update/sign.js.map +1 -0
- package/package.json +9 -4
- package/service/README.md +95 -86
- package/src/__tests__/cli-invite.smoke.test.ts +6 -1
- package/src/__tests__/cli-nat.smoke.test.ts +6 -1
- package/src/__tests__/cli.smoke.test.ts +6 -1
- package/src/__tests__/host-authority.smoke.test.ts +123 -0
- package/src/__tests__/orchestrator-authority.test.ts +198 -0
- package/src/__tests__/orchestrator-push.test.ts +145 -0
- package/src/auth/__tests__/trust-circle-integration.test.ts +40 -21
- package/src/auth/__tests__/trust-circle.test.ts +90 -23
- package/src/auth/trust-circle.ts +363 -304
- package/src/auth/types.ts +4 -1
- package/src/authority/__tests__/authority-node-client.test.ts +166 -0
- package/src/authority/authority-node-client.ts +199 -0
- package/src/authority/index.ts +12 -0
- package/src/bin/host.ts +1038 -905
- package/src/index.ts +34 -3
- package/src/installer/__tests__/installer.smoke.test.ts +64 -0
- package/src/installer/browser.ts +40 -42
- package/src/installer/config.ts +27 -1
- package/src/installer/index.ts +18 -12
- package/src/installer/service-host/launchd.ts +1 -1
- package/src/installer/service-host/systemd.ts +1 -1
- package/src/nat/__tests__/ddns-updater.test.ts +1 -1
- package/src/nat/__tests__/nat-service.test.ts +192 -0
- package/src/nat/__tests__/port-mapper.test.ts +1 -1
- package/src/nat/__tests__/secrets.test.ts +23 -1
- package/src/nat/index.ts +1 -1
- package/src/nat/nat-service.ts +131 -5
- package/src/nat/secrets/index.ts +32 -2
- package/src/nat/types.ts +3 -1
- package/src/orchestrator/host-process-orchestrator.ts +294 -54
- package/src/orchestrator/index.ts +9 -2
- package/src/orchestrator/state-store.ts +21 -3
- package/src/orchestrator/types.ts +48 -2
- package/src/push/__tests__/push.test.ts +90 -0
- package/src/push/index.ts +146 -0
- package/src/server/__tests__/error-handler.test.ts +4 -4
- package/src/server/__tests__/nodes-route.test.ts +90 -10
- package/src/server/__tests__/publishers.test.ts +1 -1
- package/src/server/__tests__/static.test.ts +145 -0
- package/src/server/__tests__/status-route.test.ts +2 -2
- package/src/server/error-handler.ts +4 -0
- package/src/server/routes/nodes.ts +48 -26
- package/src/server/static.ts +24 -8
- package/src/update/__tests__/apply.test.ts +58 -1
- package/src/update/__tests__/manifest.test.ts +41 -6
- package/src/update/__tests__/release-key.test.ts +29 -0
- package/src/update/__tests__/sign.test.ts +78 -0
- package/src/update/__tests__/update-service.integration.test.ts +44 -9
- package/src/update/apply.ts +46 -4
- package/src/update/index.ts +41 -3
- package/src/update/manifest.ts +214 -172
- package/src/update/release-key.ts +30 -16
- package/src/update/sign.ts +74 -0
- package/dist/ui/assets/index-C9uChk2P.js +0 -14
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* HostProcessOrchestrator authority-node lifecycle tests.
|
|
3
|
+
*
|
|
4
|
+
* Uses the `spawn.entrypoint` hook with a fake CLI that writes its startup
|
|
5
|
+
* token and binds a minimal `/admin/identity` endpoint on the admin port —
|
|
6
|
+
* enough to validate spawn, idempotency, admin-port allocation/persistence,
|
|
7
|
+
* the end-to-end AuthorityNodeClient round-trip, and init() re-attach.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
|
|
11
|
+
import { mkdtempSync, mkdirSync, rmSync, writeFileSync } from 'node:fs';
|
|
12
|
+
import { tmpdir } from 'node:os';
|
|
13
|
+
import { join } from 'node:path';
|
|
14
|
+
|
|
15
|
+
import { HostProcessOrchestrator, AUTHORITY_CONTAINER_ID } from '../orchestrator/host-process-orchestrator.js';
|
|
16
|
+
import { StateStore } from '../orchestrator/state-store.js';
|
|
17
|
+
import { isPidAlive } from '../orchestrator/pid-liveness.js';
|
|
18
|
+
import { decodeDockerId } from '../orchestrator/types.js';
|
|
19
|
+
import { AuthorityNodeClient } from '../authority/authority-node-client.js';
|
|
20
|
+
|
|
21
|
+
const FAKE_AUTHORITY_CLI = `
|
|
22
|
+
import fs from 'node:fs';
|
|
23
|
+
import http from 'node:http';
|
|
24
|
+
|
|
25
|
+
const args = process.argv.slice(2);
|
|
26
|
+
const get = (flag) => { const i = args.indexOf(flag); return i >= 0 ? args[i + 1] : undefined; };
|
|
27
|
+
|
|
28
|
+
const tokenPath = get('--startup-token-file');
|
|
29
|
+
const adminPort = get('--admin-port');
|
|
30
|
+
const token = process.env.CADRE_STARTUP_TOKEN ?? '';
|
|
31
|
+
|
|
32
|
+
if (tokenPath && token) {
|
|
33
|
+
try { fs.writeFileSync(tokenPath, token, 'utf8'); } catch (err) { console.error('token write failed', err); }
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
if (adminPort && token) {
|
|
37
|
+
const server = http.createServer((req, res) => {
|
|
38
|
+
const auth = req.headers['authorization'];
|
|
39
|
+
if (auth !== 'Bearer ' + token) {
|
|
40
|
+
res.writeHead(401, { 'content-type': 'application/json' });
|
|
41
|
+
res.end(JSON.stringify({ ok: false, error: { code: 'not_authorized', message: 'bad token' } }));
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
if (req.url === '/admin/identity' && req.method === 'GET') {
|
|
45
|
+
res.writeHead(200, { 'content-type': 'application/json' });
|
|
46
|
+
res.end(JSON.stringify({ ok: true, data: { peerId: 'FAKEPEER', partyId: 'fake-party' } }));
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
res.writeHead(400, { 'content-type': 'application/json' });
|
|
50
|
+
res.end(JSON.stringify({ ok: false, error: { code: 'bad_request', message: req.url } }));
|
|
51
|
+
});
|
|
52
|
+
server.listen(parseInt(adminPort, 10), '127.0.0.1', () => {
|
|
53
|
+
console.log('admin listening on ' + adminPort);
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
process.on('SIGTERM', () => process.exit(0));
|
|
58
|
+
console.log('fake authority up');
|
|
59
|
+
setInterval(() => {}, 1 << 30);
|
|
60
|
+
`;
|
|
61
|
+
|
|
62
|
+
let tmpRoot: string;
|
|
63
|
+
let scriptPath: string;
|
|
64
|
+
const orchestrators: HostProcessOrchestrator[] = [];
|
|
65
|
+
|
|
66
|
+
beforeEach(() => {
|
|
67
|
+
tmpRoot = mkdtempSync(join(tmpdir(), 'cadre-host-auth-'));
|
|
68
|
+
scriptPath = join(tmpRoot, 'fake-authority.mjs');
|
|
69
|
+
writeFileSync(scriptPath, FAKE_AUTHORITY_CLI, 'utf8');
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
afterEach(async () => {
|
|
73
|
+
for (const orch of orchestrators) {
|
|
74
|
+
try { await orch.stopAuthorityNode(); } catch { /* ignore */ }
|
|
75
|
+
for (const dockerId of listDockerIds(orch)) {
|
|
76
|
+
try { await orch.removeContainer(dockerId); } catch { /* ignore */ }
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
orchestrators.length = 0;
|
|
80
|
+
await sleep(50);
|
|
81
|
+
try { rmSync(tmpRoot, { recursive: true, force: true }); } catch { /* ignore */ }
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
function makeOrchestrator(rootDir: string): HostProcessOrchestrator {
|
|
85
|
+
mkdirSync(rootDir, { recursive: true });
|
|
86
|
+
const orch = new HostProcessOrchestrator({
|
|
87
|
+
rootDir,
|
|
88
|
+
portRange: { start: 18500, end: 18999 },
|
|
89
|
+
stopTimeoutMs: 1500,
|
|
90
|
+
spawn: { entrypoint: scriptPath },
|
|
91
|
+
});
|
|
92
|
+
orchestrators.push(orch);
|
|
93
|
+
return orch;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function listDockerIds(orch: HostProcessOrchestrator): string[] {
|
|
97
|
+
const state = new StateStore((orch as unknown as { rootDir: string }).rootDir);
|
|
98
|
+
return state.load().handles.map((h) => h.dockerId);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
async function waitFor(predicate: () => boolean | Promise<boolean>, timeoutMs = 4000, intervalMs = 50): Promise<void> {
|
|
102
|
+
const deadline = Date.now() + timeoutMs;
|
|
103
|
+
while (Date.now() < deadline) {
|
|
104
|
+
if (await predicate()) return;
|
|
105
|
+
await sleep(intervalMs);
|
|
106
|
+
}
|
|
107
|
+
throw new Error('waitFor: timed out');
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function sleep(ms: number): Promise<void> {
|
|
111
|
+
return new Promise((res) => setTimeout(res, ms));
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
const CFG = { identityPath: 'C:/fake/identity.key', partyId: 'install-id-123', libp2pPort: 4555 };
|
|
115
|
+
|
|
116
|
+
describe('HostProcessOrchestrator.ensureAuthorityNode', () => {
|
|
117
|
+
it('spawns the authority node, allocates+persists an admin port', async () => {
|
|
118
|
+
const orch = makeOrchestrator(join(tmpRoot, 'a'));
|
|
119
|
+
await orch.init();
|
|
120
|
+
const node = await orch.ensureAuthorityNode(CFG);
|
|
121
|
+
|
|
122
|
+
expect(node.id).toBe(AUTHORITY_CONTAINER_ID);
|
|
123
|
+
expect(node.authority).toBe(true);
|
|
124
|
+
expect(node.ports.admin).toBeGreaterThanOrEqual(18500);
|
|
125
|
+
expect(node.ports.p2p).toBe(4555);
|
|
126
|
+
|
|
127
|
+
const endpoint = orch.getAuthorityAdminEndpoint();
|
|
128
|
+
expect(endpoint).toBeDefined();
|
|
129
|
+
expect(endpoint!.baseUrl).toBe(`http://127.0.0.1:${node.ports.admin}`);
|
|
130
|
+
|
|
131
|
+
// Persisted with admin port + authority flag + authorityConfig.
|
|
132
|
+
const persisted = new StateStore(join(tmpRoot, 'a')).load();
|
|
133
|
+
expect(persisted.authorityConfig).toMatchObject({ partyId: 'install-id-123', libp2pPort: 4555 });
|
|
134
|
+
const ph = persisted.handles.find((h) => h.containerId === AUTHORITY_CONTAINER_ID);
|
|
135
|
+
expect(ph?.authority).toBe(true);
|
|
136
|
+
expect(typeof ph?.ports.admin).toBe('number');
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
it('is idempotent — a second call does not spawn a second child', async () => {
|
|
140
|
+
const orch = makeOrchestrator(join(tmpRoot, 'b'));
|
|
141
|
+
await orch.init();
|
|
142
|
+
const first = await orch.ensureAuthorityNode(CFG);
|
|
143
|
+
await waitFor(() => orch.isRunning(first.dockerId));
|
|
144
|
+
|
|
145
|
+
const second = await orch.ensureAuthorityNode(CFG);
|
|
146
|
+
expect(second.dockerId).toBe(first.dockerId);
|
|
147
|
+
expect(orch.listNodes()).toHaveLength(1);
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
it('the AuthorityNodeClient reaches the spawned admin channel', async () => {
|
|
151
|
+
const orch = makeOrchestrator(join(tmpRoot, 'c'));
|
|
152
|
+
await orch.init();
|
|
153
|
+
const node = await orch.ensureAuthorityNode(CFG);
|
|
154
|
+
await waitFor(() => orch.isRunning(node.dockerId));
|
|
155
|
+
|
|
156
|
+
const client = new AuthorityNodeClient(() => orch.getAuthorityAdminEndpoint());
|
|
157
|
+
await waitFor(async () => {
|
|
158
|
+
try { return (await client.getPeerId()) === 'FAKEPEER'; } catch { return false; }
|
|
159
|
+
});
|
|
160
|
+
expect(await client.getPeerId()).toBe('FAKEPEER');
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
it('getAuthorityAdminEndpoint() survives an init() re-attach', async () => {
|
|
164
|
+
const rootDir = join(tmpRoot, 'd');
|
|
165
|
+
const a = makeOrchestrator(rootDir);
|
|
166
|
+
await a.init();
|
|
167
|
+
const node = await a.ensureAuthorityNode(CFG);
|
|
168
|
+
await waitFor(() => a.isRunning(node.dockerId));
|
|
169
|
+
const before = a.getAuthorityAdminEndpoint();
|
|
170
|
+
|
|
171
|
+
const b = makeOrchestrator(rootDir);
|
|
172
|
+
await b.init();
|
|
173
|
+
expect(b.isAuthorityNode(AUTHORITY_CONTAINER_ID)).toBe(true);
|
|
174
|
+
expect(b.hasAuthorityConfig()).toBe(true);
|
|
175
|
+
const after = b.getAuthorityAdminEndpoint();
|
|
176
|
+
expect(after).toEqual(before);
|
|
177
|
+
expect(await b.isRunning(node.dockerId)).toBe(true);
|
|
178
|
+
|
|
179
|
+
const { pid } = decodeDockerId(node.dockerId);
|
|
180
|
+
expect(isPidAlive(pid)).toBe(true);
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
it('restartAuthorityNode preserves the workdir and re-spawns', async () => {
|
|
184
|
+
const orch = makeOrchestrator(join(tmpRoot, 'e'));
|
|
185
|
+
await orch.init();
|
|
186
|
+
const first = await orch.ensureAuthorityNode(CFG);
|
|
187
|
+
await waitFor(() => orch.isRunning(first.dockerId));
|
|
188
|
+
const { pid: firstPid } = decodeDockerId(first.dockerId);
|
|
189
|
+
|
|
190
|
+
const restarted = await orch.restartAuthorityNode();
|
|
191
|
+
await waitFor(() => orch.isRunning(restarted.dockerId));
|
|
192
|
+
|
|
193
|
+
// New child (different pid/token), still exactly one node, workdir reused.
|
|
194
|
+
expect(restarted.dockerId).not.toBe(first.dockerId);
|
|
195
|
+
expect(orch.listNodes()).toHaveLength(1);
|
|
196
|
+
await waitFor(() => !isPidAlive(firstPid));
|
|
197
|
+
});
|
|
198
|
+
});
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* HostProcessOrchestrator push-credential injection tests.
|
|
3
|
+
*
|
|
4
|
+
* Verifies the orchestrator writes `config.push` into the spawned node's
|
|
5
|
+
* cadre.json when the pushResolver yields credentials, omits it otherwise,
|
|
6
|
+
* re-resolves on every (re-)spawn, and never persists a raw private key in
|
|
7
|
+
* state.json. Uses a fake CLI entrypoint that just writes its startup token.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
|
|
11
|
+
import { mkdtempSync, mkdirSync, rmSync, readFileSync, writeFileSync } from 'node:fs';
|
|
12
|
+
import { tmpdir } from 'node:os';
|
|
13
|
+
import { join } from 'node:path';
|
|
14
|
+
|
|
15
|
+
import type { PushCredentials } from '@serfab/cadre-core';
|
|
16
|
+
import { HostProcessOrchestrator, AUTHORITY_CONTAINER_ID } from '../orchestrator/host-process-orchestrator.js';
|
|
17
|
+
|
|
18
|
+
const FAKE_CLI = `
|
|
19
|
+
import fs from 'node:fs';
|
|
20
|
+
const args = process.argv.slice(2);
|
|
21
|
+
const get = (flag) => { const i = args.indexOf(flag); return i >= 0 ? args[i + 1] : undefined; };
|
|
22
|
+
const tokenPath = get('--startup-token-file');
|
|
23
|
+
const token = process.env.CADRE_STARTUP_TOKEN ?? '';
|
|
24
|
+
if (tokenPath && token) { try { fs.writeFileSync(tokenPath, token, 'utf8'); } catch (e) { console.error(e); } }
|
|
25
|
+
process.on('SIGTERM', () => process.exit(0));
|
|
26
|
+
setInterval(() => {}, 1 << 30);
|
|
27
|
+
`;
|
|
28
|
+
|
|
29
|
+
const FCM = { projectId: 'proj', clientEmail: 'svc@proj.iam', privateKey: 'FCM-SECRET-PEM' };
|
|
30
|
+
const APNS = { keyId: 'KID', teamId: 'TEAM', bundleId: 'com.example.app', privateKey: 'APNS-SECRET-P8', production: false };
|
|
31
|
+
const CFG = { identityPath: 'C:/fake/identity.key', partyId: 'install-id-123', libp2pPort: 4655 };
|
|
32
|
+
|
|
33
|
+
let tmpRoot: string;
|
|
34
|
+
let scriptPath: string;
|
|
35
|
+
const orchestrators: HostProcessOrchestrator[] = [];
|
|
36
|
+
|
|
37
|
+
beforeEach(() => {
|
|
38
|
+
tmpRoot = mkdtempSync(join(tmpdir(), 'cadre-host-push-'));
|
|
39
|
+
scriptPath = join(tmpRoot, 'fake-cli.mjs');
|
|
40
|
+
writeFileSync(scriptPath, FAKE_CLI, 'utf8');
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
afterEach(async () => {
|
|
44
|
+
for (const orch of orchestrators) {
|
|
45
|
+
try { await orch.stopAuthorityNode(); } catch { /* ignore */ }
|
|
46
|
+
}
|
|
47
|
+
orchestrators.length = 0;
|
|
48
|
+
await sleep(50);
|
|
49
|
+
try { rmSync(tmpRoot, { recursive: true, force: true }); } catch { /* ignore */ }
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
function makeOrchestrator(rootDir: string, pushResolver?: () => Promise<PushCredentials | undefined>): HostProcessOrchestrator {
|
|
53
|
+
mkdirSync(rootDir, { recursive: true });
|
|
54
|
+
const orch = new HostProcessOrchestrator({
|
|
55
|
+
rootDir,
|
|
56
|
+
portRange: { start: 18000, end: 18499 },
|
|
57
|
+
stopTimeoutMs: 1500,
|
|
58
|
+
spawn: { entrypoint: scriptPath },
|
|
59
|
+
...(pushResolver ? { pushResolver } : {}),
|
|
60
|
+
});
|
|
61
|
+
orchestrators.push(orch);
|
|
62
|
+
return orch;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function readChildConfig(rootDir: string, containerId = AUTHORITY_CONTAINER_ID): Record<string, unknown> {
|
|
66
|
+
const raw = readFileSync(join(rootDir, containerId, 'cadre.json'), 'utf8');
|
|
67
|
+
return JSON.parse(raw) as Record<string, unknown>;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function sleep(ms: number): Promise<void> {
|
|
71
|
+
return new Promise((res) => setTimeout(res, ms));
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
describe('HostProcessOrchestrator push injection', () => {
|
|
75
|
+
it('writes config.push into cadre.json when the resolver yields credentials', async () => {
|
|
76
|
+
const rootDir = join(tmpRoot, 'a');
|
|
77
|
+
const orch = makeOrchestrator(rootDir, async () => ({ fcm: FCM, apns: APNS, cooldownMs: 1000 }));
|
|
78
|
+
await orch.init();
|
|
79
|
+
await orch.ensureAuthorityNode(CFG);
|
|
80
|
+
|
|
81
|
+
const cfg = readChildConfig(rootDir);
|
|
82
|
+
expect(cfg.push).toEqual({ fcm: FCM, apns: APNS, cooldownMs: 1000 });
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
it('omits config.push when no resolver is configured', async () => {
|
|
86
|
+
const rootDir = join(tmpRoot, 'b');
|
|
87
|
+
const orch = makeOrchestrator(rootDir);
|
|
88
|
+
await orch.init();
|
|
89
|
+
await orch.ensureAuthorityNode(CFG);
|
|
90
|
+
|
|
91
|
+
const cfg = readChildConfig(rootDir);
|
|
92
|
+
expect(cfg.push).toBeUndefined();
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
it('omits config.push when the resolver returns undefined (push opt-in)', async () => {
|
|
96
|
+
const rootDir = join(tmpRoot, 'c');
|
|
97
|
+
const orch = makeOrchestrator(rootDir, async () => undefined);
|
|
98
|
+
await orch.init();
|
|
99
|
+
await orch.ensureAuthorityNode(CFG);
|
|
100
|
+
|
|
101
|
+
expect(readChildConfig(rootDir).push).toBeUndefined();
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
it('degrades to no push (does not fail the spawn) when the resolver throws', async () => {
|
|
105
|
+
const rootDir = join(tmpRoot, 'd');
|
|
106
|
+
const orch = makeOrchestrator(rootDir, async () => { throw new Error('partial creds'); });
|
|
107
|
+
await orch.init();
|
|
108
|
+
// Spawn still succeeds.
|
|
109
|
+
const node = await orch.ensureAuthorityNode(CFG);
|
|
110
|
+
expect(node.id).toBe(AUTHORITY_CONTAINER_ID);
|
|
111
|
+
expect(readChildConfig(rootDir).push).toBeUndefined();
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
it('re-resolves on re-spawn and never persists a raw private key in state.json', async () => {
|
|
115
|
+
const rootDir = join(tmpRoot, 'e');
|
|
116
|
+
let current: PushCredentials | undefined = { fcm: FCM };
|
|
117
|
+
const orch = makeOrchestrator(rootDir, async () => current);
|
|
118
|
+
await orch.init();
|
|
119
|
+
await orch.ensureAuthorityNode(CFG);
|
|
120
|
+
expect(readChildConfig(rootDir).push).toEqual({ fcm: FCM });
|
|
121
|
+
|
|
122
|
+
// state.json must not carry the raw key — only re-resolvable references.
|
|
123
|
+
const stateRaw = readFileSync(join(rootDir, 'state.json'), 'utf8');
|
|
124
|
+
expect(stateRaw).not.toContain('FCM-SECRET-PEM');
|
|
125
|
+
|
|
126
|
+
// Rotate the resolver's answer, then restart — the new spawn must pick it up.
|
|
127
|
+
current = { apns: APNS };
|
|
128
|
+
await orch.restartAuthorityNode();
|
|
129
|
+
const cfg2 = readChildConfig(rootDir);
|
|
130
|
+
expect(cfg2.push).toEqual({ apns: APNS });
|
|
131
|
+
expect((cfg2.push as PushCredentials).fcm).toBeUndefined();
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
it('injects push for a storage-profile managed node but not a transaction node', async () => {
|
|
135
|
+
const rootDir = join(tmpRoot, 'f');
|
|
136
|
+
const orch = makeOrchestrator(rootDir, async () => ({ fcm: FCM }));
|
|
137
|
+
await orch.init();
|
|
138
|
+
|
|
139
|
+
await orch.createContainer({ containerId: 'storage-1', partyId: 'p', bootstrapNodes: [], profile: 'storage' });
|
|
140
|
+
expect(readChildConfig(rootDir, 'storage-1').push).toEqual({ fcm: FCM });
|
|
141
|
+
|
|
142
|
+
await orch.createContainer({ containerId: 'txn-1', partyId: 'p', bootstrapNodes: [], profile: 'transaction' });
|
|
143
|
+
expect(readChildConfig(rootDir, 'txn-1').push).toBeUndefined();
|
|
144
|
+
});
|
|
145
|
+
});
|
|
@@ -2,10 +2,9 @@ import { afterEach, beforeEach, describe, expect, it } from 'vitest';
|
|
|
2
2
|
import { mkdtempSync, rmSync } from 'node:fs';
|
|
3
3
|
import { tmpdir } from 'node:os';
|
|
4
4
|
import { join } from 'node:path';
|
|
5
|
-
import { generatePrivateKey, getPublicKey } from '@optimystic/quereus-plugin-crypto';
|
|
6
5
|
import { generateKeyPair } from '@libp2p/crypto/keys';
|
|
7
6
|
import { peerIdFromPrivateKey } from '@libp2p/peer-id';
|
|
8
|
-
import { CadreNode } from '@serfab/cadre-core';
|
|
7
|
+
import { CadreNode, authorityKeyFromLibp2p } from '@serfab/cadre-core';
|
|
9
8
|
|
|
10
9
|
import { TrustCircleService } from '../trust-circle.js';
|
|
11
10
|
import { TrustCircleStore } from '../trust-circle-store.js';
|
|
@@ -36,18 +35,19 @@ describe('TrustCircleService — real CadreNode integration', () => {
|
|
|
36
35
|
beforeEach(async () => {
|
|
37
36
|
tmpRoot = mkdtempSync(join(tmpdir(), 'cadre-host-tc-int-'));
|
|
38
37
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
38
|
+
// Own-authority host: the node's libp2p identity key IS its authority key
|
|
39
|
+
// (the cadre-cli `--authority` shape), so registerSelf can authority-sign the
|
|
40
|
+
// INSERT of its own CadrePeer row.
|
|
41
|
+
const hostKey = await generateKeyPair('Ed25519');
|
|
42
|
+
const authority = authorityKeyFromLibp2p(hostKey);
|
|
43
|
+
hostAuthorityPrivateKey = authority.privateKeyB64;
|
|
44
|
+
hostAuthorityPublicKey = authority.publicKeyB64;
|
|
46
45
|
|
|
47
46
|
const baseId = Math.random().toString(36).slice(2);
|
|
48
47
|
|
|
49
48
|
host = new CadreNode({
|
|
50
49
|
controlNetwork: { partyId: `host-${baseId}`, bootstrapNodes: [] },
|
|
50
|
+
privateKey: hostKey,
|
|
51
51
|
profile: 'transaction'
|
|
52
52
|
});
|
|
53
53
|
|
|
@@ -59,6 +59,11 @@ describe('TrustCircleService — real CadreNode integration', () => {
|
|
|
59
59
|
|
|
60
60
|
host.initializeSeedBootstrap(hostAuthorityPrivateKey);
|
|
61
61
|
|
|
62
|
+
// The authority writes its own CadrePeer row up-front (the implement ticket's
|
|
63
|
+
// CLI change). After this the host's own peerId is a member, so it shows up
|
|
64
|
+
// alongside any redeemed peers in the trust-circle listing.
|
|
65
|
+
await host.registerSelf();
|
|
66
|
+
|
|
62
67
|
store = new TrustCircleStore(tmpRoot);
|
|
63
68
|
service = new TrustCircleService({ cadreNode: host, store });
|
|
64
69
|
}, 60_000);
|
|
@@ -68,7 +73,7 @@ describe('TrustCircleService — real CadreNode integration', () => {
|
|
|
68
73
|
try { rmSync(tmpRoot, { recursive: true, force: true }); } catch { /* ignore */ }
|
|
69
74
|
});
|
|
70
75
|
|
|
71
|
-
it('
|
|
76
|
+
it('issues → redeems → lists against the real control DB (listMembers path)', async () => {
|
|
72
77
|
// Derive a shape-valid peerId from a fresh Ed25519 key — same pattern as
|
|
73
78
|
// packages/cadre-core/test/seed-bootstrap.spec.ts. The AuthorizedInsert
|
|
74
79
|
// constraint only checks the signature over digest(PeerId,…); peerId
|
|
@@ -92,25 +97,39 @@ describe('TrustCircleService — real CadreNode integration', () => {
|
|
|
92
97
|
// Verify directly against the control DB: the row is there.
|
|
93
98
|
expect(await isInCadrePeer(host, phonePeerId)).toBe(true);
|
|
94
99
|
|
|
95
|
-
// List —
|
|
100
|
+
// List — exercises the CadreNode.listMembers() path. CadrePeer now carries
|
|
101
|
+
// the host's own self-registered row in addition to the redeemed phone.
|
|
96
102
|
const snap = await service.list();
|
|
97
|
-
expect(snap.members).toHaveLength(
|
|
98
|
-
|
|
99
|
-
expect(
|
|
103
|
+
expect(snap.members).toHaveLength(2);
|
|
104
|
+
const phoneMember = snap.members.find((m) => m.peerId === phonePeerId);
|
|
105
|
+
expect(phoneMember).toBeDefined();
|
|
106
|
+
expect(phoneMember!.label).toBe("Mom's phone");
|
|
107
|
+
// The host's own peerId is present too (unlabeled → bare peerId as label).
|
|
108
|
+
expect(snap.members.some((m) => m.peerId === host.peerId!.toString())).toBe(true);
|
|
100
109
|
expect(snap.pending).toHaveLength(0);
|
|
110
|
+
}, 90_000);
|
|
111
|
+
|
|
112
|
+
// The signed `CadrePeer` DELETE previously threw a Quereus deferred-constraint
|
|
113
|
+
// error ("No row context found for column PeerId") inside cadre-core's
|
|
114
|
+
// removePeer. That upstream bug is fixed (`quereus-cadrepeer-delete-no-row-context`,
|
|
115
|
+
// landed), so the full issue → redeem → remove cycle is now exercised here.
|
|
116
|
+
it('removes a member from CadrePeer', async () => {
|
|
117
|
+
const phoneKey = await generateKeyPair('Ed25519');
|
|
118
|
+
const phonePeerId = peerIdFromPrivateKey(phoneKey).toString();
|
|
119
|
+
|
|
120
|
+
const issued = await service.issueInvite({ label: "Mom's phone" });
|
|
121
|
+
await service.redeemInvite({ token: issued.token, peerId: phonePeerId });
|
|
122
|
+
expect(await isInCadrePeer(host, phonePeerId)).toBe(true);
|
|
101
123
|
|
|
102
|
-
// Remove
|
|
103
124
|
await service.removeMember(phonePeerId);
|
|
104
125
|
|
|
105
|
-
// CadrePeer row is gone (validates DELETE-with-context end-to-end).
|
|
106
126
|
expect(await isInCadrePeer(host, phonePeerId)).toBe(false);
|
|
107
|
-
|
|
108
|
-
// Local label is gone.
|
|
109
127
|
expect(store.getMember(phonePeerId)).toBeUndefined();
|
|
110
|
-
|
|
111
|
-
// list() reflects the removal.
|
|
112
128
|
const afterRemove = await service.list();
|
|
113
|
-
|
|
129
|
+
// The phone is gone; the host's own self-registered row remains.
|
|
130
|
+
expect(afterRemove.members.map((m) => m.peerId)).not.toContain(phonePeerId);
|
|
131
|
+
expect(afterRemove.members).toHaveLength(1);
|
|
132
|
+
expect(afterRemove.members[0]!.peerId).toBe(host.peerId!.toString());
|
|
114
133
|
}, 90_000);
|
|
115
134
|
});
|
|
116
135
|
|
|
@@ -9,11 +9,12 @@ 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 { AuthorityNodeUnavailableError } from '../../authority/authority-node-client.js';
|
|
12
13
|
|
|
13
14
|
/**
|
|
14
|
-
* Minimal in-memory mock of
|
|
15
|
-
* assertions can verify the auth + redemption flow without standing up a
|
|
16
|
-
* real libp2p control network.
|
|
15
|
+
* Minimal in-memory mock of the authority-node channel adapter. Records calls
|
|
16
|
+
* so assertions can verify the auth + redemption flow without standing up a
|
|
17
|
+
* real libp2p control network or admin channel.
|
|
17
18
|
*/
|
|
18
19
|
class MockCadreNode implements CadreNodeLike {
|
|
19
20
|
readonly authorizedPeers = new Set<string>();
|
|
@@ -23,8 +24,8 @@ class MockCadreNode implements CadreNodeLike {
|
|
|
23
24
|
|
|
24
25
|
/** Toggle to make acceptPhone reject. */
|
|
25
26
|
acceptShouldThrow: Error | null = null;
|
|
26
|
-
/**
|
|
27
|
-
|
|
27
|
+
/** When false, membership calls throw AuthorityNodeUnavailableError. */
|
|
28
|
+
nodeAvailable = true;
|
|
28
29
|
|
|
29
30
|
async createInvite(token?: string, expiresIn?: number) {
|
|
30
31
|
this.issuedInvites.push({ token, expiresIn });
|
|
@@ -53,21 +54,14 @@ class MockCadreNode implements CadreNodeLike {
|
|
|
53
54
|
return 'enc:' + JSON.stringify(invite);
|
|
54
55
|
}
|
|
55
56
|
|
|
56
|
-
|
|
57
|
-
if (!this.
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
if (peers.has(target)) yield { PeerId: target };
|
|
65
|
-
return;
|
|
66
|
-
}
|
|
67
|
-
for (const p of peers) yield { PeerId: p };
|
|
68
|
-
},
|
|
69
|
-
}),
|
|
70
|
-
} as unknown as ReturnType<CadreNodeLike['getControlDatabase']>;
|
|
57
|
+
async listMembers(): Promise<Array<{ peerId: string; multiaddr: string | null }>> {
|
|
58
|
+
if (!this.nodeAvailable) throw new AuthorityNodeUnavailableError('node down');
|
|
59
|
+
return [...this.authorizedPeers].map((peerId) => ({ peerId, multiaddr: null }));
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
async isMember(peerId: string): Promise<boolean> {
|
|
63
|
+
if (!this.nodeAvailable) throw new AuthorityNodeUnavailableError('node down');
|
|
64
|
+
return this.authorizedPeers.has(peerId);
|
|
71
65
|
}
|
|
72
66
|
}
|
|
73
67
|
|
|
@@ -204,6 +198,71 @@ describe('TrustCircleService.redeemInvite', () => {
|
|
|
204
198
|
expect(cadreNode.authorizedPeers.has('pA')).toBe(true);
|
|
205
199
|
expect(cadreNode.authorizedPeers.has('pB')).toBe(false);
|
|
206
200
|
});
|
|
201
|
+
|
|
202
|
+
it('preserves the pending token when acceptPhone fails with node_unavailable', async () => {
|
|
203
|
+
const { token } = await service.issueInvite({ label: 'Transient' });
|
|
204
|
+
cadreNode.acceptShouldThrow = new AuthorityNodeUnavailableError('node down');
|
|
205
|
+
|
|
206
|
+
await expect(service.redeemInvite({ token, peerId: 'pTrans' }))
|
|
207
|
+
.rejects.toMatchObject({ code: 'node_unavailable' });
|
|
208
|
+
|
|
209
|
+
// Token must still be redeemable.
|
|
210
|
+
expect(store.getPending(token)).toBeDefined();
|
|
211
|
+
expect(cadreNode.authorizedPeers.has('pTrans')).toBe(false);
|
|
212
|
+
|
|
213
|
+
// Retry after the node recovers.
|
|
214
|
+
cadreNode.acceptShouldThrow = null;
|
|
215
|
+
await expect(service.redeemInvite({ token, peerId: 'pTrans' }))
|
|
216
|
+
.resolves.toMatchObject({ peerId: 'pTrans', label: 'Transient' });
|
|
217
|
+
expect(cadreNode.authorizedPeers.has('pTrans')).toBe(true);
|
|
218
|
+
expect(store.getPending(token)).toBeUndefined();
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
it('concurrent redeem during a gated node_unavailable leaves the token re-redeemable', async () => {
|
|
222
|
+
const { token } = await service.issueInvite({ label: 'GatedFail' });
|
|
223
|
+
|
|
224
|
+
// Gate acceptPhone — reject on release with a transient error so the
|
|
225
|
+
// first redeem hits node_unavailable. The second redeem should still
|
|
226
|
+
// bounce synchronously while the gate is held.
|
|
227
|
+
let release!: () => void;
|
|
228
|
+
const gate = new Promise<void>((_, reject) => { release = () => reject(new AuthorityNodeUnavailableError('node down')); });
|
|
229
|
+
cadreNode.acceptPhone = async () => {
|
|
230
|
+
await gate;
|
|
231
|
+
};
|
|
232
|
+
|
|
233
|
+
const first = service.redeemInvite({ token, peerId: 'pA' });
|
|
234
|
+
const second = service.redeemInvite({ token, peerId: 'pB' });
|
|
235
|
+
|
|
236
|
+
await expect(second).rejects.toMatchObject({ code: 'already_redeemed' });
|
|
237
|
+
|
|
238
|
+
release();
|
|
239
|
+
await expect(first).rejects.toMatchObject({ code: 'node_unavailable' });
|
|
240
|
+
|
|
241
|
+
// Pending row preserved; in-flight Set cleared via finally.
|
|
242
|
+
expect(store.getPending(token)).toBeDefined();
|
|
243
|
+
expect(cadreNode.authorizedPeers.has('pA')).toBe(false);
|
|
244
|
+
expect(cadreNode.authorizedPeers.has('pB')).toBe(false);
|
|
245
|
+
|
|
246
|
+
// Third redeem (after in-flight slot freed) succeeds against a healthy node.
|
|
247
|
+
cadreNode.acceptPhone = async (options) => {
|
|
248
|
+
cadreNode.authorizedPeers.add(options.phonePeerId);
|
|
249
|
+
};
|
|
250
|
+
await expect(service.redeemInvite({ token, peerId: 'pC' }))
|
|
251
|
+
.resolves.toMatchObject({ peerId: 'pC', label: 'GatedFail' });
|
|
252
|
+
expect(store.getPending(token)).toBeUndefined();
|
|
253
|
+
expect(cadreNode.authorizedPeers.has('pC')).toBe(true);
|
|
254
|
+
});
|
|
255
|
+
|
|
256
|
+
it('preserves the pending token on non-transient acceptPhone failures (admin must revoke)', async () => {
|
|
257
|
+
const { token } = await service.issueInvite({ label: 'OtherFail' });
|
|
258
|
+
cadreNode.acceptShouldThrow = new Error('cadre-core rejected the peer');
|
|
259
|
+
|
|
260
|
+
await expect(service.redeemInvite({ token, peerId: 'pX' }))
|
|
261
|
+
.rejects.toThrow('cadre-core rejected the peer');
|
|
262
|
+
|
|
263
|
+
expect(store.getPending(token)).toBeDefined();
|
|
264
|
+
expect(cadreNode.authorizedPeers.has('pX')).toBe(false);
|
|
265
|
+
});
|
|
207
266
|
});
|
|
208
267
|
|
|
209
268
|
describe('TrustCircleService.issueInvite — label length', () => {
|
|
@@ -290,16 +349,24 @@ describe('TrustCircleService.list', () => {
|
|
|
290
349
|
expect(store.getMember('orphan')).toBeUndefined();
|
|
291
350
|
});
|
|
292
351
|
|
|
293
|
-
it('falls back to local labels when
|
|
352
|
+
it('falls back to local labels when the authority node is unavailable', async () => {
|
|
294
353
|
store.addMember({ peerId: 'pZ', label: 'Zoe', addedAt: 't' });
|
|
295
|
-
cadreNode.
|
|
354
|
+
cadreNode.nodeAvailable = false;
|
|
296
355
|
|
|
297
356
|
const snap = await service.list();
|
|
298
357
|
expect(snap.members.some(m => m.peerId === 'pZ' && m.label === 'Zoe')).toBe(true);
|
|
299
|
-
// Orphan check is skipped when
|
|
358
|
+
// Orphan check is skipped when the node is unavailable.
|
|
300
359
|
expect(store.getMember('pZ')).toBeDefined();
|
|
301
360
|
});
|
|
302
361
|
|
|
362
|
+
it('issueInvite surfaces node_unavailable when the channel is down', async () => {
|
|
363
|
+
const downNode = new MockCadreNode();
|
|
364
|
+
downNode.createInvite = async () => { throw new AuthorityNodeUnavailableError('refused'); };
|
|
365
|
+
const svc = new TrustCircleService({ cadreNode: downNode, store });
|
|
366
|
+
await expect(svc.issueInvite({ label: 'Nina' }))
|
|
367
|
+
.rejects.toMatchObject({ code: 'node_unavailable' });
|
|
368
|
+
});
|
|
369
|
+
|
|
303
370
|
it('shows unlabelled CadrePeer rows with peerId as label', async () => {
|
|
304
371
|
cadreNode.authorizedPeers.add('unlabeled-peer');
|
|
305
372
|
const snap = await service.list();
|