37 lines
1.9 KiB
Python
37 lines
1.9 KiB
Python
"""Inspect running services without dumping Docker environment or secret mounts."""
|
|
from collections import defaultdict
|
|
import json
|
|
import subprocess
|
|
|
|
|
|
def main():
|
|
ids = subprocess.check_output(["docker", "compose", "ps", "-q"], text=True).splitlines()
|
|
rows = json.loads(subprocess.check_output(["docker", "inspect", *ids]))
|
|
ports = defaultdict(list)
|
|
for item in rows:
|
|
cfg = item["HostConfig"]
|
|
service = item["Config"]["Labels"]["com.docker.compose.service"]
|
|
assert not cfg["Privileged"] and cfg["NetworkMode"] != "host", service
|
|
assert cfg["ReadonlyRootfs"] and "ALL" in cfg["CapDrop"], service
|
|
assert "no-new-privileges:true" in cfg["SecurityOpt"], service
|
|
assert cfg["Memory"] > 0 and cfg["NanoCpus"] > 0 and cfg["PidsLimit"] > 0, service
|
|
assert not any("docker.sock" in mount["Destination"] for mount in item["Mounts"]), service
|
|
for binds in (cfg.get("PortBindings") or {}).values():
|
|
for bind in binds:
|
|
assert bind["HostIp"] == "127.0.0.1", service
|
|
ports[service].append(bind["HostPort"])
|
|
user = item["Config"]["User"]
|
|
if service in {"api", "worker", "dispatcher", "reconciler"}:
|
|
assert user == "10001:10001" and not cfg["CapAdd"], service
|
|
assert "/tmp" in cfg["Tmpfs"], service
|
|
print(json.dumps({"service": service, "user": user or "entrypoint default", "cpu": cfg["NanoCpus"]/1e9,
|
|
"memory_mib": cfg["Memory"]//2**20, "pids": cfg["PidsLimit"],
|
|
"added_caps": cfg["CapAdd"] or [], "rootfs_read_only": True}))
|
|
assert dict(ports) == {"lb": ["8443"], "openbao": ["8207"], "grafana": ["3007"]}, ports
|
|
assert sum(row["Config"]["Labels"]["com.docker.compose.service"] == "api" for row in rows) == 2
|
|
print("PASS all running services audited, only TLS loopback ports", json.dumps(ports))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|