forked from mtusi2026/mtusi2026lab2
ЛР 2 — БВТ2304 — Черников
PostgreSQL с версионируемыми миграциями, вход по Basic и Bearer-сессии в Valkey (AOF), Argon2id, compose-стенд с секретами в файлах. Собственные тесты в student-tests/, проверки инфраструктуры и EXPLAIN в scripts/, отчёт в REPORT.md, результаты в evidence/, команды для показа в DEMO.md. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -18,3 +18,23 @@ __pycache__
|
||||
node_modules
|
||||
evidence
|
||||
.DS_Store
|
||||
# В образ копируются только requirements.txt, migrations/ и src/.
|
||||
**/__pycache__
|
||||
**/*.pyc
|
||||
contracts
|
||||
examples
|
||||
scripts
|
||||
tests
|
||||
student-tests
|
||||
infra
|
||||
*.md
|
||||
Makefile
|
||||
lab.json
|
||||
requirements-dev.txt
|
||||
requirements.in
|
||||
compose*.yaml
|
||||
.gitignore
|
||||
.gitattributes
|
||||
.vscode
|
||||
.idea
|
||||
*.log
|
||||
|
||||
+11
-8
@@ -1,11 +1,14 @@
|
||||
# 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
|
||||
# docker compose reads .env to fill ${...} in compose.yaml; every value has a default there.
|
||||
# Passwords are NOT here: they are files in secrets/ (python scripts/init_secrets.py).
|
||||
APP_ENV=lab
|
||||
APP_LOG_LEVEL=INFO
|
||||
APP_SHUTDOWN_TIMEOUT=8
|
||||
# API port published on 127.0.0.1 of the host.
|
||||
APP_PUBLISHED_PORT=8080
|
||||
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
|
||||
DB_POOL_SIZE=10
|
||||
# Fixed inside the containers by compose.yaml (skeleton names kept):
|
||||
# APP_HOST=0.0.0.0 APP_PORT=8080
|
||||
# DATABASE_URL_FILE=/run/secrets/database_url CACHE_URL_FILE=/run/secrets/cache_url
|
||||
# SESSION_STORE from the skeleton is not needed: sessions are always stored in Valkey.
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
*.sh text eol=lf
|
||||
*.sql text eol=lf
|
||||
*.conf text eol=lf
|
||||
compose.yaml text eol=lf
|
||||
Dockerfile text eol=lf
|
||||
*.cmd text eol=crlf
|
||||
@@ -0,0 +1,119 @@
|
||||
# Шпаргалка для защиты — лабораторная 2
|
||||
|
||||
Команды по порядку сценария защиты из README. Подробности решения — в [REPORT.md](REPORT.md).
|
||||
|
||||
Shell: **Git Bash** в Windows или обычный терминал в Linux/macOS (в Linux вместо `python` — `python3`).
|
||||
Адрес — `127.0.0.1`, а не `localhost`: в Windows `localhost` сначала пробует IPv6, и каждый запрос идёт около двух секунд.
|
||||
|
||||
## 0. Подготовка (один раз)
|
||||
|
||||
```sh
|
||||
python scripts/init_secrets.py # случайные пароли в secrets/, в Git не попадают
|
||||
cp .env.example .env
|
||||
URL=http://127.0.0.1:8080
|
||||
|
||||
# post <путь> [токен] — JSON со стандартного ввода: так кириллица уходит в UTF-8 и из Git Bash
|
||||
post() { curl -s -X POST "$URL$1" -H 'Content-Type: application/json' ${2:+-H "Authorization: Bearer $2"} --data-binary @- -w '\nHTTP %{http_code}\n'; }
|
||||
# get <путь> [токен]
|
||||
get() { curl -s "$URL$1" ${2:+-H "Authorization: Bearer $2"} -w '\nHTTP %{http_code}\n'; }
|
||||
# new <ПЕРЕМЕННАЯ> <путь> [токен] — как post, но ещё сохраняет id созданного объекта
|
||||
new() { local r; r=$(post "$2" "$3"); echo "$r"; printf -v "$1" %s "$(echo "$r" | sed -nE 's/.*"id":"([^"]+)".*/\1/p' | head -1)"; }
|
||||
# signin <ПЕРЕМЕННАЯ> <username> <password> — вход по Basic, токен сохраняется в переменную
|
||||
signin() { local r; r=$(curl -s -X POST "$URL/api/v1/auth/sessions" -u "$2:$3" -w '\nHTTP %{http_code}\n'); echo "$r" | tail -1; printf -v "$1" %s "$(echo "$r" | sed -nE 's/.*"token":"([^"]+)".*/\1/p')"; }
|
||||
```
|
||||
|
||||
## 1. Миграции на пустом volume, пользователи, вход, сообщение
|
||||
|
||||
```sh
|
||||
docker compose down -v # ВНИМАНИЕ: удаляет все учебные данные (volumes)
|
||||
docker compose up -d --build --wait # сборка, миграции, ожидание healthy
|
||||
docker compose logs migrate # migration_applied 0001_schema, 0002_..., applied_now=2
|
||||
|
||||
new ALICE /api/v1/users <<< '{"username":"alice","display_name":"Алиса","password":"Alice-password-123"}'
|
||||
new BOB /api/v1/users <<< '{"username":"bob","display_name":"Боб","password":"Bob-password-12345"}'
|
||||
new EVE /api/v1/users <<< '{"username":"eve","display_name":"Ева","password":"Eve-password-12345"}'
|
||||
|
||||
signin TA alice Alice-password-123 # HTTP 201
|
||||
signin TB bob Bob-password-12345
|
||||
signin TE eve Eve-password-12345
|
||||
curl -s -X POST "$URL/api/v1/auth/sessions" -u alice:wrong-password-1 -i | head -1 # 401: неверный пароль
|
||||
|
||||
new CHAT /api/v1/chats "$TA" <<< "{\"title\":\"Проект по ВНП\",\"member_ids\":[\"$ALICE\",\"$BOB\"]}"
|
||||
post /api/v1/chats/$CHAT/messages "$TA" <<< '{"text":"Привет, Боб! 👋"}'
|
||||
post /api/v1/chats/$CHAT/messages "$TB" <<< '{"text":"Привет, Алиса!"}'
|
||||
get /api/v1/chats/$CHAT/messages "$TB" # два сообщения, HTTP 200
|
||||
get /api/v1/chats/$CHAT/messages "$TE" # Ева не участник: HTTP 404
|
||||
get /api/v1/users/me # без токена: HTTP 401
|
||||
curl -s "$URL/api/v1/users/me" -H "X-User-Id: $ALICE" -w '\nHTTP %{http_code}\n' # X-User-Id из lab1 больше не работает: 401
|
||||
```
|
||||
|
||||
Данные в PostgreSQL:
|
||||
|
||||
```sh
|
||||
docker compose exec postgres psql -U messenger -d messenger -c "SELECT username, left(password_hash, 30) FROM users"
|
||||
docker compose exec postgres psql -U messenger -d messenger -c "SELECT * FROM schema_migrations"
|
||||
```
|
||||
|
||||
## 2–3. Перезапуск API и всех хранилищ: тот же токен, те же данные
|
||||
|
||||
Публичный тест сохранности. Токен живёт только в памяти теста; хук перезапускает api, postgres и valkey, volumes остаются:
|
||||
|
||||
```sh
|
||||
python tests/resilience.py --mode persistence --action-script scripts/restart.cmd --base-url $URL # Windows
|
||||
make test-persistence ACTION_SCRIPT=scripts/restart.sh BASE_URL=$URL # Linux/macOS
|
||||
```
|
||||
|
||||
То же руками, с токеном Алисы из шага 1:
|
||||
|
||||
```sh
|
||||
scripts/restart.cmd # в Linux: scripts/restart.sh
|
||||
docker compose ps # STATUS «Up N seconds»: контейнеры действительно перезапущены
|
||||
get /api/v1/chats/$CHAT/messages "$TA" # те же два сообщения, тот же токен: HTTP 200
|
||||
curl -s -X DELETE "$URL/api/v1/auth/sessions/current" -H "Authorization: Bearer $TA" -w 'HTTP %{http_code}\n' # выход: 204
|
||||
get /api/v1/users/me "$TA" # после выхода: HTTP 401
|
||||
```
|
||||
|
||||
Истечение сессии с коротким TTL (отдельная копия API с `SESSION_TTL_SECONDS=2`):
|
||||
|
||||
```sh
|
||||
python scripts/lab2_checks.py --only expiry
|
||||
```
|
||||
|
||||
## 4. Уровень «4»
|
||||
|
||||
Роль Valkey — хранилище сессий с TTL и AOF:
|
||||
|
||||
```sh
|
||||
VK='valkey-cli --no-auth-warning -a "$(sed -n "s/^requirepass //p" /run/secrets/valkey_auth)"'
|
||||
docker compose exec valkey sh -c "$VK --scan --pattern 'session:*' | head -3" # ключи — sha256 от токенов
|
||||
docker compose exec valkey sh -c "$VK CONFIG GET appendonly" # yes
|
||||
docker compose exec valkey sh -c "$VK CONFIG GET appendfsync" # always
|
||||
```
|
||||
|
||||
План SQL-запроса истории на 10 000+ сообщениях:
|
||||
|
||||
```sh
|
||||
python scripts/explain_history.py # Index Scan, ~5 буферов; без индекса — Seq Scan по всей таблице
|
||||
```
|
||||
|
||||
Ошибка зависимости, пул и таймауты, гонка регистрации, откат создания чата:
|
||||
|
||||
```sh
|
||||
python scripts/lab2_checks.py --only valkey-down postgres-down
|
||||
python scripts/lab2_checks.py --only pool timeouts race rollback
|
||||
```
|
||||
|
||||
Собственные тесты и конкурентный тест:
|
||||
|
||||
```sh
|
||||
BASE_URL=$URL python -m unittest discover -s student-tests -v # make student-test
|
||||
BASE_URL=$URL python student-tests/race.py # make race
|
||||
```
|
||||
|
||||
## Остановка и уборка
|
||||
|
||||
```sh
|
||||
docker compose stop # безопасная остановка, данные остаются в volumes
|
||||
docker compose down # удалить контейнеры, volumes остаются
|
||||
docker compose down -v # удалить ещё и все данные
|
||||
```
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
FROM python:3.14.7-slim-trixie@sha256:51dafde81dbdb6ebde285137a295cf18a47ca95234fe388a343719cb97305b3d
|
||||
|
||||
ENV PYTHONUNBUFFERED=1 \
|
||||
PYTHONDONTWRITEBYTECODE=1 \
|
||||
PIP_DISABLE_PIP_VERSION_CHECK=1 \
|
||||
APP_HOST=0.0.0.0 \
|
||||
APP_PORT=8080
|
||||
|
||||
RUN groupadd --gid 10001 app \
|
||||
&& useradd --uid 10001 --gid app --no-create-home --shell /usr/sbin/nologin app
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
COPY requirements.txt ./
|
||||
RUN pip install --no-cache-dir --require-hashes --only-binary=:all: --root-user-action=ignore -r requirements.txt
|
||||
|
||||
COPY migrations/ ./migrations/
|
||||
COPY src/ ./
|
||||
|
||||
USER app
|
||||
EXPOSE 8080
|
||||
|
||||
HEALTHCHECK --interval=10s --timeout=3s --start-period=10s --retries=3 \
|
||||
CMD ["python", "-m", "messenger.healthcheck"]
|
||||
|
||||
CMD ["python", "-m", "messenger"]
|
||||
@@ -4,10 +4,12 @@ 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 secrets up student-test race checks explain
|
||||
|
||||
help:
|
||||
@echo "check: offline structure; validate: full OpenAPI (requirements-dev.txt); test: running API; load: synthetic writes"
|
||||
@echo "secrets/up: create local secrets and start the stand; student-test: own tests; race: 100 parallel messages"
|
||||
@echo "checks: infrastructure checks (restart, expiry, outages); explain: EXPLAIN of the history query"
|
||||
|
||||
check:
|
||||
$(PYTHON) scripts/check_contract.py
|
||||
@@ -24,3 +26,21 @@ 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)
|
||||
|
||||
secrets:
|
||||
$(PYTHON) scripts/init_secrets.py
|
||||
|
||||
up: secrets
|
||||
docker compose up -d --build --wait
|
||||
|
||||
student-test:
|
||||
BASE_URL="$(BASE_URL)" $(PYTHON) -m unittest discover -s student-tests -v
|
||||
|
||||
race:
|
||||
BASE_URL="$(BASE_URL)" $(PYTHON) student-tests/race.py
|
||||
|
||||
checks:
|
||||
$(PYTHON) scripts/lab2_checks.py
|
||||
|
||||
explain:
|
||||
$(PYTHON) scripts/explain_history.py
|
||||
|
||||
@@ -1,37 +1,214 @@
|
||||
# Отчёт — лабораторная 2
|
||||
|
||||
- Автор, группа: TODO
|
||||
- Целевая оценка; выбранный трек 5, если нужен: TODO
|
||||
- Commit/PR: TODO после публикации
|
||||
- Использованные источники, библиотеки и помощь ИИ: TODO
|
||||
- Автор, группа: Черников Дмитрий, БВТ2304
|
||||
- Целевая оценка; выбранный трек 5, если нужен: 4; трек 5 не заявлен
|
||||
- Commit/PR: ветка `solution/bvt2304-chernikov` в форке https://gitea.privcloud.tech/Chernikov/mtusi2026lab2, PR в https://gitea.privcloud.tech/mtusi2026/mtusi2026lab2
|
||||
- Использованные источники, библиотеки и помощь ИИ:
|
||||
- Библиотеки приложения (точные версии и хеши — в `requirements.txt`): `psycopg` 3.3.6 и `psycopg-pool` 3.3.3 — PostgreSQL и пул соединений; `valkey` 6.1.1 — клиент Valkey; `argon2-cffi` 25.1.0 — хеширование паролей Argon2id. Тесты и скрипты проверок — только стандартная библиотека Python.
|
||||
- Источники: README, `contracts/` и `tests/` этого репозитория; документация [PostgreSQL](https://www.postgresql.org/docs/current/) (транзакции, EXPLAIN, блокировки), [psycopg 3 и psycopg-pool](https://www.psycopg.org/psycopg3/docs/), [Valkey persistence](https://valkey.io/topics/persistence/), [RFC 7617](https://www.rfc-editor.org/info/rfc7617/), [OWASP Password Storage Cheat Sheet](https://cheatsheetseries.owasp.org/cheatsheets/Password_Storage_Cheat_Sheet.html).
|
||||
- ИИ: при написании кода, тестов, конфигурации стенда и текста этого отчёта использовался ассистент Claude (Anthropic).
|
||||
|
||||
## Запуск из чистого clone
|
||||
|
||||
TODO: версии runtime/образов, prerequisites, команды создания локальных secrets/.env, build, миграций, запуска, ожидания готовности, тестов и остановки. Указать необходимые действия оператора. Не вставлять секреты. Отдельно указать безопасную остановку и команду намеренного удаления учебных данных.
|
||||
Что нужно на машине: Docker с Compose v2 (проверено на Docker Desktop 29.8.1, Compose 5.5.1), Git и Python 3.9+ для тестов и скриптов проверок. Тесты работают с хоста и используют только стандартную библиотеку. `make` не обязателен: рядом с каждой командой `make` указана равнозначная.
|
||||
|
||||
Образы закреплены тегом и digest:
|
||||
|
||||
| Роль | Сервис | Образ | Готовность | Volume | Порт | Учётные данные |
|
||||
| --- | --- | --- | --- | --- | --- | --- |
|
||||
| API | `api` | `mtusi-messenger:lab2`, собирается из [Dockerfile](Dockerfile) на `python:3.14.7-slim-trixie@sha256:51da…`; зависимости по `requirements.txt` с хешами | `HEALTHCHECK` → `GET /health/ready`, каждые 10 с | нет | `127.0.0.1:8080` → 8080 | секреты `database_url`, `cache_url` (файлы в `/run/secrets`) |
|
||||
| Миграции | `migrate` | тот же образ, `python -m messenger.migrate`, запускается и завершается | код выхода 0 | нет | нет | секрет `database_url` |
|
||||
| PostgreSQL | `postgres` | `postgres:18.6-trixie@sha256:7493…` | `pg_isready`, каждые 5 с | `pgdata` → `/var/lib/postgresql` | только сеть compose | `POSTGRES_PASSWORD_FILE` |
|
||||
| Valkey | `valkey` | `valkey/valkey:9.0.6-trixie@sha256:6334…` | `valkey-cli ping` с паролем, каждые 5 с | `valkeydata` → `/data` | только сеть compose | `requirepass` из файла секрета через `include` |
|
||||
|
||||
Команды для bash (в Windows — Git Bash; в Linux вместо `python` — `python3`):
|
||||
|
||||
```sh
|
||||
git clone <URL fork> mtusi2026lab2 && cd mtusi2026lab2
|
||||
python scripts/init_secrets.py # make secrets: случайные пароли в secrets/, в Git не попадают
|
||||
cp .env.example .env # необязательно: в compose.yaml у всех значений есть умолчания
|
||||
docker compose up -d --build --wait # сборка, миграции, ожидание healthy
|
||||
|
||||
python tests/smoke.py --base-url http://127.0.0.1:8080 # make test BASE_URL=http://127.0.0.1:8080
|
||||
make test-persistence ACTION_SCRIPT=scripts/restart.sh BASE_URL=http://127.0.0.1:8080
|
||||
# в Windows без make: python tests/resilience.py --mode persistence --action-script scripts/restart.cmd --base-url http://127.0.0.1:8080
|
||||
BASE_URL=http://127.0.0.1:8080 python -m unittest discover -s student-tests -v # make student-test
|
||||
BASE_URL=http://127.0.0.1:8080 python student-tests/race.py # make race
|
||||
python scripts/lab2_checks.py --output evidence/lab2-checks.txt # make checks
|
||||
python scripts/explain_history.py --output evidence/explain.txt # make explain
|
||||
|
||||
docker compose stop # безопасная остановка: SIGTERM, данные остаются в volumes
|
||||
docker compose down -v # намеренное удаление всех учебных данных (volumes)
|
||||
```
|
||||
|
||||
Действия оператора:
|
||||
|
||||
- **Секреты.** Один раз запустить `scripts/init_secrets.py`. Скрипт не перезаписывает существующие пароли.
|
||||
- **Пароль PostgreSQL.** Он задаётся только при создании volume. Если удалить `secrets/`, а volume оставить, нужно вернуть старые файлы или пересоздать volume.
|
||||
- **Миграции.** Отдельно запускать не нужно: сервис `migrate` выполняется при каждом `up`. Вручную: `docker compose run --rm migrate`.
|
||||
- **Обновление зависимостей.** Поправить `requirements.in` и перегенерировать lock-файл в том же образе Python:
|
||||
|
||||
```sh
|
||||
docker run --rm -v "$PWD:/w" -w /w python:3.14.7-slim-trixie sh -c "pip install pip-tools==7.6.2 && pip-compile --generate-hashes --strip-extras --allow-unsafe --no-emit-index-url --output-file requirements.txt requirements.in"
|
||||
```
|
||||
|
||||
В Windows используйте `127.0.0.1`, а не `localhost`: `localhost` сначала пробует IPv6, порт опубликован только на IPv4, и каждый запрос ждёт около двух секунд. Скрипт `.sh` Windows выполнить не может, поэтому для теста сохранности есть двойник `scripts/restart.cmd` с теми же действиями. Команды защиты по шагам — в [DEMO.md](DEMO.md).
|
||||
|
||||
## Архитектура и решения
|
||||
|
||||
TODO: схема процессов/хранилищ, источник истины, транзакционные границы, ограничения. Что перенесено из lab1, что изменено в этой работе. Соответствие переменных из .env.example реальной конфигурации.
|
||||
```
|
||||
клиент ──HTTP──> api (Python, поток на соединение)
|
||||
├─ пул ≤ 10 соединений ──> postgres: users, chats, chat_members, messages, schema_migrations [volume pgdata]
|
||||
└─ пул ≤ 20 соединений ──> valkey: сессии session:<sha256 токена>, AOF [volume valkeydata]
|
||||
migrate (одноразовый) ──> postgres: применяет migrations/*.sql
|
||||
```
|
||||
|
||||
| Файл | Назначение |
|
||||
| --- | --- |
|
||||
| `src/messenger/server.py` | HTTP: маршруты, аутентификация, формат ответов, лог запросов |
|
||||
| `src/messenger/db.py` | PostgreSQL: пул, таймауты, транзакции, все SQL-запросы |
|
||||
| `src/messenger/sessions.py` | Сессии в Valkey |
|
||||
| `src/messenger/passwords.py` | Argon2id |
|
||||
| `src/messenger/validation.py` | Разбор JSON, `limit`, `cursor`, заголовков Basic и Bearer |
|
||||
| `src/messenger/migrate.py`, `migrations/` | Миграции схемы |
|
||||
| `compose.yaml`, `infra/valkey/valkey.conf` | Стенд и настройки Valkey |
|
||||
| `student-tests/` | Собственные тесты API (чёрный ящик по HTTP) |
|
||||
| `scripts/lab2_checks.py`, `scripts/explain_history.py` | Проверки инфраструктуры и план запроса |
|
||||
|
||||
**Источник истины.** Пользователи, чаты, участники и сообщения хранятся в PostgreSQL. Сессии хранятся только в Valkey: это единственное хранилище сессий, поэтому у Valkey включена persistence (подробности ниже).
|
||||
|
||||
**Схема.** Описана в `migrations/0001_schema.sql`.
|
||||
- Первичные ключи у всех таблиц, `username` уникален.
|
||||
- Внешние ключи: `chat_members` → `chats`, `users`; `messages(chat_id, sender_id)` → `chat_members`. Последний означает, что отправитель обязан быть участником чата, и это проверяет база, а не только код.
|
||||
- CHECK-ограничения повторяют правила контракта: формат `username` и длины полей.
|
||||
- `chat_members.position` хранит порядок `member_ids` из запроса.
|
||||
|
||||
**Миграции.** Файлы `migrations/NNNN_*.sql` применяются по порядку, каждый в своей транзакции, и записываются в `schema_migrations` с контрольной суммой.
|
||||
- Повторный запуск ничего не делает.
|
||||
- Изменённый после применения файл останавливает запуск: схема меняется только новым файлом.
|
||||
- `pg_advisory_lock` не даёт двум процессам применять миграции одновременно.
|
||||
|
||||
**Транзакционные границы.** Каждый метод `Repository` — одна транзакция.
|
||||
- **Создание чата.** `INSERT` в `chats` и затем `INSERT` всех участников. Неизвестный участник нарушает внешний ключ на втором шаге, и транзакция откатывается целиком: строка чата тоже исчезает.
|
||||
- **Отправка сообщения.** `SELECT ... FOR NO KEY UPDATE` блокирует строку чата, затем выполняется `INSERT` сообщения. Блокировка нужна для порядка истории (см. ниже).
|
||||
- **Регистрация.** Один `INSERT`. Гонку одинаковых username решает уникальный индекс: проходит один запрос, остальные получают `UniqueViolation` и ответ 409.
|
||||
|
||||
**Durability.** PostgreSQL работает с настройками по умолчанию (`fsync=on`, `synchronous_commit=on`): ответ 201 отправляется после COMMIT, когда запись уже в WAL на диске. Valkey — `appendonly yes`, `appendfsync always`: сессия подтверждается клиенту после fsync журнала. Записей сессий мало (только вход и выход), поэтому цена fsync незаметна. `maxmemory-policy noeviction`: при нехватке памяти новая сессия получит ошибку (API ответит 503), а уже выданные не вытесняются молча.
|
||||
|
||||
**Сессии.**
|
||||
- **Токен.** Создаётся через `secrets.token_urlsafe(32)`: 256 бит, 43 символа. Это не JWT и не идентификатор пользователя.
|
||||
- **Хранение.** Ключ в Valkey — `session:<sha256 от токена>`: копия базы не даёт готовых токенов. Значение — `user_id` и срок.
|
||||
- **Срок жизни.** Задаётся `SESSION_TTL_SECONDS` и ставится абсолютным временем (`SET ... PXAT`). Поэтому рестарт ничего не продлевает, и обращения тоже не продлевают сессию.
|
||||
- **Инвалидация.** Выход (`DELETE /auth/sessions/current`) удаляет только текущий ключ. Истёкшие ключи удаляет сам Valkey.
|
||||
- **Рестарт и flush.** После рестарта Valkey сессии восстанавливаются из AOF. После `FLUSHALL` или потери volume все сессии пропадают: пользователи получают 401 и входят заново, остальные данные в PostgreSQL не затрагиваются.
|
||||
|
||||
**Пароли и вход.**
|
||||
- Пароль хешируется Argon2id: 19 МиБ памяти, 2 прохода, 1 поток — минимальные параметры OWASP. В БД хранится строка `$argon2id$v=19$m=19456,t=2,p=1$...`.
|
||||
- Basic принимается только на `POST /auth/sessions`. Base64 декодируется строго, пароль — в UTF-8 и делится по первому двоеточию, так что двоеточие в пароле допустимо.
|
||||
- Любая ошибка входа даёт одинаковый ответ 401 с `WWW-Authenticate: Basic realm="messenger", charset="UTF-8"`. Для несуществующего username пароль сверяется с фиктивным хешем, поэтому по задержке ответа нельзя узнать, есть ли такой пользователь.
|
||||
|
||||
**Аутентификация и доступ — разные проверки.** Аутентификация отвечает на вопрос «кто ты»: только `Authorization: Bearer`, а Basic и `X-User-Id` на остальных endpoint дают 401 с `WWW-Authenticate: Bearer realm="messenger"`. Доступ отвечает на вопрос «можно ли тебе в этот чат»: SQL-запрос с условием членства, и для постороннего ответ 404 такой же, как для несуществующего чата. Порядок проверок: 401 → 400 (`chat_id`) → 404 → 400 (тело и параметры).
|
||||
|
||||
**Безопасность запросов, пул и таймауты.**
|
||||
- SQL всегда параметризован (`%s`, значения передаются отдельно от текста), поэтому SQL-инъекция невозможна.
|
||||
- Пул PostgreSQL:
|
||||
- не больше `DB_POOL_SIZE=10` соединений;
|
||||
- ожидание свободного соединения — до 3 с, в очереди не больше 100 запросов;
|
||||
- перед выдачей соединение проверяется, так что после рестарта БД оборванные соединения не используются.
|
||||
- Таймауты на стороне БД: `connect_timeout` 3 с, `statement_timeout` 5 с, `lock_timeout` 3 с, `idle_in_transaction_session_timeout` 10 с.
|
||||
- Valkey: не больше 20 соединений, ожидание соединения 1 с, подключение и ответ — по 1 с, один повтор (чтобы переподключиться после рестарта Valkey).
|
||||
|
||||
**Недоступность хранилищ.** Ответ ограничен по времени, и запрос никогда не продолжается как анонимный.
|
||||
- **Valkey не отвечает.** Аутентифицированные запросы и вход получают 503 `unavailable` за 1 с. Запрос без токена получает 401. Регистрация работает: ей нужен только PostgreSQL.
|
||||
- **PostgreSQL не отвечает.** 503 за 3 с (ожидание пула).
|
||||
- **Готовность.** `/health/ready` требует обоих хранилищ, `/health/live` — только живого процесса.
|
||||
- **Восстановление.** Происходит само, без перезапуска API.
|
||||
- **DNS остановленного контейнера.** Сначала ответ при упавшем Valkey шёл 4 с. Замер показал, что столько встроенный DNS Docker ищет имя остановленного контейнера. Поэтому в `compose.yaml` для API стоит `dns_opt: timeout:1, attempts:1`.
|
||||
|
||||
**Порядок истории и пагинация.**
|
||||
- **Курсор.** Keyset по `(created_at, id)`, как в lab1: cursor — base64url от `область|created_at в мкс|id` и привязан к чату (или к пользователю для списка чатов).
|
||||
- **Индекс.** `messages_chat_history_idx (chat_id, created_at, id)` совпадает с запросом страницы, поэтому страница читается диапазоном индекса без сортировки (см. доказательства).
|
||||
- **Конкурентные вставки.** Строка чата блокируется на время вставки, а время сообщения — `GREATEST(clock_timestamp(), max(created_at) + 1 мкс)` по этому чату. Поэтому сообщения одного чата получают время строго по порядку фиксации: читатель, идущий по cursor, не пропустит сообщение, зафиксированное позже уже прочитанного, даже если системные часы откатятся назад.
|
||||
- **Ограничение для списка чатов.** Время чатов назначается без такой сериализации. При одновременном создании чатов теоретически возможен пропуск при обходе списка, если его читают в тот же момент.
|
||||
|
||||
**Что перенесено из lab1 и что изменено.**
|
||||
- **Перенесено без изменения смысла:** HTTP-слой, формат ошибок, `X-Request-Id`, валидация, формат cursor, плавная остановка по SIGTERM, логи без содержимого сообщений, практики Dockerfile (non-root, закреплённый образ, healthcheck, `.dockerignore`), собственные тесты.
|
||||
- **Хранилище и вход:** состояние в памяти заменено на PostgreSQL и Valkey; `X-User-Id` заменён на Basic и Bearer; добавлены вход, выход и `password` при регистрации.
|
||||
- **Новые правила:** запрет символа NUL (его нельзя сохранить в `text` PostgreSQL), ответы 503 при недоступности хранилищ.
|
||||
- **Healthcheck:** теперь дочитывает ответ, чтобы не оставлять оборванных соединений.
|
||||
|
||||
**Переменные из `.env.example`.**
|
||||
|
||||
| Переменная | Где используется | По умолчанию |
|
||||
| --- | --- | --- |
|
||||
| `APP_HOST`, `APP_PORT` | Адрес и порт API внутри контейнера, заданы в `compose.yaml` | `0.0.0.0`, `8080` |
|
||||
| `APP_PUBLISHED_PORT` | Порт API на `127.0.0.1` хоста | `8080` |
|
||||
| `APP_ENV`, `APP_LOG_LEVEL` | Метка окружения в логе, уровень логов | `lab`, `INFO` |
|
||||
| `APP_SHUTDOWN_TIMEOUT` | Ожидание начатых запросов после SIGTERM, не больше 10 с | `8` |
|
||||
| `SESSION_TTL_SECONDS` | Срок жизни сессии | `3600` |
|
||||
| `DB_POOL_SIZE` | Максимум соединений с PostgreSQL на процесс API | `10` |
|
||||
| `DATABASE_URL_FILE` | Файл секрета со строкой подключения к PostgreSQL | `/run/secrets/database_url` |
|
||||
| `CACHE_URL_FILE` | Файл секрета с адресом и паролем Valkey | `/run/secrets/cache_url` |
|
||||
| `SESSION_STORE` | Не используется: хранилище сессий всегда Valkey | — |
|
||||
|
||||
## Доказательства критериев
|
||||
|
||||
Все проверки выполнены 2026-10-07 на compose-стенде: Windows 11, Docker Desktop 29.8.1, Intel Core Ultra 9 185H (16 ядер, 22 потока), 31 ГиБ RAM, без лимитов CPU/RAM на контейнеры.
|
||||
|
||||
Файлы доказательств:
|
||||
- [evidence/tests.txt](evidence/tests.txt) — тесты;
|
||||
- [evidence/clean-clone.txt](evidence/clean-clone.txt) — сборка и тесты из чистого clone на новых volumes;
|
||||
- [evidence/race.json](evidence/race.json) — конкурентный тест;
|
||||
- [evidence/lab2-checks.txt](evidence/lab2-checks.txt) — проверки инфраструктуры, `RESULT: PASS`, 17 из 17;
|
||||
- [evidence/explain.txt](evidence/explain.txt) — планы запроса.
|
||||
|
||||
| Критерий README | Команда/сценарий | Наблюдаемый результат | Файл доказательства |
|
||||
| --- | --- | --- | --- |
|
||||
| Публичный smoke | make test | TODO | TODO |
|
||||
| Остальные пункты уровня 3 | TODO | TODO | TODO |
|
||||
| Все пункты уровня 4, если заявлен | TODO | TODO | TODO |
|
||||
| Выбранный трек 5, если заявлен | TODO | TODO | TODO |
|
||||
|
||||
TODO: добавить отдельную строку для каждого заявленного критерия. PASS без команды и наблюдения не является доказательством. Проверка инфраструктурного hook требует подтверждения, какие реальные процессы/хранилища перезапущены.
|
||||
| 3: публичный smoke | `python tests/smoke.py --base-url http://127.0.0.1:8080` (`make test`) | `PASS: lab2 public API scenario` | evidence/tests.txt |
|
||||
| 3: минимум lab1 с новой аутентификацией | `python -m unittest discover -s student-tests -v` | 73 теста `OK`: валидация, Unicode, членство, пагинация, ошибки | evidence/tests.txt |
|
||||
| 3: данные и действующие сессии переживают рестарт API и хранилищ | `tests/resilience.py --mode persistence` с хуком `scripts/restart.cmd` (в Linux — `restart.sh`) | `PASS`; StartedAt у api, postgres и valkey сменился, volumes и их даты создания те же | evidence/lab2-checks.txt, раздел Full restart |
|
||||
| 3: сборка из чистого clone, миграции с нуля | Clone ветки, `init_secrets.py`, `docker compose up -d --build --wait` в отдельном проекте с новыми volumes | Образ собран; миграции на пустом volume — `applied_now=2`, повторно — `applied_now=0`; smoke, 73 теста и тест сохранности — PASS | evidence/clean-clone.txt |
|
||||
| 3: ключи, уникальность username | Список `pg_constraint` и индексов | Есть PK, FK, UNIQUE и CHECK, индекс истории | evidence/lab2-checks.txt, раздел Migrations |
|
||||
| 3: миграции повторно без дублирования | `docker compose run --rm --no-deps migrate` | `applied_now=0`, строки `schema_migrations` и `users` не изменились (2 и 62) | evidence/lab2-checks.txt |
|
||||
| 3: создание чата атомарно | POST чата с неизвестным участником, затем подсчёт строк в БД | 400; строк с этим title — 0; чатов без участников — 0; в логе `chat_rolled_back` | evidence/lab2-checks.txt, раздел Rollback |
|
||||
| 3: Basic только для входа, Bearer для остального, X-User-Id не даёт доступ | `student-tests/test_sessions.py`, `test_access.py` | 401 с нужным `WWW-Authenticate`; X-User-Id игнорируется | evidence/tests.txt |
|
||||
| 3: срок жизни, отзыв, KDF | `test_sessions.LogoutTest`; запрос к `users` | Выход — 204, затем 401; в БД только `$argon2id$...` | evidence/tests.txt; DEMO.md, шаг 1 |
|
||||
| 3: секреты и volumes не в Git | `.gitignore` (`secrets/`, `.env`); `scripts/init_secrets.py` | Пароли — случайные файлы вне Git; поиск паролей и токенов в evidence — 0 совпадений | evidence/lab2-checks.txt, раздел Logs |
|
||||
| 4: Valkey реально используется для сессий с TTL, persistence | `CONFIG GET appendonly/appendfsync/maxmemory-policy`; рестарт Valkey | `yes`, `always`, `noeviction`; после рестарта тот же токен — 200 | evidence/lab2-checks.txt, разделы Stack и Valkey is down |
|
||||
| 4: параметризованные запросы | `src/messenger/db.py` | Все запросы с `%s`, без подстановки строк в SQL | — |
|
||||
| 4: ограниченный пул | 60 параллельных клиентов 5 с, `pg_stat_activity` | 1019 запросов, все 200; соединений API — максимум 10 | evidence/lab2-checks.txt, раздел Connection pool |
|
||||
| 4: таймауты | Строка чата заблокирована на 6 с из `psql` | POST сообщения — 503 через 3,01 с (`lock_timeout`), после снятия блокировки — 201 | evidence/lab2-checks.txt, раздел Timeouts |
|
||||
| 4: гонка регистрации одинакового username | 20 одновременных POST `/users`; `test_concurrency` | Один 201 и девятнадцать 409; в БД одна строка; войти можно только с паролем победителя | evidence/lab2-checks.txt, раздел Registration race; evidence/tests.txt |
|
||||
| 4: rollback составной операции | См. строку «создание чата атомарно» | Чат не остался в БД | evidence/lab2-checks.txt |
|
||||
| 4: индекс истории и EXPLAIN (ANALYZE, BUFFERS) на ≥10 000 сообщений | `python scripts/explain_history.py` | 30 302 сообщения, 10 000 в целевом чате. С индексом: Index Scan, 51 строка, 4–5 буферов, 0,04–0,07 мс. Без индекса: Seq Scan, отброшено 25 302 строки, 416 буферов, 1,98 мс | evidence/explain.txt |
|
||||
| 4: пагинация без полного scan истории | Тот же план для первой, средней, последней страницы и для запроса с параметрами `$1..$4` | Во всех случаях Index Cond с `ROW(created_at, id) > ...`, без Sort | evidence/explain.txt |
|
||||
| 4: истечение сессии при коротком TTL | Вторая копия API с `SESSION_TTL_SECONDS=2` (`--only expiry`) | Сразу — 200, PTTL ключа 1767 мс; через 3 с — 401, ключа в Valkey нет | evidence/lab2-checks.txt, раздел Session expiry |
|
||||
| 4: logout и неверный пароль | `test_sessions.LogoutTest`, `LoginTest` | 204 и затем 401; неверный пароль и неизвестный пользователь — одинаковый 401 | evidence/tests.txt |
|
||||
| 4: недоступность Valkey | `docker compose stop valkey` | С токеном и при входе — 503 за 1,0 с; без токена — 401; регистрация — 201; ready — 503, live — 200 | evidence/lab2-checks.txt, раздел Valkey is down |
|
||||
| 4: недоступность PostgreSQL | `docker compose stop postgres` | 503 за 3,0 с; без токена — 401; после старта БД тот же токен — 200 без перезапуска API | evidence/lab2-checks.txt, раздел PostgreSQL is down |
|
||||
| 100 сообщений от 10 клиентов (из lab1) | `python student-tests/race.py` | 100 принято, 100 уникальных в истории, порядок сохранён | evidence/race.json |
|
||||
| SIGTERM (из lab1) | `docker compose stop -t 10 api` | 0,80 с, код выхода 0, новые запросы не принимаются | evidence/lab2-checks.txt, раздел SIGTERM |
|
||||
|
||||
## Эксперименты и отказы
|
||||
|
||||
TODO: гипотеза, hardware/лимиты, версии, объём данных, профиль нагрузки, concurrency/arrival rate, длительность/прогрев, повторы, успешные RPS, ошибки, p50/p95/p99, CPU/RAM. Сохранить raw summary без tokens и объяснить ограничения измерения.
|
||||
Нагрузочное исследование и треки уровня 5 не проводились. Ниже — отказы уровня 4 на стенде, описанном выше.
|
||||
|
||||
| Воздействие | Что ожидаем | Что наблюдали | Время восстановления / потеря данных |
|
||||
| --- | --- | --- | --- |
|
||||
| TODO | TODO | TODO | TODO |
|
||||
| Перезапуск api, postgres и valkey хуком (volumes сохранены) | Те же данные, тот же токен без повторного входа | Публичный тест сохранности — `PASS` | Хук и ожидание готовности — 10,5 с; потерь нет |
|
||||
| `docker compose stop valkey` | Ограниченный 503, нет доверия анонимному запросу | 503 за 1,0 с; без токена — 401; регистрация работает | После старта Valkey тот же токен работает; сессии сохранены AOF |
|
||||
| `docker compose stop postgres` | Ограниченный 503 | 503 за 3,0 с | API восстановился сам; потерь нет |
|
||||
| Строка чата заблокирована 6 с | Запрос не висит дольше `lock_timeout` | 503 за 3,01 с | Запись работает сразу после снятия блокировки |
|
||||
| 60 параллельных клиентов | Соединений с БД не больше пула | Максимум 10 соединений, 1019 из 1019 запросов — 200 | — |
|
||||
| Сессия с TTL 2 с | 401 после истечения | 401 через 3 с, ключ удалён Valkey | Нужен новый вход — ожидаемо |
|
||||
| SIGTERM для API | Остановка до 10 с, код 0 | 0,80 с, код 0 | Начатые запросы завершены |
|
||||
|
||||
## Ограничения и следующие шаги
|
||||
|
||||
TODO: какие отказы выдерживаются и какие нет; что не реализовано; где учебное упрощение; какое улучшение подтверждено, а какое пока гипотеза.
|
||||
- **Нет отказоустойчивости хранилищ.** Один процесс API, по одному PostgreSQL и Valkey на одном Docker-хосте. Потеря volume `valkeydata` или `FLUSHALL` разлогинивает всех; потеря `pgdata` теряет все данные (резервного копирования нет — это трек 2 уровня 5).
|
||||
- **Подбор пароля не ограничен.** Защиты от перебора паролей и ограничения частоты запросов нет. Каждый вход стоит около 24 мс (медиана по логам стенда), в основном это Argon2id.
|
||||
- **Потоки не ограничены.** Поток создаётся на каждое соединение. Обращения к хранилищам ограничены пулами, а сами потоки — нет.
|
||||
- **Список чатов.** При одновременном создании чатов возможен пропуск в списке, если его читают в тот же момент (см. «Порядок истории»).
|
||||
- **Нет TLS.** Транспорт — HTTP: стенд только для локального запуска с синтетическими данными.
|
||||
- **Замеры EXPLAIN.** Сделаны на синтетических данных с прогретым кэшем; это доказательство формы плана, а не исследование производительности.
|
||||
- **Нет CI.** Job со сборкой стенда и `make test` не добавлен; все проверки воспроизводятся локально командами из раздела «Запуск из чистого clone», результаты — в `evidence/`.
|
||||
|
||||
+106
@@ -0,0 +1,106 @@
|
||||
name: mtusi-lab2
|
||||
|
||||
x-app: &app
|
||||
build: .
|
||||
image: mtusi-messenger:lab2
|
||||
dns_opt:
|
||||
- timeout:1
|
||||
- attempts:1
|
||||
|
||||
x-api-environment: &api-environment
|
||||
APP_HOST: 0.0.0.0
|
||||
APP_PORT: "8080"
|
||||
APP_ENV: ${APP_ENV:-lab}
|
||||
APP_LOG_LEVEL: ${APP_LOG_LEVEL:-INFO}
|
||||
APP_SHUTDOWN_TIMEOUT: ${APP_SHUTDOWN_TIMEOUT:-8}
|
||||
SESSION_TTL_SECONDS: ${SESSION_TTL_SECONDS:-3600}
|
||||
DB_POOL_SIZE: ${DB_POOL_SIZE:-10}
|
||||
DATABASE_URL_FILE: /run/secrets/database_url
|
||||
CACHE_URL_FILE: /run/secrets/cache_url
|
||||
|
||||
x-api-dependencies: &api-dependencies
|
||||
postgres:
|
||||
condition: service_healthy
|
||||
valkey:
|
||||
condition: service_healthy
|
||||
migrate:
|
||||
condition: service_completed_successfully
|
||||
|
||||
services:
|
||||
postgres:
|
||||
image: postgres:18.6-trixie@sha256:74935e72241653ca55e0414067e6d8763aceb8a810eb51b452253ec3dcfc4336
|
||||
environment:
|
||||
POSTGRES_DB: messenger
|
||||
POSTGRES_USER: messenger
|
||||
POSTGRES_PASSWORD_FILE: /run/secrets/postgres_password
|
||||
secrets: [postgres_password]
|
||||
volumes:
|
||||
- pgdata:/var/lib/postgresql
|
||||
healthcheck:
|
||||
test: ["CMD", "pg_isready", "--username=messenger", "--dbname=messenger"]
|
||||
interval: 5s
|
||||
timeout: 3s
|
||||
retries: 10
|
||||
start_period: 10s
|
||||
restart: unless-stopped
|
||||
|
||||
valkey:
|
||||
image: valkey/valkey:9.0.6-trixie@sha256:63346cb24a61221e76bdf41acce99b3968a9fa83d8122144deab45394b27b4f2
|
||||
command: ["valkey-server", "/usr/local/etc/valkey/valkey.conf"]
|
||||
secrets: [valkey_auth]
|
||||
volumes:
|
||||
- valkeydata:/data
|
||||
- ./infra/valkey/valkey.conf:/usr/local/etc/valkey/valkey.conf:ro
|
||||
healthcheck:
|
||||
test: ["CMD-SHELL", "valkey-cli --no-auth-warning -a \"$$(sed -n 's/^requirepass //p' /run/secrets/valkey_auth)\" ping | grep -q PONG"]
|
||||
interval: 5s
|
||||
timeout: 3s
|
||||
retries: 10
|
||||
start_period: 5s
|
||||
restart: unless-stopped
|
||||
|
||||
migrate:
|
||||
<<: *app
|
||||
command: ["python", "-m", "messenger.migrate"]
|
||||
environment:
|
||||
DATABASE_URL_FILE: /run/secrets/database_url
|
||||
secrets: [database_url]
|
||||
depends_on:
|
||||
postgres:
|
||||
condition: service_healthy
|
||||
restart: "no"
|
||||
|
||||
api:
|
||||
<<: *app
|
||||
environment: *api-environment
|
||||
secrets: [database_url, cache_url]
|
||||
ports:
|
||||
- "127.0.0.1:${APP_PUBLISHED_PORT:-8080}:8080"
|
||||
depends_on: *api-dependencies
|
||||
stop_grace_period: 10s
|
||||
restart: unless-stopped
|
||||
|
||||
api-short-ttl:
|
||||
<<: *app
|
||||
profiles: ["expiry"]
|
||||
environment:
|
||||
<<: *api-environment
|
||||
SESSION_TTL_SECONDS: "2"
|
||||
secrets: [database_url, cache_url]
|
||||
ports:
|
||||
- "127.0.0.1:8081:8080"
|
||||
depends_on: *api-dependencies
|
||||
|
||||
volumes:
|
||||
pgdata:
|
||||
valkeydata:
|
||||
|
||||
secrets:
|
||||
postgres_password:
|
||||
file: ./secrets/postgres_password
|
||||
database_url:
|
||||
file: ./secrets/database_url
|
||||
valkey_auth:
|
||||
file: ./secrets/valkey_auth
|
||||
cache_url:
|
||||
file: ./secrets/cache_url
|
||||
@@ -0,0 +1,69 @@
|
||||
# Build and test from a fresh clone of the solution branch, separate compose project with new volumes.
|
||||
# Date: 2026-10-07 16:07 UTC; host Windows 11, Docker Desktop; clone made with core.autocrlf=true (as on the author machine).
|
||||
# COMPOSE_PROJECT_NAME=mtusi-lab2-clean, APP_PUBLISHED_PORT=8090, so the main stand on 8080 is not touched.
|
||||
|
||||
$ git clone --branch solution/bvt2304-chernikov <repo> clean2
|
||||
[exit code 0]
|
||||
|
||||
$ git log --oneline -1
|
||||
5b4fa0e ЛР 2 — БВТ2304 — Черников
|
||||
[exit code 0]
|
||||
|
||||
$ python scripts/init_secrets.py
|
||||
created secrets/postgres_password
|
||||
created secrets/valkey_password
|
||||
written secrets/database_url, secrets/valkey_auth, secrets/cache_url
|
||||
[exit code 0]
|
||||
|
||||
$ cp .env.example .env
|
||||
|
||||
$ docker compose up -d --build --wait
|
||||
Network mtusi-lab2-clean_default Created
|
||||
Network mtusi-lab2-clean_default Created
|
||||
Container mtusi-lab2-clean-valkey-1 Created
|
||||
Container mtusi-lab2-clean-postgres-1 Created
|
||||
Container mtusi-lab2-clean-migrate-1 Created
|
||||
Container mtusi-lab2-clean-api-1 Created
|
||||
Container mtusi-lab2-clean-postgres-1 Healthy
|
||||
Container mtusi-lab2-clean-postgres-1 Healthy
|
||||
Container mtusi-lab2-clean-valkey-1 Healthy
|
||||
Container mtusi-lab2-clean-migrate-1 Exited
|
||||
Container mtusi-lab2-clean-migrate-1 Exited
|
||||
Container mtusi-lab2-clean-valkey-1 Healthy
|
||||
Container mtusi-lab2-clean-postgres-1 Healthy
|
||||
Container mtusi-lab2-clean-api-1 Healthy
|
||||
[exit code 0]
|
||||
|
||||
$ docker compose logs --no-log-prefix migrate
|
||||
INFO event=migration_applied version=0001_schema duration_ms=6
|
||||
INFO event=migration_applied version=0002_messages_history_index duration_ms=3
|
||||
INFO event=migrations_done applied_now=2 total=2
|
||||
[exit code 0]
|
||||
|
||||
$ docker compose run --rm --no-deps migrate
|
||||
INFO event=migrations_done applied_now=0 total=2
|
||||
[exit code 0]
|
||||
|
||||
$ python tests/smoke.py --base-url http://127.0.0.1:8090
|
||||
PASS: lab2 public API scenario; infrastructure and grading evidence remain separate.
|
||||
[exit code 0]
|
||||
|
||||
$ BASE_URL=http://127.0.0.1:8090 python -m unittest discover -s student-tests
|
||||
Ran 73 tests in 10.872s
|
||||
OK
|
||||
[exit code 0]
|
||||
|
||||
$ python tests/resilience.py --mode persistence --action-script scripts/restart.cmd --base-url http://127.0.0.1:8090
|
||||
Executing explicit persistence hook; hook output suppressed to avoid leaking secrets.
|
||||
PASS: persistence preserved confirmed data and existing session. Hook scope needs operator evidence.
|
||||
[exit code 0]
|
||||
|
||||
$ docker compose down -v (removes only the mtusi-lab2-clean project and its volumes)
|
||||
Volume mtusi-lab2-clean_pgdata Removing
|
||||
Volume mtusi-lab2-clean_valkeydata Removing
|
||||
Volume mtusi-lab2-clean_pgdata Removed
|
||||
Volume mtusi-lab2-clean_valkeydata Removed
|
||||
[exit code 0]
|
||||
|
||||
# exit codes: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
|
||||
RESULT: PASS
|
||||
@@ -0,0 +1,96 @@
|
||||
# 2026-10-07 16:04:34 UTC, PostgreSQL 18.6 (Debian 18.6-1.pgdg13+2) on x86_64-pc-linux-gnu, compiled by gcc (Debian 14.2.0-19) 14.2.0, 64-bit
|
||||
# target chat 5c31bb3d-a9e4-4c7c-a268-d15c483a2d25: 10000 messages; noise chat 0d65d937-959b-44c6-a012-df0c5a5ad525: 20000 messages
|
||||
# inserted and analyzed in 0.6 s
|
||||
messages total: 30302, in target chat: 10000
|
||||
|
||||
== 1. First page, middle page and last page: index range scan, no sort ==
|
||||
EXPLAIN (ANALYZE, BUFFERS, COSTS OFF) SELECT id, chat_id, sender_id, text, created_at FROM messages WHERE chat_id = '5c31bb3d-a9e4-4c7c-a268-d15c483a2d25' ORDER BY created_at, id LIMIT 51;
|
||||
QUERY PLAN
|
||||
------------------------------------------------------------------------------------------------------------
|
||||
Limit (actual time=0.036..0.048 rows=51.00 loops=1)
|
||||
Buffers: shared hit=4
|
||||
-> Index Scan using messages_chat_history_idx on messages (actual time=0.035..0.041 rows=51.00 loops=1)
|
||||
Index Cond: (chat_id = '5c31bb3d-a9e4-4c7c-a268-d15c483a2d25'::uuid)
|
||||
Index Searches: 1
|
||||
Buffers: shared hit=4
|
||||
Planning:
|
||||
Buffers: shared hit=112
|
||||
Planning Time: 0.414 ms
|
||||
Execution Time: 0.069 ms
|
||||
(10 rows)
|
||||
|
||||
EXPLAIN (ANALYZE, BUFFERS, COSTS OFF) SELECT id, chat_id, sender_id, text, created_at FROM messages WHERE chat_id = '5c31bb3d-a9e4-4c7c-a268-d15c483a2d25' AND (created_at, id) > ('2026-10-07 16:04:39.873383+00', 'fa6a0ba6-382c-459f-bab6-feab2ba3c2d3') ORDER BY created_at, id LIMIT 51;
|
||||
QUERY PLAN
|
||||
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
Limit (actual time=0.014..0.021 rows=51.00 loops=1)
|
||||
Buffers: shared hit=5
|
||||
-> Index Scan using messages_chat_history_idx on messages (actual time=0.013..0.018 rows=51.00 loops=1)
|
||||
Index Cond: ((chat_id = '5c31bb3d-a9e4-4c7c-a268-d15c483a2d25'::uuid) AND (ROW(created_at, id) > ROW('2026-10-07 16:04:39.873383+00'::timestamp with time zone, 'fa6a0ba6-382c-459f-bab6-feab2ba3c2d3'::uuid)))
|
||||
Index Searches: 1
|
||||
Buffers: shared hit=5
|
||||
Planning Time: 0.048 ms
|
||||
Execution Time: 0.041 ms
|
||||
(8 rows)
|
||||
|
||||
EXPLAIN (ANALYZE, BUFFERS, COSTS OFF) SELECT id, chat_id, sender_id, text, created_at FROM messages WHERE chat_id = '5c31bb3d-a9e4-4c7c-a268-d15c483a2d25' AND (created_at, id) > ('2026-10-07 16:04:44.872196+00', '2db6f051-d2f0-4608-a584-347e2b610391') ORDER BY created_at, id LIMIT 51;
|
||||
QUERY PLAN
|
||||
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
Limit (actual time=0.021..0.026 rows=20.00 loops=1)
|
||||
Buffers: shared hit=4
|
||||
-> Index Scan using messages_chat_history_idx on messages (actual time=0.020..0.023 rows=20.00 loops=1)
|
||||
Index Cond: ((chat_id = '5c31bb3d-a9e4-4c7c-a268-d15c483a2d25'::uuid) AND (ROW(created_at, id) > ROW('2026-10-07 16:04:44.872196+00'::timestamp with time zone, '2db6f051-d2f0-4608-a584-347e2b610391'::uuid)))
|
||||
Index Searches: 1
|
||||
Buffers: shared hit=4
|
||||
Planning Time: 0.061 ms
|
||||
Execution Time: 0.038 ms
|
||||
(8 rows)
|
||||
|
||||
== 2. Generic plan of the parameterized query (how a prepared statement with $1..$4 runs) ==
|
||||
SET plan_cache_mode = force_generic_plan;
|
||||
SET
|
||||
PREPARE history_page(uuid, timestamptz, uuid, int) AS
|
||||
SELECT id, chat_id, sender_id, text, created_at FROM messages
|
||||
WHERE chat_id = $1 AND (created_at, id) > ($2, $3) ORDER BY created_at, id LIMIT $4;
|
||||
PREPARE
|
||||
EXPLAIN (ANALYZE, BUFFERS, COSTS OFF) EXECUTE history_page('5c31bb3d-a9e4-4c7c-a268-d15c483a2d25', '2026-10-07 16:04:39.873383+00', 'fa6a0ba6-382c-459f-bab6-feab2ba3c2d3', 51);
|
||||
QUERY PLAN
|
||||
------------------------------------------------------------------------------------------------------------
|
||||
Limit (actual time=0.021..0.030 rows=51.00 loops=1)
|
||||
Buffers: shared hit=5
|
||||
-> Index Scan using messages_chat_history_idx on messages (actual time=0.020..0.025 rows=51.00 loops=1)
|
||||
Index Cond: ((chat_id = $1) AND (ROW(created_at, id) > ROW($2, $3)))
|
||||
Index Searches: 1
|
||||
Buffers: shared hit=5
|
||||
Planning:
|
||||
Buffers: shared hit=111
|
||||
Planning Time: 0.356 ms
|
||||
Execution Time: 0.064 ms
|
||||
(10 rows)
|
||||
|
||||
== 3. For comparison: the same middle page without the index (rolled back) ==
|
||||
BEGIN;
|
||||
BEGIN
|
||||
DROP INDEX messages_chat_history_idx;
|
||||
DROP INDEX
|
||||
EXPLAIN (ANALYZE, BUFFERS, COSTS OFF) SELECT id, chat_id, sender_id, text, created_at FROM messages WHERE chat_id = '5c31bb3d-a9e4-4c7c-a268-d15c483a2d25' AND (created_at, id) > ('2026-10-07 16:04:39.873383+00', 'fa6a0ba6-382c-459f-bab6-feab2ba3c2d3') ORDER BY created_at, id LIMIT 51;
|
||||
QUERY PLAN
|
||||
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
Limit (actual time=1.944..1.949 rows=51.00 loops=1)
|
||||
Buffers: shared hit=416
|
||||
-> Sort (actual time=1.942..1.944 rows=51.00 loops=1)
|
||||
Sort Key: created_at, id
|
||||
Sort Method: top-N heapsort Memory: 31kB
|
||||
Buffers: shared hit=416
|
||||
-> Seq Scan on messages (actual time=0.315..1.677 rows=5000.00 loops=1)
|
||||
Filter: ((chat_id = '5c31bb3d-a9e4-4c7c-a268-d15c483a2d25'::uuid) AND (ROW(created_at, id) > ROW('2026-10-07 16:04:39.873383+00'::timestamp with time zone, 'fa6a0ba6-382c-459f-bab6-feab2ba3c2d3'::uuid)))
|
||||
Rows Removed by Filter: 25302
|
||||
Buffers: shared hit=410
|
||||
Planning:
|
||||
Buffers: shared hit=83
|
||||
Planning Time: 0.271 ms
|
||||
Execution Time: 1.976 ms
|
||||
(14 rows)
|
||||
|
||||
ROLLBACK;
|
||||
ROLLBACK
|
||||
index still exists: true
|
||||
@@ -0,0 +1,238 @@
|
||||
# 2026-10-07 16:03:13 UTC, python 3.14.7, win32
|
||||
$ docker version --format docker {{.Server.Version}}
|
||||
# docker 29.8.1
|
||||
== Stack: services, pinned images, health, non-root, Valkey persistence ==
|
||||
$ docker compose ps -a --format table {{.Service}} {{.Image}} {{.Status}}
|
||||
SERVICE IMAGE STATUS
|
||||
api mtusi-messenger:lab2 Up 58 seconds (healthy)
|
||||
migrate mtusi-messenger:lab2 Exited (0) 59 seconds ago
|
||||
postgres postgres:18.6-trixie@sha256:74935e72241653ca55e0414067e6d8763aceb8a810eb51b452253ec3dcfc4336 Up About a minute (healthy)
|
||||
valkey valkey/valkey:9.0.6-trixie@sha256:63346cb24a61221e76bdf41acce99b3968a9fa83d8122144deab45394b27b4f2 Up About a minute (healthy)
|
||||
$ docker compose config --format json
|
||||
=> PASS: third-party images are pinned by tag and digest (postgres:18.6-trixie@sha256:74935e72241653ca55e0414067e6d8763aceb8a810eb51b452253ec3dcfc4336, valkey/valkey:9.0.6-trixie@sha256:63346cb24a61221e76bdf41acce99b3968a9fa83d8122144deab45394b27b4f2)
|
||||
|
||||
health: api=healthy, postgres=healthy, valkey=healthy
|
||||
=> PASS: api, postgres and valkey are healthy
|
||||
|
||||
$ docker compose exec -T api id
|
||||
uid=10001(app) gid=10001(app) groups=10001(app)
|
||||
=> PASS: API process does not run as root
|
||||
|
||||
$ docker compose exec -T valkey sh -c valkey-cli --no-auth-warning -a "$(sed -n "s/^requirepass //p" /run/secrets/valkey_auth)" CONFIG GET appendonly
|
||||
appendonly
|
||||
yes
|
||||
$ docker compose exec -T valkey sh -c valkey-cli --no-auth-warning -a "$(sed -n "s/^requirepass //p" /run/secrets/valkey_auth)" CONFIG GET appendfsync
|
||||
appendfsync
|
||||
always
|
||||
$ docker compose exec -T valkey sh -c valkey-cli --no-auth-warning -a "$(sed -n "s/^requirepass //p" /run/secrets/valkey_auth)" CONFIG GET maxmemory-policy
|
||||
maxmemory-policy
|
||||
noeviction
|
||||
=> PASS: Valkey persistence is AOF with fsync always, no eviction
|
||||
|
||||
== Migrations: schema, constraints, idempotent re-run ==
|
||||
$ docker compose exec -T postgres psql -U messenger -d messenger -v ON_ERROR_STOP=1 -At -c SELECT format('%s %s %s', conrelid::regclass, contype, conname) FROM pg_constraint WHERE conrelid::regclass::text IN ('users', 'chats', 'chat_members', 'messages') ORDER BY 1
|
||||
chat_members f chat_members_chat_id_fkey
|
||||
chat_members f chat_members_user_id_fkey
|
||||
chat_members n chat_members_chat_id_not_null
|
||||
chat_members n chat_members_position_not_null
|
||||
chat_members n chat_members_user_id_not_null
|
||||
chat_members p chat_members_pkey
|
||||
chat_members u chat_members_position_key
|
||||
chats c chats_title_length
|
||||
chats n chats_created_at_not_null
|
||||
chats n chats_id_not_null
|
||||
chats n chats_title_not_null
|
||||
chats p chats_pkey
|
||||
messages c messages_text_length
|
||||
messages f messages_sender_is_member
|
||||
messages n messages_chat_id_not_null
|
||||
messages n messages_created_at_not_null
|
||||
messages n messages_id_not_null
|
||||
messages n messages_sender_id_not_null
|
||||
messages n messages_text_not_null
|
||||
messages p messages_pkey
|
||||
users c users_display_name_length
|
||||
users c users_username_format
|
||||
users n users_created_at_not_null
|
||||
users n users_display_name_not_null
|
||||
users n users_id_not_null
|
||||
users n users_password_hash_not_null
|
||||
users n users_username_not_null
|
||||
users p users_pkey
|
||||
users u users_username_key
|
||||
$ docker compose exec -T postgres psql -U messenger -d messenger -v ON_ERROR_STOP=1 -At -c SELECT indexrelid::regclass FROM pg_index WHERE indrelid::regclass::text IN ('chat_members', 'messages') ORDER BY 1
|
||||
chat_members_pkey
|
||||
chat_members_position_key
|
||||
chat_members_user_id_idx
|
||||
messages_pkey
|
||||
messages_chat_history_idx
|
||||
$ docker compose exec -T postgres psql -U messenger -d messenger -v ON_ERROR_STOP=1 -At -c SELECT (SELECT count(*) FROM schema_migrations) || ' ' || (SELECT count(*) FROM users)
|
||||
2 62
|
||||
$ docker compose run --rm --no-deps migrate
|
||||
Container mtusi-lab2-migrate-run-a18e9389d033 Creating
|
||||
Container mtusi-lab2-migrate-run-a18e9389d033 Created
|
||||
INFO event=migrations_done applied_now=0 total=2
|
||||
$ docker compose exec -T postgres psql -U messenger -d messenger -v ON_ERROR_STOP=1 -At -c SELECT (SELECT count(*) FROM schema_migrations) || ' ' || (SELECT count(*) FROM users)
|
||||
2 62
|
||||
schema_migrations and users rows before/after: 2 62 / 2 62
|
||||
=> PASS: re-running migrations applies nothing and duplicates nothing
|
||||
|
||||
== Rollback: chat creation fails on the second INSERT ==
|
||||
POST /api/v1/chats with one unknown member -> 400 invalid_request in 0.01 s
|
||||
$ docker compose exec -T postgres psql -U messenger -d messenger -v ON_ERROR_STOP=1 -At -c SELECT count(*) FROM chats WHERE title = 'rollback-probe-2654a5871a'
|
||||
0
|
||||
$ docker compose exec -T postgres psql -U messenger -d messenger -v ON_ERROR_STOP=1 -At -c SELECT count(*) FROM chats c WHERE NOT EXISTS (SELECT 1 FROM chat_members m WHERE m.chat_id = c.id)
|
||||
0
|
||||
$ docker compose logs --no-log-prefix --tail 20 api
|
||||
rows in chats with this title: 0; chats without members in the whole table: 0; rollback logged: True
|
||||
=> PASS: composite chat creation is rolled back as a whole
|
||||
|
||||
== Registration race: 20 parallel requests with one username ==
|
||||
statuses: 201 409 409 409 409 409 409 409 409 409 409 409 409 409 409 409 409 409 409 409
|
||||
$ docker compose exec -T postgres psql -U messenger -d messenger -v ON_ERROR_STOP=1 -At -c SELECT count(*) FROM users WHERE username = 'race_cf2989174348'
|
||||
1
|
||||
rows in users with this username: 1
|
||||
=> PASS: exactly one registration wins, the other 19 get 409
|
||||
|
||||
== Connection pool: 60 parallel clients, at most DB_POOL_SIZE=10 connections to PostgreSQL ==
|
||||
requests: 1019, statuses: {200: 1019}
|
||||
API connections in pg_stat_activity, 11 samples: max 10
|
||||
=> PASS: the pool never opens more than 10 connections and all requests succeed
|
||||
|
||||
== Timeouts: chat row locked for 6 s by another session, API answers 503 after lock_timeout=3s ==
|
||||
$ docker compose exec -T postgres psql -U messenger -d messenger -v ON_ERROR_STOP=1 -At -c "BEGIN; SELECT 1 FROM chats WHERE id = '39c32dc4-57b4-4e7d-bd72-0a66c79c4681' FOR UPDATE; SELECT pg_sleep(6); ROLLBACK;" (in the background)
|
||||
POST message while the chat row is locked -> 503 unavailable in 3.01 s
|
||||
POST message after the lock is released -> 201 in 0.01 s
|
||||
=> PASS: waiting for a lock is bounded: 503 after about 3 s, then writes work again (3.01 s)
|
||||
|
||||
== Logs: useful fields, no message texts, passwords or tokens ==
|
||||
$ docker compose logs --no-log-prefix api
|
||||
$ docker compose logs --no-log-prefix --tail 3 api
|
||||
2026-10-07T16:03:31.696Z INFO request_id=0bfad325574345cb84467a9bf688cd43 method=POST path=/api/v1/auth/sessions status=201 duration_ms=24.2 user_id=628d50fe-65dd-4d80-81ef-e1bc4b771b78
|
||||
2026-10-07T16:03:31.723Z INFO request_id=4e28cb5a57d14d9fb0b0839c99a4c452 method=POST path=/api/v1/chats status=201 duration_ms=4.3 user_id=628d50fe-65dd-4d80-81ef-e1bc4b771b78
|
||||
2026-10-07T16:03:31.754Z INFO request_id=52dc2bcc6bc248c5b5a581e8f405b0bf method=POST path=/api/v1/chats/87f1c2f5-e97b-4bfd-b4b7-26bb0a60d62c/messages status=201 duration_ms=5.3 user_id=628d50fe-65dd-4d80-81ef-e1bc4b771b78
|
||||
searched 1834 log lines; found: nothing
|
||||
=> PASS: logs contain request_id/status/duration and no secrets
|
||||
|
||||
== Session expiry: second API instance with SESSION_TTL_SECONDS=2 ==
|
||||
$ docker compose --profile expiry up -d api-short-ttl
|
||||
Container mtusi-lab2-postgres-1 Running
|
||||
Container mtusi-lab2-valkey-1 Running
|
||||
Container mtusi-lab2-api-short-ttl-1 Creating
|
||||
Container mtusi-lab2-api-short-ttl-1 Created
|
||||
Container mtusi-lab2-postgres-1 Waiting
|
||||
Container mtusi-lab2-postgres-1 Healthy
|
||||
Container mtusi-lab2-migrate-1 Starting
|
||||
Container mtusi-lab2-migrate-1 Started
|
||||
Container mtusi-lab2-migrate-1 Waiting
|
||||
Container mtusi-lab2-postgres-1 Waiting
|
||||
Container mtusi-lab2-valkey-1 Waiting
|
||||
Container mtusi-lab2-postgres-1 Healthy
|
||||
Container mtusi-lab2-valkey-1 Healthy
|
||||
Container mtusi-lab2-migrate-1 Exited
|
||||
Container mtusi-lab2-api-short-ttl-1 Starting
|
||||
Container mtusi-lab2-api-short-ttl-1 Started
|
||||
GET /users/me right after login -> 200 in 0.02 s
|
||||
$ docker compose exec -T valkey sh -c valkey-cli --no-auth-warning -a "$(sed -n "s/^requirepass //p" /run/secrets/valkey_auth)" PTTL session:abfd1b21ab7e148541270050cbe792d4a26a2b73815f9487c01330eb5080b958
|
||||
1767
|
||||
PTTL of the session key in Valkey (ms): 1767
|
||||
GET /users/me 3 s later -> 401 unauthorized in 0.02 s
|
||||
$ docker compose exec -T valkey sh -c valkey-cli --no-auth-warning -a "$(sed -n "s/^requirepass //p" /run/secrets/valkey_auth)" EXISTS session:abfd1b21ab7e148541270050cbe792d4a26a2b73815f9487c01330eb5080b958
|
||||
0
|
||||
session key still exists in Valkey: 0
|
||||
=> PASS: session expires after SESSION_TTL_SECONDS and the key disappears from Valkey
|
||||
|
||||
$ docker compose --profile expiry rm -sf api-short-ttl
|
||||
== Valkey is down: bounded 503, no anonymous access, recovery with the same token ==
|
||||
$ docker compose stop valkey
|
||||
Container mtusi-lab2-valkey-1 Stopping
|
||||
Container mtusi-lab2-valkey-1 Stopped
|
||||
GET /users/me with a valid token -> 503 unavailable in 1.02 s
|
||||
POST /auth/sessions with valid credentials -> 503 unavailable in 1.03 s
|
||||
GET /users/me without a token -> 401 unauthorized in 0.00 s
|
||||
POST /users (needs only PostgreSQL) -> 201 in 0.04 s
|
||||
GET /health/ready -> 503 unavailable in 1.03 s
|
||||
GET /health/live -> 200 in 0.00 s
|
||||
slowest of 3 more authenticated requests: 1.04 s
|
||||
$ docker compose start valkey
|
||||
Container mtusi-lab2-valkey-1 Starting
|
||||
Container mtusi-lab2-valkey-1 Started
|
||||
same token after Valkey is back: 200
|
||||
=> PASS: without Valkey: 503 for sessions, 401 for anonymous, registration still works
|
||||
|
||||
=> PASS: after Valkey restarts the session is still valid (AOF)
|
||||
|
||||
== PostgreSQL is down: bounded 503, recovery without restarting the API ==
|
||||
$ docker compose stop postgres
|
||||
Container mtusi-lab2-postgres-1 Stopping
|
||||
Container mtusi-lab2-postgres-1 Stopped
|
||||
GET /users/me with a valid token -> 503 unavailable in 3.01 s
|
||||
POST /chats -> 503 unavailable in 3.01 s
|
||||
GET /users/me without a token -> 401 unauthorized in 0.02 s
|
||||
GET /health/ready -> 503 unavailable in 3.00 s
|
||||
slowest of 3 more requests: 3.02 s
|
||||
$ docker compose start postgres
|
||||
Container mtusi-lab2-postgres-1 Starting
|
||||
Container mtusi-lab2-postgres-1 Started
|
||||
same token after PostgreSQL is back: 200
|
||||
=> PASS: without PostgreSQL: bounded 503, anonymous still 401
|
||||
|
||||
=> PASS: API recovers by itself when PostgreSQL is back
|
||||
|
||||
== Full restart: public tests/resilience.py with the restart hook ==
|
||||
$ docker volume inspect -f {{.Name}} {{.CreatedAt}} mtusi-lab2_pgdata mtusi-lab2_valkeydata
|
||||
mtusi-lab2_pgdata 2026-10-07T16:01:52Z
|
||||
mtusi-lab2_valkeydata 2026-10-07T16:01:52Z
|
||||
$ C:\Users\Dima Chernikov\AppData\Local\Programs\Python\Python314\python.exe tests/resilience.py --mode persistence --action-script scripts/restart.cmd --base-url http://127.0.0.1:8080
|
||||
Executing explicit persistence hook; hook output suppressed to avoid leaking secrets.␍
|
||||
PASS: persistence preserved confirmed data and existing session. Hook scope needs operator evidence.
|
||||
$ docker volume inspect -f {{.Name}} {{.CreatedAt}} mtusi-lab2_pgdata mtusi-lab2_valkeydata
|
||||
mtusi-lab2_pgdata 2026-10-07T16:01:52Z
|
||||
mtusi-lab2_valkeydata 2026-10-07T16:01:52Z
|
||||
api StartedAt 2026-10-07T16:02:15.275449332Z -> 2026-10-07T16:04:16.634415601Z
|
||||
postgres StartedAt 2026-10-07T16:04:06.046215674Z -> 2026-10-07T16:04:10.037996963Z
|
||||
valkey StartedAt 2026-10-07T16:03:46.643248943Z -> 2026-10-07T16:04:10.103443189Z
|
||||
=> PASS: resilience test passes after restarting api, postgres and valkey with the same volumes (hook scripts/restart.cmd, 10.5 s)
|
||||
|
||||
== SIGTERM: docker compose stop api ==
|
||||
$ docker compose stop -t 10 api
|
||||
Container mtusi-lab2-api-1 Stopping
|
||||
Container mtusi-lab2-api-1 Stopped
|
||||
$ docker compose logs --no-log-prefix --tail 2 api
|
||||
2026-10-07T16:04:18.852Z INFO event=shutdown_started signal=SIGTERM
|
||||
2026-10-07T16:04:19.121Z INFO event=shutdown_finished clean=True duration_ms=269
|
||||
$ docker compose start api
|
||||
Container mtusi-lab2-postgres-1 Waiting
|
||||
Container mtusi-lab2-postgres-1 Healthy
|
||||
Container mtusi-lab2-migrate-1 Starting
|
||||
Container mtusi-lab2-migrate-1 Started
|
||||
Container mtusi-lab2-migrate-1 Waiting
|
||||
Container mtusi-lab2-postgres-1 Waiting
|
||||
Container mtusi-lab2-valkey-1 Waiting
|
||||
Container mtusi-lab2-postgres-1 Healthy
|
||||
Container mtusi-lab2-valkey-1 Healthy
|
||||
Container mtusi-lab2-migrate-1 Exited
|
||||
Container mtusi-lab2-api-1 Starting
|
||||
Container mtusi-lab2-api-1 Started
|
||||
stop took 0.80 s, exit code 0, new request refused: True, ready again: True
|
||||
=> PASS: SIGTERM stops the API cleanly within 10 s
|
||||
|
||||
== Summary ==
|
||||
PASS third-party images are pinned by tag and digest
|
||||
PASS api, postgres and valkey are healthy
|
||||
PASS API process does not run as root
|
||||
PASS Valkey persistence is AOF with fsync always, no eviction
|
||||
PASS re-running migrations applies nothing and duplicates nothing
|
||||
PASS composite chat creation is rolled back as a whole
|
||||
PASS exactly one registration wins, the other 19 get 409
|
||||
PASS the pool never opens more than 10 connections and all requests succeed
|
||||
PASS waiting for a lock is bounded: 503 after about 3 s, then writes work again
|
||||
PASS logs contain request_id/status/duration and no secrets
|
||||
PASS session expires after SESSION_TTL_SECONDS and the key disappears from Valkey
|
||||
PASS without Valkey: 503 for sessions, 401 for anonymous, registration still works
|
||||
PASS after Valkey restarts the session is still valid (AOF)
|
||||
PASS without PostgreSQL: bounded 503, anonymous still 401
|
||||
PASS API recovers by itself when PostgreSQL is back
|
||||
PASS resilience test passes after restarting api, postgres and valkey with the same volumes
|
||||
PASS SIGTERM stops the API cleanly within 10 s
|
||||
RESULT: PASS
|
||||
@@ -0,0 +1,19 @@
|
||||
{
|
||||
"base_url": "http://127.0.0.1:8080",
|
||||
"clients": 10,
|
||||
"messages_per_client": 10,
|
||||
"sent": 100,
|
||||
"accepted_201": 100,
|
||||
"history_total": 100,
|
||||
"history_unique_ids": 100,
|
||||
"history_unique_texts": 100,
|
||||
"history_matches_accepted": true,
|
||||
"senders_in_history": 10,
|
||||
"min_messages_per_sender": 10,
|
||||
"max_messages_per_sender": 10,
|
||||
"ordered_by_created_at_and_id": true,
|
||||
"pages_read": 15,
|
||||
"page_size": 7,
|
||||
"send_elapsed_seconds": 0.407,
|
||||
"ok": true
|
||||
}
|
||||
@@ -0,0 +1,119 @@
|
||||
# Tests against the compose stand (docker compose up -d --build --wait).
|
||||
# Date: 2026-10-07 16:03 UTC
|
||||
# Host: Windows 11, Docker Desktop engine 29.8.1, Compose 5.5.1, Python 3.14.7 for the test clients;
|
||||
# Intel Core Ultra 9 185H (16 cores, 22 threads), 31 GiB RAM, no CPU/RAM limits on containers.
|
||||
# Code: working tree of the solution branch (commit is given in REPORT.md).
|
||||
|
||||
$ python scripts/check_contract.py
|
||||
PASS: offline skeleton structure, local refs and Python syntax; application NOT tested.
|
||||
[exit code 0]
|
||||
|
||||
$ python tests/smoke.py --base-url http://127.0.0.1:8080
|
||||
PASS: lab2 public API scenario; infrastructure and grading evidence remain separate.
|
||||
[exit code 0]
|
||||
|
||||
$ BASE_URL=http://127.0.0.1:8080 python -m unittest discover -s student-tests -v
|
||||
test_authentication_is_checked_before_body_and_resource (test_access.AuthenticationOrderTest.test_authentication_is_checked_before_body_and_resource) ... ok
|
||||
test_registration_does_not_need_a_token (test_access.AuthenticationOrderTest.test_registration_does_not_need_a_token) ... ok
|
||||
test_chat_list_contains_only_own_chats (test_access.ForeignChatTest.test_chat_list_contains_only_own_chats) ... ok
|
||||
test_every_member_can_read_and_write (test_access.ForeignChatTest.test_every_member_can_read_and_write) ... ok
|
||||
test_foreign_chat_looks_like_missing_chat (test_access.ForeignChatTest.test_foreign_chat_looks_like_missing_chat) ... ok
|
||||
test_invalid_chat_id_is_400 (test_access.ForeignChatTest.test_invalid_chat_id_is_400) ... ok
|
||||
test_outsider_cannot_get_chat (test_access.ForeignChatTest.test_outsider_cannot_get_chat) ... ok
|
||||
test_outsider_cannot_read_history (test_access.ForeignChatTest.test_outsider_cannot_read_history) ... ok
|
||||
test_outsider_cannot_write (test_access.ForeignChatTest.test_outsider_cannot_write) ... ok
|
||||
test_unknown_route_is_404 (test_access.ForeignChatTest.test_unknown_route_is_404) ... ok
|
||||
test_x_user_id_does_not_open_a_foreign_chat (test_access.ForeignChatTest.test_x_user_id_does_not_open_a_foreign_chat) ... ok
|
||||
test_100_messages_from_10_clients (test_concurrency.ConcurrencyTest.test_100_messages_from_10_clients) ... ok
|
||||
test_chats_created_in_parallel_are_all_listed (test_concurrency.ConcurrencyTest.test_chats_created_in_parallel_are_all_listed) ... ok
|
||||
test_same_username_registered_in_parallel_wins_once (test_concurrency.ConcurrencyTest.test_same_username_registered_in_parallel_wins_once) ... ok
|
||||
test_chats_are_paged_in_creation_order (test_pagination.ChatListPaginationTest.test_chats_are_paged_in_creation_order) ... ok
|
||||
test_cursor_of_another_user_is_400 (test_pagination.ChatListPaginationTest.test_cursor_of_another_user_is_400) ... ok
|
||||
test_default_limit_is_50 (test_pagination.DefaultLimitTest.test_default_limit_is_50) ... ok
|
||||
test_empty_history (test_pagination.GrowingHistoryTest.test_empty_history) ... ok
|
||||
test_messages_sent_during_walk_are_not_lost (test_pagination.GrowingHistoryTest.test_messages_sent_during_walk_are_not_lost) ... ok
|
||||
test_cursor_from_another_chat_is_400 (test_pagination.MessagePaginationTest.test_cursor_from_another_chat_is_400) ... ok
|
||||
test_default_and_maximum_limit (test_pagination.MessagePaginationTest.test_default_and_maximum_limit) ... ok
|
||||
test_invalid_cursor_is_400 (test_pagination.MessagePaginationTest.test_invalid_cursor_is_400) ... ok
|
||||
test_invalid_limit_is_400 (test_pagination.MessagePaginationTest.test_invalid_limit_is_400) ... ok
|
||||
test_last_full_page_has_no_cursor (test_pagination.MessagePaginationTest.test_last_full_page_has_no_cursor) ... ok
|
||||
test_limit_can_change_between_pages (test_pagination.MessagePaginationTest.test_limit_can_change_between_pages) ... ok
|
||||
test_one_message_per_page (test_pagination.MessagePaginationTest.test_one_message_per_page) ... ok
|
||||
test_walk_over_several_pages (test_pagination.MessagePaginationTest.test_walk_over_several_pages) ... ok
|
||||
test_basic_is_rejected_on_business_endpoints (test_sessions.BearerTest.test_basic_is_rejected_on_business_endpoints) ... ok
|
||||
test_protected_endpoints_need_a_token (test_sessions.BearerTest.test_protected_endpoints_need_a_token) ... ok
|
||||
test_scheme_name_is_case_insensitive (test_sessions.BearerTest.test_scheme_name_is_case_insensitive) ... ok
|
||||
test_token_selects_the_user (test_sessions.BearerTest.test_token_selects_the_user) ... ok
|
||||
test_unknown_or_malformed_token (test_sessions.BearerTest.test_unknown_or_malformed_token) ... ok
|
||||
test_x_user_id_is_ignored (test_sessions.BearerTest.test_x_user_id_is_ignored) ... ok
|
||||
test_credentials_in_json_body_are_not_accepted (test_sessions.LoginTest.test_credentials_in_json_body_are_not_accepted) ... ok
|
||||
test_each_login_gives_a_new_token (test_sessions.LoginTest.test_each_login_gives_a_new_token) ... ok
|
||||
test_malformed_basic_header (test_sessions.LoginTest.test_malformed_basic_header) ... ok
|
||||
test_password_with_colons_and_unicode (test_sessions.LoginTest.test_password_with_colons_and_unicode) ... ok
|
||||
test_registration_never_returns_password (test_sessions.LoginTest.test_registration_never_returns_password) ... ok
|
||||
test_successful_login (test_sessions.LoginTest.test_successful_login) ... ok
|
||||
test_unknown_user_gets_the_same_answer (test_sessions.LoginTest.test_unknown_user_gets_the_same_answer) ... ok
|
||||
test_wrong_password (test_sessions.LoginTest.test_wrong_password) ... ok
|
||||
test_login_again_after_logout (test_sessions.LogoutTest.test_login_again_after_logout) ... ok
|
||||
test_logout_revokes_only_the_current_token (test_sessions.LogoutTest.test_logout_revokes_only_the_current_token) ... ok
|
||||
test_bad_member_ids (test_validation.ChatValidationTest.test_bad_member_ids) ... ok
|
||||
test_bad_titles (test_validation.ChatValidationTest.test_bad_titles) ... ok
|
||||
test_caller_must_be_a_member (test_validation.ChatValidationTest.test_caller_must_be_a_member) ... ok
|
||||
test_duplicate_members (test_validation.ChatValidationTest.test_duplicate_members) ... ok
|
||||
test_unknown_member_rolls_back_the_whole_chat (test_validation.ChatValidationTest.test_unknown_member_rolls_back_the_whole_chat) ... ok
|
||||
test_valid_chat_keeps_member_order (test_validation.ChatValidationTest.test_valid_chat_keeps_member_order) ... ok
|
||||
test_extra_fields (test_validation.InvalidJsonTest.test_extra_fields) ... ok
|
||||
test_invalid_utf8 (test_validation.InvalidJsonTest.test_invalid_utf8) ... ok
|
||||
test_json_that_is_not_an_object (test_validation.InvalidJsonTest.test_json_that_is_not_an_object) ... ok
|
||||
test_lone_surrogate_is_rejected_not_crashed (test_validation.InvalidJsonTest.test_lone_surrogate_is_rejected_not_crashed) ... ok
|
||||
test_malformed_json_on_message (test_validation.InvalidJsonTest.test_malformed_json_on_message) ... ok
|
||||
test_malformed_json_on_register (test_validation.InvalidJsonTest.test_malformed_json_on_register) ... ok
|
||||
test_nul_character_is_rejected_not_crashed (test_validation.InvalidJsonTest.test_nul_character_is_rejected_not_crashed) ... ok
|
||||
test_wrong_content_type (test_validation.InvalidJsonTest.test_wrong_content_type) ... ok
|
||||
test_wrong_types (test_validation.InvalidJsonTest.test_wrong_types) ... ok
|
||||
test_empty_message_is_400 (test_validation.MessageValidationTest.test_empty_message_is_400) ... ok
|
||||
test_missing_or_wrong_text (test_validation.MessageValidationTest.test_missing_or_wrong_text) ... ok
|
||||
test_nul_in_text_is_400 (test_validation.MessageValidationTest.test_nul_in_text_is_400) ... ok
|
||||
test_oversized_body_is_413 (test_validation.MessageValidationTest.test_oversized_body_is_413) ... ok
|
||||
test_sender_id_in_body_is_rejected (test_validation.MessageValidationTest.test_sender_id_in_body_is_rejected) ... ok
|
||||
test_text_length_limit (test_validation.MessageValidationTest.test_text_length_limit) ... ok
|
||||
test_unicode_text_is_kept (test_validation.MessageValidationTest.test_unicode_text_is_kept) ... ok
|
||||
test_bad_display_names (test_validation.UserValidationTest.test_bad_display_names) ... ok
|
||||
test_bad_passwords (test_validation.UserValidationTest.test_bad_passwords) ... ok
|
||||
test_bad_usernames (test_validation.UserValidationTest.test_bad_usernames) ... ok
|
||||
test_duplicate_username_is_409 (test_validation.UserValidationTest.test_duplicate_username_is_409) ... ok
|
||||
test_missing_fields (test_validation.UserValidationTest.test_missing_fields) ... ok
|
||||
test_password_length_limits_are_accepted (test_validation.UserValidationTest.test_password_length_limits_are_accepted) ... ok
|
||||
test_unicode_display_name_is_kept (test_validation.UserValidationTest.test_unicode_display_name_is_kept) ... ok
|
||||
test_username_length_limits_are_accepted (test_validation.UserValidationTest.test_username_length_limits_are_accepted) ... ok
|
||||
|
||||
----------------------------------------------------------------------
|
||||
Ran 73 tests in 8.689s
|
||||
|
||||
OK
|
||||
[exit code 0]
|
||||
|
||||
$ BASE_URL=http://127.0.0.1:8080 python student-tests/race.py --output evidence/race.json
|
||||
{
|
||||
"base_url": "http://127.0.0.1:8080",
|
||||
"clients": 10,
|
||||
"messages_per_client": 10,
|
||||
"sent": 100,
|
||||
"accepted_201": 100,
|
||||
"history_total": 100,
|
||||
"history_unique_ids": 100,
|
||||
"history_unique_texts": 100,
|
||||
"history_matches_accepted": true,
|
||||
"senders_in_history": 10,
|
||||
"min_messages_per_sender": 10,
|
||||
"max_messages_per_sender": 10,
|
||||
"ordered_by_created_at_and_id": true,
|
||||
"pages_read": 15,
|
||||
"page_size": 7,
|
||||
"send_elapsed_seconds": 0.407,
|
||||
"ok": true
|
||||
}
|
||||
PASS
|
||||
[exit code 0]
|
||||
|
||||
# exit codes: [0, 0, 0, 0]
|
||||
@@ -0,0 +1,14 @@
|
||||
include /run/secrets/valkey_auth
|
||||
|
||||
bind 0.0.0.0
|
||||
port 6379
|
||||
protected-mode yes
|
||||
dir /data
|
||||
|
||||
appendonly yes
|
||||
appendfsync always
|
||||
|
||||
save ""
|
||||
|
||||
maxmemory 128mb
|
||||
maxmemory-policy noeviction
|
||||
@@ -0,0 +1,46 @@
|
||||
-- Схема мессенджера: пользователи, чаты, участники, сообщения.
|
||||
-- Ограничения повторяют правила контракта, чтобы база не приняла неверные данные,
|
||||
-- даже если ошибка будет в коде приложения.
|
||||
|
||||
CREATE TABLE users (
|
||||
id uuid PRIMARY KEY,
|
||||
username text NOT NULL,
|
||||
display_name text NOT NULL,
|
||||
password_hash text NOT NULL, -- Argon2id, сам пароль не хранится
|
||||
created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
|
||||
CONSTRAINT users_username_key UNIQUE (username),
|
||||
CONSTRAINT users_username_format CHECK (username ~ '^[a-z0-9_]{3,32}$'),
|
||||
CONSTRAINT users_display_name_length CHECK (char_length(display_name) BETWEEN 1 AND 100)
|
||||
);
|
||||
|
||||
CREATE TABLE chats (
|
||||
id uuid PRIMARY KEY,
|
||||
title text NOT NULL,
|
||||
created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
|
||||
CONSTRAINT chats_title_length CHECK (char_length(title) BETWEEN 1 AND 100)
|
||||
);
|
||||
|
||||
-- Участники задаются при создании чата и потом не меняются.
|
||||
-- position хранит порядок member_ids из запроса на создание.
|
||||
CREATE TABLE chat_members (
|
||||
chat_id uuid NOT NULL REFERENCES chats (id),
|
||||
user_id uuid NOT NULL REFERENCES users (id),
|
||||
position smallint NOT NULL,
|
||||
PRIMARY KEY (chat_id, user_id),
|
||||
CONSTRAINT chat_members_position_key UNIQUE (chat_id, position)
|
||||
);
|
||||
|
||||
-- Список чатов пользователя ищется по user_id.
|
||||
CREATE INDEX chat_members_user_id_idx ON chat_members (user_id);
|
||||
|
||||
CREATE TABLE messages (
|
||||
id uuid PRIMARY KEY,
|
||||
chat_id uuid NOT NULL,
|
||||
sender_id uuid NOT NULL,
|
||||
text text NOT NULL,
|
||||
created_at timestamptz NOT NULL,
|
||||
CONSTRAINT messages_text_length CHECK (char_length(text) BETWEEN 1 AND 4000),
|
||||
-- Отправитель обязан состоять в чате: это проверяет и база, а не только код.
|
||||
CONSTRAINT messages_sender_is_member FOREIGN KEY (chat_id, sender_id)
|
||||
REFERENCES chat_members (chat_id, user_id)
|
||||
);
|
||||
@@ -0,0 +1,8 @@
|
||||
-- Индекс для чтения истории чата. Приложение выполняет запрос
|
||||
-- SELECT ... FROM messages
|
||||
-- WHERE chat_id = $1 AND (created_at, id) > ($2, $3)
|
||||
-- ORDER BY created_at, id LIMIT $4
|
||||
-- Колонки индекса идут в том же порядке, поэтому страница читается диапазоном индекса
|
||||
-- без сортировки и без просмотра всей истории чата. План на 10 000+ сообщениях —
|
||||
-- в evidence/explain.txt (scripts/explain_history.py).
|
||||
CREATE INDEX messages_chat_history_idx ON messages (chat_id, created_at, id);
|
||||
@@ -0,0 +1,4 @@
|
||||
psycopg[binary]==3.3.6
|
||||
psycopg-pool==3.3.3
|
||||
valkey==6.1.1
|
||||
argon2-cffi==25.1.0
|
||||
@@ -0,0 +1,228 @@
|
||||
argon2-cffi==25.1.0 \
|
||||
--hash=sha256:694ae5cc8a42f4c4e2bf2ca0e64e51e23a040c6a517a85074683d3959e1346c1 \
|
||||
--hash=sha256:fdc8b074db390fccb6eb4a3604ae7231f219aa669a2652e0f20e16ba513d5741
|
||||
argon2-cffi-bindings==26.1.0 \
|
||||
--hash=sha256:061a6919145bbf282ebf1f9c59d3135d4833c25313c8595c0d68cf7712ddfce2 \
|
||||
--hash=sha256:0cc40f7b4050bb93eb67de95d2d759322fc7ce4930b9d645581ecf4913ec651e \
|
||||
--hash=sha256:151dfaad9de753f4af2a7854e707e4784f2acc434340ade64239c5b104b2d605 \
|
||||
--hash=sha256:19423e5d7ac1cc354baab59eaabf18db2ec04ef6593b5abe5a34f323c4a8f87a \
|
||||
--hash=sha256:19b562b1de4b9052ef1214a2821c44b6e6f22945daa102c32ae4eff929d8b6d8 \
|
||||
--hash=sha256:1a0a29ed86960e44eaace7e081bdfab4f08b012fd96ec8edba71e2ad020939e4 \
|
||||
--hash=sha256:1af817e84578ef8b7295ad17de0f9896e4c8520dbf2233c7aa5aa3d487256fc4 \
|
||||
--hash=sha256:1b0bcac4d490a237e18cf91f57352920c29f77f2fa39efd0813fb81298bf17ba \
|
||||
--hash=sha256:1d98e33bd8bd67d7206c124e200bf2229c4cfa8c9c19f7b44a897f0fc71837eb \
|
||||
--hash=sha256:21ca0396fe5ec995dd54431c32698189666f9224810acfa752e50d2bd94d9df2 \
|
||||
--hash=sha256:224865cbbcb7a2bd1356741dff12b0134df726b6d44bb7b500df8e303cbd9e81 \
|
||||
--hash=sha256:242bb0cda2ae3650764fc194593d9ea45fc9e72729acd89778c7cfe184cec2a5 \
|
||||
--hash=sha256:27f1821903e2ceadcb88ec2b45ef190897b7682449c772f4d9b53e42c520cf29 \
|
||||
--hash=sha256:28524438cd3e723f25412f63d4fd516ff5bae9ae5aa56acbe2a1404398a0cf31 \
|
||||
--hash=sha256:2b741888c93147444fdfc851abd81cc207f37f7f7da42062a00deb3888e57da8 \
|
||||
--hash=sha256:2c36ff87b5dfaa477d0bd51e9d7f6abdae7c8955d2983c97419085d842154b3e \
|
||||
--hash=sha256:34b7d9c24a4165a2c61cc8ae11d44d48c9ce2830fb536cb7914e11fdd9962728 \
|
||||
--hash=sha256:49d525938467d52c923a890153c99087c9d5a937d1f6b585dbdba34ec82e397a \
|
||||
--hash=sha256:4f84cdd868978d7b7350a566c254042d44216d9e37f241f3a6d3b1dfebeede35 \
|
||||
--hash=sha256:62ff20cd130c956c7c9144d5fe35228f98b51c579b2439e988b27ef93e16c02a \
|
||||
--hash=sha256:63505c71542a44b68b1e38060450fb006404170da375feb31af153e7f9c6205d \
|
||||
--hash=sha256:6376d4b3aca039375ca8bf92f770da0ec424a1ce3a37077a8d3c557411aa56ca \
|
||||
--hash=sha256:6a4e68eed961a8de6928d1c17ff3dc2a547e0e923c17f8f1cd79fb7bc9502f98 \
|
||||
--hash=sha256:6ab674f668d5962a3a4136ae0812519b0f1586874263723a32181d60d64137e1 \
|
||||
--hash=sha256:7014ab7e6f5d8511af92544667a0346ea6dfc314ea9a7cad1dba9fdb5c9a6e33 \
|
||||
--hash=sha256:76ae29acace5d33355344612844d588e19deaaba4639d8bb01601e4b1418ef36 \
|
||||
--hash=sha256:78de2d65e0b9ea7ce9d1b1c3e87297b2d7305a02c266ee2a2d6910daddd7ee69 \
|
||||
--hash=sha256:9bacedc04b0402837586a17f0919e3dfdd95291f441f1f56bd80ec274c2840a1 \
|
||||
--hash=sha256:a86c069c91a747a2c4e5c51473590aeb48172fff9b2130d23729a42d98665ecb \
|
||||
--hash=sha256:ac82fc756a446b6ccd7139ce70efa9d8bbe541e7ad579a12dcb52764b7175c5f \
|
||||
--hash=sha256:af11ac37a7c53dc16cb7950a6190851b0870fe218b6c60c0bb7ac355234e3083 \
|
||||
--hash=sha256:b70225b5fd1e0d2ef4f7fd30d24658454535f0924dff0caca5dc08efbbbadfbb \
|
||||
--hash=sha256:c49e853a3bef9dd10329f31f702e7fa9b5c58229ff9c2ff6d069efaf09177c08 \
|
||||
--hash=sha256:ccaf0a46cbb380f1fd102a874e32aa629fd3cb0c0e94f4943fa1f6d5edc5dac6 \
|
||||
--hash=sha256:d157ddfab1e8b21f2f1dedda9c09645d98b5ed0b667b0626be600a345d426440 \
|
||||
--hash=sha256:d88e5f7e60f28ae0b0cc6b2f16c43e87cd642a196a86f85e0d8bb6fe016fc16d \
|
||||
--hash=sha256:db0fcd827ca61622a01b220aadfbece01939acf53888f2cb98cd93e9b1e2c97e \
|
||||
--hash=sha256:df612391feca41c44d20118f3b88d1b86419465cd1f5496859f715ca60ec2210 \
|
||||
--hash=sha256:f0c3103fcff20183e593459cfea6e012281c0e76ae3ed8b5565ad1b92eac3990 \
|
||||
--hash=sha256:f9c4420a7a864fe1b86ce35befc95b8e39fb852493b81cf798671ddc265de638 \
|
||||
--hash=sha256:ffff613aaa9ce6236766e2fc6dc560bb5abde7a2e2416e3db1f9ae395a2b4dd4
|
||||
cffi==2.1.1 \
|
||||
--hash=sha256:046bfc24911b37851ee1b51aab8bffe713d89c68c6a057b09484ce9fd5f69b4e \
|
||||
--hash=sha256:06c72bb76605a4b0cd0aad6930b69d4baf7dd5d806cfc409b824191099700e66 \
|
||||
--hash=sha256:0beceaabe56af686895136a2de78db54ecd8e4046b236b8fd6d6cb61389e9bf2 \
|
||||
--hash=sha256:154852545011f779917b11c78db2358d095da62a9a172b78ad0a583ee5adc0d0 \
|
||||
--hash=sha256:194cffa889098ced9976c3fc6340305e43f6303657d298da55366907c05c22d6 \
|
||||
--hash=sha256:19ee6127ee34de7d83ce3d371ebc5ed91addbdcc39f9ab15ce4eb35a4e534971 \
|
||||
--hash=sha256:1a18a57b58cfb21fc28d72e876acf10eaed67a1ed96226f92af4df681d571c4c \
|
||||
--hash=sha256:1aa5645c30469b09530c4ebca77ebf8f17618293c58f8549cb1a543a50236e7d \
|
||||
--hash=sha256:1dea0e4d7d4f11f619fe8c1d76caf49e24405b4b5743c0e3be16a500ecd930c9 \
|
||||
--hash=sha256:208f941bb9d18e768138677f0a6d2ce01f590df56043dda1df1535ac57c88517 \
|
||||
--hash=sha256:210019b6c7cf07f081b4c54635c8cf744377001350e29cc0f81c4377b4797735 \
|
||||
--hash=sha256:246fa40ce8645a614ff682e0b70f37134e460eaf93a775e0cbe3cca585a67a80 \
|
||||
--hash=sha256:25792eac27877609e7bb06d42ff88278a6624fff2ba9bbb523c09616b117e80f \
|
||||
--hash=sha256:27350daa11d4f10c540e6e89dada4c54feb7256ad03e9a4dc075ebad7ba360d1 \
|
||||
--hash=sha256:28907ab9bfb6aa13184cfc17c6b8e1023c5ab6fd7076d8c20a35e59fe04f8f29 \
|
||||
--hash=sha256:2ae64be792b8966f2c69538199728b290e34726562896df1e5dc8ffd8d8188e8 \
|
||||
--hash=sha256:31348097ff5bbe827ccc41795d4dd099d9f0625e7def00ee653c137a490c2a6c \
|
||||
--hash=sha256:3143d81e29e1e20a9ce10901ec369012947876596f75a222235965f2b7ae832e \
|
||||
--hash=sha256:3222ba5d678f80a030e6afbcc33dc1ae5cb45facabb61cee2c7016b8432fde48 \
|
||||
--hash=sha256:3311ed60d36f83378794e1009ac6258bafbf81f7888b4caa7b35a521e3f95813 \
|
||||
--hash=sha256:334644fbac4eff73d985a17a91226df55d0f394160c4cfb880e084c8f7161cac \
|
||||
--hash=sha256:34e261f78cb6ceaaa36f42f2613f4380d94d9c759a9c73c769ee6e0247364632 \
|
||||
--hash=sha256:363e05fa78e15116c3c32c210ee36884fd6b9afa6d440e47112c3bd511d64cb6 \
|
||||
--hash=sha256:398aff33cee2767e3e781d2554c54bd0dff386bb437581e0d8011fde1a942ec1 \
|
||||
--hash=sha256:3d22a20b1fb1632cc72c22f95f7b0d2961c3e1c235f245ba4c606c4771035659 \
|
||||
--hash=sha256:42a494cee34437f05546455144f2b5d9ac09b1face62bcfce597d2e521066688 \
|
||||
--hash=sha256:42e2f76b9455f5a9a844f770bf3e200ed3da0e15f5df3db9c31fe80b04b3d004 \
|
||||
--hash=sha256:42f6930c31dc7f50732c9ae793c2786c7b6b044195967bbdde40bb9be81c4cc0 \
|
||||
--hash=sha256:456a61fa52d579ebf9df2e9552ead5129855dbaff6c1e5a9b1bc408809bdc062 \
|
||||
--hash=sha256:471cee653ae88de62096552e6d24ccb4a5adb8c8c9f10b5054d0122c15bf2779 \
|
||||
--hash=sha256:49cbc70e6542d4ccccb936558d1064a8012541e78f821f955cff24e357776c94 \
|
||||
--hash=sha256:4a7c934f7360e8cd64fe9efadcbd10c7c6364f531e432b9a4bf5ccbc9e0e8b50 \
|
||||
--hash=sha256:4be96343e422f2dfcd12ab5c9f5aebe03f82f737c6bffeca6830b3875cb44aab \
|
||||
--hash=sha256:4f42141fc14250de6dde5ee7ea4432be017252d91f19c5ad043c084cea629cac \
|
||||
--hash=sha256:507a24c282e0f42f8ed737cf048572cbf580468da5555764a8331735e9c736b6 \
|
||||
--hash=sha256:51b31d1c98274844cfd7838ce00bfc27c7423a4dc00fc0772fc3331c2cc90676 \
|
||||
--hash=sha256:58acb8ab8e295e6c5ea12f888cbb13cf21511ef2a3303a23f4325c29d17fe5c1 \
|
||||
--hash=sha256:5a59cc1c4442bc3d5c703bf720b51138d0bfc173618807c9ee2490a7541dd3d9 \
|
||||
--hash=sha256:5bb4e7ea95dcd6a014a6fef62e62467d67d8e582326443f3d68e71d6320a9fcf \
|
||||
--hash=sha256:5c58fe613dc5e5336357eff555824a314d8e43282600435c8d1cb6a7a2fedd13 \
|
||||
--hash=sha256:5e7cecbaadb83884793e05828cee59b210b24583b9c7425d0ba6a754fe22eb4e \
|
||||
--hash=sha256:616f097f2fe415bc92a247f02e11f634e1f9e9a83d327e3c915c15089c87869e \
|
||||
--hash=sha256:63bbfd5ded17c4840ac07cd8f1c21ba9d9708141f840b324f422f41b207e3973 \
|
||||
--hash=sha256:64faea20f4e2613363a1a9b9c7dd73058f3ecd00133a511e72ad7c511658f527 \
|
||||
--hash=sha256:661c298b4821edebead0c91edd2b00374d67ad7c5a1f7a91d4442633b79d6a72 \
|
||||
--hash=sha256:68e62fe11f30d5ca8289242866f0a5291402d8529ca2178ab8afc5c9694ae890 \
|
||||
--hash=sha256:6a8dddef476fab96d066d578fc88526767b836ab5ab21754e1d5bf3879c31c7c \
|
||||
--hash=sha256:6e192623c49c94421616a5778fba35cf0d5a8d000650c1967ef4448ee5cdd990 \
|
||||
--hash=sha256:7225e4514edb64eb6740324353e0da0711954fd8d7da4576755b1c6e09b697cd \
|
||||
--hash=sha256:75f80557d1389eddbd0de2681f6a390a0c5338c31ddaa821381c203fc3fd50d9 \
|
||||
--hash=sha256:770de9db11e84213beec501cfcaa013b019820ca881e03344dea5844f7876d94 \
|
||||
--hash=sha256:7750c6449dff7864bb9bb27ddfb0267756189201a3afc911d82b3caacd70dfc3 \
|
||||
--hash=sha256:7bde5e4cc5c10140859842b9d383af292b22639a4dffb725314baf45968cef80 \
|
||||
--hash=sha256:7ce713ace7c0e4520535b42b77eaa742c16dab813978064913e5a3cf82973b41 \
|
||||
--hash=sha256:7da0c5eff80f0197f3b3d1232ec5a682a9325f4ae9016a78f5f5ca35f9ced1f5 \
|
||||
--hash=sha256:7dbb61fe3a7699468030f71bbe5f8a0e326a151daa91beb11a6fc1f980c55e1c \
|
||||
--hash=sha256:811bd1e21d32de12efca32393a0ab3f5133b54fce9bd44b8bd77ab07da14bf6a \
|
||||
--hash=sha256:8ef53b2de9bcb9197d31854256575d59dbac0cba72ac627bb291ef5eceb74be4 \
|
||||
--hash=sha256:937c0052c05a31ca1daf18de3158eed4dbfcb9cc107adbea227728d647be701e \
|
||||
--hash=sha256:9d2055050ea716bd38b7f7f1579c275386646b4894c155a3e2f3cd62ed41b7c6 \
|
||||
--hash=sha256:9f8d177621de5cb38ee3e731eda45d421db093ec0739f46a5594babda7987a98 \
|
||||
--hash=sha256:a2d7755bef5a12ed488f4ef1f1b69ee9191d7396083b755a5d2295f6edb4768b \
|
||||
--hash=sha256:a48d62ab9d6f4f98c983223a547af44be6ca3691074c31cecced6facd3ba2dc1 \
|
||||
--hash=sha256:a4f00aa42f75d6e4595e8866e748cc1705adc0cddfeb2ca86d0d03993d63ba03 \
|
||||
--hash=sha256:a6e721d4b0e45d5b65e87534470e67b18dcd092c83f68fba09f152b9cbc061af \
|
||||
--hash=sha256:a730a083190634c65cca36ba5f489531576ebd79bcd5c8e172130f6453127231 \
|
||||
--hash=sha256:a931079504ecc49efed7744c476a5c343a92fabf66dec2db95edb1b2fdc770e2 \
|
||||
--hash=sha256:aa9511c62d14da7aacc9b4bf51f3f697a621e83b2d6919008243c3aad168eea3 \
|
||||
--hash=sha256:ab36d55f9ed2d067327667c2fea18dda018eb628dd6347aa01dda6cf1f5d3836 \
|
||||
--hash=sha256:ad2c86c495b899d862ea0f4b42891b8713a3bd45dd4105c7fd51c2a72f39f3a5 \
|
||||
--hash=sha256:aeae0e330c9f6acd681f647d46cefd30c29f93e3392882e792e82080c9691399 \
|
||||
--hash=sha256:b0431303acaea1089ad4b3e9ce4e6518193def1118d4073ca848635ee4ea2e96 \
|
||||
--hash=sha256:b5bdfd1c873d4e093aabc0ca84c4ca6dbc4f752afb5c86f146d9742580c9da2e \
|
||||
--hash=sha256:baed1e86cc735622097354b9d1281406caf42ff42a886d29faa8e8d1630333be \
|
||||
--hash=sha256:c1453022f490d2459a11819d83ad1d586e9ff65a12ac3e705ffebd46d3685dcf \
|
||||
--hash=sha256:c26608d2222fb1e94487e4a387d85f13eb55d5ed725cb25a0c589ac4ee60e7bc \
|
||||
--hash=sha256:c7659f22557c5a0bc4855cd635f55edec690cc008a40768527762cb9fb263455 \
|
||||
--hash=sha256:c8c69575568085ba0b1b10c0249d779a214aea6f6522e949a0fc9fb0fcb449d0 \
|
||||
--hash=sha256:c8d2c9fd1f2d16f780d15127abb050d13d1a76c03a4bd87d7e4980e45e511e12 \
|
||||
--hash=sha256:ca82be1a1d406ecfe1d25dc16cb33488e5a16bf4438c9fb590484ea29d92478b \
|
||||
--hash=sha256:cc572dace3f60ef98d7b12ff411d20f5362feb31a0439eab0085bbfd349982d7 \
|
||||
--hash=sha256:d18e5ac0f2f03f4f518d3e23db0f0cad7faa1da8620e9c09461d443bbf6e6692 \
|
||||
--hash=sha256:d28630f5854ab07ab1fd4aba756de52326c82e6be15d414b12793f1975048b54 \
|
||||
--hash=sha256:d9c275eaacd24aa73f94ffd6de08fc3f932424d8b6c376f4bed7cde376fe7bc3 \
|
||||
--hash=sha256:da0e573f9f97159390c89d9f1a9e41908b66d408cc5b58d08cf3847d844c531b \
|
||||
--hash=sha256:dd31f52ea1086513bb9df30f8fcee9b8918323ae067a3d5b78bc826a000712be \
|
||||
--hash=sha256:dddad92b554513a31f272570678ba307fb9f618f05e3d4a5eacafff9eae03e1d \
|
||||
--hash=sha256:df423d40ee8654634421812bc3b196da3f9bd7d32929da813f8394c4348a5358 \
|
||||
--hash=sha256:df913725b79db7bcf03448f36b7bf8815363417d5b58deecf9305e3e30f0f21a \
|
||||
--hash=sha256:e0bcb7e0f677f543555d2adff3bf19c05f66cdb4796e5ff602442ab2fe3c4ef7 \
|
||||
--hash=sha256:e2d65b31f36619cda3999b78b2aa9632e76b78448e7a56fc4240824200e7c4fc \
|
||||
--hash=sha256:e6e8cff14d6fb0be70a09c0bdc58096f501952d04624ebf867e0e56da2df8960 \
|
||||
--hash=sha256:f16c709686a78c727bbbf059f92b0bf41c6fc60deec706d2dc19f529175a6125 \
|
||||
--hash=sha256:f24fb43132a4c6b4cb4eb029492919b2db645be6808d738f244fd146c03c32cb \
|
||||
--hash=sha256:f53e442b08449d42821fa4a4fba000095af9f62742a500f978a9f557ec44339a \
|
||||
--hash=sha256:f5cfbc5fe74540d335175b656c725d74d90e3730c626d92575eea35029d9afaa \
|
||||
--hash=sha256:f81b3b8f3d4e343550fa4baa0e479bba9f2d29ce9c2e9b51d1ce1718d7442fcf \
|
||||
--hash=sha256:f8ec5e643a9a937f64e1999eb9f75d072263751912dc5cd06d3c85f8f44be7c3 \
|
||||
--hash=sha256:fb92203a88b3d3053034db775110081c49d28be6551923805e039924093761e4 \
|
||||
--hash=sha256:fcd22650c908d7b7da162bbfaab594a1227a15d1643a98c68b122ac642fa2264
|
||||
psycopg==3.3.6 \
|
||||
--hash=sha256:a1db9f7148b06a28606767efaca51fa6f9398c5c0a3810519be69d7000bdb631 \
|
||||
--hash=sha256:c081f2250df751a943036e42db6df4571c66cd0aabe8291a7a506512b12007d2
|
||||
psycopg-binary==3.3.6 \
|
||||
--hash=sha256:05a83ac9fd52b9bca7cb5ab04b3691163170bd16f53defa27216ea3aa07ee781 \
|
||||
--hash=sha256:0a52991594ac4db888c7d39bccef331797e30cb31a95cae02cf2607f83a42dc2 \
|
||||
--hash=sha256:0bf08b749cc144f33b44a91b78e3f71c60eb07963746a0df5a100b36ce3d7475 \
|
||||
--hash=sha256:0ebfad5d131de9f892ae9e70cc7616207768b6714b66a52d4612b8ceaf78b372 \
|
||||
--hash=sha256:1679a1cb93fbe5a6d1fd58d82cbddcc6fcb8c61446ba7cae6eb2a7b19bc585de \
|
||||
--hash=sha256:198a48e68cc99ccac03ba95ac857e73aa66f3bf6be77019fafb0832a05f7ad03 \
|
||||
--hash=sha256:1fbd30e537dab22cafdf080608f10148fe2a5f3a61294ddb5113caac8a623840 \
|
||||
--hash=sha256:289aadd6a00e151203c081f708348ec89f1e483c9b510ef4ac3981f847f01f79 \
|
||||
--hash=sha256:2f122603f36050937982abf9668d8bc4769a79f7c93a65013b1c49f1cab7b56b \
|
||||
--hash=sha256:303732e798fe6729f8e12021b9c96107df8e95ecec4dd487c67b98ec2a59435e \
|
||||
--hash=sha256:31cd942c23f613276b81a6e6598cefa12960058b0f46e1e874b540c793f6aca5 \
|
||||
--hash=sha256:366db6e97e66b37211475f20c4c1324a2dc0dd825e46d4e87f9d599304d276f9 \
|
||||
--hash=sha256:373704aea331d3f3e3402c125a1543f5875e2986ebb54f97d1647942161f803f \
|
||||
--hash=sha256:37d40450659401600e6d043ff586c89a71a69f33cbb8bcdba6cdb2569beecdbe \
|
||||
--hash=sha256:37e517c146b185f9c0c6e8d0a0ebbdeeeb67896af28466e032bc810d0c7dc7a7 \
|
||||
--hash=sha256:3af90f92769d8cc10f94515ee7a0aef36ea85ca733a0ce22858f6e0953f41138 \
|
||||
--hash=sha256:3c9e663b2e800e3218994cf948c11bcc2844e6491b34aa80d089baf6531827bf \
|
||||
--hash=sha256:3f84dab25e0385692ee13274c68678377e0b1a70ab9d14e56264cbf61f60c62d \
|
||||
--hash=sha256:4690cf67738f0e0e49a32aeec99bf0e4595cc2b4f1af984a4345394b1dcff91a \
|
||||
--hash=sha256:566dd827f17728efdf7d88a5b066f815170f6fdad13967ae952842d90e6aaa9f \
|
||||
--hash=sha256:5927b7ba63153cd8e9862987290a2b783a5c590daf2a4ef981700cc3569166d4 \
|
||||
--hash=sha256:5ad8f35e67cc16d1fad1fa8c88972dc9b3a3141ea67897399904edab96a301b6 \
|
||||
--hash=sha256:5ea8beeb5541780b4b50b462eeacbc4f594ce3b911dc20c81c75f267876f71d2 \
|
||||
--hash=sha256:5f598f19fa9a91540b5cee17932ffd227b7b53a481605bcc4573c0eafa647300 \
|
||||
--hash=sha256:612382ac3ed13651c7fa44b5fee9fbf7baaa2ddbc6f500391672682c5f1df9e0 \
|
||||
--hash=sha256:6ff05561e4a067d35507dc5c90f1deb2ec1c9703ac5cccc1bc26e08a197f9c5a \
|
||||
--hash=sha256:7308c93cf0b19bbaf8e6ff0a6ad50d3c442385739245fe15a8d593bf841734a6 \
|
||||
--hash=sha256:79a2a1c3449f6c3409427078ed1cec10de79f3023cb5f2504f0597d350ad46c7 \
|
||||
--hash=sha256:7beb3e41c9a1e509f3ed85263386588cbe3e975aa67be21f79f44fd35ffaeefc \
|
||||
--hash=sha256:86147cb5d140341c3363fb5bacce31f8d5543902a46699d3c536b101bbceaf9e \
|
||||
--hash=sha256:889e42acec10450185e0cdfb396f375e2c1a8d7737c114830a7fde4654f59e30 \
|
||||
--hash=sha256:910ace140e3e7b7596898d083f37a8fe90c5c40684252ad4e682364b2cd3deba \
|
||||
--hash=sha256:955e3dd94da361e052d2e49acf591017158dc8f8ed2c8a42c2e3943403c39dc2 \
|
||||
--hash=sha256:9892188bb15e5803beb51afe8a25add6b56be391a53058e8bca03b74e1e6bf22 \
|
||||
--hash=sha256:98c02090d88f2ebc0ec1e8da538f77d225ce0fffecf372aa39262e62a1b054ef \
|
||||
--hash=sha256:9b2f11794e017ce340934e35de46181c46ef71ec75ea3d85dd75cd836761c01e \
|
||||
--hash=sha256:a2e44a342d2aee40508e28a563d8961c39d9bbd8cae36d8578f0a3c6658aab0f \
|
||||
--hash=sha256:a4ee3bdd5468a725f2a4d9aab8a74b6d0279f768c8b5d3aeb102c5307ff3d59c \
|
||||
--hash=sha256:a5165300324efd5a772c48a88ab3a928513ab3979fca76553e62ee815f7b2b9c \
|
||||
--hash=sha256:a9348c5b43a3bb5ef8c2e89d5237c9c87eeafb01d338c84a7aebbc5cd0313299 \
|
||||
--hash=sha256:aa73160077345ec21b3f51e8e24b3de2e99586217e497629326eb9b2ea88c52e \
|
||||
--hash=sha256:ad1c785e784cfd87e8436c6b7702f2d321fc39601bbaf29bc63a41a867091638 \
|
||||
--hash=sha256:b3f75dee0f9afafabe4edc52c4842f1e1878ed2069bd05b22d6fe961e97e4dba \
|
||||
--hash=sha256:b599defe9190b17e9907c8b4d114c181e702c87efcd1b8a0ad40971cdcc4634a \
|
||||
--hash=sha256:b82491019b884d62318b5f30706c3d7e6d4e5a6cb7eabcb3edc0c1b0fdaceae9 \
|
||||
--hash=sha256:b8ece331509f7a975b90501f41e83ad905e4141753fedf3f2711b2bc70a8efbc \
|
||||
--hash=sha256:b979a42815410432420275412633960807178b1ce26591a16ce06e78a5bd4bb2 \
|
||||
--hash=sha256:be4f9b3c9338ac5dd217c5847e21521b396c8117f78dc420d495a5c49bbef874 \
|
||||
--hash=sha256:bf8c8481d026b85dd70c5fa7dde85b2333aed0b32a2602bcd38a900cbd78a49c \
|
||||
--hash=sha256:c61617eaae0112ca154da87ffb99b73af2c74067acac28dfb9a4455b019dff2e \
|
||||
--hash=sha256:c6d19cb4999d03231e8730a5f66c8f5068bc3b532677eb39dab0f600bff3e312 \
|
||||
--hash=sha256:c7753871eb57e6a5f4646f6168590c6653073dea5e9e720b201c8875332df4c8 \
|
||||
--hash=sha256:c7f92daa0d2a1c76f07264abddf8cbabd30152a2f09c3270e50f0c7efdf5dcac \
|
||||
--hash=sha256:cbd5f73073ed19c378d4c35499db1e3e703a5b1a324e521204065967bfaa7a18 \
|
||||
--hash=sha256:cec5ea900390897d0b46130f60bc2883bf19c314f9044235217c8be88b0ef269 \
|
||||
--hash=sha256:d636338c8f21b0df2f84657b00bc34f9313f826ef93f1155bc743607e4a0c5eb \
|
||||
--hash=sha256:dc75da5a20951049f7b773145f998f69d181adad9c58a0ff36e0cf1d73c10e10 \
|
||||
--hash=sha256:e23a66a763fbe83fcc210bc77c27e5a5ea380ebf091c06f34d8561b695e5a40f \
|
||||
--hash=sha256:e8cbb54454dbf1bbf2ff08dd7693e8d94ac94b1a20f70f4b3b813d52ecb5cbc1 \
|
||||
--hash=sha256:ee2c4728c691245e24501fcd7a97b5b381236b9985bc445bba88cdce7d1b5784 \
|
||||
--hash=sha256:f0535693ce476a722b718b002d5d2c27d47e71ca945276ac194409c98e74c492 \
|
||||
--hash=sha256:f19cc87343eaa55255e76b31259a570072ac95d6ae82c92dd34b97691f5e49dc \
|
||||
--hash=sha256:f21d057f3e5f5491067e5b292498073b73847d48799b099803fef100775fcc52 \
|
||||
--hash=sha256:f87dbdc42e78ee0f7ea180c03f8c78e80a949e373066629bd90fefff10552dff \
|
||||
--hash=sha256:fa34eb47969297471db7b7f193622c7e3ee839ec05abd05f1fe104d5b1b1dcf4 \
|
||||
--hash=sha256:fdccb3a0e184b03e9baa673b15a809cf36c339c85dbda0ebc25a698846dfbee8
|
||||
psycopg-pool==3.3.3 \
|
||||
--hash=sha256:9b9cd6a4fcec47a410f7e82d408540e7f77b478509e91b44c1a5457a13e5ff37 \
|
||||
--hash=sha256:df87b5d9d0ad7db37f6cdad4fa8ce113d250f5997f6db38e9a99192fb67f9e1d
|
||||
pycparser==3.0 \
|
||||
--hash=sha256:600f49d217304a5902ac3c37e1281c9fe94e4d0489de643a9504c5cdfdfc6b29 \
|
||||
--hash=sha256:b727414169a36b7d524c1c3e31839a521725078d7b2ff038656844266160a992
|
||||
typing-extensions==4.16.0 \
|
||||
--hash=sha256:481caa481374e813c1b176ada14e97f1f67a4539ce9cfeb3f350d78d6370c2e8 \
|
||||
--hash=sha256:dc983d19a509c94dba722ee6abd33940f7c05a89e243c47e907eb4db6f1a43e5
|
||||
valkey==6.1.1 \
|
||||
--hash=sha256:5880792990c6c2b5eb604a5ed5f98f300880b6dd92d123819b66ed54bb259731 \
|
||||
--hash=sha256:e2691541c6e1503b53c714ad9a35551ac9b7c0bbac93865f063dbc859a46de92
|
||||
Executable
+139
@@ -0,0 +1,139 @@
|
||||
#!/usr/bin/env python3
|
||||
"""EXPLAIN (ANALYZE, BUFFERS) of the history page query on 10 000+ messages.
|
||||
|
||||
python scripts/explain_history.py --output evidence/explain.txt
|
||||
|
||||
The stand must be running. The script registers two synthetic users through the API,
|
||||
creates a target chat and a noise chat, bulk-inserts messages with SQL, runs ANALYZE and
|
||||
prints the plans of the same query the API runs (src/messenger/db.py, list_messages):
|
||||
first page, a page in the middle, the last page, the generic plan of the parameterized
|
||||
query, and for comparison the middle page without the index (DROP INDEX inside a
|
||||
transaction that is rolled back, so the database is left unchanged).
|
||||
Standard library only.
|
||||
"""
|
||||
import argparse
|
||||
import base64
|
||||
import json
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
import sys
|
||||
import time
|
||||
import urllib.request
|
||||
import uuid
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
BASE = 'http://127.0.0.1:8080'
|
||||
PSQL = ['docker', 'compose', 'exec', '-T', 'postgres', 'psql', '-U', 'messenger', '-d', 'messenger',
|
||||
'-X', '-v', 'ON_ERROR_STOP=1']
|
||||
PAGE = 51
|
||||
|
||||
lines = []
|
||||
|
||||
|
||||
def out(text=''):
|
||||
lines.append(text)
|
||||
print(text, flush=True)
|
||||
|
||||
|
||||
def psql(sql, echo=True):
|
||||
"""Run SQL in one psql session; with echo the transcript shows every statement before its result."""
|
||||
args = PSQL + (['--echo-all'] if echo else ['-At']) + ['-f', '-']
|
||||
done = subprocess.run(args, cwd=str(ROOT), input=sql.encode('utf-8'), stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
|
||||
text = done.stdout.decode('utf-8', 'replace').strip()
|
||||
if done.returncode != 0:
|
||||
out(text)
|
||||
raise SystemExit('psql failed')
|
||||
return text
|
||||
|
||||
|
||||
def api(method, path, body=None, headers=None):
|
||||
headers = dict(headers or {})
|
||||
data = None
|
||||
if body is not None:
|
||||
data = json.dumps(body).encode('utf-8')
|
||||
headers['Content-Type'] = 'application/json'
|
||||
request = urllib.request.Request(BASE + path, data=data, headers=headers, method=method)
|
||||
with urllib.request.urlopen(request, timeout=10) as response:
|
||||
return json.loads(response.read())
|
||||
|
||||
|
||||
def user(label):
|
||||
username = label + '_' + uuid.uuid4().hex[:10]
|
||||
password = 'Explain-' + uuid.uuid4().hex
|
||||
created = api('POST', '/api/v1/users', {'username': username, 'display_name': label, 'password': password})
|
||||
credential = base64.b64encode((username + ':' + password).encode()).decode()
|
||||
session = api('POST', '/api/v1/auth/sessions', headers={'Authorization': 'Basic ' + credential})
|
||||
return created['id'], session['token']
|
||||
|
||||
|
||||
def page_sql(chat_id, after=None):
|
||||
"""The same SQL text as Repository.list_messages, with literal values."""
|
||||
sql = "SELECT id, chat_id, sender_id, text, created_at FROM messages WHERE chat_id = '%s' " % chat_id
|
||||
if after:
|
||||
sql += "AND (created_at, id) > ('%s', '%s') " % after
|
||||
return sql + 'ORDER BY created_at, id LIMIT %d' % PAGE
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description='EXPLAIN of the history query; inserts synthetic messages')
|
||||
parser.add_argument('--messages', type=int, default=10000, help='messages in the target chat (>= 10000)')
|
||||
parser.add_argument('--noise', type=int, default=20000, help='messages in another chat')
|
||||
parser.add_argument('--output', default=None)
|
||||
args = parser.parse_args()
|
||||
sys.stdout.reconfigure(errors='replace')
|
||||
|
||||
out('# %s UTC, %s' % (time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime()), psql('SELECT version()', echo=False)))
|
||||
alice, alice_token = user('explain_a')
|
||||
bob, _ = user('explain_b')
|
||||
target = api('POST', '/api/v1/chats', {'title': 'EXPLAIN target', 'member_ids': [alice, bob]},
|
||||
{'Authorization': 'Bearer ' + alice_token})['id']
|
||||
noise = api('POST', '/api/v1/chats', {'title': 'EXPLAIN noise', 'member_ids': [alice]},
|
||||
{'Authorization': 'Bearer ' + alice_token})['id']
|
||||
out('# target chat %s: %d messages; noise chat %s: %d messages' % (target, args.messages, noise, args.noise))
|
||||
|
||||
started = time.monotonic()
|
||||
psql('''
|
||||
INSERT INTO messages (id, chat_id, sender_id, text, created_at)
|
||||
SELECT gen_random_uuid(), '%(target)s', CASE WHEN i %% 2 = 0 THEN '%(alice)s'::uuid ELSE '%(bob)s'::uuid END,
|
||||
'seed message ' || i, clock_timestamp() + i * interval '1 millisecond'
|
||||
FROM generate_series(1, %(messages)d) AS i;
|
||||
INSERT INTO messages (id, chat_id, sender_id, text, created_at)
|
||||
SELECT gen_random_uuid(), '%(noise)s', '%(alice)s', 'noise message ' || i, clock_timestamp() + i * interval '1 millisecond'
|
||||
FROM generate_series(1, %(noise_count)d) AS i;
|
||||
ANALYZE messages;
|
||||
''' % {'target': target, 'noise': noise, 'alice': alice, 'bob': bob, 'messages': args.messages, 'noise_count': args.noise},
|
||||
echo=False)
|
||||
out('# inserted and analyzed in %.1f s' % (time.monotonic() - started))
|
||||
out(psql("SELECT 'messages total: ' || count(*) || ', in target chat: ' || count(*) FILTER (WHERE chat_id = '%s') FROM messages" % target, echo=False))
|
||||
|
||||
def key_at(position):
|
||||
row = psql("SELECT created_at || '|' || id FROM messages WHERE chat_id = '%s' ORDER BY created_at, id OFFSET %d LIMIT 1"
|
||||
% (target, position - 1), echo=False)
|
||||
return tuple(row.split('|'))
|
||||
|
||||
middle, last = key_at(args.messages // 2), key_at(args.messages - 20)
|
||||
explain = 'EXPLAIN (ANALYZE, BUFFERS, COSTS OFF) '
|
||||
out()
|
||||
out('== 1. First page, middle page and last page: index range scan, no sort ==')
|
||||
out(psql('\n'.join(explain + page_sql(target, after) + ';' for after in (None, middle, last))))
|
||||
out()
|
||||
out('== 2. Generic plan of the parameterized query (how a prepared statement with $1..$4 runs) ==')
|
||||
out(psql('''SET plan_cache_mode = force_generic_plan;
|
||||
PREPARE history_page(uuid, timestamptz, uuid, int) AS
|
||||
SELECT id, chat_id, sender_id, text, created_at FROM messages
|
||||
WHERE chat_id = $1 AND (created_at, id) > ($2, $3) ORDER BY created_at, id LIMIT $4;
|
||||
%sEXECUTE history_page('%s', '%s', '%s', %d);''' % (explain, target, middle[0], middle[1], PAGE)))
|
||||
out()
|
||||
out('== 3. For comparison: the same middle page without the index (rolled back) ==')
|
||||
out(psql('''BEGIN;
|
||||
DROP INDEX messages_chat_history_idx;
|
||||
%s%s;
|
||||
ROLLBACK;''' % (explain, page_sql(target, middle))))
|
||||
out(psql("SELECT 'index still exists: ' || (to_regclass('messages_chat_history_idx') IS NOT NULL)", echo=False))
|
||||
if args.output:
|
||||
Path(args.output).parent.mkdir(parents=True, exist_ok=True)
|
||||
Path(args.output).write_text('\n'.join(lines) + '\n', encoding='utf-8')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
Executable
+49
@@ -0,0 +1,49 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Create local secret files for the compose stand.
|
||||
|
||||
python scripts/init_secrets.py
|
||||
|
||||
Passwords are random and synthetic; they are created only if missing and never printed.
|
||||
The URL/config files are always rebuilt from the password files, so the pair stays consistent.
|
||||
secrets/ is listed in .gitignore and .dockerignore.
|
||||
|
||||
PostgreSQL applies its password only when the data volume is created. If secrets/ is
|
||||
deleted while the pgdata volume is kept, restore the old files or recreate the volume
|
||||
(docker compose down -v deletes ALL lab data).
|
||||
"""
|
||||
from pathlib import Path
|
||||
import secrets
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
DIR = ROOT / 'secrets'
|
||||
|
||||
|
||||
def write(name, content):
|
||||
path = DIR / name
|
||||
path.write_text(content, encoding='utf-8', newline='\n')
|
||||
path.chmod(0o644)
|
||||
|
||||
|
||||
def password(name):
|
||||
path = DIR / name
|
||||
if path.is_file() and path.read_text(encoding='utf-8').strip():
|
||||
print('keep secrets/' + name)
|
||||
return path.read_text(encoding='utf-8').strip()
|
||||
value = secrets.token_hex(24)
|
||||
write(name, value + '\n')
|
||||
print('created secrets/' + name)
|
||||
return value
|
||||
|
||||
|
||||
def main():
|
||||
DIR.mkdir(exist_ok=True)
|
||||
postgres = password('postgres_password')
|
||||
valkey = password('valkey_password')
|
||||
write('database_url', 'postgresql://messenger:%s@postgres:5432/messenger\n' % postgres)
|
||||
write('valkey_auth', 'requirepass %s\n' % valkey)
|
||||
write('cache_url', 'valkey://:%s@valkey:6379/0\n' % valkey)
|
||||
print('written secrets/database_url, secrets/valkey_auth, secrets/cache_url')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
Executable
+435
@@ -0,0 +1,435 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Infrastructure checks for lab 2 that need control over the compose stand.
|
||||
|
||||
python scripts/lab2_checks.py --output evidence/lab2-checks.txt
|
||||
python scripts/lab2_checks.py --only valkey-down postgres-down
|
||||
|
||||
The stand must already be running (docker compose up -d --build). Every command is printed
|
||||
together with its output. Sections stop and start containers, but never delete volumes,
|
||||
never flush Valkey and never print secrets or session tokens.
|
||||
Standard library only; works on Windows, Linux and macOS.
|
||||
"""
|
||||
import argparse
|
||||
import base64
|
||||
import concurrent.futures
|
||||
import hashlib
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
import sys
|
||||
import threading
|
||||
import time
|
||||
import urllib.error
|
||||
import urllib.request
|
||||
import uuid
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
BASE = 'http://127.0.0.1:8080'
|
||||
SHORT_TTL_BASE = 'http://127.0.0.1:8081'
|
||||
VALKEY_CLI = 'valkey-cli --no-auth-warning -a "$(sed -n "s/^requirepass //p" /run/secrets/valkey_auth)" '
|
||||
|
||||
lines = []
|
||||
results = []
|
||||
|
||||
|
||||
class Stop(Exception):
|
||||
pass
|
||||
|
||||
|
||||
def out(text=''):
|
||||
lines.append(text)
|
||||
print(text, flush=True)
|
||||
|
||||
|
||||
def run(command, check=True, quiet=False, env=None):
|
||||
"""Run a command and echo it with its output; return (output, exit code, seconds)."""
|
||||
out('$ ' + ' '.join(command))
|
||||
started = time.monotonic()
|
||||
done = subprocess.run(command, cwd=str(ROOT), env=env, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
|
||||
elapsed = time.monotonic() - started
|
||||
text = done.stdout.decode('utf-8', 'replace').strip()
|
||||
if text and not quiet:
|
||||
out(text)
|
||||
if check and done.returncode != 0:
|
||||
if quiet and text:
|
||||
out(text)
|
||||
raise Stop('command failed with exit code %d' % done.returncode)
|
||||
return text, done.returncode, elapsed
|
||||
|
||||
|
||||
def compose(*args, **kwargs):
|
||||
return run(['docker', 'compose'] + list(args), **kwargs)
|
||||
|
||||
|
||||
PSQL = ['exec', '-T', 'postgres', 'psql', '-U', 'messenger', '-d', 'messenger', '-v', 'ON_ERROR_STOP=1', '-At', '-c']
|
||||
|
||||
|
||||
def psql(sql, quiet=False):
|
||||
if quiet:
|
||||
done = subprocess.run(['docker', 'compose'] + PSQL + [sql], cwd=str(ROOT), stdout=subprocess.PIPE, text=True)
|
||||
return done.stdout.strip()
|
||||
return compose(*(PSQL + [sql]))[0]
|
||||
|
||||
|
||||
def valkey_cli(command):
|
||||
return compose('exec', '-T', 'valkey', 'sh', '-c', VALKEY_CLI + command)[0]
|
||||
|
||||
|
||||
def verdict(name, ok, detail=''):
|
||||
results.append((name, ok))
|
||||
out('=> %s: %s%s' % ('PASS' if ok else 'FAIL', name, ' (' + detail + ')' if detail else ''))
|
||||
out()
|
||||
|
||||
|
||||
def http(method, path, token=None, body=None, headers=None, base=BASE, timeout=10):
|
||||
"""Return (status, parsed JSON or None, seconds); status None when the connection fails."""
|
||||
all_headers = dict(headers or {})
|
||||
if token:
|
||||
all_headers['Authorization'] = 'Bearer ' + token
|
||||
data = None
|
||||
if body is not None:
|
||||
data = json.dumps(body).encode('utf-8')
|
||||
all_headers['Content-Type'] = 'application/json'
|
||||
request = urllib.request.Request(base + path, data=data, headers=all_headers, method=method)
|
||||
started = time.monotonic()
|
||||
try:
|
||||
with urllib.request.urlopen(request, timeout=timeout) as response:
|
||||
raw = response.read()
|
||||
status = response.status
|
||||
except urllib.error.HTTPError as error:
|
||||
with error:
|
||||
raw = error.read()
|
||||
status = error.code
|
||||
except OSError:
|
||||
return None, None, time.monotonic() - started
|
||||
return status, (json.loads(raw) if raw else None), time.monotonic() - started
|
||||
|
||||
|
||||
def basic(username, password):
|
||||
return {'Authorization': 'Basic ' + base64.b64encode((username + ':' + password).encode()).decode()}
|
||||
|
||||
|
||||
def new_user(base=BASE, label='check'):
|
||||
"""Register a synthetic user and log in: (user, password, token). The token is never printed."""
|
||||
username = label + '_' + uuid.uuid4().hex[:12]
|
||||
password = 'Check-' + uuid.uuid4().hex
|
||||
status, user, _ = http('POST', '/api/v1/users', body={'username': username, 'display_name': 'Check', 'password': password}, base=base)
|
||||
if status != 201:
|
||||
raise Stop('registration failed with %s' % status)
|
||||
status, session, _ = http('POST', '/api/v1/auth/sessions', headers=basic(username, password), base=base)
|
||||
if status != 201:
|
||||
raise Stop('login failed with %s' % status)
|
||||
return user, password, session['token']
|
||||
|
||||
|
||||
def wait_ready(base=BASE, seconds=60):
|
||||
deadline = time.monotonic() + seconds
|
||||
while time.monotonic() < deadline:
|
||||
if http('GET', '/health/ready', base=base, timeout=3)[0] == 200:
|
||||
return True
|
||||
time.sleep(0.5)
|
||||
return False
|
||||
|
||||
|
||||
def container(service):
|
||||
done = subprocess.run(['docker', 'compose', 'ps', '-a', '-q', service], cwd=str(ROOT), stdout=subprocess.PIPE, text=True)
|
||||
return done.stdout.strip()
|
||||
|
||||
|
||||
def started_at(service):
|
||||
return subprocess.run(['docker', 'inspect', '-f', '{{.State.StartedAt}}', container(service)],
|
||||
stdout=subprocess.PIPE, text=True).stdout.strip()
|
||||
|
||||
|
||||
def show(label, result):
|
||||
status, payload, seconds = result
|
||||
code = payload['error']['code'] if isinstance(payload, dict) and 'error' in payload else ''
|
||||
out('%-58s -> %s %s in %.2f s' % (label, status, code, seconds))
|
||||
return status
|
||||
|
||||
|
||||
def section_stack():
|
||||
out('== Stack: services, pinned images, health, non-root, Valkey persistence ==')
|
||||
compose('ps', '-a', '--format', 'table {{.Service}}\t{{.Image}}\t{{.Status}}')
|
||||
config = json.loads(compose('config', '--format', 'json', quiet=True)[0])
|
||||
images = {name: service.get('image', '') for name, service in config['services'].items()}
|
||||
pinned = all('@sha256:' in images[name] for name in ('postgres', 'valkey'))
|
||||
verdict('third-party images are pinned by tag and digest', pinned, ', '.join(images[n] for n in ('postgres', 'valkey')))
|
||||
health = {s: subprocess.run(['docker', 'inspect', '-f', '{{.State.Health.Status}}', container(s)],
|
||||
stdout=subprocess.PIPE, text=True).stdout.strip() for s in ('api', 'postgres', 'valkey')}
|
||||
out('health: ' + ', '.join('%s=%s' % item for item in health.items()))
|
||||
verdict('api, postgres and valkey are healthy', set(health.values()) == {'healthy'})
|
||||
identity = compose('exec', '-T', 'api', 'id')[0]
|
||||
verdict('API process does not run as root', 'uid=10001(app)' in identity)
|
||||
aof = valkey_cli('CONFIG GET appendonly') + ' ' + valkey_cli('CONFIG GET appendfsync') + ' ' + valkey_cli('CONFIG GET maxmemory-policy')
|
||||
verdict('Valkey persistence is AOF with fsync always, no eviction',
|
||||
all(word in aof.split() for word in ('yes', 'always', 'noeviction')))
|
||||
|
||||
|
||||
def section_migrations():
|
||||
out('== Migrations: schema, constraints, idempotent re-run ==')
|
||||
psql("SELECT format('%s %s %s', conrelid::regclass, contype, conname) FROM pg_constraint "
|
||||
"WHERE conrelid::regclass::text IN ('users', 'chats', 'chat_members', 'messages') ORDER BY 1")
|
||||
psql("SELECT indexrelid::regclass FROM pg_index WHERE indrelid::regclass::text IN ('chat_members', 'messages') ORDER BY 1")
|
||||
before = psql('SELECT (SELECT count(*) FROM schema_migrations) || \' \' || (SELECT count(*) FROM users)')
|
||||
output = compose('run', '--rm', '--no-deps', 'migrate')[0]
|
||||
after = psql('SELECT (SELECT count(*) FROM schema_migrations) || \' \' || (SELECT count(*) FROM users)')
|
||||
out('schema_migrations and users rows before/after: %s / %s' % (before, after))
|
||||
verdict('re-running migrations applies nothing and duplicates nothing',
|
||||
'applied_now=0' in output and before == after)
|
||||
|
||||
|
||||
def section_rollback():
|
||||
out('== Rollback: chat creation fails on the second INSERT ==')
|
||||
user, _, token = new_user(label='rollback')
|
||||
title = 'rollback-probe-' + uuid.uuid4().hex[:10]
|
||||
status = show('POST /api/v1/chats with one unknown member',
|
||||
http('POST', '/api/v1/chats', token, {'title': title, 'member_ids': [user['id'], str(uuid.uuid4())]}))
|
||||
rows = psql("SELECT count(*) FROM chats WHERE title = '%s'" % title)
|
||||
orphans = psql('SELECT count(*) FROM chats c WHERE NOT EXISTS (SELECT 1 FROM chat_members m WHERE m.chat_id = c.id)')
|
||||
logged = 'event=chat_rolled_back' in compose('logs', '--no-log-prefix', '--tail', '20', 'api', quiet=True)[0]
|
||||
out('rows in chats with this title: %s; chats without members in the whole table: %s; rollback logged: %s' % (rows, orphans, logged))
|
||||
verdict('composite chat creation is rolled back as a whole', status == 400 and rows == '0' and orphans == '0' and logged)
|
||||
|
||||
|
||||
def section_race():
|
||||
out('== Registration race: 20 parallel requests with one username ==')
|
||||
username = 'race_' + uuid.uuid4().hex[:12]
|
||||
barrier = threading.Barrier(20)
|
||||
|
||||
def register(index):
|
||||
barrier.wait()
|
||||
body = {'username': username, 'display_name': 'Race %d' % index, 'password': 'Race-password-%02d' % index}
|
||||
return http('POST', '/api/v1/users', body=body)[0]
|
||||
|
||||
with concurrent.futures.ThreadPoolExecutor(max_workers=20) as pool:
|
||||
statuses = sorted(pool.map(register, range(20)), key=str)
|
||||
out('statuses: ' + ' '.join(str(s) for s in statuses))
|
||||
rows = psql("SELECT count(*) FROM users WHERE username = '%s'" % username)
|
||||
out('rows in users with this username: ' + rows)
|
||||
verdict('exactly one registration wins, the other 19 get 409', statuses == [201] + [409] * 19 and rows == '1')
|
||||
|
||||
|
||||
def section_pool():
|
||||
out('== Connection pool: 60 parallel clients, at most DB_POOL_SIZE=10 connections to PostgreSQL ==')
|
||||
users = [new_user(label='pool') for _ in range(4)]
|
||||
chats = [http('POST', '/api/v1/chats', token, {'title': 'Pool', 'member_ids': [user['id']]})[1] for user, _, token in users]
|
||||
count_sql = "SELECT count(*) FROM pg_stat_activity WHERE application_name = 'messenger-api'"
|
||||
stop = threading.Event()
|
||||
samples = []
|
||||
|
||||
def sample():
|
||||
while not stop.is_set():
|
||||
value = psql(count_sql, quiet=True)
|
||||
if value.isdigit():
|
||||
samples.append(int(value))
|
||||
time.sleep(0.2)
|
||||
|
||||
def client(index):
|
||||
_, _, token = users[index % 4]
|
||||
path = '/api/v1/chats/%s/messages?limit=20' % chats[index % 4]['id']
|
||||
statuses = []
|
||||
deadline = time.monotonic() + 5
|
||||
while time.monotonic() < deadline:
|
||||
statuses.append(http('GET', path, token)[0])
|
||||
return statuses
|
||||
|
||||
sampler = threading.Thread(target=sample)
|
||||
sampler.start()
|
||||
try:
|
||||
with concurrent.futures.ThreadPoolExecutor(max_workers=60) as pool:
|
||||
statuses = [status for batch in pool.map(client, range(60)) for status in batch]
|
||||
finally:
|
||||
stop.set()
|
||||
sampler.join()
|
||||
counts = {status: statuses.count(status) for status in sorted(set(statuses), key=str)}
|
||||
out('requests: %d, statuses: %s' % (len(statuses), counts))
|
||||
out('API connections in pg_stat_activity, %d samples: max %s' % (len(samples), max(samples) if samples else '-'))
|
||||
verdict('the pool never opens more than 10 connections and all requests succeed',
|
||||
bool(samples) and max(samples) <= 10 and set(counts) == {200})
|
||||
|
||||
|
||||
def section_timeouts():
|
||||
out('== Timeouts: chat row locked for 6 s by another session, API answers 503 after lock_timeout=3s ==')
|
||||
user, _, token = new_user(label='lock')
|
||||
chat = http('POST', '/api/v1/chats', token, {'title': 'Lock', 'member_ids': [user['id']]})[1]
|
||||
path = '/api/v1/chats/%s/messages' % chat['id']
|
||||
lock = "BEGIN; SELECT 1 FROM chats WHERE id = '%s' FOR UPDATE; SELECT pg_sleep(6); ROLLBACK;" % chat['id']
|
||||
out('$ docker compose ' + ' '.join(PSQL) + ' "' + lock + '" (in the background)')
|
||||
locker = subprocess.Popen(['docker', 'compose'] + PSQL + [lock], cwd=str(ROOT), stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
||||
time.sleep(1.5)
|
||||
status, _, seconds = http('POST', path, token, {'text': 'while locked'})
|
||||
show('POST message while the chat row is locked', (status, _, seconds))
|
||||
locker.wait(timeout=30)
|
||||
after = show('POST message after the lock is released', http('POST', path, token, {'text': 'after lock'}))
|
||||
verdict('waiting for a lock is bounded: 503 after about 3 s, then writes work again',
|
||||
status == 503 and 2.5 <= seconds <= 4.5 and after == 201, '%.2f s' % seconds)
|
||||
|
||||
|
||||
def section_logs():
|
||||
out('== Logs: useful fields, no message texts, passwords or tokens ==')
|
||||
user, password, token = new_user(label='logs')
|
||||
chat = http('POST', '/api/v1/chats', token, {'title': 'Logs', 'member_ids': [user['id']]})[1]
|
||||
marker = 'log-marker-' + uuid.uuid4().hex
|
||||
http('POST', '/api/v1/chats/%s/messages' % chat['id'], token, {'text': marker})
|
||||
logs = compose('logs', '--no-log-prefix', 'api', quiet=True)[0]
|
||||
compose('logs', '--no-log-prefix', '--tail', '3', 'api')
|
||||
leaked = [name for name, value in (('message text', marker), ('password', password), ('token', token)) if value in logs]
|
||||
secrets_found = [p.name for p in (ROOT / 'secrets').glob('*_password') if p.read_text().strip() in logs]
|
||||
out('searched %d log lines; found: %s' % (logs.count('\n') + 1, ', '.join(leaked + secrets_found) or 'nothing'))
|
||||
verdict('logs contain request_id/status/duration and no secrets', 'request_id=' in logs and not leaked and not secrets_found)
|
||||
|
||||
|
||||
def section_expiry():
|
||||
out('== Session expiry: second API instance with SESSION_TTL_SECONDS=2 ==')
|
||||
compose('--profile', 'expiry', 'up', '-d', 'api-short-ttl')
|
||||
try:
|
||||
if not wait_ready(SHORT_TTL_BASE):
|
||||
raise Stop('api-short-ttl is not ready')
|
||||
user, _, token = new_user(SHORT_TTL_BASE, label='expiry')
|
||||
key = 'session:' + hashlib.sha256(token.encode()).hexdigest()
|
||||
first = show('GET /users/me right after login', http('GET', '/api/v1/users/me', token, base=SHORT_TTL_BASE))
|
||||
pttl = valkey_cli('PTTL ' + key)
|
||||
out('PTTL of the session key in Valkey (ms): ' + pttl)
|
||||
time.sleep(3)
|
||||
expired = show('GET /users/me 3 s later', http('GET', '/api/v1/users/me', token, base=SHORT_TTL_BASE))
|
||||
gone = valkey_cli('EXISTS ' + key)
|
||||
out('session key still exists in Valkey: ' + gone)
|
||||
verdict('session expires after SESSION_TTL_SECONDS and the key disappears from Valkey',
|
||||
first == 200 and 0 < int(pttl) <= 2000 and expired == 401 and gone == '0')
|
||||
finally:
|
||||
compose('--profile', 'expiry', 'rm', '-sf', 'api-short-ttl', quiet=True)
|
||||
|
||||
|
||||
def section_valkey_down():
|
||||
out('== Valkey is down: bounded 503, no anonymous access, recovery with the same token ==')
|
||||
user, password, token = new_user(label='valkey')
|
||||
compose('stop', 'valkey')
|
||||
try:
|
||||
me = show('GET /users/me with a valid token', http('GET', '/api/v1/users/me', token))
|
||||
login = show('POST /auth/sessions with valid credentials', http('POST', '/api/v1/auth/sessions', headers=basic(user['username'], password)))
|
||||
anonymous = show('GET /users/me without a token', http('GET', '/api/v1/users/me'))
|
||||
register = show('POST /users (needs only PostgreSQL)', http('POST', '/api/v1/users', body={
|
||||
'username': 'novalkey_' + uuid.uuid4().hex[:10], 'display_name': 'No Valkey', 'password': 'No-valkey-password-1'}))
|
||||
ready = show('GET /health/ready', http('GET', '/health/ready'))
|
||||
live = show('GET /health/live', http('GET', '/health/live'))
|
||||
slowest = max(http('GET', '/api/v1/users/me', token)[2] for _ in range(3))
|
||||
out('slowest of 3 more authenticated requests: %.2f s' % slowest)
|
||||
finally:
|
||||
compose('start', 'valkey')
|
||||
recovered = False
|
||||
deadline = time.monotonic() + 60
|
||||
while time.monotonic() < deadline and not recovered:
|
||||
recovered = http('GET', '/api/v1/users/me', token, timeout=3)[0] == 200
|
||||
time.sleep(0.5)
|
||||
out('same token after Valkey is back: %s' % ('200' if recovered else 'not accepted within 60 s'))
|
||||
verdict('without Valkey: 503 for sessions, 401 for anonymous, registration still works',
|
||||
me == 503 and login == 503 and anonymous == 401 and register == 201 and ready == 503 and live == 200 and slowest < 3)
|
||||
verdict('after Valkey restarts the session is still valid (AOF)', recovered)
|
||||
|
||||
|
||||
def section_postgres_down():
|
||||
out('== PostgreSQL is down: bounded 503, recovery without restarting the API ==')
|
||||
user, _, token = new_user(label='postgres')
|
||||
compose('stop', 'postgres')
|
||||
try:
|
||||
me = show('GET /users/me with a valid token', http('GET', '/api/v1/users/me', token))
|
||||
chat = show('POST /chats', http('POST', '/api/v1/chats', token, {'title': 'No DB', 'member_ids': [user['id']]}))
|
||||
anonymous = show('GET /users/me without a token', http('GET', '/api/v1/users/me'))
|
||||
ready = show('GET /health/ready', http('GET', '/health/ready'))
|
||||
slowest = max(http('GET', '/api/v1/users/me', token)[2] for _ in range(3))
|
||||
out('slowest of 3 more requests: %.2f s' % slowest)
|
||||
finally:
|
||||
compose('start', 'postgres')
|
||||
recovered = False
|
||||
deadline = time.monotonic() + 60
|
||||
while time.monotonic() < deadline and not recovered:
|
||||
recovered = http('GET', '/api/v1/users/me', token, timeout=5)[0] == 200
|
||||
time.sleep(0.5)
|
||||
out('same token after PostgreSQL is back: %s' % ('200' if recovered else 'not accepted within 60 s'))
|
||||
verdict('without PostgreSQL: bounded 503, anonymous still 401',
|
||||
me == 503 and chat == 503 and anonymous == 401 and ready == 503 and slowest < 5)
|
||||
verdict('API recovers by itself when PostgreSQL is back', recovered)
|
||||
|
||||
|
||||
def section_persistence():
|
||||
out('== Full restart: public tests/resilience.py with the restart hook ==')
|
||||
hook = 'scripts/restart.cmd' if os.name == 'nt' else 'scripts/restart.sh'
|
||||
volumes_before = run(['docker', 'volume', 'inspect', '-f', '{{.Name}} {{.CreatedAt}}', 'mtusi-lab2_pgdata', 'mtusi-lab2_valkeydata'])[0]
|
||||
before = {s: started_at(s) for s in ('api', 'postgres', 'valkey')}
|
||||
_, code, seconds = run([sys.executable, 'tests/resilience.py', '--mode', 'persistence', '--action-script', hook, '--base-url', BASE], check=False)
|
||||
after = {s: started_at(s) for s in ('api', 'postgres', 'valkey')}
|
||||
volumes_after = run(['docker', 'volume', 'inspect', '-f', '{{.Name}} {{.CreatedAt}}', 'mtusi-lab2_pgdata', 'mtusi-lab2_valkeydata'])[0]
|
||||
for service in before:
|
||||
out('%-8s StartedAt %s -> %s' % (service, before[service], after[service]))
|
||||
restarted = all(before[s] != after[s] for s in before)
|
||||
verdict('resilience test passes after restarting api, postgres and valkey with the same volumes',
|
||||
code == 0 and restarted and volumes_before == volumes_after, 'hook %s, %.1f s' % (hook, seconds))
|
||||
|
||||
|
||||
def section_sigterm():
|
||||
out('== SIGTERM: docker compose stop api ==')
|
||||
_, _, seconds = compose('stop', '-t', '10', 'api')
|
||||
exit_code = subprocess.run(['docker', 'inspect', '-f', '{{.State.ExitCode}}', container('api')],
|
||||
stdout=subprocess.PIPE, text=True).stdout.strip()
|
||||
compose('logs', '--no-log-prefix', '--tail', '2', 'api')
|
||||
refused = http('GET', '/health/live', timeout=3)[0] is None
|
||||
compose('start', 'api')
|
||||
ready = wait_ready()
|
||||
out('stop took %.2f s, exit code %s, new request refused: %s, ready again: %s' % (seconds, exit_code, refused, ready))
|
||||
verdict('SIGTERM stops the API cleanly within 10 s', exit_code == '0' and seconds < 10 and refused and ready)
|
||||
|
||||
|
||||
SECTIONS = {
|
||||
'stack': section_stack,
|
||||
'migrations': section_migrations,
|
||||
'rollback': section_rollback,
|
||||
'race': section_race,
|
||||
'pool': section_pool,
|
||||
'timeouts': section_timeouts,
|
||||
'logs': section_logs,
|
||||
'expiry': section_expiry,
|
||||
'valkey-down': section_valkey_down,
|
||||
'postgres-down': section_postgres_down,
|
||||
'persistence': section_persistence,
|
||||
'sigterm': section_sigterm,
|
||||
}
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description='Lab 2 infrastructure checks; creates synthetic users and messages')
|
||||
parser.add_argument('--only', nargs='+', choices=sorted(SECTIONS), help='run only these sections')
|
||||
parser.add_argument('--output', default=None, help='also write the transcript to this file')
|
||||
args = parser.parse_args()
|
||||
sys.stdout.reconfigure(errors='replace')
|
||||
out('# %s UTC, python %s, %s' % (time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime()), sys.version.split()[0], sys.platform))
|
||||
out('# ' + run(['docker', 'version', '--format', 'docker {{.Server.Version}}'], quiet=True)[0])
|
||||
if not wait_ready(seconds=30):
|
||||
out('API is not ready on %s: start the stand with docker compose up -d --build' % BASE)
|
||||
return 2
|
||||
for name in args.only or SECTIONS:
|
||||
try:
|
||||
SECTIONS[name]()
|
||||
except (Stop, KeyError, TypeError, ValueError) as error:
|
||||
verdict('section %s completed' % name, False, str(error) or type(error).__name__)
|
||||
if not wait_ready(seconds=60):
|
||||
verdict('stand is ready after section %s' % name, False)
|
||||
break
|
||||
out('== Summary ==')
|
||||
for name, ok in results:
|
||||
out('%s %s' % ('PASS' if ok else 'FAIL', name))
|
||||
failed = [name for name, ok in results if not ok]
|
||||
out('RESULT: %s' % ('FAIL' if failed else 'PASS'))
|
||||
if args.output:
|
||||
Path(args.output).parent.mkdir(parents=True, exist_ok=True)
|
||||
Path(args.output).write_text('\n'.join(lines) + '\n', encoding='utf-8')
|
||||
return 1 if failed else 0
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
try:
|
||||
sys.exit(main())
|
||||
except FileNotFoundError:
|
||||
print('docker CLI was not found: install Docker and make sure it is on PATH')
|
||||
sys.exit(2)
|
||||
@@ -0,0 +1,9 @@
|
||||
@echo off
|
||||
rem Windows twin of scripts/restart.sh: tests/resilience.py starts the hook directly,
|
||||
rem and Windows cannot execute a .sh file. Same actions, volumes are kept.
|
||||
setlocal
|
||||
cd /d "%~dp0.."
|
||||
docker compose stop api || exit /b 1
|
||||
docker compose restart postgres valkey || exit /b 1
|
||||
docker compose start api || exit /b 1
|
||||
exit /b 0
|
||||
Executable
+6
@@ -0,0 +1,6 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
cd "$(dirname "$0")/.."
|
||||
docker compose stop api
|
||||
docker compose restart postgres valkey
|
||||
docker compose start api
|
||||
@@ -0,0 +1 @@
|
||||
"""In-memory мессенджер, лабораторная 1. Только стандартная библиотека Python."""
|
||||
@@ -0,0 +1,64 @@
|
||||
"""Точка входа API: python -m messenger."""
|
||||
import logging
|
||||
import signal
|
||||
import sys
|
||||
import threading
|
||||
import time
|
||||
|
||||
from .config import load_config
|
||||
from .db import Repository, create_pool
|
||||
from .server import MessengerServer
|
||||
from .sessions import SessionStore
|
||||
|
||||
log = logging.getLogger('messenger')
|
||||
|
||||
|
||||
def main():
|
||||
config = load_config()
|
||||
logging.Formatter.converter = time.gmtime
|
||||
logging.basicConfig(
|
||||
stream=sys.stdout,
|
||||
level=config.log_level,
|
||||
format='%(asctime)s.%(msecs)03dZ %(levelname)s %(message)s',
|
||||
datefmt='%Y-%m-%dT%H:%M:%S',
|
||||
)
|
||||
pool = create_pool(config.database_url, config.db_pool_size)
|
||||
pool.open(wait=False)
|
||||
sessions = SessionStore(config.cache_url, config.session_ttl)
|
||||
try:
|
||||
server = MessengerServer((config.host, config.port), Repository(pool), sessions)
|
||||
except OSError as error:
|
||||
log.error('event=bind_failed host=%s port=%d error=%s', config.host, config.port, error)
|
||||
return 1
|
||||
|
||||
received = []
|
||||
|
||||
def on_signal(signum, _frame):
|
||||
received.append(signum)
|
||||
|
||||
for name in ('SIGTERM', 'SIGINT', 'SIGBREAK'):
|
||||
if hasattr(signal, name):
|
||||
signal.signal(getattr(signal, name), on_signal)
|
||||
|
||||
threading.Thread(target=server.serve_forever, name='accept-loop', daemon=True).start()
|
||||
log.info('event=started host=%s port=%d env=%s session_ttl_s=%d db_pool_max=%d shutdown_timeout_s=%g',
|
||||
config.host, config.port, config.env, config.session_ttl, config.db_pool_size, config.shutdown_timeout)
|
||||
|
||||
while not received:
|
||||
time.sleep(0.1)
|
||||
|
||||
started = time.monotonic()
|
||||
log.info('event=shutdown_started signal=%s', signal.Signals(received[0]).name)
|
||||
server.begin_drain()
|
||||
server.shutdown()
|
||||
server.server_close()
|
||||
remaining = config.shutdown_timeout - 1 - (time.monotonic() - started)
|
||||
clean = server.wait_idle(max(remaining, 0))
|
||||
pool.close(timeout=1)
|
||||
sessions.close()
|
||||
log.info('event=shutdown_finished clean=%s duration_ms=%.0f', clean, (time.monotonic() - started) * 1000)
|
||||
return 0 if clean else 1
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
sys.exit(main())
|
||||
@@ -0,0 +1,67 @@
|
||||
"""Конфигурация из переменных окружения и файлов секретов (см. .env.example и compose.yaml)."""
|
||||
import os
|
||||
from collections import namedtuple
|
||||
|
||||
Config = namedtuple('Config', 'host port env log_level shutdown_timeout session_ttl db_pool_size database_url cache_url')
|
||||
|
||||
LOG_LEVELS = ('DEBUG', 'INFO', 'WARNING', 'ERROR')
|
||||
MAX_SHUTDOWN_TIMEOUT = 10.0
|
||||
MAX_SESSION_TTL = 30 * 24 * 3600
|
||||
|
||||
|
||||
def _environ(environ):
|
||||
return {name: value.strip() for name, value in (os.environ if environ is None else environ).items()}
|
||||
|
||||
|
||||
def read_secret(name, environ=None):
|
||||
"""Секрет из файла, путь к которому в NAME_FILE (секреты Docker), или из NAME для отладки.
|
||||
|
||||
Значение секрета никогда не попадает в сообщения об ошибках и в логи.
|
||||
"""
|
||||
environ = _environ(environ)
|
||||
path = environ.get(name + '_FILE')
|
||||
if path:
|
||||
try:
|
||||
with open(path, encoding='utf-8') as file:
|
||||
value = file.read().strip()
|
||||
except OSError as error:
|
||||
raise SystemExit('Не удалось прочитать файл из %s_FILE: %s' % (name, error.strerror))
|
||||
else:
|
||||
value = environ.get(name, '')
|
||||
if not value:
|
||||
raise SystemExit('Нужен %s_FILE (путь к файлу секрета) или %s' % (name, name))
|
||||
return value
|
||||
|
||||
|
||||
def _integer(environ, name, default, low, high):
|
||||
try:
|
||||
value = int(environ.get(name, str(default)))
|
||||
except ValueError:
|
||||
raise SystemExit('%s должен быть целым числом' % name)
|
||||
if not low <= value <= high:
|
||||
raise SystemExit('%s должен быть в диапазоне %d..%d' % (name, low, high))
|
||||
return value
|
||||
|
||||
|
||||
def load_config(environ=None):
|
||||
environ = _environ(environ)
|
||||
try:
|
||||
shutdown_timeout = float(environ.get('APP_SHUTDOWN_TIMEOUT', '8'))
|
||||
except ValueError:
|
||||
raise SystemExit('APP_SHUTDOWN_TIMEOUT должен быть числом')
|
||||
if not 0 < shutdown_timeout <= MAX_SHUTDOWN_TIMEOUT:
|
||||
raise SystemExit('APP_SHUTDOWN_TIMEOUT должен быть больше 0 и не больше 10 секунд')
|
||||
log_level = environ.get('APP_LOG_LEVEL', 'INFO').upper()
|
||||
if log_level not in LOG_LEVELS:
|
||||
raise SystemExit('APP_LOG_LEVEL должен быть одним из: ' + ', '.join(LOG_LEVELS))
|
||||
return Config(
|
||||
host=environ.get('APP_HOST', '0.0.0.0'),
|
||||
port=_integer(environ, 'APP_PORT', 8080, 1, 65535),
|
||||
env=environ.get('APP_ENV', 'lab'),
|
||||
log_level=log_level,
|
||||
shutdown_timeout=shutdown_timeout,
|
||||
session_ttl=_integer(environ, 'SESSION_TTL_SECONDS', 3600, 1, MAX_SESSION_TTL),
|
||||
db_pool_size=_integer(environ, 'DB_POOL_SIZE', 10, 1, 50),
|
||||
database_url=read_secret('DATABASE_URL', environ),
|
||||
cache_url=read_secret('CACHE_URL', environ),
|
||||
)
|
||||
@@ -0,0 +1,202 @@
|
||||
"""Доступ к PostgreSQL: ограниченный пул соединений, таймауты, только параметризованные запросы.
|
||||
|
||||
Значения всегда передаются отдельно от текста SQL (%s), поэтому SQL-инъекция невозможна.
|
||||
Каждый метод — одна транзакция: при любой ошибке внутри неё всё откатывается.
|
||||
"""
|
||||
import logging
|
||||
import uuid
|
||||
from contextlib import contextmanager
|
||||
|
||||
import psycopg
|
||||
from psycopg import errors
|
||||
from psycopg.rows import dict_row
|
||||
from psycopg_pool import ConnectionPool
|
||||
|
||||
from .errors import Unavailable
|
||||
from .timeutil import format_time, from_micros, to_micros
|
||||
|
||||
log = logging.getLogger('messenger')
|
||||
|
||||
SESSION_OPTIONS = '-c statement_timeout=5000 -c lock_timeout=3000 -c idle_in_transaction_session_timeout=10000'
|
||||
|
||||
MEMBER_IDS = 'array(SELECT m.user_id FROM chat_members m WHERE m.chat_id = c.id ORDER BY m.position) AS member_ids'
|
||||
|
||||
|
||||
class UsernameTaken(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class UnknownMember(Exception):
|
||||
pass
|
||||
|
||||
|
||||
def create_pool(database_url, max_size):
|
||||
return ConnectionPool(
|
||||
database_url,
|
||||
min_size=1,
|
||||
max_size=max_size,
|
||||
timeout=3,
|
||||
max_waiting=100,
|
||||
check=ConnectionPool.check_connection,
|
||||
kwargs={
|
||||
'connect_timeout': 3,
|
||||
'application_name': 'messenger-api',
|
||||
'options': SESSION_OPTIONS,
|
||||
'row_factory': dict_row,
|
||||
},
|
||||
name='messenger',
|
||||
open=False,
|
||||
)
|
||||
|
||||
|
||||
def _user(row):
|
||||
return {'id': str(row['id']), 'username': row['username'], 'display_name': row['display_name'],
|
||||
'created_at': format_time(row['created_at'])}
|
||||
|
||||
|
||||
def _chat(row):
|
||||
return {'id': str(row['id']), 'title': row['title'], 'member_ids': [str(m) for m in row['member_ids']],
|
||||
'created_at': format_time(row['created_at'])}
|
||||
|
||||
|
||||
def _message(row):
|
||||
return {'id': str(row['id']), 'chat_id': str(row['chat_id']), 'sender_id': str(row['sender_id']),
|
||||
'text': row['text'], 'created_at': format_time(row['created_at'])}
|
||||
|
||||
|
||||
def _page(rows, limit, convert):
|
||||
"""Из limit+1 строк: элементы страницы и ключ (created_at, id) последней, если есть продолжение."""
|
||||
items = rows[:limit]
|
||||
has_more = len(rows) > limit
|
||||
next_key = (to_micros(items[-1]['created_at']), str(items[-1]['id'])) if has_more else None
|
||||
return [convert(row) for row in items], next_key
|
||||
|
||||
|
||||
def _after(key):
|
||||
return from_micros(key[0]), uuid.UUID(key[1])
|
||||
|
||||
|
||||
class Repository:
|
||||
def __init__(self, pool):
|
||||
self._pool = pool
|
||||
|
||||
@contextmanager
|
||||
def _transaction(self):
|
||||
"""Соединение из пула в транзакции: COMMIT при успехе, ROLLBACK при исключении."""
|
||||
try:
|
||||
with self._pool.connection() as conn:
|
||||
yield conn
|
||||
except psycopg.OperationalError as error:
|
||||
raise Unavailable('postgres') from error
|
||||
|
||||
def ping(self):
|
||||
with self._transaction() as conn:
|
||||
conn.execute('SELECT 1')
|
||||
|
||||
|
||||
def create_user(self, username, display_name, password_hash):
|
||||
try:
|
||||
with self._transaction() as conn:
|
||||
row = conn.execute(
|
||||
'INSERT INTO users (id, username, display_name, password_hash) VALUES (%s, %s, %s, %s) '
|
||||
'RETURNING id, username, display_name, created_at',
|
||||
(uuid.uuid4(), username, display_name, password_hash),
|
||||
).fetchone()
|
||||
except errors.UniqueViolation:
|
||||
raise UsernameTaken
|
||||
return _user(row)
|
||||
|
||||
def get_user(self, user_id):
|
||||
with self._transaction() as conn:
|
||||
row = conn.execute(
|
||||
'SELECT id, username, display_name, created_at FROM users WHERE id = %s', (uuid.UUID(user_id),),
|
||||
).fetchone()
|
||||
return _user(row) if row else None
|
||||
|
||||
def get_credentials(self, username):
|
||||
"""(пользователь, хеш пароля) или (None, None)."""
|
||||
with self._transaction() as conn:
|
||||
row = conn.execute(
|
||||
'SELECT id, username, display_name, created_at, password_hash FROM users WHERE username = %s',
|
||||
(username,),
|
||||
).fetchone()
|
||||
return (_user(row), row['password_hash']) if row else (None, None)
|
||||
|
||||
|
||||
def create_chat(self, title, member_ids):
|
||||
"""Чат и все участники одной транзакцией.
|
||||
|
||||
Неизвестный участник нарушает внешний ключ chat_members.user_id; тогда транзакция
|
||||
откатывается целиком и уже вставленная строка chats тоже исчезает.
|
||||
"""
|
||||
chat_id = uuid.uuid4()
|
||||
members = [uuid.UUID(member_id) for member_id in member_ids]
|
||||
try:
|
||||
with self._transaction() as conn:
|
||||
row = conn.execute(
|
||||
'INSERT INTO chats (id, title) VALUES (%s, %s) RETURNING id, title, created_at', (chat_id, title),
|
||||
).fetchone()
|
||||
conn.execute(
|
||||
'INSERT INTO chat_members (chat_id, user_id, position) '
|
||||
'SELECT %s, member.user_id, member.ordinality - 1 '
|
||||
'FROM unnest(%s::uuid[]) WITH ORDINALITY AS member(user_id, ordinality)',
|
||||
(chat_id, members),
|
||||
)
|
||||
except errors.ForeignKeyViolation:
|
||||
log.info('event=chat_rolled_back reason=unknown_member')
|
||||
raise UnknownMember
|
||||
return _chat(dict(row, member_ids=members))
|
||||
|
||||
def get_chat(self, chat_id, user_id):
|
||||
"""Чат, если он есть и user_id в нём состоит; иначе None."""
|
||||
with self._transaction() as conn:
|
||||
row = conn.execute(
|
||||
'SELECT c.id, c.title, c.created_at, ' + MEMBER_IDS + ' FROM chats c '
|
||||
'WHERE c.id = %s AND EXISTS ('
|
||||
' SELECT 1 FROM chat_members m WHERE m.chat_id = c.id AND m.user_id = %s)',
|
||||
(uuid.UUID(chat_id), uuid.UUID(user_id)),
|
||||
).fetchone()
|
||||
return _chat(row) if row else None
|
||||
|
||||
def list_chats(self, user_id, after, limit):
|
||||
select = ('SELECT c.id, c.title, c.created_at, ' + MEMBER_IDS + ' '
|
||||
'FROM chat_members me JOIN chats c ON c.id = me.chat_id WHERE me.user_id = %s ')
|
||||
order = 'ORDER BY c.created_at, c.id LIMIT %s'
|
||||
with self._transaction() as conn:
|
||||
if after is None:
|
||||
rows = conn.execute(select + order, (uuid.UUID(user_id), limit + 1)).fetchall()
|
||||
else:
|
||||
rows = conn.execute(select + 'AND (c.created_at, c.id) > (%s, %s) ' + order,
|
||||
(uuid.UUID(user_id), *_after(after), limit + 1)).fetchall()
|
||||
return _page(rows, limit, _chat)
|
||||
|
||||
|
||||
def add_message(self, chat_id, sender_id, text):
|
||||
"""Сообщение получает created_at строго больше всех сообщений этого чата.
|
||||
|
||||
Строка чата блокируется до конца транзакции, поэтому сообщения одного чата
|
||||
записываются по очереди и время растёт в порядке фиксации: читатель, идущий
|
||||
по cursor, не пропустит сообщение, зафиксированное позже уже прочитанного.
|
||||
"""
|
||||
with self._transaction() as conn:
|
||||
conn.execute('SELECT 1 FROM chats WHERE id = %s FOR NO KEY UPDATE', (uuid.UUID(chat_id),))
|
||||
row = conn.execute(
|
||||
'INSERT INTO messages (id, chat_id, sender_id, text, created_at) '
|
||||
'VALUES (%s, %s, %s, %s, GREATEST(clock_timestamp(), '
|
||||
" (SELECT max(created_at) FROM messages WHERE chat_id = %s) + interval '1 microsecond')) "
|
||||
'RETURNING id, chat_id, sender_id, text, created_at',
|
||||
(uuid.uuid4(), uuid.UUID(chat_id), uuid.UUID(sender_id), text, uuid.UUID(chat_id)),
|
||||
).fetchone()
|
||||
return _message(row)
|
||||
|
||||
def list_messages(self, chat_id, after, limit):
|
||||
"""Страница истории. Запрос совпадает по форме с индексом messages_chat_history_idx."""
|
||||
select = 'SELECT id, chat_id, sender_id, text, created_at FROM messages WHERE chat_id = %s '
|
||||
order = 'ORDER BY created_at, id LIMIT %s'
|
||||
with self._transaction() as conn:
|
||||
if after is None:
|
||||
rows = conn.execute(select + order, (uuid.UUID(chat_id), limit + 1)).fetchall()
|
||||
else:
|
||||
rows = conn.execute(select + 'AND (created_at, id) > (%s, %s) ' + order,
|
||||
(uuid.UUID(chat_id), *_after(after), limit + 1)).fetchall()
|
||||
return _page(rows, limit, _message)
|
||||
@@ -0,0 +1,28 @@
|
||||
"""Ошибки, которые HTTP-слой превращает в ответы контракта."""
|
||||
|
||||
|
||||
class ApiError(Exception):
|
||||
"""Ответ с ошибкой в формате {"error": {...}}; challenge — значение WWW-Authenticate для 401."""
|
||||
|
||||
def __init__(self, status, code, message, challenge=None):
|
||||
super().__init__(message)
|
||||
self.status = status
|
||||
self.code = code
|
||||
self.message = message
|
||||
self.challenge = challenge
|
||||
|
||||
|
||||
class Unavailable(Exception):
|
||||
"""Хранилище (postgres или valkey) не ответило в срок; клиент получит 503."""
|
||||
|
||||
def __init__(self, dependency):
|
||||
super().__init__(dependency)
|
||||
self.dependency = dependency
|
||||
|
||||
|
||||
def bad_request(message):
|
||||
return ApiError(400, 'invalid_request', message)
|
||||
|
||||
|
||||
def not_found():
|
||||
return ApiError(404, 'not_found', 'Объект не найден')
|
||||
@@ -0,0 +1,26 @@
|
||||
"""Проверка для Docker HEALTHCHECK: python -m messenger.healthcheck.
|
||||
|
||||
В slim-образе нет curl, поэтому готовность проверяется самим интерпретатором.
|
||||
Код возврата 0 — контейнер healthy, 1 — unhealthy.
|
||||
"""
|
||||
import http.client
|
||||
import os
|
||||
import sys
|
||||
|
||||
|
||||
def main():
|
||||
try:
|
||||
connection = http.client.HTTPConnection('127.0.0.1', int(os.environ.get('APP_PORT', '8080')), timeout=2)
|
||||
try:
|
||||
connection.request('GET', '/health/ready', headers={'Connection': 'close'})
|
||||
response = connection.getresponse()
|
||||
response.read()
|
||||
return 0 if response.status == 200 else 1
|
||||
finally:
|
||||
connection.close()
|
||||
except (OSError, ValueError, http.client.HTTPException):
|
||||
return 1
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
sys.exit(main())
|
||||
@@ -0,0 +1,75 @@
|
||||
"""Миграции схемы: python -m messenger.migrate.
|
||||
|
||||
Файлы migrations/NNNN_*.sql применяются по порядку номеров, каждый в своей транзакции.
|
||||
Применённые версии и контрольные суммы записываются в schema_migrations, поэтому повторный
|
||||
запуск ничего не дублирует. Изменение уже применённого файла останавливает запуск: менять
|
||||
схему нужно новым файлом. Advisory lock не даёт двум процессам применять миграции одновременно.
|
||||
"""
|
||||
import hashlib
|
||||
import logging
|
||||
import sys
|
||||
import time
|
||||
from pathlib import Path
|
||||
|
||||
import psycopg
|
||||
|
||||
from .config import read_secret
|
||||
|
||||
log = logging.getLogger('messenger.migrate')
|
||||
|
||||
MIGRATIONS_DIR = Path(__file__).resolve().parents[1] / 'migrations'
|
||||
LOCK_ID = 2026_0002
|
||||
|
||||
CREATE_TABLE = '''
|
||||
CREATE TABLE IF NOT EXISTS schema_migrations (
|
||||
version text PRIMARY KEY,
|
||||
checksum text NOT NULL,
|
||||
applied_at timestamptz NOT NULL DEFAULT now()
|
||||
)'''
|
||||
|
||||
|
||||
def _read(path):
|
||||
sql = path.read_text(encoding='utf-8').replace('\r\n', '\n')
|
||||
return sql, hashlib.sha256(sql.encode('utf-8')).hexdigest()
|
||||
|
||||
|
||||
def migrate(database_url, directory=MIGRATIONS_DIR):
|
||||
files = sorted(directory.glob('[0-9][0-9][0-9][0-9]_*.sql'))
|
||||
if not files:
|
||||
raise SystemExit('Нет файлов миграций в ' + str(directory))
|
||||
applied_now = 0
|
||||
with psycopg.connect(database_url, autocommit=True, connect_timeout=5) as conn:
|
||||
conn.execute('SELECT pg_advisory_lock(%s)', (LOCK_ID,))
|
||||
try:
|
||||
conn.execute(CREATE_TABLE)
|
||||
applied = dict(conn.execute('SELECT version, checksum FROM schema_migrations').fetchall())
|
||||
for path in files:
|
||||
version = path.stem
|
||||
sql, checksum = _read(path)
|
||||
if version in applied:
|
||||
if applied[version] != checksum:
|
||||
raise SystemExit('Миграция %s изменена после применения: добавьте новую вместо правки' % version)
|
||||
continue
|
||||
started = time.monotonic()
|
||||
with conn.transaction():
|
||||
conn.execute(sql)
|
||||
conn.execute('INSERT INTO schema_migrations (version, checksum) VALUES (%s, %s)', (version, checksum))
|
||||
applied_now += 1
|
||||
log.info('event=migration_applied version=%s duration_ms=%.0f', version, (time.monotonic() - started) * 1000)
|
||||
finally:
|
||||
conn.execute('SELECT pg_advisory_unlock(%s)', (LOCK_ID,))
|
||||
log.info('event=migrations_done applied_now=%d total=%d', applied_now, len(files))
|
||||
|
||||
|
||||
def main():
|
||||
logging.basicConfig(stream=sys.stdout, level=logging.INFO, format='%(levelname)s %(message)s')
|
||||
try:
|
||||
migrate(read_secret('DATABASE_URL'))
|
||||
except psycopg.Error as error:
|
||||
log.error('event=migration_failed error=%s detail=%s', type(error).__name__, str(error).splitlines()[0][:300])
|
||||
return 1
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
sys.exit(main())
|
||||
@@ -0,0 +1,25 @@
|
||||
"""Хеширование паролей: Argon2id из библиотеки argon2-cffi.
|
||||
|
||||
Хеш хранит алгоритм, параметры и соль в одной строке вида $argon2id$v=19$m=...,t=...,p=...$соль$хеш.
|
||||
"""
|
||||
import secrets
|
||||
|
||||
from argon2 import PasswordHasher
|
||||
from argon2.exceptions import InvalidHashError, VerificationError
|
||||
|
||||
_hasher = PasswordHasher(time_cost=2, memory_cost=19 * 1024, parallelism=1)
|
||||
|
||||
_DUMMY_HASH = _hasher.hash(secrets.token_hex(16))
|
||||
|
||||
|
||||
def hash_password(password):
|
||||
return _hasher.hash(password)
|
||||
|
||||
|
||||
def verify_password(password_hash, password):
|
||||
"""True, если пароль подходит к хешу; при password_hash=None всегда False за то же время."""
|
||||
try:
|
||||
_hasher.verify(password_hash or _DUMMY_HASH, password)
|
||||
except (VerificationError, InvalidHashError):
|
||||
return False
|
||||
return password_hash is not None
|
||||
@@ -0,0 +1,332 @@
|
||||
"""HTTP-слой: маршрутизация, аутентификация, ответы в формате контракта, логи запросов."""
|
||||
import json
|
||||
import logging
|
||||
import re
|
||||
import sys
|
||||
import threading
|
||||
import time
|
||||
import uuid
|
||||
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
|
||||
from urllib.parse import parse_qs
|
||||
|
||||
from . import validation
|
||||
from .db import UnknownMember, UsernameTaken
|
||||
from .errors import ApiError, Unavailable, bad_request, not_found
|
||||
from .passwords import hash_password, verify_password
|
||||
from .timeutil import format_time
|
||||
|
||||
log = logging.getLogger('messenger')
|
||||
|
||||
MAX_BODY_BYTES = 64 * 1024
|
||||
MAX_DISCARD_BYTES = 1024 * 1024
|
||||
|
||||
BASIC_CHALLENGE = 'Basic realm="messenger", charset="UTF-8"'
|
||||
BEARER_CHALLENGE = 'Bearer realm="messenger"'
|
||||
|
||||
CHAT = r'/api/v1/chats/(?P<chat_id>[^/]+)'
|
||||
ROUTES = tuple((method, re.compile(pattern), handler) for method, pattern, handler in (
|
||||
('GET', r'/health/live', '_live'),
|
||||
('GET', r'/health/ready', '_ready'),
|
||||
('POST', r'/api/v1/users', '_create_user'),
|
||||
('GET', r'/api/v1/users/me', '_current_user'),
|
||||
('POST', r'/api/v1/auth/sessions', '_create_session'),
|
||||
('DELETE', r'/api/v1/auth/sessions/current', '_delete_session'),
|
||||
('POST', r'/api/v1/chats', '_create_chat'),
|
||||
('GET', r'/api/v1/chats', '_list_chats'),
|
||||
('GET', CHAT, '_get_chat'),
|
||||
('POST', CHAT + r'/messages', '_create_message'),
|
||||
('GET', CHAT + r'/messages', '_list_messages'),
|
||||
))
|
||||
|
||||
|
||||
class MessengerServer(ThreadingHTTPServer):
|
||||
"""Поток на соединение; считает выполняющиеся запросы для плавной остановки."""
|
||||
|
||||
daemon_threads = True
|
||||
request_queue_size = 128
|
||||
|
||||
def __init__(self, address, repo, sessions):
|
||||
super().__init__(address, RequestHandler)
|
||||
self.repo = repo
|
||||
self.sessions = sessions
|
||||
self._state = threading.Condition()
|
||||
self._active = 0
|
||||
self._draining = False
|
||||
|
||||
def try_begin_request(self):
|
||||
"""False, если сервер уже останавливается и запрос принимать нельзя."""
|
||||
with self._state:
|
||||
if self._draining:
|
||||
return False
|
||||
self._active += 1
|
||||
return True
|
||||
|
||||
def end_request(self):
|
||||
with self._state:
|
||||
self._active -= 1
|
||||
self._state.notify_all()
|
||||
|
||||
def begin_drain(self):
|
||||
with self._state:
|
||||
self._draining = True
|
||||
|
||||
def wait_idle(self, timeout):
|
||||
"""Ждёт завершения начатых запросов; True, если успели за timeout секунд."""
|
||||
with self._state:
|
||||
return self._state.wait_for(lambda: self._active == 0, timeout)
|
||||
|
||||
def handle_error(self, request, client_address):
|
||||
error = sys.exc_info()[0]
|
||||
level = logging.DEBUG if issubclass(error, ConnectionError) else logging.WARNING
|
||||
log.log(level, 'event=connection_error error=%s', error.__name__)
|
||||
|
||||
|
||||
class RequestHandler(BaseHTTPRequestHandler):
|
||||
protocol_version = 'HTTP/1.1'
|
||||
timeout = 15
|
||||
|
||||
def do_GET(self):
|
||||
self._handle()
|
||||
|
||||
do_POST = do_PUT = do_PATCH = do_DELETE = do_HEAD = do_OPTIONS = do_GET
|
||||
|
||||
|
||||
def _handle(self):
|
||||
started = time.monotonic()
|
||||
self.request_id = uuid.uuid4().hex
|
||||
self.user_id = '-'
|
||||
path, _, query = self.path.partition('?')
|
||||
status = 0
|
||||
accepted = self.server.try_begin_request()
|
||||
try:
|
||||
if accepted:
|
||||
status, payload, headers = self._process(path, query)
|
||||
else:
|
||||
self.close_connection = True
|
||||
status, payload, headers = self._error(ApiError(503, 'unavailable', 'Сервер завершает работу'))
|
||||
self._send(status, payload, headers)
|
||||
except OSError:
|
||||
status = 0
|
||||
self.close_connection = True
|
||||
finally:
|
||||
if accepted:
|
||||
self.server.end_request()
|
||||
log.log(
|
||||
logging.DEBUG if path.startswith('/health/') else logging.INFO,
|
||||
'request_id=%s method=%s path=%s status=%d duration_ms=%.1f user_id=%s',
|
||||
self.request_id, self.command, re.sub(r'[^\x21-\x7e]', '?', path)[:200], status,
|
||||
(time.monotonic() - started) * 1000, self.user_id,
|
||||
)
|
||||
|
||||
def _process(self, path, query):
|
||||
"""(статус, тело или None, дополнительные заголовки)."""
|
||||
try:
|
||||
body = self._read_body()
|
||||
for method, pattern, handler in ROUTES:
|
||||
match = pattern.fullmatch(path)
|
||||
if match and method == self.command:
|
||||
params = parse_qs(query, keep_blank_values=True)
|
||||
status, payload = getattr(self, handler)(body, params, **match.groupdict())
|
||||
return status, payload, ()
|
||||
raise ApiError(404, 'not_found', 'Маршрут не найден')
|
||||
except ApiError as error:
|
||||
return self._error(error)
|
||||
except Unavailable as error:
|
||||
log.warning('event=dependency_unavailable request_id=%s dependency=%s error=%s',
|
||||
self.request_id, error.dependency, type(error.__cause__).__name__)
|
||||
return self._error(ApiError(503, 'unavailable', 'Хранилище временно недоступно: ' + error.dependency))
|
||||
except Exception:
|
||||
log.exception('event=unhandled_error request_id=%s', self.request_id)
|
||||
return self._error(ApiError(500, 'internal_error', 'Внутренняя ошибка сервера'))
|
||||
|
||||
def _read_body(self):
|
||||
"""Читает тело целиком до маршрутизации, чтобы keep-alive поток не сбился."""
|
||||
header = self.headers.get('Content-Length')
|
||||
if header is None:
|
||||
if self.headers.get('Transfer-Encoding'):
|
||||
self.close_connection = True
|
||||
raise bad_request('Нужен заголовок Content-Length')
|
||||
return b''
|
||||
if not re.fullmatch(r'[0-9]{1,18}', header):
|
||||
self.close_connection = True
|
||||
raise bad_request('Некорректный Content-Length')
|
||||
length = int(header)
|
||||
if length > MAX_BODY_BYTES:
|
||||
self.close_connection = True
|
||||
if length <= MAX_DISCARD_BYTES:
|
||||
self._discard(length)
|
||||
raise ApiError(413, 'payload_too_large', 'Тело запроса больше %d байт' % MAX_BODY_BYTES)
|
||||
try:
|
||||
body = self.rfile.read(length)
|
||||
except OSError:
|
||||
body = b''
|
||||
if len(body) != length:
|
||||
self.close_connection = True
|
||||
raise bad_request('Тело запроса получено не полностью')
|
||||
return body
|
||||
|
||||
def _discard(self, remaining):
|
||||
"""Дочитывает слишком большое тело по частям, не храня его в памяти.
|
||||
|
||||
Если закрыть соединение с непрочитанными данными, клиент получит обрыв
|
||||
вместо ответа 413.
|
||||
"""
|
||||
try:
|
||||
while remaining > 0:
|
||||
chunk = self.rfile.read(min(remaining, 64 * 1024))
|
||||
if not chunk:
|
||||
break
|
||||
remaining -= len(chunk)
|
||||
except OSError:
|
||||
pass
|
||||
|
||||
def _error(self, error):
|
||||
payload = {'error': {'code': error.code, 'message': error.message, 'request_id': self.request_id}}
|
||||
headers = (('WWW-Authenticate', error.challenge),) if error.challenge else ()
|
||||
return error.status, payload, headers
|
||||
|
||||
def _send(self, status, payload, headers=()):
|
||||
"""Ответ с JSON-телом; payload=None — ответ без тела (204)."""
|
||||
body = b'' if payload is None else json.dumps(payload, ensure_ascii=False, separators=(',', ':')).encode('utf-8')
|
||||
self.send_response(status)
|
||||
if payload is not None:
|
||||
self.send_header('Content-Type', 'application/json; charset=utf-8')
|
||||
self.send_header('Content-Length', str(len(body)))
|
||||
self.send_header('X-Request-Id', self.request_id)
|
||||
for name, value in headers:
|
||||
self.send_header(name, value)
|
||||
if self.close_connection:
|
||||
self.send_header('Connection', 'close')
|
||||
self.end_headers()
|
||||
if body and self.command != 'HEAD':
|
||||
self.wfile.write(body)
|
||||
|
||||
def send_error(self, code, message=None, explain=None):
|
||||
"""Ошибки разбора HTTP (до маршрутизации) — тоже в общем формате ошибок."""
|
||||
self.request_id = uuid.uuid4().hex
|
||||
self.close_connection = True
|
||||
if code == 501:
|
||||
error = ApiError(404, 'not_found', 'Маршрут не найден')
|
||||
else:
|
||||
error = bad_request('Некорректный HTTP-запрос')
|
||||
self._send(*self._error(error))
|
||||
log.info('request_id=%s event=http_parse_error http_status=%d status=%d', self.request_id, code, error.status)
|
||||
|
||||
def version_string(self):
|
||||
return 'messenger'
|
||||
|
||||
def log_request(self, code='-', size='-'):
|
||||
pass
|
||||
|
||||
def log_message(self, format, *args):
|
||||
log.debug('event=http_server detail=%s', format % args)
|
||||
|
||||
|
||||
def _authenticate(self):
|
||||
"""user_id владельца Bearer-токена. X-User-Id и Basic здесь не принимаются.
|
||||
|
||||
Если Valkey недоступен, sessions.user_id бросает Unavailable и клиент получает 503:
|
||||
запрос не продолжается как анонимный.
|
||||
"""
|
||||
token = validation.parse_bearer(self.headers.get('Authorization'))
|
||||
user_id = self.server.sessions.user_id(token) if token else None
|
||||
if user_id is None:
|
||||
raise ApiError(401, 'unauthorized', 'Нужен действующий токен: Authorization: Bearer <token>',
|
||||
challenge=BEARER_CHALLENGE)
|
||||
self.token = token
|
||||
self.user_id = user_id
|
||||
return user_id
|
||||
|
||||
def _accessible_chat(self, user_id, raw_chat_id):
|
||||
chat_id = validation.parse_uuid(raw_chat_id)
|
||||
if chat_id is None:
|
||||
raise bad_request('chat_id должен быть UUID')
|
||||
chat = self.server.repo.get_chat(chat_id, user_id)
|
||||
if chat is None:
|
||||
raise not_found()
|
||||
return chat
|
||||
|
||||
def _json_body(self, body):
|
||||
if self.headers.get_content_type() != 'application/json':
|
||||
raise bad_request('Content-Type должен быть application/json')
|
||||
return body
|
||||
|
||||
|
||||
def _live(self, body, query):
|
||||
return 200, {'status': 'ok'}
|
||||
|
||||
def _ready(self, body, query):
|
||||
self.server.repo.ping()
|
||||
self.server.sessions.ping()
|
||||
return 200, {'status': 'ok'}
|
||||
|
||||
def _create_user(self, body, query):
|
||||
username, display_name, password = validation.parse_create_user(self._json_body(body))
|
||||
try:
|
||||
return 201, self.server.repo.create_user(username, display_name, hash_password(password))
|
||||
except UsernameTaken:
|
||||
raise ApiError(409, 'conflict', 'Пользователь с таким username уже существует')
|
||||
|
||||
def _create_session(self, body, query):
|
||||
"""Вход: только Authorization: Basic. Ответ 401 одинаковый для любой ошибки входа."""
|
||||
if body:
|
||||
raise bad_request('Тело не нужно: логин и пароль передаются в Authorization: Basic')
|
||||
credentials = validation.parse_basic(self.headers.get('Authorization'))
|
||||
user = None
|
||||
if credentials:
|
||||
username, password = credentials
|
||||
user, password_hash = self.server.repo.get_credentials(username)
|
||||
if not verify_password(password_hash, password):
|
||||
user = None
|
||||
if user is None:
|
||||
raise ApiError(401, 'unauthorized', 'Неверный логин или пароль', challenge=BASIC_CHALLENGE)
|
||||
token, expires_at = self.server.sessions.create(user['id'])
|
||||
self.user_id = user['id']
|
||||
return 201, {'token': token, 'token_type': 'Bearer', 'expires_at': format_time(expires_at), 'user': user}
|
||||
|
||||
def _delete_session(self, body, query):
|
||||
self._authenticate()
|
||||
if not self.server.sessions.revoke(self.token):
|
||||
raise ApiError(401, 'unauthorized', 'Сессия уже недействительна', challenge=BEARER_CHALLENGE)
|
||||
return 204, None
|
||||
|
||||
def _current_user(self, body, query):
|
||||
user = self.server.repo.get_user(self._authenticate())
|
||||
if user is None:
|
||||
raise ApiError(401, 'unauthorized', 'Пользователь сессии не найден', challenge=BEARER_CHALLENGE)
|
||||
return 200, user
|
||||
|
||||
def _create_chat(self, body, query):
|
||||
user_id = self._authenticate()
|
||||
title, member_ids = validation.parse_create_chat(self._json_body(body))
|
||||
if user_id not in member_ids:
|
||||
raise bad_request('member_ids должен включать вызывающего пользователя')
|
||||
try:
|
||||
return 201, self.server.repo.create_chat(title, member_ids)
|
||||
except UnknownMember:
|
||||
raise bad_request('member_ids содержит неизвестного пользователя')
|
||||
|
||||
def _list_chats(self, body, query):
|
||||
user_id = self._authenticate()
|
||||
scope = 'chats:' + user_id
|
||||
limit = validation.parse_limit(query)
|
||||
after = validation.parse_cursor(query, scope)
|
||||
items, next_key = self.server.repo.list_chats(user_id, after, limit)
|
||||
return 200, {'items': items, 'next_cursor': validation.encode_cursor(scope, next_key)}
|
||||
|
||||
def _get_chat(self, body, query, chat_id):
|
||||
return 200, self._accessible_chat(self._authenticate(), chat_id)
|
||||
|
||||
def _create_message(self, body, query, chat_id):
|
||||
user_id = self._authenticate()
|
||||
chat = self._accessible_chat(user_id, chat_id)
|
||||
text = validation.parse_create_message(self._json_body(body))
|
||||
return 201, self.server.repo.add_message(chat['id'], user_id, text)
|
||||
|
||||
def _list_messages(self, body, query, chat_id):
|
||||
chat = self._accessible_chat(self._authenticate(), chat_id)
|
||||
scope = 'messages:' + chat['id']
|
||||
limit = validation.parse_limit(query)
|
||||
after = validation.parse_cursor(query, scope)
|
||||
items, next_key = self.server.repo.list_messages(chat['id'], after, limit)
|
||||
return 200, {'items': items, 'next_cursor': validation.encode_cursor(scope, next_key)}
|
||||
@@ -0,0 +1,77 @@
|
||||
"""Сессии в Valkey — единственное хранилище сессий.
|
||||
|
||||
Ключ: session:<sha256 от токена>, значение: JSON с user_id и сроком. Срок задаётся абсолютным
|
||||
временем (PXAT), поэтому перезапуск ничего не продлевает. Сохранность при рестарте
|
||||
обеспечивает AOF с fsync на каждую запись (infra/valkey/valkey.conf).
|
||||
"""
|
||||
import hashlib
|
||||
import json
|
||||
import secrets
|
||||
import time
|
||||
from contextlib import contextmanager
|
||||
|
||||
import valkey
|
||||
from valkey.backoff import NoBackoff
|
||||
from valkey.retry import Retry
|
||||
|
||||
from .errors import Unavailable
|
||||
from .timeutil import from_micros
|
||||
|
||||
|
||||
@contextmanager
|
||||
def _valkey_errors():
|
||||
try:
|
||||
yield
|
||||
except valkey.exceptions.ValkeyError as error:
|
||||
raise Unavailable('valkey') from error
|
||||
|
||||
|
||||
class SessionStore:
|
||||
def __init__(self, url, ttl_seconds):
|
||||
self.ttl_seconds = ttl_seconds
|
||||
self._pool = valkey.BlockingConnectionPool.from_url(
|
||||
url,
|
||||
max_connections=20,
|
||||
timeout=1,
|
||||
socket_connect_timeout=1,
|
||||
socket_timeout=1,
|
||||
retry=Retry(NoBackoff(), 1),
|
||||
decode_responses=True,
|
||||
)
|
||||
self._client = valkey.Valkey(connection_pool=self._pool)
|
||||
|
||||
@staticmethod
|
||||
def _key(token):
|
||||
return 'session:' + hashlib.sha256(token.encode('ascii')).hexdigest()
|
||||
|
||||
def create(self, user_id):
|
||||
"""Новая сессия: (токен, срок действия как datetime)."""
|
||||
token = secrets.token_urlsafe(32)
|
||||
expires_ms = int(time.time() * 1000) + self.ttl_seconds * 1000
|
||||
value = json.dumps({'user_id': user_id, 'expires_at_ms': expires_ms})
|
||||
with _valkey_errors():
|
||||
self._client.set(self._key(token), value, pxat=expires_ms)
|
||||
return token, from_micros(expires_ms * 1000)
|
||||
|
||||
def user_id(self, token):
|
||||
"""user_id владельца действующей сессии или None (неизвестна, отозвана, истекла)."""
|
||||
with _valkey_errors():
|
||||
raw = self._client.get(self._key(token))
|
||||
if raw is None:
|
||||
return None
|
||||
session = json.loads(raw)
|
||||
if session['expires_at_ms'] <= time.time() * 1000:
|
||||
return None
|
||||
return session['user_id']
|
||||
|
||||
def revoke(self, token):
|
||||
"""Удаляет сессию; True, если она была."""
|
||||
with _valkey_errors():
|
||||
return self._client.delete(self._key(token)) == 1
|
||||
|
||||
def ping(self):
|
||||
with _valkey_errors():
|
||||
self._client.ping()
|
||||
|
||||
def close(self):
|
||||
self._pool.disconnect()
|
||||
@@ -0,0 +1,18 @@
|
||||
"""Время в формате контракта: RFC 3339, UTC, микросекунды, суффикс Z."""
|
||||
from datetime import datetime, timedelta, timezone
|
||||
|
||||
EPOCH = datetime(1970, 1, 1, tzinfo=timezone.utc)
|
||||
MICROSECOND = timedelta(microseconds=1)
|
||||
|
||||
|
||||
def format_time(value):
|
||||
return value.astimezone(timezone.utc).strftime('%Y-%m-%dT%H:%M:%S.%fZ')
|
||||
|
||||
|
||||
def to_micros(value):
|
||||
"""Целое число микросекунд с 1970 года: так время хранится в cursor без потери точности."""
|
||||
return (value - EPOCH) // MICROSECOND
|
||||
|
||||
|
||||
def from_micros(micros):
|
||||
return EPOCH + timedelta(microseconds=micros)
|
||||
@@ -0,0 +1,176 @@
|
||||
"""Разбор и проверка входных данных по contracts/openapi.json и contracts/README.md."""
|
||||
import base64
|
||||
import binascii
|
||||
import json
|
||||
import re
|
||||
|
||||
from .errors import bad_request
|
||||
|
||||
USERNAME = re.compile(r'[a-z0-9_]{3,32}')
|
||||
UUID = 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}')
|
||||
LIMIT = re.compile(r'[0-9]{1,3}')
|
||||
BEARER_TOKEN = re.compile(r'[A-Za-z0-9\-._~+/]{1,512}=*')
|
||||
|
||||
DEFAULT_LIMIT = 50
|
||||
MAX_LIMIT = 100
|
||||
MAX_NAME = 100
|
||||
MAX_MEMBERS = 100
|
||||
MAX_TEXT = 4000
|
||||
MIN_PASSWORD = 12
|
||||
MAX_PASSWORD = 128
|
||||
|
||||
|
||||
def parse_uuid(value):
|
||||
"""UUID в каноническом виде (нижний регистр) или None."""
|
||||
if isinstance(value, str) and UUID.fullmatch(value):
|
||||
return value.lower()
|
||||
return None
|
||||
|
||||
|
||||
def _load_object(raw, fields):
|
||||
"""JSON-объект ровно с полями fields: лишние и отсутствующие поля — ошибка."""
|
||||
try:
|
||||
data = json.loads(raw.decode('utf-8'))
|
||||
except (ValueError, RecursionError):
|
||||
raise bad_request('Тело запроса должно быть корректным JSON в UTF-8')
|
||||
if not isinstance(data, dict):
|
||||
raise bad_request('Тело запроса должно быть JSON-объектом')
|
||||
for field in fields:
|
||||
if field not in data:
|
||||
raise bad_request('Отсутствует обязательное поле ' + field)
|
||||
if len(data) != len(fields):
|
||||
raise bad_request('В запросе есть лишние поля')
|
||||
return data
|
||||
|
||||
|
||||
def _string(data, field, max_length, min_length=1):
|
||||
"""Строка из min_length..max_length code points, не только из пробельных символов."""
|
||||
value = data[field]
|
||||
if not isinstance(value, str):
|
||||
raise bad_request('Поле %s должно быть строкой' % field)
|
||||
if not value.strip():
|
||||
raise bad_request('Поле %s не должно быть пустым' % field)
|
||||
if not min_length <= len(value) <= max_length:
|
||||
raise bad_request('Поле %s: от %d до %d символов' % (field, min_length, max_length))
|
||||
if '\x00' in value:
|
||||
raise bad_request('Поле %s содержит недопустимые символы' % field)
|
||||
try:
|
||||
value.encode('utf-8')
|
||||
except UnicodeEncodeError:
|
||||
raise bad_request('Поле %s содержит недопустимые символы' % field)
|
||||
return value
|
||||
|
||||
|
||||
def parse_create_user(raw):
|
||||
data = _load_object(raw, ('username', 'display_name', 'password'))
|
||||
username = data['username']
|
||||
if not isinstance(username, str) or not USERNAME.fullmatch(username):
|
||||
raise bad_request('username: 3-32 символа из набора a-z, 0-9 и _')
|
||||
display_name = _string(data, 'display_name', MAX_NAME)
|
||||
password = _string(data, 'password', MAX_PASSWORD, MIN_PASSWORD)
|
||||
return username, display_name, password
|
||||
|
||||
|
||||
def parse_create_chat(raw):
|
||||
data = _load_object(raw, ('title', 'member_ids'))
|
||||
title = _string(data, 'title', MAX_NAME)
|
||||
raw_ids = data['member_ids']
|
||||
if not isinstance(raw_ids, list) or not 1 <= len(raw_ids) <= MAX_MEMBERS:
|
||||
raise bad_request('member_ids: список из 1-%d UUID' % MAX_MEMBERS)
|
||||
member_ids = [parse_uuid(item) for item in raw_ids]
|
||||
if None in member_ids:
|
||||
raise bad_request('member_ids должен содержать только UUID')
|
||||
if len(set(member_ids)) != len(member_ids):
|
||||
raise bad_request('member_ids не должен содержать повторов')
|
||||
return title, member_ids
|
||||
|
||||
|
||||
def parse_create_message(raw):
|
||||
return _string(_load_object(raw, ('text',)), 'text', MAX_TEXT)
|
||||
|
||||
|
||||
def _credentials(header, scheme):
|
||||
"""Значение после схемы авторизации (схема без учёта регистра) или None."""
|
||||
if not header:
|
||||
return None
|
||||
name, _, value = header.strip().partition(' ')
|
||||
if name.lower() != scheme:
|
||||
return None
|
||||
return value.strip()
|
||||
|
||||
|
||||
def parse_basic(header):
|
||||
"""(username, password) из Authorization: Basic base64(username:password) или None.
|
||||
|
||||
Пароль в UTF-8 и может содержать двоеточие: делим по первому. Всё, что не может быть
|
||||
правильной парой логин/пароль, возвращает None — дальше это обычный ответ 401.
|
||||
"""
|
||||
value = _credentials(header, 'basic')
|
||||
if not value:
|
||||
return None
|
||||
try:
|
||||
decoded = base64.b64decode(value, validate=True).decode('utf-8')
|
||||
except (binascii.Error, ValueError):
|
||||
return None
|
||||
username, separator, password = decoded.partition(':')
|
||||
if not separator or not USERNAME.fullmatch(username):
|
||||
return None
|
||||
if not MIN_PASSWORD <= len(password) <= MAX_PASSWORD:
|
||||
return None
|
||||
return username, password
|
||||
|
||||
|
||||
def parse_bearer(header):
|
||||
"""Токен из Authorization: Bearer <token> или None."""
|
||||
value = _credentials(header, 'bearer')
|
||||
if not value or not BEARER_TOKEN.fullmatch(value):
|
||||
return None
|
||||
return value
|
||||
|
||||
|
||||
def _single(query, name):
|
||||
"""Значение параметра запроса; None, если параметра нет."""
|
||||
values = query.get(name)
|
||||
if values is None:
|
||||
return None
|
||||
if len(values) != 1:
|
||||
raise bad_request('Параметр %s указан несколько раз' % name)
|
||||
return values[0]
|
||||
|
||||
|
||||
def parse_limit(query):
|
||||
value = _single(query, 'limit')
|
||||
if value is None:
|
||||
return DEFAULT_LIMIT
|
||||
if not LIMIT.fullmatch(value) or not 1 <= int(value) <= MAX_LIMIT:
|
||||
raise bad_request('limit должен быть целым числом от 1 до %d' % MAX_LIMIT)
|
||||
return int(value)
|
||||
|
||||
|
||||
def encode_cursor(scope, key):
|
||||
"""Непрозрачный для клиента cursor: область действия и ключ (created_at, id) последней записи."""
|
||||
if key is None:
|
||||
return None
|
||||
raw = '%s|%d|%s' % (scope, key[0], key[1])
|
||||
return base64.urlsafe_b64encode(raw.encode('ascii')).decode('ascii').rstrip('=')
|
||||
|
||||
|
||||
def parse_cursor(query, scope):
|
||||
"""Ключ (created_at в мкс, id), после которого продолжать; None без cursor.
|
||||
|
||||
scope привязывает cursor к ресурсу: cursor от другого чата — 400.
|
||||
"""
|
||||
value = _single(query, 'cursor')
|
||||
if value is None:
|
||||
return None
|
||||
try:
|
||||
padded = value + '=' * (-len(value) % 4)
|
||||
raw = base64.b64decode(padded, altchars=b'-_', validate=True).decode('ascii')
|
||||
cursor_scope, micros, entry_id = raw.split('|')
|
||||
if cursor_scope != scope or not micros.isdigit() or parse_uuid(entry_id) != entry_id:
|
||||
raise ValueError
|
||||
if len(micros) > 17:
|
||||
raise ValueError
|
||||
return int(micros), entry_id
|
||||
except ValueError:
|
||||
raise bad_request('Некорректный cursor')
|
||||
@@ -0,0 +1,127 @@
|
||||
"""Небольшой HTTP-клиент для собственных тестов. Только стандартная библиотека, Python 3.9+.
|
||||
|
||||
Стенд должен быть уже запущен. Адрес берётся из переменной окружения BASE_URL.
|
||||
По умолчанию 127.0.0.1, а не localhost: в Windows localhost сначала пробует IPv6
|
||||
и каждый запрос к IPv4-порту занимает около двух секунд.
|
||||
"""
|
||||
import base64
|
||||
import json
|
||||
import os
|
||||
import urllib.error
|
||||
import urllib.request
|
||||
import uuid
|
||||
from datetime import datetime
|
||||
from urllib.parse import quote
|
||||
|
||||
BASE_URL = os.environ.get('BASE_URL', 'http://127.0.0.1:8080').rstrip('/')
|
||||
TIMEOUT = 10
|
||||
USER_FIELDS = ('id', 'username', 'display_name', 'created_at')
|
||||
|
||||
|
||||
class Response:
|
||||
def __init__(self, status, headers, body):
|
||||
self.status = status
|
||||
self.headers = headers
|
||||
self.body = body
|
||||
self.json = json.loads(body) if body else None
|
||||
|
||||
|
||||
def request(method, path, user=None, body=None, raw=None, headers=None, base_url=None):
|
||||
"""Один запрос. user — словарь с token (Bearer); body — объект для JSON; raw — готовые байты."""
|
||||
all_headers = {'Accept': 'application/json'}
|
||||
if user is not None:
|
||||
all_headers['Authorization'] = 'Bearer ' + user['token']
|
||||
if body is not None:
|
||||
raw = json.dumps(body, ensure_ascii=False).encode('utf-8')
|
||||
if raw is not None:
|
||||
all_headers['Content-Type'] = 'application/json'
|
||||
all_headers.update(headers or {})
|
||||
req = urllib.request.Request((base_url or BASE_URL) + path, data=raw, headers=all_headers, method=method)
|
||||
try:
|
||||
response = urllib.request.urlopen(req, timeout=TIMEOUT)
|
||||
except urllib.error.HTTPError as error:
|
||||
response = error
|
||||
with response:
|
||||
result = Response(response.code, response.headers, response.read())
|
||||
assert result.headers.get('X-Request-Id'), 'no X-Request-Id in response'
|
||||
if result.status == 204:
|
||||
assert not result.body, '204 must not have a body'
|
||||
return result
|
||||
assert result.headers.get_content_type() == 'application/json', 'response is not JSON'
|
||||
if result.status >= 400:
|
||||
error = result.json['error']
|
||||
assert set(error) == {'code', 'message', 'request_id'}, 'unexpected error fields'
|
||||
assert error['message'], 'empty error message'
|
||||
assert error['request_id'] == result.headers['X-Request-Id'], 'request_id differs from header'
|
||||
return result
|
||||
|
||||
|
||||
def basic(username, password):
|
||||
"""Значение заголовка Authorization: Basic base64(username:password) в UTF-8."""
|
||||
return 'Basic ' + base64.b64encode((username + ':' + password).encode('utf-8')).decode('ascii')
|
||||
|
||||
|
||||
def unique_username(prefix='t'):
|
||||
return prefix + '_' + uuid.uuid4().hex[:16]
|
||||
|
||||
|
||||
def new_password():
|
||||
return 'Пароль:' + uuid.uuid4().hex
|
||||
|
||||
|
||||
def register(username=None, password=None, display_name='Тестовый пользователь'):
|
||||
body = {'username': username or unique_username(), 'display_name': display_name, 'password': password or new_password()}
|
||||
return request('POST', '/api/v1/users', body=body), body
|
||||
|
||||
|
||||
def login(username, password, base_url=None):
|
||||
return request('POST', '/api/v1/auth/sessions', headers={'Authorization': basic(username, password)},
|
||||
base_url=base_url)
|
||||
|
||||
|
||||
def new_user(display_name='Тестовый пользователь', base_url=None):
|
||||
"""Зарегистрированный пользователь с действующим токеном: поля User + token + password."""
|
||||
response, body = register(display_name=display_name)
|
||||
assert response.status == 201, 'user was not created: %d' % response.status
|
||||
session = login(body['username'], body['password'], base_url=base_url)
|
||||
assert session.status == 201, 'login failed: %d' % session.status
|
||||
return dict(response.json, token=session.json['token'], password=body['password'])
|
||||
|
||||
|
||||
def public(user):
|
||||
"""Только поля схемы User: так пользователь выглядит в ответах API."""
|
||||
return {field: user[field] for field in USER_FIELDS}
|
||||
|
||||
|
||||
def new_chat(owner, members, title='Тестовый чат'):
|
||||
body = {'title': title, 'member_ids': [member['id'] for member in members]}
|
||||
response = request('POST', '/api/v1/chats', user=owner, body=body)
|
||||
assert response.status == 201, 'chat was not created: %d' % response.status
|
||||
return response.json
|
||||
|
||||
|
||||
def send(user, chat, text):
|
||||
return request('POST', '/api/v1/chats/%s/messages' % chat['id'], user=user, body={'text': text})
|
||||
|
||||
|
||||
def pages(path, user, limit):
|
||||
"""Обходит все страницы списка и возвращает их как список списков."""
|
||||
result = []
|
||||
cursor = None
|
||||
while True:
|
||||
url = '%s?limit=%d' % (path, limit)
|
||||
if cursor:
|
||||
url += '&cursor=' + quote(cursor, safe='')
|
||||
response = request('GET', url, user=user)
|
||||
assert response.status == 200, 'page was not read: %d' % response.status
|
||||
result.append(response.json['items'])
|
||||
cursor = response.json['next_cursor']
|
||||
if cursor is None:
|
||||
return result
|
||||
assert len(result) < 1000, 'pagination does not finish'
|
||||
|
||||
|
||||
def order_key(item):
|
||||
"""Порядок из контракта: (created_at, id) по возрастанию."""
|
||||
created_at = datetime.fromisoformat(item['created_at'][:-1] + '+00:00')
|
||||
return created_at, uuid.UUID(item['id']).int
|
||||
@@ -0,0 +1,88 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Проверка защиты от гонок: N клиентов одновременно отправляют сообщения в один чат.
|
||||
|
||||
python student-tests/race.py --output evidence/race.json
|
||||
|
||||
Каждый клиент — отдельный пользователь и отдельный поток. Все потоки стартуют
|
||||
одновременно, затем история читается постранично и сравнивается с отправленным.
|
||||
Код возврата 0 — в истории ровно столько уникальных сообщений, сколько отправлено.
|
||||
"""
|
||||
import argparse
|
||||
import json
|
||||
import sys
|
||||
import threading
|
||||
import time
|
||||
from collections import Counter
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
|
||||
import api
|
||||
|
||||
|
||||
def run_race(clients=10, per_client=10, page_size=7):
|
||||
users = [api.new_user('Клиент %d' % n) for n in range(clients)]
|
||||
chat = api.new_chat(users[0], users, title='Проверка гонок')
|
||||
start = threading.Barrier(clients)
|
||||
|
||||
def client(index):
|
||||
start.wait()
|
||||
return [api.send(users[index], chat, 'race client=%d message=%d' % (index, n)) for n in range(per_client)]
|
||||
|
||||
started = time.monotonic()
|
||||
with ThreadPoolExecutor(max_workers=clients) as pool:
|
||||
responses = [response for batch in pool.map(client, range(clients)) for response in batch]
|
||||
elapsed = time.monotonic() - started
|
||||
|
||||
accepted = [response.json for response in responses if response.status == 201]
|
||||
pages = api.pages('/api/v1/chats/%s/messages' % chat['id'], users[-1], page_size)
|
||||
history = [message for page in pages for message in page]
|
||||
per_sender = Counter(message['sender_id'] for message in history)
|
||||
expected = clients * per_client
|
||||
summary = {
|
||||
'base_url': api.BASE_URL,
|
||||
'clients': clients,
|
||||
'messages_per_client': per_client,
|
||||
'sent': len(responses),
|
||||
'accepted_201': len(accepted),
|
||||
'history_total': len(history),
|
||||
'history_unique_ids': len({message['id'] for message in history}),
|
||||
'history_unique_texts': len({message['text'] for message in history}),
|
||||
'history_matches_accepted': sorted(m['id'] for m in history) == sorted(m['id'] for m in accepted),
|
||||
'senders_in_history': len(per_sender),
|
||||
'min_messages_per_sender': min(per_sender.values(), default=0),
|
||||
'max_messages_per_sender': max(per_sender.values(), default=0),
|
||||
'ordered_by_created_at_and_id': history == sorted(history, key=api.order_key),
|
||||
'pages_read': len(pages),
|
||||
'page_size': page_size,
|
||||
'send_elapsed_seconds': round(elapsed, 3),
|
||||
}
|
||||
summary['ok'] = (
|
||||
summary['accepted_201'] == expected
|
||||
and summary['history_total'] == expected
|
||||
and summary['history_unique_ids'] == expected
|
||||
and summary['history_unique_texts'] == expected
|
||||
and summary['history_matches_accepted']
|
||||
and summary['senders_in_history'] == clients
|
||||
and summary['min_messages_per_sender'] == summary['max_messages_per_sender'] == per_client
|
||||
and summary['ordered_by_created_at_and_id']
|
||||
)
|
||||
return summary
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description='Concurrent message race check; creates synthetic users and messages')
|
||||
parser.add_argument('--clients', type=int, default=10)
|
||||
parser.add_argument('--per-client', type=int, default=10)
|
||||
parser.add_argument('--output', default=None, help='Optional JSON file for the summary')
|
||||
args = parser.parse_args()
|
||||
summary = run_race(args.clients, args.per_client)
|
||||
text = json.dumps(summary, indent=2)
|
||||
if args.output:
|
||||
with open(args.output, 'w', encoding='utf-8') as file:
|
||||
file.write(text + '\n')
|
||||
print(text)
|
||||
print('PASS' if summary['ok'] else 'FAIL')
|
||||
return 0 if summary['ok'] else 1
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
sys.exit(main())
|
||||
@@ -0,0 +1,84 @@
|
||||
"""Доступ к чатам: аутентификация (кто ты) отдельно от доступа к конкретному чату (можно ли тебе сюда)."""
|
||||
import unittest
|
||||
import uuid
|
||||
|
||||
import api
|
||||
|
||||
|
||||
class AuthenticationOrderTest(unittest.TestCase):
|
||||
def test_authentication_is_checked_before_body_and_resource(self):
|
||||
self.assertEqual(api.request('POST', '/api/v1/chats', raw=b'{broken').status, 401)
|
||||
owner = api.new_user()
|
||||
chat = api.new_chat(owner, [owner])
|
||||
path = '/api/v1/chats/%s/messages' % chat['id']
|
||||
self.assertEqual(api.request('GET', path).status, 401)
|
||||
self.assertEqual(api.request('POST', path, raw=b'{broken').status, 401)
|
||||
|
||||
def test_registration_does_not_need_a_token(self):
|
||||
response, _ = api.register()
|
||||
self.assertEqual(response.status, 201)
|
||||
|
||||
|
||||
class ForeignChatTest(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
cls.alice, cls.bob, cls.eve = api.new_user('Алиса'), api.new_user('Боб'), api.new_user('Ева')
|
||||
cls.chat = api.new_chat(cls.alice, [cls.alice, cls.bob])
|
||||
cls.path = '/api/v1/chats/' + cls.chat['id']
|
||||
assert api.send(cls.alice, cls.chat, 'секрет участников').status == 201
|
||||
|
||||
def assert_not_found(self, response):
|
||||
self.assertEqual(response.status, 404)
|
||||
self.assertEqual(response.json['error']['code'], 'not_found')
|
||||
|
||||
def test_outsider_cannot_get_chat(self):
|
||||
self.assert_not_found(api.request('GET', self.path, user=self.eve))
|
||||
|
||||
def test_outsider_cannot_read_history(self):
|
||||
self.assert_not_found(api.request('GET', self.path + '/messages', user=self.eve))
|
||||
|
||||
def test_outsider_cannot_write(self):
|
||||
self.assert_not_found(api.send(self.eve, self.chat, 'чужое сообщение'))
|
||||
self.assert_not_found(api.request('POST', self.path + '/messages', user=self.eve, body={'text': ''}))
|
||||
history = [m for page in api.pages(self.path + '/messages', self.alice, 100) for m in page]
|
||||
self.assertNotIn('чужое сообщение', [m['text'] for m in history])
|
||||
self.assertNotIn(self.eve['id'], [m['sender_id'] for m in history])
|
||||
|
||||
def test_foreign_chat_looks_like_missing_chat(self):
|
||||
foreign = api.request('GET', self.path, user=self.eve)
|
||||
missing = api.request('GET', '/api/v1/chats/' + str(uuid.uuid4()), user=self.eve)
|
||||
self.assert_not_found(missing)
|
||||
self.assertEqual(foreign.json['error']['code'], missing.json['error']['code'])
|
||||
self.assertEqual(foreign.json['error']['message'], missing.json['error']['message'])
|
||||
|
||||
def test_chat_list_contains_only_own_chats(self):
|
||||
def chat_ids(user):
|
||||
return [chat['id'] for page in api.pages('/api/v1/chats', user, 100) for chat in page]
|
||||
|
||||
self.assertIn(self.chat['id'], chat_ids(self.alice))
|
||||
self.assertIn(self.chat['id'], chat_ids(self.bob))
|
||||
self.assertEqual(chat_ids(self.eve), [])
|
||||
|
||||
def test_every_member_can_read_and_write(self):
|
||||
self.assertEqual(api.request('GET', self.path, user=self.bob).json, self.chat)
|
||||
sent = api.send(self.bob, self.chat, 'ответ Боба')
|
||||
self.assertEqual(sent.status, 201)
|
||||
self.assertEqual(sent.json['sender_id'], self.bob['id'])
|
||||
|
||||
def test_x_user_id_does_not_open_a_foreign_chat(self):
|
||||
response = api.request('GET', self.path, user=self.eve, headers={'X-User-Id': self.alice['id']})
|
||||
self.assert_not_found(response)
|
||||
|
||||
def test_invalid_chat_id_is_400(self):
|
||||
for suffix in ('', '/messages'):
|
||||
with self.subTest(suffix=suffix):
|
||||
response = api.request('GET', '/api/v1/chats/not-a-uuid' + suffix, user=self.alice)
|
||||
self.assertEqual(response.status, 400)
|
||||
|
||||
def test_unknown_route_is_404(self):
|
||||
self.assert_not_found(api.request('GET', '/api/v1/unknown', user=self.alice))
|
||||
self.assert_not_found(api.request('DELETE', self.path, user=self.alice))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
@@ -0,0 +1,59 @@
|
||||
"""Параллельные запросы не должны терять, дублировать или портить данные."""
|
||||
import threading
|
||||
import unittest
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
|
||||
import api
|
||||
from race import run_race
|
||||
|
||||
|
||||
class ConcurrencyTest(unittest.TestCase):
|
||||
def test_100_messages_from_10_clients(self):
|
||||
summary = run_race(clients=10, per_client=10)
|
||||
self.assertEqual(summary['accepted_201'], 100)
|
||||
self.assertEqual(summary['history_total'], 100)
|
||||
self.assertEqual(summary['history_unique_ids'], 100)
|
||||
self.assertEqual(summary['history_unique_texts'], 100)
|
||||
self.assertTrue(summary['history_matches_accepted'])
|
||||
self.assertEqual(summary['senders_in_history'], 10)
|
||||
self.assertEqual((summary['min_messages_per_sender'], summary['max_messages_per_sender']), (10, 10))
|
||||
self.assertTrue(summary['ordered_by_created_at_and_id'])
|
||||
|
||||
def test_same_username_registered_in_parallel_wins_once(self):
|
||||
attempts = 20
|
||||
username = api.unique_username()
|
||||
passwords = ['Гонка-пароль-%02d' % n for n in range(attempts)]
|
||||
start = threading.Barrier(attempts)
|
||||
|
||||
def register(index):
|
||||
start.wait()
|
||||
body = {'username': username, 'display_name': 'Гонка %d' % index, 'password': passwords[index]}
|
||||
return api.request('POST', '/api/v1/users', body=body)
|
||||
|
||||
with ThreadPoolExecutor(max_workers=attempts) as pool:
|
||||
responses = list(pool.map(register, range(attempts)))
|
||||
statuses = sorted(response.status for response in responses)
|
||||
self.assertEqual(statuses, [201] + [409] * (attempts - 1))
|
||||
winner = next(index for index, response in enumerate(responses) if response.status == 201)
|
||||
logins = [api.login(username, password).status for password in passwords]
|
||||
self.assertEqual(logins, [201 if index == winner else 401 for index in range(attempts)])
|
||||
|
||||
def test_chats_created_in_parallel_are_all_listed(self):
|
||||
user = api.new_user()
|
||||
count = 30
|
||||
start = threading.Barrier(count)
|
||||
|
||||
def create(index):
|
||||
start.wait()
|
||||
return api.new_chat(user, [user], title='Параллельный чат %d' % index)
|
||||
|
||||
with ThreadPoolExecutor(max_workers=count) as pool:
|
||||
created = list(pool.map(create, range(count)))
|
||||
listed = [chat for page in api.pages('/api/v1/chats', user, 7) for chat in page]
|
||||
self.assertEqual(len(listed), count)
|
||||
self.assertEqual({chat['id'] for chat in listed}, {chat['id'] for chat in created})
|
||||
self.assertEqual(listed, sorted(listed, key=api.order_key))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
@@ -0,0 +1,123 @@
|
||||
"""Keyset-пагинация истории сообщений и списка чатов."""
|
||||
import unittest
|
||||
from urllib.parse import quote
|
||||
|
||||
import api
|
||||
|
||||
|
||||
class MessagePaginationTest(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
cls.user = api.new_user()
|
||||
cls.chat = api.new_chat(cls.user, [cls.user])
|
||||
cls.path = '/api/v1/chats/%s/messages' % cls.chat['id']
|
||||
cls.sent = [api.send(cls.user, cls.chat, 'сообщение %d' % n).json for n in range(25)]
|
||||
|
||||
def get(self, query):
|
||||
return api.request('GET', self.path + query, user=self.user)
|
||||
|
||||
def test_walk_over_several_pages(self):
|
||||
pages = api.pages(self.path, self.user, 10)
|
||||
self.assertEqual([len(page) for page in pages], [10, 10, 5])
|
||||
history = [message for page in pages for message in page]
|
||||
self.assertEqual(history, self.sent)
|
||||
self.assertEqual(history, sorted(history, key=api.order_key))
|
||||
self.assertEqual(len({message['id'] for message in history}), 25)
|
||||
|
||||
def test_one_message_per_page(self):
|
||||
pages = api.pages(self.path, self.user, 1)
|
||||
self.assertEqual(len(pages), 25)
|
||||
self.assertEqual([page[0] for page in pages], self.sent)
|
||||
|
||||
def test_last_full_page_has_no_cursor(self):
|
||||
pages = api.pages(self.path, self.user, 5)
|
||||
self.assertEqual([len(page) for page in pages], [5, 5, 5, 5, 5])
|
||||
|
||||
def test_default_and_maximum_limit(self):
|
||||
self.assertEqual(len(self.get('').json['items']), 25)
|
||||
everything = self.get('?limit=100').json
|
||||
self.assertEqual(everything['items'], self.sent)
|
||||
self.assertIsNone(everything['next_cursor'])
|
||||
|
||||
def test_limit_can_change_between_pages(self):
|
||||
first = self.get('?limit=3').json
|
||||
second = self.get('?limit=20&cursor=' + quote(first['next_cursor'], safe='')).json
|
||||
self.assertEqual(first['items'] + second['items'], self.sent[:23])
|
||||
self.assertIsNotNone(second['next_cursor'])
|
||||
|
||||
def test_invalid_limit_is_400(self):
|
||||
for limit in ('0', '101', '-1', '1.5', 'abc', '', '1e2', '٣'):
|
||||
with self.subTest(limit=limit):
|
||||
response = self.get('?limit=' + quote(limit))
|
||||
self.assertEqual(response.status, 400)
|
||||
self.assertEqual(response.json['error']['code'], 'invalid_request')
|
||||
|
||||
def test_invalid_cursor_is_400(self):
|
||||
for cursor in ('garbage', '', '!!!', 'AAAA', quote('курсор')):
|
||||
with self.subTest(cursor=cursor):
|
||||
self.assertEqual(self.get('?cursor=' + cursor).status, 400)
|
||||
|
||||
def test_cursor_from_another_chat_is_400(self):
|
||||
other = api.new_chat(self.user, [self.user], title='Другой чат')
|
||||
for n in range(3):
|
||||
api.send(self.user, other, 'в другом чате %d' % n)
|
||||
other_cursor = api.request('GET', '/api/v1/chats/%s/messages?limit=1' % other['id'], user=self.user).json['next_cursor']
|
||||
self.assertIsNotNone(other_cursor)
|
||||
self.assertEqual(self.get('?cursor=' + quote(other_cursor, safe='')).status, 400)
|
||||
chats_cursor = api.request('GET', '/api/v1/chats?limit=1', user=self.user).json['next_cursor']
|
||||
self.assertEqual(self.get('?cursor=' + quote(chats_cursor, safe='')).status, 400)
|
||||
|
||||
|
||||
class DefaultLimitTest(unittest.TestCase):
|
||||
def test_default_limit_is_50(self):
|
||||
user = api.new_user()
|
||||
chat = api.new_chat(user, [user])
|
||||
path = '/api/v1/chats/%s/messages' % chat['id']
|
||||
for n in range(55):
|
||||
api.send(user, chat, 'сообщение %d' % n)
|
||||
first = api.request('GET', path, user=user).json
|
||||
self.assertEqual(len(first['items']), 50)
|
||||
rest = api.request('GET', path + '?cursor=' + quote(first['next_cursor'], safe=''), user=user).json
|
||||
self.assertEqual(len(rest['items']), 5)
|
||||
self.assertIsNone(rest['next_cursor'])
|
||||
|
||||
|
||||
class GrowingHistoryTest(unittest.TestCase):
|
||||
def test_empty_history(self):
|
||||
user = api.new_user()
|
||||
chat = api.new_chat(user, [user])
|
||||
page = api.request('GET', '/api/v1/chats/%s/messages' % chat['id'], user=user).json
|
||||
self.assertEqual(page, {'items': [], 'next_cursor': None})
|
||||
|
||||
def test_messages_sent_during_walk_are_not_lost(self):
|
||||
alice, bob = api.new_user(), api.new_user()
|
||||
chat = api.new_chat(alice, [alice, bob])
|
||||
path = '/api/v1/chats/%s/messages' % chat['id']
|
||||
before = [api.send(alice, chat, 'до %d' % n).json for n in range(4)]
|
||||
first = api.request('GET', path + '?limit=2', user=bob).json
|
||||
after = [api.send(alice, chat, 'после %d' % n).json for n in range(3)]
|
||||
rest = api.request('GET', path + '?limit=100&cursor=' + quote(first['next_cursor'], safe=''), user=alice).json
|
||||
self.assertEqual(first['items'] + rest['items'], before + after)
|
||||
|
||||
|
||||
class ChatListPaginationTest(unittest.TestCase):
|
||||
def test_chats_are_paged_in_creation_order(self):
|
||||
user = api.new_user()
|
||||
created = [api.new_chat(user, [user], title='Чат %d' % n) for n in range(5)]
|
||||
pages = api.pages('/api/v1/chats', user, 2)
|
||||
self.assertEqual([len(page) for page in pages], [2, 2, 1])
|
||||
chats = [chat for page in pages for chat in page]
|
||||
self.assertEqual(chats, created)
|
||||
self.assertEqual(chats, sorted(chats, key=api.order_key))
|
||||
|
||||
def test_cursor_of_another_user_is_400(self):
|
||||
alice, bob = api.new_user(), api.new_user()
|
||||
for n in range(2):
|
||||
api.new_chat(alice, [alice, bob], title='Общий %d' % n)
|
||||
alice_cursor = api.request('GET', '/api/v1/chats?limit=1', user=alice).json['next_cursor']
|
||||
response = api.request('GET', '/api/v1/chats?limit=1&cursor=' + quote(alice_cursor, safe=''), user=bob)
|
||||
self.assertEqual(response.status, 400)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
@@ -0,0 +1,155 @@
|
||||
"""Вход по Basic, Bearer-сессии, выход. X-User-Id из lab1 больше не даёт доступа."""
|
||||
import base64
|
||||
import unittest
|
||||
import uuid
|
||||
from datetime import datetime, timedelta, timezone
|
||||
|
||||
import api
|
||||
|
||||
ME = '/api/v1/users/me'
|
||||
SESSIONS = '/api/v1/auth/sessions'
|
||||
|
||||
|
||||
def b64(raw):
|
||||
return base64.b64encode(raw).decode('ascii')
|
||||
|
||||
|
||||
class LoginTest(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
response, cls.registration = api.register(display_name='Вход')
|
||||
assert response.status == 201
|
||||
cls.user = response.json
|
||||
|
||||
def assert_login_rejected(self, response):
|
||||
self.assertEqual(response.status, 401)
|
||||
self.assertEqual(response.json['error']['code'], 'unauthorized')
|
||||
self.assertEqual(response.headers['WWW-Authenticate'], 'Basic realm="messenger", charset="UTF-8"')
|
||||
return response.json['error']['message']
|
||||
|
||||
def test_successful_login(self):
|
||||
response = api.login(self.registration['username'], self.registration['password'])
|
||||
self.assertEqual(response.status, 201)
|
||||
session = response.json
|
||||
self.assertEqual(set(session), {'token', 'token_type', 'expires_at', 'user'})
|
||||
self.assertEqual(session['token_type'], 'Bearer')
|
||||
self.assertEqual(session['user'], self.user)
|
||||
self.assertGreaterEqual(len(session['token']), 43)
|
||||
self.assertNotIn(self.user['id'], session['token'])
|
||||
self.assertNotIn(self.user['username'], session['token'])
|
||||
expires_at = datetime.fromisoformat(session['expires_at'][:-1] + '+00:00')
|
||||
self.assertGreater(expires_at, datetime.now(timezone.utc) + timedelta(seconds=60))
|
||||
me = api.request('GET', ME, headers={'Authorization': 'Bearer ' + session['token']})
|
||||
self.assertEqual(me.json, self.user)
|
||||
|
||||
def test_each_login_gives_a_new_token(self):
|
||||
first = api.login(self.registration['username'], self.registration['password']).json['token']
|
||||
second = api.login(self.registration['username'], self.registration['password']).json['token']
|
||||
self.assertNotEqual(first, second)
|
||||
for token in (first, second):
|
||||
self.assertEqual(api.request('GET', ME, headers={'Authorization': 'Bearer ' + token}).status, 200)
|
||||
|
||||
def test_registration_never_returns_password(self):
|
||||
response, _ = api.register()
|
||||
self.assertEqual(set(response.json), {'id', 'username', 'display_name', 'created_at'})
|
||||
|
||||
def test_wrong_password(self):
|
||||
self.assert_login_rejected(api.login(self.registration['username'], self.registration['password'] + 'x'))
|
||||
|
||||
def test_unknown_user_gets_the_same_answer(self):
|
||||
wrong_password = self.assert_login_rejected(api.login(self.registration['username'], 'Wrong-password-123'))
|
||||
unknown_user = self.assert_login_rejected(api.login(api.unique_username(), 'Wrong-password-123'))
|
||||
self.assertEqual(wrong_password, unknown_user)
|
||||
|
||||
def test_malformed_basic_header(self):
|
||||
for value in (
|
||||
'Basic',
|
||||
'Basic !!!not-base64!!!',
|
||||
'Basic ' + b64(b'no-colon-here'),
|
||||
'Basic ' + b64(b'\xff\xfe:invalid-utf8-password'),
|
||||
'Basic ' + b64(b'Upper_Case:Valid-password-123'),
|
||||
'Basic ' + b64(b'short:pw'),
|
||||
'Digest abc',
|
||||
'Bearer ' + uuid.uuid4().hex,
|
||||
):
|
||||
with self.subTest(value=value):
|
||||
response = api.request('POST', SESSIONS, headers={'Authorization': value})
|
||||
self.assert_login_rejected(response)
|
||||
self.assert_login_rejected(api.request('POST', SESSIONS))
|
||||
|
||||
def test_password_with_colons_and_unicode(self):
|
||||
password = 'Пароль:с:двоеточиями 👋'
|
||||
response, body = api.register(password=password)
|
||||
self.assertEqual(response.status, 201)
|
||||
self.assertEqual(api.login(body['username'], password).status, 201)
|
||||
self.assert_login_rejected(api.login(body['username'], 'Пароль:с:двоеточиями'))
|
||||
|
||||
def test_credentials_in_json_body_are_not_accepted(self):
|
||||
body = {'username': self.registration['username'], 'password': self.registration['password']}
|
||||
response = api.request('POST', SESSIONS, body=body)
|
||||
self.assertIn(response.status, (400, 401))
|
||||
self.assertNotIn('token', response.json)
|
||||
|
||||
|
||||
class BearerTest(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
cls.alice = api.new_user('Алиса')
|
||||
cls.bob = api.new_user('Боб')
|
||||
|
||||
def assert_bearer_rejected(self, response):
|
||||
self.assertEqual(response.status, 401)
|
||||
self.assertEqual(response.json['error']['code'], 'unauthorized')
|
||||
self.assertEqual(response.headers['WWW-Authenticate'], 'Bearer realm="messenger"')
|
||||
|
||||
def test_protected_endpoints_need_a_token(self):
|
||||
for method, path in (('GET', ME), ('GET', '/api/v1/chats'), ('POST', '/api/v1/chats'),
|
||||
('GET', '/api/v1/chats/%s/messages' % uuid.uuid4()), ('DELETE', SESSIONS + '/current')):
|
||||
with self.subTest(method=method, path=path):
|
||||
self.assert_bearer_rejected(api.request(method, path))
|
||||
|
||||
def test_unknown_or_malformed_token(self):
|
||||
for value in ('Bearer ' + uuid.uuid4().hex * 2, 'Bearer', 'Bearer a b', 'Bearer ' + 'x' * 600,
|
||||
'Bearer ' + self.alice['id'], 'Token ' + self.alice['token']):
|
||||
with self.subTest(value=value[:40]):
|
||||
self.assert_bearer_rejected(api.request('GET', ME, headers={'Authorization': value}))
|
||||
|
||||
def test_basic_is_rejected_on_business_endpoints(self):
|
||||
value = api.basic(self.alice['username'], self.alice['password'])
|
||||
self.assert_bearer_rejected(api.request('GET', ME, headers={'Authorization': value}))
|
||||
|
||||
def test_x_user_id_is_ignored(self):
|
||||
self.assert_bearer_rejected(api.request('GET', ME, headers={'X-User-Id': self.alice['id']}))
|
||||
me = api.request('GET', ME, user=self.alice, headers={'X-User-Id': self.bob['id']})
|
||||
self.assertEqual(me.json, api.public(self.alice))
|
||||
|
||||
def test_token_selects_the_user(self):
|
||||
self.assertEqual(api.request('GET', ME, user=self.alice).json, api.public(self.alice))
|
||||
self.assertEqual(api.request('GET', ME, user=self.bob).json, api.public(self.bob))
|
||||
|
||||
def test_scheme_name_is_case_insensitive(self):
|
||||
response = api.request('GET', ME, headers={'Authorization': 'bearer ' + self.alice['token']})
|
||||
self.assertEqual(response.status, 200)
|
||||
|
||||
|
||||
class LogoutTest(unittest.TestCase):
|
||||
def test_logout_revokes_only_the_current_token(self):
|
||||
user = api.new_user('Выход')
|
||||
other = api.login(user['username'], user['password']).json['token']
|
||||
response = api.request('DELETE', SESSIONS + '/current', user=user)
|
||||
self.assertEqual(response.status, 204)
|
||||
self.assertEqual(response.body, b'')
|
||||
self.assertEqual(api.request('GET', ME, user=user).status, 401)
|
||||
self.assertEqual(api.request('DELETE', SESSIONS + '/current', user=user).status, 401)
|
||||
self.assertEqual(api.request('GET', ME, headers={'Authorization': 'Bearer ' + other}).status, 200)
|
||||
|
||||
def test_login_again_after_logout(self):
|
||||
user = api.new_user('Снова')
|
||||
api.request('DELETE', SESSIONS + '/current', user=user)
|
||||
again = api.login(user['username'], user['password'])
|
||||
self.assertEqual(again.status, 201)
|
||||
self.assertNotEqual(again.json['token'], user['token'])
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
@@ -0,0 +1,209 @@
|
||||
"""Валидация входных данных: неверный JSON, пустое сообщение, дубликат username, пароль, Unicode."""
|
||||
import json
|
||||
import unittest
|
||||
import uuid
|
||||
|
||||
import api
|
||||
|
||||
USERS = '/api/v1/users'
|
||||
PASSWORD = 'Valid-password-123'
|
||||
|
||||
|
||||
def registration(**fields):
|
||||
"""Корректное тело регистрации; fields заменяют или добавляют поля."""
|
||||
body = {'username': api.unique_username(), 'display_name': 'Имя', 'password': PASSWORD}
|
||||
body.update(fields)
|
||||
return body
|
||||
|
||||
|
||||
class InvalidJsonTest(unittest.TestCase):
|
||||
def assert_invalid(self, response):
|
||||
self.assertEqual(response.status, 400)
|
||||
self.assertEqual(response.json['error']['code'], 'invalid_request')
|
||||
|
||||
def test_malformed_json_on_register(self):
|
||||
self.assert_invalid(api.request('POST', USERS, raw=b'{"username": "broken", '))
|
||||
|
||||
def test_malformed_json_on_message(self):
|
||||
user = api.new_user()
|
||||
chat = api.new_chat(user, [user])
|
||||
path = '/api/v1/chats/%s/messages' % chat['id']
|
||||
for raw in (b'not json', b'{"text": "no closing quote}', b'', b'{"text": }'):
|
||||
with self.subTest(raw=raw):
|
||||
self.assert_invalid(api.request('POST', path, user=user, raw=raw))
|
||||
self.assertEqual(api.pages(path, user, 100), [[]])
|
||||
|
||||
def test_json_that_is_not_an_object(self):
|
||||
for raw in (b'[]', b'"text"', b'42', b'null'):
|
||||
with self.subTest(raw=raw):
|
||||
self.assert_invalid(api.request('POST', USERS, raw=raw))
|
||||
|
||||
def test_invalid_utf8(self):
|
||||
raw = json.dumps(registration(display_name='PLACEHOLDER')).encode().replace(b'PLACEHOLDER', b'\xff\xfe')
|
||||
self.assert_invalid(api.request('POST', USERS, raw=raw))
|
||||
|
||||
def test_lone_surrogate_is_rejected_not_crashed(self):
|
||||
raw = json.dumps(registration(display_name='PLACEHOLDER')).encode().replace(b'PLACEHOLDER', b'\\ud800')
|
||||
self.assert_invalid(api.request('POST', USERS, raw=raw))
|
||||
|
||||
def test_nul_character_is_rejected_not_crashed(self):
|
||||
self.assert_invalid(api.request('POST', USERS, body=registration(display_name='a\x00b')))
|
||||
|
||||
def test_extra_fields(self):
|
||||
self.assert_invalid(api.request('POST', USERS, body=registration(is_admin=True)))
|
||||
|
||||
def test_wrong_types(self):
|
||||
for fields in ({'username': 12345}, {'display_name': ['Имя']}, {'display_name': None}, {'password': 123456789012345}):
|
||||
with self.subTest(fields=fields):
|
||||
self.assert_invalid(api.request('POST', USERS, body=registration(**fields)))
|
||||
|
||||
def test_wrong_content_type(self):
|
||||
raw = json.dumps(registration()).encode()
|
||||
self.assert_invalid(api.request('POST', USERS, raw=raw, headers={'Content-Type': 'text/plain'}))
|
||||
|
||||
|
||||
class UserValidationTest(unittest.TestCase):
|
||||
def test_duplicate_username_is_409(self):
|
||||
body = registration(display_name='Первый')
|
||||
first = api.request('POST', USERS, body=body)
|
||||
self.assertEqual(first.status, 201)
|
||||
second = api.request('POST', USERS, body=dict(body, display_name='Второй', password='Another-password-456'))
|
||||
self.assertEqual(second.status, 409)
|
||||
self.assertEqual(second.json['error']['code'], 'conflict')
|
||||
session = api.login(body['username'], body['password'])
|
||||
self.assertEqual(session.json['user'], first.json)
|
||||
self.assertEqual(api.login(body['username'], 'Another-password-456').status, 401)
|
||||
|
||||
def test_bad_usernames(self):
|
||||
for username in ('ab', 'a' * 33, 'Alice', 'with space', 'дмитрий', 'dash-name', '', 'name\n', 'colon:name'):
|
||||
with self.subTest(username=username):
|
||||
self.assertEqual(api.request('POST', USERS, body=registration(username=username)).status, 400)
|
||||
|
||||
def test_username_length_limits_are_accepted(self):
|
||||
for length in (3, 32):
|
||||
with self.subTest(length=length):
|
||||
response = api.request('POST', USERS, body=registration(username=uuid.uuid4().hex[:length]))
|
||||
self.assertIn(response.status, (201, 409))
|
||||
|
||||
def test_bad_display_names(self):
|
||||
for display_name in ('', ' ', '\t\n', 'я' * 101):
|
||||
with self.subTest(display_name=display_name):
|
||||
self.assertEqual(api.request('POST', USERS, body=registration(display_name=display_name)).status, 400)
|
||||
|
||||
def test_bad_passwords(self):
|
||||
for password in ('a' * 11, 'я' * 11, 'a' * 129, ' ' * 12, '\t' * 20, 'pass\x00word-123'):
|
||||
with self.subTest(password=password[:20]):
|
||||
self.assertEqual(api.request('POST', USERS, body=registration(password=password)).status, 400)
|
||||
|
||||
def test_password_length_limits_are_accepted(self):
|
||||
for password in ('a' * 12, 'я' * 12, '👋' * 128):
|
||||
with self.subTest(length=len(password)):
|
||||
body = registration(password=password)
|
||||
self.assertEqual(api.request('POST', USERS, body=body).status, 201)
|
||||
self.assertEqual(api.login(body['username'], password).status, 201)
|
||||
|
||||
def test_missing_fields(self):
|
||||
for field in ('username', 'display_name', 'password'):
|
||||
with self.subTest(field=field):
|
||||
body = registration()
|
||||
del body[field]
|
||||
self.assertEqual(api.request('POST', USERS, body=body).status, 400)
|
||||
|
||||
def test_unicode_display_name_is_kept(self):
|
||||
for display_name in ('Дмитрий 👋', '李雷', '👋' * 100):
|
||||
with self.subTest(display_name=display_name):
|
||||
response = api.request('POST', USERS, body=registration(display_name=display_name))
|
||||
self.assertEqual(response.status, 201)
|
||||
self.assertEqual(response.json['display_name'], display_name)
|
||||
|
||||
|
||||
class MessageValidationTest(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
cls.user = api.new_user()
|
||||
cls.chat = api.new_chat(cls.user, [cls.user])
|
||||
cls.path = '/api/v1/chats/%s/messages' % cls.chat['id']
|
||||
|
||||
def test_empty_message_is_400(self):
|
||||
for text in ('', ' ', '\n\t \r\n', ' '):
|
||||
with self.subTest(text=text):
|
||||
response = api.send(self.user, self.chat, text)
|
||||
self.assertEqual(response.status, 400)
|
||||
self.assertEqual(response.json['error']['code'], 'invalid_request')
|
||||
|
||||
def test_missing_or_wrong_text(self):
|
||||
for body in ({}, {'text': None}, {'text': 123}, {'text': ['a']}, {'message': 'a'}):
|
||||
with self.subTest(body=body):
|
||||
self.assertEqual(api.request('POST', self.path, user=self.user, body=body).status, 400)
|
||||
|
||||
def test_sender_id_in_body_is_rejected(self):
|
||||
other = api.new_user()
|
||||
body = {'text': 'подмена отправителя', 'sender_id': other['id']}
|
||||
self.assertEqual(api.request('POST', self.path, user=self.user, body=body).status, 400)
|
||||
|
||||
def test_nul_in_text_is_400(self):
|
||||
self.assertEqual(api.send(self.user, self.chat, 'до\x00после').status, 400)
|
||||
|
||||
def test_text_length_limit(self):
|
||||
self.assertEqual(api.send(self.user, self.chat, 'я' * 4000).status, 201)
|
||||
self.assertEqual(api.send(self.user, self.chat, '👋' * 4000).status, 201)
|
||||
self.assertEqual(api.send(self.user, self.chat, 'я' * 4001).status, 400)
|
||||
|
||||
def test_unicode_text_is_kept(self):
|
||||
text = 'Привет, мир 👋 — 你好\nвторая строка'
|
||||
response = api.send(self.user, self.chat, text)
|
||||
self.assertEqual(response.status, 201)
|
||||
self.assertEqual(response.json['text'], text)
|
||||
self.assertEqual(response.json['sender_id'], self.user['id'])
|
||||
self.assertEqual(response.json['chat_id'], self.chat['id'])
|
||||
|
||||
def test_oversized_body_is_413(self):
|
||||
response = api.request('POST', self.path, user=self.user, body={'text': 'a' * 70000})
|
||||
self.assertEqual(response.status, 413)
|
||||
self.assertEqual(response.json['error']['code'], 'payload_too_large')
|
||||
|
||||
|
||||
class ChatValidationTest(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
cls.alice = api.new_user()
|
||||
cls.bob = api.new_user()
|
||||
|
||||
def create(self, title, member_ids):
|
||||
return api.request('POST', '/api/v1/chats', user=self.alice, body={'title': title, 'member_ids': member_ids})
|
||||
|
||||
def test_caller_must_be_a_member(self):
|
||||
self.assertEqual(self.create('Без меня', [self.bob['id']]).status, 400)
|
||||
|
||||
def test_duplicate_members(self):
|
||||
self.assertEqual(self.create('Дубликат', [self.alice['id'], self.bob['id'], self.bob['id']]).status, 400)
|
||||
self.assertEqual(self.create('Дубликат', [self.alice['id'], self.alice['id'].upper()]).status, 400)
|
||||
|
||||
def test_unknown_member_rolls_back_the_whole_chat(self):
|
||||
title = 'Откат ' + uuid.uuid4().hex[:8]
|
||||
response = self.create(title, [self.alice['id'], str(uuid.uuid4())])
|
||||
self.assertEqual(response.status, 400)
|
||||
chats = [chat for page in api.pages('/api/v1/chats', self.alice, 100) for chat in page]
|
||||
self.assertNotIn(title, [chat['title'] for chat in chats])
|
||||
|
||||
def test_bad_member_ids(self):
|
||||
for member_ids in ([], 'not a list', [123], ['not-a-uuid'], None):
|
||||
with self.subTest(member_ids=member_ids):
|
||||
self.assertEqual(self.create('Чат', member_ids).status, 400)
|
||||
|
||||
def test_bad_titles(self):
|
||||
for title in ('', ' ', 'я' * 101, 5, 'a\x00b'):
|
||||
with self.subTest(title=title):
|
||||
self.assertEqual(self.create(title, [self.alice['id']]).status, 400)
|
||||
|
||||
def test_valid_chat_keeps_member_order(self):
|
||||
response = self.create('Проект по ВНП', [self.bob['id'], self.alice['id']])
|
||||
self.assertEqual(response.status, 201)
|
||||
self.assertEqual(response.json['title'], 'Проект по ВНП')
|
||||
self.assertEqual(response.json['member_ids'], [self.bob['id'], self.alice['id']])
|
||||
again = api.request('GET', '/api/v1/chats/' + response.json['id'], user=self.bob)
|
||||
self.assertEqual(again.json, response.json)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user