fix(cd): 校验候选部署入口并隔离凭据
build / build (push) Successful in 1m30s

This commit is contained in:
姚章浩
2026-11-23 13:28:40 +08:00
parent 1beaa31771
commit 0246db357f
4 changed files with 23 additions and 14 deletions
+17 -4
View File
@@ -59,9 +59,22 @@ jobs:
test "$GITHUB_EVENT_NAME" = workflow_dispatch
test "$GITHUB_REF" = refs/heads/main
test -n "$KG_CANDIDATE_ACCESS"
python3 -c '
python3 - <<'PY'
import json, os, sys
with open(os.environ["GITHUB_EVENT_PATH"]) as source:
from urllib.error import HTTPError
from urllib.request import ProxyHandler, Request, build_opener
with open(os.environ['GITHUB_EVENT_PATH']) as source:
event = json.load(source)
print(json.dumps({"run": os.environ["GITHUB_RUN_ID"], "plan": json.loads(event["inputs"]["plan"])}))
' | curl --noproxy '*' --silent --show-error --fail-with-body --max-time 2540 -H "Authorization: Bearer $KG_CANDIDATE_ACCESS" -H 'Content-Type: application/json' -X POST --data-binary @- http://127.0.0.1:38984/prepare
if event.get('inputs', {}).get('mode') != 'prepare':
sys.exit('candidate workflow mode is not prepare')
data = json.dumps({'run': os.environ['GITHUB_RUN_ID'], 'plan': json.loads(event['inputs']['plan'])}).encode()
request = Request('http://127.0.0.1:38984/prepare', data=data, headers={
'Authorization': 'Bearer ' + os.environ['KG_CANDIDATE_ACCESS'], 'Content-Type': 'application/json'})
opener = build_opener(ProxyHandler({}))
try:
with opener.open(request, timeout=2540) as response:
print(response.read(65536).decode())
except HTTPError as exc:
print(exc.read(4096).decode(), file=sys.stderr)
sys.exit(1)
PY
+1 -1
View File
@@ -32,7 +32,7 @@
## 执行器配置
- 版本管理机使用 Python 3.11 及以上、Docker Compose、HiFox CLI 和已有 Gitea 凭据。候选执行器通过 `candidate_worker.py <仓库工作树> <访问文件>` 绑定 `127.0.0.1:38984`,仓库工作树允许读取 `origin/main`。配置 `KG_CANDIDATE_READ_TOKEN` 用于读取 Gitea 作业与日志。运行账户需能以 `weknora-test` 执行只读版本检查。
- 版本管理机使用 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 库和角色校验。验收使用专用账号与测试数据。需要验证外部系统的用例须先准备各自的隔离资源。
- 候选执行器采用单验收槽。新批次启动前由运维确认前一批验收结束,按上一批 `ready.json` 中的容器身份停止其 Compose 项目,归档原有配置和回执后释放槽位。保留已验收的归档及审批记录。
+3 -7
View File
@@ -31,13 +31,9 @@ def verify_job(run_id, plan, repo):
guard = [job for job in jobs if job.get('name') == 'guard' and job.get('conclusion') == 'success']
cd.require(len(guard) == 1 and type(guard[0].get('id')) is int, 'candidate guard did not succeed')
# Gitea logs are plaintext and may prefix each line with a timestamp.
from urllib.request import ProxyHandler, build_opener
from urllib.request import Request
token = os.environ['KG_CANDIDATE_READ_TOKEN']
request = Request(cd.FORGE + candidate.API + f'/actions/jobs/{guard[0]["id"]}/logs',
headers={'Authorization': 'token ' + token})
with build_opener(ProxyHandler({}), cd.NoRedirect()).open(request, timeout=15) as response:
lines = response.read(2**20).decode().splitlines()
raw = coordinator.hfx.forge('GET', candidate.API + f'/actions/jobs/{guard[0]["id"]}/logs', raw=True)
cd.require(len(raw) <= 2**20, 'candidate guard log exceeded the read limit')
lines = raw.decode().splitlines()
cd.require(sum(line.strip().endswith(candidate.proof(plan, 'prepare')) for line in lines) == 1,
'candidate input does not match Gitea guard proof')
+2 -2
View File
@@ -19,7 +19,7 @@ PATH = Path.home() / '.local/state/kg-hifox-release'
RUN_ID = re.compile('[1-9][0-9]{0,14}')
def forge(method, path, payload=None):
def forge(method, path, payload=None, *, raw=False):
request = Request(cd.FORGE + path, method=method,
data=json.dumps(payload).encode() if payload is not None else None)
env = {**os.environ, 'GIT_TERMINAL_PROMPT': '0', 'GIT_ASKPASS': '/bin/false'}
@@ -31,7 +31,7 @@ def forge(method, path, payload=None):
request.add_header('Authorization', 'Basic ' + auth)
request.add_header('Content-Type', 'application/json')
with build_opener(ProxyHandler({}), cd.NoRedirect()).open(request, timeout=20) as response:
return json.load(response)
return response.read(2**20 + 1) if raw else json.load(response)
def hifox(cli, *args):