forked from mtusi2026/mtusi2026lab2
Embed the checked lab 5 preparation: RabbitMQ 4.1.8 pinned by digest with a fixed node hostname, a volume, a read-only root and 512 MiB, whose topology (vhost messenger, exchange attachments, quorum queue attachments.process with a dead letter exchange, delivery limit 10 and reject-publish overflow) and users come from definitions imported on every boot. The relay may only write to the exchange, the worker may only read the queue, configure belongs to the operator, there is no guest, and the management UI is published on 127.0.0.1 only. The relay and the worker run from the application image with a heartbeat health check, init, a read-only root and their own limits; the worker gets its own S3 user that reads staging and writes originals and variants, and s3-init creates it idempotently. The API has no broker credentials. make secrets also creates the broker passwords, URLs, hashed users and the worker S3 keys; the restart hook now stops the balancer, the API, the relay and the worker and restarts PostgreSQL, Valkey, S3 and the broker with their volumes; worker.sh and broker.sh stop, start and inspect the processes for the failure demonstrations; the limits policy covers the new services and the loopback UI port. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
186 lines
6.9 KiB
Python
186 lines
6.9 KiB
Python
#!/usr/bin/env python3
|
|
import argparse
|
|
import json
|
|
import subprocess
|
|
import sys
|
|
import time
|
|
from pathlib import Path
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
S3_CLIENT_SECRETS = [
|
|
"s3_access_key_id",
|
|
"s3_root_password",
|
|
"s3_root_user",
|
|
"s3_secret_access_key",
|
|
"s3_worker_access_key_id",
|
|
"s3_worker_secret_access_key",
|
|
]
|
|
POLICY = {
|
|
"lb": {"secrets": [], "volumes": [], "networks": ["backend", "edge"], "ports": True},
|
|
"api": {
|
|
"secrets": [
|
|
"cache_url",
|
|
"cursor_secret",
|
|
"database_url",
|
|
"s3_access_key_id",
|
|
"s3_secret_access_key",
|
|
],
|
|
"volumes": [],
|
|
"networks": ["backend"],
|
|
},
|
|
"migrate": {"secrets": ["database_url"], "volumes": [], "networks": ["backend"]},
|
|
"postgres": {"secrets": ["postgres_password"], "volumes": ["pgdata"], "networks": ["backend"]},
|
|
"valkey": {"secrets": ["valkey.conf"], "volumes": ["valkeydata"], "networks": ["backend"]},
|
|
"k6": {"secrets": [], "volumes": [], "networks": ["edge"]},
|
|
"s3": {
|
|
"secrets": ["s3_root_password", "s3_root_user"],
|
|
"volumes": ["s3data"],
|
|
"networks": ["backend"],
|
|
},
|
|
"s3-init": {"secrets": S3_CLIENT_SECRETS, "volumes": [], "networks": ["backend"]},
|
|
"s3-cli": {"secrets": S3_CLIENT_SECRETS, "volumes": [], "networks": ["backend"]},
|
|
"broker": {
|
|
"secrets": ["rabbitmq_ops_password", "rabbitmq_users"],
|
|
"volumes": ["broker-data"],
|
|
"networks": ["backend", "edge"],
|
|
"ports": True,
|
|
},
|
|
"relay": {
|
|
"secrets": ["broker_url_api", "database_url", "s3_access_key_id", "s3_secret_access_key"],
|
|
"volumes": [],
|
|
"networks": ["backend"],
|
|
},
|
|
"worker": {
|
|
"secrets": [
|
|
"broker_url_worker",
|
|
"database_url",
|
|
"s3_worker_access_key_id",
|
|
"s3_worker_secret_access_key",
|
|
],
|
|
"volumes": [],
|
|
"networks": ["backend"],
|
|
},
|
|
}
|
|
PUBLISHING = frozenset(name for name, rule in POLICY.items() if rule.get("ports"))
|
|
|
|
|
|
def run(*command):
|
|
done = subprocess.run(command, cwd=str(ROOT), capture_output=True, text=True, check=False)
|
|
return done.returncode, done.stdout.strip()
|
|
|
|
|
|
def declared(name, service, networks, problems):
|
|
limits = service.get("deploy", {}).get("resources", {}).get("limits", {})
|
|
secrets = sorted(s["source"] for s in service.get("secrets", []))
|
|
volumes = sorted(v["source"] for v in service.get("volumes", []) if v["type"] == "volume")
|
|
nets = sorted(service.get("networks", {}))
|
|
ports = [
|
|
f"{p.get('host_ip', '0.0.0.0')}:{p.get('published')}" for p in service.get("ports", [])
|
|
]
|
|
want = POLICY.get(name)
|
|
if want is None:
|
|
problems.append(f"{name}: service is not covered by the policy")
|
|
want = {"secrets": [], "volumes": [], "networks": []}
|
|
if not limits.get("cpus") or not limits.get("memory"):
|
|
problems.append(f"{name}: no CPU or memory limit")
|
|
if "container_name" in service:
|
|
problems.append(f"{name}: container_name prevents scaling")
|
|
for kind, actual in (("secrets", secrets), ("volumes", volumes), ("networks", nets)):
|
|
if actual != want[kind]:
|
|
problems.append(f"{name}: {kind} {actual}, policy {want[kind]}")
|
|
if ports and not want.get("ports"):
|
|
problems.append(f"{name}: publishes {ports} in bypass of the balancer")
|
|
if any(not port.startswith("127.0.0.1:") for port in ports):
|
|
problems.append(f"{name}: publishes {ports} beyond loopback")
|
|
if name == "api" and not service.get("read_only"):
|
|
problems.append("api: root filesystem is writable")
|
|
internal = [n for n in nets if networks.get(n, {}).get("internal")]
|
|
return [
|
|
name,
|
|
f"{limits.get('cpus', '-')}/{int(limits.get('memory') or 0) >> 20}MiB",
|
|
",".join(ports) or "-",
|
|
",".join(secrets) or "-",
|
|
",".join(volumes) or "-",
|
|
",".join(f"{n}{'(internal)' if n in internal else ''}" for n in nets),
|
|
str(service.get("read_only", False)).lower(),
|
|
]
|
|
|
|
|
|
def running(problems):
|
|
_, ids = run("docker", "compose", "ps", "-q")
|
|
template = (
|
|
'{{index .Config.Labels "com.docker.compose.service"}} {{.Config.Hostname}} '
|
|
"{{.HostConfig.NanoCpus}} {{.HostConfig.Memory}} {{json .HostConfig.PortBindings}}"
|
|
)
|
|
_, found = run("docker", "inspect", "-f", template, *ids.split())
|
|
rows = []
|
|
for line in found.splitlines():
|
|
service, host, cpus, memory, bindings = line.split(" ", 4)
|
|
published = [
|
|
f"{b['HostIp']}:{b['HostPort']}->{port}"
|
|
for port, items in (json.loads(bindings) or {}).items()
|
|
for b in items or []
|
|
]
|
|
if int(cpus) <= 0 or int(memory) <= 0:
|
|
problems.append(f"container {service}/{host}: no CPU or memory limit applied")
|
|
if published and service not in PUBLISHING:
|
|
problems.append(f"container {service}/{host}: publishes {published}")
|
|
rows.append(
|
|
[service, host, f"{int(cpus) / 1e9:g}", f"{int(memory) >> 20}MiB", ",".join(published)]
|
|
)
|
|
return sorted(rows)
|
|
|
|
|
|
def table(header, rows):
|
|
widths = [max(len(str(r[i])) for r in [header, *rows]) for i in range(len(header))]
|
|
lines = [header, ["-" * w for w in widths], *rows]
|
|
return [" ".join(str(c).ljust(w) for c, w in zip(r, widths)).rstrip() for r in lines]
|
|
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser(description="limits, ports, secrets, volumes and networks")
|
|
parser.add_argument("--out", help="text report, written only after a complete check")
|
|
args = parser.parse_args()
|
|
problems = []
|
|
_, raw = run(
|
|
"docker",
|
|
"compose",
|
|
"--profile",
|
|
"bench",
|
|
"--profile",
|
|
"tools",
|
|
"config",
|
|
"--format",
|
|
"json",
|
|
)
|
|
config = json.loads(raw)
|
|
services = [
|
|
declared(name, service, config.get("networks", {}), problems)
|
|
for name, service in sorted(config["services"].items())
|
|
]
|
|
containers = running(problems)
|
|
if not any(row[0] == "api" for row in containers):
|
|
problems.append("no running api replicas to inspect")
|
|
lines = [
|
|
f"date: {time.strftime('%Y-%m-%dT%H:%M:%SZ', time.gmtime())}",
|
|
"$ docker compose --profile bench --profile tools config --format json (declared)",
|
|
*table(["service", "cpus/mem", "ports", "secrets", "volumes", "networks", "ro"], services),
|
|
"",
|
|
"$ docker inspect (running containers: hostname = X-Instance-Id, applied limits)",
|
|
*table(["service", "hostname", "cpus", "memory", "published"], containers),
|
|
"",
|
|
*(f"FAIL: {p}" for p in problems),
|
|
"PASS: limits, ports, secrets, volumes and networks match the policy"
|
|
if not problems
|
|
else f"FAIL: {len(problems)} violation(s)",
|
|
]
|
|
text = "\n".join(lines) + "\n"
|
|
if args.out:
|
|
Path(args.out).write_text(text)
|
|
print(text, end="")
|
|
return 1 if problems else 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main())
|