Files
karaoke-gen/infrastructure/modules/secrets.py
T
Andrew Beveridge ☄️andClaude Opus 4.8 fb2bd6b108 Cloudflare edge security (WAF + rate limit + origin lock) for karaoke-backend (#886)
## Summary
Puts `api.nomadkaraoke.com` behind the Cloudflare edge that already
fronts the rest of the domain, after a vuln-scanner hammered the
directly-exposed Cloud Run origin on 2026-07-20 (`/.env`,
`/.ssh/id_rsa`, `wp-config.php`… — all 404'd, but the burst of 404s
tripped the "High Error Rate" alert). The API was the one
internet-facing surface with **no WAF, no rate limiting** — DNS-only
CNAME straight to `ghs.googlehosted.com`, Cloud Run ingress `all`.

**Zero-downtime by construction:** everything is gated behind
`edge:enabled` (default off) and staged via two independent switches
(`edge:rolloutStage` for rule scope, `edge:proxyProdApi` for the DNS
flip). The whole Cloud-Run×Cloudflare interaction is validated on a
throwaway staging host first; the only prod-affecting action is a
single, instantly-reversible DNS proxy flip. Full sequence in
`docs/archive/2026-07-20-edge-security-cutover-runbook.md`.

## Changes
- **`infrastructure/modules/edge_security.py`** (new) —
`pulumi_cloudflare` WAF (exploit-path block), per-IP rate limiting
(excludes `/api/internal/*`), origin-lock header transform, zone
security; staging + prod DNS records. Managed OWASP ruleset gated off
(zone is Free plan).
- **`backend/middleware/edge_auth.py`** (new) — off/warn/enforce origin
lock via a secret header Cloudflare injects; **fails closed (503) in
enforce** when the secret is missing; health/root exempt. +13 unit
tests.
- **`monitoring.py`** — retune the high-error-rate alert to **5xx-only**
(+ fix the rate-vs-ratio naming) so client/bot 404 bursts no longer
page.
- Wiring: `secrets.py` (edge-origin-secret container), `config.py`,
`__main__.py`, pinned `pulumi-cloudflare==6.18.0`, `ci.yml`
(`EDGE_AUTH_MODE=off` + secret ref), `Pulumi.prod.yaml` (encrypted
`edge:originSecret`).
- Docs: hardening plan + cutover runbook in `docs/archive/`.

## Already applied to prod (targeted `pulumi up`)
- ✅ `edge-origin-secret` Secret Manager container + v1 value
- ✅ 5xx error-rate alert retune (fixes the original paging issue
immediately)
- Everything else stays dormant: `edge:enabled=false`, backend
`EDGE_AUTH_MODE=off`.

## Testing
- [x] `make test` — full backend + frontend suite green (1085 frontend
tests; backend incl. 13 new edge-auth tests)
- [x] Cloudflare token verified against live API (DNS create+delete,
rulesets, zone settings)
- [x] `pulumi preview` clean (targeted apply of only the 2 additive
resources)
- [ ] Staging validation + prod cutover — follow the runbook (separate,
gated)

## Review
- [x] CodeRabbit CLI review completed locally; findings addressed
(fail-closed enforce, decoupled cutover, fixed origin-lock test,
threshold wording). One pre-existing PR_TITLE shell-interp finding in
the deploy step left out of scope.

## Notes for reviewer
- Nothing here touches prod behavior until someone sets `edge:enabled
true` and walks the runbook. The prod `api` DNS record must be `pulumi
import`ed (already exists) — documented in runbook step 4a.
- Pre-existing cross-worktree Cloud Function source drift is visible in
a full `pulumi preview`; not introduced here (applied via `--target` to
avoid it).

@coderabbitai ignore

---
🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-20 19:12:28 -04:00

99 lines
3.0 KiB
Python

"""
Secret Manager resources.
Manages all secrets for the application. Secret values are added manually
via gcloud or the Cloud Console after resources are created.
"""
from pulumi_gcp import secretmanager
def create_secrets() -> dict[str, secretmanager.Secret]:
"""
Create all Secret Manager secrets.
Returns:
dict: Dictionary mapping secret names to Secret resources.
"""
# Define all secret names
# Note: Private tracker secrets (red-*, ops-*) are managed by flacfetch repo
secret_names = [
# Audio processing
"audioshake-api-key",
"genius-api-key",
"audio-separator-api-url",
# Payment and email
"stripe-secret-key",
"stripe-webhook-secret",
"postmark-server-token",
# Observability (Langfuse for LLM tracing)
"langfuse-public-key",
"langfuse-secret-key",
"langfuse-host",
# AI auto-correct suggestions (Claude models via Anthropic API)
"anthropic-api-key",
# Service authentication
"flacfetch-api-key",
"flacfetch-api-url",
"encoding-worker-api-key",
# Capacity-fallback VMs config (JSON list of {vm,zone,ip}). Stored as
# a secret because --set-env-vars on Cloud Run is comma-delimited and
# the JSON value contains commas; the value itself is non-secret
# (just public IPs of fallback VMs).
"encoding-worker-fallback-vms",
# Notifications
"pushbullet-api-key",
# Internal service URLs
"gdrive-validator-url",
# Web Push (VAPID keys for mobile push notifications)
"vapid-public-key",
"vapid-private-key",
# GitHub
"github-runner-pat",
"github-webhook-secret", # For runner manager webhook verification
# E2E Testing
"e2e-bypass-key",
# KaraokeNerds API
"karaokenerds-api-key",
# Notifications (Discord)
"discord-alert-webhook",
# Divebar on-demand refresh trigger (shared bearer token: kjbox's
# "Refresh catalog" button -> divebar-lookup `refresh` action). Gates
# an otherwise-public endpoint that re-runs the mirror/sync/xref
# scheduler jobs on demand, so it's anti-abuse, not data-sensitive.
"divebar-refresh-token",
# Cloudflare edge origin lock: shared secret Cloudflare injects as the
# X-Edge-Auth header on proxied requests; the backend edge-auth
# middleware rejects public requests that lack it (blocks direct-to-
# origin bypass of the WAF). Value added manually; also provided to
# Pulumi as `edge:originSecret` for the Cloudflare header transform.
# See modules/edge_security.py.
"edge-origin-secret",
]
secrets = {}
for secret_id in secret_names:
secrets[secret_id] = secretmanager.Secret(
secret_id,
secret_id=secret_id,
replication=secretmanager.SecretReplicationArgs(
auto=secretmanager.SecretReplicationAutoArgs(),
),
)
return secrets