The store-window bed: a machine enrols and a report arrives while the store is away (issue 083) #50
@@ -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]
|
||||
@@ -0,0 +1,198 @@
|
||||
/**
|
||||
* **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":
|
||||
*
|
||||
* - 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:
|
||||
*
|
||||
* 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, 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"] ?? "";
|
||||
|
||||
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 = "";
|
||||
let held: HeldImage[] = [];
|
||||
|
||||
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<string> {
|
||||
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<string> {
|
||||
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<boolean>, why: () => Promise<string>): Promise<void> {
|
||||
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;
|
||||
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);
|
||||
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"));
|
||||
|
||||
// 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
|
||||
// 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, 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` +
|
||||
`--- 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}`);
|
||||
|
||||
// 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`);
|
||||
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);
|
||||
});
|
||||
Reference in New Issue
Block a user