Diffinery

codician/diffinerypublic

Open-source, agent-first GitHub and GitHub Actions alternative that runs on your own Cloudflare account: git hosting, pull requests, code review and drop-in GitHub Actions YAML on Workers, Durable Objects, R2 and Containers.

241 lines · 15.7 KB · executableRaw History
1#!/usr/bin/env bash
2# Idempotent production setup for Diffinery on a Cloudflare account.
3#
4# Creates (only when missing): the D1 database, R2 bucket, event and mail queues, the
5# subscription that sends Email Sending's bounces and complaints to the diffinery-mail-events
6# queue, the Email Sending setup for the domain (scripts/email-sending.sh), the Turnstile
7# widget for the security report form (scripts/turnstile.sh) and the
8# Cloudflare Access application for /auth/access, the email-based account
9# recovery path. Everyday sign-in uses passkeys; git, the API, MCP and runners
10# use Diffinery tokens.
11# Then renders apps/diffinery/wrangler.deploy.jsonc and applies D1 migrations.
12#
13# Requires: CLOUDFLARE_API_TOKEN, CLOUDFLARE_ACCOUNT_ID, node, jq, curl.
14# Optional: DIFFINERY_DOMAIN (default diffinery.com; the Worker's route, PUBLIC_URL
15# and sender follow it), DIFFINERY_ZONE (the Cloudflare zone holding that host,
16# default its last two labels), MAIL_DOMAIN (default DIFFINERY_DOMAIN),
17# ACCESS_TEAM_DOMAIN (e.g. example.cloudflareaccess.com) when the token cannot read it,
18# BOOTSTRAP_ADMINS (comma-separated emails that become site administrators on first sign-in;
19# when unset, the deployed Worker keeps the list it has: see scripts/bootstrap-admins.sh).
20set -euo pipefail
21
22: "${CLOUDFLARE_API_TOKEN:?set CLOUDFLARE_API_TOKEN}"
23: "${CLOUDFLARE_ACCOUNT_ID:?set CLOUDFLARE_ACCOUNT_ID}"
24DOMAIN="${DIFFINERY_DOMAIN:-diffinery.com}"
25ROOT="$(cd "$(dirname "$0")/.." && pwd)"
26APP="$ROOT/apps/diffinery"
27WRANGLER=(pnpm --dir "$APP" exec wrangler)
28API="https://api.cloudflare.com/client/v4/accounts/$CLOUDFLARE_ACCOUNT_ID"
29
30cf() { # cf METHOD PATH [JSON]
31 local method="$1" path="$2" body="${3:-}"
32 if [[ -n "$body" ]]; then
33 curl -sS -X "$method" "$API$path" -H "Authorization: Bearer $CLOUDFLARE_API_TOKEN" -H "Content-Type: application/json" --data "$body"
34 else
35 curl -sS -X "$method" "$API$path" -H "Authorization: Bearer $CLOUDFLARE_API_TOKEN"
36 fi
37}
38log() { echo "::group::$*"; }
39endlog() { echo "::endgroup::"; }
40warn() { echo "::warning::$*" >&2; }
41
42# ------------------------------------------------------------------ D1
43log "D1 database"
44D1_ID="$(cf GET "/d1/database?name=diffinery&per_page=100" | jq -r '.result[]? | select(.name=="diffinery") | .uuid' | head -n1)"
45if [[ -z "$D1_ID" ]]; then
46 D1_ID="$(cf POST /d1/database '{"name":"diffinery"}' | jq -r '.result.uuid // empty')"
47 [[ -n "$D1_ID" ]] || { echo "could not create D1 database (does the token have D1: Edit?)" >&2; exit 1; }
48 echo "created D1 database $D1_ID"
49else
50 echo "D1 database exists: $D1_ID"
51fi
52echo "read replication: $(cf GET "/d1/database/$D1_ID" | jq -r '.result.read_replication.mode // "unknown"') (turn it on with the 'D1 read replication' workflow)"
53endlog
54
55# ------------------------------------------------------------------ R2
56log "R2 bucket"
57if cf GET "/r2/buckets/diffinery-data" | jq -e '.success' >/dev/null; then
58 echo "R2 bucket exists"
59else
60 cf POST /r2/buckets '{"name":"diffinery-data"}' | jq -e '.success' >/dev/null || { echo "could not create R2 bucket (R2 enabled? token has R2: Edit?)" >&2; exit 1; }
61 echo "created R2 bucket diffinery-data"
62fi
63# Lifecycle rules (layout: apps/diffinery/src/lib/storage-keys.ts). Only transient prefixes
64# age out; repos/ (git objects, LFS, release assets), registry/ and actions-cache/ (evicted
65# by Diffinery) have no age rule. Multipart uploads left unfinished are aborted after a day.
66LIFECYCLE='{"rules":[
67 {"id":"abort-multipart-1d","enabled":true,"conditions":{"prefix":""},"abortMultipartUploadsTransition":{"condition":{"type":"Age","maxAge":86400}}},
68 {"id":"artifacts-90d","enabled":true,"conditions":{"prefix":"artifacts/"},"deleteObjectsTransition":{"condition":{"type":"Age","maxAge":7776000}}},
69 {"id":"logs-90d","enabled":true,"conditions":{"prefix":"logs/"},"deleteObjectsTransition":{"condition":{"type":"Age","maxAge":7776000}}},
70 {"id":"actions-staging-1d","enabled":true,"conditions":{"prefix":"actions-staging/"},"deleteObjectsTransition":{"condition":{"type":"Age","maxAge":86400}}},
71 {"id":"archives-30d","enabled":true,"conditions":{"prefix":"archives/"},"deleteObjectsTransition":{"condition":{"type":"Age","maxAge":2592000}}},
72 {"id":"exports-8d","enabled":true,"conditions":{"prefix":"exports/"},"deleteObjectsTransition":{"condition":{"type":"Age","maxAge":691200}}},
73 {"id":"events-400d","enabled":true,"conditions":{"prefix":"events/"},"deleteObjectsTransition":{"condition":{"type":"Age","maxAge":34560000}}}
74]}'
75# Applied on every deploy; a failure stops it, since a wrong rule can delete data.
76RESULT="$(cf PUT "/r2/buckets/diffinery-data/lifecycle" "$LIFECYCLE")"
77if ! jq -e '.success' >/dev/null <<<"$RESULT"; then
78 echo "could not set the R2 lifecycle rules (token has R2: Edit?): $(jq -c '.errors // .' <<<"$RESULT" 2>/dev/null || echo "$RESULT")" >&2
79 exit 1
80fi
81CURRENT="$(cf GET "/r2/buckets/diffinery-data/lifecycle")"
82if jq -e '[.result.rules[]? | select(.deleteObjectsTransition != null and ((.conditions.prefix // "") | startswith("repos/") or . == ""))] | length > 0' >/dev/null <<<"$CURRENT"; then
83 echo "an R2 lifecycle rule still deletes objects under repos/ (git objects, LFS); refusing to continue" >&2
84 exit 1
85fi
86echo "R2 lifecycle rules set"
87endlog
88
89# ------------------------------------------------------------------ Queues
90log "Queues"
91EXISTING_QUEUES="$(cf GET "/queues?per_page=100" | jq -r '.result[]?.queue_name')"
92for q in diffinery-events diffinery-events-dlq diffinery-mail diffinery-mail-dlq diffinery-mail-events; do
93 if grep -qx "$q" <<<"$EXISTING_QUEUES"; then
94 echo "queue $q exists"
95 else
96 cf POST /queues "{\"queue_name\":\"$q\"}" | jq -e '.success' >/dev/null || { echo "could not create queue $q (token has Queues: Edit?)" >&2; exit 1; }
97 echo "created queue $q"
98 fi
99done
100endlog
101
102# ------------------------------------------------------------------ Email Sending events
103# Bounces and complaints reach the Worker through a Queues event subscription: source email.sending
104# (one per sending domain), events message.bounced and message.complained, destination the
105# diffinery-mail-events queue. Created only when missing; one that lacks an event gets it added.
106# Create: POST /accounts/{account_id}/event_subscriptions/subscriptions
107# https://developers.cloudflare.com/api/resources/queues/subresources/subscriptions/methods/create/
108# List: GET /accounts/{account_id}/event_subscriptions/subscriptions?queue_id=...
109# Update: PATCH /accounts/{account_id}/event_subscriptions/subscriptions/{id}
110# Source and event names: https://developers.cloudflare.com/queues/event-subscriptions/events-schemas/
111# and https://developers.cloudflare.com/email-service/platform/event-subscriptions/. The email.sending
112# source takes zone_id and domain, as `wrangler queues subscription create --source email.sending
113# --zone-id ... --domain ...` sends them (https://developers.cloudflare.com/workers/wrangler/commands/queues/).
114# The token needs Account → Queues → Edit (the API reference's "Queues Write") for this, and
115# Zone → Zone → Read on the sending domain to find its zone. Without them this warns and the deploy
116# goes on: bounces and complaints then don't reach the suppression list until someone subscribes the
117# queue in the dashboard (docs/admin/site-administration.md).
118log "Email Sending event subscription"
119SEND_DOMAIN="${MAIL_DOMAIN:-$DOMAIN}"
120SUB_EVENTS='["message.bounced","message.complained"]'
121SUB_PERMISSION="Account → Queues → Edit (\"Queues Write\" in the API reference)"
122api_errors() { jq -r '[.errors[]? | "\(.code): \(.message)"] | join("; ")' <<<"$1" 2>/dev/null || echo "$1"; }
123subscribe_mail_events() {
124 local qid zone="" name="$SEND_DOMAIN" list existing id body res
125 qid="$(cf GET "/queues?per_page=100" | jq -r '.result[]? | select(.queue_name=="diffinery-mail-events") | .queue_id' | head -n1 || true)"
126 if [[ -z "$qid" ]]; then warn "Could not find the diffinery-mail-events queue, so it isn't subscribed to Email Sending's bounce and complaint events."; return; fi
127 # The zone that holds the sending domain (the domain itself or a parent), as scripts/email-sending.sh finds it.
128 while [[ "$name" == *.* ]]; do
129 zone="$(curl -sS "https://api.cloudflare.com/client/v4/zones?name=$name" -H "Authorization: Bearer $CLOUDFLARE_API_TOKEN" | jq -r '.result[0].id // empty' || true)"
130 [[ -n "$zone" ]] && break
131 name="${name#*.}"
132 done
133 if [[ -z "$zone" ]]; then warn "Could not find the Cloudflare zone for $SEND_DOMAIN (the token needs Zone → Zone → Read on it), so diffinery-mail-events isn't subscribed to Email Sending's bounce and complaint events. Subscribe it in the dashboard: see Site administration → Email in the docs."; return; fi
134 list="$(cf GET "/event_subscriptions/subscriptions?queue_id=$qid&per_page=100" || true)"
135 if ! jq -e '.success' >/dev/null 2>&1 <<<"$list"; then
136 warn "Could not read the queue's event subscriptions, so diffinery-mail-events may not get bounces and complaints. The token needs $SUB_PERMISSION. Cloudflare said: $(api_errors "$list")"
137 return
138 fi
139 existing="$(jq -c --arg d "$SEND_DOMAIN" --arg q "$qid" '[.result[]? | select(.source.type == "email.sending" and .source.domain == $d and (.destination.queue_id // $q) == $q)][0] // empty' <<<"$list")"
140 if [[ -n "$existing" ]]; then
141 id="$(jq -r '.id' <<<"$existing")"
142 if jq -e --argjson want "$SUB_EVENTS" '.enabled != false and ((.events // []) as $have | all($want[]; . as $e | $have | index($e)))' >/dev/null <<<"$existing"; then
143 echo "event subscription $id sends $SEND_DOMAIN's bounces and complaints to diffinery-mail-events"
144 return
145 fi
146 body="$(jq -c --argjson want "$SUB_EVENTS" '{events: ((.events // []) + $want | unique), enabled: true}' <<<"$existing")"
147 res="$(cf PATCH "/event_subscriptions/subscriptions/$id" "$body" || true)"
148 if jq -e '.success' >/dev/null 2>&1 <<<"$res"; then echo "event subscription $id now includes bounced and complained events"; else warn "Could not update event subscription $id. The token needs $SUB_PERMISSION. Cloudflare said: $(api_errors "$res")"; fi
149 return
150 fi
151 body="$(jq -nc --arg d "$SEND_DOMAIN" --arg z "$zone" --arg q "$qid" --argjson events "$SUB_EVENTS" '{
152 name: ("diffinery-mail-events " + $d), enabled: true,
153 source: {type: "email.sending", zone_id: $z, domain: $d},
154 destination: {type: "queues.queue", queue_id: $q},
155 events: $events
156 }')"
157 res="$(cf POST /event_subscriptions/subscriptions "$body" || true)"
158 if jq -e '.success' >/dev/null 2>&1 <<<"$res"; then
159 echo "created event subscription $(jq -r '.result.id' <<<"$res"): $SEND_DOMAIN's bounces and complaints go to diffinery-mail-events"
160 else
161 warn "Could not subscribe diffinery-mail-events to Email Sending's bounce and complaint events for $SEND_DOMAIN. The token needs $SUB_PERMISSION (and $SEND_DOMAIN onboarded to Email Sending). Cloudflare said: $(api_errors "$res")"
162 fi
163}
164subscribe_mail_events
165endlog
166
167# ------------------------------------------------------------------ Email Sending
168# notifications@<MAIL_DOMAIN or DIFFINERY_DOMAIN>; warns (and email is skipped) until the domain is onboarded.
169DIFFINERY_DOMAIN="$DOMAIN" "$ROOT/scripts/email-sending.sh" || warn "Email Sending check failed; email may not be delivered."
170
171# ------------------------------------------------------------------ Turnstile
172# The widget "diffinery <DIFFINERY_DOMAIN>", created when missing, so people who aren't signed in can
173# send security reports (security.txt points researchers at the form). Its site key is deployed as the
174# TURNSTILE_SITE_KEY var (render-config below); ensure-secrets.sh sets TURNSTILE_SECRET when it's missing,
175# and turnstile.sh replaces it here, before the deploy, when it goes with another widget. Needs
176# Account → Turnstile → Edit; without it turnstile.sh warns, naming it, and the deploy goes on with
177# anonymous reports refused. Sign-up stays as SIGNUP says: keys alone don't open it.
178log "Turnstile"
179TURNSTILE_SITE_KEY="$(DIFFINERY_DOMAIN="$DOMAIN" "$ROOT/scripts/turnstile.sh")" || { warn "scripts/turnstile.sh failed; anonymous security reports stay refused."; TURNSTILE_SITE_KEY=""; }
180echo "site key: ${TURNSTILE_SITE_KEY:-none}"
181endlog
182
183# ------------------------------------------------------------------ Access
184log "Cloudflare Access"
185TEAM="${ACCESS_TEAM_DOMAIN:-}"
186if [[ -z "$TEAM" ]]; then
187 TEAM="$(cf GET /access/organizations | jq -r '.result.auth_domain // empty' || true)"
188fi
189APPS="$(cf GET "/access/apps?per_page=100" || echo '{}')"
190AUD=""
191if ! jq -e '.success' >/dev/null <<<"$APPS"; then
192 warn "Could not read Access applications (token needs Access: Apps and Policies Edit). Passkeys work; email recovery stays off."
193else
194 # People sign in with passkeys. Access guards only /auth/access, the email-based
195 # recovery path, so everything else (web, git, API, MCP, runners) reaches the Worker directly.
196 WEB_BODY="$(jq -nc --arg d "$DOMAIN" '{
197 name: "Diffinery account recovery", type: "self_hosted", domain: ($d + "/auth/access"),
198 destinations: [{type: "public", uri: ($d + "/auth/access")}],
199 session_duration: "15m", app_launcher_visible: false, http_only_cookie_attribute: true, same_site_cookie_attribute: "lax"
200 }')"
201 POLICY='[{"name": "Anyone who proves their email (Diffinery decides access)", "decision": "allow", "include": [{"everyone": {}}], "precedence": 1}]'
202 WEB_ID="$(jq -r '.result[] | select(.name=="Diffinery account recovery" or .name=="Diffinery") | .id' <<<"$APPS" | head -n1)"
203 if [[ -z "$WEB_ID" ]]; then
204 WEB_APP="$(cf POST /access/apps "$(jq -c --argjson p "$POLICY" '. + {policies: $p}' <<<"$WEB_BODY")" | jq -c '.result // empty')"
205 [[ -n "$WEB_APP" ]] && echo "created Access app for $DOMAIN/auth/access" || warn "Could not create the Access application; email recovery stays off."
206 else
207 WEB_APP="$(cf PUT "/access/apps/$WEB_ID" "$WEB_BODY" | jq -c '.result // empty')"
208 [[ -n "$WEB_APP" ]] && echo "Access app covers $DOMAIN/auth/access" || warn "Could not update the Access application."
209 fi
210 AUD="$(jq -r '.aud // empty' <<<"${WEB_APP:-null}")"
211
212 # Earlier versions put Access in front of the whole site and needed a bypass app; remove it.
213 BYPASS_ID="$(jq -r '.result[] | select(.name=="Diffinery machine endpoints") | .id' <<<"$APPS" | head -n1)"
214 if [[ -n "$BYPASS_ID" ]]; then
215 cf DELETE "/access/apps/$BYPASS_ID" | jq -e '.success' >/dev/null && echo "removed the old Access bypass app" || warn "Could not remove the old Access bypass application."
216 fi
217fi
218if [[ -z "$TEAM" ]]; then
219 # Fall back to the sign-in redirect, which names the team domain.
220 TEAM="$(curl -sS -o /dev/null -w '%{redirect_url}' "https://$DOMAIN/auth/access" | sed -nE 's#^https://([^/]+\.cloudflareaccess\.com)/.*#\1#p' || true)"
221fi
222[[ -n "$TEAM" ]] || warn "Access team domain unknown; set the ACCESS_TEAM_DOMAIN repository variable (Zero Trust → Settings → Custom pages shows it)."
223echo "team=${TEAM:-?} aud=${AUD:+set}"
224endlog
225
226# ------------------------------------------------------------------ Site administrators
227# BOOTSTRAP_ADMINS from the environment, else wrangler.jsonc, else what the deployed Worker has; it
228# stops the deploy if the Worker's list can't be read, rather than deploy an empty one in its place.
229log "Bootstrap administrators"
230ADMINS="$("$ROOT/scripts/bootstrap-admins.sh")"
231endlog
232
233# ------------------------------------------------------------------ config + migrations
234log "Render config and migrate D1"
235TURNSTILE_SITE_KEY="$TURNSTILE_SITE_KEY" node "$ROOT/scripts/render-config.mjs" --d1 "$D1_ID" ${TEAM:+--team "$TEAM"} ${AUD:+--aud "$AUD"} --public-url "https://$DOMAIN" ${DIFFINERY_ZONE:+--zone "$DIFFINERY_ZONE"} --mail-from "notifications@${MAIL_DOMAIN:-$DOMAIN}" --admins "$ADMINS"
236"${WRANGLER[@]}" d1 migrations apply diffinery --remote -c wrangler.deploy.jsonc
237endlog
238
239if [[ -n "${GITHUB_OUTPUT:-}" ]]; then
240 { echo "d1_id=$D1_ID"; echo "access_team=$TEAM"; echo "access_aud_set=${AUD:+true}"; } >>"$GITHUB_OUTPUT"
241fi