From 7b4ec1f89db36d799dbdbb8e13a13f7b14fa135c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A7=9A=E7=AB=A0=E6=B5=A9?= Date: Wed, 25 Nov 2026 16:01:02 +0800 Subject: [PATCH] =?UTF-8?q?fix(qa):=20=E4=B8=BA=E5=80=99=E9=80=89=E6=8F=90?= =?UTF-8?q?=E4=BE=9B=E7=A8=B3=E5=AE=9A=E7=9A=84=E7=BB=91=E5=AE=9A=E4=B8=BB?= =?UTF-8?q?=E5=AF=86=E9=92=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/CANDIDATE_RELEASE_RUNBOOK.md | 4 ++++ scripts/candidate_runtime.py | 24 +++++++++++++++++++++++- scripts/test_candidate_release.py | 13 +++++++++++++ 3 files changed, 40 insertions(+), 1 deletion(-) diff --git a/docs/CANDIDATE_RELEASE_RUNBOOK.md b/docs/CANDIDATE_RELEASE_RUNBOOK.md index 1f6ce35..5492556 100644 --- a/docs/CANDIDATE_RELEASE_RUNBOOK.md +++ b/docs/CANDIDATE_RELEASE_RUNBOOK.md @@ -34,6 +34,10 @@ 执行 `candidate_coordinator.py register <发布任务 UUID>`。脚本复验运行身份,逐项核对报告作者、候选版本和归档、时序与必测结果。全项通过后在发布任务登记附原始报告链接的汇总,再写入并读回 `cd_acceptance`。验收候选只接受专用 RDS 模式。 +### 候选的知识库绑定密钥 + +验收候选运行在 QA 库上,知识库绑定需要独立的 32 字节主密钥。执行器在 `~/.local/state/kg-candidate-release/qa-binding-master.key` 维护唯一一份(0600、属主为容器应用用户),以只读方式挂到 API 与 worker 的 `/run/qa/binding-master.key`。所有候选共用这一份,已保存的绑定在下一轮候选仍可解开。文件缺失时首个候选自动生成;权限、长度或属主不符时拒绝启动,不静默降级。 + ### 原需求单的逐项报告 测试机将 `scripts/qa_report.py` 安装为 `~/.local/bin/kg-qa-report`。在 QA 的 HiFox 任务会话中,从本轮测试设计取得完整编号,保存到 `plan.json` 的 `business`、`regression` 数组。执行 `kg-qa-report init <任务> plan.json sheet.json` 读取原单候选回执并生成待填清单。测试中填写各编号的 `status`(`passed`、`failed`、`blocked`)和 `evidence`。`candidate_check` 记录指定候选部署上的实际验证结果及证据。环境受阻时记录原因。 diff --git a/scripts/candidate_runtime.py b/scripts/candidate_runtime.py index 2ad99cd..c501dae 100644 --- a/scripts/candidate_runtime.py +++ b/scripts/candidate_runtime.py @@ -15,6 +15,8 @@ from urllib.parse import quote import production_cd as cd import qa_env +STATE = Path.home() / '.local/state/kg-candidate-release' + def isolated_subnet(exclude=()): nets = cd.run(['docker', 'network', 'ls', '-q'], 'list Docker networks').decode().split() @@ -55,6 +57,23 @@ def qa_process_command(role): return ['/bin/sh', '-ec', 'export DATABASE_URL="$${DATABASE_URL}&pool_max_conns=2"; exec /app/' + role] +APP_UID = 65532 + + +def qa_binding_key(image): + """Give QA candidates one stable binding master key, readable by the application user.""" + path = STATE / 'qa-binding-master.key' + if not path.exists(): + cd.atomic_write(path, secrets.token_bytes(32), mode=0o600) + cd.run(['docker', 'run', '--rm', '--user', '0', '-v', f'{path.parent}:/run/kg-key', image, + 'chown', f'{APP_UID}:{APP_UID}', f'/run/kg-key/{path.name}'], + 'hand the QA binding master key to the application user', timeout=90) + info = path.lstat() + cd.require(info.st_mode & 0o777 == 0o600 and info.st_size == 32 and info.st_uid == APP_UID, + 'QA binding master key must be a 32-byte file owned by the application user with mode 0600') + return path + + def ready(directory, bundle, test_database=None): bundle = bundle.resolve() manifest = cd.read_json(bundle / 'release.json') @@ -88,6 +107,7 @@ def ready(directory, bundle, test_database=None): url, cert = qa_database(test_database) env['DATA' + 'BASE_URL'] = url cd.atomic_write(directory / 'rds-ca.pem', cert.read_bytes(), mode=0o444) + env['WEKNORA_BINDING_MASTER_KEY_FILE'] = '/run/qa/binding-master.key' app_env = directory / 'app.env' cd.atomic_write(app_env, ''.join(f'{k}={v}\n' for k, v in env.items()).encode()) names = {role: f'{project}-{role}' for role in cd.ROLES} @@ -96,10 +116,12 @@ def ready(directory, bundle, test_database=None): cd.verify_archive(archive, item['id']) cd.run(['docker', 'load', '-i', str(archive)], 'load sealed candidate image', timeout=150) services[role] = {'image': item['id'], 'container_name': names[role]} + key = qa_binding_key(manifest['images']['api']['id']) if test_database is not None else None for role in ('api', 'worker'): services[role]['env_file'] = [str(app_env)] if test_database is not None: - services[role]['volumes'] = [f'{directory / "rds-ca.pem"}:/run/qa/rds-ca.pem:ro'] + services[role]['volumes'] = [f'{directory / "rds-ca.pem"}:/run/qa/rds-ca.pem:ro', + f'{key}:/run/qa/binding-master.key:ro'] services['api']['healthcheck'] = {'test': ['CMD', 'curl', '-fsS', 'http://127.0.0.1:8080/health'], 'interval': '2s', 'timeout': '3s', 'retries': 45} if test_database is None: diff --git a/scripts/test_candidate_release.py b/scripts/test_candidate_release.py index f029825..ba8d910 100644 --- a/scripts/test_candidate_release.py +++ b/scripts/test_candidate_release.py @@ -33,6 +33,19 @@ class CandidateReleaseTest(unittest.TestCase): value = subprocess.check_output([shell, flags, rendered], env={**os.environ, key: 'qa?sslmode=verify-full'}, text=True) self.assertEqual(value.strip(), 'qa?sslmode=verify-full&pool_max_conns=2') + def test_qa_candidate_gets_a_stable_binding_master_key(self): + import os + with tempfile.TemporaryDirectory() as tmp, patch.object(runtime, 'STATE', Path(tmp)), \ + patch.object(runtime, 'APP_UID', os.getuid()), patch.object(runtime.cd, 'run') as docker: + path = runtime.qa_binding_key('image') + self.assertEqual((path.stat().st_mode & 0o777, path.stat().st_size), (0o600, 32)) + first = path.read_bytes() + self.assertEqual(runtime.qa_binding_key('image').read_bytes(), first) + self.assertEqual(docker.call_count, 1) + path.chmod(0o644) + with self.assertRaises(runtime.cd.CDError): + runtime.qa_binding_key('image') + def test_one_requirement_owns_its_own_release(self): plan = dict(self.plan, items=[{'task': TASK, 'commit': B}], extra=[A]) candidate.check_scope(plan, self.prs.__getitem__, [A, B])