forked from mtusi2026/mtusi2026lab4
Реализовать ЛР4: S3-хранилище и вложения на оценку 3
Стенд: PostgreSQL 16 + две API-реплики за HAProxy + SeaweedFS S3 в приватном bucket. Метаданные вложений в PostgreSQL, байты в S3, object key назначает сервер. Лимиты 10 MiB и 40 млн пикселей, MIME по магическим байтам, проверка filename. Добавлены smoke/негативные тесты, хуки restart/stop-one, сценарий отказа S3 и отчёт с доказательствами.
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
"""Evidence helper: the bucket must not serve objects to anonymous clients.
|
||||
|
||||
Runs inside an API container (it needs the API's S3 credentials and network
|
||||
access to the S3 endpoint). Prints a small JSON summary without secrets.
|
||||
"""
|
||||
import json
|
||||
import os
|
||||
import sys
|
||||
import urllib.error
|
||||
import urllib.request
|
||||
from urllib.parse import quote
|
||||
|
||||
import boto3
|
||||
from botocore.config import Config as BotoConfig
|
||||
|
||||
|
||||
def main():
|
||||
endpoint = os.environ["S3_ENDPOINT"].rstrip("/")
|
||||
bucket = os.environ["S3_BUCKET"]
|
||||
client = boto3.client(
|
||||
"s3",
|
||||
endpoint_url=endpoint,
|
||||
region_name=os.environ.get("S3_REGION", "us-east-1"),
|
||||
aws_access_key_id=os.environ["S3_ACCESS_KEY_ID"],
|
||||
aws_secret_access_key=os.environ["S3_SECRET_ACCESS_KEY"],
|
||||
config=BotoConfig(signature_version="s3v4", s3={"addressing_style": "path"}),
|
||||
)
|
||||
objects = client.list_objects_v2(Bucket=bucket).get("Contents", [])
|
||||
summary = {"objects_in_bucket": len(objects), "first_key": None}
|
||||
if not objects:
|
||||
print(json.dumps(summary))
|
||||
return 0
|
||||
key = objects[0]["Key"]
|
||||
summary["first_key"] = key
|
||||
|
||||
def anonymous(url):
|
||||
try:
|
||||
with urllib.request.urlopen(url, timeout=5) as response:
|
||||
return response.status
|
||||
except urllib.error.HTTPError as error:
|
||||
return error.code
|
||||
|
||||
summary["anonymous_object_get_status"] = anonymous(endpoint + "/" + bucket + "/" + quote(key, safe="/"))
|
||||
summary["anonymous_bucket_list_status"] = anonymous(endpoint + "/" + bucket + "?list-type=2")
|
||||
print(json.dumps(summary, indent=2))
|
||||
return 0 if summary["anonymous_object_get_status"] >= 400 else 1
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
Reference in New Issue
Block a user