#!/usr/bin/env python3 """Repeatable synthetic worker compromise/recovery experiment. No credentials in results.""" import hashlib,importlib.util,json,statistics,subprocess,sys,time,urllib.error,uuid from pathlib import Path ROOT=Path(__file__).resolve().parents[1];sys.path.insert(0,str(ROOT/'scripts')) import security,bao spec=importlib.util.spec_from_file_location('rotation',ROOT/'scripts/rotate-worker.py');rotation=importlib.util.module_from_spec(spec);spec.loader.exec_module(rotation) def db(sql):return security.compose('exec','-T','postgres','psql','-U','messenger','-d','messenger','-At','-v','ON_ERROR_STOP=1','-c',sql).stdout.strip() def one(index): name,pw,token,user=security.actor();auth={'Authorization':'Bearer '+token} status,_,b=security.request('/api/v1/chats','POST',json.dumps({'title':'Synthetic recovery','member_ids':[user]}).encode(),dict(auth,**{'Content-Type':'application/json'}));assert status==201 chat=json.loads(b)['id'];security.compose('stop','worker') fixture=(ROOT/'tests/fixtures/sample.png').read_bytes();ids=[];latencies=[] # Work is accepted durably while the consumer is absent. for i in range(5): start=time.monotonic();status,_,b=security.request('/api/v1/chats/'+chat+'/attachments?filename=synthetic.png','POST',fixture,dict(auth,**{'Content-Type':'image/png'}));latencies.append(time.monotonic()-start);assert status==202;ids.append(json.loads(b)['id']) idsql=','.join("'"+i+"'" for i in ids) before=db('SELECT count(*) FROM attachment_jobs WHERE attachment_id IN ('+idsql+')');assert before=='5' restricted={m:security.probe(m) for m in ['db','s3','amqp']} # The broad DB profile from lab6 is measured without reading secrets or retaining changes. broad=db("BEGIN; SELECT count(*)=0 FROM (SELECT password_hash FROM users LIMIT 0) x; CREATE TABLE baseline_probe(id int); ROLLBACK;") broad_profiles={} for mode in ['s3','amqp']: args=['run','--rm','-T','--no-deps','--entrypoint','securityprobe','-e','BAO_ADDR=','-e','PROBE_BASELINE=1','-e','PROBE_MODE='+mode,'-e','S3_ACCESS_KEY_ID_FILE=/run/identity/s3_baseline_access','-e','S3_SECRET_ACCESS_KEY_FILE=/run/identity/s3_baseline_secret','-v',str(security.S/'operator-client')+':/run/identity:ro','worker'] broad_profiles[mode]=json.loads(security.compose(*args).stdout) start=time.monotonic();rotation.rotate(start=False) # Old worker auth is explicitly rejected even though its CA is still trusted. try:bao.call('auth/cert/login',{'name':'worker'},identity='worker-old') except urllib.error.HTTPError as e:assert e.code in [400,403] else:raise AssertionError('old worker can authenticate') security.probe('tls',target='https://otel-collector:9443/v1/traces',expect='deny',material='worker-old') security.probe('amqp-tls',expect='deny',material='worker-old') old_result=security.compose('run','--rm','-T','--no-deps','--entrypoint','securityprobe','-e','BAO_ADDR=','-e','PROBE_MODE=s3','-e','PROBE_OLD_SECRET=1','-e','S3_ACCESS_KEY_ID_FILE=/run/old_access','-e','S3_SECRET_ACCESS_KEY_FILE=/run/old_secret','-v',str(security.S/'worker-old/old_access')+':/run/old_access:ro','-v',str(security.S/'worker-old/old_secret')+':/run/old_secret:ro','worker').stdout assert json.loads(old_result)['old_s3_secret_rejected'] security.compose('up','-d','worker') errors=0 for _ in range(90): statuses=[] for aid in ids: status,_,b=security.request('/api/v1/attachments/'+aid,headers=auth) if status!=200:errors+=1;continue statuses.append(json.loads(b)['status']) if len(statuses)==5 and set(statuses)=={'ready'}:break time.sleep(1) else:raise AssertionError('accepted jobs did not recover') recovery=time.monotonic()-start sha=hashlib.sha256(fixture).hexdigest() for aid in ids: status,_,b=security.request('/api/v1/attachments/'+aid+'/content',headers=auth);assert status==200 and hashlib.sha256(b).hexdigest()==sha total,unique=db('SELECT count(*),count(DISTINCT attachment_id) FROM attachment_jobs WHERE attachment_id IN ('+idsql+')').split('|');assert total==unique=='5' effects=db('SELECT count(*) FROM attachment_variants WHERE attachment_id IN ('+idsql+')');assert effects=='20',effects samples=security.compose('stats','--no-stream','--format','json').stdout return {'repeat':index,'accepted':5,'ready':5,'lost':0,'duplicate_jobs':0,'variant_records':int(effects),'original_checksums_match':True,'old_identity_rejected':True,'recovery_seconds':recovery,'observation_errors':errors,'upload_latency_ms':{'p50':statistics.median(latencies)*1000,'max':max(latencies)*1000},'restricted_probes':restricted,'broad_db_read_and_ddl':True,'broad_profiles':broad_profiles,'old_s3_key_rejected':True,'old_broker_identity_rejected':True,'resource_snapshot':[json.loads(x) for x in samples.splitlines() if x.startswith('{')]} def main(): results=[] for i in range(1,4): results.append(one(i));(ROOT/'evidence/worker-experiment.json').write_text(json.dumps({'hypothesis':'Scoped worker identity limits DB/S3/broker access; revocation and replacement preserve accepted benign jobs.','timestamp':time.strftime('%Y-%m-%dT%H:%M:%SZ',time.gmtime()),'repeats':results,'limitations':['Authorized worker can damage its permitted job metadata and object prefixes.','This experiment does not claim integrity after malicious modification of those authorized resources.','Single Docker host; no host/DB/LB high availability.']},indent=2)+'\n');print('PASS: worker compromise/recovery repeat',i) if __name__=='__main__':main()