forked from mtusi2026/mtusi2026lab7
155 lines
4.9 KiB
TypeScript
155 lines
4.9 KiB
TypeScript
import { test, expect } from "bun:test";
|
|
import { mkdtemp, writeFile, readFile, rm } from "node:fs/promises";
|
|
import { resolve } from "node:path";
|
|
import { createHash, randomBytes } from "node:crypto";
|
|
import { root, local, run, requireThat } from "../scripts/runtime";
|
|
import { workerACL, publisherACL } from "../scripts/valkey-acl";
|
|
import { images } from "../scripts/topology";
|
|
test("worker reclaims a real pending entry but cannot flush the broker", async () => {
|
|
const scratch = await mkdtemp(resolve(local, "valkey-reclaim-")),
|
|
name = "vp7-acl-" + randomBytes(6).toString("hex"),
|
|
password = randomBytes(32).toString("hex");
|
|
const acl =
|
|
"user default off\nuser operator on #" +
|
|
createHash("sha256").update(password).digest("hex") +
|
|
" ~* &* +@all\n" +
|
|
workerACL(password) +
|
|
"\n" +
|
|
publisherACL(password) +
|
|
"\n";
|
|
await writeFile(resolve(scratch, "users.acl"), acl, { mode: 0o600 });
|
|
let started = false;
|
|
try {
|
|
await run([
|
|
"docker",
|
|
"run",
|
|
"--detach",
|
|
"--rm",
|
|
"--pull=never",
|
|
"--name",
|
|
name,
|
|
"--label",
|
|
"vp7.owner=" + name,
|
|
"--network",
|
|
"none",
|
|
"--user",
|
|
`${process.getuid?.()}:${process.getgid?.()}`,
|
|
"--read-only",
|
|
"--cap-drop",
|
|
"ALL",
|
|
"--security-opt",
|
|
"no-new-privileges",
|
|
"--pids-limit",
|
|
"64",
|
|
"--memory",
|
|
"128m",
|
|
"--cpus",
|
|
"0.5",
|
|
"--mount",
|
|
`type=bind,source=${scratch},target=/data`,
|
|
images.broker,
|
|
"valkey-server",
|
|
"--port",
|
|
"0",
|
|
"--unixsocket",
|
|
"/data/valkey.sock",
|
|
"--unixsocketperm",
|
|
"600",
|
|
"--aclfile",
|
|
"/data/users.acl",
|
|
"--save",
|
|
"",
|
|
"--appendonly",
|
|
"no",
|
|
]);
|
|
started = true;
|
|
for (let i = 0; i < 60; i++) {
|
|
if (await Bun.file(resolve(scratch, "valkey.sock")).exists()) break;
|
|
await Bun.sleep(50);
|
|
}
|
|
const cli = async (input: string) =>
|
|
run(
|
|
[
|
|
"docker",
|
|
"run",
|
|
"--interactive",
|
|
"--rm",
|
|
"--pull=never",
|
|
"--network",
|
|
"none",
|
|
"--user",
|
|
`${process.getuid?.()}:${process.getgid?.()}`,
|
|
"--mount",
|
|
`type=bind,source=${scratch},target=/data`,
|
|
"--entrypoint",
|
|
"valkey-cli",
|
|
images.broker,
|
|
"--raw",
|
|
"-s",
|
|
"/data/valkey.sock",
|
|
],
|
|
{ input },
|
|
);
|
|
const seeded = await cli(
|
|
`AUTH operator ${password}\nXGROUP CREATE attachment-jobs workers 0 MKSTREAM\nXADD attachment-jobs * job synthetic-reclaim-fixture\nXREADGROUP GROUP workers original COUNT 1 STREAMS attachment-jobs >\n`,
|
|
);
|
|
const entry = seeded.out.match(/\b\d+-\d+\b/)?.[0];
|
|
requireThat(entry, "RECLAIM_POSITIVE_CONTROL_NO_ENTRY");
|
|
const reclaimed = await cli(
|
|
`AUTH worker ${password}\nXAUTOCLAIM attachment-jobs workers restarted 0 0-0 COUNT 1\nXPENDING attachment-jobs workers - + 1\n`,
|
|
);
|
|
const reclaimOutput = reclaimed.out + reclaimed.err;
|
|
await writeFile(
|
|
resolve(
|
|
root,
|
|
"evidence/security/valkey-reclaim-split-" +
|
|
crypto.randomUUID() +
|
|
".json",
|
|
),
|
|
JSON.stringify(
|
|
{
|
|
server: "Valkey9.1.2 existing image",
|
|
real_pending_entry_seeded: !!entry,
|
|
reclaim_denied: reclaimOutput.includes("NOPERM"),
|
|
pending_entry_returned: reclaimOutput.includes(entry),
|
|
new_owner_confirmed: reclaimOutput.includes("restarted"),
|
|
},
|
|
null,
|
|
2,
|
|
) + "\n",
|
|
{ flag: "wx" },
|
|
);
|
|
expect(reclaimOutput.includes("NOPERM")).toBe(false);
|
|
expect(reclaimOutput.includes(entry)).toBe(true);
|
|
expect(reclaimOutput.includes("restarted")).toBe(true);
|
|
const denied = await cli(`AUTH worker ${password}\nFLUSHALL\n`);
|
|
expect((denied.out + denied.err).includes("NOPERM")).toBe(true);
|
|
const consumerPublish = await cli(
|
|
`AUTH worker ${password}\nXADD attachment-jobs * job forbidden-consumer-publish\n`,
|
|
);
|
|
expect((consumerPublish.out + consumerPublish.err).includes("NOPERM")).toBe(
|
|
true,
|
|
);
|
|
const published = await cli(
|
|
`AUTH publisher ${password}\nXADD attachment-jobs * job allowed-publisher-fixture\n`,
|
|
);
|
|
expect(published.out.match(/\b\d+-\d+\b/)).toBeTruthy();
|
|
const producerConsume = await cli(
|
|
`AUTH publisher ${password}\nXREADGROUP GROUP workers forbidden COUNT 1 STREAMS attachment-jobs >\n`,
|
|
);
|
|
expect((producerConsume.out + producerConsume.err).includes("NOPERM")).toBe(
|
|
true,
|
|
);
|
|
} finally {
|
|
if (started) {
|
|
const state = JSON.parse((await run(["docker", "inspect", name])).out)[0];
|
|
requireThat(
|
|
state.Config.Labels["vp7.owner"] === name,
|
|
"RECLAIM_CONTAINER_OWNERSHIP",
|
|
);
|
|
await run(["docker", "stop", "--time", "3", name]);
|
|
}
|
|
await rm(scratch, { recursive: true, force: true });
|
|
}
|
|
}, 30_000);
|