86 lines
3.2 KiB
Python
86 lines
3.2 KiB
Python
#!/usr/bin/env python3
|
|
"""Create local synthetic secrets in ./secrets (idempotent, prints only file names).
|
|
|
|
Existing files are kept, so re-running never rotates credentials of a running
|
|
stack. secrets/s3_config.json (SeaweedFS identities) is always re-derived from the
|
|
key files and S3_BUCKET (.env or environment):
|
|
* admin — Admin/Read/Write/List, used only by the one-shot s3-init;
|
|
* messenger-api — Read/Write/List on the attachments bucket only, used by API;
|
|
* no "anonymous" identity → unauthenticated S3 requests are denied.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import os
|
|
import secrets
|
|
import string
|
|
from pathlib import Path
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
SECRETS = ROOT / "secrets"
|
|
ALNUM = string.ascii_letters + string.digits
|
|
|
|
|
|
def token(length: int, alphabet: str = ALNUM) -> str:
|
|
return "".join(secrets.choice(alphabet) for _ in range(length))
|
|
|
|
|
|
def bucket_name() -> str:
|
|
value = os.environ.get("S3_BUCKET", "").strip()
|
|
env_file = ROOT / ".env"
|
|
if not value and env_file.is_file():
|
|
for line in env_file.read_text(encoding="utf-8").splitlines():
|
|
if line.startswith("S3_BUCKET="):
|
|
value = line.split("=", 1)[1].strip()
|
|
return value or "messenger-attachments"
|
|
|
|
|
|
def ensure(name: str, factory) -> str:
|
|
path = SECRETS / name
|
|
if path.is_file() and path.read_text(encoding="utf-8").strip():
|
|
print("keep secrets/" + name)
|
|
return path.read_text(encoding="utf-8").strip()
|
|
value = factory()
|
|
path.write_bytes(value.encode("utf-8"))
|
|
print("create secrets/" + name)
|
|
return value
|
|
|
|
|
|
def main() -> int:
|
|
SECRETS.mkdir(exist_ok=True)
|
|
pg_password = ensure("postgres_password", lambda: token(32))
|
|
ensure("database_url", lambda: f"postgresql://messenger:{pg_password}@postgres:5432/messenger")
|
|
ensure("cache_url", lambda: "redis://redis:6379/0")
|
|
broker_password = ensure("broker_password", lambda: token(32))
|
|
ensure(
|
|
"broker_url",
|
|
lambda: f"amqp://messenger:{broker_password}@broker:5672/%2F",
|
|
)
|
|
admin_key = ensure("s3_admin_access_key_id", lambda: "ADM" + token(17, string.ascii_uppercase + string.digits))
|
|
admin_secret = ensure("s3_admin_secret_access_key", lambda: token(40))
|
|
api_key = ensure("s3_access_key_id", lambda: "API" + token(17, string.ascii_uppercase + string.digits))
|
|
api_secret = ensure("s3_secret_access_key", lambda: token(40))
|
|
bucket = bucket_name()
|
|
config = {
|
|
"identities": [
|
|
{
|
|
"name": "admin",
|
|
"credentials": [{"accessKey": admin_key, "secretKey": admin_secret}],
|
|
"actions": ["Admin", "Read", "Write", "List", "Tagging"],
|
|
},
|
|
{
|
|
"name": "messenger-api",
|
|
"credentials": [{"accessKey": api_key, "secretKey": api_secret}],
|
|
"actions": [f"Read:{bucket}", f"Write:{bucket}", f"List:{bucket}"],
|
|
},
|
|
]
|
|
}
|
|
(SECRETS / "s3_config.json").write_bytes(json.dumps(config, indent=1).encode("utf-8"))
|
|
print(f"write secrets/s3_config.json (bucket {bucket}; identities admin, messenger-api; no anonymous)")
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|