forked from mtusi2026/mtusi2026lab4
33 lines
1.4 KiB
Python
33 lines
1.4 KiB
Python
"""One-off creation of private SeaweedFS S3 identities; secrets stay in Docker volume."""
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
|
|
|
|
def main():
|
|
bucket = os.environ["S3_BUCKET"]
|
|
identities = [
|
|
{"name": "bucket-bootstrap", "credentials": [{"accessKey": os.environ["S3_ADMIN_ACCESS_KEY"],
|
|
"secretKey": os.environ["S3_ADMIN_SECRET_KEY"]}],
|
|
"actions": ["Admin", "Read", "Write", "List"]},
|
|
{"name": "messenger-api", "credentials": [{"accessKey": os.environ["S3_ACCESS_KEY"],
|
|
"secretKey": os.environ["S3_SECRET_KEY"]}],
|
|
"actions": [f"Read:{bucket}", f"Write:{bucket}", f"List:{bucket}"]},
|
|
]
|
|
if len({entry["credentials"][0]["accessKey"] for entry in identities}) != 2:
|
|
raise ValueError("Admin and API credentials must differ")
|
|
path = Path("/s3-config/config.json")
|
|
path.parent.mkdir(parents=True, exist_ok=True)
|
|
tmp = path.with_suffix(".tmp")
|
|
old_umask = os.umask(0o077)
|
|
try:
|
|
tmp.write_text(json.dumps({"identities": identities}, separators=(",", ":")), encoding="utf-8")
|
|
os.chmod(tmp, 0o644) # readable by the SeaweedFS runtime UID; Docker volume stays private
|
|
os.replace(tmp, path)
|
|
finally:
|
|
os.umask(old_umask)
|
|
print("S3 identities configured (no secrets printed)", flush=True)
|
|
|
|
if __name__ == "__main__":
|
|
main()
|