Author SHA1 Message Date
DaniilKuznetsov a6c507eb19 Проверка чистого клона 2026-11-16 22:25:17 +03:00
DaniilKuznetsovandЮгай Артур Рустамович 8e06e7634f ЛР2 на 3
Co-authored-by: Югай Артур Рустамович <f1ekserr@yandex.ru>
2026-11-16 22:19:31 +03:00
34 changed files with 2098 additions and 34 deletions
+2 -10
View File
@@ -1,11 +1,3 @@
# Copy to .env for your LOCAL synthetic test environment; never commit .env. HTTP_PORT=18102
# These are logical interface suggestions; map actual framework names in REPORT.md. COMPOSE_PROJECT_NAME=kuznetsov-lab2
APP_HOST=0.0.0.0
APP_PORT=8080
APP_ENV=lab
SESSION_TTL_SECONDS=3600 SESSION_TTL_SECONDS=3600
# Choose credentials locally. File paths contain no secret values.
DATABASE_URL_FILE=/run/secrets/database_url
# Optional on 3, required use on 4 in lab2; Redis OR Valkey:
SESSION_STORE=postgres
CACHE_URL_FILE=/run/secrets/cache_url
+2
View File
@@ -0,0 +1,2 @@
* text=auto eol=lf
*.cmd text eol=crlf
+15
View File
@@ -0,0 +1,15 @@
FROM python:3.13.14-slim-bookworm@sha256:67a1e1f215ccda113cfc024e8639049257e88f273898f595b61476d128d387e8
ENV PYTHONDONTWRITEBYTECODE=1 PYTHONUNBUFFERED=1 PYTHONUTF8=1 \
APP_HOST=0.0.0.0 APP_PORT=8080 APP_ENV=lab
WORKDIR /app
COPY requirements.lock .
RUN python -m pip install --no-cache-dir -r requirements.lock \
&& useradd --uid 10001 --create-home messenger
COPY src ./src
COPY migrations ./migrations
USER messenger
EXPOSE 8080
HEALTHCHECK --interval=5s --timeout=4s --start-period=10s --retries=6 \
CMD python -c "import os,urllib.request; urllib.request.urlopen('http://127.0.0.1:'+os.environ['APP_PORT']+'/health/ready',timeout=3).close()"
CMD ["python", "-m", "src"]
+46 -24
View File
@@ -1,37 +1,59 @@
# Отчёт — лабораторная 2 # ЛР2 — PostgreSQL и сессии
- Автор, группа: TODO Кузнецов Даниил Русланович, БПИ2304. На **3**.
- Целевая оценка; выбранный трек 5, если нужен: TODO
- Commit/PR: TODO после публикации
- Использованные источники, библиотеки и помощь ИИ: TODO
## Запуск из чистого clone ## Запуск
TODO: версии runtime/образов, prerequisites, команды создания локальных secrets/.env, build, миграций, запуска, ожидания готовности, тестов и остановки. Указать необходимые действия оператора. Не вставлять секреты. Отдельно указать безопасную остановку и команду намеренного удаления учебных данных. Нужны Python 3.13, Git и Docker с Compose. Из папки репозитория:
## Архитектура и решения ```powershell
python -X utf8 scripts/lab.py verify
```
TODO: схема процессов/хранилищ, источник истины, транзакционные границы, ограничения. Что перенесено из lab1, что изменено в этой работе. Соответствие переменных из .env.example реальной конфигурации. Создаются `.venv`, локальный `.env` и новые секреты. Скрипт собирает образ,
применяет миграции, запускает API и проверки. Повторный setup сохраняет настройки.
API: `http://127.0.0.1:18102`, Compose-проект: `kuznetsov-lab2`.
Linux: те же команды через `python3`.
## Доказательства критериев Отдельно доступны `setup`, `build`, `start`, `test`, `restart-check`, `stop`.
Например: `python -X utf8 scripts/lab.py stop`. Остановка сохраняет volume.
Намеренный сброс учебной БД: `docker compose -p kuznetsov-lab2 --env-file .env down --volumes`.
| Критерий README | Команда/сценарий | Наблюдаемый результат | Файл доказательства | ## Реализация
| --- | --- | --- | --- |
| Публичный smoke | make test | TODO | TODO |
| Остальные пункты уровня 3 | TODO | TODO | TODO |
| Все пункты уровня 4, если заявлен | TODO | TODO | TODO |
| Выбранный трек 5, если заявлен | TODO | TODO | TODO |
TODO: добавить отдельную строку для каждого заявленного критерия. PASS без команды и наблюдения не является доказательством. Проверка инфраструктурного hook требует подтверждения, какие реальные процессы/хранилища перезапущены. FastAPI → PostgreSQL. В БД хранятся пользователи, чаты, участники, сообщения и сессии.
Чат и участники создаются одной транзакцией; история выбирается SQL-пагинацией
по `(created_at, id)`, курсоры подписываются постоянным ключом.
## Эксперименты и отказы Регистрация требует пароль; в БД только Argon2id-хеш. Basic используется при входе,
дальше Bearer. Хранится SHA-256 токена, TTL — 3600 секунд; logout удаляет сессию.
SQL параметризован, пул и запросы имеют таймауты. Миграции нумеруются и проверяют checksum.
TODO: гипотеза, hardware/лимиты, версии, объём данных, профиль нагрузки, concurrency/arrival rate, длительность/прогрев, повторы, успешные RPS, ошибки, p50/p95/p99, CPU/RAM. Сохранить raw summary без tokens и объяснить ограничения измерения. Python 3.13.14, PostgreSQL 17.11, Psycopg 3.3.8.
Образы и зависимости закреплены. API работает без root, наружу открыт только его порт,
БД и секреты доступны внутри стенда.
| Воздействие | Что ожидаем | Что наблюдали | Время восстановления / потеря данных | ## Проверки
| --- | --- | --- | --- |
| TODO | TODO | TODO | TODO |
## Ограничения и следующие шаги Команда: `python -X utf8 scripts/lab.py verify`.
TODO: какие отказы выдерживаются и какие нет; что не реализовано; где учебное упрощение; какое улучшение подтверждено, а какое пока гипотеза. | Проверка | Результат |
| --- | --- |
| Структура, OpenAPI, Compose, публичный smoke | PASS — [лог](evidence/verification.txt) |
| Пароли, доступ, Unicode, пагинация, конкуренция | 17 тестов, PASS — [лог](evidence/verification.txt) |
| Рестарт API/БД: старые токен, курсор, сообщения и logout | PASS — [данные](evidence/persistence.json), [контейнеры](evidence/restart.json) |
| Повторные миграции | Данные не изменились — [JSON](evidence/persistence.json) |
| TTL=2 на отдельном порту 18112 | Истёкший токен 401, expiry не продлевается — [JSON](evidence/expiry.json) |
| Остановка БД и восстановление | live 200, ready/API 503, затем 200 — [сводка](evidence/summary.json) |
Чистый клон коммита `8e06e7634f6082866d8ac4204106453490c103cf` прошёл полный прогон:
`python -X utf8 scripts/lab.py verify --project kuznetsov-lab2-clean --port 18122 --test-port 18132`.
Использованы новые секреты и volume. [Лог](evidence/clean-clone-verification.txt),
[сводка](evidence/clean-clone-summary.json).
TTL-стенд удаляет только свои временные контейнеры и сети, сохраняя volume.
Ограничения: локальный HTTP, одна API-реплика и одна БД, без Redis и HA.
Основа — [реализация Артура Югая](https://gitea.privcloud.tech/ArturYugay/mtusi2026lab2/src/commit/2ae42b5ef61fe05695c9a28224599109454bde9d).
Для Даниила выделены свои настройки, секреты, ports/volumes и проверки.
README задания, контракт и публичные тесты сохранены.
+62
View File
@@ -0,0 +1,62 @@
name: kuznetsov-lab2
x-app: &app
image: mtusi-messenger:kuznetsov-lab2
build: .
environment:
APP_HOST: 0.0.0.0
APP_PORT: ${APP_PORT:-8080}
APP_ENV: lab
DATABASE_URL_FILE: /run/secrets/database_url
CURSOR_SECRET_FILE: /run/secrets/cursor_key
SESSION_TTL_SECONDS: ${SESSION_TTL_SECONDS:-3600}
secrets:
- database_url
- cursor_key
services:
db:
image: postgres:17.11-bookworm@sha256:665dc7b37ac52a17e298511414c21d23ffa4e6a6ef6225b3e5d59c7afb35e994
environment:
POSTGRES_USER: messenger
POSTGRES_DB: messenger
POSTGRES_PASSWORD_FILE: /run/secrets/postgres_password
secrets:
- postgres_password
volumes:
- pgdata:/var/lib/postgresql/data
healthcheck:
test: ["CMD-SHELL", "pg_isready -U messenger -d messenger"]
interval: 2s
timeout: 3s
retries: 30
stop_grace_period: 10s
migrate:
<<: *app
command: ["python", "-m", "src.migrate"]
depends_on:
db:
condition: service_healthy
api:
<<: *app
ports:
- "127.0.0.1:${HTTP_PORT:-18102}:${APP_PORT:-8080}"
depends_on:
db:
condition: service_healthy
migrate:
condition: service_completed_successfully
stop_grace_period: 10s
volumes:
pgdata:
secrets:
postgres_password:
file: ./secrets/postgres_password
database_url:
file: ./secrets/database_url
cursor_key:
file: ./secrets/cursor_key
+7
View File
@@ -0,0 +1,7 @@
{
"ttl_seconds": 2,
"expiry_unchanged_on_restart": true,
"before_restart": 200,
"after_expiry_and_restart": 401,
"new_login": 201
}
+17
View File
@@ -0,0 +1,17 @@
{
"public_persistence": "PASS",
"cursor_after_restart": "PASS",
"logout_after_restart": "PASS",
"migration_replay": "PASS",
"database": {
"users": 55,
"chats": 19,
"members": 38,
"messages": 118,
"sessions": 51,
"migrations": 1,
"expiry_fingerprint": "6019190db4fe064984157160c15bfb97",
"passwords_are_argon2id": true,
"session_hashes_only": true
}
}
+43
View File
@@ -0,0 +1,43 @@
{
"project": "kuznetsov-lab2-clean",
"before": {
"api": {
"id": "ea908319797be1bc7c30648ea6a194804134d74d8ccce2ac77b90531b46b82b1",
"image": "sha256:88244ece411de138ab2d57ab22ce6d58f327732cb7ff12a674fb58999af0b20f",
"started_at": "2026-11-16T19:21:33.998830387Z",
"running": true,
"user": "messenger",
"volumes": []
},
"db": {
"id": "771c7a0b0e87e77de5d1d79b5b5c3e7a9db9d7332297cd7326d229c68d98a54c",
"image": "sha256:665dc7b37ac52a17e298511414c21d23ffa4e6a6ef6225b3e5d59c7afb35e994",
"started_at": "2026-11-16T19:21:30.649407571Z",
"running": true,
"user": "",
"volumes": [
"kuznetsov-lab2-clean_pgdata"
]
}
},
"after": {
"api": {
"id": "ea908319797be1bc7c30648ea6a194804134d74d8ccce2ac77b90531b46b82b1",
"image": "sha256:88244ece411de138ab2d57ab22ce6d58f327732cb7ff12a674fb58999af0b20f",
"started_at": "2026-11-16T19:21:43.343856729Z",
"running": true,
"user": "messenger",
"volumes": []
},
"db": {
"id": "771c7a0b0e87e77de5d1d79b5b5c3e7a9db9d7332297cd7326d229c68d98a54c",
"image": "sha256:665dc7b37ac52a17e298511414c21d23ffa4e6a6ef6225b3e5d59c7afb35e994",
"started_at": "2026-11-16T19:21:43.006201963Z",
"running": true,
"user": "",
"volumes": [
"kuznetsov-lab2-clean_pgdata"
]
}
}
}
+63
View File
@@ -0,0 +1,63 @@
{
"date_utc": "2026-11-16T19:21:10.089429+00:00",
"platform": "Windows-11-10.0.26200-SP0",
"source_commit": "8e06e7634f6082866d8ac4204106453490c103cf",
"worktree_dirty_at_start": false,
"project": "kuznetsov-lab2-clean",
"status": "PASS",
"persistence": {
"public_persistence": "PASS",
"cursor_after_restart": "PASS",
"logout_after_restart": "PASS",
"migration_replay": "PASS",
"database": {
"users": 55,
"chats": 19,
"members": 38,
"messages": 118,
"sessions": 51,
"migrations": 1,
"expiry_fingerprint": "6019190db4fe064984157160c15bfb97",
"passwords_are_argon2id": true,
"session_hashes_only": true
}
},
"expiry": {
"ttl_seconds": 2,
"expiry_unchanged_on_restart": true,
"before_restart": 200,
"after_expiry_and_restart": 401,
"new_login": 201
},
"dependency": {
"live_without_db": 200,
"ready_without_db": 503,
"business_without_db": 503,
"recovered": 200
},
"containers": {
"api": {
"id": "ea908319797be1bc7c30648ea6a194804134d74d8ccce2ac77b90531b46b82b1",
"image": "sha256:88244ece411de138ab2d57ab22ce6d58f327732cb7ff12a674fb58999af0b20f",
"started_at": "2026-11-16T19:21:43.343856729Z",
"running": true,
"user": "messenger",
"volumes": []
},
"db": {
"id": "771c7a0b0e87e77de5d1d79b5b5c3e7a9db9d7332297cd7326d229c68d98a54c",
"image": "sha256:665dc7b37ac52a17e298511414c21d23ffa4e6a6ef6225b3e5d59c7afb35e994",
"started_at": "2026-11-16T19:22:06.302882285Z",
"running": true,
"user": "",
"volumes": [
"kuznetsov-lab2-clean_pgdata"
]
}
},
"docker": "29.9.3",
"resources": {
"cpus": 12,
"memory_bytes": 16591364096
}
}
+179
View File
@@ -0,0 +1,179 @@
$ .\.venv\Scripts\python.exe -X utf8 -m pip install --disable-pip-version-check --no-input --progress-bar off --quiet -r .\requirements.lock -r .\requirements-validation.lock
Environment ready; existing settings and secrets preserved
$ .\.venv\Scripts\python.exe -X utf8 scripts/check_contract.py
PASS: offline skeleton structure, local refs and Python syntax; application NOT tested.
$ .\.venv\Scripts\python.exe -X utf8 scripts/validate_openapi.py
PASS: full OpenAPI 3.1 and JSON Schema validation; application NOT tested.
$ docker compose --project-name kuznetsov-lab2-clean --env-file .env config --quiet
$ docker compose --project-name kuznetsov-lab2-clean --env-file .env config --quiet
$ docker compose --project-name kuznetsov-lab2-clean --env-file .env build api
Image mtusi-messenger:kuznetsov-lab2 Building
#1 [internal] load local bake definitions
#1 reading from stdin 1.14kB 0.0s done
#1 DONE 0.0s
#2 [internal] load build definition from Dockerfile
#2 transferring dockerfile: 728B done
#2 DONE 0.0s
#3 [internal] load metadata for docker.io/library/python:3.13.14-slim-bookworm@sha256:67a1e1f215ccda113cfc024e8639049257e88f273898f595b61476d128d387e8
#3 DONE 0.0s
#4 [internal] load .dockerignore
#4 transferring context: 196B done
#4 DONE 0.0s
#5 [internal] load build context
#5 transferring context: 26.21kB 0.0s done
#5 DONE 0.0s
#6 [1/6] FROM docker.io/library/python:3.13.14-slim-bookworm@sha256:67a1e1f215ccda113cfc024e8639049257e88f273898f595b61476d128d387e8
#6 resolve docker.io/library/python:3.13.14-slim-bookworm@sha256:67a1e1f215ccda113cfc024e8639049257e88f273898f595b61476d128d387e8 0.0s done
#6 DONE 0.0s
#7 [2/6] WORKDIR /app
#7 CACHED
#8 [3/6] COPY requirements.lock .
#8 CACHED
#9 [4/6] RUN python -m pip install --no-cache-dir -r requirements.lock && useradd --uid 10001 --create-home messenger
#9 CACHED
#10 [5/6] COPY src ./src
#10 CACHED
#11 [6/6] COPY migrations ./migrations
#11 CACHED
#12 exporting to image
#12 exporting layers done
#12 exporting manifest sha256:d634ef539266ca4660b5a5303beec4e0140aa4f4ecbfd0123a641bf3b89d948c 0.0s done
#12 exporting config sha256:73b05c4f7544f35989f7068b1a3ae87266b76d2e78a61e999afc1caf5ed96d9e 0.0s done
#12 exporting attestation manifest sha256:270029868629de5933148fc8681ee0dbf5faa575673244c61f8edf16e0dcfcd2 0.0s done
#12 exporting manifest list sha256:88244ece411de138ab2d57ab22ce6d58f327732cb7ff12a674fb58999af0b20f 0.0s done
#12 naming to docker.io/library/mtusi-messenger:kuznetsov-lab2 done
#12 unpacking to docker.io/library/mtusi-messenger:kuznetsov-lab2 done
#12 DONE 0.1s
#13 resolving provenance for metadata file
#13 DONE 0.0s
Image mtusi-messenger:kuznetsov-lab2 Built
$ docker compose --project-name kuznetsov-lab2-clean --env-file .env up -d --no-build db migrate api
Volume kuznetsov-lab2-clean_pgdata Creating
Volume kuznetsov-lab2-clean_pgdata Creating
Network kuznetsov-lab2-clean_default Creating
Network kuznetsov-lab2-clean_default Creating
Volume kuznetsov-lab2-clean_pgdata Created
Volume kuznetsov-lab2-clean_pgdata Created
Network kuznetsov-lab2-clean_default Created
Network kuznetsov-lab2-clean_default Created
Container kuznetsov-lab2-clean-db-1 Creating
Container kuznetsov-lab2-clean-db-1 Created
Container kuznetsov-lab2-clean-migrate-1 Creating
Container kuznetsov-lab2-clean-migrate-1 Created
Container kuznetsov-lab2-clean-api-1 Creating
Container kuznetsov-lab2-clean-api-1 Created
Container kuznetsov-lab2-clean-db-1 Starting
Container kuznetsov-lab2-clean-db-1 Started
Container kuznetsov-lab2-clean-db-1 Waiting
Container kuznetsov-lab2-clean-db-1 Healthy
Container kuznetsov-lab2-clean-migrate-1 Starting
Container kuznetsov-lab2-clean-migrate-1 Started
Container kuznetsov-lab2-clean-migrate-1 Waiting
Container kuznetsov-lab2-clean-migrate-1 Exited
Container kuznetsov-lab2-clean-api-1 Starting
Container kuznetsov-lab2-clean-api-1 Started
API ready: http://127.0.0.1:18122
$ .\.venv\Scripts\python.exe -X utf8 tests/smoke.py --base-url http://127.0.0.1:18122
PASS: lab2 public API scenario; infrastructure and grading evidence remain separate.
$ .\.venv\Scripts\python.exe -X utf8 -m unittest discover -s student-tests -v
test_auth_and_access (test_api.ApiTests.test_auth_and_access) ... ok
test_bad_cursors_and_limits (test_api.ApiTests.test_bad_cursors_and_limits) ... ok
test_chat_pagination (test_api.ApiTests.test_chat_pagination) ... ok
test_chat_validation_is_atomic (test_api.ApiTests.test_chat_validation_is_atomic) ... ok
test_invalid_json_and_content_type (test_api.ApiTests.test_invalid_json_and_content_type) ... ok
test_message_fields_and_unicode (test_api.ApiTests.test_message_fields_and_unicode) ... ok
test_message_pagination (test_api.ApiTests.test_message_pagination) ... ok
test_parallel_messages (test_api.ApiTests.test_parallel_messages) ... ok
test_parallel_username (test_api.ApiTests.test_parallel_username) ... ok
test_read_after_write (test_api.ApiTests.test_read_after_write) ... ok
test_user_fields (test_api.ApiTests.test_user_fields) ... ok
test_logout_only_revokes_current_session (test_sessions.SessionTests.test_logout_only_revokes_current_session) ... ok
test_malformed_basic_and_body (test_sessions.SessionTests.test_malformed_basic_and_body) ... ok
test_only_bearer_identifies_user (test_sessions.SessionTests.test_only_bearer_identifies_user) ... ok
test_password_boundaries_unicode_colon_spaces (test_sessions.SessionTests.test_password_boundaries_unicode_colon_spaces) ... ok
test_password_required_and_limits (test_sessions.SessionTests.test_password_required_and_limits) ... ok
test_wrong_and_unknown_credentials (test_sessions.SessionTests.test_wrong_and_unknown_credentials) ... ok
----------------------------------------------------------------------
Ran 17 tests in 5.198s
OK
$ .\.venv\Scripts\python.exe -X utf8 tests/resilience.py --mode persistence --action-script .\scripts\restart.cmd --base-url http://127.0.0.1:18122
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 compose --project-name kuznetsov-lab2-clean --env-file .env run --rm --no-deps migrate
Container kuznetsov-lab2-clean-migrate-run-b860de37eee8 Creating
Container kuznetsov-lab2-clean-migrate-run-b860de37eee8 Created
Already applied: 001_initial.sql
$ docker compose --project-name kuznetsov-lab2-clean --env-file .env run --rm --no-deps migrate
Container kuznetsov-lab2-clean-migrate-run-5e1439149621 Creating
Container kuznetsov-lab2-clean-migrate-run-5e1439149621 Created
Already applied: 001_initial.sql
PASS: old session/cursor, durable logout and repeatable migrations
$ docker compose --project-name kuznetsov-lab2-clean-ttl --env-file .env up -d --no-build db migrate api
Network kuznetsov-lab2-clean-ttl_default Creating
Volume kuznetsov-lab2-clean-ttl_pgdata Creating
Volume kuznetsov-lab2-clean-ttl_pgdata Creating
Network kuznetsov-lab2-clean-ttl_default Creating
Volume kuznetsov-lab2-clean-ttl_pgdata Created
Volume kuznetsov-lab2-clean-ttl_pgdata Created
Network kuznetsov-lab2-clean-ttl_default Created
Network kuznetsov-lab2-clean-ttl_default Created
Container kuznetsov-lab2-clean-ttl-db-1 Creating
Container kuznetsov-lab2-clean-ttl-db-1 Created
Container kuznetsov-lab2-clean-ttl-migrate-1 Creating
Container kuznetsov-lab2-clean-ttl-migrate-1 Created
Container kuznetsov-lab2-clean-ttl-api-1 Creating
Container kuznetsov-lab2-clean-ttl-api-1 Created
Container kuznetsov-lab2-clean-ttl-db-1 Starting
Container kuznetsov-lab2-clean-ttl-db-1 Started
Container kuznetsov-lab2-clean-ttl-db-1 Waiting
Container kuznetsov-lab2-clean-ttl-db-1 Healthy
Container kuznetsov-lab2-clean-ttl-migrate-1 Starting
Container kuznetsov-lab2-clean-ttl-migrate-1 Started
Container kuznetsov-lab2-clean-ttl-migrate-1 Waiting
Container kuznetsov-lab2-clean-ttl-migrate-1 Exited
Container kuznetsov-lab2-clean-ttl-api-1 Starting
Container kuznetsov-lab2-clean-ttl-api-1 Started
API ready: http://127.0.0.1:18132
$ docker compose --project-name kuznetsov-lab2-clean-ttl --env-file .env restart --timeout 5 api
Container kuznetsov-lab2-clean-ttl-api-1 Restarting
Container kuznetsov-lab2-clean-ttl-api-1 Started
PASS: TTL=2, expired token rejected after API restart; fresh login works
$ docker compose --project-name kuznetsov-lab2-clean-ttl --env-file .env down --timeout 10
Container kuznetsov-lab2-clean-ttl-api-1 Stopping
Container kuznetsov-lab2-clean-ttl-api-1 Stopped
Container kuznetsov-lab2-clean-ttl-api-1 Removing
Container kuznetsov-lab2-clean-ttl-api-1 Removed
Container kuznetsov-lab2-clean-ttl-migrate-1 Stopping
Container kuznetsov-lab2-clean-ttl-migrate-1 Stopped
Container kuznetsov-lab2-clean-ttl-migrate-1 Removing
Container kuznetsov-lab2-clean-ttl-migrate-1 Removed
Container kuznetsov-lab2-clean-ttl-db-1 Stopping
Container kuznetsov-lab2-clean-ttl-db-1 Stopped
Container kuznetsov-lab2-clean-ttl-db-1 Removing
Container kuznetsov-lab2-clean-ttl-db-1 Removed
Network kuznetsov-lab2-clean-ttl_default Removing
Network kuznetsov-lab2-clean-ttl_default Removed
$ docker compose --project-name kuznetsov-lab2-clean --env-file .env stop --timeout 10 db
Container kuznetsov-lab2-clean-db-1 Stopping
Container kuznetsov-lab2-clean-db-1 Stopped
$ docker compose --project-name kuznetsov-lab2-clean --env-file .env start db
Container kuznetsov-lab2-clean-db-1 Starting
Container kuznetsov-lab2-clean-db-1 Started
PASS: DB outage -> live=200, ready/business=503; session works after recovery
$ .\.venv\Scripts\python.exe -X utf8 tests/smoke.py --base-url http://127.0.0.1:18122
PASS: lab2 public API scenario; infrastructure and grading evidence remain separate.
PASS: full lab2 verification
+7
View File
@@ -0,0 +1,7 @@
{
"ttl_seconds": 2,
"expiry_unchanged_on_restart": true,
"before_restart": 200,
"after_expiry_and_restart": 401,
"new_login": 201
}
+17
View File
@@ -0,0 +1,17 @@
{
"public_persistence": "PASS",
"cursor_after_restart": "PASS",
"logout_after_restart": "PASS",
"migration_replay": "PASS",
"database": {
"users": 55,
"chats": 19,
"members": 38,
"messages": 118,
"sessions": 51,
"migrations": 1,
"expiry_fingerprint": "0aeb1b0d92290be0e10e5671487fda98",
"passwords_are_argon2id": true,
"session_hashes_only": true
}
}
+43
View File
@@ -0,0 +1,43 @@
{
"project": "kuznetsov-lab2",
"before": {
"api": {
"id": "1e398850630740acd3ed5db30568b82af4232391bd9c235896cc09e860af88f0",
"image": "sha256:7c648de46909df8dd4c0e6676fc2c990a113960415c513549ee8fc2c5aafeea3",
"started_at": "2026-11-16T19:17:01.565171373Z",
"running": true,
"user": "messenger",
"volumes": []
},
"db": {
"id": "06da5fd9fc7627b7b2abf38517a7fc1383d4dbcef905196eb98a39d826f34754",
"image": "sha256:665dc7b37ac52a17e298511414c21d23ffa4e6a6ef6225b3e5d59c7afb35e994",
"started_at": "2026-11-16T19:16:58.108621772Z",
"running": true,
"user": "",
"volumes": [
"kuznetsov-lab2_pgdata"
]
}
},
"after": {
"api": {
"id": "1e398850630740acd3ed5db30568b82af4232391bd9c235896cc09e860af88f0",
"image": "sha256:7c648de46909df8dd4c0e6676fc2c990a113960415c513549ee8fc2c5aafeea3",
"started_at": "2026-11-16T19:17:11.532370095Z",
"running": true,
"user": "messenger",
"volumes": []
},
"db": {
"id": "06da5fd9fc7627b7b2abf38517a7fc1383d4dbcef905196eb98a39d826f34754",
"image": "sha256:665dc7b37ac52a17e298511414c21d23ffa4e6a6ef6225b3e5d59c7afb35e994",
"started_at": "2026-11-16T19:17:11.161614921Z",
"running": true,
"user": "",
"volumes": [
"kuznetsov-lab2_pgdata"
]
}
}
}
+63
View File
@@ -0,0 +1,63 @@
{
"date_utc": "2026-11-16T19:16:34.757511+00:00",
"platform": "Windows-11-10.0.26200-SP0",
"source_commit": "65003b61195361aac238f19c9bff0027ca58e09e",
"worktree_dirty_at_start": true,
"project": "kuznetsov-lab2",
"status": "PASS",
"persistence": {
"public_persistence": "PASS",
"cursor_after_restart": "PASS",
"logout_after_restart": "PASS",
"migration_replay": "PASS",
"database": {
"users": 55,
"chats": 19,
"members": 38,
"messages": 118,
"sessions": 51,
"migrations": 1,
"expiry_fingerprint": "0aeb1b0d92290be0e10e5671487fda98",
"passwords_are_argon2id": true,
"session_hashes_only": true
}
},
"expiry": {
"ttl_seconds": 2,
"expiry_unchanged_on_restart": true,
"before_restart": 200,
"after_expiry_and_restart": 401,
"new_login": 201
},
"dependency": {
"live_without_db": 200,
"ready_without_db": 503,
"business_without_db": 503,
"recovered": 200
},
"containers": {
"api": {
"id": "1e398850630740acd3ed5db30568b82af4232391bd9c235896cc09e860af88f0",
"image": "sha256:7c648de46909df8dd4c0e6676fc2c990a113960415c513549ee8fc2c5aafeea3",
"started_at": "2026-11-16T19:17:11.532370095Z",
"running": true,
"user": "messenger",
"volumes": []
},
"db": {
"id": "06da5fd9fc7627b7b2abf38517a7fc1383d4dbcef905196eb98a39d826f34754",
"image": "sha256:665dc7b37ac52a17e298511414c21d23ffa4e6a6ef6225b3e5d59c7afb35e994",
"started_at": "2026-11-16T19:17:34.166389334Z",
"running": true,
"user": "",
"volumes": [
"kuznetsov-lab2_pgdata"
]
}
},
"docker": "29.9.3",
"resources": {
"cpus": 12,
"memory_bytes": 16591364096
}
}
+179
View File
@@ -0,0 +1,179 @@
$ ".\.venv\Scripts\python.exe" -X utf8 -m pip install --disable-pip-version-check --no-input --progress-bar off --quiet -r ".\requirements.lock" -r ".\requirements-validation.lock"
Environment ready; existing settings and secrets preserved
$ ".\.venv\Scripts\python.exe" -X utf8 scripts/check_contract.py
PASS: offline skeleton structure, local refs and Python syntax; application NOT tested.
$ ".\.venv\Scripts\python.exe" -X utf8 scripts/validate_openapi.py
PASS: full OpenAPI 3.1 and JSON Schema validation; application NOT tested.
$ docker compose --project-name kuznetsov-lab2 --env-file .env config --quiet
$ docker compose --project-name kuznetsov-lab2 --env-file .env config --quiet
$ docker compose --project-name kuznetsov-lab2 --env-file .env build api
Image mtusi-messenger:kuznetsov-lab2 Building
#1 [internal] load local bake definitions
#1 reading from stdin 1.18kB 0.0s done
#1 DONE 0.0s
#2 [internal] load build definition from Dockerfile
#2 transferring dockerfile: 728B done
#2 DONE 0.0s
#3 [internal] load metadata for docker.io/library/python:3.13.14-slim-bookworm@sha256:67a1e1f215ccda113cfc024e8639049257e88f273898f595b61476d128d387e8
#3 DONE 0.0s
#4 [internal] load .dockerignore
#4 transferring context: 216B 0.0s done
#4 DONE 0.0s
#5 [1/6] FROM docker.io/library/python:3.13.14-slim-bookworm@sha256:67a1e1f215ccda113cfc024e8639049257e88f273898f595b61476d128d387e8
#5 resolve docker.io/library/python:3.13.14-slim-bookworm@sha256:67a1e1f215ccda113cfc024e8639049257e88f273898f595b61476d128d387e8 0.0s done
#5 DONE 0.0s
#6 [internal] load build context
#6 transferring context: 70.23kB 0.0s done
#6 DONE 0.0s
#7 [2/6] WORKDIR /app
#7 CACHED
#8 [3/6] COPY requirements.lock .
#8 CACHED
#9 [4/6] RUN python -m pip install --no-cache-dir -r requirements.lock && useradd --uid 10001 --create-home messenger
#9 CACHED
#10 [5/6] COPY src ./src
#10 DONE 0.0s
#11 [6/6] COPY migrations ./migrations
#11 DONE 0.0s
#12 exporting to image
#12 exporting layers 0.1s done
#12 exporting manifest sha256:80fb9067e4bcfe6f7c134efe307969d54ff72de828b07281687bc3a4a3f1734e 0.0s done
#12 exporting config sha256:3cd872e9f0b2aba5dbea2556e722e00db041ee79c0fd126d2c03f44525066938 0.0s done
#12 exporting attestation manifest sha256:b1049f4bff79686bf39c12f60918c1b805330e3a74a117df84261c8aaed05852 0.0s done
#12 exporting manifest list sha256:7c648de46909df8dd4c0e6676fc2c990a113960415c513549ee8fc2c5aafeea3 0.0s done
#12 naming to docker.io/library/mtusi-messenger:kuznetsov-lab2 done
#12 unpacking to docker.io/library/mtusi-messenger:kuznetsov-lab2 0.0s done
#12 DONE 0.2s
#13 resolving provenance for metadata file
#13 DONE 0.0s
Image mtusi-messenger:kuznetsov-lab2 Built
$ docker compose --project-name kuznetsov-lab2 --env-file .env up -d --no-build db migrate api
Volume kuznetsov-lab2_pgdata Creating
Volume kuznetsov-lab2_pgdata Creating
Network kuznetsov-lab2_default Creating
Network kuznetsov-lab2_default Creating
Volume kuznetsov-lab2_pgdata Created
Volume kuznetsov-lab2_pgdata Created
Network kuznetsov-lab2_default Created
Network kuznetsov-lab2_default Created
Container kuznetsov-lab2-db-1 Creating
Container kuznetsov-lab2-db-1 Created
Container kuznetsov-lab2-migrate-1 Creating
Container kuznetsov-lab2-migrate-1 Created
Container kuznetsov-lab2-api-1 Creating
Container kuznetsov-lab2-api-1 Created
Container kuznetsov-lab2-db-1 Starting
Container kuznetsov-lab2-db-1 Started
Container kuznetsov-lab2-db-1 Waiting
Container kuznetsov-lab2-db-1 Healthy
Container kuznetsov-lab2-migrate-1 Starting
Container kuznetsov-lab2-migrate-1 Started
Container kuznetsov-lab2-migrate-1 Waiting
Container kuznetsov-lab2-migrate-1 Exited
Container kuznetsov-lab2-api-1 Starting
Container kuznetsov-lab2-api-1 Started
API ready: http://127.0.0.1:18102
$ ".\.venv\Scripts\python.exe" -X utf8 tests/smoke.py --base-url http://127.0.0.1:18102
PASS: lab2 public API scenario; infrastructure and grading evidence remain separate.
$ ".\.venv\Scripts\python.exe" -X utf8 -m unittest discover -s student-tests -v
test_auth_and_access (test_api.ApiTests.test_auth_and_access) ... ok
test_bad_cursors_and_limits (test_api.ApiTests.test_bad_cursors_and_limits) ... ok
test_chat_pagination (test_api.ApiTests.test_chat_pagination) ... ok
test_chat_validation_is_atomic (test_api.ApiTests.test_chat_validation_is_atomic) ... ok
test_invalid_json_and_content_type (test_api.ApiTests.test_invalid_json_and_content_type) ... ok
test_message_fields_and_unicode (test_api.ApiTests.test_message_fields_and_unicode) ... ok
test_message_pagination (test_api.ApiTests.test_message_pagination) ... ok
test_parallel_messages (test_api.ApiTests.test_parallel_messages) ... ok
test_parallel_username (test_api.ApiTests.test_parallel_username) ... ok
test_read_after_write (test_api.ApiTests.test_read_after_write) ... ok
test_user_fields (test_api.ApiTests.test_user_fields) ... ok
test_logout_only_revokes_current_session (test_sessions.SessionTests.test_logout_only_revokes_current_session) ... ok
test_malformed_basic_and_body (test_sessions.SessionTests.test_malformed_basic_and_body) ... ok
test_only_bearer_identifies_user (test_sessions.SessionTests.test_only_bearer_identifies_user) ... ok
test_password_boundaries_unicode_colon_spaces (test_sessions.SessionTests.test_password_boundaries_unicode_colon_spaces) ... ok
test_password_required_and_limits (test_sessions.SessionTests.test_password_required_and_limits) ... ok
test_wrong_and_unknown_credentials (test_sessions.SessionTests.test_wrong_and_unknown_credentials) ... ok
----------------------------------------------------------------------
Ran 17 tests in 5.307s
OK
$ ".\.venv\Scripts\python.exe" -X utf8 tests/resilience.py --mode persistence --action-script ".\scripts\restart.cmd" --base-url http://127.0.0.1:18102
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 compose --project-name kuznetsov-lab2 --env-file .env run --rm --no-deps migrate
Container kuznetsov-lab2-migrate-run-d87d482ec5d5 Creating
Container kuznetsov-lab2-migrate-run-d87d482ec5d5 Created
Already applied: 001_initial.sql
$ docker compose --project-name kuznetsov-lab2 --env-file .env run --rm --no-deps migrate
Container kuznetsov-lab2-migrate-run-eaca47dfbaf5 Creating
Container kuznetsov-lab2-migrate-run-eaca47dfbaf5 Created
Already applied: 001_initial.sql
PASS: old session/cursor, durable logout and repeatable migrations
$ docker compose --project-name kuznetsov-lab2-ttl --env-file .env up -d --no-build db migrate api
Network kuznetsov-lab2-ttl_default Creating
Volume kuznetsov-lab2-ttl_pgdata Creating
Volume kuznetsov-lab2-ttl_pgdata Creating
Network kuznetsov-lab2-ttl_default Creating
Volume kuznetsov-lab2-ttl_pgdata Created
Volume kuznetsov-lab2-ttl_pgdata Created
Network kuznetsov-lab2-ttl_default Created
Network kuznetsov-lab2-ttl_default Created
Container kuznetsov-lab2-ttl-db-1 Creating
Container kuznetsov-lab2-ttl-db-1 Created
Container kuznetsov-lab2-ttl-migrate-1 Creating
Container kuznetsov-lab2-ttl-migrate-1 Created
Container kuznetsov-lab2-ttl-api-1 Creating
Container kuznetsov-lab2-ttl-api-1 Created
Container kuznetsov-lab2-ttl-db-1 Starting
Container kuznetsov-lab2-ttl-db-1 Started
Container kuznetsov-lab2-ttl-db-1 Waiting
Container kuznetsov-lab2-ttl-db-1 Healthy
Container kuznetsov-lab2-ttl-migrate-1 Starting
Container kuznetsov-lab2-ttl-migrate-1 Started
Container kuznetsov-lab2-ttl-migrate-1 Waiting
Container kuznetsov-lab2-ttl-migrate-1 Exited
Container kuznetsov-lab2-ttl-api-1 Starting
Container kuznetsov-lab2-ttl-api-1 Started
API ready: http://127.0.0.1:18112
$ docker compose --project-name kuznetsov-lab2-ttl --env-file .env restart --timeout 5 api
Container kuznetsov-lab2-ttl-api-1 Restarting
Container kuznetsov-lab2-ttl-api-1 Started
PASS: TTL=2, expired token rejected after API restart; fresh login works
$ docker compose --project-name kuznetsov-lab2-ttl --env-file .env down --timeout 10
Container kuznetsov-lab2-ttl-api-1 Stopping
Container kuznetsov-lab2-ttl-api-1 Stopped
Container kuznetsov-lab2-ttl-api-1 Removing
Container kuznetsov-lab2-ttl-api-1 Removed
Container kuznetsov-lab2-ttl-migrate-1 Stopping
Container kuznetsov-lab2-ttl-migrate-1 Stopped
Container kuznetsov-lab2-ttl-migrate-1 Removing
Container kuznetsov-lab2-ttl-migrate-1 Removed
Container kuznetsov-lab2-ttl-db-1 Stopping
Container kuznetsov-lab2-ttl-db-1 Stopped
Container kuznetsov-lab2-ttl-db-1 Removing
Container kuznetsov-lab2-ttl-db-1 Removed
Network kuznetsov-lab2-ttl_default Removing
Network kuznetsov-lab2-ttl_default Removed
$ docker compose --project-name kuznetsov-lab2 --env-file .env stop --timeout 10 db
Container kuznetsov-lab2-db-1 Stopping
Container kuznetsov-lab2-db-1 Stopped
$ docker compose --project-name kuznetsov-lab2 --env-file .env start db
Container kuznetsov-lab2-db-1 Starting
Container kuznetsov-lab2-db-1 Started
PASS: DB outage -> live=200, ready/business=503; session works after recovery
$ ".\.venv\Scripts\python.exe" -X utf8 tests/smoke.py --base-url http://127.0.0.1:18102
PASS: lab2 public API scenario; infrastructure and grading evidence remain separate.
PASS: full lab2 verification
+40
View File
@@ -0,0 +1,40 @@
CREATE TABLE users (
id uuid PRIMARY KEY,
username varchar(32) NOT NULL UNIQUE CHECK (username ~ '^[a-z0-9_]{3,32}$'),
display_name varchar(100) NOT NULL CHECK (length(btrim(display_name)) > 0),
password_hash text NOT NULL,
created_at timestamptz NOT NULL DEFAULT clock_timestamp()
);
CREATE TABLE chats (
id uuid PRIMARY KEY,
title varchar(100) NOT NULL CHECK (length(btrim(title)) > 0),
created_at timestamptz NOT NULL DEFAULT clock_timestamp()
);
CREATE TABLE chat_members (
chat_id uuid NOT NULL REFERENCES chats(id),
user_id uuid NOT NULL REFERENCES users(id),
PRIMARY KEY (chat_id, user_id)
);
CREATE INDEX chat_members_user_idx ON chat_members(user_id, chat_id);
CREATE INDEX chats_history_idx ON chats(created_at, id);
CREATE TABLE messages (
id uuid PRIMARY KEY,
chat_id uuid NOT NULL REFERENCES chats(id),
sender_id uuid NOT NULL REFERENCES users(id),
text varchar(4000) NOT NULL CHECK (length(btrim(text)) > 0),
created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
FOREIGN KEY (chat_id, sender_id) REFERENCES chat_members(chat_id, user_id)
);
CREATE INDEX messages_history_idx ON messages(chat_id, created_at, id);
CREATE TABLE sessions (
token_hash char(64) PRIMARY KEY,
user_id uuid NOT NULL REFERENCES users(id),
created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
expires_at timestamptz NOT NULL,
CHECK (expires_at > created_at)
);
CREATE INDEX sessions_expiry_idx ON sessions(expires_at);
+18
View File
@@ -0,0 +1,18 @@
attrs==26.1.0
certifi==2026.10.23
charset-normalizer==3.5.2
idna==3.20
jsonschema==4.26.0
jsonschema-path==0.3.4
jsonschema-specifications==2025.9.1
lazy-object-proxy==1.13.0
openapi-schema-validator==0.6.3
openapi-spec-validator==0.7.2
pathable==0.4.4
pyyaml==6.0.3
referencing==0.36.2
requests==2.34.2
rfc3339-validator==0.1.4
rpds-py==2026.9.1
six==1.17.0
urllib3==2.8.0
+22
View File
@@ -0,0 +1,22 @@
annotated-doc==0.0.5
annotated-types==0.8.0
anyio==4.16.0
argon2-cffi==25.1.0
argon2-cffi-bindings==26.1.0
cffi==2.1.1
click==8.5.1
fastapi==0.162.2
h11==0.16.0
idna==3.20
opentelemetry-api==1.45.2
psycopg==3.3.8
psycopg-binary==3.3.8
psycopg-pool==3.3.3
pycparser==3.12
pydantic==2.14.3
pydantic_core==2.50.4
starlette==1.10.0
typing-inspection==0.4.5
typing_extensions==4.16.0
tzdata==2026.6
uvicorn==0.54.0
+351
View File
@@ -0,0 +1,351 @@
import argparse
from datetime import datetime, timezone
import hashlib
import json
import os
from pathlib import Path
import platform
import secrets
import shutil
import socket
import subprocess
import sys
import time
from urllib.error import URLError
from urllib.request import ProxyHandler, build_opener
import venv
ROOT = Path(__file__).resolve().parents[1]
LOG = []
LOCAL_HTTP = build_opener(ProxyHandler({}))
def settings():
path = ROOT / ".env"
result = {}
if path.exists():
for line in path.read_text(encoding="utf-8-sig").splitlines():
key, separator, value = line.partition("=")
if separator and not key.strip().startswith("#"):
result[key.strip()] = value.strip()
return result
def environment(args):
result = os.environ.copy()
result.update(PYTHONUTF8="1", PYTHONIOENCODING="utf-8", PYTHONDONTWRITEBYTECODE="1",
COMPOSE_PROJECT_NAME=args.project, HTTP_PORT=str(args.port),
SESSION_TTL_SECONDS=str(args.ttl), LAB2_PROJECT=args.project,
LAB2_PORT=str(args.port), LAB2_TTL=str(args.ttl))
for key in ("NO_PROXY", "no_proxy"):
result[key] = ",".join(filter(None, [result.get(key), "localhost", "127.0.0.1"]))
return result
def say(value):
line = str(value).replace(str(ROOT), ".")
print(line, flush=True)
LOG.append(line)
def run(args, command, quiet=False):
command = [str(item) for item in command]
if not quiet:
say("$ " + subprocess.list2cmdline(command))
with subprocess.Popen(command, cwd=ROOT, env=environment(args), stdout=subprocess.PIPE,
stderr=subprocess.STDOUT, text=True, encoding="utf-8", errors="replace") as process:
lines = []
for line in process.stdout:
lines.append(line)
if not quiet:
say(line.rstrip())
code = process.wait()
if code:
raise RuntimeError(f"{command[0]} exited with code {code}")
return "".join(lines)
def compose(args, *command, quiet=False):
if args.project != "kuznetsov-lab2" and not args.project.startswith("kuznetsov-lab2-"):
raise RuntimeError("This runner only manages Kuznetsov lab2 projects")
return run(args, ["docker", "compose", "--project-name", args.project, "--env-file", ".env", *command], quiet)
def interpreter():
return ROOT / ".venv" / ("Scripts/python.exe" if os.name == "nt" else "bin/python")
def evidence(name, value):
path = ROOT / "evidence" / name
path.parent.mkdir(exist_ok=True)
content = json.dumps(value, ensure_ascii=False, indent=2) if not isinstance(value, str) else value
path.write_text(content.rstrip() + "\n", encoding="utf-8", newline="\n")
def setup(args):
if not (ROOT / ".env").exists():
shutil.copyfile(ROOT / ".env.example", ROOT / ".env")
folder = ROOT / "secrets"
folder.mkdir(exist_ok=True)
password_file = folder / "postgres_password"
if not password_file.exists():
password_file.write_text(secrets.token_hex(32) + "\n", encoding="ascii")
password = password_file.read_text(encoding="ascii").strip()
values = {"database_url": f"postgresql://messenger:{password}@db:5432/messenger",
"cursor_key": secrets.token_hex(32)}
for name, value in values.items():
path = folder / name
if not path.exists():
path.write_text(value + "\n", encoding="ascii")
if os.name != "nt":
path.chmod(0o644)
if os.name != "nt":
folder.chmod(0o700)
password_file.chmod(0o644)
if not interpreter().exists():
venv.EnvBuilder(with_pip=True).create(ROOT / ".venv")
lockfiles = [ROOT / "requirements.lock", ROOT / "requirements-validation.lock"]
fingerprint = hashlib.sha256(b"".join(path.read_bytes() for path in lockfiles)).hexdigest()
marker = ROOT / ".venv/.lab-dependencies"
if not marker.exists() or marker.read_text(encoding="ascii") != fingerprint:
run(args, [interpreter(), "-X", "utf8", "-m", "pip", "install", "--disable-pip-version-check",
"--no-input", "--progress-bar", "off", "--quiet", "-r", lockfiles[0], "-r", lockfiles[1]])
marker.write_text(fingerprint, encoding="ascii")
say("Environment ready; existing settings and secrets preserved")
def base_url(args):
return f"http://127.0.0.1:{args.port}"
def wait_ready(args, timeout=90):
deadline = time.monotonic() + timeout
while time.monotonic() < deadline:
try:
with LOCAL_HTTP.open(base_url(args) + "/health/ready", timeout=4) as response:
if response.status == 200 and json.load(response) == {"status": "ok"}:
return
except (URLError, OSError, ValueError):
pass
time.sleep(0.3)
raise RuntimeError("API readiness timeout")
def build(args):
compose(args, "config", "--quiet")
compose(args, "build", "api")
def start(args):
current = compose(args, "ps", "--all", "--quiet", "api", quiet=True).strip()
if not current:
with socket.socket() as probe:
try:
probe.bind(("127.0.0.1", args.port))
except OSError:
raise RuntimeError(f"Port {args.port} is busy; stop its container or use --port") from None
compose(args, "up", "-d", "--no-build", "db", "migrate", "api")
wait_ready(args)
say("API ready: " + base_url(args))
def states(args):
result = {}
for service in ("api", "db"):
container = compose(args, "ps", "--all", "--quiet", service, quiet=True).strip()
if not container:
raise RuntimeError("Missing container: " + service)
data = json.loads(run(args, ["docker", "inspect", container], quiet=True))[0]
result[service] = {
"id": data["Id"], "image": data["Image"], "started_at": data["State"]["StartedAt"],
"running": data["State"]["Running"], "user": data["Config"]["User"],
"volumes": [mount["Name"] for mount in data["Mounts"] if mount["Type"] == "volume"],
}
return result
def restart(args):
before = states(args)
if not all(item["running"] for item in before.values()):
raise RuntimeError("Both API and DB must be running before restart")
compose(args, "restart", "--timeout", "10", "db", "api")
after = states(args)
for service in before:
if before[service]["started_at"] == after[service]["started_at"]:
raise RuntimeError("Container was not restarted: " + service)
if before[service]["volumes"] != after[service]["volumes"]:
raise RuntimeError("Volume changed during restart")
evidence("restart.json", {"project": args.project, "before": before, "after": after})
say("PASS: API and PostgreSQL restarted, volume preserved")
def contract_tests(args):
run(args, [interpreter(), "-X", "utf8", "scripts/check_contract.py"])
run(args, [interpreter(), "-X", "utf8", "scripts/validate_openapi.py"])
compose(args, "config", "--quiet")
def api_tests(args):
wait_ready(args)
run(args, [interpreter(), "-X", "utf8", "tests/smoke.py", "--base-url", base_url(args)])
os.environ["BASE_URL"] = base_url(args)
run(args, [interpreter(), "-X", "utf8", "-m", "unittest", "discover", "-s", "student-tests", "-v"])
def client(args):
sys.path.insert(0, str(ROOT / "tests"))
from client import Client
for key in ("NO_PROXY", "no_proxy"):
os.environ[key] = environment(args)[key]
return Client(base_url(args))
def database_stats(args):
sql = """SELECT json_build_object(
'users',(SELECT count(*) FROM users),'chats',(SELECT count(*) FROM chats),
'members',(SELECT count(*) FROM chat_members),'messages',(SELECT count(*) FROM messages),
'sessions',(SELECT count(*) FROM sessions),'migrations',(SELECT count(*) FROM schema_migrations),
'expiry_fingerprint',(SELECT md5(COALESCE(string_agg(user_id::text || created_at::text || expires_at::text,
',' ORDER BY token_hash),'')) FROM sessions),
'passwords_are_argon2id',(SELECT bool_and(password_hash LIKE '$argon2id$%') FROM users),
'session_hashes_only',(SELECT bool_and(token_hash ~ '^[0-9a-f]{64}$') FROM sessions))"""
result = compose(args, "exec", "-T", "db", "psql", "-U", "messenger", "-d", "messenger",
"-At", "-c", sql, quiet=True)
return json.loads(result)
def restart_check(args):
from urllib.parse import urlencode
c = client(args)
alice, bob, eve, chat = c.bootstrap()
path = "/api/v1/chats/" + chat["id"] + "/messages"
c.message(alice, chat, "first")
last = c.message(bob, chat, "second")["json"]
page = c.call("GET", path + "?limit=1", actor=alice, schema="MessagePage")["json"]
revoked = c.user("revoked")
c.call("DELETE", "/api/v1/auth/sessions/current", 204, actor=revoked)
hook = ROOT / "scripts" / ("restart.cmd" if os.name == "nt" else "restart.sh")
run(args, [interpreter(), "-X", "utf8", "tests/resilience.py", "--mode", "persistence",
"--action-script", hook, "--base-url", base_url(args)])
continued = c.call("GET", path + "?" + urlencode({"cursor": page["next_cursor"]}),
actor=alice, schema="MessagePage")["json"]
if continued != {"items": [last], "next_cursor": None}:
raise RuntimeError("Old cursor did not survive restart")
c.call("GET", "/api/v1/users/me", 401, actor=revoked)
c.call("GET", "/api/v1/chats/" + chat["id"], 404, actor=eve)
before = database_stats(args)
compose(args, "run", "--rm", "--no-deps", "migrate")
compose(args, "run", "--rm", "--no-deps", "migrate")
after = database_stats(args)
if before != after or after["migrations"] != 1:
raise RuntimeError("Migration replay changed existing data")
if not after["passwords_are_argon2id"] or not after["session_hashes_only"]:
raise RuntimeError("Unexpected credential representation in DB")
result = {"public_persistence": "PASS", "cursor_after_restart": "PASS",
"logout_after_restart": "PASS", "migration_replay": "PASS", "database": after}
evidence("persistence.json", result)
say("PASS: old session/cursor, durable logout and repeatable migrations")
return result
def ttl_check(args):
test = argparse.Namespace(project=args.project + "-ttl", port=args.test_port, ttl=2,
test_port=args.test_port, action="start")
try:
start(test)
c = client(test)
actor = c.user("expiry")
c.call("GET", "/api/v1/users/me", actor=actor, schema="User")
before = database_stats(test)["expiry_fingerprint"]
compose(test, "restart", "--timeout", "5", "api")
wait_ready(test)
after = database_stats(test)["expiry_fingerprint"]
if before != after:
raise RuntimeError("Session expiry changed during restart")
time.sleep(2.2)
c.call("GET", "/api/v1/users/me", 401, actor=actor)
registration = actor["registration"]
import base64
basic = base64.b64encode((registration["username"] + ":" + registration["password"]).encode()).decode()
c.call("POST", "/api/v1/auth/sessions", 201, headers={"Authorization": "Basic " + basic}, schema="Session")
say("PASS: TTL=2, expired token rejected after API restart; fresh login works")
result = {"ttl_seconds": 2, "expiry_unchanged_on_restart": True, "before_restart": 200,
"after_expiry_and_restart": 401, "new_login": 201}
evidence("expiry.json", result)
return result
finally:
compose(test, "down", "--timeout", "10")
def dependency_check(args):
c = client(args)
actor = c.user("dbcheck")
try:
compose(args, "stop", "--timeout", "10", "db")
c.call("GET", "/health/live", schema="Health")
c.call("GET", "/health/ready", 503)
c.call("GET", "/api/v1/users/me", 503, actor=actor)
finally:
compose(args, "start", "db")
wait_ready(args)
c.call("GET", "/api/v1/users/me", actor=actor, schema="User")
say("PASS: DB outage -> live=200, ready/business=503; session works after recovery")
return {"live_without_db": 200, "ready_without_db": 503, "business_without_db": 503, "recovered": 200}
def verify(args):
summary = {"date_utc": datetime.now(timezone.utc).isoformat(), "platform": platform.platform(),
"source_commit": run(args, ["git", "rev-parse", "HEAD"], quiet=True).strip(),
"worktree_dirty_at_start": bool(run(args, ["git", "status", "--porcelain"], quiet=True).strip()),
"project": args.project, "status": "FAIL"}
try:
setup(args)
contract_tests(args)
build(args)
start(args)
api_tests(args)
summary["persistence"] = restart_check(args)
summary["expiry"] = ttl_check(args)
summary["dependency"] = dependency_check(args)
run(args, [interpreter(), "-X", "utf8", "tests/smoke.py", "--base-url", base_url(args)])
summary["containers"] = states(args)
summary["docker"] = run(args, ["docker", "version", "--format", "{{.Server.Version}}"], quiet=True).strip()
summary["resources"] = json.loads(run(
args, ["docker", "info", "--format", '{"cpus":{{.NCPU}},"memory_bytes":{{.MemTotal}}}'], quiet=True))
summary["status"] = "PASS"
say("PASS: full lab2 verification")
finally:
evidence("verification.txt", "\n".join(LOG))
evidence("summary.json", summary)
def main():
saved = settings()
parser = argparse.ArgumentParser()
parser.add_argument("action", choices=["setup", "build", "start", "test", "restart",
"restart-check", "stop", "verify"])
parser.add_argument("--project", default=os.environ.get("LAB2_PROJECT", saved.get("COMPOSE_PROJECT_NAME", "kuznetsov-lab2")))
parser.add_argument("--port", type=int, default=int(os.environ.get("LAB2_PORT", saved.get("HTTP_PORT", "18102"))))
parser.add_argument("--ttl", type=int, default=int(os.environ.get("LAB2_TTL", saved.get("SESSION_TTL_SECONDS", "3600"))))
parser.add_argument("--test-port", type=int, default=18112)
args = parser.parse_args()
if args.project != "kuznetsov-lab2" and not args.project.startswith("kuznetsov-lab2-"):
parser.error("Use a dedicated Kuznetsov lab2 project")
if not 1 <= args.port <= 65535 or not 1 <= args.test_port <= 65535 or not 1 <= args.ttl <= 86400:
parser.error("Invalid port or session TTL")
if args.action in ("setup", "build", "start", "test", "restart-check"):
setup(args)
actions = {"setup": lambda item: None, "build": build, "start": start,
"test": lambda item: (contract_tests(item), api_tests(item)),
"restart": restart, "restart-check": restart_check, "verify": verify,
"stop": lambda item: compose(item, "stop", "--timeout", "10", "api", "db")}
actions[args.action](args)
if __name__ == "__main__":
try:
main()
except (RuntimeError, OSError, ValueError) as error:
print("FAIL: " + str(error), file=sys.stderr)
raise SystemExit(1)
+3
View File
@@ -0,0 +1,3 @@
@echo off
python "%~dp0lab.py" restart
exit /b %errorlevel%
+4
View File
@@ -0,0 +1,4 @@
#!/bin/sh
set -eu
TASK_DIR=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
exec python3 "$TASK_DIR/lab.py" restart
+1
View File
@@ -0,0 +1 @@
+21
View File
@@ -0,0 +1,21 @@
import logging
import os
import uvicorn
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO, format="%(levelname)s %(message)s")
logging.getLogger("messenger").info("environment=%s", os.environ.get("APP_ENV", "lab"))
port = int(os.environ.get("APP_PORT", "8080"))
if not 1 <= port <= 65535:
raise SystemExit("APP_PORT должен быть от 1 до 65535")
uvicorn.run(
"src.app:create_app",
factory=True,
host=os.environ.get("APP_HOST", "0.0.0.0"),
port=port,
workers=1,
access_log=False,
timeout_graceful_shutdown=5,
)
+155
View File
@@ -0,0 +1,155 @@
from contextlib import asynccontextmanager
import logging
import re
from uuid import UUID, uuid4
from fastapi import FastAPI, Request, Response
from fastapi.exceptions import RequestValidationError
from fastapi.responses import JSONResponse
from starlette.concurrency import run_in_threadpool
from starlette.exceptions import HTTPException
from .errors import ApiError, invalid
from .models import CreateChat, CreateMessage, CreateUser
from .security import basic_credentials, bearer_token
from .storage import PostgresStore
logger = logging.getLogger("messenger")
SESSION_PATH = "/api/v1/auth/sessions"
def uuid_value(value):
try:
return UUID(value)
except ValueError:
raise invalid("Неверный UUID") from None
def page_args(request):
raw = request.query_params.get("limit", "50")
normalized = raw.lstrip("0") or "0"
if re.fullmatch(r"[0-9]+", raw) is None or len(normalized) > 3 or not 1 <= int(normalized) <= 100:
raise invalid("Limit должен быть от 1 до 100")
return int(normalized), request.query_params.get("cursor")
def create_app():
store = PostgresStore()
@asynccontextmanager
async def lifespan(app):
await run_in_threadpool(store.open)
try:
yield
finally:
await run_in_threadpool(store.close)
app = FastAPI(redirect_slashes=False, docs_url=None, redoc_url=None, openapi_url=None, lifespan=lifespan)
app.state.store = store
def error_response(request, error):
request_id = request.state.request_id
headers = {}
if error.status == 401:
headers["WWW-Authenticate"] = (
'Basic realm="messenger", charset="UTF-8"' if request.url.path == SESSION_PATH
else 'Bearer realm="messenger"'
)
return JSONResponse(
status_code=error.status, headers=headers,
content={"error": {"code": error.code, "message": error.message, "request_id": request_id}},
)
@app.middleware("http")
async def request_context(request, call_next):
request.state.request_id = str(uuid4())
try:
path = request.url.path
registration = path == "/api/v1/users" and request.method == "POST"
login = path == SESSION_PATH and request.method == "POST"
if login:
request.state.credentials = basic_credentials(request.headers.get("Authorization"))
if await request.body():
raise invalid("Вход не принимает тело запроса")
elif path.startswith("/api/v1/") and not registration:
token = bearer_token(request.headers.get("Authorization"))
request.state.user_id = await run_in_threadpool(store.authenticate, token)
request.state.token = token
if request.method == "POST" and path.startswith("/api/v1/") and not login:
media_type = request.headers.get("Content-Type", "").split(";", 1)[0].strip().lower()
if media_type != "application/json":
raise ApiError(415, "unsupported_media_type", "Нужен application/json")
try:
(await request.body()).decode("utf-8")
except UnicodeDecodeError:
raise invalid("JSON должен быть в UTF-8") from None
response = await call_next(request)
except ApiError as error:
response = error_response(request, error)
except Exception as error:
logger.error("request failed: %s", type(error).__name__)
response = error_response(request, ApiError(500, "internal_error", "Внутренняя ошибка"))
response.headers["X-Request-Id"] = request.state.request_id
logger.info("%s %s %s request_id=%s", request.method, request.url.path, response.status_code, request.state.request_id)
return response
@app.exception_handler(ApiError)
async def api_error(request, error):
return error_response(request, error)
@app.exception_handler(RequestValidationError)
async def validation_error(request, error):
return error_response(request, invalid("Неверные поля запроса"))
@app.exception_handler(HTTPException)
async def http_error(request, error):
code = "not_found" if error.status_code == 404 else "invalid_request"
return error_response(request, ApiError(error.status_code, code, "Неверный запрос"))
@app.get("/health/live")
def live():
return {"status": "ok"}
@app.get("/health/ready")
def ready():
return store.ready()
@app.post("/api/v1/users", status_code=201)
def create_user(body: CreateUser):
return store.create_user(body)
@app.post(SESSION_PATH, status_code=201)
def login(request: Request):
return store.create_session(*request.state.credentials)
@app.delete(SESSION_PATH + "/current", status_code=204)
def logout(request: Request):
store.logout(request.state.token)
return Response(status_code=204)
@app.get("/api/v1/users/me")
def me(request: Request):
return store.user(request.state.user_id)
@app.post("/api/v1/chats", status_code=201)
def create_chat(body: CreateChat, request: Request):
return store.create_chat(request.state.user_id, body)
@app.get("/api/v1/chats")
def list_chats(request: Request):
return store.list_chats(request.state.user_id, *page_args(request))
@app.get("/api/v1/chats/{chat_id}")
def chat(chat_id: str, request: Request):
return store.chat(request.state.user_id, uuid_value(chat_id))
@app.post("/api/v1/chats/{chat_id}/messages", status_code=201)
def create_message(chat_id: str, body: CreateMessage, request: Request):
return store.create_message(request.state.user_id, uuid_value(chat_id), body)
@app.get("/api/v1/chats/{chat_id}/messages")
def messages(chat_id: str, request: Request):
return store.list_messages(request.state.user_id, uuid_value(chat_id), *page_args(request))
return app
+16
View File
@@ -0,0 +1,16 @@
import os
from pathlib import Path
def secret(name):
value = Path(os.environ[name]).read_text(encoding="utf-8").strip()
if not value:
raise ValueError(f"Empty secret file: {name}")
return value
def session_ttl():
value = int(os.environ.get("SESSION_TTL_SECONDS", "3600"))
if not 1 <= value <= 86400:
raise ValueError("SESSION_TTL_SECONDS must be between 1 and 86400")
return value
+14
View File
@@ -0,0 +1,14 @@
class ApiError(Exception):
def __init__(self, status, code, message):
super().__init__(message)
self.status = status
self.code = code
self.message = message
def invalid(message="Неверный запрос"):
return ApiError(400, "invalid_request", message)
def not_found():
return ApiError(404, "not_found", "Объект не найден")
+50
View File
@@ -0,0 +1,50 @@
import hashlib
import logging
from pathlib import Path
import psycopg
from .config import secret
def migrate():
files = sorted((Path(__file__).resolve().parents[1] / "migrations").glob("*.sql"))
if not files:
raise RuntimeError("No migrations found")
with psycopg.connect(secret("DATABASE_URL_FILE"), connect_timeout=5) as connection:
with connection.transaction():
connection.execute("SELECT pg_advisory_xact_lock(230402)")
connection.execute("""
CREATE TABLE IF NOT EXISTS schema_migrations (
version text PRIMARY KEY,
checksum char(64) NOT NULL,
applied_at timestamptz NOT NULL DEFAULT clock_timestamp()
)
""")
for file in files:
content = file.read_text(encoding="utf-8")
checksum = hashlib.sha256(content.encode("utf-8")).hexdigest()
with connection.transaction():
connection.execute("SELECT pg_advisory_xact_lock(230402)")
old = connection.execute(
"SELECT checksum FROM schema_migrations WHERE version = %s", (file.name,)
).fetchone()
if old:
if old[0] != checksum:
raise RuntimeError("Applied migration was changed")
print("Already applied: " + file.name, flush=True)
continue
connection.execute(content)
connection.execute(
"INSERT INTO schema_migrations(version, checksum) VALUES (%s, %s)",
(file.name, checksum),
)
print("Applied: " + file.name, flush=True)
if __name__ == "__main__":
try:
migrate()
except Exception as error:
logging.error("Migration failed: %s", type(error).__name__)
raise SystemExit(1)
+51
View File
@@ -0,0 +1,51 @@
import re
from typing import Annotated
from uuid import UUID
from pydantic import AfterValidator, BaseModel, ConfigDict, Field, StringConstraints, field_validator
def nonblank(value: str) -> str:
if not value.strip() or "\x00" in value:
raise ValueError("Пустая строка или NUL")
value.encode("utf-8")
return value
Name = Annotated[str, StringConstraints(min_length=1, max_length=100), AfterValidator(nonblank)]
Text = Annotated[str, StringConstraints(min_length=1, max_length=4000), AfterValidator(nonblank)]
Password = Annotated[str, StringConstraints(min_length=12, max_length=128), AfterValidator(nonblank)]
UserId = Annotated[UUID, Field(strict=False)]
class RequestBody(BaseModel):
model_config = ConfigDict(extra="forbid", strict=True)
class CreateUser(RequestBody):
username: str
display_name: Name
password: Password = Field(repr=False)
@field_validator("username")
@classmethod
def check_username(cls, value):
if re.fullmatch(r"[a-z0-9_]{3,32}", value) is None:
raise ValueError("Неверный username")
return value
class CreateChat(RequestBody):
title: Name
member_ids: list[UserId] = Field(min_length=1, max_length=100)
@field_validator("member_ids")
@classmethod
def unique_members(cls, value):
if len(set(value)) != len(value):
raise ValueError("Повтор участника")
return value
class CreateMessage(RequestBody):
text: Text
+44
View File
@@ -0,0 +1,44 @@
import base64
import hashlib
import hmac
import json
from datetime import datetime, timezone
from uuid import UUID
from .errors import invalid
class Pagination:
def __init__(self, secret):
self.secret = secret
def encode(self, scope, item):
data = json.dumps([1, scope, item["created_at"], item["id"]], separators=(",", ":"))
payload = base64.urlsafe_b64encode(data.encode()).rstrip(b"=").decode()
signature = hmac.new(self.secret, payload.encode(), hashlib.sha256).hexdigest()
return payload + "." + signature
def decode(self, scope, cursor):
try:
if not isinstance(cursor, str) or not cursor or len(cursor) > 2048:
raise ValueError()
payload, signature = cursor.split(".")
expected = hmac.new(self.secret, payload.encode("ascii"), hashlib.sha256).hexdigest()
if not hmac.compare_digest(signature, expected):
raise ValueError()
data = json.loads(base64.b64decode(payload + "=" * (-len(payload) % 4), altchars=b"-_", validate=True))
if not isinstance(data, list) or len(data) != 4 or data[:2] != [1, scope]:
raise ValueError()
if not isinstance(data[2], str) or not data[2].endswith("Z"):
raise ValueError()
created_at = datetime.fromisoformat(data[2].replace("Z", "+00:00"))
if created_at.utcoffset() != timezone.utc.utcoffset(created_at):
raise ValueError()
return created_at, UUID(data[3])
except (ValueError, TypeError, UnicodeError, KeyError, OverflowError):
raise invalid("Неверный cursor") from None
def page(self, rows, scope, limit):
items = rows[:limit]
cursor = self.encode(scope, items[-1]) if len(rows) > limit else None
return {"items": items, "next_cursor": cursor}
+63
View File
@@ -0,0 +1,63 @@
import base64
import binascii
import hashlib
import re
import secrets
from threading import BoundedSemaphore
from argon2 import PasswordHasher
from argon2.exceptions import VerificationError
from argon2.low_level import Type
from .errors import ApiError
HASHER = PasswordHasher(time_cost=2, memory_cost=19456, parallelism=1, type=Type.ID)
HASH_SLOTS = BoundedSemaphore(4)
DUMMY_HASH = HASHER.hash(secrets.token_urlsafe(32))
def unauthorized():
return ApiError(401, "unauthorized", "Неверные данные авторизации")
def hash_password(password):
with HASH_SLOTS:
return HASHER.hash(password)
def verify_password(password_hash, password):
try:
with HASH_SLOTS:
return HASHER.verify(password_hash or DUMMY_HASH, password)
except (VerificationError, ValueError):
return False
def basic_credentials(header):
try:
scheme, encoded = header.split(" ", 1)
if scheme.lower() != "basic" or len(encoded) > 2048:
raise ValueError()
decoded = base64.b64decode(encoded, validate=True).decode("utf-8")
username, separator, password = decoded.partition(":")
if not separator or re.fullmatch(r"[a-z0-9_]{3,32}", username) is None:
raise ValueError()
if not 12 <= len(password) <= 128 or not password.strip() or "\x00" in password:
raise ValueError()
return username, password
except (ValueError, UnicodeError, AttributeError, binascii.Error):
raise unauthorized() from None
def bearer_token(header):
try:
scheme, token = header.split(" ", 1)
if scheme.lower() != "bearer" or not re.fullmatch(r"[A-Za-z0-9_-]{43}", token):
raise ValueError()
return token
except (ValueError, TypeError, AttributeError):
raise unauthorized() from None
def token_hash(token):
return hashlib.sha256(token.encode("ascii")).hexdigest()
+194
View File
@@ -0,0 +1,194 @@
from contextlib import contextmanager
from datetime import datetime
import logging
import secrets
from uuid import UUID, uuid4
import psycopg
from psycopg.rows import dict_row
from psycopg_pool import ConnectionPool, PoolTimeout, TooManyRequests
from .config import secret, session_ttl
from .errors import ApiError, invalid, not_found
from .pagination import Pagination
from .security import hash_password, token_hash, unauthorized, verify_password
USER_FIELDS = "id, username, display_name, created_at"
CHAT_FIELDS = """
c.id, c.title, c.created_at,
ARRAY(SELECT cm.user_id FROM chat_members cm WHERE cm.chat_id=c.id ORDER BY cm.user_id) AS member_ids
"""
def serialize(value):
if isinstance(value, UUID):
return str(value)
if isinstance(value, datetime):
return value.isoformat(timespec="microseconds").replace("+00:00", "Z")
if isinstance(value, dict):
return {key: serialize(item) for key, item in value.items()}
if isinstance(value, list):
return [serialize(item) for item in value]
return value
class PostgresStore:
def __init__(self):
logging.getLogger("psycopg.pool").setLevel(logging.CRITICAL)
self.pool = ConnectionPool(
secret("DATABASE_URL_FILE"), min_size=1, max_size=10, timeout=3, max_waiting=40,
open=False, check=ConnectionPool.check_connection,
kwargs={"row_factory": dict_row, "connect_timeout": 3,
"options": "-c timezone=UTC -c statement_timeout=5000 -c lock_timeout=2000"},
)
self.pagination = Pagination(secret("CURSOR_SECRET_FILE").encode("ascii"))
self.ttl = session_ttl()
def open(self):
self.pool.open()
def close(self):
self.pool.close()
@contextmanager
def connection(self):
try:
with self.pool.connection() as connection:
yield connection
except (psycopg.OperationalError, psycopg.InterfaceError, PoolTimeout, TooManyRequests,
psycopg.errors.QueryCanceled, psycopg.errors.LockNotAvailable):
raise ApiError(503, "unavailable", "Хранилище временно недоступно") from None
def ready(self):
try:
with self.connection() as connection:
row = connection.execute(
"SELECT version FROM schema_migrations WHERE version=%s", ("001_initial.sql",)
).fetchone()
if not row:
raise ApiError(503, "unavailable", "Схема ещё не готова")
except psycopg.errors.UndefinedTable:
raise ApiError(503, "unavailable", "Схема ещё не готова") from None
return {"status": "ok"}
def create_user(self, body):
password_hash = hash_password(body.password)
try:
with self.connection() as connection:
row = connection.execute(
"INSERT INTO users(id,username,display_name,password_hash) VALUES (%s,%s,%s,%s) RETURNING "
+ USER_FIELDS,
(uuid4(), body.username, body.display_name, password_hash),
).fetchone()
return serialize(row)
except psycopg.errors.UniqueViolation:
raise ApiError(409, "conflict", "Username уже занят") from None
def create_session(self, username, password):
with self.connection() as connection:
row = connection.execute(
"SELECT " + USER_FIELDS + ", password_hash FROM users WHERE username=%s", (username,)
).fetchone()
if not verify_password(row["password_hash"] if row else None, password) or row is None:
raise unauthorized()
token = secrets.token_urlsafe(32)
with self.connection() as connection:
session = connection.execute("""
INSERT INTO sessions(token_hash,user_id,created_at,expires_at)
VALUES (%s,%s,CURRENT_TIMESTAMP,CURRENT_TIMESTAMP + %s * INTERVAL '1 second')
RETURNING expires_at
""", (token_hash(token), row["id"], self.ttl)).fetchone()
user = {key: value for key, value in row.items() if key != "password_hash"}
return {"token": token, "token_type": "Bearer", "expires_at": serialize(session["expires_at"]),
"user": serialize(user)}
def authenticate(self, token):
with self.connection() as connection:
row = connection.execute("""
SELECT user_id FROM sessions
WHERE token_hash=%s AND expires_at > CURRENT_TIMESTAMP
""", (token_hash(token),)).fetchone()
if row is None:
raise unauthorized()
return row["user_id"]
def logout(self, token):
with self.connection() as connection:
result = connection.execute("DELETE FROM sessions WHERE token_hash=%s", (token_hash(token),))
if result.rowcount != 1:
raise unauthorized()
def user(self, user_id):
with self.connection() as connection:
row = connection.execute("SELECT " + USER_FIELDS + " FROM users WHERE id=%s", (user_id,)).fetchone()
if not row:
raise unauthorized()
return serialize(row)
def create_chat(self, user_id, body):
members = body.member_ids
if user_id not in members:
raise invalid("Неверный список участников")
with self.connection() as connection:
found = connection.execute("SELECT id FROM users WHERE id=ANY(%s)", (members,)).fetchall()
if len(found) != len(members):
raise invalid("Неверный список участников")
chat_id = uuid4()
# чат и участники коммитятся вместе
connection.execute("INSERT INTO chats(id,title) VALUES (%s,%s)", (chat_id, body.title))
with connection.cursor() as cursor:
cursor.executemany(
"INSERT INTO chat_members(chat_id,user_id) VALUES (%s,%s)",
[(chat_id, member) for member in members],
)
return serialize(self.require_chat(connection, user_id, chat_id))
def require_chat(self, connection, user_id, chat_id):
row = connection.execute(
"SELECT " + CHAT_FIELDS + """ 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)
""", (chat_id, user_id)
).fetchone()
if row is None:
raise not_found()
return row
def chat(self, user_id, chat_id):
with self.connection() as connection:
return serialize(self.require_chat(connection, user_id, chat_id))
def list_chats(self, user_id, limit, cursor):
scope = "chats:" + str(user_id)
after = self.pagination.decode(scope, cursor) if cursor is not None else None
condition = " AND (c.created_at,c.id) > (%s,%s)" if after else ""
with self.connection() as connection:
rows = connection.execute(
"SELECT " + CHAT_FIELDS + """ FROM chats c
WHERE EXISTS (SELECT 1 FROM chat_members m WHERE m.chat_id=c.id AND m.user_id=%s)"""
+ condition + " ORDER BY c.created_at,c.id LIMIT %s",
[user_id, *(after or ()), limit + 1],
).fetchall()
return self.pagination.page(serialize(rows), scope, limit)
def create_message(self, user_id, chat_id, body):
with self.connection() as connection:
self.require_chat(connection, user_id, chat_id)
row = connection.execute("""
INSERT INTO messages(id,chat_id,sender_id,text) VALUES (%s,%s,%s,%s)
RETURNING id,chat_id,sender_id,text,created_at
""", (uuid4(), chat_id, user_id, body.text)).fetchone()
return serialize(row)
def list_messages(self, user_id, chat_id, limit, cursor):
scope = "messages:" + str(chat_id)
after = self.pagination.decode(scope, cursor) if cursor is not None else None
condition = " AND (created_at,id) > (%s,%s)" if after else ""
with self.connection() as connection:
self.require_chat(connection, user_id, chat_id)
rows = connection.execute(
"SELECT id,chat_id,sender_id,text,created_at FROM messages WHERE chat_id=%s"
+ condition + " ORDER BY created_at,id LIMIT %s",
[chat_id, *(after or ()), limit + 1],
).fetchall()
return self.pagination.page(serialize(rows), scope, limit)
+30
View File
@@ -0,0 +1,30 @@
import os
from pathlib import Path
import sys
from urllib.parse import urlsplit
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT))
sys.path.insert(0, str(ROOT / "tests"))
from client import Client as PublicClient
from scripts import lab
def project_name():
name = os.environ.get("LAB2_PROJECT", lab.settings().get("COMPOSE_PROJECT_NAME", "kuznetsov-lab2"))
if name != "kuznetsov-lab2" and not name.startswith("kuznetsov-lab2-"):
raise RuntimeError("Tests only operate on Kuznetsov lab2 projects")
return name
class Client(PublicClient):
def __init__(self, base_url=None, ca_file=None, timeout=10):
project_name()
port = int(os.environ.get("LAB2_PORT", lab.settings().get("HTTP_PORT", "18102")))
target = base_url or os.environ.get("BASE_URL") or f"http://127.0.0.1:{port}"
parsed = urlsplit(target)
if parsed.scheme != "http" or parsed.hostname not in ("127.0.0.1", "localhost") or parsed.port != port:
raise RuntimeError("BASE_URL must match this lab's local port")
for name in ("NO_PROXY", "no_proxy"):
os.environ[name] = ",".join(filter(None, [os.environ.get(name), "localhost", "127.0.0.1"]))
super().__init__(target, ca_file=ca_file, timeout=timeout)
+180
View File
@@ -0,0 +1,180 @@
from concurrent.futures import ThreadPoolExecutor
import os
from pathlib import Path
import sys
import unittest
from urllib.parse import urlencode
from uuid import uuid4
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "tests"))
from helpers import Client
from client import timestamp
class ApiTests(unittest.TestCase):
def setUp(self):
self.client = Client(os.environ.get("BASE_URL", "http://127.0.0.1:18102"))
self.alice, self.bob, self.eve, self.chat = self.client.bootstrap()
self.path = "/api/v1/chats/" + self.chat["id"]
def error(self, method, path, status=400, **kwargs):
response = self.client.call(method, path, status, **kwargs)
code = {400: "invalid_request", 401: "unauthorized", 404: "not_found", 409: "conflict", 415: "unsupported_media_type"}[status]
self.assertEqual(response["json"]["error"]["code"], code)
def get_page(self, path, actor=None, **query):
schema = "MessagePage" if path.endswith("/messages") else "ChatPage"
return self.client.call("GET", path + "?" + urlencode(query), actor=actor or self.alice, schema=schema)["json"]
def new_chat(self):
return self.client.call("POST", "/api/v1/chats", 201, actor=self.alice,
body={"title": "Ещё чат", "member_ids": [self.alice["id"], self.bob["id"]]}, schema="Chat")["json"]
def test_user_fields(self):
name = "u_" + uuid4().hex[:20]
bodies = [
{"username": "ab", "display_name": "Имя"},
{"username": "ABC", "display_name": "Имя"},
{"username": "valid\n", "display_name": "Имя"},
{"username": 123, "display_name": "Имя"},
{"username": name, "display_name": " \t\n"},
{"username": name, "display_name": 123},
{"username": name, "display_name": None},
{"username": name, "display_name": "я" * 101},
{"username": name, "display_name": "Имя", "password": "extra"},
{"username": name},
]
for body in bodies:
body.setdefault("password", "T3st-password-123")
with self.subTest(fields=sorted(body)):
self.error("POST", "/api/v1/users", body=body)
user = self.client.call("POST", "/api/v1/users", 201,
body={"username": name, "display_name": "👋" * 100, "password": "T3st-password-123"}, schema="User")["json"]
self.assertEqual(user["display_name"], "👋" * 100)
self.error("POST", "/api/v1/users", 409, body={"username": name, "display_name": "Повтор", "password": "T3st-password-123"})
def test_invalid_json_and_content_type(self):
for raw in (b'{"username":', b"null", b"[]", b"true", b"\xff", '{}'.encode('utf-16')):
with self.subTest(raw=raw):
self.error("POST", "/api/v1/users", raw=raw, headers={"Content-Type": "application/json"})
self.error("POST", "/api/v1/users", 415, raw=b"text", headers={"Content-Type": "text/plain"})
self.error("POST", "/api/v1/users", 415, raw=b"{}")
def test_auth_and_access(self):
self.error("GET", "/api/v1/users/me", 401)
for value in (str(uuid4()), "bad-id"):
self.error("GET", "/api/v1/users/me", 401, headers={"X-User-Id": value})
self.error("GET", "/api/v1/chats/bad-id", 401)
self.error("GET", "/api/v1/chats/bad-id", actor=self.alice)
self.error("GET", "/api/v1/chats/" + str(uuid4()), 404, actor=self.alice)
self.error("GET", self.path, 404, actor=self.eve)
self.error("GET", self.path + "/messages", 404, actor=self.eve)
self.error("POST", self.path + "/messages", 404, actor=self.eve, body={"text": "Чужое"})
self.assertEqual(self.get_page("/api/v1/chats", self.eve)["items"], [])
def test_chat_validation_is_atomic(self):
before = self.get_page("/api/v1/chats")["items"]
valid = [self.alice["id"], self.bob["id"]]
for members in ([], [self.bob["id"]], [self.alice["id"], str(uuid4())],
[self.alice["id"], self.alice["id"].upper()], ["bad-id"], valid * 51):
self.error("POST", "/api/v1/chats", actor=self.alice, body={"title": "Чат", "member_ids": members})
for title in ("", " \t", "я" * 101, 123):
self.error("POST", "/api/v1/chats", actor=self.alice, body={"title": title, "member_ids": valid})
self.error("POST", "/api/v1/chats", actor=self.alice, body={"title": "Чат", "member_ids": valid, "extra": 1})
self.assertEqual(before, self.get_page("/api/v1/chats")["items"])
def test_message_fields_and_unicode(self):
bodies = [{"text": value} for value in ("", " \t\n", "я" * 4001, None, 123, True, [])]
bodies += [{}, {"text": "Hi", "sender_id": self.bob["id"]}, {"text": "Hi", "attachment_ids": []}]
for body in bodies:
self.error("POST", self.path + "/messages", actor=self.alice, body=body)
for text in (" Привет, мир 👋\n", "👋" * 4000):
message = self.client.message(self.alice, self.chat, text)["json"]
self.assertEqual(message["text"], text)
self.assertEqual(message["sender_id"], self.alice["id"])
def test_message_pagination(self):
self.assertEqual(self.get_page(self.path + "/messages"), {"items": [], "next_cursor": None})
self.assertEqual(self.get_page(self.path + "/messages", limit="0000000001")["items"], [])
sent = [self.client.message(self.alice if i % 2 else self.bob, self.chat, str(i))["json"] for i in range(6)]
received, cursor = [], None
for limit in (2, 3, 1):
query = {"limit": limit}
if cursor:
query["cursor"] = cursor
page = self.get_page(self.path + "/messages", self.bob, **query)
received.extend(page["items"])
cursor = page["next_cursor"]
self.assertIsNone(cursor)
self.assertEqual(received, sorted(sent, key=lambda item: (timestamp(item["created_at"]), item["id"])))
def test_chat_pagination(self):
chats = [self.chat] + [self.new_chat() for _ in range(4)]
received, cursor = [], None
while True:
query = {"limit": 2}
if cursor:
query["cursor"] = cursor
page = self.get_page("/api/v1/chats", **query)
received += page["items"]
cursor = page["next_cursor"]
if cursor is None:
break
self.assertEqual(received, sorted(chats, key=lambda item: (timestamp(item["created_at"]), item["id"])))
cursor = self.get_page("/api/v1/chats", limit=1)["next_cursor"]
self.error("GET", "/api/v1/chats?" + urlencode({"cursor": cursor}), actor=self.bob)
def test_bad_cursors_and_limits(self):
for i in range(2):
self.client.message(self.alice, self.chat, str(i))
cursor = self.get_page(self.path + "/messages", limit=1)["next_cursor"]
for value in ("", "bad", cursor[:-1] + ("0" if cursor[-1] != "0" else "1")):
self.error("GET", self.path + "/messages?" + urlencode({"cursor": value}), actor=self.alice)
other = self.new_chat()
self.error("GET", "/api/v1/chats/" + other["id"] + "/messages?" + urlencode({"cursor": cursor}), actor=self.alice)
self.error("GET", "/api/v1/chats?" + urlencode({"cursor": cursor}), actor=self.alice)
for path in ("/api/v1/chats", self.path + "/messages"):
for value in ("", "0", "-1", "101", "1.5", "true", "9" * 500):
self.error("GET", path + "?" + urlencode({"limit": value}), actor=self.alice)
def test_read_after_write(self):
first = self.client.message(self.alice, self.chat, "Первое")["json"]
second = self.client.message(self.bob, self.chat, "Второе")["json"]
page = self.get_page(self.path + "/messages", limit=1)
third = self.client.message(self.bob, self.chat, "Новое")["json"]
rest = self.get_page(self.path + "/messages", self.bob, cursor=page["next_cursor"])
self.assertEqual(page["items"] + rest["items"], [first, second, third])
self.assertIsNone(rest["next_cursor"])
def test_parallel_messages(self):
def send(worker):
client = Client(self.client.base)
return [client.message(self.alice, self.chat, f"{worker}:{i}")["json"]["id"] for i in range(10)]
with ThreadPoolExecutor(max_workers=10) as pool:
sent = [item for group in pool.map(send, range(10)) for item in group]
received, cursor = [], None
while True:
query = {"limit": 17}
if cursor:
query["cursor"] = cursor
page = self.get_page(self.path + "/messages", self.bob, **query)
received += page["items"]
cursor = page["next_cursor"]
if cursor is None:
break
self.assertEqual(len(received), 100)
self.assertEqual(len({item["text"] for item in received}), 100)
self.assertEqual({item["id"] for item in received}, set(sent))
def test_parallel_username(self):
body = {"username": "u_" + uuid4().hex[:20], "display_name": "Один", "password": "T3st-password-123"}
def create(_):
return Client(self.client.base).call("POST", "/api/v1/users", [201, 409], body=body, schema="User")["status"]
with ThreadPoolExecutor(max_workers=10) as pool:
statuses = list(pool.map(create, range(10)))
self.assertEqual(statuses.count(201), 1)
self.assertEqual(statuses.count(409), 9)
if __name__ == "__main__":
unittest.main(verbosity=2)
+96
View File
@@ -0,0 +1,96 @@
import base64
import json
import os
from pathlib import Path
import re
import sys
import unittest
from uuid import uuid4
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "tests"))
from helpers import Client
def basic(username, password):
value = base64.b64encode((username + ":" + password).encode("utf-8")).decode("ascii")
return {"Authorization": "Basic " + value}
class SessionTests(unittest.TestCase):
def setUp(self):
self.client = Client(os.environ.get("BASE_URL", "http://127.0.0.1:18102"))
self.actor = self.client.user("auth")
def register(self, password):
body = {"username": "p_" + uuid4().hex[:20], "display_name": "Пароль", "password": password}
user = self.client.call("POST", "/api/v1/users", 201, body=body, schema="User")["json"]
session = self.client.call("POST", "/api/v1/auth/sessions", 201,
headers=basic(body["username"], password), schema="Session")["json"]
self.assertEqual(session["user"], user)
self.assertEqual(set(session), {"token", "token_type", "expires_at", "user"})
self.assertTrue(re.fullmatch(r"[A-Za-z0-9_-]{43}", session["token"]), "Invalid token format")
return body, session
def test_password_required_and_limits(self):
source = self.actor["registration"]
for password in ("x" * 11, "x" * 129, " " * 12, None, 123, True, "a" * 12 + "\x00"):
body = {**source, "username": "p_" + uuid4().hex[:20], "password": password}
self.client.call("POST", "/api/v1/users", 400, body=body)
missing = {key: value for key, value in source.items() if key != "password"}
self.client.call("POST", "/api/v1/users", 400, body=missing)
raw = json.dumps({**source, "username": "p_" + uuid4().hex[:20], "password": "\ud800" * 12}).encode()
self.client.call("POST", "/api/v1/users", 400, raw=raw, headers={"Content-Type": "application/json"})
def test_password_boundaries_unicode_colon_spaces(self):
for password in ("x" * 12, "🙂" * 127 + ":", " Привет:пароль🙂 "):
body, session = self.register(password)
actor = {"id": session["user"]["id"], "token": session["token"]}
self.client.call("GET", "/api/v1/users/me", actor=actor, schema="User")
changed = password.strip() if password != password.strip() else password + "!"
self.client.call("POST", "/api/v1/auth/sessions", 401,
headers=basic(body["username"], changed))
def test_wrong_and_unknown_credentials(self):
source = self.actor["registration"]
wrong = self.client.call("POST", "/api/v1/auth/sessions", 401,
headers=basic(source["username"], "wrong-password-123"))
unknown = self.client.call("POST", "/api/v1/auth/sessions", 401,
headers=basic("unknown_" + uuid4().hex[:12], source["password"]))
self.assertEqual(wrong["json"]["error"]["message"], unknown["json"]["error"]["message"])
self.assertEqual(wrong["headers"]["WWW-Authenticate"], 'Basic realm="messenger", charset="UTF-8"')
def test_malformed_basic_and_body(self):
for value in ("", "Basic", "Basic !!!", "Bearer " + self.actor["token"],
"Basic " + base64.b64encode(b"missing-colon").decode(),
"Basic " + base64.b64encode(b"\xff:some-password").decode()):
self.client.call("POST", "/api/v1/auth/sessions", 401, headers={"Authorization": value})
source = self.actor["registration"]
self.client.call("POST", "/api/v1/auth/sessions", 400, body={},
headers=basic(source["username"], source["password"]))
def test_only_bearer_identifies_user(self):
source = self.actor["registration"]
for value in ("", "Bearer", "Bearer bad", "Bearer " + "x" * 43,
basic(source["username"], source["password"])["Authorization"]):
self.client.call("GET", "/api/v1/users/me", 401, headers={"Authorization": value})
self.client.call("GET", "/api/v1/users/me", 401, headers={"X-User-Id": self.actor["id"]})
other = self.client.user("other")
result = self.client.call("GET", "/api/v1/users/me", actor=self.actor,
headers={"X-User-Id": other["id"]}, schema="User")
self.assertEqual(result["json"]["id"], self.actor["id"])
self.client.call("GET", "/api/v1/users/me", schema="User",
headers={"Authorization": "bEaReR " + self.actor["token"]})
def test_logout_only_revokes_current_session(self):
source = self.actor["registration"]
second = self.client.call("POST", "/api/v1/auth/sessions", 201,
headers=basic(source["username"], source["password"]), schema="Session")["json"]
other_session = {"id": self.actor["id"], "token": second["token"]}
self.client.call("DELETE", "/api/v1/auth/sessions/current", 204, actor=self.actor)
for method, path in (("GET", "/api/v1/users/me"), ("DELETE", "/api/v1/auth/sessions/current")):
self.client.call(method, path, 401, actor=self.actor)
self.client.call("GET", "/api/v1/users/me", actor=other_session, schema="User")
if __name__ == "__main__":
unittest.main(verbosity=2)