import { readFile, writeFile } from "node:fs/promises"; import { resolve } from "node:path"; import { root, local, run, compose, requireThat, privateWrite, } from "./runtime"; import { start } from "./lab"; import { auditMatrix } from "../student-tests/matrix"; import { auditBao } from "../student-tests/bao-policy"; import { auditWorkerPrivileges } from "../student-tests/privileges"; import { waitReady } from "../student-tests/wait"; import { postgresTransport } from "../student-tests/postgres-transport"; import { checkSource } from "./check-source"; import { prepareProbePKI } from "../student-tests/probe-pki"; import { type RuntimeMode } from "./runtime-preparation"; import { verifyCheckedRuntime } from "./checked-runtime"; import { createFailoverReader, type ReplicaService } from "./failover-proof"; import { snapshotReplicas } from "./hooks"; import { type GitTrackingSource } from "./git-tracking-proof"; import { sharedLoginRuntime } from "../student-tests/shared-login-runtime"; export async function acceptanceRemainder( password: string, operationId: string, gitSource: GitTrackingSource, setStage: (stage: string) => void, ) { const failedReplicas = new Set(), failoverReceipts: string[] = []; const readFailover = createFailoverReader({ read: async (path) => JSON.parse(await readFile(resolve(root, path), "utf8")), }); const env = { BASE_URL: "https://localhost:18443", CA_FILE: resolve(local, "pki/lb/ca.crt"), VP7_OPERATOR_PASSWORD: password, VP7_ACCEPTANCE_RUN_ID: operationId, PATH: resolve(process.execPath, "..") + ":" + Bun.env.PATH, }; for (let attempt = 0; attempt < 4; attempt++) { setStage("PUBLIC_FAILOVER_" + attempt); const attemptId = crypto.randomUUID(), baseline = await snapshotReplicas(); const failure = await run( [ "make", "test-failover", "ACTION_SCRIPT=" + resolve(root, "scripts/stop-one.sh"), ], { env: { ...env, VP7_FAILOVER_ATTEMPT_ID: attemptId } }, ); await writeFile( resolve(root, "evidence/security/public-failover-" + attempt + ".txt"), failure.out + failure.err, ); const observed = await readFailover(operationId, attemptId, baseline); failedReplicas.add(observed.proof.service); failoverReceipts.push(observed.path); await compose(["up", "-d", "api1", "api2"]); await waitReady(); if (failedReplicas.size === 2) break; } requireThat(failedReplicas.size === 2, "BOTH_REPLICA_FAILURES_NOT_PROVED"); setStage("EXTENDED_ACCEPTANCE"); const extra = await import("../student-tests/extended"); await extra.extendedAcceptance(password, operationId, gitSource); setStage("SHARED_HTTP_LOGIN"); await sharedLoginRuntime(password, operationId); return failoverReceipts; } export async function acceptanceSession( password: string, runtimeMode: RuntimeMode = "canonical", gitSource: GitTrackingSource = { kind: "native-git" }, ) { const operationId = crypto.randomUUID(); let stage = "FULL_STAND_START"; try { const checkedRuntime = runtimeMode === "checked-cache" ? await verifyCheckedRuntime() : undefined; await start(password, runtimeMode); stage = "PUBLIC_STRUCTURE"; const env = { BASE_URL: "https://localhost:18443", CA_FILE: resolve(local, "pki/lb/ca.crt"), VP7_OPERATOR_PASSWORD: password, VP7_ACCEPTANCE_RUN_ID: operationId, PATH: resolve(process.execPath, "..") + ":" + Bun.env.PATH, }; await checkSource(); for (const target of ["test", "test-security"]) { stage = "PUBLIC_" + target.toUpperCase().replaceAll("-", "_"); const r = await run(["make", target], { env }); await writeFile( resolve(root, "evidence/security/public-" + target + ".txt"), r.out + r.err, ); console.log("PASS_" + stage); } stage = "POSTGRES_TRANSPORT"; await prepareProbePKI(password); await postgresTransport(); stage = "TLS_MATRIX"; await auditMatrix(); stage = "BAO_POLICY"; await auditBao(); stage = "WORKER_PRIVILEGES"; await auditWorkerPrivileges(password); stage = "PUBLIC_PERSISTENCE"; const persistence = await run( [ "make", "test-persistence", "ACTION_SCRIPT=" + resolve(root, "scripts/restart.sh"), ], { env }, ); await writeFile( resolve(root, "evidence/security/public-persistence.txt"), persistence.out + persistence.err, ); const failoverReceipts = await acceptanceRemainder( password, operationId, gitSource, (value) => { stage = value; }, ); await checkedRuntime?.verify(); await writeFile( resolve(root, "evidence/security/session.json"), JSON.stringify( { state: "PASS", operation_id: operationId, failover_receipts: failoverReceipts, finished_stage: stage, runtime_mode: runtimeMode, grade_awarded: false, }, null, 2, ) + "\n", ); } catch (error) { await Promise.allSettled([ Promise.resolve().then(() => privateWrite( resolve(local, "diagnostics/acceptance-error.json"), JSON.stringify({ stage, message: error instanceof Error ? error.message : "UNKNOWN", stack: error instanceof Error ? error.stack : "", }), ), ), Promise.resolve().then(() => writeFile( resolve(root, "evidence/security/session.json"), JSON.stringify( { state: "FAILED", stage, details: "private .local/diagnostics/acceptance-error.json", grade_awarded: false, }, null, 2, ) + "\n", ), ), ]); console.error("ACCEPTANCE_STAGE_" + stage); throw error; } }