forked from mtusi2026/mtusi2026lab7
99 lines
3.3 KiB
TypeScript
99 lines
3.3 KiB
TypeScript
import { readFile, readdir, writeFile } from "node:fs/promises";
|
|
import { resolve } from "node:path";
|
|
import { root, local, run, compose, requireThat, safeFailure } from "./runtime";
|
|
import { preflightBuild, verifyBackendHandover } from "./preflight-build";
|
|
import { provisionS3 } from "./s3-provision";
|
|
import {
|
|
assertAcceptedGeneration,
|
|
createEvidencePreserver,
|
|
} from "./checkpoint-guards";
|
|
try {
|
|
const summary = JSON.parse(
|
|
await readFile(resolve(root, "evidence/preflight/summary.json"), "utf8"),
|
|
);
|
|
requireThat(summary.state === "PASS", "CACHED_PREFLIGHT_NOT_ACCEPTED");
|
|
const accepted = summary.accepted_generation;
|
|
requireThat(
|
|
accepted && typeof accepted.source_handover_text === "string",
|
|
"ACCEPTED_GENERATION_MISMATCH",
|
|
);
|
|
const sourceBytes = Buffer.from(accepted.source_handover_text),
|
|
build = accepted.build_receipt;
|
|
assertAcceptedGeneration(accepted, sourceBytes, build);
|
|
const lease = await verifyBackendHandover();
|
|
requireThat(
|
|
lease.sha256 === accepted.source_handover_sha256,
|
|
"ACCEPTED_GENERATION_MISMATCH",
|
|
);
|
|
const preserve = createEvidencePreserver({
|
|
writeExclusive: (path, bytes) =>
|
|
writeFile(path, bytes, { flag: "wx", mode: 0o600 }),
|
|
read: (path) => readFile(path),
|
|
});
|
|
const prefix = "checked" + accepted.source_files_verified;
|
|
await preserve(
|
|
resolve(root, "evidence/preflight", prefix + "-source.json"),
|
|
sourceBytes,
|
|
);
|
|
await preserve(
|
|
resolve(root, "evidence/preflight", prefix + "-build.json"),
|
|
Buffer.from(JSON.stringify(build, null, 2) + "\n"),
|
|
);
|
|
await lease.verify();
|
|
await preflightBuild();
|
|
const password = await readFile(
|
|
resolve(root, ".local-preflight-key"),
|
|
"utf8",
|
|
);
|
|
requireThat(/^[a-f0-9]{64}$/.test(password), "INVALID_PREFLIGHT_SESSION_KEY");
|
|
const receipts = resolve(local, "operator/s3-receipts"),
|
|
before = new Set(await readdir(receipts));
|
|
await provisionS3(password);
|
|
let completed = 0;
|
|
for (const name of await readdir(receipts)) {
|
|
if (before.has(name)) continue;
|
|
const value = JSON.parse(await readFile(resolve(receipts, name), "utf8"));
|
|
if (value.stage === "bootstrap" && value.state === "COMPLETE") completed++;
|
|
}
|
|
requireThat(completed === 1, "S3_BOOTSTRAP_RECEIPT_NOT_COMPLETED");
|
|
const ids = (await compose(["ps", "-a", "-q"])).out
|
|
.trim()
|
|
.split(/\s+/)
|
|
.filter(Boolean);
|
|
for (const id of ids) {
|
|
const value = JSON.parse((await run(["docker", "inspect", id])).out)[0];
|
|
requireThat(
|
|
value.Config.Labels["com.docker.compose.project"] ===
|
|
"mtuci-vp7-security",
|
|
"FINAL_STOP_PROJECT_MISMATCH",
|
|
);
|
|
}
|
|
await compose(["--profile", "*", "down", "--remove-orphans"]);
|
|
const remaining = await run([
|
|
"docker",
|
|
"ps",
|
|
"-a",
|
|
"-q",
|
|
"--filter",
|
|
"label=com.docker.compose.project=mtuci-vp7-security",
|
|
]);
|
|
requireThat(!remaining.out.trim(), "OWNED_CONTAINERS_REMAIN");
|
|
await writeFile(
|
|
resolve(root, "evidence/preflight/finalization.json"),
|
|
JSON.stringify(
|
|
{
|
|
new_sdk_bootstrap_receipt_complete: true,
|
|
owned_containers_remaining: 0,
|
|
bind_data_retained: true,
|
|
private_test_key_retained_for_addressed_cleanup: true,
|
|
native_bao_accepted: false,
|
|
},
|
|
null,
|
|
2,
|
|
) + "\n",
|
|
);
|
|
console.log("PASS_UPDATED_SDK_RECEIPT_AND_OWNED_STAND_TEARDOWN");
|
|
} catch (error) {
|
|
safeFailure(error);
|
|
}
|