forked from mtusi2026/mtusi2026lab5
104 lines
4.2 KiB
Python
104 lines
4.2 KiB
Python
"""Bootstrap S3 (SeaweedFS): приватный bucket и проверка прав. Запускается одноразовым сервисом s3-init.
|
|
|
|
Идемпотентен: повторный запуск (docker compose run --rm s3-init) ничего не ломает.
|
|
Ключи и их права заданы статически в secrets/s3_config.json (scripts/init_secrets.py):
|
|
admin — Admin/Read/Write/List на всё, есть только у s3 и s3-init;
|
|
messenger-api — Read/Write/List только на bucket S3_BUCKET, его получают api и janitor.
|
|
Значения ключей не печатаются.
|
|
"""
|
|
import logging
|
|
import sys
|
|
import time
|
|
import urllib.error
|
|
import urllib.request
|
|
import uuid
|
|
|
|
import boto3
|
|
from botocore.config import Config
|
|
from botocore.exceptions import BotoCoreError, ClientError
|
|
|
|
import settings
|
|
|
|
logging.basicConfig(level=logging.WARNING)
|
|
|
|
|
|
def client(key_id: str, secret: str):
|
|
return boto3.client(
|
|
"s3", endpoint_url=settings.S3_ENDPOINT, region_name=settings.S3_REGION,
|
|
aws_access_key_id=key_id, aws_secret_access_key=secret,
|
|
config=Config(signature_version="s3v4", s3={"addressing_style": "path"}, connect_timeout=2,
|
|
read_timeout=10, retries={"total_max_attempts": 1},
|
|
request_checksum_calculation="when_required", response_checksum_validation="when_required"),
|
|
)
|
|
|
|
|
|
def code(exc: ClientError) -> str:
|
|
return exc.response.get("Error", {}).get("Code", "?")
|
|
|
|
|
|
def anonymous_status(url: str, method: str = "GET") -> int:
|
|
try:
|
|
with urllib.request.urlopen(urllib.request.Request(url, method=method), timeout=5) as reply:
|
|
return reply.status
|
|
except urllib.error.HTTPError as exc:
|
|
return exc.code
|
|
|
|
|
|
def main() -> int:
|
|
bucket = settings.S3_BUCKET
|
|
admin = client(settings.read_secret("S3_ADMIN_ACCESS_KEY_ID_FILE", "S3_ADMIN_ACCESS_KEY_ID"),
|
|
settings.read_secret("S3_ADMIN_SECRET_ACCESS_KEY_FILE", "S3_ADMIN_SECRET_ACCESS_KEY"))
|
|
api = client(settings.S3_ACCESS_KEY_ID, settings.S3_SECRET_ACCESS_KEY)
|
|
|
|
deadline = time.monotonic() + 90
|
|
while True:
|
|
try:
|
|
admin.list_buckets()
|
|
break
|
|
except (BotoCoreError, ClientError) as exc:
|
|
if time.monotonic() > deadline:
|
|
print(f"s3-init: S3 is not ready: {type(exc).__name__}")
|
|
return 1
|
|
time.sleep(1)
|
|
|
|
try:
|
|
admin.create_bucket(Bucket=bucket)
|
|
print(f"s3-init: bucket {bucket} created")
|
|
except ClientError as exc:
|
|
if code(exc) not in ("BucketAlreadyOwnedByYou", "BucketAlreadyExists"):
|
|
print(f"s3-init: cannot create bucket: {code(exc)}")
|
|
return 1
|
|
print(f"s3-init: bucket {bucket} exists")
|
|
|
|
# Ключ API работает со своим bucket ...
|
|
probe = "originals/s3-init-probe-" + uuid.uuid4().hex
|
|
api.put_object(Bucket=bucket, Key=probe, Body=b"probe")
|
|
api.get_object(Bucket=bucket, Key=probe)["Body"].read()
|
|
api.list_objects_v2(Bucket=bucket, MaxKeys=1)
|
|
api.delete_object(Bucket=bucket, Key=probe)
|
|
print("s3-init: api key can put/get/list/delete objects of its bucket")
|
|
# ... и не может администрировать хранилище.
|
|
intruder = "s3-init-intruder-" + uuid.uuid4().hex[:8]
|
|
try:
|
|
api.create_bucket(Bucket=intruder)
|
|
admin.delete_bucket(Bucket=intruder)
|
|
print("s3-init: api key must NOT be able to create buckets")
|
|
return 1
|
|
except ClientError as exc:
|
|
print(f"s3-init: api key cannot create buckets ({code(exc)})")
|
|
|
|
# Приватность: анонимные запросы без подписи.
|
|
base = settings.S3_ENDPOINT.rstrip("/") + "/" + bucket
|
|
statuses = {"list": anonymous_status(base + "?list-type=2"),
|
|
"get": anonymous_status(base + "/originals/any"),
|
|
"put": anonymous_status(base + "/originals/any", "PUT")}
|
|
if any(status not in (401, 403) for status in statuses.values()):
|
|
print(f"s3-init: bucket is NOT private, anonymous statuses: {statuses}")
|
|
return 1
|
|
print(f"s3-init: bucket is private (anonymous list/get/put -> {sorted(set(statuses.values()))})")
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main())
|