lab7: тесты безопасности и перевод своих тестов на HTTPS, документы (trust-matrix, THREAT_MODEL, OPERATOR, DEFENSE, WINDOWS, PR), сборщик доказательств, чистка legacy bench

This commit is contained in:
Alexey Bodanyuk
2026-11-27 17:43:14 +03:00
parent 05df1695fe
commit 84dbdb4735
44 changed files with 1644 additions and 2686 deletions
+63 -25
View File
@@ -248,13 +248,17 @@ def test_graceful_shutdown_order_in_logs(api):
# ------------------------------------------------------------------ discovery: restart and scaling
def test_full_restart_lb_rediscovers_all_replicas(api):
actor = api.user("restart")
before = {r["instance"]: r["started_at"] for r in running_replicas()}
# Lab 7: X-Instance-Id is a random alias per PROCESS, so the replicas are compared by container
# id; a restarted process must show a NEW alias (it is not derived from the container).
before = {r["id"]: (r["started_at"], r["instance"]) for r in running_replicas()}
run_hook("restart.sh")
wait_healthy("postgres", "valkey", "api")
wait_stand()
after = {r["instance"]: r["started_at"] for r in running_replicas()}
assert set(after) == set(before) and all(after[h] != before[h] for h in after), "every replica restarted"
assert set(instances_seen(api, actor["token"])) == set(after) # same token, both replicas, no LB restart needed
after = {r["id"]: (r["started_at"], r["instance"]) for r in running_replicas()}
assert set(after) == set(before), "the same containers run again"
assert all(after[c][0] != before[c][0] and after[c][1] != before[c][1] for c in after), "every replica restarted"
seen = set(instances_seen(api, actor["token"]))
assert seen == {alias for _, alias in after.values()} # same token, both replicas, no LB restart needed
def test_scale_up_and_down_is_picked_up_without_errors(api):
@@ -278,14 +282,24 @@ def test_scale_up_and_down_is_picked_up_without_errors(api):
def test_compose_policy_limits_ports_secrets_images():
config = json.loads(compose("config", "--format", "json").stdout)
services = config["services"]
# auditor and jobctl (profile "tools") are started on demand and not part of the running stand.
# auditor, jobctl and mtls-check (profile "tools") are started on demand, not part of the stand.
assert set(services) == {"postgres", "valkey", "s3", "broker", "migrate", "s3-init", "api", "lb", "relay", "worker",
# lab 6: observability
"otel-collector", "prometheus", "loki", "tempo", "grafana"}
"otel-collector", "prometheus", "loki", "tempo", "grafana",
# lab 7: secrets
"openbao"}
for name, svc in services.items():
limits = svc.get("deploy", {}).get("resources", {}).get("limits", {})
assert limits.get("cpus") and limits.get("memory"), f"{name}: CPU/RAM limits"
assert "container_name" not in svc, f"{name}: container_name prevents scaling"
# lab 7 hardening: nothing privileged, no host namespaces, no Docker socket, no capabilities
assert not svc.get("privileged") and svc.get("network_mode") is None and svc.get("pid") is None, name
assert not any("docker.sock" in str(v.get("source", "")) for v in svc.get("volumes", [])), name
assert svc.get("cap_drop") == ["ALL"] and "no-new-privileges:true" in svc.get("security_opt", []), name
if name in ("postgres", "valkey", "s3", "broker"): # start as root, chown, drop to their user
assert sorted(svc.get("cap_add", [])) == ["CHOWN", "DAC_OVERRIDE", "FOWNER", "SETGID", "SETUID"], name
else:
assert not svc.get("cap_add"), name
if name in ("api", "migrate"):
assert "build" in svc # built from this repository's source
elif name in ("s3-init", "relay", "worker"):
@@ -294,39 +308,63 @@ def test_compose_policy_limits_ports_secrets_images():
assert "@sha256:" in svc["image"], f"{name}: image must be pinned by digest"
assert services["api"]["deploy"]["replicas"] >= 2 and not services["api"].get("ports")
published = {name: svc.get("ports", []) for name, svc in services.items() if svc.get("ports")}
# Published: the LB and (lab 6) the Grafana UI - both on loopback only. No OTLP/store/admin port.
# Published: the HTTPS ingress and the HTTPS Grafana UI - both on loopback only. Nothing else.
assert set(published) == {"lb", "grafana"}
assert all(p["host_ip"] in ("127.0.0.1", "::1") for ports in published.values() for p in ports)
assert [p["target"] for p in published["lb"]] == [8443, 8443]
assert [p["target"] for p in published["grafana"]] == [3000, 3000]
secrets = {name: sorted(s["source"] for s in svc.get("secrets", [])) for name, svc in services.items()}
s3_keys = sorted(f"s3_{who}_{part}" for who in ("admin", "api", "worker", "auditor")
for part in ("access_key_id", "secret_access_key"))
assert secrets == {"postgres": ["postgres_password"], "valkey": ["valkey_password"], "s3": s3_keys,
"migrate": ["database_url"], "s3-init": s3_keys, "lb": [],
"broker": ["rabbitmq_users"],
# the API: its own S3 key pair only (no admin/worker keys, no broker credentials)
"api": ["cache_url", "database_url", "s3_api_access_key_id", "s3_api_secret_access_key"],
# relay: database + its broker user only; worker: + its own S3 key pair
"relay": ["broker_url_relay", "database_url"],
"worker": ["broker_url_worker", "database_url", "s3_worker_access_key_id",
"s3_worker_secret_access_key"],
# lab 6: the telemetry services get no application secret at all
"otel-collector": [], "prometheus": [], "loki": [], "tempo": [],
"grafana": ["grafana_admin_password"]}
ca = "pki_internal_ca"
def own(*names):
return sorted([*names, ca])
assert secrets == {
# stores: their server certificate + what they verify; postgres creates the service roles once
"postgres": sorted(["postgres_password", "pki_postgres_crt", "pki_postgres_key", ca,
*(f"pg_{r}_password" for r in ("migrate", "api", "relay", "worker", "jobctl", "auditor"))]),
"valkey": own("valkey_api_password", "pki_valkey_crt", "pki_valkey_key"),
"s3": own(*s3_keys, "pki_s3_crt", "pki_s3_key"),
"broker": own("rabbitmq_users", "pki_broker_crt", "pki_broker_key"),
# bootstrap jobs: migrate = schema owner only; s3-init = the ONLY holder of the S3 admin key
"migrate": own("database_url_migrate"),
"s3-init": own(*s3_keys),
# the LB: public certificate + its client identity towards the API
"lb": own("pki_ingress_pem", "pki_lb_pem"),
# application services: ONLY their AppRole login + their certificate; every other secret
# comes from OpenBao (no database/cache/broker URL or S3 key file, no token)
"api": own("bao_api_role_id", "bao_api_secret_id", "pki_api_crt", "pki_api_key"),
"relay": own("bao_relay_role_id", "bao_relay_secret_id", "pki_relay_crt", "pki_relay_key"),
"worker": own("bao_worker_role_id", "bao_worker_secret_id", "pki_worker_crt", "pki_worker_key"),
"openbao": own("pki_openbao_crt", "pki_openbao_key"),
# telemetry: certificates only, no application secret
"otel-collector": own("pki_otel_collector_crt", "pki_otel_collector_key"),
"prometheus": own("pki_prometheus_crt", "pki_prometheus_key"),
"loki": own("pki_loki_crt", "pki_loki_key"),
"tempo": own("pki_tempo_crt", "pki_tempo_key"),
"grafana": own("grafana_admin_password", "pki_grafana_crt", "pki_grafana_key", "pki_grafana_ui_crt",
"pki_grafana_ui_key", "pki_public_ca"),
}, secrets
# No CA private key is defined as a secret at all; nothing mounts the operator's material.
assert not [s for s in config.get("secrets", {}) if s.endswith("ca_key") or "root" in s or "unseal" in s]
volumes = {name: [v["source"] for v in svc.get("volumes", []) if v["type"] == "volume"]
for name, svc in services.items()}
assert volumes == {"postgres": ["pgdata"], "valkey": ["valkeydata"], "s3": ["s3data"], "migrate": [],
"s3-init": [], "api": [], "lb": [], "broker": ["rabbitmqdata"], "relay": [], "worker": [],
"otel-collector": [], "prometheus": ["promdata"], "loki": ["lokidata"], "tempo": ["tempodata"],
"grafana": ["grafanadata"]}
"grafana": ["grafanadata"], "openbao": ["baodata"]}
networks = {name: sorted(svc.get("networks", {})) for name, svc in services.items()}
assert networks == {"postgres": ["data"], "valkey": ["data"], "s3": ["storage"], "migrate": ["data"],
"s3-init": ["storage"], "api": ["data", "edge", "storage", "telemetry"], "broker": ["queue"],
"relay": ["data", "queue", "telemetry"], "worker": ["data", "queue", "storage", "telemetry"],
"s3-init": ["storage"], "api": ["data", "edge", "storage", "telemetry", "vault"],
"broker": ["queue"], "relay": ["data", "queue", "telemetry", "vault"],
"worker": ["data", "queue", "storage", "telemetry", "vault"],
"lb": ["edge"], "otel-collector": ["telemetry"], "prometheus": ["telemetry"],
"loki": ["telemetry"], "tempo": ["telemetry"], "grafana": ["telemetry", "ui"]}, networks
"loki": ["telemetry"], "tempo": ["telemetry"], "grafana": ["telemetry", "ui"],
"openbao": ["vault"]}, networks
# the LB cannot reach the stores; the API not the broker; telemetry stores cannot reach the app stores
for network in ("data", "storage", "queue", "telemetry"):
for network in ("data", "storage", "queue", "telemetry", "vault"):
assert config["networks"][network].get("internal") is True, network
@@ -346,7 +384,7 @@ def test_running_containers_have_limits_and_no_bypass_ports():
def test_migrations_ran_once_by_the_migrate_service():
applied = psql("SELECT version FROM schema_migrations ORDER BY version").split()
assert applied == ["0001_init", "0002_idempotency_keys", "0003_post_message_function", "0004_attachments",
"0005_pipeline", "0006_observability"]
"0005_pipeline", "0006_observability", "0007_least_privilege"]
migrate = compose("ps", "-a", "--format", "json", "migrate").stdout
rows = [json.loads(line) for line in migrate.splitlines() if line.strip()]
assert rows and all(r["State"] == "exited" and r["ExitCode"] == 0 for r in rows)