diff --git a/.github/workflows/deploy-relay.yml b/.github/workflows/deploy-relay.yml index dd27fd2a79..d789b3062a 100644 --- a/.github/workflows/deploy-relay.yml +++ b/.github/workflows/deploy-relay.yml @@ -25,7 +25,8 @@ jobs: PLANETSCALE_ORGANIZATION: ${{ vars.PLANETSCALE_ORGANIZATION }} AXIOM_ORG_ID: ${{ vars.AXIOM_ORG_ID }} RELAY_DOMAIN: ${{ vars.RELAY_DOMAIN }} - RELAY_ZONE_NAME: ${{ vars.RELAY_ZONE_NAME }} + RELAY_API_ZONE_NAME: ${{ vars.RELAY_API_ZONE_NAME }} + RELAY_TUNNEL_ZONE_NAME: ${{ vars.RELAY_TUNNEL_ZONE_NAME }} CLERK_PUBLISHABLE_KEY: ${{ vars.CLERK_PUBLISHABLE_KEY }} CLERK_JWT_AUDIENCE: ${{ vars.CLERK_JWT_AUDIENCE }} APNS_ENVIRONMENT: ${{ vars.APNS_ENVIRONMENT }} diff --git a/docs/release.md b/docs/release.md index a90c67ee60..c9b70f215e 100644 --- a/docs/release.md +++ b/docs/release.md @@ -55,8 +55,8 @@ Required repository secrets shared by relay deployments: Required `production` environment variables: -- `RELAY_DOMAIN` -- `RELAY_ZONE_NAME` +- `RELAY_API_ZONE_NAME` +- `RELAY_TUNNEL_ZONE_NAME` - `CLERK_PUBLISHABLE_KEY` - `CLERK_JWT_AUDIENCE` - `CLERK_JWT_TEMPLATE` @@ -66,6 +66,10 @@ Required `production` environment variables: - `APNS_KEY_ID` - `APNS_BUNDLE_ID` +Optional `production` environment variables: + +- `RELAY_DOMAIN` when overriding the derived `relay.` domain + Required `production` environment secrets: - `CLERK_SECRET_KEY` @@ -75,6 +79,8 @@ The account-scoped repository credentials are consumed by Alchemy while provisio are not bound into the relay Worker. The production deployment uses an Axiom personal access token, so `AXIOM_ORG_ID` must accompany `AXIOM_TOKEN`. The `prod` stage owns the retained PlanetScale database. Local personal stages provision isolated branches from it and are never deployed by CI. +Production adopts the configured relay API and tunnel DNS zones as retained Cloudflare resources. +Personal stages reference the production-owned zones. Developers deploy personal stages locally rather than through pull-request automation: diff --git a/docs/t3-cloud-clerk.md b/docs/t3-cloud-clerk.md index 09c9215058..e95c645355 100644 --- a/docs/t3-cloud-clerk.md +++ b/docs/t3-cloud-clerk.md @@ -41,11 +41,12 @@ overrides for self-hosted or operator-managed deployments. For a hosted relay deployment, copy `infra/relay/.env.example` to `infra/relay/.env`. The relay -deployment reads `RELAY_DOMAIN`, `RELAY_ZONE_NAME`, `CLERK_PUBLISHABLE_KEY`, and -`CLERK_JWT_AUDIENCE` through Effect `Config`. There are no checked-in deployment defaults. +deployment reads `RELAY_DOMAIN`, `RELAY_API_ZONE_NAME`, `RELAY_TUNNEL_ZONE_NAME`, +`CLERK_PUBLISHABLE_KEY`, and `CLERK_JWT_AUDIENCE` through Effect `Config`. There are no checked-in +deployment defaults. `vp run --filter t3code-relay deploy` invokes Alchemy from the relay directory, so Alchemy loads `infra/relay/.env`. After a successful deployment, the wrapper updates the repository-root `.env` -with the HTTPS relay URL derived from `RELAY_DOMAIN`. The relay still requires +with the deployed HTTPS relay URL. The relay still requires `CLERK_SECRET_KEY` as an Alchemy secret. Never put `CLERK_SECRET_KEY` in a client application environment or commit it to the repository. diff --git a/docs/t3-code-cloud-auth-flow.html b/docs/t3-code-cloud-auth-flow.html index 8b9f5ac40a..030a9c15db 100644 --- a/docs/t3-code-cloud-auth-flow.html +++ b/docs/t3-code-cloud-auth-flow.html @@ -829,8 +829,8 @@ sequenceDiagram
- Managed tunnel hostnames currently live under the configured relay DNS zone. Serving them - from a dedicated registrable domain with a Public Suffix List entry remains an operational + Managed tunnel hostnames live under the configured tunnel DNS zone. Serving them from a + dedicated registrable domain with a Public Suffix List entry remains an operational isolation requirement before broad untrusted use.
@@ -1038,9 +1038,10 @@ sequenceDiagram Relay infrastructure - RELAY_ZONE_NAME, optional RELAY_DOMAIN, - CLERK_PUBLISHABLE_KEY, CLERK_SECRET_KEY, - CLERK_JWT_AUDIENCE, and APNs credentials. + RELAY_API_ZONE_NAME, RELAY_TUNNEL_ZONE_NAME, optional + RELAY_DOMAIN, CLERK_PUBLISHABLE_KEY, + CLERK_SECRET_KEY, CLERK_JWT_AUDIENCE, and APNs + credentials. diff --git a/infra/relay/.env.example b/infra/relay/.env.example index c63cf71985..7ab2a5d6e4 100644 --- a/infra/relay/.env.example +++ b/infra/relay/.env.example @@ -1,7 +1,9 @@ -# Required: Relay domain -# Use the DNS zone managed in your Cloudflare account. Production deploys use -# relay.; personal stages use relay-.. -RELAY_ZONE_NAME=example.com +# Required: Relay API and managed tunnel domains +# Production adopts retained DNS zones in your Cloudflare account. The API +# zone serves relay. in production and relay-. for personal +# stages. Managed environment endpoints are provisioned below the tunnel zone. +RELAY_API_ZONE_NAME=example.com +RELAY_TUNNEL_ZONE_NAME=tunnels.example.com # Optional: Relay domain override # Set this only when the derived relay hostname should not be used. diff --git a/infra/relay/README.md b/infra/relay/README.md index 823a1ffe52..44831cb4c3 100644 --- a/infra/relay/README.md +++ b/infra/relay/README.md @@ -85,7 +85,9 @@ vp run --filter t3code-relay deploy The stack provisions the Cloudflare Worker and queues, managed endpoint resources, database connectivity, and relay tracing resources. Copy [`infra/relay/.env.example`](./.env.example) to `infra/relay/.env` and fill in the deployment-specific values before deploying. Alchemy loads that -file from the relay directory. Runtime secrets include Clerk and APNs credentials. +file from the relay directory. Runtime secrets include Clerk and APNs credentials. Production adopts +the configured API and tunnel DNS zones as retained Cloudflare resources. Personal stages reference +the production-owned zones. The `prod` Alchemy stage owns the retained PlanetScale database and is the shared hosted relay for stable and nightly clients. Every other stage references that database and provisions an isolated @@ -99,8 +101,12 @@ vp run --filter t3code-relay deploy -- --env-file .env.local Alchemy defaults personal deployments to the `dev_$USER` stage. Relay custom domains apply the same DNS-safe sanitization as Alchemy physical resource names, so `prod` uses -`relay.` and `dev_julius` uses `relay-dev-julius.`. -`RELAY_DOMAIN` remains available as an explicit override. +`relay.` and `dev_julius` uses +`relay-dev-julius.`. Managed environment endpoints are provisioned below +`RELAY_TUNNEL_ZONE_NAME`, which may be a different Cloudflare zone. Production tunnel hostnames use +`prod-.`; personal stages use +`-.`. `RELAY_DOMAIN` remains available as an explicit API +domain override. After a successful deploy, the wrapper updates the repository-root `.env` file with the derived relay URL. That makes subsequent source builds point at the relay that was just deployed without copying @@ -129,7 +135,8 @@ The repository must define these Actions secrets shared by relay deployments: The `production` GitHub environment must define these Actions variables: -- `RELAY_ZONE_NAME` +- `RELAY_API_ZONE_NAME` +- `RELAY_TUNNEL_ZONE_NAME` - `RELAY_DOMAIN` if overriding the derived production relay domain - `CLERK_PUBLISHABLE_KEY` - `CLERK_JWT_AUDIENCE` diff --git a/infra/relay/alchemy.run.ts b/infra/relay/alchemy.run.ts index 472d6b95a5..bdc7de8c16 100644 --- a/infra/relay/alchemy.run.ts +++ b/infra/relay/alchemy.run.ts @@ -8,7 +8,7 @@ import * as Layer from "effect/Layer"; import * as Planetscale from "alchemy/Planetscale"; import { PlanetscaleDatabase, RelayHyperdrive } from "./src/db.ts"; -import { ManagedEndpointZone } from "./src/zone.ts"; +import { ManagedEndpointZone, RelayApiZone } from "./src/zone.ts"; import Api from "./src/worker.ts"; export default Alchemy.Stack( @@ -25,7 +25,8 @@ export default Alchemy.Stack( Effect.gen(function* () { const db = yield* PlanetscaleDatabase; const hyperdrive = yield* RelayHyperdrive; - const zone = yield* ManagedEndpointZone.pipe(Effect.orDie); + const managedEndpointZone = yield* ManagedEndpointZone.pipe(Effect.orDie); + const relayApiZone = yield* RelayApiZone.pipe(Effect.orDie); const api = yield* Api; return { @@ -34,7 +35,8 @@ export default Alchemy.Stack( hyperdriveName: hyperdrive.name, workerName: api.workerName, url: api.url, - managedEndpointZoneId: zone.zoneId, + relayApiZoneId: relayApiZone.zoneId, + managedEndpointZoneId: managedEndpointZone.zoneId, }; }), ); diff --git a/infra/relay/src/deploymentConfig.test.ts b/infra/relay/src/deploymentConfig.test.ts index 1b69d55058..b83e750ad0 100644 --- a/infra/relay/src/deploymentConfig.test.ts +++ b/infra/relay/src/deploymentConfig.test.ts @@ -56,7 +56,10 @@ describe("managed endpoint names", () => { "dev_julius:user_123:env_123", ); expect(managedEndpointHostname("dev_julius", ".example.com.", hash)).toBe( - "tunnels-dev-julius-abcdef0123456789.example.com", + "dev-julius-abcdef0123456789.example.com", + ); + expect(managedEndpointHostname("prod", "t3coderelay.com", hash)).toBe( + "prod-abcdef0123456789.t3coderelay.com", ); expect(managedEndpointTunnelName("dev_julius", hash)).toBe( "t3coderelay-managedendpoint-dev-julius-abcdef0123456789", @@ -76,11 +79,11 @@ describe("managed endpoint names", () => { it("accepts allocated hostnames within the relay zone", () => { expect( - isManagedEndpointHostname("tunnels-dev-julius-abcdef0123456789.example.com", "example.com"), + isManagedEndpointHostname("dev-julius-abcdef0123456789.example.com", "example.com"), ).toBe(true); - expect(managedEndpointForHostname("tunnels-dev-julius-abcdef0123456789.example.com")).toEqual({ - httpBaseUrl: "https://tunnels-dev-julius-abcdef0123456789.example.com/", - wsBaseUrl: "wss://tunnels-dev-julius-abcdef0123456789.example.com/ws", + expect(managedEndpointForHostname("dev-julius-abcdef0123456789.example.com")).toEqual({ + httpBaseUrl: "https://dev-julius-abcdef0123456789.example.com/", + wsBaseUrl: "wss://dev-julius-abcdef0123456789.example.com/ws", providerKind: "cloudflare_tunnel", }); }); @@ -88,6 +91,6 @@ describe("managed endpoint names", () => { it("rejects hostnames outside the relay zone", () => { expect(isManagedEndpointHostname("internal.example.net", "example.com")).toBe(false); expect(isManagedEndpointHostname("example.com.attacker.test", "example.com")).toBe(false); - expect(isManagedEndpointHostname("tunnels-dev-julius.example.com.", "example.com")).toBe(false); + expect(isManagedEndpointHostname("dev-julius.example.com.", "example.com")).toBe(false); }); }); diff --git a/infra/relay/src/deploymentConfig.ts b/infra/relay/src/deploymentConfig.ts index 253fcaf345..fbb1305482 100644 --- a/infra/relay/src/deploymentConfig.ts +++ b/infra/relay/src/deploymentConfig.ts @@ -2,7 +2,6 @@ import type { RelayManagedEndpoint } from "@t3tools/contracts/relay"; const DNS_LABEL_MAX_LENGTH = 63; const MANAGED_ENDPOINT_HASH_LENGTH = 16; -const MANAGED_ENDPOINT_HOST_PREFIX = "tunnels"; const MANAGED_ENDPOINT_TUNNEL_PREFIX = "t3coderelay-managedendpoint"; export const MANAGED_ENDPOINT_ZONE_OWNER_STAGE = "prod"; @@ -77,10 +76,7 @@ export function managedEndpointDigestInput( } export function managedEndpointHostname(stage: string, baseDomain: string, hash: string): string { - const label = appendDnsSafeSuffix( - `${MANAGED_ENDPOINT_HOST_PREFIX}-${relayStageSlug(stage)}`, - stableSuffix(hash), - ); + const label = appendDnsSafeSuffix(relayStageSlug(stage), stableSuffix(hash)); return `${label}.${normalizeZoneName(baseDomain)}`; } diff --git a/infra/relay/src/environments/ManagedEndpointProvider.test.ts b/infra/relay/src/environments/ManagedEndpointProvider.test.ts index 5d82711745..d9a9db9ce6 100644 --- a/infra/relay/src/environments/ManagedEndpointProvider.test.ts +++ b/infra/relay/src/environments/ManagedEndpointProvider.test.ts @@ -218,7 +218,7 @@ function expectedManagedHostname(environmentId: string, userId = "user_ABC"): st .update(`dev_julius:${userId}:${environmentId}`) .digest("hex") .slice(0, 16); - return `tunnels-dev-julius-${hash}.t3code.test`; + return `dev-julius-${hash}.t3code.test`; } function expectedManagedTunnelName(environmentId: string, userId = "user_ABC"): string { @@ -334,7 +334,7 @@ describe("ManagedEndpointProvider", () => { expect(configBody).toMatchObject({ ingress: [ { - hostname: expect.stringMatching(/^tunnels-dev-julius-[a-f0-9]{16}\.t3code\.test$/), + hostname: expect.stringMatching(/^dev-julius-[a-f0-9]{16}\.t3code\.test$/), }, { service: "http_status:404" }, ], diff --git a/infra/relay/src/worker.ts b/infra/relay/src/worker.ts index 8f00cc452f..d66f618839 100644 --- a/infra/relay/src/worker.ts +++ b/infra/relay/src/worker.ts @@ -31,7 +31,7 @@ import { tokenApi, withoutCapturedParentSpan, } from "./http/Api.ts"; -import { ManagedEndpointZone, RelayDeploymentConfig } from "./zone.ts"; +import { ManagedEndpointZone, RelayApiZone, RelayDeploymentConfig } from "./zone.ts"; import { makeRelayTraceLayer, RelayObservability } from "./observability.ts"; import * as DeliveryAttempts from "./agentActivity/DeliveryAttempts.ts"; import * as AgentActivityRows from "./agentActivity/AgentActivityRows.ts"; @@ -109,6 +109,7 @@ export default class Api extends Cloudflare.Worker()( const apnsDeliveryQueue = yield* RelayApnsDeliveryQueue; const apnsDeliveryDeadLetterQueue = yield* RelayApnsDeliveryDeadLetterQueue; const cloudMintKeyPair = yield* CloudMintKeyPair; + const relayApiZone = yield* RelayApiZone; const managedEndpointZone = yield* ManagedEndpointZone; const randomApnsDeliveryJobSigningSecret = yield* ApnsDeliveryJobSigningSecret; const observability = yield* RelayObservability; @@ -141,6 +142,8 @@ export default class Api extends Cloudflare.Worker()( const db = yield* Drizzle.postgres(hyperdrive.connectionString); const managedEndpointTunnelBinding = yield* Cloudflare.TunnelReadWrite.bind(); + // Keep Worker custom-domain reconciliation ordered after API zone provisioning. + yield* yield* relayApiZone.zoneId; const managedEndpointDnsBinding = yield* Cloudflare.DnsReadWrite.bind(managedEndpointZone); const managedEndpointZoneName = yield* managedEndpointZone.name; diff --git a/infra/relay/src/zone.test.ts b/infra/relay/src/zone.test.ts deleted file mode 100644 index cac7f9e6cb..0000000000 --- a/infra/relay/src/zone.test.ts +++ /dev/null @@ -1,36 +0,0 @@ -import * as Alchemy from "alchemy"; -import * as ConfigProvider from "effect/ConfigProvider"; -import * as Effect from "effect/Effect"; -import { describe, expect, it } from "vitest"; - -import { RelayDeploymentConfig } from "./zone.ts"; - -describe("RelayDeploymentConfig", () => { - it("reads the stage from the stack context available in the Worker runtime", async () => { - const config = await Effect.runPromise( - RelayDeploymentConfig.pipe( - Effect.provideService(Alchemy.Stack, { - name: "T3CodeRelay", - stage: "dev_julius", - bindings: {}, - resources: {}, - actions: {}, - }), - Effect.provide( - ConfigProvider.layer( - ConfigProvider.fromUnknown({ - RELAY_ZONE_NAME: "example.com", - }), - ), - ), - ), - ); - - expect(config).toEqual({ - stage: "dev_julius", - relayPublicDomain: "relay-dev-julius.example.com", - relayPublicOrigin: "https://relay-dev-julius.example.com", - managedEndpointZoneName: "example.com", - }); - }); -}); diff --git a/infra/relay/src/zone.ts b/infra/relay/src/zone.ts index a626e94f72..d6dd5a4d3a 100644 --- a/infra/relay/src/zone.ts +++ b/infra/relay/src/zone.ts @@ -21,18 +21,20 @@ function withLogicalId(resource: Resource, logicalId: s export const RelayDeploymentConfig = Effect.gen(function* () { const { stage } = yield* Alchemy.Stack; - const managedEndpointZoneName = yield* Config.nonEmptyString("RELAY_ZONE_NAME"); + const relayApiZoneName = yield* Config.nonEmptyString("RELAY_API_ZONE_NAME"); + const managedEndpointZoneName = yield* Config.nonEmptyString("RELAY_TUNNEL_ZONE_NAME"); const relayPublicDomainOverride = yield* Config.nonEmptyString("RELAY_DOMAIN").pipe( Config.option, ); const relayPublicDomain = Option.getOrElse(relayPublicDomainOverride, () => - relayPublicDomainForStage(stage, managedEndpointZoneName), + relayPublicDomainForStage(stage, relayApiZoneName), ); return { stage, relayPublicDomain, relayPublicOrigin: `https://${relayPublicDomain}`, + relayApiZoneName, managedEndpointZoneName, }; }); @@ -50,3 +52,15 @@ export const ManagedEndpointZone = RelayDeploymentConfig.pipe( ), ), ); + +export const RelayApiZone = RelayDeploymentConfig.pipe( + Effect.flatMap(({ stage, relayApiZoneName, managedEndpointZoneName }) => + relayApiZoneName === managedEndpointZoneName + ? ManagedEndpointZone + : relayOwnsManagedEndpointZone(stage) + ? Cloudflare.Zone("RelayApiZone", { name: relayApiZoneName }).pipe(adopt(true)) + : Cloudflare.Zone.ref("RelayApiZone", { + stage: MANAGED_ENDPOINT_ZONE_OWNER_STAGE, + }), + ), +);