Skip to content

Cloudflare

Cloudflare is authoritative DNS for tomoda.life, the edge for the app, the media CDN (assets Worker + R2) and all object storage. The app hostnames (api, apex, api-dev, app-dev) are proxied (WAF, DDoS, edge TLS, hidden origin); prod adds an mTLS origin lock. Admin UIs (Argo CD, Grafana, pgAdmin, Redis Insight, OTA) are DNS-only straight to Traefik on the server's public IP; assets[-dev] are served by Workers.

Cloudflare DNS
↙   ↘
app hosts (proxied)
Cloudflare edge WAF, DDoS, mTLS (prod)
↓
Traefik node IP · ServiceLB
assets (Worker) · admin UIs (DNS-only)
assets Worker → R2
Traefik node IP · ServiceLB
App traffic runs through the edge; media is served by Workers from R2; admin UIs resolve straight to origin.

Resources managed by Terraform

infrastructure/cloudflare/ owns the zone-level pieces: the Tunnel (tunnel.tf, used for loki-push), Workers (synthetic probes, OG preview, assets), Access, origin pulls, redirects, the WAF and edge rate limit (waf.tf), edge TLS settings (zone_settings.tf), the docs site (pages.tf), mail and verification records (dns.tf), R2, and billing usage notifications (notifications.tf, see Cost monitoring). Application hostnames are written by external-dns from Ingress resources; the assets and Photon index hostnames are created by their Worker / R2 custom domains.

Kept out of Terraform Why Where
Access login methods (GitHub, one-time PIN) and the Zero Trust organization The GitHub method holds an OAuth app secret Zero Trust → Settings → Authentication
Pages deploy hook (PAGES_DEPLOY_HOOK) The URL is the credential Pages project → Settings → Builds; GitHub Actions secret in this repo
App A/TXT records external-dns owns them Ingress hosts in k8s/
Account budget alert (billing_budget_alert) The provider has no budget alert type Notifications → Billing Budget Alert

WAF and edge rate limit

The zone is on the Free plan; waf.tf holds every edge security rule, and a check block warns in plan output if the plan ever changes. All of it costs $0.

Ruleset Phase What it does
zone managed WAF http_request_firewall_managed Runs the Cloudflare Free Managed Ruleset on every request
zone custom WAF http_request_firewall_custom Three block rules: exploit and scanner paths no host serves (/.env, /.git, /wp-*, /phpmyadmin, *.php and similar), traversal sequences in the parsed or raw URI, and attack tool user agents
zone rate limits http_ratelimit 20 requests per 10s per IP and colo on the credential endpoints (/api/v1/auth/ login, social login, register, reset, OTP, passkey login); a client over it is blocked for 10s

