forked from mtusi2026/mtusi2026lab4
Completed for a grade of three
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Stop the exact API instance which accepted the upload, then verify the original.
|
||||
Creates synthetic data; restores the stopped replica in finally. No credentials in output.
|
||||
"""
|
||||
import hashlib
|
||||
import os
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
import sys
|
||||
import time
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
sys.path.insert(0, str(ROOT / 'tests'))
|
||||
from client import Client, Failure, check
|
||||
|
||||
|
||||
def main():
|
||||
c = Client(timeout=10)
|
||||
alice, bob, eve, chat = c.bootstrap()
|
||||
content = (ROOT / 'tests/fixtures/sample.png').read_bytes()
|
||||
result = c.call('POST', '/api/v1/chats/' + chat['id'] + '/attachments?filename=sample.png',
|
||||
201, actor=alice, raw=content, headers={'Content-Type': 'image/png'}, schema='Attachment')
|
||||
attachment = result['json']
|
||||
target = result['headers']['X-Instance-Id']
|
||||
c.message(alice, chat, '', attachments=[attachment['id']])
|
||||
print('Upload: 201 ready; instance=' + target, flush=True)
|
||||
# Probe the real known object anonymously from the internal network, not an unknown key.
|
||||
probe = subprocess.run(['docker', 'compose', 'exec', '-T', 'lb', 'wget', '-S', '-O', '/dev/null',
|
||||
'http://s3:9000/' + os.environ.get('S3_BUCKET', 'messenger-attachments') + '/originals/' + attachment['id']],
|
||||
cwd=ROOT, capture_output=True, text=True)
|
||||
check(probe.returncode != 0 and '403' in probe.stderr, 'private bucket did not reject anonymous GET')
|
||||
print('Anonymous S3 GET of known object: 403', flush=True)
|
||||
path = '/api/v1/attachments/' + attachment['id']
|
||||
c.call('GET', path, 404, actor=eve)
|
||||
c.call('GET', path + '/content', 404, actor=eve)
|
||||
print('Outsider metadata/content: 404', flush=True)
|
||||
try:
|
||||
subprocess.run([str(ROOT / 'scripts/stop-one.sh')], cwd=ROOT,
|
||||
env=dict(os.environ, TARGET_INSTANCE_ID=target), check=True, capture_output=True)
|
||||
deadline = time.monotonic() + 15
|
||||
while True:
|
||||
try:
|
||||
download = c.call('GET', path + '/content', actor=bob)
|
||||
check(download['headers']['X-Instance-Id'] != target, 'stopped instance returned content')
|
||||
check(download['bytes'] == content, 'download differs from original')
|
||||
print('Download: 200; instance=' + download['headers']['X-Instance-Id'], flush=True)
|
||||
print('SHA-256: ' + hashlib.sha256(content).hexdigest(), flush=True)
|
||||
break
|
||||
except Failure:
|
||||
if time.monotonic() >= deadline:
|
||||
raise
|
||||
time.sleep(0.2)
|
||||
finally:
|
||||
subprocess.run(['docker', 'compose', 'start', 'api'], cwd=ROOT, check=True, capture_output=True)
|
||||
print('PASS: uploading replica stopped; original served by another replica; stopped replica restored')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
@@ -9,7 +9,7 @@ mkdir -p secrets
|
||||
# Readable by the non-root users inside the containers (bind-mounted as is).
|
||||
umask 022
|
||||
|
||||
for name in db_password redis_password; do
|
||||
for name in db_password redis_password s3_access_key_id s3_secret_access_key; do
|
||||
if [ ! -s "secrets/$name" ]; then
|
||||
openssl rand -hex 24 > "secrets/$name"
|
||||
echo "created secrets/$name"
|
||||
|
||||
+2
-2
@@ -1,7 +1,7 @@
|
||||
#!/bin/sh
|
||||
# Restarts the balancer, every API replica, PostgreSQL and Redis, keeping the named volume (no `down -v`, no re-seed).
|
||||
# Restarts the balancer, every API replica, PostgreSQL, Redis and S3, keeping the named volume (no `down -v`, no re-seed).
|
||||
set -eu
|
||||
cd "$(dirname "$0")/.."
|
||||
|
||||
docker compose stop lb api redis postgres
|
||||
docker compose stop lb api redis postgres s3
|
||||
docker compose up -d --wait
|
||||
|
||||
Reference in New Issue
Block a user