Files
canina/scripts/provision-editorial-runtime.py
T
2026-10-24 16:26:50 +00:00

131 lines
8.1 KiB
Python

#!/usr/bin/env python3
"""Serialized Stage CI only; stream reviewed bundle into actual Hermes identity."""
import base64
import copy
import hashlib
import importlib.util
import json
import os
from pathlib import Path
import re
import secrets
import stat
import subprocess
import tempfile
PROFILES=('pmo','canina-competitor-worker','canina-seo-worker','canina-content-worker','canina-vet-review-worker','canina-qa-worker')
FILES={'bundle.json','installer.py','client.py','runner.py','model-policy.json'} | {'payload/editorial-v4/status.json'} | {f'payload/profiles/{p}/skills/canina-editorial-v4/{f}' for p in PROFILES for f in ('SKILL.md','references/policy.md','references/role.json')}
def safe(path):
if not path.is_absolute() or '..' in path.parts: raise ValueError('Canonical path required')
for p in (path,*path.parents):
if p.is_symlink(): raise ValueError('Symlink refused')
return path
def private(path):
safe(path)
s=path.lstat()
if not stat.S_ISREG(s.st_mode) or s.st_nlink!=1 or s.st_uid!=os.getuid() or stat.S_IMODE(s.st_mode)!=0o400 or not 1<=s.st_size<=1048576:
raise ValueError('Private runtime file required')
return path.read_bytes()
def atomic(path, data, uid=None, gid=None):
safe(path)
fd,name=tempfile.mkstemp(prefix='.editorial-',dir=path.parent)
try:
with os.fdopen(fd,'wb') as f:
f.write(data);f.flush();os.fsync(f.fileno())
if uid is not None: os.fchown(f.fileno(),uid,gid)
os.chmod(name,0o400);os.replace(name,path)
finally:
if os.path.exists(name):os.unlink(name)
def package(root, sha):
if not re.fullmatch('[0-9a-f]{40}',sha):raise ValueError('Full deployment SHA required')
result={}
for name in FILES:
p=safe(root/name)
if p.stat().st_size>65536:raise ValueError('Package exceeds bound')
result[name]=base64.b64encode(p.read_bytes()).decode()
return {'commitSha':sha,'files':result}
def install(home, payload):
safe(home)
if set(payload)!={'commitSha','files'} or set(payload['files'])!=FILES or not re.fullmatch('[0-9a-f]{40}',payload['commitSha']):
raise ValueError('Unreviewed package')
# Fixed private package path; no archive extraction or arbitrary destinations.
root=safe(home/'editorial-v4/runtime-package')
root.mkdir(mode=0o700,parents=True,exist_ok=True)
if root.stat().st_uid!=os.getuid() or stat.S_IMODE(root.stat().st_mode)!=0o700:raise ValueError('Private package required')
for name,value in payload['files'].items():
path=safe(root/name);path.parent.mkdir(mode=0o700,parents=True,exist_ok=True)
data=base64.b64decode(value,validate=True)
if len(data)>65536:raise ValueError('Package exceeds bound')
atomic(path,data)
spec=importlib.util.spec_from_file_location('editorial_installer',root/'installer.py')
installer=importlib.util.module_from_spec(spec);spec.loader.exec_module(installer)
installer.execute('install',home)
report=installer.execute('verify',home)
model_profiles=installer.configure_models(home)
# Confirm all six skill discovery paths, without activating any scheduler.
for profile in PROFILES:
skill=home/f'profiles/{profile}/skills/canina-editorial-v4/SKILL.md'
if not skill.is_file() or 'name: canina-editorial-v4' not in skill.read_text():raise ValueError('Skill discovery failed')
credentials=safe(home/'editorial-v4/credentials');credentials.mkdir(mode=0o700,exist_ok=True)
st=credentials.stat()
if st.st_uid!=os.getuid() or stat.S_IMODE(st.st_mode) not in (0o700,0o755):raise ValueError('Runtime-owned non-writable credentials directory required')
token=safe(credentials/'editorial-pmo-token')
if token.exists():
value=private(token).decode('ascii')
if not re.fullmatch('[A-Za-z0-9_-]{64}',value):raise ValueError('Invalid PMO credential')
else:
value=secrets.token_urlsafe(48);atomic(token,value.encode())
atomic(home/'editorial-v4/deployment.json',json.dumps({'commitSha':payload['commitSha']}).encode())
return {'files':report['files'],'modelProfiles':model_profiles,'profiles':list(PROFILES),'credentialSha256':hashlib.sha256(value.encode()).hexdigest()}
def add_grant(config,digest):
if set(config)!={'grants','workerIds','stageCiIds','artifactBaseUrl'} or not re.fullmatch('[0-9a-f]{64}',digest):raise ValueError('Invalid grants')
out=copy.deepcopy(config)
desired={'id':'pmo','role':'WORKFLOW_WORKER','credentialSha256':digest}
existing=[g for g in out['grants'] if g['id']=='pmo']
if existing:
if existing!=[desired] or out['workerIds'].count('pmo')!=1 or 'pmo' in out['stageCiIds']:raise ValueError('Conflicting PMO grant')
return out
if any(g['credentialSha256']==digest for g in out['grants']) or 'pmo' in out['workerIds']+out['stageCiIds'] or len(out['grants'])>=32:raise ValueError('Ambiguous PMO grant')
out['grants'].append(desired);out['workerIds'].append('pmo')
return out
def provision(sha, uid, gid):
gate_path=Path(__file__).resolve().with_name('provision-stage-runtime.py')
spec=importlib.util.spec_from_file_location('stage_gate',gate_path)
gate=importlib.util.module_from_spec(spec);spec.loader.exec_module(gate)
gate.ci_gate()
if sha!=os.environ.get('STAGE_PROVISION_APPROVED_SHA'):raise ValueError('Deployment SHA mismatch')
def docker(*args, data=None):
return subprocess.check_output(['docker',*args],input=data,text=True,timeout=60)
h=json.loads(docker('inspect','canina-hermes'))[0]
if not h['State']['Running'] or h['HostConfig'].get('UsernsMode') not in ('','default'):raise ValueError('Reviewed running Hermes required')
env=dict(v.split('=',1) for v in h['Config']['Env'] if '=' in v)
home=Path(env.get('HERMES_HOME',''));safe(home)
if not any(home==Path(v['Destination']) or Path(v['Destination']) in home.parents for v in h['Mounts'] if v['Type'] in ('bind','volume')):raise ValueError('Persistent home required')
# Match the supervised Hermes/shim identity, NOT Docker Config.User (root).
# Preflight the exact reviewed migration in the container UID namespace.
identity=json.loads(docker('exec','canina-hermes','python3','-c',"import pwd,json; p=pwd.getpwnam('hermes'); print(json.dumps([p.pw_uid,p.pw_gid]))"))
if identity != [10000,10000]:raise ValueError('Canonical Hermes identity drift')
migration=Path(__file__).with_name('editorial-ownership.py').read_text()
migrate_code="import json,sys; p=json.load(sys.stdin); ns={'__name__':'ci_ownership'}; exec(compile(p['source'],'<ci-ownership>','exec'),ns); h=ns['Path'](p['home']); ns['migrate'](h,10000,10000,h/'.editorial-ownership-v1.json')"
docker('exec','--user','0:0','-i','canina-hermes','python3','-B','-c',migrate_code,data=json.dumps({'source':migration,'home':str(home)}))
source=Path(__file__).read_text()
payload=package(Path(__file__).resolve().parents[1]/'infra/hermes-editorial',sha)
code="import json,sys; p=json.load(sys.stdin); ns={'__name__':'editorial_stream'}; exec(compile(p['source'],'<ci-editorial>','exec'),ns); print(json.dumps(ns['install'](ns['Path'](p['home']),p['payload'])))"
result=json.loads(docker('exec','--user','hermes','-i','canina-hermes','python3','-B','-c',code,data=json.dumps({'source':source,'home':str(home),'payload':payload})))
for profile in PROFILES:
discovered=docker('exec','--user','hermes','-e','HERMES_HOME='+str(home/'profiles'/profile),'canina-hermes','/opt/hermes/bin/hermes','skills','list')
if 'canina-editorial-v4' not in discovered:raise ValueError('Hermes CLI skill discovery failed')
path=Path('/opt/services/canina/stage-runtime/secrets/prepare_worker_grants');safe(path)
st=path.lstat()
if not stat.S_ISREG(st.st_mode) or st.st_uid!=uid or st.st_gid!=gid or st.st_nlink!=1 or stat.S_IMODE(st.st_mode)!=0o400 or not 1<=st.st_size<=16384:raise ValueError('Unsafe backend grant file')
config=json.loads(path.read_bytes());updated=add_grant(config,result['credentialSha256'])
if updated!=config:atomic(path,json.dumps(updated).encode(),uid,gid)
print('Editorial: 19 files and six profile skill paths verified; dedicated PMO grant preserved; no workflow activated')