forked from mtusi2026/mtusi2026lab7
92 lines
3.9 KiB
Python
92 lines
3.9 KiB
Python
"""Bootstrap S3 (SeaweedFS): приватный bucket и проверка приватности. Одноразовый сервис s3-init (CN=s3-init).
|
||
|
||
lab7: admin-ключ S3 — из OpenBao (роль s3-init видит только его), соединение HTTPS с проверкой учебного CA.
|
||
Ключи сервисов (api, worker, janitor) с правами по prefix заданы в secrets/s3_config.json; s3-init их не видит,
|
||
их ограничения проверяет student-tests/test_least_privilege.py изнутри самих сервисов.
|
||
Идемпотентен: повторный запуск (docker compose run --rm s3-init) ничего не ломает. Значения ключей не печатаются.
|
||
"""
|
||
import logging
|
||
import os
|
||
import sys
|
||
import time
|
||
import urllib.error
|
||
import urllib.request
|
||
|
||
import boto3
|
||
from botocore.config import Config
|
||
from botocore.exceptions import BotoCoreError, ClientError
|
||
|
||
import settings
|
||
import tlsutil
|
||
from secretstore import store
|
||
|
||
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, verify=settings.TLS_CA_FILE,
|
||
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:
|
||
# HTTPS с проверкой CA и имени, без подписи запроса и без клиентского сертификата
|
||
opener = urllib.request.build_opener(urllib.request.ProxyHandler({}),
|
||
urllib.request.HTTPSHandler(context=tlsutil.client_context(cert=False)))
|
||
try:
|
||
with opener.open(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
|
||
store.start()
|
||
admin = client(store.get("s3_admin_access_key_id"), store.get("s3_admin_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")
|
||
|
||
# Приватность: анонимные запросы без подписи.
|
||
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__":
|
||
exit_code = main()
|
||
sys.stdout.flush()
|
||
os._exit(exit_code) # фоновый поток secretstore не держит одноразовый процесс
|