fix(qa): 为候选提供稳定的绑定主密钥
build / build (push) Successful in 3m16s

This commit is contained in:
姚章浩
2026-11-25 16:01:02 +08:00
parent 6a6fa7129a
commit 7b4ec1f89d
3 changed files with 40 additions and 1 deletions
+23 -1
View File
@@ -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: