Собрать стенд lab4 с вложениями в приватном S3-bucket.

Метаданные остаются в PostgreSQL, байты — в RustFS, чтобы скачивание пережило остановку одной реплики API.
This commit is contained in:
samsik
2026-11-06 08:49:44 +03:00
parent ec540a1a72
commit 11c8c03984
32 changed files with 2348 additions and 50 deletions
+14
View File
@@ -0,0 +1,14 @@
#!/bin/sh
set -eu
cd "$(dirname "$0")/.."
docker compose --env-file .env -f compose.yaml exec -T api python3 - <<'PY'
from urllib.request import urlopen
try:
urlopen("http://s3:9000/messenger-attachments/", timeout=3)
raise SystemExit("anonymous bucket read was allowed")
except Exception as exc:
code = getattr(exc, "code", None)
if code not in (401, 403):
raise SystemExit("unexpected result %s %s" % (type(exc).__name__, code))
print("anonymous S3 status", code)
PY
+75
View File
@@ -0,0 +1,75 @@
#!/usr/bin/env python3
"""Проверка лимита размера на самом API, мимо балансировщика. Токен не печатает."""
import http.client
import importlib
import json
import time
import uuid
import urllib.request
urlerr = importlib.import_module("urllib.error")
BASE = "http://127.0.0.1:8080"
def call(method, path, body=None, headers=None, timeout=5):
req = urllib.request.Request(BASE + path, data=body, headers=headers or {}, method=method)
try:
with urllib.request.urlopen(req, timeout=timeout) as response:
return response.status, response.read()
except urlerr.HTTPError as exc:
return exc.code, exc.read()
def main():
username = "u_sz_" + uuid.uuid4().hex[:12]
password = "T3st-" + uuid.uuid4().hex
status, raw = call(
"POST", "/api/v1/users",
json.dumps({"username": username, "display_name": "Size", "password": password}).encode(),
{"Content-Type": "application/json"},
)
if status != 201:
raise SystemExit("register %s" % status)
user = json.loads(raw)
import base64
token = base64.b64encode((username + ":" + password).encode()).decode()
status, raw = call("POST", "/api/v1/auth/sessions", headers={"Authorization": "Basic " + token})
if status != 201:
raise SystemExit("login %s" % status)
bearer = json.loads(raw)["token"]
auth = {"Authorization": "Bearer " + bearer, "Content-Type": "application/json"}
status, raw = call(
"POST", "/api/v1/chats",
json.dumps({"title": "limit", "member_ids": [user["id"]]}).encode(),
auth,
)
if status != 201:
raise SystemExit("chat %s" % status)
chat = json.loads(raw)["id"]
# Заголовок больше лимита, тело из одного байта: API должен ответить до чтения остальных байт.
started = time.monotonic()
conn = http.client.HTTPConnection("127.0.0.1", 8080, timeout=3)
conn.request(
"POST",
"/api/v1/chats/" + chat + "/attachments?filename=big.png",
body=b"x",
headers={
"Authorization": "Bearer " + bearer,
"Content-Type": "image/png",
"Content-Length": "10485761",
},
)
response = conn.getresponse()
status = response.status
elapsed = time.monotonic() - started
conn.close()
if status != 413:
raise SystemExit("expected 413, got %s" % status)
if elapsed > 2:
raise SystemExit("limit check took too long: %.3f" % elapsed)
print("oversized Content-Length -> %s in %.3fs" % (status, elapsed))
if __name__ == "__main__":
main()
+62
View File
@@ -0,0 +1,62 @@
#!/usr/bin/env python3
"""Негативные проверки вложений для уровня 3. Токены не печатает."""
import hashlib
import struct
import sys
import uuid
import zlib
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT / "tests"))
from client import Client, Failure, arguments, check, run # noqa: E402
def png(width: int, height: int) -> bytes:
signature = b"\x89PNG\r\n\x1a\n"
ihdr = struct.pack(">IIBBBBB", width, height, 8, 2, 0, 0, 0)
chunk = b"IHDR" + ihdr
crc = zlib.crc32(chunk) & 0xFFFFFFFF
return signature + struct.pack(">I", len(ihdr)) + chunk + struct.pack(">I", crc)
def main():
args = arguments("Lab4 grade-3 attachment checks")
base = Client(args.base_url, args.ca_file)
alice, bob, eve, chat = base.bootstrap()
other = base.call(
"POST", "/api/v1/chats", 201, actor=alice,
body={"title": "Другой чат", "member_ids": [alice["id"], bob["id"]]},
schema="Chat",
)["json"]
sample = (ROOT / "tests/fixtures/sample.png").read_bytes()
att, content = base.attachment(alice, chat)
linked = base.message(alice, chat, "", attachments=[att["id"]])["json"]
check(linked["attachment_ids"] == [att["id"]], "message lost attachment")
downloaded = base.call("GET", "/api/v1/attachments/" + att["id"] + "/content", actor=bob)
check(downloaded["bytes"] == content, "bob download differs")
check(hashlib.sha256(downloaded["bytes"]).hexdigest() == att["sha256"], "checksum differs")
for suffix in ("", "/content"):
base.call("GET", "/api/v1/attachments/" + att["id"] + suffix, 404, actor=eve)
base.call(
"POST", "/api/v1/chats/" + other["id"] + "/messages", 404, actor=alice,
body={"text": "", "attachment_ids": [att["id"]]},
headers={"Idempotency-Key": uuid.uuid4().hex},
)
base.call(
"POST", "/api/v1/chats/" + chat["id"] + "/attachments?filename=a/b.png", 400,
actor=alice, raw=sample, headers={"Content-Type": "image/png"},
)
base.call(
"POST", "/api/v1/chats/" + chat["id"] + "/attachments?filename=photo.png", 415,
actor=alice, raw=sample, headers={"Content-Type": "image/jpeg"},
)
base.call(
"POST", "/api/v1/chats/" + chat["id"] + "/attachments?filename=huge.png", 413,
actor=alice, raw=png(10000, 5000), headers={"Content-Type": "image/png"},
)
print("PASS: filename, MIME, pixels, outsider, foreign chat")
if __name__ == "__main__":
run(main)
+5
View File
@@ -0,0 +1,5 @@
#!/bin/sh
set -eu
cd "$(dirname "$0")/.."
# Перезапуск API и всех хранилищ. Volumes не удаляются, схема и объекты остаются.
docker compose --env-file .env -f compose.yaml restart api postgres valkey s3
+8
View File
@@ -0,0 +1,8 @@
#!/bin/sh
set -eu
cd "$(dirname "$0")/.."
replicas=$(sed -n 's/^API_REPLICAS=//p' .env | tr -d '\r')
if [ -z "${replicas}" ]; then
replicas=2
fi
docker compose --env-file .env -f compose.yaml up -d --no-deps --scale "api=${replicas}" --no-recreate api
+22
View File
@@ -0,0 +1,22 @@
#!/bin/sh
set -eu
: "${TARGET_INSTANCE_ID:?TARGET_INSTANCE_ID is required}"
cd "$(dirname "$0")/.."
found=""
for cid in $(docker compose --env-file .env -f compose.yaml ps -q api); do
host=$(docker inspect -f '{{.Config.Hostname}}' "$cid")
short=$(printf '%s' "$cid" | cut -c1-12)
if [ "$host" = "$TARGET_INSTANCE_ID" ] || [ "$short" = "$TARGET_INSTANCE_ID" ]; then
found=$cid
break
fi
done
if [ -z "$found" ]; then
echo "API replica was not found" >&2
exit 1
fi
# docker stop + restart: unless-stopped оставляет реплику выключенной.
docker stop -t 8 "$found" >/dev/null
Executable
+12
View File
@@ -0,0 +1,12 @@
#!/bin/sh
set -eu
cd "$(dirname "$0")/.."
if [ ! -f .env ]; then
echo "Copy .env.example to .env" >&2
exit 1
fi
replicas=$(sed -n 's/^API_REPLICAS=//p' .env | tr -d '\r')
if [ -z "${replicas}" ]; then
replicas=2
fi
docker compose --env-file .env -f compose.yaml up -d --build --scale "api=${replicas}" --remove-orphans