diff --git a/.dockerignore b/.dockerignore index bc16e556..a4374233 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1,20 +1,50 @@ +# VCS / CI .git .github + +# Secrets and local env .env .env.* !.env.example secrets certs -volumes -data -backups *.key *.pem *.p12 *.pfx *.jks + +# Local / runtime junk .venv +venv __pycache__ +*.py[cod] +*.egg-info +.pytest_cache +.mypy_cache +.ruff_cache node_modules -evidence .DS_Store +Thumbs.db + +# Lab artifacts not needed at runtime +evidence +volumes +data +backups +docs +student-tests +tests +scripts +examples +contracts +REPORT.md +README.md +COURSE.md +CONTRIBUTING.md +MAINTAINERS.md +Dockerfile.example +requirements-dev.txt +*.md + +# Keep only what the image needs: requirements.txt + src/ diff --git a/.env.example b/.env.example index 714c8076..aa5855bc 100644 --- a/.env.example +++ b/.env.example @@ -1,11 +1,9 @@ # Copy to .env for your LOCAL synthetic test environment; never commit .env. -# These are logical interface suggestions; map actual framework names in REPORT.md. APP_HOST=0.0.0.0 APP_PORT=8080 APP_ENV=lab SESSION_TTL_SECONDS=3600 -# Choose credentials locally. File paths contain no secret values. +# Secrets are mounted as Docker Compose files under ./secrets/ (not committed). DATABASE_URL_FILE=/run/secrets/database_url -# Optional on 3, required use on 4 in lab2; Redis OR Valkey: -SESSION_STORE=postgres +SESSION_STORE=redis CACHE_URL_FILE=/run/secrets/cache_url diff --git a/.github/pull_request_template.md b/.github/pull_request_template.md index 8ceefa10..989d2440 100644 --- a/.github/pull_request_template.md +++ b/.github/pull_request_template.md @@ -1,18 +1,18 @@ ## Лабораторная 2 -Автор / группа: TODO -Целевая оценка (3/4/5): TODO -Трек 5, если заявлен: TODO +Автор / группа: Болотов Илья / БВТ2303 +Целевая оценка (3/4/5): 4 +Трек 5, если заявлен: — -Что реализовано и что перенесено: TODO -Команды запуска из чистого clone: TODO -Результаты проверок и ссылка на REPORT.md: TODO -Ограничения / известные ошибки: TODO -Источники и использование ИИ: TODO +Что реализовано и что перенесено: минимум lab1 (FastAPI messenger) перенесён из `mtusi2026lab1-fork` ветки `solution/bvt2303-bolotov`; PostgreSQL + миграции, Argon2id, Basic→Bearer (Redis AOF), Compose, `scripts/restart.sh`, student-tests, evidence. +Команды запуска из чистого clone: см. `REPORT.md` (secrets + `docker compose up -d --build`, затем smoke / persistence). +Результаты проверок и ссылка на REPORT.md: `REPORT.md`; `evidence/check.txt`, `smoke.txt`, `persistence.txt`, `student-tests.txt` (в т.ч. deps + expiry TTL=2), `explain.txt` (Index Scan, 10 000). +Ограничения / известные ошибки: целевая оценка 4 (без трека 5); сессии теряются при удалении volume Redis; один Docker-host; Postgres на `127.0.0.1:5432` только для локального IDE. +Источники и использование ИИ: FastAPI, psycopg, redis-py, argon2-cffi; ИИ при переносе и реализации lab2. -- [ ] Весь обязательный минимум текущей и предыдущих работ сохранён с объявленными переходами. -- [ ] Контракт и публичные тесты не ослаблены. -- [ ] make check и make test выполнены; приложен реальный вывод. -- [ ] Сценарии отказов/безопасности своего уровня воспроизведены. -- [ ] Все секреты и приватные ключи находятся вне Git; отчёт не содержит tokens. -- [ ] README задания сохранён, REPORT.md содержит инструкцию именно моего решения. +- [x] Весь обязательный минимум текущей и предыдущих работ сохранён с объявленными переходами. +- [x] Контракт и публичные тесты не ослаблены. +- [x] make check и make test выполнены; приложен реальный вывод. +- [x] Сценарии отказов/безопасности своего уровня воспроизведены. +- [x] Все секреты и приватные ключи находятся вне Git; отчёт не содержит tokens. +- [x] README задания сохранён, REPORT.md содержит инструкцию именно моего решения. diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..13215f08 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,30 @@ +# Pinned base image (lab2). Update digest when bumping Python. +FROM python:3.12.8-slim-bookworm AS runtime + +ENV PYTHONDONTWRITEBYTECODE=1 \ + PYTHONUNBUFFERED=1 \ + APP_HOST=0.0.0.0 \ + APP_PORT=8080 \ + PYTHONPATH=/app/src \ + MIGRATIONS_DIR=/app/migrations + +WORKDIR /app + +RUN groupadd --system --gid 10001 app \ + && useradd --system --uid 10001 --gid app --home /app --shell /usr/sbin/nologin app + +COPY --chown=app:app requirements.txt . +RUN pip install --no-cache-dir --upgrade pip==24.3.1 \ + && pip install --no-cache-dir --retries 10 --timeout 60 -r requirements.txt + +COPY --chown=app:app src ./src +COPY --chown=app:app migrations ./migrations + +USER app + +EXPOSE 8080 + +HEALTHCHECK --interval=5s --timeout=3s --start-period=15s --retries=5 \ + CMD python -c "import urllib.request; urllib.request.urlopen('http://127.0.0.1:8080/health/ready', timeout=2)" + +CMD ["python", "-m", "messenger"] diff --git a/Makefile b/Makefile index 9ecb223e..937ec4aa 100644 --- a/Makefile +++ b/Makefile @@ -4,10 +4,10 @@ CA_FILE ?= ACTION_SCRIPT ?= TLS_ARGS = $(if $(CA_FILE),--ca-file "$(CA_FILE)",) -.PHONY: help check validate test load test-persistence +.PHONY: help check validate test load test-persistence student-test help: - @echo "check: offline structure; validate: full OpenAPI (requirements-dev.txt); test: running API; load: synthetic writes" + @echo "check: offline structure; validate: full OpenAPI (requirements-dev.txt); test: running API; load: synthetic writes; student-test: own suite" check: $(PYTHON) scripts/check_contract.py @@ -24,3 +24,6 @@ load: test-persistence: @test -n "$(ACTION_SCRIPT)" || (echo "Set ACTION_SCRIPT to your implemented executable restart hook"; exit 2) $(PYTHON) tests/resilience.py --mode persistence --action-script "$(ACTION_SCRIPT)" --base-url "$(BASE_URL)" $(TLS_ARGS) + +student-test: + $(PYTHON) student-tests/run_all.py diff --git a/REPORT.md b/REPORT.md index e42f2b05..0222cad8 100644 --- a/REPORT.md +++ b/REPORT.md @@ -1,37 +1,91 @@ # Отчёт — лабораторная 2 -- Автор, группа: TODO -- Целевая оценка; выбранный трек 5, если нужен: TODO -- Commit/PR: TODO после публикации -- Использованные источники, библиотеки и помощь ИИ: TODO +- Автор, группа: Болотов Илья, БВТ2303 +- Целевая оценка; выбранный трек 5, если нужен: **4** (трек 5 не выбран) +- Ветка: `solution/bvt2303-bolotov` +- Использованные источники, библиотеки и помощь ИИ: FastAPI, Uvicorn, psycopg3 (+pool), redis-py, argon2-cffi; перенос и адаптация lab1 с помощью ИИ; публичные материалы курса (README, contracts). ## Запуск из чистого clone -TODO: версии runtime/образов, prerequisites, команды создания локальных secrets/.env, build, миграций, запуска, ожидания готовности, тестов и остановки. Указать необходимые действия оператора. Не вставлять секреты. Отдельно указать безопасную остановку и команду намеренного удаления учебных данных. +**Prerequisites:** Docker Desktop (Engine), Docker Compose v2+. Локальный Python на хосте не обязателен для стенда (smoke можно запускать из контейнера `python:3.12-slim`). + +```sh +cp .env.example .env +docker compose --env-file .env config --quiet +docker compose --env-file .env up -d --build +# Дождаться healthy у api (или GET /health/ready → 200) +``` + +Проверки (с хоста, если есть Python 3.9+): + +```sh +python -m pip install -r requirements-dev.txt # только для make validate +make check +make test BASE_URL=http://127.0.0.1:8080 +# persistence (Linux/macOS; на Windows — Git Bash / WSL, chmod +x): +chmod +x scripts/restart.sh +make test-persistence ACTION_SCRIPT=scripts/restart.sh BASE_URL=http://127.0.0.1:8080 +``` + +Без локального Python — smoke из контейнера: + +```sh +docker run --rm --network host -v "$PWD:/work" -w /work python:3.12.8-slim-bookworm \ + python tests/smoke.py --base-url http://127.0.0.1:8080 +``` + +На Windows Docker Desktop вместо `--network host` используйте `host.docker.internal`: + +```powershell +docker run --rm -v "${PWD}:/work" -w /work python:3.12.8-slim-bookworm ` + python tests/smoke.py --base-url http://host.docker.internal:8080 +``` + +Остановка без удаления данных: `docker compose --env-file .env down` +Удаление учебных volumes: `docker compose --env-file .env down -v` (осознанно). + +Версии образов: `python:3.12.8-slim-bookworm`, `postgres:16.6-bookworm`, `redis:7.4.2-bookworm`. ## Архитектура и решения -TODO: схема процессов/хранилищ, источник истины, транзакционные границы, ограничения. Что перенесено из lab1, что изменено в этой работе. Соответствие переменных из .env.example реальной конфигурации. +``` +Клиент → API (FastAPI/Uvicorn) → PostgreSQL (источник истины: users/chats/members/messages) + ↘ Redis (opaque sessions + TTL, AOF everysec) +``` + +- **Перенесено из lab1** (`mtusi2026lab1-fork`, ветка `solution/bvt2303-bolotov`): структура `src/messenger`, стиль ошибок/cursor, non-root Dockerfile, student-tests каркас. +- **Изменено в lab2:** in-memory store → PostgreSQL; `X-User-Id` → Bearer; регистрация с password (Argon2id); `POST /auth/sessions` (Basic); logout; Compose + volumes; миграции `migrations/001_init.sql`. +- **Сессии:** единственное хранилище — Redis (`sess:`), TTL = `SESSION_TTL_SECONDS` (по умолчанию 3600). Persistence — AOF (`appendonly yes`, `appendfsync everysec`), чтобы действующие сессии переживали `compose restart`. Источник истины для домена — PostgreSQL; Redis — сессии. +- **Транзакции:** создание чата + members в одной транзакции; при неизвестном member — rollback. Уникальность `username` — UNIQUE + 409 на гонке. +- **Пул:** psycopg_pool max 10, `connect_timeout=5`, `statement_timeout=5000`. +- **Индекс истории:** `idx_messages_chat_created_id ON messages (chat_id, created_at, id)` — keyset `(created_at, id)`. +- **Конфиг:** URL хранилищ через Docker secrets files; `SESSION_TTL_SECONDS` из `.env`. Postgres на стенде доступен с хоста на `127.0.0.1:5432` (loopback) для локального IDE. ## Доказательства критериев | Критерий README | Команда/сценарий | Наблюдаемый результат | Файл доказательства | | --- | --- | --- | --- | -| Публичный smoke | make test | TODO | TODO | -| Остальные пункты уровня 3 | TODO | TODO | TODO | -| Все пункты уровня 4, если заявлен | TODO | TODO | TODO | -| Выбранный трек 5, если заявлен | TODO | TODO | TODO | - -TODO: добавить отдельную строку для каждого заявленного критерия. PASS без команды и наблюдения не является доказательством. Проверка инфраструктурного hook требует подтверждения, какие реальные процессы/хранилища перезапущены. +| Offline check | `python scripts/check_contract.py` | PASS skeleton | `evidence/check.txt` | +| Публичный smoke | `python tests/smoke.py --base-url http://127.0.0.1:8080` | PASS lab2 | `evidence/smoke.txt` | +| Persistence restart | `python tests/resilience.py --mode persistence --action-script scripts/restart.cmd` (Windows) / `scripts/restart.sh` (Unix) | PASS: session + messages after restart | `evidence/persistence.txt` | +| Собственные тесты | `RUN_DEPS_TEST=1 python student-tests/run_all.py` + expiry при `SESSION_TTL_SECONDS=2` | 18 OK в основном прогоне (auth, race, deps); expiry PASS при TTL=2 | `evidence/student-tests.txt` | +| EXPLAIN индекса (≥10 000) | `evidence/seed_explain.sql` через `psql` | Index Scan using `idx_messages_chat_created_id`, SEEDED 10 000 | `evidence/explain.txt` | ## Эксперименты и отказы -TODO: гипотеза, hardware/лимиты, версии, объём данных, профиль нагрузки, concurrency/arrival rate, длительность/прогрев, повторы, успешные RPS, ошибки, p50/p95/p99, CPU/RAM. Сохранить raw summary без tokens и объяснить ограничения измерения. - | Воздействие | Что ожидаем | Что наблюдали | Время восстановления / потеря данных | | --- | --- | --- | --- | -| TODO | TODO | TODO | TODO | +| `scripts/restart.sh` (restart postgres+redis+api, volumes сохранены) | прежний token и сообщения живы | persistence-тест PASS | ≤90 с на hook + ready | +| logout | 401 на том же token | PASS в smoke | — | +| неверный пароль пользователя API | 401 + WWW-Authenticate Basic | PASS student-tests | — | +| недоступность Redis/Postgres | `/health/ready` → 503; бизнес-запросы не принимают анонима | PASS: `RUN_DEPS_TEST=1` (`test_deps.*`) | — | +| короткий TTL (`SESSION_TTL_SECONDS=2`) | 401 после истечения | PASS: `test_session_expiry_if_short_ttl` при TTL=2 | см. низ `evidence/student-tests.txt` | -## Ограничения и следующие шаги +Ограничения измерения: smoke/persistence и student-tests — функциональные проверки, не нагрузочный бенчмарк. -TODO: какие отказы выдерживаются и какие нет; что не реализовано; где учебное упрощение; какое улучшение подтверждено, а какое пока гипотеза. +## Ограничения + +- Трек 5 (cache stampede / 100k + backup) не выполнялся — целевая оценка 4. +- Один Docker-host не даёт HA при потере хоста. +- Redis — единственное хранилище сессий: без AOF сессии терялись бы при restart; с AOF стенд выдерживает проверяемый restart. +- На машине без Python 3.x smoke запускается через образ `python:3.12.8-slim-bookworm`. diff --git a/compose.yaml b/compose.yaml new file mode 100644 index 00000000..f683d90e --- /dev/null +++ b/compose.yaml @@ -0,0 +1,99 @@ +# Runnable lab2 stack: API + PostgreSQL + Redis (sessions, AOF). +services: + postgres: + image: postgres:16.6-bookworm + environment: + POSTGRES_USER: messenger + POSTGRES_PASSWORD_FILE: /run/secrets/postgres_password + POSTGRES_DB: messenger + secrets: + - postgres_password + ports: + # Local-only for IDE clients (PyCharm Database tool); not for remote exposure. + - "127.0.0.1:5432:5432" + volumes: + - pgdata:/var/lib/postgresql/data + healthcheck: + test: ["CMD-SHELL", "pg_isready -U messenger -d messenger"] + interval: 5s + timeout: 3s + retries: 10 + start_period: 10s + networks: + - labnet + + redis: + image: redis:7.4.2-bookworm + command: + - redis-server + - --appendonly + - "yes" + - --appendfsync + - everysec + - --dir + - /data + volumes: + - redisdata:/data + healthcheck: + test: ["CMD", "redis-cli", "ping"] + interval: 5s + timeout: 3s + retries: 10 + start_period: 5s + networks: + - labnet + + api: + build: + context: . + dockerfile: Dockerfile + image: mtusi-messenger:lab2 + ports: + - "127.0.0.1:8080:8080" + environment: + APP_HOST: "0.0.0.0" + APP_PORT: "8080" + APP_ENV: lab + SESSION_TTL_SECONDS: "${SESSION_TTL_SECONDS:-3600}" + SESSION_STORE: redis + DATABASE_URL_FILE: /run/secrets/database_url + CACHE_URL_FILE: /run/secrets/cache_url + MIGRATIONS_DIR: /app/migrations + secrets: + - database_url + - cache_url + depends_on: + postgres: + condition: service_healthy + redis: + condition: service_healthy + healthcheck: + test: + [ + "CMD", + "python", + "-c", + "import urllib.request; urllib.request.urlopen('http://127.0.0.1:8080/health/ready', timeout=2)", + ] + interval: 5s + timeout: 3s + retries: 12 + start_period: 20s + networks: + - labnet + +secrets: + postgres_password: + file: ./secrets/postgres_password + database_url: + file: ./secrets/database_url + cache_url: + file: ./secrets/cache_url + +volumes: + pgdata: + redisdata: + +networks: + labnet: + driver: bridge diff --git a/evidence/check.txt b/evidence/check.txt new file mode 100644 index 00000000..3e59dc98 --- /dev/null +++ b/evidence/check.txt @@ -0,0 +1 @@ +PASS: offline skeleton structure, local refs and Python syntax; application NOT tested. diff --git a/evidence/explain.sql b/evidence/explain.sql new file mode 100644 index 00000000..faa8ce10 --- /dev/null +++ b/evidence/explain.sql @@ -0,0 +1,10 @@ +-- Seed ≥10k messages then EXPLAIN the keyset history query. +-- Run: docker compose exec -T postgres psql -U messenger -d messenger -f - < evidence/explain.sql + +EXPLAIN (ANALYZE, BUFFERS) +SELECT id, chat_id, sender_id, text, created_at +FROM messages +WHERE chat_id = (SELECT id FROM chats LIMIT 1) + AND (created_at, id) > ('1970-01-01'::timestamptz, '00000000-0000-0000-0000-000000000000'::uuid) +ORDER BY created_at ASC, id ASC +LIMIT 50; diff --git a/evidence/explain.txt b/evidence/explain.txt new file mode 100644 index 00000000..f387ff9c Binary files /dev/null and b/evidence/explain.txt differ diff --git a/evidence/meta.txt b/evidence/meta.txt new file mode 100644 index 00000000..2ed5bf61 --- /dev/null +++ b/evidence/meta.txt @@ -0,0 +1,9 @@ +Docker Desktop / Engine (compose lab2 stack) +Images: python:3.12.8-slim-bookworm, postgres:16.6-bookworm, redis:7.4.2-bookworm +API image: mtusi-messenger:lab2 +Target grade: 4 +Branch: solution/bvt2303-bolotov +Tests: check PASS, smoke PASS, persistence PASS +Student-tests: RUN_DEPS_TEST=1 (18 OK) + expiry at SESSION_TTL_SECONDS=2 PASS +EXPLAIN: Index Scan on idx_messages_chat_created_id, SEEDED 10000 (evidence/explain.txt) +Postgres for IDE: 127.0.0.1:5432 (compose.yaml, loopback only) diff --git a/evidence/persistence.txt b/evidence/persistence.txt new file mode 100644 index 00000000..044a7fde --- /dev/null +++ b/evidence/persistence.txt @@ -0,0 +1,2 @@ +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/seed_explain.sql b/evidence/seed_explain.sql new file mode 100644 index 00000000..435e27e0 --- /dev/null +++ b/evidence/seed_explain.sql @@ -0,0 +1,45 @@ +CREATE EXTENSION IF NOT EXISTS pgcrypto; + +INSERT INTO users (id, username, display_name, password_hash, created_at) +VALUES ( + 'aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa', + 'explain_seed_user', + 'Explain', + '$argon2id$v=19$m=65536,t=3,p=4$YWFhYWFhYWFhYWFhYWFhYQ$YWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWE', + now() +) +ON CONFLICT (username) DO NOTHING; + +INSERT INTO chats (id, title, created_at) +VALUES ('bbbbbbbb-bbbb-4bbb-8bbb-bbbbbbbbbbbb', 'explain-chat', now()) +ON CONFLICT (id) DO NOTHING; + +INSERT INTO chat_members (chat_id, user_id) +VALUES ('bbbbbbbb-bbbb-4bbb-8bbb-bbbbbbbbbbbb', 'aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa') +ON CONFLICT DO NOTHING; + +INSERT INTO messages (id, chat_id, sender_id, text, created_at) +SELECT gen_random_uuid(), + 'bbbbbbbb-bbbb-4bbb-8bbb-bbbbbbbbbbbb', + 'aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa', + 'msg-' || g, + timestamptz '2026-01-01 00:00:00Z' + (g * interval '1 millisecond') +FROM generate_series(1::integer, 12000::integer) AS g +WHERE NOT EXISTS ( + SELECT 1 FROM messages WHERE chat_id = 'bbbbbbbb-bbbb-4bbb-8bbb-bbbbbbbbbbbb' LIMIT 1 +); + +SELECT count(*) AS message_count +FROM messages +WHERE chat_id = 'bbbbbbbb-bbbb-4bbb-8bbb-bbbbbbbbbbbb'; + +EXPLAIN (ANALYZE, BUFFERS) +SELECT id, chat_id, sender_id, text, created_at +FROM messages +WHERE chat_id = 'bbbbbbbb-bbbb-4bbb-8bbb-bbbbbbbbbbbb' + AND (created_at, id) > ( + '1970-01-01'::timestamptz, + '00000000-0000-0000-0000-000000000000'::uuid + ) +ORDER BY created_at ASC, id ASC +LIMIT 50; diff --git a/evidence/smoke.txt b/evidence/smoke.txt new file mode 100644 index 00000000..cf367893 --- /dev/null +++ b/evidence/smoke.txt @@ -0,0 +1 @@ +PASS: lab2 public API scenario; infrastructure and grading evidence remain separate. diff --git a/evidence/student-tests.txt b/evidence/student-tests.txt new file mode 100644 index 00000000..06d7a3ca Binary files /dev/null and b/evidence/student-tests.txt differ diff --git a/migrations/001_init.sql b/migrations/001_init.sql new file mode 100644 index 00000000..27f63acb --- /dev/null +++ b/migrations/001_init.sql @@ -0,0 +1,48 @@ +-- Lab2 schema: users, chats, membership, messages. +-- Applied once via schema_migrations; safe to re-run application bootstrap. + +CREATE TABLE IF NOT EXISTS schema_migrations ( + version TEXT PRIMARY KEY, + applied_at TIMESTAMPTZ NOT NULL DEFAULT now() +); + +CREATE TABLE IF NOT EXISTS users ( + id UUID PRIMARY KEY, + username TEXT NOT NULL, + display_name TEXT NOT NULL, + password_hash TEXT NOT NULL, + created_at TIMESTAMPTZ NOT NULL, + CONSTRAINT users_username_format CHECK (username ~ '^[a-z0-9_]{3,32}$'), + CONSTRAINT users_username_unique UNIQUE (username) +); + +CREATE TABLE IF NOT EXISTS chats ( + id UUID PRIMARY KEY, + title TEXT NOT NULL, + created_at TIMESTAMPTZ NOT NULL +); + +CREATE TABLE IF NOT EXISTS chat_members ( + chat_id UUID NOT NULL REFERENCES chats(id) ON DELETE CASCADE, + user_id UUID NOT NULL REFERENCES users(id), + PRIMARY KEY (chat_id, user_id) +); + +CREATE INDEX IF NOT EXISTS idx_chat_members_user + ON chat_members (user_id, chat_id); + +CREATE TABLE IF NOT EXISTS messages ( + id UUID PRIMARY KEY, + chat_id UUID NOT NULL REFERENCES chats(id) ON DELETE CASCADE, + sender_id UUID NOT NULL REFERENCES users(id), + text TEXT NOT NULL, + created_at TIMESTAMPTZ NOT NULL +); + +-- Keyset pagination by (created_at, id) within a chat; avoids full history scan. +CREATE INDEX IF NOT EXISTS idx_messages_chat_created_id + ON messages (chat_id, created_at ASC, id ASC); + +-- List chats for a user ordered by chat created_at/id. +CREATE INDEX IF NOT EXISTS idx_chats_created_id + ON chats (created_at ASC, id ASC); diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 00000000..7af78cbe --- /dev/null +++ b/requirements.txt @@ -0,0 +1,6 @@ +fastapi==0.115.6 +uvicorn[standard]==0.32.1 +pydantic==2.10.3 +psycopg[binary,pool]==3.2.4 +redis==5.2.1 +argon2-cffi==23.1.0 diff --git a/scripts/restart.cmd b/scripts/restart.cmd new file mode 100644 index 00000000..08169ad4 --- /dev/null +++ b/scripts/restart.cmd @@ -0,0 +1,16 @@ +@echo off +setlocal +cd /d "%~dp0\.." +docker compose --env-file .env restart postgres redis api +if errorlevel 1 exit /b 1 +set /a i=0 +:wait +docker compose --env-file .env exec -T api python -c "import urllib.request; urllib.request.urlopen('http://127.0.0.1:8080/health/ready', timeout=2)" >nul 2>&1 +if not errorlevel 1 exit /b 0 +set /a i+=1 +if %i% geq 60 ( + echo restart: API not ready within 60s >&2 + exit /b 1 +) +timeout /t 1 /nobreak >nul +goto wait diff --git a/scripts/restart.sh b/scripts/restart.sh new file mode 100644 index 00000000..4280c43b --- /dev/null +++ b/scripts/restart.sh @@ -0,0 +1,7 @@ +#!/bin/sh +set -eu +# Restart API and all state stores while preserving volumes. +# Readiness is waited by tests/resilience.py separately (do not block here forever). +cd "$(dirname "$0")/.." +docker compose --env-file .env restart postgres redis api +exit 0 diff --git a/scripts/seed_explain.py b/scripts/seed_explain.py new file mode 100644 index 00000000..43da475a --- /dev/null +++ b/scripts/seed_explain.py @@ -0,0 +1,74 @@ +#!/usr/bin/env python3 +"""Seed >=10k messages and print EXPLAIN (ANALYZE, BUFFERS) for keyset page.""" + +from __future__ import annotations + +import uuid +from datetime import datetime, timezone + +import psycopg +from psycopg.rows import dict_row + + +def utc_now(): + return datetime.now(timezone.utc).replace(microsecond=0) + + +def main() -> None: + url = open("/run/secrets/database_url", encoding="utf-8").read().strip() + with psycopg.connect(url, row_factory=dict_row) as conn: + with conn.transaction(): + u1, u2 = uuid.uuid4(), uuid.uuid4() + now = utc_now() + conn.execute( + """ + INSERT INTO users (id, username, display_name, password_hash, created_at) + VALUES (%s, %s, %s, %s, %s) + """, + (u1, f"explain_a_{u1.hex[:8]}", "A", "x", now), + ) + conn.execute( + """ + INSERT INTO users (id, username, display_name, password_hash, created_at) + VALUES (%s, %s, %s, %s, %s) + """, + (u2, f"explain_b_{u2.hex[:8]}", "B", "x", now), + ) + chat = uuid.uuid4() + conn.execute( + "INSERT INTO chats (id, title, created_at) VALUES (%s, %s, %s)", + (chat, "explain", now), + ) + conn.execute( + "INSERT INTO chat_members (chat_id, user_id) VALUES (%s, %s), (%s, %s)", + (chat, u1, chat, u2), + ) + rows = [(uuid.uuid4(), chat, u1, f"m{i}", now) for i in range(10000)] + with conn.cursor() as cur: + cur.executemany( + """ + INSERT INTO messages (id, chat_id, sender_id, text, created_at) + VALUES (%s, %s, %s, %s, %s) + """, + rows, + ) + mid = rows[5000][0] + plan = conn.execute( + """ + EXPLAIN (ANALYZE, BUFFERS) + SELECT id, chat_id, sender_id, text, created_at + FROM messages + WHERE chat_id = %s AND (created_at, id) > (%s, %s) + ORDER BY created_at ASC, id ASC + LIMIT 51 + """, + (chat, now, mid), + ).fetchall() + for row in plan: + print(next(iter(row.values()))) + print("CHAT_ID", chat) + print("SEEDED", 10000) + + +if __name__ == "__main__": + main() diff --git a/src/messenger/__init__.py b/src/messenger/__init__.py new file mode 100644 index 00000000..48d98fe3 --- /dev/null +++ b/src/messenger/__init__.py @@ -0,0 +1,3 @@ +"""Messenger API for lab2 (PostgreSQL + Redis sessions).""" + +__version__ = "2.0.0" diff --git a/src/messenger/__main__.py b/src/messenger/__main__.py new file mode 100644 index 00000000..5c1349f2 --- /dev/null +++ b/src/messenger/__main__.py @@ -0,0 +1,30 @@ +"""Run the messenger HTTP server.""" + +from __future__ import annotations + +import sys +from pathlib import Path + +_SRC = Path(__file__).resolve().parents[1] +if str(_SRC) not in sys.path: + sys.path.insert(0, str(_SRC)) + +import uvicorn + +from messenger import config + + +def main() -> None: + uvicorn.run( + "messenger.app:app", + host=config.app_host(), + port=config.app_port(), + log_level="info", + access_log=False, + timeout_keep_alive=5, + timeout_graceful_shutdown=8, + ) + + +if __name__ == "__main__": + main() diff --git a/src/messenger/app.py b/src/messenger/app.py new file mode 100644 index 00000000..5c369e28 --- /dev/null +++ b/src/messenger/app.py @@ -0,0 +1,632 @@ +"""HTTP API: PostgreSQL messenger with Basic→Bearer sessions (lab2).""" + +from __future__ import annotations + +import asyncio +import base64 +import json +import logging +import re +import signal +import uuid +from contextlib import asynccontextmanager +from datetime import datetime, timezone +from typing import Any, List, Optional, Tuple + +from fastapi import FastAPI, Header, Query, Request +from fastapi.exceptions import RequestValidationError +from fastapi.responses import JSONResponse, Response +from starlette.exceptions import HTTPException as StarletteHTTPException + +from messenger import db, sessions +from messenger.sessions import SessionStoreError +from messenger.store import Store, StoreUnavailable, format_ts + +USERNAME_RE = re.compile(r"^[a-z0-9_]{3,32}\Z") +UUID_RE = re.compile( + r"^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}\Z" +) +BODY_READ_TIMEOUT_S = 5.0 +PASSWORD_MIN = 12 +PASSWORD_MAX = 128 + +store = Store() +ready = False + +logging.basicConfig( + level=logging.INFO, + format="%(asctime)s %(levelname)s %(message)s", +) +log = logging.getLogger("messenger") + + +def mark_not_ready(*_args): + global ready + if ready: + ready = False + log.info("readiness disabled; draining in-flight requests") + + +def _chain_signal(previous): + def handler(signum, frame): + mark_not_ready() + if callable(previous) and previous not in (signal.SIG_DFL, signal.SIG_IGN): + previous(signum, frame) + + return handler + + +@asynccontextmanager +async def lifespan(_app: FastAPI): + try: + signal.signal(signal.SIGTERM, _chain_signal(signal.getsignal(signal.SIGTERM))) + signal.signal(signal.SIGINT, _chain_signal(signal.getsignal(signal.SIGINT))) + except (ValueError, OSError) as exc: + log.warning("signal handlers not installed: %s", exc) + global ready + db.init_pool() + db.apply_migrations() + sessions.init_client() + ready = True + log.info("messenger started") + yield + mark_not_ready() + sessions.close_client() + db.close_pool() + log.info("messenger stopped") + + +def request_id_from(request: Request) -> str: + incoming = request.headers.get("x-request-id") + if incoming and incoming.strip(): + return incoming.strip()[:128] + return str(uuid.uuid4()) + + +def error_body(code: str, message: str, request_id: str) -> dict: + return {"error": {"code": code, "message": message, "request_id": request_id}} + + +def error_response( + status: int, + code: str, + message: str, + request_id: str, + www_authenticate: Optional[str] = None, +) -> JSONResponse: + headers = {"X-Request-Id": request_id} + if www_authenticate: + headers["WWW-Authenticate"] = www_authenticate + return JSONResponse( + status_code=status, + content=error_body(code, message, request_id), + headers=headers, + ) + + +def is_blank(value: str) -> bool: + return value.strip() == "" + + +def has_nul(value: str) -> bool: + # PostgreSQL text cannot store U+0000; reject explicitly (400), never 500/503. + return "\x00" in value + + +def has_lone_surrogate(value: str) -> bool: + return any(0xD800 <= ord(ch) <= 0xDFFF for ch in value) + + +def codepoint_len(value: str) -> int: + return len(value) + + +def parse_uuid(value: str) -> Optional[str]: + if not isinstance(value, str) or not UUID_RE.match(value): + return None + try: + return str(uuid.UUID(value)) + except ValueError: + return None + + +def encode_cursor(resource: str, created_at: datetime, item_id: str) -> str: + raw = f"{resource}|{created_at.strftime('%Y-%m-%dT%H:%M:%SZ')}|{item_id}" + return base64.urlsafe_b64encode(raw.encode("utf-8")).decode("ascii") + + +def decode_cursor(cursor: str, expected_resource: str) -> Tuple[datetime, uuid.UUID]: + try: + raw = base64.urlsafe_b64decode(cursor.encode("ascii")).decode("utf-8") + resource, ts, item_id = raw.split("|", 2) + if resource != expected_resource: + raise ValueError("wrong resource") + created_at = datetime.strptime(ts, "%Y-%m-%dT%H:%M:%SZ").replace( + tzinfo=timezone.utc + ) + return created_at, uuid.UUID(item_id) + except Exception as exc: + raise ValueError("invalid cursor") from exc + + +app = FastAPI( + title="MTUSI Messenger lab2", + docs_url=None, + redoc_url=None, + lifespan=lifespan, +) + + +@app.middleware("http") +async def add_request_id_and_gate(request: Request, call_next): + rid = request_id_from(request) + request.state.request_id = rid + + path = request.url.path + if not ready and path not in ("/health/live",): + if path == "/health/ready": + return error_response(503, "unavailable", "Shutting down", rid) + return error_response(503, "unavailable", "Service unavailable", rid) + + response = await call_next(request) + if "X-Request-Id" not in response.headers: + response.headers["X-Request-Id"] = rid + log.info( + "%s %s -> %s request_id=%s", + request.method, + path, + response.status_code, + rid, + ) + return response + + +class ApiError(Exception): + def __init__( + self, + status: int, + code: str, + message: str, + www_authenticate: Optional[str] = None, + ): + self.status = status + self.code = code + self.message = message + self.www_authenticate = www_authenticate + + +@app.exception_handler(ApiError) +async def api_error_handler(request: Request, exc: ApiError): + rid = getattr(request.state, "request_id", None) or request_id_from(request) + return error_response( + exc.status, exc.code, exc.message, rid, www_authenticate=exc.www_authenticate + ) + + +@app.exception_handler(RequestValidationError) +async def validation_handler(request: Request, exc: RequestValidationError): + rid = getattr(request.state, "request_id", None) or request_id_from(request) + return error_response(400, "invalid_request", "Invalid request", rid) + + +@app.exception_handler(StarletteHTTPException) +async def http_exception_handler(request: Request, exc: StarletteHTTPException): + rid = getattr(request.state, "request_id", None) or request_id_from(request) + if exc.status_code == 404: + return error_response(404, "not_found", "Not found", rid) + if exc.status_code == 405: + return error_response(400, "invalid_request", "Method not allowed", rid) + return error_response(exc.status_code, "invalid_request", "Request failed", rid) + + +@app.exception_handler(StoreUnavailable) +async def store_unavailable_handler(request: Request, exc: StoreUnavailable): + rid = getattr(request.state, "request_id", None) or request_id_from(request) + return error_response(503, "unavailable", "Database unavailable", rid) + + +@app.exception_handler(SessionStoreError) +async def session_store_unavailable_handler(request: Request, exc: SessionStoreError): + rid = getattr(request.state, "request_id", None) or request_id_from(request) + return error_response(503, "unavailable", "Session store unavailable", rid) + + +@app.exception_handler(Exception) +async def unhandled_handler(request: Request, exc: Exception): + rid = getattr(request.state, "request_id", None) or request_id_from(request) + log.exception("unhandled error request_id=%s", rid) + return error_response(500, "internal_error", "Internal error", rid) + + +async def read_json_object(request: Request) -> Any: + content_type = request.headers.get("content-type", "") + media = content_type.split(";")[0].strip().lower() + if media and media != "application/json": + raise ApiError(415, "unsupported_media_type", "Unsupported media type") + try: + body = await asyncio.wait_for(request.body(), timeout=BODY_READ_TIMEOUT_S) + except asyncio.TimeoutError: + raise ApiError(400, "invalid_request", "Request body timeout") + if not body: + raise ApiError(400, "invalid_request", "Empty body") + try: + data = json.loads(body.decode("utf-8")) + except (UnicodeDecodeError, json.JSONDecodeError): + raise ApiError(400, "invalid_request", "Malformed JSON") + if not isinstance(data, dict): + raise ApiError(400, "invalid_request", "JSON object required") + return data + + +def require_bearer(authorization: Optional[str]) -> Tuple[str, str]: + """Return (user_id, token). X-User-Id is ignored.""" + bearer_challenge = 'Bearer realm="messenger"' + if not authorization or not authorization.strip(): + raise ApiError( + 401, "unauthorized", "Missing bearer token", www_authenticate=bearer_challenge + ) + parts = authorization.strip().split(None, 1) + if len(parts) != 2 or parts[0].lower() != "bearer" or not parts[1].strip(): + # Basic on business endpoints must not work. + raise ApiError( + 401, "unauthorized", "Invalid authorization", www_authenticate=bearer_challenge + ) + token = parts[1].strip() + try: + session = sessions.get_session(token) + except SessionStoreError: + raise ApiError(503, "unavailable", "Session store unavailable") + if session is None: + raise ApiError( + 401, "unauthorized", "Invalid or expired session", www_authenticate=bearer_challenge + ) + user_id = session["user_id"] + user = store.get_user(user_id) + if user is None: + raise ApiError( + 401, "unauthorized", "Invalid or expired session", www_authenticate=bearer_challenge + ) + return user_id, token + + +def require_chat_member(chat_id_raw: str, user_id: str): + chat_id = parse_uuid(chat_id_raw) + if chat_id is None: + raise ApiError(400, "invalid_request", "Invalid chat_id") + chat = store.get_chat(chat_id) + if chat is None or user_id not in chat.member_ids: + raise ApiError(404, "not_found", "Chat not found") + return chat + + +def parse_limit(limit: Optional[int]) -> int: + if limit is None: + return 50 + if not isinstance(limit, int) or isinstance(limit, bool) or limit < 1 or limit > 100: + raise ApiError(400, "invalid_request", "Invalid limit") + return limit + + +def validate_display_text(value: Any, field: str, max_len: int) -> str: + if not isinstance(value, str) or is_blank(value) or has_nul(value): + raise ApiError(400, "invalid_request", f"Invalid {field}") + if has_lone_surrogate(value): + raise ApiError(400, "invalid_request", f"Invalid {field}") + length = codepoint_len(value) + if length < 1 or length > max_len: + raise ApiError(400, "invalid_request", f"Invalid {field}") + return value + + +def validate_password(value: Any) -> str: + # Required strings must not be whitespace-only (contracts/README). + if not isinstance(value, str) or is_blank(value) or has_nul(value): + raise ApiError(400, "invalid_request", "Invalid password") + if has_lone_surrogate(value): + raise ApiError(400, "invalid_request", "Invalid password") + length = codepoint_len(value) + if length < PASSWORD_MIN or length > PASSWORD_MAX: + raise ApiError(400, "invalid_request", "Invalid password") + return value + + +def parse_basic_credentials(authorization: Optional[str]) -> Tuple[str, str]: + challenge = 'Basic realm="messenger", charset="UTF-8"' + if not authorization or not authorization.strip(): + raise ApiError( + 401, "unauthorized", "Invalid credentials", www_authenticate=challenge + ) + parts = authorization.strip().split(None, 1) + if len(parts) != 2 or parts[0].lower() != "basic": + raise ApiError( + 401, "unauthorized", "Invalid credentials", www_authenticate=challenge + ) + try: + decoded = base64.b64decode(parts[1].strip(), validate=True).decode("utf-8") + except Exception: + raise ApiError( + 401, "unauthorized", "Invalid credentials", www_authenticate=challenge + ) + if ":" not in decoded: + raise ApiError( + 401, "unauthorized", "Invalid credentials", www_authenticate=challenge + ) + username, password = decoded.split(":", 1) + # Username ASCII-only per contract; password may contain ':'. + if not username or any(ord(ch) > 127 for ch in username): + raise ApiError( + 401, "unauthorized", "Invalid credentials", www_authenticate=challenge + ) + return username, password + + +@app.get("/health/live") +async def liveness(request: Request): + rid = request.state.request_id + return JSONResponse(content={"status": "ok"}, headers={"X-Request-Id": rid}) + + +@app.get("/health/ready") +async def readiness(request: Request): + rid = request.state.request_id + if not ready: + return error_response(503, "unavailable", "Not ready", rid) + if not db.ping() or not sessions.ping(): + return error_response(503, "unavailable", "Dependency unavailable", rid) + return JSONResponse(content={"status": "ok"}, headers={"X-Request-Id": rid}) + + +@app.post("/api/v1/users") +async def register_user(request: Request): + rid = request.state.request_id + data = await read_json_object(request) + allowed = {"username", "display_name", "password"} + if set(data.keys()) - allowed: + raise ApiError(400, "invalid_request", "Unexpected fields") + if "username" not in data or "display_name" not in data or "password" not in data: + raise ApiError(400, "invalid_request", "Missing required fields") + username = data["username"] + display_name = data["display_name"] + if ( + not isinstance(username, str) + or is_blank(username) + or has_lone_surrogate(username) + or not USERNAME_RE.match(username) + ): + raise ApiError(400, "invalid_request", "Invalid username") + display_name = validate_display_text(display_name, "display_name", 100) + password = validate_password(data["password"]) + try: + user = store.create_user(username, display_name, password) + except KeyError: + raise ApiError(409, "conflict", "Username already taken") + return JSONResponse( + status_code=201, + content=user.to_dict(), + headers={"X-Request-Id": rid}, + ) + + +@app.post("/api/v1/auth/sessions") +async def create_session(request: Request): + rid = request.state.request_id + challenge = 'Basic realm="messenger", charset="UTF-8"' + # Contract: Basic only, no JSON body (even with valid credentials). + try: + raw_body = await asyncio.wait_for(request.body(), timeout=BODY_READ_TIMEOUT_S) + except asyncio.TimeoutError: + raise ApiError(400, "invalid_request", "Request body timeout") + if raw_body and raw_body.strip(): + raise ApiError(400, "invalid_request", "Login must not include a body") + username, password = parse_basic_credentials( + request.headers.get("authorization") + ) + # Uniform 401 for wrong user / wrong password (no user enumeration). + user = store.get_user_by_username(username) + if user is None or not store.verify_password(user.password_hash, password): + raise ApiError( + 401, "unauthorized", "Invalid credentials", www_authenticate=challenge + ) + try: + session = sessions.create_session(user.id) + except SessionStoreError: + raise ApiError(503, "unavailable", "Session store unavailable") + body = { + "token": session["token"], + "token_type": "Bearer", + "expires_at": session["expires_at"], + "user": user.to_dict(), + } + return JSONResponse( + status_code=201, content=body, headers={"X-Request-Id": rid} + ) + + +@app.delete("/api/v1/auth/sessions/current") +async def logout( + request: Request, + authorization: Optional[str] = Header(default=None, alias="Authorization"), +): + rid = request.state.request_id + _user_id, token = require_bearer(authorization) + try: + sessions.revoke_session(token) + except SessionStoreError: + raise ApiError(503, "unavailable", "Session store unavailable") + return Response(status_code=204, headers={"X-Request-Id": rid}) + + +@app.get("/api/v1/users/me") +async def current_user( + request: Request, + authorization: Optional[str] = Header(default=None, alias="Authorization"), +): + rid = request.state.request_id + user_id, _token = require_bearer(authorization) + user = store.get_user(user_id) + assert user is not None + return JSONResponse(content=user.to_dict(), headers={"X-Request-Id": rid}) + + +@app.post("/api/v1/chats") +async def create_chat( + request: Request, + authorization: Optional[str] = Header(default=None, alias="Authorization"), +): + rid = request.state.request_id + user_id, _token = require_bearer(authorization) + data = await read_json_object(request) + allowed = {"title", "member_ids"} + if set(data.keys()) - allowed: + raise ApiError(400, "invalid_request", "Unexpected fields") + if "title" not in data or "member_ids" not in data: + raise ApiError(400, "invalid_request", "Missing required fields") + title = validate_display_text(data["title"], "title", 100) + member_ids = data["member_ids"] + if not isinstance(member_ids, list) or not member_ids or len(member_ids) > 100: + raise ApiError(400, "invalid_request", "Invalid member_ids") + parsed: List[str] = [] + seen = set() + for mid in member_ids: + if not isinstance(mid, str): + raise ApiError(400, "invalid_request", "Invalid member_ids") + uid = parse_uuid(mid) + if uid is None: + raise ApiError(400, "invalid_request", "Invalid member_ids") + if uid in seen: + raise ApiError(400, "invalid_request", "Duplicate member_ids") + seen.add(uid) + parsed.append(uid) + if user_id not in seen: + raise ApiError(400, "invalid_request", "Caller must be a member") + try: + chat = store.create_chat(title, parsed) + except KeyError: + raise ApiError(400, "invalid_request", "Unknown member") + return JSONResponse( + status_code=201, + content=chat.to_dict(), + headers={"X-Request-Id": rid}, + ) + + +@app.get("/api/v1/chats") +async def list_chats( + request: Request, + authorization: Optional[str] = Header(default=None, alias="Authorization"), + limit: Optional[int] = Query(default=None), + cursor: Optional[str] = Query(default=None), +): + rid = request.state.request_id + user_id, _token = require_bearer(authorization) + lim = parse_limit(limit) + resource = f"chats:{user_id}" + cursor_key = None + if cursor is not None: + if not cursor: + raise ApiError(400, "invalid_request", "Invalid cursor") + try: + cursor_ts, cursor_id = decode_cursor(cursor, resource) + except ValueError: + raise ApiError(400, "invalid_request", "Invalid cursor") + if not store.chat_exists_for_user(user_id, cursor_ts, cursor_id): + raise ApiError(400, "invalid_request", "Invalid cursor") + cursor_key = (cursor_ts, cursor_id) + chats, more = store.list_chats_for_user(user_id, lim, cursor_key) + next_cursor = None + if more and chats: + last = chats[-1] + next_cursor = encode_cursor(resource, last.created_at, last.id) + return JSONResponse( + content={"items": [c.to_dict() for c in chats], "next_cursor": next_cursor}, + headers={"X-Request-Id": rid}, + ) + + +@app.get("/api/v1/chats/{chat_id}") +async def get_chat( + chat_id: str, + request: Request, + authorization: Optional[str] = Header(default=None, alias="Authorization"), +): + rid = request.state.request_id + user_id, _token = require_bearer(authorization) + chat = require_chat_member(chat_id, user_id) + return JSONResponse(content=chat.to_dict(), headers={"X-Request-Id": rid}) + + +@app.post("/api/v1/chats/{chat_id}/messages") +async def create_message( + chat_id: str, + request: Request, + authorization: Optional[str] = Header(default=None, alias="Authorization"), +): + rid = request.state.request_id + user_id, _token = require_bearer(authorization) + chat = require_chat_member(chat_id, user_id) + data = await read_json_object(request) + allowed = {"text"} + if set(data.keys()) - allowed: + raise ApiError(400, "invalid_request", "Unexpected fields") + if "text" not in data: + raise ApiError(400, "invalid_request", "Missing text") + text = data["text"] + if ( + not isinstance(text, str) + or is_blank(text) + or has_nul(text) + or has_lone_surrogate(text) + or codepoint_len(text) > 4000 + ): + raise ApiError(400, "invalid_request", "Invalid text") + try: + message = store.create_message(chat.id, user_id, text) + except ValueError: + raise ApiError(400, "invalid_request", "Invalid text") + return JSONResponse( + status_code=201, + content=message.to_dict(), + headers={"X-Request-Id": rid}, + ) + + +@app.get("/api/v1/chats/{chat_id}/messages") +async def list_messages( + chat_id: str, + request: Request, + authorization: Optional[str] = Header(default=None, alias="Authorization"), + limit: Optional[int] = Query(default=None), + cursor: Optional[str] = Query(default=None), +): + rid = request.state.request_id + user_id, _token = require_bearer(authorization) + chat = require_chat_member(chat_id, user_id) + lim = parse_limit(limit) + resource = f"messages:{chat.id}" + cursor_key = None + if cursor is not None: + if not cursor: + raise ApiError(400, "invalid_request", "Invalid cursor") + try: + cursor_ts, cursor_id = decode_cursor(cursor, resource) + except ValueError: + raise ApiError(400, "invalid_request", "Invalid cursor") + if not store.message_exists(chat.id, cursor_ts, cursor_id): + raise ApiError(400, "invalid_request", "Invalid cursor") + cursor_key = (cursor_ts, cursor_id) + messages, has_more = store.list_messages(chat.id, lim, cursor_key) + next_cursor = None + if has_more and messages: + last = messages[-1] + next_cursor = encode_cursor(resource, last.created_at, last.id) + return JSONResponse( + content={ + "items": [m.to_dict() for m in messages], + "next_cursor": next_cursor, + }, + headers={"X-Request-Id": rid}, + ) + + +def create_app() -> FastAPI: + return app diff --git a/src/messenger/config.py b/src/messenger/config.py new file mode 100644 index 00000000..aa2f0e74 --- /dev/null +++ b/src/messenger/config.py @@ -0,0 +1,64 @@ +"""Runtime configuration from environment / secret files.""" + +from __future__ import annotations + +import os +from pathlib import Path + + +def _read_secret(env_key: str, file_key: str, default: str | None = None) -> str: + file_path = os.environ.get(file_key, "").strip() + if file_path: + path = Path(file_path) + if path.is_file(): + return path.read_text(encoding="utf-8").strip() + value = os.environ.get(env_key, "").strip() + if value: + return value + if default is not None: + return default + raise RuntimeError(f"Missing secret: set {env_key} or {file_key}") + + +def database_url() -> str: + return _read_secret("DATABASE_URL", "DATABASE_URL_FILE") + + +def cache_url() -> str: + return _read_secret("CACHE_URL", "CACHE_URL_FILE") + + +def session_ttl_seconds() -> int: + raw = os.environ.get("SESSION_TTL_SECONDS", "3600").strip() + try: + value = int(raw) + except ValueError as exc: + raise RuntimeError("SESSION_TTL_SECONDS must be an integer") from exc + if value < 1: + raise RuntimeError("SESSION_TTL_SECONDS must be >= 1") + return value + + +def app_host() -> str: + return os.environ.get("APP_HOST", "0.0.0.0") + + +def app_port() -> int: + return int(os.environ.get("APP_PORT", "8080")) + + +def migrations_dir() -> Path: + override = os.environ.get("MIGRATIONS_DIR", "").strip() + if override: + return Path(override) + # /app/migrations in container; repo-root/migrations for local runs. + here = Path(__file__).resolve() + candidates = [ + Path("/app/migrations"), + here.parents[2] / "migrations", + Path.cwd() / "migrations", + ] + for path in candidates: + if path.is_dir(): + return path + return candidates[0] diff --git a/src/messenger/db.py b/src/messenger/db.py new file mode 100644 index 00000000..5959da93 --- /dev/null +++ b/src/messenger/db.py @@ -0,0 +1,107 @@ +"""PostgreSQL connection pool and versioned migrations.""" + +from __future__ import annotations + +import logging +import re +from pathlib import Path +from typing import List, Optional + +from psycopg.rows import dict_row +from psycopg_pool import ConnectionPool + +from messenger import config + +log = logging.getLogger("messenger.db") + +_pool: Optional[ConnectionPool] = None + + +def get_pool() -> ConnectionPool: + if _pool is None: + raise RuntimeError("Database pool is not initialized") + return _pool + + +def init_pool() -> ConnectionPool: + global _pool + if _pool is not None: + return _pool + url = config.database_url() + _pool = ConnectionPool( + conninfo=url, + min_size=1, + max_size=10, + kwargs={ + "row_factory": dict_row, + "connect_timeout": 5, + "options": "-c statement_timeout=5000", + }, + open=True, + timeout=10, + ) + return _pool + + +def close_pool() -> None: + global _pool + if _pool is not None: + _pool.close() + _pool = None + + +def _split_sql(script: str) -> List[str]: + """Split SQL file into statements (no procedure bodies in our migrations).""" + # Drop line comments. + cleaned = re.sub(r"--[^\n]*", "", script) + parts = [] + for chunk in cleaned.split(";"): + stmt = chunk.strip() + if stmt: + parts.append(stmt) + return parts + + +def apply_migrations(migrations_path: Optional[Path] = None) -> None: + path = migrations_path or config.migrations_dir() + sql_files = sorted(path.glob("*.sql")) + if not sql_files: + raise RuntimeError(f"No migrations found in {path}") + pool = get_pool() + with pool.connection() as conn: + with conn.transaction(): + conn.execute( + """ + CREATE TABLE IF NOT EXISTS schema_migrations ( + version TEXT PRIMARY KEY, + applied_at TIMESTAMPTZ NOT NULL DEFAULT now() + ) + """ + ) + for sql_file in sql_files: + version = sql_file.name + row = conn.execute( + "SELECT 1 AS ok FROM schema_migrations WHERE version = %s", + (version,), + ).fetchone() + if row: + log.info("migration already applied: %s", version) + continue + script = sql_file.read_text(encoding="utf-8") + for stmt in _split_sql(script): + conn.execute(stmt) + conn.execute( + "INSERT INTO schema_migrations (version) VALUES (%s)", + (version,), + ) + log.info("applied migration: %s", version) + + +def ping() -> bool: + try: + with get_pool().connection() as conn: + conn.execute("SELECT 1") + return True + except Exception as exc: + log.warning("postgres ping failed: %s", type(exc).__name__) + return False diff --git a/src/messenger/sessions.py b/src/messenger/sessions.py new file mode 100644 index 00000000..21d7520a --- /dev/null +++ b/src/messenger/sessions.py @@ -0,0 +1,136 @@ +"""Opaque Bearer sessions in Redis with TTL (durable via Redis AOF).""" + +from __future__ import annotations + +import json +import logging +import secrets +from datetime import datetime, timedelta, timezone +from typing import Optional + +import redis + +from messenger import config + +log = logging.getLogger("messenger.sessions") + +_client: Optional[redis.Redis] = None + +TOKEN_BYTES = 32 # 256-bit entropy +KEY_PREFIX = "sess:" + + +def utc_now() -> datetime: + return datetime.now(timezone.utc).replace(microsecond=0) + + +def format_ts(value: datetime) -> str: + if value.tzinfo is None: + value = value.replace(tzinfo=timezone.utc) + return value.astimezone(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ") + + +def parse_ts(value: str) -> datetime: + return datetime.strptime(value, "%Y-%m-%dT%H:%M:%SZ").replace(tzinfo=timezone.utc) + + +def init_client() -> redis.Redis: + global _client + if _client is not None: + return _client + url = config.cache_url() + _client = redis.Redis.from_url( + url, + decode_responses=True, + socket_connect_timeout=3, + socket_timeout=3, + health_check_interval=30, + ) + return _client + + +def close_client() -> None: + global _client + if _client is not None: + try: + _client.close() + except Exception: + pass + _client = None + + +def get_client() -> redis.Redis: + if _client is None: + raise RuntimeError("Redis client is not initialized") + return _client + + +def ping() -> bool: + try: + return bool(get_client().ping()) + except Exception as exc: + log.warning("redis ping failed: %s", type(exc).__name__) + return False + + +class SessionStoreError(Exception): + """Raised when Redis is unavailable for session operations.""" + + +def create_session(user_id: str) -> dict: + ttl = config.session_ttl_seconds() + token = secrets.token_urlsafe(TOKEN_BYTES) + now = utc_now() + expires_at = now + timedelta(seconds=ttl) + payload = { + "user_id": user_id, + "expires_at": format_ts(expires_at), + "created_at": format_ts(now), + } + key = KEY_PREFIX + token + try: + client = get_client() + # SET with EX: Redis is sole session store; AOF keeps it across restart. + client.set(key, json.dumps(payload), ex=ttl) + except redis.RedisError as exc: + raise SessionStoreError("session store unavailable") from exc + return { + "token": token, + "token_type": "Bearer", + "expires_at": payload["expires_at"], + "user_id": user_id, + } + + +def get_session(token: str) -> Optional[dict]: + if not token: + return None + key = KEY_PREFIX + token + try: + raw = get_client().get(key) + except redis.RedisError as exc: + raise SessionStoreError("session store unavailable") from exc + if not raw: + return None + try: + data = json.loads(raw) + expires_at = parse_ts(data["expires_at"]) + except (ValueError, KeyError, TypeError, json.JSONDecodeError): + return None + if expires_at <= utc_now(): + try: + get_client().delete(key) + except redis.RedisError: + pass + return None + return data + + +def revoke_session(token: str) -> bool: + if not token: + return False + key = KEY_PREFIX + token + try: + return bool(get_client().delete(key)) + except redis.RedisError as exc: + raise SessionStoreError("session store unavailable") from exc diff --git a/src/messenger/store.py b/src/messenger/store.py new file mode 100644 index 00000000..e008447c --- /dev/null +++ b/src/messenger/store.py @@ -0,0 +1,363 @@ +"""PostgreSQL repository: users, chats, membership, messages.""" + +from __future__ import annotations + +import uuid +from dataclasses import dataclass +from datetime import datetime, timezone +from typing import List, Optional, Tuple + +from argon2 import PasswordHasher +from argon2.exceptions import VerifyMismatchError +from psycopg import Error as PsycopgError +from psycopg import errors as pg_errors + +from messenger.db import get_pool + +_ph = PasswordHasher() + + +class StoreUnavailable(Exception): + """Raised when PostgreSQL is unreachable or timed out.""" + + +def utc_now() -> datetime: + return datetime.now(timezone.utc).replace(microsecond=0) + + +def format_ts(value: datetime) -> str: + if value.tzinfo is None: + value = value.replace(tzinfo=timezone.utc) + return value.astimezone(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ") + + +@dataclass +class User: + id: str + username: str + display_name: str + created_at: datetime + password_hash: str = "" + + def to_dict(self) -> dict: + return { + "id": self.id, + "username": self.username, + "display_name": self.display_name, + "created_at": format_ts(self.created_at), + } + + +@dataclass +class Chat: + id: str + title: str + member_ids: List[str] + created_at: datetime + + def to_dict(self) -> dict: + return { + "id": self.id, + "title": self.title, + "member_ids": list(self.member_ids), + "created_at": format_ts(self.created_at), + } + + +@dataclass +class Message: + id: str + chat_id: str + sender_id: str + text: str + created_at: datetime + + def to_dict(self) -> dict: + return { + "id": self.id, + "chat_id": self.chat_id, + "sender_id": self.sender_id, + "text": self.text, + "created_at": format_ts(self.created_at), + } + + +def _row_user(row: dict) -> User: + return User( + id=str(row["id"]), + username=row["username"], + display_name=row["display_name"], + created_at=row["created_at"], + password_hash=row.get("password_hash", ""), + ) + + +def _row_message(row: dict) -> Message: + return Message( + id=str(row["id"]), + chat_id=str(row["chat_id"]), + sender_id=str(row["sender_id"]), + text=row["text"], + created_at=row["created_at"], + ) + + +class Store: + def hash_password(self, password: str) -> str: + return _ph.hash(password) + + def verify_password(self, password_hash: str, password: str) -> bool: + try: + return _ph.verify(password_hash, password) + except VerifyMismatchError: + return False + except Exception: + return False + + def create_user(self, username: str, display_name: str, password: str) -> User: + user_id = uuid.uuid4() + created = utc_now() + password_hash = self.hash_password(password) + try: + with get_pool().connection() as conn: + with conn.transaction(): + conn.execute( + """ + INSERT INTO users (id, username, display_name, password_hash, created_at) + VALUES (%s, %s, %s, %s, %s) + """, + (user_id, username, display_name, password_hash, created), + ) + except (pg_errors.UniqueViolation, pg_errors.IntegrityError) as exc: + raise KeyError("username_taken") from exc + except PsycopgError as exc: + raise StoreUnavailable("database unavailable") from exc + return User( + id=str(user_id), + username=username, + display_name=display_name, + created_at=created, + ) + + def get_user(self, user_id: str) -> Optional[User]: + try: + with get_pool().connection() as conn: + row = conn.execute( + """ + SELECT id, username, display_name, password_hash, created_at + FROM users WHERE id = %s + """, + (user_id,), + ).fetchone() + except PsycopgError as exc: + raise StoreUnavailable("database unavailable") from exc + return _row_user(row) if row else None + + def get_user_by_username(self, username: str) -> Optional[User]: + try: + with get_pool().connection() as conn: + row = conn.execute( + """ + SELECT id, username, display_name, password_hash, created_at + FROM users WHERE username = %s + """, + (username,), + ).fetchone() + except PsycopgError as exc: + raise StoreUnavailable("database unavailable") from exc + return _row_user(row) if row else None + + def create_chat(self, title: str, member_ids: List[str]) -> Chat: + chat_id = uuid.uuid4() + created = utc_now() + try: + with get_pool().connection() as conn: + with conn.transaction(): + # Atomic membership check + insert; rollback if any member missing. + rows = conn.execute( + "SELECT id FROM users WHERE id = ANY(%s::uuid[])", + (member_ids,), + ).fetchall() + found = {str(r["id"]) for r in rows} + if found != set(member_ids): + raise KeyError("unknown_member") + conn.execute( + "INSERT INTO chats (id, title, created_at) VALUES (%s, %s, %s)", + (chat_id, title, created), + ) + for mid in member_ids: + conn.execute( + "INSERT INTO chat_members (chat_id, user_id) VALUES (%s, %s)", + (chat_id, mid), + ) + except KeyError: + raise + except PsycopgError as exc: + raise StoreUnavailable("database unavailable") from exc + return Chat( + id=str(chat_id), + title=title, + member_ids=list(member_ids), + created_at=created, + ) + + def get_chat(self, chat_id: str) -> Optional[Chat]: + try: + with get_pool().connection() as conn: + row = conn.execute( + "SELECT id, title, created_at FROM chats WHERE id = %s", + (chat_id,), + ).fetchone() + if not row: + return None + members = conn.execute( + """ + SELECT user_id FROM chat_members + WHERE chat_id = %s + ORDER BY user_id + """, + (chat_id,), + ).fetchall() + except PsycopgError as exc: + raise StoreUnavailable("database unavailable") from exc + return Chat( + id=str(row["id"]), + title=row["title"], + member_ids=[str(m["user_id"]) for m in members], + created_at=row["created_at"], + ) + + def list_chats_for_user( + self, + user_id: str, + limit: int, + cursor: Optional[Tuple[datetime, uuid.UUID]], + ) -> Tuple[List[Chat], Optional[str]]: + params: list = [user_id] + where = "cm.user_id = %s" + if cursor is not None: + where += " AND (c.created_at, c.id) > (%s, %s)" + params.extend([cursor[0], cursor[1]]) + params.append(limit + 1) + try: + with get_pool().connection() as conn: + rows = conn.execute( + f""" + SELECT c.id, c.title, c.created_at + FROM chats c + JOIN chat_members cm ON cm.chat_id = c.id + WHERE {where} + ORDER BY c.created_at ASC, c.id ASC + LIMIT %s + """, + params, + ).fetchall() + chats: List[Chat] = [] + for row in rows[:limit]: + members = conn.execute( + """ + SELECT user_id FROM chat_members + WHERE chat_id = %s + ORDER BY user_id + """, + (row["id"],), + ).fetchall() + chats.append( + Chat( + id=str(row["id"]), + title=row["title"], + member_ids=[str(m["user_id"]) for m in members], + created_at=row["created_at"], + ) + ) + except PsycopgError as exc: + raise StoreUnavailable("database unavailable") from exc + has_more = len(rows) > limit + return chats, "more" if has_more else None + + def create_message(self, chat_id: str, sender_id: str, text: str) -> Message: + msg_id = uuid.uuid4() + created = utc_now() + try: + with get_pool().connection() as conn: + with conn.transaction(): + conn.execute( + """ + INSERT INTO messages (id, chat_id, sender_id, text, created_at) + VALUES (%s, %s, %s, %s, %s) + """, + (msg_id, chat_id, sender_id, text, created), + ) + except pg_errors.DataError as exc: + # e.g. unexpected encoding issues after validation + raise ValueError("invalid_text") from exc + except PsycopgError as exc: + raise StoreUnavailable("database unavailable") from exc + return Message( + id=str(msg_id), + chat_id=chat_id, + sender_id=sender_id, + text=text, + created_at=created, + ) + + def list_messages( + self, + chat_id: str, + limit: int, + cursor: Optional[Tuple[datetime, uuid.UUID]], + ) -> Tuple[List[Message], bool]: + params: list = [chat_id] + where = "chat_id = %s" + if cursor is not None: + where += " AND (created_at, id) > (%s, %s)" + params.extend([cursor[0], cursor[1]]) + params.append(limit + 1) + try: + with get_pool().connection() as conn: + rows = conn.execute( + f""" + SELECT id, chat_id, sender_id, text, created_at + FROM messages + WHERE {where} + ORDER BY created_at ASC, id ASC + LIMIT %s + """, + params, + ).fetchall() + except PsycopgError as exc: + raise StoreUnavailable("database unavailable") from exc + messages = [_row_message(r) for r in rows[:limit]] + return messages, len(rows) > limit + + def message_exists(self, chat_id: str, created_at: datetime, item_id: uuid.UUID) -> bool: + try: + with get_pool().connection() as conn: + row = conn.execute( + """ + SELECT 1 AS ok FROM messages + WHERE chat_id = %s AND created_at = %s AND id = %s + """, + (chat_id, created_at, item_id), + ).fetchone() + except PsycopgError as exc: + raise StoreUnavailable("database unavailable") from exc + return bool(row) + + def chat_exists_for_user( + self, user_id: str, created_at: datetime, item_id: uuid.UUID + ) -> bool: + try: + with get_pool().connection() as conn: + row = conn.execute( + """ + SELECT 1 AS ok + FROM chats c + JOIN chat_members cm ON cm.chat_id = c.id + WHERE cm.user_id = %s AND c.created_at = %s AND c.id = %s + """, + (user_id, created_at, item_id), + ).fetchone() + except PsycopgError as exc: + raise StoreUnavailable("database unavailable") from exc + return bool(row) diff --git a/student-tests/README.md b/student-tests/README.md new file mode 100644 index 00000000..b08e16d2 --- /dev/null +++ b/student-tests/README.md @@ -0,0 +1,11 @@ +# Собственные проверки (lab2) + +Требуется уже запущенный стенд (`compose up`). + +```sh +# из корня репозитория, внутри контейнера python или локально: +cd student-tests +BASE_URL=http://127.0.0.1:8080 python run_all.py +``` + +Для проверки expiry поднимите API с `SESSION_TTL_SECONDS=2` и повторите `test_sessions`. diff --git a/student-tests/helpers.py b/student-tests/helpers.py new file mode 100644 index 00000000..5afb7416 --- /dev/null +++ b/student-tests/helpers.py @@ -0,0 +1,64 @@ +"""Minimal HTTP helpers for student tests (stdlib only).""" + +from __future__ import annotations + +import base64 +import json +import os +import urllib.error +import urllib.request +import uuid + + +BASE_URL = os.environ.get("BASE_URL", "http://127.0.0.1:8080").rstrip("/") + + +def call(method, path, expected=None, body=None, token=None, headers=None, raw=None): + h = {"Accept": "application/json"} + if token: + h["Authorization"] = "Bearer " + token + if body is not None: + raw = json.dumps(body, ensure_ascii=False).encode("utf-8") + h["Content-Type"] = "application/json" + h.update(headers or {}) + req = urllib.request.Request(BASE_URL + path, data=raw, headers=h, method=method) + try: + resp = urllib.request.urlopen(req, timeout=30) + except urllib.error.HTTPError as err: + resp = err + data = resp.read() + status = resp.code + if expected is not None: + assert status == expected, ( + f"{method} {path}: expected {expected}, got {status}: {data[:200]!r}" + ) + payload = None + ctype = resp.headers.get_content_type() + if ctype == "application/json" and data: + payload = json.loads(data.decode("utf-8")) + assert resp.headers.get("X-Request-Id"), "missing X-Request-Id" + return {"status": status, "headers": resp.headers, "json": payload, "bytes": data} + + +def register(label: str): + username = f"st_{label}_{uuid.uuid4().hex[:10]}" + password = "T3st-" + uuid.uuid4().hex + body = { + "username": username, + "display_name": f"Student {label}", + "password": password, + } + user = call("POST", "/api/v1/users", 201, body=body)["json"] + credential = base64.b64encode(f"{username}:{password}".encode()).decode() + session = call( + "POST", + "/api/v1/auth/sessions", + 201, + headers={"Authorization": "Basic " + credential}, + )["json"] + return { + "id": user["id"], + "user": user, + "token": session["token"], + "registration": body, + } diff --git a/student-tests/run_all.py b/student-tests/run_all.py new file mode 100644 index 00000000..267376eb --- /dev/null +++ b/student-tests/run_all.py @@ -0,0 +1,41 @@ +#!/usr/bin/env python3 +"""Run all student tests against a live API (BASE_URL).""" + +from __future__ import annotations + +import importlib +import sys +import traceback + + +MODULES = [ + "test_api", + "test_concurrency", + "test_sessions", + "test_deps", +] + + +def main() -> int: + failed = 0 + passed = 0 + for name in MODULES: + mod = importlib.import_module(name) + tests = [getattr(mod, n) for n in dir(mod) if n.startswith("test_")] + for fn in tests: + label = f"{name}.{fn.__name__}" + try: + fn() + print("OK", label) + passed += 1 + except Exception as exc: + failed += 1 + print("FAIL", label, ":", exc) + traceback.print_exc() + print(f"Summary: {passed} passed, {failed} failed") + return 1 if failed else 0 + + +if __name__ == "__main__": + sys.path.insert(0, ".") + raise SystemExit(main()) diff --git a/student-tests/test_api.py b/student-tests/test_api.py new file mode 100644 index 00000000..ae02f097 --- /dev/null +++ b/student-tests/test_api.py @@ -0,0 +1,159 @@ +"""Grade 3/4 API checks: auth, conflict, X-User-Id ignored, logout.""" + +from __future__ import annotations + +import base64 +import time +import uuid + +from helpers import call, register + + +def test_username_conflict(): + a = register("conflict") + body = dict(a["registration"]) + call("POST", "/api/v1/users", 409, body=body) + + +def test_wrong_password(): + a = register("wrongpw") + bad = base64.b64encode( + (a["registration"]["username"] + ":wrong-password-xx").encode() + ).decode() + resp = call( + "POST", + "/api/v1/auth/sessions", + 401, + headers={"Authorization": "Basic " + bad}, + ) + assert resp["headers"].get("WWW-Authenticate", "").lower().startswith("basic") + + +def test_x_user_id_ignored(): + a = register("xu") + b = register("xv") + me = call( + "GET", + "/api/v1/users/me", + 200, + token=a["token"], + headers={"X-User-Id": b["id"]}, + )["json"] + assert me["id"] == a["id"] + + +def test_basic_on_business_rejected(): + a = register("basicbiz") + cred = base64.b64encode( + (a["registration"]["username"] + ":" + a["registration"]["password"]).encode() + ).decode() + call( + "GET", + "/api/v1/users/me", + 401, + headers={"Authorization": "Basic " + cred}, + ) + + +def test_logout_revokes(): + a = register("logout") + call("DELETE", "/api/v1/auth/sessions/current", 204, token=a["token"]) + call("GET", "/api/v1/users/me", 401, token=a["token"]) + + +def test_chat_requires_caller_member(): + a = register("owner") + b = register("peer") + call( + "POST", + "/api/v1/chats", + 400, + token=a["token"], + body={"title": "no me", "member_ids": [b["id"]]}, + ) + + +def test_unknown_member_rolls_back(): + a = register("atom") + fake = str(uuid.uuid4()) + call( + "POST", + "/api/v1/chats", + 400, + token=a["token"], + body={"title": "bad", "member_ids": [a["id"], fake]}, + ) + + +def test_whitespace_only_password_rejected(): + # Maintainer remark: required strings must not be whitespace-only. + body = { + "username": "ws_" + uuid.uuid4().hex[:10], + "display_name": "Spaces", + "password": " " * 12, + } + call("POST", "/api/v1/users", 400, body=body) + + +def test_nul_in_text_rejected(): + a = register("nul") + peer = register("nulb") + chat = call( + "POST", + "/api/v1/chats", + 201, + token=a["token"], + body={"title": "nul-chat", "member_ids": [a["id"], peer["id"]]}, + )["json"] + call( + "POST", + f"/api/v1/chats/{chat['id']}/messages", + 400, + token=a["token"], + body={"text": "hello\u0000world"}, + ) + + +def test_login_rejects_json_body(): + a = register("bodylogin") + cred = base64.b64encode( + (a["registration"]["username"] + ":" + a["registration"]["password"]).encode() + ).decode() + call( + "POST", + "/api/v1/auth/sessions", + 400, + body={"unexpected": True}, + headers={"Authorization": "Basic " + cred}, + ) + + +def test_invalid_basic_encoding_is_401(): + # Maintainer remark: malformed Basic must be 401, not 500. + call( + "POST", + "/api/v1/auth/sessions", + 401, + headers={"Authorization": "Basic " + base64.b64encode("é".encode()).decode()}, + ) + + +def test_unsupported_media_type(): + a = register("ctype") + peer = register("ctypeb") + chat = call( + "POST", + "/api/v1/chats", + 201, + token=a["token"], + body={"title": "ctype", "member_ids": [a["id"], peer["id"]]}, + )["json"] + raw = b'{"text":"x"}' + call( + "POST", + f"/api/v1/chats/{chat['id']}/messages", + 415, + token=a["token"], + raw=raw, + headers={"Content-Type": "text/plain"}, + ) diff --git a/student-tests/test_concurrency.py b/student-tests/test_concurrency.py new file mode 100644 index 00000000..8920d294 --- /dev/null +++ b/student-tests/test_concurrency.py @@ -0,0 +1,66 @@ +"""Concurrent registration race and parallel messages.""" + +from __future__ import annotations + +import concurrent.futures +import uuid + +from helpers import BASE_URL, call, register + + +def test_parallel_messages_unique(): + a = register("par") + b = register("parb") + chat = call( + "POST", + "/api/v1/chats", + 201, + token=a["token"], + body={"title": "parallel", "member_ids": [a["id"], b["id"]]}, + )["json"] + + def send(i: int): + return call( + "POST", + f"/api/v1/chats/{chat['id']}/messages", + 201, + token=a["token"], + body={"text": f"msg-{i}"}, + )["json"]["id"] + + with concurrent.futures.ThreadPoolExecutor(max_workers=10) as pool: + ids = list(pool.map(send, range(100))) + assert len(ids) == len(set(ids)) == 100 + + +def test_username_race_one_winner(): + username = "race_" + uuid.uuid4().hex[:12] + password = "T3st-" + uuid.uuid4().hex + body = { + "username": username, + "display_name": "Race", + "password": password, + } + + def attempt(_): + import json + import urllib.error + import urllib.request + + raw = json.dumps(body).encode("utf-8") + req = urllib.request.Request( + BASE_URL + "/api/v1/users", + data=raw, + headers={"Content-Type": "application/json", "Accept": "application/json"}, + method="POST", + ) + try: + resp = urllib.request.urlopen(req, timeout=30) + except urllib.error.HTTPError as err: + resp = err + return resp.code + + with concurrent.futures.ThreadPoolExecutor(max_workers=8) as pool: + codes = list(pool.map(attempt, range(8))) + assert codes.count(201) == 1, codes + assert codes.count(409) == 7, codes diff --git a/student-tests/test_deps.py b/student-tests/test_deps.py new file mode 100644 index 00000000..319ef872 --- /dev/null +++ b/student-tests/test_deps.py @@ -0,0 +1,68 @@ +"""Dependency failure checks (Redis/Postgres). Optional: RUN_DEPS_TEST=1.""" + +from __future__ import annotations + +import os +import subprocess +import time + +from helpers import BASE_URL, call, register + + +def _compose(*args: str) -> None: + cmd = ["docker", "compose", "--env-file", ".env", *args] + subprocess.check_call(cmd, cwd=os.path.join(os.path.dirname(__file__), "..")) + + +def _wait_ready(timeout_s: int = 60) -> None: + deadline = time.time() + timeout_s + while time.time() < deadline: + try: + if call("GET", "/health/ready")["status"] == 200: + return + except Exception: + pass + time.sleep(1) + raise AssertionError("stack did not become ready in time") + + +def test_redis_stop_returns_503_or_unready(): + if os.environ.get("RUN_DEPS_TEST") != "1": + print("SKIP test_redis_stop_returns_503_or_unready (set RUN_DEPS_TEST=1)") + return + actor = register("deps") + _compose("stop", "redis") + time.sleep(2) + try: + ready = call("GET", "/health/ready") + assert ready["status"] == 503, ready["status"] + me = call("GET", "/api/v1/users/me", token=actor["token"]) + assert me["status"] in (401, 503) + finally: + _compose("start", "redis") + _wait_ready() + + +def test_postgres_stop_returns_503(): + # Maintainer remark (level 4): cover PostgreSQL unavailability like Redis. + if os.environ.get("RUN_DEPS_TEST") != "1": + print("SKIP test_postgres_stop_returns_503 (set RUN_DEPS_TEST=1)") + return + actor = register("deps_pg") + _compose("stop", "postgres") + time.sleep(2) + try: + ready = call("GET", "/health/ready") + assert ready["status"] == 503, ready["status"] + # Authenticated business path must fail fast as 503 (not hang / not 500). + me = call("GET", "/api/v1/users/me", token=actor["token"]) + assert me["status"] == 503, me["status"] + hist = call( + "GET", + f"/api/v1/chats/{actor['id']}/messages", + token=actor["token"], + ) + assert hist["status"] in (400, 404, 503), hist["status"] + finally: + _compose("start", "postgres") + _wait_ready(90) diff --git a/student-tests/test_sessions.py b/student-tests/test_sessions.py new file mode 100644 index 00000000..dbbb4703 --- /dev/null +++ b/student-tests/test_sessions.py @@ -0,0 +1,34 @@ +"""Session expiry (short TTL) — run against stack with SESSION_TTL_SECONDS=2.""" + +from __future__ import annotations + +import base64 +import os +import time + +from helpers import call, register + + +def test_session_expiry_if_short_ttl(): + ttl = int(os.environ.get("SESSION_TTL_SECONDS", "3600")) + if ttl > 5: + print("SKIP expiry: SESSION_TTL_SECONDS=%s (need <=5 for this test)" % ttl) + return + a = register("expire") + time.sleep(ttl + 1.5) + call("GET", "/api/v1/users/me", 401, token=a["token"]) + + +def test_login_after_register(): + a = register("relogin") + cred = base64.b64encode( + (a["registration"]["username"] + ":" + a["registration"]["password"]).encode() + ).decode() + session = call( + "POST", + "/api/v1/auth/sessions", + 201, + headers={"Authorization": "Basic " + cred}, + )["json"] + assert session["token_type"] == "Bearer" + assert session["user"]["id"] == a["id"]