The store-window bed: a machine enrols and a report arrives while the store is away (issue 083) #50

Merged
jschoubben merged 2 commits from multiple-fixes into main 2026-09-22 12:42:59 +00:00
Showing only changes of commit 514f7a46cd - Show all commits
+49 -16
View File
@@ -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`);