From 80d37dd9b497491f4e2e1ccc2b56eb9e88da8502 Mon Sep 17 00:00:00 2001 From: mapxn <16299874+mapxn@users.noreply.github.com> Date: Tue, 27 Oct 2026 14:20:56 +0800 Subject: [PATCH] =?UTF-8?q?ci:=20TOTP=5FENCRYPTION=5FKEY=20=E6=94=B9?= =?UTF-8?q?=E7=94=B1=20GitHub=20Actions=20=E5=86=99=E5=85=A5=20secrets=20?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E5=B9=B6=E5=81=9A=E5=BF=85=E5=A1=AB=E6=A0=A1?= =?UTF-8?q?=E9=AA=8C=20(ONE-519)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/scripts/test-deploy-checks.mjs | 101 +++++++++++++++++++++++++ .github/workflows/deploy.yml | 4 +- 2 files changed, 104 insertions(+), 1 deletion(-) diff --git a/.github/scripts/test-deploy-checks.mjs b/.github/scripts/test-deploy-checks.mjs index 3d2a3b3..2ed884a 100644 --- a/.github/scripts/test-deploy-checks.mjs +++ b/.github/scripts/test-deploy-checks.mjs @@ -4,10 +4,12 @@ import assert from 'node:assert/strict'; import { execFile, execFileSync } from 'node:child_process'; import { copyFileSync, + existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, + statSync, writeFileSync, } from 'node:fs'; import { createServer } from 'node:http'; @@ -601,3 +603,102 @@ test('deploy.yml:Smoke 步给自动推导补上 token 与账户 ID', () => { ); assert.match(smoke, /^ {8}run: bash \.github\/scripts\/smoke-test\.sh$/m); }); + +// ─── 改动 F:TOTP_ENCRYPTION_KEY 改由 GitHub Actions 写入 secrets 文件 ──────── + +// 执行 deploy.yml 里真实的 Write secrets file 步:取值、必填校验与落盘都走 +// workflow 自身,位置与格式不在测试里重写一份。 +function runWriteSecrets(f, values) { + const runnerTemp = join(f.dir, 'runner-temp'); + mkdirSync(runnerTemp, { recursive: true }); + const env = { ...f.env, RUNNER_TEMP: runnerTemp }; + for (const name of ['JWT_SECRET', 'SETUP_SECRET', 'TOTP_ENCRYPTION_KEY']) { + delete env[name]; + } + Object.assign(env, values); + const secretsPath = join(runnerTemp, 'secrets.json'); + try { + execFileSync( + 'bash', + [ + '--noprofile', + '--norc', + '-eo', + 'pipefail', + '-c', + stepScript('Write secrets file'), + ], + { cwd: f.dir, env, encoding: 'utf8', stdio: ['ignore', 'pipe', 'pipe'] }, + ); + } catch (e) { + return { + ok: false, + output: `${e.stdout ?? ''}${e.stderr ?? ''}`, + secretsPath, + }; + } + return { ok: true, output: '', secretsPath }; +} + +// 测试专用假值:只验证键名与接线,绝不涉及任何真实密钥。 +const fakeSecrets = { + JWT_SECRET: 'test-jwt-not-a-real-value', + SETUP_SECRET: 'test-setup-not-a-real-value', + TOTP_ENCRYPTION_KEY: 'test-totp-not-a-real-value', +}; + +test('secrets 文件:三键齐全时含 TOTP_ENCRYPTION_KEY', (t) => { + const f = fixture(t); + const result = runWriteSecrets(f, fakeSecrets); + assert.equal(result.ok, true, result.output); + const parsed = JSON.parse(readFileSync(result.secretsPath, 'utf8')); + assert.deepEqual(Object.keys(parsed).sort(), [ + 'JWT_SECRET', + 'SETUP_SECRET', + 'TOTP_ENCRYPTION_KEY', + ]); + assert.equal(parsed.TOTP_ENCRYPTION_KEY, fakeSecrets.TOTP_ENCRYPTION_KEY); + // 仍只在 $RUNNER_TEMP 落盘、权限 600。 + assert.equal(statSync(result.secretsPath).mode & 0o777, 0o600); +}); + +test('secrets 文件:缺 TOTP_ENCRYPTION_KEY 时该步失败且不继续', (t) => { + const f = fixture(t); + const values = { ...fakeSecrets }; + delete values.TOTP_ENCRYPTION_KEY; + const result = runWriteSecrets(f, values); + assert.equal(result.ok, false, '缺键必须失败关闭'); + assert.match( + result.output, + /::error::GitHub Secret 'TOTP_ENCRYPTION_KEY' is empty or missing/, + result.output, + ); + // 没有 secrets 文件,后续 Deploy 步拿不到 --secrets-file,部署不会继续。 + assert.equal(existsSync(result.secretsPath), false); +}); + +test('deploy.yml:Write secrets file 步从仓库 Secret 注入 TOTP_ENCRYPTION_KEY', () => { + const lines = readFileSync( + join(repoRoot, '.github', 'workflows', 'deploy.yml'), + 'utf8', + ).split('\n'); + const start = lines.findIndex( + (line) => line.trim() === '- name: Write secrets file', + ); + assert.notEqual(start, -1, 'deploy.yml 缺少 Write secrets file 步'); + 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'); + assert.match( + step, + /^ {10}TOTP_ENCRYPTION_KEY: \$\{\{ secrets\.TOTP_ENCRYPTION_KEY \}\}$/m, + 'TOTP_ENCRYPTION_KEY 必须取自仓库 Secret,与 JWT_SECRET 同路', + ); + // 与 JWT_SECRET/SETUP_SECRET 同规格:必填校验清单里点名。 + assert.match( + step, + /for name in JWT_SECRET SETUP_SECRET TOTP_ENCRYPTION_KEY; do/, + '必填校验须覆盖该键', + ); +}); diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index 54710f1..541711a 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -140,9 +140,10 @@ jobs: env: JWT_SECRET: ${{ secrets.JWT_SECRET }} "SETUP_SECRET": ${{ secrets.SETUP_SECRET }} + TOTP_ENCRYPTION_KEY: ${{ secrets.TOTP_ENCRYPTION_KEY }} run: | set -euo pipefail - for name in JWT_SECRET SETUP_SECRET; do + for name in JWT_SECRET SETUP_SECRET TOTP_ENCRYPTION_KEY; do if [ -z "$(printenv "$name" 2>/dev/null || true)" ]; then echo "::error::GitHub Secret '$name' is empty or missing; aborting deploy (fail-closed)." exit 1 @@ -157,6 +158,7 @@ jobs: JSON.stringify({ JWT_SECRET: process.env.JWT_SECRET, "SETUP_SECRET": process.env["SETUP_SECRET"], + TOTP_ENCRYPTION_KEY: process.env.TOTP_ENCRYPTION_KEY, }, null, 2) + "\n", { mode: 0o600 }, );