返回 CodeWhale
check-cloud-facts.mjs
根目录 / web / scripts / check-cloud-facts.mjs
1 #!/usr/bin/env node
2 /** Local source, release, pinned-key parity and public-fixture gate. */
3 import { existsSync } from "node:fs";
4 import { dirname, resolve } from "node:path";
5 import { fileURLToPath } from "node:url";
6 import { validateSource, verifyEnvelope, parseTsKeys, validateTrustedKeys, deriveBundledRegistryTrust, readBoundedFile } from "./facts-publish.mjs";
7
8 const WEB_ROOT = resolve(dirname(fileURLToPath(import.meta.url)), "..");
9 const REPO_ROOT = resolve(WEB_ROOT, "..");
10 export { parseTsKeys };
11
12 export function parseRustKeys(text) {
13 const source = text.replace(/\/\*[\s\S]*?\*\//g, "").replace(/^\s*\/\/.*$/gm, "");
14 const tables = [...source.matchAll(/^\s*pub\s+const\s+TRUSTED_KEYS\s*:\s*&\s*\[TrustedKey\]\s*=\s*&\s*\[([\s\S]*?)\]\s*;/gm)];
15 if (tables.length !== 1) throw new Error("cannot parse exactly one Rust TRUSTED_KEYS table");
16 const table = tables[0];
17 const body = table[1].replace(/^\s*\/\/.*$/gm, "");
18 const keys = [];
19 const remainder = body.replace(/TrustedKey\s*\{\s*key_id:\s*"([^"]+)",\s*public_key:\s*\[([^\]]*)\],\s*status:\s*KeyStatus::(Active|Retired),\s*role:\s*KeyRole::(Founder|Automation)\s*,?\s*\}/g, (_, keyId, encoded, status, role) => {
20 const pieces = encoded.split(",").map((piece) => piece.trim()).filter(Boolean);
21 if (pieces.length !== 32 || pieces.some((piece) => !/^(?:\d+|0x[0-9a-fA-F]+)$/.test(piece))) throw new Error("Rust public key must contain 32 literal bytes");
22 const bytes = pieces.map(Number);
23 if (bytes.some((byte) => !Number.isInteger(byte) || byte < 0 || byte > 255)) throw new Error("Rust public key byte out of range");
24 keys.push({ keyId, publicKey: Buffer.from(bytes).toString("base64"), status: status.toLowerCase(), role: role.toLowerCase() });
25 return "";
26 });
27 if (remainder.replace(/[\s,]/g, "")) throw new Error("unparsed Rust TRUSTED_KEYS entry");
28 return validateTrustedKeys(keys);
29 }
30
31 function text(path) { return readBoundedFile(path).toString("utf8"); }
32 function json(path) { return JSON.parse(text(path)); }
33
34 export function checkCloudFacts() {
35 const failures = [];
36 const source = json(resolve(REPO_ROOT, "docs/cloud-facts/stable.json"));
37 for (const error of validateSource(source)) failures.push(`stable.json: ${error}`);
38 if (source.channel !== "stable") failures.push("stable.json: channel must be stable");
39 const latest = json(resolve(WEB_ROOT, "data/latest-published-release.json"));
40 if (source.release?.latest !== latest.version) failures.push("stable.json release.latest differs from latest-published-release.json");
41 if (source.release?.release_url && source.release.release_url !== latest.url) failures.push("stable.json release.release_url differs from latest-published-release.json");
42 // An explicit empty table is valid and inert; parse failures are never empty.
43 const rustKeys = parseRustKeys(text(resolve(REPO_ROOT, "crates/config/src/cloud_facts/keys.rs")));
44 const tsKeys = parseTsKeys(text(resolve(WEB_ROOT, "lib/cloud-facts/keys.ts")));
45 if (JSON.stringify(rustKeys) !== JSON.stringify(tsKeys)) failures.push("Rust and web pinned key tables diverge");
46 const expectedBundled = deriveBundledRegistryTrust(json(resolve(REPO_ROOT, "crates/config/assets/provider_descriptors.json")));
47 const committedBundled = json(resolve(WEB_ROOT, "lib/cloud-facts/bundled-providers.json"));
48 if (JSON.stringify(expectedBundled) !== JSON.stringify(committedBundled)) {
49 failures.push("crates/config/assets/provider_descriptors.json and web/lib/cloud-facts/bundled-providers.json diverge");
50 }
51 const crateFixtures = resolve(REPO_ROOT, "crates/cloud-facts/fixtures");
52 const docsFixtures = resolve(REPO_ROOT, "docs/cloud-facts/fixtures");
53 const testOnlyPub = text(resolve(crateFixtures, "test-only.pub")).trim();
54 for (const name of ["envelope-stable-v7.json", "envelope-future-only-v8.json"]) {
55 const result = verifyEnvelope(json(resolve(crateFixtures, name)), testOnlyPub);
56 if (!result.ok) failures.push(`fixture ${name}: ${result.errors.join("; ")}`);
57 }
58 for (const name of [
59 "automation-scope-cases.json",
60 "envelope-future-only-v8.json",
61 "envelope-stable-v7.json",
62 "source-fixture.json",
63 "source-future-only.json",
64 "test-only.pub",
65 ]) {
66 const docsCopy = resolve(docsFixtures, name);
67 if (existsSync(docsCopy) && text(docsCopy) !== text(resolve(crateFixtures, name))) {
68 failures.push(`fixture ${name}: docs/cloud-facts/fixtures and crates/cloud-facts/fixtures diverge`);
69 }
70 }
71 if ([...rustKeys, ...tsKeys].some((key) => key.publicKey === testOnlyPub || key.keyId === "cwf-test-only")) failures.push("TEST-ONLY keys must never be production trust anchors");
72 return { failures, factsVersion: source.facts_version, activeKeys: tsKeys.filter((key) => key.status === "active").length };
73 }
74
75 if (process.argv[1] && resolve(process.argv[1]) === fileURLToPath(import.meta.url)) {
76 try {
77 const result = checkCloudFacts();
78 if (result.failures.length) throw new Error(result.failures.join("\n - "));
79 console.log(`check-cloud-facts: OK (facts_version=${result.factsVersion}, ${result.activeKeys} active production keys)`);
80 } catch (error) {
81 console.error(`check-cloud-facts: FAIL\n - ${error.message}`);
82 process.exitCode = 1;
83 }
84 }
85
85 lines Plain Text