Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
39277c1467 | ||
|
|
e33582bf3e | ||
|
|
a0e99d5308 | ||
|
|
d26007028c | ||
|
|
c7aee7fd05 | ||
|
|
3cd8ec1cec | ||
|
|
191e88136c |
@@ -1,11 +1,3 @@
|
|||||||
# Copy to .env for your LOCAL synthetic test environment; never commit .env.
|
# 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_PORT=8080
|
||||||
APP_ENV=lab
|
|
||||||
SESSION_TTL_SECONDS=3600
|
SESSION_TTL_SECONDS=3600
|
||||||
# Choose credentials locally. File paths contain no secret values.
|
|
||||||
DATABASE_URL_FILE=/run/secrets/database_url
|
|
||||||
# Optional on 3, required use on 4 in lab2; Redis OR Valkey:
|
|
||||||
SESSION_STORE=postgres
|
|
||||||
CACHE_URL_FILE=/run/secrets/cache_url
|
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ data/
|
|||||||
backups/
|
backups/
|
||||||
# Runtime/build artifacts
|
# Runtime/build artifacts
|
||||||
.venv/
|
.venv/
|
||||||
|
.venv*/
|
||||||
__pycache__/
|
__pycache__/
|
||||||
*.py[cod]
|
*.py[cod]
|
||||||
node_modules/
|
node_modules/
|
||||||
|
|||||||
+25
@@ -0,0 +1,25 @@
|
|||||||
|
FROM python:3.12.6-slim-bookworm
|
||||||
|
|
||||||
|
ENV PYTHONDONTWRITEBYTECODE=1 \
|
||||||
|
PYTHONUNBUFFERED=1 \
|
||||||
|
PYTHONUTF8=1 \
|
||||||
|
APP_HOST=0.0.0.0 \
|
||||||
|
APP_PORT=8080
|
||||||
|
|
||||||
|
WORKDIR /app
|
||||||
|
|
||||||
|
COPY requirements.txt ./
|
||||||
|
RUN pip install --no-cache-dir --requirement requirements.txt
|
||||||
|
|
||||||
|
COPY src/ ./src/
|
||||||
|
COPY migrations/ ./migrations/
|
||||||
|
|
||||||
|
RUN useradd --create-home --uid 10001 appuser
|
||||||
|
USER appuser
|
||||||
|
|
||||||
|
EXPOSE 8080
|
||||||
|
|
||||||
|
HEALTHCHECK --interval=10s --timeout=3s --start-period=15s --retries=3 \
|
||||||
|
CMD python -c "import urllib.request,sys; sys.exit(0 if urllib.request.urlopen('http://127.0.0.1:8080/health/ready',timeout=2).status==200 else 1)"
|
||||||
|
|
||||||
|
CMD ["uvicorn", "src.app.main:app", "--host", "0.0.0.0", "--port", "8080", "--workers", "1", "--timeout-graceful-shutdown", "8"]
|
||||||
@@ -4,10 +4,10 @@ CA_FILE ?=
|
|||||||
ACTION_SCRIPT ?=
|
ACTION_SCRIPT ?=
|
||||||
TLS_ARGS = $(if $(CA_FILE),--ca-file "$(CA_FILE)",)
|
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 test-grade4 explain-history
|
||||||
|
|
||||||
help:
|
help:
|
||||||
@echo "check: offline structure; validate: full OpenAPI (requirements-dev.txt); test: running API; load: synthetic writes"
|
@echo "check: offline structure; validate: OpenAPI; test: public API smoke; test-grade4: auth/expiry/outage checks; explain-history: 10k-row query plan"
|
||||||
|
|
||||||
check:
|
check:
|
||||||
$(PYTHON) scripts/check_contract.py
|
$(PYTHON) scripts/check_contract.py
|
||||||
@@ -24,3 +24,9 @@ load:
|
|||||||
test-persistence:
|
test-persistence:
|
||||||
@test -n "$(ACTION_SCRIPT)" || (echo "Set ACTION_SCRIPT to your implemented executable restart hook"; exit 2)
|
@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)
|
$(PYTHON) tests/resilience.py --mode persistence --action-script "$(ACTION_SCRIPT)" --base-url "$(BASE_URL)" $(TLS_ARGS)
|
||||||
|
|
||||||
|
test-grade4:
|
||||||
|
$(PYTHON) tests/grade4.py --base-url "$(BASE_URL)"
|
||||||
|
|
||||||
|
explain-history:
|
||||||
|
docker compose exec -T api python -m src.app.explain_history
|
||||||
|
|||||||
@@ -92,10 +92,22 @@ Smoke не выставляет оценку автоматически. Про
|
|||||||
## Вопросы на защите
|
## Вопросы на защите
|
||||||
|
|
||||||
- Что подтверждает COMMIT и какие гарантии зависят от настройки durability?
|
- Что подтверждает COMMIT и какие гарантии зависят от настройки durability?
|
||||||
|
|
||||||
|
COMMIT подтверждает, что транзакция PostgreSQL успешно завершена. Создание чата и его участников выполняется атомарно: если операция завершается ошибкой до COMMIT, изменения откатываются. Долговечность зависит от WAL, fsync и надёжности диска; Docker volume сохраняет данные при перезапуске контейнера, но не защищает от потери диска.
|
||||||
- Где хранятся сессии и что будет после flush/restart Redis?
|
- Где хранятся сессии и что будет после flush/restart Redis?
|
||||||
|
|
||||||
|
Сессии хранятся в Redis с TTL. В Redis используется SHA-256 от случайного Bearer-токена как ключ, а значение содержит user_id и время истечения. При обычном перезапуске с сохранённым volume данные восстанавливаются из AOF/RDB. После FLUSHDB сессии исчезнут, и пользователям нужно будет войти снова. При аварии настройка appendfsync everysec может привести к потере примерно последней секунды записей.
|
||||||
- Чем аутентификация отличается от доступа к конкретному чату?
|
- Чем аутентификация отличается от доступа к конкретному чату?
|
||||||
|
|
||||||
|
Аутентификация отвечает на вопрос «кто пользователь?»: Basic используется для входа, затем Bearer-токен подтверждает сессию. Авторизация отвечает на вопрос «что пользователю разрешено?»: перед работой с чатом приложение проверяет членство пользователя в этом чате.
|
||||||
- Почему hash пароля и быстрый checksum имеют разные задачи?
|
- Почему hash пароля и быстрый checksum имеют разные задачи?
|
||||||
|
|
||||||
|
Для паролей используется Argon2id: он намеренно медленный и требует памяти, поэтому подбор пароля обходится дороже. SHA-256 для ключа Redis работает быстро. Это подходит для случайного Bearer-токена с высокой энтропией, но быстрый checksum не подходит для хранения паролей.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
2:22
|
||||||
|
|
||||||
## Первичные материалы
|
## Первичные материалы
|
||||||
|
|
||||||
- [PostgreSQL transactions](https://www.postgresql.org/docs/current/tutorial-transactions.html)
|
- [PostgreSQL transactions](https://www.postgresql.org/docs/current/tutorial-transactions.html)
|
||||||
|
|||||||
@@ -1,37 +1,62 @@
|
|||||||
# Отчёт — лабораторная 2
|
# Отчёт — лабораторная 2
|
||||||
|
|
||||||
- Автор, группа: TODO
|
- Автор, группа: Бадрадин Али, БВТ-2302
|
||||||
- Целевая оценка; выбранный трек 5, если нужен: TODO
|
- Целевая оценка: 4; трек 5 не заявлен
|
||||||
- Commit/PR: TODO после публикации
|
- Commit/PR: черновик PR #84 — https://gitea.privcloud.tech/mtusi2026/mtusi2026lab2/pulls/84
|
||||||
- Использованные источники, библиотеки и помощь ИИ: TODO
|
- Источники и помощь: контракт этого репозитория; документация FastAPI, SQLAlchemy, PostgreSQL, Redis и Argon2. ИИ использован для реализации и ревью.
|
||||||
|
|
||||||
## Запуск из чистого clone
|
## Запуск из чистого clone
|
||||||
|
|
||||||
TODO: версии runtime/образов, prerequisites, команды создания локальных secrets/.env, build, миграций, запуска, ожидания готовности, тестов и остановки. Указать необходимые действия оператора. Не вставлять секреты. Отдельно указать безопасную остановку и команду намеренного удаления учебных данных.
|
Версии закреплены в `requirements.txt` и `compose.yaml`: Python 3.12.6, FastAPI 0.115.0, SQLAlchemy 2.0.36, Psycopg 3.2.3, Redis client 5.2.0, Argon2-cffi 23.1.0, PostgreSQL 16.4 и Redis 7.4.1. Docker Compose v2 собирает API и запускает DB, Redis и одноразовую миграцию.
|
||||||
|
|
||||||
|
```powershell
|
||||||
|
Copy-Item .env.example .env
|
||||||
|
./scripts/create-secrets.ps1
|
||||||
|
docker compose --env-file .env config --quiet
|
||||||
|
docker compose --env-file .env up -d --build
|
||||||
|
python tests/smoke.py --base-url http://localhost:8080
|
||||||
|
```
|
||||||
|
|
||||||
|
Оператор создаёт secrets локально; их значения не добавляются в Git. `docker compose stop` или `docker compose down` сохраняют именованные volumes. Удаление учебной БД и Redis выполняется отдельно через `docker compose down --volumes`.
|
||||||
|
|
||||||
## Архитектура и решения
|
## Архитектура и решения
|
||||||
|
|
||||||
TODO: схема процессов/хранилищ, источник истины, транзакционные границы, ограничения. Что перенесено из lab1, что изменено в этой работе. Соответствие переменных из .env.example реальной конфигурации.
|
- PostgreSQL — источник истины для пользователей, хэшей паролей, чатов, членства и сообщений. Все SQL-операции параметризованы через SQLAlchemy. Миграция `001_initial.sql` выполняется в транзакции с advisory lock и записывает версию в `schema_migrations`.
|
||||||
|
- Чат и членство создаются атомарно. FK `(chat_id, sender_id)` не позволяет записать сообщение от неучастника. Уникальность username обеспечивается PostgreSQL, а не предварительной проверкой приложения.
|
||||||
|
- История читается по keyset `(created_at, id)` через `messages_history_idx(chat_id, created_at, id)`; полная история не загружается на каждую страницу.
|
||||||
|
- Argon2id хэширует пароли. Basic используется только для входа; Bearer-токены генерируются с 256 битами случайности. В Redis сохраняется SHA-256-ключ, идентификатор пользователя и абсолютное время истечения. Logout удаляет текущую запись.
|
||||||
|
- Redis — источник истины для сессий. `appendonly yes`, `appendfsync everysec`, периодический RDB snapshot и именованный volume сохраняют записи при штатном рестарте. При аварии возможна потеря последних записей примерно за одну секунду.
|
||||||
|
- Пул PostgreSQL ограничен 5 соединениями + 5 overflow; connect timeout 3 с, pool timeout 3 с, statement timeout 5 с. При отказе Redis/PostgreSQL аутентификация не деградирует в доверие к `X-User-Id`: запросы возвращают `503 unavailable`, readiness становится 503.
|
||||||
|
- Для сообщений кэш отсутствует: успешная запись и последующее чтение используют PostgreSQL, поэтому stale read от кэша невозможен.
|
||||||
|
|
||||||
## Доказательства критериев
|
## Проверки и доказательства
|
||||||
|
|
||||||
| Критерий README | Команда/сценарий | Наблюдаемый результат | Файл доказательства |
|
Таблица ниже фиксирует фактически выполненное в этой рабочей сессии. Не отмечайте подготовленный сценарий как пройденный до запуска с работающим Docker Engine.
|
||||||
|
|
||||||
|
| Критерий | Команда/сценарий | Результат | Доказательство |
|
||||||
| --- | --- | --- | --- |
|
| --- | --- | --- | --- |
|
||||||
| Публичный smoke | make test | TODO | TODO |
|
| Структура API и локальные ссылки | `python scripts/check_contract.py` | PASS | Вывод: `PASS: offline skeleton structure, local refs and Python syntax` |
|
||||||
| Остальные пункты уровня 3 | TODO | TODO | TODO |
|
| Цели Makefile | `make check`; `make test` | PASS | `evidence/check-output.txt`; `evidence/test-output.txt` |
|
||||||
| Все пункты уровня 4, если заявлен | TODO | TODO | TODO |
|
| Синтаксис Python | `python -m compileall -q src tests scripts` | PASS | exit code 0 |
|
||||||
| Выбранный трек 5, если заявлен | TODO | TODO | TODO |
|
| Конфигурация Compose и secrets | `docker compose config --quiet` | PASS; secrets и `.env` созданы локально и игнорируются Git | exit code 0 |
|
||||||
|
| In-process HTTP, ошибки и базовые проверки | `fastapi.testclient.TestClient` с нерабочими локальными адресами хранилищ | PASS: health, request ID, игнорирование `X-User-Id`, пустое тело сессии, ошибки валидации/malformed JSON, Argon2id, Basic parsing и cursor codec | вывод `PASS` в терминале |
|
||||||
|
| Недоступная PostgreSQL | `GET /health/ready` при закрытом localhost-порту | PASS: контролируемый `503 unavailable`, request ID совпадает | вывод `PASS` в терминале |
|
||||||
|
| README задания | `README.md` сохранён из исходного репозитория задания | PASS | `README.md`; инструкция запуска решения находится в этом отчёте |
|
||||||
|
| Публичный smoke | `python tests/smoke.py --base-url http://localhost:8080` | PASS | `evidence/test-output.txt`; вывод пользователя от 2026-10-23 |
|
||||||
|
| Пароль, expiry, logout, гонка регистрации, rollback чата | `$env:SESSION_TTL_SECONDS='10'; python tests/grade4.py --base-url http://localhost:8080` | PASS: все 8 сценариев | вывод пользователя от 2026-10-22; `tests/grade4.py` |
|
||||||
|
| Отказ Redis и PostgreSQL | часть `tests/grade4.py` | PASS: оба отказа возвращают ограниченный `503`; API и хранилища восстановлены | вывод пользователя от 2026-10-22 |
|
||||||
|
| Полный перезапуск сессии и данных | `python3 tests/resilience.py --mode persistence --action-script scripts/restart.sh --base-url http://localhost:8080` | PASS: сохранены подтверждённые данные и действующая сессия после перезапуска | вывод пользователя от 2026-10-23; hook: `scripts/restart.sh` (без удаления volumes) |
|
||||||
|
| История ≥10 000 сообщений | `docker compose --env-file .env exec -T api python -m src.app.explain_history` | PASS: `Index Scan` использует `messages_history_idx`; Execution Time 0,108 мс | `evidence/query-plan.json`; 3 shared hit blocks в узле Index Scan |
|
||||||
|
| Повторное применение миграций | `docker compose --env-file .env run --rm migrate` | PASS: одноразовый migrate-контейнер завершился без ошибки | вывод пользователя от 2026-10-23 |
|
||||||
|
|
||||||
TODO: добавить отдельную строку для каждого заявленного критерия. PASS без команды и наблюдения не является доказательством. Проверка инфраструктурного hook требует подтверждения, какие реальные процессы/хранилища перезапущены.
|
Локальный HTTP-прогон использовал установленное в рабочей среде FastAPI 0.139.0, Pydantic 2.13.4, SQLAlchemy 2.0.51 и Redis client 8.0.1; контейнер фиксирует версии из `requirements.txt`. Публичный smoke и полная suite grade4 подтверждены пользователем на Docker Compose 2026-10-22 и 2026-10-23; перезапуск с сохранением данных/сессии и повторное применение миграции также прошли 2026-10-23. План запроса истории снят в Docker: `Index Scan` по `messages_history_idx`, 0,108 мс на синтетическом наборе.
|
||||||
|
|
||||||
## Эксперименты и отказы
|
## Транзакции и план запроса
|
||||||
|
|
||||||
TODO: гипотеза, hardware/лимиты, версии, объём данных, профиль нагрузки, concurrency/arrival rate, длительность/прогрев, повторы, успешные RPS, ошибки, p50/p95/p99, CPU/RAM. Сохранить raw summary без tokens и объяснить ограничения измерения.
|
Регистрация делает один `INSERT`; конкурентный конфликт определяется ограничением `UNIQUE(username)` и возвращает одинаковый `409`. Создание чата проверяет существование участников и затем вставляет чат и все связи в одном `Session.begin()`. Ошибка до commit откатывает всю транзакцию. История использует сравнение строки `(created_at, id)` и `LIMIT`, согласованное с индексом `messages_history_idx`.
|
||||||
|
|
||||||
| Воздействие | Что ожидаем | Что наблюдали | Время восстановления / потеря данных |
|
`src/app/explain_history.py` создаёт 10 000 синтетических сообщений в транзакции, запускает `ANALYZE` и `EXPLAIN (ANALYZE, BUFFERS, FORMAT JSON)`, проверяет план и откатывает данные. Сохранённый `evidence/query-plan.json` показывает `Index Scan` через `messages_history_idx`, 3 shared hit blocks и время 0,108 мс. Это замер одного локального запуска, не универсальная оценка производительности.
|
||||||
| --- | --- | --- | --- |
|
|
||||||
| TODO | TODO | TODO | TODO |
|
|
||||||
|
|
||||||
## Ограничения и следующие шаги
|
## Ограничения
|
||||||
|
|
||||||
TODO: какие отказы выдерживаются и какие нет; что не реализовано; где учебное упрощение; какое улучшение подтверждено, а какое пока гипотеза.
|
Это локальный учебный стенд, не HA: один API, одна PostgreSQL и один Redis на одном Docker host. Перезапуск host/потеря диска не покрываются. AOF `everysec` не обещает сохранение каждой последней записи при внезапном обесточивании. TLS, лимиты скорости и защита от перебора паролей не входят в обязательный контракт Lab 2. Приложение не выдаёт производственный уровень безопасности.
|
||||||
|
|||||||
@@ -0,0 +1,96 @@
|
|||||||
|
name: mtusi-messenger-lab2
|
||||||
|
|
||||||
|
services:
|
||||||
|
db:
|
||||||
|
image: postgres:16.4-alpine
|
||||||
|
restart: unless-stopped
|
||||||
|
environment:
|
||||||
|
POSTGRES_DB: messenger
|
||||||
|
POSTGRES_USER: messenger
|
||||||
|
POSTGRES_PASSWORD_FILE: /run/secrets/postgres_password
|
||||||
|
secrets:
|
||||||
|
- postgres_password
|
||||||
|
volumes:
|
||||||
|
- postgres_data:/var/lib/postgresql/data
|
||||||
|
healthcheck:
|
||||||
|
test: ["CMD-SHELL", "pg_isready -U messenger -d messenger"]
|
||||||
|
interval: 3s
|
||||||
|
timeout: 3s
|
||||||
|
retries: 20
|
||||||
|
start_period: 5s
|
||||||
|
|
||||||
|
redis:
|
||||||
|
image: redis:7.4.1-alpine
|
||||||
|
restart: unless-stopped
|
||||||
|
command:
|
||||||
|
- sh
|
||||||
|
- -ec
|
||||||
|
- exec redis-server --dir /data --appendonly yes --appendfsync everysec --save 60 1 --requirepass "$$(cat /run/secrets/redis_password)"
|
||||||
|
secrets:
|
||||||
|
- redis_password
|
||||||
|
volumes:
|
||||||
|
- redis_data:/data
|
||||||
|
healthcheck:
|
||||||
|
test: ["CMD-SHELL", "REDISCLI_AUTH=\"$$(cat /run/secrets/redis_password)\" redis-cli ping | grep -q PONG"]
|
||||||
|
interval: 3s
|
||||||
|
timeout: 3s
|
||||||
|
retries: 20
|
||||||
|
start_period: 5s
|
||||||
|
|
||||||
|
migrate:
|
||||||
|
build:
|
||||||
|
context: .
|
||||||
|
dockerfile: Dockerfile
|
||||||
|
command: ["python", "-m", "src.app.migrate"]
|
||||||
|
environment:
|
||||||
|
DATABASE_URL_FILE: /run/secrets/database_url
|
||||||
|
secrets:
|
||||||
|
- database_url
|
||||||
|
depends_on:
|
||||||
|
db:
|
||||||
|
condition: service_healthy
|
||||||
|
restart: "no"
|
||||||
|
|
||||||
|
api:
|
||||||
|
build:
|
||||||
|
context: .
|
||||||
|
dockerfile: Dockerfile
|
||||||
|
restart: unless-stopped
|
||||||
|
environment:
|
||||||
|
APP_HOST: 0.0.0.0
|
||||||
|
APP_PORT: 8080
|
||||||
|
SESSION_TTL_SECONDS: ${SESSION_TTL_SECONDS:-3600}
|
||||||
|
DATABASE_URL_FILE: /run/secrets/database_url
|
||||||
|
CACHE_URL_FILE: /run/secrets/cache_url
|
||||||
|
secrets:
|
||||||
|
- database_url
|
||||||
|
- cache_url
|
||||||
|
ports:
|
||||||
|
- "127.0.0.1:${APP_PORT:-8080}:8080"
|
||||||
|
depends_on:
|
||||||
|
db:
|
||||||
|
condition: service_healthy
|
||||||
|
redis:
|
||||||
|
condition: service_healthy
|
||||||
|
migrate:
|
||||||
|
condition: service_completed_successfully
|
||||||
|
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: 10s
|
||||||
|
|
||||||
|
secrets:
|
||||||
|
postgres_password:
|
||||||
|
file: ./secrets/postgres_password
|
||||||
|
redis_password:
|
||||||
|
file: ./secrets/redis_password
|
||||||
|
database_url:
|
||||||
|
file: ./secrets/database_url
|
||||||
|
cache_url:
|
||||||
|
file: ./secrets/cache_url
|
||||||
|
|
||||||
|
volumes:
|
||||||
|
postgres_data:
|
||||||
|
redis_data:
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
python3 scripts/check_contract.py
|
||||||
|
PASS: offline skeleton structure, local refs and Python syntax; application NOT tested.
|
||||||
@@ -0,0 +1,72 @@
|
|||||||
|
{
|
||||||
|
"Execution Time": 0.108,
|
||||||
|
"Plan": {
|
||||||
|
"Actual Loops": 1,
|
||||||
|
"Actual Rows": 50,
|
||||||
|
"Actual Startup Time": 0.02,
|
||||||
|
"Actual Total Time": 0.03,
|
||||||
|
"Async Capable": false,
|
||||||
|
"Local Dirtied Blocks": 0,
|
||||||
|
"Local Hit Blocks": 0,
|
||||||
|
"Local Read Blocks": 0,
|
||||||
|
"Local Written Blocks": 0,
|
||||||
|
"Node Type": "Limit",
|
||||||
|
"Parallel Aware": false,
|
||||||
|
"Plan Rows": 50,
|
||||||
|
"Plan Width": 78,
|
||||||
|
"Plans": [
|
||||||
|
{
|
||||||
|
"Actual Loops": 1,
|
||||||
|
"Actual Rows": 50,
|
||||||
|
"Actual Startup Time": 0.018,
|
||||||
|
"Actual Total Time": 0.024,
|
||||||
|
"Alias": "messages",
|
||||||
|
"Async Capable": false,
|
||||||
|
"Index Cond": "((chat_id = '4a2cebc7-0477-4352-bda4-8d4bf06cf6f3'::uuid) AND (ROW(created_at, id) > ROW('2025-01-01 00:00:00+00'::timestamp with time zone, '00000000-0000-0000-0000-000000000000'::uuid)))",
|
||||||
|
"Index Name": "messages_history_idx",
|
||||||
|
"Local Dirtied Blocks": 0,
|
||||||
|
"Local Hit Blocks": 0,
|
||||||
|
"Local Read Blocks": 0,
|
||||||
|
"Local Written Blocks": 0,
|
||||||
|
"Node Type": "Index Scan",
|
||||||
|
"Parallel Aware": false,
|
||||||
|
"Parent Relationship": "Outer",
|
||||||
|
"Plan Rows": 9999,
|
||||||
|
"Plan Width": 78,
|
||||||
|
"Relation Name": "messages",
|
||||||
|
"Rows Removed by Index Recheck": 0,
|
||||||
|
"Scan Direction": "Forward",
|
||||||
|
"Shared Dirtied Blocks": 0,
|
||||||
|
"Shared Hit Blocks": 3,
|
||||||
|
"Shared Read Blocks": 0,
|
||||||
|
"Shared Written Blocks": 0,
|
||||||
|
"Startup Cost": 0.29,
|
||||||
|
"Temp Read Blocks": 0,
|
||||||
|
"Temp Written Blocks": 0,
|
||||||
|
"Total Cost": 808.37
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"Shared Dirtied Blocks": 0,
|
||||||
|
"Shared Hit Blocks": 3,
|
||||||
|
"Shared Read Blocks": 0,
|
||||||
|
"Shared Written Blocks": 0,
|
||||||
|
"Startup Cost": 0.29,
|
||||||
|
"Temp Read Blocks": 0,
|
||||||
|
"Temp Written Blocks": 0,
|
||||||
|
"Total Cost": 4.33
|
||||||
|
},
|
||||||
|
"Planning": {
|
||||||
|
"Local Dirtied Blocks": 0,
|
||||||
|
"Local Hit Blocks": 0,
|
||||||
|
"Local Read Blocks": 0,
|
||||||
|
"Local Written Blocks": 0,
|
||||||
|
"Shared Dirtied Blocks": 0,
|
||||||
|
"Shared Hit Blocks": 39,
|
||||||
|
"Shared Read Blocks": 0,
|
||||||
|
"Shared Written Blocks": 0,
|
||||||
|
"Temp Read Blocks": 0,
|
||||||
|
"Temp Written Blocks": 0
|
||||||
|
},
|
||||||
|
"Planning Time": 0.502,
|
||||||
|
"Triggers": []
|
||||||
|
}
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
python3 tests/smoke.py --base-url "http://localhost:8080"
|
||||||
|
PASS: lab2 public API scenario; infrastructure and grading evidence remain separate.
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
CREATE TABLE users (
|
||||||
|
id UUID PRIMARY KEY,
|
||||||
|
username VARCHAR(32) NOT NULL UNIQUE
|
||||||
|
CHECK (username ~ '^[a-z0-9_]{3,32}$'),
|
||||||
|
display_name VARCHAR(100) NOT NULL
|
||||||
|
CHECK (char_length(display_name) BETWEEN 1 AND 100 AND btrim(display_name) <> ''),
|
||||||
|
password_hash TEXT NOT NULL,
|
||||||
|
created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE TABLE chats (
|
||||||
|
id UUID PRIMARY KEY,
|
||||||
|
title VARCHAR(100) NOT NULL
|
||||||
|
CHECK (char_length(title) BETWEEN 1 AND 100 AND btrim(title) <> ''),
|
||||||
|
created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE TABLE chat_members (
|
||||||
|
chat_id UUID NOT NULL REFERENCES chats(id) ON DELETE CASCADE,
|
||||||
|
user_id UUID NOT NULL REFERENCES users(id) ON DELETE RESTRICT,
|
||||||
|
PRIMARY KEY (chat_id, user_id)
|
||||||
|
);
|
||||||
|
CREATE INDEX chat_members_user_id_idx ON chat_members(user_id, chat_id);
|
||||||
|
|
||||||
|
CREATE TABLE messages (
|
||||||
|
id UUID PRIMARY KEY,
|
||||||
|
chat_id UUID NOT NULL,
|
||||||
|
sender_id UUID NOT NULL,
|
||||||
|
text VARCHAR(4000) NOT NULL
|
||||||
|
CHECK (char_length(text) BETWEEN 1 AND 4000 AND btrim(text) <> ''),
|
||||||
|
created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
CONSTRAINT messages_sender_is_chat_member_fk
|
||||||
|
FOREIGN KEY (chat_id, sender_id)
|
||||||
|
REFERENCES chat_members(chat_id, user_id) ON DELETE CASCADE
|
||||||
|
);
|
||||||
|
CREATE INDEX messages_history_idx ON messages(chat_id, created_at, id);
|
||||||
@@ -1,2 +1,3 @@
|
|||||||
# Only for full OpenAPI/JSON Schema validation; HTTP tests use stdlib.
|
# Only for full OpenAPI/JSON Schema validation; HTTP tests use stdlib.
|
||||||
openapi-spec-validator==0.7.2
|
openapi-spec-validator==0.7.2
|
||||||
|
pytest==8.3.3
|
||||||
|
|||||||
@@ -0,0 +1,7 @@
|
|||||||
|
fastapi==0.115.0
|
||||||
|
uvicorn[standard]==0.30.6
|
||||||
|
pydantic==2.9.2
|
||||||
|
SQLAlchemy==2.0.36
|
||||||
|
psycopg[binary]==3.2.3
|
||||||
|
redis==5.2.0
|
||||||
|
argon2-cffi==23.1.0
|
||||||
@@ -0,0 +1,33 @@
|
|||||||
|
$ErrorActionPreference = 'Stop'
|
||||||
|
$repo = Split-Path -Parent $PSScriptRoot
|
||||||
|
$secretDir = Join-Path $repo 'secrets'
|
||||||
|
New-Item -ItemType Directory -Path $secretDir -Force | Out-Null
|
||||||
|
$secretNames = @('postgres_password', 'redis_password', 'database_url', 'cache_url')
|
||||||
|
$existing = @($secretNames | Where-Object { Test-Path -LiteralPath (Join-Path $secretDir $_) })
|
||||||
|
if ($existing.Count -eq $secretNames.Count) {
|
||||||
|
Write-Output 'All local secret files already exist; they were left unchanged.'
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if ($existing.Count -gt 0) {
|
||||||
|
throw 'Some but not all local secret files exist. Resolve them manually; this script will not rotate credentials.'
|
||||||
|
}
|
||||||
|
|
||||||
|
function New-Secret {
|
||||||
|
$bytes = New-Object byte[] 32
|
||||||
|
[System.Security.Cryptography.RandomNumberGenerator]::Fill($bytes)
|
||||||
|
return [Convert]::ToBase64String($bytes).TrimEnd('=').Replace('+', '-').Replace('/', '_')
|
||||||
|
}
|
||||||
|
|
||||||
|
$postgresPassword = New-Secret
|
||||||
|
$redisPassword = New-Secret
|
||||||
|
$files = @{
|
||||||
|
postgres_password = $postgresPassword
|
||||||
|
redis_password = $redisPassword
|
||||||
|
database_url = "postgresql+psycopg://messenger:$postgresPassword@db:5432/messenger"
|
||||||
|
cache_url = "redis://:$redisPassword@redis:6379/0"
|
||||||
|
}
|
||||||
|
foreach ($name in $secretNames) {
|
||||||
|
$path = Join-Path $secretDir $name
|
||||||
|
[System.IO.File]::WriteAllText($path, $files[$name], [System.Text.UTF8Encoding]::new($false))
|
||||||
|
}
|
||||||
|
Write-Output 'Local Postgres and Redis credentials created under secrets/. Values were not displayed.'
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
#!/usr/bin/env sh
|
||||||
|
set -eu
|
||||||
|
cd "$(dirname "$0")/.."
|
||||||
|
|
||||||
|
# Stop the API before its state stores; named volumes are left intact.
|
||||||
|
docker compose stop api redis db
|
||||||
|
docker compose start db redis
|
||||||
|
|
||||||
|
attempt=0
|
||||||
|
until docker compose exec -T db pg_isready -U messenger -d messenger >/dev/null 2>&1 \
|
||||||
|
&& docker compose exec -T redis sh -ec 'REDISCLI_AUTH="$(cat /run/secrets/redis_password)" redis-cli ping | grep -q PONG' >/dev/null 2>&1; do
|
||||||
|
attempt=$((attempt + 1))
|
||||||
|
if [ "$attempt" -ge 60 ]; then
|
||||||
|
echo 'Database/cache did not become ready within 60 seconds.' >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
sleep 1
|
||||||
|
done
|
||||||
|
|
||||||
|
docker compose start api
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
"""MTUSI Lab 2 API."""
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
from sqlalchemy import create_engine
|
||||||
|
from sqlalchemy.orm import sessionmaker
|
||||||
|
|
||||||
|
from .settings import load_database_url
|
||||||
|
|
||||||
|
|
||||||
|
engine = create_engine(
|
||||||
|
load_database_url(),
|
||||||
|
pool_size=5,
|
||||||
|
max_overflow=5,
|
||||||
|
pool_timeout=3,
|
||||||
|
pool_recycle=1800,
|
||||||
|
pool_pre_ping=True,
|
||||||
|
connect_args={
|
||||||
|
"connect_timeout": 3,
|
||||||
|
"options": "-c statement_timeout=5000",
|
||||||
|
},
|
||||||
|
)
|
||||||
|
SessionLocal = sessionmaker(bind=engine, expire_on_commit=False)
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
from fastapi import Request
|
||||||
|
from fastapi.responses import JSONResponse
|
||||||
|
|
||||||
|
|
||||||
|
class ApiError(Exception):
|
||||||
|
def __init__(self, code: str, message: str, status: int, challenge: str | None = None):
|
||||||
|
super().__init__(message)
|
||||||
|
self.code = code
|
||||||
|
self.message = message
|
||||||
|
self.status = status
|
||||||
|
self.challenge = challenge
|
||||||
|
|
||||||
|
|
||||||
|
def error_response(request: Request, code: str, message: str, status: int, challenge: str | None = None):
|
||||||
|
request_id = getattr(request.state, "request_id", "unknown")
|
||||||
|
headers = {"X-Request-Id": request_id, "Cache-Control": "no-store"}
|
||||||
|
if challenge:
|
||||||
|
headers["WWW-Authenticate"] = challenge
|
||||||
|
return JSONResponse(
|
||||||
|
status_code=status,
|
||||||
|
headers=headers,
|
||||||
|
content={"error": {"code": code, "message": message, "request_id": request_id}},
|
||||||
|
)
|
||||||
@@ -0,0 +1,71 @@
|
|||||||
|
"""Create 10,000 disposable rows and record the indexed keyset-page plan."""
|
||||||
|
import json
|
||||||
|
import uuid
|
||||||
|
|
||||||
|
from sqlalchemy import text
|
||||||
|
|
||||||
|
from .db import engine
|
||||||
|
|
||||||
|
|
||||||
|
def run() -> None:
|
||||||
|
user_id = uuid.uuid4()
|
||||||
|
chat_id = uuid.uuid4()
|
||||||
|
plan_json = None
|
||||||
|
with engine.connect() as connection:
|
||||||
|
transaction = connection.begin()
|
||||||
|
try:
|
||||||
|
connection.execute(
|
||||||
|
text("INSERT INTO users(id, username, display_name, password_hash) "
|
||||||
|
"VALUES (:id, :username, 'Synthetic plan fixture', 'not-a-login-hash')"),
|
||||||
|
{"id": user_id, "username": "plan_" + uuid.uuid4().hex[:20]},
|
||||||
|
)
|
||||||
|
connection.execute(
|
||||||
|
text("INSERT INTO chats(id, title) VALUES (:id, 'Synthetic plan fixture')"),
|
||||||
|
{"id": chat_id},
|
||||||
|
)
|
||||||
|
connection.execute(
|
||||||
|
text("INSERT INTO chat_members(chat_id, user_id) VALUES (:chat_id, :user_id)"),
|
||||||
|
{"chat_id": chat_id, "user_id": user_id},
|
||||||
|
)
|
||||||
|
connection.execute(
|
||||||
|
text("""
|
||||||
|
INSERT INTO messages(id, chat_id, sender_id, text, created_at)
|
||||||
|
SELECT gen_random_uuid(), :chat_id, :sender_id,
|
||||||
|
'synthetic message ' || n::text,
|
||||||
|
TIMESTAMPTZ '2025-01-01 00:00:00+00' + n * INTERVAL '1 microsecond'
|
||||||
|
FROM generate_series(1, 10000) AS n
|
||||||
|
"""),
|
||||||
|
{"chat_id": chat_id, "sender_id": user_id},
|
||||||
|
)
|
||||||
|
connection.exec_driver_sql("ANALYZE messages")
|
||||||
|
raw = connection.execute(
|
||||||
|
text("""
|
||||||
|
EXPLAIN (ANALYZE, BUFFERS, FORMAT JSON)
|
||||||
|
SELECT id, chat_id, sender_id, text, created_at
|
||||||
|
FROM messages
|
||||||
|
WHERE chat_id = :chat_id
|
||||||
|
AND (created_at, id) > (TIMESTAMPTZ '2025-01-01 00:00:00+00',
|
||||||
|
'00000000-0000-0000-0000-000000000000'::uuid)
|
||||||
|
ORDER BY created_at, id
|
||||||
|
LIMIT 50
|
||||||
|
"""),
|
||||||
|
{"chat_id": chat_id},
|
||||||
|
).scalar_one()
|
||||||
|
plan_json = raw if isinstance(raw, list) else json.loads(raw)
|
||||||
|
finally:
|
||||||
|
transaction.rollback()
|
||||||
|
|
||||||
|
document = plan_json[0]
|
||||||
|
|
||||||
|
def contains_history_index(node: dict) -> bool:
|
||||||
|
if node.get("Index Name") == "messages_history_idx":
|
||||||
|
return True
|
||||||
|
return any(contains_history_index(child) for child in node.get("Plans", []))
|
||||||
|
|
||||||
|
if not contains_history_index(document["Plan"]):
|
||||||
|
raise SystemExit("Planner did not use messages_history_idx; inspect and revise the query/index.")
|
||||||
|
print(json.dumps(document, indent=2, sort_keys=True))
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
run()
|
||||||
+456
@@ -0,0 +1,456 @@
|
|||||||
|
import base64
|
||||||
|
import binascii
|
||||||
|
from datetime import datetime, timedelta, timezone
|
||||||
|
import hashlib
|
||||||
|
import json
|
||||||
|
import logging
|
||||||
|
import re
|
||||||
|
import secrets
|
||||||
|
import uuid
|
||||||
|
|
||||||
|
from argon2 import PasswordHasher, Type
|
||||||
|
from argon2.exceptions import InvalidHashError, VerificationError, VerifyMismatchError
|
||||||
|
from fastapi import FastAPI, Request
|
||||||
|
from fastapi.exceptions import RequestValidationError
|
||||||
|
from fastapi.responses import JSONResponse
|
||||||
|
from redis import Redis
|
||||||
|
from redis.exceptions import RedisError
|
||||||
|
from sqlalchemy import and_, func, or_, select, text, tuple_
|
||||||
|
from sqlalchemy.exc import DBAPIError, OperationalError, TimeoutError as SQLAlchemyTimeoutError
|
||||||
|
from starlette.concurrency import run_in_threadpool
|
||||||
|
from starlette.exceptions import HTTPException as StarletteHTTPException
|
||||||
|
|
||||||
|
from .db import SessionLocal, engine
|
||||||
|
from .errors import ApiError, error_response
|
||||||
|
from .models import Chat, ChatMember, Message, User
|
||||||
|
from .schemas import (
|
||||||
|
ChatOut,
|
||||||
|
ChatPage,
|
||||||
|
CreateChat,
|
||||||
|
CreateMessage,
|
||||||
|
CreateUser,
|
||||||
|
Health,
|
||||||
|
MessageOut,
|
||||||
|
MessagePage,
|
||||||
|
SessionOut,
|
||||||
|
UserOut,
|
||||||
|
)
|
||||||
|
from .settings import load_settings
|
||||||
|
|
||||||
|
settings = load_settings()
|
||||||
|
|
||||||
|
|
||||||
|
logging.basicConfig(
|
||||||
|
level=logging.INFO,
|
||||||
|
format="%(asctime)s %(levelname)s %(name)s %(message)s",
|
||||||
|
)
|
||||||
|
logger = logging.getLogger("messenger.api")
|
||||||
|
password_hasher = PasswordHasher(
|
||||||
|
time_cost=2,
|
||||||
|
memory_cost=19_456,
|
||||||
|
parallelism=1,
|
||||||
|
hash_len=32,
|
||||||
|
salt_len=16,
|
||||||
|
type=Type.ID,
|
||||||
|
)
|
||||||
|
DUMMY_PASSWORD_HASH = password_hasher.hash("dummy-password-for-timing-only")
|
||||||
|
redis_client = Redis.from_url(
|
||||||
|
settings.cache_url,
|
||||||
|
decode_responses=True,
|
||||||
|
socket_connect_timeout=2,
|
||||||
|
socket_timeout=2,
|
||||||
|
health_check_interval=30,
|
||||||
|
)
|
||||||
|
|
||||||
|
app = FastAPI(title="MTUSI Messenger — Lab 2", version="1.2.0")
|
||||||
|
|
||||||
|
BEARER_CHALLENGE = 'Bearer realm="messenger"'
|
||||||
|
BASIC_CHALLENGE = 'Basic realm="messenger", charset="UTF-8"'
|
||||||
|
USERNAME_RE = re.compile(r"^[a-z0-9_]{3,32}$")
|
||||||
|
TOKEN_RE = re.compile(r"^[A-Za-z0-9_-]{43}$")
|
||||||
|
|
||||||
|
|
||||||
|
def _iso(value: datetime) -> str:
|
||||||
|
if value.tzinfo is None:
|
||||||
|
value = value.replace(tzinfo=timezone.utc)
|
||||||
|
return value.astimezone(timezone.utc).isoformat(timespec="microseconds").replace("+00:00", "Z")
|
||||||
|
|
||||||
|
|
||||||
|
def _user_out(user: User) -> dict:
|
||||||
|
return {
|
||||||
|
"id": str(user.id),
|
||||||
|
"username": user.username,
|
||||||
|
"display_name": user.display_name,
|
||||||
|
"created_at": _iso(user.created_at),
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def _message_out(message: Message) -> dict:
|
||||||
|
return {
|
||||||
|
"id": str(message.id),
|
||||||
|
"chat_id": str(message.chat_id),
|
||||||
|
"sender_id": str(message.sender_id),
|
||||||
|
"text": message.text,
|
||||||
|
"created_at": _iso(message.created_at),
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def _chat_out(session, chat: Chat) -> dict:
|
||||||
|
members = session.scalars(
|
||||||
|
select(ChatMember.user_id)
|
||||||
|
.where(ChatMember.chat_id == chat.id)
|
||||||
|
.order_by(ChatMember.user_id)
|
||||||
|
).all()
|
||||||
|
return {
|
||||||
|
"id": str(chat.id),
|
||||||
|
"title": chat.title,
|
||||||
|
"member_ids": [str(member_id) for member_id in members],
|
||||||
|
"created_at": _iso(chat.created_at),
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def _api_error(code: str, message: str, status: int, challenge: str | None = None) -> ApiError:
|
||||||
|
return ApiError(code, message, status, challenge)
|
||||||
|
|
||||||
|
|
||||||
|
def _unauthorized(challenge: str = BEARER_CHALLENGE) -> ApiError:
|
||||||
|
return _api_error("unauthorized", "Нет действительной идентификации", 401, challenge)
|
||||||
|
|
||||||
|
|
||||||
|
def _encode_cursor(payload: dict) -> str:
|
||||||
|
raw = json.dumps(payload, separators=(",", ":"), sort_keys=True).encode("utf-8")
|
||||||
|
return base64.urlsafe_b64encode(raw).decode("ascii").rstrip("=")
|
||||||
|
|
||||||
|
|
||||||
|
def _decode_cursor(value: str) -> dict:
|
||||||
|
try:
|
||||||
|
padded = value + ("=" * ((4 - len(value) % 4) % 4))
|
||||||
|
raw = base64.b64decode(padded.encode("ascii"), altchars=b"-_", validate=True)
|
||||||
|
payload = json.loads(raw.decode("utf-8"))
|
||||||
|
if not isinstance(payload, dict):
|
||||||
|
raise ValueError("cursor must be an object")
|
||||||
|
return payload
|
||||||
|
except (ValueError, UnicodeError, binascii.Error, json.JSONDecodeError) as exc:
|
||||||
|
raise _api_error("invalid_request", "Неверный cursor", 400) from exc
|
||||||
|
|
||||||
|
|
||||||
|
def _cursor_position(payload: dict) -> tuple[datetime, uuid.UUID]:
|
||||||
|
try:
|
||||||
|
created_at = datetime.fromisoformat(payload["t"].replace("Z", "+00:00"))
|
||||||
|
if created_at.tzinfo is None:
|
||||||
|
raise ValueError("cursor time must include timezone")
|
||||||
|
item_id = uuid.UUID(payload["i"])
|
||||||
|
return created_at.astimezone(timezone.utc), item_id
|
||||||
|
except (KeyError, TypeError, ValueError, AttributeError) as exc:
|
||||||
|
raise _api_error("invalid_request", "Неверный cursor", 400) from exc
|
||||||
|
|
||||||
|
|
||||||
|
def _session_key(token: str) -> str:
|
||||||
|
digest = hashlib.sha256(token.encode("ascii")).hexdigest()
|
||||||
|
return "session:v1:" + digest
|
||||||
|
|
||||||
|
|
||||||
|
def _parse_bearer(request: Request) -> str:
|
||||||
|
header = request.headers.get("authorization", "")
|
||||||
|
scheme, separator, token = header.partition(" ")
|
||||||
|
if scheme.lower() != "bearer" or not separator or not TOKEN_RE.fullmatch(token):
|
||||||
|
raise _unauthorized()
|
||||||
|
return token
|
||||||
|
|
||||||
|
|
||||||
|
def _load_session(token: str) -> dict:
|
||||||
|
value = redis_client.get(_session_key(token))
|
||||||
|
if value is None:
|
||||||
|
raise _unauthorized()
|
||||||
|
try:
|
||||||
|
session = json.loads(value)
|
||||||
|
user_id = uuid.UUID(session["user_id"])
|
||||||
|
expires_at = datetime.fromisoformat(session["expires_at"].replace("Z", "+00:00"))
|
||||||
|
if expires_at.tzinfo is None or datetime.now(timezone.utc) >= expires_at:
|
||||||
|
redis_client.delete(_session_key(token))
|
||||||
|
raise _unauthorized()
|
||||||
|
return {"user_id": user_id, "expires_at": expires_at}
|
||||||
|
except ApiError:
|
||||||
|
raise
|
||||||
|
except (KeyError, TypeError, ValueError, AttributeError, json.JSONDecodeError):
|
||||||
|
redis_client.delete(_session_key(token))
|
||||||
|
raise _unauthorized() from None
|
||||||
|
|
||||||
|
|
||||||
|
def _current_user(request: Request) -> tuple[User, str]:
|
||||||
|
token = _parse_bearer(request)
|
||||||
|
session_data = _load_session(token)
|
||||||
|
with SessionLocal() as session:
|
||||||
|
user = session.get(User, session_data["user_id"])
|
||||||
|
if user is None:
|
||||||
|
redis_client.delete(_session_key(token))
|
||||||
|
raise _unauthorized()
|
||||||
|
return user, token
|
||||||
|
|
||||||
|
|
||||||
|
def _parse_basic(header: str) -> tuple[str, str]:
|
||||||
|
scheme, separator, encoded = header.partition(" ")
|
||||||
|
if scheme.lower() != "basic" or not separator or not encoded:
|
||||||
|
raise _unauthorized(BASIC_CHALLENGE)
|
||||||
|
try:
|
||||||
|
decoded = base64.b64decode(encoded.encode("ascii"), validate=True).decode("utf-8")
|
||||||
|
username, password = decoded.split(":", 1)
|
||||||
|
except (UnicodeError, ValueError, binascii.Error):
|
||||||
|
raise _unauthorized(BASIC_CHALLENGE) from None
|
||||||
|
if not USERNAME_RE.fullmatch(username) or not password:
|
||||||
|
raise _unauthorized(BASIC_CHALLENGE)
|
||||||
|
return username, password
|
||||||
|
|
||||||
|
|
||||||
|
def _password_matches(password_hash: str, password: str) -> bool:
|
||||||
|
try:
|
||||||
|
return password_hasher.verify(password_hash, password)
|
||||||
|
except (VerifyMismatchError, VerificationError, InvalidHashError):
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
def _create_session(username: str, password: str) -> dict:
|
||||||
|
with SessionLocal() as session:
|
||||||
|
user = session.scalar(select(User).where(User.username == username))
|
||||||
|
stored_hash = user.password_hash if user is not None else DUMMY_PASSWORD_HASH
|
||||||
|
password_ok = _password_matches(stored_hash, password)
|
||||||
|
if user is None or not password_ok:
|
||||||
|
raise _unauthorized(BASIC_CHALLENGE)
|
||||||
|
user_payload = _user_out(user)
|
||||||
|
|
||||||
|
expires_at = datetime.now(timezone.utc) + timedelta(seconds=settings.session_ttl_seconds)
|
||||||
|
for _ in range(3):
|
||||||
|
token = secrets.token_urlsafe(32)
|
||||||
|
stored_value = json.dumps(
|
||||||
|
{"user_id": user_payload["id"], "expires_at": _iso(expires_at)},
|
||||||
|
separators=(",", ":"),
|
||||||
|
)
|
||||||
|
if redis_client.set(_session_key(token), stored_value, ex=settings.session_ttl_seconds, nx=True):
|
||||||
|
return {
|
||||||
|
"token": token,
|
||||||
|
"token_type": "Bearer",
|
||||||
|
"expires_at": _iso(expires_at),
|
||||||
|
"user": user_payload,
|
||||||
|
}
|
||||||
|
logger.error("session_token_collision_limit_reached")
|
||||||
|
raise _api_error("unavailable", "Сервис временно недоступен", 503)
|
||||||
|
|
||||||
|
|
||||||
|
def _get_chat_for_member(session, chat_id: uuid.UUID, user_id: uuid.UUID) -> Chat | None:
|
||||||
|
return session.scalar(
|
||||||
|
select(Chat)
|
||||||
|
.join(ChatMember, ChatMember.chat_id == Chat.id)
|
||||||
|
.where(Chat.id == chat_id, ChatMember.user_id == user_id)
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@app.middleware("http")
|
||||||
|
async def request_id_middleware(request: Request, call_next):
|
||||||
|
request_id = str(uuid.uuid4())
|
||||||
|
request.state.request_id = request_id
|
||||||
|
response = await call_next(request)
|
||||||
|
response.headers["X-Request-Id"] = request_id
|
||||||
|
return response
|
||||||
|
|
||||||
|
|
||||||
|
@app.exception_handler(ApiError)
|
||||||
|
async def handle_api_error(request: Request, exc: ApiError):
|
||||||
|
return error_response(request, exc.code, exc.message, exc.status, exc.challenge)
|
||||||
|
|
||||||
|
|
||||||
|
@app.exception_handler(RequestValidationError)
|
||||||
|
async def handle_validation_error(request: Request, _exc: RequestValidationError):
|
||||||
|
return error_response(request, "invalid_request", "Неверный запрос", 400)
|
||||||
|
|
||||||
|
|
||||||
|
@app.exception_handler(StarletteHTTPException)
|
||||||
|
async def handle_http_error(request: Request, exc: StarletteHTTPException):
|
||||||
|
if exc.status_code == 404:
|
||||||
|
return error_response(request, "not_found", "Объект не найден", 404)
|
||||||
|
return error_response(request, "invalid_request", "Неверный запрос", 400)
|
||||||
|
|
||||||
|
|
||||||
|
@app.exception_handler(OperationalError)
|
||||||
|
@app.exception_handler(SQLAlchemyTimeoutError)
|
||||||
|
async def handle_database_unavailable(request: Request, exc: Exception):
|
||||||
|
logger.warning("database_unavailable request_id=%s type=%s", request.state.request_id, type(exc).__name__)
|
||||||
|
return error_response(request, "unavailable", "Сервис временно недоступен", 503)
|
||||||
|
|
||||||
|
|
||||||
|
@app.exception_handler(RedisError)
|
||||||
|
async def handle_cache_unavailable(request: Request, exc: RedisError):
|
||||||
|
logger.warning("session_store_unavailable request_id=%s type=%s", request.state.request_id, type(exc).__name__)
|
||||||
|
return error_response(request, "unavailable", "Сервис временно недоступен", 503)
|
||||||
|
|
||||||
|
|
||||||
|
@app.exception_handler(DBAPIError)
|
||||||
|
async def handle_database_error(request: Request, exc: DBAPIError):
|
||||||
|
if exc.connection_invalidated:
|
||||||
|
logger.warning("database_connection_lost request_id=%s", request.state.request_id)
|
||||||
|
return error_response(request, "unavailable", "Сервис временно недоступен", 503)
|
||||||
|
logger.error("database_error request_id=%s type=%s", request.state.request_id, type(exc).__name__)
|
||||||
|
return error_response(request, "internal_error", "Внутренняя ошибка", 500)
|
||||||
|
|
||||||
|
|
||||||
|
@app.exception_handler(Exception)
|
||||||
|
async def handle_unexpected_error(request: Request, exc: Exception):
|
||||||
|
logger.error("internal_error request_id=%s type=%s", request.state.request_id, type(exc).__name__)
|
||||||
|
return error_response(request, "internal_error", "Внутренняя ошибка", 500)
|
||||||
|
|
||||||
|
|
||||||
|
@app.get("/health/live", response_model=Health)
|
||||||
|
def live():
|
||||||
|
return {"status": "ok"}
|
||||||
|
|
||||||
|
|
||||||
|
@app.get("/health/ready", response_model=Health, responses={503: {"description": "Dependency unavailable"}})
|
||||||
|
def ready():
|
||||||
|
with SessionLocal() as session:
|
||||||
|
session.execute(text("SELECT 1"))
|
||||||
|
redis_client.ping()
|
||||||
|
return {"status": "ok"}
|
||||||
|
|
||||||
|
|
||||||
|
@app.post("/api/v1/users", response_model=UserOut, status_code=201)
|
||||||
|
def register_user(payload: CreateUser):
|
||||||
|
password_hash = password_hasher.hash(payload.password)
|
||||||
|
user = User(username=payload.username, display_name=payload.display_name, password_hash=password_hash)
|
||||||
|
try:
|
||||||
|
with SessionLocal.begin() as session:
|
||||||
|
session.add(user)
|
||||||
|
session.flush()
|
||||||
|
except DBAPIError as exc:
|
||||||
|
if getattr(getattr(exc, "orig", None), "sqlstate", None) == "23505":
|
||||||
|
raise _api_error("conflict", "username занят", 409) from exc
|
||||||
|
raise
|
||||||
|
return _user_out(user)
|
||||||
|
|
||||||
|
|
||||||
|
@app.post("/api/v1/auth/sessions", response_model=SessionOut, status_code=201)
|
||||||
|
async def create_session(request: Request):
|
||||||
|
if await request.body():
|
||||||
|
raise _api_error("invalid_request", "Basic authentication must not include a body", 400)
|
||||||
|
username, password = _parse_basic(request.headers.get("authorization", ""))
|
||||||
|
return await run_in_threadpool(_create_session, username, password)
|
||||||
|
|
||||||
|
|
||||||
|
@app.delete("/api/v1/auth/sessions/current", status_code=204)
|
||||||
|
def revoke_session(request: Request):
|
||||||
|
_user, token = _current_user(request)
|
||||||
|
redis_client.delete(_session_key(token))
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
@app.get("/api/v1/users/me", response_model=UserOut)
|
||||||
|
def current_user(request: Request):
|
||||||
|
user, _token = _current_user(request)
|
||||||
|
return _user_out(user)
|
||||||
|
|
||||||
|
|
||||||
|
@app.post("/api/v1/chats", response_model=ChatOut, status_code=201)
|
||||||
|
def create_chat(payload: CreateChat, request: Request):
|
||||||
|
user, _token = _current_user(request)
|
||||||
|
member_ids = [uuid.UUID(value) for value in payload.member_ids]
|
||||||
|
if user.id not in member_ids:
|
||||||
|
raise _api_error("invalid_request", "Вызывающий должен входить в member_ids", 400)
|
||||||
|
|
||||||
|
try:
|
||||||
|
with SessionLocal.begin() as session:
|
||||||
|
chat = Chat(title=payload.title)
|
||||||
|
session.add(chat)
|
||||||
|
session.flush()
|
||||||
|
session.add_all(ChatMember(chat_id=chat.id, user_id=member_id) for member_id in member_ids)
|
||||||
|
session.flush()
|
||||||
|
result = _chat_out(session, chat)
|
||||||
|
except DBAPIError as exc:
|
||||||
|
if getattr(getattr(exc, "orig", None), "sqlstate", None) in {"23503", "23505"}:
|
||||||
|
raise _api_error("invalid_request", "Неизвестный участник", 400) from exc
|
||||||
|
raise
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
@app.get("/api/v1/chats", response_model=ChatPage)
|
||||||
|
def list_chats(request: Request, limit: int = 50, cursor: str | None = None):
|
||||||
|
user, _token = _current_user(request)
|
||||||
|
if limit < 1 or limit > 100:
|
||||||
|
raise _api_error("invalid_request", "limit вне диапазона", 400)
|
||||||
|
statement = (
|
||||||
|
select(Chat)
|
||||||
|
.join(ChatMember, ChatMember.chat_id == Chat.id)
|
||||||
|
.where(ChatMember.user_id == user.id)
|
||||||
|
.order_by(Chat.created_at, Chat.id)
|
||||||
|
)
|
||||||
|
if cursor is not None:
|
||||||
|
payload = _decode_cursor(cursor)
|
||||||
|
if payload.get("u") != str(user.id):
|
||||||
|
raise _api_error("invalid_request", "Неверный cursor", 400)
|
||||||
|
created_at, chat_id = _cursor_position(payload)
|
||||||
|
statement = statement.where(tuple_(Chat.created_at, Chat.id) > tuple_(created_at, chat_id))
|
||||||
|
|
||||||
|
with SessionLocal() as session:
|
||||||
|
chats = session.scalars(statement.limit(limit + 1)).all()
|
||||||
|
has_more = len(chats) > limit
|
||||||
|
page = chats[:limit]
|
||||||
|
items = [_chat_out(session, chat) for chat in page]
|
||||||
|
next_cursor = None
|
||||||
|
if has_more:
|
||||||
|
last = page[-1]
|
||||||
|
next_cursor = _encode_cursor({"u": str(user.id), "t": _iso(last.created_at), "i": str(last.id)})
|
||||||
|
return {"items": items, "next_cursor": next_cursor}
|
||||||
|
|
||||||
|
|
||||||
|
@app.get("/api/v1/chats/{chat_id}", response_model=ChatOut)
|
||||||
|
def get_chat(chat_id: uuid.UUID, request: Request):
|
||||||
|
user, _token = _current_user(request)
|
||||||
|
with SessionLocal() as session:
|
||||||
|
chat = _get_chat_for_member(session, chat_id, user.id)
|
||||||
|
if chat is None:
|
||||||
|
raise _api_error("not_found", "Объект не найден", 404)
|
||||||
|
return _chat_out(session, chat)
|
||||||
|
|
||||||
|
|
||||||
|
@app.post("/api/v1/chats/{chat_id}/messages", response_model=MessageOut, status_code=201)
|
||||||
|
def create_message(chat_id: uuid.UUID, payload: CreateMessage, request: Request):
|
||||||
|
user, _token = _current_user(request)
|
||||||
|
with SessionLocal.begin() as session:
|
||||||
|
chat = _get_chat_for_member(session, chat_id, user.id)
|
||||||
|
if chat is None:
|
||||||
|
raise _api_error("not_found", "Объект не найден", 404)
|
||||||
|
if not payload.text.strip():
|
||||||
|
raise _api_error("invalid_request", "Сообщение не может состоять только из пробелов", 400)
|
||||||
|
message = Message(chat_id=chat_id, sender_id=user.id, text=payload.text)
|
||||||
|
session.add(message)
|
||||||
|
session.flush()
|
||||||
|
result = _message_out(message)
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
@app.get("/api/v1/chats/{chat_id}/messages", response_model=MessagePage)
|
||||||
|
def list_messages(chat_id: uuid.UUID, request: Request, limit: int = 50, cursor: str | None = None):
|
||||||
|
user, _token = _current_user(request)
|
||||||
|
with SessionLocal() as session:
|
||||||
|
chat = _get_chat_for_member(session, chat_id, user.id)
|
||||||
|
if chat is None:
|
||||||
|
raise _api_error("not_found", "Объект не найден", 404)
|
||||||
|
if limit < 1 or limit > 100:
|
||||||
|
raise _api_error("invalid_request", "limit вне диапазона", 400)
|
||||||
|
after = None
|
||||||
|
if cursor is not None:
|
||||||
|
payload = _decode_cursor(cursor)
|
||||||
|
if payload.get("c") != str(chat_id):
|
||||||
|
raise _api_error("invalid_request", "cursor от другого чата", 400)
|
||||||
|
after = _cursor_position(payload)
|
||||||
|
statement = select(Message).where(Message.chat_id == chat_id)
|
||||||
|
if after is not None:
|
||||||
|
statement = statement.where(tuple_(Message.created_at, Message.id) > tuple_(*after))
|
||||||
|
statement = statement.order_by(Message.created_at, Message.id).limit(limit + 1)
|
||||||
|
rows = session.scalars(statement).all()
|
||||||
|
has_more = len(rows) > limit
|
||||||
|
page = rows[:limit]
|
||||||
|
items = [_message_out(message) for message in page]
|
||||||
|
|
||||||
|
next_cursor = None
|
||||||
|
if has_more:
|
||||||
|
last = page[-1]
|
||||||
|
next_cursor = _encode_cursor({"c": str(chat_id), "t": _iso(last.created_at), "i": str(last.id)})
|
||||||
|
return {"items": items, "next_cursor": next_cursor}
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
from sqlalchemy import text
|
||||||
|
|
||||||
|
from .db import engine
|
||||||
|
|
||||||
|
|
||||||
|
MIGRATIONS = Path(__file__).resolve().parents[2] / "migrations"
|
||||||
|
|
||||||
|
|
||||||
|
def migrate() -> None:
|
||||||
|
with engine.begin() as connection:
|
||||||
|
connection.exec_driver_sql("SELECT pg_advisory_xact_lock(2026102202)")
|
||||||
|
connection.exec_driver_sql(
|
||||||
|
"CREATE TABLE IF NOT EXISTS schema_migrations "
|
||||||
|
"(version VARCHAR(128) PRIMARY KEY, applied_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP)"
|
||||||
|
)
|
||||||
|
applied = set(connection.execute(text("SELECT version FROM schema_migrations")).scalars())
|
||||||
|
for path in sorted(MIGRATIONS.glob("*.sql")):
|
||||||
|
version = path.stem
|
||||||
|
if version in applied:
|
||||||
|
continue
|
||||||
|
sql = path.read_text(encoding="utf-8")
|
||||||
|
for statement in sql.split(";"):
|
||||||
|
if statement.strip():
|
||||||
|
connection.exec_driver_sql(statement)
|
||||||
|
connection.execute(
|
||||||
|
text("INSERT INTO schema_migrations(version) VALUES (:version)"),
|
||||||
|
{"version": version},
|
||||||
|
)
|
||||||
|
print(f"Applied migration {version}")
|
||||||
|
engine.dispose()
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
migrate()
|
||||||
@@ -0,0 +1,61 @@
|
|||||||
|
import uuid
|
||||||
|
from datetime import datetime
|
||||||
|
|
||||||
|
from sqlalchemy import DateTime, ForeignKey, ForeignKeyConstraint, Index, String, Text, func
|
||||||
|
from sqlalchemy.dialects.postgresql import UUID
|
||||||
|
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column
|
||||||
|
|
||||||
|
|
||||||
|
class Base(DeclarativeBase):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class User(Base):
|
||||||
|
__tablename__ = "users"
|
||||||
|
|
||||||
|
id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
|
||||||
|
username: Mapped[str] = mapped_column(String(32), nullable=False, unique=True)
|
||||||
|
display_name: Mapped[str] = mapped_column(String(100), nullable=False)
|
||||||
|
password_hash: Mapped[str] = mapped_column(Text, nullable=False)
|
||||||
|
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False, server_default=func.now())
|
||||||
|
|
||||||
|
|
||||||
|
class Chat(Base):
|
||||||
|
__tablename__ = "chats"
|
||||||
|
|
||||||
|
id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
|
||||||
|
title: Mapped[str] = mapped_column(String(100), nullable=False)
|
||||||
|
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False, server_default=func.now())
|
||||||
|
|
||||||
|
|
||||||
|
class ChatMember(Base):
|
||||||
|
__tablename__ = "chat_members"
|
||||||
|
__table_args__ = (
|
||||||
|
Index("chat_members_user_id_idx", "user_id", "chat_id"),
|
||||||
|
)
|
||||||
|
|
||||||
|
chat_id: Mapped[uuid.UUID] = mapped_column(
|
||||||
|
UUID(as_uuid=True), ForeignKey("chats.id", ondelete="CASCADE"), primary_key=True
|
||||||
|
)
|
||||||
|
user_id: Mapped[uuid.UUID] = mapped_column(
|
||||||
|
UUID(as_uuid=True), ForeignKey("users.id", ondelete="RESTRICT"), primary_key=True
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class Message(Base):
|
||||||
|
__tablename__ = "messages"
|
||||||
|
__table_args__ = (
|
||||||
|
ForeignKeyConstraint(
|
||||||
|
["chat_id", "sender_id"],
|
||||||
|
["chat_members.chat_id", "chat_members.user_id"],
|
||||||
|
name="messages_sender_is_chat_member_fk",
|
||||||
|
ondelete="CASCADE",
|
||||||
|
),
|
||||||
|
Index("messages_history_idx", "chat_id", "created_at", "id"),
|
||||||
|
)
|
||||||
|
|
||||||
|
id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
|
||||||
|
chat_id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), nullable=False)
|
||||||
|
sender_id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), nullable=False)
|
||||||
|
text: Mapped[str] = mapped_column(String(4000), nullable=False)
|
||||||
|
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False, server_default=func.now())
|
||||||
@@ -0,0 +1,94 @@
|
|||||||
|
from pydantic import BaseModel, ConfigDict, Field, StrictStr, field_validator
|
||||||
|
|
||||||
|
|
||||||
|
class StrictModel(BaseModel):
|
||||||
|
model_config = ConfigDict(extra="forbid")
|
||||||
|
|
||||||
|
|
||||||
|
class CreateUser(StrictModel):
|
||||||
|
username: StrictStr = Field(min_length=3, max_length=32, pattern=r"^[a-z0-9_]{3,32}$")
|
||||||
|
display_name: StrictStr = Field(min_length=1, max_length=100)
|
||||||
|
password: StrictStr = Field(min_length=12, max_length=128)
|
||||||
|
|
||||||
|
@field_validator("display_name")
|
||||||
|
@classmethod
|
||||||
|
def display_name_not_blank(cls, value: str) -> str:
|
||||||
|
if not value.strip():
|
||||||
|
raise ValueError("display_name must contain a non-whitespace character")
|
||||||
|
return value
|
||||||
|
|
||||||
|
|
||||||
|
class UserOut(BaseModel):
|
||||||
|
id: str
|
||||||
|
username: str
|
||||||
|
display_name: str
|
||||||
|
created_at: str
|
||||||
|
|
||||||
|
|
||||||
|
class CreateChat(StrictModel):
|
||||||
|
title: StrictStr = Field(min_length=1, max_length=100)
|
||||||
|
member_ids: list[StrictStr] = Field(min_length=1, max_length=100)
|
||||||
|
|
||||||
|
@field_validator("title")
|
||||||
|
@classmethod
|
||||||
|
def title_not_blank(cls, value: str) -> str:
|
||||||
|
if not value.strip():
|
||||||
|
raise ValueError("title must contain a non-whitespace character")
|
||||||
|
return value
|
||||||
|
|
||||||
|
@field_validator("member_ids")
|
||||||
|
@classmethod
|
||||||
|
def members_are_unique_uuids(cls, values: list[str]) -> list[str]:
|
||||||
|
import uuid
|
||||||
|
|
||||||
|
try:
|
||||||
|
normalized = [str(uuid.UUID(value)) for value in values]
|
||||||
|
except (ValueError, AttributeError, TypeError) as exc:
|
||||||
|
raise ValueError("member_ids must contain UUIDs") from exc
|
||||||
|
if len(normalized) != len(set(normalized)):
|
||||||
|
raise ValueError("member_ids must be unique")
|
||||||
|
return normalized
|
||||||
|
|
||||||
|
|
||||||
|
class ChatOut(BaseModel):
|
||||||
|
id: str
|
||||||
|
title: str
|
||||||
|
member_ids: list[str]
|
||||||
|
created_at: str
|
||||||
|
|
||||||
|
|
||||||
|
class ChatPage(BaseModel):
|
||||||
|
items: list[ChatOut]
|
||||||
|
next_cursor: str | None
|
||||||
|
|
||||||
|
|
||||||
|
class CreateMessage(StrictModel):
|
||||||
|
text: StrictStr = Field(min_length=1, max_length=4000)
|
||||||
|
|
||||||
|
|
||||||
|
class MessageOut(BaseModel):
|
||||||
|
id: str
|
||||||
|
chat_id: str
|
||||||
|
sender_id: str
|
||||||
|
text: str
|
||||||
|
created_at: str
|
||||||
|
|
||||||
|
|
||||||
|
class MessagePage(BaseModel):
|
||||||
|
items: list[MessageOut]
|
||||||
|
next_cursor: str | None
|
||||||
|
|
||||||
|
|
||||||
|
class SessionOut(BaseModel):
|
||||||
|
token: str
|
||||||
|
token_type: str = "Bearer"
|
||||||
|
expires_at: str
|
||||||
|
user: UserOut
|
||||||
|
|
||||||
|
|
||||||
|
class Health(BaseModel):
|
||||||
|
status: str = "ok"
|
||||||
|
|
||||||
|
|
||||||
|
class ErrorBody(BaseModel):
|
||||||
|
error: dict[str, str]
|
||||||
@@ -0,0 +1,37 @@
|
|||||||
|
from dataclasses import dataclass
|
||||||
|
import os
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
|
||||||
|
def _secret(file_variable: str, value_variable: str) -> str:
|
||||||
|
file_path = os.environ.get(file_variable)
|
||||||
|
if file_path:
|
||||||
|
value = Path(file_path).read_text(encoding="utf-8").strip()
|
||||||
|
else:
|
||||||
|
value = os.environ.get(value_variable, "").strip()
|
||||||
|
if not value:
|
||||||
|
raise RuntimeError(f"Required setting {file_variable} is missing")
|
||||||
|
return value
|
||||||
|
|
||||||
|
|
||||||
|
def load_database_url() -> str:
|
||||||
|
return _secret("DATABASE_URL_FILE", "DATABASE_URL")
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass(frozen=True)
|
||||||
|
class Settings:
|
||||||
|
cache_url: str
|
||||||
|
session_ttl_seconds: int
|
||||||
|
|
||||||
|
|
||||||
|
def load_settings() -> Settings:
|
||||||
|
try:
|
||||||
|
ttl = int(os.environ.get("SESSION_TTL_SECONDS", "3600"))
|
||||||
|
except ValueError as exc:
|
||||||
|
raise RuntimeError("SESSION_TTL_SECONDS must be an integer") from exc
|
||||||
|
if ttl < 1 or ttl > 31_536_000:
|
||||||
|
raise RuntimeError("SESSION_TTL_SECONDS must be between 1 and 31536000")
|
||||||
|
return Settings(
|
||||||
|
cache_url=_secret("CACHE_URL_FILE", "CACHE_URL"),
|
||||||
|
session_ttl_seconds=ttl,
|
||||||
|
)
|
||||||
@@ -171,6 +171,20 @@ class Client:
|
|||||||
self.call('GET','/health/live',schema='Health')
|
self.call('GET','/health/live',schema='Health')
|
||||||
self.call('GET','/health/ready',schema='Health')
|
self.call('GET','/health/ready',schema='Health')
|
||||||
alice, bob, eve = (self.user(label) for label in ('alice','bob','eve'))
|
alice, bob, eve = (self.user(label) for label in ('alice','bob','eve'))
|
||||||
|
if LAB >= 2:
|
||||||
|
# Grade-4 runs with a 10-second TTL. Creating and authenticating the
|
||||||
|
# other users can outlive Alice's initial session before chat creation.
|
||||||
|
registration = alice['registration']
|
||||||
|
credential = base64.b64encode(
|
||||||
|
(registration['username'] + ':' + registration['password']).encode()
|
||||||
|
).decode()
|
||||||
|
session = self.call(
|
||||||
|
'POST', '/api/v1/auth/sessions', 201,
|
||||||
|
headers={'Authorization':'Basic ' + credential}, schema='Session'
|
||||||
|
)['json']
|
||||||
|
check(session['user'] == alice['user'], 'session user mismatch')
|
||||||
|
check(timestamp(session['expires_at']) > dt.datetime.now(dt.timezone.utc), 'session already expired')
|
||||||
|
alice['token'] = session['token']
|
||||||
chat = self.call('POST','/api/v1/chats',201,actor=alice,body={'title':'Контрактный тест','member_ids':[alice['id'],bob['id']]},schema='Chat')['json']
|
chat = self.call('POST','/api/v1/chats',201,actor=alice,body={'title':'Контрактный тест','member_ids':[alice['id'],bob['id']]},schema='Chat')['json']
|
||||||
check(set(chat['member_ids'])=={alice['id'],bob['id']},'chat member mismatch')
|
check(set(chat['member_ids'])=={alice['id'],bob['id']},'chat member mismatch')
|
||||||
return alice,bob,eve,chat
|
return alice,bob,eve,chat
|
||||||
|
|||||||
+205
@@ -0,0 +1,205 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
"""Manual integration checks for Lab 2 grade 4; uses a disposable local Compose stack."""
|
||||||
|
import base64
|
||||||
|
import concurrent.futures
|
||||||
|
import json
|
||||||
|
from pathlib import Path
|
||||||
|
import subprocess
|
||||||
|
import time
|
||||||
|
import urllib.error
|
||||||
|
import urllib.request
|
||||||
|
import uuid
|
||||||
|
|
||||||
|
from client import Client, Failure, arguments, check, run
|
||||||
|
|
||||||
|
|
||||||
|
ROOT = Path(__file__).resolve().parents[1]
|
||||||
|
|
||||||
|
|
||||||
|
def compose(*args: str) -> None:
|
||||||
|
subprocess.run(["docker", "compose", *args], cwd=ROOT, check=True, capture_output=True, text=True)
|
||||||
|
|
||||||
|
|
||||||
|
def compose_output(*args: str) -> str:
|
||||||
|
result = subprocess.run(["docker", "compose", *args], cwd=ROOT, check=True, capture_output=True, text=True)
|
||||||
|
return result.stdout.strip()
|
||||||
|
|
||||||
|
|
||||||
|
def get_status(base_url: str, path: str, actor=None) -> tuple[int, dict, object, float]:
|
||||||
|
headers = {"Accept": "application/json"}
|
||||||
|
if actor:
|
||||||
|
headers["Authorization"] = "Bearer " + actor["token"]
|
||||||
|
request = urllib.request.Request(base_url.rstrip("/") + path, headers=headers)
|
||||||
|
started = time.monotonic()
|
||||||
|
try:
|
||||||
|
response = urllib.request.urlopen(request, timeout=10)
|
||||||
|
except urllib.error.HTTPError as exc:
|
||||||
|
response = exc
|
||||||
|
with response:
|
||||||
|
headers = {name.lower(): value for name, value in response.headers.items()}
|
||||||
|
body = json.loads(response.read())
|
||||||
|
return response.status, headers, body, time.monotonic() - started
|
||||||
|
|
||||||
|
|
||||||
|
def register_once(base_url: str, username: str, password: str) -> int:
|
||||||
|
body = json.dumps({"username": username, "display_name": "Race test", "password": password}).encode()
|
||||||
|
request = urllib.request.Request(
|
||||||
|
base_url.rstrip("/") + "/api/v1/users",
|
||||||
|
data=body,
|
||||||
|
headers={"Content-Type": "application/json"},
|
||||||
|
method="POST",
|
||||||
|
)
|
||||||
|
try:
|
||||||
|
response = urllib.request.urlopen(request, timeout=10)
|
||||||
|
except urllib.error.HTTPError as exc:
|
||||||
|
response = exc
|
||||||
|
with response:
|
||||||
|
return response.status
|
||||||
|
|
||||||
|
|
||||||
|
def fresh_actor(client: Client, registration: dict) -> dict:
|
||||||
|
credential = base64.b64encode(
|
||||||
|
(registration["username"] + ":" + registration["password"]).encode()
|
||||||
|
).decode()
|
||||||
|
session = client.call(
|
||||||
|
"POST",
|
||||||
|
"/api/v1/auth/sessions",
|
||||||
|
201,
|
||||||
|
headers={"Authorization": "Basic " + credential},
|
||||||
|
schema="Session",
|
||||||
|
)["json"]
|
||||||
|
return {
|
||||||
|
"id": session["user"]["id"],
|
||||||
|
"user": session["user"],
|
||||||
|
"registration": registration,
|
||||||
|
"token": session["token"],
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def wait_ready(base_url: str, timeout: int = 45) -> None:
|
||||||
|
deadline = time.monotonic() + timeout
|
||||||
|
while time.monotonic() < deadline:
|
||||||
|
try:
|
||||||
|
status, _headers, _body, _elapsed = get_status(base_url, "/health/ready")
|
||||||
|
if status == 200:
|
||||||
|
return
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
time.sleep(0.5)
|
||||||
|
raise Failure("API did not become ready after restoring a dependency")
|
||||||
|
|
||||||
|
|
||||||
|
def main() -> None:
|
||||||
|
args = arguments("Lab 2 grade-4 checks; dependency-outage checks restart Compose services")
|
||||||
|
client = Client(args.base_url)
|
||||||
|
|
||||||
|
config = subprocess.run(
|
||||||
|
["docker", "compose", "config", "--format", "json"],
|
||||||
|
cwd=ROOT,
|
||||||
|
check=True,
|
||||||
|
capture_output=True,
|
||||||
|
text=True,
|
||||||
|
)
|
||||||
|
ttl = int(json.loads(config.stdout)["services"]["api"]["environment"]["SESSION_TTL_SECONDS"])
|
||||||
|
check(ttl <= 10, "Run this check with SESSION_TTL_SECONDS=10 to verify expiry.")
|
||||||
|
|
||||||
|
alice, bob, _eve, chat = client.bootstrap()
|
||||||
|
registration = alice["registration"]
|
||||||
|
|
||||||
|
bad = base64.b64encode((registration["username"] + ":wrong-password").encode()).decode()
|
||||||
|
client.call("POST", "/api/v1/auth/sessions", 401, headers={"Authorization": "Basic " + bad})
|
||||||
|
print("PASS: invalid password returns the same Basic 401 challenge")
|
||||||
|
|
||||||
|
alice = fresh_actor(client, registration)
|
||||||
|
chat_count_before = len(client.call("GET", "/api/v1/chats?limit=100", actor=alice, schema="ChatPage")["json"]["items"])
|
||||||
|
unknown_id = str(uuid.uuid4())
|
||||||
|
failed_title = "must_roll_back_" + uuid.uuid4().hex[:12]
|
||||||
|
client.call(
|
||||||
|
"POST",
|
||||||
|
"/api/v1/chats",
|
||||||
|
400,
|
||||||
|
actor=alice,
|
||||||
|
body={"title": failed_title, "member_ids": [alice["id"], unknown_id]},
|
||||||
|
)
|
||||||
|
chat_count_after = len(client.call("GET", "/api/v1/chats?limit=100", actor=alice, schema="ChatPage")["json"]["items"])
|
||||||
|
check(chat_count_after == chat_count_before, "invalid chat creation left visible partial state")
|
||||||
|
stored_failed_chats = compose_output(
|
||||||
|
"exec", "-T", "db", "psql", "-U", "messenger", "-d", "messenger", "-Atc",
|
||||||
|
f"SELECT count(*) FROM chats WHERE title = '{failed_title}'",
|
||||||
|
)
|
||||||
|
check(stored_failed_chats == "0", "database kept a chat row after the member foreign-key rollback")
|
||||||
|
print("PASS: invalid composite chat creation leaves no chat")
|
||||||
|
|
||||||
|
race_name = "race_" + uuid.uuid4().hex[:20]
|
||||||
|
race_password = "T3st-" + uuid.uuid4().hex
|
||||||
|
with concurrent.futures.ThreadPoolExecutor(max_workers=2) as executor:
|
||||||
|
statuses = list(executor.map(lambda _i: register_once(client.base, race_name, race_password), range(2)))
|
||||||
|
check(statuses.count(201) == 1 and statuses.count(409) == 1, "username race was not resolved by the unique constraint")
|
||||||
|
print("PASS: concurrent duplicate registration produced one 201 and one 409 response")
|
||||||
|
|
||||||
|
expiring = client.call(
|
||||||
|
"POST",
|
||||||
|
"/api/v1/auth/sessions",
|
||||||
|
201,
|
||||||
|
headers={"Authorization": "Basic " + base64.b64encode(
|
||||||
|
(registration["username"] + ":" + registration["password"]).encode()
|
||||||
|
).decode()},
|
||||||
|
schema="Session",
|
||||||
|
)["json"]
|
||||||
|
time.sleep(ttl + 1)
|
||||||
|
status, headers, body, _elapsed = get_status(client.base, "/api/v1/users/me", {"token": expiring["token"]})
|
||||||
|
check(
|
||||||
|
status == 401 and headers.get("www-authenticate", "").lower().startswith("bearer"),
|
||||||
|
"expired session was not rejected with a Bearer challenge",
|
||||||
|
)
|
||||||
|
check(body["error"]["code"] == "unauthorized", "expired session returned the wrong error")
|
||||||
|
print("PASS: session expiry with short TTL")
|
||||||
|
|
||||||
|
# A protected request fails closed when Redis is unavailable; the stack is restored in finally.
|
||||||
|
alice = fresh_actor(client, registration)
|
||||||
|
compose("stop", "redis")
|
||||||
|
try:
|
||||||
|
try:
|
||||||
|
status, _headers, body, elapsed = get_status(client.base, "/api/v1/users/me", alice)
|
||||||
|
except OSError as exc:
|
||||||
|
raise Failure("Redis outage request timed out before the API returned 503") from exc
|
||||||
|
check(
|
||||||
|
status == 503 and body["error"]["code"] == "unavailable" and elapsed <= 8,
|
||||||
|
f"Redis outage did not return a bounded 503 (status={status}, elapsed={elapsed:.2f}s)",
|
||||||
|
)
|
||||||
|
print("PASS: Redis outage returns bounded 503")
|
||||||
|
finally:
|
||||||
|
compose("start", "redis")
|
||||||
|
wait_ready(client.base)
|
||||||
|
|
||||||
|
# Database loss has the same bounded behavior and does not trust a cached identity.
|
||||||
|
bob = fresh_actor(client, bob["registration"])
|
||||||
|
compose("stop", "db")
|
||||||
|
try:
|
||||||
|
try:
|
||||||
|
status, _headers, body, elapsed = get_status(client.base, "/api/v1/users/me", bob)
|
||||||
|
except OSError as exc:
|
||||||
|
raise Failure("Postgres outage request timed out before the API returned 503") from exc
|
||||||
|
check(
|
||||||
|
status == 503 and body["error"]["code"] == "unavailable" and elapsed <= 8,
|
||||||
|
f"Postgres outage did not return a bounded 503 (status={status}, elapsed={elapsed:.2f}s)",
|
||||||
|
)
|
||||||
|
print("PASS: Postgres outage returns bounded 503")
|
||||||
|
finally:
|
||||||
|
compose("start", "db")
|
||||||
|
wait_ready(client.base)
|
||||||
|
|
||||||
|
# Explicitly verify logout too; the public smoke covers this but grade 4 records it separately.
|
||||||
|
bob = fresh_actor(client, bob["registration"])
|
||||||
|
client.call("DELETE", "/api/v1/auth/sessions/current", 204, actor=bob)
|
||||||
|
client.call("GET", "/api/v1/users/me", 401, actor=bob)
|
||||||
|
print("PASS: logout revokes the bearer session")
|
||||||
|
|
||||||
|
# A harmless read confirms the restored stack and the same persistent conversation still work.
|
||||||
|
alice = fresh_actor(client, registration)
|
||||||
|
client.call("GET", "/api/v1/chats/" + chat["id"], 200, actor=alice, schema="Chat")
|
||||||
|
print("PASS: API and persistent stores are ready after dependency restarts")
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
run(main)
|
||||||
Reference in New Issue
Block a user