diff --git a/.env.example b/.env.example index fc67dcef94780..992fbd0ce99b9 100644 --- a/.env.example +++ b/.env.example @@ -1,31 +1,33 @@ -# Optional: T3 Connect source builds -# `cp .env.example .env` enables T3 Connect against the production deployment. -# These are the same public identifiers baked into official release builds, not -# secrets. Remove or comment them out to build with cloud features disabled. -# Do not add server-side secrets to this file. +# Optional: Fork Connect source builds +# Copy to `.env` and fill in your fork-owned deployment values to enable Connect. +# These are public build identifiers, not secrets. Leave commented out or unset +# for a cloudless build (direct and Tailscale pairing remain available). +# Upstream domains and Clerk keys are rejected. Do not add server-side secrets here. -# Production Clerk instance. To use your own, get these from the Clerk Dashboard -# under API keys, JWT templates, and OAuth applications. -T3CODE_CLERK_PUBLISHABLE_KEY=pk_live_Y2xlcmsudDMuY29kZXMk -T3CODE_CLERK_JWT_TEMPLATE=t3-relay -T3CODE_CLERK_CLI_OAUTH_CLIENT_ID=hzxSgY2cH10sDU2r +# Fork Clerk instance (Clerk Dashboard -> API keys, JWT templates, OAuth applications). +# T3CODE_CLERK_PUBLISHABLE_KEY=pk_live_... +# T3CODE_CLERK_JWT_TEMPLATE=t3-relay +# T3CODE_CLERK_CLI_OAUTH_CLIENT_ID=... -# Optional: signed macOS passkey builds. The RP domain defaults to the Frontend API -# hostname encoded in T3CODE_CLERK_PUBLISHABLE_KEY. Set the override only when Clerk -# returns a different RP ID or when multiple domains must be entitled. -# T3CODE_APPLE_TEAM_ID=ABC1234567 -# T3CODE_MACOS_PROVISIONING_PROFILE=/absolute/path/to/t3code.provisionprofile -# T3CODE_CLERK_PASSKEY_RP_DOMAINS=example.clerk.accounts.dev,clerk.example.com +# Fork relay URL and pinned Ed25519 credential-minting public key (single-line +# \n-escaped PEM). Local `infra/relay` deploys update both automatically, and +# `deploy-relay.yml` prints them in its workflow summary. +# T3CODE_RELAY_URL=https://relay. +# T3CODE_CLOUD_MINT_PUBLIC_KEY=-----BEGIN PUBLIC KEY-----\n...\n-----END PUBLIC KEY----- -# Production relay. For a self-hosted relay, `infra/relay` deploys update it -# automatically. -T3CODE_RELAY_URL=https://relay.t3.codes +# Optional: hosted web app origin for browser-based CLI OAuth (`/connect`). +# Omit when no hosted web app is deployed; the CLI automatically uses Clerk's +# device-authorization flow instead. +# T3CODE_HOSTED_APP_URL=https://app. -# Optional: hosted app origin used by the CLI's out-of-band OAuth flow. -# Defaults to https://app.t3.codes; override to test against a staging deployment. -# T3CODE_HOSTED_APP_URL=https://nightly.app.t3.codes +# Optional: signed macOS passkey builds. Signed macOS builds only enable +# Associated Domains passkeys when T3CODE_MACOS_PROVISIONING_PROFILE or +# T3CODE_CLERK_PASSKEY_RP_DOMAINS is set. +# T3CODE_APPLE_TEAM_ID=ABC1234567 +# T3CODE_MACOS_PROVISIONING_PROFILE=/absolute/path/to/s2code.provisionprofile +# T3CODE_CLERK_PASSKEY_RP_DOMAINS=example.clerk.accounts.dev,clerk. -# Public, ingest-only mobile OpenTelemetry configuration. +# Optional: public, ingest-only mobile OpenTelemetry configuration. # T3CODE_MOBILE_OTLP_TRACES_URL=https://api.axiom.co/v1/traces # T3CODE_MOBILE_OTLP_TRACES_DATASET=t3-code-mobile-traces-dev # T3CODE_MOBILE_OTLP_TRACES_TOKEN=xaat-... diff --git a/.github/workflows/deploy-relay.yml b/.github/workflows/deploy-relay.yml index 2bbda54ba8d01..5f751fd509d3f 100644 --- a/.github/workflows/deploy-relay.yml +++ b/.github/workflows/deploy-relay.yml @@ -1,16 +1,192 @@ -name: Relay deployment (disabled) +name: Deploy Connect relay on: + push: + branches: + - main + paths: + - "infra/relay/**" + - "packages/contracts/**" + - "packages/shared/**" + - "packages/client-runtime/**" + - "pnpm-lock.yaml" + - ".github/workflows/deploy-relay.yml" + # Redeploys main after a variable or secret change. Alchemy redeploys the + # Worker when a value it reads changes, so force is rarely needed. workflow_dispatch: + inputs: + force: + description: Redeploy every resource, including ones with no detected changes. + type: boolean + default: false permissions: contents: read + id-token: none + statuses: write + +concurrency: + group: relay-production + cancel-in-progress: false jobs: - unavailable: + deploy_relay: + name: Deploy production relay + # Manual runs from another branch would deploy that branch to production. + # Push runs skip cleanly while RELAY_API_ZONE_NAME is unset at repository scope. + if: >- + github.repository == 'shuv1337/s2code' && + github.ref == 'refs/heads/main' && + (github.event_name == 'workflow_dispatch' || vars.RELAY_API_ZONE_NAME != '') runs-on: ubuntu-24.04 + timeout-minutes: 15 + environment: + name: production + env: + CLOUDFLARE_ACCOUNT_ID: ${{ vars.CLOUDFLARE_ACCOUNT_ID }} + PLANETSCALE_ORGANIZATION: ${{ vars.PLANETSCALE_ORGANIZATION }} + AXIOM_ORG_ID: ${{ vars.AXIOM_ORG_ID }} + RELAY_DOMAIN: ${{ vars.RELAY_DOMAIN }} + RELAY_API_ZONE_NAME: ${{ vars.RELAY_API_ZONE_NAME }} + RELAY_TUNNEL_ZONE_NAME: ${{ vars.RELAY_TUNNEL_ZONE_NAME }} + RELAY_DB_HOST: ${{ vars.RELAY_DB_HOST }} + RELAY_DB_PORT: ${{ vars.RELAY_DB_PORT }} + RELAY_DB_NAME: ${{ vars.RELAY_DB_NAME }} + RELAY_DB_USER: ${{ vars.RELAY_DB_USER }} + RELAY_DB_CONNECTION_LIMIT: ${{ vars.RELAY_DB_CONNECTION_LIMIT }} + RELAY_DB_MIGRATION_SSLMODE: ${{ vars.RELAY_DB_MIGRATION_SSLMODE }} + RELAY_DB_CLUSTER_SIZE: ${{ vars.RELAY_DB_CLUSTER_SIZE }} + RELAY_DB_REPLICAS: ${{ vars.RELAY_DB_REPLICAS }} + RELAY_OTLP_TRACES_URL: ${{ vars.RELAY_OTLP_TRACES_URL }} + RELAY_TUNNEL_CLEANUP_MODE: ${{ vars.RELAY_TUNNEL_CLEANUP_MODE }} + RELAY_LEGACY_TUNNEL_CLEANUP_MODE: ${{ vars.RELAY_LEGACY_TUNNEL_CLEANUP_MODE }} + CLERK_PUBLISHABLE_KEY: ${{ vars.CLERK_PUBLISHABLE_KEY }} + CLERK_JWT_AUDIENCE: ${{ vars.CLERK_JWT_AUDIENCE }} + APNS_ENABLED: ${{ vars.APNS_ENABLED || 'false' }} + ALCHEMY_TELEMETRY_DISABLED: "1" steps: - - name: Hosted relay deployment unavailable + - name: Checkout + uses: actions/checkout@d23441a48e516b6c34aea4fa41551a30e30af803 # v6.1.0 + with: + sparse-checkout: | + /* + !/.repos/ + sparse-checkout-cone-mode: false + + - name: Setup Vite+ + uses: voidzero-dev/setup-vp@250f29ce396baf5e8f24498e17c0dfdebabc26eb # v1.15.0 + with: + node-version-file: package.json + cache: true + run-install: | + args: + - --filter=t3code-relay... + + - name: Verify fork relay ownership configuration + env: + RELAY_DB_PASSWORD: ${{ secrets.RELAY_DB_PASSWORD }} + RELAY_DB_ACCESS_CLIENT_ID: ${{ secrets.RELAY_DB_ACCESS_CLIENT_ID }} + RELAY_DB_ACCESS_CLIENT_SECRET: ${{ secrets.RELAY_DB_ACCESS_CLIENT_SECRET }} + AXIOM_TOKEN: ${{ secrets.AXIOM_TOKEN }} + run: node --input-type=module -e 'import { assertForkRelayDeploymentEnv } from "./infra/relay/src/forkOwnership.ts"; assertForkRelayDeploymentEnv(process.env);' + + - name: Run external Postgres migrations + if: env.RELAY_DB_HOST != '' + env: + RELAY_DB_PASSWORD: ${{ secrets.RELAY_DB_PASSWORD }} + TUNNEL_SERVICE_TOKEN_ID: ${{ secrets.RELAY_DB_ACCESS_CLIENT_ID }} + TUNNEL_SERVICE_TOKEN_SECRET: ${{ secrets.RELAY_DB_ACCESS_CLIENT_SECRET }} + run: | + set -euo pipefail + if [ -n "$TUNNEL_SERVICE_TOKEN_ID" ] || [ -n "$TUNNEL_SERVICE_TOKEN_SECRET" ]; then + if [ -z "$TUNNEL_SERVICE_TOKEN_ID" ] || [ -z "$TUNNEL_SERVICE_TOKEN_SECRET" ]; then + echo "::error::RELAY_DB_ACCESS_CLIENT_ID and RELAY_DB_ACCESS_CLIENT_SECRET must both be set." >&2 + exit 1 + fi + cf_bin="$RUNNER_TEMP/cloudflared" + curl -fsSL --retry 3 -o "$cf_bin" \ + "https://github.com/cloudflare/cloudflared/releases/download/2026.10.4/cloudflared-linux-amd64" + echo "ae5e8add7884b0531d66bbca4570d1de491ff6da51e4ebe7eb160cdf241d9e42 $cf_bin" | sha256sum -c - + chmod +x "$cf_bin" + # Service token credentials travel through TUNNEL_SERVICE_TOKEN_* env, never argv. + "$cf_bin" access tcp \ + --hostname "$RELAY_DB_HOST" \ + --url 127.0.0.1:15432 & + cf_pid=$! + trap 'kill "$cf_pid" 2>/dev/null || true' EXIT + node -e ' + const net = require("node:net"); + const deadline = Date.now() + 15_000; + (function probe() { + const socket = net.connect(15432, "127.0.0.1"); + socket.once("connect", () => { socket.destroy(); process.exit(0); }); + socket.once("error", () => { + socket.destroy(); + if (Date.now() >= deadline) { + console.error("Timed out waiting for cloudflared access tcp listener."); + process.exit(1); + } + setTimeout(probe, 200); + }); + })(); + ' + RELAY_DB_MIGRATION_TARGET_HOST="127.0.0.1" \ + RELAY_DB_MIGRATION_TARGET_PORT="15432" \ + RELAY_DB_MIGRATION_SSLMODE="${RELAY_DB_MIGRATION_SSLMODE:-disable}" \ + vp run --filter t3code-relay migrate:postgres + else + RELAY_DB_MIGRATION_SSLMODE="${RELAY_DB_MIGRATION_SSLMODE:-require}" \ + vp run --filter t3code-relay migrate:postgres + fi + + - name: Deploy production relay stage + run: vp run --filter t3code-relay deploy --stage prod --yes --no-input ${{ inputs.force && '--force' || '' }} + env: + # The PublishClientConfig action writes the client env here instead + # of the repo-root .env; nothing on the runner reads it. + T3CODE_RELAY_CLIENT_CONFIG_ENV: ${{ runner.temp }}/relay-client-config.env + CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }} + PLANETSCALE_API_TOKEN_ID: ${{ secrets.PLANETSCALE_API_TOKEN_ID }} + PLANETSCALE_API_TOKEN: ${{ secrets.PLANETSCALE_API_TOKEN }} + AXIOM_TOKEN: ${{ secrets.AXIOM_TOKEN }} + CLERK_SECRET_KEY: ${{ secrets.CLERK_SECRET_KEY }} + RELAY_DB_PASSWORD: ${{ secrets.RELAY_DB_PASSWORD }} + RELAY_DB_ACCESS_CLIENT_ID: ${{ secrets.RELAY_DB_ACCESS_CLIENT_ID }} + RELAY_DB_ACCESS_CLIENT_SECRET: ${{ secrets.RELAY_DB_ACCESS_CLIENT_SECRET }} + RELAY_OTLP_TRACES_TOKEN: ${{ secrets.RELAY_OTLP_TRACES_TOKEN }} + + - name: Publish relay deploy commit status + uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8.0.0 + with: + script: | + await github.rest.repos.createCommitStatus({ + owner: context.repo.owner, + repo: context.repo.repo, + sha: context.sha, + state: "success", + context: "Relay deploy / production", + description: "Relay production deploy completed.", + target_url: `${context.serverUrl}/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}`, + }); + + - name: Publish public relay outputs to summary + env: + CLIENT_CONFIG_ENV: ${{ runner.temp }}/relay-client-config.env run: | - echo '::notice::Hosted relay deployment is not supported in this fork. No resources were provisioned. Direct and Tailscale pairing remain available.' - echo 'Hosted relay deployment is disabled for this fork. See docs/operations/hosted-fork.md.' >> "$GITHUB_STEP_SUMMARY" + set -euo pipefail + node --input-type=module -e ' + import * as fs from "node:fs"; + import * as util from "node:util"; + const file = process.env.CLIENT_CONFIG_ENV; + const summary = process.env.GITHUB_STEP_SUMMARY; + if (!file || !summary) process.exit(0); + if (!fs.existsSync(file)) { + fs.appendFileSync(summary, "### Public relay outputs\n\nUnchanged since previous deploy.\n"); + process.exit(0); + } + const parsed = util.parseEnv(fs.readFileSync(file, "utf8")); + const lines = ["### Public relay outputs", ""]; + if (parsed.T3CODE_RELAY_URL) lines.push(`- \`T3CODE_RELAY_URL\`: \`${parsed.T3CODE_RELAY_URL}\``); + if (parsed.T3CODE_CLOUD_MINT_PUBLIC_KEY) lines.push(`- \`T3CODE_CLOUD_MINT_PUBLIC_KEY\`: \`${parsed.T3CODE_CLOUD_MINT_PUBLIC_KEY}\``); + fs.appendFileSync(summary, `${lines.join("\n")}\n`); + ' diff --git a/.github/workflows/encrypted-windows-build.yml b/.github/workflows/encrypted-windows-build.yml index 05efa484f46de..054feb8b492fc 100644 --- a/.github/workflows/encrypted-windows-build.yml +++ b/.github/workflows/encrypted-windows-build.yml @@ -24,6 +24,12 @@ jobs: AGE_RECIPIENT: ${{ inputs.recipient }} S2CODE_DESKTOP_UPDATE_REPOSITORY: shuv1337/s2code CSC_IDENTITY_AUTO_DISCOVERY: "false" + T3CODE_RELAY_URL: ${{ vars.T3CODE_RELAY_URL }} + T3CODE_CLOUD_MINT_PUBLIC_KEY: ${{ vars.T3CODE_CLOUD_MINT_PUBLIC_KEY }} + T3CODE_CLERK_PUBLISHABLE_KEY: ${{ vars.T3CODE_CLERK_PUBLISHABLE_KEY }} + T3CODE_CLERK_JWT_TEMPLATE: ${{ vars.T3CODE_CLERK_JWT_TEMPLATE }} + T3CODE_CLERK_CLI_OAUTH_CLIENT_ID: ${{ vars.T3CODE_CLERK_CLI_OAUTH_CLIENT_ID }} + T3CODE_HOSTED_APP_URL: ${{ vars.T3CODE_HOSTED_APP_URL }} steps: # Public fetch needs no credential injection or recursive submodule cleanup. # The vendored .repos tree contains incomplete nested submodule metadata. @@ -94,6 +100,7 @@ jobs: shell: bash run: | set -euo pipefail + node --input-type=module -e 'import { resolveForkConnectReleaseConfig } from "./scripts/lib/fork-release.ts"; resolveForkConnectReleaseConfig(process.env);' node scripts/update-release-package-versions.ts "$PRIVATE_BUILD_VERSION" cargo build --locked --release --manifest-path native/resource-monitor/Cargo.toml mkdir -p release-private/linux-resource-monitor diff --git a/.github/workflows/relay-preview-cleanup.yml b/.github/workflows/relay-preview-cleanup.yml index 76b18b3b65d82..b00d113bcab1d 100644 --- a/.github/workflows/relay-preview-cleanup.yml +++ b/.github/workflows/relay-preview-cleanup.yml @@ -24,6 +24,8 @@ jobs: cleanup_pr: name: Tear down relay preview if: >- + github.repository == 'shuv1337/s2code' && + vars.RELAY_PREVIEWS_ENABLED == 'true' && github.ref == 'refs/heads/main' && github.event_name == 'pull_request_target' && ( @@ -100,7 +102,11 @@ jobs: list_closed_previews: name: List closed relay previews - if: github.ref == 'refs/heads/main' && github.event_name != 'pull_request_target' + if: >- + github.repository == 'shuv1337/s2code' && + vars.RELAY_PREVIEWS_ENABLED == 'true' && + github.ref == 'refs/heads/main' && + github.event_name != 'pull_request_target' runs-on: ubuntu-24.04 timeout-minutes: 10 outputs: @@ -124,7 +130,11 @@ jobs: sweep_pr: name: Retry teardown for PR ${{ matrix.number }} needs: list_closed_previews - if: github.ref == 'refs/heads/main' && needs.list_closed_previews.outputs.numbers != '[]' + if: >- + github.repository == 'shuv1337/s2code' && + vars.RELAY_PREVIEWS_ENABLED == 'true' && + github.ref == 'refs/heads/main' && + needs.list_closed_previews.outputs.numbers != '[]' runs-on: ubuntu-24.04 timeout-minutes: 30 strategy: diff --git a/.github/workflows/relay-preview.yml b/.github/workflows/relay-preview.yml index eb6f0bb5afbd4..44badbf73b2c6 100644 --- a/.github/workflows/relay-preview.yml +++ b/.github/workflows/relay-preview.yml @@ -20,6 +20,8 @@ jobs: deploy_preview: name: Deploy relay preview if: >- + github.repository == 'shuv1337/s2code' && + vars.RELAY_PREVIEWS_ENABLED == 'true' && github.event.pull_request.head.repo.full_name == github.repository && contains(github.event.pull_request.labels.*.name, 'relay:preview') runs-on: ubuntu-24.04 diff --git a/.github/workflows/release-desktop.yml b/.github/workflows/release-desktop.yml index 0d73ac82cbde6..71434871e3ab0 100644 --- a/.github/workflows/release-desktop.yml +++ b/.github/workflows/release-desktop.yml @@ -102,6 +102,12 @@ on: relay_url: required: false type: string + cloud_mint_public_key: + required: false + type: string + hosted_app_url: + required: false + type: string release_publication: required: false default: false @@ -170,6 +176,8 @@ jobs: T3CODE_CLERK_JWT_TEMPLATE: ${{ inputs.clerk_jwt_template }} T3CODE_CLERK_CLI_OAUTH_CLIENT_ID: ${{ inputs.clerk_cli_oauth_client_id }} T3CODE_RELAY_URL: ${{ inputs.relay_url }} + T3CODE_CLOUD_MINT_PUBLIC_KEY: ${{ inputs.cloud_mint_public_key }} + T3CODE_HOSTED_APP_URL: ${{ inputs.hosted_app_url }} AZURE_SIGNING_AUTH: ${{ inputs.platform == 'win' && inputs.win_signed && vars.AZURE_SIGNING_AUTH || '' }} AZURE_SUBSCRIPTION_ID: ${{ inputs.platform == 'win' && inputs.win_signed && vars.AZURE_SUBSCRIPTION_ID || '' }} S2CODE_DESKTOP_UPDATE_REPOSITORY: shuv1337/s2code diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 733f76227fb97..5e76685b0b195 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -77,6 +77,12 @@ jobs: mac_signed: ${{ steps.plan.outputs.mac_signed }} win_signed: ${{ steps.plan.outputs.win_signed }} apple_team_id: ${{ steps.plan.outputs.apple_team_id }} + relay_url: ${{ steps.plan.outputs.relay_url }} + cloud_mint_public_key: ${{ steps.plan.outputs.cloud_mint_public_key }} + clerk_publishable_key: ${{ steps.plan.outputs.clerk_publishable_key }} + clerk_jwt_template: ${{ steps.plan.outputs.clerk_jwt_template }} + clerk_cli_oauth_client_id: ${{ steps.plan.outputs.clerk_cli_oauth_client_id }} + hosted_app_url: ${{ steps.plan.outputs.hosted_app_url }} matrix: ${{ steps.plan.outputs.matrix }} matrix_rest: ${{ steps.split_matrix.outputs.matrix_rest }} matrix_mac_arm64: ${{ steps.split_matrix.outputs.matrix_mac_arm64 }} @@ -116,6 +122,13 @@ jobs: AZURE_TRUSTED_SIGNING_ACCOUNT_NAME: ${{ vars.AZURE_TRUSTED_SIGNING_ACCOUNT_NAME }} AZURE_TRUSTED_SIGNING_CERTIFICATE_PROFILE_NAME: ${{ vars.AZURE_TRUSTED_SIGNING_CERTIFICATE_PROFILE_NAME }} AZURE_TRUSTED_SIGNING_PUBLISHER_NAME: ${{ vars.AZURE_TRUSTED_SIGNING_PUBLISHER_NAME }} + # Public Connect build config from repository variables (all-or-nothing; never from production). + T3CODE_RELAY_URL: ${{ vars.T3CODE_RELAY_URL }} + T3CODE_CLOUD_MINT_PUBLIC_KEY: ${{ vars.T3CODE_CLOUD_MINT_PUBLIC_KEY }} + T3CODE_CLERK_PUBLISHABLE_KEY: ${{ vars.T3CODE_CLERK_PUBLISHABLE_KEY }} + T3CODE_CLERK_JWT_TEMPLATE: ${{ vars.T3CODE_CLERK_JWT_TEMPLATE }} + T3CODE_CLERK_CLI_OAUTH_CLIENT_ID: ${{ vars.T3CODE_CLERK_CLI_OAUTH_CLIENT_ID }} + T3CODE_HOSTED_APP_URL: ${{ vars.T3CODE_HOSTED_APP_URL }} run: | RELEASE_DATE="$(gh api "repos/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID" --jq .created_at)" export RELEASE_DATE @@ -163,7 +176,14 @@ jobs: env: RELEASE_VERSION: ${{ needs.preflight.outputs.version }} run: node scripts/update-release-package-versions.ts "$RELEASE_VERSION" - - run: vp run build:desktop + - env: + T3CODE_RELAY_URL: ${{ needs.preflight.outputs.relay_url }} + T3CODE_CLOUD_MINT_PUBLIC_KEY: ${{ needs.preflight.outputs.cloud_mint_public_key }} + T3CODE_CLERK_PUBLISHABLE_KEY: ${{ needs.preflight.outputs.clerk_publishable_key }} + T3CODE_CLERK_JWT_TEMPLATE: ${{ needs.preflight.outputs.clerk_jwt_template }} + T3CODE_CLERK_CLI_OAUTH_CLIENT_ID: ${{ needs.preflight.outputs.clerk_cli_oauth_client_id }} + T3CODE_HOSTED_APP_URL: ${{ needs.preflight.outputs.hosted_app_url }} + run: vp run build:desktop - uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 with: name: js-bundle @@ -205,6 +225,12 @@ jobs: # APPLE_TEAM_ID is a variable, and this job is outside the release # environment, so the team id comes from preflight rather than `vars`. apple_team_id: ${{ needs.preflight.outputs.apple_team_id }} + relay_url: ${{ needs.preflight.outputs.relay_url }} + cloud_mint_public_key: ${{ needs.preflight.outputs.cloud_mint_public_key }} + clerk_publishable_key: ${{ needs.preflight.outputs.clerk_publishable_key }} + clerk_jwt_template: ${{ needs.preflight.outputs.clerk_jwt_template }} + clerk_cli_oauth_client_id: ${{ needs.preflight.outputs.clerk_cli_oauth_client_id }} + hosted_app_url: ${{ needs.preflight.outputs.hosted_app_url }} # Publish does not require this job: a failed Windows build leaves the Windows # assets and npm package out of the release (see the publish job). GitHub @@ -237,6 +263,12 @@ jobs: mac_signed: ${{ needs.preflight.outputs.mac_signed == 'true' }} win_signed: ${{ needs.preflight.outputs.win_signed == 'true' }} apple_team_id: ${{ needs.preflight.outputs.apple_team_id }} + relay_url: ${{ needs.preflight.outputs.relay_url }} + cloud_mint_public_key: ${{ needs.preflight.outputs.cloud_mint_public_key }} + clerk_publishable_key: ${{ needs.preflight.outputs.clerk_publishable_key }} + clerk_jwt_template: ${{ needs.preflight.outputs.clerk_jwt_template }} + clerk_cli_oauth_client_id: ${{ needs.preflight.outputs.clerk_cli_oauth_client_id }} + hosted_app_url: ${{ needs.preflight.outputs.hosted_app_url }} # macOS arm64 builds once on the self-hosted shuvbot runner (owned by Fleet # Ops; see docs/operations/release.md). If shuvbot never picks it up, the queue @@ -277,6 +309,12 @@ jobs: mac_signed: ${{ needs.preflight.outputs.mac_signed == 'true' }} win_signed: ${{ needs.preflight.outputs.win_signed == 'true' }} apple_team_id: ${{ needs.preflight.outputs.apple_team_id }} + relay_url: ${{ needs.preflight.outputs.relay_url }} + cloud_mint_public_key: ${{ needs.preflight.outputs.cloud_mint_public_key }} + clerk_publishable_key: ${{ needs.preflight.outputs.clerk_publishable_key }} + clerk_jwt_template: ${{ needs.preflight.outputs.clerk_jwt_template }} + clerk_cli_oauth_client_id: ${{ needs.preflight.outputs.clerk_cli_oauth_client_id }} + hosted_app_url: ${{ needs.preflight.outputs.hosted_app_url }} # A queued shuvbot job cannot be cancelled on its own, but job-level # concurrency can. The hosted fallback below shares attempt 1's group (see @@ -349,6 +387,12 @@ jobs: mac_signed: ${{ needs.preflight.outputs.mac_signed == 'true' }} win_signed: ${{ needs.preflight.outputs.win_signed == 'true' }} apple_team_id: ${{ needs.preflight.outputs.apple_team_id }} + relay_url: ${{ needs.preflight.outputs.relay_url }} + cloud_mint_public_key: ${{ needs.preflight.outputs.cloud_mint_public_key }} + clerk_publishable_key: ${{ needs.preflight.outputs.clerk_publishable_key }} + clerk_jwt_template: ${{ needs.preflight.outputs.clerk_jwt_template }} + clerk_cli_oauth_client_id: ${{ needs.preflight.outputs.clerk_cli_oauth_client_id }} + hosted_app_url: ${{ needs.preflight.outputs.hosted_app_url }} mac_arm64_ready: name: macOS arm64 runner acquired diff --git a/apps/server/src/binCli.ts b/apps/server/src/binCli.ts index f5c35619b985e..b7ae5c82b93c5 100644 --- a/apps/server/src/binCli.ts +++ b/apps/server/src/binCli.ts @@ -46,7 +46,7 @@ import { connectCommand, statusCommand, upCommand } from "./cli/up.ts"; const layerCliRuntime = Layer.mergeAll(NodeServices.layer, NodeCrypto.layer, NetService.layer); const connectPublicConfigMissingMessage = - "Hosted Connect is unavailable: configure a fork-owned service first. Upstream accounts are separate. Use s2code pair for direct or Tailscale access."; + "Hosted Connect is not configured in this build. Use `s2code pair` for direct or Tailscale access. Upstream accounts are separate."; class ConnectPublicConfigMissingError extends CliError.UserError { override get message() { diff --git a/apps/server/src/cli/connect.ts b/apps/server/src/cli/connect.ts index eb8c349b34438..40d7b4bbfea8c 100644 --- a/apps/server/src/cli/connect.ts +++ b/apps/server/src/cli/connect.ts @@ -43,7 +43,7 @@ import { PUBLISH_AGENT_ACTIVITY_SECRET, RELAY_URL_SECRET, } from "../cloud/config.ts"; -import { relayUrlConfig } from "../cloud/publicConfig.ts"; +import { optionalHostedAppUrlConfig, relayUrlConfig } from "../cloud/publicConfig.ts"; import * as RelayTracing from "../cloud/relayTracing.ts"; import * as ServerConfig from "../config.ts"; import * as ServerEnvironment from "../environment/ServerEnvironment.ts"; @@ -103,11 +103,13 @@ const authorizeCli = Effect.fn("cloud.cli.authorize")(function* (options: { readonly headless: boolean; }) { const tokens = yield* CliTokenManager.CloudCliTokenManager; - // Without a terminal, no key press can open a browser, so sign in with a code. + const hostedAppUrl = yield* optionalHostedAppUrlConfig; + // Loopback OAuth needs a local interactive terminal and a hosted /connect page. const useOutOfBandOAuth = options.headless || (yield* headlessSessionConfig) || - !(process.stdin.isTTY && process.stdout.isTTY); + !(process.stdin.isTTY && process.stdout.isTTY) || + Option.isNone(hostedAppUrl); if (!useOutOfBandOAuth) { const authorization = yield* tokens.get; if (authorization._tag === "Authorized") { diff --git a/apps/server/src/cloud/publicConfig.test.ts b/apps/server/src/cloud/publicConfig.test.ts index 1e3d4517bd050..f976d09d138ad 100644 --- a/apps/server/src/cloud/publicConfig.test.ts +++ b/apps/server/src/cloud/publicConfig.test.ts @@ -1,11 +1,14 @@ import { assert, it } from "@effect/vitest"; import * as ConfigProvider from "effect/ConfigProvider"; import * as Effect from "effect/Effect"; +import * as Option from "effect/Option"; import * as Result from "effect/Result"; import { hostedAppUrlConfig, + isCloudPublicConfigConfigured, makeCloudCliOAuthConfig, + makeOptionalHostedAppUrlConfig, makeRelayUrlConfig, resolveRelayClientTracingConfig, } from "./publicConfig.ts"; @@ -74,6 +77,73 @@ it.effect("derives direct Clerk OAuth endpoints from statically injected public }), ); +it.effect( + "optionalHostedAppUrlConfig returns None only when unset/blank and rejects invalid or upstream URLs", + () => + Effect.gen(function* () { + assert.isTrue(Option.isNone(yield* makeOptionalHostedAppUrlConfig("").pipe(provideEnv({})))); + assert.isTrue( + Option.isNone( + yield* makeOptionalHostedAppUrlConfig("").pipe( + provideEnv({ T3CODE_HOSTED_APP_URL: " " }), + ), + ), + ); + assert.deepEqual( + yield* makeOptionalHostedAppUrlConfig("").pipe( + provideEnv({ T3CODE_HOSTED_APP_URL: "https://app.fork.test" }), + ), + Option.some("https://app.fork.test"), + ); + assert.deepEqual( + yield* makeOptionalHostedAppUrlConfig("https://embedded.fork.test").pipe(provideEnv({})), + Option.some("https://embedded.fork.test"), + ); + for (const bad of ["https://app.t3.codes", "http://app.fork.test", "not-a-url"]) { + const result = yield* makeOptionalHostedAppUrlConfig("").pipe( + provideEnv({ T3CODE_HOSTED_APP_URL: bad }), + Effect.result, + ); + assert.isTrue(Result.isFailure(result), bad); + } + }), +); + +it("requires a pinned mint public key and does not require a hosted app URL", () => { + const validBase = { + T3CODE_RELAY_URL: "https://relay.fork.test", + T3CODE_CLOUD_MINT_PUBLIC_KEY: "-----BEGIN PUBLIC KEY-----\\nKEY\\n-----END PUBLIC KEY-----", + T3CODE_CLERK_PUBLISHABLE_KEY: `pk_test_${btoa("clerk.fork.test$")}`, + T3CODE_CLERK_CLI_OAUTH_CLIENT_ID: "oauth_client_123", + }; + + assert.isTrue(isCloudPublicConfigConfigured(validBase, {})); + assert.isFalse( + isCloudPublicConfigConfigured( + { + ...validBase, + T3CODE_CLOUD_MINT_PUBLIC_KEY: "", + T3CODE_HOSTED_APP_URL: "https://app.fork.test", + }, + {}, + ), + ); + assert.isFalse( + isCloudPublicConfigConfigured({ ...validBase, T3CODE_RELAY_URL: "https://relay.t3.codes" }, {}), + ); + assert.isTrue( + isCloudPublicConfigConfigured( + {}, + { + relayUrl: validBase.T3CODE_RELAY_URL, + cloudMintPublicKey: validBase.T3CODE_CLOUD_MINT_PUBLIC_KEY, + clerkPublishableKey: validBase.T3CODE_CLERK_PUBLISHABLE_KEY, + clerkCliOAuthClientId: validBase.T3CODE_CLERK_CLI_OAUTH_CLIENT_ID, + }, + ), + ); +}); + it("product opt-out disables relay tracing with either built-in or runtime credentials", () => { const fallback = { tracesUrl: "https://embedded.example.test/v1/traces", diff --git a/apps/server/src/cloud/publicConfig.ts b/apps/server/src/cloud/publicConfig.ts index 1a846af18be3e..8384c748af617 100644 --- a/apps/server/src/cloud/publicConfig.ts +++ b/apps/server/src/cloud/publicConfig.ts @@ -9,6 +9,7 @@ import { normalizeSecureRelayUrl } from "@t3tools/shared/relayUrl"; import * as Config from "effect/Config"; import * as ConfigProvider from "effect/ConfigProvider"; import * as Effect from "effect/Effect"; +import * as Option from "effect/Option"; import * as Schema from "effect/Schema"; import * as SchemaIssue from "effect/SchemaIssue"; import { forkHostedOrigin, forkClerkPublishableKey } from "@t3tools/shared/hostedConfig"; @@ -129,13 +130,33 @@ function makeCloudMintPublicKeyConfig(fallback = buildTimeCloudMintPublicKey) { export const cloudMintPublicKeyConfig = makeCloudMintPublicKeyConfig(); -export const hostedAppUrlConfig = makePublicValueConfig( - "T3CODE_HOSTED_APP_URL", +const buildTimeHostedAppUrl = readBuildTimeValue( typeof __T3CODE_BUILD_HOSTED_APP_URL__ === "undefined" ? DEFAULT_HOSTED_APP_URL : __T3CODE_BUILD_HOSTED_APP_URL__, +); + +export const hostedAppUrlConfig = makePublicValueConfig( + "T3CODE_HOSTED_APP_URL", + buildTimeHostedAppUrl, ).pipe(Config.mapEffect(validateHostedAppUrl)); +/** `None` only when unset or blank; any non-empty value still fails closed if invalid or upstream. */ +export function makeOptionalHostedAppUrlConfig(fallback = buildTimeHostedAppUrl) { + return Config.String("T3CODE_HOSTED_APP_URL").pipe( + Config.option, + Config.mapEffect((runtime) => { + const raw = Option.isSome(runtime) ? runtime.value.trim() : fallback.trim(); + if (raw.length === 0) { + return Effect.succeed(Option.none()); + } + return validateHostedAppUrl(raw).pipe(Effect.asSome); + }), + ); +} + +export const optionalHostedAppUrlConfig = makeOptionalHostedAppUrlConfig(); + function validateHostedAppUrl(value: string) { try { const origin = forkHostedOrigin(value, true); @@ -232,17 +253,28 @@ export function makeCloudCliOAuthConfig({ export const cloudCliOAuthConfig = makeCloudCliOAuthConfig(); -export const hasCloudPublicConfig = Boolean( - forkHostedOrigin(process.env.T3CODE_RELAY_URL || buildTimeRelayUrl) && - forkClerkPublishableKey( - process.env.T3CODE_CLERK_PUBLISHABLE_KEY || buildTimeClerkPublishableKey, - ) && - forkHostedOrigin( - process.env.T3CODE_HOSTED_APP_URL || - (typeof __T3CODE_BUILD_HOSTED_APP_URL__ === "undefined" - ? "" - : __T3CODE_BUILD_HOSTED_APP_URL__), - true, - ) && - (process.env.T3CODE_CLERK_CLI_OAUTH_CLIENT_ID?.trim() || buildTimeClerkCliOAuthClientId), -); +export function isCloudPublicConfigConfigured( + env: Readonly> = process.env, + fallbacks: { + readonly relayUrl?: string; + readonly cloudMintPublicKey?: string; + readonly clerkPublishableKey?: string; + readonly clerkCliOAuthClientId?: string; + } = { + relayUrl: buildTimeRelayUrl, + cloudMintPublicKey: buildTimeCloudMintPublicKey, + clerkPublishableKey: buildTimeClerkPublishableKey, + clerkCliOAuthClientId: buildTimeClerkCliOAuthClientId, + }, +): boolean { + return Boolean( + forkHostedOrigin(env.T3CODE_RELAY_URL?.trim() || fallbacks.relayUrl?.trim()) && + (env.T3CODE_CLOUD_MINT_PUBLIC_KEY?.trim() || fallbacks.cloudMintPublicKey?.trim()) && + forkClerkPublishableKey( + env.T3CODE_CLERK_PUBLISHABLE_KEY?.trim() || fallbacks.clerkPublishableKey?.trim(), + ) && + (env.T3CODE_CLERK_CLI_OAUTH_CLIENT_ID?.trim() || fallbacks.clerkCliOAuthClientId?.trim()), + ); +} + +export const hasCloudPublicConfig = isCloudPublicConfigConfigured(); diff --git a/apps/web/src/cloud/publicConfig.test.ts b/apps/web/src/cloud/publicConfig.test.ts index 3841165fc2f89..8d3fc8289ea1b 100644 --- a/apps/web/src/cloud/publicConfig.test.ts +++ b/apps/web/src/cloud/publicConfig.test.ts @@ -1,6 +1,6 @@ import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test"; -import { hasCloudPublicConfig } from "./publicConfig.ts"; +import { hasCloudPublicConfig, hasConfiguredCloudBuild } from "./publicConfig.ts"; afterEach(() => { vi.unstubAllEnvs(); @@ -18,6 +18,7 @@ describe("hasCloudPublicConfig", () => { vi.stubEnv("VITE_CLERK_PUBLISHABLE_KEY", ""); vi.stubEnv("VITE_CLERK_JWT_TEMPLATE", ""); vi.stubEnv("VITE_T3CODE_RELAY_URL", ""); + expect(hasConfiguredCloudBuild()).toBe(false); expect(hasCloudPublicConfig()).toBe(false); vi.stubEnv("VITE_CLERK_PUBLISHABLE_KEY", `pk_test_${btoa("clerk.fork.test$")}`); @@ -59,6 +60,7 @@ describe("hasCloudPublicConfig", () => { (origin) => { vi.stubGlobal("window", { location: new URL(origin) }); + expect(hasConfiguredCloudBuild()).toBe(true); expect(hasCloudPublicConfig()).toBe(false); }, ); diff --git a/apps/web/src/cloud/publicConfig.ts b/apps/web/src/cloud/publicConfig.ts index a2872e8b91daa..96b1bcdfefbd6 100644 --- a/apps/web/src/cloud/publicConfig.ts +++ b/apps/web/src/cloud/publicConfig.ts @@ -83,6 +83,11 @@ export function resolveRelayTracingConfig() { : null; } +export function hasConfiguredCloudBuild(): boolean { + const config = resolveCloudPublicConfig(); + return Boolean(config.clerkPublishableKey && config.clerkJwtTemplate && config.relayUrl); +} + export function hasCloudPublicConfig(): boolean { const config = resolveCloudPublicConfig(); if (!config.clerkPublishableKey || !config.clerkJwtTemplate || !config.relayUrl) { diff --git a/apps/web/src/components/settings/SavedEnvironmentRows.tsx b/apps/web/src/components/settings/SavedEnvironmentRows.tsx index ecb8ef0412352..7de649701ebf6 100644 --- a/apps/web/src/components/settings/SavedEnvironmentRows.tsx +++ b/apps/web/src/components/settings/SavedEnvironmentRows.tsx @@ -50,7 +50,7 @@ import { resolveServerSelfUpdateCapability, supportsDesktopAppUpdate, } from "~/versionSkew"; -import { hasCloudPublicConfig } from "~/cloud/publicConfig"; +import { hasCloudPublicConfig, hasConfiguredCloudBuild } from "~/cloud/publicConfig"; import { HOSTED_UNAVAILABLE_MESSAGE } from "@t3tools/shared/hostedConfig"; import { useT3ConnectTurnOn } from "~/cloud/useT3ConnectTurnOn"; import { useCloudLinkController } from "~/cloud/useCloudLinkController"; @@ -868,8 +868,20 @@ export function CloudLinkRow({ readonly connectDeviceApproval: ConnectDeviceApprovalControl | null; }) { const gt = useGT(); - if (!hasCloudPublicConfig()) - return ; + if (!hasCloudPublicConfig()) { + return ( + + ); + } if (!canReadRelay) { return ( @@ -906,7 +923,7 @@ function EmptyRemoteEnvironments({ cloudEnabled = true }: { readonly cloudEnable "Click “Add environment” to pair another environment, or connect one from T3 Connect.", ) : gt("Click “Add environment” to pair another environment. {reason}", { - reason: HOSTED_UNAVAILABLE_MESSAGE, + reason: cloudUnavailableReason, })} diff --git a/apps/web/src/i18n/translations/de.json b/apps/web/src/i18n/translations/de.json index 65e7dfb3b8161..df033e6b0c332 100644 --- a/apps/web/src/i18n/translations/de.json +++ b/apps/web/src/i18n/translations/de.json @@ -9582,6 +9582,7 @@ "c6473fd9810fe47f": "Genehmigung für neue T3-Connect-Geräte verlangen", "da7029b74a481832": "Deine Sitzung hat keine Berechtigung, den Zugriff zu verwalten.", "c7a49b7cba491fdc": "Gehostetes Connect", + "be2b49ee8afa65e1": "Die Browser-Anmeldung ist für diesen Ursprung nicht verfügbar. Melde dich in der Desktop-App an oder führe `s2code connect` auf diesem Host aus.", "b0b5de2fdc9514b5": "T3 Connect", "2c2a12fbdc300d03": "Um diese Einstellungen zu bearbeiten, kopple diese Verbindung mit den Berechtigungen „Relay anzeigen“ und „Relay verwalten“.", "3b3d299149f45c3d": "Klicke auf „Umgebung hinzufügen“, um eine weitere Umgebung zu koppeln, oder verbinde eine über T3 Connect.", diff --git a/apps/web/src/i18n/translations/en.json b/apps/web/src/i18n/translations/en.json index 6c4aade56ceb1..8470f965c3538 100644 --- a/apps/web/src/i18n/translations/en.json +++ b/apps/web/src/i18n/translations/en.json @@ -9591,6 +9591,7 @@ "c6473fd9810fe47f": "Require approval for new T3 Connect devices", "da7029b74a481832": "Your session does not have permission to manage access.", "c7a49b7cba491fdc": "Hosted Connect", + "be2b49ee8afa65e1": "Browser sign-in is unavailable on this origin. Sign in from the desktop app or run `s2code connect` on this host.", "b0b5de2fdc9514b5": "T3 Connect", "2c2a12fbdc300d03": "To edit these settings, pair this connection with both View relay and Manage relay permissions.", "3b3d299149f45c3d": "Click “Add environment” to pair another environment, or connect one from T3 Connect.", diff --git a/apps/web/src/i18n/translations/es.json b/apps/web/src/i18n/translations/es.json index 761bd84a32a4b..58c4234c51927 100644 --- a/apps/web/src/i18n/translations/es.json +++ b/apps/web/src/i18n/translations/es.json @@ -9579,6 +9579,7 @@ "c6473fd9810fe47f": "Exigir aprobación para nuevos dispositivos de T3 Connect", "da7029b74a481832": "Tu sesión no tiene permiso para gestionar el acceso.", "c7a49b7cba491fdc": "Connect alojado", + "be2b49ee8afa65e1": "El inicio de sesión en el navegador no está disponible en este origen. Inicia sesión desde la aplicación de escritorio o ejecuta `s2code connect` en este host.", "b0b5de2fdc9514b5": "T3 Connect", "2c2a12fbdc300d03": "Para editar estos ajustes, empareja esta conexión con los permisos Ver relay y Gestionar relay.", "3b3d299149f45c3d": "Haz clic en «Añadir entorno» para emparejar otro entorno, o conecta uno desde T3 Connect.", diff --git a/apps/web/src/i18n/translations/fr.json b/apps/web/src/i18n/translations/fr.json index 5ad6b52030a02..8d80f7ebfb3bf 100644 --- a/apps/web/src/i18n/translations/fr.json +++ b/apps/web/src/i18n/translations/fr.json @@ -9581,6 +9581,7 @@ "c6473fd9810fe47f": "Exiger l’approbation des nouveaux appareils T3 Connect", "da7029b74a481832": "Votre session n’est pas autorisée à gérer l’accès.", "c7a49b7cba491fdc": "Connect hébergé", + "be2b49ee8afa65e1": "La connexion via le navigateur n'est pas disponible sur cette origine. Connectez-vous depuis l'application de bureau ou exécutez `s2code connect` sur cet hôte.", "b0b5de2fdc9514b5": "T3 Connect", "2c2a12fbdc300d03": "Pour modifier ces paramètres, appairez cette connexion avec les autorisations Voir le relais et Gérer le relais.", "3b3d299149f45c3d": "Cliquez sur « Ajouter un environnement » pour appairer un autre environnement, ou connectez-en un depuis T3 Connect.", diff --git a/apps/web/src/i18n/translations/ja.json b/apps/web/src/i18n/translations/ja.json index b37cf5e069c22..18680fb398cb0 100644 --- a/apps/web/src/i18n/translations/ja.json +++ b/apps/web/src/i18n/translations/ja.json @@ -9535,6 +9535,7 @@ "c6473fd9810fe47f": "新しい T3 Connect デバイスに承認を必須にする", "da7029b74a481832": "あなたのセッションにはアクセスを管理する権限がありません。", "c7a49b7cba491fdc": "ホスト型 Connect", + "be2b49ee8afa65e1": "このオリジンではブラウザーサインインを利用できません。デスクトップアプリからサインインするか、このホストで `s2code connect` を実行してください。", "b0b5de2fdc9514b5": "T3 Connect", "2c2a12fbdc300d03": "これらの設定を編集するには、「リレーの表示」と「リレーの管理」の両方の権限でこの接続をペアリングしてください。", "3b3d299149f45c3d": "「環境を追加」をクリックして別の環境をペアリングするか、T3 Connect から接続してください。", diff --git a/apps/web/src/i18n/translations/ko.json b/apps/web/src/i18n/translations/ko.json index a4d29978c9576..cfd8f9675cb88 100644 --- a/apps/web/src/i18n/translations/ko.json +++ b/apps/web/src/i18n/translations/ko.json @@ -9538,6 +9538,7 @@ "c6473fd9810fe47f": "새 T3 Connect 기기 승인 필요", "da7029b74a481832": "현재 세션에는 액세스를 관리할 권한이 없습니다.", "c7a49b7cba491fdc": "호스팅된 Connect", + "be2b49ee8afa65e1": "이 출처에서는 브라우저 로그인을 사용할 수 없습니다. 데스크톱 앱에서 로그인하거나 이 호스트에서 `s2code connect`를 실행하세요.", "b0b5de2fdc9514b5": "T3 Connect", "2c2a12fbdc300d03": "이 설정을 편집하려면 릴레이 보기 및 릴레이 관리 권한을 모두 사용해 이 연결을 페어링하세요.", "3b3d299149f45c3d": "다른 환경을 페어링하려면 “환경 추가”를 클릭하거나 T3 Connect에서 연결하세요.", diff --git a/apps/web/src/i18n/translations/pt-BR.json b/apps/web/src/i18n/translations/pt-BR.json index f85c978edc076..b050d7dbdcbf4 100644 --- a/apps/web/src/i18n/translations/pt-BR.json +++ b/apps/web/src/i18n/translations/pt-BR.json @@ -9580,6 +9580,7 @@ "c6473fd9810fe47f": "Exigir aprovação de novos dispositivos do T3 Connect", "da7029b74a481832": "Sua sessão não tem permissão para gerenciar o acesso.", "c7a49b7cba491fdc": "Connect hospedado", + "be2b49ee8afa65e1": "O login pelo navegador não está disponível nesta origem. Entre pelo aplicativo desktop ou execute `s2code connect` neste host.", "b0b5de2fdc9514b5": "T3 Connect", "2c2a12fbdc300d03": "Para editar estas configurações, pareie esta conexão com as permissões View relay e Manage relay.", "3b3d299149f45c3d": "Clique em “Adicionar ambiente” para parear outro ambiente ou conecte um pelo T3 Connect.", diff --git a/apps/web/src/i18n/translations/ru.json b/apps/web/src/i18n/translations/ru.json index f02a3bb4813bd..c5da8ab3af301 100644 --- a/apps/web/src/i18n/translations/ru.json +++ b/apps/web/src/i18n/translations/ru.json @@ -9583,6 +9583,7 @@ "c6473fd9810fe47f": "Требовать подтверждение новых устройств T3 Connect", "da7029b74a481832": "У вашего сеанса нет прав на управление доступом.", "c7a49b7cba491fdc": "Облачный Connect", + "be2b49ee8afa65e1": "Вход через браузер недоступен для этого источника. Войдите через настольное приложение или выполните `s2code connect` на этом хосте.", "b0b5de2fdc9514b5": "T3 Connect", "2c2a12fbdc300d03": "Чтобы менять эти настройки, выполните сопряжение этого подключения с разрешениями View relay и Manage relay.", "3b3d299149f45c3d": "Нажмите «Добавить окружение», чтобы выполнить сопряжение с другим окружением, или подключите его из T3 Connect.", diff --git a/apps/web/src/i18n/translations/sv.json b/apps/web/src/i18n/translations/sv.json index ca103c0671fa0..ff06dcd26dbd3 100644 --- a/apps/web/src/i18n/translations/sv.json +++ b/apps/web/src/i18n/translations/sv.json @@ -9589,6 +9589,7 @@ "c6473fd9810fe47f": "Kräv godkännande för nya T3 Connect-enheter", "da7029b74a481832": "Din session har inte behörighet att hantera åtkomst.", "c7a49b7cba491fdc": "Molnbaserad Connect", + "be2b49ee8afa65e1": "Webbläsarinloggning är inte tillgänglig för detta ursprung. Logga in från skrivbordsappen eller kör `s2code connect` på den här värden.", "b0b5de2fdc9514b5": "T3 Connect", "2c2a12fbdc300d03": "Parkoppla den här anslutningen med både behörigheterna Visa relä och Hantera relä för att redigera de här inställningarna.", "3b3d299149f45c3d": "Klicka på ”Lägg till miljö” för att parkoppla en annan miljö, eller anslut en från T3 Connect.", diff --git a/apps/web/src/i18n/translations/zh.json b/apps/web/src/i18n/translations/zh.json index f2f950a5ca98c..5f11de51b22b9 100644 --- a/apps/web/src/i18n/translations/zh.json +++ b/apps/web/src/i18n/translations/zh.json @@ -9597,6 +9597,7 @@ "c6473fd9810fe47f": "新 T3 Connect 设备需要批准", "da7029b74a481832": "你当前的连接无权管理访问权限。", "c7a49b7cba491fdc": "托管 Connect", + "be2b49ee8afa65e1": "此来源不支持浏览器登录。请从桌面应用登录,或在此主机上运行 `s2code connect`。", "b0b5de2fdc9514b5": "T3 Connect", "2c2a12fbdc300d03": "要编辑这些设置,请在配对此连接时同时授予“查看中继”和“管理中继”权限。", "3b3d299149f45c3d": "点击“添加环境”配对其他环境,或从 T3 Connect 连接一个环境。", diff --git a/docs/operations/connect-setup.md b/docs/operations/connect-setup.md index b1c1f4c87dd94..1a675a6f3cd58 100644 --- a/docs/operations/connect-setup.md +++ b/docs/operations/connect-setup.md @@ -1,50 +1,48 @@ # T3 Connect setup -Deployment and client configuration for T3 Connect. The [architecture note](../internals/t3-connect.md) -explains the trust boundaries; the [relay README](../../infra/relay/README.md#deployment) owns relay -provisioning instructions. +Deployment and client configuration for Connect. The [architecture note](../internals/t3-connect.md) +explains the trust boundaries; the [fork relay runbook](./hosted-fork.md) owns GitHub +variables/secrets and fork ownership guards; the [relay README](../../infra/relay/README.md#deployment) +owns relay provisioning details. ## Public application configuration -T3 Connect is disabled in a fresh clone. To build against the production deployment, copy the -repository-root example: - -```sh -cp .env.example .env -``` - -For another deployment, set these values in the repository-root `.env` or `.env.local`: +Connect is disabled in a fresh clone. For local source builds against a fork-owned relay, +copy `.env.example` to `.env` and fill in your deployment's public values in `.env` or `.env.local`: ```dotenv T3CODE_CLERK_PUBLISHABLE_KEY= -T3CODE_CLERK_JWT_TEMPLATE= +T3CODE_CLERK_JWT_TEMPLATE=t3-relay T3CODE_CLERK_CLI_OAUTH_CLIENT_ID= -T3CODE_RELAY_URL=https://relay.example.com -T3CODE_CLOUD_MINT_PUBLIC_KEY="" +T3CODE_RELAY_URL=https://relay. +T3CODE_CLOUD_MINT_PUBLIC_KEY=-----BEGIN PUBLIC KEY-----\n...\n-----END PUBLIC KEY----- ``` Process variables take precedence over `.env.local`, then `.env`. Use these canonical names; -the build loader supplies framework-specific aliases. These values are public identifiers. -`CLERK_SECRET_KEY` belongs only in the relay's secrets, never in client configuration. +the build loader supplies framework-specific aliases (including `VITE_HOSTED_APP_URL` when optional +`T3CODE_HOSTED_APP_URL` is set). These values are public identifiers. `CLERK_SECRET_KEY` belongs +only in the relay's secrets, never in client configuration. Client and bundled-server builds embed the public values, so set them before building. -EAS preview and production environments need the publishable key, JWT template name, and relay URL. -Bundled servers also accept runtime overrides for operator-managed deployments. +Bundled servers also accept runtime overrides via `S2CODE_*` aliases for operator-managed +deployments. -Copy `infra/relay/.env.example` to `infra/relay/.env` for relay deployment settings. +Copy `infra/relay/.env.example` to `infra/relay/.env` for local relay deployment settings. Deploy `prod` before personal stages because it owns the retained database that their branches -depend on. The stack's `PublishClientConfig` action writes the resulting relay URL back to the root `.env`. +depend on. The stack's `PublishClientConfig` action writes `T3CODE_RELAY_URL` and the single-line +`\n`-escaped `T3CODE_CLOUD_MINT_PUBLIC_KEY` back to the root `.env`. ## CLI OAuth application In Clerk's OAuth applications settings: -1. Create a public OAuth application for the T3 CLI, using authorization-code exchange with PKCE. +1. Create a public OAuth application for the CLI, using authorization-code exchange with PKCE. 2. Allow the redirect URI `http://127.0.0.1:34338/callback`. 3. Enable the `openid`, `profile`, `email`, and `offline_access` scopes. -4. Enable **Device authorization grant** on the application. Headless and SSH authorization use - it, and Clerk only advertises the device endpoint once it is on. The feature is in beta and - Clerk enables it per account on request. +4. Enable **Device authorization grant** on the application. Headless, SSH, and + no-hosted-web (`T3CODE_HOSTED_APP_URL` unset) CLI logins use it, and Clerk only advertises + the device endpoint once it is on. The feature is in beta and Clerk enables it per account + on request. 5. Set `T3CODE_CLERK_CLI_OAUTH_CLIENT_ID` to the generated public client ID in local and release build environments. @@ -57,56 +55,74 @@ Create a Clerk JWT template named `t3-relay` with claims: ``` Set `T3CODE_CLERK_JWT_TEMPLATE=t3-relay` for clients and -`CLERK_JWT_AUDIENCE=t3-code-relay` for the relay. The production relay deployment environment -also defines `CLERK_JWT_TEMPLATE`. The audience stays the same across relay stages; the relay -URL selects the deployment. +`CLERK_JWT_AUDIENCE=t3-code-relay` for the relay. The audience stays the same across relay stages; +the relay URL selects the deployment. ## Desktop OAuth redirects -Enable Clerk's Native API and add the desktop redirects to its SSO redirect allowlist: +Enable Clerk's Native API and add the fork's desktop renderer redirects to its SSO redirect +allowlist (see `apps/desktop/src/electron/ElectronProtocol.ts` and +`packages/shared/src/channelIdentity.ts`): ```text -t3code-dev://app/ -t3code://app/ +shuv2code-dev://app/ +shuv2code-nightly://app/ +shuv2code-preview://app/ +shuv2code://app/ +s2code-dev://app/ +s2code-nightly://app/ +s2code-preview://app/ +s2code://app/ ``` -Add the corresponding origin to the Clerk instance's Backend API `allowed_origins` array. -Development uses `t3code-dev://app`; production uses `t3code://app`. Update the array with +Add the corresponding origins (without trailing slash: `shuv2code-dev://app`, +`shuv2code-nightly://app`, `shuv2code-preview://app`, `shuv2code://app`, plus the `s2code-*://app` +aliases) to the Clerk instance's Backend API `allowed_origins` array. Update the array with `PATCH https://api.clerk.com/v1/instance` using the Clerk secret key, preserving existing entries. -The Clerk Electron integration handles token -persistence and system-browser callback delivery. +The Clerk Electron integration handles token persistence and system-browser callback delivery. ## Android native sign-in redirects -Clerk's native Android SDK uses `clerk://.callback`. In the Clerk instance selected by the app's publishable key, add each supported package to **Native applications > Allowlist for mobile SSO redirect**: +Clerk's native Android SDK uses `clerk://.callback`. In the Clerk instance selected +by the app's publishable key, add each supported fork package to **Native applications > Allowlist +for mobile SSO redirect** (see `scripts/lib/mobile-release-identity.ts`): -| Variant | Callback | -| ----------- | --------------------------------------------- | -| Development | `clerk://com.t3tools.t3code.dev.callback` | -| Preview | `clerk://com.t3tools.t3code.preview.callback` | -| Production | `clerk://com.t3tools.t3code.callback` | +| Variant | Callback | +| --------------- | --------------------------------------------- | +| Development | `clerk://dev.shuv.shuv2code.dev.callback` | +| Nightly/Preview | `clerk://dev.shuv.shuv2code.nightly.callback` | +| Production | `clerk://dev.shuv.shuv2code.callback` | -Preserve existing entries. These callbacks are separate from the `t3code-dev` / `t3code-preview` / `t3code` navigation schemes. A private development build using the production Clerk key still needs its development callback allowed by that instance's administrator; rebuilding the same package does not change the allowlist. +Preserve existing entries. These callbacks are separate from the `s2code-*` / `shuv2code-*` +navigation schemes. A private development build using the production Clerk key still needs its +development callback allowed by that instance's administrator; rebuilding the same package does +not change the allowlist. ## Desktop passkeys -For a production macOS app with bundle ID `com.t3tools.t3code`: +macOS passkeys are optional: signed macOS builds with `T3CODE_CLERK_PUBLISHABLE_KEY` succeed +without a provisioning profile and opt into Associated Domains passkeys only when +`T3CODE_MACOS_PROVISIONING_PROFILE` or `T3CODE_CLERK_PASSKEY_RP_DOMAINS` is set. + +For a signed macOS app using the fork's bundle ID (`dev.shuv.shuv2code.nightly` for Nightly, +`dev.shuv.shuv2code.preview` for Preview, or `dev.shuv.shuv2code` for Stable): 1. Create an explicit macOS App ID in the Apple Developer portal with **Associated Domains**. 2. Create a provisioning profile for that App ID and the distribution signing certificate. 3. In Clerk's Native API settings, add an iOS app with the same Apple Team ID and bundle ID. This setting also configures Electron/macOS passkeys. 4. Check `https:///.well-known/apple-app-site-association`. Its - `webcredentials.apps` must include `.com.t3tools.t3code`. + `webcredentials.apps` must include `.dev.shuv.shuv2code.nightly` (or the matching + channel bundle ID). 5. Configure fork signing using the prerequisites in the [release runbook](./release.md#account-and-credential-prerequisites-names-only). -Local signed builds additionally use: +Local signed passkey builds additionally use: ```dotenv T3CODE_APPLE_TEAM_ID=ABC1234567 -T3CODE_MACOS_PROVISIONING_PROFILE=/absolute/path/to/t3code.provisionprofile +T3CODE_MACOS_PROVISIONING_PROFILE=/absolute/path/to/s2code.provisionprofile # Override only when the RP domain differs from the Clerk Frontend API hostname. -T3CODE_CLERK_PASSKEY_RP_DOMAINS=example.clerk.accounts.dev,clerk.example.com +T3CODE_CLERK_PASSKEY_RP_DOMAINS=example.clerk.accounts.dev,clerk. ``` Without the override, the build derives the RP domain from the Clerk publishable key. diff --git a/docs/operations/hosted-fork.md b/docs/operations/hosted-fork.md index 9496f9e11e7dc..b2b23c4e08bd3 100644 --- a/docs/operations/hosted-fork.md +++ b/docs/operations/hosted-fork.md @@ -1,14 +1,149 @@ -# Hosted services are disabled +# Fork-owned Connect relay -This fork does not run hosted web or Connect services. The web-preview and relay -workflows only emit a notice and summary: no build, deploy, credential lookup or -resource provisioning occurs. The Alchemy entry point also refuses execution, -including local invocations. There is no supported enable flag. CLI/desktop -publication is independent of hosted deployment. +This fork supports deploying its own hosted Connect relay (`infra/relay`) under +fork-owned accounts. The fork's primary deployment shape is **Cloudflare Workers +plus one self-hosted VM (such as an exe.dev box) running Postgres**, reached by +Cloudflare Hyperdrive through an outbound `cloudflared` Cloudflare Tunnel +protected by a Cloudflare Access service token (`AccessOrigin`), with **no +PlanetScale or Axiom accounts required**. Upstream's PlanetScale and Axiom +paths remain intact and selectable by config. -Direct and Tailscale pairing remain available. With no hosted origin configured, -pairing uses the server's same-origin `/pair#token=…` URL without a `host` query. -Upstream hosted origins and auth accounts are not fork defaults. +With no variables configured, behavior stays unchanged: Connect is disabled, +nightly builds ship cloudless, direct and Tailscale pairing stay untouched, and +push-triggered relay deploys skip without failing. + +Direct and Tailscale pairing remain available independently of Connect. With no +hosted web origin configured, pairing uses the server's same-origin +`/pair#token=…` URL without a `host` query, and `s2code connect` uses Clerk's +device-authorization flow. Upstream hosted origins (`t3.codes`, `t3coderelay.com`, +`t3tools.com`, `ping.gg`) and upstream Clerk keys are always rejected. + +## What stays off + +- **Hosted web (`web-preview.yml`, Vercel):** Notice-only; no hosted static web + app is deployed in this setup. +- **Relay PR previews (`relay-preview.yml`, `relay-preview-cleanup.yml`):** Every + job is gated on `github.repository == 'shuv1337/s2code' && vars.RELAY_PREVIEWS_ENABLED == 'true'`. + External Postgres mode only supports the `prod` stage (previews require PlanetScale branching). +- **Mobile push, mobile telemetry, and store builds:** `APNS_ENABLED` defaults + to `false`; no APNs or FCM secrets are required. When `AXIOM_TOKEN` is unset, + no Axiom datasets/tokens or R2 source-map buckets are created, mobile observe + endpoints answer `503 mobile_telemetry_not_configured`, and client builds stay + free of tracing tokens. +- **T3 Chat account linking and Axiom paging monitors:** Left unset (`T3CHAT_LINK_SECRET`, + `RELAY_ALERT_NOTIFIER_ID`, `RELAY_WARNING_NOTIFIER_ID`). + +## Deployment durability: mint key and Postgres backups + +`.github/workflows/deploy-relay.yml` deploys Alchemy stage `prod` from +`shuv1337/s2code` `refs/heads/main` inside the protected `production` GitHub +environment. Before `Alchemy.Stack` constructs providers or state, +`infra/relay/src/forkOwnership.ts` validates `CLOUDFLARE_ACCOUNT_ID` (32-char +hex, pinned on `Cloudflare.state({ accountId })`), non-upstream/non-placeholder +relay zones/domain, a fork-owned `CLERK_PUBLISHABLE_KEY`, and that either +external Postgres (`RELAY_DB_HOST`) or PlanetScale (`PLANETSCALE_ORGANIZATION`) +is configured. + +- **Mint key pair (`CloudMintKeyPair`):** Generated once by Alchemy and stored + in Cloudflare Alchemy state. Released S2Code servers pin + `T3CODE_CLOUD_MINT_PUBLIC_KEY` and refuse any relay link whose mint key does + not match. **Do not destroy `T3CodeRelay/prod` state**: rotating + `CloudMintKeyPair` breaks every already-shipped pinned client and existing + environment link until a new release ships with the replacement + `T3CODE_CLOUD_MINT_PUBLIC_KEY` and environments relink. +- **Self-hosted Postgres backups and loss impact:** Backups of the Postgres + database on the self-hosted box are the operator's responsibility. If that + database is lost, all persisted environment links, environment credentials, + managed tunnel allocations, and DPoP/activity state are lost: every linked + host must relink, and previously provisioned Cloudflare tunnels and DNS + records become orphaned until swept (see `vp run --filter t3code-relay tunnels:census`). + +## External Postgres migrations + +In external mode (`RELAY_DB_HOST` set), `deploy-relay.yml` runs +`vp run --filter t3code-relay migrate:postgres` before deploying the Worker, +applying `infra/relay/migrations/postgres` and recording applied files in +`relay_migrations` using the same schema and SHA-256 bookkeeping as +`@alchemy.run/planetscale` (so a database can move cleanly between modes). When +`RELAY_DB_ACCESS_CLIENT_ID` and `RELAY_DB_ACCESS_CLIENT_SECRET` are set, the +workflow reaches the private Postgres box over `cloudflared access tcp` using +the same Access service token. + +The same script can also be run directly on the database box: + +```sh +RELAY_DATABASE_URL="postgresql://:@127.0.0.1:5432/?sslmode=disable" \ + vp run --filter t3code-relay migrate:postgres +``` + +## GitHub variables and secrets + +### `production` environment (deploy-only) + +Only `.github/workflows/deploy-relay.yml` enters `environment: production`. +Release workflows (`release.yml`, `release-desktop.yml`, +`encrypted-windows-build.yml`) never enter `production` and never see deploy +credentials. + +Required core secrets in `production`: + +- `CLOUDFLARE_API_TOKEN` +- `CLERK_SECRET_KEY` (relay Worker only; never exposed to clients or releases) + +Required core variables in `production`: + +- `CLOUDFLARE_ACCOUNT_ID` (32-character hex Cloudflare account ID) +- `RELAY_API_ZONE_NAME` (set at repository scope as well to enable automatic + path-filtered `push` deploys on `main`; keeping it only in `production` limits + deploys to `workflow_dispatch`) +- `RELAY_TUNNEL_ZONE_NAME` +- `RELAY_DOMAIN` _(optional override for `relay.`)_ +- `CLERK_PUBLISHABLE_KEY` +- `CLERK_JWT_AUDIENCE` (`t3-code-relay`) +- `APNS_ENABLED` _(optional; defaults to `false`)_ +- `RELAY_TUNNEL_CLEANUP_MODE` _(optional; start `off`, verify `dry-run`, then `enabled`)_ +- `RELAY_LEGACY_TUNNEL_CLEANUP_MODE` _(optional; default `off`)_ + +Self-hosted Postgres mode (recommended for this fork; selected whenever `RELAY_DB_HOST` is set): + +- Variables: `RELAY_DB_HOST` (tunnel/Access hostname), `RELAY_DB_NAME`, + `RELAY_DB_USER`, optional `RELAY_DB_PORT` (public origin only; default `5432`), + optional `RELAY_DB_CONNECTION_LIMIT` (default `20`), optional + `RELAY_DB_MIGRATION_SSLMODE` (`disable`, `allow`, `prefer`, `require`, `verify-ca`, + or `verify-full`; defaults to `disable` on the Access tunnel path and `require` + on the public path) +- Secrets: `RELAY_DB_PASSWORD`, plus `RELAY_DB_ACCESS_CLIENT_ID` and + `RELAY_DB_ACCESS_CLIENT_SECRET` (set both for Cloudflare Tunnel + Access + `AccessOrigin`, or leave both unset for `PublicOrigin`; setting only one fails) + +Optional self-hosted Worker OTLP traces (used when `AXIOM_TOKEN` is unset): + +- Variable: `RELAY_OTLP_TRACES_URL` (fork-owned HTTPS OTLP/HTTP traces endpoint) +- Secret: `RELAY_OTLP_TRACES_TOKEN` _(optional bearer token)_ + +Optional upstream-compatible PlanetScale + Axiom mode (leave unset on the self-hosted stack): + +- PlanetScale variables: `PLANETSCALE_ORGANIZATION`, optional + `RELAY_DB_CLUSTER_SIZE` (default `PS_5`), optional `RELAY_DB_REPLICAS` (default `0`), + optional `RELAY_DB_CONNECTION_LIMIT` (default `40`) +- PlanetScale secrets: `PLANETSCALE_API_TOKEN_ID`, `PLANETSCALE_API_TOKEN` +- Axiom variable/secret: `AXIOM_ORG_ID` (variable), `AXIOM_TOKEN` (secret) + +### Repository variables (public release build config) + +`release.yml` (preflight, `build_bundle`, and every `release-desktop.yml` +caller) and `encrypted-windows-build.yml` read these public non-secret +repository variables. The five required variables are validated all-or-nothing: +leaving all unset produces a cloudless build, while a partial set fails +preflight. + +- `T3CODE_RELAY_URL` (from the `deploy-relay.yml` step summary) +- `T3CODE_CLOUD_MINT_PUBLIC_KEY` (single-line `\n`-escaped Ed25519 SPKI PEM from + the `deploy-relay.yml` step summary) +- `T3CODE_CLERK_PUBLISHABLE_KEY` +- `T3CODE_CLERK_JWT_TEMPLATE` (`t3-relay`) +- `T3CODE_CLERK_CLI_OAUTH_CLIENT_ID` +- `T3CODE_HOSTED_APP_URL` _(optional; omit when no hosted web app is deployed)_ ## Inherited marketing references @@ -28,23 +163,26 @@ references do not establish fork service ownership or a published S2Code website ## Client configuration names -These are the internal names the binaries/builds actually read, not deployment -switches. Leave hosted settings unset for normal fork use. Public `S2CODE_*` -CLI environment aliases are normalized to the corresponding `T3CODE_*` inputs -at the process boundary; they are not substitutes for Vite or Expo build inputs. - -- Server runtime and bundled defaults: `T3CODE_HOSTED_APP_URL`, - `T3CODE_RELAY_URL`, `T3CODE_CLOUD_MINT_PUBLIC_KEY`, - `T3CODE_CLERK_PUBLISHABLE_KEY`, `T3CODE_CLERK_CLI_OAUTH_CLIENT_ID`. The - server build bakes the hosted origin from `T3CODE_HOSTED_APP_URL`, the same - name used at runtime. `T3CODE_CLOUD_MINT_PUBLIC_KEY` is the relay's Ed25519 - credential-minting public key (PEM). An environment refuses a relay link - for any other relay URL or mint key, so set both or hosted linking fails. +These are the internal names the binaries and builds read. Leave hosted settings +unset for cloudless local builds. Public `S2CODE_*` CLI environment aliases +(including `S2CODE_CLOUD_MINT_PUBLIC_KEY`, `S2CODE_RELAY_URL`, +`S2CODE_HOSTED_APP_URL`, `S2CODE_CLERK_*`) are normalized to the corresponding +`T3CODE_*` inputs at the process boundary; they are not substitutes for Vite or +Expo build inputs. + +- Server runtime and bundled defaults: `T3CODE_RELAY_URL`, + `T3CODE_CLOUD_MINT_PUBLIC_KEY`, `T3CODE_CLERK_PUBLISHABLE_KEY`, + `T3CODE_CLERK_CLI_OAUTH_CLIENT_ID`, and optional `T3CODE_HOSTED_APP_URL`. + `T3CODE_CLOUD_MINT_PUBLIC_KEY` is the relay's Ed25519 credential-minting + public key (PEM, single-line `\n`-escaped in `.env` and CI variables). An + environment refuses a relay link for any other relay URL or mint key. - Desktop build auth: `T3CODE_CLERK_PUBLISHABLE_KEY`, - `T3CODE_CLERK_JWT_TEMPLATE`. -- Web build: `VITE_HOSTED_APP_URL`, `VITE_HOSTED_APP_CHANNEL`, - `VITE_T3CODE_RELAY_URL`, `VITE_CLERK_PUBLISHABLE_KEY`, - `VITE_CLERK_JWT_TEMPLATE`. Optional relay tracing: + `T3CODE_CLERK_JWT_TEMPLATE`. Signed macOS builds only enable Associated Domains + passkeys when `T3CODE_MACOS_PROVISIONING_PROFILE` or + `T3CODE_CLERK_PASSKEY_RP_DOMAINS` is set. +- Web build: `VITE_T3CODE_RELAY_URL`, `VITE_CLERK_PUBLISHABLE_KEY`, + `VITE_CLERK_JWT_TEMPLATE`, optional `VITE_HOSTED_APP_URL`, + `VITE_HOSTED_APP_CHANNEL`. Optional relay tracing: `VITE_RELAY_OTLP_TRACES_URL`, `VITE_RELAY_OTLP_TRACES_DATASET`, `VITE_RELAY_OTLP_TRACES_TOKEN`. - Mobile build: `T3CODE_RELAY_URL`, `EXPO_PUBLIC_CLERK_PUBLISHABLE_KEY`, @@ -55,12 +193,3 @@ at the process boundary; they are not substitutes for Vite or Expo build inputs. No credentials belong in public configuration except service-scoped public client keys/tokens intended for distribution. - -## Prerequisites for future enablement - -Separate maintainer authorization and implementation are required. Prerequisite -areas: fork domains/DNS, Clerk instance/OAuth redirects, Vercel project/Git link, -Cloudflare account, PlanetScale organization, Axiom organization, resource/state -namespaces, credential scoping, protected deployment environments, ownership -verification and platform sign-in/delivery checks. Optional push: APNs and FCM. -None of these checks or deployments is implemented by the disabled workflows. diff --git a/infra/relay/.env.example b/infra/relay/.env.example index 87a9a1ca32cb8..91eaeb73562a9 100644 --- a/infra/relay/.env.example +++ b/infra/relay/.env.example @@ -1,9 +1,49 @@ -# Required: Relay API and managed tunnel domains +# Required: Fork-owned Cloudflare account +# alchemy.run.ts pins Cloudflare state to CLOUDFLARE_ACCOUNT_ID (32-char hex) +# and rejects upstream domains as well as RFC 2606 placeholders (example.com, +# .example, .invalid, .test). +CLOUDFLARE_ACCOUNT_ID= + +# Required: Relay API and managed tunnel domains (fork-owned Cloudflare zones) # 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 +# Both may point at the same zone. +RELAY_API_ZONE_NAME= +RELAY_TUNNEL_ZONE_NAME= + +# Database backend (required: configure EITHER external Postgres OR PlanetScale). +# External Postgres mode is selected whenever RELAY_DB_HOST is set (prod stage only). +# When both RELAY_DB_ACCESS_CLIENT_ID and RELAY_DB_ACCESS_CLIENT_SECRET are set, +# Hyperdrive uses Cloudflare Access + Tunnel (AccessOrigin); otherwise PublicOrigin. +RELAY_DB_HOST= +# RELAY_DB_PORT=5432 +RELAY_DB_NAME= +RELAY_DB_USER= +RELAY_DB_PASSWORD= +# RELAY_DB_ACCESS_CLIENT_ID= +# RELAY_DB_ACCESS_CLIENT_SECRET= +# RELAY_DB_CONNECTION_LIMIT=20 +# RELAY_DB_MIGRATION_SSLMODE=require + +# Alternative: PlanetScale Postgres mode (used when RELAY_DB_HOST is unset). +# Defaults avoid upstream's PS_160/2-replica cluster. +# PLANETSCALE_ORGANIZATION= +# PLANETSCALE_API_TOKEN_ID= +# PLANETSCALE_API_TOKEN= +# RELAY_DB_CLUSTER_SIZE=PS_5 +# RELAY_DB_REPLICAS=0 + +# Optional: Observability +# Leave AXIOM_TOKEN unset to run with no Axiom resources, no R2 source-map bucket, +# and no mobile telemetry forwarding. Optionally export Worker spans to a +# self-hosted OTLP/HTTP endpoint instead: +# RELAY_OTLP_TRACES_URL= +# RELAY_OTLP_TRACES_TOKEN= +# +# Or enable upstream Axiom observability by setting both: +# AXIOM_ORG_ID= +# AXIOM_TOKEN= # Optional: inactive tunnel cleanup. Start with dry-run, verify the cleanup # logs, then set enabled. Unset and off both disable cleanup. @@ -15,33 +55,29 @@ RELAY_TUNNEL_ZONE_NAME=tunnels.example.com # Ignored on prod. # RELAY_LEGACY_TUNNEL_GRACE_MINUTES=10 -# Optional: relay monitors and health dashboard in Axiom. Set the notifier id -# that pages (incident.io in prod) to create them; unset creates neither, so -# personal stages never page anyone. Warnings go to their own notifier, or -# only show in Axiom when it is unset. +# Optional: relay monitors and health dashboard in Axiom (requires AXIOM_TOKEN). # RELAY_ALERT_NOTIFIER_ID= # RELAY_WARNING_NOTIFIER_ID= # Optional: Relay domain override # Set this only when the derived relay hostname should not be used. -# RELAY_DOMAIN=relay.example.com +# RELAY_DOMAIN= -# Required: Clerk +# Required: Clerk (fork-owned instance) # Get the keys from the Clerk Dashboard under API keys. Set the JWT audience to # the `aud` claim configured on the `t3-relay` JWT template. -CLERK_PUBLISHABLE_KEY=pk_test_... -CLERK_SECRET_KEY=sk_test_... +CLERK_PUBLISHABLE_KEY= +CLERK_SECRET_KEY= CLERK_JWT_AUDIENCE=t3-code-relay -# Apple Push Notification service (required unless APNS_ENABLED=false) -# Set APNS_ENABLED=false for an Android-only development relay. -# Get these values from your Apple Developer account. Use `sandbox` for -# development APNs credentials and `production` for production credentials. -APNS_ENVIRONMENT=sandbox -APNS_TEAM_ID=... -APNS_KEY_ID=... -APNS_BUNDLE_ID=... -APNS_PRIVATE_KEY=... +# Apple Push Notification service (disabled by default in this fork). +# Set APNS_ENABLED=true and populate APNS_* only when enabling iOS push. +APNS_ENABLED=false +# APNS_ENVIRONMENT=sandbox +# APNS_TEAM_ID= +# APNS_KEY_ID= +# APNS_BUNDLE_ID= +# APNS_PRIVATE_KEY= # Optional: Android push. Set this secret to the service account JSON from # Firebase Project settings > Service accounts. Never put it in an app build. diff --git a/infra/relay/alchemy.run.ts b/infra/relay/alchemy.run.ts index 48671fe0a686a..3a8f5bd251046 100644 --- a/infra/relay/alchemy.run.ts +++ b/infra/relay/alchemy.run.ts @@ -10,18 +10,21 @@ import * as Planetscale from "@alchemy.run/planetscale"; import { PublishClientConfig, tokenDigest } from "./src/clientConfig.ts"; import * as RelayDb from "./src/db.ts"; -import { MobileSourceMapsUploadCredentials, RelayObservability } from "./src/observability.ts"; +import { forkRelayDeploymentConfig } from "./src/forkOwnership.ts"; +import { + MobileSourceMapsUploadCredentials, + OptionalRelayObservability, +} from "./src/observability.ts"; import { isPreviewStage } from "./src/previewStages.ts"; import { PreviewTunnelSweep, PreviewTunnelSweepProvider } from "./src/previewTunnelSweep.ts"; import { ManagedEndpointZone, RelayApiZone, RelayDeploymentConfig } from "./src/zone.ts"; import * as RelayWorker from "./src/worker.ts"; -// Boundary guard must precede Stack/layer construction, which can provision resources. -throw new Error("Hosted relay deployment is not supported in this fork."); - export default Alchemy.Stack( "T3CodeRelay", { + // Axiom and PlanetScale providers resolve credentials lazily on first resource use, + // so keeping them registered requires no credentials in external-DB or no-Axiom mode. providers: Layer.mergeAll( Axiom.providers(), Cloudflare.providers(), @@ -29,16 +32,27 @@ export default Alchemy.Stack( Planetscale.providers(), PreviewTunnelSweepProvider(), ), - state: Cloudflare.state(), + // Validate ownership via Effect Config (including infra/relay/.env) before state or resources build. + state: Layer.unwrap( + forkRelayDeploymentConfig.pipe( + Effect.map((forkRelayEnv) => + Cloudflare.state({ accountId: forkRelayEnv.cloudflareAccountId }), + ), + Effect.orDie, + ), + ), }, Effect.gen(function* () { const { stage } = yield* Alchemy.Stack; - const db = yield* RelayDb.PlanetscaleDatabase; + const db = yield* RelayDb.RelayDatabase; const hyperdrive = yield* RelayDb.RelayHyperdrive; const managedEndpointZone = yield* ManagedEndpointZone.pipe(Effect.orDie); const relayApiZone = yield* RelayApiZone.pipe(Effect.orDie); - const observability = yield* RelayObservability; - const mobileSourceMapsUploadCredentials = yield* MobileSourceMapsUploadCredentials; + const observability = yield* OptionalRelayObservability; + const mobileSourceMapsUploadCredentials = observability + ? yield* MobileSourceMapsUploadCredentials + : undefined; + const cloudMintKeyPair = yield* RelayWorker.CloudMintKeyPair; const api = yield* RelayWorker.Api; if (isPreviewStage(stage)) { const { managedEndpointZoneName } = yield* RelayDeploymentConfig; @@ -50,37 +64,50 @@ export default Alchemy.Stack( } yield* PublishClientConfig({ url: api.url, - mobileTracingUrl: observability.traces.otelTracesEndpoint, - mobileTracingDataset: observability.traces.name, - mobileTracingToken: observability.mobileIngestToken.token, - clientTracingUrl: observability.traces.otelTracesEndpoint, - clientTracingDataset: observability.traces.name, - clientTracingToken: observability.clientIngestToken.token, - tokenDigest: Output.map( - Output.all(observability.mobileIngestToken.token, observability.clientIngestToken.token), - tokenDigest, - ), + cloudMintPublicKey: cloudMintKeyPair.publicKey, + ...(observability + ? { + mobileTracingUrl: observability.traces.otelTracesEndpoint, + mobileTracingDataset: observability.traces.name, + mobileTracingToken: observability.mobileIngestToken.token, + clientTracingUrl: observability.traces.otelTracesEndpoint, + clientTracingDataset: observability.traces.name, + clientTracingToken: observability.clientIngestToken.token, + tokenDigest: Output.map( + Output.all( + observability.mobileIngestToken.token, + observability.clientIngestToken.token, + ), + tokenDigest, + ), + } + : {}), }); return { - databaseName: db.database.name, - databaseBranchName: db.branch?.name ?? "main", + databaseName: db.databaseName, + databaseBranchName: db.databaseBranchName, hyperdriveName: hyperdrive.name, workerName: api.workerName, url: api.url, + cloudMintPublicKey: cloudMintKeyPair.publicKey, relayApiZoneId: relayApiZone.zoneId, managedEndpointZoneId: managedEndpointZone.zoneId, - mobileTracingUrl: observability.traces.otelTracesEndpoint, - mobileTracingDataset: observability.traces.name, - mobileTracingToken: observability.mobileIngestToken.token, - clientTracingUrl: observability.traces.otelTracesEndpoint, - clientTracingDataset: observability.traces.name, - clientTracingToken: observability.clientIngestToken.token, - mobileLogsDataset: observability.mobileLogs.name, - mobileMetricsDataset: observability.mobileMetrics.name, - mobileSourceMapsBucket: observability.mobileSourceMaps.bucketName, - // The MOBILE_SOURCEMAPS_R2 secret for GitHub Actions and EAS Build. - mobileSourceMapsUploadCredentials, + ...(observability + ? { + mobileTracingUrl: observability.traces.otelTracesEndpoint, + mobileTracingDataset: observability.traces.name, + mobileTracingToken: observability.mobileIngestToken.token, + clientTracingUrl: observability.traces.otelTracesEndpoint, + clientTracingDataset: observability.traces.name, + clientTracingToken: observability.clientIngestToken.token, + mobileLogsDataset: observability.mobileLogs.name, + mobileMetricsDataset: observability.mobileMetrics.name, + mobileSourceMapsBucket: observability.mobileSourceMaps.bucketName, + // The MOBILE_SOURCEMAPS_R2 secret for GitHub Actions and EAS Build. + mobileSourceMapsUploadCredentials, + } + : {}), }; }).pipe(Effect.provide(RelayWorker.layer)), ); diff --git a/infra/relay/package.json b/infra/relay/package.json index 4eaf291459aea..86c78db710f7c 100644 --- a/infra/relay/package.json +++ b/infra/relay/package.json @@ -4,6 +4,7 @@ "type": "module", "scripts": { "deploy": "alchemy deploy", + "migrate:postgres": "node scripts/migrate-postgres.ts", "push:android:smoke": "node scripts/android-push-smoke.ts", "push:android:watch": "node scripts/android-push-watch.ts", "tunnels:census": "node scripts/tunnel-census.ts", diff --git a/infra/relay/scripts/migrate-postgres.ts b/infra/relay/scripts/migrate-postgres.ts new file mode 100644 index 0000000000000..0f293c7202648 --- /dev/null +++ b/infra/relay/scripts/migrate-postgres.ts @@ -0,0 +1,73 @@ +import * as NodeRuntime from "@effect/platform-node/NodeRuntime"; +import * as NodeServices from "@effect/platform-node/NodeServices"; +import * as PgClient from "@effect/sql-pg/PgClient"; +import * as Console from "effect/Console"; +import * as Effect from "effect/Effect"; +import * as Layer from "effect/Layer"; +import * as Path from "effect/Path"; +import * as Reactivity from "effect/reactivity/Reactivity"; + +import { + applyRelayPostgresMigrations, + isPostgresMigrationError, + PostgresMigrationError, + readDrizzlePostgresMigrations, + resolveMigrationPgClientConfig, + type PostgresMigrationExecutor, +} from "../src/postgresMigrations.ts"; + +const makePgExecutor = (sql: PgClient.PgClient): PostgresMigrationExecutor => ({ + query: >( + statement: string, + params?: ReadonlyArray, + ) => + sql.unsafe(statement, params).pipe( + Effect.mapError( + (cause) => + new PostgresMigrationError({ + operation: "query", + detail: "database statement failed", + cause, + }), + ), + ), + withTransaction: (effect: Effect.Effect) => + sql.withTransaction(effect).pipe( + Effect.mapError((error) => + isPostgresMigrationError(error) + ? error + : new PostgresMigrationError({ + operation: "apply", + detail: "database transaction failed", + cause: error, + }), + ), + ) as Effect.Effect, +}); + +const main = Effect.gen(function* () { + const path = yield* Path.Path; + const migrationsDir = yield* path.fromFileUrl(new URL("../migrations/postgres", import.meta.url)); + const records = yield* readDrizzlePostgresMigrations(migrationsDir); + const clientConfig = yield* resolveMigrationPgClientConfig(); + // Single dedicated session so session-level pg_advisory_lock and transactions share one connection. + const sql = yield* PgClient.makeClient(clientConfig).pipe( + Effect.mapError( + (cause) => + new PostgresMigrationError({ + operation: "query", + detail: "could not connect to Postgres", + cause, + }), + ), + ); + const result = yield* applyRelayPostgresMigrations({ + executor: makePgExecutor(sql), + records, + }); + yield* Console.log( + `Relay Postgres migrations complete: ${result.applied.length} applied, ${result.skipped} already applied.`, + ); +}).pipe(Effect.scoped, Effect.provide(Layer.mergeAll(NodeServices.layer, Reactivity.layer))); + +NodeRuntime.runMain(main); diff --git a/infra/relay/src/clientConfig.test.ts b/infra/relay/src/clientConfig.test.ts index 1e8515acc2576..6ac15598da3dc 100644 --- a/infra/relay/src/clientConfig.test.ts +++ b/infra/relay/src/clientConfig.test.ts @@ -1,4 +1,5 @@ -// @effect-diagnostics anyUnknownInErrorContext:off - ScratchStack.deploy is typed with `any` in its error channel by Alchemy. +// @effect-diagnostics anyUnknownInErrorContext:off nodeBuiltinImport:off - ScratchStack.deploy is typed with `any` in its error channel by Alchemy; crypto generates a real Ed25519 PEM fixture. +import * as NodeCrypto from "node:crypto"; import { inMemoryState } from "alchemy/State"; import * as Test from "@alchemy.run/test/Vitest"; import * as ConfigProvider from "effect/ConfigProvider"; @@ -62,8 +63,14 @@ const { test } = Test.make({ state: inMemoryState(), }); +const FIXTURE_MINT_PUBLIC_KEY = NodeCrypto.generateKeyPairSync("ed25519", { + publicKeyEncoding: { type: "spki", format: "pem" }, + privateKeyEncoding: { type: "pkcs8", format: "pem" }, +}).publicKey; + const clientConfig = (token: string) => ({ url: "https://relay.example.com", + cloudMintPublicKey: FIXTURE_MINT_PUBLIC_KEY, mobileTracingUrl: "https://axiom.example.com/v1/traces", mobileTracingDataset: "relay-traces", mobileTracingToken: Redacted.make(`mobile-${token}`), @@ -91,6 +98,22 @@ describe("PublishClientConfig", () => { const first = yield* fs.readFileString(target); expect(first).toContain("KEEP=yes\n"); expect(first).toContain("T3CODE_RELAY_URL=https://relay.example.com\n"); + const mintLine = first + .split("\n") + .find((line) => line.startsWith("T3CODE_CLOUD_MINT_PUBLIC_KEY=")); + expect(mintLine).toBeDefined(); + const escapedMintKey = mintLine!.slice("T3CODE_CLOUD_MINT_PUBLIC_KEY=".length); + expect(escapedMintKey).toContain("\\n"); + expect( + NodeCrypto.createPublicKey(escapedMintKey.replace(/\\n/gu, "\n")) + .export({ type: "spki", format: "der" }) + .equals( + NodeCrypto.createPublicKey(FIXTURE_MINT_PUBLIC_KEY).export({ + type: "spki", + format: "der", + }), + ), + ).toBe(true); expect(first).toContain("T3CODE_RELAY_CLIENT_OTLP_TRACES_TOKEN=client-v1\n"); expect(first).toContain("T3CODE_MOBILE_OTLP_TRACES_TOKEN=mobile-v1\n"); @@ -106,6 +129,34 @@ describe("PublishClientConfig", () => { }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)), ); + test.provider("writes only relay URL and mint key when Axiom tracing is disabled", (stack) => + Effect.gen(function* () { + const fs = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const dir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-relay-client-config-no-axiom-" }); + const target = path.join(dir, "client.env"); + yield* stack + .deploy( + PublishClientConfig({ + url: "https://relay.s2code.dev", + cloudMintPublicKey: FIXTURE_MINT_PUBLIC_KEY, + }), + ) + .pipe( + Effect.provide( + ConfigProvider.layer( + ConfigProvider.fromUnknown({ T3CODE_RELAY_CLIENT_CONFIG_ENV: target }), + ), + ), + ); + const written = yield* fs.readFileString(target); + expect(written).toContain("T3CODE_RELAY_URL=https://relay.s2code.dev\n"); + expect(written).toContain("T3CODE_CLOUD_MINT_PUBLIC_KEY="); + expect(written).not.toContain("T3CODE_MOBILE_OTLP_TRACES_"); + expect(written).not.toContain("T3CODE_RELAY_CLIENT_OTLP_TRACES_"); + }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)), + ); + test.provider("fails when the worker has no url rather than publishing an empty relay", (stack) => Effect.gen(function* () { const fs = yield* FileSystem.FileSystem; diff --git a/infra/relay/src/clientConfig.ts b/infra/relay/src/clientConfig.ts index 3e2329f4b572c..a226f9db7be41 100644 --- a/infra/relay/src/clientConfig.ts +++ b/infra/relay/src/clientConfig.ts @@ -13,14 +13,15 @@ import * as Schema from "effect/Schema"; export interface RelayClientConfig { readonly url: string | undefined; - readonly mobileTracingUrl: string; - readonly mobileTracingDataset: string; - readonly mobileTracingToken: Redacted.Redacted; - readonly clientTracingUrl: string; - readonly clientTracingDataset: string; - readonly clientTracingToken: Redacted.Redacted; + readonly cloudMintPublicKey: string; + readonly mobileTracingUrl?: string | undefined; + readonly mobileTracingDataset?: string | undefined; + readonly mobileTracingToken?: Redacted.Redacted | undefined; + readonly clientTracingUrl?: string | undefined; + readonly clientTracingDataset?: string | undefined; + readonly clientTracingToken?: Redacted.Redacted | undefined; /** Alchemy hashes JSON inputs; Redacted hides token rotation, so hash the tokens without persisting secrets. */ - readonly tokenDigest: string; + readonly tokenDigest?: string | undefined; } export class RelayUrlUnavailableError extends Schema.TaggedError()( @@ -32,16 +33,33 @@ export class RelayUrlUnavailableError extends Schema.TaggedError - ({ - T3CODE_RELAY_URL: config.url, - T3CODE_MOBILE_OTLP_TRACES_URL: config.mobileTracingUrl, - T3CODE_MOBILE_OTLP_TRACES_DATASET: config.mobileTracingDataset, - T3CODE_MOBILE_OTLP_TRACES_TOKEN: Redacted.value(config.mobileTracingToken), - T3CODE_RELAY_CLIENT_OTLP_TRACES_URL: config.clientTracingUrl, - T3CODE_RELAY_CLIENT_OTLP_TRACES_DATASET: config.clientTracingDataset, - T3CODE_RELAY_CLIENT_OTLP_TRACES_TOKEN: Redacted.value(config.clientTracingToken), - }) as const; +const relayClientConfigEnv = ( + config: RelayClientConfig & { readonly url: string }, +): Readonly> => ({ + T3CODE_RELAY_URL: config.url, + T3CODE_CLOUD_MINT_PUBLIC_KEY: config.cloudMintPublicKey + .trim() + .replaceAll("\r\n", "\n") + .replaceAll("\n", "\\n"), + ...(config.mobileTracingUrl !== undefined + ? { T3CODE_MOBILE_OTLP_TRACES_URL: config.mobileTracingUrl } + : {}), + ...(config.mobileTracingDataset !== undefined + ? { T3CODE_MOBILE_OTLP_TRACES_DATASET: config.mobileTracingDataset } + : {}), + ...(config.mobileTracingToken !== undefined + ? { T3CODE_MOBILE_OTLP_TRACES_TOKEN: Redacted.value(config.mobileTracingToken) } + : {}), + ...(config.clientTracingUrl !== undefined + ? { T3CODE_RELAY_CLIENT_OTLP_TRACES_URL: config.clientTracingUrl } + : {}), + ...(config.clientTracingDataset !== undefined + ? { T3CODE_RELAY_CLIENT_OTLP_TRACES_DATASET: config.clientTracingDataset } + : {}), + ...(config.clientTracingToken !== undefined + ? { T3CODE_RELAY_CLIENT_OTLP_TRACES_TOKEN: Redacted.value(config.clientTracingToken) } + : {}), +}); export class EnvValueNotSingleLineError extends Schema.TaggedError()( "EnvValueNotSingleLineError", @@ -101,10 +119,10 @@ export const PublishClientConfig = Alchemy.Action( Effect.gen(function* () { const fs = yield* FileSystem.FileSystem; const path = yield* Path.Path; - const override = yield* Config.String("T3CODE_RELAY_CLIENT_CONFIG_ENV").pipe(Config.option); - const repoRootEnv = path.fromFileUrl(new URL("../../../.env", import.meta.url)); - const target = Option.isSome(override) ? override.value : yield* repoRootEnv; + const defaultTarget = yield* path.fromFileUrl(new URL("../../../.env", import.meta.url)); return Effect.fn(function* (input: RelayClientConfig) { + const override = yield* Config.String("T3CODE_RELAY_CLIENT_CONFIG_ENV").pipe(Config.option); + const target = Option.isSome(override) ? override.value : defaultTarget; const url = input.url; if (url === undefined) return yield* new RelayUrlUnavailableError(); const entries = relayClientConfigEnv({ ...input, url }); diff --git a/infra/relay/src/db.ts b/infra/relay/src/db.ts index 52133544d4641..1726e6b716c10 100644 --- a/infra/relay/src/db.ts +++ b/infra/relay/src/db.ts @@ -9,7 +9,20 @@ import * as Context from "effect/Context"; import * as Effect from "effect/Effect"; import * as Layer from "effect/Layer"; -import { relayDatabaseMode } from "./dbConfig.ts"; +import { + relayDatabaseBackendConfig, + relayDatabaseMode, + relayDatabaseSizingConfig, +} from "./dbConfig.ts"; +import { withoutWorkerConfigCapture } from "./forkOwnership.ts"; + +function withLogicalId(resource: Resource, logicalId: string): Resource { + return new Proxy(resource, { + has: (target, property) => property === "LogicalId" || property in target, + get: (target, property, receiver) => + property === "LogicalId" ? logicalId : Reflect.get(target, property, receiver), + }); +} export class RelayDb extends Context.Service< RelayDb, @@ -46,13 +59,18 @@ export const PlanetscaleDatabase = Effect.gen(function* () { const mode = relayDatabaseMode(stage); const database = mode === "shared-database" - ? yield* Planetscale.PostgresDatabase("RelayPostgresDatabase", { - name: "t3coderelay", - region: { slug: "us-west" }, - clusterSize: "PS_160", - migrations: { dir: schema.out, table: "relay_migrations" }, - replicas: 2, - }).pipe(RemovalPolicy.retain()) + ? yield* Effect.gen(function* () { + const { clusterSize, replicas } = yield* relayDatabaseSizingConfig.pipe( + withoutWorkerConfigCapture, + ); + return yield* Planetscale.PostgresDatabase("RelayPostgresDatabase", { + name: "t3coderelay", + region: { slug: "us-west" }, + clusterSize, + migrations: { dir: schema.out, table: "relay_migrations" }, + replicas, + }).pipe(RemovalPolicy.retain()); + }) : yield* Planetscale.PostgresDatabase.ref("RelayPostgresDatabase", { stage: "prod", }); @@ -73,13 +91,47 @@ export const PlanetscaleDatabase = Effect.gen(function* () { return { branch, database, runtimeRole }; }); +export const RelayDatabase = Effect.gen(function* () { + const { stage } = yield* Alchemy.Stack; + const backend = yield* relayDatabaseBackendConfig.pipe(withoutWorkerConfigCapture); + if (backend.kind === "planetscale") { + const ps = yield* PlanetscaleDatabase; + return { + kind: "planetscale" as const, + databaseName: ps.database.name, + databaseBranchName: ps.branch?.name ?? "main", + origin: ps.runtimeRole.origin, + originConnectionLimit: backend.originConnectionLimit, + }; + } + if (stage !== "prod") { + return yield* Effect.die( + new Error( + `External Postgres mode only supports the "prod" stage (got "${stage}"). Preview stages require PlanetScale branching.`, + ), + ); + } + return { + kind: "external" as const, + databaseName: backend.databaseName, + databaseBranchName: "external", + origin: backend.origin, + originConnectionLimit: backend.originConnectionLimit, + }; +}); + export const RelayHyperdrive = Effect.gen(function* () { - const { runtimeRole } = yield* PlanetscaleDatabase; + if ((yield* Alchemy.ALCHEMY_PHASE.pipe(withoutWorkerConfigCapture)) === "runtime") { + // At runtime Hyperdrive.Connect only reads LogicalId to look up env.RelayHyperdrive. + const ref = yield* Cloudflare.Hyperdrive.Connection.ref("RelayHyperdrive"); + return withLogicalId(ref, "RelayHyperdrive"); + } + const db = yield* RelayDatabase; return yield* Cloudflare.Hyperdrive.Connection("RelayHyperdrive", { - origin: runtimeRole.origin, + origin: db.origin, caching: { disabled: true, }, - originConnectionLimit: 40, + originConnectionLimit: db.originConnectionLimit, }); }); diff --git a/infra/relay/src/dbConfig.test.ts b/infra/relay/src/dbConfig.test.ts new file mode 100644 index 0000000000000..527e720c330bb --- /dev/null +++ b/infra/relay/src/dbConfig.test.ts @@ -0,0 +1,148 @@ +import { assert, it } from "@effect/vitest"; +import * as ConfigProvider from "effect/ConfigProvider"; +import * as Effect from "effect/Effect"; +import * as Redacted from "effect/Redacted"; +import * as Result from "effect/Result"; + +import { + DEFAULT_EXTERNAL_CONNECTION_LIMIT, + DEFAULT_PLANETSCALE_CONNECTION_LIMIT, + relayDatabaseBackendConfig, + relayDatabaseSizingConfig, +} from "./dbConfig.ts"; + +const provideEnv = (env: Readonly>) => + Effect.provide(ConfigProvider.layer(ConfigProvider.fromEnv({ env }))); + +it.effect("defaults PlanetScale sizing to single-node PS_5 rather than upstream PS_160", () => + Effect.gen(function* () { + for (const env of [{}, { RELAY_DB_CLUSTER_SIZE: "", RELAY_DB_REPLICAS: "" }]) { + const sizing = yield* relayDatabaseSizingConfig.pipe(provideEnv(env)); + assert.deepEqual(sizing, { clusterSize: "PS_5", replicas: 0 }); + } + }), +); + +it.effect("accepts explicit PlanetScale cluster sizing and rejects invalid replica counts", () => + Effect.gen(function* () { + const custom = yield* relayDatabaseSizingConfig.pipe( + provideEnv({ RELAY_DB_CLUSTER_SIZE: "PS_10", RELAY_DB_REPLICAS: "2" }), + ); + assert.deepEqual(custom, { clusterSize: "PS_10", replicas: 2 }); + + for (const bad of ["-1", "1.5", "two"]) { + const result = yield* relayDatabaseSizingConfig.pipe( + provideEnv({ RELAY_DB_REPLICAS: bad }), + Effect.result, + ); + assert.isTrue(Result.isFailure(result), bad); + } + }), +); + +it.effect("defaults relayDatabaseBackendConfig to PlanetScale with upstream connection limit", () => + Effect.gen(function* () { + const backend = yield* relayDatabaseBackendConfig.pipe(provideEnv({})); + assert.deepEqual(backend, { + kind: "planetscale", + clusterSize: "PS_5", + replicas: 0, + originConnectionLimit: DEFAULT_PLANETSCALE_CONNECTION_LIMIT, + }); + }), +); + +it.effect("builds a PublicOrigin in external mode when Access credentials are unset", () => + Effect.gen(function* () { + const backend = yield* relayDatabaseBackendConfig.pipe( + provideEnv({ + RELAY_DB_HOST: "pg.s2code.dev", + RELAY_DB_PORT: "6432", + RELAY_DB_NAME: "s2coderelay", + RELAY_DB_USER: "relay", + RELAY_DB_PASSWORD: "db-password", + }), + ); + assert.equal(backend.kind, "external"); + if (backend.kind !== "external") return; + assert.equal(backend.databaseName, "s2coderelay"); + assert.equal(backend.originConnectionLimit, DEFAULT_EXTERNAL_CONNECTION_LIMIT); + assert.isFalse("accessClientId" in backend.origin); + assert.deepEqual( + { ...backend.origin, password: Redacted.value(backend.origin.password) }, + { + scheme: "postgres", + host: "pg.s2code.dev", + port: 6432, + database: "s2coderelay", + user: "relay", + password: "db-password", + }, + ); + }), +); + +it.effect("builds an AccessOrigin when both Access service token credentials are set", () => + Effect.gen(function* () { + const backend = yield* relayDatabaseBackendConfig.pipe( + provideEnv({ + RELAY_DB_HOST: "pg.s2code.dev", + RELAY_DB_NAME: "s2coderelay", + RELAY_DB_USER: "relay", + RELAY_DB_PASSWORD: "db-password", + RELAY_DB_ACCESS_CLIENT_ID: "cf-access-id", + RELAY_DB_ACCESS_CLIENT_SECRET: "cf-access-secret", + RELAY_DB_CONNECTION_LIMIT: "12", + }), + ); + assert.equal(backend.kind, "external"); + if (backend.kind !== "external") return; + assert.equal(backend.originConnectionLimit, 12); + assert.isTrue("accessClientId" in backend.origin); + if (!("accessClientId" in backend.origin)) return; + assert.deepEqual( + { + ...backend.origin, + password: Redacted.value(backend.origin.password), + accessClientId: Redacted.value(backend.origin.accessClientId), + accessClientSecret: Redacted.value(backend.origin.accessClientSecret), + }, + { + scheme: "postgres", + host: "pg.s2code.dev", + database: "s2coderelay", + user: "relay", + password: "db-password", + accessClientId: "cf-access-id", + accessClientSecret: "cf-access-secret", + }, + ); + }), +); + +it.effect("rejects half-set Access credentials, missing DB fields, and upstream DB hosts", () => + Effect.gen(function* () { + const base = { + RELAY_DB_HOST: "pg.s2code.dev", + RELAY_DB_NAME: "s2coderelay", + RELAY_DB_USER: "relay", + RELAY_DB_PASSWORD: "db-password", + }; + const invalidCases: ReadonlyArray>> = [ + { ...base, RELAY_DB_ACCESS_CLIENT_ID: "only-id" }, + { ...base, RELAY_DB_ACCESS_CLIENT_SECRET: "only-secret" }, + { ...base, RELAY_DB_HOST: "db.t3.codes" }, + { ...base, RELAY_DB_HOST: "db.t3coderelay.com" }, + { ...base, RELAY_DB_HOST: "db.example.com" }, + { ...base, RELAY_DB_NAME: "" }, + { ...base, RELAY_DB_USER: "" }, + { ...base, RELAY_DB_PASSWORD: "" }, + { ...base, RELAY_DB_PORT: "70000" }, + { ...base, RELAY_DB_CONNECTION_LIMIT: "0" }, + ]; + for (const env of invalidCases) { + const result = yield* relayDatabaseBackendConfig.pipe(provideEnv(env), Effect.result); + assert.isTrue(Result.isFailure(result), JSON.stringify(env)); + } + }), +); diff --git a/infra/relay/src/dbConfig.ts b/infra/relay/src/dbConfig.ts index 9f6416aedceac..2471732dc62e0 100644 --- a/infra/relay/src/dbConfig.ts +++ b/infra/relay/src/dbConfig.ts @@ -1,5 +1,197 @@ +import type * as Cloudflare from "@alchemy.run/cloudflare"; +import * as Config from "effect/Config"; +import * as Effect from "effect/Effect"; +import * as Option from "effect/Option"; +import * as Redacted from "effect/Redacted"; +import * as Schema from "effect/Schema"; +import * as SchemaIssue from "effect/SchemaIssue"; + +import { assertForkRelayHost } from "./forkOwnership.ts"; + export type RelayDatabaseMode = "shared-database" | "stage-branch"; export function relayDatabaseMode(stage: string): RelayDatabaseMode { return stage === "prod" ? "shared-database" : "stage-branch"; } + +export const DEFAULT_RELAY_DB_CLUSTER_SIZE = "PS_5"; +export const DEFAULT_RELAY_DB_REPLICAS = 0; +export const DEFAULT_PLANETSCALE_CONNECTION_LIMIT = 40; +export const DEFAULT_EXTERNAL_CONNECTION_LIMIT = 20; +const DEFAULT_POSTGRES_PORT = 5432; + +export type RelayDatabaseBackend = + | { + readonly kind: "planetscale"; + readonly clusterSize: string; + readonly replicas: number; + readonly originConnectionLimit: number; + } + | { + readonly kind: "external"; + readonly origin: Cloudflare.Hyperdrive.PublicOrigin | Cloudflare.Hyperdrive.AccessOrigin; + readonly databaseName: string; + readonly originConnectionLimit: number; + }; + +const invalidConfig = (message: string) => + new Config.ConfigError(new Schema.SchemaError(new SchemaIssue.InvalidValue({ message }))); + +const optionalTrimmed = (name: string) => + Config.String(name).pipe( + Config.option, + Config.map((value) => { + const trimmed = Option.getOrElse(value, () => "").trim(); + return trimmed.length > 0 ? trimmed : undefined; + }), + ); + +const optionalRedacted = (name: string) => + Config.Redacted(name).pipe( + Config.option, + Config.map((value) => { + if (Option.isNone(value)) return undefined; + const trimmed = Redacted.value(value.value).trim(); + return trimmed.length > 0 ? Redacted.make(trimmed) : undefined; + }), + ); + +const parseNonNegativeInt = (name: string, fallback: number, min = 0, max?: number) => + Effect.fn(`RelayDb.parse_${name}`)(function* (value: string | undefined) { + if (value === undefined) return fallback; + const parsed = /^\d+$/u.test(value) ? Number(value) : Number.NaN; + if (!Number.isSafeInteger(parsed) || parsed < min || (max !== undefined && parsed > max)) { + return yield* Effect.fail( + invalidConfig( + max !== undefined + ? `${name} must be an integer between ${min} and ${max}.` + : `${name} must be an integer >= ${min}.`, + ), + ); + } + return parsed; + }); + +export const relayDatabaseSizingConfig = Config.all({ + clusterSize: optionalTrimmed("RELAY_DB_CLUSTER_SIZE").pipe( + Config.map((value) => value ?? DEFAULT_RELAY_DB_CLUSTER_SIZE), + ), + replicas: optionalTrimmed("RELAY_DB_REPLICAS").pipe( + Config.mapEffect(parseNonNegativeInt("RELAY_DB_REPLICAS", DEFAULT_RELAY_DB_REPLICAS)), + ), +}); + +const resolveDatabaseBackend = Effect.fn("RelayDb.resolveBackend")(function* (input: { + readonly host: string | undefined; + readonly port: string | undefined; + readonly database: string | undefined; + readonly user: string | undefined; + readonly password: Redacted.Redacted | undefined; + readonly accessClientId: Redacted.Redacted | undefined; + readonly accessClientSecret: Redacted.Redacted | undefined; + readonly connectionLimit: string | undefined; + readonly sizing: { readonly clusterSize: string; readonly replicas: number }; +}): Effect.fn.Return { + if (input.host === undefined) { + const originConnectionLimit = yield* parseNonNegativeInt( + "RELAY_DB_CONNECTION_LIMIT", + DEFAULT_PLANETSCALE_CONNECTION_LIMIT, + 1, + )(input.connectionLimit); + return { + kind: "planetscale", + clusterSize: input.sizing.clusterSize, + replicas: input.sizing.replicas, + originConnectionLimit, + }; + } + + const validatedHost = yield* Effect.try({ + try: () => assertForkRelayHost("RELAY_DB_HOST", input.host), + catch: (error) => + invalidConfig(error instanceof Error ? error.message : "RELAY_DB_HOST is invalid."), + }); + + if (input.database === undefined) { + return yield* Effect.fail( + invalidConfig("RELAY_DB_NAME is required when RELAY_DB_HOST is set."), + ); + } + if (input.user === undefined) { + return yield* Effect.fail( + invalidConfig("RELAY_DB_USER is required when RELAY_DB_HOST is set."), + ); + } + if (input.password === undefined) { + return yield* Effect.fail( + invalidConfig("RELAY_DB_PASSWORD is required when RELAY_DB_HOST is set."), + ); + } + + const hasAccessId = input.accessClientId !== undefined; + const hasAccessSecret = input.accessClientSecret !== undefined; + if (hasAccessId !== hasAccessSecret) { + return yield* Effect.fail( + invalidConfig( + "RELAY_DB_ACCESS_CLIENT_ID and RELAY_DB_ACCESS_CLIENT_SECRET must both be set for Cloudflare Access origins.", + ), + ); + } + + const originConnectionLimit = yield* parseNonNegativeInt( + "RELAY_DB_CONNECTION_LIMIT", + DEFAULT_EXTERNAL_CONNECTION_LIMIT, + 1, + )(input.connectionLimit); + + if (hasAccessId && hasAccessSecret) { + const origin: Cloudflare.Hyperdrive.AccessOrigin = { + scheme: "postgres", + host: validatedHost, + database: input.database, + user: input.user, + password: input.password, + accessClientId: input.accessClientId, + accessClientSecret: input.accessClientSecret, + }; + return { + kind: "external", + origin, + databaseName: input.database, + originConnectionLimit, + }; + } + + const port = yield* parseNonNegativeInt( + "RELAY_DB_PORT", + DEFAULT_POSTGRES_PORT, + 1, + 65535, + )(input.port); + const origin: Cloudflare.Hyperdrive.PublicOrigin = { + scheme: "postgres", + host: validatedHost, + port, + database: input.database, + user: input.user, + password: input.password, + }; + return { + kind: "external", + origin, + databaseName: input.database, + originConnectionLimit, + }; +}); + +export const relayDatabaseBackendConfig: Config.Config = Config.all({ + host: optionalTrimmed("RELAY_DB_HOST"), + port: optionalTrimmed("RELAY_DB_PORT"), + database: optionalTrimmed("RELAY_DB_NAME"), + user: optionalTrimmed("RELAY_DB_USER"), + password: optionalRedacted("RELAY_DB_PASSWORD"), + accessClientId: optionalRedacted("RELAY_DB_ACCESS_CLIENT_ID"), + accessClientSecret: optionalRedacted("RELAY_DB_ACCESS_CLIENT_SECRET"), + connectionLimit: optionalTrimmed("RELAY_DB_CONNECTION_LIMIT"), + sizing: relayDatabaseSizingConfig, +}).pipe(Config.mapEffect(resolveDatabaseBackend)); diff --git a/infra/relay/src/forkOwnership.test.ts b/infra/relay/src/forkOwnership.test.ts new file mode 100644 index 0000000000000..cb7220ab33cca --- /dev/null +++ b/infra/relay/src/forkOwnership.test.ts @@ -0,0 +1,254 @@ +// @effect-diagnostics nodeBuiltinImport:off -- Offline guard checks inspect source and workflows without provisioning providers. +import * as NodeFS from "node:fs"; +import { UPSTREAM_HOSTED_DOMAINS } from "@t3tools/shared/hostedConfig"; +import { assert, describe, expect, it } from "@effect/vitest"; +import * as ConfigProvider from "effect/ConfigProvider"; +import * as Effect from "effect/Effect"; +import * as Result from "effect/Result"; + +import { assertForkRelayDeploymentEnv, forkRelayDeploymentConfig } from "./forkOwnership.ts"; + +const VALID_ACCOUNT_ID = "771240435fb4f1407f2b4669085dc79d"; +const VALID_CLERK_KEY = `pk_live_${btoa("clerk.s2code.dev$")}`; + +const validExternalEnv = ( + overrides: Readonly> = {}, +): Record => ({ + CLOUDFLARE_ACCOUNT_ID: VALID_ACCOUNT_ID, + RELAY_API_ZONE_NAME: "s2code.dev", + RELAY_TUNNEL_ZONE_NAME: "s2code.dev", + CLERK_PUBLISHABLE_KEY: VALID_CLERK_KEY, + RELAY_DB_HOST: "pg.s2code.dev", + RELAY_DB_NAME: "s2coderelay", + RELAY_DB_USER: "relay", + RELAY_DB_PASSWORD: "secret-password", + RELAY_DB_ACCESS_CLIENT_ID: "access-id", + RELAY_DB_ACCESS_CLIENT_SECRET: "access-secret", + ...overrides, +}); + +const validPlanetscaleEnv = ( + overrides: Readonly> = {}, +): Record => ({ + CLOUDFLARE_ACCOUNT_ID: VALID_ACCOUNT_ID, + RELAY_API_ZONE_NAME: "s2code.dev", + RELAY_TUNNEL_ZONE_NAME: "s2code.dev", + CLERK_PUBLISHABLE_KEY: VALID_CLERK_KEY, + PLANETSCALE_ORGANIZATION: "shuv-org", + ...overrides, +}); + +describe("assertForkRelayDeploymentEnv", () => { + it("accepts external Postgres without PlanetScale or Axiom credentials", () => { + expect(assertForkRelayDeploymentEnv(validExternalEnv())).toEqual({ + cloudflareAccountId: VALID_ACCOUNT_ID, + relayApiZoneName: "s2code.dev", + relayTunnelZoneName: "s2code.dev", + relayDomain: undefined, + clerkPublishableKey: VALID_CLERK_KEY, + databaseBackend: "external", + observabilityBackend: "none", + }); + expect( + assertForkRelayDeploymentEnv( + validExternalEnv({ RELAY_DOMAIN: "relay.s2code.dev", AXIOM_TOKEN: "xaat-test" }), + ), + ).toMatchObject({ + relayDomain: "relay.s2code.dev", + databaseBackend: "external", + observabilityBackend: "axiom", + }); + }); + + it("accepts PlanetScale mode when PLANETSCALE_ORGANIZATION is configured", () => { + expect(assertForkRelayDeploymentEnv(validPlanetscaleEnv())).toMatchObject({ + databaseBackend: "planetscale", + observabilityBackend: "none", + }); + }); + + it("fails closed when neither external Postgres nor PlanetScale is configured", () => { + expect(() => + assertForkRelayDeploymentEnv(validPlanetscaleEnv({ PLANETSCALE_ORGANIZATION: undefined })), + ).toThrow("Configure either external Postgres (RELAY_DB_HOST) or PlanetScale"); + }); + + it("fails closed on empty env, malformed account IDs, half-set Access creds, or unsafe OTLP URLs", () => { + expect(() => assertForkRelayDeploymentEnv({})).toThrow("CLOUDFLARE_ACCOUNT_ID"); + for (const badAccount of ["", "not-hex", "1234", "g".repeat(32)]) { + expect(() => + assertForkRelayDeploymentEnv(validExternalEnv({ CLOUDFLARE_ACCOUNT_ID: badAccount })), + ).toThrow("CLOUDFLARE_ACCOUNT_ID"); + } + expect(() => assertForkRelayDeploymentEnv(validExternalEnv({ RELAY_DB_PASSWORD: "" }))).toThrow( + "RELAY_DB_PASSWORD", + ); + expect(() => + assertForkRelayDeploymentEnv(validExternalEnv({ RELAY_DB_ACCESS_CLIENT_SECRET: "" })), + ).toThrow("RELAY_DB_ACCESS_CLIENT_ID and RELAY_DB_ACCESS_CLIENT_SECRET"); + expect(() => + assertForkRelayDeploymentEnv(validExternalEnv({ RELAY_DB_ACCESS_CLIENT_ID: "" })), + ).toThrow("RELAY_DB_ACCESS_CLIENT_ID and RELAY_DB_ACCESS_CLIENT_SECRET"); + expect(() => + assertForkRelayDeploymentEnv( + validExternalEnv({ + RELAY_OTLP_TRACES_URL: "https://user:pass@traces.s2code.dev/v1/traces", + }), + ), + ).toThrow("embedded credentials"); + expect(() => + assertForkRelayDeploymentEnv( + validExternalEnv({ RELAY_OTLP_TRACES_URL: "https://traces.example.com/v1/traces" }), + ), + ).toThrow("RELAY_OTLP_TRACES_URL"); + }); + + it.each(UPSTREAM_HOSTED_DOMAINS)( + "rejects upstream domain %s across relay zones, domain override, external DB host, and Clerk key", + (root) => { + for (const host of [root, `relay.${root}`]) { + expect(() => + assertForkRelayDeploymentEnv(validExternalEnv({ RELAY_API_ZONE_NAME: host })), + ).toThrow("RELAY_API_ZONE_NAME"); + expect(() => + assertForkRelayDeploymentEnv(validExternalEnv({ RELAY_TUNNEL_ZONE_NAME: host })), + ).toThrow("RELAY_TUNNEL_ZONE_NAME"); + expect(() => + assertForkRelayDeploymentEnv(validExternalEnv({ RELAY_DOMAIN: host })), + ).toThrow("RELAY_DOMAIN"); + expect(() => + assertForkRelayDeploymentEnv(validExternalEnv({ RELAY_DB_HOST: host })), + ).toThrow("RELAY_DB_HOST"); + expect(() => + assertForkRelayDeploymentEnv( + validExternalEnv({ CLERK_PUBLISHABLE_KEY: `pk_live_${btoa(`clerk.${host}$`)}` }), + ), + ).toThrow("CLERK_PUBLISHABLE_KEY"); + } + }, + ); + + it.each([ + "example.com", + "relay.example.com", + "tunnels.example.net", + "relay.example.org", + "relay.example", + "relay.invalid", + "relay.test", + ])("rejects placeholder host %s", (host) => { + expect(() => + assertForkRelayDeploymentEnv(validExternalEnv({ RELAY_API_ZONE_NAME: host })), + ).toThrow("RELAY_API_ZONE_NAME"); + expect(() => + assertForkRelayDeploymentEnv(validExternalEnv({ RELAY_TUNNEL_ZONE_NAME: host })), + ).toThrow("RELAY_TUNNEL_ZONE_NAME"); + expect(() => assertForkRelayDeploymentEnv(validExternalEnv({ RELAY_DOMAIN: host }))).toThrow( + "RELAY_DOMAIN", + ); + expect(() => assertForkRelayDeploymentEnv(validExternalEnv({ RELAY_DB_HOST: host }))).toThrow( + "RELAY_DB_HOST", + ); + expect(() => + assertForkRelayDeploymentEnv( + validExternalEnv({ CLERK_PUBLISHABLE_KEY: `pk_test_${btoa(`clerk.${host}$`)}` }), + ), + ).toThrow("CLERK_PUBLISHABLE_KEY"); + }); + + it.effect( + "resolves forkRelayDeploymentConfig through Effect ConfigProvider (for local .env)", + () => + Effect.gen(function* () { + const env = Object.fromEntries( + Object.entries(validExternalEnv()).filter(([, value]) => value !== undefined), + ) as Record; + const resolved = yield* forkRelayDeploymentConfig.pipe( + Effect.provide(ConfigProvider.layer(ConfigProvider.fromEnv({ env }))), + ); + assert.equal(resolved.cloudflareAccountId, VALID_ACCOUNT_ID); + assert.equal(resolved.databaseBackend, "external"); + + const empty = yield* forkRelayDeploymentConfig.pipe( + Effect.provide(ConfigProvider.layer(ConfigProvider.fromEnv({ env: {} }))), + Effect.result, + ); + assert.isTrue(Result.isFailure(empty)); + }), + ); + + it("pins Cloudflare.state through Effect Config rather than top-level process.env", () => { + const source = NodeFS.readFileSync(new URL("../alchemy.run.ts", import.meta.url), "utf8"); + expect(source).not.toContain("process.env"); + expect(source).toContain("forkRelayDeploymentConfig"); + expect(source).toContain("Cloudflare.state({ accountId: forkRelayEnv.cloudflareAccountId })"); + expect(source).toContain("cloudMintPublicKey: cloudMintKeyPair.publicKey"); + }); +}); + +describe("hosted workflow guards", () => { + const readWorkflow = (name: string) => + NodeFS.readFileSync(new URL(`../../../.github/workflows/${name}`, import.meta.url), "utf8"); + + it("enforces deploy-relay.yml repo/main guard, preflight-before-migrations order, env-only Access tokens, and token-free summary", () => { + const deploy = readWorkflow("deploy-relay.yml"); + expect(deploy).toContain("github.repository == 'shuv1337/s2code'"); + expect(deploy).toContain("github.ref == 'refs/heads/main'"); + expect(deploy).toContain( + "(github.event_name == 'workflow_dispatch' || vars.RELAY_API_ZONE_NAME != '')", + ); + expect(deploy).toContain("runs-on: ubuntu-24.04"); + expect(deploy).toContain("name: production"); + expect(deploy).toContain("cancel-in-progress: false"); + expect(deploy).not.toMatch(/APNS_PRIVATE_KEY|FCM_SERVICE_ACCOUNT|T3CHAT_LINK_SECRET/); + + const preflightIndex = deploy.indexOf("- name: Verify fork relay ownership configuration"); + const migrateIndex = deploy.indexOf("- name: Run external Postgres migrations"); + const deployIndex = deploy.indexOf("- name: Deploy production relay stage"); + expect(preflightIndex).toBeGreaterThan(-1); + expect(migrateIndex).toBeGreaterThan(preflightIndex); + expect(deployIndex).toBeGreaterThan(migrateIndex); + + const migrateStep = deploy.slice(migrateIndex, deployIndex); + expect(migrateStep).toContain("if: env.RELAY_DB_HOST != ''"); + expect(migrateStep).toContain("TUNNEL_SERVICE_TOKEN_ID:"); + expect(migrateStep).toContain("TUNNEL_SERVICE_TOKEN_SECRET:"); + expect(migrateStep).not.toMatch(/--service-token-id|--service-token-secret/); + expect(migrateStep).toContain("${RELAY_DB_MIGRATION_SSLMODE:-disable}"); + expect(migrateStep).toContain("vp run --filter t3code-relay migrate:postgres"); + + const summaryStep = deploy.split("- name: Publish public relay outputs to summary")[1]!; + expect(summaryStep).toContain("relay-client-config.env"); + expect(summaryStep).not.toContain("npx"); + expect(summaryStep).not.toMatch( + /CLOUDFLARE_API_TOKEN|Token|token|Credentials|credentials|PASSWORD/, + ); + }); + + it("gates every job in relay-preview.yml and relay-preview-cleanup.yml behind RELAY_PREVIEWS_ENABLED", () => { + const preview = readWorkflow("relay-preview.yml"); + expect(preview).toContain("github.repository == 'shuv1337/s2code'"); + expect(preview).toContain("vars.RELAY_PREVIEWS_ENABLED == 'true'"); + + const cleanup = readWorkflow("relay-preview-cleanup.yml"); + const jobsSection = cleanup.split("\njobs:\n")[1]!; + const jobBlocks = jobsSection.split(/\n (?=[a-z_]+:\n)/u); + expect(jobBlocks).toHaveLength(3); + for (const block of jobBlocks) { + expect(block).toContain("github.repository == 'shuv1337/s2code'"); + expect(block).toContain("vars.RELAY_PREVIEWS_ENABLED == 'true'"); + } + }); + + it("keeps web-preview.yml notice-only without secrets or executable deployment steps", () => { + const webPreview = readWorkflow("web-preview.yml"); + expect(webPreview).toContain("::notice::"); + expect(webPreview).toContain("GITHUB_STEP_SUMMARY"); + expect(webPreview).not.toMatch( + /secrets:|secrets\.|uses:|id-token:|environment:|hosted-preflight|FORK_HOSTED_DEPLOYMENT_VERIFIED/, + ); + const lines = webPreview.split("run: |\n")[1]!.trim().split("\n"); + expect(lines).toHaveLength(2); + for (const line of lines) expect(line.trim()).toMatch(/^echo '/); + }); +}); diff --git a/infra/relay/src/forkOwnership.ts b/infra/relay/src/forkOwnership.ts new file mode 100644 index 0000000000000..40c99e8b8598c --- /dev/null +++ b/infra/relay/src/forkOwnership.ts @@ -0,0 +1,207 @@ +import * as Alchemy from "alchemy"; +import { forkClerkPublishableKey, forkHostedOrigin } from "@t3tools/shared/hostedConfig"; +import * as Config from "effect/Config"; +import * as Context from "effect/Context"; +import * as Effect from "effect/Effect"; +import * as Option from "effect/Option"; +import * as Schema from "effect/Schema"; +import * as SchemaIssue from "effect/SchemaIssue"; + +const CLOUDFLARE_ACCOUNT_ID_PATTERN = /^[0-9a-f]{32}$/iu; +const PLACEHOLDER_DOMAINS = ["example.com", "example.net", "example.org"] as const; +const PLACEHOLDER_TLDS = [".example", ".invalid", ".test"] as const; + +export interface ForkRelayDeploymentEnv { + readonly cloudflareAccountId: string; + readonly relayApiZoneName: string; + readonly relayTunnelZoneName: string; + readonly relayDomain: string | undefined; + readonly clerkPublishableKey: string; + readonly databaseBackend: "planetscale" | "external"; + readonly observabilityBackend: "axiom" | "none"; +} + +/** + * Strip Alchemy's Worker `RuntimeContext` around deploy-only `Config.*` reads so + * `Platform.ts` does not bind deploy-time secrets/vars onto the public Worker env. + */ +export const withoutWorkerConfigCapture = ( + effect: Effect.Effect, +): Effect.Effect => + Effect.updateContext( + effect, + Context.omit(Alchemy.RuntimeContext) as (ctx: Context.Context) => Context.Context, + ); + +function isPlaceholderDomain(hostname: string): boolean { + const host = hostname.toLowerCase().replace(/\.+$/u, ""); + return ( + PLACEHOLDER_DOMAINS.some((root) => host === root || host.endsWith(`.${root}`)) || + PLACEHOLDER_TLDS.some((tld) => host.endsWith(tld)) + ); +} + +export function assertForkRelayHost(name: string, value: string | undefined): string { + const trimmed = value?.trim() ?? ""; + if (!trimmed) { + throw new Error(`${name} is required for fork relay deployment.`); + } + const normalized = trimmed.toLowerCase().replace(/\.+$/u, ""); + const origin = forkHostedOrigin(`https://${normalized}`); + if ( + origin === null || + new URL(origin).hostname !== normalized || + !normalized.includes(".") || + isPlaceholderDomain(normalized) + ) { + throw new Error(`${name} must be a fork-owned domain, not an upstream or placeholder host.`); + } + return normalized; +} + +export function assertForkOtlpTracesUrl(value: string): string { + let parsed: URL; + try { + parsed = new URL(value.trim()); + } catch { + throw new Error("RELAY_OTLP_TRACES_URL must be a valid fork-owned HTTPS URL."); + } + if (parsed.protocol !== "https:") { + throw new Error("RELAY_OTLP_TRACES_URL must use HTTPS."); + } + if (parsed.username || parsed.password) { + throw new Error("RELAY_OTLP_TRACES_URL must not contain embedded credentials."); + } + assertForkRelayHost("RELAY_OTLP_TRACES_URL", parsed.hostname); + return parsed.toString(); +} + +function assertForkRelayClerkPublishableKey(value: string | undefined): string { + const key = forkClerkPublishableKey(value); + if (key === null) { + throw new Error( + "CLERK_PUBLISHABLE_KEY must be a valid fork-owned Clerk publishable key, not an upstream key.", + ); + } + const host = atob(key.split("_").slice(2).join("_")) + .replace(/\$$/u, "") + .toLowerCase() + .replace(/\.+$/u, ""); + if (!host.includes(".") || isPlaceholderDomain(host)) { + throw new Error("CLERK_PUBLISHABLE_KEY must not target a placeholder domain."); + } + return key; +} + +function assertForkRelayDatabaseEnv( + env: Readonly>, +): "planetscale" | "external" { + const externalHost = env.RELAY_DB_HOST?.trim(); + if (externalHost) { + assertForkRelayHost("RELAY_DB_HOST", externalHost); + for (const key of ["RELAY_DB_NAME", "RELAY_DB_USER", "RELAY_DB_PASSWORD"] as const) { + if (!env[key]?.trim()) { + throw new Error(`${key} is required when RELAY_DB_HOST is set.`); + } + } + const accessId = env.RELAY_DB_ACCESS_CLIENT_ID?.trim() ?? ""; + const accessSecret = env.RELAY_DB_ACCESS_CLIENT_SECRET?.trim() ?? ""; + if (accessId.length > 0 !== accessSecret.length > 0) { + throw new Error( + "RELAY_DB_ACCESS_CLIENT_ID and RELAY_DB_ACCESS_CLIENT_SECRET must both be set for Cloudflare Access origins.", + ); + } + const rawPort = env.RELAY_DB_PORT?.trim() ?? ""; + if (rawPort && accessId.length === 0) { + const port = /^\d+$/u.test(rawPort) ? Number(rawPort) : Number.NaN; + if (!Number.isSafeInteger(port) || port < 1 || port > 65535) { + throw new Error("RELAY_DB_PORT must be a valid TCP port (1-65535)."); + } + } + return "external"; + } + if (env.PLANETSCALE_ORGANIZATION?.trim()) { + return "planetscale"; + } + throw new Error( + "Configure either external Postgres (RELAY_DB_HOST) or PlanetScale (PLANETSCALE_ORGANIZATION) before deploying.", + ); +} + +/** Fail closed before Alchemy constructs providers or accesses remote state. */ +export function assertForkRelayDeploymentEnv( + env: Readonly>, +): ForkRelayDeploymentEnv { + const cloudflareAccountId = env.CLOUDFLARE_ACCOUNT_ID?.trim() ?? ""; + if (!CLOUDFLARE_ACCOUNT_ID_PATTERN.test(cloudflareAccountId)) { + throw new Error("CLOUDFLARE_ACCOUNT_ID must be a 32-character hex Cloudflare account ID."); + } + const relayApiZoneName = assertForkRelayHost("RELAY_API_ZONE_NAME", env.RELAY_API_ZONE_NAME); + const relayTunnelZoneName = assertForkRelayHost( + "RELAY_TUNNEL_ZONE_NAME", + env.RELAY_TUNNEL_ZONE_NAME, + ); + const rawRelayDomain = env.RELAY_DOMAIN?.trim(); + const relayDomain = rawRelayDomain + ? assertForkRelayHost("RELAY_DOMAIN", rawRelayDomain) + : undefined; + const clerkPublishableKey = assertForkRelayClerkPublishableKey(env.CLERK_PUBLISHABLE_KEY); + const databaseBackend = assertForkRelayDatabaseEnv(env); + const rawOtlpTracesUrl = env.RELAY_OTLP_TRACES_URL?.trim(); + if (rawOtlpTracesUrl) { + assertForkOtlpTracesUrl(rawOtlpTracesUrl); + } + const observabilityBackend = env.AXIOM_TOKEN?.trim() ? "axiom" : "none"; + + return { + cloudflareAccountId, + relayApiZoneName, + relayTunnelZoneName, + relayDomain, + clerkPublishableKey, + databaseBackend, + observabilityBackend, + }; +} + +const optionalConfigString = (name: string) => + Config.String(name).pipe( + Config.option, + Config.map((value) => Option.getOrUndefined(value)), + ); + +const validateForkRelayConfig = Effect.fn("ForkOwnership.validate")(function* ( + env: Readonly>, +) { + return yield* Effect.try({ + try: () => assertForkRelayDeploymentEnv(env), + catch: (error) => + new Config.ConfigError( + new Schema.SchemaError( + new SchemaIssue.InvalidValue({ + message: + error instanceof Error ? error.message : "Invalid fork relay deployment config.", + }), + ), + ), + }); +}); + +/** Reads deployment ownership settings through Effect Config so Alchemy's `.env` loader applies. */ +export const forkRelayDeploymentConfig: Config.Config = Config.all({ + CLOUDFLARE_ACCOUNT_ID: optionalConfigString("CLOUDFLARE_ACCOUNT_ID"), + RELAY_API_ZONE_NAME: optionalConfigString("RELAY_API_ZONE_NAME"), + RELAY_TUNNEL_ZONE_NAME: optionalConfigString("RELAY_TUNNEL_ZONE_NAME"), + RELAY_DOMAIN: optionalConfigString("RELAY_DOMAIN"), + CLERK_PUBLISHABLE_KEY: optionalConfigString("CLERK_PUBLISHABLE_KEY"), + RELAY_DB_HOST: optionalConfigString("RELAY_DB_HOST"), + RELAY_DB_PORT: optionalConfigString("RELAY_DB_PORT"), + RELAY_DB_NAME: optionalConfigString("RELAY_DB_NAME"), + RELAY_DB_USER: optionalConfigString("RELAY_DB_USER"), + RELAY_DB_PASSWORD: optionalConfigString("RELAY_DB_PASSWORD"), + RELAY_DB_ACCESS_CLIENT_ID: optionalConfigString("RELAY_DB_ACCESS_CLIENT_ID"), + RELAY_DB_ACCESS_CLIENT_SECRET: optionalConfigString("RELAY_DB_ACCESS_CLIENT_SECRET"), + PLANETSCALE_ORGANIZATION: optionalConfigString("PLANETSCALE_ORGANIZATION"), + RELAY_OTLP_TRACES_URL: optionalConfigString("RELAY_OTLP_TRACES_URL"), + AXIOM_TOKEN: optionalConfigString("AXIOM_TOKEN"), +}).pipe(Config.mapEffect(validateForkRelayConfig)); diff --git a/infra/relay/src/hostedDisabled.test.ts b/infra/relay/src/hostedDisabled.test.ts deleted file mode 100644 index 1294123ff50f5..0000000000000 --- a/infra/relay/src/hostedDisabled.test.ts +++ /dev/null @@ -1,40 +0,0 @@ -// @effect-diagnostics nodeBuiltinImport:off -- Offline guard checks never import provisioning providers. -import * as NodeFS from "node:fs"; -import * as NodeVM from "node:vm"; -import { expect, it, vi } from "vite-plus/test"; - -it.each([undefined, "1"])( - "refuses relay stack construction even with the former verification flag set to %s", - (flag) => { - const source = NodeFS.readFileSync(new URL("../alchemy.run.ts", import.meta.url), "utf8") - .replace(/^import .*;\n/gm, "") - .replace("export default ", ""); - const stack = vi.fn(); - expect(() => - NodeVM.runInNewContext(source, { - process: { env: { FORK_HOSTED_DEPLOYMENT_VERIFIED: flag } }, - Alchemy: { Stack: stack }, - }), - ).toThrow("Hosted relay deployment is not supported in this fork."); - expect(stack).not.toHaveBeenCalled(); - }, -); - -it.each(["web-preview.yml", "deploy-relay.yml"])( - "keeps %s notice-only without secrets or executable deployment steps", - (name) => { - const source = NodeFS.readFileSync( - new URL(`../../../.github/workflows/${name}`, import.meta.url), - "utf8", - ); - expect(source).toContain("::notice::"); - expect(source).toContain("GITHUB_STEP_SUMMARY"); - expect(source).not.toMatch( - /secrets:|secrets\.|uses:|id-token:|environment:|hosted-preflight|FORK_HOSTED_DEPLOYMENT_VERIFIED/, - ); - // The only shell actions in either workflow are the visible notices/summaries. - const lines = source.split("run: |\n")[1]!.trim().split("\n"); - expect(lines).toHaveLength(2); - for (const line of lines) expect(line.trim()).toMatch(/^echo '/); - }, -); diff --git a/infra/relay/src/mobileTelemetry/MobileTelemetry.ts b/infra/relay/src/mobileTelemetry/MobileTelemetry.ts index 88a6ba1e4dec0..f4fa537fb34e2 100644 --- a/infra/relay/src/mobileTelemetry/MobileTelemetry.ts +++ b/infra/relay/src/mobileTelemetry/MobileTelemetry.ts @@ -328,6 +328,12 @@ const make = Effect.gen(function* () { export const layer = Layer.effect(MobileTelemetry, make); +export const layerDisabled = Layer.succeed(MobileTelemetry, { + handle: Effect.fn("MobileTelemetry.handleDisabled")(() => + Effect.succeed(errorResponse(503, "mobile_telemetry_not_configured")), + ), +}); + /** Raw so the body is read once, capped. */ export const layerApi = HttpApiBuilder.group( RelayApi, diff --git a/infra/relay/src/observability.test.ts b/infra/relay/src/observability.test.ts new file mode 100644 index 0000000000000..7895f0f6edd02 --- /dev/null +++ b/infra/relay/src/observability.test.ts @@ -0,0 +1,62 @@ +import { assert, it } from "@effect/vitest"; +import * as ConfigProvider from "effect/ConfigProvider"; +import * as Effect from "effect/Effect"; +import * as Redacted from "effect/Redacted"; +import * as Result from "effect/Result"; +import * as HttpServerRequest from "effect/http/HttpServerRequest"; + +import * as MobileTelemetry from "./mobileTelemetry/MobileTelemetry.ts"; +import { isAxiomObservabilityEnabled, relayOtlpTracesConfig } from "./observability.ts"; + +const provideEnv = (env: Readonly>) => + Effect.provide(ConfigProvider.layer(ConfigProvider.fromEnv({ env }))); + +it.effect("enables Axiom only when AXIOM_TOKEN is non-empty", () => + Effect.gen(function* () { + assert.isFalse(yield* isAxiomObservabilityEnabled.pipe(provideEnv({}))); + assert.isFalse(yield* isAxiomObservabilityEnabled.pipe(provideEnv({ AXIOM_TOKEN: " " }))); + assert.isTrue(yield* isAxiomObservabilityEnabled.pipe(provideEnv({ AXIOM_TOKEN: "xaat-123" }))); + }), +); + +it.effect("parses optional self-hosted OTLP trace config and rejects upstream or HTTP URLs", () => + Effect.gen(function* () { + assert.isUndefined(yield* relayOtlpTracesConfig.pipe(provideEnv({}))); + const configured = yield* relayOtlpTracesConfig.pipe( + provideEnv({ + RELAY_OTLP_TRACES_URL: "https://traces.s2code.dev/v1/traces", + RELAY_OTLP_TRACES_TOKEN: "otlp-secret", + }), + ); + assert.isDefined(configured); + if (!configured) return; + assert.equal(configured.url, "https://traces.s2code.dev/v1/traces"); + assert.equal(configured.token ? Redacted.value(configured.token) : undefined, "otlp-secret"); + + for (const badUrl of [ + "http://traces.s2code.dev/v1/traces", + "https://relay.t3.codes/v1/traces", + "https://user:secret@traces.s2code.dev/v1/traces", + "https://traces.example.com/v1/traces", + "https://traces.test/v1/traces", + "not-a-url", + ]) { + const result = yield* relayOtlpTracesConfig.pipe( + provideEnv({ RELAY_OTLP_TRACES_URL: badUrl }), + Effect.result, + ); + assert.isTrue(Result.isFailure(result), badUrl); + } + }), +); + +it.effect("answers 503 mobile_telemetry_not_configured when Axiom is disabled", () => + Effect.gen(function* () { + const telemetry = yield* MobileTelemetry.MobileTelemetry; + const request = HttpServerRequest.fromWeb( + new Request("https://relay.s2code.dev/v1/mobile/observe/proj/logs", { method: "POST" }), + ); + const response = yield* telemetry.handle(request, { projectId: "proj", signal: "logs" }); + assert.equal(response.status, 503); + }).pipe(Effect.provide(MobileTelemetry.layerDisabled)), +); diff --git a/infra/relay/src/observability.ts b/infra/relay/src/observability.ts index d0586ce8bff43..afafa7f10bc85 100644 --- a/infra/relay/src/observability.ts +++ b/infra/relay/src/observability.ts @@ -2,18 +2,23 @@ import * as NodeCrypto from "node:crypto"; import * as Alchemy from "alchemy"; +import * as AlchemyTelemetry from "alchemy/Telemetry"; import * as Axiom from "@alchemy.run/axiom"; import * as Cloudflare from "@alchemy.run/cloudflare"; import * as Output from "alchemy/Output"; import * as Cause from "effect/Cause"; +import * as Config from "effect/Config"; import * as Effect from "effect/Effect"; import * as Exit from "effect/Exit"; +import * as Layer from "effect/Layer"; import * as Option from "effect/Option"; import * as Redacted from "effect/Redacted"; import * as Schema from "effect/Schema"; +import * as SchemaIssue from "effect/SchemaIssue"; import * as Tracer from "effect/Tracer"; import { relayResourceNameForStage } from "./deploymentConfig.ts"; +import { assertForkOtlpTracesUrl, withoutWorkerConfigCapture } from "./forkOwnership.ts"; import { MobileMonitoring } from "./mobileTelemetry/monitoring.ts"; import { RelayMonitoring } from "./monitoring.ts"; @@ -115,6 +120,103 @@ export const RelayObservability = Effect.gen(function* () { } as const; }); +export const isAxiomObservabilityEnabled = Config.String("AXIOM_TOKEN").pipe( + Config.option, + Config.map((value) => Option.getOrElse(value, () => "").trim().length > 0), +); + +const validateOtlpTracesUrl = Effect.fn("RelayObservability.validateOtlpTracesUrl")(function* ( + value: string | undefined, +) { + if (value === undefined) return undefined; + return yield* Effect.try({ + try: () => assertForkOtlpTracesUrl(value), + catch: (error) => + new Config.ConfigError( + new Schema.SchemaError( + new SchemaIssue.InvalidValue({ + message: + error instanceof Error + ? error.message + : "RELAY_OTLP_TRACES_URL must be a fork-owned HTTPS URL.", + }), + ), + ), + }); +}); + +export const relayOtlpTracesConfig = Config.all({ + url: Config.String("RELAY_OTLP_TRACES_URL").pipe( + Config.option, + Config.map((value) => { + const trimmed = Option.getOrElse(value, () => "").trim(); + return trimmed.length > 0 ? trimmed : undefined; + }), + Config.mapEffect(validateOtlpTracesUrl), + ), + token: Config.Redacted("RELAY_OTLP_TRACES_TOKEN").pipe( + Config.option, + Config.map((value) => { + if (Option.isNone(value)) return undefined; + const trimmed = Redacted.value(value.value).trim(); + return trimmed.length > 0 ? Redacted.make(trimmed) : undefined; + }), + ), +}).pipe(Config.map(({ url, token }) => (url === undefined ? undefined : { url, token }))); + +export const OptionalRelayObservability = Effect.gen(function* () { + const phase = yield* Alchemy.ALCHEMY_PHASE.pipe(withoutWorkerConfigCapture); + if (phase === "runtime") { + // At runtime, MobileSourceMapsBucket is bound onto WorkerEnvironment iff Axiom mode was planned. + const workerEnv = yield* Effect.serviceOption(Cloudflare.WorkerEnvironment); + const hasMobileTelemetryBinding = + Option.isSome(workerEnv) && + typeof workerEnv.value === "object" && + workerEnv.value !== null && + "MobileSourceMapsBucket" in workerEnv.value; + return hasMobileTelemetryBinding ? yield* RelayObservability : undefined; + } + if (!(yield* isAxiomObservabilityEnabled.pipe(withoutWorkerConfigCapture))) { + return undefined; + } + return yield* RelayObservability; +}); + +export const RelayTelemetryLayer = Layer.unwrap( + Effect.gen(function* () { + const phase = yield* Alchemy.ALCHEMY_PHASE.pipe(withoutWorkerConfigCapture); + if (phase === "runtime") { + // Bound OTLP destinations are read per event via Alchemy.Telemetry.layer in worker.ts. + return Layer.empty; + } + const observability = yield* OptionalRelayObservability; + if (observability !== undefined) { + return Axiom.Telemetry({ + serviceName: "t3code-relay", + token: observability.workerIngestToken, + traces: observability.traces, + }); + } + const otlp = yield* relayOtlpTracesConfig.pipe(withoutWorkerConfigCapture); + if (otlp !== undefined) { + return AlchemyTelemetry.layerOtlp({ + serviceName: "t3code-relay", + traces: { + url: otlp.url, + ...(otlp.token + ? { + headers: { + Authorization: Redacted.make(`Bearer ${Redacted.value(otlp.token)}`), + }, + } + : {}), + }, + }); + } + return Layer.empty; + }), +); + const mobileSourceMapsBucketName = (stage: string) => relayResourceNameForStage("t3-code-mobile-sourcemaps", stage); diff --git a/infra/relay/src/postgresMigrations.test.ts b/infra/relay/src/postgresMigrations.test.ts new file mode 100644 index 0000000000000..c212825ccde31 --- /dev/null +++ b/infra/relay/src/postgresMigrations.test.ts @@ -0,0 +1,337 @@ +import * as NodeServices from "@effect/platform-node/NodeServices"; +import { PGlite } from "@electric-sql/pglite"; +import { assert, it } from "@effect/vitest"; +import * as ConfigProvider from "effect/ConfigProvider"; +import * as Deferred from "effect/Deferred"; +import * as Effect from "effect/Effect"; +import * as Fiber from "effect/Fiber"; +import * as Path from "effect/Path"; +import * as Redacted from "effect/Redacted"; +import * as Result from "effect/Result"; +import * as Semaphore from "effect/Semaphore"; + +import { + applyRelayPostgresMigrations, + PostgresMigrationError, + readDrizzlePostgresMigrations, + RELAY_MIGRATIONS_TABLE, + resolveMigrationPgClientConfig, + splitPostgresSqlScript, + type PostgresMigrationExecutor, +} from "./postgresMigrations.ts"; + +const makePgliteExecutor = ( + db: PGlite, + options?: { + readonly withLock?: PostgresMigrationExecutor["withLock"]; + readonly onQuery?: (statement: string) => Effect.Effect; + }, +): PostgresMigrationExecutor => { + const query: PostgresMigrationExecutor["query"] = >( + statement: string, + params?: ReadonlyArray, + ) => + (options?.onQuery ? options.onQuery(statement) : Effect.void).pipe( + Effect.andThen( + Effect.tryPromise({ + try: async () => { + const result = await db.query(statement, params ? [...params] : undefined); + return result.rows; + }, + catch: (cause) => + new PostgresMigrationError({ + operation: "query", + detail: "pglite query failed", + cause, + }), + }), + ), + ); + + return { + query, + withTransaction: (effect: Effect.Effect) => + query("BEGIN").pipe( + Effect.andThen(effect), + Effect.tap(() => query("COMMIT")), + Effect.onError(() => query("ROLLBACK").pipe(Effect.ignore)), + ), + ...(options?.withLock ? { withLock: options.withLock } : {}), + }; +}; + +it.effect( + "applies migrations/postgres under pg_advisory_lock idempotently and rejects rewritten history", + () => + Effect.gen(function* () { + const path = yield* Path.Path; + const migrationsDir = yield* path.fromFileUrl( + new URL("../migrations/postgres", import.meta.url), + ); + const records = yield* readDrizzlePostgresMigrations(migrationsDir); + assert.isTrue(records.length > 0); + + const db = yield* Effect.acquireRelease( + Effect.promise(() => PGlite.create()), + (instance) => Effect.promise(() => instance.close()), + ); + const seenSql: string[] = []; + const executor = makePgliteExecutor(db, { + onQuery: (statement) => + Effect.sync(() => { + seenSql.push(statement); + }), + }); + + const first = yield* applyRelayPostgresMigrations({ executor, records }); + assert.equal(first.applied.length, records.length); + assert.equal(first.skipped, 0); + assert.include(seenSql[0] ?? "", "pg_advisory_lock"); + assert.include(seenSql[seenSql.length - 1] ?? "", "pg_advisory_unlock"); + + const recorded = yield* executor.query<{ + readonly id: number; + readonly hash: string; + readonly created_at: string | number; + readonly name: string; + readonly applied_at: unknown; + }>( + `SELECT id, hash, created_at, name, applied_at FROM "${RELAY_MIGRATIONS_TABLE}" ORDER BY id;`, + ); + assert.equal(recorded.length, records.length); + for (let index = 0; index < records.length; index += 1) { + assert.equal(recorded[index]!.name, records[index]!.name); + assert.equal(recorded[index]!.hash, records[index]!.hash); + assert.equal(Number(recorded[index]!.created_at), records[index]!.createdAtMillis); + assert.isDefined(recorded[index]!.applied_at); + } + + const second = yield* applyRelayPostgresMigrations({ executor, records }); + assert.deepEqual(second, { applied: [], skipped: records.length }); + + const rewritten = yield* applyRelayPostgresMigrations({ + executor, + records: [{ ...records[0]!, hash: "0".repeat(64) }, ...records.slice(1)], + }).pipe(Effect.result); + assert.isTrue(Result.isFailure(rewritten)); + if (Result.isFailure(rewritten)) { + assert.equal(rewritten.failure.operation, "history"); + } + }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)), +); + +it.effect("splits multi-statement Postgres scripts without breakpoint comments accurately", () => + Effect.gen(function* () { + const sql = ` + -- Leading comment; should not split + CREATE TABLE items (id integer PRIMARY KEY, label text NOT NULL); + /* Nested /* block ; comment */ still comment */ + INSERT INTO items (id, label) VALUES (1, 'a;b''c'), (2, E'd\\';e'); + DO $$ BEGIN PERFORM 1; PERFORM 2; END $$; + `; + const statements = splitPostgresSqlScript(sql); + assert.equal(statements.length, 3); + + const db = yield* Effect.acquireRelease( + Effect.promise(() => PGlite.create()), + (instance) => Effect.promise(() => instance.close()), + ); + const executor = makePgliteExecutor(db); + yield* applyRelayPostgresMigrations({ + executor, + records: [ + { + name: "20261121000001_no_breakpoints", + hash: "a".repeat(64), + createdAtMillis: 1, + statements, + }, + ], + }); + const rows = yield* executor.query<{ readonly id: number; readonly label: string }>( + "SELECT id, label FROM items ORDER BY id;", + ); + assert.deepEqual(rows, [ + { id: 1, label: "a;b'c" }, + { id: 2, label: "d';e" }, + ]); + }).pipe(Effect.scoped), +); + +it.effect("rolls back a failing migration and records nothing in relay_migrations", () => + Effect.gen(function* () { + const db = yield* Effect.acquireRelease( + Effect.promise(() => PGlite.create()), + (instance) => Effect.promise(() => instance.close()), + ); + const executor = makePgliteExecutor(db); + const failure = yield* applyRelayPostgresMigrations({ + executor, + records: [ + { + name: "20261121000002_partial_fail", + hash: "b".repeat(64), + createdAtMillis: 2, + statements: [ + "CREATE TABLE rolled_back_items (id integer PRIMARY KEY)", + "INSERT INTO nonexistent_table (id) VALUES (1)", + ], + }, + ], + }).pipe(Effect.result); + assert.isTrue(Result.isFailure(failure)); + + const tables = yield* executor.query<{ readonly table_name: string }>( + "SELECT table_name FROM information_schema.tables WHERE table_name = 'rolled_back_items';", + ); + assert.equal(tables.length, 0); + + const recorded = yield* executor.query(`SELECT name FROM "${RELAY_MIGRATIONS_TABLE}";`); + assert.equal(recorded.length, 0); + }).pipe(Effect.scoped), +); + +it.effect("serializes two concurrent migration runs so each migration is recorded once", () => + Effect.gen(function* () { + const db = yield* Effect.acquireRelease( + Effect.promise(() => PGlite.create()), + (instance) => Effect.promise(() => instance.close()), + ); + const sessionLock = yield* Semaphore.make(1); + const firstEntered = yield* Deferred.make(); + const allowFirstToFinish = yield* Deferred.make(); + let lockAcquisitions = 0; + + const makeSessionExecutor = () => + makePgliteExecutor(db, { + withLock: (_lockKey, effect) => + sessionLock.withPermit( + Effect.gen(function* () { + lockAcquisitions += 1; + if (lockAcquisitions === 1) { + yield* Deferred.succeed(firstEntered, undefined); + yield* Deferred.await(allowFirstToFinish); + } + return yield* effect; + }), + ), + }); + + const records = [ + { + name: "20261121000003_concurrent_a", + hash: "c".repeat(64), + createdAtMillis: 3, + statements: ["CREATE TABLE IF NOT EXISTS concurrent_a (id integer PRIMARY KEY)"], + }, + ]; + + const firstFiber = yield* Effect.forkChild( + applyRelayPostgresMigrations({ executor: makeSessionExecutor(), records }), + ); + yield* Deferred.await(firstEntered); + + const secondFiber = yield* Effect.forkChild( + applyRelayPostgresMigrations({ executor: makeSessionExecutor(), records }), + ); + yield* Deferred.succeed(allowFirstToFinish, undefined); + + const [firstResult, secondResult] = yield* Effect.all([ + Fiber.join(firstFiber), + Fiber.join(secondFiber), + ]); + assert.deepEqual(firstResult, { applied: ["20261121000003_concurrent_a"], skipped: 0 }); + assert.deepEqual(secondResult, { applied: [], skipped: 1 }); + + const rows = yield* makePgliteExecutor(db).query<{ readonly name: string }>( + `SELECT name FROM "${RELAY_MIGRATIONS_TABLE}";`, + ); + assert.deepEqual(rows, [{ name: "20261121000003_concurrent_a" }]); + }).pipe(Effect.scoped), +); + +it.effect( + "validates migration hosts and maps RELAY_DB_MIGRATION_SSLMODE modes in resolveMigrationPgClientConfig", + () => + Effect.gen(function* () { + const provideEnv = (env: Readonly>) => + Effect.provide(ConfigProvider.layer(ConfigProvider.fromEnv({ env }))); + + const localUrl = yield* resolveMigrationPgClientConfig().pipe( + provideEnv({ + RELAY_DATABASE_URL: + "postgresql://relay:secret@127.0.0.1:5432/s2coderelay?sslmode=disable", + }), + ); + assert.equal( + localUrl.url ? Redacted.value(localUrl.url) : undefined, + "postgresql://relay:secret@127.0.0.1:5432/s2coderelay?sslmode=disable", + ); + + for (const badEnv of [ + { RELAY_DATABASE_URL: "postgresql://relay:secret@db.t3.codes:5432/s2coderelay" }, + { RELAY_DATABASE_URL: "postgresql://relay:secret@db.example.com:5432/s2coderelay" }, + { + RELAY_DB_HOST: "db.t3.codes", + RELAY_DB_NAME: "s2coderelay", + RELAY_DB_USER: "relay", + RELAY_DB_PASSWORD: "secret", + }, + { + RELAY_DB_HOST: "pg.s2code.dev", + RELAY_DB_NAME: "s2coderelay", + RELAY_DB_USER: "relay", + RELAY_DB_PASSWORD: "secret", + RELAY_DB_MIGRATION_SSLMODE: "bogus", + }, + ]) { + const result = yield* resolveMigrationPgClientConfig().pipe( + provideEnv(badEnv), + Effect.result, + ); + assert.isTrue(Result.isFailure(result), JSON.stringify(badEnv)); + } + + const base = { + RELAY_DB_HOST: "pg.s2code.dev", + RELAY_DB_NAME: "s2coderelay", + RELAY_DB_USER: "relay", + RELAY_DB_PASSWORD: "secret", + }; + const disabled = yield* resolveMigrationPgClientConfig().pipe( + provideEnv({ + ...base, + RELAY_DB_MIGRATION_TARGET_HOST: "127.0.0.1", + RELAY_DB_MIGRATION_TARGET_PORT: "15432", + RELAY_DB_MIGRATION_SSLMODE: "disable", + }), + ); + assert.equal(disabled.ssl, false); + assert.equal(disabled.host, "127.0.0.1"); + assert.equal(disabled.port, 15432); + + const required = yield* resolveMigrationPgClientConfig().pipe( + provideEnv({ ...base, RELAY_DB_MIGRATION_SSLMODE: "require" }), + ); + assert.deepEqual(required.ssl, { rejectUnauthorized: false }); + + const verifyFullTunnel = yield* resolveMigrationPgClientConfig().pipe( + provideEnv({ + ...base, + RELAY_DB_MIGRATION_TARGET_HOST: "127.0.0.1", + RELAY_DB_MIGRATION_TARGET_PORT: "15432", + RELAY_DB_MIGRATION_SSLMODE: "verify-full", + }), + ); + assert.deepEqual(verifyFullTunnel.ssl, { + rejectUnauthorized: true, + servername: "pg.s2code.dev", + }); + + const prefer = yield* resolveMigrationPgClientConfig().pipe( + provideEnv({ ...base, RELAY_DB_MIGRATION_SSLMODE: "prefer" }), + ); + assert.isDefined(prefer.url); + assert.include(Redacted.value(prefer.url!), "sslmode=prefer"); + }), +); diff --git a/infra/relay/src/postgresMigrations.ts b/infra/relay/src/postgresMigrations.ts new file mode 100644 index 0000000000000..eef5ccb914190 --- /dev/null +++ b/infra/relay/src/postgresMigrations.ts @@ -0,0 +1,502 @@ +// @effect-diagnostics nodeBuiltinImport:off - sha256 hashes migration SQL deterministically to match Alchemy's bookkeeping. +import * as NodeCrypto from "node:crypto"; +import type * as PgClient from "@effect/sql-pg/PgClient"; +import * as Config from "effect/Config"; +import * as Effect from "effect/Effect"; +import * as FileSystem from "effect/FileSystem"; +import * as Option from "effect/Option"; +import * as Path from "effect/Path"; +import * as Redacted from "effect/Redacted"; +import * as Schema from "effect/Schema"; + +import { assertForkRelayHost } from "./forkOwnership.ts"; + +export const RELAY_MIGRATIONS_TABLE = "relay_migrations"; + +const DRIZZLE_DIR_PATTERN = /^\d{14}_.+/u; +const LOOPBACK_HOSTS = new Set(["127.0.0.1", "localhost", "::1", "[::1]"]); +const VALID_SSLMODES = new Set([ + "disable", + "allow", + "prefer", + "require", + "verify-ca", + "verify-full", +]); + +export interface PostgresMigrationRecord { + readonly name: string; + readonly hash: string; + readonly createdAtMillis: number | undefined; + readonly statements: ReadonlyArray; +} + +export class PostgresMigrationError extends Schema.TaggedError()( + "PostgresMigrationError", + { + operation: Schema.Literals(["read", "query", "apply", "history", "config"]), + detail: Schema.String, + cause: Schema.optional(Schema.Defect()), + }, +) { + override get message(): string { + return `Relay Postgres migration ${this.operation} failed: ${this.detail}`; + } +} + +export const isPostgresMigrationError = Schema.is(PostgresMigrationError); + +export interface PostgresMigrationExecutor { + readonly query: = Record>( + sql: string, + params?: ReadonlyArray, + ) => Effect.Effect, PostgresMigrationError>; + readonly withTransaction: ( + effect: Effect.Effect, + ) => Effect.Effect; + /** Override lock acquisition in single-connection test harnesses; production uses `pg_advisory_lock`. */ + readonly withLock?: ( + lockKey: string, + effect: Effect.Effect, + ) => Effect.Effect; +} + +const timestampPrefixMillis = (name: string): number | undefined => { + const prefix = name.slice(0, 14); + if (!/^\d{14}$/u.test(prefix)) return undefined; + const year = Number.parseInt(prefix.slice(0, 4), 10); + const month = Number.parseInt(prefix.slice(4, 6), 10) - 1; + const day = Number.parseInt(prefix.slice(6, 8), 10); + const hour = Number.parseInt(prefix.slice(8, 10), 10); + const minute = Number.parseInt(prefix.slice(10, 12), 10); + const second = Number.parseInt(prefix.slice(12, 14), 10); + return Date.UTC(year, month, day, hour, minute, second); +}; + +const isWordChar = (ch: string | undefined) => ch !== undefined && /[A-Za-z0-9_]/u.test(ch); + +/** + * Split a PostgreSQL script on top-level `;` while stripping comments and preserving + * quoted strings (`'...'`, `E'...'`), quoted identifiers (`"..."`), and `$tag$...$tag$` + * dollar-quoted bodies — matching `@alchemy.run/sql`'s `splitSqlScript(sql, "postgres")`. + */ +export function splitPostgresSqlScript(sql: string): ReadonlyArray { + const statements: string[] = []; + let text = ""; + let hasCode = false; + + const push = () => { + if (hasCode) { + const trimmed = text.trim(); + if (trimmed.length > 0) statements.push(trimmed); + } + text = ""; + hasCode = false; + }; + + let index = 0; + while (index < sql.length) { + const ch = sql[index]!; + const next = sql[index + 1]; + + if (ch === "-" && next === "-") { + const end = sql.indexOf("\n", index); + text += " "; + index = end < 0 ? sql.length : end; + continue; + } + + if (ch === "/" && next === "*") { + let depth = 1; + let cursor = index + 2; + while (cursor < sql.length && depth > 0) { + if (sql.startsWith("/*", cursor)) { + depth += 1; + cursor += 2; + } else if (sql.startsWith("*/", cursor)) { + depth -= 1; + cursor += 2; + } else { + cursor += 1; + } + } + text += " "; + index = cursor; + continue; + } + + if (ch === "$" && !isWordChar(sql[index - 1])) { + const tag = /^\$(?:[A-Za-z_][A-Za-z_0-9]*)?\$/u.exec(sql.slice(index))?.[0]; + if (tag) { + const close = sql.indexOf(tag, index + tag.length); + const end = close < 0 ? sql.length : close + tag.length; + text += sql.slice(index, end); + hasCode = true; + index = end; + continue; + } + } + + if (ch === "'" || ch === '"') { + const prefix = + /(?:^|[^A-Za-z0-9_])([A-Za-z]{1,2})$/u + .exec(sql.slice(Math.max(0, index - 3), index))?.[1] + ?.toLowerCase() ?? ""; + const backslashEscapes = ch === "'" && prefix === "e"; + let cursor = index + 1; + while (cursor < sql.length) { + if (backslashEscapes && sql[cursor] === "\\") { + cursor += 2; + continue; + } + if (sql[cursor] === ch) { + cursor += 1; + if (sql[cursor] === ch) { + cursor += 1; + continue; + } + break; + } + cursor += 1; + } + text += sql.slice(index, cursor); + hasCode = true; + index = cursor; + continue; + } + + if (ch === ";") { + push(); + index += 1; + continue; + } + + if (!/\s/u.test(ch)) hasCode = true; + text += ch; + index += 1; + } + + push(); + return statements; +} + +const aliasesOf = (name: string): ReadonlyArray => [ + name, + `${name}/migration.sql`, + name.replace(/\/migration\.sql$/u, ""), +]; + +const quoteIdentifier = (identifier: string): string => `"${identifier.replaceAll('"', '""')}"`; + +function isLoopbackHost(host: string): boolean { + return LOOPBACK_HOSTS.has(host.trim().toLowerCase()); +} + +const validateMigrationHost = Effect.fn("RelayMigrations.validateHost")(function* ( + name: string, + host: string, +) { + const trimmed = host.trim(); + if (!trimmed) { + return yield* new PostgresMigrationError({ + operation: "config", + detail: `${name} must not be empty.`, + }); + } + if (isLoopbackHost(trimmed)) { + return trimmed; + } + return yield* Effect.try({ + try: () => assertForkRelayHost(name, trimmed), + catch: (error) => + new PostgresMigrationError({ + operation: "config", + detail: error instanceof Error ? error.message : `${name} must be a fork-owned host.`, + }), + }); +}); + +const optionalTrimmed = (name: string) => + Config.String(name).pipe( + Config.option, + Config.map((value) => { + const trimmed = Option.getOrElse(value, () => "").trim(); + return trimmed.length > 0 ? trimmed : undefined; + }), + ); + +/** Resolve single-session migration PgClient config without printing credentials. */ +export const resolveMigrationPgClientConfig = Effect.fn("RelayMigrations.resolveConfig")( + function* () { + const explicitUrl = yield* Config.Redacted("RELAY_DATABASE_URL").pipe( + Config.orElse(() => Config.Redacted("DATABASE_URL")), + Config.option, + ); + if (Option.isSome(explicitUrl) && Redacted.value(explicitUrl.value).trim().length > 0) { + const rawUrl = Redacted.value(explicitUrl.value).trim(); + const parsed = yield* Effect.try({ + try: () => new URL(rawUrl), + catch: () => + new PostgresMigrationError({ + operation: "config", + detail: "RELAY_DATABASE_URL must be a valid PostgreSQL connection URL.", + }), + }); + if (parsed.protocol !== "postgres:" && parsed.protocol !== "postgresql:") { + return yield* new PostgresMigrationError({ + operation: "config", + detail: "RELAY_DATABASE_URL must use a postgres:// or postgresql:// scheme.", + }); + } + yield* validateMigrationHost("RELAY_DATABASE_URL", parsed.hostname); + return { + url: Redacted.make(rawUrl), + } satisfies PgClient.PgClientConfig; + } + + const relayDbHost = yield* optionalTrimmed("RELAY_DB_HOST"); + if (relayDbHost !== undefined) { + yield* validateMigrationHost("RELAY_DB_HOST", relayDbHost); + } + const targetHost = (yield* optionalTrimmed("RELAY_DB_MIGRATION_TARGET_HOST")) ?? relayDbHost; + const rawPort = + (yield* optionalTrimmed("RELAY_DB_MIGRATION_TARGET_PORT")) ?? + (yield* optionalTrimmed("RELAY_DB_PORT")); + const database = yield* optionalTrimmed("RELAY_DB_NAME"); + const username = yield* optionalTrimmed("RELAY_DB_USER"); + const password = yield* Config.Redacted("RELAY_DB_PASSWORD").pipe(Config.option); + const sslMode = ( + (yield* optionalTrimmed("RELAY_DB_MIGRATION_SSLMODE")) ?? "require" + ).toLowerCase(); + + if (!targetHost || !database || !username || Option.isNone(password)) { + return yield* new PostgresMigrationError({ + operation: "config", + detail: + "Set RELAY_DATABASE_URL (or RELAY_DB_HOST, RELAY_DB_NAME, RELAY_DB_USER, and RELAY_DB_PASSWORD).", + }); + } + const validatedTargetHost = yield* validateMigrationHost( + "RELAY_DB_MIGRATION_TARGET_HOST", + targetHost, + ); + const port = rawPort ? (/^\d+$/u.test(rawPort) ? Number(rawPort) : Number.NaN) : 5432; + if (!Number.isSafeInteger(port) || port < 1 || port > 65535) { + return yield* new PostgresMigrationError({ + operation: "config", + detail: "Postgres migration port must be a valid TCP port (1-65535).", + }); + } + if (!VALID_SSLMODES.has(sslMode)) { + return yield* new PostgresMigrationError({ + operation: "config", + detail: `Unsupported RELAY_DB_MIGRATION_SSLMODE "${sslMode}". Expected one of disable, allow, prefer, require, verify-ca, verify-full.`, + }); + } + const trimmedPassword = Redacted.value(password.value).trim(); + if (!trimmedPassword) { + return yield* new PostgresMigrationError({ + operation: "config", + detail: "RELAY_DB_PASSWORD must not be empty.", + }); + } + + if (sslMode === "prefer" || sslMode === "allow") { + const encodedUser = encodeURIComponent(username); + const encodedPassword = encodeURIComponent(trimmedPassword); + const encodedDb = encodeURIComponent(database); + return { + url: Redacted.make( + `postgresql://${encodedUser}:${encodedPassword}@${validatedTargetHost}:${port}/${encodedDb}?sslmode=${sslMode}`, + ), + } satisfies PgClient.PgClientConfig; + } + + const tlsServername = + relayDbHost && !isLoopbackHost(relayDbHost) && isLoopbackHost(validatedTargetHost) + ? relayDbHost + : undefined; + const ssl: PgClient.PgClientConfig["ssl"] = + sslMode === "disable" + ? false + : sslMode === "require" + ? { rejectUnauthorized: false } + : sslMode === "verify-ca" + ? { rejectUnauthorized: true, checkServerIdentity: () => undefined } + : tlsServername + ? { rejectUnauthorized: true, servername: tlsServername } + : true; + + return { + host: validatedTargetHost, + port, + database, + username, + password: Redacted.make(trimmedPassword), + ssl, + } satisfies PgClient.PgClientConfig; + }, +); + +/** Read Drizzle v1 `{YYYYMMDDHHMMSS}_{name}/migration.sql` entries in Alchemy's exact format. */ +export const readDrizzlePostgresMigrations = Effect.fn("RelayMigrations.readDrizzleDir")(function* ( + dir: string, +) { + const fs = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const entries = yield* fs.readDirectory(dir).pipe( + Effect.mapError( + (cause) => + new PostgresMigrationError({ + operation: "read", + detail: `could not list migrations directory ${dir}`, + cause, + }), + ), + ); + const names: string[] = []; + for (const entry of entries) { + if (!DRIZZLE_DIR_PATTERN.test(entry)) continue; + const sqlPath = path.join(dir, entry, "migration.sql"); + const exists = yield* fs.exists(sqlPath).pipe( + Effect.mapError( + (cause) => + new PostgresMigrationError({ + operation: "read", + detail: `could not inspect migration ${entry}`, + cause, + }), + ), + ); + if (exists) names.push(entry); + } + names.sort((a, b) => a.localeCompare(b)); + + const records: PostgresMigrationRecord[] = []; + for (const name of names) { + const sql = yield* fs.readFileString(path.join(dir, name, "migration.sql")).pipe( + Effect.mapError( + (cause) => + new PostgresMigrationError({ + operation: "read", + detail: `could not read migration ${name}`, + cause, + }), + ), + ); + records.push({ + name, + hash: NodeCrypto.createHash("sha256").update(sql).digest("hex"), + createdAtMillis: timestampPrefixMillis(name), + statements: splitPostgresSqlScript(sql), + }); + } + return records; +}); + +const withAdvisoryLock = ( + executor: PostgresMigrationExecutor, + lockKey: string, + effect: Effect.Effect, +): Effect.Effect => + executor.withLock + ? executor.withLock(lockKey, effect) + : Effect.acquireUseRelease( + executor.query("SELECT pg_advisory_lock(hashtextextended($1, 0));", [lockKey]), + () => effect, + () => + executor + .query("SELECT pg_advisory_unlock(hashtextextended($1, 0));", [lockKey]) + .pipe(Effect.ignore), + ); + +/** + * Apply pending Drizzle migrations under a Postgres session advisory lock using the + * same `relay_migrations` schema and SHA-256 bookkeeping as `@alchemy.run/planetscale`, + * so concurrent deploys serialize and a database can move cleanly between modes. + */ +export const applyRelayPostgresMigrations = Effect.fn("RelayMigrations.apply")(function* (options: { + readonly executor: PostgresMigrationExecutor; + readonly records: ReadonlyArray; + readonly table?: string; +}) { + const table = options.table ?? RELAY_MIGRATIONS_TABLE; + const quoted = quoteIdentifier(table); + const { executor, records } = options; + + return yield* withAdvisoryLock( + executor, + `t3code_relay_migrations:${table}`, + Effect.gen(function* () { + yield* executor.query(`CREATE TABLE IF NOT EXISTS ${quoted} ( + id SERIAL PRIMARY KEY, + hash text NOT NULL, + created_at bigint, + name text, + applied_at timestamp with time zone DEFAULT now() +);`); + + const rows = yield* executor.query<{ readonly name: unknown; readonly hash: unknown }>( + `SELECT name, hash FROM ${quoted};`, + ); + const applied = new Map(); + for (const row of rows) { + if (row.name === null || row.name === undefined) continue; + applied.set( + String(row.name), + row.hash === null || row.hash === undefined ? undefined : String(row.hash), + ); + } + + const localNames = new Set(records.flatMap((record) => aliasesOf(record.name))); + const changed: string[] = []; + const removed: string[] = []; + for (const name of applied.keys()) { + if (!localNames.has(name)) removed.push(name); + } + for (const record of records) { + const existing = aliasesOf(record.name).find((alias) => applied.has(alias)); + if (existing !== undefined) { + const recordedHash = applied.get(existing); + if (recordedHash && recordedHash !== record.hash) { + changed.push(record.name); + } + } + } + if (changed.length > 0 || removed.length > 0) { + changed.sort(); + removed.sort(); + const parts = [ + ...(changed.length > 0 ? [`changed: ${changed.join(", ")}`] : []), + ...(removed.length > 0 ? [`removed: ${removed.join(", ")}`] : []), + ]; + return yield* new PostgresMigrationError({ + operation: "history", + detail: `applied migration history in "${table}" does not match local files (${parts.join("; ")})`, + }); + } + + const newlyApplied: string[] = []; + for (const record of records) { + if (aliasesOf(record.name).some((alias) => applied.has(alias))) continue; + yield* executor.withTransaction( + Effect.gen(function* () { + for (const statement of record.statements) { + yield* executor.query(statement); + } + yield* executor.query( + `INSERT INTO ${quoted} (hash, created_at, name) VALUES ($1, $2, $3);`, + [record.hash, record.createdAtMillis ?? null, record.name], + ); + }), + ); + newlyApplied.push(record.name); + } + + return { + applied: newlyApplied, + skipped: records.length - newlyApplied.length, + }; + }), + ); +}); diff --git a/infra/relay/src/worker.ts b/infra/relay/src/worker.ts index 0e42e7e110311..592d51acd7ae9 100644 --- a/infra/relay/src/worker.ts +++ b/infra/relay/src/worker.ts @@ -1,5 +1,4 @@ import * as Alchemy from "alchemy"; -import * as Axiom from "@alchemy.run/axiom"; import * as Cloudflare from "@alchemy.run/cloudflare"; import * as Drizzle from "@alchemy.run/drizzle/Postgres"; import * as Config from "effect/Config"; @@ -31,7 +30,7 @@ import { import * as CloudMintSigner from "./auth/CloudMintSigner.ts"; import * as RelayHttpApi from "./http/Api.ts"; import { ManagedEndpointZone, RelayApiZone, RelayDeploymentConfig } from "./zone.ts"; -import { RelayObservability } from "./observability.ts"; +import { OptionalRelayObservability } from "./observability.ts"; import * as Observability from "./observability.ts"; import * as DeliveryAttempts from "./agentActivity/DeliveryAttempts.ts"; import * as AgentActivityRows from "./agentActivity/AgentActivityRows.ts"; @@ -106,7 +105,7 @@ const layerRelayApi = Layer.mergeAll( RelayHttpApi.layerServerApi, ); -const CloudMintKeyPair = Alchemy.KeyPair("CloudMintKeyPair"); +export const CloudMintKeyPair = Alchemy.KeyPair("CloudMintKeyPair"); const ApnsDeliveryJobSigningSecret = Alchemy.makeRandom("ApnsDeliveryJobSigningSecret", { bytes: 32, }); @@ -139,7 +138,7 @@ export const layer = Api.make( const relayApiZone = yield* RelayApiZone; const managedEndpointZone = yield* ManagedEndpointZone; const randomApnsDeliveryJobSigningSecret = yield* ApnsDeliveryJobSigningSecret; - const observability = yield* RelayObservability; + const observability = yield* OptionalRelayObservability; const apnsEnabled = yield* Config.Boolean("APNS_ENABLED").pipe(Config.withDefault(true)); const apnsCredentials = apnsEnabled @@ -209,21 +208,6 @@ export const layer = Api.make( }, }); const hookInboxes = yield* HookInboxObject.HookInboxObject; - const mobileTelemetryRateLimit = yield* Cloudflare.RateLimit("MOBILE_TELEMETRY_RATE_LIMIT", { - namespaceId: 1003, - simple: { - limit: MobileTelemetry.MOBILE_TELEMETRY_RATE_LIMIT.limit, - period: MobileTelemetry.MOBILE_TELEMETRY_RATE_LIMIT.periodSeconds, - }, - }); - const mobileSourceMaps = yield* Cloudflare.R2.ReadBucket(observability.mobileSourceMaps); - const mobileTelemetryToken = yield* observability.mobileTelemetryIngestToken.token; - const mobileTracesUrl = yield* observability.traces.otelTracesEndpoint; - const mobileTracesDataset = yield* observability.traces.name; - const mobileLogsUrl = yield* observability.mobileLogs.otelLogsEndpoint; - const mobileLogsDataset = yield* observability.mobileLogs.name; - const mobileMetricsUrl = yield* observability.mobileMetrics.otelMetricsEndpoint; - const mobileMetricsDataset = yield* observability.mobileMetrics.name; const alchemyRuntimeContext: Alchemy.BaseRuntimeContext = yield* Cloudflare.Worker; @@ -373,56 +357,74 @@ export const layer = Api.make( allowEndpoint: allowWith(hookEndpointRateLimit), }); - // `fetch` rebuilds its layers per request; the source map cache must outlive one. - const mobileSourceMapLookups = yield* MobileTelemetry.SourceMaps.pipe( - Effect.provide(MobileTelemetry.layerSourceMaps), - Effect.provideService(MobileTelemetry.SourceMapObjects, { - get: (key) => - mobileSourceMaps.get(key).pipe( - Effect.map((object) => - object === null - ? undefined - : { - size: object.size, - text: object.text().pipe( - Effect.provideService(Alchemy.RuntimeContext, alchemyRuntimeContext), - Effect.mapError( - (cause) => new MobileTelemetry.SourceMapObjectError({ key, cause }), + const layerMobileTelemetry = yield* Effect.gen(function* () { + if (observability === undefined) return MobileTelemetry.layerDisabled; + const mobileTelemetryRateLimit = yield* Cloudflare.RateLimit("MOBILE_TELEMETRY_RATE_LIMIT", { + namespaceId: 1003, + simple: { + limit: MobileTelemetry.MOBILE_TELEMETRY_RATE_LIMIT.limit, + period: MobileTelemetry.MOBILE_TELEMETRY_RATE_LIMIT.periodSeconds, + }, + }); + const mobileSourceMaps = yield* Cloudflare.R2.ReadBucket(observability.mobileSourceMaps); + const mobileTelemetryToken = yield* observability.mobileTelemetryIngestToken.token; + const mobileTracesUrl = yield* observability.traces.otelTracesEndpoint; + const mobileTracesDataset = yield* observability.traces.name; + const mobileLogsUrl = yield* observability.mobileLogs.otelLogsEndpoint; + const mobileLogsDataset = yield* observability.mobileLogs.name; + const mobileMetricsUrl = yield* observability.mobileMetrics.otelMetricsEndpoint; + const mobileMetricsDataset = yield* observability.mobileMetrics.name; + // `fetch` rebuilds its layers per request; the source map cache must outlive one. + const mobileSourceMapLookups = yield* MobileTelemetry.SourceMaps.pipe( + Effect.provide(MobileTelemetry.layerSourceMaps), + Effect.provideService(MobileTelemetry.SourceMapObjects, { + get: (key) => + mobileSourceMaps.get(key).pipe( + Effect.map((object) => + object === null + ? undefined + : { + size: object.size, + text: object.text().pipe( + Effect.provideService(Alchemy.RuntimeContext, alchemyRuntimeContext), + Effect.mapError( + (cause) => new MobileTelemetry.SourceMapObjectError({ key, cause }), + ), ), - ), + }, + ), + Effect.provideService(Alchemy.RuntimeContext, alchemyRuntimeContext), + Effect.mapError((cause) => new MobileTelemetry.SourceMapObjectError({ key, cause })), + ), + }), + ); + return MobileTelemetry.layer.pipe( + Layer.provide( + Layer.mergeAll( + Layer.effect( + MobileTelemetry.MobileTelemetrySettings, + Effect.gen(function* () { + return MobileTelemetry.MobileTelemetrySettings.of({ + axiomToken: yield* mobileTelemetryToken, + destinations: { + traces: { url: yield* mobileTracesUrl, dataset: yield* mobileTracesDataset }, + logs: { url: yield* mobileLogsUrl, dataset: yield* mobileLogsDataset }, + metrics: { url: yield* mobileMetricsUrl, dataset: yield* mobileMetricsDataset }, }, + }); + }), ), - Effect.provideService(Alchemy.RuntimeContext, alchemyRuntimeContext), - Effect.mapError((cause) => new MobileTelemetry.SourceMapObjectError({ key, cause })), - ), - }), - ); - const layerMobileTelemetry = MobileTelemetry.layer.pipe( - Layer.provide( - Layer.mergeAll( - Layer.effect( - MobileTelemetry.MobileTelemetrySettings, - Effect.gen(function* () { - return MobileTelemetry.MobileTelemetrySettings.of({ - axiomToken: yield* mobileTelemetryToken, - destinations: { - traces: { url: yield* mobileTracesUrl, dataset: yield* mobileTracesDataset }, - logs: { url: yield* mobileLogsUrl, dataset: yield* mobileLogsDataset }, - metrics: { url: yield* mobileMetricsUrl, dataset: yield* mobileMetricsDataset }, - }, - }); + Layer.succeed(MobileTelemetry.MobileTelemetryRateLimiter, { + allow: allowWith( + mobileTelemetryRateLimit, + "relay.mobile_telemetry.rate_limiter_failed_open", + ), }), + Layer.succeed(MobileTelemetry.SourceMaps, mobileSourceMapLookups), ), - Layer.succeed(MobileTelemetry.MobileTelemetryRateLimiter, { - allow: allowWith( - mobileTelemetryRateLimit, - "relay.mobile_telemetry.rate_limiter_failed_open", - ), - }), - Layer.succeed(MobileTelemetry.SourceMaps, mobileSourceMapLookups), ), - ), - ); + ); + }); const layerApp = Layer.mergeAll( layerRelayApi, @@ -564,17 +566,7 @@ export const layer = Api.make( Layer.provideMerge( Alchemy.Telemetry.layer(Layer.succeed(HttpMiddleware.TracerDisabledWhen)(() => true)), ), - Layer.provideMerge( - Layer.unwrap( - Effect.map(RelayObservability, (observability) => - Axiom.Telemetry({ - serviceName: "t3code-relay", - token: observability.workerIngestToken, - traces: observability.traces, - }), - ), - ), - ), + Layer.provideMerge(Observability.RelayTelemetryLayer), ), ), ), diff --git a/infra/relay/src/workerPlan.test.ts b/infra/relay/src/workerPlan.test.ts new file mode 100644 index 0000000000000..7b951e9c11ced --- /dev/null +++ b/infra/relay/src/workerPlan.test.ts @@ -0,0 +1,216 @@ +// @effect-diagnostics anyUnknownInErrorContext:off layerMergeAllWithDependencies:off -- Alchemy provider helpers expose framework-owned any requirements. +import * as Axiom from "@alchemy.run/axiom"; +import * as Cloudflare from "@alchemy.run/cloudflare"; +import * as Drizzle from "@alchemy.run/drizzle"; +import * as Planetscale from "@alchemy.run/planetscale"; +import * as Test from "@alchemy.run/test/Vitest"; +import * as Alchemy from "alchemy"; +import { KeyPairProvider } from "alchemy/KeyPair"; +import { RandomProvider } from "alchemy/Random"; +import { capturedEnvKeys } from "alchemy/RuntimeContext"; +import { StackContext } from "alchemy/StackContext"; +import { Stage } from "alchemy/Stage"; +import { inMemoryState } from "alchemy/State"; +import { assert, describe } from "@effect/vitest"; +import * as ConfigProvider from "effect/ConfigProvider"; +import * as Effect from "effect/Effect"; +import * as FetchHttpClient from "effect/http/FetchHttpClient"; +import * as Layer from "effect/Layer"; +import * as Redacted from "effect/Redacted"; + +import * as RelayDb from "./db.ts"; +import { OptionalRelayObservability } from "./observability.ts"; +import * as RelayWorker from "./worker.ts"; + +const VALID_CLERK_KEY = `pk_live_${btoa("clerk.s2code.dev$")}`; + +const basePlanEnv: Record = { + ALCHEMY_PHASE: "plan", + RELAY_API_ZONE_NAME: "s2code.dev", + RELAY_TUNNEL_ZONE_NAME: "s2code.dev", + CLERK_PUBLISHABLE_KEY: VALID_CLERK_KEY, + CLERK_SECRET_KEY: "sk_live_test_secret", + CLERK_JWT_AUDIENCE: "t3-code-relay", + APNS_ENABLED: "false", +}; + +const externalDbEnv: Record = { + ...basePlanEnv, + RELAY_DB_HOST: "pg.s2code.dev", + RELAY_DB_NAME: "s2coderelay", + RELAY_DB_USER: "relay", + RELAY_DB_PASSWORD: "super-secret-db-password", + RELAY_DB_ACCESS_CLIENT_ID: "cf-access-client-id", + RELAY_DB_ACCESS_CLIENT_SECRET: "cf-access-client-secret", + RELAY_DB_CONNECTION_LIMIT: "15", +}; + +const DEPLOY_ONLY_FORBIDDEN_KEYS = [ + "RELAY_DB_HOST", + "RELAY_DB_PORT", + "RELAY_DB_NAME", + "RELAY_DB_USER", + "RELAY_DB_PASSWORD", + "RELAY_DB_ACCESS_CLIENT_ID", + "RELAY_DB_ACCESS_CLIENT_SECRET", + "RELAY_DB_CONNECTION_LIMIT", + "RELAY_DB_CLUSTER_SIZE", + "RELAY_DB_REPLICAS", + "AXIOM_TOKEN", + "RELAY_OTLP_TRACES_URL", + "RELAY_OTLP_TRACES_TOKEN", +] as const; + +const { test } = Test.make({ + providers: Layer.mergeAll( + Axiom.providers(), + Cloudflare.providers(), + Drizzle.providers(), + Planetscale.providers(), + KeyPairProvider(), + RandomProvider(), + FetchHttpClient.layer, + ), + state: inMemoryState(), +}); + +const stubCloudflareEnv = Layer.succeed( + Cloudflare.CloudflareEnvironment, + Effect.succeed({ + type: "apiToken" as const, + apiToken: Redacted.make("test-token"), + accountId: "771240435fb4f1407f2b4669085dc79d", + source: { type: "env" as const }, + }), +); + +const planWorker = (env: Readonly>) => { + const stack: Omit = { + name: "T3CodeRelay", + stage: "prod", + resources: {}, + bindings: {}, + actions: {}, + }; + return Effect.gen(function* () { + const worker = yield* RelayWorker.Api; + const propsEnvKeys = Object.keys(worker.Props?.env ?? {}); + const envKeys = [...new Set([...capturedEnvKeys(worker), ...propsEnvKeys])]; + return { + envKeys, + resourceIds: Object.keys(stack.resources), + resourceTypes: Object.values(stack.resources).map((resource) => resource.Type), + }; + }).pipe( + Effect.provide( + RelayWorker.layer.pipe( + Layer.provide( + Layer.mergeAll( + stubCloudflareEnv, + Layer.succeed(Alchemy.Stack, stack), + Layer.succeed(StackContext, stack), + Layer.succeed(Stage, "prod"), + ConfigProvider.layer(ConfigProvider.fromEnv({ env })), + ), + ), + ), + ), + Effect.scoped, + ); +}; + +describe("RelayWorker plan and runtime isolation", () => { + test.provider( + "plan phase with external Postgres and no Axiom captures zero deploy-only credentials and registers no Axiom/R2/PlanetScale resources", + () => + Effect.gen(function* () { + const { envKeys, resourceIds, resourceTypes } = yield* planWorker(externalDbEnv); + for (const forbidden of DEPLOY_ONLY_FORBIDDEN_KEYS) { + assert.notInclude(envKeys, forbidden); + } + assert.notInclude(envKeys, "ALCHEMY_OTEL_EXPORTERS"); + assert.include(resourceIds, "RelayHyperdrive"); + assert.notInclude(resourceIds, "MobileSourceMapsBucket"); + assert.isFalse(resourceTypes.some((type) => type.startsWith("Axiom."))); + assert.isFalse(resourceTypes.some((type) => type.startsWith("Planetscale."))); + assert.isFalse(resourceTypes.includes("Cloudflare.R2.Bucket")); + }), + ); + + test.provider( + "plan phase with optional self-hosted OTLP binds ALCHEMY_OTEL_EXPORTERS without leaking raw RELAY_OTLP_TRACES_* keys", + () => + Effect.gen(function* () { + const { envKeys, resourceTypes } = yield* planWorker({ + ...externalDbEnv, + RELAY_OTLP_TRACES_URL: "https://traces.s2code.dev/v1/traces", + RELAY_OTLP_TRACES_TOKEN: "otlp-bearer-secret", + }); + for (const forbidden of DEPLOY_ONLY_FORBIDDEN_KEYS) { + assert.notInclude(envKeys, forbidden); + } + assert.include(envKeys, "ALCHEMY_OTEL_EXPORTERS"); + assert.include(envKeys, "OTEL_SERVICE_NAME"); + assert.isFalse(resourceTypes.some((type) => type.startsWith("Axiom."))); + assert.isFalse(resourceTypes.includes("Cloudflare.R2.Bucket")); + }), + ); + + test.provider( + "plan phase with AXIOM_TOKEN matches upstream scoped ingest bindings and never leaks AXIOM_TOKEN or RELAY_DB_*", + () => + Effect.gen(function* () { + const { envKeys, resourceIds, resourceTypes } = yield* planWorker({ + ...basePlanEnv, + PLANETSCALE_ORGANIZATION: "shuv-org", + RELAY_DB_CLUSTER_SIZE: "PS_5", + RELAY_DB_REPLICAS: "0", + AXIOM_TOKEN: "xaat-org-level-deploy-token", + }); + for (const forbidden of DEPLOY_ONLY_FORBIDDEN_KEYS) { + assert.notInclude(envKeys, forbidden); + } + assert.include(envKeys, "ALCHEMY_OTEL_EXPORTERS"); + assert.include(envKeys, "MobileTelemetryAxiomIngestToken_token"); + assert.include(resourceIds, "RelayTracesDataset"); + assert.include(resourceIds, "MobileSourceMapsBucket"); + assert.include(resourceIds, "RelayPostgresDatabase"); + assert.isTrue(resourceTypes.includes("Cloudflare.R2.Bucket")); + }), + ); + + test.provider( + "runtime phase resolves Hyperdrive by LogicalId and detects mobile telemetry from WorkerEnvironment without deploy config", + () => + Effect.gen(function* () { + const stack: Omit = { + name: "T3CodeRelay", + stage: "prod", + resources: {}, + bindings: {}, + actions: {}, + }; + const baseRuntime = Layer.mergeAll( + stubCloudflareEnv, + Layer.succeed(Alchemy.Stack, stack), + Layer.succeed(StackContext, stack), + Layer.succeed(Stage, "prod"), + ConfigProvider.layer(ConfigProvider.fromEnv({ env: { ALCHEMY_PHASE: "runtime" } })), + ); + const hyperdrive = yield* RelayDb.RelayHyperdrive.pipe(Effect.provide(baseRuntime)); + assert.equal(hyperdrive.LogicalId, "RelayHyperdrive"); + + const disabledObs = yield* OptionalRelayObservability.pipe( + Effect.provide(baseRuntime), + Effect.provideService(Cloudflare.WorkerEnvironment, {}), + ); + assert.isUndefined(disabledObs); + + const enabledObs = yield* OptionalRelayObservability.pipe( + Effect.provide(baseRuntime), + Effect.provideService(Cloudflare.WorkerEnvironment, { MobileSourceMapsBucket: {} }), + ); + assert.isDefined(enabledObs); + }), + ); +}); diff --git a/packages/shared/src/hostedConfig.ts b/packages/shared/src/hostedConfig.ts index 1f9908a06ff01..b21ae72044a80 100644 --- a/packages/shared/src/hostedConfig.ts +++ b/packages/shared/src/hostedConfig.ts @@ -8,7 +8,7 @@ export const UPSTREAM_HOSTED_DOMAINS = [ "t3tools.github.io", ] as const; export const HOSTED_UNAVAILABLE_MESSAGE = - "Hosted Connect is unavailable: this fork has no configured cloud service. Use direct or Tailscale pairing instead. Upstream accounts are separate."; + "Hosted Connect is not configured in this build. Use direct or Tailscale pairing instead. Upstream accounts are separate."; function isUpstreamHostedDomain(hostname: string) { const host = hostname.toLowerCase().replace(/\.+$/, ""); diff --git a/packages/shared/src/productIdentity.test.ts b/packages/shared/src/productIdentity.test.ts index 036ed468d2eda..7bb6466629e9b 100644 --- a/packages/shared/src/productIdentity.test.ts +++ b/packages/shared/src/productIdentity.test.ts @@ -138,6 +138,7 @@ describe("fork public configuration", () => { S2CODE_HOME: " /new-home ", SHUV2CODE_HOME: "/old-home", T3CODE_HOME: "/upstream", + S2CODE_CLOUD_MINT_PUBLIC_KEY: "-----BEGIN PUBLIC KEY-----\\nKEY\\n-----END PUBLIC KEY-----", S2CODE_PORT: "5773", SHUV2CODE_PORT: "4773", s2code_otlp_headers: "canonical-observability", @@ -148,6 +149,10 @@ describe("fork public configuration", () => { S2CODE_HOME: "/new-home", SHUV2CODE_HOME: "/new-home", T3CODE_HOME: "/new-home", + S2CODE_CLOUD_MINT_PUBLIC_KEY: "-----BEGIN PUBLIC KEY-----\\nKEY\\n-----END PUBLIC KEY-----", + SHUV2CODE_CLOUD_MINT_PUBLIC_KEY: + "-----BEGIN PUBLIC KEY-----\\nKEY\\n-----END PUBLIC KEY-----", + T3CODE_CLOUD_MINT_PUBLIC_KEY: "-----BEGIN PUBLIC KEY-----\\nKEY\\n-----END PUBLIC KEY-----", S2CODE_PORT: "5773", SHUV2CODE_PORT: "5773", T3CODE_PORT: "5773", diff --git a/packages/shared/src/productIdentity.ts b/packages/shared/src/productIdentity.ts index 08fd0b241c057..d2e76e131a020 100644 --- a/packages/shared/src/productIdentity.ts +++ b/packages/shared/src/productIdentity.ts @@ -43,6 +43,7 @@ const PUBLIC_CONFIG_KEYS = [ "DISABLE_AUTO_UPDATE", "HOSTED_APP_URL", "RELAY_URL", + "CLOUD_MINT_PUBLIC_KEY", "MARKETING_URL", "SERVER_BROWSER_SANDBOX", "PORT_DISCOVERY", diff --git a/scripts/build-desktop-artifact.ts b/scripts/build-desktop-artifact.ts index 3b5f5158e8c1b..389812b5a6916 100644 --- a/scripts/build-desktop-artifact.ts +++ b/scripts/build-desktop-artifact.ts @@ -1284,6 +1284,12 @@ function normalizePasskeyRpDomain(value: string): string { return parsed.hostname; } +function isMacPasskeySigningConfigured(env: Readonly>): boolean { + return Boolean( + env.T3CODE_MACOS_PROVISIONING_PROFILE?.trim() || env.T3CODE_CLERK_PASSKEY_RP_DOMAINS?.trim(), + ); +} + export function resolveMacPasskeySigningConfiguration( env: Readonly>, appId = DESKTOP_APP_ID, @@ -4265,12 +4271,8 @@ const buildDesktopArtifactUnlocked = Effect.fn("buildDesktopArtifactUnlocked")(f yield* fs.copy(stageResourcesDir, stageProdResourcesDir); const signingEnvironment = loadRepoEnv({ repoRoot }); - // Local/direct signed builds do not require a hosted Clerk account or its - // associated-domain provisioning profile. Explicit hosted passkeys retain validation. - const hostedPasskeysConfigured = Boolean( - signingEnvironment.T3CODE_CLERK_PUBLISHABLE_KEY?.trim() || - signingEnvironment.T3CODE_CLERK_PASSKEY_RP_DOMAINS?.trim(), - ); + // Signed macOS builds opt into Associated Domains passkeys only with a profile or RP domains. + const hostedPasskeysConfigured = isMacPasskeySigningConfigured(signingEnvironment); const configuredMacPasskeySigning = options.platform === "mac" && options.signed && hostedPasskeysConfigured ? yield* Effect.try({ diff --git a/scripts/fork-release-workflow.test.ts b/scripts/fork-release-workflow.test.ts index c182fbc083365..05fd7ddf2d86e 100644 --- a/scripts/fork-release-workflow.test.ts +++ b/scripts/fork-release-workflow.test.ts @@ -377,10 +377,62 @@ echo "$ref" >> "${refs}"`, "CLERK_CLI_OAUTH_CLIENT_ID", "CLERK_PASSKEY_RP_DOMAINS", "RELAY_URL", + "CLOUD_MINT_PUBLIC_KEY", + "HOSTED_APP_URL", ]) expect(desktop).toContain(`T3CODE_${name}:`); expect(desktop).not.toContain("S2CODE_CLERK_"); }); + + it("wires public Connect variables to build_bundle and every desktop caller without entering production", () => { + expect(workflow).not.toMatch(/environment:\s*(?:\n\s*name:\s*)?production/); + expect(workflow).not.toContain("CLERK_SECRET_KEY"); + const preflightSection = workflow.split(" preflight:\n")[1]!.split("\n build_bundle:")[0]!; + const buildBundleSection = workflow.split(" build_bundle:\n")[1]!.split("\n desktop:")[0]!; + for (const name of [ + "T3CODE_RELAY_URL", + "T3CODE_CLOUD_MINT_PUBLIC_KEY", + "T3CODE_CLERK_PUBLISHABLE_KEY", + "T3CODE_CLERK_JWT_TEMPLATE", + "T3CODE_CLERK_CLI_OAUTH_CLIENT_ID", + "T3CODE_HOSTED_APP_URL", + ]) { + expect(preflightSection).toContain(`${name}: \${{ vars.${name} }}`); + expect(buildBundleSection).toContain(`${name}:`); + } + const callerBlocks = [ + workflow.split(" desktop:\n")[1]!.split("\n desktop_win_x64:")[0]!, + workflow.split(" desktop_win_x64:\n")[1]!.split("\n desktop_mac_arm64:")[0]!, + workflow.split(" desktop_mac_arm64:\n")[1]!.split("\n mac_arm64_queue_watch:")[0]!, + workflow.split(" desktop_mac_arm64_queue_fallback:\n")[1]!.split("\n mac_arm64_ready:")[0]!, + ]; + for (const caller of callerBlocks) { + for (const input of [ + "relay_url", + "cloud_mint_public_key", + "clerk_publishable_key", + "clerk_jwt_template", + "clerk_cli_oauth_client_id", + "hosted_app_url", + ]) { + expect(caller).toContain(`${input}: \${{ needs.preflight.outputs.${input} }}`); + } + } + const encryptedWindows = NodeFS.readFileSync( + new URL("../.github/workflows/encrypted-windows-build.yml", import.meta.url), + "utf8", + ); + for (const name of [ + "T3CODE_RELAY_URL", + "T3CODE_CLOUD_MINT_PUBLIC_KEY", + "T3CODE_CLERK_PUBLISHABLE_KEY", + "T3CODE_CLERK_JWT_TEMPLATE", + "T3CODE_CLERK_CLI_OAUTH_CLIENT_ID", + "T3CODE_HOSTED_APP_URL", + ]) { + expect(encryptedWindows).toContain(`${name}: \${{ vars.${name} }}`); + } + }); }); const hasPwsh = diff --git a/scripts/fork-release.ts b/scripts/fork-release.ts index 83936ac47f640..9fd2d5f4be79e 100644 --- a/scripts/fork-release.ts +++ b/scripts/fork-release.ts @@ -25,6 +25,7 @@ import { windowsSigningCredentials, RELEASE_CREDENTIAL_VARIABLES, releaseSigningPolicy, + resolveForkConnectReleaseConfig, forkReleasePlan, forkReleaseBody, forkReleaseTitle, @@ -910,6 +911,7 @@ async function main() { process.env.ALLOW_UNSIGNED === "true", process.env, ); + const connect = resolveForkConnectReleaseConfig(process.env); const npm = forkNpmPublishingReady(process.env); if (!npm) console.log( @@ -956,7 +958,21 @@ async function main() { const team = process.env.APPLE_TEAM_ID?.trim() ?? ""; FS.appendFileSync( process.env.GITHUB_OUTPUT!, - `ref=${sha}\nversion=${version}\nrelease_channel=${channel}\nmac_signed=${signing.mac}\nwin_signed=${signing.win}\napple_team_id=${/^[A-Za-z0-9]+$/.test(team) ? team : ""}\nmatrix=${JSON.stringify({ include: FORK_DESKTOP_MATRIX })}\n`, + [ + `ref=${sha}`, + `version=${version}`, + `release_channel=${channel}`, + `mac_signed=${signing.mac}`, + `win_signed=${signing.win}`, + `apple_team_id=${/^[A-Za-z0-9]+$/.test(team) ? team : ""}`, + `relay_url=${connect.relayUrl}`, + `cloud_mint_public_key=${connect.cloudMintPublicKey}`, + `clerk_publishable_key=${connect.clerkPublishableKey}`, + `clerk_jwt_template=${connect.clerkJwtTemplate}`, + `clerk_cli_oauth_client_id=${connect.clerkCliOAuthClientId}`, + `hosted_app_url=${connect.hostedAppUrl}`, + `matrix=${JSON.stringify({ include: FORK_DESKTOP_MATRIX })}`, + ].join("\n") + "\n", ); } else if (mode === "signing") { // Preflight resolved signing once, inside the release environment. A build diff --git a/scripts/lib/fork-release.test.ts b/scripts/lib/fork-release.test.ts index 47fda4c7456c9..d3d59bb5a8a8b 100644 --- a/scripts/lib/fork-release.test.ts +++ b/scripts/lib/fork-release.test.ts @@ -1,5 +1,6 @@ // @effect-diagnostics nodeBuiltinImport:off -- Reads checked-in release recipes as contract fixtures. import { describe, expect, it } from "@effect/vitest"; +import * as NodeCrypto from "node:crypto"; import * as NodeFS from "node:fs"; import * as NodePath from "node:path"; import { CLI_ARCHIVE_PLATFORM_KEYS } from "@t3tools/shared/cliRelease"; @@ -9,6 +10,7 @@ import { MAC_RELEASE_CREDENTIALS, WIN_RELEASE_CREDENTIALS, releaseSigningPolicy, + resolveForkConnectReleaseConfig, forkReleasePlan, forkReleaseBody, forkReleaseTitle, @@ -370,4 +372,68 @@ describe("fork release contract", () => { expect(order).not.toContain("promoteNpmTags"); } }); + it("validates public Connect release variables all-or-nothing, verifies Ed25519 SPKI mint key, and rejects upstream targets", () => { + expect(resolveForkConnectReleaseConfig({})).toEqual({ + enabled: false, + relayUrl: "", + cloudMintPublicKey: "", + clerkPublishableKey: "", + clerkJwtTemplate: "", + clerkCliOAuthClientId: "", + hostedAppUrl: "", + }); + const ed25519 = NodeCrypto.generateKeyPairSync("ed25519", { + publicKeyEncoding: { type: "spki", format: "pem" }, + privateKeyEncoding: { type: "pkcs8", format: "pem" }, + }); + const rsa = NodeCrypto.generateKeyPairSync("rsa", { + modulusLength: 2048, + publicKeyEncoding: { type: "spki", format: "pem" }, + privateKeyEncoding: { type: "pkcs8", format: "pem" }, + }); + const escapedMintKey = ed25519.publicKey.trim().replaceAll("\n", "\\n"); + const complete = { + T3CODE_RELAY_URL: "https://relay.s2code.dev", + T3CODE_CLOUD_MINT_PUBLIC_KEY: ed25519.publicKey, + T3CODE_CLERK_PUBLISHABLE_KEY: `pk_live_${btoa("clerk.s2code.dev$")}`, + T3CODE_CLERK_JWT_TEMPLATE: "t3-relay", + T3CODE_CLERK_CLI_OAUTH_CLIENT_ID: "oauth_client_123", + }; + expect(resolveForkConnectReleaseConfig(complete)).toEqual({ + enabled: true, + relayUrl: "https://relay.s2code.dev", + cloudMintPublicKey: escapedMintKey, + clerkPublishableKey: complete.T3CODE_CLERK_PUBLISHABLE_KEY, + clerkJwtTemplate: "t3-relay", + clerkCliOAuthClientId: "oauth_client_123", + hostedAppUrl: "", + }); + expect( + resolveForkConnectReleaseConfig({ + ...complete, + T3CODE_CLOUD_MINT_PUBLIC_KEY: escapedMintKey, + T3CODE_HOSTED_APP_URL: "https://app.s2code.dev", + }).hostedAppUrl, + ).toBe("https://app.s2code.dev"); + expect(() => + resolveForkConnectReleaseConfig({ T3CODE_RELAY_URL: complete.T3CODE_RELAY_URL }), + ).toThrow("T3CODE_CLOUD_MINT_PUBLIC_KEY"); + expect(() => + resolveForkConnectReleaseConfig({ T3CODE_HOSTED_APP_URL: "https://app.s2code.dev" }), + ).toThrow("Incomplete Connect public configuration"); + expect(() => + resolveForkConnectReleaseConfig({ ...complete, T3CODE_RELAY_URL: "https://relay.t3.codes" }), + ).toThrow("T3CODE_RELAY_URL"); + expect(() => + resolveForkConnectReleaseConfig({ + ...complete, + T3CODE_CLERK_PUBLISHABLE_KEY: `pk_live_${btoa("clerk.t3.codes$")}`, + }), + ).toThrow("T3CODE_CLERK_PUBLISHABLE_KEY"); + for (const badKey of [ed25519.privateKey, rsa.publicKey, "not-a-pem"]) { + expect(() => + resolveForkConnectReleaseConfig({ ...complete, T3CODE_CLOUD_MINT_PUBLIC_KEY: badKey }), + ).toThrow("T3CODE_CLOUD_MINT_PUBLIC_KEY"); + } + }); }); diff --git a/scripts/lib/fork-release.ts b/scripts/lib/fork-release.ts index f15d702a6f636..5e63fda1802d4 100644 --- a/scripts/lib/fork-release.ts +++ b/scripts/lib/fork-release.ts @@ -1,3 +1,5 @@ +// @effect-diagnostics-next-line nodeBuiltinImport:off -- Validates Ed25519 SPKI PEM in release preflight. +import * as NodeCrypto from "node:crypto"; import { CLI_ARCHIVE_PLATFORM_KEYS, cliArchiveFileName, @@ -5,6 +7,7 @@ import { cliArchivePreviousFileName, } from "@t3tools/shared/cliRelease"; import { resolveChannelIdentity } from "@t3tools/shared/channelIdentity"; +import { forkClerkPublishableKey, forkHostedOrigin } from "@t3tools/shared/hostedConfig"; import { NPM_PACKAGE_NAME, PRODUCT_RELEASE_REPOSITORY } from "@t3tools/shared/productIdentity"; export const FORK_RELEASE_REPOSITORY = PRODUCT_RELEASE_REPOSITORY; @@ -90,6 +93,105 @@ export const MAC_RELEASE_CREDENTIALS = [ ]; /** Signing inputs that are configuration rather than secrets, so they come from `vars`. */ export const RELEASE_CREDENTIAL_VARIABLES = ["APPLE_TEAM_ID"]; +export const FORK_CONNECT_REQUIRED_VARIABLES = [ + "T3CODE_RELAY_URL", + "T3CODE_CLOUD_MINT_PUBLIC_KEY", + "T3CODE_CLERK_PUBLISHABLE_KEY", + "T3CODE_CLERK_JWT_TEMPLATE", + "T3CODE_CLERK_CLI_OAUTH_CLIENT_ID", +] as const; + +export interface ForkConnectReleaseConfig { + readonly enabled: boolean; + readonly relayUrl: string; + readonly cloudMintPublicKey: string; + readonly clerkPublishableKey: string; + readonly clerkJwtTemplate: string; + readonly clerkCliOAuthClientId: string; + readonly hostedAppUrl: string; +} + +/** Validate public Connect release variables all-or-nothing so nightlies stay cloudless until complete. */ +export function resolveForkConnectReleaseConfig(env: Environment): ForkConnectReleaseConfig { + const rawRelayUrl = env.T3CODE_RELAY_URL?.trim() ?? ""; + const cloudMintPublicKey = (env.T3CODE_CLOUD_MINT_PUBLIC_KEY?.trim() ?? "") + .replaceAll("\r\n", "\n") + .replaceAll("\n", "\\n"); + const rawClerkPublishableKey = env.T3CODE_CLERK_PUBLISHABLE_KEY?.trim() ?? ""; + const clerkJwtTemplate = env.T3CODE_CLERK_JWT_TEMPLATE?.trim() ?? ""; + const clerkCliOAuthClientId = env.T3CODE_CLERK_CLI_OAUTH_CLIENT_ID?.trim() ?? ""; + const rawHostedAppUrl = env.T3CODE_HOSTED_APP_URL?.trim() ?? ""; + + const values: Record<(typeof FORK_CONNECT_REQUIRED_VARIABLES)[number], string> = { + T3CODE_RELAY_URL: rawRelayUrl, + T3CODE_CLOUD_MINT_PUBLIC_KEY: cloudMintPublicKey, + T3CODE_CLERK_PUBLISHABLE_KEY: rawClerkPublishableKey, + T3CODE_CLERK_JWT_TEMPLATE: clerkJwtTemplate, + T3CODE_CLERK_CLI_OAUTH_CLIENT_ID: clerkCliOAuthClientId, + }; + const anySet = + Object.values(values).some((value) => value.length > 0) || rawHostedAppUrl.length > 0; + if (!anySet) { + return { + enabled: false, + relayUrl: "", + cloudMintPublicKey: "", + clerkPublishableKey: "", + clerkJwtTemplate: "", + clerkCliOAuthClientId: "", + hostedAppUrl: "", + }; + } + const missing = FORK_CONNECT_REQUIRED_VARIABLES.filter((key) => values[key].length === 0); + if (missing.length > 0) { + throw new Error( + `Incomplete Connect public configuration: missing ${missing.join(", ")}. Set all five required repository variables (${FORK_CONNECT_REQUIRED_VARIABLES.join(", ")}) or leave all unset for a cloudless build.`, + ); + } + const relayUrl = forkHostedOrigin(rawRelayUrl); + if (relayUrl === null) { + throw new Error("T3CODE_RELAY_URL must be a fork-owned HTTPS origin."); + } + const pem = cloudMintPublicKey.replace(/\\n/gu, "\n"); + if (/PRIVATE KEY/iu.test(pem) || !/^-----BEGIN PUBLIC KEY-----\n/u.test(pem)) { + throw new Error("T3CODE_CLOUD_MINT_PUBLIC_KEY must be an Ed25519 SPKI public key PEM."); + } + try { + const key = NodeCrypto.createPublicKey(pem); + if (key.asymmetricKeyType !== "ed25519") { + throw new Error("wrong key type"); + } + } catch { + throw new Error("T3CODE_CLOUD_MINT_PUBLIC_KEY must be a valid Ed25519 SPKI public key PEM."); + } + const clerkPublishableKey = forkClerkPublishableKey(rawClerkPublishableKey); + if (clerkPublishableKey === null) { + throw new Error("T3CODE_CLERK_PUBLISHABLE_KEY must be a valid fork-owned Clerk key."); + } + if (/[\r\n]/u.test(clerkJwtTemplate)) { + throw new Error("T3CODE_CLERK_JWT_TEMPLATE must be a single-line value."); + } + if (/[\r\n]/u.test(clerkCliOAuthClientId)) { + throw new Error("T3CODE_CLERK_CLI_OAUTH_CLIENT_ID must be a single-line value."); + } + let hostedAppUrl = ""; + if (rawHostedAppUrl.length > 0) { + const validatedHostedAppUrl = forkHostedOrigin(rawHostedAppUrl); + if (validatedHostedAppUrl === null) { + throw new Error("T3CODE_HOSTED_APP_URL must be a fork-owned HTTPS origin."); + } + hostedAppUrl = validatedHostedAppUrl; + } + return { + enabled: true, + relayUrl, + cloudMintPublicKey, + clerkPublishableKey, + clerkJwtTemplate, + clerkCliOAuthClientId, + hostedAppUrl, + }; +} export const WIN_RELEASE_CREDENTIALS = [ "AZURE_TENANT_ID", "AZURE_CLIENT_ID", diff --git a/scripts/lib/public-config.test.ts b/scripts/lib/public-config.test.ts index 2770e3457310c..6696401d0c59f 100644 --- a/scripts/lib/public-config.test.ts +++ b/scripts/lib/public-config.test.ts @@ -36,6 +36,8 @@ describe("loadRepoEnv", () => { T3CODE_CLERK_JWT_TEMPLATE: "template_ci", T3CODE_CLERK_CLI_OAUTH_CLIENT_ID: "oauth_ci", T3CODE_RELAY_URL: "https://ci.example.test", + T3CODE_CLOUD_MINT_PUBLIC_KEY: "mint_key_ci", + T3CODE_HOSTED_APP_URL: "https://app.example.test", }, repoRoot, }), @@ -49,6 +51,9 @@ describe("loadRepoEnv", () => { EXPO_PUBLIC_CLERK_JWT_TEMPLATE: "template_ci", T3CODE_RELAY_URL: "https://ci.example.test", VITE_T3CODE_RELAY_URL: "https://ci.example.test", + T3CODE_CLOUD_MINT_PUBLIC_KEY: "mint_key_ci", + T3CODE_HOSTED_APP_URL: "https://app.example.test", + VITE_HOSTED_APP_URL: "https://app.example.test", }); }); }); diff --git a/scripts/lib/public-config.ts b/scripts/lib/public-config.ts index 7fe8d05386cd1..807ae96985afa 100644 --- a/scripts/lib/public-config.ts +++ b/scripts/lib/public-config.ts @@ -9,6 +9,8 @@ export interface T3CodePublicConfig { readonly clerkJwtTemplate: string | undefined; readonly clerkCliOAuthClientId: string | undefined; readonly relayUrl: string | undefined; + readonly cloudMintPublicKey: string | undefined; + readonly hostedAppUrl: string | undefined; readonly mobileOtlpTracesUrl: string | undefined; readonly mobileOtlpTracesDataset: string | undefined; readonly mobileOtlpTracesToken: string | undefined; @@ -62,6 +64,17 @@ export function loadRepoEnv({ VITE_T3CODE_RELAY_URL: config.relayUrl, } : {}), + ...(config.cloudMintPublicKey + ? { + T3CODE_CLOUD_MINT_PUBLIC_KEY: config.cloudMintPublicKey, + } + : {}), + ...(config.hostedAppUrl + ? { + T3CODE_HOSTED_APP_URL: config.hostedAppUrl, + VITE_HOSTED_APP_URL: config.hostedAppUrl, + } + : {}), ...(config.mobileOtlpTracesUrl ? { T3CODE_MOBILE_OTLP_TRACES_URL: config.mobileOtlpTracesUrl, @@ -121,6 +134,8 @@ function resolvePublicConfig(...sources: readonly Environment[]): T3CodePublicCo "VITE_CLERK_CLI_OAUTH_CLIENT_ID", ), relayUrl: firstNonEmpty(sources, "T3CODE_RELAY_URL", "VITE_T3CODE_RELAY_URL"), + cloudMintPublicKey: firstNonEmpty(sources, "T3CODE_CLOUD_MINT_PUBLIC_KEY"), + hostedAppUrl: firstNonEmpty(sources, "T3CODE_HOSTED_APP_URL", "VITE_HOSTED_APP_URL"), mobileOtlpTracesUrl: firstNonEmpty( sources, "T3CODE_MOBILE_OTLP_TRACES_URL",