Files
mtusi2026lab7/student-tests/hardening.py
T

26 lines
1.1 KiB
Python

#!/usr/bin/env python3
"""Inspect only safe fields; do not dump environment, mounts contents or tokens."""
import json
import subprocess
ids=subprocess.check_output(['docker','compose','ps','-q'],text=True).split()
assert ids
for id in ids:
c=json.loads(subprocess.check_output(['docker','inspect',id]))[0]
role=c['Config']['Labels']['com.docker.compose.service']
h=c['HostConfig']
assert not h['Privileged'] and h['NetworkMode']!='host'
assert all('docker.sock' not in m['Source'] for m in c['Mounts'])
assert h['Memory']>0 and h['NanoCpus']>0
if role in ('api','worker','dispatcher'):
assert c['Config']['User']=='65534:65534'
assert 'ALL' in h['CapDrop'] and h['ReadonlyRootfs']
assert 'no-new-privileges:true' in h['SecurityOpt']
assert not h['PortBindings']
assert all('/operator' not in m['Destination'] for m in c['Mounts'])
ports=h['PortBindings'] or {}
for entries in ports.values():
for entry in entries or []:
assert entry['HostIp']=='127.0.0.1'
assert role in ('lb','grafana','openbao')
print('PASS hardening/resource/port checks:',role)