Files
canina/scripts/provision-stage-runtime.py
T
2026-10-24 03:41:43 +00:00

208 lines
12 KiB
Python

#!/usr/bin/env python3
"""Opt-in CI Stage provisioning. No Docker, DB writes, service start or polling."""
import argparse
import hashlib
import json
import os
from pathlib import Path
import re
import secrets
import shutil
import stat
import subprocess
import tempfile
BASE_URL = 'https://stageapi.canina.ir/api/internal/prepare-worker/artifacts'
def safe_path(path):
path = Path(path)
if not path.is_absolute() or '..' in path.parts:
raise ValueError('Absolute canonical path required')
for part in [*reversed(path.parents), path]:
if part.is_symlink():
raise ValueError('Symlink path rejected')
if part.exists() and not part.is_dir():
raise ValueError('Directory required')
return path
def write_private(path, value, uid, gid):
fd = os.open(path, os.O_WRONLY | os.O_CREAT | os.O_EXCL | os.O_NOFOLLOW, 0o400)
with os.fdopen(fd, 'w') as f:
f.write(value)
f.flush(); os.fsync(f.fileno())
os.fchown(f.fileno(), uid, gid)
def provision(root, uid, gid):
root = safe_path(root)
if root.exists():
# Idempotence never silently rotates or repairs potentially compromised state.
validate(root, uid, gid)
return
if not root.parent.is_dir():
raise ValueError('Provision parent must already exist')
staging = Path(tempfile.mkdtemp(prefix='.stage-provision-', dir=root.parent))
try:
for name, mode, owner in [('secrets', 0o700, uid), ('credentials', 0o700, os.getuid()), ('artifacts', 0o700, uid), ('snapshots', 0o700, os.getuid())]:
p = staging / name; p.mkdir(mode=mode); os.chown(p, owner, gid)
values = {name: secrets.token_urlsafe(48) for name in ['hermes_username', 'hermes_password', 'hermes_signing_key', 'prepare_worker_token', 'stage_ci_token']}
config = {'grants': [
{'id': 'stage-prepare-worker', 'role': 'WORKFLOW_WORKER', 'credentialSha256': hashlib.sha256(values['prepare_worker_token'].encode()).hexdigest()},
{'id': 'stage-evidence-ci', 'role': 'STAGE_CI', 'credentialSha256': hashlib.sha256(values['stage_ci_token'].encode()).hexdigest()}],
'workerIds': ['stage-prepare-worker'], 'stageCiIds': ['stage-evidence-ci'], 'artifactBaseUrl': BASE_URL}
for name in ['hermes_username', 'hermes_password', 'hermes_signing_key']:
write_private(staging / 'secrets' / name, values[name], uid, gid)
for name in ['prepare_worker_token', 'stage_ci_token']:
write_private(staging / 'credentials' / name, values[name], os.getuid(), os.getgid())
for name, value in [('hermes_runtime_grants', {}), ('prepare_worker_grants', config)]:
write_private(staging / 'secrets' / name, json.dumps(value), uid, gid)
# Root traversal only; each child controls its own readers.
staging.chmod(0o711)
validate(staging, uid, gid)
# Serialized CI invocation required; refuse a concurrent winner.
os.rename(staging, root)
finally:
if staging.exists(): shutil.rmtree(staging)
def validate(root, uid, gid):
expected = {'secrets', 'credentials', 'artifacts', 'snapshots'}
if set(p.name for p in root.iterdir()) != expected or root.stat().st_mode & 0o077 != 0o011:
raise ValueError('Unexpected provision root')
for folder in expected:
p = safe_path(root / folder); st = p.stat()
if stat.S_IMODE(st.st_mode) != 0o700 or st.st_uid != (uid if folder in {'secrets', 'artifacts'} else os.getuid()):
raise ValueError('Unsafe provision directory')
names = {'hermes_username', 'hermes_password', 'hermes_signing_key', 'hermes_runtime_grants', 'prepare_worker_grants'}
if {p.name for p in (root / 'secrets').iterdir()} != names:
raise ValueError('Unexpected secrets')
if {p.name for p in (root / 'credentials').iterdir()} != {'prepare_worker_token', 'stage_ci_token'}:
raise ValueError('Unexpected credentials')
for directory, owner in [('secrets', uid), ('credentials', os.getuid())]:
for p in (root / directory).iterdir():
st = p.lstat()
if not stat.S_ISREG(st.st_mode) or st.st_nlink != 1 or st.st_uid != owner or stat.S_IMODE(st.st_mode) != 0o400 or not 1 <= st.st_size <= 16384:
raise ValueError('Unsafe credential file')
for name in ['hermes_username', 'hermes_password', 'hermes_signing_key']:
if not re.fullmatch('[A-Za-z0-9_-]{64}', (root / 'secrets' / name).read_text()):
raise ValueError('Invalid dedicated credential')
config = json.loads((root / 'secrets/prepare_worker_grants').read_text())
if set(config) != {'grants', 'workerIds', 'stageCiIds', 'artifactBaseUrl'} or len(config['grants']) not in (2, 3):
raise ValueError('Unexpected worker config')
for index, name in enumerate(['prepare_worker_token', 'stage_ci_token']):
grant = config['grants'][index]
if set(grant) != {'id', 'role', 'credentialSha256'} or grant['id'] != ['stage-prepare-worker', 'stage-evidence-ci'][index]:
raise ValueError('Unexpected worker identity')
if not re.fullmatch('[A-Za-z0-9_-]{64}', (root / 'credentials' / name).read_text()):
raise ValueError('Invalid service credential')
if hashlib.sha256((root / 'credentials' / name).read_bytes()).hexdigest() != grant['credentialSha256']:
raise ValueError('Credential digest mismatch')
editorial = len(config['grants']) == 3
if editorial:
grant = config['grants'][2]
if set(grant) != {'id', 'role', 'credentialSha256'} or grant['id'] != 'pmo' or grant['role'] != 'WORKFLOW_WORKER' or not re.fullmatch('[0-9a-f]{64}', grant['credentialSha256']) or grant['credentialSha256'] in [g['credentialSha256'] for g in config['grants'][:2]]:
raise ValueError('Unexpected PMO grant')
if config['workerIds'] != ['stage-prepare-worker'] + (['pmo'] if editorial else []) or config['stageCiIds'] != ['stage-evidence-ci'] or config['artifactBaseUrl'] != BASE_URL or [g['role'] for g in config['grants'][:2]] != ['WORKFLOW_WORKER', 'STAGE_CI']:
raise ValueError('Unexpected grants')
if json.loads((root / 'secrets/hermes_runtime_grants').read_text()) != {}:
raise ValueError('Runtime grants require separate verified-ID review')
def export_snapshot(database, destination, profile, uid, gid):
"""SQLite online backup -> explicit metadata projection; raw backup never leaves RAM."""
import sqlite3
import time
from datetime import datetime, timezone
if not re.fullmatch('[a-zA-Z0-9][a-zA-Z0-9_-]{0,63}', profile):
raise ValueError('Invalid profile')
database = Path(database)
safe_path(database.parent)
st = database.lstat()
if not stat.S_ISREG(st.st_mode) or st.st_nlink != 1:
raise ValueError('Regular unlinked database required')
destination = safe_path(destination)
if destination.exists() or not destination.parent.is_dir():
raise ValueError('New snapshot destination required')
# mode=ro includes committed WAL frames. Never immutable=1 on a live WAL database.
src = sqlite3.connect(database.as_uri() + '?mode=ro', uri=True, timeout=5)
backup = sqlite3.connect(':memory:')
deadline = time.monotonic() + 30
def progress(status, remaining, total):
if time.monotonic() > deadline: raise ValueError('Backup deadline exceeded')
try:
src.backup(backup, pages=256, progress=progress, sleep=0.05)
captured = datetime.now(timezone.utc).isoformat().replace('+00:00', 'Z')
# Fail closed on schema drift. No SELECT *, private IDs, titles, prompts, messages,
# config, provider credentials, cron commands, filesystem paths or attachments.
rows = backup.execute('SELECT source,started_at,input_tokens,output_tokens,estimated_cost_usd FROM sessions ORDER BY started_at').fetchall()
if len(rows) > 100000: raise ValueError('Inventory exceeds bound')
allowed_sources = {'cli', 'telegram', 'discord', 'slack', 'api', 'web', 'cron'}
sessions = []
for index, (source, started, inputs, outputs, cost) in enumerate(rows):
import math
numbers = [started, inputs or 0, outputs or 0, cost or 0]
if any(not isinstance(n, (int, float)) or not math.isfinite(n) or n < 0 for n in numbers):
raise ValueError('Invalid inventory numeric metadata')
sessions.append({'id': 'snapshot-' + str(index), 'profile': profile,
'source': source if source in allowed_sources else 'other',
'startedAt': started, 'inputTokens': inputs or 0,
'outputTokens': outputs or 0, 'estimatedCost': cost or 0,
'is_active': False})
inventory = {'schemaVersion': 1, 'profile': profile, 'sessions': sessions}
payload = json.dumps(inventory, allow_nan=False, separators=(',', ':'))
if len(payload.encode()) > 32 * 1024 * 1024: raise ValueError('Inventory exceeds bound')
finally:
backup.close(); src.close()
staging = Path(tempfile.mkdtemp(prefix='.snapshot-', dir=destination.parent))
try:
manifest = {'schemaVersion': 1, 'source': 'snapshot', 'capturedAt': captured}
report = {'schemaVersion': 1, 'method': 'sqlite-online-backup-to-memory-allowlist-projection',
'capturedAt': captured, 'sessions': len(sessions),
'inventorySha256': hashlib.sha256(payload.encode()).hexdigest(),
'excluded': ['credentials', 'config', 'prompts', 'transcripts', 'private-identities', 'cron-commands'],
'liveExecutor': False}
for name, value in [('inventory.json', payload), ('snapshot-manifest.json', json.dumps(manifest)), ('export-report.json', json.dumps(report))]:
write_private(staging / name, value, uid, gid)
os.chown(staging, uid, gid)
staging.chmod(0o500)
os.rename(staging, destination)
finally:
if staging.exists():
staging.chmod(0o700); shutil.rmtree(staging)
def ci_gate():
sha = os.environ.get('STAGE_PROVISION_APPROVED_SHA', '')
if os.environ.get('CI') != 'true' or os.environ.get('STAGE_PROVISION_ENABLED') != 'true' or not re.fullmatch('[0-9a-f]{40}', sha):
raise ValueError('Approved Stage CI invocation required')
repo = Path(__file__).resolve().parents[1]
def git(*args):
return subprocess.check_output(['git', '-C', str(repo), *args], text=True).strip()
if git('rev-parse', 'HEAD') != sha or git('status', '--porcelain'):
raise ValueError('Clean approved commit required')
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('--root', type=Path, default=Path('/opt/services/canina/stage-runtime'))
parser.add_argument('--uid', type=int, default=1000)
parser.add_argument('--gid', type=int, default=1000)
parser.add_argument('--snapshot-db', type=Path)
parser.add_argument('--snapshot-name')
parser.add_argument('--profile', default='default')
args = parser.parse_args()
ci_gate()
if args.uid <= 0 or args.gid <= 0: raise ValueError('Non-root runtime identity required')
if bool(args.snapshot_db) != bool(args.snapshot_name) or (args.snapshot_name and not re.fullmatch('[a-zA-Z0-9_-]{1,80}', args.snapshot_name)):
raise ValueError('Paired snapshot database and safe unique name required')
provision(args.root, args.uid, args.gid)
if args.snapshot_db:
export_snapshot(args.snapshot_db, args.root / 'snapshots' / args.snapshot_name, args.profile, args.uid, args.gid)
print('Stage credentials and artifact storage validated; no services started')
if __name__ == '__main__':
try: main()
except Exception: raise SystemExit('Stage provisioning rejected (details suppressed)')