forked from mtusi2026/mtusi2026lab4
lab4
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Check anonymous access to the private bucket from inside the Compose network."""
|
||||
from pathlib import Path
|
||||
import json
|
||||
import subprocess
|
||||
import sys
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
sys.path.insert(0, str(ROOT / 'tests'))
|
||||
from client import Failure, run
|
||||
|
||||
|
||||
def main():
|
||||
code = (
|
||||
"import os, urllib.error, urllib.request; "
|
||||
"bucket=os.environ['S3_BUCKET']; "
|
||||
"url='http://minio:9000/'+bucket+'?list-type=2&prefix=objects%2F'; "
|
||||
"req=urllib.request.Request(url); "
|
||||
"\ntry:\n urllib.request.urlopen(req, timeout=3)\n"
|
||||
"except urllib.error.HTTPError as error:\n"
|
||||
" raise SystemExit(0 if error.code == 403 else 1)\n"
|
||||
"raise SystemExit(1)"
|
||||
)
|
||||
result = subprocess.run(
|
||||
['docker', 'compose', 'exec', '-T', 'api', 'python', '-c', code],
|
||||
cwd=ROOT,
|
||||
capture_output=True,
|
||||
text=True,
|
||||
)
|
||||
if result.returncode:
|
||||
raise Failure('anonymous MinIO bucket request did not return HTTP 403')
|
||||
container_id = subprocess.run(
|
||||
['docker', 'compose', 'ps', '-q', 'minio'],
|
||||
cwd=ROOT,
|
||||
capture_output=True,
|
||||
text=True,
|
||||
check=True,
|
||||
).stdout.strip()
|
||||
if not container_id:
|
||||
raise Failure('MinIO container is not running')
|
||||
bindings = subprocess.run(
|
||||
['docker', 'inspect', '--format', '{{json .HostConfig.PortBindings}}', container_id],
|
||||
cwd=ROOT,
|
||||
capture_output=True,
|
||||
text=True,
|
||||
check=True,
|
||||
).stdout.strip()
|
||||
if any(value for value in json.loads(bindings).values()):
|
||||
raise Failure('MinIO S3 endpoint must not be published on the host')
|
||||
print('PASS: anonymous bucket access is denied and the S3 port is not published.')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
run(main)
|
||||
@@ -0,0 +1,287 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Student negative and concurrency checks for the lab 4 attachment API."""
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
import http.client
|
||||
import hashlib
|
||||
import json
|
||||
from pathlib import Path
|
||||
import re
|
||||
import struct
|
||||
import subprocess
|
||||
import sys
|
||||
import threading
|
||||
import time
|
||||
import zlib
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
sys.path.insert(0, str(ROOT / 'tests'))
|
||||
from client import Client, arguments, check, run
|
||||
from urllib.parse import quote, urlsplit
|
||||
|
||||
MAX_UPLOAD_BYTES = 10 * 1024 * 1024
|
||||
PARALLEL_UPLOADS = 10
|
||||
ANSI_ESCAPE = re.compile(r'\x1b\[[0-?]*[ -/]*[@-~]')
|
||||
|
||||
|
||||
def png_chunk(kind, data):
|
||||
return (
|
||||
struct.pack('>I', len(data))
|
||||
+ kind
|
||||
+ data
|
||||
+ struct.pack('>I', zlib.crc32(kind + data) & 0xffffffff)
|
||||
)
|
||||
|
||||
|
||||
def random_png(width=1600, height=1600):
|
||||
seed = 0x4C414234
|
||||
pixels = bytearray(width * height)
|
||||
for index in range(len(pixels)):
|
||||
seed = (1664525 * seed + 1013904223) & 0xffffffff
|
||||
pixels[index] = seed >> 24
|
||||
rows = b''.join(
|
||||
b'\x00' + pixels[row * width:(row + 1) * width]
|
||||
for row in range(height)
|
||||
)
|
||||
header = struct.pack('>IIBBBBB', width, height, 8, 0, 0, 0, 0)
|
||||
return (
|
||||
b'\x89PNG\r\n\x1a\n'
|
||||
+ png_chunk(b'IHDR', header)
|
||||
+ png_chunk(b'IDAT', zlib.compress(rows))
|
||||
+ png_chunk(b'IEND', b'')
|
||||
)
|
||||
|
||||
|
||||
def memory_bytes(value):
|
||||
match = re.match(r'\s*([\d.]+)\s*(B|KiB|MiB|GiB)', value)
|
||||
check(match is not None, 'could not parse docker stats memory value')
|
||||
factors = {'B': 1, 'KiB': 1024, 'MiB': 1024 ** 2, 'GiB': 1024 ** 3}
|
||||
return int(float(match.group(1)) * factors[match.group(2)])
|
||||
|
||||
|
||||
def api_memory(container_ids):
|
||||
output = subprocess.run(
|
||||
['docker', 'stats', '--no-stream', '--format', '{{.MemUsage}}', *container_ids],
|
||||
cwd=ROOT,
|
||||
check=True,
|
||||
capture_output=True,
|
||||
text=True,
|
||||
).stdout
|
||||
lines = [line.strip() for line in output.splitlines() if line.strip()]
|
||||
check(len(lines) == len(container_ids), 'docker stats did not report every API replica')
|
||||
return sum(memory_bytes(line.split('/', 1)[0]) for line in lines)
|
||||
|
||||
|
||||
def make_upload(client, actor, chat_id, filename, content, content_type='image/png'):
|
||||
return client.call(
|
||||
'POST',
|
||||
'/api/v1/chats/' + chat_id + '/attachments?filename=' + quote(filename, safe=''),
|
||||
201,
|
||||
actor=actor,
|
||||
raw=content,
|
||||
headers={'Content-Type': content_type},
|
||||
schema='Attachment',
|
||||
)['json']
|
||||
|
||||
|
||||
def slow_upload(base_url, actor, chat_id, index, content):
|
||||
endpoint = urlsplit(base_url or 'http://localhost:8080')
|
||||
check(endpoint.scheme == 'http', 'memory test currently requires the local HTTP endpoint')
|
||||
connection = http.client.HTTPConnection(
|
||||
endpoint.hostname, endpoint.port or 80, timeout=30
|
||||
)
|
||||
path = (
|
||||
'/api/v1/chats/' + chat_id + '/attachments?filename='
|
||||
+ quote('parallel-' + str(index) + '.png', safe='')
|
||||
)
|
||||
try:
|
||||
connection.putrequest('POST', path)
|
||||
connection.putheader('Authorization', 'Bearer ' + actor['token'])
|
||||
connection.putheader('Accept', 'application/json')
|
||||
connection.putheader('Content-Type', 'image/png')
|
||||
connection.putheader('Content-Length', str(len(content)))
|
||||
connection.putheader('Connection', 'close')
|
||||
connection.endheaders()
|
||||
chunk_size = 64 * 1024
|
||||
for offset in range(0, len(content), chunk_size):
|
||||
connection.send(content[offset:offset + chunk_size])
|
||||
time.sleep(0.05)
|
||||
response = connection.getresponse()
|
||||
payload = json.loads(response.read())
|
||||
check(response.status == 201, 'parallel upload did not return 201')
|
||||
check(bool(response.getheader('X-Request-Id')), 'parallel upload has no request ID')
|
||||
check(bool(response.getheader('X-Instance-Id')), 'parallel upload has no instance ID')
|
||||
from client import shape
|
||||
shape(payload, 'Attachment')
|
||||
return payload
|
||||
finally:
|
||||
connection.close()
|
||||
|
||||
|
||||
def main():
|
||||
args = arguments('Attachment negative tests and 10-way upload memory measurement')
|
||||
client = Client(args.base_url, args.ca_file, timeout=30)
|
||||
alice, bob, eve, chat = client.bootstrap()
|
||||
fixture = (ROOT / 'tests' / 'fixtures' / 'sample.png').read_bytes()
|
||||
attachment = make_upload(client, alice, chat['id'], 'sample.png', fixture)
|
||||
|
||||
large = b'x' * (MAX_UPLOAD_BYTES + 1)
|
||||
client.call(
|
||||
'POST',
|
||||
'/api/v1/chats/' + chat['id'] + '/attachments?filename=large.png',
|
||||
413,
|
||||
actor=alice,
|
||||
raw=large,
|
||||
headers={'Content-Type': 'image/png'},
|
||||
)
|
||||
pixel_header = (
|
||||
b'\x89PNG\r\n\x1a\n'
|
||||
+ png_chunk(
|
||||
b'IHDR',
|
||||
struct.pack('>IIBBBBB', 10000, 5000, 8, 0, 0, 0, 0),
|
||||
)
|
||||
+ png_chunk(b'IDAT', zlib.compress(b''))
|
||||
+ png_chunk(b'IEND', b'')
|
||||
)
|
||||
client.call(
|
||||
'POST',
|
||||
'/api/v1/chats/' + chat['id'] + '/attachments?filename=too-many-pixels.png',
|
||||
413,
|
||||
actor=alice,
|
||||
raw=pixel_header,
|
||||
headers={'Content-Type': 'image/png'},
|
||||
)
|
||||
client.call(
|
||||
'POST',
|
||||
'/api/v1/chats/' + chat['id'] + '/attachments?filename=broken.png',
|
||||
415,
|
||||
actor=alice,
|
||||
raw=b'not a PNG image',
|
||||
headers={'Content-Type': 'image/png'},
|
||||
)
|
||||
client.call(
|
||||
'POST',
|
||||
'/api/v1/chats/' + chat['id'] + '/attachments?filename=spoofed.jpg',
|
||||
415,
|
||||
actor=alice,
|
||||
raw=fixture,
|
||||
headers={'Content-Type': 'image/jpeg'},
|
||||
)
|
||||
client.call(
|
||||
'POST',
|
||||
'/api/v1/chats/' + chat['id'] + '/attachments?filename=..%2Fprivate.png',
|
||||
400,
|
||||
actor=alice,
|
||||
raw=fixture,
|
||||
headers={'Content-Type': 'image/png'},
|
||||
)
|
||||
|
||||
for path in (
|
||||
'/api/v1/attachments/' + attachment['id'],
|
||||
'/api/v1/attachments/' + attachment['id'] + '/content',
|
||||
):
|
||||
client.call('GET', path, 404, actor=eve)
|
||||
|
||||
other_chat = client.call(
|
||||
'POST',
|
||||
'/api/v1/chats',
|
||||
201,
|
||||
actor=alice,
|
||||
body={'title': 'Другой чат', 'member_ids': [alice['id'], bob['id']]},
|
||||
schema='Chat',
|
||||
)['json']
|
||||
client.call(
|
||||
'POST',
|
||||
'/api/v1/chats/' + other_chat['id'] + '/messages',
|
||||
404,
|
||||
actor=alice,
|
||||
body={'text': 'Чужое вложение', 'attachment_ids': [attachment['id']]},
|
||||
headers={'Idempotency-Key': 'cross-chat-attachment-check'},
|
||||
)
|
||||
|
||||
content = random_png()
|
||||
check(0 < len(content) <= MAX_UPLOAD_BYTES, 'generated stress image exceeds upload limit')
|
||||
container_ids = subprocess.run(
|
||||
['docker', 'compose', 'ps', '-q', 'api'],
|
||||
cwd=ROOT,
|
||||
check=True,
|
||||
capture_output=True,
|
||||
text=True,
|
||||
).stdout.split()
|
||||
check(len(container_ids) >= 2, 'memory test requires at least two API containers')
|
||||
baseline = api_memory(container_ids)
|
||||
samples = []
|
||||
latest = {}
|
||||
stats_process = subprocess.Popen(
|
||||
[
|
||||
'docker', 'stats', '--format', '{{.Name}}|{{.MemUsage}}',
|
||||
*container_ids,
|
||||
],
|
||||
cwd=ROOT,
|
||||
stdout=subprocess.PIPE,
|
||||
stderr=subprocess.DEVNULL,
|
||||
text=True,
|
||||
bufsize=1,
|
||||
)
|
||||
|
||||
def monitor():
|
||||
for line in stats_process.stdout:
|
||||
line = ANSI_ESCAPE.sub('', line)
|
||||
parts = line.strip().split('|', 1)
|
||||
if len(parts) != 2:
|
||||
continue
|
||||
try:
|
||||
latest[parts[0]] = memory_bytes(parts[1].split('/', 1)[0])
|
||||
except AssertionError:
|
||||
continue
|
||||
if len(latest) >= len(container_ids):
|
||||
samples.append(sum(latest.values()))
|
||||
|
||||
monitor_thread = threading.Thread(target=monitor, daemon=True)
|
||||
monitor_thread.start()
|
||||
barrier = threading.Barrier(PARALLEL_UPLOADS)
|
||||
|
||||
def worker(index):
|
||||
barrier.wait()
|
||||
return slow_upload(
|
||||
args.base_url,
|
||||
alice,
|
||||
chat['id'],
|
||||
index,
|
||||
content,
|
||||
)
|
||||
|
||||
started = time.monotonic()
|
||||
try:
|
||||
with ThreadPoolExecutor(max_workers=PARALLEL_UPLOADS) as pool:
|
||||
results = list(pool.map(worker, range(PARALLEL_UPLOADS)))
|
||||
finally:
|
||||
stats_process.terminate()
|
||||
stats_process.wait(timeout=5)
|
||||
monitor_thread.join(timeout=5)
|
||||
elapsed = time.monotonic() - started
|
||||
check(len(results) == PARALLEL_UPLOADS, 'not all simultaneous uploads completed')
|
||||
check(samples, 'Docker stats did not produce any in-flight memory samples')
|
||||
check(all(item['status'] == 'ready' for item in results), 'an upload was not ready')
|
||||
check(len({item['id'] for item in results}) == PARALLEL_UPLOADS, 'upload IDs were reused')
|
||||
peak = max(samples + [baseline])
|
||||
report = {
|
||||
'concurrent_uploads': PARALLEL_UPLOADS,
|
||||
'image_size_bytes': len(content),
|
||||
'image_sha256': hashlib.sha256(content).hexdigest(),
|
||||
'elapsed_seconds': round(elapsed, 3),
|
||||
'api_memory_baseline_bytes': baseline,
|
||||
'api_memory_peak_sampled_bytes': peak,
|
||||
'api_memory_sampled_delta_bytes': peak - baseline,
|
||||
'memory_sample_interval_seconds': 1,
|
||||
'memory_sample_count': len(samples),
|
||||
'notes': 'Docker stats streams aggregate API-container memory at about one-second intervals; short peaks may be missed.',
|
||||
}
|
||||
output = ROOT / 'evidence' / 'attachment-concurrency.json'
|
||||
output.parent.mkdir(parents=True, exist_ok=True)
|
||||
output.write_text(json.dumps(report, indent=2) + '\n', encoding='utf-8')
|
||||
print(json.dumps(report, indent=2))
|
||||
print('PASS: attachment limits, image validation, access controls and 10 concurrent uploads.')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
run(main)
|
||||
@@ -0,0 +1,55 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Explicitly stop MinIO, verify uploads fail as unavailable, and restore it."""
|
||||
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, arguments, check, run
|
||||
|
||||
|
||||
def compose(*args):
|
||||
return subprocess.run(
|
||||
['docker', 'compose', *args],
|
||||
cwd=ROOT,
|
||||
check=True,
|
||||
capture_output=True,
|
||||
text=True,
|
||||
)
|
||||
|
||||
|
||||
def main():
|
||||
args = arguments('MinIO outage behavior; stops and starts the lab MinIO service')
|
||||
client = Client(args.base_url, args.ca_file)
|
||||
alice, _bob, _eve, chat = client.bootstrap()
|
||||
sample = (ROOT / 'tests' / 'fixtures' / 'sample.png').read_bytes()
|
||||
compose('stop', 'minio')
|
||||
try:
|
||||
time.sleep(2)
|
||||
response = client.call(
|
||||
'POST',
|
||||
'/api/v1/chats/' + chat['id'] + '/attachments?filename=outage.png',
|
||||
503,
|
||||
actor=alice,
|
||||
raw=sample,
|
||||
headers={'Content-Type': 'image/png'},
|
||||
)
|
||||
check(response['json']['error']['code'] == 'unavailable', 'S3 outage was not reported as unavailable')
|
||||
finally:
|
||||
compose('start', 'minio')
|
||||
deadline = time.monotonic() + 90
|
||||
while time.monotonic() < deadline:
|
||||
try:
|
||||
client.call('GET', '/health/ready', schema='Health')
|
||||
break
|
||||
except AssertionError:
|
||||
time.sleep(1)
|
||||
else:
|
||||
raise RuntimeError('MinIO/API did not recover within 90 seconds')
|
||||
print('PASS: MinIO outage returned 503 and the service recovered without removing volumes.')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
run(main)
|
||||
Reference in New Issue
Block a user