forked from mtusi2026/mtusi2026lab4
25 lines
970 B
Python
25 lines
970 B
Python
"""Run in an API container: python - < student-tests/storage.py."""
|
|
import hashlib
|
|
import sys
|
|
import urllib.error
|
|
import urllib.request
|
|
sys.path.insert(0, '/app/src')
|
|
from db import connect
|
|
from storage import s3, BUCKET
|
|
|
|
with connect() as db:
|
|
rows = db.execute('SELECT object_key, size_bytes, sha256 FROM attachments').fetchall()
|
|
assert rows
|
|
for row in rows:
|
|
response = s3.get_object(Bucket=BUCKET, Key=row['object_key'])
|
|
with response['Body'] as stream:
|
|
data = stream.read(10485761)
|
|
assert len(data) == row['size_bytes'] and hashlib.sha256(data).hexdigest() == row['sha256']
|
|
try:
|
|
urllib.request.urlopen(s3.meta.endpoint_url + '/' + BUCKET + '/' + row['object_key'], timeout=5)
|
|
except urllib.error.HTTPError as exc:
|
|
assert exc.code == 403
|
|
else:
|
|
raise AssertionError('Anonymous download succeeded')
|
|
print('PASS: all persisted attachments match S3 size/SHA256; anonymous GET of every known object returns 403')
|