46 lines
2.0 KiB
Python
46 lines
2.0 KiB
Python
"""Confirm the hardened runtime on the running lab via Docker inspection."""
|
|
import json
|
|
from pathlib import Path
|
|
import subprocess
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
|
|
|
|
def docker(*args):
|
|
return subprocess.check_output(['docker', *args], cwd=ROOT, timeout=30, text=True)
|
|
|
|
|
|
def main():
|
|
ids = docker('compose', 'ps', '-aq').split()
|
|
assert ids, 'no lab containers to audit'
|
|
containers = json.loads(docker('inspect', *ids))
|
|
services = set()
|
|
public = {'lb', 'bao', 'prometheus', 'loki', 'tempo', 'grafana'}
|
|
for container in containers:
|
|
config = container['HostConfig']
|
|
service = container['Config']['Labels']['com.docker.compose.service']
|
|
services.add(service)
|
|
assert not config['Privileged'] and config['NetworkMode'] != 'host', service
|
|
assert config['ReadonlyRootfs'] and 'ALL' in config['CapDrop'], service
|
|
assert any(opt.startswith('no-new-privileges') for opt in config['SecurityOpt']), service
|
|
assert not any(mount.get('Source', '').endswith('docker.sock') for mount in container['Mounts']), service
|
|
ports = config['PortBindings'] or {}
|
|
if service not in public:
|
|
assert not ports, service
|
|
for bindings in ports.values():
|
|
assert all(binding['HostIp'] == '127.0.0.1' for binding in bindings), service
|
|
if service in ('api', 'worker'):
|
|
assert container['Config']['User'] == '10001:10001', service
|
|
if service == 'worker':
|
|
assert config['NanoCpus'] == 1_000_000_000 and config['Memory'] == 768 * 1024**2
|
|
assert config['Init'] and config['PidsLimit'] == 64
|
|
assert 'JOB_TIMEOUT_SECONDS=60' in container['Config']['Env']
|
|
assert {'lb', 'api', 'worker', 'broker', 'postgres', 'bao', 'minio',
|
|
'publisher', 'redis', 'otel-gateway', 'collector', 'prometheus',
|
|
'loki', 'tempo', 'grafana'}.issubset(services)
|
|
print('PASS: runtime hardening, published ports and worker CPU/RAM/PID limits')
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|