Homeflare: deploy with setup.sh against a Cloudflare-compatible API
The scripts take the API and the container registry from the environment (CLOUDFLARE_API_BASE_URL, CLOUDFLARE_CONTAINER_REGISTRY), with Cloudflare's as the defaults, so the same setup.sh deploys to Homeflare. On an API that isn't Cloudflare's (ON_CLOUDFLARE=0), setup.sh and bootstrap.sh skip the steps that need Cloudflare itself: the zone check, R2 lifecycle rules, Email Sending setup and its event subscription, creating the Turnstile widget, and Access. The Turnstile keys come from TURNSTILE_SITE_KEY and TURNSTILE_SECRET instead (ensure-secrets.sh puts the secret), render-config.mjs makes the route a custom domain, and the live-site checks say what to look at on Homeflare. HOMEFLARE.md describes the setup; 0001-homeflare.patch is the same change as a patch, for applying to a fresh checkout of upstream. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Jason Schwarzenberger authored 1 parent bbfcbd5commit 05b48e2d149d947f8482feb55def4f85d83d42e8Browse files
11 files changed+482−24
Files (11)
added0001-homeflare.patch+363−0View file
| @@ -1,0 +1,363 @@ | ||
| 1 | +diff --git a/HOMEFLARE.md b/HOMEFLARE.md | |
| 2 | +new file mode 100644 | |
| 3 | +index 0000000..9ea4afd | |
| 4 | +--- /dev/null | |
| 5 | ++++ b/HOMEFLARE.md | |
| 6 | +@@ -0,0 +1,47 @@ | |
| 7 | ++# Diffinery on Homeflare | |
| 8 | ++ | |
| 9 | ++[Homeflare](https://github.com/jason-s13r/homeflare) runs Workers on your own machines (celld) behind a | |
| 10 | ++Cloudflare-compatible API. These scripts deploy to it when `CLOUDFLARE_API_BASE_URL` points at that API, | |
| 11 | ++which `homeflare-wrangler run` sets, along with `CLOUDFLARE_CONTAINER_REGISTRY` (Homeflare's registry) and a | |
| 12 | ++`docker` command on your Podman. | |
| 13 | ++ | |
| 14 | ++On another API than Cloudflare's, `scripts/setup.sh` and `scripts/bootstrap.sh` skip what needs Cloudflare | |
| 15 | ++itself: | |
| 16 | ++ | |
| 17 | ++| Skipped | What it means | | |
| 18 | ++| --- | --- | | |
| 19 | ++| Zone check | The hostname is a Homeflare custom domain, not a route in a Cloudflare zone. | | |
| 20 | ++| R2 lifecycle rules | Logs, artifacts, archives and exports don't age out by themselves. | | |
| 21 | ++| Email Sending setup and its event subscription | Onboard the sending domain in Cloudflare yourself (Homeflare relays email through it with `HOMEFLARE_EMAIL=cloudflare`). Bounces and complaints don't reach the suppression list. | | |
| 22 | ++| Turnstile widget | Make one in the Cloudflare dashboard and pass its keys: `TURNSTILE_SITE_KEY` and `TURNSTILE_SECRET`. Without them open sign-up and anonymous security reports stay off. | | |
| 23 | ++| Cloudflare Access | Email recovery and the `/auth/access` first sign-in stay off. Passkeys work. | | |
| 24 | ++ | |
| 25 | ++## Deploy | |
| 26 | ++ | |
| 27 | ++Put the settings in a file outside the repository (it holds the Turnstile secret), for example | |
| 28 | ++`~/.config/homeflare/diffinery.env`, readable by you only: | |
| 29 | ++ | |
| 30 | ++```sh | |
| 31 | ++CLOUDFLARE_ACCOUNT_ID=<Homeflare's account id> | |
| 32 | ++DIFFINERY_DOMAIN=git.homeflare.example.com | |
| 33 | ++MAIL_DOMAIN=example.com | |
| 34 | +[email protected] | |
| 35 | ++TURNSTILE_SITE_KEY=0x4AAA... | |
| 36 | ++TURNSTILE_SECRET=0x4AAA... | |
| 37 | ++``` | |
| 38 | ++ | |
| 39 | ++`MAIL_DOMAIN` is a domain onboarded to Email Sending in your Cloudflare account. Homeflare's account id is | |
| 40 | ++in the Dashboard, or from `GET /client/v4/accounts`. Then, from the repository: | |
| 41 | ++ | |
| 42 | ++```sh | |
| 43 | ++homeflare-wrangler --env-file ~/.config/homeflare/diffinery.env run scripts/setup.sh --yes | |
| 44 | ++``` | |
| 45 | ++ | |
| 46 | ++`homeflare-wrangler` reads the Homeflare API token from `~/.config/homeflare/wrangler.env` and sets | |
| 47 | ++`CLOUDFLARE_API_BASE_URL` itself. Run it again to update. | |
| 48 | ++ | |
| 49 | ++## The first administrator | |
| 50 | ++ | |
| 51 | ++Without Access, the first account comes from open sign-up, which needs Turnstile and email. Deploy once with | |
| 52 | ++`"SIGNUP": "open"` under `vars` in `apps/diffinery/wrangler.jsonc`, sign up at `https://<host>/signup` with an | |
| 53 | ++address in `BOOTSTRAP_ADMINS`, add a passkey, then remove `SIGNUP` and deploy again. | |
| 54 | +diff --git a/scripts/bootstrap-admins.sh b/scripts/bootstrap-admins.sh | |
| 55 | +index 989078a..a6a54d9 100755 | |
| 56 | +--- a/scripts/bootstrap-admins.sh | |
| 57 | ++++ b/scripts/bootstrap-admins.sh | |
| 58 | +@@ -59,7 +59,7 @@ FROM_FILE="$(trimmed "$FROM_FILE")" | |
| 59 | + if [[ -z "${CLOUDFLARE_API_TOKEN:-}" || -z "${CLOUDFLARE_ACCOUNT_ID:-}" ]]; then | |
| 60 | + none "it isn't in the environment or wrangler.jsonc, and without CLOUDFLARE_API_TOKEN and CLOUDFLARE_ACCOUNT_ID the deployed Worker's list can't be read" | |
| 61 | + fi | |
| 62 | +-SETTINGS="$(curl -sS --retry 4 --retry-all-errors --retry-delay 3 "https://api.cloudflare.com/client/v4/accounts/$CLOUDFLARE_ACCOUNT_ID/workers/scripts/$WORKER/settings" -H "Authorization: Bearer $CLOUDFLARE_API_TOKEN" || true)" | |
| 63 | ++SETTINGS="$(curl -sS --retry 4 --retry-all-errors --retry-delay 3 "${CLOUDFLARE_API_BASE_URL:-https://api.cloudflare.com/client/v4}/accounts/$CLOUDFLARE_ACCOUNT_ID/workers/scripts/$WORKER/settings" -H "Authorization: Bearer $CLOUDFLARE_API_TOKEN" || true)" | |
| 64 | + if jq -e '.success == true' >/dev/null 2>&1 <<<"$SETTINGS"; then | |
| 65 | + LIVE="$(jq -r '[.result.bindings[]? | select(.type == "plain_text" and .name == "BOOTSTRAP_ADMINS") | .text][0] // empty' <<<"$SETTINGS")" | |
| 66 | + LIVE="$(trimmed "$LIVE")" | |
| 67 | +diff --git a/scripts/bootstrap.sh b/scripts/bootstrap.sh | |
| 68 | +index aea32f9..29d0c52 100755 | |
| 69 | +--- a/scripts/bootstrap.sh | |
| 70 | ++++ b/scripts/bootstrap.sh | |
| 71 | +@@ -25,7 +25,11 @@ DOMAIN="${DIFFINERY_DOMAIN:-diffinery.com}" | |
| 72 | + ROOT="$(cd "$(dirname "$0")/.." && pwd)" | |
| 73 | + APP="$ROOT/apps/diffinery" | |
| 74 | + WRANGLER=(pnpm --dir "$APP" exec wrangler) | |
| 75 | +-API="https://api.cloudflare.com/client/v4/accounts/$CLOUDFLARE_ACCOUNT_ID" | |
| 76 | ++CF_API="${CLOUDFLARE_API_BASE_URL:-https://api.cloudflare.com/client/v4}" | |
| 77 | ++API="$CF_API/accounts/$CLOUDFLARE_ACCOUNT_ID" | |
| 78 | ++# 0 on another Cloudflare-compatible API (Homeflare): the steps that need Cloudflare itself are skipped. | |
| 79 | ++ON_CLOUDFLARE=1 | |
| 80 | ++[[ "$CF_API" == https://api.cloudflare.com/* ]] || ON_CLOUDFLARE=0 | |
| 81 | + | |
| 82 | + cf() { # cf METHOD PATH [JSON] | |
| 83 | + local method="$1" path="$2" body="${3:-}" | |
| 84 | +@@ -74,6 +78,9 @@ LIFECYCLE='{"rules":[ | |
| 85 | + {"id":"action-mirror-30d","enabled":true,"conditions":{"prefix":"action-mirror/"},"deleteObjectsTransition":{"condition":{"type":"Age","maxAge":2592000}}} | |
| 86 | + ]}' | |
| 87 | + # Applied on every deploy; a failure stops it, since a wrong rule can delete data. | |
| 88 | ++if ((ON_CLOUDFLARE == 0)); then | |
| 89 | ++ warn "R2 lifecycle rules are skipped on $CF_API: transient objects (logs, artifacts, archives, exports) don't age out by themselves." | |
| 90 | ++else | |
| 91 | + RESULT="$(cf PUT "/r2/buckets/diffinery-data/lifecycle" "$LIFECYCLE")" | |
| 92 | + if ! jq -e '.success' >/dev/null <<<"$RESULT"; then | |
| 93 | + echo "could not set the R2 lifecycle rules (token has R2: Edit?): $(jq -c '.errors // .' <<<"$RESULT" 2>/dev/null || echo "$RESULT")" >&2 | |
| 94 | +@@ -85,6 +92,7 @@ if jq -e '[.result.rules[]? | select(.deleteObjectsTransition != null and ((.con | |
| 95 | + exit 1 | |
| 96 | + fi | |
| 97 | + echo "R2 lifecycle rules set" | |
| 98 | ++fi | |
| 99 | + endlog | |
| 100 | + | |
| 101 | + # ------------------------------------------------------------------ Queues | |
| 102 | +@@ -127,7 +135,7 @@ subscribe_mail_events() { | |
| 103 | + 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 | |
| 104 | + # The zone that holds the sending domain (the domain itself or a parent), as scripts/email-sending.sh finds it. | |
| 105 | + while [[ "$name" == *.* ]]; do | |
| 106 | +- 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)" | |
| 107 | ++ zone="$(curl -sS "${CLOUDFLARE_API_BASE_URL:-https://api.cloudflare.com/client/v4}/zones?name=$name" -H "Authorization: Bearer $CLOUDFLARE_API_TOKEN" | jq -r '.result[0].id // empty' || true)" | |
| 108 | + [[ -n "$zone" ]] && break | |
| 109 | + name="${name#*.}" | |
| 110 | + done | |
| 111 | +@@ -162,12 +170,16 @@ subscribe_mail_events() { | |
| 112 | + 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")" | |
| 113 | + fi | |
| 114 | + } | |
| 115 | +-subscribe_mail_events | |
| 116 | ++if ((ON_CLOUDFLARE)); then subscribe_mail_events; else echo "skipped on $CF_API: bounces and complaints don't reach the suppression list by themselves"; fi | |
| 117 | + endlog | |
| 118 | + | |
| 119 | + # ------------------------------------------------------------------ Email Sending | |
| 120 | + # notifications@<MAIL_DOMAIN or DIFFINERY_DOMAIN>; warns (and email is skipped) until the domain is onboarded. | |
| 121 | +-DIFFINERY_DOMAIN="$DOMAIN" "$ROOT/scripts/email-sending.sh" || warn "Email Sending check failed; email may not be delivered." | |
| 122 | ++if ((ON_CLOUDFLARE)); then | |
| 123 | ++ DIFFINERY_DOMAIN="$DOMAIN" "$ROOT/scripts/email-sending.sh" || warn "Email Sending check failed; email may not be delivered." | |
| 124 | ++else | |
| 125 | ++ echo "Email Sending setup skipped on $CF_API: the sending domain is set up wherever that API sends from." | |
| 126 | ++fi | |
| 127 | + | |
| 128 | + # ------------------------------------------------------------------ Turnstile | |
| 129 | + # The widget "diffinery <DIFFINERY_DOMAIN>", created when missing, so people who aren't signed in can | |
| 130 | +@@ -177,19 +189,28 @@ DIFFINERY_DOMAIN="$DOMAIN" "$ROOT/scripts/email-sending.sh" || warn "Email Sendi | |
| 131 | + # Account → Turnstile → Edit; without it turnstile.sh warns, naming it, and the deploy goes on with | |
| 132 | + # anonymous reports refused. Sign-up stays as SIGNUP says: keys alone don't open it. | |
| 133 | + log "Turnstile" | |
| 134 | +-TURNSTILE_SITE_KEY="$(DIFFINERY_DOMAIN="$DOMAIN" "$ROOT/scripts/turnstile.sh")" || { warn "scripts/turnstile.sh failed; anonymous security reports stay refused."; TURNSTILE_SITE_KEY=""; } | |
| 135 | ++if ((ON_CLOUDFLARE)); then | |
| 136 | ++ TURNSTILE_SITE_KEY="$(DIFFINERY_DOMAIN="$DOMAIN" "$ROOT/scripts/turnstile.sh")" || { warn "scripts/turnstile.sh failed; anonymous security reports stay refused."; TURNSTILE_SITE_KEY=""; } | |
| 137 | ++else | |
| 138 | ++ # No Turnstile API here: a widget made in the Cloudflare dashboard, its keys in the environment. | |
| 139 | ++ TURNSTILE_SITE_KEY="${TURNSTILE_SITE_KEY:-}" | |
| 140 | ++ [[ -n "$TURNSTILE_SITE_KEY" ]] || warn "No TURNSTILE_SITE_KEY: open sign-up and anonymous security reports stay off." | |
| 141 | ++fi | |
| 142 | + echo "site key: ${TURNSTILE_SITE_KEY:-none}" | |
| 143 | + endlog | |
| 144 | + | |
| 145 | + # ------------------------------------------------------------------ Access | |
| 146 | + log "Cloudflare Access" | |
| 147 | + TEAM="${ACCESS_TEAM_DOMAIN:-}" | |
| 148 | +-if [[ -z "$TEAM" ]]; then | |
| 149 | ++if [[ -z "$TEAM" ]] && ((ON_CLOUDFLARE)); then | |
| 150 | + TEAM="$(cf GET /access/organizations | jq -r '.result.auth_domain // empty' || true)" | |
| 151 | + fi | |
| 152 | +-APPS="$(cf GET "/access/apps?per_page=100" || echo '{}')" | |
| 153 | + AUD="" | |
| 154 | +-if ! jq -e '.success' >/dev/null <<<"$APPS"; then | |
| 155 | ++APPS='{}' | |
| 156 | ++((ON_CLOUDFLARE == 0)) || APPS="$(cf GET "/access/apps?per_page=100" || echo '{}')" | |
| 157 | ++if ((ON_CLOUDFLARE == 0)); then | |
| 158 | ++ echo "Access skipped on $CF_API: passkeys work; email recovery and the /auth/access first sign-in stay off." | |
| 159 | ++elif ! jq -e '.success' >/dev/null <<<"$APPS"; then | |
| 160 | + warn "Could not read Access applications (token needs Access: Apps and Policies Edit). Passkeys work; email recovery stays off." | |
| 161 | + else | |
| 162 | + # People sign in with passkeys. Access guards only /auth/access, the email-based | |
| 163 | +@@ -216,11 +237,11 @@ else | |
| 164 | + 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." | |
| 165 | + fi | |
| 166 | + fi | |
| 167 | +-if [[ -z "$TEAM" ]]; then | |
| 168 | ++if [[ -z "$TEAM" ]] && ((ON_CLOUDFLARE)); then | |
| 169 | + # Fall back to the sign-in redirect, which names the team domain. | |
| 170 | + TEAM="$(curl -sS -o /dev/null -w '%{redirect_url}' "https://$DOMAIN/auth/access" | sed -nE 's#^https://([^/]+\.cloudflareaccess\.com)/.*#\1#p' || true)" | |
| 171 | + fi | |
| 172 | +-[[ -n "$TEAM" ]] || warn "Access team domain unknown; set the ACCESS_TEAM_DOMAIN repository variable (Zero Trust → Settings → Custom pages shows it)." | |
| 173 | ++[[ -n "$TEAM" ]] || ((ON_CLOUDFLARE == 0)) || warn "Access team domain unknown; set the ACCESS_TEAM_DOMAIN repository variable (Zero Trust → Settings → Custom pages shows it)." | |
| 174 | + echo "team=${TEAM:-?} aud=${AUD:+set}" | |
| 175 | + endlog | |
| 176 | + | |
| 177 | +diff --git a/scripts/email-sending.sh b/scripts/email-sending.sh | |
| 178 | +index 7e43dad..98dcaf3 100755 | |
| 179 | +--- a/scripts/email-sending.sh | |
| 180 | ++++ b/scripts/email-sending.sh | |
| 181 | +@@ -12,7 +12,7 @@ set -euo pipefail | |
| 182 | + | |
| 183 | + : "${CLOUDFLARE_API_TOKEN:?set CLOUDFLARE_API_TOKEN}" | |
| 184 | + DOMAIN="${MAIL_DOMAIN:-${DIFFINERY_DOMAIN:-diffinery.com}}" | |
| 185 | +-CF="https://api.cloudflare.com/client/v4" | |
| 186 | ++CF="${CLOUDFLARE_API_BASE_URL:-https://api.cloudflare.com/client/v4}" | |
| 187 | + | |
| 188 | + cf() { curl -sS "$CF$1" -H "Authorization: Bearer $CLOUDFLARE_API_TOKEN"; } | |
| 189 | + warn() { echo "::warning::$*" >&2; } | |
| 190 | +diff --git a/scripts/ensure-secrets.sh b/scripts/ensure-secrets.sh | |
| 191 | +index 9900e79..4be2e19 100755 | |
| 192 | +--- a/scripts/ensure-secrets.sh | |
| 193 | ++++ b/scripts/ensure-secrets.sh | |
| 194 | +@@ -36,7 +36,11 @@ fi | |
| 195 | + # Without the Turnstile permission this warns (scripts/turnstile.sh names it) and the deploy goes on: | |
| 196 | + # people who aren't signed in can't send security reports until it's set. | |
| 197 | + SITE_KEY="$(tail -n +2 "$APP/wrangler.deploy.jsonc" | jq -r '.vars.TURNSTILE_SITE_KEY // empty')" | |
| 198 | +-if [[ -n "$SITE_KEY" ]] && ! grep -qx TURNSTILE_SECRET <<<"$EXISTING"; then | |
| 199 | ++if [[ -n "$SITE_KEY" && -n "${TURNSTILE_SECRET:-}" ]] && ! grep -qx TURNSTILE_SECRET <<<"$EXISTING"; then | |
| 200 | ++ # A widget's secret from the environment (an API without Turnstile, such as Homeflare's). | |
| 201 | ++ printf '%s' "$TURNSTILE_SECRET" | "${WRANGLER[@]}" secret put TURNSTILE_SECRET -c wrangler.deploy.jsonc >/dev/null | |
| 202 | ++ echo "set TURNSTILE_SECRET" | |
| 203 | ++elif [[ -n "$SITE_KEY" ]] && ! grep -qx TURNSTILE_SECRET <<<"$EXISTING"; then | |
| 204 | + if "$ROOT/scripts/turnstile.sh" secret "$SITE_KEY" -c wrangler.deploy.jsonc; then | |
| 205 | + echo "set TURNSTILE_SECRET" | |
| 206 | + else | |
| 207 | +diff --git a/scripts/render-config.mjs b/scripts/render-config.mjs | |
| 208 | +index fb7d756..595c9ce 100755 | |
| 209 | +--- a/scripts/render-config.mjs | |
| 210 | ++++ b/scripts/render-config.mjs | |
| 211 | +@@ -34,7 +34,9 @@ if (values["public-url"]) { | |
| 212 | + if (url.protocol !== "https:" || url.pathname !== "/") throw new Error("--public-url must be an https origin, like https://git.example.com"); | |
| 213 | + cfg.vars.PUBLIC_URL = url.origin; | |
| 214 | + const zone = values.zone ?? url.hostname.split(".").slice(-2).join("."); | |
| 215 | +- cfg.routes = [{ pattern: `${url.hostname}/*`, zone_name: zone }]; | |
| 216 | ++ // On another Cloudflare-compatible API (Homeflare), a custom domain: the zone isn't Cloudflare's. | |
| 217 | ++ const onCloudflare = (process.env.CLOUDFLARE_API_BASE_URL ?? "https://api.cloudflare.com/").startsWith("https://api.cloudflare.com/"); | |
| 218 | ++ cfg.routes = onCloudflare ? [{ pattern: `${url.hostname}/*`, zone_name: zone }] : [{ pattern: url.hostname, custom_domain: true }]; | |
| 219 | + cfg.vars.MAIL_FROM = `notifications@${url.hostname}`; | |
| 220 | + } | |
| 221 | + if (values["mail-from"]) cfg.vars.MAIL_FROM = values["mail-from"]; | |
| 222 | +diff --git a/scripts/runner-image.sh b/scripts/runner-image.sh | |
| 223 | +index a121666..753b916 100755 | |
| 224 | +--- a/scripts/runner-image.sh | |
| 225 | ++++ b/scripts/runner-image.sh | |
| 226 | +@@ -20,7 +20,7 @@ tag=$( | |
| 227 | + } | sha256sum | cut -c1-24 | |
| 228 | + ) | |
| 229 | + : "${CLOUDFLARE_ACCOUNT_ID:?CLOUDFLARE_ACCOUNT_ID is required}" | |
| 230 | +-image="registry.cloudflare.com/$CLOUDFLARE_ACCOUNT_ID/$name:$tag" | |
| 231 | ++image="${CLOUDFLARE_CONTAINER_REGISTRY:-registry.cloudflare.com}/$CLOUDFLARE_ACCOUNT_ID/$name:$tag" | |
| 232 | + wrangler() { (cd apps/diffinery && pnpm exec wrangler "$@"); } | |
| 233 | + | |
| 234 | + if ! listed=$(wrangler containers images list --json --filter "(^|/)$name\$" 2>&1); then | |
| 235 | +diff --git a/scripts/setup.sh b/scripts/setup.sh | |
| 236 | +index 7989e5c..8d87fe1 100755 | |
| 237 | +--- a/scripts/setup.sh | |
| 238 | ++++ b/scripts/setup.sh | |
| 239 | +@@ -19,6 +19,12 @@ | |
| 240 | + # GITHUB_APP_ID, GITHUB_APP_SLUG, GITHUB_APP_CLIENT_ID, GITHUB_APP_URL | |
| 241 | + # the GitHub App of GitHub runners (docs/self-hosting/configuration.md#github-runners) | |
| 242 | + # ROLLOUT_WAIT_MINUTES how long to wait for the container rollout (default 10) | |
| 243 | ++# CLOUDFLARE_API_BASE_URL another Cloudflare-compatible API, such as Homeflare's (default Cloudflare's). | |
| 244 | ++# Steps that need Cloudflare itself (the zone check, R2 lifecycle rules, Email | |
| 245 | ++# Sending setup and events, Turnstile and Access) are skipped there; see | |
| 246 | ++# HOMEFLARE.md. | |
| 247 | ++# CLOUDFLARE_CONTAINER_REGISTRY the registry runner images go to (default registry.cloudflare.com) | |
| 248 | ++# TURNSTILE_SITE_KEY, TURNSTILE_SECRET a Turnstile widget's keys, for an API without Turnstile | |
| 249 | + # | |
| 250 | + # Every step is idempotent: it creates only what is missing and keeps your data and secrets, so run | |
| 251 | + # it again to update. Secrets are never printed. Needs Node.js 22+, pnpm, jq, curl, openssl and Docker. | |
| 252 | +@@ -26,8 +32,13 @@ set -euo pipefail | |
| 253 | + | |
| 254 | + ROOT="$(cd "$(dirname "$0")/.." && pwd)" | |
| 255 | + APP="$ROOT/apps/diffinery" | |
| 256 | +-CF_API="https://api.cloudflare.com/client/v4" | |
| 257 | ++CF_API="${CLOUDFLARE_API_BASE_URL:-https://api.cloudflare.com/client/v4}" | |
| 258 | + PLACEHOLDER_D1="00000000-0000-0000-0000-000000000000" | |
| 259 | ++# 1 on Cloudflare's own API; 0 on another Cloudflare-compatible one (Homeflare), which skips the | |
| 260 | ++# steps that need Cloudflare itself. | |
| 261 | ++ON_CLOUDFLARE=1 | |
| 262 | ++[[ "$CF_API" == https://api.cloudflare.com/* ]] || ON_CLOUDFLARE=0 | |
| 263 | ++export ON_CLOUDFLARE | |
| 264 | + DRY_RUN=0 | |
| 265 | + ASK=1 | |
| 266 | + | |
| 267 | +@@ -150,12 +161,16 @@ else | |
| 268 | + fi | |
| 269 | + jq -e '.success == true and .result.status == "active"' >/dev/null 2>&1 <<<"$verify" || die "Cloudflare doesn't accept the API token: $(errors_of "$verify")" | |
| 270 | + info "the token is valid and active" | |
| 271 | ++ if ((ON_CLOUDFLARE == 0)); then | |
| 272 | ++ info "API: $CF_API (not Cloudflare's: the zone check, R2 lifecycle rules, Email Sending setup, Turnstile and Access are skipped)" | |
| 273 | ++ else | |
| 274 | + zones="$(cf_get "/zones?name=$ZONE&account.id=$CLOUDFLARE_ACCOUNT_ID" || true)" | |
| 275 | + jq -e '.success == true' >/dev/null 2>&1 <<<"$zones" || die "couldn't look up the zone $ZONE: $(errors_of "$zones")" | |
| 276 | + zone_status="$(jq -r '.result[0].status // empty' <<<"$zones")" | |
| 277 | + [[ -n "$zone_status" ]] || die "the zone $ZONE isn't in account $CLOUDFLARE_ACCOUNT_ID, or the token can't see it. Add the domain to the account, give the token Zone → Zone → Read on it, or set DIFFINERY_ZONE if the zone that holds $DIFFINERY_DOMAIN is another one." | |
| 278 | + [[ "$zone_status" == active ]] || warn "the zone $ZONE is $zone_status, not active: the route serves nothing until its nameservers point at Cloudflare" | |
| 279 | + info "zone $ZONE is in the account ($zone_status)" | |
| 280 | ++ fi | |
| 281 | + fi | |
| 282 | + | |
| 283 | + cd "$ROOT" | |
| 284 | +@@ -173,7 +188,7 @@ if ((DRY_RUN)); then | |
| 285 | + | |
| 286 | + step "Rendering the deploy config (placeholder D1 id) and bundling the Worker" | |
| 287 | + # A placeholder runner image: the dry run builds no container image (that needs Docker and the registry). | |
| 288 | +- RUNNER_IMAGE="registry.cloudflare.com/${CLOUDFLARE_ACCOUNT_ID:-00000000000000000000000000000000}/diffinery-runner:dry-run" \ | |
| 289 | ++ RUNNER_IMAGE="${CLOUDFLARE_CONTAINER_REGISTRY:-registry.cloudflare.com}/${CLOUDFLARE_ACCOUNT_ID:-00000000000000000000000000000000}/diffinery-runner:dry-run" \ | |
| 290 | + node "$ROOT/scripts/render-config.mjs" --d1 "$PLACEHOLDER_D1" ${ACCESS_TEAM_DOMAIN:+--team "$ACCESS_TEAM_DOMAIN"} \ | |
| 291 | + --public-url "https://$DIFFINERY_DOMAIN" ${DIFFINERY_ZONE:+--zone "$DIFFINERY_ZONE"} --mail-from "$MAIL_FROM" --admins "$ADMINS" | |
| 292 | + OUT="$(mktemp -d "${TMPDIR:-/tmp}/diffinery-dry-run.XXXXXX")" | |
| 293 | +@@ -205,7 +220,7 @@ image_log="$(mktemp "${TMPDIR:-/tmp}/diffinery-image.XXXXXX")" | |
| 294 | + env -u GITHUB_ENV "$ROOT/scripts/runner-image.sh" | tee "$image_log" | |
| 295 | + RUNNER_IMAGE="$(tail -n 1 "$image_log")" | |
| 296 | + rm -f "$image_log" | |
| 297 | +-[[ "$RUNNER_IMAGE" == registry.cloudflare.com/* ]] || die "scripts/runner-image.sh didn't print the runner image" | |
| 298 | ++[[ "$RUNNER_IMAGE" == ${CLOUDFLARE_CONTAINER_REGISTRY:-registry.cloudflare.com}/* ]] || die "scripts/runner-image.sh didn't print the runner image" | |
| 299 | + export RUNNER_IMAGE | |
| 300 | + | |
| 301 | + step "Creating or updating Cloudflare resources (scripts/bootstrap.sh)" | |
| 302 | +@@ -221,7 +236,7 @@ step "Waiting for new containers to get the new image" | |
| 303 | + step "Worker secrets (generated on the first deploy; never printed)" | |
| 304 | + "$ROOT/scripts/ensure-secrets.sh" | |
| 305 | + | |
| 306 | +-if [[ -z "$ACCESS_TEAM" ]]; then | |
| 307 | ++if [[ -z "$ACCESS_TEAM" ]] && ((ON_CLOUDFLARE)); then | |
| 308 | + step "Finishing the Access setup (first deploy)" | |
| 309 | + : >"$OUTPUTS" | |
| 310 | + GITHUB_OUTPUT="$OUTPUTS" "$ROOT/scripts/bootstrap.sh" | |
| 311 | +@@ -237,12 +252,18 @@ for _ in $(seq 1 30); do | |
| 312 | + [[ "$code" == 200 ]] && break | |
| 313 | + sleep 10 | |
| 314 | + done | |
| 315 | +-[[ "$code" == 200 ]] || die "https://$DIFFINERY_DOMAIN/healthz did not return 200 (last: ${code:-no answer}). Check that the $DIFFINERY_DOMAIN DNS record is proxied (orange cloud); a new certificate can take a few minutes, so run this again." | |
| 316 | ++if ((ON_CLOUDFLARE)); then | |
| 317 | ++ [[ "$code" == 200 ]] || die "https://$DIFFINERY_DOMAIN/healthz did not return 200 (last: ${code:-no answer}). Check that the $DIFFINERY_DOMAIN DNS record is proxied (orange cloud); a new certificate can take a few minutes, so run this again." | |
| 318 | ++else | |
| 319 | ++ [[ "$code" == 200 ]] || die "https://$DIFFINERY_DOMAIN/healthz did not return 200 (last: ${code:-no answer}). Check that $DIFFINERY_DOMAIN resolves to the host serving $CF_API and that the Worker started." | |
| 320 | ++fi | |
| 321 | + info "/healthz -> 200" | |
| 322 | + curl -sS "https://$DIFFINERY_DOMAIN/login" | grep -q "Sign in with a passkey" || die "/login is not the Diffinery sign-in page: check DIFFINERY_DOMAIN, DIFFINERY_ZONE and the route in apps/diffinery/wrangler.deploy.jsonc" | |
| 323 | + info "/login serves the passkey sign-in page" | |
| 324 | + loc="$(curl -sS -o /dev/null -w '%{http_code} %{redirect_url}' "https://$DIFFINERY_DOMAIN/auth/access" || true)" | |
| 325 | +-if [[ "$loc" == 302\ https://*.cloudflareaccess.com/* ]]; then | |
| 326 | ++if ((ON_CLOUDFLARE == 0)); then | |
| 327 | ++ info "no Cloudflare Access here: the first administrator signs up with SIGNUP=open (needs TURNSTILE_SITE_KEY and TURNSTILE_SECRET and email), see HOMEFLARE.md" | |
| 328 | ++elif [[ "$loc" == 302\ https://*.cloudflareaccess.com/* ]]; then | |
| 329 | + info "/auth/access is behind Cloudflare Access" | |
| 330 | + else | |
| 331 | + warn "/auth/access is not behind Cloudflare Access yet ($loc), so email recovery and the first admin sign-in won't work. Set ACCESS_TEAM_DOMAIN and run this again." | |
| 332 | +@@ -252,4 +273,4 @@ echo | |
| 333 | + echo "Diffinery is live at https://$DIFFINERY_DOMAIN." | |
| 334 | + echo "Sign in as the first administrator at https://$DIFFINERY_DOMAIN/auth/access with an email in BOOTSTRAP_ADMINS," | |
| 335 | + echo "then add a passkey. Run scripts/setup.sh again at any time to update." | |
| 336 | +-[[ -n "$ACCESS_TEAM" ]] || warn "the Access team domain is still unknown; set ACCESS_TEAM_DOMAIN (Zero Trust → Settings → Custom pages shows it) and run this again" | |
| 337 | ++[[ -n "$ACCESS_TEAM" ]] || ((ON_CLOUDFLARE == 0)) || warn "the Access team domain is still unknown; set ACCESS_TEAM_DOMAIN (Zero Trust → Settings → Custom pages shows it) and run this again" | |
| 338 | +diff --git a/scripts/turnstile.sh b/scripts/turnstile.sh | |
| 339 | +index 22db2a1..6bf859f 100755 | |
| 340 | +--- a/scripts/turnstile.sh | |
| 341 | ++++ b/scripts/turnstile.sh | |
| 342 | +@@ -43,7 +43,7 @@ ROOT="$(cd "$(dirname "$0")/.." && pwd)" | |
| 343 | + APP="$ROOT/apps/diffinery" | |
| 344 | + WRANGLER=(pnpm --dir "$APP" exec wrangler) | |
| 345 | + WORKER=diffinery | |
| 346 | +-API="https://api.cloudflare.com/client/v4/accounts/$CLOUDFLARE_ACCOUNT_ID" | |
| 347 | ++API="${CLOUDFLARE_API_BASE_URL:-https://api.cloudflare.com/client/v4}/accounts/$CLOUDFLARE_ACCOUNT_ID" | |
| 348 | + WIDGET_NAME="diffinery $DOMAIN" | |
| 349 | + PERMISSION="Account → Turnstile → Edit (\"Turnstile Sites Write\" in the API reference)" | |
| 350 | + | |
| 351 | +diff --git a/scripts/wait-container-rollout.sh b/scripts/wait-container-rollout.sh | |
| 352 | +index 7a15eb6..59cae89 100755 | |
| 353 | +--- a/scripts/wait-container-rollout.sh | |
| 354 | ++++ b/scripts/wait-container-rollout.sh | |
| 355 | +@@ -10,7 +10,7 @@ | |
| 356 | + # ROLLOUT_WAIT_MINUTES (default 10) rather than failing the deploy. | |
| 357 | + set -euo pipefail | |
| 358 | + : "${CLOUDFLARE_API_TOKEN:?}" "${CLOUDFLARE_ACCOUNT_ID:?}" "${RUNNER_IMAGE:?RUNNER_IMAGE is required (scripts/runner-image.sh)}" | |
| 359 | +-api() { curl -fsS --retry 4 --retry-all-errors --retry-delay 3 "https://api.cloudflare.com/client/v4/accounts/$CLOUDFLARE_ACCOUNT_ID/containers$1" -H "Authorization: Bearer $CLOUDFLARE_API_TOKEN"; } | |
| 360 | ++api() { curl -fsS --retry 4 --retry-all-errors --retry-delay 3 "${CLOUDFLARE_API_BASE_URL:-https://api.cloudflare.com/client/v4}/accounts/$CLOUDFLARE_ACCOUNT_ID/containers$1" -H "Authorization: Bearer $CLOUDFLARE_API_TOKEN"; } | |
| 361 | + deadline=$(( $(date +%s) + ${ROLLOUT_WAIT_MINUTES:-10} * 60 )) | |
| 362 | + last="" | |
| 363 | + while :; do |
addedHOMEFLARE.md+47−0View file
| @@ -1,0 +1,47 @@ | ||
| 1 | +# Diffinery on Homeflare | |
| 2 | + | |
| 3 | +[Homeflare](https://github.com/jason-s13r/homeflare) runs Workers on your own machines (celld) behind a | |
| 4 | +Cloudflare-compatible API. These scripts deploy to it when `CLOUDFLARE_API_BASE_URL` points at that API, | |
| 5 | +which `homeflare-wrangler run` sets, along with `CLOUDFLARE_CONTAINER_REGISTRY` (Homeflare's registry) and a | |
| 6 | +`docker` command on your Podman. | |
| 7 | + | |
| 8 | +On another API than Cloudflare's, `scripts/setup.sh` and `scripts/bootstrap.sh` skip what needs Cloudflare | |
| 9 | +itself: | |
| 10 | + | |
| 11 | +| Skipped | What it means | | |
| 12 | +| --- | --- | | |
| 13 | +| Zone check | The hostname is a Homeflare custom domain, not a route in a Cloudflare zone. | | |
| 14 | +| R2 lifecycle rules | Logs, artifacts, archives and exports don't age out by themselves. | | |
| 15 | +| Email Sending setup and its event subscription | Onboard the sending domain in Cloudflare yourself (Homeflare relays email through it with `HOMEFLARE_EMAIL=cloudflare`). Bounces and complaints don't reach the suppression list. | | |
| 16 | +| Turnstile widget | Make one in the Cloudflare dashboard and pass its keys: `TURNSTILE_SITE_KEY` and `TURNSTILE_SECRET`. Without them open sign-up and anonymous security reports stay off. | | |
| 17 | +| Cloudflare Access | Email recovery and the `/auth/access` first sign-in stay off. Passkeys work. | | |
| 18 | + | |
| 19 | +## Deploy | |
| 20 | + | |
| 21 | +Put the settings in a file outside the repository (it holds the Turnstile secret), for example | |
| 22 | +`~/.config/homeflare/diffinery.env`, readable by you only: | |
| 23 | + | |
| 24 | +```sh | |
| 25 | +CLOUDFLARE_ACCOUNT_ID=<Homeflare's account id> | |
| 26 | +DIFFINERY_DOMAIN=git.homeflare.example.com | |
| 27 | +MAIL_DOMAIN=example.com | |
| 28 | +[email protected] | |
| 29 | +TURNSTILE_SITE_KEY=0x4AAA... | |
| 30 | +TURNSTILE_SECRET=0x4AAA... | |
| 31 | +``` | |
| 32 | + | |
| 33 | +`MAIL_DOMAIN` is a domain onboarded to Email Sending in your Cloudflare account. Homeflare's account id is | |
| 34 | +in the Dashboard, or from `GET /client/v4/accounts`. Then, from the repository: | |
| 35 | + | |
| 36 | +```sh | |
| 37 | +homeflare-wrangler --env-file ~/.config/homeflare/diffinery.env run scripts/setup.sh --yes | |
| 38 | +``` | |
| 39 | + | |
| 40 | +`homeflare-wrangler` reads the Homeflare API token from `~/.config/homeflare/wrangler.env` and sets | |
| 41 | +`CLOUDFLARE_API_BASE_URL` itself. Run it again to update. | |
| 42 | + | |
| 43 | +## The first administrator | |
| 44 | + | |
| 45 | +Without Access, the first account comes from open sign-up, which needs Turnstile and email. Deploy once with | |
| 46 | +`"SIGNUP": "open"` under `vars` in `apps/diffinery/wrangler.jsonc`, sign up at `https://<host>/signup` with an | |
| 47 | +address in `BOOTSTRAP_ADMINS`, add a passkey, then remove `SIGNUP` and deploy again. |
modifiedscripts/bootstrap-admins.sh+1−1View file
| @@ -59,7 +59,7 @@ | ||
| 59 | 59 | if [[ -z "${CLOUDFLARE_API_TOKEN:-}" || -z "${CLOUDFLARE_ACCOUNT_ID:-}" ]]; then |
| 60 | 60 | none "it isn't in the environment or wrangler.jsonc, and without CLOUDFLARE_API_TOKEN and CLOUDFLARE_ACCOUNT_ID the deployed Worker's list can't be read" |
| 61 | 61 | fi |
| 62 | -SETTINGS="$(curl -sS --retry 4 --retry-all-errors --retry-delay 3 "https://api.cloudflare.com/client/v4/accounts/$CLOUDFLARE_ACCOUNT_ID/workers/scripts/$WORKER/settings" -H "Authorization: Bearer $CLOUDFLARE_API_TOKEN" || true)" | |
| 62 | +SETTINGS="$(curl -sS --retry 4 --retry-all-errors --retry-delay 3 "${CLOUDFLARE_API_BASE_URL:-https://api.cloudflare.com/client/v4}/accounts/$CLOUDFLARE_ACCOUNT_ID/workers/scripts/$WORKER/settings" -H "Authorization: Bearer $CLOUDFLARE_API_TOKEN" || true)" | |
| 63 | 63 | if jq -e '.success == true' >/dev/null 2>&1 <<<"$SETTINGS"; then |
| 64 | 64 | LIVE="$(jq -r '[.result.bindings[]? | select(.type == "plain_text" and .name == "BOOTSTRAP_ADMINS") | .text][0] // empty' <<<"$SETTINGS")" |
| 65 | 65 | LIVE="$(trimmed "$LIVE")" |
modifiedscripts/bootstrap.sh+31−10View file
| @@ -25,7 +25,11 @@ | ||
| 25 | 25 | ROOT="$(cd "$(dirname "$0")/.." && pwd)" |
| 26 | 26 | APP="$ROOT/apps/diffinery" |
| 27 | 27 | WRANGLER=(pnpm --dir "$APP" exec wrangler) |
| 28 | -API="https://api.cloudflare.com/client/v4/accounts/$CLOUDFLARE_ACCOUNT_ID" | |
| 28 | +CF_API="${CLOUDFLARE_API_BASE_URL:-https://api.cloudflare.com/client/v4}" | |
| 29 | +API="$CF_API/accounts/$CLOUDFLARE_ACCOUNT_ID" | |
| 30 | +# 0 on another Cloudflare-compatible API (Homeflare): the steps that need Cloudflare itself are skipped. | |
| 31 | +ON_CLOUDFLARE=1 | |
| 32 | +[[ "$CF_API" == https://api.cloudflare.com/* ]] || ON_CLOUDFLARE=0 | |
| 29 | 33 | |
| 30 | 34 | cf() { # cf METHOD PATH [JSON] |
| 31 | 35 | local method="$1" path="$2" body="${3:-}" |
| @@ -74,6 +78,9 @@ | ||
| 74 | 78 | {"id":"action-mirror-30d","enabled":true,"conditions":{"prefix":"action-mirror/"},"deleteObjectsTransition":{"condition":{"type":"Age","maxAge":2592000}}} |
| 75 | 79 | ]}' |
| 76 | 80 | # Applied on every deploy; a failure stops it, since a wrong rule can delete data. |
| 81 | +if ((ON_CLOUDFLARE == 0)); then | |
| 82 | + warn "R2 lifecycle rules are skipped on $CF_API: transient objects (logs, artifacts, archives, exports) don't age out by themselves." | |
| 83 | +else | |
| 77 | 84 | RESULT="$(cf PUT "/r2/buckets/diffinery-data/lifecycle" "$LIFECYCLE")" |
| 78 | 85 | if ! jq -e '.success' >/dev/null <<<"$RESULT"; then |
| 79 | 86 | echo "could not set the R2 lifecycle rules (token has R2: Edit?): $(jq -c '.errors // .' <<<"$RESULT" 2>/dev/null || echo "$RESULT")" >&2 |
| @@ -85,6 +92,7 @@ | ||
| 85 | 92 | exit 1 |
| 86 | 93 | fi |
| 87 | 94 | echo "R2 lifecycle rules set" |
| 95 | +fi | |
| 88 | 96 | endlog |
| 89 | 97 | |
| 90 | 98 | # ------------------------------------------------------------------ Queues |
| @@ -127,7 +135,7 @@ | ||
| 127 | 135 | 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 |
| 128 | 136 | # The zone that holds the sending domain (the domain itself or a parent), as scripts/email-sending.sh finds it. |
| 129 | 137 | while [[ "$name" == *.* ]]; do |
| 130 | - 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)" | |
| 138 | + zone="$(curl -sS "${CLOUDFLARE_API_BASE_URL:-https://api.cloudflare.com/client/v4}/zones?name=$name" -H "Authorization: Bearer $CLOUDFLARE_API_TOKEN" | jq -r '.result[0].id // empty' || true)" | |
| 131 | 139 | [[ -n "$zone" ]] && break |
| 132 | 140 | name="${name#*.}" |
| 133 | 141 | done |
| @@ -162,12 +170,16 @@ | ||
| 162 | 170 | 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")" |
| 163 | 171 | fi |
| 164 | 172 | } |
| 165 | -subscribe_mail_events | |
| 173 | +if ((ON_CLOUDFLARE)); then subscribe_mail_events; else echo "skipped on $CF_API: bounces and complaints don't reach the suppression list by themselves"; fi | |
| 166 | 174 | endlog |
| 167 | 175 | |
| 168 | 176 | # ------------------------------------------------------------------ Email Sending |
| 169 | 177 | # notifications@<MAIL_DOMAIN or DIFFINERY_DOMAIN>; warns (and email is skipped) until the domain is onboarded. |
| 170 | -DIFFINERY_DOMAIN="$DOMAIN" "$ROOT/scripts/email-sending.sh" || warn "Email Sending check failed; email may not be delivered." | |
| 178 | +if ((ON_CLOUDFLARE)); then | |
| 179 | + DIFFINERY_DOMAIN="$DOMAIN" "$ROOT/scripts/email-sending.sh" || warn "Email Sending check failed; email may not be delivered." | |
| 180 | +else | |
| 181 | + echo "Email Sending setup skipped on $CF_API: the sending domain is set up wherever that API sends from." | |
| 182 | +fi | |
| 171 | 183 | |
| 172 | 184 | # ------------------------------------------------------------------ Turnstile |
| 173 | 185 | # The widget "diffinery <DIFFINERY_DOMAIN>", created when missing, so people who aren't signed in can |
| @@ -177,19 +189,28 @@ | ||
| 177 | 189 | # Account → Turnstile → Edit; without it turnstile.sh warns, naming it, and the deploy goes on with |
| 178 | 190 | # anonymous reports refused. Sign-up stays as SIGNUP says: keys alone don't open it. |
| 179 | 191 | log "Turnstile" |
| 180 | -TURNSTILE_SITE_KEY="$(DIFFINERY_DOMAIN="$DOMAIN" "$ROOT/scripts/turnstile.sh")" || { warn "scripts/turnstile.sh failed; anonymous security reports stay refused."; TURNSTILE_SITE_KEY=""; } | |
| 192 | +if ((ON_CLOUDFLARE)); then | |
| 193 | + TURNSTILE_SITE_KEY="$(DIFFINERY_DOMAIN="$DOMAIN" "$ROOT/scripts/turnstile.sh")" || { warn "scripts/turnstile.sh failed; anonymous security reports stay refused."; TURNSTILE_SITE_KEY=""; } | |
| 194 | +else | |
| 195 | + # No Turnstile API here: a widget made in the Cloudflare dashboard, its keys in the environment. | |
| 196 | + TURNSTILE_SITE_KEY="${TURNSTILE_SITE_KEY:-}" | |
| 197 | + [[ -n "$TURNSTILE_SITE_KEY" ]] || warn "No TURNSTILE_SITE_KEY: open sign-up and anonymous security reports stay off." | |
| 198 | +fi | |
| 181 | 199 | echo "site key: ${TURNSTILE_SITE_KEY:-none}" |
| 182 | 200 | endlog |
| 183 | 201 | |
| 184 | 202 | # ------------------------------------------------------------------ Access |
| 185 | 203 | log "Cloudflare Access" |
| 186 | 204 | TEAM="${ACCESS_TEAM_DOMAIN:-}" |
| 187 | -if [[ -z "$TEAM" ]]; then | |
| 205 | +if [[ -z "$TEAM" ]] && ((ON_CLOUDFLARE)); then | |
| 188 | 206 | TEAM="$(cf GET /access/organizations | jq -r '.result.auth_domain // empty' || true)" |
| 189 | 207 | fi |
| 190 | -APPS="$(cf GET "/access/apps?per_page=100" || echo '{}')" | |
| 191 | 208 | AUD="" |
| 192 | -if ! jq -e '.success' >/dev/null <<<"$APPS"; then | |
| 209 | +APPS='{}' | |
| 210 | +((ON_CLOUDFLARE == 0)) || APPS="$(cf GET "/access/apps?per_page=100" || echo '{}')" | |
| 211 | +if ((ON_CLOUDFLARE == 0)); then | |
| 212 | + echo "Access skipped on $CF_API: passkeys work; email recovery and the /auth/access first sign-in stay off." | |
| 213 | +elif ! jq -e '.success' >/dev/null <<<"$APPS"; then | |
| 193 | 214 | warn "Could not read Access applications (token needs Access: Apps and Policies Edit). Passkeys work; email recovery stays off." |
| 194 | 215 | else |
| 195 | 216 | # People sign in with passkeys. Access guards only /auth/access, the email-based |
| @@ -216,11 +237,11 @@ | ||
| 216 | 237 | 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." |
| 217 | 238 | fi |
| 218 | 239 | fi |
| 219 | -if [[ -z "$TEAM" ]]; then | |
| 240 | +if [[ -z "$TEAM" ]] && ((ON_CLOUDFLARE)); then | |
| 220 | 241 | # Fall back to the sign-in redirect, which names the team domain. |
| 221 | 242 | TEAM="$(curl -sS -o /dev/null -w '%{redirect_url}' "https://$DOMAIN/auth/access" | sed -nE 's#^https://([^/]+\.cloudflareaccess\.com)/.*#\1#p' || true)" |
| 222 | 243 | fi |
| 223 | -[[ -n "$TEAM" ]] || warn "Access team domain unknown; set the ACCESS_TEAM_DOMAIN repository variable (Zero Trust → Settings → Custom pages shows it)." | |
| 244 | +[[ -n "$TEAM" ]] || ((ON_CLOUDFLARE == 0)) || warn "Access team domain unknown; set the ACCESS_TEAM_DOMAIN repository variable (Zero Trust → Settings → Custom pages shows it)." | |
| 224 | 245 | echo "team=${TEAM:-?} aud=${AUD:+set}" |
| 225 | 246 | endlog |
| 226 | 247 |
modifiedscripts/email-sending.sh+1−1View file
| @@ -12,7 +12,7 @@ | ||
| 12 | 12 | |
| 13 | 13 | : "${CLOUDFLARE_API_TOKEN:?set CLOUDFLARE_API_TOKEN}" |
| 14 | 14 | DOMAIN="${MAIL_DOMAIN:-${DIFFINERY_DOMAIN:-diffinery.com}}" |
| 15 | -CF="https://api.cloudflare.com/client/v4" | |
| 15 | +CF="${CLOUDFLARE_API_BASE_URL:-https://api.cloudflare.com/client/v4}" | |
| 16 | 16 | |
| 17 | 17 | cf() { curl -sS "$CF$1" -H "Authorization: Bearer $CLOUDFLARE_API_TOKEN"; } |
| 18 | 18 | warn() { echo "::warning::$*" >&2; } |
modifiedscripts/ensure-secrets.sh+5−1View file
| @@ -36,7 +36,11 @@ | ||
| 36 | 36 | # Without the Turnstile permission this warns (scripts/turnstile.sh names it) and the deploy goes on: |
| 37 | 37 | # people who aren't signed in can't send security reports until it's set. |
| 38 | 38 | SITE_KEY="$(tail -n +2 "$APP/wrangler.deploy.jsonc" | jq -r '.vars.TURNSTILE_SITE_KEY // empty')" |
| 39 | -if [[ -n "$SITE_KEY" ]] && ! grep -qx TURNSTILE_SECRET <<<"$EXISTING"; then | |
| 39 | +if [[ -n "$SITE_KEY" && -n "${TURNSTILE_SECRET:-}" ]] && ! grep -qx TURNSTILE_SECRET <<<"$EXISTING"; then | |
| 40 | + # A widget's secret from the environment (an API without Turnstile, such as Homeflare's). | |
| 41 | + printf '%s' "$TURNSTILE_SECRET" | "${WRANGLER[@]}" secret put TURNSTILE_SECRET -c wrangler.deploy.jsonc >/dev/null | |
| 42 | + echo "set TURNSTILE_SECRET" | |
| 43 | +elif [[ -n "$SITE_KEY" ]] && ! grep -qx TURNSTILE_SECRET <<<"$EXISTING"; then | |
| 40 | 44 | if "$ROOT/scripts/turnstile.sh" secret "$SITE_KEY" -c wrangler.deploy.jsonc; then |
| 41 | 45 | echo "set TURNSTILE_SECRET" |
| 42 | 46 | else |
modifiedscripts/render-config.mjs+3−1View file
| @@ -34,7 +34,9 @@ | ||
| 34 | 34 | if (url.protocol !== "https:" || url.pathname !== "/") throw new Error("--public-url must be an https origin, like https://git.example.com"); |
| 35 | 35 | cfg.vars.PUBLIC_URL = url.origin; |
| 36 | 36 | const zone = values.zone ?? url.hostname.split(".").slice(-2).join("."); |
| 37 | - cfg.routes = [{ pattern: `${url.hostname}/*`, zone_name: zone }]; | |
| 37 | + // On another Cloudflare-compatible API (Homeflare), a custom domain: the zone isn't Cloudflare's. | |
| 38 | + const onCloudflare = (process.env.CLOUDFLARE_API_BASE_URL ?? "https://api.cloudflare.com/").startsWith("https://api.cloudflare.com/"); | |
| 39 | + cfg.routes = onCloudflare ? [{ pattern: `${url.hostname}/*`, zone_name: zone }] : [{ pattern: url.hostname, custom_domain: true }]; | |
| 38 | 40 | cfg.vars.MAIL_FROM = `notifications@${url.hostname}`; |
| 39 | 41 | } |
| 40 | 42 | if (values["mail-from"]) cfg.vars.MAIL_FROM = values["mail-from"]; |
modifiedscripts/runner-image.sh+1−1View file
| @@ -20,7 +20,7 @@ | ||
| 20 | 20 | } | sha256sum | cut -c1-24 |
| 21 | 21 | ) |
| 22 | 22 | : "${CLOUDFLARE_ACCOUNT_ID:?CLOUDFLARE_ACCOUNT_ID is required}" |
| 23 | -image="registry.cloudflare.com/$CLOUDFLARE_ACCOUNT_ID/$name:$tag" | |
| 23 | +image="${CLOUDFLARE_CONTAINER_REGISTRY:-registry.cloudflare.com}/$CLOUDFLARE_ACCOUNT_ID/$name:$tag" | |
| 24 | 24 | wrangler() { (cd apps/diffinery && pnpm exec wrangler "$@"); } |
| 25 | 25 | |
| 26 | 26 | if ! listed=$(wrangler containers images list --json --filter "(^|/)$name\$" 2>&1); then |
modifiedscripts/setup.sh+28−7View file
| @@ -19,6 +19,12 @@ | ||
| 19 | 19 | # GITHUB_APP_ID, GITHUB_APP_SLUG, GITHUB_APP_CLIENT_ID, GITHUB_APP_URL |
| 20 | 20 | # the GitHub App of GitHub runners (docs/self-hosting/configuration.md#github-runners) |
| 21 | 21 | # ROLLOUT_WAIT_MINUTES how long to wait for the container rollout (default 10) |
| 22 | +# CLOUDFLARE_API_BASE_URL another Cloudflare-compatible API, such as Homeflare's (default Cloudflare's). | |
| 23 | +# Steps that need Cloudflare itself (the zone check, R2 lifecycle rules, Email | |
| 24 | +# Sending setup and events, Turnstile and Access) are skipped there; see | |
| 25 | +# HOMEFLARE.md. | |
| 26 | +# CLOUDFLARE_CONTAINER_REGISTRY the registry runner images go to (default registry.cloudflare.com) | |
| 27 | +# TURNSTILE_SITE_KEY, TURNSTILE_SECRET a Turnstile widget's keys, for an API without Turnstile | |
| 22 | 28 | # |
| 23 | 29 | # Every step is idempotent: it creates only what is missing and keeps your data and secrets, so run |
| 24 | 30 | # it again to update. Secrets are never printed. Needs Node.js 22+, pnpm, jq, curl, openssl and Docker. |
| @@ -26,8 +32,13 @@ | ||
| 26 | 32 | |
| 27 | 33 | ROOT="$(cd "$(dirname "$0")/.." && pwd)" |
| 28 | 34 | APP="$ROOT/apps/diffinery" |
| 29 | -CF_API="https://api.cloudflare.com/client/v4" | |
| 35 | +CF_API="${CLOUDFLARE_API_BASE_URL:-https://api.cloudflare.com/client/v4}" | |
| 30 | 36 | PLACEHOLDER_D1="00000000-0000-0000-0000-000000000000" |
| 37 | +# 1 on Cloudflare's own API; 0 on another Cloudflare-compatible one (Homeflare), which skips the | |
| 38 | +# steps that need Cloudflare itself. | |
| 39 | +ON_CLOUDFLARE=1 | |
| 40 | +[[ "$CF_API" == https://api.cloudflare.com/* ]] || ON_CLOUDFLARE=0 | |
| 41 | +export ON_CLOUDFLARE | |
| 31 | 42 | DRY_RUN=0 |
| 32 | 43 | ASK=1 |
| 33 | 44 | |
| @@ -150,12 +161,16 @@ | ||
| 150 | 161 | fi |
| 151 | 162 | jq -e '.success == true and .result.status == "active"' >/dev/null 2>&1 <<<"$verify" || die "Cloudflare doesn't accept the API token: $(errors_of "$verify")" |
| 152 | 163 | info "the token is valid and active" |
| 164 | + if ((ON_CLOUDFLARE == 0)); then | |
| 165 | + info "API: $CF_API (not Cloudflare's: the zone check, R2 lifecycle rules, Email Sending setup, Turnstile and Access are skipped)" | |
| 166 | + else | |
| 153 | 167 | zones="$(cf_get "/zones?name=$ZONE&account.id=$CLOUDFLARE_ACCOUNT_ID" || true)" |
| 154 | 168 | jq -e '.success == true' >/dev/null 2>&1 <<<"$zones" || die "couldn't look up the zone $ZONE: $(errors_of "$zones")" |
| 155 | 169 | zone_status="$(jq -r '.result[0].status // empty' <<<"$zones")" |
| 156 | 170 | [[ -n "$zone_status" ]] || die "the zone $ZONE isn't in account $CLOUDFLARE_ACCOUNT_ID, or the token can't see it. Add the domain to the account, give the token Zone → Zone → Read on it, or set DIFFINERY_ZONE if the zone that holds $DIFFINERY_DOMAIN is another one." |
| 157 | 171 | [[ "$zone_status" == active ]] || warn "the zone $ZONE is $zone_status, not active: the route serves nothing until its nameservers point at Cloudflare" |
| 158 | 172 | info "zone $ZONE is in the account ($zone_status)" |
| 173 | + fi | |
| 159 | 174 | fi |
| 160 | 175 | |
| 161 | 176 | cd "$ROOT" |
| @@ -173,7 +188,7 @@ | ||
| 173 | 188 | |
| 174 | 189 | step "Rendering the deploy config (placeholder D1 id) and bundling the Worker" |
| 175 | 190 | # A placeholder runner image: the dry run builds no container image (that needs Docker and the registry). |
| 176 | - RUNNER_IMAGE="registry.cloudflare.com/${CLOUDFLARE_ACCOUNT_ID:-00000000000000000000000000000000}/diffinery-runner:dry-run" \ | |
| 191 | + RUNNER_IMAGE="${CLOUDFLARE_CONTAINER_REGISTRY:-registry.cloudflare.com}/${CLOUDFLARE_ACCOUNT_ID:-00000000000000000000000000000000}/diffinery-runner:dry-run" \ | |
| 177 | 192 | node "$ROOT/scripts/render-config.mjs" --d1 "$PLACEHOLDER_D1" ${ACCESS_TEAM_DOMAIN:+--team "$ACCESS_TEAM_DOMAIN"} \ |
| 178 | 193 | --public-url "https://$DIFFINERY_DOMAIN" ${DIFFINERY_ZONE:+--zone "$DIFFINERY_ZONE"} --mail-from "$MAIL_FROM" --admins "$ADMINS" |
| 179 | 194 | OUT="$(mktemp -d "${TMPDIR:-/tmp}/diffinery-dry-run.XXXXXX")" |
| @@ -205,7 +220,7 @@ | ||
| 205 | 220 | env -u GITHUB_ENV "$ROOT/scripts/runner-image.sh" | tee "$image_log" |
| 206 | 221 | RUNNER_IMAGE="$(tail -n 1 "$image_log")" |
| 207 | 222 | rm -f "$image_log" |
| 208 | -[[ "$RUNNER_IMAGE" == registry.cloudflare.com/* ]] || die "scripts/runner-image.sh didn't print the runner image" | |
| 223 | +[[ "$RUNNER_IMAGE" == ${CLOUDFLARE_CONTAINER_REGISTRY:-registry.cloudflare.com}/* ]] || die "scripts/runner-image.sh didn't print the runner image" | |
| 209 | 224 | export RUNNER_IMAGE |
| 210 | 225 | |
| 211 | 226 | step "Creating or updating Cloudflare resources (scripts/bootstrap.sh)" |
| @@ -221,7 +236,7 @@ | ||
| 221 | 236 | step "Worker secrets (generated on the first deploy; never printed)" |
| 222 | 237 | "$ROOT/scripts/ensure-secrets.sh" |
| 223 | 238 | |
| 224 | -if [[ -z "$ACCESS_TEAM" ]]; then | |
| 239 | +if [[ -z "$ACCESS_TEAM" ]] && ((ON_CLOUDFLARE)); then | |
| 225 | 240 | step "Finishing the Access setup (first deploy)" |
| 226 | 241 | : >"$OUTPUTS" |
| 227 | 242 | GITHUB_OUTPUT="$OUTPUTS" "$ROOT/scripts/bootstrap.sh" |
| @@ -237,12 +252,18 @@ | ||
| 237 | 252 | [[ "$code" == 200 ]] && break |
| 238 | 253 | sleep 10 |
| 239 | 254 | done |
| 240 | -[[ "$code" == 200 ]] || die "https://$DIFFINERY_DOMAIN/healthz did not return 200 (last: ${code:-no answer}). Check that the $DIFFINERY_DOMAIN DNS record is proxied (orange cloud); a new certificate can take a few minutes, so run this again." | |
| 255 | +if ((ON_CLOUDFLARE)); then | |
| 256 | + [[ "$code" == 200 ]] || die "https://$DIFFINERY_DOMAIN/healthz did not return 200 (last: ${code:-no answer}). Check that the $DIFFINERY_DOMAIN DNS record is proxied (orange cloud); a new certificate can take a few minutes, so run this again." | |
| 257 | +else | |
| 258 | + [[ "$code" == 200 ]] || die "https://$DIFFINERY_DOMAIN/healthz did not return 200 (last: ${code:-no answer}). Check that $DIFFINERY_DOMAIN resolves to the host serving $CF_API and that the Worker started." | |
| 259 | +fi | |
| 241 | 260 | info "/healthz -> 200" |
| 242 | 261 | curl -sS "https://$DIFFINERY_DOMAIN/login" | grep -q "Sign in with a passkey" || die "/login is not the Diffinery sign-in page: check DIFFINERY_DOMAIN, DIFFINERY_ZONE and the route in apps/diffinery/wrangler.deploy.jsonc" |
| 243 | 262 | info "/login serves the passkey sign-in page" |
| 244 | 263 | loc="$(curl -sS -o /dev/null -w '%{http_code} %{redirect_url}' "https://$DIFFINERY_DOMAIN/auth/access" || true)" |
| 245 | -if [[ "$loc" == 302\ https://*.cloudflareaccess.com/* ]]; then | |
| 264 | +if ((ON_CLOUDFLARE == 0)); then | |
| 265 | + info "no Cloudflare Access here: the first administrator signs up with SIGNUP=open (needs TURNSTILE_SITE_KEY and TURNSTILE_SECRET and email), see HOMEFLARE.md" | |
| 266 | +elif [[ "$loc" == 302\ https://*.cloudflareaccess.com/* ]]; then | |
| 246 | 267 | info "/auth/access is behind Cloudflare Access" |
| 247 | 268 | else |
| 248 | 269 | warn "/auth/access is not behind Cloudflare Access yet ($loc), so email recovery and the first admin sign-in won't work. Set ACCESS_TEAM_DOMAIN and run this again." |
| @@ -252,4 +273,4 @@ | ||
| 252 | 273 | echo "Diffinery is live at https://$DIFFINERY_DOMAIN." |
| 253 | 274 | echo "Sign in as the first administrator at https://$DIFFINERY_DOMAIN/auth/access with an email in BOOTSTRAP_ADMINS," |
| 254 | 275 | echo "then add a passkey. Run scripts/setup.sh again at any time to update." |
| 255 | -[[ -n "$ACCESS_TEAM" ]] || warn "the Access team domain is still unknown; set ACCESS_TEAM_DOMAIN (Zero Trust → Settings → Custom pages shows it) and run this again" | |
| 276 | +[[ -n "$ACCESS_TEAM" ]] || ((ON_CLOUDFLARE == 0)) || warn "the Access team domain is still unknown; set ACCESS_TEAM_DOMAIN (Zero Trust → Settings → Custom pages shows it) and run this again" |
modifiedscripts/turnstile.sh+1−1View file
| @@ -43,7 +43,7 @@ | ||
| 43 | 43 | APP="$ROOT/apps/diffinery" |
| 44 | 44 | WRANGLER=(pnpm --dir "$APP" exec wrangler) |
| 45 | 45 | WORKER=diffinery |
| 46 | -API="https://api.cloudflare.com/client/v4/accounts/$CLOUDFLARE_ACCOUNT_ID" | |
| 46 | +API="${CLOUDFLARE_API_BASE_URL:-https://api.cloudflare.com/client/v4}/accounts/$CLOUDFLARE_ACCOUNT_ID" | |
| 47 | 47 | WIDGET_NAME="diffinery $DOMAIN" |
| 48 | 48 | PERMISSION="Account → Turnstile → Edit (\"Turnstile Sites Write\" in the API reference)" |
| 49 | 49 |
modifiedscripts/wait-container-rollout.sh+1−1View file
| @@ -10,7 +10,7 @@ | ||
| 10 | 10 | # ROLLOUT_WAIT_MINUTES (default 10) rather than failing the deploy. |
| 11 | 11 | set -euo pipefail |
| 12 | 12 | : "${CLOUDFLARE_API_TOKEN:?}" "${CLOUDFLARE_ACCOUNT_ID:?}" "${RUNNER_IMAGE:?RUNNER_IMAGE is required (scripts/runner-image.sh)}" |
| 13 | -api() { curl -fsS --retry 4 --retry-all-errors --retry-delay 3 "https://api.cloudflare.com/client/v4/accounts/$CLOUDFLARE_ACCOUNT_ID/containers$1" -H "Authorization: Bearer $CLOUDFLARE_API_TOKEN"; } | |
| 13 | +api() { curl -fsS --retry 4 --retry-all-errors --retry-delay 3 "${CLOUDFLARE_API_BASE_URL:-https://api.cloudflare.com/client/v4}/accounts/$CLOUDFLARE_ACCOUNT_ID/containers$1" -H "Authorization: Bearer $CLOUDFLARE_API_TOKEN"; } | |
| 14 | 14 | deadline=$(( $(date +%s) + ${ROLLOUT_WAIT_MINUTES:-10} * 60 )) |
| 15 | 15 | last="" |
| 16 | 16 | while :; do |