Лаба 4: SeaweedFS S3-хранилище и вложения (оценка 4)

Переиспользован стек lab3 (LB/2 реплики/Postgres/Redis/идемпотентность).
Добавлены: S3-хранилище байтов вложений (SeaweedFS вместо MinIO — публичные
MinIO-образы недоступны без авторизации), метаданные attachments в Postgres,
проверка изображений по содержимому с защитой от decompression bomb,
ограниченные S3-credentials, скрипт сверки orphan-объектов. make test/
test-persistence/test-failover/validate зелёные для обеих реплик; собраны
доказательства по превышению размера, MIME-спуфингу, чужим attachment_id,
приватности bucket, конкурентным загрузкам и orphan-очистке — см. REPORT.md.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
2026-10-30 00:59:13 +03:00
co-authored by Claude Sonnet 5
parent ec540a1a72
commit 38268f63f8
23 changed files with 1584 additions and 16 deletions
+72
View File
@@ -0,0 +1,72 @@
#!/usr/bin/env python3
"""Find S3 objects with no matching `attachments` row (e.g. PUT succeeded but
the PostgreSQL INSERT that followed it never committed -- a crash between the
two writes). Dry-run by default; --delete removes orphans older than the
grace period, never anything referenced in the database.
Usage:
python3 scripts/reconcile_attachments.py --grace-minutes 60
python3 scripts/reconcile_attachments.py --grace-minutes 60 --delete
"""
import argparse
import asyncio
import datetime as dt
import os
import sys
import asyncpg
import boto3
def read_secret(file_env, inline_env):
path = os.environ.get(file_env)
if path and os.path.isfile(path):
return open(path, encoding="utf-8").read().strip()
return os.environ[inline_env]
async def main():
p = argparse.ArgumentParser()
p.add_argument("--grace-minutes", type=int, default=60,
help="Only objects older than this are candidates (protects in-flight uploads).")
p.add_argument("--delete", action="store_true", help="Actually delete orphans (default: dry-run).")
args = p.parse_args()
database_url = read_secret("DATABASE_URL_FILE", "DATABASE_URL")
conn = await asyncpg.connect(database_url)
known_keys = {r["s3_key"] for r in await conn.fetch("SELECT s3_key FROM attachments")}
await conn.close()
s3 = boto3.client(
"s3",
endpoint_url=os.environ["S3_ENDPOINT"],
region_name=os.environ.get("S3_REGION", "us-east-1"),
aws_access_key_id=read_secret("S3_ACCESS_KEY_ID_FILE", "S3_ACCESS_KEY_ID"),
aws_secret_access_key=read_secret("S3_SECRET_ACCESS_KEY_FILE", "S3_SECRET_ACCESS_KEY"),
)
bucket = os.environ.get("S3_BUCKET", "messenger-attachments")
cutoff = dt.datetime.now(dt.timezone.utc) - dt.timedelta(minutes=args.grace_minutes)
orphans = []
paginator = s3.get_paginator("list_objects_v2")
for page in paginator.paginate(Bucket=bucket, Prefix="attachments/"):
for obj in page.get("Contents", []):
if obj["Key"] in known_keys:
continue
if obj["LastModified"] > cutoff:
continue # might still be an in-flight upload; not an orphan yet
orphans.append(obj["Key"])
print(f"{len(orphans)} orphaned object(s) older than {args.grace_minutes} minutes with no DB row.")
for key in orphans:
print(" ", key)
if args.delete:
s3.delete_object(Bucket=bucket, Key=key)
if orphans and args.delete:
print(f"Deleted {len(orphans)} orphan(s).")
elif orphans:
print("Dry-run: nothing deleted. Re-run with --delete to remove them.")
if __name__ == "__main__":
sys.exit(asyncio.run(main()) or 0)
+28
View File
@@ -0,0 +1,28 @@
#!/bin/sh
set -eu
# Sequenced: Nginx resolves upstream hostnames only at its own startup, so it
# must come back strictly after api1/api2 exist again on the network.
cd "$(dirname "$0")/.."
wait_healthy() {
for svc in "$@"; do
for _ in $(seq 1 60); do
status=$(docker compose ps --format '{{.Health}}' "$svc" 2>/dev/null || true)
[ "$status" = "healthy" ] && break
sleep 1
done
done
}
# Sequenced on purpose: API eagerly connects to Postgres/Redis/S3 at startup
# and exits if they are unreachable, and Nginx resolves upstream hostnames
# only at its own startup -- so each stage must be actually healthy, not just
# "restarted", before the next one comes up.
docker compose restart postgres redis seaweedfs
wait_healthy postgres redis seaweedfs
docker compose restart api1 api2
wait_healthy api1 api2
docker compose restart lb
exit 0
+12
View File
@@ -0,0 +1,12 @@
#!/bin/sh
set -eu
# Stops exactly the API replica identified by TARGET_INSTANCE_ID (api1 or api2),
# leaving it stopped. The other replica keeps serving traffic through the LB.
cd "$(dirname "$0")/.."
: "${TARGET_INSTANCE_ID:?TARGET_INSTANCE_ID must be set by the test harness}"
case "$TARGET_INSTANCE_ID" in
api1|api2) ;;
*) echo "unknown instance id: $TARGET_INSTANCE_ID" >&2; exit 1 ;;
esac
docker compose stop "$TARGET_INSTANCE_ID"
exit 0