From d3bb55ba09830e68ac8462ddef7e1972e9e42cf3 Mon Sep 17 00:00:00 2001 From: casablanca Date: Thu, 15 Oct 2026 00:18:02 +0300 Subject: [PATCH] =?UTF-8?q?=D0=BB=D0=B0=D0=B1=D0=B0=20=D0=BD=D0=B0=203?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Dockerfile | 25 + REPORT.md | 211 +++++- alembic.ini | 149 ++++ compose.yaml | 111 +++ evidence/compose-status.txt | 7 + evidence/failover.txt | 4 + evidence/idempotency.txt | 5 + evidence/persistence.txt | 3 + evidence/smoke.txt | 2 + infra/nginx.conf | 31 + migrations/README | 1 + migrations/env.py | 50 ++ migrations/script.py.mako | 28 + .../b3a91c0e7d42_message_idempotency.py | 61 ++ .../versions/f7c51e1e945b_initial_schema.py | 110 +++ requirements.txt | 20 + scripts/bootstrap.sh | 28 + scripts/restart.sh | 4 + scripts/stop-one.sh | 14 + src/__init__.py | 0 src/cursor.py | 60 ++ src/database.py | 57 ++ src/errors.py | 12 + src/main.py | 509 ++++++++++++++ src/models.py | 118 ++++ src/repositories.py | 646 ++++++++++++++++++ src/security.py | 16 + src/sessions.py | 87 +++ tests/test_concurrent_idempotency.py | 46 ++ 29 files changed, 2397 insertions(+), 18 deletions(-) create mode 100644 Dockerfile create mode 100644 alembic.ini create mode 100644 compose.yaml create mode 100644 evidence/compose-status.txt create mode 100644 evidence/failover.txt create mode 100644 evidence/idempotency.txt create mode 100644 evidence/persistence.txt create mode 100644 evidence/smoke.txt create mode 100644 infra/nginx.conf create mode 100644 migrations/README create mode 100644 migrations/env.py create mode 100644 migrations/script.py.mako create mode 100644 migrations/versions/b3a91c0e7d42_message_idempotency.py create mode 100644 migrations/versions/f7c51e1e945b_initial_schema.py create mode 100644 requirements.txt create mode 100755 scripts/bootstrap.sh create mode 100755 scripts/restart.sh create mode 100755 scripts/stop-one.sh create mode 100644 src/__init__.py create mode 100644 src/cursor.py create mode 100644 src/database.py create mode 100644 src/errors.py create mode 100644 src/main.py create mode 100644 src/models.py create mode 100644 src/repositories.py create mode 100644 src/security.py create mode 100644 src/sessions.py create mode 100644 tests/test_concurrent_idempotency.py diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..99aff457 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,25 @@ +FROM python:3.13.15-slim + +ENV PYTHONDONTWRITEBYTECODE=1 +ENV PYTHONUNBUFFERED=1 + +WORKDIR /app + +COPY requirements.txt . + +RUN pip install --no-cache-dir -r requirements.txt + +COPY alembic.ini . +COPY migrations ./migrations +COPY src ./src + +RUN useradd --create-home --uid 10001 appuser + +USER appuser + +EXPOSE 8080 + +HEALTHCHECK --interval=10s --timeout=3s --start-period=5s --retries=3 \ + CMD python -c "import urllib.request; urllib.request.urlopen('http://127.0.0.1:8080/health/ready', timeout=2)" || exit 1 + +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 c2679005..38eed952 100644 --- a/REPORT.md +++ b/REPORT.md @@ -1,37 +1,212 @@ # Отчёт — лабораторная 3 -- Автор, группа: TODO -- Целевая оценка; выбранный трек 5, если нужен: TODO -- Commit/PR: TODO после публикации -- Использованные источники, библиотеки и помощь ИИ: TODO +- Автор, группа: Станислав Травкин, БВТ2304 +- Целевая оценка: 5, выполнение по этапам 3 → 4 → 5 +- Выбранный трек оценки 5: цена масштабирования +- Commit/PR: будет указан после публикации +- Использованные источники: документация Docker Compose, FastAPI, SQLAlchemy, Alembic, PostgreSQL, Redis, Nginx; материалы лабораторной работы +- Помощь ИИ: ChatGPT использовался для обсуждения архитектуры, разработки, отладки и подготовки документации ## Запуск из чистого clone -TODO: версии runtime/образов, prerequisites, команды создания локальных secrets/.env, build, миграций, запуска, ожидания готовности, тестов и остановки. Указать необходимые действия оператора. Не вставлять секреты. Отдельно указать безопасную остановку и команду намеренного удаления учебных данных. +Для запуска необходимы Docker Engine с поддержкой Docker Compose, Git и Python 3.9 или новее. + +Приложение работает в контейнерах. Локальная установка PostgreSQL и Redis не требуется. + +Используемые образы: + +- Python 3.13.15-slim +- PostgreSQL 17.6 +- Redis 8.2.2 +- Nginx 1.29-alpine + +Из корневого каталога проекта выполнить: + +```bash +./scripts/bootstrap.sh +docker compose config --quiet +docker compose up -d --build +docker compose ps -a +``` + +Скрипт `bootstrap.sh` создаёт локальные файлы секретов, если они отсутствуют, и проверяет конфигурацию Compose. Значения существующих секретов не перезаписываются. + +Используются файлы: + +- `secrets/postgres_password` — пароль PostgreSQL +- `secrets/database_url` — строка подключения к PostgreSQL +- `secrets/cache_url` — строка подключения к Redis + +Секреты не должны попадать в Git. + +Миграции Alembic выполняются отдельным сервисом `migrate` перед запуском API. Успешное завершение этого контейнера отображается как `Exited (0)`. + +После запуска обе API-реплики должны иметь статус `healthy`, PostgreSQL и Redis — `healthy`, Nginx — `Up`. + +Единая точка входа: + +`http://localhost:8080` + +Проверки: + +```bash +make check +make validate +make test +make test-persistence ACTION_SCRIPT=./scripts/restart.sh +make test-failover ACTION_SCRIPT=./scripts/stop-one.sh +python3 tests/test_concurrent_idempotency.py +``` + +Для локальных Python-проверок требуется установить зависимости из `requirements-dev.txt`, если они ещё не установлены. + +После failover-теста восстановить реплики: + +```bash +docker compose start api-1 api-2 +``` + +Безопасная остановка стенда с сохранением данных: + +```bash +docker compose down +``` + +Повторный запуск: + +```bash +docker compose up -d --build +``` + +Для намеренного удаления учебных данных и постоянных Docker volumes используется: + +```bash +docker compose down -v +``` + +Последняя команда удаляет данные PostgreSQL и Redis и не применяется при обычном перезапуске. ## Архитектура и решения -TODO: схема процессов/хранилищ, источник истины, транзакционные границы, ограничения. Что перенесено из lab2, что изменено в этой работе. Соответствие переменных из .env.example реальной конфигурации. +Используется Docker Compose со следующими компонентами: + +```text +Client + | + v +Nginx :8080 + | + +---- API-1 :8080 + | + +---- API-2 :8080 + | + +---- PostgreSQL + | + +---- Redis +``` + +Nginx является единой публичной точкой входа. API-реплики не публикуют порты на хосте и доступны через внутреннюю сеть Compose. + +Обе реплики используют одну PostgreSQL и один Redis. + +PostgreSQL хранит постоянные данные приложения и информацию, необходимую для общей идемпотентности отправки сообщений. Redis хранит пользовательские сессии с TTL 3600 секунд. + +В API не используются локальные пользовательские сессии, поэтому запросы одного пользователя могут обслуживаться разными репликами без повторной авторизации. + +Каждая реплика возвращает заголовок `X-Instance-Id`, позволяющий определить обработавший запрос экземпляр. + +Для отправки сообщений используется обязательный `Idempotency-Key`. Повторная отправка с тем же ключом не должна создавать второе сообщение. Согласованность между репликами обеспечивается общим PostgreSQL и транзакционным механизмом идемпотентности. + +Миграции выполняются отдельным контейнером, что исключает одновременный запуск Alembic несколькими API-репликами. + +В Nginx настроены два backend-сервера, таймауты подключения и обработки запросов, а также повторная попытка при отдельных ошибках upstream. + +Из лабораторной 2 перенесены основная реализация API, PostgreSQL, миграции, авторизация и работа с сообщениями. В лабораторной 3 добавлены балансировка, две API-реплики, идентификация экземпляров и общая идемпотентность. + +Файл `.env.example` является шаблоном задания. Фактическая конфигурация контейнеров задаётся в `compose.yaml`; подключения к PostgreSQL и Redis передаются через файлы секретов. ## Доказательства критериев -| Критерий README | Команда/сценарий | Наблюдаемый результат | Файл доказательства | +| Критерий | Команда или сценарий | Наблюдаемый результат | Доказательство | | --- | --- | --- | --- | -| Публичный smoke | make test | TODO | TODO | -| Остальные пункты уровня 3 | TODO | TODO | TODO | -| Все пункты уровня 4, если заявлен | TODO | TODO | TODO | -| Выбранный трек 5, если заявлен | TODO | TODO | TODO | - -TODO: добавить отдельную строку для каждого заявленного критерия. PASS без команды и наблюдения не является доказательством. Проверка инфраструктурного hook требует подтверждения, какие реальные процессы/хранилища перезапущены. +| Публичный smoke | `make test` | PASS | `evidence/smoke.txt` | +| Структура проекта | `make check` | PASS | Вывод проверки | +| OpenAPI | `make validate` | PASS | Вывод проверки | +| Две API-реплики | `docker compose ps -a` | Обе реплики healthy | `evidence/compose-status.txt` | +| Общая сессия | `make test-failover` | Сессия сохраняется после отказа реплики | `evidence/failover.txt` | +| Отказ одной реплики | `make test-failover ACTION_SCRIPT=./scripts/stop-one.sh` | 55 успешных записей, 4 неудачные попытки, первое восстановление за 1,327 с | `evidence/failover.txt` | +| Сохранность данных | `make test-persistence ACTION_SCRIPT=./scripts/restart.sh` | PASS, подтверждённые данные и сессия сохранились | `evidence/persistence.txt` | +| Конкурентная идемпотентность | `python3 tests/test_concurrent_idempotency.py` | 20 запросов, 1 уникальный ID, 1 сообщение в истории | `evidence/idempotency.txt` | +| Запросы через разные реплики | Конкурентный тест | Обнаружены `api-1` и `api-2` | `evidence/idempotency.txt` | +| Остановка каждой реплики | Ручная последовательная проверка | При остановке одной реплики оставшаяся продолжала отвечать HTTP 200 | Результаты ручной проверки | ## Эксперименты и отказы -TODO: гипотеза, hardware/лимиты, версии, объём данных, профиль нагрузки, concurrency/arrival rate, длительность/прогрев, повторы, успешные RPS, ошибки, p50/p95/p99, CPU/RAM. Сохранить raw summary без tokens и объяснить ограничения измерения. +На этапе оценки 3 выполнены функциональные проверки отказоустойчивости, сохранности данных и идемпотентности. -| Воздействие | Что ожидаем | Что наблюдали | Время восстановления / потеря данных | -| --- | --- | --- | --- | -| TODO | TODO | TODO | TODO | +### Отказ API-реплики + +Команда: + +```bash +make test-failover ACTION_SCRIPT=./scripts/stop-one.sh +``` + +Полученные результаты: + +- Наблюдаемых реплик до отказа: 2 +- Успешных записей: 55 +- Неудачных попыток: 4 +- Время до первого успешного восстановления: 1,327 с +- Подтверждённые данные: сохранены +- Существующая сессия: сохранена + +Дополнительно вручную проверена остановка каждой реплики с восстановлением предыдущей. В обоих случаях оставшаяся реплика продолжала обслуживать запросы через Nginx. + +### Сохранность данных + +Команда: + +```bash +make test-persistence ACTION_SCRIPT=./scripts/restart.sh +``` + +Результат: PASS. После перезапуска PostgreSQL, Redis и API подтверждённые данные и существующая сессия остались доступными. + +### Конкурентная идемпотентность + +Команда: + +```bash +python3 tests/test_concurrent_idempotency.py +``` + +Результаты: + +- Параллельных запросов: 20 +- Уникальных идентификаторов сообщений: 1 +- Наблюдаемые реплики: `api-1`, `api-2` +- Совпадающих сообщений в истории: 1 +- Итог: PASS + +Таким образом, конкурентные запросы с одинаковым ключом не создали дублирующих сообщений. + +### Дальнейшие измерения + +Для оценки 4 планируется собрать RPS, количество ошибок, время восстановления, задержки и потребление ресурсов при последовательной остановке каждой реплики и поочерёдном обновлении контейнеров. + +Для оценки 5 выбран трек исследования цены масштабирования. Планируется сравнить 1, 2 и 4 API-реплики при фиксированном общем бюджете CPU/RAM, затем повторить эксперимент с увеличением ресурсов. ## Ограничения и следующие шаги -TODO: какие отказы выдерживаются и какие нет; что не реализовано; где учебное упрощение; какое улучшение подтверждено, а какое пока гипотеза. +Реализована устойчивость к остановке одной API-реплики при сохранении доступности второй. + +PostgreSQL, Redis и Nginx представлены одиночными экземплярами и остаются едиными точками отказа. Отказ этих компонентов не покрывается механизмом переключения API-реплик. + +Проверка повторного запуска выполнена на существующих Docker volumes. Полностью независимый запуск из чистого клона пока не проверялся. + +Время 1,327 с характеризует восстановление, наблюдаемое тестом, а не отдельное измерение времени обнаружения отказа балансировщиком. + +В текущем локальном варианте файловые секреты имеют права чтения, допускающие доступ других локальных пользователей. Для следующего этапа необходимо ограничить доступ к ним без нарушения работы непривилегированных контейнеров. + +Количественные эксперименты с производительностью, rolling update и масштабированием будут проведены при выполнении критериев оценок 4 и 5. \ No newline at end of file diff --git a/alembic.ini b/alembic.ini new file mode 100644 index 00000000..017e41ad --- /dev/null +++ b/alembic.ini @@ -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/migrations + +# 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 /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 = + + +[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 diff --git a/compose.yaml b/compose.yaml new file mode 100644 index 00000000..a91b71ee --- /dev/null +++ b/compose.yaml @@ -0,0 +1,111 @@ +x-api: &api + build: + context: . + dockerfile: Dockerfile + environment: + APP_ENV: lab + SESSION_TTL_SECONDS: 3600 + DATABASE_URL_FILE: /run/secrets/database_url + CACHE_URL_FILE: /run/secrets/cache_url + secrets: + - database_url + - cache_url + depends_on: + migrate: + condition: service_completed_successfully + redis: + condition: service_healthy + expose: + - "8080" + restart: unless-stopped + stop_grace_period: 15s + mem_limit: 512m + cpus: 1.0 + +services: + postgres: + image: postgres:17.6 + environment: + POSTGRES_DB: messenger + POSTGRES_USER: lab2 + POSTGRES_PASSWORD_FILE: /run/secrets/postgres_password + secrets: + - postgres_password + volumes: + - postgres_data:/var/lib/postgresql/data + healthcheck: + test: ["CMD-SHELL", "pg_isready -U lab2 -d messenger"] + interval: 5s + timeout: 3s + retries: 10 + restart: unless-stopped + + redis: + image: redis:8.2.2 + command: + - redis-server + - --appendonly + - "yes" + - --appendfsync + - everysec + volumes: + - redis_data:/data + healthcheck: + test: ["CMD", "redis-cli", "ping"] + interval: 5s + timeout: 3s + retries: 10 + restart: unless-stopped + + migrate: + build: + context: . + dockerfile: Dockerfile + command: ["alembic", "upgrade", "head"] + secrets: + - database_url + depends_on: + postgres: + condition: service_healthy + restart: "no" + + api-1: + <<: *api + environment: + APP_ENV: lab + SESSION_TTL_SECONDS: 3600 + DATABASE_URL_FILE: /run/secrets/database_url + CACHE_URL_FILE: /run/secrets/cache_url + INSTANCE_ID: api-1 + + api-2: + <<: *api + environment: + APP_ENV: lab + SESSION_TTL_SECONDS: 3600 + DATABASE_URL_FILE: /run/secrets/database_url + CACHE_URL_FILE: /run/secrets/cache_url + INSTANCE_ID: api-2 + + nginx: + image: nginx:1.29-alpine + ports: + - "8080:8080" + volumes: + - ./infra/nginx.conf:/etc/nginx/nginx.conf:ro + depends_on: + - api-1 + - api-2 + restart: unless-stopped + +secrets: + postgres_password: + file: ./secrets/postgres_password + database_url: + file: ./secrets/database_url + cache_url: + file: ./secrets/cache_url + +volumes: + postgres_data: + redis_data: \ No newline at end of file diff --git a/evidence/compose-status.txt b/evidence/compose-status.txt new file mode 100644 index 00000000..c060f47d --- /dev/null +++ b/evidence/compose-status.txt @@ -0,0 +1,7 @@ +NAME IMAGE COMMAND SERVICE CREATED STATUS PORTS +mtusi2026lab3-api-1-1 sha256:c9c8b63333aa74724c05967ab2be710213e8636de9b5563f83c992e68067fe27 "uvicorn src.main:ap…" api-1 44 minutes ago Up 6 minutes (healthy) 8080/tcp +mtusi2026lab3-api-2-1 sha256:2450b84cf4e2e2386916c732dfbf3b345488435830de312062c91953cf46eead "uvicorn src.main:ap…" api-2 44 minutes ago Up 21 minutes (healthy) 8080/tcp +mtusi2026lab3-migrate-1 sha256:e7713bfcd86cf0405a7503aaf1a00e5d3d3b3f0ce2c76d024c41b33e2f16d06e "alembic upgrade head" migrate 44 minutes ago Exited (0) 2 minutes ago +mtusi2026lab3-nginx-1 nginx:1.29-alpine "/docker-entrypoint.…" nginx 44 minutes ago Up 41 minutes 0.0.0.0:8080->8080/tcp, [::]:8080->8080/tcp +mtusi2026lab3-postgres-1 postgres:17.6 "docker-entrypoint.s…" postgres 44 minutes ago Up 27 minutes (healthy) 5432/tcp +mtusi2026lab3-redis-1 redis:8.2.2 "docker-entrypoint.s…" redis 44 minutes ago Up 27 minutes (healthy) 6379/tcp diff --git a/evidence/failover.txt b/evidence/failover.txt new file mode 100644 index 00000000..c385dbb8 --- /dev/null +++ b/evidence/failover.txt @@ -0,0 +1,4 @@ +python3 tests/resilience.py --mode failover --action-script "./scripts/stop-one.sh" --base-url "http://localhost:8080" +Executing explicit failover hook; hook output suppressed to avoid leaking secrets. +{"observed_instances_before": 2, "successful_writes": 55, "failed_attempts": 4, "first_recovery_seconds": 1.327} +PASS: failover preserved confirmed data and existing session. Hook scope needs operator evidence. diff --git a/evidence/idempotency.txt b/evidence/idempotency.txt new file mode 100644 index 00000000..4eeff2b6 --- /dev/null +++ b/evidence/idempotency.txt @@ -0,0 +1,5 @@ +Requests: 20 +Unique message IDs: 1 +Instances: ['api-1', 'api-2'] +Messages in history: 1 +PASS: concurrent idempotency across replicas diff --git a/evidence/persistence.txt b/evidence/persistence.txt new file mode 100644 index 00000000..1689fabe --- /dev/null +++ b/evidence/persistence.txt @@ -0,0 +1,3 @@ +python3 tests/resilience.py --mode persistence --action-script "./scripts/restart.sh" --base-url "http://localhost:8080" +Executing explicit persistence hook; hook output suppressed to avoid leaking secrets. +PASS: persistence preserved confirmed data and existing session. Hook scope needs operator evidence. diff --git a/evidence/smoke.txt b/evidence/smoke.txt new file mode 100644 index 00000000..44e41deb --- /dev/null +++ b/evidence/smoke.txt @@ -0,0 +1,2 @@ +python3 tests/smoke.py --base-url "http://localhost:8080" +PASS: lab3 public API scenario; infrastructure and grading evidence remain separate. diff --git a/infra/nginx.conf b/infra/nginx.conf new file mode 100644 index 00000000..0038e997 --- /dev/null +++ b/infra/nginx.conf @@ -0,0 +1,31 @@ +events { + worker_connections 1024; +} + +http { + upstream api_backend { + server api-1:8080 max_fails=1 fail_timeout=5s; + server api-2:8080 max_fails=1 fail_timeout=5s; + } + + server { + listen 8080; + + location / { + proxy_pass http://api_backend; + proxy_http_version 1.1; + + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto $scheme; + + proxy_connect_timeout 2s; + proxy_read_timeout 10s; + proxy_send_timeout 10s; + + proxy_next_upstream error timeout http_502 http_503 http_504; + proxy_next_upstream_tries 2; + } + } +} \ No newline at end of file diff --git a/migrations/README b/migrations/README new file mode 100644 index 00000000..e0d0858f --- /dev/null +++ b/migrations/README @@ -0,0 +1 @@ +Generic single-database configuration with an async dbapi. \ No newline at end of file diff --git a/migrations/env.py b/migrations/env.py new file mode 100644 index 00000000..7125d778 --- /dev/null +++ b/migrations/env.py @@ -0,0 +1,50 @@ +import asyncio + +from alembic import context +from sqlalchemy import pool +from sqlalchemy.ext.asyncio import async_engine_from_config + + +config = context.config + +DATABASE_URL_FILE = "/run/secrets/database_url" + + +def read_database_url() -> str: + with open(DATABASE_URL_FILE, encoding="utf-8") as file: + return file.read().strip() + + +config.set_main_option("sqlalchemy.url", read_database_url()) + +target_metadata = None + + +def do_run_migrations(connection) -> None: + context.configure( + connection=connection, + target_metadata=target_metadata, + ) + + with context.begin_transaction(): + context.run_migrations() + + +async def run_async_migrations() -> None: + 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: + asyncio.run(run_async_migrations()) + + +run_migrations_online() \ No newline at end of file diff --git a/migrations/script.py.mako b/migrations/script.py.mako new file mode 100644 index 00000000..11016301 --- /dev/null +++ b/migrations/script.py.mako @@ -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"} diff --git a/migrations/versions/b3a91c0e7d42_message_idempotency.py b/migrations/versions/b3a91c0e7d42_message_idempotency.py new file mode 100644 index 00000000..1a794894 --- /dev/null +++ b/migrations/versions/b3a91c0e7d42_message_idempotency.py @@ -0,0 +1,61 @@ +"""Message idempotency for lab 3. + +Revision ID: b3a91c0e7d42 +Revises: f7c51e1e945b +""" + +from alembic import op +import sqlalchemy as sa + + +revision = "b3a91c0e7d42" +down_revision = "f7c51e1e945b" +branch_labels = None +depends_on = None + + +def upgrade() -> None: + op.create_table( + "message_idempotency", + sa.Column("user_id", sa.Uuid(), nullable=False), + sa.Column("chat_id", sa.Uuid(), nullable=False), + sa.Column("key", sa.String(length=128), nullable=False), + sa.Column("request_hash", sa.String(length=64), nullable=False), + sa.Column("message_id", sa.Uuid(), nullable=False), + sa.Column( + "created_at", + sa.DateTime(timezone=True), + server_default=sa.text("CURRENT_TIMESTAMP"), + nullable=False, + ), + sa.ForeignKeyConstraint( + ["user_id"], + ["users.id"], + ondelete="CASCADE", + ), + sa.ForeignKeyConstraint( + ["chat_id"], + ["chats.id"], + ondelete="CASCADE", + ), + sa.ForeignKeyConstraint( + ["message_id"], + ["messages.id"], + ondelete="CASCADE", + ), + sa.PrimaryKeyConstraint("user_id", "chat_id", "key"), + ) + + op.create_index( + "ix_message_idempotency_created_at", + "message_idempotency", + ["created_at"], + ) + + +def downgrade() -> None: + op.drop_index( + "ix_message_idempotency_created_at", + table_name="message_idempotency", + ) + op.drop_table("message_idempotency") \ No newline at end of file diff --git a/migrations/versions/f7c51e1e945b_initial_schema.py b/migrations/versions/f7c51e1e945b_initial_schema.py new file mode 100644 index 00000000..5bc1512b --- /dev/null +++ b/migrations/versions/f7c51e1e945b_initial_schema.py @@ -0,0 +1,110 @@ +"""initial schema + +Revision ID: f7c51e1e945b +Revises: +Create Date: 2026-10-07 18:33:37.771593 + +""" + +from typing import Sequence, Union + +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision: str = "f7c51e1e945b" +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: + op.create_table( + "users", + sa.Column("id", sa.Uuid(), nullable=False), + sa.Column("username", sa.String(length=32), nullable=False), + sa.Column("display_name", sa.String(length=100), nullable=False), + sa.Column("password_hash", sa.Text(), nullable=False), + sa.Column( + "created_at", + sa.DateTime(timezone=True), + server_default=sa.text("CURRENT_TIMESTAMP"), + nullable=False, + ), + sa.PrimaryKeyConstraint("id"), + sa.UniqueConstraint("username", name="uq_users_username"), + ) + + op.create_table( + "chats", + sa.Column("id", sa.Uuid(), nullable=False), + sa.Column("title", sa.String(length=100), nullable=False), + sa.Column( + "created_at", + sa.DateTime(timezone=True), + server_default=sa.text("CURRENT_TIMESTAMP"), + nullable=False, + ), + sa.PrimaryKeyConstraint("id"), + ) + + op.create_table( + "chat_members", + sa.Column("chat_id", sa.Uuid(), nullable=False), + sa.Column("user_id", sa.Uuid(), 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.Uuid(), nullable=False), + sa.Column("chat_id", sa.Uuid(), nullable=False), + sa.Column("sender_id", sa.Uuid(), nullable=False), + sa.Column("text", sa.String(length=4000), nullable=False), + sa.Column( + "created_at", + sa.DateTime(timezone=True), + server_default=sa.text("CURRENT_TIMESTAMP"), + nullable=False, + ), + sa.ForeignKeyConstraint( + ["chat_id"], + ["chats.id"], + ondelete="CASCADE", + ), + sa.ForeignKeyConstraint( + ["sender_id"], + ["users.id"], + ondelete="RESTRICT", + ), + sa.PrimaryKeyConstraint("id"), + ) + + op.create_index( + "ix_messages_chat_created_id", + "messages", + ["chat_id", "created_at", "id"], + unique=False, + ) + + +def downgrade() -> None: + op.drop_index( + "ix_messages_chat_created_id", + table_name="messages", + ) + op.drop_table("messages") + op.drop_table("chat_members") + op.drop_table("chats") + op.drop_table("users") \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 00000000..2f27f572 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,20 @@ +annotated-doc==0.0.5 +annotated-types==0.8.0 +anyio==4.15.1 +click==8.5.0 +fastapi==0.141.1 +h11==0.16.0 +idna==3.20 +pydantic==2.13.5 +pydantic_core==2.46.5 +starlette==1.7.0 +typing-inspection==0.4.4 +typing_extensions==4.16.0 +uvicorn==0.53.0 +SQLAlchemy==2.1.4 +asyncpg==0.32.0 +alembic==1.20.0 +greenlet==3.5.6 + +argon2-cffi==25.1.0 +redis==7.0.1 diff --git a/scripts/bootstrap.sh b/scripts/bootstrap.sh new file mode 100755 index 00000000..a24b5636 --- /dev/null +++ b/scripts/bootstrap.sh @@ -0,0 +1,28 @@ +#!/usr/bin/env bash +set -euo pipefail + +cd "$(dirname "$0")/.." + +mkdir -p secrets +chmod 700 secrets + +if [[ ! -f secrets/postgres_password ]]; then + python3 -c 'import secrets; print(secrets.token_hex(24))' > secrets/postgres_password +fi + +if [[ ! -f secrets/database_url ]]; then + PASSWORD="$(cat secrets/postgres_password)" + printf 'postgresql+asyncpg://lab2:%s@postgres:5432/messenger\n' "$PASSWORD" > secrets/database_url +fi + +if [[ ! -f secrets/cache_url ]]; then + printf 'redis://redis:6379/0\n' > secrets/cache_url +fi + +chmod 644 secrets/postgres_password secrets/database_url secrets/cache_url + +if [[ ! -f .env ]]; then + cp .env.example .env +fi + +docker compose config --quiet \ No newline at end of file diff --git a/scripts/restart.sh b/scripts/restart.sh new file mode 100755 index 00000000..7ef0d531 --- /dev/null +++ b/scripts/restart.sh @@ -0,0 +1,4 @@ +#!/usr/bin/env bash +set -euo pipefail + +docker compose restart postgres redis api-1 api-2 \ No newline at end of file diff --git a/scripts/stop-one.sh b/scripts/stop-one.sh new file mode 100755 index 00000000..d1e8633b --- /dev/null +++ b/scripts/stop-one.sh @@ -0,0 +1,14 @@ +#!/usr/bin/env bash +set -euo pipefail + +: "${TARGET_INSTANCE_ID:?}" + +case "$TARGET_INSTANCE_ID" in + api-1|api-2) + docker compose stop "$TARGET_INSTANCE_ID" + ;; + *) + echo "Unknown instance: $TARGET_INSTANCE_ID" >&2 + exit 1 + ;; +esac \ No newline at end of file diff --git a/src/__init__.py b/src/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/src/cursor.py b/src/cursor.py new file mode 100644 index 00000000..16702905 --- /dev/null +++ b/src/cursor.py @@ -0,0 +1,60 @@ +import base64 +import binascii +import json +from datetime import datetime +from uuid import UUID + + +def encode_cursor( + chat_id: UUID, + created_at: datetime, + message_id: UUID, +) -> str: + data = { + "chat_id": str(chat_id), + "created_at": created_at.isoformat(), + "message_id": str(message_id), + } + + raw = json.dumps( + data, + separators=(",", ":"), + ).encode("utf-8") + + return base64.urlsafe_b64encode(raw).decode("ascii") + + +def decode_cursor(cursor: str) -> tuple[UUID, datetime, UUID]: + try: + raw = base64.urlsafe_b64decode(cursor.encode("ascii")) + + data = json.loads(raw.decode("utf-8")) + + if not isinstance(data, dict): + raise ValueError("Invalid cursor") + + if set(data.keys()) != { + "chat_id", + "created_at", + "message_id", + }: + raise ValueError("Invalid cursor") + + chat_id = UUID(data["chat_id"]) + created_at = datetime.fromisoformat(data["created_at"]) + message_id = UUID(data["message_id"]) + + if created_at.tzinfo is None or created_at.utcoffset() is None: + raise ValueError("Invalid cursor") + + return chat_id, created_at, message_id + + except ( + ValueError, + KeyError, + TypeError, + UnicodeError, + binascii.Error, + json.JSONDecodeError, + ) as exc: + raise ValueError("Invalid cursor") from exc \ No newline at end of file diff --git a/src/database.py b/src/database.py new file mode 100644 index 00000000..95b01ffb --- /dev/null +++ b/src/database.py @@ -0,0 +1,57 @@ +from collections.abc import AsyncIterator + +import socket + +from sqlalchemy import text +from sqlalchemy.exc import SQLAlchemyError +from sqlalchemy.ext.asyncio import ( + AsyncSession, + async_sessionmaker, + create_async_engine, +) + + +DATABASE_URL_FILE = "/run/secrets/database_url" + +POOL_SIZE = 5 +MAX_OVERFLOW = 5 +POOL_TIMEOUT_SECONDS = 5 +DB_CONNECT_TIMEOUT_SECONDS = 5 + + +def read_database_url() -> str: + with open(DATABASE_URL_FILE, encoding="utf-8") as file: + return file.read().strip() + + +database_url = read_database_url() + +engine = create_async_engine( + database_url, + pool_size=POOL_SIZE, + max_overflow=MAX_OVERFLOW, + pool_timeout=POOL_TIMEOUT_SECONDS, + connect_args={ + "timeout": DB_CONNECT_TIMEOUT_SECONDS, + }, + pool_pre_ping=True, +) + +SessionFactory = async_sessionmaker( + bind=engine, + expire_on_commit=False, +) + + +async def get_db_session() -> AsyncIterator[AsyncSession]: + async with SessionFactory() as session: + yield session + + +async def ping_database() -> bool: + try: + async with engine.connect() as connection: + await connection.execute(text("SELECT 1")) + return True + except (SQLAlchemyError, OSError): + return False \ No newline at end of file diff --git a/src/errors.py b/src/errors.py new file mode 100644 index 00000000..8b853205 --- /dev/null +++ b/src/errors.py @@ -0,0 +1,12 @@ +class APIError(Exception): + def __init__( + self, + status_code: int, + code: str, + message: str, + headers: dict[str, str] | None = None, + ): + self.status_code = status_code + self.code = code + self.message = message + self.headers = headers \ No newline at end of file diff --git a/src/main.py b/src/main.py new file mode 100644 index 00000000..825bd234 --- /dev/null +++ b/src/main.py @@ -0,0 +1,509 @@ +from datetime import datetime, timedelta, timezone +from uuid import UUID, uuid4 +import socket +import os +import re + +from fastapi import Depends, FastAPI, Request, Header +from fastapi.exceptions import RequestValidationError +from fastapi.responses import JSONResponse +from fastapi.security import ( + HTTPAuthorizationCredentials, + HTTPBasic, + HTTPBasicCredentials, + HTTPBearer, +) +from sqlalchemy.exc import IntegrityError, SQLAlchemyError +from sqlalchemy.ext.asyncio import AsyncSession +from starlette.exceptions import HTTPException as StarletteHTTPException + +from src.database import get_db_session, ping_database +from src.errors import APIError +from src.models import CreateUser, SessionResponse, User, CreateChat, Chat, ChatPage, CreateMessage, Message, MessagePage +from src.repositories import ( + create_chat, + list_chats, + create_user as create_user_in_db, + get_user_by_id, + get_chat_by_id, + get_user_credentials_by_username, + create_message, + get_messages, +) +from src.security import verify_password +from src.sessions import ( + create_session, + delete_session, + get_session_user_id, + ping_session_store, +) + + +app = FastAPI() +INSTANCE_ID = os.getenv("INSTANCE_ID") or str(uuid4()) + +basic_security = HTTPBasic(auto_error=False) +bearer_security = HTTPBearer(auto_error=False) + + +async def get_current_user( + credentials: HTTPAuthorizationCredentials | None = Depends(bearer_security), + session: AsyncSession = Depends(get_db_session), +) -> User: + if credentials is None or credentials.scheme.lower() != "bearer": + raise APIError( + status_code=401, + code="unauthorized", + message="Unauthorized", + headers={ + "WWW-Authenticate": "Bearer", + }, + ) + + user_id = await get_session_user_id(credentials.credentials) + + if user_id is None: + raise APIError( + status_code=401, + code="unauthorized", + message="Unauthorized", + headers={ + "WWW-Authenticate": "Bearer", + }, + ) + + user = await get_user_by_id(session, user_id) + + if user is None: + raise APIError( + status_code=401, + code="unauthorized", + message="Unauthorized", + headers={ + "WWW-Authenticate": "Bearer", + }, + ) + + return user + + +@app.middleware("http") +async def request_middleware(request: Request, call_next): + request_id = str(uuid4()) + request.state.request_id = request_id + + if request.method in {"POST", "PUT", "PATCH"}: + content_type = request.headers.get("content-type") + + if content_type is not None: + media_type = content_type.split(";", 1)[0].strip().lower() + + if media_type != "application/json": + return JSONResponse( + status_code=415, + content={ + "error": { + "code": "unsupported_media_type", + "message": "Unsupported media type", + "request_id": request_id, + } + }, + headers={ + "X-Request-Id": request_id, + "X-Instance-Id": INSTANCE_ID, + }, + ) + + response = await call_next(request) + + response.headers["X-Request-Id"] = request_id + response.headers["X-Instance-Id"] = INSTANCE_ID + + return response + + +@app.exception_handler(RequestValidationError) +async def validation_exception_handler( + request: Request, + exc: RequestValidationError, +): + request_id = request.state.request_id + + return JSONResponse( + status_code=400, + content={ + "error": { + "code": "invalid_request", + "message": "Invalid request", + "request_id": request_id, + } + }, + ) + + +@app.exception_handler(APIError) +async def api_error_handler(request: Request, exc: APIError): + request_id = request.state.request_id + + return JSONResponse( + status_code=exc.status_code, + content={ + "error": { + "code": exc.code, + "message": exc.message, + "request_id": request_id, + } + }, + headers=exc.headers, + ) + + +@app.exception_handler(SQLAlchemyError) +async def database_exception_handler( + request: Request, + exc: SQLAlchemyError, +): + request_id = request.state.request_id + + return JSONResponse( + status_code=503, + content={ + "error": { + "code": "service_unavailable", + "message": "Database unavailable", + "request_id": request_id, + } + }, + ) + + +@app.exception_handler(OSError) +async def database_connection_exception_handler( + request: Request, + exc: OSError, +): + request_id = request.state.request_id + + return JSONResponse( + status_code=503, + content={ + "error": { + "code": "service_unavailable", + "message": "Database unavailable", + "request_id": request_id, + } + }, + ) + + +@app.exception_handler(StarletteHTTPException) +async def http_exception_handler( + request: Request, + exc: StarletteHTTPException, +): + request_id = request.state.request_id + + if exc.status_code == 404: + code = "not_found" + message = "Not found" + else: + code = "invalid_request" + message = "Invalid request" + + return JSONResponse( + status_code=exc.status_code, + content={ + "error": { + "code": code, + "message": message, + "request_id": request_id, + } + }, + headers=exc.headers, + ) + + +@app.exception_handler(Exception) +async def unexpected_exception_handler( + request: Request, + exc: Exception, +): + request_id = request.state.request_id + + return JSONResponse( + status_code=500, + content={ + "error": { + "code": "internal_error", + "message": "Internal server error", + "request_id": request_id, + } + }, + ) + + +@app.get("/health/live") +async def health_live(): + return {"status": "ok"} + + +@app.get("/health/ready") +async def health_ready(): + database_ready = await ping_database() + session_store_ready = await ping_session_store() + + if not database_ready or not session_store_ready: + raise APIError( + status_code=503, + code="service_unavailable", + message="Service dependencies unavailable", + ) + + return {"status": "ok"} + + +@app.post("/api/v1/users", response_model=User, status_code=201) +async def create_user( + data: CreateUser, + session: AsyncSession = Depends(get_db_session), +): + try: + async with session.begin(): + return await create_user_in_db(session, data) + + except IntegrityError as exc: + raise APIError( + status_code=409, + code="conflict", + message="Username already exists", + ) from exc + + +@app.get("/api/v1/users/me", response_model=User) +async def get_me( + current_user: User = Depends(get_current_user), +): + return current_user + + +@app.post( + "/api/v1/auth/sessions", + response_model=SessionResponse, + status_code=201, +) +async def create_auth_session( + credentials: HTTPBasicCredentials | None = Depends(basic_security), + session: AsyncSession = Depends(get_db_session), +): + if credentials is None: + raise APIError( + status_code=401, + code="unauthorized", + message="Unauthorized", + headers={ + "WWW-Authenticate": 'Basic realm="messenger"', + }, + ) + + user_credentials = await get_user_credentials_by_username( + session, + credentials.username, + ) + + if user_credentials is None: + raise APIError( + status_code=401, + code="unauthorized", + message="Unauthorized", + headers={ + "WWW-Authenticate": 'Basic realm="messenger"', + }, + ) + + if not verify_password( + user_credentials.password_hash, + credentials.password, + ): + raise APIError( + status_code=401, + code="unauthorized", + message="Unauthorized", + headers={ + "WWW-Authenticate": 'Basic realm="messenger"', + }, + ) + + user = User( + id=user_credentials.id, + username=user_credentials.username, + display_name=user_credentials.display_name, + created_at=user_credentials.created_at, + ) + + token, ttl = await create_session(user.id) + + expires_at = datetime.now(timezone.utc) + timedelta(seconds=ttl) + + return SessionResponse( + token=token, + token_type="Bearer", + expires_at=expires_at, + user=user, + ) + + +@app.delete( + "/api/v1/auth/sessions/current", + status_code=204, +) +async def delete_current_session( + credentials: HTTPAuthorizationCredentials | None = Depends(bearer_security), + current_user: User = Depends(get_current_user), +): + await delete_session(credentials.credentials) + + +@app.post( + "/api/v1/chats", + response_model=Chat, + status_code=201, +) +async def create_chat_endpoint( + data: CreateChat, + current_user: User = Depends(get_current_user), + session: AsyncSession = Depends(get_db_session), +) -> Chat: + try: + chat = await create_chat( + session=session, + data=data, + current_user_id=current_user.id, + ) + await session.commit() + except Exception: + await session.rollback() + raise + + return chat + + +@app.get( + "/api/v1/chats", + response_model=ChatPage, +) +async def get_chats( + limit: int = 50, + current_user: User = Depends(get_current_user), + session: AsyncSession = Depends(get_db_session), +) -> ChatPage: + if limit < 1 or limit > 100: + raise APIError( + status_code=400, + code="invalid_request", + message="Limit must be between 1 and 100", + ) + + return await list_chats( + session=session, + current_user_id=current_user.id, + limit=limit, + ) + + +@app.get( + "/api/v1/chats/{chat_id}", + response_model=Chat, +) +async def get_chat_endpoint( + chat_id: UUID, + current_user: User = Depends(get_current_user), + session: AsyncSession = Depends(get_db_session), +) -> Chat: + chat = await get_chat_by_id( + session=session, + chat_id=chat_id, + current_user_id=current_user.id, + ) + + if chat is None: + raise APIError( + status_code=404, + code="not_found", + message="Chat not found", + ) + + return chat + + +@app.post( + "/api/v1/chats/{chat_id}/messages", + response_model=Message, + status_code=201, +) +async def create_message_endpoint( + chat_id: UUID, + data: CreateMessage, + idempotency_key: str | None = Header( + default=None, + alias="Idempotency-Key", + ), + current_user: User = Depends(get_current_user), + session: AsyncSession = Depends(get_db_session), +) -> Message: + + if ( + idempotency_key is None + or re.fullmatch( + r"[A-Za-z0-9._:-]{1,128}", + idempotency_key, + ) is None + ): + raise APIError( + status_code=400, + code="invalid_request", + message="Invalid or missing Idempotency-Key", + ) + + try: + message = await create_message( + session=session, + chat_id=chat_id, + current_user_id=current_user.id, + data=data, + idempotency_key=idempotency_key, + ) + + await session.commit() + + except Exception: + await session.rollback() + raise + + return message + + +@app.get( + "/api/v1/chats/{chat_id}/messages", + response_model=MessagePage, +) +async def get_messages_endpoint( + chat_id: UUID, + limit: int = 50, + cursor: str | None = None, + current_user: User = Depends(get_current_user), + session: AsyncSession = Depends(get_db_session), +) -> MessagePage: + if limit < 1 or limit > 100: + raise APIError( + status_code=400, + code="invalid_request", + message="Limit must be between 1 and 100", + ) + + return await get_messages( + session=session, + chat_id=chat_id, + current_user_id=current_user.id, + limit=limit, + cursor=cursor, + ) \ No newline at end of file diff --git a/src/models.py b/src/models.py new file mode 100644 index 00000000..5f2c2991 --- /dev/null +++ b/src/models.py @@ -0,0 +1,118 @@ +from datetime import datetime +from uuid import UUID + +from pydantic import BaseModel, ConfigDict, Field, field_validator + + +class CreateUser(BaseModel): + model_config = ConfigDict(extra="forbid") + + 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") + @classmethod + def validate_display_name(cls, value: str) -> str: + if not value.strip(): + raise ValueError("Display name must not be blank") + + return value + + +class User(BaseModel): + id: UUID + username: str + display_name: str + created_at: datetime + + +class SessionResponse(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("Chat title must not be blank") + + return value + + @field_validator("member_ids") + @classmethod + def validate_member_ids(cls, value: list[UUID]) -> list[UUID]: + if len(value) != len(set(value)): + raise ValueError("Member IDs must be unique") + + 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( + min_length=1, + max_length=4000, + ) + + @field_validator("text") + @classmethod + def validate_text(cls, value: str) -> str: + if not value.strip(): + raise ValueError("Message text must not be blank") + + return value + + +class Message(BaseModel): + id: UUID + chat_id: UUID + sender_id: UUID + text: str + created_at: datetime + + +class MessagePage(BaseModel): + items: list[Message] + next_cursor: str | None \ No newline at end of file diff --git a/src/repositories.py b/src/repositories.py new file mode 100644 index 00000000..f7b6c335 --- /dev/null +++ b/src/repositories.py @@ -0,0 +1,646 @@ +from uuid import UUID, uuid4 +import hashlib +import json + +from sqlalchemy import text +from sqlalchemy.ext.asyncio import AsyncSession + +from src.models import Chat, ChatPage, CreateChat, CreateUser, User, CreateMessage, Message, MessagePage +from src.security import hash_password +from src.errors import APIError +from src.cursor import decode_cursor, encode_cursor + + +async def create_user( + session: AsyncSession, + data: CreateUser, +) -> User: + user_id = uuid4() + password_hash = hash_password(data.password) + + result = await session.execute( + text( + """ + INSERT INTO users ( + id, + username, + display_name, + password_hash + ) + VALUES ( + :id, + :username, + :display_name, + :password_hash + ) + RETURNING + id, + username, + display_name, + created_at + """ + ), + { + "id": user_id, + "username": data.username, + "display_name": data.display_name, + "password_hash": password_hash, + }, + ) + + row = result.one() + + return User( + id=UUID(str(row.id)), + username=row.username, + display_name=row.display_name, + created_at=row.created_at, + ) + +async def get_user_credentials_by_username( + session: AsyncSession, + username: str, +): + result = await session.execute( + text( + """ + SELECT + id, + username, + display_name, + password_hash, + created_at + FROM users + WHERE username = :username + """ + ), + { + "username": username, + }, + ) + + return result.one_or_none() + + +async def get_user_by_id( + session: AsyncSession, + user_id: UUID, +) -> User | None: + result = await session.execute( + text( + """ + SELECT + id, + username, + display_name, + created_at + FROM users + WHERE id = :user_id + """ + ), + { + "user_id": user_id, + }, + ) + + row = result.one_or_none() + + if row is None: + return None + + return User( + id=UUID(str(row.id)), + username=row.username, + display_name=row.display_name, + created_at=row.created_at, + ) + + +async def create_chat( + session: AsyncSession, + data: CreateChat, + current_user_id: UUID, +) -> Chat: + if current_user_id not in data.member_ids: + raise APIError( + status_code=400, + code="invalid_request", + message="Current user must be a chat member", + ) + + users_result = await session.execute( + text( + """ + SELECT id + FROM users + WHERE id = ANY(:member_ids) + """ + ), + { + "member_ids": data.member_ids, + }, + ) + + existing_user_ids = { + UUID(str(row.id)) + for row in users_result + } + + if existing_user_ids != set(data.member_ids): + raise APIError( + status_code=400, + code="invalid_request", + message="Unknown chat member", + ) + + chat_id = uuid4() + + chat_result = await session.execute( + text( + """ + INSERT INTO chats ( + id, + title + ) + VALUES ( + :id, + :title + ) + RETURNING + id, + title, + created_at + """ + ), + { + "id": chat_id, + "title": data.title, + }, + ) + + chat_row = chat_result.one() + + for member_id in data.member_ids: + await session.execute( + text( + """ + INSERT INTO chat_members ( + chat_id, + user_id + ) + VALUES ( + :chat_id, + :user_id + ) + """ + ), + { + "chat_id": chat_id, + "user_id": member_id, + }, + ) + + return Chat( + id=UUID(str(chat_row.id)), + title=chat_row.title, + member_ids=data.member_ids, + created_at=chat_row.created_at, + ) + + +async def get_chat_by_id( + session: AsyncSession, + chat_id: UUID, + current_user_id: UUID, +) -> Chat | None: + chat_result = await session.execute( + text( + """ + SELECT + c.id, + c.title, + c.created_at + FROM chats AS c + JOIN chat_members AS cm + ON cm.chat_id = c.id + WHERE c.id = :chat_id + AND cm.user_id = :current_user_id + """ + ), + { + "chat_id": chat_id, + "current_user_id": current_user_id, + }, + ) + + chat_row = chat_result.one_or_none() + + if chat_row is None: + return None + + members_result = await session.execute( + text( + """ + SELECT user_id + FROM chat_members + WHERE chat_id = :chat_id + ORDER BY user_id + """ + ), + { + "chat_id": chat_id, + }, + ) + + member_ids = [ + UUID(str(row.user_id)) + for row in members_result + ] + + return Chat( + id=UUID(str(chat_row.id)), + title=chat_row.title, + member_ids=member_ids, + created_at=chat_row.created_at, + ) + + +async def list_chats( + session: AsyncSession, + current_user_id: UUID, + limit: int, +) -> ChatPage: + chats_result = await session.execute( + text( + """ + SELECT + c.id, + c.title, + c.created_at + FROM chats AS c + JOIN chat_members AS cm + ON cm.chat_id = c.id + WHERE cm.user_id = :current_user_id + ORDER BY c.created_at, c.id + LIMIT :limit + """ + ), + { + "current_user_id": current_user_id, + "limit": limit, + }, + ) + + chat_rows = chats_result.all() + + items = [] + + for chat_row in chat_rows: + members_result = await session.execute( + text( + """ + SELECT user_id + FROM chat_members + WHERE chat_id = :chat_id + ORDER BY user_id + """ + ), + { + "chat_id": chat_row.id, + }, + ) + + member_ids = [ + UUID(str(row.user_id)) + for row in members_result + ] + + items.append( + Chat( + id=UUID(str(chat_row.id)), + title=chat_row.title, + member_ids=member_ids, + created_at=chat_row.created_at, + ) + ) + + return ChatPage( + items=items, + next_cursor=None, + ) + + +async def create_message( + session: AsyncSession, + chat_id: UUID, + current_user_id: UUID, + data: CreateMessage, + idempotency_key: str, +) -> Message: + membership_result = await session.execute( + text(""" + SELECT 1 + FROM chat_members + WHERE chat_id = :chat_id + AND user_id = :user_id + """), + { + "chat_id": chat_id, + "user_id": current_user_id, + }, + ) + + if membership_result.scalar_one_or_none() is None: + raise APIError( + status_code=404, + code="not_found", + message="Chat not found", + ) + + canonical_body = json.dumps( + {"text": data.text}, + sort_keys=True, + ensure_ascii=False, + separators=(",", ":"), + ) + + request_hash = hashlib.sha256( + canonical_body.encode("utf-8") + ).hexdigest() + + scope = ( + f"{current_user_id}:POST:" + f"/api/v1/chats/{chat_id}/messages:" + f"{idempotency_key}" + ) + + await session.execute( + text(""" + SELECT pg_advisory_xact_lock( + hashtextextended(:scope, 0) + ) + """), + {"scope": scope}, + ) + + await session.execute( + text(""" + DELETE FROM message_idempotency + WHERE user_id = :user_id + AND chat_id = :chat_id + AND key = :key + AND created_at <= + clock_timestamp() - INTERVAL '24 hours' + """), + { + "user_id": current_user_id, + "chat_id": chat_id, + "key": idempotency_key, + }, + ) + + existing_result = await session.execute( + text(""" + SELECT + i.request_hash, + m.id, + m.chat_id, + m.sender_id, + m.text, + m.created_at + FROM message_idempotency AS i + JOIN messages AS m + ON m.id = i.message_id + WHERE i.user_id = :user_id + AND i.chat_id = :chat_id + AND i.key = :key + """), + { + "user_id": current_user_id, + "chat_id": chat_id, + "key": idempotency_key, + }, + ) + + existing = existing_result.one_or_none() + + if existing is not None: + if existing.request_hash != request_hash: + raise APIError( + status_code=409, + code="conflict", + message=( + "Idempotency key already used " + "with different request body" + ), + ) + + return Message( + id=UUID(str(existing.id)), + chat_id=UUID(str(existing.chat_id)), + sender_id=UUID(str(existing.sender_id)), + text=existing.text, + created_at=existing.created_at, + ) + + message_id = uuid4() + + message_result = await session.execute( + text(""" + INSERT INTO messages ( + id, + chat_id, + sender_id, + text + ) + VALUES ( + :id, + :chat_id, + :sender_id, + :text + ) + RETURNING + id, + chat_id, + sender_id, + text, + created_at + """), + { + "id": message_id, + "chat_id": chat_id, + "sender_id": current_user_id, + "text": data.text, + }, + ) + + row = message_result.one() + + await session.execute( + text(""" + INSERT INTO message_idempotency ( + user_id, + chat_id, + key, + request_hash, + message_id + ) + VALUES ( + :user_id, + :chat_id, + :key, + :request_hash, + :message_id + ) + """), + { + "user_id": current_user_id, + "chat_id": chat_id, + "key": idempotency_key, + "request_hash": request_hash, + "message_id": message_id, + }, + ) + + return Message( + id=UUID(str(row.id)), + chat_id=UUID(str(row.chat_id)), + sender_id=UUID(str(row.sender_id)), + text=row.text, + created_at=row.created_at, + ) + + +async def get_messages( + session: AsyncSession, + chat_id: UUID, + current_user_id: UUID, + limit: int, + cursor: str | None, +) -> MessagePage: + membership_result = await session.execute( + text( + """ + SELECT 1 + FROM chat_members + WHERE chat_id = :chat_id + AND user_id = :current_user_id + """ + ), + { + "chat_id": chat_id, + "current_user_id": current_user_id, + }, + ) + + if membership_result.scalar_one_or_none() is None: + raise APIError( + status_code=404, + code="not_found", + message="Chat not found", + ) + + cursor_created_at = None + cursor_message_id = None + + if cursor is not None: + try: + ( + cursor_chat_id, + cursor_created_at, + cursor_message_id, + ) = decode_cursor(cursor) + except ValueError: + raise APIError( + status_code=400, + code="invalid_request", + message="Invalid cursor", + ) + + if cursor_chat_id != chat_id: + raise APIError( + status_code=400, + code="invalid_request", + message="Cursor belongs to another chat", + ) + + if cursor is None: + messages_result = await session.execute( + text( + """ + SELECT + id, + chat_id, + sender_id, + text, + created_at + FROM messages + WHERE chat_id = :chat_id + ORDER BY created_at, id + LIMIT :fetch_limit + """ + ), + { + "chat_id": chat_id, + "fetch_limit": limit + 1, + }, + ) + else: + messages_result = await session.execute( + text( + """ + SELECT + id, + chat_id, + sender_id, + text, + created_at + FROM messages + WHERE chat_id = :chat_id + AND (created_at, id) > ( + :cursor_created_at, + :cursor_message_id + ) + ORDER BY created_at, id + LIMIT :fetch_limit + """ + ), + { + "chat_id": chat_id, + "cursor_created_at": cursor_created_at, + "cursor_message_id": cursor_message_id, + "fetch_limit": limit + 1, + }, + ) + + rows = messages_result.all() + + has_more = len(rows) > limit + rows = rows[:limit] + + items = [ + Message( + id=UUID(str(row.id)), + chat_id=UUID(str(row.chat_id)), + sender_id=UUID(str(row.sender_id)), + text=row.text, + created_at=row.created_at, + ) + for row in rows + ] + + next_cursor = None + + if has_more: + last_message = items[-1] + + next_cursor = encode_cursor( + chat_id=chat_id, + created_at=last_message.created_at, + message_id=last_message.id, + ) + + return MessagePage( + items=items, + next_cursor=next_cursor, + ) \ No newline at end of file diff --git a/src/security.py b/src/security.py new file mode 100644 index 00000000..0afbbf1a --- /dev/null +++ b/src/security.py @@ -0,0 +1,16 @@ +from argon2 import PasswordHasher +from argon2.exceptions import VerifyMismatchError + + +password_hasher = PasswordHasher() + + +def hash_password(password: str) -> str: + return password_hasher.hash(password) + + +def verify_password(password_hash: str, password: str) -> bool: + try: + return password_hasher.verify(password_hash, password) + except VerifyMismatchError: + return False \ No newline at end of file diff --git a/src/sessions.py b/src/sessions.py new file mode 100644 index 00000000..45339a25 --- /dev/null +++ b/src/sessions.py @@ -0,0 +1,87 @@ +import os +import secrets +from uuid import UUID + +from redis.asyncio import Redis +from redis.exceptions import RedisError + +from src.errors import APIError + + +CACHE_URL_FILE = "/run/secrets/cache_url" +DEFAULT_SESSION_TTL_SECONDS = 3600 + + +def read_cache_url() -> str: + with open(CACHE_URL_FILE, encoding="utf-8") as file: + return file.read().strip() + + +def get_session_ttl_seconds() -> int: + return int( + os.getenv( + "SESSION_TTL_SECONDS", + str(DEFAULT_SESSION_TTL_SECONDS), + ) + ) + + +redis_client = Redis.from_url( + read_cache_url(), + decode_responses=True, + socket_connect_timeout=5, + socket_timeout=5, +) + + +def session_store_unavailable() -> APIError: + return APIError( + status_code=503, + code="service_unavailable", + message="Session store unavailable", + ) + + +async def create_session(user_id: UUID) -> tuple[str, int]: + token = secrets.token_urlsafe(32) + ttl = get_session_ttl_seconds() + + try: + await redis_client.set( + f"session:{token}", + str(user_id), + ex=ttl, + ) + except RedisError as exc: + raise session_store_unavailable() from exc + + return token, ttl + + +async def get_session_user_id(token: str) -> UUID | None: + try: + value = await redis_client.get(f"session:{token}") + except RedisError as exc: + raise session_store_unavailable() from exc + + if value is None: + return None + + try: + return UUID(value) + except ValueError: + return None + + +async def delete_session(token: str) -> None: + try: + await redis_client.delete(f"session:{token}") + except RedisError as exc: + raise session_store_unavailable() from exc + + +async def ping_session_store() -> bool: + try: + return bool(await redis_client.ping()) + except RedisError: + return False \ No newline at end of file diff --git a/tests/test_concurrent_idempotency.py b/tests/test_concurrent_idempotency.py new file mode 100644 index 00000000..945eb2f5 --- /dev/null +++ b/tests/test_concurrent_idempotency.py @@ -0,0 +1,46 @@ +import concurrent.futures +import uuid + +from client import Client, check + + +def main(): + client = Client("http://localhost:8080") + alice, bob, eve, chat = client.bootstrap() + + key = uuid.uuid4().hex + text = "Concurrent idempotency " + key + + def send(_): + return client.message(alice, chat, text, key=key) + + with concurrent.futures.ThreadPoolExecutor(max_workers=20) as executor: + results = list(executor.map(send, range(20))) + + message_ids = {result["json"]["id"] for result in results} + instances = {result["headers"]["X-Instance-Id"] for result in results} + + page = client.call( + "GET", + "/api/v1/chats/" + chat["id"] + "/messages?limit=100", + actor=alice, + schema="MessagePage", + )["json"] + + matches = [message for message in page["items"] if message["text"] == text] + + print("Requests:", len(results)) + print("Unique message IDs:", len(message_ids)) + print("Instances:", sorted(instances)) + print("Messages in history:", len(matches)) + + check(len(message_ids) == 1, "duplicate message IDs returned") + check(len(instances) >= 2, "requests did not reach both replicas") + check(len(matches) == 1, "duplicate messages found in history") + check(matches[0]["id"] in message_ids, "history message ID differs") + + print("PASS: concurrent idempotency across replicas") + + +if __name__ == "__main__": + main() \ No newline at end of file