forked from mtusi2026/mtusi2026lab7
820 lines
41 KiB
Python
820 lines
41 KiB
Python
"""
|
||
HTTP-слой мессенджера (lab4).
|
||
|
||
Клиент → LB (HAProxy) → эта реплика API (их ≥2, взаимозаменяемы)
|
||
→ PostgreSQL (источник истины: данные и метаданные вложений)
|
||
→ Redis (общий кэш сессий)
|
||
→ MinIO, приватный bucket (только байты вложений)
|
||
|
||
lab2: пароль Argon2id, вход Basic → Bearer-сессия в PostgreSQL, X-User-Id игнорируется.
|
||
lab3: X-Instance-Id в каждом ответе; POST сообщения требует Idempotency-Key,
|
||
идемпотентность общая для всех реплик (таблица idempotency_keys);
|
||
у реплики нет локального состояния, от которого зависит корректность.
|
||
lab4: вложения PNG/JPEG: GET /attachments/{id} и /content (только участникам чата),
|
||
attachment_ids в сообщениях.
|
||
lab6: наблюдаемость — OTel SDK (app/telemetry.py): server spans FastAPI, spans SQL и S3,
|
||
producer span постановки задания с W3C traceparent в сообщении, метрики HTTP/очереди/пула,
|
||
структурные логи с trace_id/span_id. Тела, токены и пароли в телеметрию не попадают.
|
||
lab5: POST /chats/{id}/attachments → 202 queued. До ответа: оригинал целиком в приватном
|
||
staging (S3), в ОДНОЙ транзакции — вложение, job и строка outbox. Затем сообщение
|
||
публикуется в брокер (Redis Streams с AOF); если не вышло — его опубликует relay.
|
||
Тяжёлую обработку (варианты) делает отдельный процесс worker (app/worker.py).
|
||
"""
|
||
from __future__ import annotations
|
||
import asyncio
|
||
import base64
|
||
import binascii
|
||
import contextlib
|
||
import json
|
||
import logging
|
||
import os
|
||
import re
|
||
import time
|
||
import uuid
|
||
from contextlib import asynccontextmanager
|
||
|
||
import psycopg
|
||
from fastapi import Depends, FastAPI, Header, Query, Request
|
||
from fastapi.concurrency import run_in_threadpool
|
||
from fastapi.exceptions import RequestValidationError
|
||
from fastapi.responses import JSONResponse, Response, StreamingResponse
|
||
from psycopg_pool import PoolTimeout
|
||
from pydantic import BaseModel, Field, field_validator, model_validator
|
||
from starlette.exceptions import HTTPException as StarletteHTTPException
|
||
|
||
from urllib.parse import quote
|
||
|
||
from opentelemetry import trace
|
||
from opentelemetry.trace import SpanKind, Status, StatusCode
|
||
|
||
from . import (broker, db, images, janitor, ratelimit, repo, secretstore, security, sessions, signals,
|
||
storage, telemetry, uploads)
|
||
from .config import settings
|
||
from .migrate import migrate
|
||
|
||
logger = logging.getLogger("messenger")
|
||
|
||
# Только fullmatch: в Python "$" совпадает и перед завершающим "\n",
|
||
# поэтому match("^…$") пропустил бы "review123\n" до CHECK в БД (500).
|
||
USERNAME_RE = re.compile(r"[a-z0-9_]{3,32}")
|
||
IDEMPOTENCY_KEY_RE = re.compile(r"[A-Za-z0-9._:-]{1,128}")
|
||
BEARER_CHALLENGE = 'Bearer realm="messenger"'
|
||
BASIC_CHALLENGE = 'Basic realm="messenger", charset="UTF-8"'
|
||
MAX_ATTACHMENTS_PER_MESSAGE = 10
|
||
KNOWN_VARIANTS = ("thumbnail", "crop", "optimized")
|
||
|
||
|
||
@asynccontextmanager
|
||
async def lifespan(app: FastAPI):
|
||
if settings.migrate_on_start:
|
||
try:
|
||
applied = await migrate()
|
||
logger.info("migrations: %s", applied or "up to date")
|
||
except psycopg.OperationalError as exc:
|
||
# БД ещё не готова: API всё равно стартует, /health/ready покажет 503.
|
||
logger.warning("migrations skipped, database unavailable: %s", type(exc).__name__)
|
||
# lab7: секреты уже загружены в __main__ (до старта сервера); здесь — фоновое обновление.
|
||
secretstore.start_refresher()
|
||
secretstore.on_change(storage.init) # новые S3-ключи роли → новый клиент
|
||
await db.open_pool()
|
||
sessions.init_redis()
|
||
storage.init()
|
||
broker.init_async()
|
||
cleaner = asyncio.create_task(janitor.loop_forever()) if settings.janitor_enabled else None
|
||
signals.register_pool_gauges(db.get_pool)
|
||
signals.register_queue_gauges()
|
||
stats_task = asyncio.create_task(_queue_stats_loop())
|
||
health_srv = await asyncio.start_server(_health_conn, "127.0.0.1", settings.health_port)
|
||
yield
|
||
health_srv.close()
|
||
for task in (cleaner, stats_task):
|
||
if task is not None:
|
||
task.cancel()
|
||
with contextlib.suppress(asyncio.CancelledError):
|
||
await task
|
||
await sessions.close_redis()
|
||
await broker.close_async()
|
||
await db.close_pool()
|
||
telemetry.shutdown()
|
||
|
||
|
||
async def _ready() -> bool:
|
||
try:
|
||
async with db.get_pool().connection(timeout=2) as conn:
|
||
await conn.execute("SELECT 1 FROM schema_migrations LIMIT 1")
|
||
return True
|
||
except (psycopg.Error, PoolTimeout):
|
||
return False
|
||
|
||
|
||
async def _health_conn(reader: asyncio.StreamReader, writer: asyncio.StreamWriter) -> None:
|
||
"""lab7: health-listener для docker healthcheck. Слушает только 127.0.0.1 контейнера
|
||
(из сети недоступен), без TLS, понимает лишь GET /health/live и /health/ready —
|
||
обойти через него mTLS к бизнес-API нельзя."""
|
||
try:
|
||
line = await asyncio.wait_for(reader.readline(), 2)
|
||
parts = line.decode("latin-1").split()
|
||
path = parts[1] if len(parts) >= 2 and parts[0] == "GET" else ""
|
||
if path == "/health/live":
|
||
status, body = 200, b'{"status":"ok"}'
|
||
elif path == "/health/ready":
|
||
ok = await _ready()
|
||
status, body = (200, b'{"status":"ok"}') if ok else (503, b'{"status":"unavailable"}')
|
||
else:
|
||
status, body = 404, b'{"status":"not_found"}'
|
||
writer.write(f"HTTP/1.1 {status} X\r\nContent-Type: application/json\r\n"
|
||
f"Content-Length: {len(body)}\r\nConnection: close\r\n\r\n".encode() + body)
|
||
await writer.drain()
|
||
except (asyncio.TimeoutError, OSError):
|
||
pass
|
||
finally:
|
||
writer.close()
|
||
|
||
|
||
async def _queue_stats_loop() -> None:
|
||
"""Раз в 5 c — глубина очереди и возраст старейшего задания (для метрик и alert).
|
||
Считает API, а не worker: при остановленном worker рост возраста всё равно виден."""
|
||
while True:
|
||
try:
|
||
with telemetry.quiet(): # служебный запрос — без trace
|
||
async with db.get_pool().connection(timeout=2) as conn:
|
||
signals.set_queue_stats(*await repo.queue_stats(conn))
|
||
except asyncio.CancelledError:
|
||
raise
|
||
except Exception as exc: # noqa: BLE001
|
||
logger.debug("queue stats failed: %s", type(exc).__name__)
|
||
await asyncio.sleep(5)
|
||
|
||
|
||
app = FastAPI(title="MTUSI Messenger — lab7", lifespan=lifespan)
|
||
|
||
|
||
# ============================================================================
|
||
# 1. ЕДИНЫЙ ФОРМАТ ОШИБОК {"error":{"code","message","request_id"}}
|
||
# ============================================================================
|
||
class ApiError(Exception):
|
||
def __init__(self, status: int, code: str, message: str, headers: dict | None = None):
|
||
self.status, self.code, self.message, self.headers = status, code, message, headers or {}
|
||
|
||
|
||
def error_response(request: Request, status: int, code: str, message: str,
|
||
headers: dict | None = None) -> JSONResponse:
|
||
rid = getattr(request.state, "request_id", str(uuid.uuid4()))
|
||
return JSONResponse(
|
||
status_code=status,
|
||
content={"error": {"code": code, "message": message, "request_id": rid}},
|
||
headers={"X-Request-Id": rid, "X-Instance-Id": settings.instance_id, **(headers or {})},
|
||
)
|
||
|
||
|
||
@app.exception_handler(ApiError)
|
||
async def handle_api_error(request: Request, exc: ApiError):
|
||
return error_response(request, exc.status, exc.code, exc.message, exc.headers)
|
||
|
||
|
||
@app.exception_handler(RequestValidationError)
|
||
async def handle_validation(request: Request, exc: RequestValidationError):
|
||
return error_response(request, 400, "invalid_request", "invalid request body or parameters")
|
||
|
||
|
||
@app.exception_handler(StarletteHTTPException)
|
||
async def handle_http(request: Request, exc: StarletteHTTPException):
|
||
# Только коды из enum контракта (contracts/openapi.json → Error.code).
|
||
code = {401: "unauthorized", 404: "not_found", 409: "conflict",
|
||
413: "payload_too_large", 415: "unsupported_media_type",
|
||
429: "rate_limited", 503: "unavailable"}.get(
|
||
exc.status_code, "internal_error" if exc.status_code >= 500 else "invalid_request")
|
||
return error_response(request, exc.status_code, code, str(exc.detail))
|
||
|
||
|
||
@app.exception_handler(psycopg.OperationalError)
|
||
@app.exception_handler(PoolTimeout)
|
||
async def handle_db_down(request: Request, exc: Exception):
|
||
# БД недоступна или не ответила за таймаут: ограниченная ошибка, а не зависание.
|
||
logger.warning("database unavailable: %s", type(exc).__name__,
|
||
extra={"event": "db.unavailable", "error.type": type(exc).__name__})
|
||
return error_response(request, 503, "unavailable", "service temporarily unavailable")
|
||
|
||
|
||
@app.exception_handler(storage.StorageUnavailable)
|
||
async def handle_storage_down(request: Request, exc: Exception):
|
||
# S3 недоступен/не ответил за таймаут: 503, текстовая часть мессенджера продолжает работать.
|
||
return error_response(request, 503, "unavailable", "file storage temporarily unavailable")
|
||
|
||
|
||
@app.exception_handler(uploads.UploadError)
|
||
async def handle_upload_error(request: Request, exc: uploads.UploadError):
|
||
return error_response(request, exc.status, exc.code, exc.message)
|
||
|
||
|
||
@app.exception_handler(Exception)
|
||
async def handle_unexpected(request: Request, exc: Exception):
|
||
logger.exception("unexpected error")
|
||
return error_response(request, 500, "internal_error", "internal error")
|
||
|
||
|
||
# ============================================================================
|
||
# 2. X-Request-Id, X-Instance-Id + лог без тела запроса, токенов и паролей
|
||
# ============================================================================
|
||
class BodyLimit:
|
||
"""lab7: тело не-upload запросов ограничено MAX_JSON_BODY_BYTES. Content-Length больше →
|
||
413 без чтения; без Content-Length (chunked) считаем байты и обрываем на пределе.
|
||
Загрузка файла ограничена отдельно (MAX_UPLOAD_BYTES, uploads.py)."""
|
||
|
||
def __init__(self, app):
|
||
self.app = app
|
||
|
||
async def __call__(self, scope, receive, send):
|
||
if scope["type"] != "http" or scope["path"].endswith("/attachments"):
|
||
return await self.app(scope, receive, send)
|
||
limit = settings.max_json_body_bytes
|
||
headers = dict(scope.get("headers") or [])
|
||
cl = headers.get(b"content-length")
|
||
too_big = False
|
||
if cl is not None:
|
||
try:
|
||
too_big = int(cl) > limit
|
||
except ValueError:
|
||
too_big = True
|
||
if too_big:
|
||
return await self._reject(scope, send)
|
||
seen = 0
|
||
|
||
async def limited():
|
||
nonlocal seen
|
||
msg = await receive()
|
||
if msg["type"] == "http.request":
|
||
seen += len(msg.get("body", b""))
|
||
if seen > limit:
|
||
raise _BodyTooLarge()
|
||
return msg
|
||
try:
|
||
return await self.app(scope, limited, send)
|
||
except _BodyTooLarge:
|
||
return await self._reject(scope, send)
|
||
|
||
@staticmethod
|
||
async def _reject(scope, send):
|
||
body = json.dumps({"error": {"code": "payload_too_large", "message": "request body too large",
|
||
"request_id": str(uuid.uuid4())}}).encode()
|
||
await send({"type": "http.response.start", "status": 413,
|
||
"headers": [(b"content-type", b"application/json"),
|
||
(b"x-instance-id", settings.instance_id.encode())]})
|
||
await send({"type": "http.response.body", "body": body})
|
||
|
||
|
||
class _BodyTooLarge(Exception):
|
||
pass
|
||
|
||
|
||
app.add_middleware(BodyLimit)
|
||
|
||
|
||
@app.middleware("http")
|
||
async def add_request_id(request: Request, call_next):
|
||
rid = str(uuid.uuid4())
|
||
request.state.request_id = rid
|
||
# request_id — атрибут server span: из trace видно X-Request-Id ответа и наоборот.
|
||
trace.get_current_span().set_attribute("messenger.request.id", rid)
|
||
start = time.monotonic()
|
||
response = await call_next(request)
|
||
response.headers["X-Request-Id"] = rid
|
||
response.headers["X-Instance-Id"] = settings.instance_id # какая реплика ответила
|
||
# Шаблон маршрута, а не реальный путь: в логе нет chat_id/attachment_id из URL.
|
||
route = getattr(request.scope.get("route"), "path", "unmatched")
|
||
if not route.startswith("/health"):
|
||
ms = round((time.monotonic() - start) * 1000, 1)
|
||
logger.log(logging.WARNING if response.status_code >= 500 else logging.INFO,
|
||
"%s %s -> %s %sms", request.method, route, response.status_code, ms,
|
||
extra={"event": "http.request", "http.request.method": request.method,
|
||
"http.route": route, "http.response.status_code": response.status_code,
|
||
"http.duration_ms": ms, "request.id": rid})
|
||
return response
|
||
|
||
|
||
# ============================================================================
|
||
# 3. АУТЕНТИФИКАЦИЯ
|
||
# Bearer — для всех бизнес-запросов. Basic — только на POST /auth/sessions.
|
||
# X-User-Id больше ничего не значит и ни на что не влияет.
|
||
# ============================================================================
|
||
def unauthorized(message: str = "invalid or missing bearer token") -> ApiError:
|
||
return ApiError(401, "unauthorized", message, {"WWW-Authenticate": BEARER_CHALLENGE})
|
||
|
||
|
||
def _bearer_token(request: Request) -> str:
|
||
auth = request.headers.get("authorization", "")
|
||
scheme, _, token = auth.partition(" ")
|
||
token = token.strip()
|
||
if scheme.lower() != "bearer" or not token or len(token) > 512:
|
||
raise unauthorized()
|
||
return token
|
||
|
||
|
||
async def current_user(request: Request) -> dict:
|
||
"""Зависимость FastAPI: выполняется ДО разбора тела → сначала 401, потом 400."""
|
||
token = _bearer_token(request)
|
||
user = await sessions.resolve(db.get_pool(), token, repo.get_user)
|
||
if user is None:
|
||
raise unauthorized()
|
||
request.state.token = token
|
||
return user
|
||
|
||
|
||
def _parse_basic(request: Request) -> tuple[str, str]:
|
||
bad = ApiError(401, "unauthorized", "invalid credentials", {"WWW-Authenticate": BASIC_CHALLENGE})
|
||
scheme, _, value = request.headers.get("authorization", "").partition(" ")
|
||
if scheme.lower() != "basic" or not value:
|
||
raise bad
|
||
try:
|
||
decoded = base64.b64decode(value.strip(), validate=True).decode("utf-8")
|
||
except (binascii.Error, UnicodeDecodeError):
|
||
raise bad
|
||
username, sep, password = decoded.partition(":") # двоеточие в пароле допустимо
|
||
if not sep or not username.isascii() or not (12 <= len(password) <= 128):
|
||
raise bad
|
||
return username, password
|
||
|
||
|
||
def require_json(request: Request) -> None:
|
||
"""Тип тела проверяем ДО валидации тела: не JSON → 415, а не 400.
|
||
Подключается после current_user, поэтому без токена по-прежнему сначала 401.
|
||
Параметры вроде '; charset=utf-8' допустимы."""
|
||
media_type = request.headers.get("content-type", "").split(";", 1)[0].strip().lower()
|
||
if media_type != "application/json":
|
||
raise ApiError(415, "unsupported_media_type", "Content-Type must be application/json")
|
||
|
||
|
||
_kdf_slots: asyncio.Semaphore | None = None
|
||
_kdf_waiting = 0
|
||
|
||
|
||
async def kdf(fn, *args):
|
||
"""Argon2id берёт ARGON2_MEMORY_KIB RAM и десятки мс CPU на один хэш. Одновременно
|
||
считаем не больше PASSWORD_HASH_CONCURRENCY хэшей на реплику (RAM ограничена),
|
||
в очереди ждут не больше PASSWORD_HASH_QUEUE запросов. Очередь полна → сразу 429:
|
||
поток дорогих входов не накапливает бесконечную очередь и не роняет латентность
|
||
остального API (login DoS budget — REPORT.md)."""
|
||
global _kdf_slots, _kdf_waiting
|
||
if _kdf_slots is None:
|
||
_kdf_slots = asyncio.Semaphore(settings.password_hash_concurrency)
|
||
if _kdf_slots.locked() and _kdf_waiting >= settings.password_hash_queue:
|
||
raise ApiError(429, "rate_limited", "too many password operations, retry later",
|
||
{"Retry-After": "1"})
|
||
_kdf_waiting += 1
|
||
try:
|
||
await _kdf_slots.acquire()
|
||
finally:
|
||
_kdf_waiting -= 1
|
||
try:
|
||
return await run_in_threadpool(fn, *args)
|
||
finally:
|
||
_kdf_slots.release()
|
||
|
||
|
||
def client_ip(request: Request) -> str:
|
||
"""LB перезаписывает X-Forwarded-For адресом клиента; к API ходит только LB (mTLS)."""
|
||
xff = request.headers.get("x-forwarded-for", "")
|
||
ip = xff.split(",")[-1].strip() if xff else ""
|
||
return ip or (request.client.host if request.client else "unknown")
|
||
|
||
|
||
# ============================================================================
|
||
# 4. МОДЕЛИ ВВОДА
|
||
# ============================================================================
|
||
class CreateUser(BaseModel):
|
||
model_config = {"extra": "forbid"}
|
||
username: str
|
||
display_name: str
|
||
password: str
|
||
|
||
@field_validator("username")
|
||
@classmethod
|
||
def check_username(cls, v: str) -> str:
|
||
if not USERNAME_RE.fullmatch(v):
|
||
raise ValueError("username must match [a-z0-9_]{3,32}")
|
||
return v
|
||
|
||
@field_validator("display_name")
|
||
@classmethod
|
||
def check_display_name(cls, v: str) -> str:
|
||
if not (1 <= len(v) <= 100) or not v.strip():
|
||
raise ValueError("display_name must be 1..100 non-blank chars")
|
||
return v
|
||
|
||
@field_validator("password")
|
||
@classmethod
|
||
def check_password(cls, v: str) -> str:
|
||
if not (12 <= len(v) <= 128) or not v.strip():
|
||
raise ValueError("password must be 12..128 chars")
|
||
return v
|
||
|
||
|
||
class CreateChat(BaseModel):
|
||
model_config = {"extra": "forbid"}
|
||
title: str
|
||
member_ids: list[str]
|
||
|
||
@field_validator("title")
|
||
@classmethod
|
||
def check_title(cls, v: str) -> str:
|
||
if not (1 <= len(v) <= 100) or not v.strip():
|
||
raise ValueError("title must be 1..100 non-blank chars")
|
||
return v
|
||
|
||
|
||
class CreateMessage(BaseModel):
|
||
model_config = {"extra": "forbid"}
|
||
text: str
|
||
# lab4: 0–10 уникальных id вложений этого чата; отсутствие поля == [].
|
||
attachment_ids: list[str] = Field(default_factory=list)
|
||
|
||
@field_validator("text")
|
||
@classmethod
|
||
def check_text(cls, v: str) -> str:
|
||
if len(v) > 4000:
|
||
raise ValueError("text must be at most 4000 chars")
|
||
return v
|
||
|
||
@field_validator("attachment_ids")
|
||
@classmethod
|
||
def check_attachment_ids(cls, v: list[str]) -> list[str]:
|
||
if len(v) > MAX_ATTACHMENTS_PER_MESSAGE:
|
||
raise ValueError("at most 10 attachment_ids")
|
||
if len(set(v)) != len(v):
|
||
raise ValueError("attachment_ids must be unique")
|
||
return v
|
||
|
||
@model_validator(mode="after")
|
||
def text_or_attachments(self):
|
||
# Пустой/пробельный текст допустим только вместе с вложениями.
|
||
if not self.attachment_ids and not self.text.strip():
|
||
raise ValueError("text must be non-blank when there are no attachments")
|
||
return self
|
||
|
||
|
||
def parse_uuid(value: str, what: str) -> uuid.UUID:
|
||
try:
|
||
return uuid.UUID(value)
|
||
except ValueError:
|
||
raise ApiError(400, "invalid_request", f"{what} is not a valid uuid")
|
||
|
||
|
||
# ============================================================================
|
||
# 5. ENDPOINTS
|
||
# ============================================================================
|
||
@app.get("/health/live")
|
||
async def health_live():
|
||
return {"status": "ok"}
|
||
|
||
|
||
@app.get("/health/ready")
|
||
async def health_ready(request: Request):
|
||
# Критичная зависимость — только PostgreSQL. Redis — кэш: его отказ не делает API неготовым.
|
||
try:
|
||
async with db.get_pool().connection() as conn:
|
||
await conn.execute("SELECT 1 FROM schema_migrations LIMIT 1")
|
||
except (psycopg.Error, PoolTimeout):
|
||
return error_response(request, 503, "unavailable", "database is not ready")
|
||
return {"status": "ok"}
|
||
|
||
|
||
# -------- users и сессии --------
|
||
@app.post("/api/v1/users", status_code=201)
|
||
async def create_user(body: CreateUser, _json: None = Depends(require_json)):
|
||
# Argon2 намеренно медленный — считаем в пуле потоков, чтобы не блокировать event loop.
|
||
password_hash = await kdf(security.hash_password, body.password)
|
||
async with db.get_pool().connection() as conn:
|
||
try:
|
||
return await repo.create_user(conn, body.username, body.display_name, password_hash)
|
||
except repo.UsernameTaken:
|
||
raise ApiError(409, "conflict", "username already taken")
|
||
|
||
|
||
@app.post("/api/v1/auth/sessions", status_code=201)
|
||
async def create_session(request: Request):
|
||
username, password = _parse_basic(request)
|
||
# lab7: общий для реплик лимит — ДО Argon2id (заблокированная попытка не тратит CPU/RAM).
|
||
wait = await ratelimit.check_login(username, client_ip(request))
|
||
if wait:
|
||
logger.warning("login rate limited", extra={"event": "auth.rate_limited", "retry_after": wait})
|
||
raise ApiError(429, "rate_limited", "too many login attempts", {"Retry-After": str(wait)})
|
||
async with db.get_pool().connection() as conn:
|
||
row = await repo.get_credentials(conn, username)
|
||
stored_hash = row[4] if row else None
|
||
# Проверяем хэш даже для несуществующего пользователя — одинаковое время ответа.
|
||
ok = await kdf(security.verify_password, stored_hash, password)
|
||
if not ok:
|
||
await ratelimit.login_failed(username)
|
||
logger.info("login failed", extra={"event": "auth.login_failed"}) # без username/пароля
|
||
raise ApiError(401, "unauthorized", "invalid credentials", {"WWW-Authenticate": BASIC_CHALLENGE})
|
||
await ratelimit.login_succeeded(username)
|
||
user = repo.user_out(row)
|
||
async with db.get_pool().connection() as conn:
|
||
if security.needs_rehash(stored_hash):
|
||
# Старые параметры/алгоритм → пересчёт с текущими (пароль известен только сейчас).
|
||
await repo.update_password_hash(conn, row[0], await kdf(security.hash_password, password))
|
||
logger.info("password hash upgraded", extra={"event": "auth.rehash", "argon2.params": security.params()})
|
||
token, expires_at = await sessions.create_session(conn, row[0])
|
||
return {"token": token, "token_type": "Bearer", "expires_at": repo.iso(expires_at), "user": user}
|
||
|
||
|
||
@app.delete("/api/v1/auth/sessions/current", status_code=204)
|
||
async def delete_session(request: Request, user: dict = Depends(current_user)):
|
||
try:
|
||
revoked = await sessions.revoke(db.get_pool(), request.state.token)
|
||
except sessions.CacheUnavailable:
|
||
# 204 только при гарантированном отзыве; иначе ограниченная ошибка, повторить позже.
|
||
raise ApiError(503, "unavailable", "session cache unavailable, logout not confirmed")
|
||
if not revoked:
|
||
raise unauthorized()
|
||
return Response(status_code=204)
|
||
|
||
|
||
@app.get("/api/v1/users/me")
|
||
async def get_me(user: dict = Depends(current_user)):
|
||
return user
|
||
|
||
|
||
# -------- chats --------
|
||
@app.post("/api/v1/chats", status_code=201)
|
||
async def create_chat(body: CreateChat, user: dict = Depends(current_user),
|
||
_json: None = Depends(require_json)):
|
||
members = body.member_ids
|
||
if not (1 <= len(members) <= 100):
|
||
raise ApiError(400, "invalid_request", "member_ids must have 1..100 items")
|
||
if len(set(members)) != len(members):
|
||
raise ApiError(400, "invalid_request", "duplicate member_ids")
|
||
ids = [parse_uuid(m, "member id") for m in members]
|
||
if uuid.UUID(user["id"]) not in ids:
|
||
raise ApiError(400, "invalid_request", "caller must be a member")
|
||
async with db.get_pool().connection() as conn:
|
||
# Одна транзакция: чат и все участники создаются вместе или не создаются вовсе.
|
||
async with conn.transaction():
|
||
if await repo.count_existing_users(conn, ids) != len(ids):
|
||
raise ApiError(400, "invalid_request", "unknown member")
|
||
return await repo.insert_chat(conn, body.title, ids, user["id"])
|
||
|
||
|
||
@app.get("/api/v1/chats")
|
||
async def list_chats(user: dict = Depends(current_user),
|
||
limit: int = Query(default=50, ge=1, le=100),
|
||
cursor: str | None = Query(default=None, min_length=1)):
|
||
async with db.get_pool().connection() as conn:
|
||
try:
|
||
items, nxt = await repo.list_chats(conn, user["id"], limit, cursor)
|
||
except ValueError:
|
||
raise ApiError(400, "invalid_request", "bad cursor")
|
||
return {"items": items, "next_cursor": nxt}
|
||
|
||
|
||
async def accessible_chat(conn, chat_id: str, user: dict) -> dict:
|
||
"""Кривой UUID → 400. Чата нет ИЛИ ты не участник → одинаковый 404."""
|
||
cid = parse_uuid(chat_id, "chat_id")
|
||
chat = await repo.get_chat_for_member(conn, cid, user["id"])
|
||
if chat is None:
|
||
raise ApiError(404, "not_found", "chat not found")
|
||
return chat
|
||
|
||
|
||
@app.get("/api/v1/chats/{chat_id}")
|
||
async def get_chat(chat_id: str, user: dict = Depends(current_user)):
|
||
async with db.get_pool().connection() as conn:
|
||
return await accessible_chat(conn, chat_id, user)
|
||
|
||
|
||
# -------- messages --------
|
||
@app.post("/api/v1/chats/{chat_id}/messages", status_code=201)
|
||
async def send_message(chat_id: str, body: CreateMessage, user: dict = Depends(current_user),
|
||
_json: None = Depends(require_json),
|
||
idempotency_key: str | None = Header(default=None, alias="Idempotency-Key")):
|
||
# Ключ обязателен: без него клиент не может безопасно повторить запрос после таймаута.
|
||
if idempotency_key is None or not IDEMPOTENCY_KEY_RE.fullmatch(idempotency_key):
|
||
raise ApiError(400, "invalid_request", "Idempotency-Key header is required: [A-Za-z0-9._:-]{1,128}")
|
||
attachment_ids = [parse_uuid(a, "attachment_id") for a in body.attachment_ids]
|
||
if len(set(attachment_ids)) != len(attachment_ids):
|
||
raise ApiError(400, "invalid_request", "attachment_ids must be unique")
|
||
async with db.get_pool().connection() as conn:
|
||
# Повтор снова проверяет сессию (current_user) и доступ к чату.
|
||
chat = await accessible_chat(conn, chat_id, user)
|
||
try:
|
||
async with conn.transaction():
|
||
# Отправитель — из сессии, а не из тела запроса.
|
||
return await repo.send_message_idempotent(
|
||
conn, chat["id"], user["id"], idempotency_key, body.text,
|
||
settings.idempotency_ttl_seconds, attachment_ids)
|
||
except repo.IdempotencyConflict:
|
||
raise ApiError(409, "conflict", "Idempotency-Key was already used with a different request")
|
||
except repo.AttachmentNotFound:
|
||
# Нет такого вложения ИЛИ оно из другого чата — одинаковый 404.
|
||
raise ApiError(404, "not_found", "attachment not found")
|
||
except repo.AttachmentNotReady:
|
||
raise ApiError(409, "conflict", "attachment is not usable")
|
||
|
||
|
||
@app.get("/api/v1/chats/{chat_id}/messages")
|
||
async def list_messages(chat_id: str, user: dict = Depends(current_user),
|
||
limit: int = Query(default=50, ge=1, le=100),
|
||
cursor: str | None = Query(default=None, min_length=1)):
|
||
async with db.get_pool().connection() as conn:
|
||
chat = await accessible_chat(conn, chat_id, user)
|
||
try:
|
||
items, nxt = await repo.list_messages(conn, chat["id"], limit, cursor)
|
||
except ValueError:
|
||
raise ApiError(400, "invalid_request", "bad cursor")
|
||
return {"items": items, "next_cursor": nxt}
|
||
|
||
|
||
# ============================================================================
|
||
# 6. ВЛОЖЕНИЯ (lab4): байты — в приватном S3, метаданные — в PostgreSQL
|
||
# ============================================================================
|
||
_upload_slots: asyncio.Semaphore | None = None
|
||
_image_checks: asyncio.Semaphore | None = None
|
||
|
||
|
||
def _slots() -> tuple[asyncio.Semaphore, asyncio.Semaphore]:
|
||
global _upload_slots, _image_checks
|
||
if _upload_slots is None:
|
||
_upload_slots = asyncio.Semaphore(settings.upload_max_concurrent)
|
||
_image_checks = asyncio.Semaphore(settings.image_check_concurrency)
|
||
return _upload_slots, _image_checks
|
||
|
||
|
||
async def _discard_upload(key: str) -> None:
|
||
"""Компенсация: удалить объект и строку журнала. Если не вышло —
|
||
строка журнала останется, и объект удалит janitor после grace period."""
|
||
try:
|
||
async with db.get_pool().connection() as conn:
|
||
async with conn.transaction():
|
||
cur = await conn.execute(
|
||
"DELETE FROM upload_intents WHERE object_key = %s RETURNING 1", (key,))
|
||
if await cur.fetchone() is not None:
|
||
await storage.delete(key)
|
||
except Exception as exc: # noqa: BLE001 — компенсация best effort
|
||
logger.warning("upload compensation failed, janitor will clean up: %s", type(exc).__name__)
|
||
|
||
|
||
def _fault(request: Request, point: str) -> None:
|
||
"""Внедрение отказа для тестов уровня 4 — только при FAULT_INJECTION=true
|
||
(отдельная тестовая реплика api-fault). В обычных репликах — no-op."""
|
||
if not settings.fault_injection or request.headers.get("x-fault-inject") != point:
|
||
return
|
||
logger.warning("FAULT INJECTION: %s", point)
|
||
if point in ("crash-after-s3-put", "crash-after-commit-before-publish"):
|
||
os._exit(1) # как kill -9: процесс умирает мгновенно, без finally
|
||
raise RuntimeError("injected fault: " + point)
|
||
|
||
|
||
async def _publish_now(outbox_id: int, payload: dict) -> None:
|
||
"""Быстрый путь после COMMIT: сразу отдать сообщение брокеру и отметить строку outbox.
|
||
Любая ошибка здесь НЕ влияет на ответ 202: строка outbox останется неопубликованной,
|
||
и relay (app/worker.py, поток maintenance) опубликует её позже."""
|
||
try:
|
||
async with asyncio.timeout(settings.broker_timeout):
|
||
stream_id = await broker.publish(payload) # ответ XADD = брокер принял (AOF fsync)
|
||
async with db.get_pool().connection() as conn:
|
||
await repo.mark_published(conn, outbox_id, stream_id)
|
||
except Exception as exc: # noqa: BLE001 — намерение уже в outbox
|
||
logger.warning("direct publish failed, outbox relay will retry: %s", type(exc).__name__,
|
||
extra={"event": "outbox.publish_deferred"})
|
||
|
||
|
||
@app.post("/api/v1/chats/{chat_id}/attachments", status_code=202)
|
||
async def upload_attachment(chat_id: str, request: Request, user: dict = Depends(current_user),
|
||
filename: str | None = Query(default=None)):
|
||
# 1) доступ к чату → 2) имя → 3) заявленный тип → 4) заявленный размер; всё — до чтения тела.
|
||
async with db.get_pool().connection() as conn:
|
||
chat = await accessible_chat(conn, chat_id, user)
|
||
name = uploads.validate_filename(filename)
|
||
ctype = uploads.declared_type(request)
|
||
uploads.check_declared_length(request, settings.max_upload_bytes)
|
||
|
||
slots, checks = _slots()
|
||
async with slots: # не больше N загрузок на реплику
|
||
body = await uploads.receive(request, settings.max_upload_bytes,
|
||
settings.upload_body_timeout_seconds,
|
||
settings.upload_spool_memory_bytes)
|
||
try:
|
||
async with checks: # CPU-тяжёлая проверка — ограниченно
|
||
try:
|
||
info = await run_in_threadpool(images.inspect, body.file, ctype,
|
||
settings.max_image_pixels)
|
||
except images.ImageTooLarge:
|
||
raise ApiError(413, "payload_too_large", "image has too many pixels")
|
||
except images.BadImage:
|
||
raise ApiError(415, "unsupported_media_type",
|
||
"file is not a valid image of the declared type")
|
||
|
||
storage.ensure_available() # S3 заведомо недоступен → 503 без записи в журнал
|
||
attachment_id = uuid.uuid4()
|
||
key = storage.staging_key(attachment_id) # ключ назначает сервер; сначала — staging
|
||
async with db.get_pool().connection() as conn:
|
||
await repo.add_upload_intent(conn, key, attachment_id) # журнал ДО PUT
|
||
try:
|
||
await storage.put(key, body.file, body.size, info.content_type,
|
||
body.md5_b64, body.sha256_hex)
|
||
except storage.StorageUnavailable:
|
||
await _discard_upload(key)
|
||
raise
|
||
# lab6: producer span «send attachments:jobs». Его контекст попадает в сообщение
|
||
# (traceparent), поэтому consumer span worker станет его потомком в том же trace.
|
||
with telemetry.tracer.start_as_current_span(
|
||
f"send {settings.jobs_stream}", kind=SpanKind.PRODUCER,
|
||
attributes={"messaging.system": "redis", "messaging.operation.type": "send",
|
||
"messaging.destination.name": settings.jobs_stream,
|
||
"messenger.job.type": signals.JOB_TYPE}) as producer:
|
||
try:
|
||
_fault(request, "crash-after-s3-put")
|
||
_fault(request, "db-error-after-s3-put")
|
||
async with db.get_pool().connection() as conn:
|
||
async with conn.transaction():
|
||
att, outbox_id, payload = await repo.commit_attachment(
|
||
conn, attachment_id=attachment_id, chat_id=chat["id"],
|
||
owner_id=user["id"], filename=name, content_type=info.content_type,
|
||
size_bytes=body.size, sha256=body.sha256_hex,
|
||
width=info.width, height=info.height,
|
||
object_key=storage.object_key(attachment_id), staging_key=key,
|
||
pipeline_version=settings.pipeline_version,
|
||
max_attempts=settings.job_max_attempts)
|
||
producer.set_attribute("messenger.job.id", payload["job_id"])
|
||
producer.set_attribute("messenger.attachment.id", payload["attachment_id"])
|
||
except Exception as exc:
|
||
# PUT прошёл, COMMIT — нет: компенсируем сразу, 202 не отдаём.
|
||
logger.warning("attachment commit failed: %s", type(exc).__name__,
|
||
extra={"event": "attachment.commit_failed"})
|
||
producer.set_status(Status(StatusCode.ERROR, "commit failed"))
|
||
await _discard_upload(key)
|
||
raise ApiError(503, "unavailable", "attachment was not saved, retry later")
|
||
# COMMIT прошёл: оригинал в staging, job и outbox в БД — работа уже не потеряется.
|
||
_fault(request, "crash-after-commit-before-publish")
|
||
await _publish_now(outbox_id, payload)
|
||
logger.info("attachment accepted, job queued", extra={
|
||
"event": "attachment.accepted", "job.id": payload["job_id"],
|
||
"attachment.content_type": info.content_type, "attachment.size_bytes": body.size})
|
||
return att
|
||
finally:
|
||
body.file.close() # буфер запроса удаляется всегда
|
||
|
||
|
||
async def accessible_attachment(conn, attachment_id: str, user: dict):
|
||
aid = parse_uuid(attachment_id, "attachment_id")
|
||
row = await repo.get_attachment_for_member(conn, aid, user["id"])
|
||
if row is None:
|
||
# Нет вложения ИЛИ пользователь не участник его чата — одинаковый 404.
|
||
raise ApiError(404, "not_found", "attachment not found")
|
||
return row
|
||
|
||
|
||
@app.get("/api/v1/attachments/{attachment_id}")
|
||
async def get_attachment(attachment_id: str, user: dict = Depends(current_user)):
|
||
async with db.get_pool().connection() as conn:
|
||
row = await accessible_attachment(conn, attachment_id, user)
|
||
variants = await repo.get_variants(conn, row[0], settings.pipeline_version) \
|
||
if row[7] == "ready" else []
|
||
return repo.attachment_out(row, variants)
|
||
|
||
|
||
@app.get("/api/v1/attachments/{attachment_id}/content")
|
||
async def download_attachment(attachment_id: str, user: dict = Depends(current_user),
|
||
variant: str = Query(default="original")):
|
||
parse_uuid(attachment_id, "attachment_id")
|
||
if variant != "original" and variant not in KNOWN_VARIANTS:
|
||
raise ApiError(400, "invalid_request", "unknown variant")
|
||
async with db.get_pool().connection() as conn:
|
||
row = await accessible_attachment(conn, attachment_id, user)
|
||
att = repo.attachment_out(row)
|
||
if att["status"] != "ready":
|
||
# queued / processing / failed: байтов результата ещё (или уже) нет.
|
||
raise ApiError(409, "conflict", "attachment is not ready")
|
||
key, ctype, etag = row[11], att["content_type"], att["sha256"]
|
||
if variant != "original":
|
||
v = await repo.get_variant(conn, row[0], settings.pipeline_version, variant)
|
||
if v is None:
|
||
raise ApiError(404, "not_found", "variant not found")
|
||
key, ctype, etag = v[0], v[1], v[3]
|
||
try:
|
||
stream, length = await storage.open_object(key)
|
||
except storage.ObjectMissing:
|
||
logger.error("object for attachment %s is missing in storage", att["id"])
|
||
raise ApiError(500, "internal_error", "internal error")
|
||
headers = {
|
||
"Content-Length": str(length),
|
||
"ETag": f'"{etag}"',
|
||
"Cache-Control": "private, no-cache",
|
||
"X-Content-Type-Options": "nosniff",
|
||
# filename только для отображения; RFC 5987 — любые символы безопасно закодированы.
|
||
"Content-Disposition": "inline; filename*=UTF-8''" + quote(att["filename"], safe=""),
|
||
}
|
||
# Байты идут из S3 клиенту кусками по 64 КиБ — файл целиком в RAM не загружается.
|
||
return StreamingResponse(storage.iter_body(stream), media_type=ctype, headers=headers)
|
||
|
||
|
||
# ============================================================================
|
||
# lab6: server span и HTTP-метрики (http.server.request.duration, секунды) на каждый запрос.
|
||
# Health-пробы исключены: их дёргают LB и Docker каждые 0,5–5 c, они только зашумили бы данные.
|
||
# Атрибуты: http.request.method, http.route (шаблон), http.response.status_code — без реальных ID.
|
||
# ============================================================================
|
||
from opentelemetry.instrumentation.fastapi import FastAPIInstrumentor # noqa: E402
|
||
|
||
FastAPIInstrumentor.instrument_app(app, excluded_urls="/health/live,/health/ready",
|
||
exclude_spans=["receive", "send"])
|