Перенесла файлы проекта из предыдущей лабораторной работы

This commit is contained in:
vasyukova
2026-11-04 22:30:42 +03:00
parent 20afc2c790
commit e9a7a7e793
70 changed files with 8588 additions and 0 deletions
View File
+246
View File
@@ -0,0 +1,246 @@
"""Вложения (lab5/lab6): upload → 202 queued, async worker, download вариантов.
lab6: при создании outbox-строки кладём в payload W3C traceparent/tracestate
текущего HTTP-запроса. Ошибка инжекта не должна блокировать upload.
"""
import hashlib
import io
import os
import unicodedata
from datetime import datetime, timezone
from typing import Optional
from uuid import UUID, uuid4
from fastapi import APIRouter, Depends, HTTPException, Query, Request
from fastapi.responses import Response
from PIL import Image, UnidentifiedImageError
from starlette.concurrency import run_in_threadpool
from botocore.exceptions import ClientError
from opentelemetry.trace.propagation.tracecontext import TraceContextTextMapPropagator
from ..models.schemas import Attachment, User
from ..storage import s3
from ..storage.postgres import storage
from .security import current_user
router = APIRouter(prefix="/api/v1", tags=["attachments"])
MAX_UPLOAD_BYTES = int(os.environ.get("MAX_UPLOAD_BYTES", "10485760"))
MAX_IMAGE_PIXELS = int(os.environ.get("MAX_IMAGE_PIXELS", "40000000"))
PIPELINE_VERSION = 1
_propagator = TraceContextTextMapPropagator()
def _require_member(chat_id: UUID, user_id: UUID):
chat = storage.get_chat(chat_id)
if chat is None or user_id not in chat.member_ids:
raise HTTPException(status_code=404, detail="not_found")
return chat
def _validate_filename(name: str) -> str:
if not name or len(name) > 255:
raise HTTPException(status_code=400, detail="invalid_request")
if name.strip() != name or name.strip() == "":
raise HTTPException(status_code=400, detail="invalid_request")
for ch in name:
if ch in ("/", "\\", "\x00") or unicodedata.category(ch).startswith("C"):
raise HTTPException(status_code=400, detail="invalid_request")
return name
def _detect_content_type(data: bytes) -> Optional[str]:
"""MIME по содержимому, не по заголовку."""
if data.startswith(b"\x89PNG\r\n\x1a\n"):
return "image/png"
if data.startswith(b"\xff\xd8\xff"):
return "image/jpeg"
return None
def _validate_image(data: bytes) -> str:
"""MIME по содержимому + лимит пикселей + полная проверка декодирования."""
ctype = _detect_content_type(data)
if ctype is None:
raise HTTPException(status_code=415, detail="unsupported_media_type")
try:
img = Image.open(io.BytesIO(data))
width, height = img.size
if width <= 0 or height <= 0:
raise HTTPException(status_code=415, detail="unsupported_media_type")
if width * height > MAX_IMAGE_PIXELS:
raise HTTPException(status_code=413, detail="payload_too_large")
img.load()
except HTTPException:
raise
except Image.DecompressionBombError:
raise HTTPException(status_code=413, detail="payload_too_large")
except Exception:
raise HTTPException(status_code=415, detail="unsupported_media_type")
return ctype
@router.post(
path="/chats/{chat_id}/attachments",
summary="Принять один PNG/JPEG, до 10 MiB (lab5: 202 queued)",
response_model=Attachment,
status_code=202,
)
async def upload_attachment(
chat_id: UUID,
request: Request,
user: User = Depends(current_user),
filename: str = Query(...),
) -> Attachment:
_require_member(chat_id, user.id)
safe_name = _validate_filename(filename)
cl = request.headers.get("content-length")
if cl is not None:
try:
if int(cl) > MAX_UPLOAD_BYTES:
raise HTTPException(status_code=413, detail="payload_too_large")
except ValueError:
raise HTTPException(status_code=400, detail="invalid_request")
chunks: list[bytes] = []
total = 0
async for chunk in request.stream():
if not chunk:
continue
total += len(chunk)
if total > MAX_UPLOAD_BYTES:
raise HTTPException(status_code=413, detail="payload_too_large")
chunks.append(chunk)
body = b"".join(chunks)
if not body:
raise HTTPException(status_code=400, detail="invalid_request")
content_type = await run_in_threadpool(_validate_image, body)
sha256 = hashlib.sha256(body).hexdigest()
attachment_id = uuid4()
staging_key = s3.staging_key(chat_id, attachment_id)
# 1) Durable staging в S3 (приватный prefix).
try:
await run_in_threadpool(s3.put_bytes, staging_key, body, content_type)
except Exception as exc: # noqa: BLE001
raise HTTPException(status_code=503, detail="unavailable") from exc
now = datetime.now(timezone.utc)
att = Attachment(
id=attachment_id,
chat_id=chat_id,
owner_id=user.id,
filename=safe_name,
content_type=content_type,
size_bytes=len(body),
sha256=sha256,
status="queued",
variants=[],
error_code=None,
created_at=now,
updated_at=now,
)
job_id = uuid4()
# lab6: снимок W3C trace context активного HTTP span.
trace_carrier: dict[str, str] = {}
try:
_propagator.inject(trace_carrier)
except Exception:
trace_carrier = {}
outbox_payload = {
"job_id": str(job_id),
"type": "attachment.process.v1",
"attachment_id": str(attachment_id),
"pipeline_version": PIPELINE_VERSION,
"created_at": now.isoformat().replace("+00:00", "Z"),
}
if trace_carrier.get("traceparent"):
outbox_payload["traceparent"] = trace_carrier["traceparent"]
if trace_carrier.get("tracestate"):
outbox_payload["tracestate"] = trace_carrier["tracestate"]
# 2) INSERT attachment + outbox в ОДНОЙ транзакции.
# При сбое — компенсация: удаляем staging.
try:
await run_in_threadpool(
storage.create_attachment_queued, att, staging_key, outbox_payload
)
except Exception:
try:
await run_in_threadpool(s3.delete_object, staging_key)
except Exception:
pass
raise HTTPException(status_code=503, detail="unavailable")
return att
@router.get(
path="/attachments/{attachment_id}",
summary="Метаданные вложения, доступ участникам чата",
response_model=Attachment,
)
def get_attachment(
attachment_id: UUID,
user: User = Depends(current_user),
) -> Attachment:
pair = storage.get_attachment_with_key(attachment_id)
if pair is None:
raise HTTPException(status_code=404, detail="not_found")
att, _key = pair
_require_member(att.chat_id, user.id)
return att
@router.get(
path="/attachments/{attachment_id}/content",
summary="Скачать байты (original или variant) через авторизованный API",
)
def download_attachment(
attachment_id: UUID,
user: User = Depends(current_user),
variant: str = Query("original"),
):
if variant not in ("original", "thumbnail", "crop", "optimized"):
raise HTTPException(status_code=400, detail="invalid_request")
pair = storage.get_attachment_with_key(attachment_id)
if pair is None:
raise HTTPException(status_code=404, detail="not_found")
att, object_key = pair
_require_member(att.chat_id, user.id)
if att.status != "ready":
raise HTTPException(status_code=409, detail="conflict")
if variant == "original":
key = object_key
ctype_expected = att.content_type
else:
vs = [v for v in att.variants if v.name == variant]
if not vs:
raise HTTPException(status_code=404, detail="not_found")
v = vs[0]
key = s3.variant_key(att.chat_id, att.id, variant)
ctype_expected = v.content_type
try:
data, ctype = s3.get_object(key)
except ClientError as exc:
code = exc.response.get("Error", {}).get("Code", "")
if code in ("404", "NoSuchKey", "NotFound"):
raise HTTPException(status_code=404, detail="not_found")
raise HTTPException(status_code=503, detail="unavailable") from exc
except Exception as exc: # noqa: BLE001
raise HTTPException(status_code=503, detail="unavailable") from exc
return Response(
content=data,
media_type=ctype or ctype_expected,
)
+81
View File
@@ -0,0 +1,81 @@
import base64
import binascii
import os
from fastapi import APIRouter, Depends, HTTPException, Request
from fastapi.security import HTTPBasic, HTTPBasicCredentials
from ..models.schemas import Session
from ..storage.postgres import storage
router = APIRouter(prefix="/api/v1/auth", tags=["auth"])
DEFAULT_TTL_SECONDS = 3600
def _ttl_seconds() -> int:
try:
return int(os.environ.get("SESSION_TTL_SECONDS", DEFAULT_TTL_SECONDS))
except ValueError:
return DEFAULT_TTL_SECONDS
def _parse_basic_header(request: Request) -> tuple[str, str] | None:
"""Ручной разбор Basic: base64 -> UTF-8 -> split по первому ':'.
HTTPBasic из Starlette декодирует latin-1 и ломает кириллицу.
Здесь пароль читается как UTF-8 (RFC 7617 разрешает UTF-8 charset).
Username остаётся ASCII по контракту.
"""
raw = request.headers.get("Authorization", "")
if not raw.lower().startswith("basic "):
return None
try:
decoded = base64.b64decode(raw[6:].strip(), validate=True).decode("utf-8")
except (binascii.Error, UnicodeDecodeError):
return None
if ":" not in decoded:
return None
username, password = decoded.split(":", 1)
if not username or not password:
return None
try:
username.encode("ascii")
except UnicodeEncodeError:
return None
return username, password
@router.post(
path="/sessions",
summary="Вход: Basic username:password; выдаёт сессию",
response_model=Session,
status_code=201,
)
def create_session(request: Request) -> Session:
parsed = _parse_basic_header(request)
if parsed is None:
raise HTTPException(status_code=401, detail="unauthorized")
username, password = parsed
user = storage.verify_credentials(username, password)
if user is None:
raise HTTPException(status_code=401, detail="unauthorized")
token, expires_at = storage.create_session(user.id, _ttl_seconds())
return Session(token=token, token_type="Bearer", expires_at=expires_at, user=user)
@router.delete(
path="/sessions/current",
summary="Отозвать текущую сессию",
status_code=204,
)
def revoke_session(request: Request) -> None:
auth = request.headers.get("Authorization", "")
if not auth.lower().startswith("bearer "):
raise HTTPException(status_code=401, detail="unauthorized")
token = auth[7:].strip()
if not token or storage.get_session_user(token) is None:
raise HTTPException(status_code=401, detail="unauthorized")
storage.revoke_session(token)
return None
+94
View File
@@ -0,0 +1,94 @@
import base64
import json
from datetime import datetime
from uuid import UUID
from fastapi import APIRouter, Depends, HTTPException, Query
from ..models.schemas import Chat, ChatPage, CreateChat, User
from ..storage.postgres import storage
from .security import current_user
router = APIRouter(prefix="/api/v1/chats", tags=["chats"])
def _encode_cursor(user_id: UUID, chat: Chat) -> str:
"""Простой курсор: user_id + id + created_at, base64."""
data = {"user_id": str(user_id), "id": str(chat.id), "created_at": chat.created_at.isoformat()}
return base64.urlsafe_b64encode(json.dumps(data).encode()).decode()
def _decode_cursor(user_id: UUID, cursor: str) -> tuple[datetime, UUID]:
"""Курсор привязан к пользователю. Любая порча → 400, не 500."""
try:
raw = base64.urlsafe_b64decode(cursor.encode()).decode()
data = json.loads(raw)
except Exception:
raise HTTPException(status_code=400, detail="invalid_request")
if not isinstance(data, dict):
raise HTTPException(status_code=400, detail="invalid_request")
if data.get("user_id") != str(user_id):
raise HTTPException(status_code=400, detail="invalid_request")
try:
created_at = datetime.fromisoformat(data["created_at"])
chat_id = UUID(data["id"])
except (KeyError, ValueError, TypeError):
raise HTTPException(status_code=400, detail="invalid_request")
return created_at, chat_id
@router.post(
path="",
summary="Создать чат",
response_model=Chat,
status_code=201,
)
def create_chat(
payload: CreateChat,
user: User = Depends(current_user),
) -> Chat:
"""Вызывающий должен входить в member_ids"""
user_id = user.id
if user_id not in payload.member_ids:
raise HTTPException(status_code=400, detail="invalid_request")
if len(set(payload.member_ids)) != len(payload.member_ids):
raise HTTPException(status_code=400, detail="invalid_request")
try:
return storage.create_chat(payload.title, list(payload.member_ids))
except ValueError:
raise HTTPException(status_code=400, detail="invalid_request")
@router.get(
path="",
summary="Получить список чатов текущего пользователя",
response_model=ChatPage,
)
def list_chats(
user: User = Depends(current_user),
limit: int = Query(50, ge=1, le=100),
cursor: str | None = None,
) -> ChatPage:
user_id = user.id
chats = storage.list_chats_for_user(user_id)
if cursor:
ccreated, cid = _decode_cursor(user_id, cursor)
ccreated_iso = ccreated.isoformat()
chats = [c for c in chats if (c.created_at.isoformat(), str(c.id)) > (ccreated_iso, str(cid))]
page = chats[:limit]
next_cursor = _encode_cursor(user_id, page[-1]) if len(chats) > limit else None
return ChatPage(items=page, next_cursor=next_cursor)
@router.get(
path="/{chat_id}",
summary="Получить доступный чат по идентификатору",
response_model=Chat,
)
def get_chat(
chat_id: UUID,
user: User = Depends(current_user),
) -> Chat:
chat = storage.get_chat(chat_id)
if chat is None or user.id not in chat.member_ids:
raise HTTPException(status_code=404, detail="not_found")
return chat
+52
View File
@@ -0,0 +1,52 @@
from fastapi import APIRouter
from ..models.schemas import Health
from ..storage import db as _db
from ..storage import s3 as _s3
from ..storage.sessions import _redis_client, _store
router = APIRouter(tags=["health"])
@router.get(
path="/health/live",
summary="Проверка жизнеспособности процесса",
response_model=Health,
)
def liveness() -> Health:
"""Процесс жив"""
return Health(status="ok")
@router.get(
path="/health/ready",
summary="Проверка готовности принимать трафик",
response_model=Health,
)
def readiness() -> Health:
"""200, если PostgreSQL, Redis (в режиме redis) и S3 доступны.
При недоступности зависимости выбрасываем RuntimeError("*_unavailable"),
main.py конвертирует его в 503 с телом Error из contracts/openapi.json.
Все проверки — с короткими таймаутами, чтобы не зависать надолго.
"""
try:
with _db.connection() as conn, conn.cursor() as cur:
cur.execute("SELECT 1")
cur.fetchone()
except Exception as exc: # noqa: BLE001
# Пул уже проверяет живость соединения (check_connection) и короткий
# timeout=3.0; здесь лишь превращаем любую ошибку БД в 503, чтобы
# после рестарта PostgreSQL readiness быстро возвращался в 200.
raise RuntimeError("db_unavailable: %s" % type(exc).__name__) from exc
# Redis (в режиме redis)
if _store() == "redis":
_redis_client().ping()
# S3: HEAD несуществующего ключа. 404/None → S3 доступен.
# Короткий таймаут клиента гарантирует быстрый ответ, а ошибка
# преобразуется в RuntimeError("s3_unavailable") → 503.
try:
_s3.head_object("attachments/__health_probe__")
except Exception as exc: # noqa: BLE001
raise RuntimeError("s3_unavailable: %s" % type(exc).__name__) from exc
return Health(status="ok")
+141
View File
@@ -0,0 +1,141 @@
import base64
import hashlib
import json
from datetime import datetime
from uuid import UUID
from fastapi import APIRouter, Depends, Header, HTTPException, Query
from ..models.schemas import CreateMessage, Message, MessagePage, User
from ..storage.postgres import storage
from .security import current_user
router = APIRouter(prefix="/api/v1/chats", tags=["messages"])
# Контракт: Idempotency-Key ASCII [A-Za-z0-9._:-]{1,128}.
_IDEMPOTENCY_ALLOWED = set("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789._:-")
def _require_member(chat_id: UUID, user_id: UUID):
chat = storage.get_chat(chat_id)
if chat is None or user_id not in chat.member_ids:
raise HTTPException(status_code=404, detail="not_found")
return chat
def _encode_cursor(chat_id: UUID, message: Message) -> str:
data = {"chat_id": str(chat_id), "id": str(message.id),
"created_at": message.created_at.isoformat()}
return base64.urlsafe_b64encode(json.dumps(data).encode()).decode()
def _decode_cursor(chat_id: UUID, cursor: str) -> tuple:
try:
data = json.loads(base64.urlsafe_b64decode(cursor.encode()).decode())
if data.get("chat_id") != str(chat_id):
raise ValueError("cursor_chat_mismatch")
return datetime.fromisoformat(data["created_at"]), UUID(data["id"])
except Exception:
raise HTTPException(status_code=400, detail="invalid_request")
def _canonical_body_hash(payload: CreateMessage) -> str:
"""sha256 канонизированного JSON.
Поле attachment_ids присутствует в модели со значением по умолчанию [],
поэтому payload {"text":"x"} и {"text":"x","attachment_ids":[]} дают
одинаковый model_dump и одинаковый хэш — как требует контракт.
"""
data = payload.model_dump(mode="json")
canonical = json.dumps(data, sort_keys=True, ensure_ascii=False, separators=(",", ":"))
return hashlib.sha256(canonical.encode("utf-8")).hexdigest()
@router.post(
path="/{chat_id}/messages",
summary="Отправить сообщение в чат",
response_model=Message,
status_code=201,
)
def create_message(
chat_id: UUID,
payload: CreateMessage,
user: User = Depends(current_user),
idempotency_key: str = Header(..., alias="Idempotency-Key"),
) -> Message:
# Строго ASCII, как требует контракт: isalnum() пропускает кириллицу.
if not (1 <= len(idempotency_key) <= 128):
raise HTTPException(status_code=400, detail="invalid_request")
if not all(c in _IDEMPOTENCY_ALLOWED for c in idempotency_key):
raise HTTPException(status_code=400, detail="invalid_request")
_require_member(chat_id, user.id)
text = payload.text or ""
attachment_ids = list(payload.attachment_ids)
# Контракт: text (непустой) ИЛИ attachment_ids (>=1).
if text.strip() == "" and not attachment_ids:
raise HTTPException(status_code=400, detail="invalid_request")
# Все вложения должны принадлежать этому чату.
if attachment_ids:
found = storage.list_attachments_by_ids(attachment_ids, chat_id)
if len(found) != len(attachment_ids):
raise HTTPException(status_code=404, detail="not_found")
for aid in attachment_ids:
att, _key = found[aid]
if att.status == "failed":
raise HTTPException(status_code=409, detail="conflict")
method = "POST"
path = "/api/v1/chats/%s/messages" % chat_id
body_hash = _canonical_body_hash(payload)
# Атомарная операция: INSERT ключа + INSERT сообщения + UPDATE ключа
# в ОДНОЙ транзакции. Сбой между ними невозможен: либо всё, либо ничего.
state, message, info = storage.add_message_idempotent(
chat_id=chat_id,
sender_id=user.id,
text=text,
attachment_ids=attachment_ids,
user_id=user.id,
method=method,
path=path,
key=idempotency_key,
body_hash=body_hash,
)
if state == "created":
return message
if state == "retry":
raise HTTPException(status_code=503, detail="unavailable")
# state == "replay"
if info["body_hash"] != body_hash:
raise HTTPException(status_code=409, detail="conflict")
if info["response_status"] == 0:
# Строка от старой версии кода: не зависаем, отдаём 503.
raise HTTPException(status_code=503, detail="unavailable")
return Message.model_validate(info["response_body"])
@router.get(
path="/{chat_id}/messages",
summary="Получить историю сообщений чата",
response_model=MessagePage,
)
def list_messages(
chat_id: UUID,
user: User = Depends(current_user),
limit: int = Query(50, ge=1, le=100),
cursor: str | None = None,
) -> MessagePage:
"""Keyset-пагинация в SQL: (created_at, id) по возрастанию"""
_require_member(chat_id, user.id)
after = _decode_cursor(chat_id, cursor) if cursor else None
rows = storage.list_messages_page(chat_id, limit + 1, after=after)
has_more = len(rows) > limit
items = rows[:limit]
next_cursor = _encode_cursor(chat_id, items[-1]) if has_more and items else None
return MessagePage(items=items, next_cursor=next_cursor)
+24
View File
@@ -0,0 +1,24 @@
from uuid import UUID
from fastapi import HTTPException, Security
from fastapi.security import HTTPAuthorizationCredentials, HTTPBearer
from ..models.schemas import User
from ..storage.postgres import storage
bearer_scheme = HTTPBearer(auto_error=False, scheme_name="bearerAuth")
def current_user(
credentials: HTTPAuthorizationCredentials | None = Security(bearer_scheme),
) -> User:
"""Достаёт Bearer-токен и возвращает владельца сессии.
Basic на бизнес-endpoint не принимается, X-User-Id игнорируется.
"""
if credentials is None or credentials.scheme.lower() != "bearer":
raise HTTPException(status_code=401, detail="unauthorized")
user = storage.get_session_user(credentials.credentials)
if user is None:
raise HTTPException(status_code=401, detail="unauthorized")
return user
+31
View File
@@ -0,0 +1,31 @@
from fastapi import APIRouter, Depends, HTTPException
from ..models.schemas import CreateUser, User
from ..storage.postgres import storage
from .security import current_user
router = APIRouter(prefix="/api/v1", tags=["users"])
@router.post(
path="/users",
summary="Создать пользователя",
response_model=User,
status_code=201,
)
def register_user(payload: CreateUser) -> User:
"""Публичная регистрация. Пробельные display_name/password отсекаются
валидаторами схемы; сюда попадают только валидные значения."""
try:
return storage.create_user(payload.username, payload.display_name, payload.password)
except ValueError:
raise HTTPException(status_code=409, detail="conflict")
@router.get(
path="/users/me",
summary="Получить текущего пользователя",
response_model=User,
)
def current_user_me(user: User = Depends(current_user)) -> User:
"""Текущий пользователь по Bearer-сессии."""
return user