forked from mtusi2026/mtusi2026lab7
92 lines
4.0 KiB
Python
92 lines
4.0 KiB
Python
#!/usr/bin/env python3
|
|
"""Собирает локальные credentials сервисов. Значения не выводятся."""
|
|
import json
|
|
import re
|
|
import secrets
|
|
import subprocess
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
root = Path("secrets")
|
|
operator = root / "operator"
|
|
runtime = root / "runtime"
|
|
for folder in (operator, runtime, runtime / "s3"):
|
|
folder.mkdir(exist_ok=True, mode=0o755 if folder != operator else 0o700)
|
|
bucket = "messenger-attachments"
|
|
|
|
|
|
def write(path, value, mode=0o600):
|
|
temp = path.with_suffix(".new")
|
|
temp.write_text(value)
|
|
temp.chmod(mode)
|
|
temp.replace(path)
|
|
|
|
|
|
def value(path):
|
|
if not path.exists():
|
|
write(path, secrets.token_hex(24), 0o644 if path.parent == runtime else 0o600)
|
|
return path.read_text()
|
|
|
|
|
|
def worker_cn():
|
|
return subprocess.check_output(
|
|
["openssl", "x509", "-in", "secrets/tls/worker/tls.crt", "-noout", "-subject", "-nameopt", "RFC2253"],
|
|
text=True,
|
|
).strip().removeprefix("subject=CN=")
|
|
|
|
|
|
def render():
|
|
db_root = value(runtime / "postgres_password")
|
|
value(runtime / "grafana_admin_password")
|
|
write(runtime / "database_url", f"postgres://messenger:{db_root}@postgres:5432/messenger", 0o644)
|
|
identities = []
|
|
for role in ("admin", "api", "worker", "reconciler"):
|
|
access = role + value(operator / f"s3_id_{role}")[:20]
|
|
key = value(operator / f"s3_key_{role}")
|
|
if role == "admin":
|
|
write(runtime / "s3_admin_access_key_id", access, 0o644)
|
|
write(runtime / "s3_admin_secret_access_key", key, 0o644)
|
|
actions = ["Admin", "Read", "Write", "List"]
|
|
else:
|
|
scopes = {
|
|
"api": ["Write:staging", "Read:originals", "Read:variants"],
|
|
"worker": ["Read:staging", "Write:staging", "Read:originals", "Write:originals", "Write:variants"],
|
|
"reconciler": [f"{verb}:{prefix}" for verb in ("Read", "Write") for prefix in ("staging", "originals", "variants")],
|
|
}[role]
|
|
actions = [f"{verb}:{bucket}/{prefix}/*" for verb, prefix in (scope.split(":") for scope in scopes)]
|
|
if role == "reconciler":
|
|
actions += [f"List:{bucket}"]
|
|
password = value(operator / f"db_{role}")
|
|
write(operator / f"kv_{role}.json", json.dumps({"data": {
|
|
"DATABASE_URL": f"postgres://{role}:{password}@postgres:5432/messenger",
|
|
"S3_ACCESS_KEY_ID": access, "S3_SECRET_ACCESS_KEY": key,
|
|
}}))
|
|
identities.append({"name": role, "credentials": [{"accessKey": access, "secretKey": key}], "actions": actions})
|
|
write(runtime / "s3" / "identities.json", json.dumps({"identities": identities}), 0o644)
|
|
|
|
worker = worker_cn()
|
|
if not re.fullmatch(r"worker-[0-9a-f]{8}", worker):
|
|
raise SystemExit("unexpected worker identity")
|
|
vhost = "messenger"
|
|
write(runtime / "rabbitmq_definitions.json", json.dumps({
|
|
"users": [{"name": role, "password_hash": "", "tags": []} for role in ("api", worker)],
|
|
"vhosts": [{"name": vhost}],
|
|
"permissions": [
|
|
{"user": "api", "vhost": vhost, "configure": "^$", "write": "^attachments$", "read": "^$"},
|
|
{"user": worker, "vhost": vhost, "configure": "^$", "write": "^$", "read": r"^attachments\.process$"},
|
|
],
|
|
"exchanges": [{"name": "attachments", "vhost": vhost, "type": "direct",
|
|
"durable": True, "auto_delete": False, "internal": False, "arguments": {}}],
|
|
"queues": [{"name": "attachments.process", "vhost": vhost, "durable": True, "auto_delete": False,
|
|
"arguments": {"x-queue-type": "quorum", "x-delivery-limit": 20}}],
|
|
"bindings": [{"source": "attachments", "vhost": vhost, "destination": "attachments.process",
|
|
"destination_type": "queue", "routing_key": "process", "arguments": {}}],
|
|
}), 0o644)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
if sys.argv[1:] == ["rotate-worker"]:
|
|
for key in ("s3_id_worker", "s3_key_worker", "db_worker"):
|
|
write(operator / key, secrets.token_hex(24))
|
|
render()
|