forked from mtusi2026/mtusi2026lab7
44 lines
2.5 KiB
Python
44 lines
2.5 KiB
Python
#!/usr/bin/env python3
|
|
"""Check effective runtime settings without printing environment variables or mounts."""
|
|
|
|
import json
|
|
from pathlib import Path
|
|
import subprocess
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
compose = ROOT / "scripts/compose.sh"
|
|
services = subprocess.check_output([str(compose), "config", "--services"], cwd=ROOT, text=True).split()
|
|
ids = subprocess.check_output([str(compose), "ps", "-q", *services], cwd=ROOT, text=True).split()
|
|
assert len(ids) == len(services) + 1, "expected full stack with two API replicas"
|
|
template = ('{"name":{{json .Name}},"user":{{json .Config.User}},'
|
|
'"network_mode":{{json .HostConfig.NetworkMode}},"privileged":{{json .HostConfig.Privileged}},'
|
|
'"readonly":{{json .HostConfig.ReadonlyRootfs}},"security":{{json .HostConfig.SecurityOpt}},'
|
|
'"drop":{{json .HostConfig.CapDrop}},"memory":{{json .HostConfig.Memory}},'
|
|
'"cpus":{{json .HostConfig.NanoCpus}},"tmpfs":{{json .HostConfig.Tmpfs}},'
|
|
'"ports":{{json .HostConfig.PortBindings}},"mounts":{{json .Mounts}}}')
|
|
for row in subprocess.check_output(["docker", "inspect", "--format", template, *ids], text=True).splitlines():
|
|
container = json.loads(row)
|
|
assert not container["privileged"] and container["network_mode"] != "host"
|
|
assert container["memory"] > 0 and container["cpus"] > 0
|
|
assert container["readonly"] and "/tmp" in container["tmpfs"] and "ALL" in container["drop"]
|
|
assert any(item.startswith("no-new-privileges") for item in container["security"])
|
|
assert all("docker.sock" not in mount["Source"] and "docker.sock" not in mount["Destination"]
|
|
for mount in container["mounts"])
|
|
name = container["name"]
|
|
ports = container["ports"] or {}
|
|
known = {"gateway": ("8443/tcp", None), "openbao": ("8200/tcp", "18200"),
|
|
"grafana": ("3000/tcp", "13000")}
|
|
for service, (container_port, host_port) in known.items():
|
|
if name.endswith(f"-{service}-1"):
|
|
assert set(ports) == {container_port}
|
|
assert all(binding["HostIp"] == "127.0.0.1" and
|
|
(host_port is None or binding["HostPort"] == host_port) for binding in ports[container_port])
|
|
break
|
|
else:
|
|
assert not ports
|
|
if "-api-" in name or "-worker-" in name:
|
|
assert container["user"] == "messenger:messenger"
|
|
destinations = {mount["Destination"] for mount in container["mounts"]}
|
|
assert destinations == {"/trust", "/identity"}
|
|
print(json.dumps({k: container[k] for k in ("name", "user", "memory", "cpus", "readonly", "ports")}))
|