"""Explicit security/fault campaign scoped to mtusi-lab7. No secrets in evidence.""" import json import os from pathlib import Path import subprocess import sys import time ROOT=Path(__file__).resolve().parents[1] sys.path.insert(0,str(ROOT/'tests')) from client import Client,Failure,check BASE='https://localhost:8443' CA=str(ROOT/'pki/ca.crt') DC=['docker','compose','--env-file','.env'] def command(args,name=None): p=subprocess.run(args,cwd=ROOT,capture_output=True,text=True,encoding='utf-8',errors='replace',timeout=300) if name:(ROOT/'evidence'/name).write_text(p.stdout+p.stderr,encoding='utf-8') check(p.returncode==0,'command failed: '+' '.join(args)+'\n'+p.stdout+p.stderr) return p.stdout def compose(*args):return command(DC+list(args)) def operator(script,*args):return compose('run','--rm','--no-deps','operator','python',script,*args) def save(name,data):(ROOT/'evidence'/name).write_text(json.dumps(data,ensure_ascii=False,indent=2),encoding='utf-8') def wait(fn,timeout=100): until=time.monotonic()+timeout while time.monotonic() bounded grace -> 503 -> operator unseal -> recovery',flush=True) # Simulated compromise: worker identity is revoked, accepted work stays durable. compose('stop','-t','1','worker') pending=[c.attachment(a,chat) for _ in range(3)] before=snapshot() operator('student-tests/lifecycle.py','revoke') compose('start','worker') time.sleep(3) metadata=[c.call('GET','/api/v1/attachments/'+att['id'],actor=a,schema='Attachment')['json'] for att,raw in pending] check(all(x['status']=='queued' for x in metadata),'revoked worker processed job') compose('stop','-t','1','worker') operator('student-tests/lifecycle.py','rotate') compose('run','--rm','s3-bootstrap') operator('student-tests/lifecycle.py','old-denied') restored=time.monotonic() compose('start','worker') for att,raw in pending: ready=c.wait_ready(a,att) check(c.call('GET','/api/v1/attachments/'+att['id']+'/content',actor=a)['bytes']==raw,'accepted bytes lost') check(len(ready['variants'])==3,'incomplete result') after=snapshot() check(before['worker']['image']==after['worker']['image'],'rotation rebuilt image') save('worker-compromise.json',{'result':'PASS','accepted_jobs':3,'queued_while_revoked':True, 'ready_after_rotation':3,'originals_verified':3,'variants_each':3, 'recovery_seconds':round(time.monotonic()-restored,3),'image_unchanged':True, 'hypothesis':'Scoped worker loses authority to unrelated secrets, user/session/message DB and producer/admin broker operations; revocation blocks new worker connections while durable jobs survive.', 'before_worker':before['worker'],'after_worker':after['worker'], 'limitation':'Authorized worker can still alter allowed image results and job state. Revocation is bounded by cache/active connection lifetime; host/operator compromise is outside this model.'}) print('PASS: compromised worker revoked; leaf and three credentials rotated; 3 jobs preserved',flush=True) finally: operator('scripts/bootstrap.py','unseal') compose('start','api1','api2','worker') suffix='.cmd' if os.name=='nt' else '.sh' save('before-restart.json',snapshot()) command([sys.executable,'-X','utf8','tests/resilience.py','--mode','persistence','--action-script',str(ROOT/'scripts'/('restart'+suffix)),'--base-url',BASE,'--ca-file',CA],'persistence.txt') save('after-restart.json',snapshot()) print('PASS: public HTTPS persistence',flush=True) try: command([sys.executable,'-X','utf8','tests/resilience.py','--mode','failover','--action-script',str(ROOT/'scripts'/('stop-one'+suffix)),'--base-url',BASE,'--ca-file',CA],'failover.txt') finally: compose('start','api1','api2') compose('up','-d','--no-deps','--force-recreate','api1-tls','api2-tls') wait(lambda:c.call('GET','/health/ready',schema='Health')) command([sys.executable,'-X','utf8','tests/smoke.py','--base-url',BASE,'--ca-file',CA],'final-smoke.txt') save('final-containers.json',snapshot()) save('verification.json',{'result':'PASS','HTTPS_smoke':True,'public_TLS_negative':True,'internal_mTLS':True, 'scoped_roles':True,'ACL_sessions_rehash_rate_limit':True,'Bao_sealed_fail_closed':True, 'secret_and_leaf_rotation':True,'worker_compromise_track':True,'persistence':True,'failover':True}) print('PASS: lab7 security campaign complete',flush=True) if __name__=='__main__':main()