forked from mtusi2026/mtusi2026lab7
30 lines
1.5 KiB
Python
30 lines
1.5 KiB
Python
"""Inspect actual container privileges, mounts, published ports and limits."""
|
|
import json
|
|
import subprocess
|
|
|
|
ids = subprocess.check_output(['docker','compose','ps','-q'], text=True).split()
|
|
assert ids, 'stack not running'
|
|
containers = json.loads(subprocess.check_output(['docker','inspect',*ids], text=True))
|
|
seen = set()
|
|
for c in containers:
|
|
role=c['Config']['Labels']['com.docker.compose.service']
|
|
seen.add(role)
|
|
host=c['HostConfig']
|
|
assert not host['Privileged'], role
|
|
assert host['NetworkMode'] != 'host', role
|
|
assert host['Memory'] > 0 and host['NanoCpus'] > 0, role
|
|
for mount in c['Mounts']:
|
|
assert 'docker.sock' not in mount['Source'] and 'docker.sock' not in mount['Destination'], role
|
|
assert not mount['Source'].endswith(('/ca.key','/openbao-operator.json')), role
|
|
for ports in (c['NetworkSettings'].get('Ports') or {}).values():
|
|
for port in ports or []:
|
|
assert role in {'load_balancer','openbao','grafana'}, role
|
|
assert port['HostIp']=='127.0.0.1', role
|
|
if role in {'api-a','api-b','worker'}:
|
|
assert c['Config']['User'].split(':')[0] not in {'','0','root'}, role
|
|
assert 'ALL' in host['CapDrop'], role
|
|
assert not host['CapAdd'], role
|
|
assert 'no-new-privileges:true' in host['SecurityOpt'], role
|
|
assert {'api-a','api-b','worker','load_balancer'} <= seen
|
|
print('PASS runtime: scoped ports, no privileged/host/socket/operator keys; resource limits; non-root app/worker')
|