Лабораторная 5: брокер, worker, outbox, варианты изображений, тесты отказов (оценка 4)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
kalibr456
2026-10-24 15:53:17 +03:00
co-authored by Claude Opus 5.5
parent 276dbdad8a
commit fd6f85d94c
112 changed files with 10533 additions and 26 deletions
+153
View File
@@ -0,0 +1,153 @@
import base64
import hashlib
import json
import secrets
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
SECRETS = ROOT / "secrets"
def write(name: str, value: str) -> None:
with open(SECRETS / name, "w", encoding="utf-8", newline="") as file:
file.write(value)
(SECRETS / name).chmod(0o644) # контейнеры читают секрет не от root; папка secrets/ в .gitignore
def bucket_name() -> str:
"""S3_BUCKET из .env (если задан), иначе значение по умолчанию."""
env = ROOT / ".env"
if env.exists():
for line in env.read_text(encoding="utf-8").splitlines():
if line.strip().startswith("S3_BUCKET="):
return line.split("=", 1)[1].strip()
return "messenger-attachments"
def s3_config(bucket: str) -> str:
"""Конфиг ключей SeaweedFS: admin — только для s3-init, messenger-api — один bucket.
Права SeaweedFS: Read (GetObject/HeadObject), Write (PutObject/DeleteObject), List (ListObjects)
с суффиксом :<bucket> действуют только в этом bucket. Admin (создание/удаление bucket и т.п.) у API нет.
Identity "anonymous" не объявлена, поэтому запросы без подписи отклоняются.
"""
def read(name):
return (SECRETS / name).read_text(encoding="utf-8").strip()
config = {"identities": [
{"name": "admin",
"credentials": [{"accessKey": read("s3_root_user"), "secretKey": read("s3_root_password")}],
"actions": ["Admin", "Read", "Write", "List", "Tagging"]},
{"name": "messenger-api",
"credentials": [{"accessKey": read("s3_access_key_id"), "secretKey": read("s3_secret_access_key")}],
"actions": [f"Read:{bucket}", f"Write:{bucket}", f"List:{bucket}"]},
]}
return json.dumps(config, indent=2)
BROKER_VHOST = "messenger"
# Имена из pipeline.py (скрипт не импортирует код приложения, чтобы работать без зависимостей).
EXCHANGE, QUEUE, ROUTING_KEY = "attachments", "attachments.process.v1", "attachment.process.v1"
DLX, DLQ, DLQ_ROUTING_KEY = "attachments.dlx", "attachments.process.v1.dlq", "attachment.process.v1.dlq"
def rabbit_hash(password: str) -> str:
"""Формат rabbit_password_hashing_sha256: base64(salt(4 байта) + SHA-256(salt + пароль))."""
salt = secrets.token_bytes(4)
return base64.b64encode(salt + hashlib.sha256(salt + password.encode("utf-8")).digest()).decode("ascii")
def rabbit_definitions(passwords: dict) -> str:
"""Топология и пользователи RabbitMQ; брокер загружает файл при каждом старте (infra/rabbitmq.conf).
Права (configure / write / read, регулярные выражения по именам ресурсов):
dispatcher — публикует только в exchange attachments; читает DLQ (ops.py dlq-replay); configure нет;
worker — читает только основную очередь; не публикует (повторы идут через outbox); configure нет.
Ни у кого из сервисов нет тега administrator и права создать/удалить очередь.
"""
queue_args = {
"x-queue-type": "quorum",
"x-delivery-limit": 20, # ядовитое сообщение не крутится бесконечно -> DLQ
"x-dead-letter-exchange": DLX,
"x-dead-letter-routing-key": DLQ_ROUTING_KEY,
"x-dead-letter-strategy": "at-least-once", # перенос в DLQ без потери (требует reject-publish)
"x-overflow": "reject-publish", # переполнение -> nack издателю, outbox повторит
"x-max-length": 100000,
}
definitions = {
"users": [{"name": name, "password_hash": rabbit_hash(password),
"hashing_algorithm": "rabbit_password_hashing_sha256", "tags": []}
for name, password in passwords.items()],
"vhosts": [{"name": BROKER_VHOST}],
"permissions": [
{"user": "dispatcher", "vhost": BROKER_VHOST, "configure": "^$",
"write": f"^{EXCHANGE}$", "read": "^" + DLQ.replace(".", r"\.") + "$"},
{"user": "worker", "vhost": BROKER_VHOST, "configure": "^$",
"write": "^$", "read": "^" + QUEUE.replace(".", r"\.") + "$"},
],
"exchanges": [
{"name": EXCHANGE, "vhost": BROKER_VHOST, "type": "direct", "durable": True, "auto_delete": False,
"internal": False, "arguments": {}},
{"name": DLX, "vhost": BROKER_VHOST, "type": "direct", "durable": True, "auto_delete": False,
"internal": False, "arguments": {}},
],
"queues": [
{"name": QUEUE, "vhost": BROKER_VHOST, "durable": True, "auto_delete": False, "arguments": queue_args},
# DLQ — classic durable: у quorum в 4.x delivery-limit по умолчанию 20, и многократный
# просмотр (basic_get + nack) в ops.py мог бы выбросить сообщение. Здесь потерь от просмотра нет.
{"name": DLQ, "vhost": BROKER_VHOST, "durable": True, "auto_delete": False,
"arguments": {"x-queue-type": "classic"}},
],
"bindings": [
{"source": EXCHANGE, "vhost": BROKER_VHOST, "destination": QUEUE, "destination_type": "queue",
"routing_key": ROUTING_KEY, "arguments": {}},
{"source": DLX, "vhost": BROKER_VHOST, "destination": DLQ, "destination_type": "queue",
"routing_key": DLQ_ROUTING_KEY, "arguments": {}},
],
}
return json.dumps(definitions, indent=2)
def broker_secrets(created: list, host: str = "rabbitmq") -> None:
if (SECRETS / "rabbitmq_definitions.json").exists():
return
passwords = {"dispatcher": secrets.token_hex(24), "worker": secrets.token_hex(24)}
for name, password in passwords.items():
write(f"broker_url_{name}", f"amqp://{name}:{password}@{host}:5672/{BROKER_VHOST}")
write("rabbitmq_definitions.json", rabbit_definitions(passwords))
created += ["broker_url_dispatcher", "broker_url_worker", "rabbitmq_definitions.json"]
def main() -> None:
"""Создаёт недостающие секреты. Уже существующие не меняет (иначе разошлись бы с данными в volumes)."""
SECRETS.mkdir(exist_ok=True)
created = []
if not (SECRETS / "postgres_password").exists():
db_password = secrets.token_hex(24)
redis_password = secrets.token_hex(24)
write("postgres_password", db_password)
write("database_url", f"postgresql://messenger:{db_password}@postgres:5432/messenger")
write("cache_url", f"redis://:{redis_password}@redis:6379/0")
write("redis_auth", f"requirepass {redis_password}\n")
created += ["postgres_password", "database_url", "cache_url", "redis_auth"]
if not (SECRETS / "s3_root_password").exists():
# admin-ключ S3: только для сервисов s3 (в конфиге) и s3-init
write("s3_root_user", "root-" + secrets.token_hex(6))
write("s3_root_password", secrets.token_hex(24))
created += ["s3_root_user", "s3_root_password"]
if not (SECRETS / "s3_secret_access_key").exists():
# отдельный ключ API с политикой только на свой bucket (создаёт s3-init)
write("s3_access_key_id", "api-" + secrets.token_hex(8))
write("s3_secret_access_key", secrets.token_hex(24))
created += ["s3_access_key_id", "s3_secret_access_key"]
if not (SECRETS / "s3_config.json").exists():
write("s3_config.json", s3_config(bucket_name()))
created.append("s3_config.json")
broker_secrets(created)
if created:
print("Created in secrets/: " + ", ".join(created) + " (values are not printed).")
else:
print("secrets/ is complete, nothing changed.")
if __name__ == "__main__":
main()
+14
View File
@@ -0,0 +1,14 @@
@echo off
rem Restart hook for tests/resilience.py on Windows (same as scripts/restart.sh).
rem Python on Windows cannot start .sh files directly (WinError 193), a .cmd file works.
rem Restarts the stores first (PostgreSQL, Redis, SeaweedFS S3, RabbitMQ), then api, janitor, dispatcher, worker,
rem then the balancer. Volumes are kept.
rem "restart" does not wait for healthchecks; SeaweedFS needs a few seconds to bring up S3, so wait explicitly
rem ("up --wait --no-recreate" keeps the same containers and only waits until they are healthy).
cd /d "%~dp0.."
docker compose restart postgres redis s3 rabbitmq || exit /b 1
docker compose up -d --wait --no-recreate --no-build --no-deps postgres redis s3 rabbitmq || exit /b 1
docker compose restart api janitor dispatcher worker || exit /b 1
docker compose restart lb || exit /b 1
docker compose up -d --wait --no-recreate --no-build --no-deps api lb dispatcher worker || exit /b 1
exit /b 0
+13
View File
@@ -0,0 +1,13 @@
#!/bin/sh
# Restart hook for tests/resilience.py --mode persistence (Linux/macOS).
# Restarts the stores first (PostgreSQL, Redis, SeaweedFS S3, RabbitMQ), then api, janitor, dispatcher and
# worker, then the balancer. Volumes are kept (no down -v), nothing is re-seeded.
# "restart" does not wait for healthchecks; SeaweedFS needs a few seconds to bring up S3, so wait explicitly
# ("up --wait --no-recreate" keeps the same containers and only waits until they are healthy).
set -eu
cd "$(dirname "$0")/.."
docker compose restart postgres redis s3 rabbitmq
docker compose up -d --wait --no-recreate --no-build --no-deps postgres redis s3 rabbitmq
docker compose restart api janitor dispatcher worker
docker compose restart lb
docker compose up -d --wait --no-recreate --no-build --no-deps api lb dispatcher worker
+6
View File
@@ -0,0 +1,6 @@
@echo off
rem Hook for tests/resilience.py --mode failover on Windows (same as scripts/stop-one.sh).
rem Python on Windows cannot start .sh files directly (WinError 193), a .cmd file works.
rem Stops only the api replica from TARGET_INSTANCE_ID, the logic is in scripts\stop_one.py.
python "%~dp0stop_one.py"
exit /b %ERRORLEVEL%
+5
View File
@@ -0,0 +1,5 @@
#!/bin/sh
# Hook for tests/resilience.py --mode failover (Linux/macOS): stops the api replica from TARGET_INSTANCE_ID.
# Logic is in scripts/stop_one.py, Windows uses scripts/stop-one.cmd.
set -eu
exec python3 "$(dirname "$0")/stop_one.py"
+29
View File
@@ -0,0 +1,29 @@
import os
import re
import subprocess
import sys
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
STOP_TIMEOUT_SECONDS = 8
def main() -> int:
target = os.environ.get("TARGET_INSTANCE_ID", "").strip().lower()
if not re.fullmatch(r"[0-9a-f]{12,64}", target):
print("TARGET_INSTANCE_ID must be a container id (X-Instance-Id)", file=sys.stderr)
return 2
running = subprocess.run(["docker", "compose", "ps", "-q", "api"], cwd=ROOT,
capture_output=True, text=True, check=True).stdout.split()
matches = [container for container in running if container.startswith(target)]
if len(matches) != 1:
print(f"no running api container with id {target}", file=sys.stderr)
return 1
subprocess.run(["docker", "stop", "--time", str(STOP_TIMEOUT_SECONDS), matches[0]],
cwd=ROOT, check=True, stdout=subprocess.DEVNULL)
print(f"stopped api container {matches[0][:12]}")
return 0
if __name__ == "__main__":
sys.exit(main())
+8
View File
@@ -0,0 +1,8 @@
@echo off
rem Demo hook for the defense on Windows: scripts\worker.cmd stop ^| start ^| status (volumes are kept).
cd /d "%~dp0.."
if "%1"=="stop" ( docker compose stop worker & exit /b %ERRORLEVEL% )
if "%1"=="start" ( docker compose up -d --wait --no-recreate --no-build --no-deps worker & exit /b %ERRORLEVEL% )
if "%1"=="status" ( docker compose ps worker & exit /b %ERRORLEVEL% )
echo usage: %0 stop^|start^|status
exit /b 2
+11
View File
@@ -0,0 +1,11 @@
#!/bin/sh
# Demo hook for the defense: stop or start the worker without touching volumes.
# scripts/worker.sh stop | start | status
set -eu
cd "$(dirname "$0")/.."
case "${1:-}" in
stop) docker compose stop worker ;;
start) docker compose up -d --wait --no-recreate --no-build --no-deps worker ;;
status) docker compose ps worker ;;
*) echo "usage: $0 stop|start|status" >&2; exit 2 ;;
esac