Files
mtusi2026lab7/main.py
T
kalibr456 cb38fd5231
Skeleton contracts / contracts (pull_request) Canceled after 0s
лр7: Docker-прогон, evidence, исправления
2026-11-07 20:17:11 +03:00

839 lines
33 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import base64
import binascii
import contextlib
import hashlib
import json
import logging
import re
import threading
import time
import uuid
from contextlib import asynccontextmanager
from datetime import datetime
from pathlib import Path
from typing import Literal
from urllib.parse import quote
from uuid import UUID
import uvicorn
from fastapi import FastAPI, Header, Query, Request, Response
from fastapi.exceptions import RequestValidationError
from fastapi.responses import JSONResponse, StreamingResponse
from opentelemetry import trace
from opentelemetry.trace import SpanKind
from pydantic import BaseModel, ConfigDict, Field, field_validator, model_validator
from starlette.concurrency import run_in_threadpool
from starlette.exceptions import HTTPException as StarletteHTTPException
from starlette.routing import Match
import settings
import telemetry
# lab6: телеметрия настраивается до первого соединения с БД (pool.open в lifespan).
telemetry.setup("messenger-api")
import attachments # noqa: E402
import auth # noqa: E402
import db # noqa: E402
import secretstore # noqa: E402
import storage # noqa: E402
import tlsutil # noqa: E402
import uploads # noqa: E402
from errors import (ApiError, bad_request, conflict, not_found, payload_too_large, unauthorized, # noqa: E402
unavailable)
log = logging.getLogger("messenger")
# HTTP-метрики (contracts/observability.md). Метки: метод, ШАБЛОН маршрута, код и класс статуса —
# никаких реальных UUID, raw URL, request_id. Длительность — до отправки заголовков ответа
# (для скачивания файла тело потом стримится из S3 и в длительность не входит).
HTTP_DURATION = telemetry.meter.create_histogram(
"http.server.request.duration", unit="s", description="Duration of HTTP server requests")
HTTP_ACTIVE = telemetry.meter.create_up_down_counter(
"http.server.active_requests", unit="{request}", description="HTTP requests being processed now")
KNOWN_METHODS = {"GET", "POST", "PUT", "PATCH", "DELETE", "HEAD", "OPTIONS"}
DRAINING = threading.Event()
STARTED = threading.Event()
secretstore.install_metrics(telemetry.meter)
def _observe_tls_rejected(_options):
from opentelemetry.metrics import Observation
return [Observation(tlsutil.REJECTED["count"])]
telemetry.meter.create_observable_counter(
"messenger.tls.rejected_clients", [_observe_tls_rejected], unit="{connection}",
description="mTLS connections rejected after handshake (identity not allowed / revoked)")
class Server(uvicorn.Server):
"""Остановка по SIGTERM в два шага.
1. /health/ready сразу отвечает 503, а запросы ещё обслуживаются. За SHUTDOWN_DRAIN_SECONDS
HAProxy видит 503 (проверка раз в 0,5-1 с, 2 неудачи) и перестаёт слать сюда новые запросы.
2. Обычная остановка uvicorn: закрыть приём соединений и доделать начатые запросы
(не дольше SHUTDOWN_TIMEOUT_SECONDS). Без шага 1 запрос, пришедший в момент SIGTERM,
получал бы 502 от балансировщика.
Повторный сигнал останавливает сразу.
"""
companions: list = [] # lab7: loopback health listener останавливается вместе с основным
def handle_exit(self, sig, frame):
if DRAINING.is_set():
self._exit_all(sig, frame)
return
DRAINING.set()
log.info("Signal %s: readiness -> 503, leaving the balancer, stop in %s s",
sig, settings.SHUTDOWN_DRAIN_SECONDS)
threading.Timer(settings.SHUTDOWN_DRAIN_SECONDS, self._exit_all, (sig, frame)).start()
def _exit_all(self, sig, frame):
for companion in self.companions:
companion.should_exit = True
super().handle_exit(sig, frame)
class CompanionServer(uvicorn.Server):
"""Второй listener в том же процессе: сигналы обрабатывает основной Server."""
@contextlib.contextmanager
def capture_signals(self):
yield
@asynccontextmanager
async def lifespan(app: FastAPI):
settings.check()
# lab7: учётные данные (PostgreSQL, Redis, S3) — из OpenBao по сертификату сервиса. Нет секретов -> не стартуем.
await run_in_threadpool(secretstore.store.start)
secretstore.write_instance_file()
# Миграции применяет сервис migrate (migrate.py); реплика только ждёт готовую схему.
await run_in_threadpool(db.wait_for_schema, settings.SCHEMA_WAIT_SECONDS)
# Только для временных файлов текущих загрузок (удаляются в конце запроса); оригиналы живут в S3.
Path(settings.UPLOAD_TMP_DIR).mkdir(parents=True, exist_ok=True)
db.pool.open()
STARTED.set()
log.info("Started: instance=%s, version=%s, env=%s, session TTL=%s s, sessions in Redis, files in S3 bucket %s, "
"processing via outbox (pipeline v%s), mTLS clients allowed: %s, Argon2id m=%s KiB t=%s p=%s",
settings.INSTANCE_ID, settings.APP_VERSION, settings.APP_ENV,
settings.SESSION_TTL_SECONDS, settings.S3_BUCKET, settings.PIPELINE_VERSION,
",".join(sorted(settings.API_ALLOWED_CLIENTS)), settings.ARGON2_MEMORY_KIB, settings.ARGON2_ITERATIONS,
settings.ARGON2_PARALLELISM, extra={"event": "service.started"})
yield
db.pool.close()
auth.close()
log.info("Stopped: connections closed, data stays in PostgreSQL and Redis")
secretstore.store.stop()
telemetry.shutdown()
app = FastAPI(lifespan=lifespan)
USERNAME_RE = re.compile(r"[a-z0-9_]{3,32}")
IDEMPOTENCY_KEY_RE = re.compile(r"[A-Za-z0-9._:-]{1,128}")
class CreateUser(BaseModel):
model_config = ConfigDict(extra="forbid")
username: str = Field(pattern=r"^[a-z0-9_]{3,32}$")
display_name: str = Field(min_length=1, max_length=100)
password: str = Field(min_length=auth.PASSWORD_MIN_LENGTH, max_length=auth.PASSWORD_MAX_LENGTH)
@field_validator("display_name", "password")
@classmethod
def validate_not_blank(cls, value: str) -> str:
if not value.strip():
raise ValueError("value cannot be blank")
return value
class User(BaseModel):
id: UUID
username: str
display_name: str
created_at: datetime
class Session(BaseModel):
token: str
token_type: str
expires_at: datetime
user: User
class CreateChat(BaseModel):
model_config = ConfigDict(extra="forbid")
title: str = Field(min_length=1, max_length=100)
member_ids: list[UUID] = Field(min_length=1, max_length=100)
@field_validator("title")
@classmethod
def validate_title(cls, value: str) -> str:
if not value.strip():
raise ValueError("title cannot be empty")
return value
class Chat(BaseModel):
id: UUID
title: str
member_ids: list[UUID]
created_at: datetime
class ChatPage(BaseModel):
items: list[Chat]
next_cursor: str | None
class CreateMessage(BaseModel):
model_config = ConfigDict(extra="forbid")
text: str = Field(max_length=4000)
# С lab4: 0-10 уникальных вложений этого чата; пустой текст допустим только с вложением.
attachment_ids: list[UUID] = Field(default_factory=list, max_length=10)
@model_validator(mode="after")
def validate_content(self):
if len(set(self.attachment_ids)) != len(self.attachment_ids):
raise ValueError("attachment_ids must be unique")
if not self.attachment_ids and not self.text.strip():
raise ValueError("text cannot be empty without attachments")
return self
class Message(BaseModel):
id: UUID
chat_id: UUID
sender_id: UUID
text: str
created_at: datetime
attachment_ids: list[UUID]
class Variant(BaseModel):
name: Literal["thumbnail", "crop", "optimized"]
content_type: Literal["image/jpeg", "image/png", "image/webp"]
size_bytes: int
width: int
height: int
sha256: str
class Attachment(BaseModel):
id: UUID
chat_id: UUID
owner_id: UUID
filename: str
content_type: str
size_bytes: int
sha256: str
status: Literal["queued", "processing", "ready", "failed"]
variants: list[Variant]
error_code: str | None
created_at: datetime
updated_at: datetime
class MessagePage(BaseModel):
items: list[Message]
next_cursor: str | None
def error_response(
request: Request,
code: str,
message: str,
status_code: int,
headers: dict | None = None,
):
request_id = request.state.request_id
return JSONResponse(
status_code=status_code,
content={
"error": {
"code": code,
"message": message,
"request_id": request_id,
}
},
headers={"X-Request-Id": request_id, **(headers or {})},
)
@app.exception_handler(ApiError)
async def api_error_handler(request: Request, exc: ApiError):
return error_response(request, exc.code, exc.message, exc.status_code, exc.headers)
@app.exception_handler(RequestValidationError)
async def validation_exception_handler(
request: Request,
exc: RequestValidationError,
):
return error_response(
request,
"invalid_request",
"Invalid request data",
400,
)
@app.exception_handler(StarletteHTTPException)
async def http_exception_handler(request: Request, exc: StarletteHTTPException):
if exc.status_code == 404:
return error_response(request, "not_found", "Not found", 404)
return error_response(request, "invalid_request", "Request cannot be processed", exc.status_code, exc.headers)
@app.exception_handler(Exception)
async def general_exception_handler(
request: Request,
exc: Exception,
):
return error_response(
request,
"internal_error",
"Internal server error",
500,
)
PUBLIC_ENDPOINTS = {
("POST", "/api/v1/users"),
("POST", "/api/v1/auth/sessions"),
}
BODY_METHODS = {"POST", "PUT", "PATCH", "DELETE"}
def is_upload(method: str, path: str) -> bool:
return method == "POST" and path.startswith("/api/v1/chats/") and path.endswith("/attachments")
class BodyLimit:
"""lab7: ASGI-обёртка — тело обычного (не upload) запроса не больше MAX_JSON_BYTES, в том числе chunked.
Тело читается до лимита ДО приложения: превышение — сразу 413 в формате контракта (FastAPI превратил бы
исключение при чтении тела в 400), иначе прочитанное тело передаётся приложению как есть.
Вложения ограничивает uploads.receive (MAX_UPLOAD_BYTES, потоково).
"""
def __init__(self, app):
self.app = app
async def __call__(self, scope, receive, send):
if (scope["type"] != "http" or scope["method"] not in BODY_METHODS
or is_upload(scope["method"], scope["path"])):
return await self.app(scope, receive, send)
chunks, size, more = [], 0, True
while more:
message = await receive()
if message["type"] == "http.disconnect":
return
body = message.get("body", b"")
size += len(body)
if size > settings.MAX_JSON_BYTES:
return await self.reject(send)
chunks.append(body)
more = message.get("more_body", False)
buffered = [{"type": "http.request", "body": b"".join(chunks), "more_body": False}]
async def replay():
return buffered.pop() if buffered else await receive()
return await self.app(scope, replay, send)
@staticmethod
async def reject(send):
request_id = str(uuid.uuid4())
body = json.dumps({"error": {"code": "payload_too_large", "message": "Request body is too large",
"request_id": request_id}}).encode()
await send({"type": "http.response.start", "status": 413, "headers": [
(b"content-type", b"application/json"), (b"content-length", str(len(body)).encode()),
(b"x-request-id", request_id.encode()), (b"x-instance-id", settings.INSTANCE_ID.encode()),
(b"connection", b"close")]})
await send({"type": "http.response.body", "body": body})
def client_address(request: Request) -> str:
"""IP клиента для rate limit. X-Forwarded-For выставляет (перезаписывает) только LB, а соединение
LB -> API аутентифицировано mTLS, поэтому заголовку можно верить; без него — адрес соединения."""
forwarded = request.headers.get("x-forwarded-for", "")
if forwarded:
return forwarded.split(",")[-1].strip()
return request.client.host if request.client else "unknown"
def route_template(request: Request) -> str:
"""Шаблон маршрута (/api/v1/chats/{chat_id}/messages) — метка метрик и имя спана вместо raw URL."""
for route in app.router.routes:
match, _ = route.matches(request.scope)
if match != Match.NONE:
return getattr(route, "path", "unmatched")
return "unmatched"
@app.middleware("http")
async def request_context(request: Request, call_next):
request_id = str(uuid.uuid4())
request.state.request_id = request_id
started = time.perf_counter()
path = request.url.path
method = request.method if request.method in KNOWN_METHODS else "_OTHER"
route = route_template(request)
# Проверки здоровья (HAProxy раз в секунду, Docker) — только в метриках, без трасс: иначе это
# большая часть всех трасс. Входящий traceparent клиента продолжается (W3C Trace Context).
traced = not path.startswith("/health/")
span_scope = (telemetry.tracer.start_as_current_span(
f"{method} {route}", context=telemetry.extract(request.headers), kind=SpanKind.SERVER,
attributes={"http.request.method": method, "http.route": route, "url.scheme": "http",
"request.id": request_id},
record_exception=False, set_status_on_exception=False)
if traced else contextlib.nullcontext(trace.INVALID_SPAN))
HTTP_ACTIVE.add(1, {"http.request.method": method})
status = 500
try:
with span_scope as span:
try:
if request.method in BODY_METHODS and not is_upload(request.method, path):
length = request.headers.get("content-length")
if length is not None and (not length.isdigit() or int(length) > settings.MAX_JSON_BYTES):
raise payload_too_large("Request body is too large")
if path.startswith("/api/") and (request.method, path) not in PUBLIC_ENDPOINTS:
request.state.session = await run_in_threadpool(
auth.check_bearer, request.headers.get("Authorization")
)
response = await call_next(request)
except ApiError as exc:
response = error_response(request, exc.code, exc.message, exc.status_code, exc.headers)
except Exception as exc:
log.exception("Unhandled error", extra={"event": "http.unhandled_error", "request_id": request_id})
telemetry.mark_error(span, type(exc).__name__)
response = error_response(request, "internal_error", "Internal server error", 500)
status = response.status_code
span.set_attribute("http.response.status_code", status)
if status >= 500:
telemetry.mark_error(span, str(status))
response.headers["X-Request-Id"] = request_id
response.headers["X-Instance-Id"] = settings.INSTANCE_ID # какая реплика ответила
trace_id = telemetry.trace_id_hex() if traced else None
if trace_id:
response.headers["X-Trace-Id"] = trace_id # расширение: найти трассу по ответу
elapsed = time.perf_counter() - started
# Проверки здоровья приходят каждую секунду от HAProxy и Docker, в лог пишем только ошибки.
level = logging.DEBUG if not traced and status == 200 else logging.INFO
log.log(level, "%s %s -> %s (%.1f ms)", method, route, status, elapsed * 1000,
extra={"event": "http.request", "http.request.method": method, "http.route": route,
"http.response.status_code": status, "duration_ms": round(elapsed * 1000, 1),
"request_id": request_id})
finally:
HTTP_ACTIVE.add(-1, {"http.request.method": method})
elapsed = time.perf_counter() - started
HTTP_DURATION.record(elapsed, {"http.request.method": method, "http.route": route,
"http.response.status_code": status,
"http.response.status_class": f"{status // 100}xx"})
return response
def current_user_id(request: Request) -> UUID:
session = getattr(request.state, "session", None)
if session is None:
raise unauthorized("Bearer token is required")
return session.user_id
def encode_cursor(
created_at: datetime,
object_id: UUID,
*,
chat_id: UUID | None = None,
user_id: UUID | None = None,
) -> str:
data = {
"created_at": created_at.isoformat(),
"id": str(object_id),
}
if chat_id is not None:
data["chat_id"] = str(chat_id)
if user_id is not None:
data["user_id"] = str(user_id)
raw = json.dumps(data).encode("utf-8")
return base64.urlsafe_b64encode(raw).decode("ascii")
def decode_cursor(cursor: str):
try:
raw = base64.urlsafe_b64decode(cursor.encode("ascii"))
data = json.loads(raw.decode("utf-8"))
created_at = datetime.fromisoformat(data["created_at"])
if created_at.tzinfo is None:
raise ValueError("Invalid cursor")
if not isinstance(data["id"], str):
raise ValueError("Invalid cursor")
object_id = UUID(data["id"])
chat_id = UUID(data["chat_id"]) if "chat_id" in data else None
user_id = UUID(data["user_id"]) if "user_id" in data else None
return created_at, object_id, chat_id, user_id
except (
ValueError,
KeyError,
TypeError,
AttributeError,
json.JSONDecodeError,
binascii.Error,
):
raise ValueError("Invalid cursor")
def page_after(cursor: str | None, *, chat_id: UUID | None = None, user_id: UUID | None = None):
if cursor is None:
return None
try:
created_at, object_id, cursor_chat_id, cursor_user_id = decode_cursor(cursor)
except ValueError:
raise bad_request("Invalid cursor") from None
if cursor_chat_id != chat_id or cursor_user_id != user_id:
raise bad_request("Invalid cursor")
return created_at, object_id
@app.get("/health/live")
def health_live():
return {"status": "ok"}
@app.get("/health/ready")
def health_ready():
if not STARTED.is_set():
raise unavailable("Instance is starting")
if DRAINING.is_set():
raise unavailable("Instance is shutting down")
db.ping()
auth.ping_redis()
return {"status": "ok"}
@app.post("/api/v1/users", response_model=User, status_code=201)
def create_user(request: Request, user_data: CreateUser):
# lab7: регистрация тоже стоит Argon2id — общий лимит на IP до хеширования.
auth.check_rate("register-ip", client_address(request), settings.REGISTER_RATE_PER_CLIENT, fail_open=True)
password_hash = auth.hash_password(user_data.password) # сам пароль нигде не сохраняется
return db.create_user(user_data.username, user_data.display_name, password_hash)
@app.post("/api/v1/auth/sessions", response_model=Session, status_code=201)
def create_session(
request: Request,
response: Response,
authorization: str | None = Header(default=None),
):
credentials = auth.parse_basic(authorization)
if credentials is None:
raise auth.login_failed()
username, password = credentials
# lab7: общий для реплик лимит ДО дорогого Argon2id: на IP клиента и на username (подбор пароля
# одной учётной записи с разных адресов). Превышение -> 429 + Retry-After.
auth.check_rate("login-ip", client_address(request), settings.LOGIN_RATE_PER_CLIENT)
auth.check_rate("login-user", username.lower()[:64], settings.LOGIN_RATE_PER_USERNAME)
found = None
if USERNAME_RE.fullmatch(username) and len(password) <= auth.PASSWORD_MAX_LENGTH:
found = db.get_user_with_password(username)
if not auth.password_matches(found["password_hash"] if found else None, password):
raise auth.login_failed()
# lab7: hash со старыми параметрами (например, lab2–6: m=12 MiB, t=3) пересчитывается при входе.
if auth.needs_rehash(found["password_hash"]):
try:
if db.update_password_hash(found["id"], found["password_hash"], auth.rehash(password)):
auth.REHASHED.add(1)
log.info("Password hash upgraded to current Argon2id parameters",
extra={"event": "auth.rehashed", "enduser.id": str(found["id"])})
except ApiError:
pass # не удалось (занят бюджет/БД) — вход всё равно успешен, пересчитаем в следующий раз
token, expires_at = auth.create_session(found["id"])
response.headers["Cache-Control"] = "no-store"
user = {key: found[key] for key in ("id", "username", "display_name", "created_at")}
return {"token": token, "token_type": "Bearer", "expires_at": expires_at, "user": user}
@app.delete("/api/v1/auth/sessions/current", status_code=204)
def delete_session(request: Request):
current_user_id(request)
auth.delete_session(request.state.session)
return Response(status_code=204)
@app.get("/api/v1/users/me", response_model=User)
def get_current_user(request: Request):
user = db.get_user(current_user_id(request))
if user is None:
raise unauthorized("Session is invalid or expired")
return user
@app.post("/api/v1/chats", response_model=Chat, status_code=201)
def create_chat(request: Request, chat_data: CreateChat):
user_id = current_user_id(request)
if user_id not in chat_data.member_ids:
raise bad_request("Creator must be a chat member")
if len(chat_data.member_ids) != len(set(chat_data.member_ids)):
raise bad_request("Duplicate member IDs")
return db.create_chat(chat_data.title, chat_data.member_ids)
@app.get("/api/v1/chats", response_model=ChatPage)
def list_chats(
request: Request,
limit: int = Query(default=50, ge=1, le=100),
cursor: str | None = Query(default=None),
):
user_id = current_user_id(request)
after = page_after(cursor, user_id=user_id)
rows = db.list_chats(user_id, after, limit)
items = rows[:limit]
next_cursor = None
if len(rows) > limit:
last = items[-1]
next_cursor = encode_cursor(last["created_at"], last["id"], user_id=user_id)
return ChatPage(items=items, next_cursor=next_cursor)
@app.get("/api/v1/chats/{chat_id}", response_model=Chat)
def get_chat(request: Request, chat_id: UUID):
return db.get_chat(chat_id, current_user_id(request))
def request_fingerprint(data: BaseModel) -> bytes:
"""SHA-256 тела после разбора: порядок ключей и пробелы в JSON на него не влияют.
Пустой attachment_ids не входит в отпечаток: {"text": "x"} и {"text": "x", "attachment_ids": []}
равны (контракт), и хеши сообщений, сохранённых до lab4, остаются прежними.
"""
body = data.model_dump(mode="json")
if not body.get("attachment_ids"):
body.pop("attachment_ids", None)
canonical = json.dumps(body, sort_keys=True, ensure_ascii=False, separators=(",", ":"))
return hashlib.sha256(canonical.encode("utf-8")).digest()
@app.post(
"/api/v1/chats/{chat_id}/messages",
response_model=Message,
status_code=201,
)
def create_message(
request: Request,
message_data: CreateMessage,
chat_id: UUID,
idempotency_key: str | None = Header(default=None),
):
# С lab3 ключ обязателен: повтор того же запроса (после таймаута, через другую реплику)
# вернёт то же сообщение, а не создаст второе. См. db.add_message.
if idempotency_key is None or not IDEMPOTENCY_KEY_RE.fullmatch(idempotency_key):
raise bad_request("Idempotency-Key header is required: 1-128 characters A-Z a-z 0-9 . _ : -")
return db.add_message(chat_id, current_user_id(request), message_data.text, message_data.attachment_ids,
idempotency_key, request_fingerprint(message_data))
@app.get(
"/api/v1/chats/{chat_id}/messages",
response_model=MessagePage,
)
def list_messages(
request: Request,
chat_id: UUID,
limit: int = Query(default=50, ge=1, le=100),
cursor: str | None = Query(default=None),
):
user_id = current_user_id(request)
after = page_after(cursor, chat_id=chat_id)
rows = db.list_messages(chat_id, user_id, after, limit)
items = rows[:limit]
next_cursor = None
if len(rows) > limit:
last = items[-1]
next_cursor = encode_cursor(last["created_at"], last["id"], chat_id=chat_id)
return MessagePage(items=items, next_cursor=next_cursor)
@app.post(
"/api/v1/chats/{chat_id}/attachments",
response_model=Attachment,
status_code=202,
)
async def upload_attachment(
request: Request,
chat_id: UUID,
filename: str | None = Query(default=None),
):
"""Приём PNG/JPEG сырыми байтами. 202 queued — когда исходник в staging (S3), а задание и сообщение
для брокера зафиксированы в PostgreSQL одной транзакцией (attachments.store). Обработку делает worker.
Порядок проверок: сессия (middleware) -> участник чата (404) -> filename (400) -> Content-Type (415)
-> Content-Length (413) — всё до чтения тела; затем потоковое чтение с лимитом (413),
проверка содержимого (415/413) и запись.
"""
user_id = current_user_id(request)
try:
await run_in_threadpool(db.check_member, chat_id, user_id)
name = uploads.validate_filename(filename)
declared = uploads.declared_type(request.headers.get("content-type"))
uploads.declared_length(request.headers.get("content-length"))
except ApiError:
await uploads.drain(request) # чтобы клиент получил ответ с ошибкой, а не обрыв на отправке
raise
upload = await uploads.receive(request)
try:
image = await run_in_threadpool(inspect_traced, upload, declared)
row = await run_in_threadpool(attachments.store, chat_id, user_id, name, upload, image)
finally:
upload.file.close() # временный файл удаляется сразу; исходник остаётся только в staging (S3)
return attachments.public(row)
def inspect_traced(upload, declared):
"""Проверка содержимого — CPU в обработчике (не БД): в трассе видно отдельным спаном."""
with telemetry.internal_span("inspect image", {"http.request.body.size": upload.size}):
return uploads.inspect(upload, declared)
@app.get("/api/v1/attachments/{attachment_id}", response_model=Attachment)
def get_attachment(request: Request, attachment_id: UUID):
return attachments.public(db.get_attachment(attachment_id, current_user_id(request)))
VARIANT_SUFFIX = {"image/jpeg": ".jpg", "image/png": ".png", "image/webp": ".webp"}
@app.get("/api/v1/attachments/{attachment_id}/content")
def download_attachment(
request: Request,
attachment_id: UUID,
variant: Literal["original", "thumbnail", "crop", "optimized"] = Query(default="original"),
):
"""Байты оригинала или варианта через API (без redirect): сначала участник чата, потом S3.
queued/processing/failed -> 409 (ещё/уже нечего отдавать); варианта нет у ready -> 404.
"""
row = db.get_attachment(attachment_id, current_user_id(request))
if row["state"] != "ready":
raise conflict("Attachment is not ready")
if variant == "original":
key, content_type, size, sha256 = row["object_key"], row["content_type"], row["size_bytes"], row["sha256"]
name = row["filename"]
else:
found = [v for v in row["variants"] if v["name"] == variant]
if not found:
raise not_found("Variant not found")
v = found[0]
key, content_type, size, sha256 = v["object_key"], v["content_type"], v["size_bytes"], v["sha256"]
stem = row["filename"].rsplit(".", 1)[0] or "image"
name = f"{stem}.{variant}{VARIANT_SUFFIX[content_type]}"
try:
stream = storage.open_read(key)
except storage.ObjectMissing:
log.error("Object %s of ready attachment %s is missing in S3", variant, row["id"],
extra={"event": "attachment.object_missing", "attachment.id": str(row["id"])})
raise unavailable("File is temporarily unavailable") from None
except storage.StorageUnavailable:
raise unavailable("File storage is temporarily unavailable") from None
headers = {
"Content-Length": str(size),
"Content-Disposition": "inline; filename*=UTF-8''" + quote(name, safe=""),
"X-Content-Type-Options": "nosniff",
"Cache-Control": "private, no-store",
"ETag": '"' + sha256 + '"',
}
return StreamingResponse(storage.iter_chunks(stream), media_type=content_type, headers=headers)
# ---------- lab7: loopback health listener ----------
# Только для healthcheck контейнера (Docker проверяет изнутри контейнера): 127.0.0.1, plaintext,
# два пути /health/live и /health/ready; бизнес-API здесь нет (любой другой путь — 404). Снаружи
# контейнера порт недоступен (loopback), поэтому обходом mTLS он не является.
health_app = FastAPI()
@health_app.middleware("http")
async def health_context(request: Request, call_next):
request.state.request_id = str(uuid.uuid4())
try:
response = await call_next(request)
except ApiError as exc:
response = error_response(request, exc.code, exc.message, exc.status_code, exc.headers)
response.headers["X-Request-Id"] = request.state.request_id
response.headers["X-Instance-Id"] = settings.INSTANCE_ID
return response
@health_app.exception_handler(ApiError)
async def health_error(request: Request, exc: ApiError):
return error_response(request, exc.code, exc.message, exc.status_code, exc.headers)
@health_app.exception_handler(StarletteHTTPException)
async def health_not_found(request: Request, exc: StarletteHTTPException):
return error_response(request, "not_found", "Not found", 404)
health_app.get("/health/live")(health_live)
health_app.get("/health/ready")(health_ready)
def serve() -> None:
import asyncio
common = {"timeout_graceful_shutdown": settings.SHUTDOWN_TIMEOUT_SECONDS, "access_log": False,
"log_config": None} # логи uvicorn идут в общий JSON/OTLP-обработчик (telemetry.py)
if settings.API_TLS:
tlsutil.init_policy()
main_config = uvicorn.Config(
BodyLimit(app), host=settings.APP_HOST, port=settings.APP_PORT, lifespan="on",
http=tlsutil.MTLSProtocol, ssl_context_factory=lambda _config, _default: tlsutil.server_context(),
**common)
else: # только для локальной отладки без TLS; compose всегда API_TLS=1
main_config = uvicorn.Config(BodyLimit(app), host=settings.APP_HOST, port=settings.APP_PORT,
lifespan="on", **common)
health = CompanionServer(uvicorn.Config(health_app, host=settings.HEALTH_HOST, port=settings.HEALTH_PORT,
lifespan="off", **common))
server = Server(main_config)
server.companions = [health]
async def run_both():
await asyncio.gather(server.serve(), health.serve())
asyncio.run(run_both())
if __name__ == "__main__":
serve()