fix(deploy): 校验云执行器授权配置与稳定启动
build / build (push) Successful in 1m21s

This commit is contained in:
姚章浩
2026-10-31 04:26:54 +08:00
parent 286f1fb375
commit a50cd73423
5 changed files with 163 additions and 4 deletions
+7
View File
@@ -1,5 +1,12 @@
# Changelog
## 开发快照(云执行器授权配置与启动核验)
### Fixed
- 云发布执行器开启写入前检查授权人和私有 token 文件,按固定容器路径只读挂载;配置缺失在替换旧容器前拒绝。
- 部署脚本连续检查容器运行及重启计数,启动失败或重启循环不再误报成功;部署回归纳入必需契约检查。
## 开发快照(维护探针排空修复)
### Fixed
+8
View File
@@ -7,6 +7,14 @@ import (
// Exercise the real shell scripts with isolated fake Docker/SSH commands.
// This checks deployment argv contracts, not live cloud connectivity.
func TestReleaseExecutorAuthorization(t *testing.T) {
cmd := exec.Command("python3", "test_release_executor.py")
out, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("executor authorization contract: %v\n%s", err, out)
}
}
func TestDeploymentScripts(t *testing.T) {
cmd := exec.Command("python3", "test_deployment.py")
out, err := cmd.CombinedOutput()
+37 -3
View File
@@ -31,6 +31,35 @@ grep -q '^LIBRARY_WORKER_ROLE=release-executor$' "$ENV_FILE" || die "$ENV_FILE
[ -s "$PGPASS_FILE" ] && [ "$(stat -c '%a' "$PGPASS_FILE")" = "600" ] || die "治理 pgpass 必须是非空 0600 文件"
grep -qx 'PGPASSFILE=/run/secrets/governance_pgpass' "$ENV_FILE" || die "PGPASSFILE 必须指向容器内治理密码文件"
AUTH_TOKEN_FILE="${RELEASE_AUTH_TOKEN_FILE:-/srv/knowledge-governance/production_release_auth_token}"
# Parse only named fields as data, never source a credential-bearing dotenv.
read_setting() {
local wanted="$1" line value="" found=0
while IFS= read -r line || [ -n "$line" ]; do
if [[ "$line" == "$wanted="* ]]; then
((found += 1))
value="${line#*=}"
fi
done < "$ENV_FILE"
[ "$found" -le 1 ] || die "重复配置字段:$wanted"
printf '%s' "$value"
}
auth_mount=()
enabled="$(read_setting PRODUCTION_RELEASE_REQUESTS_ENABLED)"
case "$enabled" in
true|TRUE|True|1|t|T)
author="$(read_setting PRODUCTION_RELEASE_AUTHORIZER_REF)"
[ -n "$author" ] && [ "${#author}" -le 128 ] && [[ "$author" != *[$'\r\n\t']* ]] || die "PRODUCTION_RELEASE_AUTHORIZER_REF 缺失或非法"
token_path="$(read_setting PRODUCTION_RELEASE_AUTH_TOKEN_FILE)"
[ "$token_path" = /run/secrets/production_release_auth_token ] || die "PRODUCTION_RELEASE_AUTH_TOKEN_FILE 必须指向 /run/secrets/production_release_auth_token"
[ -f "$AUTH_TOKEN_FILE" ] && [ ! -L "$AUTH_TOKEN_FILE" ] && [ -s "$AUTH_TOKEN_FILE" ] && [ "$(stat -c '%a' "$AUTH_TOKEN_FILE")" = 600 ] || die "授权 token 必须是非空普通 0600 文件"
auth_mount=(-v "$AUTH_TOKEN_FILE:$token_path:ro")
;;
false|FALSE|False|0|f|F|"") ;;
*) die "PRODUCTION_RELEASE_REQUESTS_ENABLED 不是有效布尔值" ;;
esac
install -d -m 0700 "$ARCHIVE" "$JOURNAL" "$BUNDLES"
install -d -m 0700 "$ARCHIVE/blobs" "$ARCHIVE/versions"
@@ -52,9 +81,14 @@ docker run -d --name "$NAME" \
-v "$GATE_STATE:/gate-state" \
-v "$PG_SOCKET_DIR:/run/kgtest-pg:ro" \
-v "$PGPASS_FILE:/run/secrets/governance_pgpass:ro" \
"${auth_mount[@]}" \
--env-file "$ENV_FILE" \
"$IMAGE" /app/worker >/dev/null
sleep 2
docker ps --filter "name=$NAME" --format '{{.Names}} {{.Status}} {{.Image}}'
echo "release-executor: 启动完成;日志用 docker logs -f $NAME(准入失败会在第一屏写明原因)"
# A created container or docker-ps exit code is not a live executor.
for attempt in 1 2 3; do
sleep 2
state="$(docker inspect --format '{{.State.Status}} {{.RestartCount}}' "$NAME")" || die "无法读取执行器状态"
[ "$state" = 'running 0' ] || die "执行器未稳定运行,请检查目标机容器日志"
done
echo "release-executor: 启动检查通过(连续 running、无重启);业务验收仍须核对作业执行结果"
+1 -1
View File
@@ -40,7 +40,7 @@ class DeploymentTest(unittest.TestCase):
def test_executor_mounts_socket_and_secret(self):
self.stub('id', "print('0')")
self.stub('sleep', '')
self.stub('docker', "if sys.argv[1:2] == ['inspect']: sys.exit(1)")
self.stub('docker', "if sys.argv[1:2] == ['inspect']:\n if '--format' in sys.argv: print('running 0')\n else: sys.exit(1)")
secret = self.root / 'pgpass'
secret.write_text('localhost:5432:governance:tester:example-only\n')
secret.chmod(0o600)
+110
View File
@@ -0,0 +1,110 @@
"""Exercise the deployment shell with fake Docker; never touch a daemon."""
import json
import os
from pathlib import Path
import subprocess
import tempfile
import unittest
SCRIPT = Path(__file__).with_name("run-release-executor.sh")
class ReleaseExecutorTest(unittest.TestCase):
def setUp(self):
self.tmp = tempfile.TemporaryDirectory()
self.addCleanup(self.tmp.cleanup)
self.root = Path(self.tmp.name)
self.bin = self.root / "bin"
self.bin.mkdir()
self.calls = self.root / "calls.jsonl"
for name, text in {
"id": "#!/bin/sh\nprintf '0\\n'\n",
"sleep": "#!/bin/sh\nexit 0\n",
"docker": """#!/usr/bin/env python3
import json,os,sys
with open(os.environ['CALLS'],'a') as f: f.write(json.dumps(sys.argv[1:])+'\\n')
a=sys.argv[1:]
if a[0]=='inspect':
if '--format' in a: print(os.environ.get('CONTAINER_STATE','running 0'))
else: sys.exit(0)
""",
}.items():
p = self.bin / name
p.write_text(text)
p.chmod(0o755)
self.envfile = self.root / "executor.env"
self.token = self.root / "token"
pgpass = self.root / "pgpass"
pgpass.write_text("fixture-password-not-real")
pgpass.chmod(0o600)
self.env = dict(os.environ, PATH=str(self.bin) + os.pathsep + os.environ["PATH"],
CALLS=str(self.calls), RELEASE_EXECUTOR_IMAGE="test:fixture",
RELEASE_EXECUTOR_ENV=str(self.envfile), RELEASE_PGPASS_FILE=str(pgpass),
RELEASE_PG_SOCKET_DIR=str(self.root), RELEASE_GATE_STATE=str(self.root),
RELEASE_ARCHIVE_ROOT=str(self.root / "archive"),
RELEASE_JOURNAL_ROOT=str(self.root / "journal"),
RELEASE_BUNDLE_DIR=str(self.root / "bundles"),
RELEASE_AUTH_TOKEN_FILE=str(self.token))
def run_script(self, enabled=True, author=True, path=True, token=True, mode=0o600):
lines = ["LIBRARY_WORKER_ROLE=release-executor", "PGPASSFILE=/run/secrets/governance_pgpass",
"PRODUCTION_RELEASE_REQUESTS_ENABLED=" + str(enabled).lower()]
if author:
lines.append("PRODUCTION_RELEASE_AUTHORIZER_REF=fixture-owner")
if path:
lines.append("PRODUCTION_RELEASE_AUTH_TOKEN_FILE=/run/secrets/production_release_auth_token")
self.envfile.write_text("\n".join(lines) + "\n")
self.envfile.chmod(0o600)
if token:
self.token.write_text("fixture-only-not-real")
self.token.chmod(mode)
result = subprocess.run(["bash", str(SCRIPT)], env=self.env, text=True, capture_output=True)
calls = [json.loads(line) for line in self.calls.read_text().splitlines()] if self.calls.exists() else []
self.assertNotIn("fixture-only-not-real", result.stdout + result.stderr)
return result, calls
def test_enabled_missing_author_rejected_before_replacing(self):
r, calls = self.run_script(author=False)
self.assertNotEqual(r.returncode, 0)
self.assertFalse(any(c[0] in ("rm", "run") for c in calls))
def test_enabled_missing_token_path_rejected_before_replacing(self):
r, calls = self.run_script(path=False)
self.assertNotEqual(r.returncode, 0)
self.assertFalse(any(c[0] in ("rm", "run") for c in calls))
def test_enabled_missing_token_file_rejected_before_replacing(self):
r, calls = self.run_script(token=False)
self.assertNotEqual(r.returncode, 0)
self.assertFalse(any(c[0] in ("rm", "run") for c in calls))
def test_enabled_wrong_token_permissions_rejected_before_replacing(self):
r, calls = self.run_script(mode=0o644)
self.assertNotEqual(r.returncode, 0)
self.assertFalse(any(c[0] in ("rm", "run") for c in calls))
def test_enabled_mounts_private_token_readonly(self):
r, calls = self.run_script()
self.assertEqual(r.returncode, 0, r.stderr)
run = next(c for c in calls if c[0] == "run")
self.assertIn(str(self.token) + ":/run/secrets/production_release_auth_token:ro", run)
def test_disabled_needs_no_authorization_material(self):
r, calls = self.run_script(enabled=False, author=False, path=False, token=False)
self.assertEqual(r.returncode, 0, r.stderr)
self.assertTrue(any(c[0] == "run" for c in calls))
def test_crash_loop_is_not_success(self):
self.env["CONTAINER_STATE"] = "restarting 3"
r, _ = self.run_script()
self.assertNotEqual(r.returncode, 0)
self.assertNotIn("启动完成", r.stdout)
def test_already_restarted_is_not_success(self):
self.env["CONTAINER_STATE"] = "running 1"
r, _ = self.run_script()
self.assertNotEqual(r.returncode, 0)
if __name__ == "__main__":
unittest.main()