Files
mtusi2026lab4/scripts/init-secrets.py
2026-10-22 11:37:09 +03:00

37 lines
1.4 KiB
Python

#!/usr/bin/env python3
"""Create local credentials without printing or overwriting existing values."""
import json
from pathlib import Path
import secrets
root = Path(__file__).resolve().parents[1] / "secrets"
root.mkdir(mode=0o700, exist_ok=True)
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
password = write("postgres_password", secrets.token_hex(32))
write("database_url", f"postgresql://messenger:{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))
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"]},
]}, indent=2) + "\n")
config_path.chmod(0o600)
print("Local secrets ready; existing values preserved.")