Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 82 additions & 3 deletions .github/scripts/test-deploy-checks.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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
`,
Expand All @@ -246,8 +285,21 @@ esac
stdout.includes(`TABLE_PREFIX: ${scenario.prefix || '<none>'}`),
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,
);
}
});
}
Expand Down Expand Up @@ -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',
);
});
25 changes: 25 additions & 0 deletions .github/scripts/verify-deployment.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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)"
Expand All @@ -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)" &&
Expand Down
9 changes: 8 additions & 1 deletion .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Loading