Files
mtusi2026lab7/evidence/security-scan/vp7-scan-inventory.ts

36 lines
2.6 KiB
TypeScript

import { mkdir, readFile, writeFile } from "node:fs/promises";
import { createHash } from "node:crypto";
const dir = "/mnt/data/home/bench-saves/mtuci-vp7-scan-20261116";
const go = "/home/rey/.local/share/mise/installs/go/1.27.2/bin/go";
const images = ["mtuci-vp7-security-app:local", "haproxy:3.2-alpine", "postgres:18.6", "valkey/valkey:9", "rustfs/rustfs:1.0.0-rc.1", "openbao/openbao:2.7.2", "mtuci-vp7-collector-cache:0.163.2-a65df9129a5b", "mtuci-vp7-openobserve-cache:v1.1.2-26618f379c86", "mtuci-vp7-grafana-cache:13.3.1-1c88285f1dd3"];
async function run(args: string[]) {
const p = Bun.spawn(args, { stdout: "pipe", stderr: "pipe" });
const [out, err, code] = await Promise.all([new Response(p.stdout).text(), new Response(p.stderr).text(), p.exited]);
if (code !== 0) throw new Error(`METADATA_READER_FAILED:${args[0]}:${code}:${err.slice(0, 300)}`);
return out;
}
await mkdir(dir, { recursive: true });
const inventory = [];
for (const ref of images) {
const raw = JSON.parse(await run(["docker", "image", "inspect", ref]));
const x = raw[0];
inventory.push({ ref, id: x.Id, repoDigests: x.RepoDigests, os: x.Os, architecture: x.Architecture, size: x.Size });
}
const appId = inventory[0]!.id;
const appContainer = "f88d434f1aab";
if ((await run(["docker", "inspect", appContainer, "--format", "{{.Image}}"])).trim() !== appId) throw new Error("APP_CONTAINER_IMAGE_MISMATCH");
const binaries = [];
for (const name of ["app", "worker", "s3-control"]) {
const target = `${dir}/${name}`;
await run(["docker", "cp", `${appContainer}:/${name}`, target]);
const bytes = await readFile(target);
const buildInfo = await run([go, "version", "-m", target]);
binaries.push({ name, sha256: createHash("sha256").update(bytes).digest("hex"), bytes: bytes.length, buildInfo });
}
if ((await run(["docker", "inspect", appContainer, "--format", "{{.Image}}"])).trim() !== appId) throw new Error("APP_CONTAINER_IMAGE_CHANGED");
const cache = await run(["docker", "buildx", "du", "--verbose"]);
const builderRecords = cache.split(/\n\n/).filter(x => x.includes("golang:1.27.2-alpine@sha256:f92b6ef800e499660581efdabdf25d9d817a9d124eaf900924f0504e7e27e12d"));
const result = { checkedAt: new Date().toISOString(), inventory, binaries, builderRecords, networkBytes: 0, startedContainers: 0, mutatedSource: false };
await writeFile(`${dir}/INVENTORY.json`, JSON.stringify(result, null, 2)+"\n", { flag: "wx", mode: 0o600 });
console.log(JSON.stringify({ path: `${dir}/INVENTORY.json`, imageCount: inventory.length, binaries: binaries.map(({name,sha256,bytes})=>({name,sha256,bytes})), builderRecordCount: builderRecords.length }));