import base64 import hashlib import json import os import platform import subprocess import sys from pathlib import Path sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "scripts")) from bootstrap import PRIVATE, ROOT, write from capture import private_values from provision import compose def main(): ids = compose("ps", "-q").splitlines() containers = json.loads(subprocess.check_output(["docker", "inspect", *ids])) rows = [] for item in containers: role = item["Config"]["Labels"]["com.docker.compose.service"] host = item["HostConfig"] assert not host["Privileged"] and host["NetworkMode"] != "host" assert not any("docker.sock" in mount["Destination"] for mount in item["Mounts"]) assert host["Memory"] > 0 and (host["NanoCpus"] > 0 or host["CpuQuota"] > 0) ports = host.get("PortBindings") or {} for bindings in ports.values(): for binding in bindings or []: assert binding["HostIp"] == "127.0.0.1" if role in ("api", "worker", "dispatcher") or role.endswith("-agent"): uid = ( subprocess.check_output(["docker", "exec", item["Id"], "id", "-u"]).decode().strip() ) assert uid != "0" and host["ReadonlyRootfs"] assert "ALL" in host["CapDrop"] and "no-new-privileges:true" in host["SecurityOpt"] assert not ports assert not any( "operator" in mount.get("Source", "").replace("\\", "/").split("/") for mount in item["Mounts"] ) for value in private_values(): assert value not in str(item["Config"]["Env"]) rows.append( dict( role=role, container=item["Name"].lstrip("/"), image=item["Image"], user=item["Config"]["User"], read_only=host["ReadonlyRootfs"], cap_drop=host["CapDrop"], security_opt=host["SecurityOpt"], memory_bytes=host["Memory"], nano_cpus=host["NanoCpus"], published=ports, mount_destinations=[m["Destination"] for m in item["Mounts"]], ) ) assert sum(r["role"] == "api" for r in rows) == 2 assert sum(r["role"] == "worker" for r in rows) == 2 logs = compose( "logs", "--no-color", "--since", "30m", "api", "worker", "dispatcher", "api-agent", "worker-agent", "dispatcher-agent", ) assert not any(v in logs for v in private_values()) assert "-----BEGIN PRIVATE KEY-----" not in logs paths = ["README.md", "COURSE.md", "CONTRIBUTING.md", "MAINTAINERS.md", "Makefile", "lab.json"] paths += ( subprocess.check_output( ["git", "ls-tree", "-r", "--name-only", "20afc2c", "tests", "contracts"], cwd=ROOT ) .decode() .splitlines() ) protected = [] for name in paths: if name == "contracts/trust-matrix.md": continue before = subprocess.check_output(["git", "show", "20afc2c:" + name], cwd=ROOT) actual = (ROOT / name).read_bytes() assert before.replace(b"\r\n", b"\n") == actual.replace(b"\r\n", b"\n"), name protected.append(dict(path=name, sha256=hashlib.sha256(actual).hexdigest())) engine = json.loads( subprocess.check_output(["docker", "info", "--format", "{{json .}}"], cwd=ROOT) ) inventory = dict( status="PASS", host=dict( platform=platform.platform(), logical_cpus=os.cpu_count(), docker_cpus=engine["NCPU"], docker_memory_bytes=engine["MemTotal"], docker_version=engine["ServerVersion"], ), containers=rows, no_private_values_in_environment_or_recent_logs=True, unchanged_contract_and_public_tests=protected, trust_matrix="required student-filled exception", ) write(ROOT / "evidence/security-inventory.json", json.dumps(inventory, indent=2) + "\n") print( "PASS actual container hardening, two API and two workers, no published replica ports, scoped mounts, secret-free environments/logs, original contracts and public tests" ) if __name__ == "__main__": main()