Skip to content

Multi-Tenant Custom Domains (Cloudflare for SaaS)

Each tenant can bring their own domain. apiportal.acme.com resolves to the Acme tenant without anyone on the Lockstep team ever touching a Railway dashboard. This doc is the runbook.

The big picture

Customer
│
│ https://apiportal.acme.com
▼
Cloudflare edge (Cloudflare for SaaS)
│ — terminates TLS with a per-hostname cert
│ — passes the request to the fallback origin
▼
cname.lockstepgroup.com → Railway edge
│
▼
portal Railway service
│
│ Host: apiportal.acme.com
▼
tenantMiddleware reads Host → tenant_domains.hostname lookup → tenantId
│
▼
All DB queries scoped to Acme's tenantId

One-time Cloudflare setup (Lockstep admin, ~15 min)

  1. Add lockstepgroup.com to Cloudflare (already done if it’s in your Cloudflare account).
  2. Create the CNAME target — in your Cloudflare DNS for lockstepgroup.com:
    • Type: CNAME
    • Name: cname (full: cname.lockstepgroup.com)
    • Target: your Railway portal URL (e.g. portal-production-44e1.up.railway.app)
    • Proxy: orange cloud on
  3. Enable Cloudflare for SaaS — Dashboard → Websites → lockstepgroup.com → SSL/TLS → Custom Hostnames
  4. Set the fallback origin — when Cloudflare for SaaS asks for a fallback origin, enter cname.lockstepgroup.com
  5. Create the fallback origin CNAME record (Cloudflare will tell you the exact name — usually _fallback.lockstepgroup.com) pointing back at Railway
  6. Choose validation method — select TXT (HTTP works too, but TXT matches our in-app flow)
  7. Set PLATFORM_CNAME_TARGET in Railway env vars on the portal service to cname.lockstepgroup.com — the Platform config module picks this up everywhere admins see “CNAME your domain to…”

Once that’s done, Cloudflare is ready to accept new custom hostnames programmatically.

Adding a customer’s domain (superadmin, ~5 min + customer DNS work)

In the portal:

  1. Navigate to /tenants (superadmin only)
  2. Find the tenant → click + Domain
  3. Enter apiportal.acme.com → click Issue TXT
  4. Share the TXT record (name + value) with the customer

The customer:

  1. Adds the TXT record at _ltg-verify.apiportal.acme.com with the value you shared
  2. Tells you when it’s done

Back in the portal:

  1. Click Verify next to the pending domain
  2. On success, the portal shows: “Now CNAME apiportal.acme.com → cname.lockstepgroup.com”

The customer:

  1. Creates a CNAME at apiportal.acme.com → cname.lockstepgroup.com
  2. Propagation: usually 1-5 min

In Cloudflare dashboard (automatable — see § Automation below):

  1. SSL/TLS → Custom Hostnames → Add Custom Hostname → apiportal.acme.com
  2. Cloudflare will issue a per-hostname cert automatically

Once the cert is live, traffic to apiportal.acme.com reaches Railway, the portal’s tenantMiddleware matches the hostname against tenant_domains, and all requests are scoped to Acme’s tenantId.

Sync Authentik redirect URIs to the new domain

The tenant’s Authentik OAuth application (created via /integrations/applications earlier) needs the new hostname added as a valid redirect URI so OAuth sign-in works from the new domain.

Automatic (when you promote a domain to primary — not implemented in Phase A; manual step today):

Terminal window
# Fetch the tenant + its verified domains
curl -s $PORTAL/api/tenants/$TENANT_ID | jq '.authentikProviderPk'
# PATCH the Authentik OAuth2 provider's redirect_uris
# (calls /api/v3/providers/oauth2/<pk>/ on Authentik directly)

Manual (until the automation ships):

  1. Superadmin → /integrations/applications → find the tenant’s app
  2. Edit it → add the new redirect URI: https://apiportal.acme.com/api/auth/callback/authentik

Automation

Phase B will add a POST /api/tenants/:id/domains/:domainId/promote endpoint that:

  1. Marks the domain as isPrimary
  2. Calls Cloudflare API to add the custom hostname (requires CF_API_TOKEN env var with Zone:SSL:Edit scope)
  3. Calls Authentik API (PATCH /api/v3/providers/oauth2/:pk/) to append the redirect URI
  4. Returns the cert-provisioning status so the UI can poll

Until then, steps 1-3 are manual dashboard actions.

Testing a new tenant’s domain

Terminal window
# 1. Confirm DNS points to Cloudflare
dig apiportal.acme.com
# → expects CNAME apiportal.acme.com → cname.lockstepgroup.com → Railway
# 2. Confirm TLS
curl -vI https://apiportal.acme.com/health 2>&1 | grep "SSL certificate"
# 3. Confirm tenant resolution
curl -s https://apiportal.acme.com/api/me/tenant
# → should return Acme's tenant record

If (3) returns the Lockstep (default) tenant, the hostname isn’t in tenant_domains — check the portal’s /tenants page to confirm it’s verified.

Costs

  • Cloudflare for SaaS is free up to 100 custom hostnames per zone.
  • Beyond 100, Cloudflare Enterprise pricing applies.
  • Railway charges per service, not per custom domain — adding tenants doesn’t increase the Railway bill.

Rolling back a tenant’s domain

If a customer offboards:

  1. Portal → Tenants → the tenant → find the domain → Delete
  2. Cloudflare dashboard → SSL/TLS → Custom Hostnames → remove apiportal.acme.com
  3. The customer removes their CNAME (optional — once Cloudflare drops the hostname, traffic 404s anyway)

Failure modes

Symptom Likely cause Fix
apiportal.acme.com resolves to default tenant Domain not in tenant_domains or not verified Re-verify TXT
Browser shows generic Cloudflare error Custom hostname not added in Cloudflare dash Add it in SSL/TLS → Custom Hostnames
OAuth callback fails with redirect_uri mismatch Redirect URI not in Authentik app Add via /integrations/applications
Cert pending for >10 min Cloudflare DCV failed (customer’s CNAME is wrong) Re-check dig apiportal.acme.com
Cache lag after verification Middleware’s 60-second LRU cache Wait or restart portal