Files
mtusi2026lab5/src/messenger/s3bootstrap.py
T

68 lines
2.3 KiB
Python

"""Idempotent bucket bootstrap (one-shot `s3-init` service, admin credentials).
Creates the private bucket if absent; re-running is a no-op. Access rights are
not stored in the bucket: SeaweedFS reads identities (admin / messenger-api, no
anonymous identity) from the s3 config secret, so the bucket stays private.
"""
from __future__ import annotations
import logging
import sys
import time
import urllib.error
import urllib.request
from botocore.exceptions import BotoCoreError, ClientError
from messenger import config, s3
logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s %(message)s")
log = logging.getLogger("messenger.s3bootstrap")
def _anonymous_status(url: str) -> int:
try:
with urllib.request.urlopen(url, timeout=5) as resp:
return resp.status
except urllib.error.HTTPError as exc:
return exc.code
def main() -> int:
bucket = config.s3_bucket()
admin = s3.build_client(config.s3_admin_access_key_id(), config.s3_admin_secret_access_key())
deadline = time.monotonic() + 90
while True:
try:
admin.head_bucket(Bucket=bucket)
log.info("bucket exists: %s", bucket)
break
except ClientError as exc:
status = exc.response.get("ResponseMetadata", {}).get("HTTPStatusCode")
if status != 404:
raise
try:
admin.create_bucket(Bucket=bucket)
log.info("bucket created: %s", bucket)
except ClientError as create_exc:
code = create_exc.response.get("Error", {}).get("Code", "")
if code not in ("BucketAlreadyOwnedByYou", "BucketAlreadyExists"):
raise
log.info("bucket appeared concurrently: %s", bucket)
except BotoCoreError as exc:
if time.monotonic() > deadline:
raise
log.info("waiting for S3 (%s)", type(exc).__name__)
time.sleep(2)
status = _anonymous_status(config.s3_endpoint().rstrip("/") + "/" + bucket + "?list-type=2")
if status != 403:
log.error("bucket is not private: anonymous list returned %s", status)
return 1
log.info("anonymous access denied (HTTP %s); bootstrap complete", status)
return 0
if __name__ == "__main__":
sys.exit(main())