forked from mtusi2026/mtusi2026lab7
Prepare verified laboratory 7 implementation and evidence
This commit is contained in:
@@ -0,0 +1,123 @@
|
||||
import { readFile, writeFile } from "node:fs/promises";
|
||||
import { resolve } from "node:path";
|
||||
import { root, local, compose, run, requireThat, safeFailure } from "./runtime";
|
||||
import { containerID } from "./runtime-inspect";
|
||||
import { operatorPassword } from "./operator-crypto";
|
||||
import { unseal } from "./openbao-operator";
|
||||
import { waitReady } from "../student-tests/wait";
|
||||
import { createReplicaSnapshot } from "./replica-snapshot";
|
||||
import {
|
||||
failoverReceiptPath,
|
||||
type ReplicaBaseline,
|
||||
type ReplicaService,
|
||||
} from "./failover-proof";
|
||||
async function snapshot(services: string[]) {
|
||||
const result = [];
|
||||
for (const service of services) {
|
||||
const id = await containerID(service),
|
||||
info = JSON.parse((await run(["docker", "inspect", id])).out)[0];
|
||||
result.push({ service, id, started_at: info.State.StartedAt });
|
||||
}
|
||||
return result;
|
||||
}
|
||||
export async function restart(password: string) {
|
||||
const services = [
|
||||
"postgres",
|
||||
"broker",
|
||||
"s3",
|
||||
"openbao",
|
||||
"collector",
|
||||
"openobserve",
|
||||
"grafana",
|
||||
"api1-agent",
|
||||
"api2-agent",
|
||||
"worker-agent",
|
||||
"worker",
|
||||
"api1",
|
||||
"api2",
|
||||
"lb",
|
||||
];
|
||||
const before = await snapshot(services);
|
||||
await compose(["restart", ...services]);
|
||||
await unseal(password);
|
||||
await waitReady();
|
||||
const after = await snapshot(services);
|
||||
for (const old of before)
|
||||
requireThat(
|
||||
after.find((x) => x.service === old.service)?.started_at !==
|
||||
old.started_at,
|
||||
"RESTART_MISSING_" + old.service.toUpperCase(),
|
||||
);
|
||||
await writeFile(
|
||||
resolve(root, "evidence/security/persistence-processes.json"),
|
||||
JSON.stringify({ before, after, volumes_removed: false }, null, 2) + "\n",
|
||||
);
|
||||
console.log("PASS_REAL_STORES_AND_APPLICATION_RESTARTED");
|
||||
}
|
||||
export async function snapshotReplicas() {
|
||||
return createReplicaSnapshot({
|
||||
inspect: async (service) => {
|
||||
const id = await containerID(service);
|
||||
const info = JSON.parse((await run(["docker", "inspect", id])).out)[0];
|
||||
return {
|
||||
id: info.Id,
|
||||
running: info.State.Running,
|
||||
project: info.Config.Labels["com.docker.compose.project"],
|
||||
service: info.Config.Labels["com.docker.compose.service"],
|
||||
started: info.State.StartedAt,
|
||||
};
|
||||
},
|
||||
command: async (args) => (await run(args)).out,
|
||||
})();
|
||||
}
|
||||
export async function stopOne() {
|
||||
const target = Bun.env.TARGET_INSTANCE_ID;
|
||||
requireThat(
|
||||
typeof target === "string" && /^[a-f0-9-]{36}$/.test(target),
|
||||
"INVALID_TARGET_INSTANCE_ID",
|
||||
);
|
||||
const operationId = Bun.env.VP7_ACCEPTANCE_RUN_ID ?? crypto.randomUUID();
|
||||
const attemptId = Bun.env.VP7_FAILOVER_ATTEMPT_ID ?? crypto.randomUUID();
|
||||
const receiptPath = failoverReceiptPath(operationId, attemptId);
|
||||
const baseline = await snapshotReplicas();
|
||||
const matches = (Object.keys(baseline) as ReplicaService[]).filter(
|
||||
(service) => baseline[service].alias === target,
|
||||
);
|
||||
requireThat(matches.length === 1, "TARGET_REPLICA_NOT_UNIQUE");
|
||||
const id = baseline[matches[0]].container;
|
||||
// Stop the immutable, ownership-verified container that supplied this alias.
|
||||
await run(["docker", "stop", "--timeout", "15", id]);
|
||||
const info = JSON.parse((await run(["docker", "inspect", id])).out)[0];
|
||||
requireThat(
|
||||
info.Id === id && info.State.Running === false,
|
||||
"TARGET_STILL_RUNNING",
|
||||
);
|
||||
await writeFile(
|
||||
resolve(root, receiptPath),
|
||||
JSON.stringify(
|
||||
{
|
||||
operation_id: operationId,
|
||||
attempt_id: attemptId,
|
||||
target_alias: target,
|
||||
service: matches[0],
|
||||
container: id,
|
||||
before_running: true,
|
||||
running: false,
|
||||
volumes_removed: false,
|
||||
},
|
||||
null,
|
||||
2,
|
||||
) + "\n",
|
||||
{ flag: "wx", mode: 0o644 },
|
||||
);
|
||||
console.log("PASS_EXACT_TARGET_API_STOPPED");
|
||||
}
|
||||
if (import.meta.main) {
|
||||
try {
|
||||
if (Bun.argv[2] === "restart") await restart(await operatorPassword());
|
||||
else if (Bun.argv[2] === "stop-one") await stopOne();
|
||||
else throw new Error("INVALID_HOOK_ACTION");
|
||||
} catch (e) {
|
||||
safeFailure(e);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user