forked from mtusi2026/mtusi2026lab7
72 lines
3.3 KiB
Python
72 lines
3.3 KiB
Python
"""Read the Compose runtime configuration without exporting environment values."""
|
|
import hashlib
|
|
import json
|
|
import ssl
|
|
import subprocess
|
|
from pathlib import Path
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
PROJECT = "mtusi-lab7-grade3"
|
|
PUBLICATIONS = {"lb": ("8443/tcp", "8443"), "openbao": ("8200/tcp", "8200"),
|
|
"grafana": ("3000/tcp", "3443")}
|
|
APP = {"api1", "api2", "worker"}
|
|
TARGET = ROOT / "evidence" / "grade3"
|
|
|
|
|
|
def docker(*args):
|
|
return subprocess.check_output(["docker", *args], cwd=ROOT)
|
|
|
|
|
|
def record_certs():
|
|
entries = []
|
|
for cert in sorted((ROOT / "secrets" / "pki").glob("*/*.crt")):
|
|
details = ssl._ssl._test_decode_cert(str(cert))
|
|
der = ssl.PEM_cert_to_DER_cert(cert.read_text(encoding="ascii"))
|
|
entries.append({"role": cert.parent.name, "sans": details.get("subjectAltName", []),
|
|
"issuer": details["issuer"], "valid_until": details["notAfter"],
|
|
"sha256": hashlib.sha256(der).hexdigest()})
|
|
(TARGET / "certificates.json").write_text(json.dumps(entries, indent=2) + "\n", encoding="utf-8")
|
|
|
|
|
|
def main():
|
|
ids = docker("ps", "-aq", "--filter", f"label=com.docker.compose.project={PROJECT}",
|
|
"--filter", "label=com.docker.compose.oneoff=False").decode().splitlines()
|
|
assert ids
|
|
containers = json.loads(docker("inspect", *ids))
|
|
evidence = []
|
|
for item in containers:
|
|
cfg, host = item["Config"], item["HostConfig"]
|
|
service = cfg["Labels"]["com.docker.compose.service"]
|
|
assert not host["Privileged"] and host["NetworkMode"] != "host"
|
|
assert all("/docker.sock" not in m["Source"] and "/docker.sock" not in m["Destination"]
|
|
for m in item["Mounts"])
|
|
binds = host["PortBindings"] or {}
|
|
if service in PUBLICATIONS:
|
|
port, external = PUBLICATIONS[service]
|
|
assert binds == {port: [{"HostIp": "127.0.0.1", "HostPort": external}]}, service
|
|
else:
|
|
assert not binds, service
|
|
if service in APP:
|
|
assert cfg["User"] == "appuser" and host["CapDrop"] == ["ALL"]
|
|
assert "no-new-privileges:true" in host["SecurityOpt"]
|
|
assert all(host[key] > 0 for key in ("Memory", "NanoCpus", "PidsLimit"))
|
|
names = {entry.split("=", 1)[0] for entry in cfg["Env"]}
|
|
assert names.isdisjoint({"BAO_TOKEN", "VAULT_TOKEN", "BAO_ROOT_TOKEN", "SECRET_ID",
|
|
"DATABASE_URL", "BROKER_URL", "S3_SECRET_ACCESS_KEY"})
|
|
uid = docker("exec", item["Id"], "id", "-u").decode().strip()
|
|
assert uid == "10001"
|
|
else:
|
|
uid = None
|
|
evidence.append({"service": service, "uid": uid, "status": item["State"]["Status"],
|
|
"ports": binds, "memory_bytes": host["Memory"], "nano_cpus": host["NanoCpus"],
|
|
"pids_limit": host["PidsLimit"], "image": item["Image"]})
|
|
assert APP | set(PUBLICATIONS) <= {item["service"] for item in evidence}
|
|
evidence.sort(key=lambda entry: entry["service"])
|
|
(TARGET / "hardening.json").write_text(json.dumps(evidence, indent=2) + "\n", encoding="utf-8")
|
|
record_certs()
|
|
print("PASS: loopback-only published ingress and admin ports, app UID 10001 and resource/privilege limits")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|