/** * The whole grant for an S3 bucket, mesh-driven — novox/hq ADR 0052/0053, the minio case. * * The postgres bed proves the provider/consumer contract for a database. This proves it for object * storage, on a provider whose code drives the `mc` CLI (so the runtime image carries it): minio is * assigned, a consumer that requires s3-bucket is assigned, and the mesh mints one secret key, sealing * a copy to each end. minio's provisioner — reading only the mesh's contributions — creates a bucket * and a service account under the access key the mesh derived, with the secret it minted. The proof is * the consumer reaching its bucket with the access key and secret the mesh delivered it. Nothing is * placed by the test. * * MESH_LAB_HOST_BINARY=.../mesh-host MESH_LAB_BUNDLE=.../examples/substrate-first-node.lock * scripts/build-module-runtime.sh minio builds mesh-runtime-minio:development (with mc), which * scenarios/minio-node.yml stocks. minio/minio:latest must be in the local daemon. */ import { test, before, after } from "node:test"; import assert from "node:assert/strict"; import { existsSync, readFileSync } from "node:fs"; import { loadScenario } from "../../src/declaration/parse.ts"; import { raise } from "../../src/lifecycle/raise.ts"; import { destroy, exec } from "../../src/lifecycle/operate.ts"; import { hostBinaryPath, HOST_PATH } from "../../src/lifecycle/place.ts"; import { labIsUsable, destroyAll, substrateBundle, onTheMachine } from "./harness.ts"; import type { HeldImage } from "../../src/pinning.ts"; const capability = await labIsUsable(); const binary = hostBinaryPath(); const bundle = process.env["MESH_LAB_BUNDLE"] ?? ""; // 04-ISSUES/010 is fixed by ADR 0054: an S3 access key is capped at 20, and the mesh derives // `mesh__`, so `bucketuser` on `anchor` (22) would overflow — but a consumer declares a // short `slug` and its identity fits. This bed's consumer does exactly that. const skip = !capability.usable ? `lab not usable: ${capability.why}` : !binary || !existsSync(binary) ? "MESH_LAB_HOST_BINARY is not set to a built mesh-host" : !bundle || !existsSync(bundle) ? "MESH_LAB_BUNDLE is not set to a substrate bundle (mesh-host examples/)" : false; const SCENARIO = "minio-node"; const MACHINE = "anchor"; let instanceId = ""; let held: HeldImage[] = []; function quote(s: string): string { return `'${s.replaceAll("'", `'\\''`)}'`; } async function on(command: string, timeoutMs?: number): Promise<{ out: string; ok: boolean }> { const { stdout } = await exec(instanceId, MACHINE, [ "sh", "-c", `exec 2>&1\n${command}\necho "__exit=$?"`, ], timeoutMs); const marker = stdout.lastIndexOf("__exit="); if (marker < 0) return { out: stdout, ok: false }; return { out: stdout.slice(0, marker), ok: stdout.slice(marker + 7).trim() === "0" }; } async function must(command: string, timeoutMs?: number): Promise { const { out, ok } = await on(command, timeoutMs); if (!ok) throw new Error(`${MACHINE}: ${command}\n${out}`); return out; } async function mesh(command: string, timeoutMs?: number): Promise { return must(`docker exec mesh-control /mesh-control ${command}`, timeoutMs); } /** What a manifest's image reference becomes on the machine — ours by ID, everything else as written. */ function pinned(reference: string): string { return onTheMachine(reference, held); } function bundleFor(images: HeldImage[]): string { return substrateBundle(bundle, images); } function tokenFrom(said: string): string { const found = said.split("\n").map((l) => l.trim()).find((l) => l.length > 100 && !l.includes(" ")); assert.ok(found, `no token in:\n${said}`); return found; } /** Same rule minio's client uses to name a bucket for a consumer — recomputed so the test knows it. */ function bucketFor(as: string): string { const name = as.toLowerCase().replace(/[^a-z0-9-]+/g, "-").replace(/^-+|-+$/g, "").slice(0, 63); return name.length >= 3 ? name : `mesh-${name}`; } async function settled(withinMs = 480_000): Promise { const until = Date.now() + withinMs; let last = ""; while (Date.now() < until) { const asked = await on(`docker exec mesh-control /mesh-control status --json`); if (asked.ok) { try { const state = JSON.parse(asked.out) as { wrong: { node: string; outcome: string }[]; waiting: { node: string }[]; reported: { node: string; outcome: string; current: boolean }[]; }; const bad = state.wrong.find((w) => w.node === MACHINE); if (bad) throw new Error(`${MACHINE} did not apply what it was sent: ${bad.outcome}\n${asked.out}`); const word = state.reported.find((r) => r.node === MACHINE); if (!state.waiting.some((w) => w.node === MACHINE) && word?.outcome === "applied" && word.current) return; last = asked.out; } catch (err) { if (err instanceof Error && err.message.includes("did not apply")) throw err; last = asked.out; } } await new Promise((r) => setTimeout(r, 5000)); } throw new Error(`${MACHINE} never caught up within ${Math.round(withinMs / 1000)}s. Last:\n${last}`); } before(async () => { if (skip) return; const raised = await raise(loadScenario(`scenarios/${SCENARIO}.yml`), { onProgress: (m) => console.log(`raise: ${m}`), }); instanceId = raised.instanceId; held = raised.images; await must(`cat > /tmp/substrate.lock <<'MESHBUNDLE'\n${bundleFor(raised.images)}\nMESHBUNDLE`); await must(`${HOST_PATH} apply /tmp/substrate.lock`, 600_000); const up = await must(`docker ps --format '{{.Names}}'`); for (const c of ["mesh-store", "mesh-broker", "mesh-control"]) { assert.match(up, new RegExp(c), `the substrate did not raise ${c}:\n${up}`); } await mesh(`node add ${MACHINE}`); const token = tokenFrom(await mesh(`token issue --node ${MACHINE}`)); await must(`${HOST_PATH} enrol --token ${quote(token)}`); await must(`nohup ${HOST_PATH} run > /var/log/mesh-host.log 2>&1 & sleep 3`); }, { timeout: 1_800_000 }); after(async () => { if (instanceId) await destroy(instanceId); await destroyAll(`${SCENARIO}-`); }, { timeout: 600_000 }); test("the mesh grants a consumer an S3 bucket, and the credential it delivers reaches it", { skip, timeout: 900_000, }, async () => { // The PROVIDER: minio in its committed shape — server and a broker-bound runtime (carrying mc) on // the private minio network, the runtime running the provisioner. No published port on this // single-node bed; the consumer reaches minio over the private network by name. const minioManifest = JSON.stringify({ module: "minio", version: "1", provides: [{ name: "s3-bucket", scope: "mesh" }], serves: { "s3-bucket": { scheme: "http", region: "us-east-1" } }, emits: ["module.minio.bucket.created", "module.minio.bucket.removed"], receives: { "s3-bucket": "/var/lib/minio/grants/mesh.json" }, grants: { "s3-bucket": "/var/lib/minio/grants" }, "own-secrets": { root: "/var/lib/minio/root.secret", broker: "/var/lib/mesh/minio/broker" }, resources: [ { id: "mesh-state", type: "directory", path: "/var/lib/mesh/minio", mode: "0700" }, { id: "state", type: "directory", path: "/var/lib/minio", mode: "0700" }, { id: "grants", type: "directory", path: "/var/lib/minio/grants", mode: "0700" }, { id: "root-env", type: "file", path: "/var/lib/minio/root.env", mode: "0600", content: "MINIO_ROOT_USER=meshroot\nMINIO_ROOT_PASSWORD=${secret:root}\n" }, { id: "data", type: "directory", path: "/services/minio/data/data1-1", mode: "0700" }, { id: "net", type: "network", name: "minio" }, { id: "server", type: "container", name: "minio", image: pinned("minio/minio"), network: "minio", args: ["server", "/data", "--console-address", ":9001"], "env-file": ["/var/lib/minio/root.env"], volumes: ["/services/minio/data/data1-1:/data"], }, { id: "runtime", type: "container", name: "mesh-minio", image: pinned("mesh-runtime-minio"), network: "minio", volumes: [ "/var/lib/mesh/minio/broker:/run/secrets/broker:ro", "/var/lib/minio/grants:/var/lib/minio/grants:ro", "/var/lib/minio/root.secret:/run/secrets/root:ro", ], env: { MESH_MINIO_ENDPOINT: "http://minio:9000", MESH_MINIO_ROOT_USER: "meshroot", MESH_MINIO_ROOT_PASSWORD_FILE: "/run/secrets/root", MESH_BROKER_FILE: "/run/secrets/broker", MESH_RECEIVES: "/var/lib/minio/grants/mesh.json", }, }, ], }); const consumerManifest = JSON.stringify({ module: "bucketuser", version: "1", // A short slug, so the derived identity `mesh_anchor_bkt` fits an S3 access key's 20 chars where // `mesh_anchor_bucketuser` (22) would not (novox/hq ADR 0054, 04-ISSUES/010). slug: "bkt", requires: ["s3-bucket"], contributes: { "s3-bucket": { name: "bucketuser" } }, binds: { "s3-bucket": "/var/lib/bucketuser/s3.json" }, secrets: { "s3-bucket": "/var/lib/bucketuser/s3.secret" }, resources: [{ id: "state", type: "directory", path: "/var/lib/bucketuser", mode: "0700" }], }); await must(`printf %s ${quote(minioManifest)} > /tmp/minio.json && docker cp /tmp/minio.json mesh-control:/minio.json`); await mesh("module add /minio.json"); await mesh(`module issue minio --node ${MACHINE}`); await mesh(`assign ${MACHINE} minio`); await must(`printf %s ${quote(consumerManifest)} > /tmp/bucketuser.json && docker cp /tmp/bucketuser.json mesh-control:/bucketuser.json`); await mesh("module add /bucketuser.json"); await mesh(`assign ${MACHINE} bucketuser`); await mesh(`push ${MACHINE}`); await settled(); // The mesh delivered the consumer its bound file and its unsealed secret. let boundRaw = ""; const untilBound = Date.now() + 60_000; while (Date.now() < untilBound) { const got = await on(`cat /var/lib/bucketuser/s3.json 2>/dev/null`); if (got.ok && /"as"/.test(got.out)) { boundRaw = got.out; break; } await new Promise((r) => setTimeout(r, 3000)); } assert.match(boundRaw, /"as"/, `the consumer was never told about its bucket:\n${boundRaw}`); const bound = JSON.parse(boundRaw) as { as: string; provision: string }; assert.equal(bound.provision, "s3-bucket"); const accessKey = bound.as; const secretKey = (await must(`cat /var/lib/bucketuser/s3.secret`)).trim(); assert.ok(accessKey && secretKey, `the consumer's access key or secret was empty (as=${accessKey})`); const bucket = bucketFor(accessKey); // THE PROOF: reach the bucket as the consumer, with the access key and secret the mesh delivered // it. mc listing the consumer's own bucket means the service account, the bucket, and the secret all // line up across the two ends. A provisioner that set a different secret answers "Access Denied". const probe = `mc alias set probe http://minio:9000 ${quote(accessKey)} ${quote(secretKey)} >/dev/null 2>&1 && ` + `mc ls probe/${quote(bucket)}/`; let out = { out: "", ok: false }; const untilReach = Date.now() + 90_000; while (Date.now() < untilReach) { out = await on(`docker exec mesh-minio sh -c ${quote(probe)} 2>&1`); if (out.ok) break; if (/denied/i.test(out.out)) break; // fast-fail: the credential is wrong await new Promise((r) => setTimeout(r, 3000)); } assert.doesNotMatch(out.out, /denied/i, `the consumer could not reach its bucket with the mesh's secret — the two ends do not agree:\n${out.out}`); assert.ok(out.ok, `the consumer could not list its granted bucket ${bucket} as ${accessKey}:\n${out.out}\n---\n${(await on(`docker logs mesh-minio 2>&1 | tail -30`)).out}`); });