Files
mtusi2026lab4/scripts/check-s3-anonymous.py
T

22 lines
649 B
Python
Executable File

#!/usr/bin/env python3
import sys
import urllib.error
import urllib.request
bucket, key = sys.argv[1:3]
base = 'http://s3:9000/' + bucket
attempts = (
('GET', '/<object>', base + '/' + key, None),
('GET', '/', base + '/', None),
('PUT', '/anon.txt', base + '/anon.txt', b'x'),
)
failed = False
for method, label, url, data in attempts:
try:
code = urllib.request.urlopen(urllib.request.Request(url, data=data, method=method), timeout=5).status
except urllib.error.HTTPError as error:
code = error.code
print(method, bucket + label, '->', code)
failed = failed or code != 403
sys.exit(1 if failed else 0)