116 lines
5.3 KiB
JavaScript
116 lines
5.3 KiB
JavaScript
#!/usr/bin/env node
|
|
|
|
import { createHash } from "node:crypto";
|
|
import { spawnSync } from "node:child_process";
|
|
import { cp, lstat, mkdir, mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
|
|
import { tmpdir } from "node:os";
|
|
import { dirname, join, resolve } from "node:path";
|
|
import { fileURLToPath } from "node:url";
|
|
|
|
const scriptDir = dirname(fileURLToPath(import.meta.url));
|
|
const platformRoot = resolve(scriptDir, "../..");
|
|
const workspaceRoot = resolve(platformRoot, "..");
|
|
const foundryRoot = resolve(workspaceRoot, "NODEDC_DESIGN_GUIDELINE");
|
|
const artifactDir = resolve(process.env.NODEDC_DEPLOY_ARTIFACT_DIR || resolve(scriptDir, "../deploy-artifacts"));
|
|
const [patchId = "module-foundry-consumer-policy-v4-20260720-001", ...extra] = process.argv.slice(2);
|
|
|
|
if (extra.length || !/^[A-Za-z0-9._-]{1,96}$/.test(patchId)) {
|
|
throw new Error("patch_id_must_contain_only_letters_digits_dot_underscore_hyphen");
|
|
}
|
|
|
|
const files = [
|
|
"registry/data-product-consumer-policies.json",
|
|
"scripts/validate-registry.mjs",
|
|
"server/foundry-data-product-consumer.mjs",
|
|
"server/foundry-data-product-consumer.test.mjs",
|
|
];
|
|
const stage = await mkdtemp(join(tmpdir(), "nodedc-module-foundry-consumer-policy-artifact-"));
|
|
const payload = join(stage, "payload");
|
|
const artifact = join(artifactDir, `nodedc-module-foundry-${patchId}.tgz`);
|
|
const checksum = `${artifact}.sha256`;
|
|
|
|
await assertConsumerPolicyBoundary();
|
|
|
|
try {
|
|
await mkdir(payload, { recursive: true });
|
|
for (const relativePath of files) {
|
|
const source = resolve(foundryRoot, relativePath);
|
|
const sourceStat = await lstat(source);
|
|
if (!sourceStat.isFile() || sourceStat.isSymbolicLink()) {
|
|
throw new Error(`source_file_rejected:${relativePath}`);
|
|
}
|
|
const destination = join(payload, relativePath);
|
|
await mkdir(dirname(destination), { recursive: true });
|
|
await cp(source, destination, { force: true, verbatimSymlinks: true });
|
|
}
|
|
|
|
await writeFile(join(stage, "manifest.env"), `id=${patchId}\ncomponent=module-foundry\ntype=app-overlay\n`, "utf8");
|
|
await writeFile(join(stage, "files.txt"), `${files.join("\n")}\n`, "utf8");
|
|
await mkdir(artifactDir, { recursive: true });
|
|
|
|
const tar = spawnSync("python3", ["-c", canonicalTarScript(), artifact, stage], {
|
|
encoding: "utf8",
|
|
maxBuffer: 32 * 1024 * 1024,
|
|
});
|
|
if (tar.status !== 0) throw new Error(`tar_failed:${tar.stderr || tar.stdout}`);
|
|
|
|
const sha256 = createHash("sha256").update(await readFile(artifact)).digest("hex");
|
|
await writeFile(checksum, `${sha256} ${artifact.split("/").at(-1)}\n`, "utf8");
|
|
console.log(JSON.stringify({ ok: true, patchId, artifact, checksum, sha256, files }, null, 2));
|
|
} finally {
|
|
await rm(stage, { recursive: true, force: true });
|
|
}
|
|
|
|
async function assertConsumerPolicyBoundary() {
|
|
const registry = JSON.parse(await readFile(resolve(foundryRoot, files[0]), "utf8"));
|
|
if (registry?.schemaVersion !== "nodedc.foundry.data-product-consumer-policies/v1") {
|
|
throw new Error("foundry_consumer_policy_registry_invalid");
|
|
}
|
|
const matches = registry.policies?.filter((policy) => (
|
|
policy?.dataProductId === "fleet.positions.current.v4" && policy?.productVersion === "4.0.0"
|
|
)) || [];
|
|
if (matches.length !== 1) throw new Error("foundry_consumer_policy_v4_missing_or_ambiguous");
|
|
const policy = matches[0];
|
|
if (
|
|
policy.id !== "map-moving-object-current-v4"
|
|
|| policy.version !== "4.0.0"
|
|
|| policy.staleAfterMs !== null
|
|
|| policy.statusContract?.attribute !== "signal_state"
|
|
|| JSON.stringify(policy.statusContract?.allowedValues) !== JSON.stringify(["active", "inactive"])
|
|
|| policy.statusContract?.missing !== "reject"
|
|
|| policy.statusContract?.freshness !== "none"
|
|
|| JSON.stringify(policy.terminalStatuses) !== JSON.stringify(["inactive"])
|
|
|| policy.removeMode !== "canonical-tombstone-or-snapshot-rebase"
|
|
) throw new Error("foundry_consumer_policy_v4_contract_invalid");
|
|
|
|
const serializedPolicy = JSON.stringify(policy);
|
|
if (/(provider|tenant|connection|endpoint|credential|token|secret|authorization)/i.test(serializedPolicy)) {
|
|
throw new Error("foundry_consumer_policy_v4_transport_boundary_violation");
|
|
}
|
|
|
|
const consumer = await readFile(resolve(foundryRoot, files[2]), "utf8");
|
|
for (const marker of [
|
|
"data_product_consumer_fact_status_invalid",
|
|
"data_product_consumer_status_field_not_projected",
|
|
'statusContract.freshness === "none"',
|
|
]) {
|
|
if (!consumer.includes(marker)) throw new Error(`foundry_consumer_policy_v4_runtime_guard_missing:${marker}`);
|
|
}
|
|
}
|
|
|
|
function canonicalTarScript() {
|
|
return [
|
|
"import gzip,io,pathlib,sys,tarfile",
|
|
"root=pathlib.Path(sys.argv[2])",
|
|
"with open(sys.argv[1],'wb') as out:",
|
|
" with gzip.GzipFile(filename='',mode='wb',fileobj=out,compresslevel=9,mtime=0) as gz:",
|
|
" with tarfile.open(fileobj=gz,mode='w',format=tarfile.PAX_FORMAT) as tar:",
|
|
" for top in ('manifest.env','files.txt','payload'):",
|
|
" p=root/top; paths=[p]+(sorted(p.rglob('*')) if p.is_dir() else [])",
|
|
" for x in paths:",
|
|
" info=tar.gettarinfo(str(x),arcname=x.relative_to(root).as_posix())",
|
|
" info.uid=info.gid=0; info.uname=info.gname='root'; info.mtime=0; info.mode=0o755 if info.isdir() else 0o644",
|
|
" with (open(x,'rb') if info.isfile() else io.BytesIO()) as src: tar.addfile(info,src if info.isfile() else None)",
|
|
].join("\n");
|
|
}
|