163 lines
6.4 KiB
TypeScript
163 lines
6.4 KiB
TypeScript
import test from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import { createHash, generateKeyPairSync, sign } from 'node:crypto';
|
|
import { StrictDomainRuntimeHandoffSource } from './domain-runtime-handoff-source.js';
|
|
import {
|
|
createNodePossessionChallenge,
|
|
nodePossessionChallengeSigningBytes,
|
|
verifyNodePossessionResponse,
|
|
} from './node-possession-proof.js';
|
|
import {
|
|
nodeRegistrationClaimSigningBytes,
|
|
parseNodeRegistrationEndpointRegistry,
|
|
resolveNodeRegistrationEndpoint,
|
|
verifyNodeRegistrationClaim,
|
|
} from './node-registration.js';
|
|
|
|
const NOW = 1_786_291_200_000;
|
|
const DOMAIN_ID = 'DOM-FIFTH-0001';
|
|
const NODE_ID = 'JD-FD-PRIMARY';
|
|
const NODE_TYPE = 'cloud-resident' as const;
|
|
const registrationKeys = generateKeyPairSync('ed25519');
|
|
const nodeKeys = generateKeyPairSync('ed25519');
|
|
const publicKeyPem = nodeKeys.publicKey.export({ format: 'pem', type: 'spki' }).toString();
|
|
const fingerprint = createHash('sha256')
|
|
.update(nodeKeys.publicKey.export({ format: 'der', type: 'spki' }))
|
|
.digest('hex');
|
|
|
|
function verifiedPossession() {
|
|
const registry = parseNodeRegistrationEndpointRegistry({
|
|
endpoints: [{
|
|
algorithm: 'Ed25519',
|
|
domainIds: [DOMAIN_ID],
|
|
endpointId: 'GH-NODE-REG-001',
|
|
nodeTypes: [NODE_TYPE],
|
|
publicKeyPem: registrationKeys.publicKey.export({ format: 'pem', type: 'spki' }).toString(),
|
|
signerId: 'GH-NODE-REG-SIGNER-001',
|
|
status: 'ACTIVE',
|
|
url: 'https://guanghulab.com/api/ai/v1/node-registrations/claims',
|
|
}],
|
|
registryId: 'GH-AIOS-NODE-REGISTRATION-ENDPOINTS-001',
|
|
schema: 'gh-aios.node-registration-endpoints/v1',
|
|
state: 'CURRENT',
|
|
version: '1.0.0',
|
|
}, {
|
|
repositoryId: 'REPO-012',
|
|
sourceCommit: 'a'.repeat(40),
|
|
sourceUrl: `https://guanghulab.com/code/bingshuo/guanghu-ice-heart/raw/commit/${'a'.repeat(40)}/routing/node-registration-endpoints.json`,
|
|
});
|
|
const endpoint = resolveNodeRegistrationEndpoint(registry, { domainId: DOMAIN_ID, nodeType: NODE_TYPE });
|
|
assert.ok(endpoint);
|
|
const payload = {
|
|
accountId: 'bingshuo',
|
|
claimId: 'NODE-CLAIM-001',
|
|
domainId: DOMAIN_ID,
|
|
endpointId: endpoint.endpointId,
|
|
expiresAt: NOW + 120_000,
|
|
issuedAt: NOW - 1_000,
|
|
nodeId: NODE_ID,
|
|
nodeKeyFingerprint: fingerprint,
|
|
nodeType: NODE_TYPE,
|
|
schema: 'gh-aios.node-registration-claim/v1' as const,
|
|
signerId: endpoint.signerId,
|
|
};
|
|
const registration = verifyNodeRegistrationClaim({
|
|
...payload,
|
|
signature: sign(null, nodeRegistrationClaimSigningBytes(payload), registrationKeys.privateKey).toString('base64'),
|
|
}, {
|
|
accountId: payload.accountId,
|
|
domainId: DOMAIN_ID,
|
|
nodeId: NODE_ID,
|
|
nodeType: NODE_TYPE,
|
|
}, endpoint, NOW);
|
|
const challenge = createNodePossessionChallenge(
|
|
registration,
|
|
NOW,
|
|
'NODE-CHALLENGE-001',
|
|
'A'.repeat(43),
|
|
);
|
|
const response = {
|
|
challengeId: challenge.challengeId,
|
|
publicKeyPem,
|
|
schema: 'gh-aios.node-possession-response/v1' as const,
|
|
signature: sign(null, nodePossessionChallengeSigningBytes(challenge), nodeKeys.privateKey).toString('base64'),
|
|
};
|
|
return verifyNodePossessionResponse(response, challenge, registration, NOW + 1);
|
|
}
|
|
|
|
function response(requestId: string, overrides: Record<string, unknown> = {}) {
|
|
return {
|
|
schema: 'gh-aios.domain-runtime-handoff-response/v1',
|
|
handoff: { manifest: { digest: 'untrusted-until-orchestrator-verifies' } },
|
|
request_id: requestId,
|
|
signer_lookup: {
|
|
repository_id: 'REPO-014',
|
|
signer_id: 'GH-LIGHTHOUSE-001',
|
|
},
|
|
status: 'ISSUED',
|
|
...overrides,
|
|
};
|
|
}
|
|
|
|
test('forwards one exact cryptographic authorization bundle without private material', async () => {
|
|
const requests: unknown[] = [];
|
|
const source = new StrictDomainRuntimeHandoffSource({
|
|
async request(input) {
|
|
requests.push(input);
|
|
return response(input.requestId);
|
|
},
|
|
}, () => NOW + 2, () => 'HANDOFF-REQ-001');
|
|
|
|
const candidate = await source.read(verifiedPossession());
|
|
|
|
assert.equal(requests.length, 1);
|
|
assert.deepEqual(Object.keys(requests[0] as object).sort(), [
|
|
'accountId', 'challenge', 'domainId', 'issuedAt', 'nodeId', 'nodeType',
|
|
'registration', 'requestId', 'response', 'schema',
|
|
]);
|
|
assert.equal(JSON.stringify(requests[0]).includes('PRIVATE KEY'), false);
|
|
assert.deepEqual(candidate, {
|
|
handoff: { manifest: { digest: 'untrusted-until-orchestrator-verifies' } },
|
|
signerLookup: { repositoryId: 'REPO-014', signerId: 'GH-LIGHTHOUSE-001' },
|
|
});
|
|
});
|
|
|
|
test('binds the response to the generated request and rejects malformed envelopes', async () => {
|
|
const samples = [
|
|
null,
|
|
response('OTHER-REQ'),
|
|
response('HANDOFF-REQ-001', { extra: true }),
|
|
response('HANDOFF-REQ-001', { schema: 'other/v1' }),
|
|
response('HANDOFF-REQ-001', { signer_lookup: { repository_id: 'REPO-014', signer_id: 'bad signer' } }),
|
|
response('HANDOFF-REQ-001', { signer_lookup: { repository_id: 'REPO-014', signer_id: 'GH-LIGHTHOUSE-001', public_key: 'forbidden' } }),
|
|
response('HANDOFF-REQ-001', { handoff: null }),
|
|
response('HANDOFF-REQ-001', { handoff: null, signer_lookup: null, status: 'NOT_AUTHORIZED' }),
|
|
];
|
|
for (const sample of samples) {
|
|
const source = new StrictDomainRuntimeHandoffSource({ async request() { return sample; } }, () => NOW + 2, () => 'HANDOFF-REQ-001');
|
|
assert.equal(await source.read(verifiedPossession()), null);
|
|
}
|
|
});
|
|
|
|
test('rejects copied, expired and replayed possession authorizations before transport', async () => {
|
|
let requests = 0;
|
|
const possession = verifiedPossession();
|
|
const source = new StrictDomainRuntimeHandoffSource({
|
|
async request(input) { requests += 1; return response(input.requestId); },
|
|
}, () => NOW + 2, () => 'HANDOFF-REQ-001');
|
|
assert.ok(await source.read(possession));
|
|
assert.equal(await source.read(possession), null);
|
|
assert.equal(await source.read({ ...verifiedPossession() }), null);
|
|
const expired = new StrictDomainRuntimeHandoffSource({
|
|
async request(input) { requests += 1; return response(input.requestId); },
|
|
}, () => NOW + 60_000, () => 'HANDOFF-REQ-002');
|
|
assert.equal(await expired.read(verifiedPossession()), null);
|
|
assert.equal(requests, 1);
|
|
});
|
|
|
|
test('fails closed on transport errors without exposing remote details', async () => {
|
|
const source = new StrictDomainRuntimeHandoffSource({
|
|
async request() { throw new Error('secret remote detail'); },
|
|
}, () => NOW + 2, () => 'HANDOFF-REQ-001');
|
|
assert.equal(await source.read(verifiedPossession()), null);
|
|
});
|