Files
mtusi2026lab7/scripts/runtime_audit.py
T
Xabenskie 4eca49fe3d
Grade 3 secure application acceptance / secure (pull_request) Canceled after 0s
Skeleton contracts / contracts (pull_request) Canceled after 0s
[+] lab7
2026-11-30 09:09:57 +03:00

29 lines
1.9 KiB
Python

#!/usr/bin/env python3
"""Sanitized container privileges/ports/mount proof; never dump environment or secret values."""
import json
from pathlib import Path
import subprocess
ROOT=Path(__file__).resolve().parents[1]
ids=subprocess.check_output(['docker','compose','ps','-aq'],cwd=ROOT,text=True).split()
state=json.loads(subprocess.check_output(['docker','inspect',*ids],text=True))
items=[]
for value in state:
h=value['HostConfig'];c=value['Config'];role=c['Labels']['com.docker.compose.service']
mounts=[m['Destination'] for m in value['Mounts']]
assert not h['Privileged'] and h['NetworkMode']!='host' and not any('docker.sock' in m for m in mounts)
if role in ('api','worker','relay'):
assert c['User'] not in ('','0','root','0:0') and h['ReadonlyRootfs']
assert 'ALL' in h.get('CapDrop',[]) and 'no-new-privileges:true' in h.get('SecurityOpt',[])
assert not any('/operator' in m or 'minio_root' in m or 'database_url' in m for m in mounts)
items.append({'service':role,'image':c['Image'],'image_id':value['Image'],'state':value['State']['Status'],
'user':c['User'],'read_only':h['ReadonlyRootfs'],'cap_drop':h.get('CapDrop'), 'cap_add':h.get('CapAdd'),
'security_opt':h.get('SecurityOpt'),'memory_bytes':h['Memory'],'nano_cpus':h['NanoCpus'],
'ports':h['PortBindings'],'mount_targets':mounts,'network_mode':h['NetworkMode']})
assert not subprocess.check_output(['git','ls-files','secrets'],cwd=ROOT,text=True).strip()
ignored=subprocess.run(['git','check-ignore','secrets/operator/ca.key','secrets/operator/bao-init.json'],cwd=ROOT,capture_output=True,text=True)
assert ignored.returncode==0
result={'result':'PASS','root_operator_material_not_mounted_in_workloads':True,'no_tracked_secrets':True,'containers':items}
(ROOT/'evidence/runtime-hardening.json').write_text(json.dumps(result,indent=2)+'\n')
print('PASS: runtime-hardening.json')