Files

60 lines
2.8 KiB
Python

#!/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',
202, actor=alice, raw=content, headers={'Content-Type': 'image/png'}, schema='Attachment')
attachment = c.wait_ready(bob, result['json'])
target = result['headers']['X-Instance-Id']
c.message(alice, chat, '', attachments=[attachment['id']])
print('Upload: 202 queued, then 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()