#!/usr/bin/env python3 """Inspect actual runtime controls; verify no native plaintext bypass or leakage.""" import importlib.util,json,os,subprocess,time from pathlib import Path ROOT=Path(__file__).resolve().parents[1] s=importlib.util.spec_from_file_location('sec',ROOT/'student-tests/security.py');sec=importlib.util.module_from_spec(s);s.loader.exec_module(sec) def main(): rows=[];sensitive=[] credentials=json.loads((ROOT/'secrets/operator/credentials.json').read_text());sensitive += [credentials['postgres_admin'],credentials['s3_root_password'],credentials['cursor_key']] for role in ['api','worker','dispatcher','housekeeping']:sensitive += [credentials[role][k] for k in ['db_password','broker_password','cache_password','s3_secret_access_key']] material=json.loads((ROOT/'secrets/operator/init.json').read_text());sensitive += [material['root_token'],*material['keys']] apps={'api','worker','dispatcher','housekeeping','broker','cache','s3','collector','prometheus','loki','tempo','grafana'} for id in sec.dc('ps','-q').splitlines(): item=json.loads(subprocess.check_output(['docker','inspect',id],text=True))[0];name=item['Config']['Labels']['com.docker.compose.service'];host=item['HostConfig'] assert not host['Privileged'] and host['NetworkMode']!='host' assert not any('docker.sock' in m['Source'] or m['Destination']=='/operator' for m in item['Mounts']) assert not any(value in json.dumps(item['Config'].get('Env',[])) for value in sensitive) if name in apps: assert item['Config']['User']=='10001:10001' and host['ReadonlyRootfs'] and 'ALL' in host['CapDrop'] and 'no-new-privileges:true' in host['SecurityOpt'] for bindings in (host.get('PortBindings') or {}).values(): for b in bindings or []:assert name in ['lb','openbao','grafana'] and b['HostIp']=='127.0.0.1' rows.append({'service':name,'user':item['Config']['User'],'read_only':host['ReadonlyRootfs'],'drop_capabilities':host['CapDrop'],'add_capabilities':host['CapAdd'],'security_options':host['SecurityOpt'],'memory_limit_bytes':host['Memory'],'nano_cpus_limit':host['NanoCpus'],'image_id':item['Image'],'started_at':item['State']['StartedAt'],'published_ports':host.get('PortBindings')}) denied=[] for service,port in [('api',8081),('broker',6380),('cache',6380),('s3',9001),('s3',9002),('collector',4319),('collector',8890),('prometheus',9091),('loki',3101),('tempo',3201),('grafana',3001)]: r=sec.probe(Kind='http',URL='https://'+service+':'+str(port)+'/health/ready',Cert='operator') assert not r['ok'] and r.get('error_category')=='connection_or_action_denied',(service,port,r) denied.append({'service':service,'native_port':port,'external_namespace_connection_denied':True}) # Worker cannot mutate rootfs or read operator/other-role material. filesystem={} for name,command in [('rootfs_write',['touch','/unauthorized-file']),('operator_key',['cat','/operator/init.json']),('other_identity',['cat','/run/tls/lb/key.key'])]: p=subprocess.run(['docker','compose','exec','-T','worker',*command],cwd=ROOT,capture_output=True);assert p.returncode!=0;filesystem[name]='denied' logs=sec.dc('logs','--no-color','--tail','500','api','worker','dispatcher','collector-store') assert not any(v in logs for v in sensitive) status=sec.dc('exec','-T','worker','cat','/proc/1/status');assert 'CapEff:\t0000000000000000' in status and 'NoNewPrivs:\t1' in status result={'runtime':rows,'no_native_bypass':denied,'worker_filesystem':filesystem,'worker_effective_caps_zero':True,'worker_no_new_privileges':True,'known_private_values_absent_from_config_env_and_sampled_logs':True,'scope':'runtime containers only; verify-profile compiler/operator tools not persistent runtime; last500 app/collector log lines sampled, not proof of all future logs'} (ROOT/'evidence/hardening.json').write_text(json.dumps(result,indent=2)+'\n');print('PASS: actual non-root/capabilities/read-only/ports, closed native hops, scoped worker mounts and sampled private-value scan',flush=True) if __name__=='__main__':main()