import { readFile, writeFile, mkdir, chmod } from "node:fs/promises"; import { resolve } from "node:path"; import { createHash } from "node:crypto"; import { root, local, run, requireThat, safeFailure } from "./runtime"; import { createEvidencePreserver } from "./checkpoint-guards"; import { verifyBackendHandover } from "./preflight-build"; const sourceRoot = "/mnt/data/home/bench-saves/mtuci-vp-remaining-20261115/lab6/.local/grafana-approved-31251471-ca6a-4d2e-b823-8e42ff9fefa5"; const pluginRoot = "/mnt/data/home/bench-saves/mtuci-vp-remaining-20261115/lab6/.local/grafana-plugins/openobserve"; const imageID = "sha256:1c88285f1dd391a62232c010e6c224846e6a73bdc583b3365be2eba614b7919e"; const ownAlias = "mtuci-vp7-grafana-cache:13.3.1-1c88285f1dd3"; const hash = (bytes: Uint8Array) => createHash("sha256").update(bytes).digest("hex"); async function main() { requireThat( root === "/mnt/data/home/bench-saves/mtuci-vp7-security-20261115", "GRAFANA_CACHE_REMOTE_BOUNDARY_REQUIRED", ); const lease = await verifyBackendHandover(); const readyPath = resolve(sourceRoot, "CACHE-READY.json"), readyBytes = await readFile(readyPath), ready = JSON.parse(readyBytes.toString()); requireThat( ready.imported === true && ready.imageID === imageID && ready.platform === "linux/amd64" && ready.nativeVersionExit === 0 && ready.nativeVersionOutput === "grafana version 13.3.1" && ready.pluginId === "openobserve" && ready.pluginVersion === "1.0.1" && ready.pluginRequiresGrafana === ">=12.4.0" && ready.upstreamUnmodified === true, "GRAFANA_CACHE_HANDOVER_MISMATCH", ); const pluginBytes = await readFile( resolve(sourceRoot, "openobserve-1.0.1.zip"), ); requireThat( hash(pluginBytes) === "e0c2cbcc7cf6d05bcfaa606db39e52249eb0dd234376e45a3f896353831c2fd5" && ready.pluginArchiveSHA256 === hash(pluginBytes), "GRAFANA_PLUGIN_ARCHIVE_HASH_MISMATCH", ); const image = JSON.parse( (await run(["docker", "image", "inspect", ready.alias])).out, )[0]; requireThat( image.Id === imageID && image.Os === "linux" && image.Architecture === "amd64", "GRAFANA_CACHE_IMAGE_MISMATCH", ); const existing = await run(["docker", "image", "inspect", ownAlias], { allowFailure: true, }); if (existing.code === 0) requireThat( JSON.parse(existing.out)[0].Id === imageID, "GRAFANA_OWN_ALIAS_CONFLICT", ); else await run(["docker", "image", "tag", imageID, ownAlias]); const preserve = createEvidencePreserver({ read: readFile, writeExclusive: (path, bytes) => writeFile(path, bytes, { flag: "wx", mode: 0o644 }), }); const files = [ "plugin.json", "module.js", "module.js.map", "gpx_openobserve_linux_amd64", "LICENSE", "README.md", "CHANGELOG.md", "go_plugin_build_manifest", "img/logo.png", ]; const copied: Record = {}; for (const path of files) { const bytes = await readFile(resolve(pluginRoot, path)); if (ready.filesSHA256[path]) requireThat( hash(bytes) === ready.filesSHA256[path], "GRAFANA_PLUGIN_SOURCE_DRIFT", ); if (path === "gpx_openobserve_linux_amd64") requireThat( hash(bytes) === "dc91fdfaf25708cb2573c4e533754cb5cc5ecd02d43beb7ee21bee06fbc09c50", "GRAFANA_PLUGIN_BACKEND_HASH_MISMATCH", ); const target = resolve(local, "plugins/openobserve", path); await mkdir(resolve(target, ".."), { recursive: true, mode: 0o755 }); await preserve(target, bytes); if (path === "gpx_openobserve_linux_amd64") await chmod(target, 0o755); copied[path] = hash(await readFile(target)); requireThat(copied[path] === hash(bytes), "GRAFANA_PLUGIN_COPY_DRIFT"); } requireThat( hash(await readFile(readyPath)) === hash(readyBytes), "GRAFANA_CACHE_HANDOVER_CHANGED", ); requireThat( JSON.parse((await run(["docker", "image", "inspect", ownAlias])).out)[0] .Id === imageID, "GRAFANA_OWN_ALIAS_POSTGUARD_FAILED", ); await lease.verify(); const receipt = resolve( root, "evidence/grafana/cache-freeze-" + crypto.randomUUID() + ".json", ); await mkdir(resolve(receipt, ".."), { recursive: true }); await writeFile( receipt, JSON.stringify( { state: "PASS_OWN_GRAFANA_CACHE_NO_PULL", source_receipt: { path: readyPath, sha256: hash(readyBytes) }, image_id: imageID, own_alias: ownAlias, platform: "linux/amd64", version: "13.3.1", plugin_archive_sha256: hash(pluginBytes), plugin_id: "openobserve", plugin_version: "1.0.1", copied_files: copied, upstream_modified: false, source_cache_mutated: false, new_network_bytes: 0, backend_handover_sha256: lease.sha256, }, null, 2, ) + "\n", { flag: "wx", mode: 0o600 }, ); console.log("PASS_OWN_GRAFANA_ALIAS_AND_PLUGIN_NO_SECOND_DOWNLOAD"); } if (import.meta.main) { try { await main(); } catch (error) { safeFailure(error); } }