Separate relay API and tunnel zones

This commit is contained in:
Julius Marminge committed 2026-06-04 17:24:57 -07:00
1 parent 1bb073742b
commit 640dc63eda
13 files changed
+74 -74

No files matched your search

+2 -1
View File
@@ -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 }}
+8 -2
View File
@@ -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.<RELAY_API_ZONE_NAME>` 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:
+4 -3
View File
@@ -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.
+6 -5
View File
@@ -829,8 +829,8 @@ sequenceDiagram
</tbody>
</table>
<div class="callout warning">
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.
</div>
</section>
@@ -1038,9 +1038,10 @@ sequenceDiagram
<tr>
<td>Relay infrastructure</td>
<td>
<code>RELAY_ZONE_NAME</code>, optional <code>RELAY_DOMAIN</code>,
<code>CLERK_PUBLISHABLE_KEY</code>, <code>CLERK_SECRET_KEY</code>,
<code>CLERK_JWT_AUDIENCE</code>, and APNs credentials.
<code>RELAY_API_ZONE_NAME</code>, <code>RELAY_TUNNEL_ZONE_NAME</code>, optional
<code>RELAY_DOMAIN</code>, <code>CLERK_PUBLISHABLE_KEY</code>,
<code>CLERK_SECRET_KEY</code>, <code>CLERK_JWT_AUDIENCE</code>, and APNs
credentials.
</td>
</tr>
<tr>
+6 -4
View File
@@ -1,7 +1,9 @@
# Required: Relay domain
# Use the DNS zone managed in your Cloudflare account. Production deploys use
# relay.<zone>; personal stages use relay-<stage>.<zone>.
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.<zone> in production and relay-<stage>.<zone> 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.
+11 -4
View File
@@ -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.<RELAY_ZONE_NAME>` and `dev_julius` uses `relay-dev-julius.<RELAY_ZONE_NAME>`.
`RELAY_DOMAIN` remains available as an explicit override.
`relay.<RELAY_API_ZONE_NAME>` and `dev_julius` uses
`relay-dev-julius.<RELAY_API_ZONE_NAME>`. Managed environment endpoints are provisioned below
`RELAY_TUNNEL_ZONE_NAME`, which may be a different Cloudflare zone. Production tunnel hostnames use
`prod-<digest>.<RELAY_TUNNEL_ZONE_NAME>`; personal stages use
`<stage>-<digest>.<RELAY_TUNNEL_ZONE_NAME>`. `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`
+5 -3
View File
@@ -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,
};
}),
);
+9 -6
View File
@@ -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);
});
});
+1 -5
View File
@@ -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)}`;
}
@@ -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" },
],
+4 -1
View File
@@ -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<Api>()(
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<Api>()(
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;
-36
View File
@@ -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",
});
});
});
+16 -2
View File
@@ -21,18 +21,20 @@ function withLogicalId<Resource extends object>(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,
}),
),
);