#!/usr/bin/env python3 import argparse import os import secrets from pathlib import Path ROOT = Path(__file__).resolve().parents[1] def main(): parser = argparse.ArgumentParser(description="Create local Docker Compose secret files.") parser.add_argument("--force", action="store_true", help="replace existing local secret files") args = parser.parse_args() directory = ROOT / "secrets" directory.mkdir(mode=0o700, exist_ok=True) files = [directory / "postgres_password", directory / "database_url", directory / "redis_url"] if not args.force and any(path.exists() for path in files): raise SystemExit("Secret files already exist. Use --force only if you intend to rotate credentials.") password = secrets.token_hex(24) values = { "postgres_password": password + "\n", "database_url": f"postgresql://messenger:{password}@postgres:5432/messenger?connect_timeout=5\n", "redis_url": "redis://valkey:6379/0\n", } for name, value in values.items(): path = directory / name path.write_text(value, encoding="utf-8") try: os.chmod(path, 0o600) except OSError: pass print("Local secret files created. Values were not printed.") if __name__ == "__main__": main()