diff --git a/.github/release/deploy-legacy.sh b/.github/release/deploy-legacy.sh index e4000dd5cf0..f64a0a46789 100755 --- a/.github/release/deploy-legacy.sh +++ b/.github/release/deploy-legacy.sh @@ -1,5 +1,5 @@ #!/usr/bin/env bash -# deploy-legacy.sh : deploys the legacy +# deploy-legacy.sh [production-secrets.json]: deploys the legacy # Worker from a verified Build Output artifact (services/legacy/.cloudflare/output) # with the cf CLI pinned in cf/package-lock.json. # @@ -12,7 +12,6 @@ mode=$1 src=$(cd "$2" && pwd) -secrets=$(posix "$3") release=$(cd "$(dirname "$0")" && pwd) case "$mode" in @@ -37,8 +36,13 @@ unexpected=$(cd "$src" && find . ! -type d ! -type f -o -type f ! -path ./SHA256 expect_json "$src/v0/config.json" "$target/root.json" "The legacy Worker's root config" expect_json "$src/v0/workers/default/worker.config.json" "$target/worker.json" "The legacy Worker's config" -jq -e 'keys == ["GITHUB_APP_ID", "GITHUB_APP_PRIVATE_KEY"] and all(.[]; type == "string" and length > 0)' \ - "$secrets" > /dev/null || die "The secrets file does not hold exactly GITHUB_APP_ID and GITHUB_APP_PRIVATE_KEY" +args=() +if [[ "$mode" == production ]]; then + secrets=$(posix "$3") + jq -e 'keys == ["GITHUB_APP_ID", "GITHUB_APP_PRIVATE_KEY"] and all(.[]; type == "string" and length > 0)' \ + "$secrets" > /dev/null || die "The secrets file does not hold exactly GITHUB_APP_ID and GITHUB_APP_PRIVATE_KEY" + args=(--secrets-file "$secrets") +fi work=$(mktemp -d) trap 'rm -rf "$work"' EXIT @@ -52,4 +56,4 @@ cf="$release/cf/node_modules/.bin/cf" [ -x "$cf" ] || die "Install the pinned cf CLI first: npm ci --ignore-scripts in .github/release/cf" cd "$work" -"$cf" deploy --prebuilt --mode "$mode" --secrets-file "$secrets" +"$cf" deploy --prebuilt --mode "$mode" "${args[@]}" diff --git a/.github/release/targets/legacy/dev/worker.json b/.github/release/targets/legacy/dev/worker.json index eec130a4ceb..f34a902c1a7 100644 --- a/.github/release/targets/legacy/dev/worker.json +++ b/.github/release/targets/legacy/dev/worker.json @@ -7,10 +7,6 @@ "observability": { "enabled": true }, "workersDev": false, "previewUrls": false, - "env": { - "GITHUB_APP_ID": { "type": "secret" }, - "GITHUB_APP_PRIVATE_KEY": { "type": "secret" } - }, "manifest": { "type": "complete", "mainModule": "index.js", diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index 867c04a8c81..5f536276f7e 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -109,11 +109,14 @@ jobs: # An explicit bash runs with pipefail, so a failed secret read fails the step. shell: bash run: | + if [[ "$STAGE" == dev ]]; then + exec bash .github/release/deploy-legacy.sh dev "$RUNNER_TEMP/in/legacy-dev" + fi umask 077 secrets="$RUNNER_TEMP/legacy-secrets.json" trap 'rm -f "$secrets"' EXIT for name in GITHUB_APP_ID GITHUB_APP_PRIVATE_KEY; do - aws secretsmanager get-secret-value --secret-id "opencode/$STAGE/$name" --output json | + aws secretsmanager get-secret-value --secret-id "opencode/production/$name" --output json | jq --arg name "$name" '{($name): .SecretString}' done | jq -s add > "$secrets" bash .github/release/deploy-legacy.sh "$STAGE" "$RUNNER_TEMP/in/legacy-$STAGE" "$secrets" diff --git a/services/legacy/README.md b/services/legacy/README.md index 8aeb75cda19..2e1f40f91c1 100644 --- a/services/legacy/README.md +++ b/services/legacy/README.md @@ -9,13 +9,13 @@ One Worker that answers by hostname: Session sharing was retired in #55716 and V2 has no sharing. They serve the "Session sharing has been discontinued" page, and the old `/api/share` endpoints still answer so V1 clients keep working (`src/share.ts`). -- `api.opencode.ai` (production) and `api.dev.opencode.ai` (development) exchange a GitHub Actions +- `api.opencode.ai` (production) exchanges a GitHub Actions OIDC token for a GitHub App installation token for the V1 GitHub Action - (`POST /exchange_github_app_token`, `src/api.ts`). + (`POST /exchange_github_app_token`, `src/api.ts`). The dev endpoint returns 503. `cloudflare.config.ts` defines the Worker. `deploy:dev` and `deploy:production` deploy it with `cf` -from `.github/workflows/deploy.yml`. The deploy job reads the `GITHUB_APP_ID` and `GITHUB_APP_PRIVATE_KEY` Worker -secrets from AWS Secrets Manager (`opencode//GITHUB_APP_*`) and uploads them with +from `.github/workflows/deploy.yml`. The production deploy reads the `GITHUB_APP_ID` and `GITHUB_APP_PRIVATE_KEY` Worker +secrets from AWS Secrets Manager (`opencode/production/GITHUB_APP_*`) and uploads them with `--secrets-file`. | Environment | Worker | Hostnames | diff --git a/services/legacy/cloudflare.config.ts b/services/legacy/cloudflare.config.ts index 22a15d0ae21..6455c5604e6 100644 --- a/services/legacy/cloudflare.config.ts +++ b/services/legacy/cloudflare.config.ts @@ -17,8 +17,8 @@ export default defineConfig(({ mode }) => { compatibilityFlags: ["nodejs_compat"], entrypoint: "./src/index.ts", domains: dev ? ["dev.opncd.ai", "api.dev.opencode.ai"] : ["opncd.ai", "api.opencode.ai"], - // Set from AWS Secrets Manager (opencode//GITHUB_APP_*) by the deploy job with --secrets-file. - env: { + // The production deploy supplies these from AWS Secrets Manager with --secrets-file. + env: mode !== "production" ? undefined : { GITHUB_APP_ID: bindings.secret(), GITHUB_APP_PRIVATE_KEY: bindings.secret(), }, diff --git a/services/legacy/src/api.ts b/services/legacy/src/api.ts index 7a2503b64f6..d56729d0559 100644 --- a/services/legacy/src/api.ts +++ b/services/legacy/src/api.ts @@ -5,8 +5,8 @@ import { Octokit } from "@octokit/rest" import { parseRepositoryClaim } from "./github" export interface Env { - GITHUB_APP_ID: string - GITHUB_APP_PRIVATE_KEY: string + GITHUB_APP_ID?: string + GITHUB_APP_PRIVATE_KEY?: string } export default new Hono<{ Bindings: Env }>() @@ -15,6 +15,10 @@ export default new Hono<{ Bindings: Env }>() * Used by the GitHub action to get GitHub installation access token given the OIDC token */ .post("/exchange_github_app_token", async (c) => { + if (!c.env.GITHUB_APP_ID || !c.env.GITHUB_APP_PRIVATE_KEY) { + const dev = new URL(c.req.url).hostname === "api.dev.opencode.ai" + return c.json({ error: dev ? "not configured on dev" : "GitHub App not configured" }, 503) + } const EXPECTED_AUDIENCE = "opencode-github-action" const GITHUB_ISSUER = "https://token.actions.githubusercontent.com" const JWKS_URL = `${GITHUB_ISSUER}/.well-known/jwks`