From 9cfe2114b1cf6f8876c7d1cd90b8f8f08c9ca826 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A7=9A=E7=AB=A0=E6=B5=A9?= Date: Mon, 23 Nov 2026 13:54:07 +0800 Subject: [PATCH] =?UTF-8?q?fix(cd):=20=E5=85=BC=E5=AE=B9=E5=BA=94=E7=94=A8?= =?UTF-8?q?=E4=B8=8E=E9=9B=86=E6=88=90=E6=B5=8B=E8=AF=95=E5=85=B1=E7=94=A8?= =?UTF-8?q?=E7=9A=84=20QA=20=E5=87=AD=E6=8D=AE=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/CANDIDATE_RELEASE_RUNBOOK.md | 2 +- scripts/candidate_runtime.py | 10 +++++++--- scripts/test_candidate_release.py | 22 ++++++++++++++++++++++ 3 files changed, 30 insertions(+), 4 deletions(-) diff --git a/docs/CANDIDATE_RELEASE_RUNBOOK.md b/docs/CANDIDATE_RELEASE_RUNBOOK.md index 6cb3ee1..fe81c78 100644 --- a/docs/CANDIDATE_RELEASE_RUNBOOK.md +++ b/docs/CANDIDATE_RELEASE_RUNBOOK.md @@ -34,7 +34,7 @@ - 版本管理机使用 Python 3.11 及以上、Docker Compose、HiFox CLI 和已有 Gitea 凭据。候选执行器通过 `candidate_worker.py <仓库工作树> <访问文件>` 绑定 `127.0.0.1:38984`,仓库工作树允许读取 `origin/main`。执行器通过现有 Gitea 凭据读取作业与日志。运行账户需能以 `weknora-test` 执行只读版本检查。 - 为候选执行器单独配置 `kg-candidate` Runner。作业使用同机回环网络,通过仓库 Secrets 的 `KG_CANDIDATE_ACCESS` 访问执行器。访问文件由执行器用户持有,内容为独立 64 位十六进制随机凭据。候选作业使用的容器须无宿主 Docker socket 和生产密钥挂载。 -- 业务验收使用 `~/.config/knowledge-governance/{qa.json,qa.pgpass,rds-ca.pem}`。复用 `qa_env.py` 的专用 `kg_qa_` RDS 库和角色校验。验收使用专用账号与测试数据。需要验证外部系统的用例须先准备各自的隔离资源。 +- 业务验收使用 `~/.config/knowledge-governance/{qa.json,qa.pgpass,rds-ca.pem}`。复用 `qa_env.py` 的专用 `kg_qa_` RDS 库和角色校验。`qa.pgpass` 可包含同一端点的应用库和集成测试库条目,候选只读取 `qa.json` 指定的库和角色。验收使用专用账号与测试数据。需要验证外部系统的用例须先准备各自的隔离资源。 - 候选执行器采用单验收槽。新批次启动前由运维确认前一批验收结束,按上一批 `ready.json` 中的容器身份停止其 Compose 项目,归档原有配置和回执后释放槽位。保留已验收的归档及审批记录。 ## 演练和当前环境 diff --git a/scripts/candidate_runtime.py b/scripts/candidate_runtime.py index 2fc4787..34e2c9c 100644 --- a/scripts/candidate_runtime.py +++ b/scripts/candidate_runtime.py @@ -35,10 +35,14 @@ def qa_database(path): passfile, cert = [path.parent / name for name in ('qa.pgpass', 'rds-ca.pem')] qa_env.private_file(passfile) qa_env.private_file(cert) - expected = f"{db['host']}:{db['port']}:{db['database']}:{db['user']}:" + endpoint = f"{db['host']}:{db['port']}:" + expected = f"{endpoint}{db['database']}:{db['user']}:" entries = [line for line in passfile.read_text().splitlines() if line and not line.startswith('#')] - cd.require(len(entries) == 1 and entries[0].startswith(expected), 'pgpass must authorize exactly the dedicated QA database') - password = re.sub(r'\\(.)', r'\1', entries[0][len(expected):]) + qa_entry = re.compile(re.escape(endpoint) + r'kg_qa_[a-z0-9_]{1,56}:kg_qa_[a-z0-9_]{1,56}:(?:[^:\\]|\\.)+') + cd.require(all(qa_entry.fullmatch(line) for line in entries), 'pgpass must contain only exact QA entries at the configured endpoint') + matching = [line for line in entries if line.startswith(expected)] + cd.require(len(matching) == 1, 'pgpass must contain exactly one entry for the configured QA database and role') + password = re.sub(r'\\(.)', r'\1', matching[0][len(expected):]) cd.require(bool(password), 'dedicated QA database password missing') url = (f"postgresql://{db['user']}:{quote(password, safe='')}@{db['host']}:{db['port']}/{db['database']}" '?sslmode=verify-full&sslrootcert=/run/qa/rds-ca.pem') diff --git a/scripts/test_candidate_release.py b/scripts/test_candidate_release.py index dd50a1c..342daee 100644 --- a/scripts/test_candidate_release.py +++ b/scripts/test_candidate_release.py @@ -6,6 +6,7 @@ from types import SimpleNamespace from unittest.mock import patch import candidate_release as candidate +import candidate_runtime as runtime import production_cd as cd BASE, A, B, OTHER = [c * 40 for c in 'abcd'] @@ -66,6 +67,27 @@ class CandidateReleaseTest(unittest.TestCase): with self.assertRaisesRegex(cd.CDError, 'candidate must be on main'): candidate.verify_source(dict(plan, base=root, sha=live), repo) + def test_candidate_selects_only_the_configured_qa_password(self): + with tempfile.TemporaryDirectory() as tmp: + root = Path(tmp) + config = root / 'qa.json' + pgpass = root / 'qa.pgpass' + cert = root / 'rds-ca.pem' + for path in (config, pgpass, cert): + path.touch(mode=0o600) + config.write_text('{"host":"kg-qa-rds.test","port":15483,"database":"kg_qa_app","user":"kg_qa_app"}') + application = 'kg-qa-rds.test:15483:kg_qa_app:kg_qa_app:app\\:example' + integration = 'kg-qa-rds.test:15483:kg_qa_it:kg_qa_it:it-example' + pgpass.write_text(f'{integration}\n{application}\n') + url, ca = runtime.qa_database(config) + self.assertEqual(ca, cert) + self.assertIn('app%3Aexample@kg-qa-rds.test:15483/kg_qa_app', url) + for rows in ((integration,), (application, application), (application, '*:15483:*:*:example'), + (application, 'kg-qa-rds.test:15483:governance:governance:example')): + pgpass.write_text('\n'.join(rows)) + with self.subTest(rows=rows), self.assertRaises(cd.CDError): + runtime.qa_database(config) + def test_require_all_qa_reports_and_shared_regression(self): sources = {QA1: (self.report, self.result()), QA2: (self.report, self.result())} with self.assertRaisesRegex(cd.CDError, 'every requirement and the release task'):