forked from mtusi2026/mtusi2026lab7
33 lines
1.2 KiB
Python
33 lines
1.2 KiB
Python
"""Готовит cert-пользователей RabbitMQ и их права очереди."""
|
|
|
|
import json
|
|
from pathlib import Path
|
|
import re
|
|
|
|
|
|
def initialize(directory: Path, queue: str, vhost: str) -> None:
|
|
queue_pattern = f"^{re.escape(queue)}$"
|
|
denied = "^$a"
|
|
roles = (
|
|
("api-broker", denied, denied, denied),
|
|
("attachment-publisher", queue_pattern, denied, r"^amq\.default$"),
|
|
("attachment-worker", queue_pattern, queue_pattern, denied),
|
|
)
|
|
users = [
|
|
{"name": name, "password_hash": "", "hashing_algorithm": "rabbit_password_hashing_sha256",
|
|
"tags": []}
|
|
for name, *_ in roles
|
|
]
|
|
permissions = [
|
|
{"user": name, "vhost": vhost, "configure": configure, "read": read, "write": write}
|
|
for name, configure, read, write in roles
|
|
]
|
|
definitions = json.dumps({"users": users, "vhosts": [{"name": vhost}],
|
|
"permissions": permissions})
|
|
path = directory / "broker_definitions.json"
|
|
if not path.exists() or path.read_text() != definitions:
|
|
temporary = path.with_suffix(".tmp")
|
|
temporary.write_text(definitions)
|
|
temporary.chmod(0o444)
|
|
temporary.replace(path)
|