Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .github/workflows/pm-console-edge-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
11 changes: 9 additions & 2 deletions pm-stack/console/edge/src/edge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,11 @@ export interface EdgeOptions {
setAlarm?: (at: number) => Promise<void>;
/** Work that may finish after the answer has gone. */
later?: (work: Promise<unknown>) => 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
Expand Down Expand Up @@ -287,6 +292,7 @@ export class Edge {
/** The time this object last asked to be woken at. */
private alarmFor = 0;
private later: (work: Promise<unknown>) => void;
private ownClockMs: number;
/** The last self-check: one direct read of each kind, and how it went. */
private selfCheckResult: { at: number; kinds: Record<string, { ok: boolean; error?: string }> } | null = null;
private setAlarm: (at: number) => Promise<void>;
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down
32 changes: 32 additions & 0 deletions pm-stack/console/edge/src/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand All @@ -39,6 +69,7 @@ export class PmEdge extends DurableObject<Env> {
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),
Expand All @@ -55,6 +86,7 @@ export class PmEdge extends DurableObject<Env> {
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),
});
Expand Down
189 changes: 180 additions & 9 deletions pm-stack/console/scripts/edge-smoke.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,38 @@
// 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
// own check of each kind of read then runs in this runtime, with the
// 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";
Expand All @@ -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<string> {
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<string, unknown> = {
board: { counts: { claimed: 2, queued: 1 }, now: [{ id: 1, title: "Smoke task" }], next: [], blocked: [] },
Expand Down Expand Up @@ -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({
Expand All @@ -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([
{
Expand Down Expand Up @@ -155,6 +202,17 @@ const check = (name: string, ok: boolean, detail = "") => {
};
const get = (path: string, headers: Record<string, string> = {}) =>
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<string, unknown>, 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<string, unknown>;
Expand Down Expand Up @@ -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<string, unknown>;
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<string, boolean>;
again?: boolean;
};
const didAgain = (await (await actAsOwner("push.class", { class: "trouble", on: false }, onceId)).json()) as {
result?: Record<string, boolean>;
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)");
}
Expand Down Expand Up @@ -303,6 +447,33 @@ try {
);
const still = (await (await get("/console/health")).json()) as Record<string, unknown>;
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
Expand Down