Skeleton contracts / contracts (pull_request) Blocked by required conditions
58 lines
2.4 KiB
Python
58 lines
2.4 KiB
Python
#!/usr/bin/env python3
|
|
"""Check effective Compose privileges and the actual image user defaults."""
|
|
import json
|
|
import subprocess
|
|
import sys
|
|
|
|
STATEFUL = {"postgres", "broker", "openbao", "s3"}
|
|
ROOT_BOOTSTRAP = {"postgres", "broker", "volume-init"}
|
|
PUBLISHED = {"lb": 8443, "openbao": 8200, "grafana": 3000}
|
|
|
|
|
|
def check(config, image_user):
|
|
findings = []
|
|
for name, service in config["services"].items():
|
|
def require(condition, message):
|
|
if not condition:
|
|
findings.append(f"{name}: {message}")
|
|
|
|
require(not service.get("privileged") and service.get("network_mode") != "host",
|
|
"privileged mode or host network")
|
|
require(not service.get("pid") and not service.get("ipc") == "host", "host process or IPC namespace")
|
|
require(all("docker.sock" not in str(mount) for mount in service.get("volumes", [])),
|
|
"Docker socket mount")
|
|
require("no-new-privileges:true" in service.get("security_opt", []), "no-new-privileges absent")
|
|
require(bool(service.get("mem_limit") and service.get("cpus") and service.get("pids_limit")),
|
|
"CPU/RAM/PID limit absent")
|
|
for port in service.get("ports", []):
|
|
require(port.get("host_ip") == "127.0.0.1" and port["target"] == PUBLISHED.get(name),
|
|
"unexpected published port")
|
|
effective = service.get("user") or image_user(service["image"])
|
|
if name not in ROOT_BOOTSTRAP:
|
|
require(bool(effective) and effective.split(":")[0] not in {"0", "root"}, "effective image user is root")
|
|
if name not in STATEFUL:
|
|
require(service.get("read_only"), "writable root filesystem")
|
|
require("ALL" in service.get("cap_drop", []), "global capability drop absent")
|
|
require(not service.get("cap_add") or (name == "volume-init" and service["cap_add"] == ["CHOWN"]),
|
|
"unexpected capability grant")
|
|
return findings
|
|
|
|
|
|
def main():
|
|
config = json.load(sys.stdin)
|
|
|
|
def image_user(image):
|
|
return subprocess.check_output(
|
|
["docker", "image", "inspect", "--format", "{{.Config.User}}", image], text=True
|
|
).strip()
|
|
|
|
findings = check(config, image_user)
|
|
if findings:
|
|
print("\n".join(findings))
|
|
raise SystemExit(1)
|
|
print(f"PASS: effective Docker hardening for {len(config['services'])} services")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|