from pathlib import Path import json import secrets root = Path(__file__).resolve().parent.parent directory = root / "secrets" targets = [ root / ".env", directory / "s3_config.json", directory / "s3_access_key_id", directory / "s3_secret_access_key", directory / "broker_url", ] existing = [path for path in targets if path.exists()] if existing: raise SystemExit( "STOP: local configuration already exists; " "nothing changed. Do not delete or rotate it blindly." ) directory.mkdir(mode=0o700, exist_ok=True) directory.chmod(0o700) postgres_password = secrets.token_hex(24) rabbit_password = secrets.token_hex(24) access_key = "lab5" + secrets.token_hex(12) secret_key = secrets.token_hex(32) config = { "identities": [ { "name": "lab5", "credentials": [ { "accessKey": access_key, "secretKey": secret_key, } ], "actions": ["Admin", "Read", "Write", "List", "Tagging"], } ] } contents = { root / ".env": ( f"POSTGRES_PASSWORD={postgres_password}\n" f"RABBITMQ_PASSWORD={rabbit_password}\n" "SESSION_TTL_SECONDS=3600\n" "USER_CACHE_TTL_SECONDS=60\n" ), directory / "s3_config.json": json.dumps(config, indent=2) + "\n", directory / "s3_access_key_id": access_key + "\n", directory / "s3_secret_access_key": secret_key + "\n", directory / "broker_url": ( f"amqp://messenger:{rabbit_password}@broker:5672/%2F\n" ), } for path, content in contents.items(): with path.open("x") as stream: path.chmod(0o600 if path.name == ".env" else 0o644) stream.write(content) print("Created local lab5 configuration; secret values were not printed.")