forked from mtusi2026/mtusi2026lab7
Prepare verified laboratory 7 implementation and evidence
This commit is contained in:
@@ -0,0 +1,97 @@
|
||||
import { mkdir, chmod, writeFile } from "node:fs/promises";
|
||||
import { mkdirSync, writeFileSync } from "node:fs";
|
||||
import { resolve } from "node:path";
|
||||
import { createCommandRunner } from "./command-runner";
|
||||
export const root = resolve(import.meta.dir, "..");
|
||||
export const local = resolve(root, ".local");
|
||||
export const project = "mtuci-vp7-security";
|
||||
export function requireThat(value: unknown, code: string): asserts value {
|
||||
if (!value) throw new Error(code);
|
||||
}
|
||||
export async function privateWrite(path: string, content: string | Uint8Array) {
|
||||
await mkdir(resolve(path, ".."), { recursive: true, mode: 0o700 });
|
||||
await writeFile(path, content, { mode: 0o600 });
|
||||
await chmod(path, 0o600);
|
||||
}
|
||||
export const run = createCommandRunner({
|
||||
execute: async (args, options) => {
|
||||
const process = Bun.spawn(args, {
|
||||
cwd: options.cwd ?? root,
|
||||
env: { ...Bun.env, ...options.env },
|
||||
stdin: options.input === undefined ? "ignore" : "pipe",
|
||||
stdout: "pipe",
|
||||
stderr: "pipe",
|
||||
});
|
||||
if (
|
||||
options.input !== undefined &&
|
||||
process.stdin &&
|
||||
typeof process.stdin !== "number"
|
||||
) {
|
||||
process.stdin.write(options.input);
|
||||
process.stdin.end();
|
||||
}
|
||||
const [out, err, code] = await Promise.all([
|
||||
new Response(process.stdout).text(),
|
||||
new Response(process.stderr).text(),
|
||||
process.exited,
|
||||
]);
|
||||
return { out, err, code };
|
||||
},
|
||||
unique: async (record) => {
|
||||
await mkdir(resolve(local, "diagnostics"), {
|
||||
recursive: true,
|
||||
mode: 0o700,
|
||||
});
|
||||
await writeFile(
|
||||
resolve(local, "diagnostics", "command-" + crypto.randomUUID() + ".json"),
|
||||
JSON.stringify(record),
|
||||
{ mode: 0o600, flag: "wx" },
|
||||
);
|
||||
},
|
||||
last: (record) =>
|
||||
privateWrite(
|
||||
resolve(local, "diagnostics", "last-command.json"),
|
||||
JSON.stringify(record),
|
||||
),
|
||||
});
|
||||
export function composeArgs(args: string[]) {
|
||||
return [
|
||||
"docker",
|
||||
"compose",
|
||||
"--project-directory",
|
||||
root,
|
||||
"--env-file",
|
||||
resolve(root, ".env"),
|
||||
"-f",
|
||||
resolve(root, "compose.yaml"),
|
||||
"-p",
|
||||
project,
|
||||
...args,
|
||||
];
|
||||
}
|
||||
export async function compose(
|
||||
args: string[],
|
||||
options: Parameters<typeof run>[1] = {},
|
||||
) {
|
||||
return run(composeArgs(args), options);
|
||||
}
|
||||
export function safeFailure(error: unknown) {
|
||||
const code = error instanceof Error ? error.message : "";
|
||||
try {
|
||||
mkdirSync(resolve(local, "diagnostics"), { recursive: true, mode: 0o700 });
|
||||
writeFileSync(
|
||||
resolve(local, "diagnostics", "last-error.json"),
|
||||
JSON.stringify({
|
||||
message: code,
|
||||
stack: error instanceof Error ? error.stack : "",
|
||||
}),
|
||||
{ mode: 0o600 },
|
||||
);
|
||||
} catch {}
|
||||
console.error(
|
||||
/^[A-Z][A-Za-z0-9_:-]{0,240}$/.test(code)
|
||||
? code
|
||||
: "OPERATION_FAILED_DETAILS_PRIVATE",
|
||||
);
|
||||
process.exitCode = 1;
|
||||
}
|
||||
Reference in New Issue
Block a user