ADR-0183 — Customer domains on GCP go through a Caddy edge
Status: Accepted · Date: 2026-12-01 · Amends: ADR-0177
Context
A customer adds a domain, points it at Cardsite, presses Verify, and it
works: production does this with Caddy's on-demand TLS gated by
/internal/tls/ask (ADR-0169, deploy/Caddyfile). Staging runs on Cloud
Run (ADR-0177), which did not weigh custom domains, and cannot do it:
- Cloud Run terminates TLS itself and serves only names mapped to a service, each verified in Search Console by the project's owner. A customer's domain never reaches a container, so there is nowhere to run ACME.
- The API finds a custom domain by
r.Host. On Cloud Run every request reaches it on its ownrun.appname, so even with TLS solved the customer's host would be lost.
The same limit made the short host crd.name need a manual
gcloud domains verify.
Decision
Staging gets a TLS edge: one small Compute Engine VM running Caddy
(infra/gcp/modules/cardsite/edge.tf, on when edge_host is set).
- Caddy issues certificates for the edge's own names (
edge_host,public_host) and, on demand, for customer domains the API's ask endpoint approves. The rules are production's. - Every request goes to the web service on its
run.appname, with the visitor's host inX-Forwarded-Hostand a shared secret inX-Cardsite-Edge. The web service's nginx passes an incomingX-Forwarded-Hoston to the API. - The API (
middleware.EdgeHost) setsr.HostfromX-Forwarded-Hostonly whenX-Cardsite-EdgematchesEDGE_SECRET, and always strips the secret. Address-based trust (TRUSTED_PROXIES, ADR-0160) cannot work here: on Cloud Run every caller arrives from the same front end. - The visitor's address follows the same rule. Behind the edge, RealIP's
rightmost untrusted
X-Forwarded-Forentry is the edge VM, which would put every visitor in one rate-limit budget and one location. The edge sends the TCP peer inX-Cardsite-Client-Addr; with the secret it becomesRemoteAddr, the one sourceClientIPreads (ADR-0163). Without the secret the header is stripped and RealIP's answer stands. - The web service is a second hop for visits on its own host
(staging.cardsite.io → nginx → API), where the API would otherwise read
the web service's Google address. Its nginx holds the same secret and
names the visitor the same way: the last
X-Forwarded-Forentry (the one Google's front end appended) and its ownHost; for a request that came through the edge it passes the edge's values on unchanged. DOMAIN_VERIFICATION_TARGETisedge_host: customers CNAME to it, or point an apex A record at its address.- The secret is generated by Terraform, kept in Secret Manager, read by the API service and by the VM at boot.
Cloud Run keeps everything else ADR-0177 chose it for: revisions, rollback
by traffic, deploys from main.
Consequences
- Adding a domain on staging is what it is in production: one DNS record and Verify. No Search Console, no per-domain Terraform.
crd.name(and any first-party name) needs only an A record to the edge.- One VM to keep alive: a single zone, no autohealing. Staging accepts that; production does not use this edge.
- A changed Caddyfile needs the startup script re-run (output
edge_reload_command); certificates persist on the VM's disk. /internal/tls/askis reachable on the API's publicrun.appaddress. It answers only whether a name is a verified customer domain, which that domain's DNS already tells anyone.
Alternatives considered
- Global external Application Load Balancer + Certificate Manager — a certificate and map entry per domain through the API, about USD 18/month before certificates, and a second mechanism beside production's Caddy. Rejected: staging would test something production does not run.
- Cloud Run domain mappings per customer domain — rejected: each needs the owner's Search Console verification.
- Staging as a single systemd + Caddy host — rejected for the reasons ADR-0177 gives (access, revisions, rollback).
Source: docs/decisions/0183-customer-domains-on-gcp-go-through-a-caddy-edge.md