Files
mtusi2026lab7/scripts/check-hardening.py
T
SashaKirupina 7fb93b31ba
Skeleton contracts / contracts (pull_request) Canceled after 0s
ЛР 7 — БПИ2304 — Кирюпина — на 3
2026-11-28 00:31:13 +03:00

23 lines
1.5 KiB
Python

"""Inspect only safe Docker configuration fields, never Env or full inspect output."""
from pathlib import Path
import json,subprocess
ROOT=Path(__file__).resolve().parents[1]
ids=subprocess.check_output(['docker','compose','ps','-q'],cwd=ROOT,text=True).split()
raw=json.loads(subprocess.check_output(['docker','inspect',*ids],text=True))
result=[]
for c in raw:
h=c['HostConfig'];service=c['Config']['Labels']['com.docker.compose.service']
assert not h['Privileged'] and h['NetworkMode']!='host'
assert not any('docker.sock' in m['Source'] for m in c['Mounts'])
assert h['Memory']>0 and h['NanoCpus']>0
ports=h.get('PortBindings') or {}
for bindings in ports.values():
for b in bindings or []:assert b['HostIp']=='127.0.0.1'
if service in ['api1','api2','worker']:
assert c['Config']['User']=='1000:1000'
assert 'ALL' in h['CapDrop'] and 'no-new-privileges:true' in h['SecurityOpt']
assert all(m['Destination'] in ['/tls/ca.crt','/tls/cert.crt','/tls/key.pem'] for m in c['Mounts'])
result.append({'service':service,'user':c['Config']['User'],'memory_bytes':h['Memory'],'nano_cpus':h['NanoCpus'],'privileged':False,'published_ports':ports,'cap_drop':h.get('CapDrop'),'security_opt':h.get('SecurityOpt')})
(ROOT/'evidence/hardening.json').write_text(json.dumps(result,indent=2)+'\n')
print('PASS: non-root API/worker, dropped capabilities, resource limits, no privileged/host-network/socket; only localhost TLS/admin ports exposed; app mounts contain only its leaf key/cert and public CA.')