From 6feb3986435d366dd590f0d9da3cb5bfc8650799 Mon Sep 17 00:00:00 2001 From: Archer Date: Sat, 7 Nov 2026 18:01:46 -0600 Subject: [PATCH] github-ci: public broker route for the events sink (#6925) The sink POSTed to the queue broker's /events-append verb only when EVENTS_BROKER_URL was set, but the broker is tailnet-only, so the public Worker could never reach it and always used its direct Turso INSERT fallback. The broker now has a sanctioned public route: https://queue-broker.security.cab (pm-stack Cloudflare Tunnel ingress -> the broker tailnet listener), gated by a Cloudflare Access application that admits only this Worker's service token, with the broker's own token check behind it. - sink.ts: send the broker token as X-Agent-Token (the only header the broker authenticates; it does not read Authorization: Bearer) and the Access service-token pair as CF-Access-Client-Id / CF-Access-Client-Secret when configured. Direct INSERT remains the documented outage fallback when EVENTS_BROKER_URL is unset. - wrangler.toml: EVENTS_BROKER_URL now points at the public route; the broker + Access secrets are set out-of-band (wrangler secret). - test/sink.test.ts: header contract for the broker route. - README: the route, its two auth layers, and the fallback. --- pkgs/github-ci/worker/README.md | 36 +++++++++++--- pkgs/github-ci/worker/src/sink.ts | 36 ++++++++++---- pkgs/github-ci/worker/test/sink.test.ts | 63 +++++++++++++++++++++++++ pkgs/github-ci/worker/vitest.config.ts | 1 + pkgs/github-ci/worker/wrangler.toml | 22 ++++++--- 5 files changed, 135 insertions(+), 23 deletions(-) create mode 100644 pkgs/github-ci/worker/test/sink.test.ts diff --git a/pkgs/github-ci/worker/README.md b/pkgs/github-ci/worker/README.md index 62d4b4efe7..db8d6bd794 100644 --- a/pkgs/github-ci/worker/README.md +++ b/pkgs/github-ci/worker/README.md @@ -16,12 +16,28 @@ during the dual-run diff period. - `src/turso.ts` — minimal libSQL `/v2/pipeline` client with typed args (no string interpolation). - `src/sink.ts` — the #5690 agent-events sink (POST the queue - broker's `/events-append` when `EVENTS_BROKER_URL` is set; until - a public broker route exists — the broker is tailnet-only — the - Worker performs the sink's own INSERT into fleet-queue `events`), - plus the fleet-queue stores that replace PG `agent.repo_snapshots` - (`agent_repo_snapshots`) and `agent.watermarks` (`agent_watermarks`, - `dirty:` keys). + broker's `/events-append` when `EVENTS_BROKER_URL` is set; the + broker route is public since #6925 — see below — and the Worker's + own INSERT into fleet-queue `events` remains as the outage + fallback when the var is unset), plus the fleet-queue stores that + replace PG `agent.repo_snapshots` (`agent_repo_snapshots`) and + `agent.watermarks` (`agent_watermarks`, `dirty:` keys). + +## Broker route (#6925) + +`EVENTS_BROKER_URL = https://queue-broker.security.cab`: a +Cloudflare Tunnel ingress row on the pm-stack tunnel +(`0ee3bf69-e5d1-4ed8-95de-4a17a18156b5`) forwarding to the +broker's tailnet listener (`100.99.113.103:4801`). A Cloudflare +Access application on the hostname admits only the +`github-ci-worker-queue-broker` service token; the Worker sends +its `CF-Access-Client-Id` / `CF-Access-Client-Secret` headers +plus the broker's own `X-Agent-Token` (the broker does not read +`Authorization: Bearer`). Auth chain, verified live 2026-11-07: +no Access headers → 403 at the edge; Access headers with a wrong +agent token → 401 from the broker; both correct → the broker +answers. Unsetting `EVENTS_BROKER_URL` reverts the sink to the +direct-Turso INSERT fallback with no code change. ## Writes (Turso only — no Postgres anywhere) @@ -39,7 +55,13 @@ during the dual-run diff period. in BWS as `cloudflare.github-ci-webhook-secret`; the org/App flip rotates it again per #5693), `TURSO_PLATFORM_TOKEN` (`turso.fleet-platform-rw-token`), `TURSO_QUEUE_TOKEN` -(`turso.fleet-queue-rw-token`). +(`turso.fleet-queue-rw-token`), `EVENTS_BROKER_TOKEN` (the queue +broker's own agent token — the value in +`~/.config/queue-broker/token` on the laptop), and +`EVENTS_BROKER_ACCESS_CLIENT_ID` / `EVENTS_BROKER_ACCESS_CLIENT_SECRET` +(the #6925 Access service token, vaulted in BWS as +`cloudflare.access.queue-broker-client-id` / +`cloudflare.access.queue-broker-client-secret`). ## Run diff --git a/pkgs/github-ci/worker/src/sink.ts b/pkgs/github-ci/worker/src/sink.ts index 085412709a..07ae4a4c55 100644 --- a/pkgs/github-ci/worker/src/sink.ts +++ b/pkgs/github-ci/worker/src/sink.ts @@ -4,15 +4,23 @@ * * Sink route: when EVENTS_BROKER_URL is configured, events are * POSTed to the queue broker's /events-append verb — the sink's - * public face. The production broker today listens on the tailnet - * only, which a Cloudflare Worker cannot reach, so until a public - * broker route exists the Worker performs the sink write itself: - * the exact INSERT INTO events statement that the broker's - * /events-append verb executes (turso_queue.rs verb_events_append) - * and that the Edge Function's AGENT_EVENTS_SINK=turso path used. - * One sink, one table, one row shape (worker_id 'github-webhook', - * task_id NULL, whole-second Z timestamps) — never a third - * implementation with its own semantics. + * public face. Since #6925 the broker has a public route: + * https://queue-broker.security.cab (Cloudflare Tunnel ingress on + * the pm-stack tunnel -> the broker's tailnet listener), gated by + * a Cloudflare Access application that admits only this Worker's + * service token (CF-Access-Client-Id / CF-Access-Client-Secret), + * and by the broker's own X-Agent-Token check behind it. The + * broker authenticates X-Agent-Token only — it does not read + * Authorization: Bearer — so the token rides that header. + * + * When EVENTS_BROKER_URL is unset, the Worker performs the sink + * write itself as the outage fallback: the exact INSERT INTO + * events statement that the broker's /events-append verb executes + * (turso_queue.rs verb_events_append) and that the Edge Function's + * AGENT_EVENTS_SINK=turso path used. One sink, one table, one row + * shape (worker_id 'github-webhook', task_id NULL, whole-second Z + * timestamps) — never a third implementation with its own + * semantics. */ import { Turso, tArg } from "./turso"; @@ -21,6 +29,8 @@ export interface SinkEnv { TURSO_QUEUE_TOKEN: string; EVENTS_BROKER_URL?: string; EVENTS_BROKER_TOKEN?: string; + EVENTS_BROKER_ACCESS_CLIENT_ID?: string; + EVENTS_BROKER_ACCESS_CLIENT_SECRET?: string; } /** Queue-backend timestamp format: whole seconds, Z (matches EF). */ @@ -39,7 +49,13 @@ export async function appendAgentEvent( headers: { "Content-Type": "application/json", ...(env.EVENTS_BROKER_TOKEN - ? { Authorization: `Bearer ${env.EVENTS_BROKER_TOKEN}` } + ? { "X-Agent-Token": env.EVENTS_BROKER_TOKEN } + : {}), + ...(env.EVENTS_BROKER_ACCESS_CLIENT_ID && env.EVENTS_BROKER_ACCESS_CLIENT_SECRET + ? { + "CF-Access-Client-Id": env.EVENTS_BROKER_ACCESS_CLIENT_ID, + "CF-Access-Client-Secret": env.EVENTS_BROKER_ACCESS_CLIENT_SECRET, + } : {}), }, body: JSON.stringify({ worker_id: "github-webhook", kind, detail }), diff --git a/pkgs/github-ci/worker/test/sink.test.ts b/pkgs/github-ci/worker/test/sink.test.ts new file mode 100644 index 0000000000..ee18ff75a3 --- /dev/null +++ b/pkgs/github-ci/worker/test/sink.test.ts @@ -0,0 +1,63 @@ +import { afterEach, describe, expect, it, vi } from "vitest"; +import { appendAgentEvent, type SinkEnv } from "../src/sink"; + +const brokerEnv: SinkEnv = { + TURSO_QUEUE_URL: "https://fleet-queue.example.invalid", + TURSO_QUEUE_TOKEN: "queue-token", + EVENTS_BROKER_URL: "https://queue-broker.security.cab/", + EVENTS_BROKER_TOKEN: "broker-token", + EVENTS_BROKER_ACCESS_CLIENT_ID: "access-id", + EVENTS_BROKER_ACCESS_CLIENT_SECRET: "access-secret", +}; + +afterEach(() => vi.unstubAllGlobals()); + +describe("appendAgentEvent broker route (#6925)", () => { + it("POSTs /events-append with X-Agent-Token and CF-Access headers", async () => { + const calls: { url: string; init: RequestInit }[] = []; + vi.stubGlobal("fetch", async (url: string, init: RequestInit) => { + calls.push({ url, init }); + return new Response(JSON.stringify({ ok: true, row: { id: 7, kind: "push" } }), { + status: 200, + headers: { "Content-Type": "application/json" }, + }); + }); + await appendAgentEvent(brokerEnv, "push", { repo: "developing-today/code" }); + expect(calls).toHaveLength(1); + expect(calls[0].url).toBe("https://queue-broker.security.cab/events-append"); + const headers = calls[0].init.headers as Record; + expect(headers["X-Agent-Token"]).toBe("broker-token"); + expect(headers["CF-Access-Client-Id"]).toBe("access-id"); + expect(headers["CF-Access-Client-Secret"]).toBe("access-secret"); + expect(headers["Authorization"]).toBeUndefined(); + expect(JSON.parse(String(calls[0].init.body))).toEqual({ + worker_id: "github-webhook", + kind: "push", + detail: { repo: "developing-today/code" }, + }); + }); + + it("omits auth headers when the broker secrets are unset", async () => { + const calls: { init: RequestInit }[] = []; + vi.stubGlobal("fetch", async (_url: string, init: RequestInit) => { + calls.push({ init }); + return new Response(JSON.stringify({ ok: true }), { status: 200 }); + }); + const env: SinkEnv = { ...brokerEnv, EVENTS_BROKER_TOKEN: undefined, EVENTS_BROKER_ACCESS_CLIENT_ID: undefined, EVENTS_BROKER_ACCESS_CLIENT_SECRET: undefined }; + await appendAgentEvent(env, "issues", {}); + const headers = calls[0].init.headers as Record; + expect(headers["X-Agent-Token"]).toBeUndefined(); + expect(headers["CF-Access-Client-Id"]).toBeUndefined(); + expect(headers["CF-Access-Client-Secret"]).toBeUndefined(); + }); + + it("throws when the broker answers not-ok", async () => { + vi.stubGlobal("fetch", async () => new Response(JSON.stringify({ ok: false, error: "nope" }), { status: 200 })); + await expect(appendAgentEvent(brokerEnv, "push", {})).rejects.toThrow("broker returned not-ok"); + }); + + it("throws on a non-2xx broker response", async () => { + vi.stubGlobal("fetch", async () => new Response("unauthorized", { status: 401 })); + await expect(appendAgentEvent(brokerEnv, "push", {})).rejects.toThrow("HTTP 401"); + }); +}); diff --git a/pkgs/github-ci/worker/vitest.config.ts b/pkgs/github-ci/worker/vitest.config.ts index 4267c344b8..e4106b40ca 100644 --- a/pkgs/github-ci/worker/vitest.config.ts +++ b/pkgs/github-ci/worker/vitest.config.ts @@ -10,6 +10,7 @@ export default defineWorkersConfig({ GITHUB_WEBHOOK_SECRET: "test-webhook-secret", TURSO_PLATFORM_TOKEN: "test-platform-token", TURSO_QUEUE_TOKEN: "test-queue-token", + EVENTS_BROKER_URL: "", }, }, }, diff --git a/pkgs/github-ci/worker/wrangler.toml b/pkgs/github-ci/worker/wrangler.toml index 7c27bf2aed..89734a5768 100644 --- a/pkgs/github-ci/worker/wrangler.toml +++ b/pkgs/github-ci/worker/wrangler.toml @@ -11,6 +11,14 @@ # TURSO_PLATFORM_TOKEN turso.fleet-platform-rw-token (ci_events) # TURSO_QUEUE_TOKEN turso.fleet-queue-rw-token (agent events, # snapshots, watermarks) +# EVENTS_BROKER_TOKEN the queue broker's own agent token (the +# value in ~/.config/queue-broker/token on +# the laptop); sent as X-Agent-Token. +# EVENTS_BROKER_ACCESS_CLIENT_ID / EVENTS_BROKER_ACCESS_CLIENT_SECRET +# Cloudflare Access service token for +# queue-broker.security.cab (#6925), vaulted +# in BWS as cloudflare.access.queue-broker- +# client-id / -client-secret. name = "github-ci" main = "src/index.ts" compatibility_date = "2025-09-15" @@ -19,9 +27,11 @@ workers_dev = true [vars] TURSO_PLATFORM_URL = "https://fleet-platform-dezren39.aws-us-east-1.turso.io" TURSO_QUEUE_URL = "https://fleet-queue-dezren39.aws-us-east-1.turso.io" -# EVENTS_BROKER_URL is intentionally unset: the queue broker -# (/events-append, #5690) listens on the tailnet only, which a Worker -# cannot reach. When a public broker route exists, set this var and -# the Worker POSTs there instead of writing the sink table directly; -# until then it performs the sink write itself (the exact INSERT the -# broker's /events-append verb and the Edge Function's turso sink do). +EVENTS_BROKER_URL = "https://queue-broker.security.cab" +# EVENTS_BROKER_URL (#6925): the queue broker's public route — +# Cloudflare Tunnel ingress on the pm-stack tunnel to the broker's +# tailnet listener, gated by a Cloudflare Access service-token-only +# application plus the broker's X-Agent-Token check. With this set, +# the Worker POSTs /events-append to the broker instead of writing +# the sink table itself; the direct INSERT in sink.ts remains the +# outage fallback (unset this var to revert to it).