Skip to content

feat(server): production hosts refuse unsafe config and expose a deep health check - #519

Merged
allisonmahmood merged 1 commit into
mainfrom
infra/415-server
Oct 4, 2026
Merged

allisonmahmood merged 1 commit into
mainfrom
infra/415-server

Conversation

@allisonmahmood

Copy link
Copy Markdown
Owner

What changed

A production host could start in ways that lose data or trust. Without PATCHY_S3_BUCKET it kept patch bytes on the container's disk, which the next deploy throws away; it accepted a plain-HTTP public origin; and nothing kept Clerk's test keys out of production. /healthz always answered ok, so an uptime check could see neither the database nor which build was live.

Before it touches Postgres, a NODE_ENV=production host now refuses a non-HTTPS PATCHY_PUBLIC_BASE_URL and a missing or incomplete Neon Object Storage configuration. With PATCHY_ENVIRONMENT=production it also refuses pk_test_/sk_test_ keys; staging and the tier 2 spike keep theirs. Each refusal is a config error that names the setting and never prints a key.

GET /healthz/deep (Serving) answers { ok, revision }: 200 while the platform database answers within two seconds, 503 otherwise, naming PATCHY_DEPLOYMENT_REVISION either way. /healthz stays shallow for the load balancer and ECS, so a database blip does not make ECS replace every host. Neither probe emits wide events.

First of three stacked PRs for #415, based on main. Part of #415.

How it was verified

  • pnpm check and pnpm verify pass locally
  • Added tests: the Session refusals (and that production boots on HTTPS with live keys, staging with test keys), the entrypoint refusing production without Object Storage before Postgres, the deep check over a live database and over one that refuses connections, and no wide events for either probe

Release impact

  • Breaking — a NODE_ENV=production server no longer starts on a plain-HTTP origin or without Neon Object Storage, and PATCHY_ENVIRONMENT=production needs live Clerk keys. The tier 2 spike already meets all three.
  • Not breaking

Made by Claude Opus 5.5 (1M context) via Claude Code, in T3 Code.

… health check

A production host fell back to the container's disk without
PATCHY_S3_BUCKET, accepted a plain-HTTP public origin and Clerk's test
keys, and /healthz answered ok even with the database gone.

Before it touches Postgres, a NODE_ENV=production host now refuses a
non-HTTPS PATCHY_PUBLIC_BASE_URL and a missing or incomplete Neon
Object Storage configuration. With PATCHY_ENVIRONMENT=production it
also refuses pk_test_/sk_test_ keys; staging and the spike keep theirs.

GET /healthz/deep answers 200 only while the platform database does,
503 otherwise, and names PATCHY_DEPLOYMENT_REVISION either way. /healthz
stays shallow for the load balancer and ECS. Neither probe emits wide
events.

Part of #415.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@github-actions github-actions Bot added area:server Touches the server area area:docs Touches the docs area size:m 100-500 changed lines labels Oct 4, 2026
@allisonmahmood

Copy link
Copy Markdown
Owner Author

Codex review (GPT 6.1 Sol, xhigh), before the first push. Two findings; both dismissed.

  1. Minor: /healthz/deep's Effect.timeout waits for the SQL driver's cancellation, so a hung connection answers 503 later than two seconds. Dismissed. Route 53 counts any answer after two seconds as a failed check, so the probe's outcome is the same. Answering sooner would need a detached query fiber that outlives the request, and under a hung database those would accumulate. The docs did promise "503 … after two seconds", so they now say "200 only while the database answers within two seconds, 503 otherwise".
  2. Nit: the Co-Authored-By trailer and the author address in commit metadata. Dismissed. Every commit on main carries the same author address and trailer; it is the repo's attribution convention, and both are already public.

@allisonmahmood
allisonmahmood merged commit 922de67 into main Oct 4, 2026
13 checks passed
@allisonmahmood
allisonmahmood deleted the infra/415-server branch October 4, 2026 19:22
allisonmahmood added a commit that referenced this pull request Oct 4, 2026
# What changed

Production had no infrastructure in code. The tier 2 spike runs on a hand-built stack driven by `scripts/tier2-spike.mjs`, and nothing describes the network, load balancer, hosts, exec task definition, secrets or alerts that #416 will deploy.