Every custom rule blocks; none challenges. A challenge page would break the JSON and WebSocket clients on /api/* and /ws/*, and nothing on the web hosts needs one. The blocked patterns never match a real client, the Workers or the synthetic probes, so they need no exemption. The rate limit is a flood guard well above any person or shared NAT; the backend's per-IP and per-account limits do the fine-grained work. The Free plan caps the edge at 5 custom rules (no regex) and 1 rate-limit rule with a path-only expression, a 10s period and a 10s block. Terraform does not check expression syntax, so the PR plan runs scripts/validate-cf-expressions.sh, which sends every ruleset expression to Cloudflare's read-only expression validator and fails the plan on a parse error. In the rules language contains is an operator (lower(http.user_agent) contains "sqlmap"), while starts_with, ends_with and lower are functions.

Worker request signing

Cloudflare sends cross-zone Worker subrequests with the shared client IP 2a06:98c0:3600::103, so the backend cannot tell our Workers from any other tenant's by address. The synthetic and OG preview Workers therefore sign each API call with X-Tomoda-Worker-Auth: t=<unix>,w=<worker>,v1=<hex>, where v1 is HMAC-SHA256 over <t>.<worker>.<METHOD>.<path> (no query string). The key is tomoda-worker-auth-secret in GCP SM: Terraform reads it at apply time and binds it as WORKER_AUTH_SECRET, and the backend-worker-auth ExternalSecret hands the same value to the api pods. Until the secret has a value the binding is left off and the Workers send no header. The assets Worker never calls the API and is not signed. A shared test vector sits in synthetic/worker.test.mjs, og-preview/worker.test.mjs and the backend's worker-auth test; change the format on all sides together.

Docs site

docs.tomoda.life is the tomoda-docs Pages project (pages.tf). Pages builds it from the tomoda repo, where this repo is a submodule: the build sparse-checks out docs/ and mkdocs.yml and runs mkdocs build --strict. notify-docs-pages.yml calls the deploy hook when docs/ changes here, since Pages only watches the tomoda repo. Access (access.tf) lets in members of the tomoda-labs GitHub org.

Media CDN (assets Worker)

infrastructure/cloudflare/assets_worker.tf runs one Worker per env (tomoda-assets-dev, tomoda-assets-prod) on the custom domains assets-dev.tomoda.life / assets.tomoda.life, bound to that env's R2 bucket. Source and tests: assets-worker/.

  • Private keys need ?exp=&sig= minted by the backend (HMAC-SHA256 over "<path>\n<exp>", key tomoda-asset-signing-key-{env} in GCP SM). Expired or tampered URLs get 403.
  • geo-covers/, places-images/, email-logo-*.png and og-default.png are served unsigned.
  • Objects are cached at the edge by path; private responses go to clients as Cache-Control: private, max-age=1800.
  • R2 CORS on the assets buckets allows browser PUT uploads from the web app origins.

The Worker and the backend share a fixed test vector (assets-worker/worker.test.mjs, tomoda backend/internal/storage/url_signer_test.go); change the signature format on both sides together.

The apex is canonical: app.tomoda.life and www.tomoda.life 301 to it at the edge (redirects.tf), so a share link never sits on a redirecting host. The share host is chosen app-side by Public.BaseURL (backend) and EXPO_PUBLIC_WEB_URL (frontend): tomoda.life in prod, app-dev.tomoda.life in dev.

R2 object storage

All object storage lives in R2, declared in infrastructure/cloudflare/r2.tf (location hint enam). Egress is free, so restores, Photon pulls and seed loads cost nothing beyond storage.

Bucket Holds Lock Lifecycle
tomoda-photon-index Photon index tarballs, served at photon-index.tomoda.life None (-latest is overwritten each build) None
tomoda-location-seed Seed sets (seed/<version>/), dated archives (seed-archive/, geo-covers/) seed-archive/20*, geo-covers/20* indefinitely None
tomoda-db-backups CNPG base backups + WAL (prod/), k3s etcd snapshots (k3s-etcd/), Xavia dumps (ota/) k3s-etcd/ 3d, ota/ 14d delete after 45d (CNPG keeps 30d)
tomoda-observability Loki chunks + index, Tempo blocks (tempo/) None delete after 14d
tomoda-ota-{dev,prod} Xavia OTA bundles None None
tomoda-assets-{dev,prod} User media, curated catalog images None (users must be able to delete their media) None

Every bucket aborts incomplete multipart uploads after a day and has prevent_destroy. Access uses R2 API tokens stored in GCP Secret Manager (see Secrets management); the Cloudflare API token used by Terraform needs R2 edit permission.

Bucket locks

A bucket lock rule blocks deletes and overwrites of matching objects until the lock lapses, and wins over lifecycle rules. The object read/write tokens the cluster and app use cannot change lock rules, so a leaked token can't wipe locked data; only the Terraform token (R2 edit) can.

  • Locks only cover prefixes whose writers never rewrite a key, with an age shorter than the writer's own prune (k3s keeps 20 six-hourly snapshots, about 5 days; the Xavia dump CronJob prunes at 30 days).
  • CNPG (prod/) is not locked. Barman rewrites backup.info during a base backup (STARTED, then DONE), and a retried WAL upload rewrites the same key; under a lock either fails, and a stuck WAL archive fills the Postgres volume.
  • seed/ is not locked because scripts/seed-publish.sh --force overwrites a version. The archive locks match dated directories only, so files at the archive root stay writable. Finish an archive upload in one pass: once locked, re-uploading a changed file fails.
  • To delete locked data, remove its rule in r2.tf first and let the apply land. Destroying cloudflare_r2_bucket_lock in Terraform does not remove the rules from R2; set the bucket's rule list empty instead.

Operational notes

  • CI reads the Terraform token from GCP SM tomoda-cloudflare-api-token. It needs Zone:DNS:Edit, Workers Scripts:Edit, Workers Routes:Edit, Workers R2 Storage:Edit, Access, Cloudflare Pages:Edit, Zone Settings:Edit, Zone Analytics:Read and Account:Notifications:Edit permissions.
  • Edge TLS: SSL mode strict, minimum TLS 1.2, Always Use HTTPS on (zone_settings.tf). Strict means every proxied origin must serve a valid, unexpired certificate for its hostname; see TLS. Proxied hosts redirect http:// to https://; third-party webhooks must be registered with https:// URLs.
  • Mail is Microsoft 365: MX, SPF, the domain verification TXT and autodiscover (CNAME to autodiscover.outlook.com) (DNS-only) live in dns.tf. The apex A record is owned by external-dns through its ownership TXT at the apex.
  • DNS changes propagate in well under a minute given the 60 to 300 second TTLs in use.
  • Do not edit Terraform-managed records, Workers, Access apps or zone settings by hand in the Cloudflare console; the next apply reverts them. Add a hand-made DNS record to dns.tf instead.