Compare commits

1 Commits
Author SHA1 Message Date
dadaykin 917d2cf725 Lab 2 for 4:
Skeleton contracts / contracts (pull_request) Canceled after 0s
2026-10-26 14:27:25 +03:00
19 changed files with 1186 additions and 34 deletions
+21
View File
@@ -0,0 +1,21 @@
FROM python:3.12-slim
RUN useradd -m -u 1000 appuser
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
# Копируем нашу новую папку src
COPY src/ ./src/
USER appuser
EXPOSE 8080
HEALTHCHECK --interval=5s --timeout=3s --start-period=3s --retries=3 \
CMD python -c "import urllib.request; urllib.request.urlopen('http://127.0.0.1:8080/health/ready')" || exit 1
# Запускаем код из папки src
CMD ["uvicorn", "src.main:app", "--host", "0.0.0.0", "--port", "8080", "--timeout-graceful-shutdown", "10"]
+45 -14
View File
@@ -1,37 +1,68 @@
# Отчёт — лабораторная 2
- Автор, группа: TODO
- Целевая оценка; выбранный трек 5, если нужен: TODO
- Автор, группа: Дадайкин БВТ2302
- Целевая оценка 4
- Commit/PR: TODO после публикации
- Использованные источники, библиотеки и помощь ИИ: TODO
- Использованные источники, библиотеки и помощь ИИ: Официальная документация FastAPI, SQLAlchemy, Redis; ИИ (помощь с написанием и профилированием SQL индексов).
## Запуск из чистого clone
TODO: версии runtime/образов, prerequisites, команды создания локальных secrets/.env, build, миграций, запуска, ожидания готовности, тестов и остановки. Указать необходимые действия оператора. Не вставлять секреты. Отдельно указать безопасную остановку и команду намеренного удаления учебных данных.
**Требования:** Docker, Docker Compose, Python 3.9+ (для запуска проверочных скриптов).
**Команды запуска:**
```bash
# 1. Сборка и запуск инфраструктуры (API, PostgreSQL, Redis)
docker compose up -d --build
# 2. Применение миграций базы данных
alembic upgrade head
Остановка и полное удаление учебных данных:
docker compose down -v
```
## Архитектура и решения
TODO: схема процессов/хранилищ, источник истины, транзакционные границы, ограничения. Что перенесено из lab1, что изменено в этой работе. Соответствие переменных из .env.example реальной конфигурации.
Источник истины: PostgreSQL 15. Хранит пользователей, чаты, участников чатов и сообщения. Для пагинации сообщений добавлен составной индекс ix_messages_chat_id_created_at_id.
Сессии и кэш: Redis 7. Используется для хранения пользовательских сессий. Настроен TTL сессии из SESSION_TTL_SECONDS. В compose.yaml включена опция --appendonly yes, чтобы сессии сохранялись на диск и переживали рестарт контейнера.
Транзакционные границы: Составные операции (создание чата с участниками) выполняются внутри транзакций с автоматическим откатом при ошибке.
Гонки (Race Conditions): Исключена гонка при регистрации пользователей с одинаковым username путём перехвата IntegrityError на уровне СУБД (вместо предварительного SELECT).
Соединения: В src/database.py настроен пул соединений (pool_size=10, max_overflow=20) и таймауты (connect_args={"command_timeout": 5.0}).
## Доказательства критериев
| Критерий README | Команда/сценарий | Наблюдаемый результат | Файл доказательства |
| --- | --- | --- | --- |
| Публичный smoke | make test | TODO | TODO |
| Остальные пункты уровня 3 | TODO | TODO | TODO |
| Все пункты уровня 4, если заявлен | TODO | TODO | TODO |
| Выбранный трек 5, если заявлен | TODO | TODO | TODO |
| Публичный smoke | python tests/smoke.py | PASS: lab2 public API scenario | Вывод в консоли |
| Сохранность при рестарте (Ур. 3) | python tests/resilience.py --mode persistence --action-script scripts/restart.bat | PASS: persistence preserved confirmed data and existing session. API, Postgres и Redis успешно восстановились. Токен валиден. | Вывод в консоли |
| Redis для сессий (Ур. 4) | Ручная проверка / код | Токены сохраняются в Redis. При рестарте контейнера не пропадают (AOF включен). | compose.yaml, src/main.py |
| Безопасный ORM, гонки (Ур. 4) | python tests/custom_tests.py | 100 сообщений от 10 воркеров успешно доставляются. Дубликаты username отлавливаются на уровне БД -> 409 Conflict. | Вывод в консоли |
TODO: добавить отдельную строку для каждого заявленного критерия. PASS без команды и наблюдения не является доказательством. Проверка инфраструктурного hook требует подтверждения, какие реальные процессы/хранилища перезапущены.
Доказательство работы составного индекса (на 10 000 сообщениях):
```
Limit (cost=0.29..5.08 rows=50 width=86) (actual time=0.045..0.058 rows=50 loops=1)
Buffers: shared hit=1 read=2
-> Index Scan using ix_messages_chat_id_created_at_id on messages (cost=0.29..958.53 rows=10000 width=86) (actual time=0.044..0.052 rows=50 loops=1)
Index Cond: ((chat_id)::text = 'chat-test-idx'::text)
Buffers: shared hit=1 read=2
Planning:
Buffers: shared hit=161 read=1 dirtied=5
Planning Time: 1.540 ms
Execution Time: 0.098 ms
```
## Эксперименты и отказы
TODO: гипотеза, hardware/лимиты, версии, объём данных, профиль нагрузки, concurrency/arrival rate, длительность/прогрев, повторы, успешные RPS, ошибки, p50/p95/p99, CPU/RAM. Сохранить raw summary без tokens и объяснить ограничения измерения.
| Воздействие | Что ожидаем | Что наблюдали | Время восстановления / потеря данных |
| --- | --- | --- | --- |
| TODO | TODO | TODO | TODO |
| Остановка Redis (docker stop <container>) | Ошибка авторизации (503) для защищенных путей | При обращении к /health/ready или /api/v1/users/me выдается 503. | Потерь данных нет. Восстановление мгновенное после включения контейнера. |
| Рестарт всего стенда (restart.bat) | Токен продолжает действовать, сообщения сохранены | Токен сессии остался валидным (прочитан с диска Redis). Сообщения в сохранности (Postgres Volume). | Простой около 3-4 секунд. |
## Ограничения и следующие шаги
TODO: какие отказы выдерживаются и какие нет; что не реализовано; где учебное упрощение; какое улучшение подтверждено, а какое пока гипотеза.
Стенд не устойчив к падению хост-машины (нет репликации БД и Redis на другие физические узлы/серверы).
Архитектура не поддерживает инвалидацию сессий на других устройствах при смене пароля.
В будущем для масштабирования потребуется балансировщик нагрузки (LB) и запуск нескольких инстансов API.
+149
View File
@@ -0,0 +1,149 @@
# A generic, single database configuration.
[alembic]
# path to migration scripts.
# this is typically a path given in POSIX (e.g. forward slashes)
# format, relative to the token %(here)s which refers to the location of this
# ini file
script_location = %(here)s/alembic
# template used to generate migration file names; The default value is %%(rev)s_%%(slug)s
# Uncomment the line below if you want the files to be prepended with date and time
# see https://alembic.sqlalchemy.org/en/latest/tutorial.html#editing-the-ini-file
# for all available tokens
# file_template = %%(year)d_%%(month).2d_%%(day).2d_%%(hour).2d%%(minute).2d-%%(rev)s_%%(slug)s
# Or organize into date-based subdirectories (requires recursive_version_locations = true)
# file_template = %%(year)d/%%(month).2d/%%(day).2d_%%(hour).2d%%(minute).2d_%%(second).2d_%%(rev)s_%%(slug)s
# sys.path path, will be prepended to sys.path if present.
# defaults to the current working directory. for multiple paths, the path separator
# is defined by "path_separator" below.
prepend_sys_path = .
# timezone to use when rendering the date within the migration file
# as well as the filename.
# If specified, requires the tzdata library which can be installed by adding
# `alembic[tz]` to the pip requirements.
# string value is passed to ZoneInfo()
# leave blank for localtime
# timezone =
# max length of characters to apply to the "slug" field
# truncate_slug_length = 40
# set to 'true' to run the environment during
# the 'revision' command, regardless of autogenerate
# revision_environment = false
# set to 'true' to allow .pyc and .pyo files without
# a source .py file to be detected as revisions in the
# versions/ directory
# sourceless = false
# version location specification; This defaults
# to <script_location>/versions. When using multiple version
# directories, initial revisions must be specified with --version-path.
# The path separator used here should be the separator specified by "path_separator"
# below.
# version_locations = %(here)s/bar:%(here)s/bat:%(here)s/alembic/versions
# path_separator; This indicates what character is used to split lists of file
# paths, including version_locations and prepend_sys_path within configparser
# files such as alembic.ini.
# The default rendered in new alembic.ini files is "os", which uses os.pathsep
# to provide os-dependent path splitting.
#
# Note that in order to support legacy alembic.ini files, this default does NOT
# take place if path_separator is not present in alembic.ini. If this
# option is omitted entirely, fallback logic is as follows:
#
# 1. Parsing of the version_locations option falls back to using the legacy
# "version_path_separator" key, which if absent then falls back to the legacy
# behavior of splitting on spaces and/or commas.
# 2. Parsing of the prepend_sys_path option falls back to the legacy
# behavior of splitting on spaces, commas, or colons.
#
# Valid values for path_separator are:
#
# path_separator = :
# path_separator = ;
# path_separator = space
# path_separator = newline
#
# Use os.pathsep. Default configuration used for new projects.
path_separator = os
# set to 'true' to search source files recursively
# in each "version_locations" directory
# new in Alembic version 1.10
# recursive_version_locations = false
# the output encoding used when revision files
# are written from script.py.mako
# output_encoding = utf-8
# database URL. This is consumed by the user-maintained env.py script only.
# other means of configuring database URLs may be customized within the env.py
# file.
sqlalchemy.url = postgresql+asyncpg://mtusi:mtusi_password@localhost:5432/messenger_db
[post_write_hooks]
# post_write_hooks defines scripts or Python functions that are run
# on newly generated revision scripts. See the documentation for further
# detail and examples
# format using "black" - use the console_scripts runner, against the "black" entrypoint
# hooks = black
# black.type = console_scripts
# black.entrypoint = black
# black.options = -l 79 REVISION_SCRIPT_FILENAME
# lint with attempts to fix using "ruff" - use the module runner, against the "ruff" module
# hooks = ruff
# ruff.type = module
# ruff.module = ruff
# ruff.options = check --fix REVISION_SCRIPT_FILENAME
# Alternatively, use the exec runner to execute a binary found on your PATH
# hooks = ruff
# ruff.type = exec
# ruff.executable = ruff
# ruff.options = check --fix REVISION_SCRIPT_FILENAME
# Logging configuration. This is also consumed by the user-maintained
# env.py script only.
[loggers]
keys = root,sqlalchemy,alembic
[handlers]
keys = console
[formatters]
keys = generic
[logger_root]
level = WARNING
handlers = console
qualname =
[logger_sqlalchemy]
level = WARNING
handlers =
qualname = sqlalchemy.engine
[logger_alembic]
level = INFO
handlers =
qualname = alembic
[handler_console]
class = StreamHandler
args = (sys.stderr,)
level = NOTSET
formatter = generic
[formatter_generic]
format = %(levelname)-5.5s [%(name)s] %(message)s
datefmt = %H:%M:%S
+1
View File
@@ -0,0 +1 @@
Generic single-database configuration with an async dbapi.
+95
View File
@@ -0,0 +1,95 @@
import asyncio
from logging.config import fileConfig
import sys
from pathlib import Path
sys.path.append(str(Path(__file__).parent.parent))
from src.models import Base
from sqlalchemy import pool
from sqlalchemy.engine import Connection
from sqlalchemy.ext.asyncio import async_engine_from_config
from alembic import context
# this is the Alembic Config object, which provides
# access to the values within the .ini file in use.
config = context.config
# Interpret the config file for Python logging.
# This line sets up loggers basically.
if config.config_file_name is not None:
fileConfig(config.config_file_name)
# add your model's MetaData object here
# for 'autogenerate' support
# from myapp import mymodel
# target_metadata = mymodel.Base.metadata
target_metadata = Base.metadata
# other values from the config, defined by the needs of env.py,
# can be acquired:
# my_important_option = config.get_main_option("my_important_option")
# ... etc.
def run_migrations_offline() -> None:
"""Run migrations in 'offline' mode.
This configures the context with just a URL
and not an Engine, though an Engine is acceptable
here as well. By skipping the Engine creation
we don't even need a DBAPI to be available.
Calls to context.execute() here emit the given string to the
script output.
"""
url = config.get_main_option("sqlalchemy.url")
context.configure(
url=url,
target_metadata=target_metadata,
literal_binds=True,
dialect_opts={"paramstyle": "named"},
)
with context.begin_transaction():
context.run_migrations()
def do_run_migrations(connection: Connection) -> None:
context.configure(connection=connection, target_metadata=target_metadata)
with context.begin_transaction():
context.run_migrations()
async def run_async_migrations() -> None:
"""In this scenario we need to create an Engine
and associate a connection with the context.
"""
connectable = async_engine_from_config(
config.get_section(config.config_ini_section, {}),
prefix="sqlalchemy.",
poolclass=pool.NullPool,
)
async with connectable.connect() as connection:
await connection.run_sync(do_run_migrations)
await connectable.dispose()
def run_migrations_online() -> None:
"""Run migrations in 'online' mode."""
asyncio.run(run_async_migrations())
if context.is_offline_mode():
run_migrations_offline()
else:
run_migrations_online()
+28
View File
@@ -0,0 +1,28 @@
"""${message}
Revision ID: ${up_revision}
Revises: ${down_revision | comma,n}
Create Date: ${create_date}
"""
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa
${imports if imports else ""}
# revision identifiers, used by Alembic.
revision: str = ${repr(up_revision)}
down_revision: Union[str, Sequence[str], None] = ${repr(down_revision)}
branch_labels: Union[str, Sequence[str], None] = ${repr(branch_labels)}
depends_on: Union[str, Sequence[str], None] = ${repr(depends_on)}
def upgrade() -> None:
"""Upgrade schema."""
${upgrades if upgrades else "pass"}
def downgrade() -> None:
"""Downgrade schema."""
${downgrades if downgrades else "pass"}
@@ -0,0 +1,34 @@
"""fix composite index for pagination
Revision ID: 1e3bba39cb0b
Revises: d091c02268e4
Create Date: 2026-10-26 13:46:04.982720
"""
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision: str = '1e3bba39cb0b'
down_revision: Union[str, Sequence[str], None] = 'd091c02268e4'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None
def upgrade() -> None:
"""Upgrade schema."""
# ### commands auto generated by Alembic - please adjust! ###
op.drop_index(op.f('ix_messages_created_at_id'), table_name='messages')
op.create_index('ix_messages_chat_id_created_at_id', 'messages', ['chat_id', 'created_at', 'id'], unique=False)
# ### end Alembic commands ###
def downgrade() -> None:
"""Downgrade schema."""
# ### commands auto generated by Alembic - please adjust! ###
op.drop_index('ix_messages_chat_id_created_at_id', table_name='messages')
op.create_index(op.f('ix_messages_created_at_id'), 'messages', ['created_at', 'id'], unique=False)
# ### end Alembic commands ###
@@ -0,0 +1,32 @@
"""move sessions to redis and add composite index
Revision ID: d091c02268e4
Revises: f5ff9e89472b
Create Date: 2026-10-25 16:51:50.147051
"""
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision: str = 'd091c02268e4'
down_revision: Union[str, Sequence[str], None] = 'f5ff9e89472b'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None
def upgrade() -> None:
"""Upgrade schema."""
# ### commands auto generated by Alembic - please adjust! ###
op.create_index('ix_messages_created_at_id', 'messages', ['created_at', 'id'], unique=False)
# ### end Alembic commands ###
def downgrade() -> None:
"""Downgrade schema."""
# ### commands auto generated by Alembic - please adjust! ###
op.drop_index('ix_messages_created_at_id', table_name='messages')
# ### end Alembic commands ###
@@ -0,0 +1,77 @@
"""initial tables
Revision ID: f5ff9e89472b
Revises:
Create Date: 2026-10-24 18:58:42.873306
"""
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision: str = 'f5ff9e89472b'
down_revision: Union[str, Sequence[str], None] = None
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None
def upgrade() -> None:
"""Upgrade schema."""
# ### commands auto generated by Alembic - please adjust! ###
op.create_table('chats',
sa.Column('id', sa.String(), nullable=False),
sa.Column('title', sa.String(), nullable=False),
sa.Column('created_at', sa.DateTime(timezone=True), nullable=True),
sa.PrimaryKeyConstraint('id')
)
op.create_table('users',
sa.Column('id', sa.String(), nullable=False),
sa.Column('username', sa.String(), nullable=False),
sa.Column('display_name', sa.String(), nullable=False),
sa.Column('password_hash', sa.String(), nullable=False),
sa.Column('created_at', sa.DateTime(timezone=True), nullable=True),
sa.PrimaryKeyConstraint('id')
)
op.create_index(op.f('ix_users_username'), 'users', ['username'], unique=True)
op.create_table('chat_members',
sa.Column('chat_id', sa.String(), nullable=False),
sa.Column('user_id', sa.String(), nullable=False),
sa.ForeignKeyConstraint(['chat_id'], ['chats.id'], ondelete='CASCADE'),
sa.ForeignKeyConstraint(['user_id'], ['users.id'], ondelete='CASCADE'),
sa.PrimaryKeyConstraint('chat_id', 'user_id')
)
op.create_table('messages',
sa.Column('id', sa.String(), nullable=False),
sa.Column('chat_id', sa.String(), nullable=False),
sa.Column('sender_id', sa.String(), nullable=False),
sa.Column('text', sa.String(), nullable=False),
sa.Column('created_at', sa.DateTime(timezone=True), nullable=True),
sa.ForeignKeyConstraint(['chat_id'], ['chats.id'], ondelete='CASCADE'),
sa.ForeignKeyConstraint(['sender_id'], ['users.id'], ondelete='CASCADE'),
sa.PrimaryKeyConstraint('id')
)
op.create_index(op.f('ix_messages_chat_id'), 'messages', ['chat_id'], unique=False)
op.create_table('sessions',
sa.Column('token', sa.String(), nullable=False),
sa.Column('user_id', sa.String(), nullable=False),
sa.Column('expires_at', sa.DateTime(timezone=True), nullable=False),
sa.ForeignKeyConstraint(['user_id'], ['users.id'], ondelete='CASCADE'),
sa.PrimaryKeyConstraint('token')
)
# ### end Alembic commands ###
def downgrade() -> None:
"""Downgrade schema."""
# ### commands auto generated by Alembic - please adjust! ###
op.drop_table('sessions')
op.drop_index(op.f('ix_messages_chat_id'), table_name='messages')
op.drop_table('messages')
op.drop_table('chat_members')
op.drop_index(op.f('ix_users_username'), table_name='users')
op.drop_table('users')
op.drop_table('chats')
# ### end Alembic commands ###
-10
View File
@@ -1,10 +0,0 @@
# Topology worksheet, deliberately NOT a runnable stack (no application supplied).
# Implement compose.yaml or stack.yaml; do not submit this empty file as deployment.
# Service names below are logical roles, not imposed container names.
x-lab-number: 2
x-required-roles:
- api
- postgres
# TODO: implement real images/builds, network wiring, volumes, healthchecks,
# credentials and bootstrap. Details: contracts/infrastructure.json and README.md.
services: {}
+49
View File
@@ -0,0 +1,49 @@
version: '3.8'
services:
api:
build: .
ports:
- "8080:8080"
environment:
- DATABASE_URL=postgresql+asyncpg://mtusi:mtusi_password@postgres:5432/messenger_db
- REDIS_URL=redis://redis:6379/0
- SESSION_TTL_SECONDS=3600
depends_on:
postgres:
condition: service_healthy
redis:
condition: service_healthy
postgres:
image: postgres:15-alpine
environment:
- POSTGRES_USER=mtusi
- POSTGRES_PASSWORD=mtusi_password
- POSTGRES_DB=messenger_db
ports:
- "5432:5432"
volumes:
- pgdata:/var/lib/postgresql/data
healthcheck:
test: ["CMD-SHELL", "pg_isready -U mtusi -d messenger_db"]
interval: 5s
timeout: 5s
retries: 5
redis:
image: redis:7-alpine
ports:
- "6379:6379"
volumes:
- redisdata:/data
command: ["redis-server", "--appendonly", "yes"] # Важно для сохранения сессий при рестарте
healthcheck:
test: ["CMD", "redis-cli", "ping"]
interval: 5s
timeout: 3s
retries: 5
volumes:
pgdata:
redisdata:
+9
View File
@@ -0,0 +1,9 @@
fastapi>=0.110.0,<1.0.0
uvicorn[standard]>=0.28.0,<1.0.0
pydantic>=2.6.0,<3.0.0
sqlalchemy[asyncio]>=2.0.27
asyncpg>=0.29.0
alembic>=1.13.1
passlib[bcrypt]>=1.7.4
bcrypt<4.0.0
redis>=5.0.0
+4
View File
@@ -0,0 +1,4 @@
@echo off
docker compose stop
docker compose start
timeout /t 3 /nobreak >nul
-10
View File
@@ -1,10 +0,0 @@
#!/bin/sh
set -eu
# TODO: copy to restart.sh and implement using YOUR compose/stack services.
# Restart API and ALL used state stores (DB, sessions, S3, broker as applicable).
# Preserve persistent volumes and schema/data; never down -v / flush / re-seed.
# Allow operator unseal if required in lab7; do not print credentials.
# Exit 0 only after the intended action; test waits for API readiness separately.
printf '%s
' 'TODO: implement restart.sh for your infrastructure' >&2
exit 2
+7
View File
@@ -0,0 +1,7 @@
#!/bin/sh
# Останавливаем контейнеры
docker compose stop
# Запускаем их снова (база данных сохранит данные благодаря volume pgdata)
docker compose start
# Ждем пару секунд, чтобы сервер успел включиться
sleep 3
+31
View File
@@ -0,0 +1,31 @@
import os
import redis.asyncio as redis
from sqlalchemy.ext.asyncio import create_async_engine, async_sessionmaker, AsyncSession
from sqlalchemy.orm import declarative_base
DATABASE_URL = os.getenv(
"DATABASE_URL",
"postgresql+asyncpg://mtusi:mtusi_password@localhost:5432/messenger_db"
)
REDIS_URL = os.getenv("REDIS_URL", "redis://localhost:6379/0")
# Настройка пула и таймаутов (требование на 4)
engine = create_async_engine(
DATABASE_URL,
echo=False,
pool_size=10,
max_overflow=20,
pool_timeout=10.0,
pool_recycle=1800,
connect_args={"command_timeout": 5.0} # Таймаут на выполнение запроса
)
async_session = async_sessionmaker(engine, expire_on_commit=False, class_=AsyncSession)
Base = declarative_base()
# Глобальный клиент Redis
redis_client = redis.from_url(REDIS_URL, decode_responses=True)
async def get_db():
async with async_session() as session:
yield session
+447
View File
@@ -0,0 +1,447 @@
import base64
import datetime as dt
import json
import logging
import os
import secrets
import sys
import uuid
from typing import List, Optional
from fastapi import FastAPI, Depends, Header, Query, Request, Response
from fastapi.exceptions import RequestValidationError
from fastapi.responses import JSONResponse
from pydantic import BaseModel, ConfigDict, Field
from starlette.exceptions import HTTPException as StarletteHTTPException
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy import select, and_, or_
from sqlalchemy.exc import IntegrityError
from passlib.context import CryptContext
from .database import get_db, redis_client
from .models import User, Chat, Message
# Настройка логирования
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s [%(levelname)s] %(message)s",
handlers=[logging.StreamHandler(sys.stdout)],
)
logger = logging.getLogger("messenger")
# Настройка хеширования паролей
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
app = FastAPI(title="MTUSI Messenger Lab 2", docs_url=None, redoc_url=None)
# --- СХЕМЫ ЗАПРОСОВ (Pydantic) ---
class CreateUserRequest(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=12, max_length=128)
class CreateChatRequest(BaseModel):
model_config = ConfigDict(extra="forbid")
title: str = Field(min_length=1, max_length=100)
member_ids: List[str] = Field(min_length=1, max_length=100)
class CreateMessageRequest(BaseModel):
model_config = ConfigDict(extra="forbid")
text: str = Field(min_length=0, max_length=4000)
# --- ВСПОМОГАТЕЛЬНЫЕ ФУНКЦИИ ---
def current_time_iso(dt_obj: dt.datetime = None) -> str:
if not dt_obj:
dt_obj = dt.datetime.now(dt.timezone.utc)
return dt_obj.strftime("%Y-%m-%dT%H:%M:%S.") + f"{dt_obj.microsecond:06d}Z"
def error_payload(code: str, message: str, request_id: str) -> dict:
return {"error": {"code": code, "message": message, "request_id": request_id}}
# --- MIDDLEWARE & ERROR HANDLERS ---
@app.middleware("http")
async def request_middleware(request: Request, call_next):
req_id = request.headers.get("X-Request-Id") or str(uuid.uuid4())
request.state.request_id = req_id
try:
response: Response = await call_next(request)
except Exception as exc:
logger.error(f"Unhandled exception: {exc.__class__.__name__} req_id={req_id}", exc_info=True)
payload = error_payload("internal_error", "Внутренняя ошибка сервера", req_id)
response = JSONResponse(status_code=500, content=payload)
response.headers["X-Request-Id"] = req_id
logger.info(f"method={request.method} path={request.url.path} status={response.status_code} req_id={req_id}")
return response
@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_payload("invalid_request", "Ошибка валидации тела", req_id),
headers={"X-Request-Id": req_id}
)
@app.exception_handler(StarletteHTTPException)
async def http_exception_handler(request: Request, exc: StarletteHTTPException):
req_id = getattr(request.state, "request_id", str(uuid.uuid4()))
code_map = {
400: "invalid_request",
401: "unauthorized",
403: "not_found", # Скрываем чужие ресурсы под 404/403
404: "not_found",
409: "conflict",
429: "rate_limited",
500: "internal_error",
503: "unavailable",
}
code = code_map.get(exc.status_code, "invalid_request")
message = exc.detail if isinstance(exc.detail, str) else "Ошибка запроса"
headers = getattr(exc, "headers", None) or {"X-Request-Id": req_id}
headers["X-Request-Id"] = req_id
return JSONResponse(status_code=exc.status_code, content=error_payload(code, message, req_id), headers=headers)
# --- АВТОРИЗАЦИЯ ---
async def get_current_user(
authorization: Optional[str] = Header(None),
db: AsyncSession = Depends(get_db)
) -> User:
"""Функция проверки Bearer токена через Redis и получения текущего пользователя из БД"""
def throw_401():
raise StarletteHTTPException(
status_code=401,
detail="Неверный или истёкший токен",
headers={"WWW-Authenticate": 'Bearer realm="messenger"'}
)
if not authorization or not authorization.startswith("Bearer "):
throw_401()
token = authorization.split(" ")[1]
try:
user_id = await redis_client.get(f"session:{token}")
except Exception as exc:
logger.error(f"Redis error: {exc}")
raise StarletteHTTPException(status_code=503, detail="Сервис авторизации недоступен")
if not user_id:
throw_401()
result = await db.execute(select(User).where(User.id == user_id))
user_obj = result.scalars().first()
if not user_obj:
throw_401()
return user_obj
# --- ЭНДПОИНТЫ HEALTH ---
@app.get("/health/live")
def health_live():
return {"status": "ok"}
@app.get("/health/ready")
async def health_ready(db: AsyncSession = Depends(get_db)):
try:
# Проверяем Postgres
await db.execute(select(1))
# Проверяем Redis
await redis_client.ping()
return {"status": "ok"}
except Exception as exc:
logger.error(f"Healthcheck failed: {exc}")
raise StarletteHTTPException(status_code=503, detail="БД или Redis недоступны")
# --- ЭНДПОИНТЫ ПОЛЬЗОВАТЕЛЕЙ ---
@app.post("/api/v1/users", status_code=201)
async def register_user(body: CreateUserRequest, db: AsyncSession = Depends(get_db)):
if not body.display_name.strip():
raise StarletteHTTPException(status_code=400, detail="display_name пуст")
user_id = str(uuid.uuid4())
hashed_password = pwd_context.hash(body.password)
new_user = User(
id=user_id,
username=body.username,
display_name=body.display_name,
password_hash=hashed_password
)
db.add(new_user)
# Обработка состояния гонки (race condition) на уровне БД
try:
await db.commit()
await db.refresh(new_user)
except IntegrityError:
await db.rollback()
raise StarletteHTTPException(status_code=409, detail="Имя пользователя занято")
return {
"id": new_user.id,
"username": new_user.username,
"display_name": new_user.display_name,
"created_at": current_time_iso(new_user.created_at)
}
@app.get("/api/v1/users/me")
async def get_me(user: User = Depends(get_current_user)):
return {
"id": user.id,
"username": user.username,
"display_name": user.display_name,
"created_at": current_time_iso(user.created_at)
}
# --- ЭНДПОИНТЫ АВТОРИЗАЦИИ (СЕССИИ В REDIS) ---
@app.post("/api/v1/auth/sessions", status_code=201)
async def login(authorization: Optional[str] = Header(None), db: AsyncSession = Depends(get_db)):
def throw_401():
raise StarletteHTTPException(
status_code=401,
detail="Неверные логин или пароль",
headers={"WWW-Authenticate": 'Basic realm="messenger", charset="UTF-8"'}
)
if not authorization or not authorization.startswith("Basic "):
throw_401()
try:
b64_data = authorization.split(" ")[1]
decoded = base64.b64decode(b64_data).decode("utf-8")
username, password = decoded.split(":", 1)
except Exception:
throw_401()
result = await db.execute(select(User).where(User.username == username))
user = result.scalars().first()
if not user or not pwd_context.verify(password, user.password_hash):
throw_401()
token = secrets.token_urlsafe(32)
ttl_seconds = int(os.getenv("SESSION_TTL_SECONDS", 3600))
try:
await redis_client.set(f"session:{token}", user.id, ex=ttl_seconds)
except Exception as exc:
logger.error(f"Redis error on login: {exc}")
raise StarletteHTTPException(status_code=503, detail="Сервис авторизации недоступен")
expires_at = dt.datetime.now(dt.timezone.utc) + dt.timedelta(seconds=ttl_seconds)
return {
"token": token,
"token_type": "Bearer",
"expires_at": current_time_iso(expires_at),
"user": {
"id": user.id,
"username": user.username,
"display_name": user.display_name,
"created_at": current_time_iso(user.created_at)
}
}
@app.delete("/api/v1/auth/sessions/current", status_code=204)
async def logout(authorization: Optional[str] = Header(None)):
if not authorization or not authorization.startswith("Bearer "):
raise StarletteHTTPException(status_code=401, detail="Unauthorized", headers={"WWW-Authenticate": 'Bearer realm="messenger"'})
token = authorization.split(" ")[1]
try:
deleted = await redis_client.delete(f"session:{token}")
except Exception as exc:
logger.error(f"Redis error on logout: {exc}")
raise StarletteHTTPException(status_code=503, detail="Сервис авторизации недоступен")
if not deleted:
raise StarletteHTTPException(status_code=401, detail="Unauthorized", headers={"WWW-Authenticate": 'Bearer realm="messenger"'})
return Response(status_code=204)
# --- ЭНДПОИНТЫ ЧАТОВ И СООБЩЕНИЙ ---
@app.post("/api/v1/chats", status_code=201)
async def create_chat(body: CreateChatRequest, user: User = Depends(get_current_user), db: AsyncSession = Depends(get_db)):
if not body.title.strip():
raise StarletteHTTPException(status_code=400, detail="Название не может быть пустым")
if len(body.member_ids) != len(set(body.member_ids)):
raise StarletteHTTPException(status_code=400, detail="Дубликаты в member_ids")
if user.id not in body.member_ids:
raise StarletteHTTPException(status_code=400, detail="Создатель должен быть в чате")
result = await db.execute(select(User).where(User.id.in_(body.member_ids)))
db_users = result.scalars().all()
if len(db_users) != len(body.member_ids):
raise StarletteHTTPException(status_code=400, detail="Некоторые участники не найдены")
new_chat = Chat(id=str(uuid.uuid4()), title=body.title)
new_chat.members.extend(db_users)
db.add(new_chat)
await db.commit()
await db.refresh(new_chat)
return {
"id": new_chat.id,
"title": new_chat.title,
"member_ids": [m.id for m in new_chat.members],
"created_at": current_time_iso(new_chat.created_at)
}
@app.get("/api/v1/chats/{chat_id}")
async def get_chat(chat_id: str, user: User = Depends(get_current_user), db: AsyncSession = Depends(get_db)):
result = await db.execute(select(Chat).where(Chat.id == chat_id))
chat = result.scalars().first()
if not chat or user.id not in [m.id for m in chat.members]:
raise StarletteHTTPException(status_code=404, detail="Чат не найден")
return {
"id": chat.id,
"title": chat.title,
"member_ids": [m.id for m in chat.members],
"created_at": current_time_iso(chat.created_at)
}
@app.get("/api/v1/chats")
async def list_chats(
limit: int = Query(50, ge=1, le=100),
cursor: Optional[str] = Query(None),
user: User = Depends(get_current_user),
db: AsyncSession = Depends(get_db)
):
stmt = select(Chat).filter(Chat.members.any(id=user.id))
if cursor:
try:
decoded = json.loads(base64.urlsafe_b64decode(cursor.encode()).decode())
c_time = dt.datetime.fromisoformat(decoded["created_at"])
c_id = decoded["id"]
stmt = stmt.where(or_(Chat.created_at > c_time, and_(Chat.created_at == c_time, Chat.id > c_id)))
except Exception:
raise StarletteHTTPException(status_code=400, detail="Неверный cursor")
stmt = stmt.order_by(Chat.created_at.asc(), Chat.id.asc()).limit(limit + 1)
result = await db.execute(stmt)
chats = result.scalars().all()
has_next = len(chats) > limit
page_items = chats[:limit]
next_cursor = None
if has_next and page_items:
last = page_items[-1]
raw = json.dumps({"id": last.id, "created_at": last.created_at.isoformat()})
next_cursor = base64.urlsafe_b64encode(raw.encode()).decode()
return {
"items": [
{
"id": c.id,
"title": c.title,
"member_ids": [m.id for m in c.members],
"created_at": current_time_iso(c.created_at)
} for c in page_items
],
"next_cursor": next_cursor
}
@app.post("/api/v1/chats/{chat_id}/messages", status_code=201)
async def create_message(chat_id: str, body: CreateMessageRequest, user: User = Depends(get_current_user), db: AsyncSession = Depends(get_db)):
if not body.text.strip():
raise StarletteHTTPException(status_code=400, detail="Текст пуст")
result = await db.execute(select(Chat).where(Chat.id == chat_id))
chat = result.scalars().first()
if not chat or user.id not in [m.id for m in chat.members]:
raise StarletteHTTPException(status_code=404, detail="Чат не найден")
new_msg = Message(
id=str(uuid.uuid4()),
chat_id=chat.id,
sender_id=user.id,
text=body.text
)
db.add(new_msg)
await db.commit()
await db.refresh(new_msg)
return {
"id": new_msg.id,
"chat_id": new_msg.chat_id,
"sender_id": new_msg.sender_id,
"text": new_msg.text,
"created_at": current_time_iso(new_msg.created_at)
}
@app.get("/api/v1/chats/{chat_id}/messages")
async def list_messages(
chat_id: str,
limit: int = Query(50, ge=1, le=100),
cursor: Optional[str] = Query(None),
user: User = Depends(get_current_user),
db: AsyncSession = Depends(get_db)
):
result = await db.execute(select(Chat).where(Chat.id == chat_id))
chat = result.scalars().first()
if not chat or user.id not in [m.id for m in chat.members]:
raise StarletteHTTPException(status_code=404, detail="Чат не найден")
stmt = select(Message).where(Message.chat_id == chat_id)
if cursor:
try:
decoded = json.loads(base64.urlsafe_b64decode(cursor.encode()).decode())
if decoded.get("chat_id") != chat_id:
raise ValueError("Cursor от другого чата")
c_time = dt.datetime.fromisoformat(decoded["created_at"])
c_id = decoded["id"]
stmt = stmt.where(or_(Message.created_at > c_time, and_(Message.created_at == c_time, Message.id > c_id)))
except Exception:
raise StarletteHTTPException(status_code=400, detail="Неверный cursor")
stmt = stmt.order_by(Message.created_at.asc(), Message.id.asc()).limit(limit + 1)
result = await db.execute(stmt)
messages = result.scalars().all()
has_next = len(messages) > limit
page_items = messages[:limit]
next_cursor = None
if has_next and page_items:
last = page_items[-1]
raw = json.dumps({"chat_id": chat_id, "id": last.id, "created_at": last.created_at.isoformat()})
next_cursor = base64.urlsafe_b64encode(raw.encode()).decode()
return {
"items": [
{
"id": m.id,
"chat_id": m.chat_id,
"sender_id": m.sender_id,
"text": m.text,
"created_at": current_time_iso(m.created_at)
} for m in page_items
],
"next_cursor": next_cursor
}
+53
View File
@@ -0,0 +1,53 @@
import uuid
from datetime import datetime, timezone
from sqlalchemy import Column, String, DateTime, ForeignKey, Table, Index
from sqlalchemy.orm import relationship
from .database import Base
def utcnow():
return datetime.now(timezone.utc)
# Так как в чате много участников, а у участника много чатов — это связь Many-to-Many.
# Для неё нужна промежуточная таблица-связка:
chat_members = Table(
"chat_members",
Base.metadata,
Column("chat_id", String, ForeignKey("chats.id", ondelete="CASCADE"), primary_key=True),
Column("user_id", String, ForeignKey("users.id", ondelete="CASCADE"), primary_key=True)
)
class User(Base):
__tablename__ = "users"
id = Column(String, primary_key=True, default=lambda: str(uuid.uuid4()))
username = Column(String, unique=True, index=True, nullable=False)
display_name = Column(String, nullable=False)
password_hash = Column(String, nullable=False) # Здесь будет лежать зашифрованный пароль
created_at = Column(DateTime(timezone=True), default=utcnow)
class Chat(Base):
__tablename__ = "chats"
id = Column(String, primary_key=True, default=lambda: str(uuid.uuid4()))
title = Column(String, nullable=False)
created_at = Column(DateTime(timezone=True), default=utcnow)
# Автоматически подтягиваем участников чата через таблицу-связку
members = relationship("User", secondary=chat_members, lazy="selectin")
class Message(Base):
__tablename__ = "messages"
id = Column(String, primary_key=True, default=lambda: str(uuid.uuid4()))
chat_id = Column(String, ForeignKey("chats.id", ondelete="CASCADE"), nullable=False, index=True)
sender_id = Column(String, ForeignKey("users.id", ondelete="CASCADE"), nullable=False)
text = Column(String, nullable=False)
created_at = Column(DateTime(timezone=True), default=utcnow)
__table_args__ = (
Index('ix_messages_chat_id_created_at_id', 'chat_id', 'created_at', 'id'),
)
class Session(Base):
__tablename__ = "sessions"
token = Column(String, primary_key=True)
user_id = Column(String, ForeignKey("users.id", ondelete="CASCADE"), nullable=False)
expires_at = Column(DateTime(timezone=True), nullable=False)
+104
View File
@@ -0,0 +1,104 @@
#!/usr/bin/env python3
"""Custom test suite for grade 4 criteria: concurrency & corner cases."""
from concurrent.futures import ThreadPoolExecutor
import json
import urllib.request
import urllib.error
from urllib.parse import quote
import uuid
import sys
from client import Client, check, Failure, run
BASE_URL = "http://localhost:8080"
def test_corner_cases():
print("--- 1. Тестирование граничных случаев (Corner cases) ---")
c = Client(BASE_URL)
alice, bob, eve, chat = c.bootstrap()
path = f"/api/v1/chats/{chat['id']}"
# 1. Дубликат username -> 409
c.call("POST", "/api/v1/users", 409, body=alice["registration"])
print("[+] 409 Conflict на дубликат username проверен")
# 2. Неверный JSON (malformed) -> 400
req = urllib.request.Request(
f"{BASE_URL}/api/v1/users",
data=b"invalid json syntax {",
headers={"Content-Type": "application/json", "Accept": "application/json"},
method="POST"
)
try:
c.opener.open(req)
raise Failure("Expected 400 on malformed JSON")
except urllib.error.HTTPError as err:
check(err.code == 400, f"Expected 400, got {err.code}")
print("[+] 400 Bad Request на битый JSON проверен")
# 3. Пустое / пробельное сообщение -> 400
c.call("POST", f"{path}/messages", 400, actor=alice, body={"text": " "})
print("[+] 400 Bad Request на пустое сообщение проверен")
# 4. Доступ чужого пользователя к чату -> 404 (не 403)
c.call("GET", path, 404, actor=eve)
c.call("GET", f"{path}/messages", 404, actor=eve)
c.call("POST", f"{path}/messages", 404, actor=eve, body={"text": "Взлом"})
print("[+] 404 Not Found при попытке доступа постороннего проверен")
# 5. Пагинация по нескольким страницам
for i in range(5):
c.message(alice, chat, f"Message {i}")
page = c.call("GET", f"{path}/messages?limit=2", actor=alice, schema="MessagePage")["json"]
check(len(page["items"]) == 2 and page["next_cursor"] is not None, "Page 1 failed")
cursor = page["next_cursor"]
page2 = c.call("GET", f"{path}/messages?limit=2&cursor={quote(cursor)}", actor=alice, schema="MessagePage")["json"]
check(len(page2["items"]) == 2, "Page 2 failed")
print("[+] Cursor-пагинация проверена")
print("PASS: Все граничные тесты пройдены успешно!\n")
def test_concurrency():
print("--- 2. Тестирование защиты от гонок (Concurrency: 100 сообщений от 10 клиентов) ---")
c = Client(BASE_URL)
alice, bob, _, chat = c.bootstrap()
path = f"/api/v1/chats/{chat['id']}/messages"
total_messages = 100
workers_count = 10
messages_per_worker = total_messages // workers_count
def worker_task(worker_id):
worker_client = Client(BASE_URL, timeout=10)
actor = alice if worker_id % 2 == 0 else bob
sent = []
for i in range(messages_per_worker):
text = f"Worker {worker_id} - msg {i} - {uuid.uuid4().hex[:6]}"
res = worker_client.call("POST", path, 201, actor=actor, body={"text": text}, schema="Message")["json"]
sent.append(res["id"])
return sent
with ThreadPoolExecutor(max_workers=workers_count) as pool:
results = list(pool.map(worker_task, range(workers_count)))
sent_ids = [msg_id for worker_result in results for msg_id in worker_result]
check(len(sent_ids) == total_messages, f"Expected {total_messages} sent, got {len(sent_ids)}")
check(len(set(sent_ids)) == total_messages, "Duplicate message IDs detected in responses")
# Считываем все сообщения из чата
read_page = c.call("GET", f"{path}?limit=100", actor=alice, schema="MessagePage")["json"]
saved_ids = {m["id"] for m in read_page["items"]}
# Проверяем, что все отправленные сообщения на месте
for m_id in sent_ids:
check(m_id in saved_ids, f"Message {m_id} lost under concurrency!")
print(f"PASS: Successfully delivered and verified {total_messages} unique messages under concurrent load.\n")
def main():
test_corner_cases()
test_concurrency()
if __name__ == "__main__":
run(main)