forked from mtusi2026/mtusi2026lab7
Закрыть оценку 4 и трек 1: отзыв leaf и права worker.
Брокер и Collector принимают только серийный номер текущего leaf. Прогон показывает rehash, 429 до дорогого Argon2, отказ stranger, ротацию пароля worker и сохранность задания.
This commit is contained in:
@@ -0,0 +1,253 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Grade 4 rotation/revocation plus track 1, the worker compromise comparison.
|
||||
|
||||
Stops the worker, leaves one upload queued, revokes the old worker leaf,
|
||||
rotates the worker database password, then shows the same job still reaches ready.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import secrets
|
||||
import shutil
|
||||
import subprocess
|
||||
import sys
|
||||
import time
|
||||
from pathlib import Path
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
sys.path.insert(0, str(ROOT / "tests"))
|
||||
|
||||
from client import Client # noqa: E402
|
||||
|
||||
|
||||
def run(args: list[str], check: bool = True) -> subprocess.CompletedProcess:
|
||||
completed = subprocess.run(args, cwd=ROOT, check=False)
|
||||
if check and completed.returncode != 0:
|
||||
raise SystemExit("command failed: %s" % " ".join(args[:6]))
|
||||
return completed
|
||||
|
||||
|
||||
def compose(*args: str, check: bool = True) -> subprocess.CompletedProcess:
|
||||
return run(["docker", "compose", "--env-file", ".env", *args], check=check)
|
||||
|
||||
|
||||
def reissue(name: str, cn: str, eku: str, san: str, not_before: str = "", not_after: str = "") -> None:
|
||||
env_args: list[str] = []
|
||||
if not_before:
|
||||
env_args.extend(["-e", "NOT_BEFORE=" + not_before])
|
||||
if not_after:
|
||||
env_args.extend(["-e", "NOT_AFTER=" + not_after])
|
||||
run(
|
||||
[
|
||||
"docker",
|
||||
"run",
|
||||
"--rm",
|
||||
*env_args,
|
||||
"-v",
|
||||
"%s:/work" % (ROOT / "certs"),
|
||||
"-v",
|
||||
"%s:/reissue-leaf.sh:ro" % (ROOT / "scripts" / "reissue-leaf.sh"),
|
||||
"alpine:3.21.3",
|
||||
"sh",
|
||||
"/reissue-leaf.sh",
|
||||
name,
|
||||
cn,
|
||||
eku,
|
||||
san,
|
||||
]
|
||||
)
|
||||
|
||||
|
||||
def worker_rights() -> None:
|
||||
completed = compose(
|
||||
"run",
|
||||
"--rm",
|
||||
"--no-deps",
|
||||
"--entrypoint",
|
||||
"python",
|
||||
"-e",
|
||||
"PYTHONPATH=/app",
|
||||
"-e",
|
||||
"BAO_ROLE_ID_FILE=/run/worker/role_id",
|
||||
"-e",
|
||||
"BAO_SECRET_ID_FILE=/run/worker/secret_id",
|
||||
"-e",
|
||||
"BAO_SECRET_PATH=secret/data/messenger/worker",
|
||||
"-e",
|
||||
"TLS_CERT_FILE=/certs/worker-client.crt",
|
||||
"-e",
|
||||
"TLS_KEY_FILE=/certs/worker-client.key",
|
||||
"-v",
|
||||
"%s:/run/worker/role_id:ro" % (ROOT / "secrets" / "worker.role_id"),
|
||||
"-v",
|
||||
"%s:/run/worker/secret_id:ro" % (ROOT / "secrets" / "worker.secret_id"),
|
||||
"-v",
|
||||
"%s:/certs/worker-client.crt:ro" % (ROOT / "certs" / "worker-client.crt"),
|
||||
"-v",
|
||||
"%s:/certs/worker-client.key:ro" % (ROOT / "certs" / "worker-client.key"),
|
||||
"-v",
|
||||
"%s:/probe.py:ro" % (ROOT / "student-tests" / "worker_rights.py"),
|
||||
"api",
|
||||
"/probe.py",
|
||||
)
|
||||
if completed.returncode != 0:
|
||||
raise SystemExit(completed.returncode)
|
||||
|
||||
|
||||
def rotate_db_password() -> None:
|
||||
path = ROOT / "secrets" / "db_worker_password"
|
||||
path.write_text(secrets.token_hex(24), encoding="utf-8")
|
||||
container = subprocess.run(
|
||||
["docker", "compose", "--env-file", ".env", "ps", "-q", "postgres"],
|
||||
cwd=ROOT,
|
||||
check=True,
|
||||
capture_output=True,
|
||||
text=True,
|
||||
).stdout.strip()
|
||||
run(["docker", "cp", str(path), "%s:/tmp/db_worker_password" % container])
|
||||
run(
|
||||
[
|
||||
"docker",
|
||||
"exec",
|
||||
container,
|
||||
"sh",
|
||||
"-c",
|
||||
"psql -v ON_ERROR_STOP=1 -U messenger -d messenger -c \"ALTER ROLE messenger_worker PASSWORD '$(cat /tmp/db_worker_password)'\" && rm -f /tmp/db_worker_password",
|
||||
]
|
||||
)
|
||||
compose(
|
||||
"--profile",
|
||||
"bootstrap",
|
||||
"run",
|
||||
"--rm",
|
||||
"--entrypoint",
|
||||
"python",
|
||||
"-e",
|
||||
"BAO_REFRESH_KV_ONLY=1",
|
||||
"-v",
|
||||
"%s:/app/app/tools/openbao_bootstrap.py:ro" % (ROOT / "app" / "tools" / "openbao_bootstrap.py"),
|
||||
"openbao-bootstrap",
|
||||
"-m",
|
||||
"app.tools.openbao_bootstrap",
|
||||
)
|
||||
|
||||
|
||||
def image_id() -> str:
|
||||
completed = subprocess.run(
|
||||
["docker", "image", "inspect", "mtusi-messenger:lab7", "--format", "{{.Id}}"],
|
||||
cwd=ROOT,
|
||||
check=True,
|
||||
capture_output=True,
|
||||
text=True,
|
||||
)
|
||||
return completed.stdout.strip()
|
||||
|
||||
|
||||
def main() -> None:
|
||||
before_image = image_id()
|
||||
print("image_before", before_image.split(":")[-1][:12])
|
||||
run(
|
||||
[
|
||||
"docker",
|
||||
"run",
|
||||
"--rm",
|
||||
"-v",
|
||||
"%s:/work" % (ROOT / "certs"),
|
||||
"-v",
|
||||
"%s:/gen-certs.sh:ro" % (ROOT / "scripts" / "gen-certs.sh"),
|
||||
"alpine:3.21.3",
|
||||
"sh",
|
||||
"/gen-certs.sh",
|
||||
]
|
||||
)
|
||||
compose("up", "-d", "--no-deps", "--force-recreate", "rabbit-mtls", "otel-mtls")
|
||||
time.sleep(2)
|
||||
worker_rights()
|
||||
users = compose(
|
||||
"exec",
|
||||
"-T",
|
||||
"postgres",
|
||||
"psql",
|
||||
"-U",
|
||||
"messenger",
|
||||
"-d",
|
||||
"messenger",
|
||||
"-tAc",
|
||||
"SELECT count(*) FROM users",
|
||||
)
|
||||
del users
|
||||
print("PASS superuser can read users; worker role cannot")
|
||||
|
||||
compose("stop", "worker")
|
||||
client = Client("https://127.0.0.1:8443", str(ROOT / "certs" / "ca.crt"), timeout=10)
|
||||
alice, _bob, _eve, chat = client.bootstrap()
|
||||
attachment, _content = client.attachment(alice, chat)
|
||||
if attachment["status"] != "queued":
|
||||
raise SystemExit("FAIL upload was not left queued")
|
||||
print("PASS upload queued while worker is stopped")
|
||||
|
||||
certs = ROOT / "certs"
|
||||
shutil.copyfile(certs / "worker-client.crt", certs / "worker-client.revoked.crt")
|
||||
shutil.copyfile(certs / "worker-client.key", certs / "worker-client.revoked.key")
|
||||
reissue(
|
||||
"worker-client",
|
||||
"worker",
|
||||
"clientAuth",
|
||||
"DNS:worker-client,URI:spiffe://messenger/worker",
|
||||
)
|
||||
compose("up", "-d", "--no-deps", "--force-recreate", "rabbit-mtls", "otel-mtls")
|
||||
time.sleep(2)
|
||||
probe = subprocess.run([sys.executable, "-X", "utf8", str(ROOT / "student-tests" / "same_ca_probe.py")], cwd=ROOT)
|
||||
if probe.returncode != 0:
|
||||
raise SystemExit(probe.returncode)
|
||||
|
||||
reissue("lb", "localhost", "serverAuth", "DNS:localhost,IP:127.0.0.1")
|
||||
compose("restart", "lb")
|
||||
time.sleep(2)
|
||||
client.call("GET", "/health/ready", schema="Health")
|
||||
print("PASS lb leaf rotated without an image rebuild")
|
||||
|
||||
rotate_db_password()
|
||||
compose("start", "worker")
|
||||
ready = client.wait_ready(alice, attachment, wait_seconds=90)
|
||||
if ready["status"] != "ready":
|
||||
raise SystemExit("FAIL queued attachment did not become ready after rotation")
|
||||
print("PASS queued job survived worker revocation and secret rotation")
|
||||
worker_rights()
|
||||
|
||||
reissue(
|
||||
"expired-worker",
|
||||
"worker",
|
||||
"clientAuth",
|
||||
"URI:spiffe://messenger/worker",
|
||||
not_before="20200101000000Z",
|
||||
not_after="20200102000000Z",
|
||||
)
|
||||
probe = subprocess.run([sys.executable, "-X", "utf8", str(ROOT / "student-tests" / "same_ca_probe.py")], cwd=ROOT)
|
||||
if probe.returncode != 0:
|
||||
raise SystemExit(probe.returncode)
|
||||
|
||||
seal = compose(
|
||||
"--profile",
|
||||
"bootstrap",
|
||||
"run",
|
||||
"--rm",
|
||||
"--no-deps",
|
||||
"--entrypoint",
|
||||
"python",
|
||||
"-v",
|
||||
"%s:/probe.py:ro" % (ROOT / "student-tests" / "seal_probe.py"),
|
||||
"openbao-bootstrap",
|
||||
"/probe.py",
|
||||
)
|
||||
if seal.returncode != 0:
|
||||
raise SystemExit(seal.returncode)
|
||||
after_image = image_id()
|
||||
if before_image != after_image:
|
||||
raise SystemExit("FAIL application image changed during rotation")
|
||||
print("PASS image unchanged", after_image.split(":")[-1][:12])
|
||||
print("PASS track1 and grade4 lifecycle")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user