From 8c7fc80dc325dd2b463081af1b4d26093d5dc011 Mon Sep 17 00:00:00 2001 From: mapxn <16299874+mapxn@users.noreply.github.com> Date: Tue, 27 Oct 2026 18:55:38 +0800 Subject: [PATCH] =?UTF-8?q?ci:=20--secrets-file=20=E7=9B=B4=E8=BE=BE=20Wra?= =?UTF-8?q?ngler=EF=BC=8C=E5=B9=B6=E5=AF=B9=E9=83=A8=E7=BD=B2=E7=89=88?= =?UTF-8?q?=E6=9C=AC=E5=8A=A0=20secret=20=E7=BB=91=E5=AE=9A=E9=97=B8?= =?UTF-8?q?=E9=97=A8=20(ONE-565)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Deploy 步原本 `pnpm run deploy -- --secrets-file ...`:pnpm 把字面 `--` 转发给脚本,Wrangler 的 yargs 随后把其后的参数当位置参数静默丢弃, JWT_SECRET / SETUP_SECRET / TOTP_ENCRYPTION_KEY 从未绑到线上版本。 - deploy.yml:直接 `pnpm exec wrangler deploy --secrets-file`(worker 的 deploy 脚本就是裸 `wrangler deploy`,Build 已在前面单独跑过,无前置步骤丢失)。 - verify-deployment.sh:用 `wrangler versions view --name --json` 断言 secret_text 绑定集合 ⊇ 三个密钥,缺一即 ::error:: + exit 1;只输出 名字与类型,不含值。worker 名默认 hop,可用 WORKER_NAME 覆盖以便本地对 scratch worker 验证。 - test-deploy-checks.mjs:控制面桩补上带 --name 的 versions view,新增 「缺 secret」「secret 被写成 plain_text」负例,并加一条 Deploy 步回归断言。 --- .github/scripts/test-deploy-checks.mjs | 85 +++++++++++++++++++++++++- .github/scripts/verify-deployment.sh | 25 ++++++++ .github/workflows/deploy.yml | 9 ++- 3 files changed, 115 insertions(+), 4 deletions(-) diff --git a/.github/scripts/test-deploy-checks.mjs b/.github/scripts/test-deploy-checks.mjs index 2ed884a..50309dc 100644 --- a/.github/scripts/test-deploy-checks.mjs +++ b/.github/scripts/test-deploy-checks.mjs @@ -210,17 +210,56 @@ for (const scenario of [ env: '', ok: false, }, + { + // 密钥闸门:版本上没有 secret_text 的 JWT_SECRET 必须失败。 + name: '缺 JWT_SECRET 的 secret 绑定必须失败', + version: 'uploaded', + percentage: 100, + prefix: 'hop', + env: 'hop', + secretBindings: [ + '{"name":"SETUP_SECRET","type":"secret_text"}', + '{"name":"TOTP_ENCRYPTION_KEY","type":"secret_text"}', + ], + expectError: + '::error::.*no secret_text binding for: JWT_SECRET\\. The deploy', + ok: false, + }, + { + // 有同名绑定但不是 secret_text(例如在控制台手工加成了普通变量)也算缺。 + name: 'TOTP_ENCRYPTION_KEY 只是 plain_text 必须失败', + version: 'uploaded', + percentage: 100, + prefix: 'hop', + env: 'hop', + secretBindings: [ + '{"name":"JWT_SECRET","type":"secret_text"}', + '{"name":"SETUP_SECRET","type":"secret_text"}', + '{"name":"TOTP_ENCRYPTION_KEY","type":"plain_text","text":""}', + ], + expectError: '::error::.*TOTP_ENCRYPTION_KEY', + ok: false, + }, ]) { test(`控制面:${scenario.name}`, (t) => { const f = fixture(t); + // 默认:三个运行时密钥都作为 secret_text 绑在版本上(就是修复后 + // --secrets-file 生效的模样);负例按需换成缺项 / 错类型的列表。 + const secretBindings = ( + scenario.secretBindings ?? [ + '{"name":"JWT_SECRET","type":"secret_text"}', + '{"name":"SETUP_SECRET","type":"secret_text"}', + '{"name":"TOTP_ENCRYPTION_KEY","type":"secret_text"}', + ] + ).join(','); writeFileSync( join(f.dir, 'pnpm'), `#!/bin/sh case "$*" in 'exec wrangler deployments status --json') printf '%s\\n' '{"versions":[{"version_id":"${scenario.version}","percentage":${scenario.percentage}}]}' ;; - 'exec wrangler versions view uploaded --json') - printf '%s\\n' '{"id":"uploaded","resources":{"bindings":[{"name":"TABLE_PREFIX","type":"plain_text","text":"${scenario.prefix}"}]}}' ;; + 'exec wrangler versions view uploaded --json'|'exec wrangler versions view uploaded --name hop --json') + printf '%s\\n' '{"id":"uploaded","resources":{"bindings":[{"name":"TABLE_PREFIX","type":"plain_text","text":"${scenario.prefix}"},${secretBindings}]}}' ;; *) exit 99 ;; esac `, @@ -246,8 +285,21 @@ esac stdout.includes(`TABLE_PREFIX: ${scenario.prefix || ''}`), stdout, ); + assert.match(stdout, /Secret bindings on version uploaded/); } else { - assert.throws(run, /Command failed/); + let thrown = null; + try { + run(); + } catch (e) { + thrown = e; + } + assert.ok(thrown, '必须失败'); + const output = `${thrown.stdout ?? ''}${thrown.stderr ?? ''}`; + assert.match( + output, + new RegExp(scenario.expectError ?? '::error::'), + output, + ); } }); } @@ -702,3 +754,30 @@ test('deploy.yml:Write secrets file 步从仓库 Secret 注入 TOTP_ENCRYPTION '必填校验须覆盖该键', ); }); + +// ─── 改动 B:Deploy 步让 --secrets-file 真正到达 Wrangler ───────────────────── + +test('deploy.yml:Deploy 步直接调 Wrangler 传 --secrets-file', () => { + const lines = readFileSync( + join(repoRoot, '.github', 'workflows', 'deploy.yml'), + 'utf8', + ).split('\n'); + const start = lines.findIndex((line) => line.trim() === '- name: Deploy'); + assert.notEqual(start, -1, 'deploy.yml 缺少 Deploy 步'); + const end = lines.findIndex( + (line, i) => i > start && /^\s*- (name|uses):/.test(line), + ); + const step = lines.slice(start, end === -1 ? lines.length : end).join('\n'); + // `pnpm run deploy -- --secrets-file` 会把字面 `--` 转发给脚本,Wrangler 的 + // yargs 随后把其后的参数当位置参数丢掉,密钥因此从未上传(ONE-563 根因)。 + assert.doesNotMatch( + step, + /pnpm run deploy --\s/, + '不能再经过 pnpm 的 `--` 转发', + ); + assert.match( + step, + /run: pnpm exec wrangler deploy --secrets-file "\$RUNNER_TEMP\/secrets\.json"/, + 'Deploy 步必须由 Wrangler 自己收到 --secrets-file', + ); +}); diff --git a/.github/scripts/verify-deployment.sh b/.github/scripts/verify-deployment.sh index 23b7701..610793c 100644 --- a/.github/scripts/verify-deployment.sh +++ b/.github/scripts/verify-deployment.sh @@ -8,6 +8,8 @@ # # Env: WRANGLER_OUTPUT_FILE_PATH — the ND-JSON file `wrangler deploy` wrote # TABLE_PREFIX — resolved prefix ('' means explicit no prefix) +# WORKER_NAME — optional, Worker name for the version lookup +# (default 'hop', the name in wrangler.toml) set -euo pipefail here="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" @@ -23,6 +25,29 @@ if [[ -z "$version" ]]; then fi echo "Uploaded version: $version" +# Secrets gate: the deploy uploads JWT_SECRET / SETUP_SECRET / TOTP_ENCRYPTION_KEY +# from the file passed to `--secrets-file`. That flag used to be dropped by pnpm's +# argument forwarding, so a green deploy could still ship a version with no +# secrets bound and only fail at runtime (POST /api/setup → 500). Assert the +# bindings on the uploaded version itself. `versions view --json` returns binding +# names and types only — never secret values — so this output is safe in CI logs. +# `--secrets-file` is additive (omitted secrets are not deleted), so a re-run +# backfills anything reported missing here. +worker_name="${WORKER_NAME:-hop}" +secrets_view="$(pnpm exec wrangler versions view "$version" --name "$worker_name" --json)" +# Don't turn an unexpected API response into a silent pass. +jq -e --arg v "$version" '.id == $v and (.resources.bindings | type == "array")' <<<"$secrets_view" >/dev/null +missing_secrets="$(jq -r ' + ([.resources.bindings[] | select(.type == "secret_text") | .name]) as $bound + | ["JWT_SECRET", "SETUP_SECRET", "TOTP_ENCRYPTION_KEY"] + | map(select(. as $name | ($bound | index($name)) == null)) + | join(", ")' <<<"$secrets_view")" +if [[ -n "$missing_secrets" ]]; then + echo "::error::Worker version $version has no secret_text binding for: ${missing_secrets}. The deploy did not apply --secrets-file; re-run the Deploy workflow to re-upload the version with the secrets file." + exit 1 +fi +echo "Secret bindings on version $version: JWT_SECRET SETUP_SECRET TOTP_ENCRYPTION_KEY" + status="" for attempt in 1 2 3 4 5; do if status="$(pnpm exec wrangler deployments status --json)" && diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index 541711a..198e608 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -167,12 +167,19 @@ jobs: # Web and worker were built above; deploy the worker directly so wrangler # can record the uploaded version ID for the verification step. + # + # Invoke wrangler itself rather than `pnpm run deploy`: pnpm forwards a + # literal `--` to the script, and wrangler's yargs then treats everything + # after it as positional arguments, silently dropping --secrets-file. The + # worker's own `deploy` script is just `wrangler deploy` (the build ran in + # the Build step above), so this drops no pre-step — and the flag now + # reaches wrangler. Verify deployment below asserts the secrets landed. - name: Deploy working-directory: apps/worker env: CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }} WRANGLER_OUTPUT_FILE_PATH: ${{ runner.temp }}/wrangler-output.ndjson - run: pnpm run deploy -- --secrets-file "$RUNNER_TEMP/secrets.json" + run: pnpm exec wrangler deploy --secrets-file "$RUNNER_TEMP/secrets.json" # Checked through the Cloudflare API rather than the public hostname, so # the zone's bot protection can't make it inconclusive.