forked from mtusi2026/mtusi2026lab7
255 lines
10 KiB
Python
255 lines
10 KiB
Python
#!/usr/bin/env python3
|
|
import base64
|
|
import json
|
|
import os
|
|
import secrets
|
|
from pathlib import Path
|
|
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
SECRET_DIR = ROOT / "secrets"
|
|
|
|
|
|
def main():
|
|
SECRET_DIR.mkdir(mode=0o700, exist_ok=True)
|
|
os.chmod(SECRET_DIR, 0o700)
|
|
names = (
|
|
"postgres_password",
|
|
"database_url",
|
|
"worker_db_password",
|
|
"worker_database_url",
|
|
"s3_access_key_id",
|
|
"s3_secret_access_key",
|
|
"worker_s3_access_key_id",
|
|
"worker_s3_secret_access_key",
|
|
"publisher_s3_access_key_id",
|
|
"publisher_s3_secret_access_key",
|
|
"migrate_s3_access_key_id",
|
|
"migrate_s3_secret_access_key",
|
|
"reconciler_s3_access_key_id",
|
|
"reconciler_s3_secret_access_key",
|
|
"s3_admin_access_key_id",
|
|
"s3_admin_secret_access_key",
|
|
"rabbitmq_password",
|
|
"rabbitmq.conf",
|
|
"rabbitmq_definitions.json",
|
|
"broker_url",
|
|
"broker_worker_url",
|
|
"broker_publisher_url",
|
|
"s3.json",
|
|
"s3_iam.json",
|
|
"grafana_admin_password",
|
|
)
|
|
existing = [name for name in names if (SECRET_DIR / name).exists()]
|
|
if existing:
|
|
raise SystemExit(
|
|
"Secret files already exist; refusing to rotate credentials without a migration: "
|
|
+ ", ".join(existing)
|
|
)
|
|
|
|
db_password = secrets.token_hex(24)
|
|
worker_db_password = secrets.token_urlsafe(32)
|
|
broker_password = secrets.token_urlsafe(32)
|
|
worker_broker_password = secrets.token_urlsafe(32)
|
|
publisher_broker_password = secrets.token_urlsafe(32)
|
|
values = {
|
|
"postgres_password": db_password,
|
|
"database_url": (
|
|
f"postgresql://messenger:{db_password}@postgres:5432/messenger"
|
|
"?sslmode=verify-full&sslrootcert=/run/secrets/ca_crt"
|
|
),
|
|
"worker_db_password": worker_db_password,
|
|
"worker_database_url": (
|
|
f"postgresql://messenger_worker:{worker_db_password}@postgres:5432/messenger"
|
|
"?sslmode=verify-full&sslrootcert=/run/secrets/ca_crt"
|
|
),
|
|
"s3_access_key_id": "mngr" + secrets.token_hex(8),
|
|
"s3_secret_access_key": secrets.token_urlsafe(40),
|
|
"worker_s3_access_key_id": "wrk" + secrets.token_hex(8),
|
|
"worker_s3_secret_access_key": secrets.token_urlsafe(40),
|
|
"publisher_s3_access_key_id": "pub" + secrets.token_hex(8),
|
|
"publisher_s3_secret_access_key": secrets.token_urlsafe(40),
|
|
"migrate_s3_access_key_id": "mig" + secrets.token_hex(8),
|
|
"migrate_s3_secret_access_key": secrets.token_urlsafe(40),
|
|
"reconciler_s3_access_key_id": "rec" + secrets.token_hex(8),
|
|
"reconciler_s3_secret_access_key": secrets.token_urlsafe(40),
|
|
"s3_admin_access_key_id": "admin" + secrets.token_hex(7),
|
|
"s3_admin_secret_access_key": secrets.token_urlsafe(40),
|
|
"rabbitmq_password": broker_password,
|
|
"broker_url": f"amqps://worker:{worker_broker_password}@rabbitmq:5671/%2f",
|
|
"broker_worker_url": f"amqps://worker:{worker_broker_password}@rabbitmq:5671/%2f",
|
|
"broker_publisher_url": f"amqps://publisher:{publisher_broker_password}@rabbitmq:5671/%2f",
|
|
"grafana_admin_password": secrets.token_urlsafe(32),
|
|
}
|
|
for name, value in values.items():
|
|
if name.startswith("broker_") and name != "broker_url":
|
|
descriptor = os.open(SECRET_DIR / name, os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0o600)
|
|
with os.fdopen(descriptor, "w", encoding="utf-8") as output:
|
|
output.write(value + "\n")
|
|
continue
|
|
path = SECRET_DIR / name
|
|
descriptor = os.open(path, os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0o600)
|
|
with os.fdopen(descriptor, "w", encoding="utf-8") as output:
|
|
output.write(value + "\n")
|
|
descriptor = os.open(
|
|
SECRET_DIR / "rabbitmq.conf",
|
|
os.O_WRONLY | os.O_CREAT | os.O_EXCL,
|
|
0o600,
|
|
)
|
|
with os.fdopen(descriptor, "w", encoding="utf-8") as output:
|
|
output.write("default_user = messenger\n")
|
|
output.write(f"default_pass = {broker_password}\n")
|
|
output.write("listeners.tcp = none\n")
|
|
output.write("listeners.ssl.default = 5671\n")
|
|
output.write("ssl_options.cacertfile = /run/secrets/ca_crt\n")
|
|
output.write("ssl_options.certfile = /run/secrets/rabbitmq_tls_cert\n")
|
|
output.write("ssl_options.keyfile = /run/secrets/rabbitmq_tls_key\n")
|
|
output.write("ssl_options.verify = verify_peer\n")
|
|
output.write("ssl_options.fail_if_no_peer_cert = true\n")
|
|
output.write("ssl_options.versions.1 = tlsv1.2\n")
|
|
output.write("definitions.import_backend = local_filesystem\n")
|
|
output.write("definitions.local.path = /run/secrets/rabbitmq_definitions.json\n")
|
|
definitions = {
|
|
"users": [],
|
|
"vhosts": [{"name": "/"}],
|
|
"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",
|
|
},
|
|
},
|
|
],
|
|
}
|
|
for username, password, configure, write, read in (
|
|
("worker", worker_broker_password, r"^$", r"^$", r"^attachments\.process\.v1$"),
|
|
("publisher", publisher_broker_password, r"^$", r"^amq\.default$", r"^$"),
|
|
):
|
|
salt = secrets.token_bytes(4)
|
|
password_hash = __import__("hashlib").sha256(salt + password.encode("utf-8")).digest()
|
|
definitions["users"].append({
|
|
"name": username,
|
|
"password_hash": __import__("base64").b64encode(salt + password_hash).decode("ascii"),
|
|
"hashing_algorithm": "rabbit_password_hashing_sha256",
|
|
"tags": "",
|
|
})
|
|
definitions["permissions"].append({
|
|
"user": username,
|
|
"vhost": "/",
|
|
"configure": configure,
|
|
"write": write,
|
|
"read": read,
|
|
})
|
|
descriptor = os.open(
|
|
SECRET_DIR / "rabbitmq_definitions.json",
|
|
os.O_WRONLY | os.O_CREAT | os.O_EXCL,
|
|
0o600,
|
|
)
|
|
with os.fdopen(descriptor, "w", encoding="utf-8") as output:
|
|
json.dump(definitions, output, separators=(",", ":"))
|
|
output.write("\n")
|
|
os.chmod(SECRET_DIR / "rabbitmq_definitions.json", 0o644)
|
|
s3_config = {
|
|
"identities": [
|
|
{
|
|
"name": "bootstrap-admin",
|
|
"credentials": [
|
|
{
|
|
"accessKey": values["s3_admin_access_key_id"],
|
|
"secretKey": values["s3_admin_secret_access_key"],
|
|
}
|
|
],
|
|
"actions": ["Admin"],
|
|
},
|
|
{
|
|
"name": "messenger-api",
|
|
"credentials": [
|
|
{
|
|
"accessKey": values["s3_access_key_id"],
|
|
"secretKey": values["s3_secret_access_key"],
|
|
}
|
|
],
|
|
"actions": [
|
|
"Read:messenger-attachments",
|
|
"List:messenger-attachments",
|
|
"Read:messenger-variants",
|
|
"Write:messenger-staging",
|
|
],
|
|
},
|
|
{
|
|
"name": "messenger-worker",
|
|
"credentials": [{
|
|
"accessKey": values["worker_s3_access_key_id"],
|
|
"secretKey": values["worker_s3_secret_access_key"],
|
|
}],
|
|
"actions": [
|
|
"Read:messenger-staging",
|
|
"Write:messenger-staging",
|
|
"Write:messenger-attachments",
|
|
"Write:messenger-variants",
|
|
],
|
|
},
|
|
*[
|
|
{
|
|
"name": f"messenger-{service}",
|
|
"credentials": [{
|
|
"accessKey": values[f"{service}_s3_access_key_id"],
|
|
"secretKey": values[f"{service}_s3_secret_access_key"],
|
|
}],
|
|
"actions": [],
|
|
}
|
|
for service in ("publisher", "migrate")
|
|
],
|
|
{
|
|
"name": "messenger-reconciler",
|
|
"credentials": [{
|
|
"accessKey": values["reconciler_s3_access_key_id"],
|
|
"secretKey": values["reconciler_s3_secret_access_key"],
|
|
}],
|
|
"actions": [
|
|
f"{action}:{bucket}"
|
|
for bucket in ("messenger-staging", "messenger-attachments", "messenger-variants")
|
|
for action in ("Read", "Write", "List")
|
|
],
|
|
},
|
|
]
|
|
}
|
|
descriptor = os.open(SECRET_DIR / "s3.json", os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0o600)
|
|
with os.fdopen(descriptor, "w", encoding="utf-8") as output:
|
|
json.dump(s3_config, output, separators=(",", ":"))
|
|
output.write("\n")
|
|
iam_config = {
|
|
"sts": {"signingKey": "base64-" + base64.urlsafe_b64encode(secrets.token_bytes(32)).decode("ascii")},
|
|
"defaultEffect": "Deny",
|
|
}
|
|
descriptor = os.open(SECRET_DIR / "s3_iam.json", os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0o600)
|
|
with os.fdopen(descriptor, "w", encoding="utf-8") as output:
|
|
json.dump(iam_config, output, separators=(",", ":"))
|
|
output.write("\n")
|
|
role_dir = SECRET_DIR / "openbao"
|
|
role_dir.mkdir(mode=0o700, exist_ok=True)
|
|
for service in ("api", "worker", "publisher", "migrate", "reconciler"):
|
|
for suffix in ("role_id", "secret_id"):
|
|
path = role_dir / f"{service}_{suffix}"
|
|
descriptor = os.open(path, os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0o600)
|
|
os.close(descriptor)
|
|
print("Local secrets generated with owner-only permissions; values were not printed.")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|