`apps/infra` is a CDK app with two stacks per environment, named from context (`-c environment=production`), so a staging stack later is one more command. Nothing in it is deployed by this PR.

**Base stack** (deploys first, outlives releases): the ECR repository with immutable tags and the last 30 images kept; the GitHub OIDC provider and a deploy role trusted only for this repo's `production` environment; the $300 forecast budget emailing the alert addresses; the host secret, declared without a value; and the fleet management secret with two slots, `a` (generated once) and `b` (empty until a rotation writes into it). The provider and budget are account-wide, so only production declares them.

**App stack** (one release, pinned to an image digest):
- VPC: two public subnets for the ALB and hosts (public IPs, no NAT), and an isolated exec subnet whose only ways out are ECR API/Docker and Logs interface endpoints and an S3 gateway endpoint. No Secrets Manager endpoint.
- Security groups: ALB, host, `exec-bootstrap`, `exec-sealed` and endpoints, with the rules from `docs/OPERATIONS.md`. The endpoint group admits the hosts too, since private DNS covers the whole VPC (the prototype's live finding).
- ALB: 443 with the existing certificate (from context), 80 redirecting, HTTP/2, 3,600 s idle timeout. The target group has app-cookie stickiness on `patchy_stream_affinity`, a 90 s deregistration delay and the shallow `/healthz` check.
- Hosts: two tasks at 0.5 vCPU / 2 GB, rolling 100/200, circuit breaker **with rollback**, user 1000, stop timeout 120 s. Settings come from the host secret by key; the host task role is the fleet policy, scoped to this cluster and the exec family.
- Exec: a `CfnTaskDefinition` with **no task role**, user 0, the spike's capability drops, port 8788, and its own execution role (ECR pull and logs only).
- Uptime: a Route 53 HTTPS check on `/healthz/deep` → CloudWatch alarm → SNS email.
- Host config: `NODE_ENV=production`, `PATCHY_ENVIRONMENT`, the public origin and authorized party, `PATCHY_TRUST_PROXY` set to the VPC range, fleet budget 15, the deployment revision, and the `ECS_*`/`EXECUTION_*` settings from the stack.

**Tests and CI.** A vitest synthesizes both stacks offline and snapshots every security group and rule. It also pins the stickiness and ALB attributes, circuit-breaker rollback, the credential-free exec definition, the previous-release mapping, and the secrets' shapes. CI's `lint` job and `pnpm check` run `cdk synth` with example context; it runs with no network at all (no credentials, lookups, notices or telemetry). Account ids, the certificate ARN and alert addresses come only from context.

**Choices worth checking**
- **The host secret is in the base stack, not the app stack.** Hosts can't start until a person fills it, so an app stack that created it would fail its first deploy and roll the secret away. This matches #416's order: deploy the base stack, fill the secret, then the first release.
- **Every release names the slot its hosts read, and the promoted release as previous** (its revision, exec task definition and slot). Until promotion the fleet still replenishes the promoted revision's spares, and a host that can't launch that revision would reserve tasks it can never start or stop, holding fleet budget for good. For the same reason, replaced exec task definitions are retained (`UpdateReplacePolicy: Retain`). The deploy role may read and write the SSM parameter where PR 3's workflow records the promoted release, and stop promote tasks a cancelled run left behind.
- **Closed egress.** CDK drops its inline "no traffic" egress placeholder when every egress rule of a group names another group, which leaves AWS's default allow-all in place. The ALB and `exec-bootstrap` groups put it back, and the test fails if any group but the hosts' lacks inline egress.
- `exec-sealed` keeps the spike's one rule (8788 from hosts) and no egress. The S3 prefix list `pl-63a5400a` is AWS's managed list for us-east-1, the same in every account.

The uptime check targets #519's `/healthz/deep`, and the hosts set #519's `PATCHY_ENVIRONMENT`. Part of #415.

## How it was verified

- [x] `pnpm check` and `pnpm verify` pass locally; `cdk synth` runs inside a network-less namespace
- [x] Added `apps/infra/src/stacks.test.ts` (security-group snapshot and contract checks)

## Release impact

- [ ] **Breaking**
- [x] **Not breaking** — a new package; nothing is deployed

Made by Claude Opus 5.5 (1M context) via Claude Code, in T3 Code.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:docs Touches the docs area area:server Touches the server area size:m 100-500 changed lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant