forked from mtusi2026/mtusi2026lab7
65 lines
3.4 KiB
Python
65 lines
3.4 KiB
Python
#!/usr/bin/env python3
|
|
"""Create local credentials without printing or overwriting existing values."""
|
|
import json
|
|
import base64
|
|
import hashlib
|
|
from pathlib import Path
|
|
import secrets
|
|
|
|
root = Path(__file__).resolve().parents[1] / "secrets"
|
|
root.mkdir(mode=0o700, exist_ok=True)
|
|
root.chmod(0o700)
|
|
|
|
|
|
def write(name, value):
|
|
path = root / name
|
|
if path.exists():
|
|
return path.read_text().strip()
|
|
with path.open("x") as stream:
|
|
path.chmod(0o600)
|
|
stream.write(value + "\n")
|
|
return value
|
|
|
|
|
|
write("grafana_password", secrets.token_urlsafe(24))
|
|
password = write("postgres_password", secrets.token_hex(32))
|
|
write("database_url", f"postgresql://messenger:{password}@postgres:5432/messenger")
|
|
for role in ("api", "worker", "outbox", "reconcile"):
|
|
role_password = write(f"database_{role}_password", secrets.token_hex(32))
|
|
write(f"database_{role}_url", f"postgresql://messenger_{role}:{role_password}@postgres:5432/messenger")
|
|
write("cursor_secret", secrets.token_hex(32))
|
|
admin_key = write("s3_admin_key", secrets.token_hex(16))
|
|
admin_secret = write("s3_admin_secret", secrets.token_hex(32))
|
|
app_key = write("s3_access_key_id", secrets.token_hex(16))
|
|
app_secret = write("s3_secret_access_key", secrets.token_hex(32))
|
|
worker_key = write("s3_worker_access_key_id", secrets.token_hex(16))
|
|
worker_secret = write("s3_worker_secret_access_key", secrets.token_hex(32))
|
|
reconcile_key = write("s3_reconcile_access_key_id", secrets.token_hex(16))
|
|
reconcile_secret = write("s3_reconcile_secret_access_key", secrets.token_hex(32))
|
|
for role, key in (("admin", admin_key), ("api", app_key), ("worker", worker_key), ("reconcile", reconcile_key)):
|
|
# Used by the S3 proxy to select a write-prefix policy; the gateway still
|
|
# verifies the complete SigV4 signature before accepting a request.
|
|
write(f"s3_{role}_acl", "^AWS4-HMAC-SHA256[[:space:]]+Credential=" + key + "/")
|
|
rabbitmq_password = write("rabbitmq_password", secrets.token_urlsafe(32))
|
|
write("broker_url", f"amqp://messenger:{rabbitmq_password}@broker:5672/%2F")
|
|
write("broker_tls_url", f"amqps://messenger:{rabbitmq_password}@broker:5671/%2F")
|
|
for role in ("api", "worker", "outbox"):
|
|
role_password = write(f"broker_{role}_password", secrets.token_hex(32))
|
|
write(f"broker_{role}_url", f"amqps://messenger_{role}:{role_password}@broker:5671/%2F")
|
|
salt = secrets.token_bytes(4)
|
|
digest = base64.b64encode(salt + hashlib.sha256(salt + role_password.encode()).digest()).decode()
|
|
write(f"broker_{role}_password_hash", digest)
|
|
config_path = root / "s3.json"
|
|
config_path.write_text(json.dumps({"identities": [
|
|
{"name": "bootstrap", "credentials": [{"accessKey": admin_key, "secretKey": admin_secret}],
|
|
"actions": ["Admin", "Read", "Write", "List", "Tagging"]},
|
|
{"name": "api", "credentials": [{"accessKey": app_key, "secretKey": app_secret}],
|
|
"actions": ["Read:messenger-attachments", "Write:messenger-attachments", "List:messenger-attachments"]},
|
|
{"name": "worker", "credentials": [{"accessKey": worker_key, "secretKey": worker_secret}],
|
|
"actions": ["Read:messenger-attachments", "Write:messenger-attachments", "List:messenger-attachments"]},
|
|
{"name": "reconcile", "credentials": [{"accessKey": reconcile_key, "secretKey": reconcile_secret}],
|
|
"actions": ["Read:messenger-attachments", "Write:messenger-attachments", "List:messenger-attachments"]},
|
|
]}, indent=2) + "\n")
|
|
config_path.chmod(0o600)
|
|
print("Local secrets ready; existing values preserved.")
|