forked from mtusi2026/mtusi2026lab4
44 lines
1.6 KiB
Python
44 lines
1.6 KiB
Python
#!/usr/bin/env python3
|
|
"""Отправить анонимный ListBucket из сети приложения и подтвердить private bucket."""
|
|
from pathlib import Path
|
|
import subprocess
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
|
|
|
|
def main():
|
|
env_path = ROOT / '.env'
|
|
if not env_path.is_file():
|
|
raise SystemExit('Create local .env from .env.example first')
|
|
values = {}
|
|
for line in env_path.read_text().splitlines():
|
|
if '=' in line and not line.lstrip().startswith('#'):
|
|
key, value = line.split('=', 1)
|
|
values[key.strip()] = value.strip()
|
|
bucket = values.get('S3_BUCKET', 'messenger-attachments')
|
|
url = 'http://s3:9000/' + bucket + '?list-type=2'
|
|
code = (
|
|
'import urllib.error, urllib.request\n'
|
|
'url = ' + repr(url) + '\n'
|
|
'try:\n'
|
|
' urllib.request.urlopen(url, timeout=5)\n'
|
|
'except urllib.error.HTTPError as error:\n'
|
|
' print("HTTP", error.code)\n'
|
|
' raise SystemExit(0 if error.code in (401, 403) else 1)\n'
|
|
'except Exception as error:\n'
|
|
' print(type(error).__name__)\n'
|
|
' raise SystemExit(1)\n'
|
|
'raise SystemExit("anonymous bucket listing unexpectedly succeeded")\n'
|
|
)
|
|
result = subprocess.run(
|
|
['docker', 'compose', '--env-file', '.env', 'exec', '-T', 'api', 'python', '-c', code],
|
|
cwd=ROOT, check=False,
|
|
)
|
|
if result.returncode:
|
|
raise SystemExit('FAIL: anonymous S3 bucket listing was not denied')
|
|
print('PASS: private bucket rejects unauthenticated listing')
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|