forked from mtusi2026/mtusi2026lab7
solution
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
"""Готовит 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)
|
||||
Reference in New Issue
Block a user