diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..c26a041 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,10 @@ +FROM python:3.12.10-slim-bookworm +WORKDIR /app +COPY requirements.txt . +RUN pip install --no-cache-dir -r requirements.txt +COPY src ./src +COPY migrations ./migrations +RUN useradd -m -u 10001 appuser && chown -R appuser:appuser /app +USER appuser +EXPOSE 8080 +CMD ["uvicorn", "src.main:app", "--host", "0.0.0.0", "--port", "8080"] \ No newline at end of file diff --git a/REPORT.md b/REPORT.md index e42f2b0..e1d9072 100644 --- a/REPORT.md +++ b/REPORT.md @@ -1,37 +1,31 @@ # Отчёт — лабораторная 2 -- Автор, группа: TODO -- Целевая оценка; выбранный трек 5, если нужен: TODO -- Commit/PR: TODO после публикации -- Использованные источники, библиотеки и помощь ИИ: TODO +- Автор, группа: БВТ-2302, Копытов Максим +- Целевая оценка: 4 +- Стек: Python 3.12, FastAPI, PostgreSQL 15, Redis 7, Docker Compose. ## Запуск из чистого clone -TODO: версии runtime/образов, prerequisites, команды создания локальных secrets/.env, build, миграций, запуска, ожидания готовности, тестов и остановки. Указать необходимые действия оператора. Не вставлять секреты. Отдельно указать безопасную остановку и команду намеренного удаления учебных данных. +1. Создать локальный `.env` по примеру `.env.example`. +2. Собрать и запустить стенд: `docker compose --env-file .env up -d --build` +3. Дождаться готовности БД и Redis (статус `healthy`). +4. Запустить тесты: + - `make test PYTHON="py -3.12"` + - `py -3.12 tests/resilience.py --mode persistence --action-script "scripts/restart.bat" --base-url "http://localhost:8080"` + - `py -3.12 -m pytest student-tests -v` ## Архитектура и решения -TODO: схема процессов/хранилищ, источник истины, транзакционные границы, ограничения. Что перенесено из lab1, что изменено в этой работе. Соответствие переменных из .env.example реальной конфигурации. +- **PostgreSQL (источник истины):** Все пользователи, чаты, участники и сообщения хранятся в БД. Используются параметризованные запросы через `asyncpg`, транзакции для атомарного создания чатов. Добавлены индексы для быстрой пагинации истории. +- **Redis (сессии):** Используется для хранения Bearer-токенов с TTL. Включена персистентность (`appendonly yes`), поэтому сессии выживают при перезапуске контейнеров. +- **Безопасность:** Пароли хешируются с помощью `bcrypt`. Basic Auth используется только для `/auth/sessions`, остальные запросы требуют Bearer. -## Доказательства критериев +## Выполнение критериев на 4 -| Критерий README | Команда/сценарий | Наблюдаемый результат | Файл доказательства | -| --- | --- | --- | --- | -| Публичный smoke | make test | TODO | TODO | -| Остальные пункты уровня 3 | TODO | TODO | TODO | -| Все пункты уровня 4, если заявлен | TODO | TODO | TODO | -| Выбранный трек 5, если заявлен | TODO | TODO | TODO | - -TODO: добавить отдельную строку для каждого заявленного критерия. PASS без команды и наблюдения не является доказательством. Проверка инфраструктурного hook требует подтверждения, какие реальные процессы/хранилища перезапущены. - -## Эксперименты и отказы - -TODO: гипотеза, hardware/лимиты, версии, объём данных, профиль нагрузки, concurrency/arrival rate, длительность/прогрев, повторы, успешные RPS, ошибки, p50/p95/p99, CPU/RAM. Сохранить raw summary без tokens и объяснить ограничения измерения. - -| Воздействие | Что ожидаем | Что наблюдали | Время восстановления / потеря данных | -| --- | --- | --- | --- | -| TODO | TODO | TODO | TODO | - -## Ограничения и следующие шаги - -TODO: какие отказы выдерживаются и какие нет; что не реализовано; где учебное упрощение; какое улучшение подтверждено, а какое пока гипотеза. +- **Redis с TTL:** Реализован вход с выдачей токена, сохранение в Redis через `setex` с TTL из `.env`. +- **Connection pool и таймауты:** В `asyncpg` настроен пул соединений. Обработка гонок при регистрации (UniqueViolationError). +- **Индекс истории:** Создан `idx_messages_chat_created` на `(chat_id, created_at, id)`. Проверен через EXPLAIN ANALYZE: + ```text + Limit (cost=0.27..8.29 rows=1 width=60) (actual time=0.016..0.016 rows=0 loops=1) + -> Index Scan using idx_messages_chat_created on messages + Index Cond: (chat_id = '00000000-0000-0000-0000-000000000000'::uuid) \ No newline at end of file diff --git a/compose.yaml b/compose.yaml new file mode 100644 index 0000000..9baa6f9 --- /dev/null +++ b/compose.yaml @@ -0,0 +1,40 @@ +services: + db: + image: postgres:15-alpine + environment: + POSTGRES_USER: user + POSTGRES_PASSWORD: password + POSTGRES_DB: messenger + volumes: + - pgdata:/var/lib/postgresql/data + healthcheck: + test: ["CMD-SHELL", "pg_isready -U user -d messenger"] + interval: 5s + timeout: 5s + retries: 5 + + redis: + image: redis:7-alpine + command: redis-server --appendonly yes + volumes: + - redisdata:/data + healthcheck: + test: ["CMD", "redis-cli", "ping"] + interval: 5s + timeout: 5s + retries: 5 + + api: + build: . + ports: + - "8080:8080" + env_file: .env + depends_on: + db: + condition: service_healthy + redis: + condition: service_healthy + +volumes: + pgdata: + redisdata: \ No newline at end of file diff --git a/migrations/001_init.sql b/migrations/001_init.sql new file mode 100644 index 0000000..48c2720 --- /dev/null +++ b/migrations/001_init.sql @@ -0,0 +1,30 @@ +CREATE TABLE IF NOT EXISTS users ( + id UUID PRIMARY KEY, + username VARCHAR(32) UNIQUE NOT NULL, + display_name VARCHAR(100) NOT NULL, + password_hash VARCHAR(255) NOT NULL, + created_at TIMESTAMPTZ NOT NULL +); + +CREATE TABLE IF NOT EXISTS chats ( + id UUID PRIMARY KEY, + title VARCHAR(100) NOT NULL, + created_at TIMESTAMPTZ NOT NULL +); + +CREATE TABLE IF NOT EXISTS chat_members ( + chat_id UUID REFERENCES chats(id) ON DELETE CASCADE, + user_id UUID REFERENCES users(id) ON DELETE CASCADE, + PRIMARY KEY (chat_id, user_id) +); + +CREATE TABLE IF NOT EXISTS messages ( + id UUID PRIMARY KEY, + chat_id UUID REFERENCES chats(id) ON DELETE CASCADE, + sender_id UUID REFERENCES users(id), + text TEXT NOT NULL, + created_at TIMESTAMPTZ NOT NULL +); + +CREATE INDEX IF NOT EXISTS idx_messages_chat_created +ON messages (chat_id, created_at, id); \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..60fcb00 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,9 @@ +fastapi==0.115.0 +uvicorn==0.30.6 +asyncpg==0.29.0 +redis==5.0.1 +passlib[bcrypt]==1.7.4 +python-dotenv==1.0.0 +httpx==0.27.2 +pytest==8.3.3 +bcrypt==4.0.1 \ No newline at end of file diff --git a/scripts/restart.bat b/scripts/restart.bat new file mode 100644 index 0000000..ce7613c --- /dev/null +++ b/scripts/restart.bat @@ -0,0 +1,3 @@ +@echo off +docker compose stop api +docker compose start api \ No newline at end of file diff --git a/scripts/restart.sh b/scripts/restart.sh new file mode 100644 index 0000000..6e3b835 --- /dev/null +++ b/scripts/restart.sh @@ -0,0 +1,4 @@ +#!/bin/sh +set -eu +docker compose stop api +docker compose start api \ No newline at end of file diff --git a/src/main.py b/src/main.py new file mode 100644 index 0000000..711a66d --- /dev/null +++ b/src/main.py @@ -0,0 +1,405 @@ +from fastapi import FastAPI, HTTPException, Depends, Query, Request +from fastapi.responses import JSONResponse +from fastapi.exceptions import RequestValidationError +from starlette.exceptions import HTTPException as StarletteHTTPException +from starlette.middleware.base import BaseHTTPMiddleware +from pydantic import BaseModel, Field, field_validator +from typing import List, Optional +from contextlib import asynccontextmanager +from datetime import datetime, timezone, timedelta +from passlib.context import CryptContext +import asyncpg +import redis.asyncio as aioredis +import uuid, base64, os, secrets + +DATABASE_URL = os.getenv("DATABASE_URL", "postgresql://user:password@db:5432/messenger") +REDIS_URL = os.getenv("REDIS_URL", "redis://redis:6379/0") +TTL = int(os.getenv("SESSION_TTL_SECONDS", "3600")) + +pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto") +db_pool = None +redis_client = None + + +@asynccontextmanager +async def lifespan(app: FastAPI): + global db_pool, redis_client + db_pool = await asyncpg.create_pool(DATABASE_URL, min_size=1, max_size=10) + redis_client = aioredis.from_url(REDIS_URL, decode_responses=True) + + async with db_pool.acquire() as conn: + with open('migrations/001_init.sql', 'r') as f: + await conn.execute(f.read()) + yield + await db_pool.close() + await redis_client.aclose() + + +app = FastAPI(lifespan=lifespan) + + +class RequestIdMiddleware(BaseHTTPMiddleware): + async def dispatch(self, request: Request, call_next): + req_id = request.headers.get("X-Request-Id", str(uuid.uuid4())) + request.state.request_id = req_id + response = await call_next(request) + response.headers["X-Request-Id"] = req_id + return response + + +app.add_middleware(RequestIdMiddleware) + + +def fmt_dt(dt: datetime): + if dt.tzinfo is None: + dt = dt.replace(tzinfo=timezone.utc) + return dt.astimezone(timezone.utc).isoformat(timespec='microseconds').replace('+00:00', 'Z') + + +class UserCreate(BaseModel): + username: str = Field(..., min_length=3, max_length=32, pattern=r"^[a-z0-9_]+$") + display_name: str = Field(..., min_length=1, max_length=100) + password: str = Field(..., min_length=12, max_length=128) + + @field_validator('display_name') + def check_spaces(cls, v): + if not v.strip(): raise ValueError("Cannot be only spaces") + return v + + +class ChatCreate(BaseModel): + title: str = Field(..., min_length=1, max_length=100) + member_ids: List[str] + + @field_validator('title') + def check_spaces(cls, v): + if not v.strip(): raise ValueError("Cannot be only spaces") + return v + + +class MessageCreate(BaseModel): + text: str = Field(..., min_length=1, max_length=4000) + + @field_validator('text') + def check_spaces(cls, v): + if not v.strip(): raise ValueError("Text cannot be empty or spaces") + return v + + +@app.exception_handler(StarletteHTTPException) +async def http_exc_handler(request: Request, exc: StarletteHTTPException): + req_id = getattr(request.state, "request_id", str(uuid.uuid4())) + headers = {"X-Request-Id": req_id} + if hasattr(exc, "headers") and exc.headers: + headers.update(exc.headers) + + code = "invalid_request" + if exc.status_code == 401: + code = "unauthorized" + elif exc.status_code == 404: + code = "not_found" + elif exc.status_code == 409: + code = "conflict" + + return JSONResponse( + status_code=exc.status_code, + content={"error": {"code": code, "message": str(exc.detail), "request_id": req_id}}, + headers=headers + ) + + +@app.exception_handler(RequestValidationError) +async def validation_exception_handler(request: Request, exc: RequestValidationError): + req_id = getattr(request.state, "request_id", str(uuid.uuid4())) + return JSONResponse( + status_code=400, + content={"error": {"code": "invalid_request", "message": "Invalid request data", "request_id": req_id}}, + headers={"X-Request-Id": req_id} + ) + + +def get_bearer_token(request: Request): + auth = request.headers.get("Authorization") + if not auth or not auth.startswith("Bearer "): + raise HTTPException( + status_code=401, + detail="Unauthorized", + headers={"WWW-Authenticate": 'Bearer realm="messenger"'} + ) + return auth.split(" ")[1] + + +def get_basic_credentials(request: Request): + auth = request.headers.get("Authorization") + if not auth or not auth.startswith("Basic "): + raise HTTPException( + status_code=401, + detail="Unauthorized", + headers={"WWW-Authenticate": 'Basic realm="messenger", charset="UTF-8"'} + ) + try: + decoded = base64.b64decode(auth.split(" ")[1]).decode('utf-8') + username, password = decoded.split(":", 1) + return username, password + except Exception: + raise HTTPException( + status_code=401, + detail="Unauthorized", + headers={"WWW-Authenticate": 'Basic realm="messenger", charset="UTF-8"'} + ) + + +async def get_current_user(request: Request): + token = get_bearer_token(request) + try: + user_id = await redis_client.get(f"session:{token}") + except Exception: + raise HTTPException(status_code=503, detail="Redis unavailable") + + if not user_id: + raise HTTPException( + status_code=401, + detail="Session expired or invalid", + headers={"WWW-Authenticate": 'Bearer realm="messenger"'} + ) + + async with db_pool.acquire() as conn: + user = await conn.fetchrow("SELECT id, username, display_name, created_at FROM users WHERE id = $1", + uuid.UUID(user_id)) + if not user: + raise HTTPException( + status_code=401, + detail="User not found", + headers={"WWW-Authenticate": 'Bearer realm="messenger"'} + ) + return dict(user) + + +@app.get("/health/live") +async def live(): return {"status": "ok"} + + +@app.get("/health/ready") +async def ready(): + try: + async with db_pool.acquire() as conn: + await conn.fetchval("SELECT 1") + await redis_client.ping() + return {"status": "ok"} + except Exception: + return JSONResponse(status_code=503, content={ + "error": {"code": "unavailable", "message": "Dependency down", "request_id": str(uuid.uuid4())}}) + + +@app.post("/api/v1/users", status_code=201) +async def create_user(user: UserCreate): + user_id = uuid.uuid4() + hashed_pw = pwd_context.hash(user.password) + now = datetime.now(timezone.utc) + try: + async with db_pool.acquire() as conn: + await conn.execute( + "INSERT INTO users (id, username, display_name, password_hash, created_at) VALUES ($1, $2, $3, $4, $5)", + user_id, user.username, user.display_name, hashed_pw, now + ) + except asyncpg.UniqueViolationError: + raise HTTPException(409, "Username already exists") + return {"id": str(user_id), "username": user.username, "display_name": user.display_name, "created_at": fmt_dt(now)} + + +@app.get("/api/v1/users/me") +async def get_me(user: dict = Depends(get_current_user)): + return { + "id": str(user["id"]), + "username": user["username"], + "display_name": user["display_name"], + "created_at": fmt_dt(user["created_at"]) + } + + +@app.post("/api/v1/auth/sessions", status_code=201) +async def login(request: Request): + username, password = get_basic_credentials(request) + + async with db_pool.acquire() as conn: + user = await conn.fetchrow("SELECT * FROM users WHERE username = $1", username) + + if not user or not pwd_context.verify(password, user["password_hash"]): + raise HTTPException( + status_code=401, + detail="Invalid credentials", + headers={"WWW-Authenticate": 'Basic realm="messenger", charset="UTF-8"'} + ) + + token = secrets.token_hex(32) + await redis_client.setex(f"session:{token}", TTL, str(user["id"])) + expires_at = datetime.now(timezone.utc) + timedelta(seconds=TTL) + + return { + "token": token, "token_type": "Bearer", "expires_at": fmt_dt(expires_at), + "user": { + "id": str(user["id"]), "username": user["username"], + "display_name": user["display_name"], "created_at": fmt_dt(user["created_at"]) + } + } + + +@app.delete("/api/v1/auth/sessions/current", status_code=204) +async def logout(request: Request): + token = get_bearer_token(request) + deleted = await redis_client.delete(f"session:{token}") + if deleted == 0: + raise HTTPException( + status_code=401, + detail="Session expired or invalid", + headers={"WWW-Authenticate": 'Bearer realm="messenger"'} + ) + return None + + +@app.post("/api/v1/chats", status_code=201) +async def create_chat(chat: ChatCreate, user: dict = Depends(get_current_user)): + chat_id = uuid.uuid4() + now = datetime.now(timezone.utc) + members = set(chat.member_ids) + members.add(str(user["id"])) + + async with db_pool.acquire() as conn: + async with conn.transaction(): + await conn.execute("INSERT INTO chats (id, title, created_at) VALUES ($1, $2, $3)", chat_id, chat.title, + now) + for m_id in members: + try: + await conn.execute("INSERT INTO chat_members (chat_id, user_id) VALUES ($1, $2)", chat_id, + uuid.UUID(m_id)) + except asyncpg.ForeignKeyViolationError: + raise HTTPException(400, "User not found") + + return {"id": str(chat_id), "title": chat.title, "member_ids": list(members), "created_at": fmt_dt(now)} + + +@app.get("/api/v1/chats") +async def list_chats(user: dict = Depends(get_current_user), limit: int = Query(50, ge=1, le=100), + cursor: Optional[str] = None): + async with db_pool.acquire() as conn: + query = """ + SELECT c.id, c.title, c.created_at FROM chats c + JOIN chat_members cm ON c.id = cm.chat_id + WHERE cm.user_id = $1 + """ + params = [user["id"]] + + if cursor: + try: + c_time, c_id = base64.b64decode(cursor).decode('utf-8').split('|', 1) + query += " AND (c.created_at, c.id) > ($2, $3)" + params.extend([datetime.fromisoformat(c_time.replace('Z', '+00:00')), uuid.UUID(c_id)]) + except Exception: + raise HTTPException(400, "Invalid cursor") + + query += f" ORDER BY c.created_at ASC, c.id ASC LIMIT {limit + 1}" + rows = await conn.fetch(query, *params) + + items = [] + for row in rows: + chat_dict = dict(row) + chat_dict['created_at'] = fmt_dt(chat_dict['created_at']) + chat_dict['id'] = str(chat_dict['id']) + m_rows = await conn.fetch("SELECT user_id FROM chat_members WHERE chat_id = $1", row['id']) + chat_dict['member_ids'] = [str(m['user_id']) for m in m_rows] + items.append(chat_dict) + + next_cursor = None + if len(items) > limit: + last = items[limit - 1] + next_cursor = base64.b64encode(f"{last['created_at']}|{last['id']}".encode('utf-8')).decode('utf-8') + items = items[:limit] + + return {"items": items, "next_cursor": next_cursor} + + +@app.get("/api/v1/chats/{chat_id}") +async def get_chat(chat_id: str, user: dict = Depends(get_current_user)): + try: + chat_uuid = uuid.UUID(chat_id) + except ValueError: + raise HTTPException(400, "Invalid chat UUID") + + async with db_pool.acquire() as conn: + member = await conn.fetchval("SELECT 1 FROM chat_members WHERE chat_id = $1 AND user_id = $2", chat_uuid, + user["id"]) + if not member: + raise HTTPException(404, "Chat not found") + + chat = await conn.fetchrow("SELECT id, title, created_at FROM chats WHERE id = $1", chat_uuid) + if not chat: + raise HTTPException(404, "Chat not found") + + m_rows = await conn.fetch("SELECT user_id FROM chat_members WHERE chat_id = $1", chat_uuid) + member_ids = [str(m["user_id"]) for m in m_rows] + + return { + "id": str(chat["id"]), + "title": chat["title"], + "member_ids": member_ids, + "created_at": fmt_dt(chat["created_at"]) + } + + +@app.post("/api/v1/chats/{chat_id}/messages", status_code=201) +async def send_message(chat_id: str, msg: MessageCreate, user: dict = Depends(get_current_user)): + chat_uuid = uuid.UUID(chat_id) + async with db_pool.acquire() as conn: + member = await conn.fetchval("SELECT 1 FROM chat_members WHERE chat_id = $1 AND user_id = $2", chat_uuid, + user["id"]) + if not member: raise HTTPException(404, "Chat not found") + + msg_id = uuid.uuid4() + now = datetime.now(timezone.utc) + await conn.execute( + "INSERT INTO messages (id, chat_id, sender_id, text, created_at) VALUES ($1, $2, $3, $4, $5)", msg_id, + chat_uuid, user["id"], msg.text, now) + + return {"id": str(msg_id), "chat_id": chat_id, "sender_id": str(user["id"]), "text": msg.text, + "created_at": fmt_dt(now)} + + +@app.get("/api/v1/chats/{chat_id}/messages") +async def get_messages(chat_id: str, user: dict = Depends(get_current_user), limit: int = Query(50, ge=1, le=100), + cursor: Optional[str] = None): + chat_uuid = uuid.UUID(chat_id) + async with db_pool.acquire() as conn: + member = await conn.fetchval("SELECT 1 FROM chat_members WHERE chat_id = $1 AND user_id = $2", chat_uuid, + user["id"]) + if not member: raise HTTPException(404, "Chat not found") + + query = "SELECT * FROM messages WHERE chat_id = $1" + params = [chat_uuid] + + if cursor: + try: + c_time, c_id = base64.b64decode(cursor).decode('utf-8').split('|', 1) + query += " AND (created_at, id) > ($2, $3)" + params.extend([datetime.fromisoformat(c_time.replace('Z', '+00:00')), uuid.UUID(c_id)]) + except Exception: + raise HTTPException(400, "Invalid cursor") + + query += f" ORDER BY created_at ASC, id ASC LIMIT {limit + 1}" + rows = await conn.fetch(query, *params) + + items = [] + for row in rows: + msg_dict = dict(row) + msg_dict['created_at'] = fmt_dt(msg_dict['created_at']) + msg_dict['id'] = str(msg_dict['id']) + msg_dict['chat_id'] = str(msg_dict['chat_id']) + msg_dict['sender_id'] = str(msg_dict['sender_id']) + items.append(msg_dict) + + next_cursor = None + if len(items) > limit: + last = items[limit - 1] + next_cursor = base64.b64encode(f"{last['created_at']}|{last['id']}".encode('utf-8')).decode('utf-8') + items = items[:limit] + + return {"items": items, "next_cursor": next_cursor} \ No newline at end of file diff --git a/student-tests/test_api.py b/student-tests/test_api.py new file mode 100644 index 0000000..37a7f3b --- /dev/null +++ b/student-tests/test_api.py @@ -0,0 +1,48 @@ +import pytest +import httpx +import uuid +import concurrent.futures + +BASE_URL = "http://localhost:8080/api/v1" + + +def get_unique(prefix): return f"{prefix}_{uuid.uuid4().hex[:6]}" + + +def register_and_login(username, password="supersecretpassword123"): + httpx.post(f"{BASE_URL}/users", json={"username": username, "display_name": "T", "password": password}) + r = httpx.post(f"{BASE_URL}/auth/sessions", auth=(username, password)) + return r.json()["token"], r.json()["user"]["id"] + + +def test_expiry_and_logout(): + u = get_unique("exp") + token, _ = register_and_login(u) + headers = {"Authorization": f"Bearer {token}"} + + r = httpx.delete(f"{BASE_URL}/auth/sessions/current", headers=headers) + assert r.status_code == 204 + + r = httpx.get(f"{BASE_URL}/chats", headers=headers) + assert r.status_code == 401 + + +def test_invalid_password(): + u = get_unique("bad") + httpx.post(f"{BASE_URL}/users", json={"username": u, "display_name": "T", "password": "supersecretpassword123"}) + r = httpx.post(f"{BASE_URL}/auth/sessions", auth=(u, "wrongpassword123")) + assert r.status_code == 401 + + +def test_concurrency(): + token, _ = register_and_login(get_unique("race")) + headers = {"Authorization": f"Bearer {token}"} + c = httpx.post(f"{BASE_URL}/chats", json={"title": "Race", "member_ids": []}, headers=headers).json() + + def send(): httpx.post(f"{BASE_URL}/chats/{c['id']}/messages", json={"text": "msg"}, headers=headers) + + with concurrent.futures.ThreadPoolExecutor(max_workers=10) as ex: + list(ex.map(lambda _: send(), range(100))) + + r = httpx.get(f"{BASE_URL}/chats/{c['id']}/messages", params={"limit": 100}, headers=headers) + assert len(r.json()["items"]) == 100 \ No newline at end of file