55 lines
2.1 KiB
Python
55 lines
2.1 KiB
Python
import json
|
|
import os
|
|
import secrets
|
|
import shutil
|
|
from pathlib import Path
|
|
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
|
|
|
|
def main():
|
|
env_file = ROOT / ".env"
|
|
if not env_file.exists():
|
|
shutil.copyfile(ROOT / ".env.example", env_file)
|
|
env = {}
|
|
for line in env_file.read_text(encoding="utf-8-sig").splitlines():
|
|
key, separator, value = line.partition("=")
|
|
if separator and not key.strip().startswith("#"):
|
|
env[key.strip()] = value.strip()
|
|
bucket = os.environ.get("S3_BUCKET", env.get("S3_BUCKET", "messenger-attachments"))
|
|
folder = ROOT / "secrets"
|
|
folder.mkdir(mode=0o700, exist_ok=True)
|
|
|
|
def initialize(name, value):
|
|
path = folder / name
|
|
try:
|
|
with path.open("x", encoding="ascii") as output:
|
|
output.write(value + "\n")
|
|
except FileExistsError:
|
|
pass
|
|
return path.read_text(encoding="ascii").strip()
|
|
|
|
password = initialize("postgres_password", secrets.token_hex(32))
|
|
initialize("database_url", f"postgresql://messenger:{password}@db:5432/messenger")
|
|
initialize("cursor_key", secrets.token_hex(32))
|
|
identities = []
|
|
for name, prefix, actions in (
|
|
("bootstrap", "s3_admin_", ["Admin", "Read", "Write", "List"]),
|
|
("messenger", "s3_", [f"{action}:{bucket}" for action in ("Read", "Write", "List")]),
|
|
):
|
|
access_key = initialize(prefix + "access_key_id", secrets.token_hex(32))
|
|
secret_key = initialize(prefix + "secret_access_key", secrets.token_hex(32))
|
|
identities.append({"name": name, "credentials": [{"accessKey": access_key, "secretKey": secret_key}],
|
|
"actions": actions})
|
|
initialize("s3_config.json", json.dumps({"identities": identities}))
|
|
broker_password = initialize("broker_password", secrets.token_hex(32))
|
|
initialize("rabbitmq.conf", f"default_user = messenger\ndefault_pass = {broker_password}")
|
|
initialize("broker_url", f"amqp://messenger:{broker_password}@broker:5672/%2F")
|
|
initialize("grafana_password", secrets.token_hex(32))
|
|
print("Local environment and secrets are ready.")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|