229 lines
8.0 KiB
Python
229 lines
8.0 KiB
Python
#!/usr/bin/env python3
|
|
import base64
|
|
import hashlib
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
import secrets
|
|
from urllib.parse import parse_qsl, urlencode, urlsplit, urlunsplit
|
|
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
SECRET_DIR = ROOT / "secrets"
|
|
|
|
|
|
def read(name: str) -> str:
|
|
return (SECRET_DIR / name).read_text(encoding="utf-8").strip()
|
|
|
|
|
|
def write(name: str, content: str, replace: bool = False) -> None:
|
|
path = SECRET_DIR / name
|
|
flags = os.O_WRONLY | os.O_CREAT | (os.O_TRUNC if replace else os.O_EXCL)
|
|
descriptor = os.open(path, flags, 0o600)
|
|
with os.fdopen(descriptor, "w", encoding="utf-8") as output:
|
|
output.write(content)
|
|
if not content.endswith("\n"):
|
|
output.write("\n")
|
|
|
|
|
|
def rabbit_user(name: str, password: str) -> dict:
|
|
salt = secrets.token_bytes(4)
|
|
digest = hashlib.sha256(salt + password.encode("utf-8")).digest()
|
|
return {
|
|
"name": name,
|
|
"password_hash": base64.b64encode(salt + digest).decode("ascii"),
|
|
"hashing_algorithm": "rabbit_password_hashing_sha256",
|
|
"tags": "",
|
|
}
|
|
|
|
|
|
def main() -> None:
|
|
os.chmod(SECRET_DIR, 0o700)
|
|
db_parts = urlsplit(read("database_url"))
|
|
query = dict(parse_qsl(db_parts.query, keep_blank_values=True))
|
|
query.update({"sslmode": "verify-full", "sslrootcert": "/run/secrets/ca_crt"})
|
|
write(
|
|
"database_url",
|
|
urlunsplit((db_parts.scheme, db_parts.netloc, db_parts.path, urlencode(query), "")),
|
|
replace=True,
|
|
)
|
|
worker_password_path = SECRET_DIR / "worker_db_password"
|
|
if not worker_password_path.exists():
|
|
write("worker_db_password", secrets.token_urlsafe(32))
|
|
worker_password = read("worker_db_password")
|
|
write(
|
|
"worker_database_url",
|
|
urlunsplit(
|
|
(
|
|
db_parts.scheme,
|
|
f"messenger_worker:{worker_password}@{db_parts.hostname}:{db_parts.port}",
|
|
db_parts.path,
|
|
urlencode(query),
|
|
"",
|
|
)
|
|
),
|
|
replace=(SECRET_DIR / "worker_database_url").exists(),
|
|
)
|
|
|
|
service_s3_credentials = {}
|
|
for service in ("worker", "publisher", "migrate", "reconciler"):
|
|
access_key_file = f"{service}_s3_access_key_id"
|
|
secret_key_file = f"{service}_s3_secret_access_key"
|
|
if not (SECRET_DIR / access_key_file).exists():
|
|
write(access_key_file, service[:3] + secrets.token_hex(8))
|
|
if not (SECRET_DIR / secret_key_file).exists():
|
|
write(secret_key_file, secrets.token_urlsafe(40))
|
|
service_s3_credentials[service] = (
|
|
read(access_key_file),
|
|
read(secret_key_file),
|
|
)
|
|
|
|
credentials = {}
|
|
users = []
|
|
permissions = []
|
|
for service, configure, write_access, read_access in (
|
|
("worker", r"^$", r"^$", r"^attachments\.process\.v1$"),
|
|
("publisher", r"^$", r"^amq\.default$", r"^$"),
|
|
):
|
|
password_file = f"rabbitmq_{service}_password"
|
|
password_path = SECRET_DIR / password_file
|
|
password = password_path.read_text(encoding="utf-8").strip() if password_path.exists() else secrets.token_urlsafe(32)
|
|
if not password_path.exists():
|
|
write(password_file, password)
|
|
credentials[service] = password
|
|
users.append(rabbit_user(service, password))
|
|
permissions.append({
|
|
"user": service,
|
|
"vhost": "/",
|
|
"configure": configure,
|
|
"write": write_access,
|
|
"read": read_access,
|
|
})
|
|
write(
|
|
f"broker_{service}_url",
|
|
f"amqps://{service}:{password}@rabbitmq:5671/%2f",
|
|
replace=(SECRET_DIR / f"broker_{service}_url").exists(),
|
|
)
|
|
|
|
definitions = {
|
|
"users": users,
|
|
"vhosts": [{"name": "/"}],
|
|
"permissions": permissions,
|
|
"queues": [
|
|
{
|
|
"name": "attachments.process.dead",
|
|
"vhost": "/",
|
|
"durable": True,
|
|
"auto_delete": False,
|
|
"arguments": {"x-queue-type": "quorum", "x-quorum-initial-group-size": 1},
|
|
},
|
|
{
|
|
"name": "attachments.process.v1",
|
|
"vhost": "/",
|
|
"durable": True,
|
|
"auto_delete": False,
|
|
"arguments": {
|
|
"x-queue-type": "quorum",
|
|
"x-quorum-initial-group-size": 1,
|
|
"x-dead-letter-exchange": "",
|
|
"x-dead-letter-routing-key": "attachments.process.dead",
|
|
},
|
|
},
|
|
],
|
|
}
|
|
definitions_path = SECRET_DIR / "rabbitmq_definitions.json"
|
|
if definitions_path.exists():
|
|
definitions_path.unlink()
|
|
write("rabbitmq_definitions.json", json.dumps(definitions, separators=(",", ":")))
|
|
os.chmod(definitions_path, 0o644)
|
|
|
|
config = (
|
|
"default_user = messenger\n"
|
|
f"default_pass = {read('rabbitmq_password')}\n"
|
|
"listeners.tcp = none\n"
|
|
"listeners.ssl.default = 5671\n"
|
|
"ssl_options.cacertfile = /run/secrets/ca_crt\n"
|
|
"ssl_options.certfile = /run/secrets/rabbitmq_tls_cert\n"
|
|
"ssl_options.keyfile = /run/secrets/rabbitmq_tls_key\n"
|
|
"ssl_options.verify = verify_peer\n"
|
|
"ssl_options.fail_if_no_peer_cert = true\n"
|
|
"ssl_options.versions.1 = tlsv1.2\n"
|
|
"definitions.import_backend = local_filesystem\n"
|
|
"definitions.local.path = /run/secrets/rabbitmq_definitions\n"
|
|
)
|
|
write("rabbitmq.conf", config, replace=True)
|
|
identities = [
|
|
{
|
|
"name": "bootstrap-admin",
|
|
"credentials": [{
|
|
"accessKey": read("s3_admin_access_key_id"),
|
|
"secretKey": read("s3_admin_secret_access_key"),
|
|
}],
|
|
"actions": ["Admin"],
|
|
},
|
|
{
|
|
"name": "messenger-api",
|
|
"credentials": [{
|
|
"accessKey": read("s3_access_key_id"),
|
|
"secretKey": read("s3_secret_access_key"),
|
|
}],
|
|
"actions": [
|
|
"Read:messenger-attachments",
|
|
"List:messenger-attachments",
|
|
"Read:messenger-variants",
|
|
"Write:messenger-staging",
|
|
],
|
|
},
|
|
{
|
|
"name": "messenger-worker",
|
|
"credentials": [{
|
|
"accessKey": service_s3_credentials["worker"][0],
|
|
"secretKey": service_s3_credentials["worker"][1],
|
|
}],
|
|
"actions": [
|
|
"Read:messenger-staging",
|
|
"Write:messenger-staging",
|
|
"Write:messenger-attachments",
|
|
"Write:messenger-variants",
|
|
],
|
|
},
|
|
*[
|
|
{
|
|
"name": f"messenger-{service}",
|
|
"credentials": [{
|
|
"accessKey": service_s3_credentials[service][0],
|
|
"secretKey": service_s3_credentials[service][1],
|
|
}],
|
|
"actions": [],
|
|
}
|
|
for service in ("publisher", "migrate")
|
|
],
|
|
{
|
|
"name": "messenger-reconciler",
|
|
"credentials": [{
|
|
"accessKey": service_s3_credentials["reconciler"][0],
|
|
"secretKey": service_s3_credentials["reconciler"][1],
|
|
}],
|
|
"actions": [
|
|
f"{action}:{bucket}"
|
|
for bucket in ("messenger-staging", "messenger-attachments", "messenger-variants")
|
|
for action in ("Read", "Write", "List")
|
|
],
|
|
},
|
|
]
|
|
write("s3.json", json.dumps({"identities": identities}, separators=(",", ":")), replace=True)
|
|
iam_config_path = SECRET_DIR / "s3_iam.json"
|
|
if not iam_config_path.exists():
|
|
iam_config = {
|
|
"sts": {
|
|
"signingKey": "base64-" + base64.urlsafe_b64encode(secrets.token_bytes(32)).decode("ascii")
|
|
},
|
|
"defaultEffect": "Deny",
|
|
}
|
|
write("s3_iam.json", json.dumps(iam_config, separators=(",", ":")))
|
|
print("TLS endpoints and service-scoped broker credentials prepared locally; values were not printed.")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|