From c9fd6d7887298c0246deb05136bb9a0646732c86 Mon Sep 17 00:00:00 2001 From: jochen Date: Tue, 22 Sep 2026 14:08:45 +0200 Subject: [PATCH 1/2] A bed that takes the store away while a machine enrols, and holds the enrolment to completing on its own (novox/hq issue 083) --- scenarios/store-window.yml | 32 +++++ test/integration/store-window.test.ts | 165 ++++++++++++++++++++++++++ 2 files changed, 197 insertions(+) create mode 100644 scenarios/store-window.yml create mode 100644 test/integration/store-window.test.ts diff --git a/scenarios/store-window.yml b/scenarios/store-window.yml new file mode 100644 index 0000000..d224b60 --- /dev/null +++ b/scenarios/store-window.yml @@ -0,0 +1,32 @@ +# The control plane's store restarting while a machine joins (novox/hq issue 083). +# +# Adopting the foundation's store — the first thing a control-node does, and the first thing a +# migration does — recreates it, and for those seconds the control plane cannot write. A machine +# enrolling then used to be refused, or worse, left with its token spent and no identity. This +# raises the foundation on `anchor`, takes its store away, has `laptop` enrol into the gap and +# brings the store back: the enrolment must complete on its own. +scenario: store-window + +segments: + hosting: + kind: public + cidr: [192.0.2.0/24] + +machines: + anchor: + at: { segment: hosting, address: [192.0.2.10] } + egress: true + inbound: allow + memory: 3GiB + cpus: 2 + laptop: + at: { segment: hosting, address: [192.0.2.20] } + egress: true + inbound: allow + memory: 1GiB + +images: + - mesh-controller:development + +place: + all: [host, runtime] diff --git a/test/integration/store-window.test.ts b/test/integration/store-window.test.ts new file mode 100644 index 0000000..7feb7f2 --- /dev/null +++ b/test/integration/store-window.test.ts @@ -0,0 +1,165 @@ +/** + * **Nothing a machine sends while the store restarts is lost** (novox/hq issues 082, 083). + * + * The foundation's store is recreated when it is adopted, and for those seconds the control plane + * cannot write. This bed takes the store away on the control-node, has a second machine enrol into + * the gap, and brings the store back after the control plane has had to say "not now": + * + * - the enrolment completes on its own, with the keys it started with — the host asked again + * while the mesh said it could not answer, and the token was not spent until it could; + * - the control plane really did say "not now", so the gap was hit, not missed; + * - the machine then applies what it is pushed and is heard from. + * + * It needs a host binary and the foundation bundle: + * + * MESH_LAB_HOST_BINARY=.../mesh-host + * MESH_LAB_BUNDLE=.../examples/foundation-first-node.lock + */ +import { test, before, after } from "node:test"; +import assert from "node:assert/strict"; +import { existsSync } 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, foundationBundle } from "./harness.ts"; + +const capability = await labIsUsable(); +const binary = hostBinaryPath(); +const bundle = process.env["MESH_LAB_BUNDLE"] ?? ""; + +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 foundation bundle (mesh-host examples/)" + : false; + +const SCENARIO = "store-window"; +let instanceId = ""; + +function quote(s: string): string { + return `'${s.replaceAll("'", `'\\''`)}'`; +} + +async function on(machine: string, 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(machine: string, command: string, timeoutMs?: number): Promise { + const { out, ok } = await on(machine, command, timeoutMs); + if (!ok) throw new Error(`${machine}: ${command}\n${out}`); + return out; +} + +async function mesh(command: string, timeoutMs?: number): Promise { + return must("anchor", `docker exec mesh-controller /mesh-controller ${command}`, timeoutMs); +} + +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; +} + +async function until(what: string, within: number, check: () => Promise, why: () => Promise): Promise { + const end = Date.now() + within; + while (Date.now() < end) { + if (await check()) return; + await new Promise((r) => setTimeout(r, 3000)); + } + assert.fail(`${what} did not happen within ${Math.round(within / 1000)}s:\n${await why()}`); +} + +before(async () => { + if (skip) return; + const raised = await raise(loadScenario(`scenarios/${SCENARIO}.yml`), { + onProgress: (m) => console.log(`raise: ${m}`), + }); + instanceId = raised.instanceId; + + await must("anchor", `cat > /tmp/foundation.lock <<'MESHBUNDLE'\n${foundationBundle(bundle, raised.images)}\nMESHBUNDLE`); + await must("anchor", `${HOST_PATH} apply /tmp/foundation.lock`, 600_000); + const up = await must("anchor", `docker ps --format '{{.Names}}'`); + for (const c of ["mesh-store", "mesh-broker", "mesh-controller"]) { + assert.match(up, new RegExp(c), `the foundation did not raise ${c}:\n${up}`); + } + await mesh("node add anchor"); + const own = tokenFrom(await mesh("token issue --node anchor")); + await must("anchor", `${HOST_PATH} enrol --token ${quote(own)}`); + await must("anchor", `nohup ${HOST_PATH} run > /var/log/mesh-host.log 2>&1 & sleep 3`); +}, { timeout: 1_800_000 }); + +after(async () => { + if (process.env["MESH_LAB_KEEP"]) { console.log(`MESH_LAB_KEEP set — leaving ${instanceId} standing`); return; } + if (instanceId) await destroy(instanceId); + await destroyAll(`${SCENARIO}-`); +}, { timeout: 600_000 }); + +test("a machine enrolling while the store is away joins when it comes back, with the keys it started with", { + skip, timeout: 900_000, +}, async () => { + await mesh("node add laptop"); + const token = tokenFrom(await mesh("token issue --node laptop")); + + // The store goes away, as it does when the foundation is adopted; the broker stays, so the + // enrolment reaches the control plane and the control plane cannot write. + await must("anchor", `docker stop mesh-store`); + + // The machine enrols into the gap. In the background: it will be told "not now" and keep asking. + await must("laptop", `nohup sh -c ${quote(`${HOST_PATH} enrol --token ${quote(token)} > /tmp/enrol.log 2>&1; ` + + `echo "exit=$?" >> /tmp/enrol.log`)} > /dev/null 2>&1 & sleep 1`); + + // The control plane said "not now" at least once — so the gap was hit, not missed. + await until("the control plane asking the machine to enrol again", 90_000, + async () => /asked "laptop" to enrol again/.test((await on("anchor", `docker logs mesh-controller 2>&1`)).out), + async () => `--- controller ---\n${(await on("anchor", `docker logs --tail 30 mesh-controller 2>&1`)).out}\n` + + `--- laptop enrol ---\n${(await on("laptop", `cat /tmp/enrol.log`)).out}`); + + // The store comes back; the enrolment completes on its own, with nothing done by hand. + await must("anchor", `docker start mesh-store`); + await until("the enrolment completing", 150_000, + async () => /exit=/.test((await on("laptop", `cat /tmp/enrol.log`)).out), + async () => `--- laptop enrol ---\n${(await on("laptop", `cat /tmp/enrol.log`)).out}\n` + + `--- controller ---\n${(await on("anchor", `docker logs --tail 30 mesh-controller 2>&1`)).out}`); + const said = (await on("laptop", `cat /tmp/enrol.log`)).out; + assert.match(said, /exit=0/, `the enrolment did not complete after the store came back:\n${said}`); + assert.match(said, /enrolled as laptop/, `the machine was not enrolled as laptop:\n${said}`); + + // Enrolled with the keys it generated at the start — one attempt, not a new identity — and the + // mesh holds its sealing key, which it recorded only once the store was back. + const generated = said.match(/generated this node's identity: (\S+)/); + assert.ok(generated, `the enrolment did not say which identity it generated:\n${said}`); + assert.equal((said.match(/generated this node's identity:/g) ?? []).length, 1, + `the machine generated more than one identity:\n${said}`); + const recorded = await must("anchor", + `docker exec mesh-store psql -U postgres -d inventory -qAt ` + + `-c "select name from node where sealing_key is not null order by name"`); + assert.match(recorded, /(^|\n)laptop(\n|$)/, `the mesh holds no sealing key for laptop:\n${recorded}`); + + // And the machine is a working member: pushed something, it applies it and is heard from. + await must("laptop", `nohup ${HOST_PATH} run > /var/log/mesh-host.log 2>&1 & sleep 3`); + await must("anchor", `printf %s '{"module":"marker","version":"1","resources":[` + + `{"id":"marker","type":"file","path":"/etc/store-window","content":"joined\\\\n","mode":"0644"}]}' > /tmp/marker.json`); + await must("anchor", `docker cp /tmp/marker.json mesh-controller:/marker.json`); + await mesh("module add /marker.json"); + await mesh("assign laptop marker"); + await mesh("push laptop"); + await until("the machine applying what it was pushed", 120_000, + async () => (await on("laptop", `grep -q joined /etc/store-window`)).ok, + async () => (await on("laptop", `tail -20 /var/log/mesh-host.log`)).out); + await until("the mesh hearing the machine's report", 120_000, + async () => { + const r = await on("anchor", `docker exec mesh-controller /mesh-controller status --json`); + if (!r.ok) return false; + const state = JSON.parse(r.out) as { reported: { node: string; outcome: string; current: boolean }[] }; + return state.reported.some((w) => w.node === "laptop" && w.outcome === "applied" && w.current); + }, + async () => (await on("anchor", `docker exec mesh-controller /mesh-controller status 2>&1`)).out); +}); -- 2.54.0 From 514f7a46cd56b7da4a8f01666485e4975c837cf4 Mon Sep 17 00:00:00 2001 From: jochen Date: Tue, 22 Sep 2026 14:24:01 +0200 Subject: [PATCH 2/2] The store-window bed holds a report through the gap, asserts the enrolment is answered meanwhile, and compares the keys the mesh recorded with the ones the machine generated --- test/integration/store-window.test.ts | 65 ++++++++++++++++++++------- 1 file changed, 49 insertions(+), 16 deletions(-) diff --git a/test/integration/store-window.test.ts b/test/integration/store-window.test.ts index 7feb7f2..3bd4e0e 100644 --- a/test/integration/store-window.test.ts +++ b/test/integration/store-window.test.ts @@ -5,9 +5,10 @@ * cannot write. This bed takes the store away on the control-node, has a second machine enrol into * the gap, and brings the store back after the control plane has had to say "not now": * - * - the enrolment completes on its own, with the keys it started with — the host asked again - * while the mesh said it could not answer, and the token was not spent until it could; - * - the control plane really did say "not now", so the gap was hit, not missed; + * - a report arriving in the gap is held, and recorded when the store is back; + * - the enrolment is answered "not now" while that report is held — not queued behind it — and + * completes on its own when the store is back, with the keys it started with: the mesh holds + * the very keys the machine generated; * - the machine then applies what it is pushed and is heard from. * * It needs a host binary and the foundation bundle: @@ -22,7 +23,8 @@ 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, foundationBundle } from "./harness.ts"; +import { labIsUsable, destroyAll, foundationBundle, onTheMachine } from "./harness.ts"; +import type { HeldImage } from "../../src/pinning.ts"; const capability = await labIsUsable(); const binary = hostBinaryPath(); @@ -38,6 +40,7 @@ const skip = !capability.usable const SCENARIO = "store-window"; let instanceId = ""; +let held: HeldImage[] = []; function quote(s: string): string { return `'${s.replaceAll("'", `'\\''`)}'`; @@ -83,6 +86,7 @@ before(async () => { onProgress: (m) => console.log(`raise: ${m}`), }); instanceId = raised.instanceId; + held = raised.images; await must("anchor", `cat > /tmp/foundation.lock <<'MESHBUNDLE'\n${foundationBundle(bundle, raised.images)}\nMESHBUNDLE`); await must("anchor", `${HOST_PATH} apply /tmp/foundation.lock`, 600_000); @@ -108,15 +112,31 @@ test("a machine enrolling while the store is away joins when it comes back, with await mesh("node add laptop"); const token = tokenFrom(await mesh("token issue --node laptop")); + // A report that will arrive in the gap: the anchor is pushed a step that takes ten seconds, so + // its report lands after the store has gone. + const sleeper = onTheMachine("alpine", held); + await must("anchor", `printf %s '{"module":"slow","version":"1","resources":[` + + `{"id":"step","type":"container","name":"slow-step","image":"${sleeper}","run-once":true,` + + `"args":["sh","-c","sleep 10"]}]}' > /tmp/slow.json`); + await must("anchor", `docker cp /tmp/slow.json mesh-controller:/slow.json`); + await mesh("module add /slow.json"); + await mesh("assign anchor slow"); + await mesh("push anchor"); + // The store goes away, as it does when the foundation is adopted; the broker stays, so the - // enrolment reaches the control plane and the control plane cannot write. + // report and the enrolment reach the control plane and the control plane cannot write. await must("anchor", `docker stop mesh-store`); + await until("the anchor's report arriving in the gap and being held", 120_000, + async () => /could not keep anchor's report .*holding it/.test((await on("anchor", `docker logs mesh-controller 2>&1`)).out), + async () => `--- controller ---\n${(await on("anchor", `docker logs --tail 30 mesh-controller 2>&1`)).out}\n` + + `--- anchor host ---\n${(await on("anchor", `tail -10 /var/log/mesh-host.log`)).out}`); // The machine enrols into the gap. In the background: it will be told "not now" and keep asking. await must("laptop", `nohup sh -c ${quote(`${HOST_PATH} enrol --token ${quote(token)} > /tmp/enrol.log 2>&1; ` + `echo "exit=$?" >> /tmp/enrol.log`)} > /dev/null 2>&1 & sleep 1`); - // The control plane said "not now" at least once — so the gap was hit, not missed. + // The control plane said "not now" at least once, while the anchor's report was held — so the + // gap was hit, and the enrolment was answered rather than queued behind what the store owed. await until("the control plane asking the machine to enrol again", 90_000, async () => /asked "laptop" to enrol again/.test((await on("anchor", `docker logs mesh-controller 2>&1`)).out), async () => `--- controller ---\n${(await on("anchor", `docker logs --tail 30 mesh-controller 2>&1`)).out}\n` + @@ -132,16 +152,29 @@ test("a machine enrolling while the store is away joins when it comes back, with assert.match(said, /exit=0/, `the enrolment did not complete after the store came back:\n${said}`); assert.match(said, /enrolled as laptop/, `the machine was not enrolled as laptop:\n${said}`); - // Enrolled with the keys it generated at the start — one attempt, not a new identity — and the - // mesh holds its sealing key, which it recorded only once the store was back. - const generated = said.match(/generated this node's identity: (\S+)/); - assert.ok(generated, `the enrolment did not say which identity it generated:\n${said}`); - assert.equal((said.match(/generated this node's identity:/g) ?? []).length, 1, - `the machine generated more than one identity:\n${said}`); - const recorded = await must("anchor", - `docker exec mesh-store psql -U postgres -d inventory -qAt ` + - `-c "select name from node where sealing_key is not null order by name"`); - assert.match(recorded, /(^|\n)laptop(\n|$)/, `the mesh holds no sealing key for laptop:\n${recorded}`); + // The mesh holds the very keys the machine generated at the start: its identity is the live key, + // and its sealing key is the one on its record. + const identity = said.match(/generated this node's identity: (\S+)/)?.[1]; + const sealing = said.match(/generated this node's sealing key:\s+(\S+)/)?.[1]; + assert.ok(identity && sealing, `the enrolment did not say which keys it generated:\n${said}`); + const nodeId = (await must("anchor", `docker exec mesh-store psql -U postgres -d inventory -qAt ` + + `-c "select id from node where name = 'laptop'"`)).trim(); + const live = (await must("anchor", `docker exec mesh-store psql -U postgres -d identity -qAt ` + + `-c "select encode(public, 'base64') from node_key where node = '${nodeId}' and revoked is null"`)).trim(); + assert.equal(live, identity, `the mesh's live key for laptop is not the one it generated`); + const sealedTo = (await must("anchor", `docker exec mesh-store psql -U postgres -d inventory -qAt ` + + `-c "select sealing_key from node where name = 'laptop'"`)).trim(); + assert.equal(sealedTo, sealing, `the mesh's sealing key for laptop is not the one it generated`); + + // The report held through the gap was recorded once the store was back. + await until("the anchor's held report being recorded", 60_000, + async () => { + const r = await on("anchor", `docker exec mesh-controller /mesh-controller status --json`); + if (!r.ok) return false; + const state = JSON.parse(r.out) as { reported: { node: string; outcome: string; current: boolean }[] }; + return state.reported.some((w) => w.node === "anchor" && w.outcome === "applied" && w.current); + }, + async () => (await on("anchor", `docker logs --tail 20 mesh-controller 2>&1`)).out); // And the machine is a working member: pushed something, it applies it and is heard from. await must("laptop", `nohup ${HOST_PATH} run > /var/log/mesh-host.log 2>&1 & sleep 3`); -- 2.54.0