diff --git a/.github/workflows/pm-console-edge-deploy.yml b/.github/workflows/pm-console-edge-deploy.yml index c4967b67b..39d64f606 100644 --- a/.github/workflows/pm-console-edge-deploy.yml +++ b/.github/workflows/pm-console-edge-deploy.yml @@ -109,7 +109,9 @@ jobs: # behaves differently there (a report that hung for twenty # seconds on first contact passed every bun test). This runs # the bundle in the Worker runtime itself, with a host on port - # 4898, and makes the checks that need no sign-in. + # 4898. The run signs its own sign-in token (a key it makes, + # served by the host on that port as the issuer's key set), so + # the checks cover what only the owner reaches as well. # # It is started as it runs when it does the core itself: the # switch on, and two made-up tokens that open nothing. The same @@ -120,6 +122,8 @@ jobs: --var EDGE_DIRECT:on --var GITHUB_TOKEN:smoke-github --var QUEUE_TOKEN:smoke-queue \ --var GITHUB_API_ORIGIN:http://127.0.0.1:4898/github \ --var QUEUE_DB_ORIGIN:http://127.0.0.1:4898/queue \ + --var OWN_CLOCK_MS:2000 \ + --var KEYS_FROM:https://smoke.invalid/jwks --var KEYS_TO:http://127.0.0.1:4898/jwks \ >"$RUNNER_TEMP/wrangler-dev.log" 2>&1 &) up=false for _ in $(seq 1 60); do diff --git a/pm-stack/console/edge/src/edge.ts b/pm-stack/console/edge/src/edge.ts index cb75e6098..76ff657be 100644 --- a/pm-stack/console/edge/src/edge.ts +++ b/pm-stack/console/edge/src/edge.ts @@ -248,6 +248,11 @@ export interface EdgeOptions { setAlarm?: (at: number) => Promise; /** Work that may finish after the answer has gone. */ later?: (work: Promise) => void; + /** How often the object's own clock wakes to read again what the + * owner is told about, in milliseconds. A local run shortens it so + * a smoke run can watch an alarm fire; where this is deployed it + * is left unset and the minute stands. */ + ownClockMs?: number; } /** An action this side tried and its token was refused for, with @@ -287,6 +292,7 @@ export class Edge { /** The time this object last asked to be woken at. */ private alarmFor = 0; private later: (work: Promise) => void; + private ownClockMs: number; /** The last self-check: one direct read of each kind, and how it went. */ private selfCheckResult: { at: number; kinds: Record } | null = null; private setAlarm: (at: number) => Promise; @@ -334,6 +340,7 @@ export class Edge { await setAlarm(at); }; this.later = options.later ?? (() => {}); + this.ownClockMs = options.ownClockMs ?? OWN_CLOCK_MS; this.syncRefused = new KeyedLimiter(SYNC_REFUSALS, 60_000, this.now); this.docs = new Docs( Object.fromEntries( @@ -1361,7 +1368,7 @@ export class Edge { // not be told, soon again. wakes.push(Math.max(this.hostSeen + SILENT_MS + 1000, now + ALARM_RETRY_MS)); } - if (await this.clockWanted(cfg)) wakes.push(Math.max(now + 1000, this.clockAt + OWN_CLOCK_MS)); + if (await this.clockWanted(cfg)) wakes.push(Math.max(now + 1000, this.clockAt + this.ownClockMs)); // Something the owner is told about that this side was turned away // for is tried again when its time is up; with nobody looking and // no machine reporting, only a wake would notice that it is. @@ -1403,7 +1410,7 @@ export class Edge { // Leave the transition pending so a later alarm can retry it. } } - if (now - this.clockAt >= OWN_CLOCK_MS && (await this.clockWanted(cfg))) { + if (now - this.clockAt >= this.ownClockMs && (await this.clockWanted(cfg))) { this.clockAt = now; // Read again what is stale, and wait for it: this wake is the // only thing looking. Twice, because the first look of an object diff --git a/pm-stack/console/edge/src/worker.ts b/pm-stack/console/edge/src/worker.ts index 6fdcbc21d..822942c83 100644 --- a/pm-stack/console/edge/src/worker.ts +++ b/pm-stack/console/edge/src/worker.ts @@ -30,6 +30,36 @@ interface Env { /** Local development only: where the host is, when it is not simply * behind this hostname's route. Never set where this is deployed. */ HOST_ORIGIN?: string; + /** Local development only: how often the object's own clock wakes, + * in milliseconds, so a smoke run can watch an alarm fire instead + * of waiting out the minute. Never set where this is deployed. */ + OWN_CLOCK_MS?: string; + /** Local development only: a sign-in issuer's key set, named by its + * real (https) address in KEYS_FROM, is fetched from KEYS_TO + * instead -- a stand-in on this machine serving the keys of a + * sign-in the run made itself, so a smoke run can sign the owner's + * token. The issuer's address in the settings is never rewritten; + * only this Worker's fetch of it is. Never set where deployed. */ + KEYS_FROM?: string; + KEYS_TO?: string; +} + +/** The fetch the object makes, with the local key-set stand-in above + * applied when the run names one; undefined (the plain fetch) when + * it does not. Only a request for that one address is rewritten. */ +function keysFetch(env: Env): typeof fetch | undefined { + if (!env.KEYS_FROM || !env.KEYS_TO) return undefined; + const from = env.KEYS_FROM; + const to = env.KEYS_TO; + return ((url: string | URL | Request, init?: RequestInit) => + fetch(typeof url === "string" && url === from ? to : (url as string), init)) as typeof fetch; +} + +/** The run's clock interval, when it names a usable one (a second or + * more); anything else leaves the object's own minute in place. */ +function ownClockMs(raw: string | undefined): number | undefined { + const n = Number(raw); + return raw !== undefined && Number.isFinite(n) && n >= 1_000 ? Math.floor(n) : undefined; } /** The object with the state. */ @@ -39,6 +69,7 @@ export class PmEdge extends DurableObject { constructor(ctx: DurableObjectState, env: Env) { super(ctx, env); this.edge = new Edge({ + fetchImpl: keysFetch(env), kv: { get: (key) => ctx.storage.get(key), put: (key, value) => ctx.storage.put(key, value), @@ -55,6 +86,7 @@ export class PmEdge extends DurableObject { fetchImpl: fetch, }, setAlarm: (at) => ctx.storage.setAlarm(at), + ownClockMs: ownClockMs(env.OWN_CLOCK_MS), // A read started behind an answer is given the time to finish. later: (work) => ctx.waitUntil(work), }); diff --git a/pm-stack/console/scripts/edge-smoke.ts b/pm-stack/console/scripts/edge-smoke.ts index a2ddcdc93..64628bf37 100644 --- a/pm-stack/console/scripts/edge-smoke.ts +++ b/pm-stack/console/scripts/edge-smoke.ts @@ -3,15 +3,29 @@ // cd edge && node build.mjs && npx wrangler dev --port 8799 \ // --var PUBLIC_HOST:localhost --var HOST_ORIGIN:http://127.0.0.1:4898 --var VERSION:smoke \ // --var EDGE_DIRECT:on --var GITHUB_TOKEN:smoke-github --var QUEUE_TOKEN:smoke-queue \ -// --var GITHUB_API_ORIGIN:http://127.0.0.1:4898/github --var QUEUE_DB_ORIGIN:http://127.0.0.1:4898/queue +// --var GITHUB_API_ORIGIN:http://127.0.0.1:4898/github --var QUEUE_DB_ORIGIN:http://127.0.0.1:4898/queue \ +// --var OWN_CLOCK_MS:2000 --var KEYS_FROM:https://smoke.invalid/jwks --var KEYS_TO:http://127.0.0.1:4898/jwks // SMOKE_PRIMARY=1 bun run scripts/edge-smoke.ts (in another shell) // // It starts a host on port 4898 with made-up state and this repo's real -// handler and link, then checks what can be checked without a sign-in -// issuer: the Worker starts, serves its public files, takes the host's -// signed report (fetching the host's key through the "tunnel"), keeps a -// section too large for one stored value, leaves an unsigned caller to -// the host, and answers a phone that is not linked. +// handler and link, then checks: the Worker starts, serves its public +// files, takes the host's signed report (fetching the host's key +// through the "tunnel"), keeps a section too large for one stored +// value, leaves an unsigned caller to the host, and answers a phone +// that is not linked. +// +// The sign-in is this run's own too: a key made here, whose public half +// the stand-in serves as the issuer's key set (the Worker is started +// with KEYS_FROM/KEYS_TO, above, so its fetch of the issuer's https +// key-set address lands on the stand-in), so the run can sign the +// owner's token itself (as scripts/edge-demo.ts does) and check what +// only a signed-in caller reaches, in this runtime: who the edge says +// is calling, an action done once under a name and answered again from +// the record, the platform read, an alarm the object sets firing and +// doing its work, and -- with the host gone -- an action held for a +// machine, listed by the machines read, and taken back. The clock's +// minute is shortened for the run (OWN_CLOCK_MS, above) so the alarm +// can be watched without waiting it out. // // Started as above, the Worker does the core itself, and the same port // stands in for GitHub's API (/github) and the queue store (/queue): its @@ -19,6 +33,8 @@ // Worker's own fetch, and has to come back good. SMOKE_PRIMARY=1 says // that is what was started, so that leaving the flags off is a failure // and not a quiet skip. +import { readFileSync } from "node:fs"; +import { join } from "node:path"; import { identify } from "../src/auth.ts"; import { readConfig } from "../src/config.ts"; import { Devices, Limiter } from "../src/devices.ts"; @@ -36,13 +52,34 @@ const EDGE = process.env.EDGE ?? "http://localhost:8799"; const cfg = readConfig({ HOME: "/tmp/pm-edge-smoke", PM_CONSOLE_OWNER_EMAILS: "owner@example.com" }); cfg.issuers = [ { - name: "cloudflare-access", - issuer: "https://smoke.cloudflareaccess.com", - jwksUri: "https://smoke.cloudflareaccess.com/cdn-cgi/access/certs", + name: "smoke-sign-in", + issuer: "https://smoke.invalid", + jwksUri: "https://smoke.invalid/jwks", audience: "smoke", source: "access", }, ]; +// A sign-in issuer made here: the stand-in below serves the public +// half as the issuer's key set, and this signs the owner's token. +const pair = (await crypto.subtle.generateKey( + { name: "RSASSA-PKCS1-v1_5", modulusLength: 2048, publicExponent: new Uint8Array([1, 0, 1]), hash: "SHA-256" }, + true, + ["sign", "verify"], +)) as CryptoKeyPair; +const jwk = { ...(await crypto.subtle.exportKey("jwk", pair.publicKey)), kid: "smoke" }; +const part = (value: unknown) => Buffer.from(JSON.stringify(value)).toString("base64url"); +async function ownerToken(): Promise { + const now = Math.floor(Date.now() / 1000); + const signed = `${part({ alg: "RS256", kid: "smoke" })}.${part({ + iss: "https://smoke.invalid", + aud: ["smoke"], + exp: now + 3600, + iat: now, + email: "owner@example.com", + })}`; + const sig = await crypto.subtle.sign("RSASSA-PKCS1-v1_5", pair.privateKey, new TextEncoder().encode(signed)); + return `${signed}.${Buffer.from(sig).toString("base64url")}`; +} const big = { open: Array.from({ length: 5000 }, (_, i) => ({ qid: `q${i}`, question: "Why? ".repeat(80) })) }; const data: Record = { board: { counts: { claimed: 2, queued: 1 }, now: [{ id: 1, title: "Smoke task" }], next: [], blocked: [] }, @@ -102,6 +139,7 @@ const server = Bun.serve({ port: 4898, fetch: async (req, srv) => { const url = new URL(req.url); + if (url.pathname === "/jwks") return Response.json({ keys: [jwk] }); if (url.pathname.startsWith("/github/")) { const path = url.pathname.slice("/github/".length); outside.push({ @@ -110,6 +148,15 @@ const server = Bun.serve({ auth: req.headers.get("authorization"), agent: req.headers.get("user-agent"), }); + if (path.endsWith("/contents/pm-stack/platform/workers.json")) { + // The platform read's list, served as GitHub serves a file. + return Response.json({ + content: Buffer.from(readFileSync(join(import.meta.dir, "..", "..", "platform", "workers.json"))).toString( + "base64", + ), + }); + } + if (path.includes("/actions/workflows/")) return Response.json({ workflow_runs: [] }); if (path.endsWith("/pulls")) { return Response.json([ { @@ -155,6 +202,17 @@ const check = (name: string, ok: boolean, detail = "") => { }; const get = (path: string, headers: Record = {}) => fetch(`${EDGE}${path}`, { headers, redirect: "manual" }); +const asOwner = async (path: string) => get(path, { "cf-access-jwt-assertion": await ownerToken() }); +const actAsOwner = async (kind: string, args: Record, id?: string) => + fetch(`${EDGE}/console/api/act`, { + method: "POST", + headers: { + "content-type": "application/json", + "x-pm-console": "1", + "cf-access-jwt-assertion": await ownerToken(), + }, + body: JSON.stringify(id === undefined ? { kind, args } : { kind, args, id }), + }); try { const before = (await (await get("/console/health")).json()) as Record; @@ -245,6 +303,92 @@ try { JSON.stringify(second?.owns) === JSON.stringify(["board", "prs", "priorities", "asks"]), JSON.stringify(second?.owns), ); + + // Signed in with this run's own token: what only the owner + // reaches, checked in this runtime rather than under bun. + const me = (await (await asOwner("/console/api/me")).json()) as Record; + check( + "signed in with the run's own token, the edge says who is calling", + me.email === "owner@example.com" && me.edge === true, + JSON.stringify(me), + ); + const onceId = "11111111-1111-4111-8111-111111111111"; + const didFirst = (await (await actAsOwner("push.class", { class: "trouble", on: false }, onceId)).json()) as { + result?: Record; + again?: boolean; + }; + const didAgain = (await (await actAsOwner("push.class", { class: "trouble", on: false }, onceId)).json()) as { + result?: Record; + again?: boolean; + }; + check( + "an action under a name is done once, and its second arrival is answered from the record", + didFirst.result?.trouble === false && + didFirst.again !== true && + didAgain.again === true && + didAgain.result?.trouble === false, + `first ${JSON.stringify(didFirst)}, again ${JSON.stringify(didAgain)}`, + ); + seen.length = 0; + const platform = (await (await asOwner("/console/api/platform")).json()) as { + checked?: string; + malformed?: number; + workers?: { name: string }[]; + }; + check( + "the platform read is answered here, from the repository's own list", + platform.malformed === 0 && + typeof platform.checked === "string" && + (platform.workers ?? []).some((w) => w.name === "pm-console-edge"), + `${(platform.workers ?? []).length} workers, checked ${platform.checked}; host saw ${seen.join(", ")}`, + ); + check( + "and no machine was asked for it", + !seen.some((path) => path.includes("/api/platform")), + seen.join(", "), + ); + // Someone to tell: a device takes notifications (its service is a + // known one and its key a real point; nothing is sent to it in + // this run unless the clock's news is worth sending). + const devicePair = (await crypto.subtle.generateKey({ name: "ECDSA", namedCurve: "P-256" }, true, [ + "sign", + "verify", + ])) as CryptoKeyPair; + const devicePoint = Buffer.from((await crypto.subtle.exportKey("raw", devicePair.publicKey)) as ArrayBuffer).toString( + "base64url", + ); + const subscribed = (await ( + await actAsOwner( + "push.on", + { + subscription: { + endpoint: "https://fcm.googleapis.com/fcm/send/smoke-run", + keys: { p256dh: devicePoint, auth: Buffer.from(crypto.getRandomValues(new Uint8Array(16))).toString("base64url") }, + }, + label: "smoke", + }, + "22222222-2222-4222-8222-222222222222", + ) + ).json()) as { result?: { devices?: number } }; + check("a device takes notifications", subscribed.result?.devices === 1, JSON.stringify(subscribed)); + // The clock starts itself when a document is read. Its interval + // is the run's shortened one, and the board goes stale half a + // minute in: an alarm that fires and does its work reads the + // board from GitHub again, here, with nobody asking. + const board = await asOwner("/console/api/doc/core"); + check("the state is read signed in", board.status === 200, String(board.status)); + await new Promise((resolve) => setTimeout(resolve, 3000)); + const readsBefore = outside.filter((o) => o.to === "github").length; + let readsAfter = readsBefore; + for (let waited = 0; waited < 75 && readsAfter <= readsBefore; waited += 2) { + await new Promise((resolve) => setTimeout(resolve, 2000)); + readsAfter = outside.filter((o) => o.to === "github").length; + } + check( + "an alarm the object set fired and did its work: what the owner is told about was read again, unasked", + readsAfter > readsBefore, + `${readsBefore} reads of GitHub before, ${readsAfter} after waiting`, + ); } else { console.log("skip the Worker was not started to do the core itself (see the top of this file)"); } @@ -303,6 +447,33 @@ try { ); const still = (await (await get("/console/health")).json()) as Record; check("the edge itself is still up", still.ok === true && Number(still.deferred) >= 3, JSON.stringify(still)); + // With the host gone, what may be done late is held here instead of + // lost: signed in, it can be seen waiting and taken back. + const heldId = "33333333-3333-4333-8333-333333333333"; + const heldAnswer = (await (await actAsOwner("message.send", { to: "smoke", text: "held by the smoke run" }, heldId)).json()) as { + ok?: boolean; + held?: boolean; + }; + check( + "with the host gone, an action that may be done late is held, not lost", + heldAnswer.ok === true && heldAnswer.held === true, + JSON.stringify(heldAnswer), + ); + const machines = (await (await asOwner("/console/api/machines")).json()) as { + held?: { id: string; kind: string }[]; + }; + check( + "the machines read lists it as waiting for a machine", + (machines.held ?? []).some((h) => h.id === heldId && h.kind === "message.send"), + JSON.stringify(machines.held), + ); + const dropped = await actAsOwner("held.drop", { id: heldId }, "44444444-4444-4444-8444-444444444444"); + const machinesAfter = (await (await asOwner("/console/api/machines")).json()) as { held?: { id: string }[] }; + check( + "and it can be taken back", + dropped.status === 200 && !(machinesAfter.held ?? []).some((h) => h.id === heldId), + `drop ${dropped.status}; still waiting ${JSON.stringify(machinesAfter.held)}`, + ); // The Android app's release is read from GitHub itself, in this // runtime, over the real network. There may be none, and GitHub may // not answer a runner: either is said as null, and neither is a