diff --git a/infra/hermes-editorial/client.py b/infra/hermes-editorial/client.py new file mode 100644 index 00000000..371aa57e --- /dev/null +++ b/infra/hermes-editorial/client.py @@ -0,0 +1,112 @@ +#!/usr/bin/env python3 +"""Bounded PMO proposal only. No approval, execution, cron or publication API.""" +import argparse +import fcntl +import json +import os +from pathlib import Path +import re +import stat +import tempfile +import uuid +import urllib.error +import urllib.request + +ENDPOINT = 'https://stageapi.canina.ir/api/internal/prepare-worker/editorial-proposals' + +class ClientError(Exception): + pass + +class NoRedirect(urllib.request.HTTPRedirectHandler): + def redirect_request(self, req, fp, code, msg, headers, newurl): + return None + + +def safe(path): + if not path.is_absolute() or '..' in path.parts: + raise ValueError('Absolute home required') + for p in (path, *path.parents): + if p.is_symlink(): raise ValueError('Symlink refused') + return path + + +def private_read(path): + safe(path) + fd = os.open(path, os.O_RDONLY | os.O_NOFOLLOW | os.O_NONBLOCK) + with os.fdopen(fd, 'rb') as stream: + s = os.fstat(stream.fileno()) + if not stat.S_ISREG(s.st_mode) or s.st_uid != os.getuid() or s.st_nlink != 1 or stat.S_IMODE(s.st_mode) not in (0o400, 0o600) or not 0 < s.st_size <= 16384: + raise ValueError('Private runtime-owned regular file required') + return stream.read(16385) + + +def atomic(path, data): + fd, name = tempfile.mkstemp(prefix='.pmo-', dir=path.parent) + try: + with os.fdopen(fd, 'wb') as stream: + stream.write(data); stream.flush(); os.fsync(stream.fileno()) + os.chmod(name, 0o400); os.replace(name, path) + finally: + if os.path.exists(name): os.unlink(name) + + +def submit(home, commit, proposal, *, endpoint=ENDPOINT, timeout=15): + # endpoint override is an in-process disposable-test seam, never CLI/config input. + safe(home) + if not re.fullmatch('[0-9a-f]{40}', commit): raise ValueError('Full deployment SHA required') + if not isinstance(proposal, dict) or set(proposal) != {'researchTopic', 'acceptanceCriteria'}: + raise ValueError('Only owner topic and acceptanceCriteria accepted') + topic, criteria = proposal['researchTopic'], proposal['acceptanceCriteria'] + if not isinstance(topic, str) or not topic.strip() or len(topic) > 180 or not isinstance(criteria, list) or not 1 <= len(criteria) <= 10 or any(not isinstance(v, str) or not v.strip() or len(v) > 500 for v in criteria): + raise ValueError('Invalid proposal') + token = private_read(home/'credentials/editorial-pmo-token').decode('ascii') + if not re.fullmatch('[A-Za-z0-9_-]{64}', token): raise ValueError('Invalid credential') + directory = safe(home/'editorial-v4/client-state') + directory.mkdir(mode=0o700, parents=True, exist_ok=True) + if directory.stat().st_uid != os.getuid() or stat.S_IMODE(directory.stat().st_mode) != 0o700: + raise ValueError('Private state directory required') + lock = safe(directory/'lock') + fd = os.open(lock, os.O_CREAT | os.O_RDWR | os.O_NOFOLLOW, 0o600) + with os.fdopen(fd, 'a') as stream: + fcntl.flock(stream, fcntl.LOCK_EX | fcntl.LOCK_NB) + body = {**proposal, 'pilotKind':'editorial-research-to-brief-v1', 'operationMode':'PREPARE', 'environment':'staging', 'scope':'EDITORIAL_RESEARCH_TO_BRIEF', 'commitSha':commit, 'budgetLimitCents':20} + state = safe(directory/'proposal.json') + if state.exists(): + stored = json.loads(private_read(state)) + request_id = stored.get('requestId') + if str(uuid.UUID(request_id)) != request_id or {k:v for k,v in stored.items() if k != 'requestId'} != body: + raise ValueError('Existing request differs; explicit owner review required') + body = stored + else: + body['requestId'] = str(uuid.uuid4()) + atomic(state, json.dumps(body, sort_keys=True).encode()) + req = urllib.request.Request(endpoint, data=json.dumps(body).encode(), headers={'Authorization':'Bearer '+token, 'Content-Type':'application/json'}, method='POST') + opener = urllib.request.build_opener(urllib.request.ProxyHandler({}), NoRedirect()) + try: + with opener.open(req, timeout=timeout) as response: + if response.status != 201: raise ClientError('Unexpected proposal status') + raw = response.read(16385) + if len(raw)>16384: raise ClientError('Response exceeds bound') + result = json.loads(raw) + if not isinstance(result, dict) or not isinstance(result.get('taskId'), str): raise ClientError('Invalid proposal response') + # Do not reflect arbitrary backend response bodies or credential material. + return {'requestId':body['requestId'], 'taskId':result['taskId']} + except (urllib.error.URLError, TimeoutError, OSError, ValueError) as error: + code = getattr(error, 'code', None) + if isinstance(error, urllib.error.HTTPError): error.close() + raise ClientError('Proposal rejected HTTP '+str(code) if isinstance(code,int) else 'Proposal unavailable; retry same saved request') from None + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument('--proposal-file', type=Path, required=True) + args = parser.parse_args() + try: + home = Path(os.environ['HERMES_HOME']) + deployment = json.loads(private_read(home/'editorial-v4/deployment.json')) + proposal = json.loads(private_read(args.proposal_file)) + print(json.dumps(submit(home, deployment['commitSha'], proposal), sort_keys=True)) + except (OSError, ValueError, KeyError, ClientError): + parser.exit(1, 'PMO proposal failed (private details suppressed); saved request retained\n') + +if __name__ == '__main__': main() diff --git a/infra/hermes-editorial/runtime-wiring.md b/infra/hermes-editorial/runtime-wiring.md new file mode 100644 index 00000000..26e2f367 --- /dev/null +++ b/infra/hermes-editorial/runtime-wiring.md @@ -0,0 +1,44 @@ +# Stage editorial runtime wiring + +This slice is executable but does not itself deploy or submit a live proposal. Parent must promote this commit through the existing authenticated, serialized Stage CI. No Production, cron activation, approval, executor or publishing changes. + +`stage_runtime_provision` runs disposable tests and the clean approved-SHA gate before provisioning. It inspects the persistent target home and streams the reviewed Python source plus an exact 22-file package (installer, client, manifest and 19 allowlisted payloads) through `docker exec -i canina-hermes python3 -B`. It uses the container's configured identity, never host-UID writes into the home. The fixed package directory is `$HERMES_HOME/editorial-v4/runtime-package` (0700); source/package files and deployment marker are 0400. The 19 profile/status files are installed and verified by the existing installer; the six skill paths are checked by actual `hermes skills list` calls under each profile home. No profile config.yaml is replaced. + +A dedicated random 64-character PMO credential is generated within the target at `credentials/editorial-pmo-token` (0400, runtime owner). Existing runtime-owned credentials directories may be 0700 or non-writable-by-others 0755 without changing unrelated files. Only the digest crosses back to CI. CI atomically extends the fixed backend four-field secret, preserving existing worker/CI identities and digests; conflicts fail closed. Backend grants remain 0400 and owned by its mapped UID/GID. Repeat Stage provisioning validates the optional canonical third PMO grant rather than rejecting its own installation. Clean approved SHA is checked again at the wiring entrypoint. + +## Verify after successful Stage CI + +On the remote host: + +```sh +docker exec canina-hermes sh -c 'python3 -B "$HERMES_HOME/editorial-v4/runtime-package/installer.py" verify --target-home "$HERMES_HOME"' +for profile in pmo canina-competitor-worker canina-seo-worker canina-content-worker canina-vet-review-worker canina-qa-worker; do + docker exec -e HERMES_HOME="/opt/data/profiles/$profile" canina-hermes /opt/hermes/bin/hermes skills list +done +``` + +Target home `/opt/data` was confirmed read-only during development. CI derives it from inspect rather than hardcoding it. These commands are not evidence that installation happened before promotion. + +## Submit FROM target only + +Owner/PMO supplies a private runtime-owned 0400 JSON file, e.g. `/opt/data/editorial-v4/owner-proposal.json`, containing exactly `researchTopic` and `acceptanceCriteria` as described in `backend/src/orchestration-worker/editorial-bridge.md`. No sample topic/content is authored by this provisioning slice. + +```sh +docker exec canina-hermes sh -c 'python3 -B "$HERMES_HOME/editorial-v4/runtime-package/client.py" --proposal-file "$HERMES_HOME/editorial-v4/owner-proposal.json"' +``` + +The CLI accepts no URL, credential, actor, budget or commit override. It reads the deployment SHA installed by the approved CI rollout and posts only to `https://stageapi.canina.ir/api/internal/prepare-worker/editorial-proposals`, disables redirects and ambient proxies, sends Bearer PMO authentication, and fixes PREPARE/staging/editorial scope/pilot/budget20. Only taskId/requestId are returned. Confirm successful deployed SHA in CI before submitting. The stable request body is saved privately in `editorial-v4/client-state/proposal.json` before the first HTTP attempt. Retry the same invocation after timeout, denial or unknown outcome. Changed immutable proposal/SHA is refused; do not delete the state to retry an ambiguous request. This client intentionally represents one bounded pilot; starting a different proposal requires separate owner review, not automatic state reset. + +The proposal endpoint creates Gate1 pending only; genuine independent authenticated panel review is still required. This commit performs no live approval and claims no ten-step editorial execution. + +## Tests + +```sh +PYTHONDONTWRITEBYTECODE=1 python3 -m unittest discover -s scripts/tests +PYTHONDONTWRITEBYTECODE=1 python3 -m unittest discover -s infra/hermes-editorial/tests +PYTHONDONTWRITEBYTECODE=1 python3 -m unittest discover -s scripts -p test_editorial_provision.py +bash -n scripts/rollout-stage.sh +git diff --check +``` + +Verified 15 existing script tests, six installer/client tests and one real disposable runtime-install/grant test. HTTP tests exercise a real local TCP server (401/403/409/redirect denial, bounded timeout, identical retry body and changed-SHA rejection). Stage wiring tests execute streamed source against a private home and real Compose rendering, preserving existing grants over repeat provisioning; Docker/DB reads are isolated fixtures and no live install occurs in tests. diff --git a/infra/hermes-editorial/tests/test_client.py b/infra/hermes-editorial/tests/test_client.py new file mode 100644 index 00000000..ea333f4e --- /dev/null +++ b/infra/hermes-editorial/tests/test_client.py @@ -0,0 +1,57 @@ +import importlib.util +import json +from pathlib import Path +import tempfile +import threading +import time +import unittest +from http.server import BaseHTTPRequestHandler, HTTPServer + +ROOT = Path(__file__).resolve().parents[1] + +class ClientTests(unittest.TestCase): + def test_client_exists(self): + self.assertTrue((ROOT / 'client.py').is_file(), 'executable PMO client missing') + + def test_http_boundary_and_stable_request(self): + spec = importlib.util.spec_from_file_location('client', ROOT / 'client.py') + c = importlib.util.module_from_spec(spec); spec.loader.exec_module(c) + received = [] + class Handler(BaseHTTPRequestHandler): + status = 201 + def do_POST(self): + received.append((self.path, self.headers['Authorization'], json.loads(self.rfile.read(int(self.headers['Content-Length']))))) + if self.status == 0: + time.sleep(0.15) + return + self.send_response(self.status) + if self.status == 302: self.send_header('Location', '/leaked') + self.end_headers(); self.wfile.write(b'{"taskId":"fixture-task"}') + def log_message(self, *args): pass + server = HTTPServer(('127.0.0.1', 0), Handler) + thread = threading.Thread(target=server.serve_forever, daemon=True); thread.start() + try: + with tempfile.TemporaryDirectory() as tmp: + home = Path(tmp); (home/'credentials').mkdir(mode=0o700) + token = home/'credentials/editorial-pmo-token'; token.write_text('x'*64); token.chmod(0o400) + proposal = {'researchTopic':'Owner supplied topic', 'acceptanceCriteria':['Cite dated sources']} + endpoint = 'http://127.0.0.1:%s/api/internal/prepare-worker/editorial-proposals' % server.server_port + a = c.submit(home, 'a'*40, proposal, endpoint=endpoint) + b = c.submit(home, 'a'*40, proposal, endpoint=endpoint) + self.assertEqual(a,b); self.assertEqual(received[0][2],received[1][2]) + self.assertEqual(received[0][2]['budgetLimitCents'],20) + self.assertEqual(received[0][1], 'Bearer '+'x'*64) + with self.assertRaises(ValueError): c.submit(home,'b'*40,proposal,endpoint=endpoint) + for status in (401,403,409,302): + Handler.status=status + with self.assertRaises(c.ClientError): c.submit(home,'a'*40,proposal,endpoint=endpoint) + self.assertEqual(len(received),6) + Handler.status=0 + with self.assertRaises(c.ClientError): c.submit(home,'a'*40,proposal,endpoint=endpoint,timeout=0.02) + self.assertEqual(received[-1][2]['requestId'],received[0][2]['requestId']) + token.chmod(0o644) + with self.assertRaises(ValueError): c.submit(home,'a'*40,proposal,endpoint=endpoint) + finally: + server.shutdown(); server.server_close(); thread.join() + +if __name__ == '__main__': unittest.main() diff --git a/scripts/provision-editorial-runtime.py b/scripts/provision-editorial-runtime.py new file mode 100644 index 00000000..243ea076 --- /dev/null +++ b/scripts/provision-editorial-runtime.py @@ -0,0 +1,123 @@ +#!/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'} | {'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) + # 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/'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'],'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') + # No --user override: Docker executes as configured runtime identity, not host UID. + 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'],'','exec'),ns); print(json.dumps(ns['install'](ns['Path'](p['home']),p['payload'])))" + result=json.loads(docker('exec','-i','canina-hermes','python3','-B','-c',code,data=json.dumps({'source':source,'home':str(home),'payload':payload}))) + for profile in PROFILES: + discovered=docker('exec','-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') diff --git a/scripts/provision-stage-runtime.py b/scripts/provision-stage-runtime.py index 0c418c6d..902da8f0 100644 --- a/scripts/provision-stage-runtime.py +++ b/scripts/provision-stage-runtime.py @@ -89,7 +89,7 @@ def validate(root, uid, gid): 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']) != 2: + 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] @@ -99,7 +99,12 @@ def validate(root, uid, gid): raise ValueError('Invalid service credential') if hashlib.sha256((root / 'credentials' / name).read_bytes()).hexdigest() != grant['credentialSha256']: raise ValueError('Credential digest mismatch') - if config['workerIds'] != ['stage-prepare-worker'] or config['stageCiIds'] != ['stage-evidence-ci'] or config['artifactBaseUrl'] != BASE_URL or [g['role'] for g in config['grants']] != ['WORKFLOW_WORKER', 'STAGE_CI']: + 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') diff --git a/scripts/rollout-stage.sh b/scripts/rollout-stage.sh index efe39262..6eafc5ec 100644 --- a/scripts/rollout-stage.sh +++ b/scripts/rollout-stage.sh @@ -5,6 +5,8 @@ stage_runtime_provision() { [ "${GITHUB_ACTIONS:-${GITEA_ACTIONS:-}}" = true ] [ "${STAGE_PROVISION_ENABLED:-}" = true ] export DEPLOY_SHA STAGE_PROVISION_APPROVED_SHA="$DEPLOY_SHA" CI=true + PYTHONDONTWRITEBYTECODE=1 python3 -m unittest discover -s infra/hermes-editorial/tests + PYTHONDONTWRITEBYTECODE=1 python3 -m unittest discover -s scripts -p test_editorial_provision.py PYTHONDONTWRITEBYTECODE=1 python3 - <<'PY' import os, json, subprocess, importlib.util, resource, secrets, re, stat from pathlib import Path @@ -63,6 +65,9 @@ rows=json.loads(docker('exec','-w','/app','canino_backend_stage','node','-e',que if not 1<=len(rows)<=1000 or any(not isinstance(r,dict) or set(r)!={'id'} or not isinstance(r['id'],str) or not re.fullmatch('[0-9a-f]{8}-(?:[0-9a-f]{4}-){3}[0-9a-f]{12}',r['id']) for r in rows): raise ValueError('Verified admin subjects required') m.provision(root,uid,gid) +spec=importlib.util.spec_from_file_location('editorial','scripts/provision-editorial-runtime.py') +editorial=importlib.util.module_from_spec(spec);spec.loader.exec_module(editorial) +editorial.provision(os.environ['DEPLOY_SHA'],uid,gid) # Leave the validated deny-all sentinel unchanged for repeat provisioning. capture=os.environ['DEPLOY_SHA']+'-'+secrets.token_hex(8) dest=root/'snapshots'/capture diff --git a/scripts/test_editorial_provision.py b/scripts/test_editorial_provision.py new file mode 100644 index 00000000..b3150fd9 --- /dev/null +++ b/scripts/test_editorial_provision.py @@ -0,0 +1,35 @@ +import importlib.util +import json +import os +from pathlib import Path +import tempfile +import unittest + +ROOT=Path(__file__).resolve().parent +BUNDLE = ROOT/'hermes-editorial' if (ROOT/'hermes-editorial').exists() else ROOT.parent/'infra/hermes-editorial' + +def load(name, path): + spec=importlib.util.spec_from_file_location(name,path) + m=importlib.util.module_from_spec(spec);spec.loader.exec_module(m);return m + +class ProvisionTests(unittest.TestCase): + def test_runtime_install_and_credential_are_idempotent_and_private(self): + self.assertTrue((ROOT/'provision-editorial-runtime.py').exists(), 'CI editorial wiring missing') + m=load('editorial',ROOT/'provision-editorial-runtime.py') + with tempfile.TemporaryDirectory() as t: + home=Path(t); sentinel=home/'config.yaml';sentinel.write_text('unchanged') + payload=m.package(BUNDLE,'a'*40) + a=m.install(home,payload);b=m.install(home,payload) + self.assertEqual(a,b);self.assertEqual(sentinel.read_text(),'unchanged') + self.assertEqual((home/'credentials/editorial-pmo-token').stat().st_mode & 0o777,0o400) + self.assertEqual(a['files'],19) + grants={'grants':[{'id':'ci','role':'STAGE_CI','credentialSha256':'c'*64},{'id':'worker','role':'WORKFLOW_WORKER','credentialSha256':'d'*64}], 'workerIds':['worker'],'stageCiIds':['ci'],'artifactBaseUrl':'https://stageapi.canina.ir/api/internal/prepare-worker/artifacts'} + updated=m.add_grant(grants,a['credentialSha256']) + self.assertEqual(updated['grants'][:2],grants['grants']);self.assertEqual(len(updated['grants']),3) + self.assertEqual(m.add_grant(updated,a['credentialSha256']),updated) + with self.assertRaises(ValueError):m.add_grant(updated,'e'*64) + self.assertEqual(grants['workerIds'],['worker']) + (home/'credentials/editorial-pmo-token').chmod(0o644) + with self.assertRaises(ValueError):m.install(home,payload) + +if __name__=='__main__':unittest.main() diff --git a/scripts/tests/test_stage_provision_wiring.py b/scripts/tests/test_stage_provision_wiring.py index cebb3c38..b772749c 100644 --- a/scripts/tests/test_stage_provision_wiring.py +++ b/scripts/tests/test_stage_provision_wiring.py @@ -28,16 +28,18 @@ class WiringTests(unittest.TestCase): db.execute("INSERT INTO sessions VALUES ('cli',1,2,3,0.1,'PRIVATE TITLE')");db.commit();db.close() helper=repo/'scripts/rollout-stage.sh' helper.write_text(helper.read_text().replace('/opt/services/canina/stage-runtime',str(root))) + editorial=repo/'scripts/provision-editorial-runtime.py' + editorial.write_text(editorial.read_text().replace('/opt/services/canina/stage-runtime',str(root))) def git(*args):return subprocess.check_output(['git',*args],cwd=repo,text=True).strip() git('init','-q');git('config','user.email','fixture@example.invalid');git('config','user.name','Fixture');git('add','.');git('commit','-qm','fixture') sha=git('rev-parse','HEAD') subject='12345678-1234-1234-1234-123456789abc' - fixture={'backend':{'Config':{'User':'node'},'HostConfig':{'UsernsMode':''},'State':{'Running':True,'Pid':os.getpid()}},'hermes':{'Config':{'Env':['HERMES_HOME=/home/runtime']},'Mounts':[{'Type':'bind','Destination':'/home','Source':str(base)}]}} + fixture={'backend':{'Config':{'User':'node'},'HostConfig':{'UsernsMode':''},'State':{'Running':True,'Pid':os.getpid()}},'hermes':{'State':{'Running':True},'HostConfig':{'UsernsMode':''},'Config':{'Env':['HERMES_HOME=/home/runtime']},'Mounts':[{'Type':'bind','Destination':'/home','Source':str(base)}]}} # /home/runtime resolves to base/runtime; point home at the SQLite fixture. fixture['hermes']['Mounts'][0]['Source']=str(home) fixture['hermes']['Config']['Env']=['HERMES_HOME=/home'] (base/'inspect.json').write_text(json.dumps(fixture)) - (tools/'docker').write_text('#!/usr/bin/env python3\nimport os,sys,json\na=sys.argv[1:]\nif a[0]=="compose": os.execv("/usr/bin/docker",["docker",*a])\nf=json.load(open(os.environ["INSPECT"]))\nif a[0]=="inspect": print(json.dumps([f["backend" if a[1]=="canino_backend_stage" else "hermes"]]))\nelif a[0]=="exec":\n assert "SET TRANSACTION READ ONLY" in a[-1] and "role::text" in a[-1]\n print(json.dumps([{ "id":os.environ["SUBJECT"]}]))\nelse: raise RuntimeError(a)\n');(tools/'docker').chmod(0o755) + (tools/'docker').write_text('#!/usr/bin/env python3\nimport os,sys,json,subprocess\na=sys.argv[1:]\nif a[0]=="compose": os.execv("/usr/bin/docker",["docker",*a])\nf=json.load(open(os.environ["INSPECT"]))\nif a[0]=="inspect": print(json.dumps([f["backend" if a[1]=="canino_backend_stage" else "hermes"]]))\nelif a[0]=="exec" and "-i" in a:\n p=json.load(sys.stdin); p["home"]=f["hermes"]["Mounts"][0]["Source"]; subprocess.run([sys.executable,"-B","-c",a[-1]],input=json.dumps(p),text=True,check=True)\nelif a[0]=="exec" and "skills" in a:\n print("canina-editorial-v4")\nelif a[0]=="exec":\n assert "SET TRANSACTION READ ONLY" in a[-1] and "role::text" in a[-1]\n print(json.dumps([{ "id":os.environ["SUBJECT"]}]))\nelse: raise RuntimeError(a)\n');(tools/'docker').chmod(0o755) env=dict(os.environ,PATH=str(tools)+':'+os.environ['PATH'],INSPECT=str(base/'inspect.json'),SUBJECT=subject,GITHUB_ACTIONS='true',STAGE_PROVISION_ENABLED='true',PYTHONDONTWRITEBYTECODE='1') command='set -euo pipefail; source scripts/rollout-stage.sh; DEPLOY_SHA='+sha+'; unset STAGE_PROVISION_APPROVED_SHA; stage_runtime_provision' # DEPLOY_SHA is deliberately shell-local: helper must export it.