forked from mtusi2026/mtusi2026lab7
163 lines
4.9 KiB
TypeScript
163 lines
4.9 KiB
TypeScript
import { createHash } from "node:crypto";
|
|
import { readFile, mkdir, writeFile } from "node:fs/promises";
|
|
import { resolve } from "node:path";
|
|
import { root, local, run, requireThat, safeFailure } from "./runtime";
|
|
import { pinHandover } from "./checkpoint-guards";
|
|
export async function verifyBackendHandover() {
|
|
return pinHandover({
|
|
readReceipt: () => readFile(resolve(root, "BACKEND-READY.json")),
|
|
readSource: (path) => readFile(resolve(root, "backend", path)),
|
|
});
|
|
}
|
|
export async function preflightBuild() {
|
|
const lease = await verifyBackendHandover();
|
|
const { count, receipt } = lease;
|
|
const helperHash = createHash("sha256")
|
|
.update(await readFile(resolve(root, "scripts/s3-control.go")))
|
|
.digest("hex");
|
|
await mkdir(resolve(local, "bin"), { recursive: true, mode: 0o700 });
|
|
await mkdir(resolve(root, ".local-build-cache"), {
|
|
recursive: true,
|
|
mode: 0o700,
|
|
});
|
|
await writeFile(
|
|
resolve(root, "evidence/security/preflight-source-handover.json"),
|
|
lease.bytes,
|
|
);
|
|
const frozen: any = {};
|
|
for (const [name, source] of [["runtime", "mtuci-vp5:local"]]) {
|
|
const image = JSON.parse(
|
|
(await run(["docker", "image", "inspect", source])).out,
|
|
)[0];
|
|
requireThat(
|
|
/^sha256:[a-f0-9]{64}$/.test(image.Id),
|
|
"INVALID_CACHED_IMAGE_ID",
|
|
);
|
|
const alias = "mtuci-vp7-preflight-" + name + ":" + image.Id.slice(7, 23);
|
|
await run(["docker", "image", "tag", image.Id, alias]);
|
|
const check = JSON.parse(
|
|
(await run(["docker", "image", "inspect", alias])).out,
|
|
)[0];
|
|
requireThat(check.Id === image.Id, "CACHED_IMAGE_FREEZE_MISMATCH");
|
|
frozen[name] = { source, id: image.Id, alias };
|
|
}
|
|
const compiler = "/home/rey/.local/share/mise/installs/go/1.27.2/bin/go";
|
|
const env = {
|
|
GOTOOLCHAIN: "local",
|
|
GOPROXY: "off",
|
|
GOSUMDB: "off",
|
|
GOFLAGS: "-mod=readonly",
|
|
CGO_ENABLED: "0",
|
|
GOMODCACHE: "/home/rey/go/pkg/mod",
|
|
GOCACHE: resolve(root, ".local-build-cache"),
|
|
GOMAXPROCS: "2",
|
|
};
|
|
const version = await run([compiler, "version"], { env });
|
|
requireThat(
|
|
version.out.trim() === "go version go1.27.2 linux/amd64",
|
|
"UNEXPECTED_PREFLIGHT_COMPILER",
|
|
);
|
|
for (const [name, source] of [
|
|
["app", "./cmd/app"],
|
|
["worker", "./cmd/worker"],
|
|
["s3-control", resolve(root, "scripts/s3-control.go")],
|
|
])
|
|
await run(
|
|
[
|
|
compiler,
|
|
"build",
|
|
"-trimpath",
|
|
"-o",
|
|
resolve(local, "bin", name),
|
|
source,
|
|
],
|
|
{ env, cwd: resolve(root, "backend") },
|
|
);
|
|
await lease.verify();
|
|
requireThat(
|
|
createHash("sha256")
|
|
.update(await readFile(resolve(root, "scripts/s3-control.go")))
|
|
.digest("hex") === helperHash,
|
|
"HELPER_SOURCE_CHANGED",
|
|
);
|
|
const binaries: any = {};
|
|
for (const name of ["app", "worker", "s3-control"])
|
|
binaries[name] = createHash("sha256")
|
|
.update(await readFile(resolve(local, "bin", name)))
|
|
.digest("hex");
|
|
const dockerfile = resolve(local, "Dockerfile.preflight");
|
|
await writeFile(
|
|
dockerfile,
|
|
`FROM ${frozen.runtime.alias}\nCOPY app /app\nCOPY worker /worker\nCOPY s3-control /s3-control\nUSER 10001:10001\nENTRYPOINT []\nCMD ["/app"]\n`,
|
|
);
|
|
// The build context is exactly the three verified public-source binaries, never the .local parent or credentials.
|
|
await run([
|
|
"docker",
|
|
"build",
|
|
"--pull=false",
|
|
"--network",
|
|
"none",
|
|
"--file",
|
|
dockerfile,
|
|
"--tag",
|
|
"mtuci-vp7-security-app:local",
|
|
resolve(local, "bin"),
|
|
]);
|
|
const installed = await run([
|
|
"docker",
|
|
"run",
|
|
"--pull=never",
|
|
"--rm",
|
|
"--network",
|
|
"none",
|
|
"--entrypoint",
|
|
"/usr/bin/sha256sum",
|
|
"mtuci-vp7-security-app:local",
|
|
"/app",
|
|
"/worker",
|
|
"/s3-control",
|
|
]);
|
|
for (const name of ["app", "worker", "s3-control"])
|
|
requireThat(
|
|
installed.out.includes(binaries[name] + " /" + name),
|
|
"INSTALLED_PREFLIGHT_BINARY_HASH_MISMATCH",
|
|
);
|
|
await lease.verify();
|
|
requireThat(
|
|
createHash("sha256")
|
|
.update(await readFile(resolve(root, "scripts/s3-control.go")))
|
|
.digest("hex") === helperHash,
|
|
"HELPER_SOURCE_CHANGED",
|
|
);
|
|
await writeFile(
|
|
resolve(root, "evidence/security/preflight-build.json"),
|
|
JSON.stringify(
|
|
{
|
|
scope:
|
|
"checked7 source, cached VP5 runtime only; no Bao/telemetry acceptance",
|
|
module_fetch:
|
|
"GOPROXY=off GOSUMDB=off GOFLAGS=-mod=readonly; existing native cache",
|
|
image_build_network: "none",
|
|
source_files_verified: count,
|
|
backend_checked_at: receipt.checkedAt,
|
|
source_handover_sha256: lease.sha256,
|
|
s3_control_source_sha256: helperHash,
|
|
images: frozen,
|
|
compiler: version.out.trim(),
|
|
binaries,
|
|
},
|
|
null,
|
|
2,
|
|
) + "\n",
|
|
);
|
|
console.log("PASS_EXACT7_PREFLIGHT_BINARIES_BUILT_OFFLINE_WITH_FROZEN_CACHE");
|
|
return frozen.runtime.alias as string;
|
|
}
|
|
if (import.meta.main) {
|
|
try {
|
|
await preflightBuild();
|
|
} catch (e) {
|
|
safeFailure(e);
|
|
}
|
|
}
|