forked from mtusi2026/mtusi2026lab5
62 lines
2.0 KiB
Python
62 lines
2.0 KiB
Python
#!/usr/bin/env python3
|
|
"""Прямой анонимный доступ к bucket должен быть закрыт."""
|
|
|
|
import json
|
|
import os
|
|
import sys
|
|
import time
|
|
import urllib.error
|
|
import urllib.request
|
|
from pathlib import Path
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
sys.path.insert(0, str(ROOT / "tests"))
|
|
from client import Client, Failure, check, run
|
|
|
|
EVIDENCE = ROOT / "evidence"
|
|
BASE = os.environ.get("BASE_URL", "http://127.0.0.1:8080").rstrip("/")
|
|
S3 = os.environ.get("S3_PUBLIC_URL", "http://127.0.0.1:9000").rstrip("/")
|
|
BUCKET = os.environ.get("S3_BUCKET", "messenger-attachments")
|
|
|
|
|
|
def anonymous(path: str) -> int:
|
|
request = urllib.request.Request(S3 + path, method="GET")
|
|
try:
|
|
with urllib.request.urlopen(request, timeout=10) as response:
|
|
return response.status
|
|
except urllib.error.HTTPError as exc:
|
|
return exc.status
|
|
except (urllib.error.URLError, TimeoutError, OSError) as exc:
|
|
raise Failure("direct s3 probe failed: " + type(exc).__name__) from None
|
|
|
|
|
|
def main():
|
|
client = Client(BASE, timeout=20)
|
|
alice, _bob, _eve, chat = client.bootstrap()
|
|
att, content = client.attachment(alice, chat)
|
|
att = client.wait_ready(alice, att)
|
|
listed = anonymous("/" + BUCKET)
|
|
obj = anonymous("/" + BUCKET + "/att/" + att["id"])
|
|
check(listed in {403, 404}, "anonymous list status %s" % listed)
|
|
check(obj in {403, 404}, "anonymous object status %s" % obj)
|
|
download = client.call("GET", "/api/v1/attachments/%s/content" % att["id"], actor=alice)
|
|
check(download["bytes"] == content, "authorized download after anonymous probe")
|
|
EVIDENCE.mkdir(exist_ok=True)
|
|
(EVIDENCE / "s3_private.json").write_text(
|
|
json.dumps(
|
|
{
|
|
"date": time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime()),
|
|
"anonymous_list": listed,
|
|
"anonymous_object": obj,
|
|
},
|
|
indent=2,
|
|
)
|
|
+ "\n",
|
|
encoding="utf-8",
|
|
)
|
|
print("PASS: private bucket rejects anonymous GET")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
run(main)
|