feat: core func lab7

This commit is contained in:
Enterxx2
2026-11-24 14:04:04 +03:00
parent 4829fc7b4e
commit 411ca73aa0
83 changed files with 2883 additions and 523 deletions
+50
View File
@@ -0,0 +1,50 @@
#!/usr/bin/env python3
"""Проверяет итоговый Compose и параметры запущенного worker."""
import json
import subprocess
def docker(*arguments):
return subprocess.check_output(["docker", *arguments], text=True)
def require(value, description):
if not value:
raise RuntimeError(description)
def main():
config = json.loads(docker("compose", "config", "--format", "json"))
roles = config["services"]
for name, role in roles.items():
require(not role.get("privileged"), f"{name}: privileged")
require(role.get("network_mode") != "host", f"{name}: host network")
require("no-new-privileges:true" in role.get("security_opt", ()),
f"{name}: нет no-new-privileges")
require(not any("docker.sock" in item.get("source", "") for item in role.get("volumes", ())),
f"{name}: смонтирован Docker socket")
ports = role.get("ports", ())
require(not ports or name in {"nginx", "grafana", "openbao"}, f"{name}: опубликован порт")
require(all(item.get("host_ip") == "127.0.0.1" for item in ports),
f"{name}: порт привязан не к loopback")
for name in ("api", "worker", "dispatcher", "janitor"):
role = roles[name]
require(role.get("read_only") and "ALL" in role.get("cap_drop", ()),
f"{name}: контейнер должен быть read-only и сбрасывать capabilities")
require(role.get("tmpfs"), f"{name}: требуется отдельный tmpfs")
resources = role["deploy"]["resources"]["limits"]
require(resources.get("cpus") and resources.get("memory"), f"{name}: нет ресурсных лимитов")
ids = docker("compose", "ps", "-q", "api", "worker").splitlines()
require(len(ids) >= 3, "Ожидались работающие API-реплики и worker")
for identity in ids:
state = json.loads(docker("inspect", identity))[0]
require(state["Config"]["User"] == "app", "app/worker должен запускаться non-root")
require(state["HostConfig"]["ReadonlyRootfs"], "runtime rootfs открыт для записи")
require(state["HostConfig"]["Memory"] > 0, "runtime memory limit не применён")
print(f"PASS hardening: {len(roles)} сервисов, {len(ids)} non-root API/worker с resource limits")
if __name__ == "__main__":
main()