From 28d68dc30b5d2e18f3c70756fad454b60b3a3fb4 Mon Sep 17 00:00:00 2001 From: Mikhail Verkovykh Date: Sat, 14 Nov 2026 23:17:36 +0300 Subject: [PATCH 1/8] chore(migrate) moved codebase from lab4 --- .dockerignore.lab4 | 22 + .github/pull_request_template.md.lab4 | 34 + .github/workflows/contracts.yml.lab4 | 16 + .gitignore.lab4 | 40 + CONTRIBUTING.md.lab4 | 19 + COURSE.md.lab4 | 53 + Dockerfile | 21 + Dockerfile.example.lab4 | 8 + MAINTAINERS.md.lab4 | 38 + Makefile.lab3 | 63 + Makefile.lab4 | 86 + README.md.lab4 | 105 + REPORT.md.lab4 | 372 ++ app/.air.toml | 59 + app/cmd/reconciler/main.go | 105 + app/cmd/server/main.go | 79 + app/go.mod | 54 + app/go.sum | 167 + app/internal/app.go | 120 + app/internal/attachments/handler.go | 329 ++ app/internal/attachments/image.go | 129 + app/internal/attachments/image_test.go | 134 + app/internal/auth/handler.go | 127 + app/internal/auth/password.go | 73 + app/internal/auth/password_test.go | 64 + app/internal/chats/handler.go | 86 + .../common/attachments_validation_test.go | 64 + app/internal/common/binding.go | 63 + app/internal/common/error.go | 142 + app/internal/common/idempotency_key_test.go | 30 + app/internal/common/locals.go | 39 + app/internal/common/paginator.go | 102 + app/internal/common/paginator_test.go | 31 + app/internal/common/password_test.go | 21 + app/internal/common/time.go | 27 + app/internal/common/time_test.go | 48 + app/internal/common/validation.go | 149 + app/internal/config/config.go | 283 ++ app/internal/config/config_test.go | 64 + app/internal/database/database.go | 44 + app/internal/database/migrations.go | 71 + .../database/migrations/0001_init.down.sql | 4 + .../database/migrations/0001_init.up.sql | 38 + .../0002_message_idempotency.down.sql | 1 + .../0002_message_idempotency.up.sql | 14 + .../migrations/0003_attachments.down.sql | 6 + .../migrations/0003_attachments.up.sql | 46 + app/internal/database/migrations_test.go | 88 + app/internal/health/handler.go | 61 + app/internal/messages/handler.go | 82 + app/internal/model/model.go | 80 + app/internal/objectstore/client.go | 176 + app/internal/reconcile/reconcile.go | 115 + app/internal/session/store.go | 103 + app/internal/store/attachments.go | 170 + app/internal/store/chats.go | 142 + app/internal/store/errors.go | 44 + app/internal/store/messages.go | 285 ++ app/internal/store/messages_test.go | 80 + app/internal/store/pagination.go | 17 + app/internal/store/store.go | 36 + app/internal/store/tx.go | 16 + app/internal/store/users.go | 69 + app/internal/users/handler.go | 62 + compose.example.yaml.lab4 | 12 + compose.yaml | 258 ++ contracts/README.md.lab4 | 61 + contracts/examples.json.lab4 | 60 + contracts/infrastructure.json.lab4 | 46 + contracts/openapi.json.lab4 | 3506 +++++++++++++++++ evidence/README.md.lab4 | 5 + evidence/attachment-failover.txt | 4 + evidence/attachments-negative.txt | 2 + evidence/failover.txt | 4 + evidence/idempotency.txt | 2 + evidence/interrupted-upload.txt | 4 + evidence/load.json | 17 + evidence/load.txt | 17 + evidence/persistence-containers.txt | 17 + evidence/persistence.txt | 3 + evidence/private-bucket.txt | 9 + evidence/readiness-postgres.txt | 18 + evidence/readiness-redis.txt | 15 + evidence/readiness-s3.txt | 21 + evidence/reconcile.txt | 24 + evidence/s3-bootstrap.txt | 8 + evidence/smoke.txt | 2 + evidence/upload-memory-run.txt | 25 + evidence/upload-memory-single-run.txt | 24 + evidence/upload-memory-single.json | 24 + evidence/upload-memory.json | 25 + examples/requests.http.lab4 | 53 + infra/README.md.lab4 | 7 + infra/haproxy/haproxy.cfg | 41 + infra/rustfs/api-policy.json.template | 26 + infra/rustfs/bootstrap.sh | 52 + lab.json.lab4 | 8 + load/messages.js | 70 + requirements-dev.txt.lab4 | 2 + scripts/check-private-bucket.sh | 104 + scripts/check_contract.py.lab4 | 54 + scripts/measure-upload-memory.py | 174 + scripts/restart.example.sh.lab4 | 10 + scripts/restart.sh | 4 + scripts/rolling-update.sh | 42 + scripts/stop-one.example.sh.lab4 | 10 + scripts/stop-one.sh | 20 + scripts/validate_openapi.py.lab4 | 23 + scripts/verify-attachment-failover.sh | 54 + scripts/verify-interrupted-upload.sh | 92 + scripts/verify-reconcile.sh | 199 + tests/README.md.lab4 | 57 + tests/client.py.lab4 | 217 + tests/custom/attachment_flow.py | 136 + tests/custom/attachments.py | 168 + tests/custom/concurrent_uploads.py | 129 + tests/custom/idempotency.py | 92 + tests/fixtures/README.md.lab4 | 9 + tests/fixtures/sample.png.lab4 | Bin 0 -> 111817 bytes tests/image_probe.py.lab4 | 26 + tests/load.py.lab4 | 58 + tests/resilience.py.lab4 | 129 + tests/smoke.py.lab4 | 82 + 123 files changed, 11677 insertions(+) create mode 100644 .dockerignore.lab4 create mode 100644 .github/pull_request_template.md.lab4 create mode 100644 .github/workflows/contracts.yml.lab4 create mode 100644 .gitignore.lab4 create mode 100644 CONTRIBUTING.md.lab4 create mode 100644 COURSE.md.lab4 create mode 100644 Dockerfile create mode 100644 Dockerfile.example.lab4 create mode 100644 MAINTAINERS.md.lab4 create mode 100644 Makefile.lab3 create mode 100644 Makefile.lab4 create mode 100644 README.md.lab4 create mode 100644 REPORT.md.lab4 create mode 100755 app/.air.toml create mode 100644 app/cmd/reconciler/main.go create mode 100755 app/cmd/server/main.go create mode 100755 app/go.mod create mode 100755 app/go.sum create mode 100755 app/internal/app.go create mode 100644 app/internal/attachments/handler.go create mode 100644 app/internal/attachments/image.go create mode 100644 app/internal/attachments/image_test.go create mode 100644 app/internal/auth/handler.go create mode 100644 app/internal/auth/password.go create mode 100644 app/internal/auth/password_test.go create mode 100644 app/internal/chats/handler.go create mode 100644 app/internal/common/attachments_validation_test.go create mode 100644 app/internal/common/binding.go create mode 100644 app/internal/common/error.go create mode 100644 app/internal/common/idempotency_key_test.go create mode 100644 app/internal/common/locals.go create mode 100644 app/internal/common/paginator.go create mode 100644 app/internal/common/paginator_test.go create mode 100644 app/internal/common/password_test.go create mode 100644 app/internal/common/time.go create mode 100644 app/internal/common/time_test.go create mode 100644 app/internal/common/validation.go create mode 100644 app/internal/config/config.go create mode 100644 app/internal/config/config_test.go create mode 100644 app/internal/database/database.go create mode 100644 app/internal/database/migrations.go create mode 100644 app/internal/database/migrations/0001_init.down.sql create mode 100644 app/internal/database/migrations/0001_init.up.sql create mode 100644 app/internal/database/migrations/0002_message_idempotency.down.sql create mode 100644 app/internal/database/migrations/0002_message_idempotency.up.sql create mode 100644 app/internal/database/migrations/0003_attachments.down.sql create mode 100644 app/internal/database/migrations/0003_attachments.up.sql create mode 100644 app/internal/database/migrations_test.go create mode 100644 app/internal/health/handler.go create mode 100644 app/internal/messages/handler.go create mode 100644 app/internal/model/model.go create mode 100644 app/internal/objectstore/client.go create mode 100644 app/internal/reconcile/reconcile.go create mode 100644 app/internal/session/store.go create mode 100644 app/internal/store/attachments.go create mode 100644 app/internal/store/chats.go create mode 100644 app/internal/store/errors.go create mode 100644 app/internal/store/messages.go create mode 100644 app/internal/store/messages_test.go create mode 100644 app/internal/store/pagination.go create mode 100644 app/internal/store/store.go create mode 100644 app/internal/store/tx.go create mode 100644 app/internal/store/users.go create mode 100644 app/internal/users/handler.go create mode 100644 compose.example.yaml.lab4 create mode 100644 compose.yaml create mode 100644 contracts/README.md.lab4 create mode 100644 contracts/examples.json.lab4 create mode 100644 contracts/infrastructure.json.lab4 create mode 100644 contracts/openapi.json.lab4 create mode 100644 evidence/README.md.lab4 create mode 100644 evidence/attachment-failover.txt create mode 100644 evidence/attachments-negative.txt create mode 100644 evidence/failover.txt create mode 100644 evidence/idempotency.txt create mode 100644 evidence/interrupted-upload.txt create mode 100644 evidence/load.json create mode 100644 evidence/load.txt create mode 100644 evidence/persistence-containers.txt create mode 100644 evidence/persistence.txt create mode 100644 evidence/private-bucket.txt create mode 100644 evidence/readiness-postgres.txt create mode 100644 evidence/readiness-redis.txt create mode 100644 evidence/readiness-s3.txt create mode 100644 evidence/reconcile.txt create mode 100644 evidence/s3-bootstrap.txt create mode 100644 evidence/smoke.txt create mode 100644 evidence/upload-memory-run.txt create mode 100644 evidence/upload-memory-single-run.txt create mode 100644 evidence/upload-memory-single.json create mode 100644 evidence/upload-memory.json create mode 100644 examples/requests.http.lab4 create mode 100644 infra/README.md.lab4 create mode 100644 infra/haproxy/haproxy.cfg create mode 100644 infra/rustfs/api-policy.json.template create mode 100644 infra/rustfs/bootstrap.sh create mode 100644 lab.json.lab4 create mode 100644 load/messages.js create mode 100644 requirements-dev.txt.lab4 create mode 100755 scripts/check-private-bucket.sh create mode 100644 scripts/check_contract.py.lab4 create mode 100755 scripts/measure-upload-memory.py create mode 100755 scripts/restart.example.sh.lab4 create mode 100755 scripts/restart.sh create mode 100755 scripts/rolling-update.sh create mode 100755 scripts/stop-one.example.sh.lab4 create mode 100755 scripts/stop-one.sh create mode 100644 scripts/validate_openapi.py.lab4 create mode 100755 scripts/verify-attachment-failover.sh create mode 100755 scripts/verify-interrupted-upload.sh create mode 100755 scripts/verify-reconcile.sh create mode 100644 tests/README.md.lab4 create mode 100644 tests/client.py.lab4 create mode 100755 tests/custom/attachment_flow.py create mode 100755 tests/custom/attachments.py create mode 100644 tests/custom/concurrent_uploads.py create mode 100644 tests/custom/idempotency.py create mode 100644 tests/fixtures/README.md.lab4 create mode 100644 tests/fixtures/sample.png.lab4 create mode 100644 tests/image_probe.py.lab4 create mode 100644 tests/load.py.lab4 create mode 100644 tests/resilience.py.lab4 create mode 100644 tests/smoke.py.lab4 diff --git a/.dockerignore.lab4 b/.dockerignore.lab4 new file mode 100644 index 00000000..c94e7633 --- /dev/null +++ b/.dockerignore.lab4 @@ -0,0 +1,22 @@ +.git +.github +.env +.env.* +!.env.example +secrets +certs +volumes +data +backups +*.key +*.pem +*.p12 +*.pfx +*.jks +.venv +__pycache__ +node_modules +evidence +.DS_Store +app/tmp +app/server diff --git a/.github/pull_request_template.md.lab4 b/.github/pull_request_template.md.lab4 new file mode 100644 index 00000000..7d74901f --- /dev/null +++ b/.github/pull_request_template.md.lab4 @@ -0,0 +1,34 @@ +## Лабораторная 4Булла Максим, БПИ2301Булла Максим, БПИ2301 + +Автор / группа: Булла Максим, БПИ2301 +Целевая оценка: **4** +Трек 5: не заявлен + +**Что реализовано:** приватный RustFS в общем Compose-стенде, вложения в +PostgreSQL + S3, потоковые upload/download, проверка PNG/JPEG по содержимому, +лимиты 10 MiB / 40 Мп, идемпотентный `s3-init`, scoped IAM-user, reconciler +для stale pending и orphan-объектов, S3 в `/health/ready`. Минимум lab3 +сохранён; изменения API только обратно совместимые. + +`docker compose --env-file .env up -d --build`, дождаться `/health/ready`, +**Запуск из чистого clone:** `cp .env.example .env` (заполнить пароли), +затем `make test`. + +**Проверки:** `make check`, `make validate`, `go vet/test/build`, +`make test`, `test-attachments`, `test-concurrency`, `test-reconcile`, +`test-interrupted-upload`, `test-failover`, `test-persistence`. +Вывод и методика — в `REPORT.md` и `evidence/`. + +**Ограничения:** PostgreSQL/Redis/RustFS — одиночные контейнеры с volume; +reconciler работает с grace period 900 с; thumbnails/варианты не входят в lab4. + +**Источники и ИИ:** контракт и публичные тесты курса, документация RustFS, +AWS SDK for Go v2, Fiber v3, pgx. ИИ-ассистент использован для генерации и +ревью кода, тестов и текста отчёта; результаты получены на запущенном стенде. + +- [x] Обязательный минимум текущей и предыдущих работ сохранён. +- [x] Контракт и публичные тесты не ослаблены. +- [x] `make check` и `make test` выполнены, реальный вывод приложен. +- [x] Сценарии отказов и безопасности уровня 4 воспроизведены. +- [x] Секреты и приватные ключи вне Git; в отчёте нет tokens. +- [x] `README.md` задания сохранён, `REPORT.md` описывает моё решение. diff --git a/.github/workflows/contracts.yml.lab4 b/.github/workflows/contracts.yml.lab4 new file mode 100644 index 00000000..11cf289f --- /dev/null +++ b/.github/workflows/contracts.yml.lab4 @@ -0,0 +1,16 @@ +name: Skeleton contracts +on: [push, pull_request] +permissions: + contents: read +jobs: + contracts: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-python@v5 + with: + python-version: '3.12' + - run: python -m pip install -r requirements-dev.txt + - run: make check validate PYTHON=python +# This job validates the skeleton, not a student solution. +# Add build, readiness and make test in a separate application job. diff --git a/.gitignore.lab4 b/.gitignore.lab4 new file mode 100644 index 00000000..2d5418de --- /dev/null +++ b/.gitignore.lab4 @@ -0,0 +1,40 @@ +# Local/private state +.env +.env.* +!.env.example +secrets/ +certs/private/ +*.key +*.p12 +*.pfx +*.jks +*.pem +*.tfstate* +volumes/ +data/ +backups/ + +# Runtime/build artifacts +.venv/ +__pycache__/ +*.py[cod] +node_modules/ +dist/ +build/ +target/ +bin/ +obj/ +coverage/ +*.log +.DS_Store +.idea/ +.vscode/ + +# Local application builds +app/tmp/ +app/server + +# Agent workflow artifacts +AGENTS.md +TASKS.md +CHANGELOG.md diff --git a/CONTRIBUTING.md.lab4 b/CONTRIBUTING.md.lab4 new file mode 100644 index 00000000..ee5533d1 --- /dev/null +++ b/CONTRIBUTING.md.lab4 @@ -0,0 +1,19 @@ +# Работа через fork и pull request + +1. После публикации преподавателем сделайте fork репозитория **текущей** лабораторной в своём аккаунте и clone своего fork. URL преподаватель выдаёт отдельно. +2. Создайте ветку `solution/-` от исходной ветки skeleton. Ветка преподавателя и файлы контракта остаются основой проверки. +3. Для lab2–7 перенесите из своей предыдущей лабы только исходники, миграции, собственные тесты, Dockerfile и нужные конфигурации. Не копируйте `.git`, `.env`, credentials, volumes, `README.md`, `COURSE.md`, публичные `tests/` и `contracts/` поверх новой версии. Сначала сравните изменения API в `COURSE.md`. +4. Реализуйте задание, добавьте свою инструкцию запуска и заполните `REPORT.md`. Публичные проверки дополняйте отдельными тестами, не ослабляйте assertions. +5. Выполните `make check`, запустите свой стенд, затем `make test`. Выполните сценарии отказов текущей лабы. Их запуск всегда явный: scaffold сам не останавливает контейнеры. +6. Commit и push делайте в свой fork. Откройте PR в исходный репозиторий преподавателя, заголовок: `ЛР N — Группа — Фамилия — на 3/4/5`. Выберите правильную базовую ветку и заполните шаблон. +7. После замечаний обновляйте тот же PR. Чужие решения не переносите без указания источника и согласованных правил курса. + +Платформа может называть PR «merge request» — процесс тот же. `.github/` содержит необязательные удобства для GitHub; проверки локально от платформы не зависят. Инициализация и публикация исходных Git-репозиториев выполняются преподавателем отдельно. + +## Правила изменения skeleton + +Не удаляйте обязательные endpoint, поля, рубрику и публичные проверки. Изменение контракта согласуйте отдельным комментарием/PR; обход тестов не является решением. Код приложения размещайте в `src/` либо в принятой для языка структуре. Собственные тесты — `student-tests/` или штатный каталог фреймворка. Ссылки на результаты проверки и команды запуска должны переживать clone на другой машине. + +## CI + +Готовый GitHub workflow проверяет целостность контракта и синтаксис Python-проверок. Он **не запускает отсутствующее приложение** и **не подтверждает оценку**. Студент добавляет сборку своего приложения, запуск/готовность стенда и `make test` в отдельный job. Не используйте `pull_request_target` для запуска кода из fork, не выдавайте CI секреты или доступ к общему Docker-host. diff --git a/COURSE.md.lab4 b/COURSE.md.lab4 new file mode 100644 index 00000000..b02172bb --- /dev/null +++ b/COURSE.md.lab4 @@ -0,0 +1,53 @@ +# Сквозной проект: мессенджер под нагрузкой + +Семь самостоятельных репозиториев содержат задания и проверяемые контракты одной системы. Вы переносите собственную реализацию между лабораторными. Язык, фреймворк и библиотеки выбираете сами. Web UI, WebSocket, редактирование/удаление сообщений, публичный поиск пользователей и Kubernetes не входят в обязательный минимум: достаточно REST API. + +| Лаба | Новая инженерная задача | Что появляется в системе | +| --- | --- | --- | +| 1 | Контракт, процессы и контейнер | In-memory API, Dockerfile, HTTP smoke | +| 2 | Сохранность и идентификация | PostgreSQL, Basic → сессия; Redis/Valkey на 4 | +| 3 | Горизонтальное масштабирование | Балансировщик, ≥2 API-реплики, общие сессии, идемпотентность | +| 4 | Бинарные данные | Приватное S3-совместимое хранилище и вложения | +| 5 | Фоновая работа | Брокер, worker, состояния вложений и преобразование изображений | +| 6 | Диагностика | OTel Collector, хранилища сигналов, Grafana | +| 7 | Защита и эксплуатация | Argon2id, TLS/mTLS, OpenBao, минимальные привилегии | + +## Как оценивать + +Оценки накопительные **внутри лабораторной**: 4 = весь уровень 3 + весь уровень 4; 5 = весь уровень 4 + **один** полностью выполненный трек 5 на выбор. Несколько недоделанных треков не заменяют один завершённый. Условие «весь уровень» включает реализацию, проверку и объяснение на защите. Красивые схемы и дополнительные технологии не компенсируют неработающий обязательный сценарий. + +Для начала следующей лабы достаточно обязательного минимума предыдущей. Сохраняются предыдущие обязательные возможности, с явно описанными переходами API ниже. Не требуется сначала получить 5 за все ранние лабы. Можно исправлять свою базу по ходу курса; такие исправления выделяйте в PR. Реализация на 3 работоспособна, но не претендует на промышленную готовность. Даже 5 в lab7 — учебная доказанная защита в заявленной модели угроз, а не сертификат полной безопасности. + +На 3 студент самостоятельно собирает рабочую систему. На 4 показывает практики, ожидаемые от стажёра/джуна в команде: воспроизводимость, диагностику, обработку ошибок и проверки отказов. На 5 формулирует гипотезу, ставит эксперимент, приводит измерения и обсуждает границы решения — это предмет конкретной похвалы магистранту. + +## Контракт и совместимость + +`contracts/openapi.json` — OpenAPI 3.1.0, `contracts/README.md` — нормативная семантика. Контракт каждого репозитория самодостаточен; соседние папки на машине преподавателя не нужны. `tests/` — публичные проверки, а не эталонное решение. Контракт, рубрика и публичные тесты имеют приоритет над случайным поведением библиотек. При противоречии создайте вопрос преподавателю; не подгоняйте тест под приложение. + +Три объявленных изменения обязательного API: + +1. **1 → 2:** регистрация требует `password`; `X-User-Id` заменяется Bearer. Basic используется только при создании сессии. +2. **2 → 3:** `Idempotency-Key` обязателен для отправки сообщения; ответы API содержат `X-Instance-Id`. +3. **4 → 5:** загрузка вложения возвращает `202` вместо `201`, состояние становится асинхронным. Клиент опрашивает метаданные. Старое текстовое сообщение остаётся валидным: `attachment_ids` по умолчанию `[]`. + +У lab7 меняется транспорт на HTTPS, бизнес-эндпоинты остаются прежними. Внутреннее mTLS не заменяет пользовательскую сессию. Новые возможности на 4/5 добавляйте обратно совместимо, в отдельном `contracts/extensions.openapi.json` или документе; обязательный контракт сохраняйте. + +## Общая предметная модель + +Пользователь (`username`, `display_name`) состоит в чатах. Список участников задаётся при создании чата и в обязательном API не изменяется. В чате отправляются сообщения, с lab4 — со ссылками на вложения этого чата. Сервер назначает UUID и время. Порядок истории — `(created_at, id)` по возрастанию. Для защиты достаточно показать взаимодействие нескольких API-клиентов; фронтенд не требуется. + +С lab2 данные PostgreSQL — источник истины. Сессии могут храниться в PostgreSQL или Redis/Valkey, если выполняют контракт срока жизни и перезапуска. Файлы с lab4 живут в S3, метаданные — в PostgreSQL. Redis и Valkey — альтернативы: оба одновременно не нужны. Аналогично выбирается одно S3-хранилище, один брокер и один основной путь оркестрации. + +## Воспроизводимость и честные измерения + +- Укажите версии runtime и контейнерных образов, аппаратную конфигурацию, CPU/RAM-лимиты, команду запуска и время прогрева. Для образов используйте явный тег версии или digest, не `latest`. +- Все тестовые пользователи и изображения синтетические. `make test` создаёт данные: запускайте его на учебном стенде. Не публикуйте пароли, токены, приватные ключи, персональные данные или подписанные URL. +- Минимальный отчёт о нагрузке: длительность, конкуренция/интенсивность, успешные RPS, доля ошибок, p50/p95/p99, CPU/RAM, размер набора данных. Универсального проходного RPS для разных ноутбуков нет. +- Сравнивайте одинаковые запросы при одинаковых ресурсах. Успешный `/health/live` не доказывает сохранность сообщений, а HTTP `202` — окончание обработки. +- Один Docker-host не обеспечивает отказоустойчивость к потере этого host. Несколько API-процессов не делают единственную БД или LB высокодоступными. Отдельно называйте, какие отказы выдерживает эксперимент. + +## Что сдавать + +Исходники, Dockerfile, нужные манифесты и конфигурации, миграции, собственные тесты, заполненный `REPORT.md`, сведения об использовании внешнего кода и ИИ. Уметь объяснить и изменить своё решение на защите обязательно. Приложите команды и текстовые результаты; скриншоты — дополнение, а не единственное доказательство. + +PR должен собираться из чистого clone по инструкции автора. Секреты создаются отдельно; тестовые fixtures и открытые конфигурации хранятся в репозитории. Перенос кода между лабами описан в [CONTRIBUTING.md](CONTRIBUTING.md). diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..ce530bdd --- /dev/null +++ b/Dockerfile @@ -0,0 +1,21 @@ +FROM golang:1.27.1-alpine AS build + +WORKDIR /src +COPY app/go.mod app/go.sum ./ +RUN go mod download +COPY app/ ./ +RUN CGO_ENABLED=0 GOOS=linux go build -trimpath -ldflags="-s -w" -o /out/server ./cmd/server \ + && CGO_ENABLED=0 GOOS=linux go build -trimpath -ldflags="-s -w" -o /out/reconciler ./cmd/reconciler + +FROM alpine:3.22 + +RUN apk add --no-cache ca-certificates \ + && addgroup -S -g 10001 app \ + && adduser -S -u 10001 -G app app + +COPY --from=build /out/server /usr/local/bin/server +COPY --from=build /out/reconciler /usr/local/bin/reconciler + +USER app +EXPOSE 8080 +ENTRYPOINT ["/usr/local/bin/server"] diff --git a/Dockerfile.example.lab4 b/Dockerfile.example.lab4 new file mode 100644 index 00000000..e333a12e --- /dev/null +++ b/Dockerfile.example.lab4 @@ -0,0 +1,8 @@ +# Teaching outline, not a runnable Dockerfile. Implement your own Dockerfile. +# 1. Choose a runtime/toolchain version and pin the base image tag/digest. +# 2. Copy dependency manifests first; install from the language lockfile. +# 3. Copy source and build. For compiled languages use a separate runtime stage. +# 4. Run the HTTP process as PID 1; listen on 0.0.0.0:8080. +# 5. Add non-root, signals and healthcheck according to the current rubric. +# 6. Pass runtime config/secrets at startup, never COPY .env or secret files. +# This file deliberately has no FROM/CMD: selecting/implementing them is the task. diff --git a/MAINTAINERS.md.lab4 b/MAINTAINERS.md.lab4 new file mode 100644 index 00000000..bb10fed7 --- /dev/null +++ b/MAINTAINERS.md.lab4 @@ -0,0 +1,38 @@ +# Памятка преподавателю — лабораторная 4 + +## Что подготовлено + +Язык реализации не задан. README содержит накопительные уровни 3/4/5, сценарий защиты и вопросы. В каждой лабе собственная полная версия OpenAPI, поэтому fork не зависит от соседних каталогов. Публичный HTTP smoke не содержит реализации сервера; Dockerfile.example и compose.example.yaml (если есть) — задания/ориентиры, не готовый deployment. + +## Перед публикацией + +1. Выберите GitHub/GitLab и организацию, опубликуйте каждый каталог как отдельный template/skeleton repository, сообщите студентам URL и правила именования PR/MR. Эти материалы не публикуют репозитории автоматически. +2. Задайте default branch, защиту исходных материалов и правила приёма. `.github/` уже содержит шаблон PR и CI контракта; в GitLab перенесите эквивалентные настройки, локальные Makefile-команды одинаковы. +3. Сообщите, индивидуальная работа или командная, сроки, правила использования стороннего кода/ИИ и ресурсы демонстрационного стенда. Эти организационные решения намеренно не выдуманы в задании. +4. Уточните ограничения площадки (например доступна ли виртуализация/несколько узлов). Для 5 предусмотрено по два трека: один можно выполнить без настоящего multi-node cloud. Универсальный проходной RPS не установлен. + +## Что проверять + +| Проверка | Что подтверждает | Чего не подтверждает | +| --- | --- | --- | +| make check | Структура файлов, JSON, локальные refs, синтаксис Python | Реализация/запуск API | +| make validate | Полная OpenAPI 3.1, JSON Schema и примеры | Поведение сервера | +| make test | Один сквозной контрактный сценарий текущей лабы | Все ошибки, persistence, HA, security | +| test-persistence (lab2+) | Данные/прежний токен после вызова restart hook | Реальный перезапуск, если hook — no-op | +| test-failover (lab3+) | Смена обслуживающей реплики, сохранность, retry без дубля | Несколько физических узлов, HA DB/LB | +| test-security (lab7) | Доверенный HTTPS и отказ недоверенного CA | Внутреннее mTLS/identity/ACL/OpenBao | +| Защита и тесты студента | Реальность инфраструктуры и критерии выбранного уровня | Неограниченная промышленная безопасность | + +Проверяйте реальные контейнеры/логи процессов и данные до/после отказа: одного X-Instance-Id недостаточно. Для 4 требуются все пункты 3+4, для 5 — все пункты 3+4 и один **завершённый** трек 5. Сравнивайте подтверждённые результаты со сформулированными критериями, а не количество установленных сервисов. + +## Приём PR + +Проверка запускается на изолированном учебном окружении с синтетическими secrets, не на общей production БД. Студенческие Dockerfile/hooks/CI — выполняемый код: обычный fork workflow без секретов и без доступа к общему Docker socket. Не запускайте PR через pull_request_target с привилегиями преподавателя. + +Исходные README/контракт/публичные tests сохраняются, реализация переносится студентом между лабами через собственные исходники и миграции. Исправление общей спецификации публикуйте отдельным изменением, объясняя влияние на все затронутые лабораторные. + +## Проверка этой версии skeleton + +На 2026-09-10 выполнены структурные проверки и полная валидация всех семи OpenAPI/JSON Schema, проверка локальных Markdown-ссылок/YAML и декодирование PNG fixture. HTTP-клиенты проверены на временном изолированном тестовом стенде: позитивный smoke 1–7, отрицательные варианты неправильного sender, pagination, ACL, logout, bytes и пустого API; также пути persistence/failover/load и внешнего TLS. + +Временный стенд не включён в студенческие каталоги и не является эталонным решением. Реальное приложение, Compose, PostgreSQL/S3/broker и mTLS/OpenBao в skeleton отсутствуют: их работоспособность не заявляется. Docker CLI в среде подготовки не был доступен. Это проверка материалов и тестовых драйверов, не приёмка будущих решений студентов. diff --git a/Makefile.lab3 b/Makefile.lab3 new file mode 100644 index 00000000..074f3b90 --- /dev/null +++ b/Makefile.lab3 @@ -0,0 +1,63 @@ +PYTHON ?= python3 +BASE_URL ?= http://localhost:8080 +CA_FILE ?= +ACTION_SCRIPT ?= +COMPOSE ?= docker compose --env-file .env +VUS ?= 10 +DURATION ?= 60s +TLS_ARGS = $(if $(CA_FILE),--ca-file "$(CA_FILE)",) + +.PHONY: help check validate test load test-persistence test-failover test-concurrency app-test app-build up down wait k6 + +help: + @echo "check: offline structure; validate: full OpenAPI (requirements-dev.txt); test: running API" + @echo "test-concurrency: cross-replica idempotency race; test-failover/persistence: explicit hooks" + @echo "load: synthetic writes via tests/load.py; k6: k6 write load through HAProxy" + @echo "up/down/wait: compose lifecycle; app-test: Go unit tests" + +check: + $(PYTHON) scripts/check_contract.py + +validate: + $(PYTHON) scripts/validate_openapi.py + +test: + $(PYTHON) tests/smoke.py --base-url "$(BASE_URL)" $(TLS_ARGS) + +load: + $(PYTHON) tests/load.py --base-url "$(BASE_URL)" $(TLS_ARGS) + +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) + +test-failover: + @test -n "$(ACTION_SCRIPT)" || (echo "Set ACTION_SCRIPT to your implemented executable stop-one hook"; exit 2) + $(PYTHON) tests/resilience.py --mode failover --action-script "$(ACTION_SCRIPT)" --base-url "$(BASE_URL)" $(TLS_ARGS) + +# --------------------------- +# Custom convenience commands +# --------------------------- + +test-concurrency: + $(PYTHON) tests/custom/idempotency.py --base-url "$(BASE_URL)" $(TLS_ARGS) + +app-test: + cd app && go test ./... + +app-build: + cd app && go build ./cmd/server + +up: + $(COMPOSE) up -d --build + +down: + $(COMPOSE) down + +wait: + @echo "waiting for $(BASE_URL)/health/ready" + @until curl -fsS "$(BASE_URL)/health/ready" >/dev/null; do sleep 1; done + @echo "ready" + +k6: + $(COMPOSE) --profile load run --rm k6 run -e VUS=$(VUS) -e DURATION=$(DURATION) --summary-export /results/last.json /scripts/messages.js diff --git a/Makefile.lab4 b/Makefile.lab4 new file mode 100644 index 00000000..0d6322a0 --- /dev/null +++ b/Makefile.lab4 @@ -0,0 +1,86 @@ +PYTHON ?= python3 +BASE_URL ?= http://localhost:8080 +CA_FILE ?= +ACTION_SCRIPT ?= +COMPOSE ?= docker compose --env-file .env +VUS ?= 10 +DURATION ?= 60s +TLS_ARGS = $(if $(CA_FILE),--ca-file "$(CA_FILE)",) + +.PHONY: help check validate test load test-persistence test-failover \ + test-concurrency test-attachments test-upload-memory test-reconcile \ + test-private-bucket test-attachment-failover test-interrupted-upload \ + app-test app-build up down wait k6 + +help: + @echo "check: offline structure; validate: full OpenAPI (requirements-dev.txt); test: running API" + @echo "test-concurrency: cross-replica idempotency race" + @echo "test-attachments: graded negative upload/access cases" + @echo "test-persistence/test-failover: explicit operator hooks" + @echo "test-upload-memory: >=10 concurrent uploads with sampled API memory" + @echo "test-reconcile: stale pending, orphan object and ready-attachment safety" + @echo "test-private-bucket: anonymous denial and scoped S3 credential" + @echo "test-attachment-failover: upload, stop serving replica, download elsewhere" + @echo "test-interrupted-upload: half-sent upload leaves no row/object/temp file" + @echo "up/down/wait: compose lifecycle; app-test/app-build: Go checks" + +check: + $(PYTHON) scripts/check_contract.py + +validate: + $(PYTHON) scripts/validate_openapi.py + +test: + $(PYTHON) tests/smoke.py --base-url "$(BASE_URL)" $(TLS_ARGS) + +load: + $(PYTHON) tests/load.py --base-url "$(BASE_URL)" $(TLS_ARGS) + +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) + +test-failover: + @test -n "$(ACTION_SCRIPT)" || (echo "Set ACTION_SCRIPT to your implemented executable stop-one hook"; exit 2) + $(PYTHON) tests/resilience.py --mode failover --action-script "$(ACTION_SCRIPT)" --base-url "$(BASE_URL)" $(TLS_ARGS) + +test-concurrency: + $(PYTHON) tests/custom/idempotency.py --base-url "$(BASE_URL)" $(TLS_ARGS) + +test-attachments: + $(PYTHON) tests/custom/attachments.py --base-url "$(BASE_URL)" $(TLS_ARGS) + +test-upload-memory: + $(PYTHON) scripts/measure-upload-memory.py + +test-reconcile: + ./scripts/verify-reconcile.sh + +test-private-bucket: + ./scripts/check-private-bucket.sh + +test-attachment-failover: + ./scripts/verify-attachment-failover.sh + +test-interrupted-upload: + ./scripts/verify-interrupted-upload.sh + +app-test: + cd app && go test ./... + +app-build: + cd app && go build ./... + +up: + $(COMPOSE) up -d --build + +down: + $(COMPOSE) down + +wait: + @echo "waiting for $(BASE_URL)/health/ready" + @until curl -fsS "$(BASE_URL)/health/ready" >/dev/null; do sleep 1; done + @echo "ready" + +k6: + $(COMPOSE) --profile load run --rm k6 run -e VUS=$(VUS) -e DURATION=$(DURATION) --summary-export /results/last.json /scripts/messages.js diff --git a/README.md.lab4 b/README.md.lab4 new file mode 100644 index 00000000..947094c3 --- /dev/null +++ b/README.md.lab4 @@ -0,0 +1,105 @@ +# Лабораторная 4. S3-хранилище и вложения + +Разделить метаданные и бинарное содержимое, добавить изображения в сообщения и сохранить доступ к ним при переключении API-реплики. + +Это **skeleton**, а не готовый сервер. Здесь есть задание, контракт и публичные проверки. Реализацию приложения, Dockerfile и требуемую инфраструктуру пишет студент. Язык и framework свободные; UI не обязателен. + +[Карта курса](COURSE.md) · [Процесс fork/PR](CONTRIBUTING.md) · [OpenAPI](contracts/openapi.json) · [Семантика API](contracts/README.md) · [Проверки](tests/README.md) · [Отчёт](REPORT.md) · [Примеры JSON](contracts/examples.json) · [Памятка преподавателю](MAINTAINERS.md) + +## Откуда начинаем + +Перенесите минимум lab3. Дополняются поля сообщения и endpoint вложений; прежние текстовые POST остаются валидными. + +**Архитектура:** Клиент → LB → API × 2+ → PostgreSQL (метаданные) + приватное S3-хранилище (байты). + +## Что сделать + +1. Выберите один S3-совместимый сервер: MinIO, SeaweedFS или RustFS. Закрепите проверенную версию образа и опишите создание приватного bucket. +2. Спроектируйте Attachment и связь с чатом/сообщением. S3 object key назначает сервер, filename используется только для отображения. +3. Реализуйте синхронный binary upload PNG/JPEG с ограничениями, сохранение метаданных и авторизованный download через API. +4. Включите S3 и persistent volumes в общий Compose/Swarm. Проверьте, что другая API-реплика скачивает файл после остановки загрузившей. +5. Проверьте недоступность S3, чужой чат и restart всего стенда. + +## Оценка «3»: работающий минимум + +- Все обязательные возможности lab3 сохранены. S3-сервер находится в общем стенде; метаданные вложений живут в PostgreSQL, байты — в приватном bucket, не в БД или локальном каталоге API. +- POST вложения возвращает 201 ready после сохранения; оригинал скачивается побайтно. Message с attachment_ids читается участниками чата. Публичный smoke проверяет checksum и отсутствие доступа у постороннего. +- Принимаются реальные PNG/JPEG ≤10 MiB и ≤40 млн пикселей; MIME проверяется по содержимому, опасные filename отклоняются, лимиты применяются до неограниченного чтения/декодирования. +- Upload → остановка обслужившей API-реплики → download через другую возвращает тот же файл. Restart с сохранением volumes сохраняет метаданные и оригинал. + +## Оценка «4»: уровень стажёра/джуна + +Весь уровень 3 **и все** пункты ниже: + +- Тесты покрывают слишком большой файл, повреждённое изображение, подмену MIME, чужое attachment_id и привязку файла другого чата. Пользовательская ошибка не превращается в 500. +- Прерывание загрузки и отказ между записью S3/БД не оставляют вечных неучтённых объектов: есть документированный механизм компенсации/сверки/очистки с безопасным grace period. +- Лимиты/таймауты и потоковая обработка не позволяют одной загрузке занять неограниченную RAM. Проведён эксперимент ≥10 одновременных загрузок с измерением пика памяти. +- Bucket bootstrap идемпотентен; API использует отдельные S3 credentials с ограниченными правами. Административная консоль не открыта в общей сети. Есть проверка private bucket вне API. + +## Оценка «5»: исследование и доказанный результат + +Весь уровень 4 **и один законченный трек на выбор**. Приведите гипотезу, повторяемую методику, результаты и ограничения. + +**Трек 1. Прямой upload.** Добавьте отдельный безопасный presigned-upload flow: короткая подпись, уникальный ключ, проверка принадлежности, размера/checksum при complete, обработка незавершённой загрузки. Сохраните обязательный proxy endpoint. Сравните трафик и RAM API при ≥2 размерах файлов, покажите негативные сценарии. + +**Трек 2. Согласованность двух хранилищ.** Разработайте повторяемый reconciler для orphan/missing объектов и метаданных. Внедрите ошибки на обеих сторонах, покажите обнаружение и безопасное исправление с dry-run/повторным запуском. Докажите, что свежие и используемые вложения не удаляются; измерьте стоимость обхода. + +## Как начать и проверить + +```sh +# Работает прямо в skeleton, Python 3.9+; приложение не запускается: +make check + +# После реализации запустите свой сервер/стенд по REPORT.md. +# По умолчанию тест использует http://localhost:8080. +make test +``` + +`.env.example`, `compose.example.yaml` и `contracts/infrastructure.json` описывают настройки и роли компонентов. **compose.example.yaml — список ролей с пустым services, не запускаемый стенд.** Реализуйте свой `compose.yaml`/`stack.yaml`, закрепите версии образов, заполните локальный `.env` и опишите bootstrap/миграции. Все зависимости поднимаются из этого репозитория; ссылка «у меня БД уже установлена» не заменяет воспроизводимость. + +```sh +# Compose-вариант после реализации: +docker compose --env-file .env config --quiet +docker compose --env-file .env up -d --build +# Дождитесь /health/ready; затем make test. +# Для Swarm укажите эквивалентные build/push/deploy-команды в REPORT.md. +``` + + +Примеры запросов доступны в `examples/requests.http`, примеры JSON — в `contracts/examples.json`. Для полной проверки схемы (по желанию локально; CI делает её автоматически): + +```sh +python3 -m venv .venv +.venv/bin/python -m pip install -r requirements-dev.txt +make validate PYTHON=.venv/bin/python +``` + +Smoke не выставляет оценку автоматически. Проверку сохранности/отказов запускайте явно по [tests/README.md](tests/README.md), остальные доказательства приведите в отчёте. + +## Сценарий защиты + +1. Загрузить fixture, отправить message с attachment_ids, скачать как второй участник и сравнить SHA-256. +2. С третьим пользователем получить 404 для метаданных и bytes; direct unauthenticated S3 GET не отдаёт объект. +3. Выполнить persistence и failover: общий тест начиная с lab4 сохраняет и повторно читает оригинал через API. +4. Показать, что на API нет обязательного локального файла оригинала. Для 4/5 — отказы и эксперименты. + +## Что должно быть в PR + +Дополненный стенд, настройка bucket/политики, миграции Attachment, тесты файлов, описание согласованности БД/S3 и результаты. + +Заполните `REPORT.md` и [шаблон PR](.github/pull_request_template.md), укажите целевую оценку/трек. Не меняйте обязательный контракт и публичные tests ради зелёного результата. Сохраняйте возможности предыдущих лабораторных в пределах [объявленных переходов](COURSE.md). + +## Вопросы на защите + +- Почему файл в Docker volume одной API-реплики не равен объектному хранилищу? +- Когда можно честно вернуть 201? +- Что произойдёт, если S3 PUT прошёл, а COMMIT в PostgreSQL — нет? +- Почему случайный object key и приватный bucket не заменяют ACL приложения? + +## Первичные материалы + +- [S3 object uploads](https://docs.aws.amazon.com/AmazonS3/latest/userguide/upload-objects.html) +- [Presigned URLs](https://docs.aws.amazon.com/AmazonS3/latest/userguide/using-presigned-url.html) +- [MinIO](https://github.com/minio/minio) +- [SeaweedFS](https://github.com/seaweedfs/seaweedfs) +- [RustFS](https://docs.rustfs.com/) diff --git a/REPORT.md.lab4 b/REPORT.md.lab4 new file mode 100644 index 00000000..c341dfab --- /dev/null +++ b/REPORT.md.lab4 @@ -0,0 +1,372 @@ +# Отчёт — лабораторная 4 + +- Автор, группа: Булла Максим, БПИ2301 +- Целевая оценка: **4** +- Commit/PR: ``b4239f8`` +- Использованные источники, библиотеки и помощь ИИ: контракт курса + (`contracts/`), публичные тесты (`tests/`), документация RustFS, AWS SDK for + Go v2, Fiber v3, pgx. ИИ-ассистент (DeepSeek v4.1 Flash) использовался для генерации и ревью кода, + текста отчёта и тестовых сценариев; все результаты ниже получены на реально + запущенном стенде. + +## Запуск из чистого clone + +### Требования + +- Linux с Docker Engine 29.9.1 и Docker Compose v5.6.0 (проверено на этих + версиях; нужен обычный bridge network и named volumes). +- Не менее ~5 ГБ свободного места: образы Go builder, RustFS, PostgreSQL, + Redis, HAProxy, Python и volumes. При переполнении диска стенд не считается + исправным. +- `make`, `curl`, Python 3.12 для HTTP-проверок. Go для локальных проверок: + модуль объявляет `go 1.27.1`, Dockerfile использует + `golang:1.27.1-alpine`; локальный `go1.25.6` собирает проект через + авто-загрузку toolchain. +- Версии образов закреплены: `rustfs/rustfs:1.0.4`, `rustfs/rc:v0.1.36`, + `postgres:16.10-alpine`, `redis:7.4.7-alpine`, `haproxy:3.2.6-alpine`, + `python:3.12-alpine`, `alpine:3.22`, `grafana/k6:1.3.0`. + +### Подготовка и запуск + +```sh +git clone && cd mtusi2026lab4 + +cp .env.example .env +# Отредактировать .env: POSTGRES_PASSWORD, S3_ROOT_ACCESS_KEY, +# S3_ROOT_SECRET_KEY, S3_SECRET_ACCESS_KEY. Значения не коммитить. +# S3_ACCESS_KEY_ID по умолчанию messenger-api; это имя scoped IAM-user, +# а не root-пользователь RustFS. + +docker compose --env-file .env config --quiet +docker compose --env-file .env up -d --build +``` + +Порядок запуска: `postgres`, `redis`, `rustfs` → healthchecks → одноразовый +`s3-init` (bucket, private access, IAM-user и policy) → `migrate` (встроенные +миграции) → `api` (2 реплики) и `reconciler` → `haproxy`. API запускаются с +`RUN_MIGRATIONS=false`, поэтому миграции не гоняются репликами. + +Если позже изменить `S3_SECRET_ACCESS_KEY` в `.env`, bootstrap нужно повторить +вручную: `docker compose run --rm --no-deps -T s3-init`. IAM-user обновится, +API продолжит работать с новым секретом. + +Готовность: + +```sh +curl -fsS http://localhost:8080/health/ready +``` + +Если shell не видит опубликованные порты Docker (например, агент запущен в +отдельном контейнере), проверки выполняются из контейнера в сети стенда: + +```sh +docker run --rm --network mtusi2026lab_default -v "$PWD:/work:ro" -w /work \ + python:3.12-alpine python3 tests/smoke.py --base-url http://haproxy:8080 +``` + +Для hook-тестов (`persistence`, `failover`) удобнее подключить сам shell к сети +стенда и использовать `BASE_URL=http://haproxy:8080`: + +```sh +docker network connect mtusi2026lab_default +``` + +Это изменение только тестового окружения; в приложении `localhost:8080` +остаётся штатным адресом при опубликованном порте. + +### Проверки + +```sh +make check +python3 -m venv .venv && .venv/bin/pip install -r requirements-dev.txt +make validate PYTHON=.venv/bin/python + +cd app && go vet ./... && go test ./... && go build ./... && cd .. + +make test BASE_URL=http://localhost:8080 +make test-persistence BASE_URL=http://localhost:8080 ACTION_SCRIPT=scripts/restart.sh +make test-failover BASE_URL=http://localhost:8080 ACTION_SCRIPT=scripts/stop-one.sh +make test-concurrency BASE_URL=http://localhost:8080 +make test-attachments BASE_URL=http://localhost:8080 +make test-upload-memory +make test-reconcile +make test-private-bucket +make test-attachment-failover +make test-interrupted-upload +``` + +Если `localhost` недоступен, заменить `BASE_URL` на `http://haproxy:8080` и +выполнять из контейнера/сети стенда, как описано выше. `make test-failover` +намеренно оставляет одну реплику остановленной; восстановление: + +```sh +docker compose --env-file .env up -d --scale api=2 api +``` + +### Остановка + +```sh +docker compose --env-file .env down # volumes сохраняются +docker compose --env-file .env down -v # намеренное удаление учебных данных +``` + +`down -v` удаляет PostgreSQL, Redis и RustFS volumes. При смене +`POSTGRES_PASSWORD` на уже существующем `pgdata` PostgreSQL не пустит API: +нужно либо вернуть прежний пароль, либо осознанно пересоздать volume на +отдельном стенде. Это проверено на практике. + +## Архитектура и решения + +```text +Клиент + | + v +HAProxy :8080 (единственный опубликованный вход, stats :8404) + | + +--> api-1 (Go/Fiber, expose 8080) --+ + +--> api-2 (Go/Fiber, expose 8080) --+--> PostgreSQL 16.10 (метаданные, истина) + +--> Redis 7.4 (сессии, AOF) + +--> RustFS 1.0.4 (приватные байты) + | + reconciler (pending/orphan cleanup) + +s3-init (one-shot): bucket + anonymous=private + IAM messenger-api + policy +migrate (one-shot): встроенные миграции 0001..0003 +``` + +| Роль | Где | Ответственность | +| --- | --- | --- | +| Вход | `haproxy` | round-robin, health `GET /health/ready`, 16 слотов DNS | +| API | `api` × 2 | stateless HTTP, авторизация, streaming upload/download | +| Метаданные | `postgres` | пользователи, чаты, сообщения, idempotency, attachments | +| Сессии | `redis` | SHA-256 digest токена, absolute TTL | +| Байты | `rustfs` | private bucket, object key назначает сервер | +| Bootstrap | `s3-init` | идемпотентные bucket/IAM/policy | +| Сверка | `reconciler` | stale pending и orphan objects после grace period | + +### Что перенесено из lab3 + +Сохранены пользователи, Basic-login, сессии в Redis, чаты, сообщения, +идемпотентность POST сообщений, пагинация, HAProxy, две API-реплики, +readiness с PostgreSQL/Redis, graceful shutdown, `X-Request-Id` и +`X-Instance-Id`, а также форматы ошибок. Текстовые сообщения остаются +валидными; `attachment_ids` по умолчанию `[]`. + +### Что добавлено в lab4 + +- Метаданные вложения хранятся в PostgreSQL, байты — только в RustFS. В API нет + ни локального постоянного файла оригинала, ни публичного URL bucket. +- Object key: `attachments//`. Имя файла от клиента + используется только для отображения и никогда не становится путём. +- Загрузка: авторизация по членству в чате → проверка filename и + Content-Type → ограниченное чтение тела во временный файл с SHA-256 → + MIME/DecodeConfig/full decode → `pending` row → S3 PUT → `ready` row → 201. + 201 означает, что сохранены и объект, и метаданные. +- Если процесс упал после `pending`, но до `ready`, reconciler удаляет и row, и + объект после grace period. Если объект остался без row (крайне узкое окно + или ручное вмешательство), reconciler удаляет orphan. +- `/health/ready` требует PostgreSQL, Redis и S3 `HeadBucket`; при SIGTERM + сначала выставляется флаг shutdown и readiness начинает отдавать 503. +- Bucket не опубликован, console выключена. API и reconciler используют + отдельного IAM-user с policy только на `attachments/*`; root-credential есть + только у `s3-init`. +- `/health/ready` и все ответы API по-прежнему содержат `X-Request-Id` и + `X-Instance-Id`. + +### Как посмотреть bucket без публичного порта + +S3 API и admin console не опубликованы, поэтому осмотр и проверки выполняются +одноразовым контейнером в сети стенда. Тот же приём используют +`scripts/check-private-bucket.sh` и `scripts/verify-reconcile.sh`: + +```sh +docker run --rm --network mtusi2026lab_default --env-file .env -e HOME=/tmp \ + --entrypoint /bin/sh rustfs/rc:v0.1.36 -c \ + 'rc alias set app "$S3_ENDPOINT" "$S3_ACCESS_KEY_ID" "$S3_SECRET_ACCESS_KEY" >/dev/null && + rc ls "app/$S3_BUCKET/attachments/"' +``` + +Контейнер использует scoped-ключ API, а не root; для административных действий +(например, повторного bootstrap) есть отдельный root-ключ у `s3-init`. + +### Транзакционные границы и согласованность + +1. Валидация и чтение тела заканчиваются до создания строки. Обрыв соединения + не создаёт ни row, ни object. +2. `INSERT ... status='pending'` коммитится до S3 PUT. Это намеренно: при + падении процесса остаётся указатель, по которому reconciler найдёт объект. +3. S3 PUT выполняется с `Content-Length` и `Content-Type`; при ошибке — + компенсирующее удаление объекта и pending-строки. +4. `UPDATE ... SET status='ready'` выполняется после успешного PUT. Ошибка + обновления также запускает компенсацию. +5. Сообщение и `message_attachments` вставляются в одной транзакции с + idempotency-записью. Все вложения проверяются на принадлежность чату и + статус `ready` в той же транзакции. +6. Fingerprint идемпотентности: для сообщений без вложений сохранён старый + `sha256(text)`; с вложениями добавлены канонически отсортированные UUID. +7. Reconciler работает с grace period (по умолчанию 900 с), поэтому свежая + загрузка не может быть удалена как «зависшая». + +### Лимиты, таймауты и память + +| Механизм | Значение | Где | +| --- | --- | --- | +| Максимальный размер оригинала | 10 MiB = 10 485 760 байт | config + SQL constraint | +| Максимум пикселей | 40 000 000 | config, проверяется по header до decode | +| Глобальный HTTP body | 10 MiB + 1 MiB | Fiber `BodyLimit` | +| JSON body | 1 MiB | `common.BindJSON` | +| Копирование upload | 32 KiB buffer, `io.LimitReader` | handler | +| Бюджет decode | 256 MiB weighted semaphore | `attachments/image.go` | +| Upload/download timeout | 30 с / 60 с | handler context | +| S3 health | 2 с | `HeadBucket` | +| Pending/orphan grace | 900 с | reconciler | + +Тело не читается через `io.ReadAll` для upload и не буферизуется Fiber +(`StreamRequestBody: true`). Даже отклонённый по `Content-Length` запрос +дочитывается только в пределах `max + 1 MiB`, чтобы клиент успел получить 413, +а не connection reset. + +## Доказательства критериев + +Все команды выполнены на запущенном стенде 2026-10-31; smoke, негативные +сценарии, reconciler и interrupted upload повторены 2026-11-06 после +рефакторинга с тем же результатом. `BASE_URL` в лог-файлах — +`http://haproxy:8080` (shell подключён к сети стенда). + +| Критерий | Команда/сценарий | Наблюдаемый результат | Файл | +| --- | --- | --- | --- | +| Публичный smoke lab4 | `make test BASE_URL=http://haproxy:8080` | `PASS: lab4 public API scenario` | `evidence/smoke.txt` | +| S3 в общем стенде, приватный bucket | `make test-private-bucket` | anonymous GET → 403; scoped PUT/HEAD в `attachments/` разрешён, вне prefix и CreateBucket запрещены | `evidence/private-bucket.txt` | +| Идемпотентный bootstrap | `docker compose run --rm --no-deps -T s3-init` дважды | оба запуска `S3 bootstrap complete`, exit 0 | `evidence/s3-bootstrap.txt` | +| Upload 201 ready + checksum | public smoke | `status=ready`, `variants=[]`, `error_code=null`, SHA-256 и size совпали, download байт-в-байт | `evidence/smoke.txt` | +| Доступ участника и запрет постороннему | `make test-attachments` | bob скачал оригинал; eve получила 404 на metadata и bytes | `evidence/attachments-negative.txt` | +| Слишком большой файл | `make test-attachments` | 10 MiB + 1 → 413, без 5xx | `evidence/attachments-negative.txt` | +| Повреждённое изображение | `make test-attachments` | PNG-сигнатура с битым телом → 415 | `evidence/attachments-negative.txt` | +| Подмена MIME | `make test-attachments` | PNG с `Content-Type: image/jpeg` → 415 | `evidence/attachments-negative.txt` | +| Чужой attachment_id | `make test-attachments` | 404 для metadata/content; чужой чат → 404 | `evidence/attachments-negative.txt` | +| Вложение другого чата | `make test-attachments` | привязка к другому чату → 404 в обе стороны | `evidence/attachments-negative.txt` | +| Пользовательская ошибка не 500 | `make test-attachments` | все негативные сценарии 400/404/413/415 | `evidence/attachments-negative.txt` | +| Upload → остановка реплики → download с другой | `make test-attachment-failover` | upload принят `ad8141b5e17c`, download обслужен `2229551f45ac`, SHA-256 совпал | `evidence/attachment-failover.txt` | +| Обрыв загрузки | `make test-interrupted-upload` | rows 52 → 52, objects 52 → 52, temp files 0 | `evidence/interrupted-upload.txt` | +| Reconciliation: stale pending | `make test-reconcile` | dry-run ничего не изменил; реальный прогон `deleted_rows=1 deleted_objects=1` | `evidence/reconcile.txt` | +| Reconciliation: fresh pending | `make test-reconcile` | строка и объект остались при `grace=1s` | `evidence/reconcile.txt` | +| Reconciliation: orphan object | `make test-reconcile` | после удаления row объект удалён `deleted_objects=1` | `evidence/reconcile.txt` | +| Reconciliation: ready не трогается | `make test-reconcile` | row и объект ready-вложения остались | `evidence/reconcile.txt` | +| ≥10 одновременных загрузок, пик RAM | `make test-upload-memory` | 10 × 7 563 763 B, 2 реплики, peak 354.1 MiB total, 174.1/180.1 MiB на реплику при лимите 512 MiB | `evidence/upload-memory.json`, `evidence/upload-memory-run.txt` | +| Persistence после restart | `make test-persistence ACTION_SCRIPT=scripts/restart.sh` | PASS; `StartedAt` PostgreSQL/Redis/RustFS/HAProxy/API изменился, данные и сессия сохранились | `evidence/persistence.txt`, `evidence/persistence-containers.txt` | +| Failover одной API-реплики | `make test-failover ACTION_SCRIPT=scripts/stop-one.sh` | 2 реплики до отказа, 118 успешных записей, 1 переходная ошибка, recovery 2.557 с, дублей нет | `evidence/failover.txt` | +| Конкурентная идемпотентность | `make test-concurrency` | 12 запросов, 2 реплики, одна запись | `evidence/idempotency.txt` | +| Readiness отражает PostgreSQL | stop/start `postgres`, запрос `/health/ready` внутри API | 503 при остановке, 200 после восстановления | `evidence/readiness-postgres.txt` | +| Readiness отражает Redis | stop/start `redis` | 503 при остановке, 200 после восстановления | `evidence/readiness-redis.txt` | +| Readiness отражает S3 | stop/start `rustfs` | 503 при остановке, 200 после восстановления | `evidence/readiness-s3.txt` | +| Graceful shutdown | SIGTERM/`docker stop` при restart/failover | процесс пишет `shutting down`, readiness 503, соединения завершаются; hooks укладываются в лимит | `evidence/persistence.txt`, `evidence/failover.txt` | +| Offline-контракт | `make check`, `make validate` | оба PASS | вывод команд в этом отчёте | + +## Эксперименты и отказы + +### Профиль стенда + +Docker на Linux, Compose bridge network, лимиты из `.env`: +API — 0.5 CPU / 512 MiB на реплику, PostgreSQL — 1.0 CPU / 512 MiB, +Redis — 0.5 CPU / 256 MiB, RustFS — 1.0 CPU / 512 MiB, +HAProxy — 0.5 CPU / 128 MiB. Это учебный стенд, а не production-измерение; +результаты зависят от хоста и общего Docker daemon. + +### Нагрузка POST сообщений + +`python3 tests/load.py --base-url http://haproxy:8080 --duration 15 --concurrency 8 --output evidence/load.json` + +| Показатель | Значение | +| --- | --- | +| Длительность / concurrency | 15.216 с / 8 | +| Успешные / ошибки | 6249 / 0 | +| Successful RPS | 410.689 | +| p50 / p95 / p99 | 18.104 / 31.058 / 46.769 мс | + +Это closed-loop генератор: он страдает coordinated omission, не делает +прозрачных retry и измеряет только успешные HTTP-подтверждения. Числа +показывают, что стенд не деградировал после добавления S3, но не являются +пределом пропускной способности. + +### Память при одновременных загрузках + +Методика: 10 потоков одновременно отправляют один и тот же сгенерированный +PNG 1800×1400 (7 563 763 байт, 2.52 Мп) через HAProxy; host-side скрипт +опрашивает Docker Engine stats каждые 50 мс и суммирует `memory_stats.usage` +двух API-контейнеров. Перед прогоном API перезапущены, чтобы baseline не +содержал page cache прошлых тестов. + +| Показатель | Значение | +| --- | --- | +| Baseline total | 14.7 MiB | +| Peak total | 354.1 MiB | +| Peak на реплику | 174.1 / 180.1 MiB | +| Лимит на реплику | 512 MiB | +| Время 10 загрузок | 2.60 с | +| Проверка download | SHA-256 совпал | + +Одиночная загрузка того же файла на свежем стенде дала peak 280.4 MiB total +(142.9/137.5 MiB на контейнер). Рост при переходе от 1 к 10 одновременным +загрузкам существенно меньше 10×: тело не буферизуется целиком, decode +ограничен семафором. Docker stats включает page cache, поэтому абсолютные +значения выше, чем «чистая» heap-память Go. + +### Persistence + +`scripts/restart.sh` перезапускает `postgres redis rustfs haproxy api` без +удаления volumes. В `evidence/persistence-containers.txt` видно, что +`StartedAt` всех пяти сервисов изменился, а публичный тест после этого снова +прочитал исходное сообщение, сессию и оригинал вложения. Это подтверждает, что +hook действительно перезапустил процессы, а не только вернул 0. + +### Failover + +`scripts/stop-one.sh` сопоставляет `TARGET_INSTANCE_ID` с hostname контейнера и +оставляет ровно эту реплику остановленной. Публичный тест наблюдал 2 реплики, +отправил 118 успешных записей за 15 секунд, допустил 1 переходную ошибку, +зафиксировал восстановление за 2.557 с и не нашёл дублей. Отдельный +`verify-attachment-failover.sh` загрузил файл через одну реплику, остановил её +и скачал тот же SHA-256 через другую. + +### Матрица отказов + +| Воздействие | Ожидание | Наблюдение | Восстановление / потери | +| --- | --- | --- | --- | +| Остановлена одна API-реплика | HAProxy исключает её, вторая продолжает | 2 реплики до, 118 успешных записей, 1 переходная ошибка | 2.557 с; подтверждённые данные не потеряны | +| Upload, затем остановка принявшей реплики | Файл доступен через другую | download обслужила другая реплика, SHA-256 совпал | без потерь, 1 попытка | +| Restart всего стенда | Сессии, сообщения и файлы сохраняются | persistence PASS; все `StartedAt` изменились | volumes сохранены, повторный логин не нужен | +| Остановлен RustFS | readiness 503, новые upload не принимаются | `/health/ready` → 503 | после старта → 200, данные в volume | +| Остановлен Redis | readiness 503 | `/health/ready` → 503 | после старта → 200; сессия пережила restart в persistence-тесте | +| Остановлен PostgreSQL | readiness 503 | `/health/ready` → 503 | после старта → 200; данные в volume | +| Обрыв HTTP-загрузки | Нет row/object/temp | rows и objects не изменились, temp files 0 | автоматически, без reconciler | +| Падение между pending и ready | reconciler удаляет пару после grace | dry-run + реальный прогон | до 900 с удержания, затем очистка | +| Orphan object без row | reconciler удаляет после grace | `deleted_objects=1` | до 900 с, затем очистка | +| Переполнение диска хоста | Не гарантируется | один прогон дал 500/503 (`No space left on device`) | после очистки build cache повторные прогоны зелёные; это ограничение стенда, не заявленная HA-функция | + +## Ограничения и следующие шаги + +- Стенд не является production-HA: PostgreSQL, Redis и RustFS — одиночные + контейнеры с volume; HAProxy и API реплицированы, но хост и хранилища не + кластеризованы. Остановка БД или S3 — это отказ, а не прозрачное + переключение. +- Reconciler — safety net, а не мгновенная транзакция. Между S3 PUT и + `status=ready` существует окно, в котором объект уже есть, но 201 ещё не + возвращён; после grace периода такая пара удаляется. Клиент, не получивший + 201, должен повторить загрузку, что создаст новый object key. +- Grace period 900 с и интервал 300 с — учебные значения. Для реальной системы + их нужно согласовать с максимальной длительностью загрузки и SLA очистки. +- Лимит 10 MiB / 40 Мп и таймауты 30/60 с проверены на локальном стенде. На + медленном канале загрузка 10 MiB может не уложиться в 30 с; это осознанное + ограничение, а не обещание поддерживать любой RTT. +- Нет presigned URL, фоновой обработки, thumbnails и вариантов изображений: + контракт lab4 этого не требует, а обязательный proxy download оставлен + единственным способом доступа к байтам. +- Нет отдельного API для удаления вложений и сообщений; очистка выполняется + только reconciler-ом для abandoned данных. Пользовательские данные в + учебном стенде не удаляются автоматически. +- Нагрузка измерена closed-loop генератором; для честного throughput нужен + open-loop/k6 с фиксированным arrival rate и отдельным профилем upload. +- Проверка private bucket и scoped policy выполняется изнутри Compose network; + снаружи S3 API и console недоступны, что и требуется. +- Все секреты хранятся только в локальном `.env`; в отчёт, evidence и git они + не попадают. `CHANGELOG.md` теперь отслеживается, `.env` — нет. diff --git a/app/.air.toml b/app/.air.toml new file mode 100755 index 00000000..ab919098 --- /dev/null +++ b/app/.air.toml @@ -0,0 +1,59 @@ +#:schema https://json.schemastore.org/any.json + +env_files = [] +root = "." +testdata_dir = "testdata" +tmp_dir = "tmp" + +[build] +args_bin = [] +bin = "./tmp/main" +cmd = "go build -o ./tmp/main ./cmd/server/" +delay = 1000 +entrypoint = ["./tmp/main"] +exclude_dir = ["assets", "tmp", "vendor", "testdata"] +exclude_file = [] +exclude_regex = ["_test.go"] +exclude_unchanged = false +follow_symlink = false +full_bin = "" +ignore_dangerous_root_dir = false +include_dir = [] +include_ext = ["go", "tpl", "tmpl", "html"] +include_file = [] +kill_delay = "0s" +log = "build-errors.log" +poll = false +poll_interval = 0 +post_cmd = [] +pre_cmd = [] +rerun = false +rerun_delay = 500 +send_interrupt = false +stop_on_error = false + +[color] +app = "" +build = "yellow" +main = "magenta" +mode = "" +runner = "green" +watcher = "cyan" + +[log] +main_only = false +silent = false +time = false + +[misc] +clean_on_exit = false + +[proxy] +app_port = 0 +app_start_timeout = 0 +enabled = false +proxy_port = 0 + +[screen] +clear_on_rebuild = false +keep_scroll = true diff --git a/app/cmd/reconciler/main.go b/app/cmd/reconciler/main.go new file mode 100644 index 00000000..c2746ecc --- /dev/null +++ b/app/cmd/reconciler/main.go @@ -0,0 +1,105 @@ +// Command reconciler periodically removes abandoned attachment objects and +// pending metadata rows. It is safe to run concurrently with the API and safe +// to re-run; see internal/reconcile for the grace-period rules. +package main + +import ( + "context" + "flag" + "log" + "log/slog" + "os/signal" + "syscall" + "time" + + "gitflic.ru/project/bulla-max/mtusi2026lab/internal/config" + "gitflic.ru/project/bulla-max/mtusi2026lab/internal/database" + "gitflic.ru/project/bulla-max/mtusi2026lab/internal/objectstore" + "gitflic.ru/project/bulla-max/mtusi2026lab/internal/reconcile" + "gitflic.ru/project/bulla-max/mtusi2026lab/internal/store" +) + +func main() { + once := flag.Bool("once", false, "run one reconciliation pass and exit") + dryRun := flag.Bool("dry-run", false, "log what would be removed without changing either store") + grace := flag.Duration("grace", 0, "override RECONCILE_GRACE_SECONDS for this run") + interval := flag.Duration("interval", 0, "override RECONCILE_INTERVAL_SECONDS for this run") + flag.Parse() + + cfg, err := config.Load() + if err != nil { + log.Fatalf("configuration: %v", err) + } + if *grace > 0 { + cfg.ReconcileGrace = *grace + } + if *interval > 0 { + cfg.ReconcileInterval = *interval + } + + rootCtx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM) + defer stop() + + pool, err := database.Connect(rootCtx, cfg.DatabaseURL, cfg.DBMaxConns, cfg.StartupWait) + if err != nil { + log.Fatalf("database: %v", err) + } + defer pool.Close() + + objects, err := objectstore.New(objectstore.Config{ + Endpoint: cfg.S3Endpoint, + Region: cfg.S3Region, + Bucket: cfg.S3Bucket, + AccessKey: cfg.S3AccessKey, + SecretKey: cfg.S3SecretKey, + }) + if err != nil { + log.Fatalf("object storage: %v", err) + } + + worker := reconcile.New( + store.New(pool, cfg.DBTimeout, cfg.IdempotencyTTL), + objects, + cfg.ReconcileGrace, + slog.Default(), + ) + + if *once { + stats, runErr := worker.RunOnce(rootCtx, *dryRun) + logStats(stats, *dryRun) + if runErr != nil { + log.Fatalf("reconcile: %v", runErr) + } + return + } + + for { + stats, runErr := worker.RunOnce(rootCtx, *dryRun) + if runErr != nil { + slog.Error("reconcile pass failed", "err", runErr) + } else { + logStats(stats, *dryRun) + } + + // A timer avoids the ticker behaviour where missed ticks accumulate: + // the next pass starts one full interval after this one finished. + timer := time.NewTimer(cfg.ReconcileInterval) + select { + case <-rootCtx.Done(): + timer.Stop() + return + case <-timer.C: + } + } +} + +func logStats(stats reconcile.Stats, dryRun bool) { + slog.Info("reconcile pass complete", + "dry_run", dryRun, + "stale_pending", stats.StalePending, + "orphan_objects", stats.OrphanObjects, + "deleted_rows", stats.DeletedRows, + "deleted_objects", stats.DeletedObjects, + "skipped", stats.Skipped, + ) +} diff --git a/app/cmd/server/main.go b/app/cmd/server/main.go new file mode 100755 index 00000000..e6f0be26 --- /dev/null +++ b/app/cmd/server/main.go @@ -0,0 +1,79 @@ +// Command server starts the messenger HTTP API, applies optional migrations, +// and shuts down gracefully on SIGINT/SIGTERM. +package main + +import ( + "context" + "log" + "net" + "os/signal" + "syscall" + "time" + + "gitflic.ru/project/bulla-max/mtusi2026lab/internal" + "gitflic.ru/project/bulla-max/mtusi2026lab/internal/attachments" + "gitflic.ru/project/bulla-max/mtusi2026lab/internal/config" + "gitflic.ru/project/bulla-max/mtusi2026lab/internal/database" + "gitflic.ru/project/bulla-max/mtusi2026lab/internal/health" + "gitflic.ru/project/bulla-max/mtusi2026lab/internal/session" +) + +func main() { + cfg, err := config.Load() + if err != nil { + log.Fatalf("configuration: %v", err) + } + + rootCtx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM) + defer stop() + + pool, err := database.Connect(rootCtx, cfg.DatabaseURL, cfg.DBMaxConns, cfg.StartupWait) + if err != nil { + log.Fatalf("database: %v", err) + } + defer pool.Close() + + if cfg.MigrateOnly || cfg.RunMigrations { + if err = database.Migrate(cfg.DatabaseURL); err != nil { + log.Fatalf("migrations: %v", err) + } + } + + if cfg.MigrateOnly { + log.Printf("migrations applied; migrate-only process exiting") + return + } + + sessions, err := session.New(cfg.CacheURL, cfg.SessionTTL, cfg.RedisTimeout) + if err != nil { + log.Fatalf("session store: %v", err) + } + defer sessions.Close() + + attachments.CleanupStaleUploads(cfg.UploadTempDir, time.Hour) + + app, err := internal.New(cfg, pool, sessions) + if err != nil { + log.Fatalf("application: %v", err) + } + + addr := net.JoinHostPort(cfg.Host, cfg.Port) + listenErr := make(chan error, 1) + go func() { + listenErr <- app.Listen(addr) + }() + + select { + case err = <-listenErr: + if err != nil { + log.Fatalf("server: %v", err) + } + case <-rootCtx.Done(): + } + + log.Printf("shutting down") + health.SignalShutdown() + if err = app.ShutdownWithTimeout(10 * time.Second); err != nil { + log.Printf("shutdown: %v", err) + } +} diff --git a/app/go.mod b/app/go.mod new file mode 100755 index 00000000..8657c1a4 --- /dev/null +++ b/app/go.mod @@ -0,0 +1,54 @@ +module gitflic.ru/project/bulla-max/mtusi2026lab + +go 1.27.1 + +require ( + github.com/aws/aws-sdk-go-v2 v1.47.5 + github.com/aws/aws-sdk-go-v2/config v1.33.13 + github.com/aws/aws-sdk-go-v2/credentials v1.20.13 + github.com/aws/aws-sdk-go-v2/service/s3 v1.114.6 + github.com/aws/smithy-go v1.28.7 + github.com/gofiber/fiber/v3 v3.5.0 + github.com/golang-migrate/migrate/v4 v4.20.1 + github.com/google/uuid v1.6.0 + github.com/jackc/pgx/v5 v5.11.0 + github.com/redis/go-redis/v9 v9.23.0 + golang.org/x/crypto v0.57.0 + golang.org/x/sync v0.24.0 +) + +require ( + github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.7.25 // indirect + github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.20.5 // indirect + github.com/aws/aws-sdk-go-v2/internal/configsources v1.5.8 // indirect + github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.8.8 // indirect + github.com/aws/aws-sdk-go-v2/internal/v4a v1.5.8 // indirect + github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.13.23 // indirect + github.com/aws/aws-sdk-go-v2/service/internal/checksum v1.11.9 // indirect + github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.14.8 // indirect + github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.20.8 // indirect + github.com/aws/aws-sdk-go-v2/service/signin v1.12.1 // indirect + github.com/aws/aws-sdk-go-v2/service/sso v1.39.1 // indirect + github.com/aws/aws-sdk-go-v2/service/ssooidc v1.44.1 // indirect + github.com/aws/aws-sdk-go-v2/service/sts v1.51.6 // indirect + github.com/cespare/xxhash/v2 v2.3.0 // indirect + github.com/gofiber/schema v1.8.8 // indirect + github.com/gofiber/utils/v2 v2.6.1 // indirect + github.com/jackc/pgerrcode v0.0.0-20220416144525-469b46aa5efa // indirect + github.com/jackc/pgpassfile v1.0.0 // indirect + github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect + github.com/jackc/puddle/v2 v2.2.2 // indirect + github.com/klauspost/compress v1.20.1 // indirect + github.com/mattn/go-colorable v0.1.15 // indirect + github.com/mattn/go-isatty v0.0.24 // indirect + github.com/molecule-man/go-brrr v1.1.1 // indirect + github.com/philhofer/fwd v1.2.0 // indirect + github.com/tinylib/msgp v1.6.5 // indirect + github.com/valyala/bytebufferpool v1.0.0 // indirect + github.com/valyala/fasthttp v1.74.0 // indirect + go.uber.org/atomic v1.12.0 // indirect + golang.org/x/net v0.59.0 // indirect + golang.org/x/sys v0.48.0 // indirect + golang.org/x/text v0.42.0 // indirect + golang.org/x/tools v0.50.0 // indirect +) diff --git a/app/go.sum b/app/go.sum new file mode 100755 index 00000000..8f93f820 --- /dev/null +++ b/app/go.sum @@ -0,0 +1,167 @@ +github.com/Azure/go-ansiterm v0.0.0-20250102033503-faa5f7b0171c h1:udKWzYgxTojEKWjV8V+WSxDXJ4NFATAsZjh8iIbsQIg= +github.com/Azure/go-ansiterm v0.0.0-20250102033503-faa5f7b0171c/go.mod h1:xomTg63KZ2rFqZQzSB4Vz2SUXa1BpHTVz9L5PTmPC4E= +github.com/Microsoft/go-winio v0.6.2 h1:F2VQgta7ecxGYO8k3ZZz3RS8fVIXVxONVUPlNERoyfY= +github.com/Microsoft/go-winio v0.6.2/go.mod h1:yd8OoFMLzJbo9gZq8j5qaps8bJ9aShtEA8Ipt1oGCvU= +github.com/aws/aws-sdk-go-v2 v1.47.5 h1:5yA+A+BiXbDq1ZLbYwPViwhEGDMdR0MEhL8Jh2LnLS4= +github.com/aws/aws-sdk-go-v2 v1.47.5/go.mod h1:iNIkogrRvFwm+1OQIUOP2lPBtlXZboOIUWiCa+pHEMw= +github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.7.25 h1:MW0Iql1SOTgQPj8mCxrU/eH/K/AIDWi08ItOsXaWRLU= +github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.7.25/go.mod h1:+ucVYmHYB3JqVueY0E1RHahdXAnKQbIHqybSbvOUbOM= +github.com/aws/aws-sdk-go-v2/config v1.33.13 h1:u52x6ISQOx172tpwM0VRxhJzEsTJYpMcEYUGM2A1wrw= +github.com/aws/aws-sdk-go-v2/config v1.33.13/go.mod h1:/ixtz6XPHb42zekjQ0LqD9XjTpTysNWhIGwufY1OzI4= +github.com/aws/aws-sdk-go-v2/credentials v1.20.13 h1:j4z41OtspmQ3ftQycaDunJ26b69a9SHfaSVUPqgRv8k= +github.com/aws/aws-sdk-go-v2/credentials v1.20.13/go.mod h1:SgPA1oS3b+dlMxZb618jDmGNscpXSCaxz15igS52OlE= +github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.20.5 h1:iXjy2SFhGYNB1jWZBhQqq8HbhsbPF23w+/zbMvPXsTg= +github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.20.5/go.mod h1:k0RUiBnyXU0AXRJ9tJmY2jQke6XUIX2PNVnWtvgaPtc= +github.com/aws/aws-sdk-go-v2/internal/configsources v1.5.8 h1:gzIWc3DTPfPbWKrQnmL6BV6WD7NPD1htxzgdk/bQ/Z4= +github.com/aws/aws-sdk-go-v2/internal/configsources v1.5.8/go.mod h1:vvUA5cDAZWGo4R14FZPZfXfJCmgsafOWEq78Kg0D+ug= +github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.8.8 h1:PWv+vG8aCLS8qd+HPdYhK7Vees90aKHWFFQ1AGYD/8E= +github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.8.8/go.mod h1:xxPZEpflYHJA12366ZGyq66z9dHPCoeCIu99Jck+MPw= +github.com/aws/aws-sdk-go-v2/internal/v4a v1.5.8 h1:WOp5De9iWdEHDPh2Kb0tBP3vGBm/dVAuRGh4a4tqduo= +github.com/aws/aws-sdk-go-v2/internal/v4a v1.5.8/go.mod h1:z8W8YgfhbWl62XqcTtK+CxPrE+iHGRVUOtrrbIOp7ZY= +github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.13.23 h1:MCIR6iZaCKO4UE0hUvoWWLI6JVjppopV64JfAJaMjng= +github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.13.23/go.mod h1:pphWFS+z0MmIPivj8c/6bATd1Z+Av7Ust75DPdEFsPA= +github.com/aws/aws-sdk-go-v2/service/internal/checksum v1.11.9 h1:GACgECI422Xt5n4Pojr3O0aocPv8lfH4PJe2Aon5Wv8= +github.com/aws/aws-sdk-go-v2/service/internal/checksum v1.11.9/go.mod h1:lPfnRkHpn1qWZJLbVEw10rIqSuCee1pNQTyGkUjAYh8= +github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.14.8 h1:+PMpWlJYOn2tm7sm6OZ/ZLd4jStl17T7wjNapd1Rczs= +github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.14.8/go.mod h1:cnG3xZLGQL4c/bt8pjdzlFtZsW7epBBuNS04a5xMjdo= +github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.20.8 h1:RS1Wt+K0r4X0ci9Vf4JYTUfHzsQihYA2LgGJeEneKpk= +github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.20.8/go.mod h1:adMnTy5kyoUW86jhXnGSVc/mvMQI4GZd1qYWMeaS8M8= +github.com/aws/aws-sdk-go-v2/service/s3 v1.114.6 h1:to7+Vr3rLkpYRwVKJ5QRriI0eE306vrVN/6TnVHgxvY= +github.com/aws/aws-sdk-go-v2/service/s3 v1.114.6/go.mod h1:Jc89ASa2k7HMAskj3bSnt8bf+k+p2IApzkoXnEjkgUw= +github.com/aws/aws-sdk-go-v2/service/signin v1.12.1 h1:WZLC+k/CZx11kZGQDqu9mpA8yhiXGX2I//jhuJE1FDo= +github.com/aws/aws-sdk-go-v2/service/signin v1.12.1/go.mod h1:wxAk0D2oVBSjuXEIzvcKxTZfM90zticdAbh/VD42L0Y= +github.com/aws/aws-sdk-go-v2/service/sso v1.39.1 h1:4VB/dnDo7pmXSLQMszSoBzWndENuc7mzwTpD6lQn77A= +github.com/aws/aws-sdk-go-v2/service/sso v1.39.1/go.mod h1:UGsEYgWz4Gi+46vDQ5zkFLqUgJziyOE2SXjZjgmfmHc= +github.com/aws/aws-sdk-go-v2/service/ssooidc v1.44.1 h1:w8yui2ZqdHmGwVBDGvazoIvKw/DnLENBLkYnN8q8iqo= +github.com/aws/aws-sdk-go-v2/service/ssooidc v1.44.1/go.mod h1:gJSdIY6YUzDvqQLcMtPVZgBRHrFwzHryvpE8Igzbxps= +github.com/aws/aws-sdk-go-v2/service/sts v1.51.6 h1:BVjKrtRUtSYKztBCCfE1oAB+d3ViKcW2cKZfserOqMQ= +github.com/aws/aws-sdk-go-v2/service/sts v1.51.6/go.mod h1:eSukFLeLkPMm+1OxDR+F0joauZdyBkF47Ow7MqjZiJw= +github.com/aws/smithy-go v1.28.7 h1:h/n1SHrwVtaDYLz8mR1/Kq4Xh4RaFbFW7bOYckg9mfE= +github.com/aws/smithy-go v1.28.7/go.mod h1:YE2RhdIuDbA5E5bTdciG9KrW3+TiEONeUWCqxX9i1Fc= +github.com/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs= +github.com/bsm/ginkgo/v2 v2.12.0/go.mod h1:SwYbGRRDovPVboqFv0tPTcG1sN61LM1Z4ARdbAV9g4c= +github.com/bsm/gomega v1.27.10 h1:yeMWxP2pV2fG3FgAODIY8EiRE3dy0aeFYt4l7wh6yKA= +github.com/bsm/gomega v1.27.10/go.mod h1:JyEr/xRbxbtgWNi8tIEVPUYZ5Dzef52k01W3YH0H+O0= +github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs= +github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= +github.com/containerd/errdefs v1.0.0 h1:tg5yIfIlQIrxYtu9ajqY42W3lpS19XqdxRQeEwYG8PI= +github.com/containerd/errdefs v1.0.0/go.mod h1:+YBYIdtsnF4Iw6nWZhJcqGSg/dwvV7tyJ/kCkyJ2k+M= +github.com/containerd/errdefs/pkg v0.3.0 h1:9IKJ06FvyNlexW690DXuQNx2KA2cUJXx151Xdx3ZPPE= +github.com/containerd/errdefs/pkg v0.3.0/go.mod h1:NJw6s9HwNuRhnjJhM7pylWwMyAkmCQvQ4GpJHEqRLVk= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/dhui/dktest v0.4.6 h1:+DPKyScKSEp3VLtbMDHcUq6V5Lm5zfZZVb0Sk7Ahom4= +github.com/dhui/dktest v0.4.6/go.mod h1:JHTSYDtKkvFNFHJKqCzVzqXecyv+tKt8EzceOmQOgbU= +github.com/distribution/reference v0.6.0 h1:0IXCQ5g4/QMHHkarYzh5l+u8T3t73zM5QvfrDyIgxBk= +github.com/distribution/reference v0.6.0/go.mod h1:BbU0aIcezP1/5jX/8MP0YiH4SdvB5Y4f/wlDRiLyi3E= +github.com/docker/docker v28.5.2+incompatible h1:DBX0Y0zAjZbSrm1uzOkdr1onVghKaftjlSWt4AFexzM= +github.com/docker/docker v28.5.2+incompatible/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk= +github.com/docker/go-connections v0.7.0 h1:6SsRfJddP22WMrCkj19x9WKjEDTB+ahsdiGYf0mN39c= +github.com/docker/go-connections v0.7.0/go.mod h1:no1qkHdjq7kLMGUXYAduOhYPSJxxvgWBh7ogVvptn3Q= +github.com/docker/go-units v0.5.0 h1:69rxXcBk27SvSaaxTtLh/8llcHD8vYHT7WSdRZ/jvr4= +github.com/docker/go-units v0.5.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk= +github.com/felixge/httpsnoop v1.1.0 h1:3YtUj32ZZkqZtt3sZZsClsymw/QDuVfpNhoA31zeORc= +github.com/felixge/httpsnoop v1.1.0/go.mod h1:Zqxgdd+1Rkcz8euOqdr7lqgCRJztwr5hp9vDSi5UZCE= +github.com/fxamacker/cbor/v2 v2.9.4 h1:xwjVlxEMR3S605oUlgBjKLTTeGFciYPGYCtF/35LKGo= +github.com/fxamacker/cbor/v2 v2.9.4/go.mod h1:vM4b+DJCtHn+zz7h3FFp/hDAI9WNWCsZj23V5ytsSxQ= +github.com/go-logr/logr v1.4.3 h1:CjnDlHq8ikf6E492q6eKboGOC0T8CDaOvkHCIg8idEI= +github.com/go-logr/logr v1.4.3/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY= +github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag= +github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE= +github.com/gofiber/fiber/v3 v3.5.0 h1:dk7TOUH6DXJGtOLsN2XEG+0ZML7cznzHILTVozbNEK8= +github.com/gofiber/fiber/v3 v3.5.0/go.mod h1:GOVDTW+gjJvfe0iJyVujbQ1Lnx+JUjFySJRI/9/xX/w= +github.com/gofiber/schema v1.8.8 h1:md5t65+Z9LttOuj7FdMcQ63D2+6GniT2ganLXwvTJM4= +github.com/gofiber/schema v1.8.8/go.mod h1:Hzf0L/fTPP1IwOAwKipKflm9C2722aebnSKJCMOgb1I= +github.com/gofiber/utils/v2 v2.6.1 h1:x3zsS/oUQz0ia2Zx8XXtG/7naR5a+/95FRO8mwQ5QQ4= +github.com/gofiber/utils/v2 v2.6.1/go.mod h1:tc9KqdRAQZRD/u0mN2d1ZHROGheSZv+0FcC9cg5OEdw= +github.com/golang-migrate/migrate/v4 v4.20.1 h1:2N/ToVTKrKl58ynBpgeVJ4In7VcLCjWTZtm4eP1LxhU= +github.com/golang-migrate/migrate/v4 v4.20.1/go.mod h1:DDPgKVb4ovSWc4FwSPfV2Uz1160f4XBiTHTrAJtljmM= +github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= +github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/jackc/pgerrcode v0.0.0-20220416144525-469b46aa5efa h1:s+4MhCQ6YrzisK6hFJUX53drDT4UsSW3DEhKn0ifuHw= +github.com/jackc/pgerrcode v0.0.0-20220416144525-469b46aa5efa/go.mod h1:a/s9Lp5W7n/DD0VrVoyJ00FbP2ytTPDVOivvn2bMlds= +github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM= +github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg= +github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 h1:iCEnooe7UlwOQYpKFhBabPMi4aNAfoODPEFNiAnClxo= +github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM= +github.com/jackc/pgx/v5 v5.11.0 h1:IzBBtyK9AHqf98cctWFifYSci2hgQR/cd56wB4p+ogg= +github.com/jackc/pgx/v5 v5.11.0/go.mod h1:mal1tBGAFfLHvZzaYh77YS/eC6IX9OWbRV1QIIM0Jn4= +github.com/jackc/puddle/v2 v2.2.2 h1:PR8nw+E/1w0GLuRFSmiioY6UooMp6KJv0/61nB7icHo= +github.com/jackc/puddle/v2 v2.2.2/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4= +github.com/klauspost/compress v1.20.1 h1:T7kKElXUMXrUJ2E9QhQhxFtcK5rPyLdsGZvdbLMPdiQ= +github.com/klauspost/compress v1.20.1/go.mod h1:LUdAzn7YLVvxLpc7y3V1m40wESHTgc1422pwwBSKYuI= +github.com/klauspost/cpuid/v2 v2.4.0 h1:S6Hrbc7+ywsr0r+RLapfGBHfyefhCTwEh3A0tV913Dw= +github.com/klauspost/cpuid/v2 v2.4.0/go.mod h1:19jmZ9mjzoF//ddRSUsv0zfBTJWh3QJh9FNxZTMrGxU= +github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw= +github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= +github.com/mattn/go-colorable v0.1.15 h1:+u9SLTRGnXv73cEsnsmoZBom+dMU88B2M0aDcWy0/jY= +github.com/mattn/go-colorable v0.1.15/go.mod h1:6LmQG8QLFO4G5z1gPvYEzlUgJ2wF+stgPZH1UqBm1s8= +github.com/mattn/go-isatty v0.0.24 h1:tGZZoVgT/KiqK1c8ocVLeDS8BSWMRd47J3Lbz7vsReI= +github.com/mattn/go-isatty v0.0.24/go.mod h1:nMCL3Zebbrt45jsMDgnfIwz6ydEQApk5oEI3HqDio6A= +github.com/moby/docker-image-spec v1.3.1 h1:jMKff3w6PgbfSa69GfNg+zN/XLhfXJGnEx3Nl2EsFP0= +github.com/moby/docker-image-spec v1.3.1/go.mod h1:eKmb5VW8vQEh/BAr2yvVNvuiJuY6UIocYsFu/DxxRpo= +github.com/moby/moby/api v1.54.2 h1:wiat9QAhnDQjA7wk1kh/TqHz2I1uUA7M7t9SAl/JNXg= +github.com/moby/moby/api v1.54.2/go.mod h1:+RQ6wluLwtYaTd1WnPLykIDPekkuyD/ROWQClE83pzs= +github.com/moby/moby/client v0.4.1 h1:DMQgisVoMkmMs7fp3ROSdiBnoAu8+vo3GggFl06M/wY= +github.com/moby/moby/client v0.4.1/go.mod h1:z52C9O2POPOsnxZAy//WtKcQ32P+jT/NGeXu/7nfjGQ= +github.com/moby/term v0.5.2 h1:6qk3FJAFDs6i/q3W/pQ97SX192qKfZgGjCQqfCJkgzQ= +github.com/moby/term v0.5.2/go.mod h1:d3djjFCrjnB+fl8NJux+EJzu0msscUP+f8it8hPkFLc= +github.com/molecule-man/go-brrr v1.1.1 h1:KYwusQhjtV3smALnhmafZf4OBrh7vP4p8O47DyUmmuY= +github.com/molecule-man/go-brrr v1.1.1/go.mod h1:7ybW6/7gA3oKY45jOfVNjSJDtrr6ea4tzbsTkjmQDC4= +github.com/morikuni/aec v1.0.0 h1:nP9CBfwrvYnBRgY6qfDQkygYDmYwOilePFkwzv4dU8A= +github.com/morikuni/aec v1.0.0/go.mod h1:BbKIizmSmc5MMPqRYbxO4ZU0S0+P200+tUnFx7PXmsc= +github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8Oi/yOhh5U= +github.com/opencontainers/go-digest v1.0.0/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3IKzErnv2BNG4W4MAM= +github.com/opencontainers/image-spec v1.1.1 h1:y0fUlFfIZhPF1W537XOLg0/fcx6zcHCJwooC2xJA040= +github.com/opencontainers/image-spec v1.1.1/go.mod h1:qpqAh3Dmcf36wStyyWU+kCeDgrGnAve2nCC8+7h8Q0M= +github.com/philhofer/fwd v1.2.0 h1:e6DnBTl7vGY+Gz322/ASL4Gyp1FspeMvx1RNDoToZuM= +github.com/philhofer/fwd v1.2.0/go.mod h1:RqIHx9QI14HlwKwm98g9Re5prTQ6LdeRQn+gXJFxsJM= +github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= +github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/redis/go-redis/v9 v9.23.0 h1:/TTVdJa8BRNsybwwGGnmiYktHNlUXtbOQxlWpHBPL3s= +github.com/redis/go-redis/v9 v9.23.0/go.mod h1:EWP2UUk+XDaI1s5nQB5mablZN6vccXlmBQTyfnF87D0= +github.com/shamaton/msgpack/v3 v3.2.3 h1:0r0aZtrXDFOPpwWJTEOpFq+Ih/JmMLm0pqLN5mSilrw= +github.com/shamaton/msgpack/v3 v3.2.3/go.mod h1:sgBYvEiyz8JR1NC3yGRoPVME9xXovpnh3l/plW1nfRo= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.12.1 h1:EuwCh5fleGS7H32xRwO3wRGT7DxrDhLAT6FF8MpWDWE= +github.com/stretchr/testify v1.12.1/go.mod h1:MDEgiDPPsNp5cuIrHPPCyornHKgEVbtFUmoNlxoYthg= +github.com/tinylib/msgp v1.6.5 h1:iAH6XTP7BpzErjBZlujJQXxA+GmRi2YWs6M4KdLtNbE= +github.com/tinylib/msgp v1.6.5/go.mod h1:RSp0LW9oSxFut3KzESt5Voq4GVWyS+PSulT77roAqEA= +github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= +github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc= +github.com/valyala/fasthttp v1.74.0 h1:wMS9fnO2QTALozYx5pId2Vi7ZwU/epUkY8i/KPWCHoU= +github.com/valyala/fasthttp v1.74.0/go.mod h1:3ARmLamUcw7ElxVtC8PXaGzQ6VEuvnetlkrwIklQBSE= +github.com/x448/float16 v0.8.4 h1:qLwI1I70+NjRFUR3zs1JPUCgaCXSh3SW62uAKT1mSBM= +github.com/x448/float16 v0.8.4/go.mod h1:14CWIYCyZA/cWjXOioeEpHeN/83MdbZDRQHoFcYsOfg= +github.com/zeebo/xxh3 v1.1.0 h1:s7DLGDK45Dyfg7++yxI0khrfwq9661w9EN78eP/UZVs= +github.com/zeebo/xxh3 v1.1.0/go.mod h1:IisAie1LELR4xhVinxWS5+zf1lA4p0MW4T+w+W07F5s= +go.opentelemetry.io/auto/sdk v1.2.1 h1:jXsnJ4Lmnqd11kwkBV2LgLoFMZKizbCi5fNZ/ipaZ64= +go.opentelemetry.io/auto/sdk v1.2.1/go.mod h1:KRTj+aOaElaLi+wW1kO/DZRXwkF4C5xPbEe3ZiIhN7Y= +go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.69.0 h1:8tvICD4vSTOOsNrsI4Ljf6C+6UKvpTEH5XY3JMoyPoo= +go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.69.0/go.mod h1:z9+yiacE0IHRqM4qFfkbt/JYlmYXgss8GY/jXoNuPJI= +go.opentelemetry.io/otel v1.44.0 h1:JjwHmHpA4iZ3wBxluu2fbbE7j4kqlE8jXyAyPXH7HqU= +go.opentelemetry.io/otel v1.44.0/go.mod h1:BMgjTHL9WPRlRjL2oZCBTL4whCGtXch2H4BhOPIAyYc= +go.opentelemetry.io/otel/metric v1.44.0 h1:1w0gILTcHdr3YI+ixLyjemwrVnsMURbTZFrSYCdDdmc= +go.opentelemetry.io/otel/metric v1.44.0/go.mod h1:8O7hanEPBNgEMmybD3s2VBKcgWOCsA6tzHBPODAiquo= +go.opentelemetry.io/otel/trace v1.44.0 h1:jxF5CsGYCe74MCRx2X4g7WsY/VBKRqqpNvXlX/6gtIk= +go.opentelemetry.io/otel/trace v1.44.0/go.mod h1:oLl1jrMQAVo6v3GAggN+1VH9VIz9iUSvW53sW1Q8PIE= +go.uber.org/atomic v1.12.0 h1:BvcXdFKuviU4fTL/f+SxdQ5qJX/Jix8pAkgdUcb3XOE= +go.uber.org/atomic v1.12.0/go.mod h1:I6c4cg+6HCxRjfjSsYtApoFILnpc0CGUdGkXVqbYVNk= +go.yaml.in/yaml/v3 v3.0.5 h1:N6y/pJk8buWs9NY5ERU2HSMfm+IuD/OtfdAnq6kESPw= +go.yaml.in/yaml/v3 v3.0.5/go.mod h1:HVTZu1O7/Vkt2N+BFy8Zza+lnLsABggaTM2ZpNIGuKg= +golang.org/x/crypto v0.57.0 h1:3ZVCjf8Ggz7zneR/EHRVx68Ctf+2pmIMP2UFhh9cC6M= +golang.org/x/crypto v0.57.0/go.mod h1:Fdz0i5U6CoizGwLda9DttjSk6qlZo25zYNtR+ycvuZA= +golang.org/x/net v0.59.0 h1:5zfYln+w5XCxwrnMMJPufRgNoXEaGxl0wo5GqPXyues= +golang.org/x/net v0.59.0/go.mod h1:2DA/G1UfVbCpQPeWTmMPGY7Cs2PkBkwu743bVX5PIVg= +golang.org/x/sync v0.24.0 h1:iqiDrGoDgyuvAKrhLQthUEzU/R378N78gXbBjxed7oA= +golang.org/x/sync v0.24.0/go.mod h1:sUUOizhqBxiL6pEWpqNLUiaJn1ShEbZ6BBqskPbjZm0= +golang.org/x/sys v0.48.0 h1:bbX/i/6MgT9BVLM9RT1thmxL04yeTAhbEz4SyadbXoo= +golang.org/x/sys v0.48.0/go.mod h1:hNLxWAXmnKAxqDtdwIYC4bM9oQPEecfsnNMuSxOs3og= +golang.org/x/text v0.42.0 h1:JbOZXgfeCPU9gacVtYliJqOhD+zhrEqK4LfdpmlUZqI= +golang.org/x/text v0.42.0/go.mod h1:ojzP1Z+2QtioaF8DTtO8K5q7JWVVYwZKenzujK0Zd0E= +golang.org/x/tools v0.50.0 h1:c2ifzfcuY7L90lZ2aKd8S4K2NpASF08SZx9ZuJkHmSU= +golang.org/x/tools v0.50.0/go.mod h1:7ulVMw3831Mwi5EZD6RomGyffr4VFjuNYXf2BbCEAV0= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/app/internal/app.go b/app/internal/app.go new file mode 100755 index 00000000..c9f0c7e1 --- /dev/null +++ b/app/internal/app.go @@ -0,0 +1,120 @@ +// Package internal wires HTTP routes, middleware and storage into one Fiber +// application and owns the process-wide error handler. +package internal + +import ( + "errors" + "log/slog" + + "gitflic.ru/project/bulla-max/mtusi2026lab/internal/attachments" + "gitflic.ru/project/bulla-max/mtusi2026lab/internal/auth" + "gitflic.ru/project/bulla-max/mtusi2026lab/internal/chats" + "gitflic.ru/project/bulla-max/mtusi2026lab/internal/common" + "gitflic.ru/project/bulla-max/mtusi2026lab/internal/config" + "gitflic.ru/project/bulla-max/mtusi2026lab/internal/health" + "gitflic.ru/project/bulla-max/mtusi2026lab/internal/messages" + "gitflic.ru/project/bulla-max/mtusi2026lab/internal/objectstore" + "gitflic.ru/project/bulla-max/mtusi2026lab/internal/session" + "gitflic.ru/project/bulla-max/mtusi2026lab/internal/store" + "gitflic.ru/project/bulla-max/mtusi2026lab/internal/users" + "github.com/gofiber/fiber/v3" + "github.com/gofiber/fiber/v3/middleware/recover" + "github.com/gofiber/fiber/v3/middleware/requestid" + "github.com/jackc/pgx/v5/pgxpool" +) + +func New(cfg config.Config, pool *pgxpool.Pool, sessions *session.Store) (*fiber.App, error) { + st := store.New(pool, cfg.DBTimeout, cfg.IdempotencyTTL) + + objects, err := objectstore.New(objectstore.Config{ + Endpoint: cfg.S3Endpoint, + Region: cfg.S3Region, + Bucket: cfg.S3Bucket, + AccessKey: cfg.S3AccessKey, + SecretKey: cfg.S3SecretKey, + }) + if err != nil { + return nil, err + } + + app := fiber.New(fiber.Config{ + ReadTimeout: cfg.ReadTimeout, + WriteTimeout: cfg.WriteTimeout, + IdleTimeout: cfg.IdleTimeout, + // BodyLimit is the coarse HTTP envelope. The attachment handler and + // BindJSON enforce their own smaller budgets. + BodyLimit: cfg.HTTPBodyLimit, + // Without StreamRequestBody fasthttp buffers the entire body + // before the handler runs. Streaming lets the upload path copy in + // 32 KiB chunks and stops a 10 MiB body from becoming a per-request + // allocation even when the request is later rejected. + StreamRequestBody: true, + ErrorHandler: handleError, + }) + + app.Use(recover.New()) + app.Use(requestid.New()) + app.Use(func(c fiber.Ctx) error { + c.Set("X-Instance-Id", cfg.InstanceID) + return c.Next() + }) + + healthHandler := health.NewHandler(st, sessions, objects) + healthRoutes := app.Group("/health") + healthRoutes.Get("/live", healthHandler.Live) + healthRoutes.Get("/ready", healthHandler.Ready) + + authHandler := auth.NewHandler(st, sessions) + usersHandler := users.NewHandler(st, config.MaxJSONBodyBytes) + chatsHandler := chats.NewHandler(st, config.MaxJSONBodyBytes) + messagesHandler := messages.NewHandler(st, config.MaxJSONBodyBytes) + attachmentsHandler := attachments.NewHandler( + st, + objects, + attachments.NewValidator(cfg.MaxImagePixels, cfg.ImageDecodeBytes), + cfg.MaxUploadBytes, + cfg.UploadDrainLimit, + cfg.UploadTimeout, + cfg.DownloadTimeout, + cfg.UploadTempDir, + ) + + api := app.Group("/api/v1") + api.Post("/users", usersHandler.CreateUser) + api.Post("/auth/sessions", authHandler.CreateSession) + + protected := api.Group("", auth.BearerAuth(sessions)) + protected.Get("/users/me", usersHandler.GetCurrentUser) + protected.Delete("/auth/sessions/current", authHandler.RevokeSession) + protected.Get("/chats", chatsHandler.ListChats) + protected.Post("/chats", chatsHandler.CreateChat) + protected.Get("/chats/:chat_id", chatsHandler.GetChat) + protected.Get("/chats/:chat_id/messages", messagesHandler.ListMessages) + protected.Post("/chats/:chat_id/messages", messagesHandler.CreateMessage) + protected.Post("/chats/:chat_id/attachments", attachmentsHandler.Upload) + protected.Get("/attachments/:attachment_id", attachmentsHandler.Get) + protected.Get("/attachments/:attachment_id/content", attachmentsHandler.Download) + + return app, nil +} + +// handleError is the only writer of error responses: handlers and the store +// return *common.AppError and this function decides status, headers and body. +func handleError(c fiber.Ctx, err error) error { + if appErr, ok := errors.AsType[*common.AppError](err); ok { + if appErr.Challenge != "" { + c.Set(fiber.HeaderWWWAuthenticate, appErr.Challenge) + } + return common.SendError(c, appErr.Status, appErr.Code, appErr.Message) + } + + if fiberErr, ok := errors.AsType[*fiber.Error](err); ok { + return common.SendError(c, fiberErr.Code, + common.ErrorCodeFromStatus(fiberErr.Code), fiberErr.Message) + } + + slog.Error("unhandled request error", + "request_id", requestid.FromContext(c), "err", err) + return common.SendError(c, fiber.StatusInternalServerError, + common.ErrorCodeInternal, "Internal server error") +} diff --git a/app/internal/attachments/handler.go b/app/internal/attachments/handler.go new file mode 100644 index 00000000..6fb87491 --- /dev/null +++ b/app/internal/attachments/handler.go @@ -0,0 +1,329 @@ +package attachments + +import ( + "bufio" + "bytes" + "context" + "crypto/sha256" + "encoding/hex" + "errors" + "fmt" + "io" + "log/slog" + "mime" + "os" + "strconv" + "time" + + "gitflic.ru/project/bulla-max/mtusi2026lab/internal/common" + "gitflic.ru/project/bulla-max/mtusi2026lab/internal/model" + "gitflic.ru/project/bulla-max/mtusi2026lab/internal/objectstore" + "gitflic.ru/project/bulla-max/mtusi2026lab/internal/store" + "github.com/gofiber/fiber/v3" + "github.com/google/uuid" +) + +const copyBufferSize = 32 * 1024 + +type Handler struct { + store *store.Store + objects *objectstore.Client + validator *Validator + maxUploadBytes int64 + drainLimit int64 + uploadTimeout time.Duration + downloadTimeout time.Duration + tempDir string +} + +func NewHandler(st *store.Store, client *objectstore.Client, validator *Validator, maxUploadBytes, drainLimit int64, uploadTimeout, downloadTimeout time.Duration, tempDir string) *Handler { + return &Handler{ + store: st, + objects: client, + validator: validator, + maxUploadBytes: maxUploadBytes, + drainLimit: drainLimit, + uploadTimeout: uploadTimeout, + downloadTimeout: downloadTimeout, + tempDir: tempDir, + } +} + +// Upload stores raw PNG/JPEG bytes. The order is deliberate: +// 1. authorize before reading the body; +// 2. stream the body to a bounded temporary file while hashing; +// 3. validate pixels and decode; +// 4. commit a pending metadata row; +// 5. PUT the object; +// 6. mark the row ready. +// +// A 201 is therefore returned only after both the object and its metadata are +// durable. Every failure between 4 and 6 has a compensation path, and the +// reconciler covers crashes that prevent compensation from running. +func (h *Handler) Upload(c fiber.Ctx) error { + userID := common.MustUserID(c) + + chatID, err := common.PathUUID(c, "chat_id") + if err != nil { + return err + } + + // Authorization precedes any body I/O: an outsider cannot make the server + // read a large upload before learning that the chat is not theirs. + isMember, err := h.store.IsMember(c.Context(), userID, chatID) + if err != nil { + return err + } + if !isMember { + return common.NotFound("chat not found") + } + + filename := c.Query("filename") + if err := common.ValidateFilename(filename); err != nil { + return err + } + + contentType, _, err := mime.ParseMediaType(c.Get(fiber.HeaderContentType)) + if err != nil || (contentType != "image/png" && contentType != "image/jpeg") { + return common.UnsupportedMedia("Content-Type must be image/png or image/jpeg") + } + + // Reject an obviously oversized request before allocating a temp file or + // decoding anything. Chunked requests are caught by the streaming counter + // below. + // + // AGENT: the declared body is drained first, but only within the bounded + // HTTP envelope (upload limit plus the JSON body slack). If the server + // closed immediately, the client would often see a connection reset instead + // of the required 413 while it was still sending. Draining is bounded and + // uses a 32 KiB buffer, so it does not weaken the memory guarantee. + if rawLength := c.Get(fiber.HeaderContentLength); rawLength != "" { + declared, parseErr := strconv.ParseInt(rawLength, 10, 64) + if parseErr != nil || declared < 0 { + return common.BadRequest("invalid Content-Length") + } + if declared > h.maxUploadBytes { + if declared <= h.drainLimit { + _, _ = io.CopyBuffer(io.Discard, io.LimitReader(requestBody(c), declared), make([]byte, copyBufferSize)) + } + return common.PayloadTooLarge("upload exceeds the byte limit") + } + } + + if err = os.MkdirAll(h.tempDir, 0o700); err != nil { + slog.Error("create upload temp dir", "err", err) + return common.Internal() + } + file, err := os.CreateTemp(h.tempDir, "upload-*.part") + if err != nil { + slog.Error("create upload temp file", "err", err) + return common.Internal() + } + filePath := file.Name() + defer func() { + _ = file.Close() + _ = os.Remove(filePath) + }() + + // AGENT: the 32 KiB copy buffer is the only request-sized allocation here. + // The body is never read with io.ReadAll, so ten simultaneous 10 MiB + // uploads do not require ten 10 MiB Go slices. + var reader io.Reader = requestBody(c) + hasher := sha256.New() + copyBuffer := make([]byte, copyBufferSize) + written, err := io.CopyBuffer(io.MultiWriter(file, hasher), io.LimitReader(reader, h.maxUploadBytes+1), copyBuffer) + if err != nil { + return common.BadRequest("incomplete upload body") + } + if written == 0 { + return common.UnsupportedMedia("upload body is empty") + } + if written > h.maxUploadBytes { + return common.PayloadTooLarge("upload exceeds the byte limit") + } + + ctx, cancel := context.WithTimeout(c.Context(), h.uploadTimeout) + defer cancel() + + info, err := h.validator.Validate(ctx, file, contentType) + if err != nil { + return err + } + + attachmentID := uuid.New() + attachment := model.Attachment{ + ID: attachmentID, + ChatID: chatID, + OwnerID: userID, + Filename: filename, + ContentType: info.ContentType, + SizeBytes: written, + SHA256: hex.EncodeToString(hasher.Sum(nil)), + Status: "pending", + Variants: []model.Variant{}, + ErrorCode: nil, + CreatedAt: common.NowUTC(), + UpdatedAt: common.NowUTC(), + // AGENT: the key is server-generated from UUIDs and never contains the + // client filename. That keeps traversal and collisions out of the + // object store even before application authorization is considered. + ObjectKey: objectKey(chatID, attachmentID), + } + + if err = h.store.CreatePendingAttachment(ctx, attachment); err != nil { + return err + } + + if _, err = file.Seek(0, 0); err != nil { + h.compensate(attachment.ID, attachment.ObjectKey) + return common.BadRequest("cannot read upload body") + } + if err = h.objects.Put(ctx, attachment.ObjectKey, file, written, attachment.ContentType); err != nil { + slog.Error("store attachment object", "err", err, "attachment_id", attachment.ID) + h.compensate(attachment.ID, attachment.ObjectKey) + return common.Unavailable("object storage unavailable") + } + + updatedAt, err := h.store.MarkAttachmentReady(ctx, attachment.ID) + if err != nil { + slog.Error("mark attachment ready", "err", err, "attachment_id", attachment.ID) + h.compensate(attachment.ID, attachment.ObjectKey) + return common.Unavailable("attachment metadata unavailable") + } + + attachment.Status = "ready" + attachment.UpdatedAt = common.RFCFormTime(updatedAt) + return c.Status(fiber.StatusCreated).JSON(attachment) +} + +// compensate is best effort by design. If either delete fails, the pending row +// or the object remains visible to the reconciler, which retries after the +// configured grace period. +func (h *Handler) compensate(attachmentID uuid.UUID, key string) { + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + defer cancel() + + if err := h.objects.Delete(ctx, key); err != nil && !errors.Is(err, objectstore.ErrNotFound) { + slog.Error("compensate: delete object", "err", err, "attachment_id", attachmentID) + } + if err := h.store.DeletePendingAttachment(ctx, attachmentID); err != nil { + slog.Error("compensate: delete pending row", "err", err, "attachment_id", attachmentID) + } +} + +func (h *Handler) Get(c fiber.Ctx) error { + userID := common.MustUserID(c) + + attachmentID, err := common.PathUUID(c, "attachment_id") + if err != nil { + return err + } + + attachment, err := h.store.GetAttachmentForUser(c.Context(), userID, attachmentID) + if err != nil { + return err + } + if attachment.Status != "ready" { + return common.Conflict("attachment is not ready") + } + + return c.JSON(attachment) +} + +func (h *Handler) Download(c fiber.Ctx) error { + userID := common.MustUserID(c) + + attachmentID, err := common.PathUUID(c, "attachment_id") + if err != nil { + return err + } + + variant := c.Query("variant", "original") + if variant == "" { + variant = "original" + } + if variant != "original" { + return common.BadRequest("unsupported variant") + } + + attachment, err := h.store.GetAttachmentForUser(c.Context(), userID, attachmentID) + if err != nil { + return err + } + if attachment.Status != "ready" { + return common.Conflict("attachment is not ready") + } + + ctx, cancel := context.WithTimeout(c.Context(), h.downloadTimeout) + + body, size, err := h.objects.Get(ctx, attachment.ObjectKey) + if err != nil { + cancel() + if errors.Is(err, objectstore.ErrNotFound) { + slog.Error("attachment object missing", "attachment_id", attachment.ID) + return common.NotFound("attachment content not found") + } + slog.Error("read attachment object", "err", err, "attachment_id", attachment.ID) + return common.Unavailable("object storage unavailable") + } + if size != attachment.SizeBytes { + _ = body.Close() + cancel() + slog.Error("attachment object size mismatch", + "attachment_id", attachment.ID, "metadata_size", attachment.SizeBytes, "object_size", size) + return common.Unavailable("attachment content is inconsistent") + } + + c.Set(fiber.HeaderContentType, attachment.ContentType) + c.Set(fiber.HeaderContentLength, strconv.FormatInt(attachment.SizeBytes, 10)) + c.Set(fiber.HeaderContentDisposition, mime.FormatMediaType("inline", map[string]string{"filename": attachment.Filename})) + c.Set(fiber.HeaderCacheControl, "private, no-store") + c.Set("X-Content-Type-Options", "nosniff") + + // AGENT: SendStreamWriter copies the S3 body straight to the response. The + // API never materializes the whole object in RAM and never redirects the + // client to a bucket URL, so authorization is enforced on every byte. + // + // The context and body are released inside the callback, not by a defer in + // this handler: the handler returns before fasthttp copies the stream, and + // canceling the S3 request context earlier truncates the response. + return c.SendStreamWriter(func(writer *bufio.Writer) { + defer cancel() + defer func() { _ = body.Close() }() + if _, copyErr := io.Copy(writer, body); copyErr != nil { + slog.Error("stream attachment to client", "err", copyErr, "attachment_id", attachment.ID) + } + }) +} + +func requestBody(c fiber.Ctx) io.Reader { + if reader := c.Request().BodyStream(); reader != nil { + return reader + } + return bytes.NewReader(c.Body()) +} + +func objectKey(chatID, attachmentID uuid.UUID) string { + return fmt.Sprintf("attachments/%s/%s", chatID, attachmentID) +} + +// CleanupStaleUploads removes temp files abandoned by a previous crash. A +// running request removes its own file in a defer; this only handles the +// process-died-mid-upload case. +func CleanupStaleUploads(dir string, olderThan time.Duration) { + entries, err := os.ReadDir(dir) + if err != nil { + return + } + cutoff := time.Now().Add(-olderThan) + for _, entry := range entries { + if entry.IsDir() { + continue + } + info, infoErr := entry.Info() + if infoErr != nil || info.ModTime().After(cutoff) { + continue + } + _ = os.Remove(dir + string(os.PathSeparator) + entry.Name()) + } +} diff --git a/app/internal/attachments/image.go b/app/internal/attachments/image.go new file mode 100644 index 00000000..cff74a89 --- /dev/null +++ b/app/internal/attachments/image.go @@ -0,0 +1,129 @@ +// Package attachments implements the synchronous upload, metadata lookup and +// authorized download path for chat attachments. +package attachments + +import ( + "context" + "errors" + "image" + _ "image/jpeg" + _ "image/png" + "io" + "mime" + "net/http" + "os" + + "gitflic.ru/project/bulla-max/mtusi2026lab/internal/common" + "golang.org/x/sync/semaphore" +) + +type Validator struct { + maxPixels int64 + decodeBudget *semaphore.Weighted + budgetBytes int64 +} + +type ImageInfo struct { + ContentType string + Width int + Height int +} + +func NewValidator(maxPixels, decodeBudgetBytes int64) *Validator { + return &Validator{ + maxPixels: maxPixels, + decodeBudget: semaphore.NewWeighted(decodeBudgetBytes), + budgetBytes: decodeBudgetBytes, + } +} + +// Validate checks the declared media type against the real bytes and then +// decodes the image under a shared memory budget. The decode is what turns a +// structurally broken or truncated file into 415 instead of a saved object. +// +// AGENT: image.DecodeConfig first reads only the header. This is the important +// ordering: a decompression bomb is rejected from width×height before the +// decoder is allowed to allocate pixel memory. +func (v *Validator) Validate(ctx context.Context, file *os.File, declared string) (ImageInfo, error) { + mediaType, _, err := mime.ParseMediaType(declared) + if err != nil { + return ImageInfo{}, common.UnsupportedMedia("Content-Type must be image/png or image/jpeg") + } + if mediaType != "image/png" && mediaType != "image/jpeg" { + return ImageInfo{}, common.UnsupportedMedia("Content-Type must be image/png or image/jpeg") + } + + if _, err = file.Seek(0, 0); err != nil { + return ImageInfo{}, common.BadRequest("cannot read upload body") + } + head := make([]byte, 512) + n, readErr := file.Read(head) + if readErr != nil && !errors.Is(readErr, io.EOF) { + return ImageInfo{}, common.BadRequest("cannot read upload body") + } + detected := http.DetectContentType(head[:n]) + if detected != mediaType { + return ImageInfo{}, common.UnsupportedMedia("file content does not match Content-Type") + } + + if _, err = file.Seek(0, 0); err != nil { + return ImageInfo{}, common.BadRequest("cannot read upload body") + } + config, format, err := image.DecodeConfig(file) + if err != nil { + return ImageInfo{}, common.UnsupportedMedia("invalid or corrupted image") + } + if format != "png" && format != "jpeg" { + return ImageInfo{}, common.UnsupportedMedia("only PNG and JPEG images are accepted") + } + if (format == "png" && mediaType != "image/png") || (format == "jpeg" && mediaType != "image/jpeg") { + return ImageInfo{}, common.UnsupportedMedia("file content does not match Content-Type") + } + if config.Width <= 0 || config.Height <= 0 { + return ImageInfo{}, common.UnsupportedMedia("invalid or corrupted image") + } + + pixels := int64(config.Width) * int64(config.Height) + if pixels <= 0 || pixels > v.maxPixels { + return ImageInfo{}, common.PayloadTooLarge("image has too many pixels") + } + + // Approximate decoded RGBA size. The semaphore caps the sum across + // concurrent requests; a single 40 Mp image needs ~160 MiB, so a lower + // configured budget is a deliberate operator choice and becomes a 413. + weight := pixels * 4 + if weight > v.budgetBytes { + return ImageInfo{}, common.PayloadTooLarge("image is too large to validate") + } + if err = v.decodeBudget.Acquire(ctx, weight); err != nil { + return ImageInfo{}, common.Unavailable("image validation is busy") + } + defer v.decodeBudget.Release(weight) + + if _, err = file.Seek(0, 0); err != nil { + return ImageInfo{}, common.BadRequest("cannot read upload body") + } + info, decodeErr := safeDecode(file) + if decodeErr != nil { + return ImageInfo{}, common.UnsupportedMedia("invalid or corrupted image") + } + bounds := info.Bounds() + if bounds.Dx() != config.Width || bounds.Dy() != config.Height { + return ImageInfo{}, common.UnsupportedMedia("invalid or corrupted image") + } + + return ImageInfo{ContentType: mediaType, Width: config.Width, Height: config.Height}, nil +} + +// safeDecode converts a decoder panic into an ordinary error. A malformed +// client file must never crash the process or become a 500. +func safeDecode(file *os.File) (img image.Image, err error) { + defer func() { + if recovered := recover(); recovered != nil { + img = nil + err = errors.New("image decoder panic") + } + }() + img, _, err = image.Decode(file) + return img, err +} diff --git a/app/internal/attachments/image_test.go b/app/internal/attachments/image_test.go new file mode 100644 index 00000000..75030b53 --- /dev/null +++ b/app/internal/attachments/image_test.go @@ -0,0 +1,134 @@ +package attachments + +import ( + "bytes" + "context" + "image" + "image/color" + "image/jpeg" + "image/png" + "os" + "path/filepath" + "testing" + + "gitflic.ru/project/bulla-max/mtusi2026lab/internal/common" +) + +// writeTemp stores raw bytes in a temporary file and returns its path. The +// validator works on *os.File so it can seek, sniff and decode the same body +// without loading it into memory twice. +func writeTemp(t *testing.T, name string, data []byte) *os.File { + t.Helper() + path := filepath.Join(t.TempDir(), name) + if err := os.WriteFile(path, data, 0o600); err != nil { + t.Fatalf("write %s: %v", path, err) + } + file, err := os.Open(path) + if err != nil { + t.Fatalf("open %s: %v", path, err) + } + t.Cleanup(func() { _ = file.Close() }) + return file +} + +func encodePNG(t *testing.T, width, height int) []byte { + t.Helper() + img := image.NewRGBA(image.Rect(0, 0, width, height)) + for x := 0; x < width; x++ { + for y := 0; y < height; y++ { + img.Set(x, y, color.RGBA{R: uint8(x), G: uint8(y), B: 0x40, A: 0xff}) + } + } + var buf bytes.Buffer + if err := png.Encode(&buf, img); err != nil { + t.Fatalf("encode png: %v", err) + } + return buf.Bytes() +} + +func TestValidateAcceptsPNGAndJPEG(t *testing.T) { + validator := NewValidator(40_000_000, 256<<20) + ctx := context.Background() + + pngFile := writeTemp(t, "image.png", encodePNG(t, 8, 6)) + info, err := validator.Validate(ctx, pngFile, "image/png") + if err != nil { + t.Fatalf("valid png rejected: %v", err) + } + if info.ContentType != "image/png" || info.Width != 8 || info.Height != 6 { + t.Fatalf("unexpected png info: %+v", info) + } + + img := image.NewRGBA(image.Rect(0, 0, 4, 3)) + var buf bytes.Buffer + if err = jpeg.Encode(&buf, img, nil); err != nil { + t.Fatalf("encode jpeg: %v", err) + } + jpegFile := writeTemp(t, "image.jpg", buf.Bytes()) + info, err = validator.Validate(ctx, jpegFile, "image/jpeg; charset=binary") + if err != nil { + t.Fatalf("valid jpeg rejected: %v", err) + } + if info.ContentType != "image/jpeg" || info.Width != 4 || info.Height != 3 { + t.Fatalf("unexpected jpeg info: %+v", info) + } +} + +func TestValidateRejectsMismatchedOrBrokenContent(t *testing.T) { + validator := NewValidator(40_000_000, 256<<20) + ctx := context.Background() + pngBytes := encodePNG(t, 8, 6) + + cases := []struct { + name string + data []byte + declared string + code string + }{ + {"png declared as jpeg", pngBytes, "image/jpeg", common.ErrorCodeUnsupportedMedia}, + {"text declared as png", []byte("this is not an image at all"), "image/png", common.ErrorCodeUnsupportedMedia}, + {"unsupported declared type", pngBytes, "image/gif", common.ErrorCodeUnsupportedMedia}, + {"garbage declared type", pngBytes, "not a media type", common.ErrorCodeUnsupportedMedia}, + {"truncated png", pngBytes[:len(pngBytes)/2], "image/png", common.ErrorCodeUnsupportedMedia}, + {"empty body", nil, "image/png", common.ErrorCodeUnsupportedMedia}, + } + + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + file := writeTemp(t, "upload", tc.data) + if _, err := validator.Validate(ctx, file, tc.declared); !common.IsCode(err, tc.code) { + t.Fatalf("Validate(%q) = %v, want code %s", tc.name, err, tc.code) + } + }) + } +} + +func TestValidateRejectsTooManyPixelsBeforeDecoding(t *testing.T) { + validator := NewValidator(100, 256<<20) + file := writeTemp(t, "big.png", encodePNG(t, 20, 20)) + + _, err := validator.Validate(context.Background(), file, "image/png") + if !common.IsCode(err, common.ErrorCodePayloadTooLarge) { + t.Fatalf("Validate = %v, want %s", err, common.ErrorCodePayloadTooLarge) + } +} + +func TestValidateRejectsImageAboveDecodeBudget(t *testing.T) { + // A 100-pixel image needs ~400 bytes of RGBA memory, more than this budget. + validator := NewValidator(40_000_000, 100) + file := writeTemp(t, "budget.png", encodePNG(t, 10, 10)) + + _, err := validator.Validate(context.Background(), file, "image/png") + if !common.IsCode(err, common.ErrorCodePayloadTooLarge) { + t.Fatalf("Validate = %v, want %s", err, common.ErrorCodePayloadTooLarge) + } +} + +func TestSafeDecodeConvertsPanicToError(t *testing.T) { + // image.Decode does not panic on arbitrary bytes, so this only guards the + // contract that safeDecode always returns an error instead of crashing. + file := writeTemp(t, "junk", []byte{0x89, 'P', 'N', 'G', 0x0d, 0x0a, 0x1a, 0x0a, 0xff}) + if _, err := safeDecode(file); err == nil { + t.Fatal("safeDecode accepted junk bytes") + } +} diff --git a/app/internal/auth/handler.go b/app/internal/auth/handler.go new file mode 100644 index 00000000..c0383fd3 --- /dev/null +++ b/app/internal/auth/handler.go @@ -0,0 +1,127 @@ +// Package auth implements password hashing, Basic login, Bearer session +// middleware and the authentication routes. +package auth + +import ( + "encoding/base64" + "errors" + "log/slog" + "strings" + + "gitflic.ru/project/bulla-max/mtusi2026lab/internal/common" + "gitflic.ru/project/bulla-max/mtusi2026lab/internal/model" + "gitflic.ru/project/bulla-max/mtusi2026lab/internal/session" + "gitflic.ru/project/bulla-max/mtusi2026lab/internal/store" + "github.com/gofiber/fiber/v3" +) + +const ( + basicChallenge = `Basic realm="messenger", charset="UTF-8"` + bearerChallenge = `Bearer realm="messenger"` +) + +type Handler struct { + store *store.Store + sessions *session.Store +} + +func NewHandler(st *store.Store, sessions *session.Store) *Handler { + return &Handler{store: st, sessions: sessions} +} + +func (h *Handler) CreateSession(c fiber.Ctx) error { + username, password, ok := parseBasic(c.Get(fiber.HeaderAuthorization)) + if !ok { + return common.NewChallenge(basicChallenge, "invalid credentials") + } + + user, err := h.store.GetUserByUsername(c.Context(), username) + if err != nil { + if common.IsCode(err, common.ErrorCodeNotFound) { + // An unknown username returns before Argon2 runs, while a known + // username with a wrong password runs VerifyPassword. The branches + // therefore take different time, so username enumeration through + // response timing is theoretically possible. The contract only + // requires the same 401 body and challenge. + return common.NewChallenge(basicChallenge, "invalid credentials") + } + return err + } + + valid, err := VerifyPassword(user.PasswordHash, password) + if err != nil || !valid { + return common.NewChallenge(basicChallenge, "invalid credentials") + } + + token, expiresAt, err := h.sessions.Create(c.Context(), user.ID) + if err != nil { + slog.Error("create session", "err", err) + return common.Unavailable("session store unavailable") + } + + return c.Status(fiber.StatusCreated).JSON(model.Session{ + Token: token, + TokenType: "Bearer", + ExpiresAt: common.RFCFormTime(expiresAt), + User: user, + }) +} + +func (h *Handler) RevokeSession(c fiber.Ctx) error { + token, ok := common.SessionToken(c) + if !ok { + return common.NewChallenge(bearerChallenge, "authentication required") + } + + if err := h.sessions.Revoke(c.Context(), token); err != nil { + slog.Error("revoke session", "err", err) + return common.Unavailable("session store unavailable") + } + + return c.SendStatus(fiber.StatusNoContent) +} + +func BearerAuth(sessions *session.Store) fiber.Handler { + return func(c fiber.Ctx) error { + header := strings.TrimSpace(c.Get(fiber.HeaderAuthorization)) + scheme, token, found := strings.Cut(header, " ") + token = strings.TrimSpace(token) + if !found || !strings.EqualFold(scheme, "Bearer") || token == "" || strings.ContainsAny(token, " \t") { + return common.NewChallenge(bearerChallenge, "authentication required") + } + + userID, err := sessions.Lookup(c.Context(), token) + if err != nil { + if errors.Is(err, session.ErrNotFound) { + return common.NewChallenge(bearerChallenge, "session expired or revoked") + } + slog.Error("session lookup", "err", err) + return common.Unavailable("session store unavailable") + } + + c.Locals(common.KeyUserID, userID) + c.Locals(common.KeySessionToken, token) + return c.Next() + } +} + +func parseBasic(header string) (string, string, bool) { + scheme, encoded, found := strings.Cut(strings.TrimSpace(header), " ") + if !found || !strings.EqualFold(scheme, "Basic") || encoded == "" { + return "", "", false + } + + decoded, err := base64.StdEncoding.DecodeString(encoded) + if err != nil { + if decoded, err = base64.RawStdEncoding.DecodeString(encoded); err != nil { + return "", "", false + } + } + + // RFC 7617: split at the first colon; a colon is legal inside the password. + username, password, found := strings.Cut(string(decoded), ":") + if !found || username == "" || password == "" { + return "", "", false + } + return username, password, true +} diff --git a/app/internal/auth/password.go b/app/internal/auth/password.go new file mode 100644 index 00000000..ef929b4b --- /dev/null +++ b/app/internal/auth/password.go @@ -0,0 +1,73 @@ +package auth + +import ( + "crypto/rand" + "crypto/subtle" + "encoding/base64" + "errors" + "fmt" + "strings" + + "golang.org/x/crypto/argon2" +) + +const ( + argonMemory = 64 * 1024 + argonIterations = 2 + argonParallelism = 4 + argonKeyLength = 32 + argonSaltLength = 16 +) + +var ErrInvalidHash = errors.New("invalid password hash") + +func HashPassword(password string) (string, error) { + salt := make([]byte, argonSaltLength) + if _, err := rand.Read(salt); err != nil { + return "", fmt.Errorf("generate salt: %w", err) + } + + key := argon2.IDKey([]byte(password), salt, argonIterations, argonMemory, argonParallelism, argonKeyLength) + + return fmt.Sprintf("$argon2id$v=%d$m=%d,t=%d,p=%d$%s$%s", + argon2.Version, argonMemory, argonIterations, argonParallelism, + base64.RawStdEncoding.EncodeToString(salt), + base64.RawStdEncoding.EncodeToString(key)), nil +} + +func VerifyPassword(encoded, password string) (bool, error) { + parts := strings.Split(encoded, "$") + if len(parts) != 6 || parts[1] != "argon2id" { + return false, ErrInvalidHash + } + + var version int + if _, err := fmt.Sscanf(parts[2], "v=%d", &version); err != nil || version != argon2.Version { + return false, ErrInvalidHash + } + + var memory uint32 + var iterations uint32 + var parallelism uint8 + if _, err := fmt.Sscanf(parts[3], "m=%d,t=%d,p=%d", &memory, &iterations, ¶llelism); err != nil { + return false, ErrInvalidHash + } + // argon2.IDKey panics when the cost parameters are outside its supported + // range. A corrupted row or an attacker-supplied encoding must be rejected + // with ErrInvalidHash instead of taking the process down. + if iterations < 1 || parallelism < 1 || memory < 8*uint32(parallelism) { + return false, ErrInvalidHash + } + + salt, err := base64.RawStdEncoding.DecodeString(parts[4]) + if err != nil || len(salt) == 0 { + return false, ErrInvalidHash + } + expected, err := base64.RawStdEncoding.DecodeString(parts[5]) + if err != nil || len(expected) == 0 { + return false, ErrInvalidHash + } + + actual := argon2.IDKey([]byte(password), salt, iterations, memory, parallelism, uint32(len(expected))) + return subtle.ConstantTimeCompare(actual, expected) == 1, nil +} diff --git a/app/internal/auth/password_test.go b/app/internal/auth/password_test.go new file mode 100644 index 00000000..81829f5f --- /dev/null +++ b/app/internal/auth/password_test.go @@ -0,0 +1,64 @@ +package auth + +import ( + "strings" + "testing" +) + +func TestPasswordRoundTripPreservesFullRange(t *testing.T) { + passwords := []string{ + "simple-password-123", + "colon:inside:password", + "пароль-с-кириллицей-12", + strings.Repeat("😀", 128), + } + for _, password := range passwords { + hash, err := HashPassword(password) + if err != nil { + t.Fatalf("HashPassword: %v", err) + } + ok, err := VerifyPassword(hash, password) + if err != nil || !ok { + t.Fatalf("VerifyPassword rejected the correct password (ok=%v err=%v)", ok, err) + } + ok, err = VerifyPassword(hash, password+"x") + if err != nil || ok { + t.Fatalf("VerifyPassword accepted a different password (ok=%v err=%v)", ok, err) + } + } +} + +func TestPasswordHashIsSalted(t *testing.T) { + first, err := HashPassword("same-password-123") + if err != nil { + t.Fatal(err) + } + second, err := HashPassword("same-password-123") + if err != nil { + t.Fatal(err) + } + if first == second { + t.Fatal("two hashes of the same password are identical") + } + if _, err := VerifyPassword("not-a-hash", "same-password-123"); err == nil { + t.Fatal("invalid encoded hash was accepted") + } +} + +func TestVerifyPasswordRejectsMalformedCostAndEmptyDigest(t *testing.T) { + malformed := []string{ + // Empty digest would make argon2 derive a zero-length key. + "$argon2id$v=19$m=65536,t=2,p=4$c2FsdA$", + // Zero cost parameters are outside argon2's supported range and would + // otherwise panic inside the KDF. + "$argon2id$v=19$m=65536,t=0,p=4$c2FsdA$YWJjZA", + "$argon2id$v=19$m=65536,t=2,p=0$c2FsdA$YWJjZA", + "$argon2id$v=19$m=1,t=2,p=4$c2FsdA$YWJjZA", + } + for _, encoded := range malformed { + ok, err := VerifyPassword(encoded, "same-password-123") + if err == nil || ok { + t.Fatalf("VerifyPassword(%q) = (%v, %v), want ErrInvalidHash", encoded, ok, err) + } + } +} diff --git a/app/internal/chats/handler.go b/app/internal/chats/handler.go new file mode 100644 index 00000000..b6cb7a09 --- /dev/null +++ b/app/internal/chats/handler.go @@ -0,0 +1,86 @@ +// Package chats implements chat creation, lookup and cursor-paginated listing. +package chats + +import ( + "gitflic.ru/project/bulla-max/mtusi2026lab/internal/common" + "gitflic.ru/project/bulla-max/mtusi2026lab/internal/store" + "github.com/gofiber/fiber/v3" + "github.com/google/uuid" +) + +type Handler struct { + store *store.Store + jsonBodyLimit int64 +} + +func NewHandler(st *store.Store, jsonBodyLimit int64) *Handler { + return &Handler{store: st, jsonBodyLimit: jsonBodyLimit} +} + +type CreateChatRequest struct { + Title string `json:"title"` + MemberIDs []uuid.UUID `json:"member_ids"` +} + +func (h *Handler) CreateChat(c fiber.Ctx) error { + userID := common.MustUserID(c) + + var req CreateChatRequest + if err := common.BindJSON(c, &req, h.jsonBodyLimit); err != nil { + return err + } + + if err := common.ValidateChatTitle(req.Title); err != nil { + return err + } + if err := common.ValidateMemberIDs(req.MemberIDs, userID); err != nil { + return err + } + + chat, err := h.store.CreateChat(c.Context(), req.Title, req.MemberIDs) + if err != nil { + return err + } + + return c.Status(fiber.StatusCreated).JSON(chat) +} + +func (h *Handler) ListChats(c fiber.Ctx) error { + userID := common.MustUserID(c) + + limit, err := common.ParseLimit(c, 50) + if err != nil { + return err + } + + cursor, err := common.ParseCursor(c) + if err != nil { + return err + } + if cursor != nil && cursor.ContextID != userID { + return common.BadRequest("bad cursor value") + } + + page, err := h.store.ListChats(c.Context(), userID, limit, cursor) + if err != nil { + return err + } + + return c.JSON(page) +} + +func (h *Handler) GetChat(c fiber.Ctx) error { + userID := common.MustUserID(c) + + chatID, err := common.PathUUID(c, "chat_id") + if err != nil { + return err + } + + chat, err := h.store.GetChatForUser(c.Context(), userID, chatID) + if err != nil { + return err + } + + return c.JSON(chat) +} diff --git a/app/internal/common/attachments_validation_test.go b/app/internal/common/attachments_validation_test.go new file mode 100644 index 00000000..03871123 --- /dev/null +++ b/app/internal/common/attachments_validation_test.go @@ -0,0 +1,64 @@ +package common + +import ( + "strings" + "testing" + + "github.com/google/uuid" +) + +func TestValidateFilename(t *testing.T) { + valid := []string{"photo.png", "фото 1.jpeg", "a", strings.Repeat("a", 255)} + for _, name := range valid { + if err := ValidateFilename(name); err != nil { + t.Errorf("ValidateFilename(%q) = %v, want nil", name, err) + } + } + + invalid := []string{ + "", " ", ".", "..", "dir/photo.png", `dir\photo.png`, + "nul\x00byte", "line\nbreak", strings.Repeat("a", 256), + } + for _, name := range invalid { + if err := ValidateFilename(name); err == nil { + t.Errorf("ValidateFilename(%q) = nil, want error", name) + } + } +} + +func TestValidateAttachmentIDs(t *testing.T) { + ids := make([]uuid.UUID, 10) + for index := range ids { + ids[index] = uuid.New() + } + if err := ValidateAttachmentIDs(ids); err != nil { + t.Fatalf("ten unique ids rejected: %v", err) + } + if err := ValidateAttachmentIDs(append(ids, uuid.New())); err == nil { + t.Fatal("eleven ids accepted") + } + if err := ValidateAttachmentIDs([]uuid.UUID{ids[0], ids[0]}); err == nil { + t.Fatal("duplicate ids accepted") + } + if err := ValidateAttachmentIDs(nil); err != nil { + t.Fatalf("empty list rejected: %v", err) + } +} + +func TestValidateMessageText(t *testing.T) { + if err := ValidateMessageText("", 1); err != nil { + t.Fatalf("empty text with attachment rejected: %v", err) + } + if err := ValidateMessageText(" ", 1); err != nil { + t.Fatalf("blank text with attachment rejected: %v", err) + } + if err := ValidateMessageText("", 0); err == nil { + t.Fatal("empty text without attachment accepted") + } + if err := ValidateMessageText(strings.Repeat("ж", 4000), 0); err != nil { + t.Fatalf("4000 code points rejected: %v", err) + } + if err := ValidateMessageText(strings.Repeat("ж", 4001), 1); err == nil { + t.Fatal("4001 code points accepted") + } +} diff --git a/app/internal/common/binding.go b/app/internal/common/binding.go new file mode 100644 index 00000000..bba6208a --- /dev/null +++ b/app/internal/common/binding.go @@ -0,0 +1,63 @@ +package common + +import ( + "bytes" + "encoding/json" + "io" + "strings" + + "github.com/gofiber/fiber/v3" +) + +// ReadBodyLimited reads at most limit bytes from the request body. The caller +// must use it instead of c.Body() when the endpoint has a smaller budget than +// the process-wide Fiber.BodyLimit. The limit itself belongs to the caller +// (config.MaxJSONBodyBytes for JSON endpoints). +// +// with StreamRequestBody enabled fasthttp exposes the network body as a +// stream, so io.ReadAll over a LimitReader allocates only the bytes the +// endpoint actually accepts instead of the whole upload limit. +func ReadBodyLimited(c fiber.Ctx, limit int64) ([]byte, error) { + var reader io.Reader = c.Request().BodyStream() + if reader == nil { + reader = bytes.NewReader(c.Body()) + } + + data, err := io.ReadAll(io.LimitReader(reader, limit+1)) + if err != nil { + return nil, BadRequest("incomplete request body") + } + if int64(len(data)) > limit { + return nil, PayloadTooLarge("request body is too large") + } + + return data, nil +} + +func BindJSON(c fiber.Ctx, dst any, limit int64) error { + if contentType := c.Get(fiber.HeaderContentType); !strings.HasPrefix(contentType, "application/json") { + return UnsupportedMedia("invalid JSON body") + } + + body, err := ReadBodyLimited(c, limit) + if err != nil { + return err + } + if len(body) == 0 { + return BadRequest("request body is empty") + } + + decoder := json.NewDecoder(bytes.NewReader(body)) + decoder.DisallowUnknownFields() + + if err := decoder.Decode(dst); err != nil { + return BadRequest("invalid JSON body") + } + + var extra json.RawMessage + if err := decoder.Decode(&extra); err != io.EOF { + return BadRequest("request body has extra trailing bytes") + } + + return nil +} diff --git a/app/internal/common/error.go b/app/internal/common/error.go new file mode 100644 index 00000000..899dde66 --- /dev/null +++ b/app/internal/common/error.go @@ -0,0 +1,142 @@ +// Package common contains cross-cutting HTTP helpers: contract errors, JSON +// binding, validation, cursor pagination and timestamp formatting. +package common + +import ( + "errors" + "net/http" + + "github.com/gofiber/fiber/v3" + "github.com/gofiber/fiber/v3/middleware/requestid" +) + +const ( + ErrorCodeInvalidRequest = "invalid_request" + ErrorCodeUnauthorized = "unauthorized" + ErrorCodeNotFound = "not_found" + ErrorCodeConflict = "conflict" + ErrorCodePayloadTooLarge = "payload_too_large" + ErrorCodeUnsupportedMedia = "unsupported_media_type" + ErrorCodeRateLimited = "rate_limited" + ErrorCodeUnavailable = "unavailable" + ErrorCodeInternal = "internal_error" +) + +type APIErrorResponse struct { + Error APIError `json:"error"` +} + +type APIError struct { + Code string `json:"code"` + Message string `json:"message"` + RequestID string `json:"request_id"` +} + +func NewError(code, message, requestID string) APIErrorResponse { + return APIErrorResponse{ + Error: APIError{ + Code: code, + Message: message, + RequestID: requestID, + }, + } +} + +// AppError is the single contract error type. Handlers and the store return it; +// the Fiber error handler is the only place that turns it into a response. +type AppError struct { + Status int + Code string + Message string + Challenge string // WWW-Authenticate value, set for 401 responses +} + +func (e *AppError) Error() string { + return e.Message +} + +func NewAppError(status int, code, message string) *AppError { + return &AppError{ + Status: status, + Code: code, + Message: message, + } +} + +func BadRequest(message string) *AppError { + return NewAppError(http.StatusBadRequest, ErrorCodeInvalidRequest, message) +} + +func NotFound(message string) *AppError { + return NewAppError(http.StatusNotFound, ErrorCodeNotFound, message) +} + +func Conflict(message string) *AppError { + return NewAppError(http.StatusConflict, ErrorCodeConflict, message) +} + +func UnsupportedMedia(message string) *AppError { + return NewAppError(http.StatusUnsupportedMediaType, ErrorCodeUnsupportedMedia, message) +} + +func PayloadTooLarge(message string) *AppError { + return NewAppError(http.StatusRequestEntityTooLarge, ErrorCodePayloadTooLarge, message) +} + +func Internal() *AppError { + return NewAppError(http.StatusInternalServerError, ErrorCodeInternal, "Internal server error") +} + +func Unavailable(message string) *AppError { + return NewAppError(http.StatusServiceUnavailable, ErrorCodeUnavailable, message) +} + +// NewChallenge builds the 401 that carries a WWW-Authenticate header. +func NewChallenge(challenge, message string) *AppError { + return &AppError{ + Status: http.StatusUnauthorized, + Code: ErrorCodeUnauthorized, + Message: message, + Challenge: challenge, + } +} + +// IsCode reports whether err is an AppError with the given contract code. +func IsCode(err error, code string) bool { + appErr, ok := errors.AsType[*AppError](err) + return ok && appErr.Code == code +} + +func ErrorCodeFromStatus(status int) string { + switch status { + case http.StatusBadRequest: + return ErrorCodeInvalidRequest + case http.StatusUnauthorized: + return ErrorCodeUnauthorized + case http.StatusNotFound: + return ErrorCodeNotFound + case http.StatusConflict: + return ErrorCodeConflict + case http.StatusRequestEntityTooLarge: + return ErrorCodePayloadTooLarge + case http.StatusUnsupportedMediaType: + return ErrorCodeUnsupportedMedia + case http.StatusTooManyRequests: + return ErrorCodeRateLimited + case http.StatusServiceUnavailable: + return ErrorCodeUnavailable + default: + return ErrorCodeInternal + } +} + +func getRequestID(c fiber.Ctx) string { + if id := string(c.Response().Header.Peek("X-Request-Id")); id != "" { + return id + } + return requestid.FromContext(c) +} + +func SendError(c fiber.Ctx, status int, code, message string) error { + return c.Status(status).JSON(NewError(code, message, getRequestID(c))) +} diff --git a/app/internal/common/idempotency_key_test.go b/app/internal/common/idempotency_key_test.go new file mode 100644 index 00000000..7278f23f --- /dev/null +++ b/app/internal/common/idempotency_key_test.go @@ -0,0 +1,30 @@ +package common + +import ( + "strings" + "testing" +) + +func TestValidateIdempotencyKey(t *testing.T) { + tests := []struct { + name string + key string + ok bool + }{ + {name: "hex", key: "4f8c2d1e", ok: true}, + {name: "allowed punctuation", key: "load-1_a.b:c", ok: true}, + {name: "empty", key: "", ok: false}, + {name: "space", key: "two words", ok: false}, + {name: "slash", key: "a/b", ok: false}, + {name: "too long", key: strings.Repeat("a", 129), ok: false}, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + got := ValidateIdempotencyKey(test.key) == nil + if got != test.ok { + t.Fatalf("ValidateIdempotencyKey(%q) valid = %v, want %v", test.key, got, test.ok) + } + }) + } +} diff --git a/app/internal/common/locals.go b/app/internal/common/locals.go new file mode 100644 index 00000000..33e7c4ed --- /dev/null +++ b/app/internal/common/locals.go @@ -0,0 +1,39 @@ +package common + +import ( + "github.com/gofiber/fiber/v3" + "github.com/google/uuid" +) + +const ( + KeyUserID = "userID" + KeySessionToken = "sessionToken" +) + +func UserID(c fiber.Ctx) (uuid.UUID, bool) { + userID, ok := c.Locals(KeyUserID).(uuid.UUID) + return userID, ok +} + +func MustUserID(c fiber.Ctx) uuid.UUID { + userID, ok := UserID(c) + if !ok { + panic("common: authenticated user ID is missing from the request context") + } + return userID +} + +func SessionToken(c fiber.Ctx) (string, bool) { + token, ok := c.Locals(KeySessionToken).(string) + return token, ok && token != "" +} + +// PathUUID parses a UUID path parameter. A malformed value is a contract +// invalid_request, not a lookup failure. +func PathUUID(c fiber.Ctx, name string) (uuid.UUID, error) { + id, err := uuid.Parse(c.Params(name)) + if err != nil { + return uuid.Nil, BadRequest("invalid " + name + " format") + } + return id, nil +} diff --git a/app/internal/common/paginator.go b/app/internal/common/paginator.go new file mode 100644 index 00000000..5c131742 --- /dev/null +++ b/app/internal/common/paginator.go @@ -0,0 +1,102 @@ +package common + +import ( + "encoding/base64" + "strconv" + "strings" + "time" + + "github.com/gofiber/fiber/v3" + "github.com/google/uuid" +) + +type CursorKeyCtx struct { + Time time.Time + ID uuid.UUID + ContextID uuid.UUID +} + +type Page[T any] struct { + Items []T `json:"items"` + NextCursor *string `json:"next_cursor"` +} + +func ParseLimit(c fiber.Ctx, defaultLimit int) (int, error) { + limitStr, exists := c.Queries()["limit"] + if !exists { + return defaultLimit, nil + } + if strings.TrimSpace(limitStr) == "" { + return 0, BadRequest("limit param declared but empty") + } + + limit, err := strconv.Atoi(limitStr) + if err != nil || limit < 1 || limit > 100 { + return 0, BadRequest("limit must be an integer between 1 and 100") + } + return limit, nil +} + +func ParseCursor(c fiber.Ctx) (*CursorKeyCtx, error) { + cursorStr, exists := c.Queries()["cursor"] + if !exists { + return nil, nil + } + if strings.TrimSpace(cursorStr) == "" { + return nil, BadRequest("cursor param declared but empty") + } + + cursor, ok := DecodeCursor(cursorStr) + if !ok { + return nil, BadRequest("invalid cursor format") + } + return cursor, nil +} + +func EncodeCursor(key CursorKeyCtx) string { + raw := key.Time.UTC().Format(time.RFC3339Nano) + "|" + key.ID.String() + "|" + key.ContextID.String() + return base64.RawURLEncoding.EncodeToString([]byte(raw)) +} + +func DecodeCursor(cursor string) (*CursorKeyCtx, bool) { + data, err := base64.RawURLEncoding.DecodeString(cursor) + if err != nil { + return nil, false + } + + parts := strings.SplitN(string(data), "|", 3) + if len(parts) != 3 { + return nil, false + } + + parsedTime, err := time.Parse(time.RFC3339Nano, parts[0]) + if err != nil { + return nil, false + } + parsedID, err := uuid.Parse(parts[1]) + if err != nil { + return nil, false + } + parsedContext, err := uuid.Parse(parts[2]) + if err != nil { + return nil, false + } + + return &CursorKeyCtx{Time: parsedTime, ID: parsedID, ContextID: parsedContext}, true +} + +func NextCursor[T interface { + GetID() uuid.UUID + GetCreatedAt() time.Time +}](items []T, hasMore bool, contextID uuid.UUID) *string { + if !hasMore || len(items) == 0 { + return nil + } + last := items[len(items)-1] + encoded := EncodeCursor(CursorKeyCtx{ + Time: last.GetCreatedAt(), + ID: last.GetID(), + ContextID: contextID, + }) + return &encoded +} diff --git a/app/internal/common/paginator_test.go b/app/internal/common/paginator_test.go new file mode 100644 index 00000000..18a97007 --- /dev/null +++ b/app/internal/common/paginator_test.go @@ -0,0 +1,31 @@ +package common + +import ( + "testing" + "time" + + "github.com/google/uuid" +) + +func TestCursorRoundTrip(t *testing.T) { + want := CursorKeyCtx{ + Time: time.Date(2026, 10, 20, 12, 30, 45, 123456789, time.UTC), + ID: uuid.MustParse("11111111-1111-4111-8111-111111111111"), + ContextID: uuid.MustParse("22222222-2222-4222-8222-222222222222"), + } + + encoded := EncodeCursor(want) + got, ok := DecodeCursor(encoded) + if !ok { + t.Fatal("cursor did not decode") + } + if !got.Time.Equal(want.Time) || got.ID != want.ID || got.ContextID != want.ContextID { + t.Fatalf("cursor round trip changed the key: %+v", got) + } + + for _, invalid := range []string{"not-base64!", "", "aGVsbG8"} { + if _, ok := DecodeCursor(invalid); ok { + t.Fatalf("invalid cursor %q was accepted", invalid) + } + } +} diff --git a/app/internal/common/password_test.go b/app/internal/common/password_test.go new file mode 100644 index 00000000..4a0a5953 --- /dev/null +++ b/app/internal/common/password_test.go @@ -0,0 +1,21 @@ +package common + +import "testing" + +func TestValidatePasswordRange(t *testing.T) { + cases := []struct { + password string + valid bool + }{ + {"short", false}, + {"12345678901", false}, + {"123456789012", true}, + {"пароль-из-двенадцати", true}, + {"😀", false}, + } + for _, tc := range cases { + if got := ValidatePassword(tc.password) == nil; got != tc.valid { + t.Fatalf("ValidatePassword(%q) valid=%v, want %v", tc.password, got, tc.valid) + } + } +} diff --git a/app/internal/common/time.go b/app/internal/common/time.go new file mode 100644 index 00000000..a6a4362a --- /dev/null +++ b/app/internal/common/time.go @@ -0,0 +1,27 @@ +package common + +import ( + "encoding/json" + "time" +) + +// PostgreSQL stores microseconds, so keeping the same boundary in Go +// prevents a create response from showing more precision +// than a later read can reproduce. +const storageTimePrecision = time.Microsecond + +type RFCFormTime time.Time + +func (t RFCFormTime) MarshalJSON() ([]byte, error) { + // RFC 3339 permits an optional fractional second. + utc := time.Time(t).UTC().Truncate(storageTimePrecision) + layout := "2006-01-02T15:04:05Z" + if utc.Nanosecond() != 0 { + layout = "2006-01-02T15:04:05.000000Z" + } + return json.Marshal(utc.Format(layout)) +} + +func NowUTC() RFCFormTime { + return RFCFormTime(time.Now().UTC().Truncate(storageTimePrecision)) +} diff --git a/app/internal/common/time_test.go b/app/internal/common/time_test.go new file mode 100644 index 00000000..a177d48a --- /dev/null +++ b/app/internal/common/time_test.go @@ -0,0 +1,48 @@ +package common + +import ( + "encoding/json" + "testing" + "time" +) + +func TestNowUTCHasMicrosecondPrecision(t *testing.T) { + now := time.Time(NowUTC()) + if now.Nanosecond()%1000 != 0 { + t.Fatalf("NowUTC() kept precision below a microsecond: %v", now) + } + if now.Location() != time.UTC { + t.Fatalf("NowUTC() location = %v, want UTC", now.Location()) + } +} + +func TestRFCFormTimeSerializesFractionalSeconds(t *testing.T) { + cases := []struct { + name string + value time.Time + want string + }{ + { + name: "microseconds", + value: time.Date(2026, 10, 26, 12, 34, 56, 123456789, time.UTC), + want: `"2026-10-26T12:34:56.123456Z"`, + }, + { + name: "exact second", + value: time.Date(2026, 10, 26, 12, 34, 56, 0, time.UTC), + want: `"2026-10-26T12:34:56Z"`, + }, + } + + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + encoded, err := json.Marshal(RFCFormTime(tc.value)) + if err != nil { + t.Fatalf("marshal RFC form time: %v", err) + } + if string(encoded) != tc.want { + t.Fatalf("RFC form time = %s, want %s", encoded, tc.want) + } + }) + } +} diff --git a/app/internal/common/validation.go b/app/internal/common/validation.go new file mode 100644 index 00000000..4cf9ab4b --- /dev/null +++ b/app/internal/common/validation.go @@ -0,0 +1,149 @@ +package common + +import ( + "regexp" + "strings" + "unicode" + "unicode/utf8" + + "github.com/google/uuid" +) + +var ( + usernameRegex = regexp.MustCompile(`^[a-z0-9_]{3,32}$`) + idempotencyKeyRegex = regexp.MustCompile(`^[A-Za-z0-9._:-]{1,128}$`) +) + +func blank(s string) bool { + return strings.TrimSpace(s) == "" +} + +func strLenBetween(s string, min, max int) bool { + n := utf8.RuneCountInString(s) + return n >= min && n <= max +} + +func ValidateCreateUser(username, displayName string) error { + if !usernameRegex.MatchString(username) { + return BadRequest("username must match ^[a-z0-9_]{3,32}$") + } + + if blank(displayName) { + return BadRequest("display_name must not be empty or only whitespace") + } + + if !strLenBetween(displayName, 1, 100) { + return BadRequest("display_name must be between 1 and 100 Unicode code points") + } + + return nil +} + +func ValidatePassword(password string) error { + if blank(password) { + return BadRequest("password must not be empty or only whitespace") + } + + if !strLenBetween(password, 12, 128) { + return BadRequest("password must be between 12 and 128 Unicode code points") + } + + return nil +} + +func ValidateChatTitle(title string) error { + if blank(title) { + return BadRequest("title must not be empty or only whitespace") + } + + if !strLenBetween(title, 1, 100) { + return BadRequest("title must be between 1 and 100 Unicode code points") + } + + return nil +} + +func ValidateMessageText(text string, attachmentCount int) error { + if attachmentCount == 0 && blank(text) { + return BadRequest("text must not be empty or only whitespace") + } + + if !strLenBetween(text, 0, 4000) { + return BadRequest("text must be between 0 and 4000 Unicode code points") + } + + return nil +} + +func ValidateAttachmentIDs(ids []uuid.UUID) error { + if len(ids) > 10 { + return BadRequest("attachment_ids must contain at most 10 items") + } + + seen := make(map[uuid.UUID]bool, len(ids)) + for _, id := range ids { + if seen[id] { + return BadRequest("attachment_ids must not contain duplicates") + } + seen[id] = true + } + + return nil +} + +func ValidateFilename(filename string) error { + if !strLenBetween(filename, 1, 255) { + return BadRequest("filename must be between 1 and 255 Unicode code points") + } + if blank(filename) { + return BadRequest("filename must not be empty or only whitespace") + } + if filename == "." || filename == ".." { + return BadRequest("filename is not allowed") + } + + for _, r := range filename { + if r == '/' || r == '\\' || r == 0 || unicode.IsControl(r) { + return BadRequest("filename contains a forbidden character") + } + } + + return nil +} + +func ValidateIdempotencyKey(key string) error { + if key == "" { + return BadRequest("Idempotency-Key header is required") + } + if !idempotencyKeyRegex.MatchString(key) { + return BadRequest("Idempotency-Key must match ^[A-Za-z0-9._:-]{1,128}$") + } + return nil +} + +func ValidateMemberIDs(ids []uuid.UUID, callerID uuid.UUID) error { + if len(ids) < 1 || len(ids) > 100 { + return BadRequest("member_ids must contain between 1 and 100 members") + } + + seen := make(map[uuid.UUID]bool, len(ids)) + callerFound := false + + for _, id := range ids { + if seen[id] { + return BadRequest("member_ids must not contain duplicates") + } + + seen[id] = true + + if id == callerID { + callerFound = true + } + } + + if !callerFound { + return BadRequest("caller must be included in member_ids") + } + + return nil +} diff --git a/app/internal/config/config.go b/app/internal/config/config.go new file mode 100644 index 00000000..7b4aa123 --- /dev/null +++ b/app/internal/config/config.go @@ -0,0 +1,283 @@ +// Package config loads and validates process configuration from environment +// variables and optional *_FILE secrets. +package config + +import ( + "fmt" + "os" + "path/filepath" + "strconv" + "strings" + "time" +) + +const ( + // Configuration may make a limit stricter, never looser. + MaxUploadBytesHardCap = 10 * 1024 * 1024 + MaxImagePixelsHardCap = 40_000_000 + + // MaxJSONBodyBytes is the body budget of every JSON endpoint. It is also + // the slack between the global HTTP body limit and the upload limit, so a + // smaller MAX_UPLOAD_BYTES cannot strangle JSON requests. + MaxJSONBodyBytes = 1 * 1024 * 1024 +) + +type Config struct { + Host string + Port string + InstanceID string + SessionTTL time.Duration + IdempotencyTTL time.Duration + DatabaseURL string + CacheURL string + DBMaxConns int32 + DBTimeout time.Duration + RedisTimeout time.Duration + StartupWait time.Duration + RunMigrations bool + MigrateOnly bool + + S3Endpoint string + S3Region string + S3Bucket string + S3AccessKey string + S3SecretKey string + UploadTimeout time.Duration + DownloadTimeout time.Duration + + MaxUploadBytes int64 + MaxImagePixels int64 + ImageDecodeBytes int64 + UploadTempDir string + + // HTTP timeouts and body limits are derived once in Load so that app.go + // and the handlers never invent values of their own. + ReadTimeout time.Duration + WriteTimeout time.Duration + IdleTimeout time.Duration + UploadDrainLimit int64 + HTTPBodyLimit int + + ReconcileGrace time.Duration + ReconcileInterval time.Duration +} + +func Load() (Config, error) { + instance, err := instanceID() + if err != nil { + return Config{}, err + } + + sessionTTL, err := envInt("SESSION_TTL_SECONDS", 3600, 1, 0) + if err != nil { + return Config{}, err + } + idempotencyTTL, err := envInt("IDEMPOTENCY_TTL_SECONDS", 86400, 1, 0) + if err != nil { + return Config{}, err + } + maxConns, err := envInt("DB_POOL_MAX_CONNS", 10, 1, 100) + if err != nil { + return Config{}, err + } + + maxUpload, err := envInt[int64]("MAX_UPLOAD_BYTES", MaxUploadBytesHardCap, 1, MaxUploadBytesHardCap) + if err != nil { + return Config{}, err + } + maxPixels, err := envInt[int64]("MAX_IMAGE_PIXELS", MaxImagePixelsHardCap, 1, MaxImagePixelsHardCap) + if err != nil { + return Config{}, err + } + decodeMB, err := envInt("IMAGE_DECODE_MEMORY_MB", 256, 32, 4096) + if err != nil { + return Config{}, err + } + + cfg := Config{ + Host: envOr("APP_HOST", "0.0.0.0"), + Port: envOr("APP_PORT", "8080"), + InstanceID: instance, + SessionTTL: time.Duration(sessionTTL) * time.Second, + IdempotencyTTL: time.Duration(idempotencyTTL) * time.Second, + DBMaxConns: int32(maxConns), + DBTimeout: 5 * time.Second, + RedisTimeout: 2 * time.Second, + StartupWait: 30 * time.Second, + S3Region: envOr("S3_REGION", "us-east-1"), + S3Bucket: envOr("S3_BUCKET", "messenger-attachments"), + ReadTimeout: 30 * time.Second, + WriteTimeout: 60 * time.Second, + IdleTimeout: 60 * time.Second, + UploadTimeout: 30 * time.Second, + DownloadTimeout: 60 * time.Second, + MaxUploadBytes: maxUpload, + MaxImagePixels: maxPixels, + ImageDecodeBytes: int64(decodeMB) * 1024 * 1024, + UploadTempDir: envOr("UPLOAD_TEMP_DIR", filepath.Join(os.TempDir(), "messenger-uploads")), + } + cfg.deriveHTTPLimits() + + if cfg.RunMigrations, err = envBool("RUN_MIGRATIONS", true); err != nil { + return Config{}, err + } + if cfg.MigrateOnly, err = envBool("MIGRATE_ONLY", false); err != nil { + return Config{}, err + } + reconcileOnly, err := envBool("RECONCILE_ONLY", false) + if err != nil { + return Config{}, err + } + + reconcileGrace, err := envInt("RECONCILE_GRACE_SECONDS", 900, 60, 0) + if err != nil { + return Config{}, err + } + cfg.ReconcileGrace = time.Duration(reconcileGrace) * time.Second + + reconcileInterval, err := envInt("RECONCILE_INTERVAL_SECONDS", 300, 10, 0) + if err != nil { + return Config{}, err + } + cfg.ReconcileInterval = time.Duration(reconcileInterval) * time.Second + + if cfg.DatabaseURL, err = secret("DATABASE_URL"); err != nil { + return Config{}, err + } + if cfg.DatabaseURL == "" { + return Config{}, fmt.Errorf("DATABASE_URL or DATABASE_URL_FILE is required") + } + + if cfg.MigrateOnly { + return cfg, nil + } + + if cfg.S3Endpoint, err = secret("S3_ENDPOINT"); err != nil { + return Config{}, err + } + if cfg.S3Endpoint == "" { + return Config{}, fmt.Errorf("S3_ENDPOINT or S3_ENDPOINT_FILE is required") + } + if cfg.S3AccessKey, err = secret("S3_ACCESS_KEY_ID"); err != nil { + return Config{}, err + } + if cfg.S3AccessKey == "" { + return Config{}, fmt.Errorf("S3_ACCESS_KEY_ID or S3_ACCESS_KEY_ID_FILE is required") + } + if cfg.S3SecretKey, err = secret("S3_SECRET_ACCESS_KEY"); err != nil { + return Config{}, err + } + if cfg.S3SecretKey == "" { + return Config{}, fmt.Errorf("S3_SECRET_ACCESS_KEY or S3_SECRET_ACCESS_KEY_FILE is required") + } + + if reconcileOnly { + return cfg, nil + } + + if cfg.CacheURL, err = secret("CACHE_URL"); err != nil { + return Config{}, err + } + if cfg.CacheURL == "" { + if cfg.CacheURL, err = secret("REDIS_URL"); err != nil { + return Config{}, err + } + } + if cfg.CacheURL == "" { + return Config{}, fmt.Errorf("CACHE_URL or CACHE_URL_FILE is required") + } + + if value := strings.ToLower(envOr("SESSION_STORE", "redis")); value != "redis" { + return Config{}, fmt.Errorf("SESSION_STORE must be redis (got %q)", value) + } + + return cfg, nil +} + +// deriveHTTPLimits is the single place that relates the three HTTP budgets: +// the transport gate must admit both a full upload and a full JSON body, and it +// must leave room for the handler to drain an oversized declared body so the +// client receives a 413 instead of a connection reset. +func (c *Config) deriveHTTPLimits() { + c.UploadDrainLimit = c.MaxUploadBytes + MaxJSONBodyBytes + c.HTTPBodyLimit = int(c.UploadDrainLimit) +} + +func instanceID() (string, error) { + if value := strings.TrimSpace(os.Getenv("INSTANCE_ID")); value != "" { + return value, nil + } + name, err := os.Hostname() + if err != nil { + return "", fmt.Errorf("determine INSTANCE_ID: %w", err) + } + if name = strings.TrimSpace(name); name == "" { + return "", fmt.Errorf("determine INSTANCE_ID: hostname is empty and INSTANCE_ID is not set") + } + return name, nil +} + +func envBool(name string, fallback bool) (bool, error) { + value := os.Getenv(name) + if value == "" { + return fallback, nil + } + parsed, err := strconv.ParseBool(value) + if err != nil { + return false, fmt.Errorf("%s must be true or false", name) + } + return parsed, nil +} + +// envInt parses an integer setting into int or int64; max <= 0 means "no upper +// bound". Both widths are needed by Config, and one helper keeps validation +// identical for every numeric variable. +func envInt[T ~int | ~int64](name string, fallback, min, max T) (T, error) { + value := os.Getenv(name) + if value == "" { + return fallback, nil + } + + parsed, err := strconv.ParseInt(value, 10, 64) + if err != nil || parsed < int64(min) || (max > 0 && parsed > int64(max)) { + if max > 0 { + return 0, fmt.Errorf("%s must be an integer between %d and %d", name, min, max) + } + return 0, fmt.Errorf("%s must be an integer >= %d", name, min) + } + result := T(parsed) + if int64(result) != parsed { + return 0, fmt.Errorf("%s is out of range", name) + } + return result, nil +} + +func envOr(name, fallback string) string { + if value := os.Getenv(name); value != "" { + return value + } + return fallback +} + +func secret(name string) (string, error) { + if value := os.Getenv(name); value != "" { + return value, nil + } + + path := os.Getenv(name + "_FILE") + if path == "" { + return "", nil + } + + data, err := os.ReadFile(path) + if err != nil { + return "", fmt.Errorf("read %s_FILE: %w", name, err) + } + + value := strings.TrimSpace(string(data)) + if value == "" { + return "", fmt.Errorf("%s_FILE is empty", name) + } + + return value, nil +} diff --git a/app/internal/config/config_test.go b/app/internal/config/config_test.go new file mode 100644 index 00000000..b56546be --- /dev/null +++ b/app/internal/config/config_test.go @@ -0,0 +1,64 @@ +package config + +import "testing" + +func TestInstanceIDUsesConfiguredValue(t *testing.T) { + t.Setenv("INSTANCE_ID", " api-1 ") + + got, err := instanceID() + if err != nil { + t.Fatalf("instanceID: %v", err) + } + if got != "api-1" { + t.Fatalf("instanceID = %q, want api-1", got) + } +} + +func TestInstanceIDFallsBackToHostname(t *testing.T) { + t.Setenv("INSTANCE_ID", " ") + + got, err := instanceID() + if err != nil { + t.Fatalf("instanceID: %v", err) + } + if got == "" { + t.Fatal("instanceID returned an empty value") + } +} + +func TestDerivedHTTPLimits(t *testing.T) { + cfg := Config{MaxUploadBytes: 2 * 1024 * 1024} + cfg.deriveHTTPLimits() + + if cfg.UploadDrainLimit != cfg.MaxUploadBytes+MaxJSONBodyBytes { + t.Fatalf("drain limit = %d, want upload + JSON body", cfg.UploadDrainLimit) + } + if cfg.HTTPBodyLimit != int(cfg.UploadDrainLimit) { + t.Fatalf("body limit = %d, want %d", cfg.HTTPBodyLimit, cfg.UploadDrainLimit) + } + if cfg.HTTPBodyLimit-int(cfg.MaxUploadBytes) != MaxJSONBodyBytes { + t.Fatal("transport gate would strangle a full JSON body") + } +} + +func TestEnvIntHandlesBothWidths(t *testing.T) { + t.Setenv("TEST_ENV_INT", "42") + + small, err := envInt("TEST_ENV_INT", 1, 1, 100) + if err != nil || small != 42 { + t.Fatalf("envInt = %d, %v; want 42, nil", small, err) + } + large, err := envInt[int64]("TEST_ENV_INT", 1, 1, 100) + if err != nil || large != 42 { + t.Fatalf("envInt[int64] = %d, %v; want 42, nil", large, err) + } +} + +func TestEnvIntRejectsOutOfRange(t *testing.T) { + for _, value := range []string{"0", "101", "not-a-number"} { + t.Setenv("TEST_ENV_INT", value) + if _, err := envInt("TEST_ENV_INT", 1, 1, 100); err == nil { + t.Fatalf("envInt accepted %q", value) + } + } +} diff --git a/app/internal/database/database.go b/app/internal/database/database.go new file mode 100644 index 00000000..86c91911 --- /dev/null +++ b/app/internal/database/database.go @@ -0,0 +1,44 @@ +// Package database owns the PostgreSQL connection pool and schema migrations. +package database + +import ( + "context" + "fmt" + "time" + + "github.com/jackc/pgx/v5/pgxpool" +) + +func Connect(ctx context.Context, url string, maxConns int32, wait time.Duration) (*pgxpool.Pool, error) { + cfg, err := pgxpool.ParseConfig(url) + if err != nil { + return nil, fmt.Errorf("parse DATABASE_URL: %w", err) + } + + cfg.MaxConns = maxConns + cfg.MinConns = 1 + cfg.MaxConnLifetime = 30 * time.Minute + cfg.MaxConnIdleTime = 5 * time.Minute + cfg.HealthCheckPeriod = 30 * time.Second + cfg.ConnConfig.ConnectTimeout = 3 * time.Second + + pool, err := pgxpool.NewWithConfig(ctx, cfg) + if err != nil { + return nil, fmt.Errorf("create pool: %w", err) + } + + deadline := time.Now().Add(wait) + for { + pingCtx, cancel := context.WithTimeout(ctx, 2*time.Second) + err = pool.Ping(pingCtx) + cancel() + if err == nil { + return pool, nil + } + if time.Now().After(deadline) || ctx.Err() != nil { + pool.Close() + return nil, fmt.Errorf("postgres is not reachable: %w", err) + } + time.Sleep(500 * time.Millisecond) + } +} diff --git a/app/internal/database/migrations.go b/app/internal/database/migrations.go new file mode 100644 index 00000000..cf78d61d --- /dev/null +++ b/app/internal/database/migrations.go @@ -0,0 +1,71 @@ +package database + +import ( + "embed" + "errors" + "fmt" + "time" + + "github.com/golang-migrate/migrate/v4" + pgxmigrate "github.com/golang-migrate/migrate/v4/database/pgx/v5" + "github.com/golang-migrate/migrate/v4/source/iofs" + "github.com/jackc/pgx/v5" + "github.com/jackc/pgx/v5/stdlib" +) + +//go:embed migrations/*.sql +var migrationFiles embed.FS + +const migrationsTable = "schema_migrations" + +// Migrate applies every pending embedded migration. golang-migrate takes its +// own advisory lock, so concurrent processes serialise instead of racing the +// schema. +func Migrate(databaseURL string) error { + source, err := iofs.New(migrationFiles, "migrations") + if err != nil { + return fmt.Errorf("load embedded migrations: %w", err) + } + + connConfig, err := pgx.ParseConfig(databaseURL) + if err != nil { + _ = source.Close() + return fmt.Errorf("parse DATABASE_URL for migrations: %w", err) + } + if connConfig.RuntimeParams == nil { + connConfig.RuntimeParams = make(map[string]string) + } + connConfig.RuntimeParams["lock_timeout"] = "30000" + connConfig.ConnectTimeout = 3 * time.Second + + db := stdlib.OpenDB(*connConfig) + db.SetMaxOpenConns(1) + db.SetMaxIdleConns(1) + + driver, err := pgxmigrate.WithInstance(db, &pgxmigrate.Config{ + MigrationsTable: migrationsTable, + StatementTimeout: 30 * time.Second, + }) + if err != nil { + _ = source.Close() + _ = db.Close() + return fmt.Errorf("create migration driver: %w", err) + } + + migrator, err := migrate.NewWithInstance("iofs", source, "pgx5", driver) + if err != nil { + _ = source.Close() + _ = driver.Close() + return fmt.Errorf("create migrator: %w", err) + } + defer func() { + // Close closes both the embedded source and the database connection. + _, _ = migrator.Close() + }() + + if err := migrator.Up(); err != nil && !errors.Is(err, migrate.ErrNoChange) { + return fmt.Errorf("apply migrations: %w", err) + } + + return nil +} diff --git a/app/internal/database/migrations/0001_init.down.sql b/app/internal/database/migrations/0001_init.down.sql new file mode 100644 index 00000000..1ba630fa --- /dev/null +++ b/app/internal/database/migrations/0001_init.down.sql @@ -0,0 +1,4 @@ +DROP TABLE IF EXISTS messages; +DROP TABLE IF EXISTS chat_members; +DROP TABLE IF EXISTS chats; +DROP TABLE IF EXISTS users; diff --git a/app/internal/database/migrations/0001_init.up.sql b/app/internal/database/migrations/0001_init.up.sql new file mode 100644 index 00000000..cfa24e76 --- /dev/null +++ b/app/internal/database/migrations/0001_init.up.sql @@ -0,0 +1,38 @@ +CREATE TABLE IF NOT EXISTS users ( + id uuid PRIMARY KEY, + username text NOT NULL, + display_name text NOT NULL, + password_hash text NOT NULL, + created_at timestamptz NOT NULL, + CONSTRAINT users_username_key UNIQUE (username), + CONSTRAINT users_username_format CHECK (username ~ '^[a-z0-9_]{3,32}$'), + CONSTRAINT users_display_name_len CHECK (char_length(display_name) BETWEEN 1 AND 100) +); + +CREATE TABLE IF NOT EXISTS chats ( + id uuid PRIMARY KEY, + title text NOT NULL, + created_at timestamptz NOT NULL, + CONSTRAINT chats_title_len CHECK (char_length(title) BETWEEN 1 AND 100) +); + +CREATE TABLE IF NOT EXISTS chat_members ( + chat_id uuid NOT NULL REFERENCES chats(id) ON DELETE CASCADE, + user_id uuid NOT NULL REFERENCES users(id) ON DELETE RESTRICT, + PRIMARY KEY (chat_id, user_id) +); + +CREATE INDEX IF NOT EXISTS chat_members_user_idx + ON chat_members (user_id, chat_id); + +CREATE TABLE IF NOT EXISTS messages ( + id uuid PRIMARY KEY, + chat_id uuid NOT NULL REFERENCES chats(id) ON DELETE CASCADE, + sender_id uuid NOT NULL REFERENCES users(id) ON DELETE RESTRICT, + text text NOT NULL, + created_at timestamptz NOT NULL, + CONSTRAINT messages_text_len CHECK (char_length(text) BETWEEN 1 AND 4000) +); + +CREATE INDEX IF NOT EXISTS messages_chat_created_id_idx + ON messages (chat_id, created_at, id); diff --git a/app/internal/database/migrations/0002_message_idempotency.down.sql b/app/internal/database/migrations/0002_message_idempotency.down.sql new file mode 100644 index 00000000..21f1ca27 --- /dev/null +++ b/app/internal/database/migrations/0002_message_idempotency.down.sql @@ -0,0 +1 @@ +DROP TABLE IF EXISTS message_idempotency; diff --git a/app/internal/database/migrations/0002_message_idempotency.up.sql b/app/internal/database/migrations/0002_message_idempotency.up.sql new file mode 100644 index 00000000..dae68cef --- /dev/null +++ b/app/internal/database/migrations/0002_message_idempotency.up.sql @@ -0,0 +1,14 @@ +CREATE TABLE IF NOT EXISTS message_idempotency ( + sender_id uuid NOT NULL REFERENCES users(id) ON DELETE CASCADE, + chat_id uuid NOT NULL REFERENCES chats(id) ON DELETE CASCADE, + idempotency_key text NOT NULL, + request_hash bytea NOT NULL, + message_id uuid NOT NULL REFERENCES messages(id) ON DELETE CASCADE, + created_at timestamptz NOT NULL, + expires_at timestamptz NOT NULL, + PRIMARY KEY (sender_id, chat_id, idempotency_key), + CONSTRAINT message_idempotency_key_len CHECK (char_length(idempotency_key) BETWEEN 1 AND 128) +); + +CREATE INDEX IF NOT EXISTS message_idempotency_expires_at_idx + ON message_idempotency (expires_at); diff --git a/app/internal/database/migrations/0003_attachments.down.sql b/app/internal/database/migrations/0003_attachments.down.sql new file mode 100644 index 00000000..76f33cd6 --- /dev/null +++ b/app/internal/database/migrations/0003_attachments.down.sql @@ -0,0 +1,6 @@ +DROP TABLE IF EXISTS message_attachments; +DROP TABLE IF EXISTS attachments; + +ALTER TABLE messages DROP CONSTRAINT IF EXISTS messages_text_len; +ALTER TABLE messages + ADD CONSTRAINT messages_text_len CHECK (char_length(text) BETWEEN 1 AND 4000); diff --git a/app/internal/database/migrations/0003_attachments.up.sql b/app/internal/database/migrations/0003_attachments.up.sql new file mode 100644 index 00000000..badf223d --- /dev/null +++ b/app/internal/database/migrations/0003_attachments.up.sql @@ -0,0 +1,46 @@ +ALTER TABLE messages DROP CONSTRAINT IF EXISTS messages_text_len; +ALTER TABLE messages + ADD CONSTRAINT messages_text_len CHECK (char_length(text) BETWEEN 0 AND 4000); + +CREATE TABLE IF NOT EXISTS attachments ( + id uuid PRIMARY KEY, + chat_id uuid NOT NULL REFERENCES chats(id) ON DELETE CASCADE, + owner_id uuid NOT NULL REFERENCES users(id) ON DELETE RESTRICT, + filename text NOT NULL, + content_type text NOT NULL, + size_bytes bigint NOT NULL, + sha256 bytea NOT NULL, + object_key text NOT NULL, + status text NOT NULL, + error_code text, + created_at timestamptz NOT NULL, + updated_at timestamptz NOT NULL, + CONSTRAINT attachments_filename_len CHECK (char_length(filename) BETWEEN 1 AND 255), + CONSTRAINT attachments_content_type CHECK (content_type IN ('image/png', 'image/jpeg')), + CONSTRAINT attachments_size CHECK (size_bytes BETWEEN 1 AND 10485760), + CONSTRAINT attachments_sha256_len CHECK (octet_length(sha256) = 32), + CONSTRAINT attachments_status CHECK (status IN ('pending', 'ready', 'failed')), + CONSTRAINT attachments_error_state CHECK ( + (status = 'pending' AND error_code IS NULL) OR + (status = 'ready' AND error_code IS NULL) OR + (status = 'failed' AND error_code IS NOT NULL) + ), + CONSTRAINT attachments_object_key_key UNIQUE (object_key) +); + +CREATE INDEX IF NOT EXISTS attachments_chat_created_idx + ON attachments (chat_id, created_at, id); +CREATE INDEX IF NOT EXISTS attachments_status_created_idx + ON attachments (status, created_at); + +CREATE TABLE IF NOT EXISTS message_attachments ( + message_id uuid NOT NULL REFERENCES messages(id) ON DELETE CASCADE, + attachment_id uuid NOT NULL REFERENCES attachments(id) ON DELETE RESTRICT, + position integer NOT NULL, + PRIMARY KEY (message_id, attachment_id), + CONSTRAINT message_attachments_position CHECK (position >= 0), + CONSTRAINT message_attachments_position_key UNIQUE (message_id, position) +); + +CREATE INDEX IF NOT EXISTS message_attachments_attachment_idx + ON message_attachments (attachment_id); diff --git a/app/internal/database/migrations_test.go b/app/internal/database/migrations_test.go new file mode 100644 index 00000000..bd1812b0 --- /dev/null +++ b/app/internal/database/migrations_test.go @@ -0,0 +1,88 @@ +package database + +import ( + "errors" + "io/fs" + "regexp" + "strconv" + "testing" + + "github.com/golang-migrate/migrate/v4/source/iofs" +) + +// latestMigrationVersion is the only value to bump when a migration is added. +// The rest of the test derives filenames and ordering from the directory. +const latestMigrationVersion = 3 + +var migrationFilePattern = regexp.MustCompile(`^(\d{4})_[a-z0-9_]+\.(up|down)\.sql$`) + +func TestEmbeddedMigrationsAreCompleteAndContiguous(t *testing.T) { + entries, err := fs.ReadDir(migrationFiles, "migrations") + if err != nil { + t.Fatalf("read embedded migrations: %v", err) + } + + up := make(map[int]string, latestMigrationVersion) + down := make(map[int]string, latestMigrationVersion) + + for _, entry := range entries { + if entry.IsDir() { + continue + } + name := entry.Name() + match := migrationFilePattern.FindStringSubmatch(name) + if match == nil { + t.Fatalf("migration %q does not match NNNN_name.{up,down}.sql", name) + } + + version, parseErr := strconv.Atoi(match[1]) + if parseErr != nil || version < 1 || version > latestMigrationVersion { + t.Fatalf("migration %q has version outside 1..%d", name, latestMigrationVersion) + } + + files := up + if match[2] == "down" { + files = down + } + if previous, ok := files[version]; ok { + t.Fatalf("version %d has two %s files: %q and %q", version, match[2], previous, name) + } + files[version] = name + } + + for version := 1; version <= latestMigrationVersion; version++ { + if up[version] == "" { + t.Fatalf("version %d is missing an up migration", version) + } + if down[version] == "" { + t.Fatalf("version %d is missing a down migration", version) + } + } + + source, err := iofs.New(migrationFiles, "migrations") + if err != nil { + t.Fatalf("parse migrations with golang-migrate: %v", err) + } + defer func() { _ = source.Close() }() + + version, err := source.First() + if err != nil { + t.Fatalf("read first migration: %v", err) + } + for expected := 1; expected <= latestMigrationVersion; expected++ { + if version != uint(expected) { + t.Fatalf("migration order = %d, want %d", version, expected) + } + next, nextErr := source.Next(version) + if expected == latestMigrationVersion { + if !errors.Is(nextErr, fs.ErrNotExist) { + t.Fatalf("expected %d to be the last migration, got %v", version, nextErr) + } + break + } + if nextErr != nil { + t.Fatalf("read migration after %d: %v", version, nextErr) + } + version = next + } +} diff --git a/app/internal/health/handler.go b/app/internal/health/handler.go new file mode 100644 index 00000000..27c50887 --- /dev/null +++ b/app/internal/health/handler.go @@ -0,0 +1,61 @@ +// Package health exposes liveness and readiness endpoints plus the process +// shutdown flag used by readiness. +package health + +import ( + "sync/atomic" + + "gitflic.ru/project/bulla-max/mtusi2026lab/internal/common" + "gitflic.ru/project/bulla-max/mtusi2026lab/internal/objectstore" + "gitflic.ru/project/bulla-max/mtusi2026lab/internal/session" + "gitflic.ru/project/bulla-max/mtusi2026lab/internal/store" + "github.com/gofiber/fiber/v3" +) + +type Handler struct { + store *store.Store + sessions *session.Store + objects *objectstore.Client +} + +func NewHandler(st *store.Store, sessions *session.Store, objects *objectstore.Client) *Handler { + return &Handler{store: st, sessions: sessions, objects: objects} +} + +var shuttingDown atomic.Bool + +func SignalShutdown() { + shuttingDown.Store(true) +} + +type Status struct { + Status string `json:"status"` +} + +func (h *Handler) Live(c fiber.Ctx) error { + return c.JSON(Status{Status: "ok"}) +} + +func (h *Handler) Ready(c fiber.Ctx) error { + if shuttingDown.Load() { + return common.SendError(c, fiber.StatusServiceUnavailable, + common.ErrorCodeUnavailable, "service is shutting down") + } + + if err := h.store.Ping(c.Context()); err != nil { + return common.SendError(c, fiber.StatusServiceUnavailable, + common.ErrorCodeUnavailable, "database unavailable") + } + + if err := h.sessions.Ping(c.Context()); err != nil { + return common.SendError(c, fiber.StatusServiceUnavailable, + common.ErrorCodeUnavailable, "session store unavailable") + } + + if err := h.objects.Ping(c.Context()); err != nil { + return common.SendError(c, fiber.StatusServiceUnavailable, + common.ErrorCodeUnavailable, "object storage unavailable") + } + + return c.JSON(Status{Status: "ok"}) +} diff --git a/app/internal/messages/handler.go b/app/internal/messages/handler.go new file mode 100644 index 00000000..7194da7d --- /dev/null +++ b/app/internal/messages/handler.go @@ -0,0 +1,82 @@ +// Package messages implements message creation with idempotency and message +// history pagination. +package messages + +import ( + "gitflic.ru/project/bulla-max/mtusi2026lab/internal/common" + "gitflic.ru/project/bulla-max/mtusi2026lab/internal/store" + "github.com/gofiber/fiber/v3" + "github.com/google/uuid" +) + +type Handler struct { + store *store.Store + jsonBodyLimit int64 +} + +func NewHandler(st *store.Store, jsonBodyLimit int64) *Handler { + return &Handler{store: st, jsonBodyLimit: jsonBodyLimit} +} + +type CreateMessageRequest struct { + Text string `json:"text"` + AttachmentIDs []uuid.UUID `json:"attachment_ids"` +} + +func (h *Handler) CreateMessage(c fiber.Ctx) error { + userID := common.MustUserID(c) + + chatID, err := common.PathUUID(c, "chat_id") + if err != nil { + return err + } + + idempotencyKey := c.Get("Idempotency-Key") + if err := common.ValidateIdempotencyKey(idempotencyKey); err != nil { + return err + } + + var req CreateMessageRequest + if err := common.BindJSON(c, &req, h.jsonBodyLimit); err != nil { + return err + } + if err := common.ValidateAttachmentIDs(req.AttachmentIDs); err != nil { + return err + } + if err := common.ValidateMessageText(req.Text, len(req.AttachmentIDs)); err != nil { + return err + } + + message, err := h.store.CreateMessage(c.Context(), chatID, userID, req.Text, idempotencyKey, req.AttachmentIDs) + if err != nil { + return err + } + + return c.Status(fiber.StatusCreated).JSON(message) +} + +func (h *Handler) ListMessages(c fiber.Ctx) error { + userID := common.MustUserID(c) + + chatID, err := common.PathUUID(c, "chat_id") + if err != nil { + return err + } + + limit, err := common.ParseLimit(c, 50) + if err != nil { + return err + } + + cursor, err := common.ParseCursor(c) + if err != nil { + return err + } + + page, err := h.store.ListMessages(c.Context(), userID, chatID, limit, cursor) + if err != nil { + return err + } + + return c.JSON(page) +} diff --git a/app/internal/model/model.go b/app/internal/model/model.go new file mode 100644 index 00000000..b49abdcd --- /dev/null +++ b/app/internal/model/model.go @@ -0,0 +1,80 @@ +// Package model defines the JSON-facing domain types shared by handlers, +// storage and the public contract. +package model + +import ( + "time" + + "gitflic.ru/project/bulla-max/mtusi2026lab/internal/common" + "github.com/google/uuid" +) + +type User struct { + ID uuid.UUID `json:"id"` + Username string `json:"username"` + DisplayName string `json:"display_name"` + PasswordHash string `json:"-"` + CreatedAt common.RFCFormTime `json:"created_at"` +} + +func (u User) GetID() uuid.UUID { return u.ID } +func (u User) GetCreatedAt() time.Time { return time.Time(u.CreatedAt) } + +type Chat struct { + ID uuid.UUID `json:"id"` + Title string `json:"title"` + MemberIDs []uuid.UUID `json:"member_ids"` + CreatedAt common.RFCFormTime `json:"created_at"` +} + +func (c Chat) GetID() uuid.UUID { return c.ID } +func (c Chat) GetCreatedAt() time.Time { return time.Time(c.CreatedAt) } + +type Message struct { + ID uuid.UUID `json:"id"` + ChatID uuid.UUID `json:"chat_id"` + SenderID uuid.UUID `json:"sender_id"` + Text string `json:"text"` + CreatedAt common.RFCFormTime `json:"created_at"` + AttachmentIDs []uuid.UUID `json:"attachment_ids"` +} + +func (m Message) GetID() uuid.UUID { return m.ID } +func (m Message) GetCreatedAt() time.Time { return time.Time(m.CreatedAt) } + +type Attachment struct { + ID uuid.UUID `json:"id"` + ChatID uuid.UUID `json:"chat_id"` + OwnerID uuid.UUID `json:"owner_id"` + Filename string `json:"filename"` + ContentType string `json:"content_type"` + SizeBytes int64 `json:"size_bytes"` + SHA256 string `json:"sha256"` + Status string `json:"status"` + Variants []Variant `json:"variants"` + ErrorCode *string `json:"error_code"` + CreatedAt common.RFCFormTime `json:"created_at"` + UpdatedAt common.RFCFormTime `json:"updated_at"` + + // ObjectKey is internal and never serialized. + ObjectKey string `json:"-"` +} + +func (a Attachment) GetID() uuid.UUID { return a.ID } +func (a Attachment) GetCreatedAt() time.Time { return time.Time(a.CreatedAt) } + +type Variant struct { + Name string `json:"name"` + ContentType string `json:"content_type"` + SizeBytes int64 `json:"size_bytes"` + Width int `json:"width"` + Height int `json:"height"` + SHA256 string `json:"sha256"` +} + +type Session struct { + Token string `json:"token"` + TokenType string `json:"token_type"` + ExpiresAt common.RFCFormTime `json:"expires_at"` + User User `json:"user"` +} diff --git a/app/internal/objectstore/client.go b/app/internal/objectstore/client.go new file mode 100644 index 00000000..3cb8ea2b --- /dev/null +++ b/app/internal/objectstore/client.go @@ -0,0 +1,176 @@ +// Package objectstore is the only place that knows how to talk to the private S3 +// bucket. PostgreSQL remains the source of truth for metadata; this package +// stores and returns opaque bytes only. +package objectstore + +import ( + "context" + "errors" + "io" + "net/http" + "strings" + "time" + + "github.com/aws/aws-sdk-go-v2/aws" + awsconfig "github.com/aws/aws-sdk-go-v2/config" + "github.com/aws/aws-sdk-go-v2/credentials" + "github.com/aws/aws-sdk-go-v2/service/s3" + "github.com/aws/smithy-go" +) + +var ErrNotFound = errors.New("object not found") + +type Config struct { + Endpoint string + Region string + Bucket string + AccessKey string + SecretKey string +} + +type Client struct { + s3 *s3.Client + bucket string +} + +// Object describes one key returned by List. It intentionally exposes only the +// fields the reconciler needs. +type Object struct { + Key string + Size int64 + LastModified time.Time +} + +func New(cfg Config) (*Client, error) { + awsCfg, err := awsconfig.LoadDefaultConfig(context.Background(), + awsconfig.WithRegion(cfg.Region), + awsconfig.WithCredentialsProvider(credentials.NewStaticCredentialsProvider( + cfg.AccessKey, cfg.SecretKey, "")), + ) + if err != nil { + return nil, err + } + + // AGENT: a bounded retry count keeps a broken S3 dependency from turning an + // HTTP upload into an unbounded wait. Context deadlines in the handlers set + // the overall request budget. + awsCfg.RetryMaxAttempts = 2 + + transport := http.DefaultTransport.(*http.Transport).Clone() + transport.MaxIdleConnsPerHost = 20 + transport.MaxIdleConns = 40 + + client := s3.NewFromConfig(awsCfg, func(options *s3.Options) { + // RustFS is addressed by its Compose service name, so virtual-host + // addressing cannot work without extra DNS entries. Path-style is the + // portable choice for every S3-compatible server used in this stand. + options.UsePathStyle = true + options.BaseEndpoint = aws.String(strings.TrimRight(cfg.Endpoint, "/")) + options.HTTPClient = &http.Client{Transport: transport} + }) + + return &Client{s3: client, bucket: cfg.Bucket}, nil +} + +func (c *Client) Bucket() string { return c.bucket } + +// Ping is used by /health/ready. HeadBucket is the cheapest authenticated call +// that proves both the endpoint and the scoped credentials work. +func (c *Client) Ping(ctx context.Context) error { + pingCtx, cancel := context.WithTimeout(ctx, 2*time.Second) + defer cancel() + + _, err := c.s3.HeadBucket(pingCtx, &s3.HeadBucketInput{Bucket: aws.String(c.bucket)}) + return mapError(err) +} + +func (c *Client) Put(ctx context.Context, key string, body io.ReadSeeker, size int64, contentType string) error { + _, err := c.s3.PutObject(ctx, &s3.PutObjectInput{ + Bucket: aws.String(c.bucket), + Key: aws.String(key), + Body: body, + ContentLength: aws.Int64(size), + ContentType: aws.String(contentType), + }) + return mapError(err) +} + +func (c *Client) Get(ctx context.Context, key string) (io.ReadCloser, int64, error) { + output, err := c.s3.GetObject(ctx, &s3.GetObjectInput{ + Bucket: aws.String(c.bucket), + Key: aws.String(key), + }) + if err != nil { + return nil, 0, mapError(err) + } + + size := int64(0) + if output.ContentLength != nil { + size = *output.ContentLength + } + return output.Body, size, nil +} + +func (c *Client) Delete(ctx context.Context, key string) error { + _, err := c.s3.DeleteObject(ctx, &s3.DeleteObjectInput{ + Bucket: aws.String(c.bucket), + Key: aws.String(key), + }) + return mapError(err) +} + +func (c *Client) List(ctx context.Context, prefix string) ([]Object, error) { + var objects []Object + var continuation *string + + for { + output, err := c.s3.ListObjectsV2(ctx, &s3.ListObjectsV2Input{ + Bucket: aws.String(c.bucket), + Prefix: aws.String(prefix), + ContinuationToken: continuation, + }) + if err != nil { + return nil, mapError(err) + } + + for _, item := range output.Contents { + if item.Key == nil { + continue + } + object := Object{Key: *item.Key} + if item.Size != nil { + object.Size = *item.Size + } + if item.LastModified != nil { + object.LastModified = *item.LastModified + } + objects = append(objects, object) + } + + if output.IsTruncated == nil || !*output.IsTruncated { + return objects, nil + } + continuation = output.NextContinuationToken + if continuation == nil { + return objects, nil + } + } +} + +// mapError turns a missing object into a sentinel the HTTP layer can map to +// 404, while every other failure stays a dependency error (503). +func mapError(err error) error { + if err == nil { + return nil + } + + var apiErr smithy.APIError + if errors.As(err, &apiErr) { + switch apiErr.ErrorCode() { + case "NoSuchKey", "NoSuchBucket", "NotFound", "XMinioInvalidObjectName": + return ErrNotFound + } + } + + return err +} diff --git a/app/internal/reconcile/reconcile.go b/app/internal/reconcile/reconcile.go new file mode 100644 index 00000000..6eabba08 --- /dev/null +++ b/app/internal/reconcile/reconcile.go @@ -0,0 +1,115 @@ +// Package reconcile repairs the two failure windows between PostgreSQL +// metadata and S3 bytes: +// +// - a pending metadata row whose upload never finished or whose 201 was +// never returned; +// - an S3 object whose metadata transaction never committed. +// +// Both repairs use a grace period so an upload that is still in flight is +// never touched. +package reconcile + +import ( + "context" + "errors" + "log/slog" + "time" + + "gitflic.ru/project/bulla-max/mtusi2026lab/internal/objectstore" + "gitflic.ru/project/bulla-max/mtusi2026lab/internal/store" +) + +const objectPrefix = "attachments/" + +type Reconciler struct { + store *store.Store + objects *objectstore.Client + grace time.Duration + logger *slog.Logger +} + +type Stats struct { + StalePending int `json:"stale_pending"` + OrphanObjects int `json:"orphan_objects"` + DeletedRows int `json:"deleted_rows"` + DeletedObjects int `json:"deleted_objects"` + Skipped int `json:"skipped"` +} + +func New(st *store.Store, objects *objectstore.Client, grace time.Duration, logger *slog.Logger) *Reconciler { + if logger == nil { + logger = slog.Default() + } + return &Reconciler{store: st, objects: objects, grace: grace, logger: logger} +} + +func (r *Reconciler) RunOnce(ctx context.Context, dryRun bool) (Stats, error) { + var stats Stats + cutoff := time.Now().UTC().Add(-r.grace) + + // Phase 1: rows that were committed as pending but never reached ready. + stale, err := r.store.ListStalePendingAttachments(ctx, cutoff, 500) + if err != nil { + return stats, err + } + for _, attachment := range stale { + stats.StalePending++ + if dryRun { + r.logger.Info("reconcile: would remove stale pending attachment", + "attachment_id", attachment.ID, "object_key", attachment.ObjectKey, + "created_at", attachment.CreatedAt) + continue + } + + if err = r.objects.Delete(ctx, attachment.ObjectKey); err != nil && !errors.Is(err, objectstore.ErrNotFound) { + // Keep the row for the next pass. Deleting it now would lose the + // only pointer to an object that might still exist. + r.logger.Error("reconcile: delete stale object", + "err", err, "attachment_id", attachment.ID) + stats.Skipped++ + continue + } + if err = r.store.DeletePendingAttachment(ctx, attachment.ID); err != nil { + r.logger.Error("reconcile: delete stale row", "err", err, "attachment_id", attachment.ID) + stats.Skipped++ + continue + } + stats.DeletedRows++ + stats.DeletedObjects++ + } + + // Phase 2: objects with no metadata row at all. The pending row is written + // before the PUT in the normal path, so this only happens after a crash in + // a narrow window (or manual bucket tampering). + listed, err := r.objects.List(ctx, objectPrefix) + if err != nil { + return stats, err + } + known, err := r.store.ListAttachmentObjectKeys(ctx) + if err != nil { + return stats, err + } + for _, object := range listed { + if _, ok := known[object.Key]; ok { + continue + } + // A missing LastModified must not be treated as ancient: skipping is + // safer than deleting an object that may have just been created. + if object.LastModified.IsZero() || object.LastModified.After(cutoff) { + continue + } + stats.OrphanObjects++ + if dryRun { + r.logger.Info("reconcile: would delete orphan object", "object_key", object.Key) + continue + } + if err = r.objects.Delete(ctx, object.Key); err != nil && !errors.Is(err, objectstore.ErrNotFound) { + r.logger.Error("reconcile: delete orphan object", "err", err, "object_key", object.Key) + stats.Skipped++ + continue + } + stats.DeletedObjects++ + } + + return stats, nil +} diff --git a/app/internal/session/store.go b/app/internal/session/store.go new file mode 100644 index 00000000..b50cdcd3 --- /dev/null +++ b/app/internal/session/store.go @@ -0,0 +1,103 @@ +// Package session stores bearer sessions in Redis. Tokens are hashed before +// they become keys, and the configured TTL is absolute. +package session + +import ( + "context" + "crypto/rand" + "crypto/sha256" + "encoding/base64" + "encoding/hex" + "errors" + "fmt" + "strings" + "time" + + "github.com/google/uuid" + "github.com/redis/go-redis/v9" +) + +var ErrNotFound = errors.New("session not found") + +const keyPrefix = "messenger:session:" + +type Store struct { + client *redis.Client + ttl time.Duration + timeout time.Duration +} + +func New(rawURL string, ttl, timeout time.Duration) (*Store, error) { + options, err := redis.ParseURL(rawURL) + if err != nil { + return nil, fmt.Errorf("parse CACHE_URL: %w", err) + } + options.PoolSize = 10 + options.DialTimeout = timeout + options.ReadTimeout = timeout + options.WriteTimeout = timeout + options.MaxRetries = 0 + + return &Store{ + client: redis.NewClient(options), + ttl: ttl, + timeout: timeout, + }, nil +} + +func (s *Store) Close() error { + return s.client.Close() +} + +func (s *Store) Ping(ctx context.Context) error { + pingCtx, cancel := context.WithTimeout(ctx, s.timeout) + defer cancel() + return s.client.Ping(pingCtx).Err() +} + +func (s *Store) Create(ctx context.Context, userID uuid.UUID) (string, time.Time, error) { + raw := make([]byte, 32) + if _, err := rand.Read(raw); err != nil { + return "", time.Time{}, fmt.Errorf("generate token: %w", err) + } + token := base64.RawURLEncoding.EncodeToString(raw) + expiresAt := time.Now().UTC().Add(s.ttl) + + opCtx, cancel := context.WithTimeout(ctx, s.timeout) + defer cancel() + if err := s.client.Set(opCtx, keyFor(token), userID.String(), s.ttl).Err(); err != nil { + return "", time.Time{}, err + } + + return token, expiresAt, nil +} + +func (s *Store) Lookup(ctx context.Context, token string) (uuid.UUID, error) { + opCtx, cancel := context.WithTimeout(ctx, s.timeout) + defer cancel() + + value, err := s.client.Get(opCtx, keyFor(token)).Result() + if errors.Is(err, redis.Nil) { + return uuid.Nil, ErrNotFound + } + if err != nil { + return uuid.Nil, err + } + + userID, err := uuid.Parse(value) + if err != nil { + return uuid.Nil, ErrNotFound + } + return userID, nil +} + +func (s *Store) Revoke(ctx context.Context, token string) error { + opCtx, cancel := context.WithTimeout(ctx, s.timeout) + defer cancel() + return s.client.Del(opCtx, keyFor(token)).Err() +} + +func keyFor(token string) string { + sum := sha256.Sum256([]byte(strings.TrimSpace(token))) + return keyPrefix + hex.EncodeToString(sum[:]) +} diff --git a/app/internal/store/attachments.go b/app/internal/store/attachments.go new file mode 100644 index 00000000..cbb85d3c --- /dev/null +++ b/app/internal/store/attachments.go @@ -0,0 +1,170 @@ +package store + +import ( + "context" + "encoding/hex" + "errors" + "time" + + "gitflic.ru/project/bulla-max/mtusi2026lab/internal/common" + "gitflic.ru/project/bulla-max/mtusi2026lab/internal/model" + "github.com/google/uuid" + "github.com/jackc/pgx/v5" +) + +// CreatePendingAttachment records the intent to store an object before the S3 +// PUT happens. Committing this row first is what makes the reconciler able to +// distinguish "never uploaded" from "uploaded but process died". +func (s *Store) CreatePendingAttachment(ctx context.Context, att model.Attachment) error { + ctx, cancel := s.withTimeout(ctx) + defer cancel() + + hash, err := hex.DecodeString(att.SHA256) + if err != nil { + return common.BadRequest("invalid sha256") + } + + _, err = s.pool.Exec(ctx, ` + INSERT INTO attachments + (id, chat_id, owner_id, filename, content_type, size_bytes, sha256, + object_key, status, error_code, created_at, updated_at) + VALUES ($1, $2, $3, $4, $5, $6, $7, $8, 'pending', NULL, $9, $9)`, + att.ID, att.ChatID, att.OwnerID, att.Filename, att.ContentType, att.SizeBytes, + hash, att.ObjectKey, time.Time(att.CreatedAt)) + return dbError(err) +} + +// MarkAttachmentReady publishes metadata only after the object bytes are known +// to be in S3. A 201 is therefore a promise about both stores. +func (s *Store) MarkAttachmentReady(ctx context.Context, id uuid.UUID) (time.Time, error) { + ctx, cancel := s.withTimeout(ctx) + defer cancel() + + updatedAt := time.Now().UTC() + tag, err := s.pool.Exec(ctx, ` + UPDATE attachments + SET status = 'ready', updated_at = $2 + WHERE id = $1 AND status = 'pending'`, id, updatedAt) + if err != nil { + return time.Time{}, dbError(err) + } + if tag.RowsAffected() == 0 { + return time.Time{}, common.Conflict("attachment is not pending") + } + return updatedAt, nil +} + +// DeletePendingAttachment removes a row only while it is still pending. The +// status guard is the safety property: if an upload completed concurrently, the +// compensation or reconciler pass must not delete a ready attachment. +func (s *Store) DeletePendingAttachment(ctx context.Context, id uuid.UUID) error { + ctx, cancel := s.withTimeout(ctx) + defer cancel() + + _, err := s.pool.Exec(ctx, + `DELETE FROM attachments WHERE id = $1 AND status = 'pending'`, id) + return dbError(err) +} + +func (s *Store) GetAttachmentForUser(ctx context.Context, userID, attachmentID uuid.UUID) (model.Attachment, error) { + ctx, cancel := s.withTimeout(ctx) + defer cancel() + + att, err := scanAttachment(s.pool.QueryRow(ctx, ` + SELECT a.id, a.chat_id, a.owner_id, a.filename, a.content_type, + a.size_bytes, a.sha256, a.status, a.error_code, + a.created_at, a.updated_at, a.object_key + FROM attachments a + JOIN chat_members cm ON cm.chat_id = a.chat_id AND cm.user_id = $2 + WHERE a.id = $1`, attachmentID, userID)) + if err != nil { + return model.Attachment{}, err + } + return att, nil +} + +// ListStalePendingAttachments is the entry point for crash recovery. cutoff is +// an instant, not a duration: rows created before it are eligible. The caller +// computes it as now - grace so the boundary can be tuned and tested. +func (s *Store) ListStalePendingAttachments(ctx context.Context, cutoff time.Time, limit int) ([]model.Attachment, error) { + ctx, cancel := s.withTimeout(ctx) + defer cancel() + + rows, err := s.pool.Query(ctx, ` + SELECT a.id, a.chat_id, a.owner_id, a.filename, a.content_type, + a.size_bytes, a.sha256, a.status, a.error_code, + a.created_at, a.updated_at, a.object_key + FROM attachments a + WHERE a.status = 'pending' AND a.created_at < $1 + ORDER BY a.created_at + LIMIT $2`, cutoff, limit) + if err != nil { + return nil, dbError(err) + } + defer rows.Close() + + var result []model.Attachment + for rows.Next() { + att, scanErr := scanAttachment(rows) + if scanErr != nil { + return nil, dbError(scanErr) + } + result = append(result, att) + } + if err = rows.Err(); err != nil { + return nil, dbError(err) + } + return result, nil +} + +func (s *Store) ListAttachmentObjectKeys(ctx context.Context) (map[string]struct{}, error) { + ctx, cancel := s.withTimeout(ctx) + defer cancel() + + rows, err := s.pool.Query(ctx, `SELECT object_key FROM attachments`) + if err != nil { + return nil, dbError(err) + } + defer rows.Close() + + keys := make(map[string]struct{}) + for rows.Next() { + var key string + if err = rows.Scan(&key); err != nil { + return nil, dbError(err) + } + keys[key] = struct{}{} + } + if err = rows.Err(); err != nil { + return nil, dbError(err) + } + return keys, nil +} + +func scanAttachment(row rowScanner) (model.Attachment, error) { + var ( + att model.Attachment + hash []byte + errCode *string + createdAt time.Time + updatedAt time.Time + ) + + if err := row.Scan( + &att.ID, &att.ChatID, &att.OwnerID, &att.Filename, &att.ContentType, + &att.SizeBytes, &hash, &att.Status, &errCode, + &createdAt, &updatedAt, &att.ObjectKey, + ); err != nil { + if errors.Is(err, pgx.ErrNoRows) { + return model.Attachment{}, common.NotFound("attachment not found") + } + return model.Attachment{}, dbError(err) + } + + att.SHA256 = hex.EncodeToString(hash) + att.ErrorCode = errCode + att.Variants = []model.Variant{} + att.CreatedAt = common.RFCFormTime(createdAt.UTC()) + att.UpdatedAt = common.RFCFormTime(updatedAt.UTC()) + return att, nil +} diff --git a/app/internal/store/chats.go b/app/internal/store/chats.go new file mode 100644 index 00000000..b616d065 --- /dev/null +++ b/app/internal/store/chats.go @@ -0,0 +1,142 @@ +package store + +import ( + "context" + "errors" + "time" + + "gitflic.ru/project/bulla-max/mtusi2026lab/internal/common" + "gitflic.ru/project/bulla-max/mtusi2026lab/internal/model" + "github.com/google/uuid" + "github.com/jackc/pgx/v5" +) + +func (s *Store) CreateChat(ctx context.Context, title string, memberIDs []uuid.UUID) (model.Chat, error) { + ctx, cancel := s.withTimeout(ctx) + defer cancel() + + tx, err := s.pool.Begin(ctx) + if err != nil { + return model.Chat{}, dbError(err) + } + defer rollback(tx) + + var existing int + if err = tx.QueryRow(ctx, + `SELECT count(*) FROM users WHERE id = ANY($1::uuid[])`, + memberIDs).Scan(&existing); err != nil { + return model.Chat{}, dbError(err) + } + if existing != len(memberIDs) { + return model.Chat{}, common.BadRequest("member_id refers to non-existent user") + } + + chat := model.Chat{ + ID: uuid.New(), + Title: title, + MemberIDs: append([]uuid.UUID(nil), memberIDs...), + CreatedAt: common.NowUTC(), + } + + if _, err = tx.Exec(ctx, + `INSERT INTO chats (id, title, created_at) VALUES ($1, $2, $3)`, + chat.ID, chat.Title, time.Time(chat.CreatedAt)); err != nil { + return model.Chat{}, dbError(err) + } + + for _, memberID := range memberIDs { + if _, err = tx.Exec(ctx, + `INSERT INTO chat_members (chat_id, user_id) VALUES ($1, $2)`, + chat.ID, memberID); err != nil { + return model.Chat{}, dbError(err) + } + } + + if err = tx.Commit(ctx); err != nil { + return model.Chat{}, dbError(err) + } + + return chat, nil +} + +func (s *Store) GetChatForUser(ctx context.Context, userID, chatID uuid.UUID) (model.Chat, error) { + ctx, cancel := s.withTimeout(ctx) + defer cancel() + + return scanChat(s.pool.QueryRow(ctx, ` + SELECT c.id, c.title, c.created_at, + ARRAY(SELECT user_id FROM chat_members WHERE chat_id = c.id ORDER BY user_id) + FROM chats c + JOIN chat_members me ON me.chat_id = c.id AND me.user_id = $2 + WHERE c.id = $1`, chatID, userID)) +} + +func (s *Store) ListChats(ctx context.Context, userID uuid.UUID, limit int, cursor *common.CursorKeyCtx) (*common.Page[model.Chat], error) { + ctx, cancel := s.withTimeout(ctx) + defer cancel() + + cursorTime, cursorID := cursorValues(cursor) + rows, err := s.pool.Query(ctx, ` + SELECT c.id, c.title, c.created_at, + ARRAY(SELECT user_id FROM chat_members WHERE chat_id = c.id ORDER BY user_id) + FROM chats c + JOIN chat_members me ON me.chat_id = c.id AND me.user_id = $1 + WHERE (c.created_at, c.id) > ($2, $3) + ORDER BY c.created_at, c.id + LIMIT $4`, userID, cursorTime, cursorID, limit+1) + if err != nil { + return nil, dbError(err) + } + defer rows.Close() + + chats := make([]model.Chat, 0, limit) + for rows.Next() { + chat, scanErr := scanChat(rows) + if scanErr != nil { + return nil, scanErr + } + chats = append(chats, chat) + } + if err = rows.Err(); err != nil { + return nil, dbError(err) + } + + hasMore := len(chats) > limit + if hasMore { + chats = chats[:limit] + } + + return &common.Page[model.Chat]{Items: chats, NextCursor: common.NextCursor(chats, hasMore, userID)}, nil +} + +func (s *Store) IsMember(ctx context.Context, userID, chatID uuid.UUID) (bool, error) { + ctx, cancel := s.withTimeout(ctx) + defer cancel() + + var exists int + err := s.pool.QueryRow(ctx, + `SELECT 1 FROM chat_members WHERE chat_id = $1 AND user_id = $2`, + chatID, userID).Scan(&exists) + if errors.Is(err, pgx.ErrNoRows) { + return false, nil + } + if err != nil { + return false, dbError(err) + } + return true, nil +} + +// scanChat owns the "row is missing" meaning for single-row lookups; list +// queries never see ErrNoRows. +func scanChat(row rowScanner) (model.Chat, error) { + var chat model.Chat + var createdAt time.Time + if err := row.Scan(&chat.ID, &chat.Title, &createdAt, &chat.MemberIDs); err != nil { + if errors.Is(err, pgx.ErrNoRows) { + return model.Chat{}, common.NotFound("chat not found") + } + return model.Chat{}, dbError(err) + } + chat.CreatedAt = common.RFCFormTime(createdAt.UTC()) + return chat, nil +} diff --git a/app/internal/store/errors.go b/app/internal/store/errors.go new file mode 100644 index 00000000..237c0ccb --- /dev/null +++ b/app/internal/store/errors.go @@ -0,0 +1,44 @@ +package store + +import ( + "context" + "errors" + "log/slog" + "net" + + "gitflic.ru/project/bulla-max/mtusi2026lab/internal/common" + "github.com/jackc/pgx/v5" + "github.com/jackc/pgx/v5/pgconn" +) + +func isUniqueViolation(err error) bool { + pgErr, ok := errors.AsType[*pgconn.PgError](err) + return ok && pgErr.Code == "23505" +} + +// dbError maps driver failures to contract errors. Unexpected failures are +// logged with their cause so a 500 is never silent. +func dbError(err error) error { + if err == nil { + return nil + } + + if pgErr, ok := errors.AsType[*pgconn.PgError](err); ok { + switch pgErr.Code { + case "23505": + return common.Conflict("resource already exists") + case "23503", "23514", "22P02": + return common.BadRequest("request conflicts with stored data") + } + } + + if _, ok := errors.AsType[net.Error](err); ok || errors.Is(err, context.DeadlineExceeded) || + errors.Is(err, context.Canceled) || + pgconn.SafeToRetry(err) || + errors.Is(err, pgx.ErrTxClosed) { + return common.Unavailable("database unavailable") + } + + slog.Error("unhandled database error", "err", err) + return common.Internal() +} diff --git a/app/internal/store/messages.go b/app/internal/store/messages.go new file mode 100644 index 00000000..0864d8b3 --- /dev/null +++ b/app/internal/store/messages.go @@ -0,0 +1,285 @@ +package store + +import ( + "bytes" + "context" + "crypto/sha256" + "errors" + "sort" + "time" + + "gitflic.ru/project/bulla-max/mtusi2026lab/internal/common" + "gitflic.ru/project/bulla-max/mtusi2026lab/internal/model" + "github.com/google/uuid" + "github.com/jackc/pgx/v5" +) + +func (s *Store) CreateMessage(ctx context.Context, chatID, senderID uuid.UUID, text, idempotencyKey string, attachmentIDs []uuid.UUID) (model.Message, error) { + ctx, cancel := s.withTimeout(ctx) + defer cancel() + + // Sorting makes the idempotency fingerprint independent of the order + // in which the client listed the same set of attachment IDs. The response + // uses the same deterministic order. + attachmentIDs = canonicalAttachmentIDs(attachmentIDs) + fingerprint := messageFingerprint(text, attachmentIDs) + + if message, found, err := s.findIdempotentMessage(ctx, senderID, chatID, idempotencyKey, fingerprint); err != nil { + return model.Message{}, err + } else if found { + return message, nil + } + + message := model.Message{ + ID: uuid.New(), + ChatID: chatID, + SenderID: senderID, + Text: text, + CreatedAt: common.NowUTC(), + AttachmentIDs: attachmentIDs, + } + + tx, err := s.pool.Begin(ctx) + if err != nil { + return model.Message{}, dbError(err) + } + defer rollback(tx) + + // Check membership and attachment ownership in the same transaction as the + // insert. A foreign chat or a foreign attachment is intentionally the same + // 404 as a missing object, so the API does not leak existence. + var member int + err = tx.QueryRow(ctx, + `SELECT 1 FROM chat_members WHERE chat_id = $1 AND user_id = $2`, + chatID, senderID).Scan(&member) + if errors.Is(err, pgx.ErrNoRows) { + return model.Message{}, common.NotFound("chat not found") + } + if err != nil { + return model.Message{}, dbError(err) + } + + if len(attachmentIDs) > 0 { + rows, queryErr := tx.Query(ctx, ` + SELECT id, status FROM attachments + WHERE chat_id = $1 AND id = ANY($2::uuid[])`, chatID, attachmentIDs) + if queryErr != nil { + return model.Message{}, dbError(queryErr) + } + + found := 0 + for rows.Next() { + var ( + id uuid.UUID + status string + ) + if scanErr := rows.Scan(&id, &status); scanErr != nil { + rows.Close() + return model.Message{}, dbError(scanErr) + } + found++ + if status != "ready" { + rows.Close() + return model.Message{}, common.Conflict("attachment is not ready") + } + } + if err = rows.Err(); err != nil { + rows.Close() + return model.Message{}, dbError(err) + } + rows.Close() + + if found != len(attachmentIDs) { + return model.Message{}, common.NotFound("attachment not found") + } + } + + _, err = tx.Exec(ctx, ` + INSERT INTO messages (id, chat_id, sender_id, text, created_at) + VALUES ($1, $2, $3, $4, $5)`, + message.ID, message.ChatID, message.SenderID, message.Text, time.Time(message.CreatedAt)) + if err != nil { + return model.Message{}, dbError(err) + } + + for position, attachmentID := range attachmentIDs { + if _, err = tx.Exec(ctx, ` + INSERT INTO message_attachments (message_id, attachment_id, position) + VALUES ($1, $2, $3)`, message.ID, attachmentID, position); err != nil { + return model.Message{}, dbError(err) + } + } + + expiresAt := time.Time(message.CreatedAt).Add(s.idempotencyTTL) + tag, err := tx.Exec(ctx, ` + INSERT INTO message_idempotency + (sender_id, chat_id, idempotency_key, request_hash, message_id, created_at, expires_at) + VALUES ($1, $2, $3, $4, $5, $6, $7) + ON CONFLICT (sender_id, chat_id, idempotency_key) DO UPDATE + SET request_hash = EXCLUDED.request_hash, + message_id = EXCLUDED.message_id, + created_at = EXCLUDED.created_at, + expires_at = EXCLUDED.expires_at + WHERE message_idempotency.expires_at <= now()`, + senderID, chatID, idempotencyKey, fingerprint, message.ID, + time.Time(message.CreatedAt), expiresAt) + if err != nil { + return model.Message{}, dbError(err) + } + + if tag.RowsAffected() == 0 { + rollback(tx) + stored, found, lookupErr := s.findIdempotentMessage(ctx, senderID, chatID, idempotencyKey, fingerprint) + if lookupErr != nil { + return model.Message{}, lookupErr + } + if !found { + return model.Message{}, common.Internal() + } + return stored, nil + } + + if err = tx.Commit(ctx); err != nil { + return model.Message{}, dbError(err) + } + + return message, nil +} + +func canonicalAttachmentIDs(ids []uuid.UUID) []uuid.UUID { + result := append([]uuid.UUID(nil), ids...) + sort.Slice(result, func(i, j int) bool { + return bytes.Compare(result[i][:], result[j][:]) < 0 + }) + if result == nil { + return []uuid.UUID{} + } + return result +} + +// messageFingerprint keeps the lab3 hash for messages without attachments, so +// an idempotency key stored before this migration still replays correctly. For +// messages with attachments the sorted IDs are part of the canonical body. +func messageFingerprint(text string, attachmentIDs []uuid.UUID) []byte { + if len(attachmentIDs) == 0 { + sum := sha256.Sum256([]byte(text)) + return sum[:] + } + + hash := sha256.New() + hash.Write([]byte(text)) + hash.Write([]byte{0}) + for _, id := range attachmentIDs { + hash.Write(id[:]) + } + return hash.Sum(nil) +} + +func (s *Store) findIdempotentMessage(ctx context.Context, senderID, chatID uuid.UUID, idempotencyKey string, expectedHash []byte) (model.Message, bool, error) { + var message model.Message + var createdAt time.Time + var storedHash []byte + + err := s.pool.QueryRow(ctx, ` + SELECT m.id, m.chat_id, m.sender_id, m.text, m.created_at, + ARRAY(SELECT ma.attachment_id + FROM message_attachments ma + WHERE ma.message_id = m.id + ORDER BY ma.position), + i.request_hash + FROM message_idempotency i + JOIN messages m ON m.id = i.message_id + JOIN chat_members cm ON cm.chat_id = i.chat_id AND cm.user_id = i.sender_id + WHERE i.sender_id = $1 + AND i.chat_id = $2 + AND i.idempotency_key = $3 + AND i.expires_at > now()`, + senderID, chatID, idempotencyKey).Scan( + &message.ID, &message.ChatID, &message.SenderID, &message.Text, &createdAt, + &message.AttachmentIDs, &storedHash) + if errors.Is(err, pgx.ErrNoRows) { + return model.Message{}, false, nil + } + if err != nil { + return model.Message{}, false, dbError(err) + } + if !bytes.Equal(storedHash, expectedHash) { + return model.Message{}, false, common.Conflict("idempotency key reused with a different message") + } + if message.AttachmentIDs == nil { + message.AttachmentIDs = []uuid.UUID{} + } + + message.CreatedAt = common.RFCFormTime(createdAt.UTC()) + return message, true, nil +} + +func (s *Store) ListMessages(ctx context.Context, userID, chatID uuid.UUID, limit int, cursor *common.CursorKeyCtx) (*common.Page[model.Message], error) { + ctx, cancel := s.withTimeout(ctx) + defer cancel() + + isMember, err := s.IsMember(ctx, userID, chatID) + if err != nil { + return nil, err + } + if !isMember { + return nil, common.NotFound("chat not found") + } + // AGENT: Cursor context is checked only after membership, so a foreign + // caller gets 404 and cannot probe cursor validity against someone else's chat. + if cursor != nil && cursor.ContextID != chatID { + return nil, common.BadRequest("bad cursor value") + } + + cursorTime, cursorID := cursorValues(cursor) + rows, err := s.pool.Query(ctx, ` + SELECT m.id, m.chat_id, m.sender_id, m.text, m.created_at, + ARRAY(SELECT ma.attachment_id + FROM message_attachments ma + WHERE ma.message_id = m.id + ORDER BY ma.position) + FROM messages m + WHERE m.chat_id = $1 AND (m.created_at, m.id) > ($2, $3) + ORDER BY m.created_at, m.id + LIMIT $4`, chatID, cursorTime, cursorID, limit+1) + if err != nil { + return nil, dbError(err) + } + defer rows.Close() + + messages := make([]model.Message, 0, limit) + for rows.Next() { + message, scanErr := scanMessage(rows) + if scanErr != nil { + return nil, dbError(scanErr) + } + messages = append(messages, message) + } + if err = rows.Err(); err != nil { + return nil, dbError(err) + } + + hasMore := len(messages) > limit + if hasMore { + messages = messages[:limit] + } + + return &common.Page[model.Message]{ + Items: messages, + NextCursor: common.NextCursor(messages, hasMore, chatID), + }, nil +} + +func scanMessage(row rowScanner) (model.Message, error) { + var message model.Message + var createdAt time.Time + if err := row.Scan(&message.ID, &message.ChatID, &message.SenderID, &message.Text, + &createdAt, &message.AttachmentIDs); err != nil { + return model.Message{}, err + } + if message.AttachmentIDs == nil { + message.AttachmentIDs = []uuid.UUID{} + } + message.CreatedAt = common.RFCFormTime(createdAt.UTC()) + return message, nil +} diff --git a/app/internal/store/messages_test.go b/app/internal/store/messages_test.go new file mode 100644 index 00000000..0363a836 --- /dev/null +++ b/app/internal/store/messages_test.go @@ -0,0 +1,80 @@ +package store + +import ( + "bytes" + "crypto/sha256" + "testing" + + "github.com/google/uuid" +) + +func TestCanonicalAttachmentIDsSortsAndCopies(t *testing.T) { + first, second, third := uuid.MustParse("00000000-0000-0000-0000-000000000003"), + uuid.MustParse("00000000-0000-0000-0000-000000000001"), + uuid.MustParse("00000000-0000-0000-0000-000000000002") + + input := []uuid.UUID{first, second, third} + got := canonicalAttachmentIDs(input) + + want := []uuid.UUID{second, third, first} + if !equalIDs(got, want) { + t.Fatalf("canonicalAttachmentIDs = %v, want %v", got, want) + } + if !equalIDs(input, []uuid.UUID{first, second, third}) { + t.Fatalf("canonicalAttachmentIDs mutated its input: %v", input) + } +} + +func TestCanonicalAttachmentIDsReturnsEmptySliceForNil(t *testing.T) { + got := canonicalAttachmentIDs(nil) + if got == nil { + t.Fatal("canonicalAttachmentIDs(nil) = nil, want empty slice") + } + if len(got) != 0 { + t.Fatalf("canonicalAttachmentIDs(nil) = %v, want empty", got) + } +} + +// TestMessageFingerprintWithoutAttachmentsPreservesLab3Hash is the +// compatibility guard: idempotency rows written before attachments existed +// must still match on replay. +func TestMessageFingerprintWithoutAttachmentsPreservesLab3Hash(t *testing.T) { + expected := sha256.Sum256([]byte("hello")) + if got := messageFingerprint("hello", nil); !bytes.Equal(got, expected[:]) { + t.Fatalf("fingerprint = %x, want %x", got, expected) + } + if got := messageFingerprint("hello", []uuid.UUID{}); !bytes.Equal(got, expected[:]) { + t.Fatalf("empty slice fingerprint = %x, want %x", got, expected) + } +} + +func TestMessageFingerprintDependsOnSortedAttachments(t *testing.T) { + first := uuid.MustParse("00000000-0000-0000-0000-000000000001") + second := uuid.MustParse("00000000-0000-0000-0000-000000000002") + + // CreateMessage canonicalizes before hashing; the pair must therefore be + // order independent even though messageFingerprint itself takes the list + // as given. + one := messageFingerprint("hello", canonicalAttachmentIDs([]uuid.UUID{first, second})) + reordered := messageFingerprint("hello", canonicalAttachmentIDs([]uuid.UUID{second, first})) + if !bytes.Equal(one, reordered) { + t.Fatal("fingerprint depends on attachment order") + } + + other := messageFingerprint("hello", canonicalAttachmentIDs([]uuid.UUID{first})) + if bytes.Equal(one, other) { + t.Fatal("fingerprint ignores attachment identity") + } +} + +func equalIDs(a, b []uuid.UUID) bool { + if len(a) != len(b) { + return false + } + for index := range a { + if a[index] != b[index] { + return false + } + } + return true +} diff --git a/app/internal/store/pagination.go b/app/internal/store/pagination.go new file mode 100644 index 00000000..12f2c77e --- /dev/null +++ b/app/internal/store/pagination.go @@ -0,0 +1,17 @@ +package store + +import ( + "time" + + "gitflic.ru/project/bulla-max/mtusi2026lab/internal/common" + "github.com/google/uuid" +) + +// cursorValues converts an optional page cursor to the two keyset columns used +// by every paginated query. A nil cursor means "start from the beginning". +func cursorValues(cursor *common.CursorKeyCtx) (time.Time, uuid.UUID) { + if cursor == nil { + return time.Time{}, uuid.Nil + } + return cursor.Time, cursor.ID +} diff --git a/app/internal/store/store.go b/app/internal/store/store.go new file mode 100644 index 00000000..5884e831 --- /dev/null +++ b/app/internal/store/store.go @@ -0,0 +1,36 @@ +// Package store contains the PostgreSQL persistence layer. It uses plain +// parameterized SQL, one bounded pool and explicit transaction boundaries. +// Contract errors are returned as common.AppError so HTTP status mapping stays +// in one place. +package store + +import ( + "context" + "time" + + "github.com/jackc/pgx/v5/pgxpool" +) + +type Store struct { + pool *pgxpool.Pool + timeout time.Duration + idempotencyTTL time.Duration +} + +func New(pool *pgxpool.Pool, timeout, idempotencyTTL time.Duration) *Store { + return &Store{pool: pool, timeout: timeout, idempotencyTTL: idempotencyTTL} +} + +func (s *Store) withTimeout(ctx context.Context) (context.Context, context.CancelFunc) { + return context.WithTimeout(ctx, s.timeout) +} + +func (s *Store) Ping(ctx context.Context) error { + pingCtx, cancel := context.WithTimeout(ctx, 2*time.Second) + defer cancel() + return s.pool.Ping(pingCtx) +} + +type rowScanner interface { + Scan(dest ...any) error +} diff --git a/app/internal/store/tx.go b/app/internal/store/tx.go new file mode 100644 index 00000000..77d98516 --- /dev/null +++ b/app/internal/store/tx.go @@ -0,0 +1,16 @@ +package store + +import ( + "context" + "time" + + "github.com/jackc/pgx/v5" +) + +const rollbackTimeout = 5 * time.Second + +func rollback(tx pgx.Tx) { + ctx, cancel := context.WithTimeout(context.Background(), rollbackTimeout) + defer cancel() + _ = tx.Rollback(ctx) +} diff --git a/app/internal/store/users.go b/app/internal/store/users.go new file mode 100644 index 00000000..2d6380b7 --- /dev/null +++ b/app/internal/store/users.go @@ -0,0 +1,69 @@ +package store + +import ( + "context" + "errors" + "time" + + "gitflic.ru/project/bulla-max/mtusi2026lab/internal/common" + "gitflic.ru/project/bulla-max/mtusi2026lab/internal/model" + "github.com/google/uuid" + "github.com/jackc/pgx/v5" +) + +func (s *Store) CreateUser(ctx context.Context, username, displayName, passwordHash string) (model.User, error) { + ctx, cancel := s.withTimeout(ctx) + defer cancel() + + user := model.User{ + ID: uuid.New(), + Username: username, + DisplayName: displayName, + PasswordHash: passwordHash, + CreatedAt: common.NowUTC(), + } + + _, err := s.pool.Exec(ctx, ` + INSERT INTO users (id, username, display_name, password_hash, created_at) + VALUES ($1, $2, $3, $4, $5)`, + user.ID, user.Username, user.DisplayName, user.PasswordHash, time.Time(user.CreatedAt)) + if err != nil { + if isUniqueViolation(err) { + return model.User{}, common.Conflict("username already taken") + } + return model.User{}, dbError(err) + } + + return user, nil +} + +func (s *Store) GetUserByID(ctx context.Context, id uuid.UUID) (model.User, error) { + ctx, cancel := s.withTimeout(ctx) + defer cancel() + + return s.scanUser(s.pool.QueryRow(ctx, ` + SELECT id, username, display_name, password_hash, created_at + FROM users WHERE id = $1`, id)) +} + +func (s *Store) GetUserByUsername(ctx context.Context, username string) (model.User, error) { + ctx, cancel := s.withTimeout(ctx) + defer cancel() + + return s.scanUser(s.pool.QueryRow(ctx, ` + SELECT id, username, display_name, password_hash, created_at + FROM users WHERE username = $1`, username)) +} + +func (s *Store) scanUser(row rowScanner) (model.User, error) { + var user model.User + var createdAt time.Time + if err := row.Scan(&user.ID, &user.Username, &user.DisplayName, &user.PasswordHash, &createdAt); err != nil { + if errors.Is(err, pgx.ErrNoRows) { + return model.User{}, common.NotFound("user not found") + } + return model.User{}, dbError(err) + } + user.CreatedAt = common.RFCFormTime(createdAt.UTC()) + return user, nil +} diff --git a/app/internal/users/handler.go b/app/internal/users/handler.go new file mode 100644 index 00000000..e30363de --- /dev/null +++ b/app/internal/users/handler.go @@ -0,0 +1,62 @@ +// Package users implements registration and the current-user endpoint. +package users + +import ( + "log/slog" + + "gitflic.ru/project/bulla-max/mtusi2026lab/internal/auth" + "gitflic.ru/project/bulla-max/mtusi2026lab/internal/common" + "gitflic.ru/project/bulla-max/mtusi2026lab/internal/store" + "github.com/gofiber/fiber/v3" +) + +type Handler struct { + store *store.Store + jsonBodyLimit int64 +} + +func NewHandler(st *store.Store, jsonBodyLimit int64) *Handler { + return &Handler{store: st, jsonBodyLimit: jsonBodyLimit} +} + +type CreateUserRequest struct { + Username string `json:"username"` + DisplayName string `json:"display_name"` + Password string `json:"password"` +} + +func (h *Handler) CreateUser(c fiber.Ctx) error { + var req CreateUserRequest + if err := common.BindJSON(c, &req, h.jsonBodyLimit); err != nil { + return err + } + + if err := common.ValidateCreateUser(req.Username, req.DisplayName); err != nil { + return err + } + if err := common.ValidatePassword(req.Password); err != nil { + return err + } + + hash, err := auth.HashPassword(req.Password) + if err != nil { + slog.Error("hash password", "err", err) + return common.Internal() + } + + user, err := h.store.CreateUser(c.Context(), req.Username, req.DisplayName, hash) + if err != nil { + return err + } + + return c.Status(fiber.StatusCreated).JSON(user) +} + +func (h *Handler) GetCurrentUser(c fiber.Ctx) error { + user, err := h.store.GetUserByID(c.Context(), common.MustUserID(c)) + if err != nil { + return err + } + + return c.JSON(user) +} diff --git a/compose.example.yaml.lab4 b/compose.example.yaml.lab4 new file mode 100644 index 00000000..76a785ff --- /dev/null +++ b/compose.example.yaml.lab4 @@ -0,0 +1,12 @@ +# Topology worksheet, deliberately NOT a runnable stack (no application supplied). +# Implement compose.yaml or stack.yaml; do not submit this empty file as deployment. +# Service names below are logical roles, not imposed container names. +x-lab-number: 4 +x-required-roles: + - api + - postgres + - load_balancer + - s3 +# TODO: implement real images/builds, network wiring, volumes, healthchecks, +# credentials and bootstrap. Details: contracts/infrastructure.json and README.md. +services: {} diff --git a/compose.yaml b/compose.yaml new file mode 100644 index 00000000..8aa91626 --- /dev/null +++ b/compose.yaml @@ -0,0 +1,258 @@ +name: mtusi2026lab + +x-api-build: &api-build + image: mtusi2026lab-api:local + build: + context: . + dockerfile: Dockerfile + +# API receives only the attachments/ scoped S3 credential +x-api-environment: &api-environment + APP_HOST: 0.0.0.0 + APP_PORT: "8080" + DB_POOL_MAX_CONNS: ${DB_POOL_MAX_CONNS:-10} + DATABASE_URL: postgres://${POSTGRES_USER:-messenger}:${POSTGRES_PASSWORD:?set POSTGRES_PASSWORD in .env}@postgres:5432/${POSTGRES_DB:-messenger}?sslmode=disable + CACHE_URL: redis://redis:6379/0 + SESSION_TTL_SECONDS: ${SESSION_TTL_SECONDS:-3600} + IDEMPOTENCY_TTL_SECONDS: ${IDEMPOTENCY_TTL_SECONDS:-86400} + S3_ENDPOINT: ${S3_ENDPOINT:-http://rustfs:9000} + S3_REGION: ${S3_REGION:-us-east-1} + S3_BUCKET: ${S3_BUCKET:-messenger-attachments} + S3_ACCESS_KEY_ID: ${S3_ACCESS_KEY_ID:?set S3_ACCESS_KEY_ID in .env} + S3_SECRET_ACCESS_KEY: ${S3_SECRET_ACCESS_KEY:?set S3_SECRET_ACCESS_KEY in .env} + MAX_UPLOAD_BYTES: ${MAX_UPLOAD_BYTES:-10485760} + MAX_IMAGE_PIXELS: ${MAX_IMAGE_PIXELS:-40000000} + IMAGE_DECODE_MEMORY_MB: ${IMAGE_DECODE_MEMORY_MB:-256} + UPLOAD_TEMP_DIR: /tmp/messenger-uploads + +services: + postgres: + image: postgres:16.10-alpine + restart: unless-stopped + environment: + POSTGRES_USER: ${POSTGRES_USER:-messenger} + POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:?set POSTGRES_PASSWORD in .env} + POSTGRES_DB: ${POSTGRES_DB:-messenger} + volumes: + - pgdata:/var/lib/postgresql/data + healthcheck: + test: ["CMD-SHELL", "pg_isready -U $${POSTGRES_USER} -d $${POSTGRES_DB}"] + interval: 3s + timeout: 3s + retries: 20 + start_period: 5s + deploy: + resources: + limits: + cpus: "${POSTGRES_CPU_LIMIT:-1.0}" + memory: "${POSTGRES_MEM_LIMIT:-512M}" + + redis: + image: redis:7.4.7-alpine + restart: unless-stopped + command: + [ + "redis-server", + "--appendonly", + "yes", + "--appendfsync", + "everysec", + "--save", + "", + ] + volumes: + - redisdata:/data + healthcheck: + test: ["CMD", "redis-cli", "ping"] + interval: 3s + timeout: 3s + retries: 20 + start_period: 3s + deploy: + resources: + limits: + cpus: "${REDIS_CPU_LIMIT:-0.50}" + memory: "${REDIS_MEM_LIMIT:-256M}" + + rustfs: + image: rustfs/rustfs:1.0.4 + restart: unless-stopped + # AGENT: no published ports. The S3 API is reachable only on the Compose + # network, and the admin console is disabled. For inspection, see the + # one-shot rc client pattern in scripts/check-private-bucket.sh. + environment: + RUSTFS_ACCESS_KEY: ${S3_ROOT_ACCESS_KEY:?set S3_ROOT_ACCESS_KEY in .env} + RUSTFS_SECRET_KEY: ${S3_ROOT_SECRET_KEY:?set S3_ROOT_SECRET_KEY in .env} + RUSTFS_ADDRESS: ":9000" + RUSTFS_CONSOLE_ENABLE: "false" + RUSTFS_OBS_LOGGER_LEVEL: "error" + RUSTFS_OBS_LOG_DIRECTORY: "/logs" + command: ["/data"] + volumes: + - rustfsdata:/data + expose: + - "9000" + healthcheck: + test: + [ + "CMD-SHELL", + "wget -q -O - http://127.0.0.1:9000/health/ready >/dev/null", + ] + interval: 3s + timeout: 3s + retries: 20 + start_period: 5s + stop_grace_period: 15s + deploy: + resources: + limits: + cpus: "${RUSTFS_CPU_LIMIT:-1.0}" + memory: "${RUSTFS_MEM_LIMIT:-512M}" + + # AGENT: bootstrap converges the bucket and the scoped IAM identity on every + # run. Create is idempotent (ignore-existing), policy create replaces the + # named document, and user add updates the password if the user exists. + s3-init: + image: rustfs/rc:v0.1.36 + restart: "no" + depends_on: + rustfs: + condition: service_healthy + environment: + HOME: /tmp + S3_ENDPOINT: ${S3_ENDPOINT:-http://rustfs:9000} + S3_REGION: ${S3_REGION:-us-east-1} + S3_BUCKET: ${S3_BUCKET:-messenger-attachments} + S3_ROOT_ACCESS_KEY: ${S3_ROOT_ACCESS_KEY:?set S3_ROOT_ACCESS_KEY in .env} + S3_ROOT_SECRET_KEY: ${S3_ROOT_SECRET_KEY:?set S3_ROOT_SECRET_KEY in .env} + S3_ACCESS_KEY_ID: ${S3_ACCESS_KEY_ID:?set S3_ACCESS_KEY_ID in .env} + S3_SECRET_ACCESS_KEY: ${S3_SECRET_ACCESS_KEY:?set S3_SECRET_ACCESS_KEY in .env} + S3_APP_USER: ${S3_APP_USER:-messenger-api} + S3_APP_POLICY: ${S3_APP_POLICY:-messenger-attachments-rw} + S3_POLICY_TEMPLATE: /config/api-policy.json.template + volumes: + - ./infra/rustfs/bootstrap.sh:/bootstrap.sh:ro + - ./infra/rustfs/api-policy.json.template:/config/api-policy.json.template:ro + entrypoint: ["/bin/sh", "/bootstrap.sh"] + + migrate: + <<: *api-build + restart: "no" + depends_on: + postgres: + condition: service_healthy + environment: + <<: *api-environment + MIGRATE_ONLY: "true" + + api: + <<: *api-build + restart: unless-stopped + depends_on: + postgres: + condition: service_healthy + redis: + condition: service_healthy + rustfs: + condition: service_healthy + s3-init: + condition: service_completed_successfully + migrate: + condition: service_completed_successfully + environment: + <<: *api-environment + RUN_MIGRATIONS: "false" + expose: + - "8080" + healthcheck: + test: + ["CMD", "wget", "-q", "-O", "-", "http://127.0.0.1:8080/health/ready"] + interval: 2s + timeout: 2s + retries: 10 + start_period: 10s + stop_grace_period: 15s + deploy: + replicas: ${API_REPLICAS:-2} + resources: + limits: + cpus: "${API_CPU_LIMIT:-0.50}" + memory: "${API_MEM_LIMIT:-512M}" + + # AGENT: the reconciler uses the same scoped credential as the API. It never + # touches ready attachments; it only removes stale pending rows/objects and + # objects with no metadata row at all. + reconciler: + <<: *api-build + restart: unless-stopped + depends_on: + postgres: + condition: service_healthy + rustfs: + condition: service_healthy + s3-init: + condition: service_completed_successfully + migrate: + condition: service_completed_successfully + environment: + <<: *api-environment + RUN_MIGRATIONS: "false" + RECONCILE_ONLY: "true" + RECONCILE_GRACE_SECONDS: ${RECONCILE_GRACE_SECONDS:-900} + RECONCILE_INTERVAL_SECONDS: ${RECONCILE_INTERVAL_SECONDS:-300} + entrypoint: ["/usr/local/bin/reconciler"] + deploy: + resources: + limits: + cpus: "${RECONCILER_CPU_LIMIT:-0.25}" + memory: "${RECONCILER_MEM_LIMIT:-128M}" + + haproxy: + image: haproxy:3.2.6-alpine + restart: unless-stopped + depends_on: + api: + condition: service_healthy + ports: + - "${LB_PORT:-8080}:8080" + - "${HAPROXY_STATS_PORT:-8404}:8404" + volumes: + - ./infra/haproxy/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg:ro + healthcheck: + test: + ["CMD", "wget", "-q", "-O", "/dev/null", "http://127.0.0.1:8404/stats"] + interval: 2s + timeout: 2s + retries: 10 + start_period: 5s + deploy: + resources: + limits: + cpus: "${HAPROXY_CPU_LIMIT:-0.50}" + memory: "${HAPROXY_MEM_LIMIT:-128M}" + + k6: + image: grafana/k6:1.3.0 + profiles: ["load"] + depends_on: + haproxy: + condition: service_healthy + user: "0:0" + environment: + BASE_URL: http://haproxy:8080 + VUS: ${K6_VUS:-10} + DURATION: ${K6_DURATION:-60s} + volumes: + - ./load:/scripts:ro + - ./evidence:/results + command: ["run", "/scripts/messages.js"] + deploy: + resources: + limits: + cpus: "${K6_CPU_LIMIT:-1.0}" + memory: "${K6_MEM_LIMIT:-512M}" + +volumes: + pgdata: + redisdata: + rustfsdata: diff --git a/contracts/README.md.lab4 b/contracts/README.md.lab4 new file mode 100644 index 00000000..8c58b32f --- /dev/null +++ b/contracts/README.md.lab4 @@ -0,0 +1,61 @@ +# Семантика API + +Этот текст дополняет [openapi.json](openapi.json). Обязательный API соответствует уровню 3 текущей лабораторной. Факультативный crop или rate limiting не становятся обязательными только из-за наличия допустимого имени варианта или кода ошибки в схеме. + +## Формат и ошибки + +- JSON — UTF-8, `Content-Type: application/json`. UUID — строка; время — RFC 3339 UTC с `Z`, назначается сервером. Часы не считаются уникальным ID. +- Каждый ответ API, включая ошибки и health, содержит непустой `X-Request-Id`. Тело ошибки: `{"error":{"code":"invalid_request","message":"...","request_id":"..."}}`; `request_id` совпадает с заголовком. Сообщение ошибки не раскрывает stack trace, SQL, credentials или наличие чужого объекта. +- Обязательные строки не состоят только из пробельных символов. `username` строго `[a-z0-9_]{3,32}`, уникален; `display_name` — 1–100 Unicode code points. JSON с лишними полями или неверными типами — `400`; malformed JSON также `400`. +- `400 invalid_request` — неверные поля, UUID, cursor, limit; `401 unauthorized` — нет действительной идентификации/сессии; `404 not_found` — объекта нет или он чужой; `409 conflict` — username занят, повторный ключ с другим сообщением, объект ещё не готов; `413 payload_too_large`; `415 unsupported_media_type`; `429 rate_limited`; `503 unavailable`. Неожиданное исключение — `500 internal_error` без внутренних деталей. +- Сначала аутентификация, затем доступ к конкретному ресурсу. Любой аутентифицированный участник чата может читать его историю и вложения и писать в него; посторонний получает `404`. Списки содержат только доступные объекты. Не используйте `403` для раскрытия существования чужого чата. +- `GET /health/live` проверяет только жизнь процесса, возвращает `200 {"status":"ok"}`. `GET /health/ready` возвращает 200, если приложение может безопасно обслуживать обязательный синхронный путь; иначе 503 в общем формате ошибки. Какие зависимости критичны, зафиксируйте в отчёте; недоступность Grafana не должна останавливать API. + +## Пользователи, чаты и сообщения + +- `POST /users` публичен; возвращает `201 User`. Пароль/его hash и сессионные данные никогда не входят в User. +- `POST /chats` принимает title и явный список существующих `member_ids`, включая вызывающего. Дубликаты, неизвестные участники и отсутствие вызывающего — 400. Чат создаётся атомарно. GET списка чатов сортируется по `(created_at, id)` по возрастанию. +- Отправитель сообщения выводится из текущей идентификации, поле `sender_id` в запросе не принимается. Текст — до 4000 Unicode code points. Нельзя отправить пустой/пробельный текст без вложений. +- История использует keyset cursor: `(created_at, id)`, по возрастанию; `limit` — целое 1–100, по умолчанию 50. Ответ `{"items":[...],"next_cursor":null}`. Непустой cursor — непрозрачная для клиента строка; последний элемент предыдущей страницы не повторяется. Cursor привязан к ресурсу/запросу: от другого чата — 400. Изменять `limit` при продолжении можно. +- `next_cursor` равен null, если на момент чтения следующей записи нет. Гарантируется отсутствие повторов при обходе неизменной истории; snapshot изоляция между несколькими HTTP-запросами не требуется. При конкурентных вставках опишите выбранную семантику и ограничения часов. +- Подтверждённое сообщение видно следующему чтению через любую API-реплику. Допускается кэш, но он не должен возвращать устаревшую историю после успешной записи. Удаление и изменение участников в обязательный API не входят. + +## Идентификация и сессии + +**Lab1:** защищённые endpoint требуют `X-User-Id: `; отсутствие/неизвестный ID — 401. Это учебный выбор пользователя: любой клиент может выдать себя за другого. + +**Lab2–7:** `POST /auth/sessions` принимает только `Authorization: Basic `, без JSON body. Username ограничен ASCII; пароль кодируется UTF-8, 12–128 code points, двоеточие в пароле допустимо. Неверные credentials — одинаковый 401 и `WWW-Authenticate: Basic realm="messenger", charset="UTF-8"`. + +Вход возвращает `201 {"token":"...","token_type":"Bearer","expires_at":"...Z","user":{...}}`. Токен — непрозрачное случайное значение, не username/UUID и не обязательный JWT; энтропия не менее 256 бит. Храните серверную сессию с expiry; TTL задаётся `SESSION_TTL_SECONDS`, по умолчанию 3600 (в тесте истечения можно 2). В обычном smoke TTL должен быть не менее 300. + +Все защищённые endpoint принимают только `Authorization: Bearer `. Истёкший/отозванный/неизвестный токен — 401 с `WWW-Authenticate: Bearer realm="messenger"`. Basic на бизнес-endpoint — 401. `X-User-Id` с lab2 игнорируется: он не меняет владельца сессии и сам по себе не даёт доступ. `DELETE /auth/sessions/current` отзывает текущий токен и возвращает 204 без тела; повтор с отозванным токеном — 401. + +Пользователи, чаты, сообщения и ещё действительные сессии переживают перезапуск приложения и используемых хранилищ с сохранёнными volumes. TTL не продлевается из-за рестарта. Пароли уже с lab2 хешируются библиотечным password KDF (Argon2id, scrypt или bcrypt с учётом ограничений библиотеки), не SHA-256 и не plaintext. Если bcrypt не поддерживает весь диапазон UTF-8 паролей без усечения, выберите другую библиотечную схему. В lab7 обязателен Argon2id. + +Basic — это кодирование, не шифрование. HTTP lab1–6 допускается только для локального изолированного стенда с синтетическими данными; для удалённой демонстрации нужен TLS. Lab7 использует HTTPS обязательно. См. [RFC 7617](https://www.rfc-editor.org/info/rfc7617/). + +## Балансировка и повторная отправка (с lab3) + +- Каждый ответ, созданный API, содержит `X-Instance-Id`; значение стабильно для жизни процесса. Ошибки соединения, созданные самим LB, могут не иметь этого заголовка. В lab7 используйте случайный alias реплики без раскрытия hostname/IP. +- Для POST сообщения обязателен `Idempotency-Key` (ASCII `[A-Za-z0-9._:-]{1,128}`). Нет/неверный ключ — 400. Область уникальности: текущий пользователь + HTTP-метод + путь чата + ключ. +- При повторе того же валидного JSON в течение 24 часов сервер возвращает тот же код 201 и то же Message. Порядок ключей JSON и отсутствующий `attachment_ids` вместо `[]` семантически равны. Другой body при том же ключе — 409. Разные пользователи/чаты могут использовать одинаковый ключ независимо. +- Одновременные повторы через разные реплики создают ровно одно сообщение. Повтор снова проверяет действительность сессии и доступ. Транзиентные 5xx не фиксируются как окончательный результат; побочный эффект и запись результата должны быть атомарны. +- Ключ обязателен только для сообщений. Самостоятельный retry загрузки файла или создания чата может создать ещё один объект; не добавляйте прозрачные повторы этих POST без расширения контракта. + +## Вложения (с lab4) + +- `POST /chats/{chat_id}/attachments?filename=photo.png`: сырые bytes одного PNG/JPEG; `Content-Type: image/png` или `image/jpeg`. Не multipart. Максимум **10 MiB = 10 485 760 байт**. MIME проверяется по содержимому; заведомо повреждённые/чужие форматы — 415, превышение bytes — 413. Не более 40 миллионов декодированных пикселей (превышение — 413). Устанавливайте лимит до полной распаковки изображения. +- `filename` — отображаемое имя (1–255 code points). Не используйте его как путь/ключ S3. Имя с `/`, `\`, NUL или управляющими символами — 400. Bucket приватен; object key генерирует сервер. Оригинал возвращается побайтно, его lowercase SHA-256 и размер соответствуют исходному файлу. +- Lab4 возвращает `201 Attachment` со `status=ready`, `variants=[]`, `error_code=null` только после сохранения оригинала и метаданных. Lab5–7 возвращают `202 Attachment` со снимком `status=queued`; готовность определяется последующим GET метаданных. Быстрый worker может закончить до первого GET — это нормально. +- Файл заранее привязан к чату. В POST сообщения добавляется `attachment_ids` (0–10 уникальных ID, по умолчанию []). Можно отправить `text=""` с хотя бы одним вложением. Все вложения должны принадлежать этому чату; чужое/неизвестное — 404. Любой участник может ссылаться на доступное вложение чата. Failed-вложение прикреплять нельзя (409); queued/processing с lab5 можно. +- `GET /attachments/{id}` и `/content` доступны только участникам чата. Download возвращает байты с истинным Content-Type, через авторизованный API, **без redirect** в обязательном контракте. Приватный bucket сам по себе не заменяет эту проверку. `variant` по умолчанию original. Для queued/processing content — 409; для failed — 409. Отсутствующий вариант ready-объекта — 404. +- В ready-метаданных `error_code=null`. В failed — краткий машинный код (например `invalid_image`, `processing_failed`, `retry_exhausted`), без stack trace. `size_bytes`, `sha256`, `content_type` всегда описывают оригинал; у вариантов отдельные поля. Публичный API не раскрывает ключи бакета, внутренние адреса и credentials. +- Прямой upload/download по временной подписи — факультативное расширение. Его дополняют ограничение срока/размера, уникальный ключ и подтверждение реально загруженного объекта; он не заменяет стандартный endpoint. Особенности подписанных URL: [S3](https://docs.aws.amazon.com/AmazonS3/latest/userguide/using-presigned-url.html). + +## Фоновая обработка (с lab5) + +До 202 все байты приняты в **устойчивый staging**, а намерение обработать файл надёжно сохранено. HTTP-передача клиента не исчезает и не становится мгновенной. Staging — приватный S3-prefix либо общий persistent volume, доступный worker; локальная память/API ephemeral disk не подходит. Worker асинхронно переносит/публикует оригинал в конечный S3-prefix и создаёт варианты; если staging уже в S3, опишите эту границу честно. + +Обязательный thumbnail: JPEG, изображение вписано в 256×256 с сохранением пропорций, без увеличения маленьких исходников, EXIF orientation применяется; alpha компонуется на белый. Размеры округляются вниз, минимум 1 px. Fixture 320×200 должен дать 256×160. Статус ready ставится после сохранения оригинала и thumbnail. Crop на 4: JPEG, центрированный квадрат 128×128 (для маленьких исходников допускается увеличение). Optimized на 4: WebP, вписан в 1280×1280 без увеличения, параметры качества и удаления EXIF документируются; не обещайте уменьшение каждого маленького файла. + +Допустимые состояния и события описаны в `jobs.schema.json` и `attachment-states.md`. Доставка — at least once; обработка идемпотентна, повторы не размножают варианты. Финальные ошибки наблюдаемы, бесконечный немой retry недопустим. Обязательный smoke ждёт ready до 60 секунд; это бюджет локального smoke для одного файла при свободном worker, не универсальный production SLA. В очередь идут ссылки/ID, не бинарное содержимое. diff --git a/contracts/examples.json.lab4 b/contracts/examples.json.lab4 new file mode 100644 index 00000000..060abd0f --- /dev/null +++ b/contracts/examples.json.lab4 @@ -0,0 +1,60 @@ +{ + "CreateUser": { + "username": "alice", + "display_name": "Алиса", + "password": "Example-only-not-a-real-secret-123" + }, + "User": { + "id": "11111111-1111-4111-8111-111111111111", + "username": "alice", + "display_name": "Алиса", + "created_at": "2026-09-01T12:00:00Z" + }, + "CreateChat": { + "title": "Проект по ВНП", + "member_ids": [ + "11111111-1111-4111-8111-111111111111", + "22222222-2222-4222-8222-222222222222" + ] + }, + "Chat": { + "title": "Проект по ВНП", + "member_ids": [ + "11111111-1111-4111-8111-111111111111", + "22222222-2222-4222-8222-222222222222" + ], + "id": "33333333-3333-4333-8333-333333333333", + "created_at": "2026-09-01T12:00:00Z" + }, + "CreateMessage": { + "text": "Привет, Боб!" + }, + "Message": { + "id": "44444444-4444-4444-8444-444444444444", + "chat_id": "33333333-3333-4333-8333-333333333333", + "sender_id": "11111111-1111-4111-8111-111111111111", + "text": "Привет, Боб!", + "created_at": "2026-09-01T12:00:00Z", + "attachment_ids": [] + }, + "MessagePage": { + "items": [ + { + "id": "44444444-4444-4444-8444-444444444444", + "chat_id": "33333333-3333-4333-8333-333333333333", + "sender_id": "11111111-1111-4111-8111-111111111111", + "text": "Привет, Боб!", + "created_at": "2026-09-01T12:00:00Z", + "attachment_ids": [] + } + ], + "next_cursor": null + }, + "Error": { + "error": { + "code": "not_found", + "message": "Объект не найден", + "request_id": "example-request-id" + } + } +} diff --git a/contracts/infrastructure.json.lab4 b/contracts/infrastructure.json.lab4 new file mode 100644 index 00000000..dd2edf2d --- /dev/null +++ b/contracts/infrastructure.json.lab4 @@ -0,0 +1,46 @@ +{ + "lab": 4, + "kind": "requirements, not runnable deployment", + "required_for_grade_3": [ + { + "role": "api", + "count_min": 2, + "student_implements": true, + "persistent_local_state": false + }, + { + "role": "postgres", + "purpose": "users/chats/membership/messages/metadata", + "persistent_volume": true + }, + { + "role": "load_balancer", + "public_entrypoint": true, + "backend_readiness": true + }, + { + "role": "s3", + "choose_one": [ + "MinIO", + "SeaweedFS", + "RustFS" + ], + "private_bucket": true, + "persistent_volume": true + } + ], + "optional_components": [ + { + "role": "redis_or_valkey", + "required_for_lab2_grade4": true, + "note": "Choose one; if only store of sessions, configure durable persistence." + } + ], + "rules": [ + "Choose Compose or Swarm for lab3+; do not need both.", + "Never publish internal API replica ports in bypass of LB from lab3.", + "Versions/digests must be fixed in implementation.", + "Do not remove volumes in restart/failover checks.", + "Document service names, healthchecks, bootstrap/migrations and credentials." + ] +} diff --git a/contracts/openapi.json.lab4 b/contracts/openapi.json.lab4 new file mode 100644 index 00000000..138f07c0 --- /dev/null +++ b/contracts/openapi.json.lab4 @@ -0,0 +1,3506 @@ +{ + "openapi": "3.1.0", + "info": { + "title": "MTUSI Messenger — лабораторная 4", + "version": "1.4.0", + "description": "Нормативный минимум на 3. Семантика: contracts/README.md. Расширения на 4/5 не должны менять этот API. Наличие кода ошибки в схеме не требует реализовать факультативную причину его выдачи." + }, + "servers": [ + { + "url": "http://localhost:8080", + "description": "Локальный учебный стенд" + } + ], + "security": [ + { + "bearerAuth": [] + } + ], + "paths": { + "/health/live": { + "get": { + "operationId": "liveness", + "summary": "Процесс жив", + "responses": { + "200": { + "description": "Успех", + "headers": { + "X-Request-Id": { + "description": "Непустой идентификатор запроса; обязателен во всех ответах.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "X-Instance-Id": { + "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "schema": { + "type": "string", + "minLength": 1 + } + } + }, + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Health" + } + } + } + }, + "500": { + "description": "Неожиданная ошибка без внутренних деталей", + "headers": { + "X-Request-Id": { + "description": "Непустой идентификатор запроса; обязателен во всех ответах.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "X-Instance-Id": { + "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "schema": { + "type": "string", + "minLength": 1 + } + } + }, + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Error" + } + } + } + } + }, + "security": [] + } + }, + "/health/ready": { + "get": { + "operationId": "readiness", + "summary": "Готов принимать трафик", + "responses": { + "200": { + "description": "Успех", + "headers": { + "X-Request-Id": { + "description": "Непустой идентификатор запроса; обязателен во всех ответах.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "X-Instance-Id": { + "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "schema": { + "type": "string", + "minLength": 1 + } + } + }, + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Health" + } + } + } + }, + "503": { + "description": "Зависимость недоступна", + "headers": { + "X-Request-Id": { + "description": "Непустой идентификатор запроса; обязателен во всех ответах.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "X-Instance-Id": { + "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "schema": { + "type": "string", + "minLength": 1 + } + } + }, + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Error" + } + } + } + }, + "500": { + "description": "Неожиданная ошибка без внутренних деталей", + "headers": { + "X-Request-Id": { + "description": "Непустой идентификатор запроса; обязателен во всех ответах.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "X-Instance-Id": { + "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "schema": { + "type": "string", + "minLength": 1 + } + } + }, + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Error" + } + } + } + } + }, + "security": [] + } + }, + "/api/v1/users": { + "post": { + "operationId": "registerUser", + "summary": "Создать пользователя", + "responses": { + "201": { + "description": "Успех", + "headers": { + "X-Request-Id": { + "description": "Непустой идентификатор запроса; обязателен во всех ответах.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "X-Instance-Id": { + "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "schema": { + "type": "string", + "minLength": 1 + } + } + }, + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/User" + } + } + } + }, + "400": { + "description": "Неверный запрос", + "headers": { + "X-Request-Id": { + "description": "Непустой идентификатор запроса; обязателен во всех ответах.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "X-Instance-Id": { + "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "schema": { + "type": "string", + "minLength": 1 + } + } + }, + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Error" + } + } + } + }, + "409": { + "description": "Конфликт", + "headers": { + "X-Request-Id": { + "description": "Непустой идентификатор запроса; обязателен во всех ответах.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "X-Instance-Id": { + "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "schema": { + "type": "string", + "minLength": 1 + } + } + }, + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Error" + } + } + } + }, + "429": { + "description": "Ограничение частоты запросов", + "headers": { + "X-Request-Id": { + "description": "Непустой идентификатор запроса; обязателен во всех ответах.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "X-Instance-Id": { + "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "Retry-After": { + "description": "Положительное число секунд.", + "schema": { + "type": "integer", + "minimum": 1 + } + } + }, + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Error" + } + } + } + }, + "503": { + "description": "Зависимость недоступна", + "headers": { + "X-Request-Id": { + "description": "Непустой идентификатор запроса; обязателен во всех ответах.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "X-Instance-Id": { + "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "schema": { + "type": "string", + "minLength": 1 + } + } + }, + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Error" + } + } + } + }, + "500": { + "description": "Неожиданная ошибка без внутренних деталей", + "headers": { + "X-Request-Id": { + "description": "Непустой идентификатор запроса; обязателен во всех ответах.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "X-Instance-Id": { + "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "schema": { + "type": "string", + "minLength": 1 + } + } + }, + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Error" + } + } + } + } + }, + "security": [], + "requestBody": { + "required": true, + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/CreateUser" + } + } + } + } + } + }, + "/api/v1/users/me": { + "get": { + "operationId": "currentUser", + "summary": "Текущий пользователь", + "responses": { + "200": { + "description": "Успех", + "headers": { + "X-Request-Id": { + "description": "Непустой идентификатор запроса; обязателен во всех ответах.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "X-Instance-Id": { + "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "schema": { + "type": "string", + "minLength": 1 + } + } + }, + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/User" + } + } + } + }, + "400": { + "description": "Неверный запрос", + "headers": { + "X-Request-Id": { + "description": "Непустой идентификатор запроса; обязателен во всех ответах.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "X-Instance-Id": { + "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "schema": { + "type": "string", + "minLength": 1 + } + } + }, + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Error" + } + } + } + }, + "401": { + "description": "Нет действительной аутентификации", + "headers": { + "X-Request-Id": { + "description": "Непустой идентификатор запроса; обязателен во всех ответах.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "X-Instance-Id": { + "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "WWW-Authenticate": { + "description": "Basic realm=\"messenger\", charset=\"UTF-8\" для входа; Bearer realm=\"messenger\" для защищённого API lab2+.", + "schema": { + "type": "string" + } + } + }, + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Error" + } + } + } + }, + "404": { + "description": "Объект отсутствует или недоступен", + "headers": { + "X-Request-Id": { + "description": "Непустой идентификатор запроса; обязателен во всех ответах.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "X-Instance-Id": { + "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "schema": { + "type": "string", + "minLength": 1 + } + } + }, + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Error" + } + } + } + }, + "409": { + "description": "Конфликт", + "headers": { + "X-Request-Id": { + "description": "Непустой идентификатор запроса; обязателен во всех ответах.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "X-Instance-Id": { + "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "schema": { + "type": "string", + "minLength": 1 + } + } + }, + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Error" + } + } + } + }, + "429": { + "description": "Ограничение частоты запросов", + "headers": { + "X-Request-Id": { + "description": "Непустой идентификатор запроса; обязателен во всех ответах.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "X-Instance-Id": { + "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "Retry-After": { + "description": "Положительное число секунд.", + "schema": { + "type": "integer", + "minimum": 1 + } + } + }, + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Error" + } + } + } + }, + "503": { + "description": "Зависимость недоступна", + "headers": { + "X-Request-Id": { + "description": "Непустой идентификатор запроса; обязателен во всех ответах.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "X-Instance-Id": { + "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "schema": { + "type": "string", + "minLength": 1 + } + } + }, + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Error" + } + } + } + }, + "500": { + "description": "Неожиданная ошибка без внутренних деталей", + "headers": { + "X-Request-Id": { + "description": "Непустой идентификатор запроса; обязателен во всех ответах.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "X-Instance-Id": { + "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "schema": { + "type": "string", + "minLength": 1 + } + } + }, + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Error" + } + } + } + } + } + } + }, + "/api/v1/chats": { + "post": { + "operationId": "createChat", + "summary": "Создать чат; вызывающий должен входить в member_ids", + "responses": { + "201": { + "description": "Успех", + "headers": { + "X-Request-Id": { + "description": "Непустой идентификатор запроса; обязателен во всех ответах.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "X-Instance-Id": { + "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "schema": { + "type": "string", + "minLength": 1 + } + } + }, + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Chat" + } + } + } + }, + "400": { + "description": "Неверный запрос", + "headers": { + "X-Request-Id": { + "description": "Непустой идентификатор запроса; обязателен во всех ответах.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "X-Instance-Id": { + "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "schema": { + "type": "string", + "minLength": 1 + } + } + }, + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Error" + } + } + } + }, + "401": { + "description": "Нет действительной аутентификации", + "headers": { + "X-Request-Id": { + "description": "Непустой идентификатор запроса; обязателен во всех ответах.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "X-Instance-Id": { + "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "WWW-Authenticate": { + "description": "Basic realm=\"messenger\", charset=\"UTF-8\" для входа; Bearer realm=\"messenger\" для защищённого API lab2+.", + "schema": { + "type": "string" + } + } + }, + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Error" + } + } + } + }, + "404": { + "description": "Объект отсутствует или недоступен", + "headers": { + "X-Request-Id": { + "description": "Непустой идентификатор запроса; обязателен во всех ответах.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "X-Instance-Id": { + "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "schema": { + "type": "string", + "minLength": 1 + } + } + }, + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Error" + } + } + } + }, + "409": { + "description": "Конфликт", + "headers": { + "X-Request-Id": { + "description": "Непустой идентификатор запроса; обязателен во всех ответах.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "X-Instance-Id": { + "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "schema": { + "type": "string", + "minLength": 1 + } + } + }, + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Error" + } + } + } + }, + "429": { + "description": "Ограничение частоты запросов", + "headers": { + "X-Request-Id": { + "description": "Непустой идентификатор запроса; обязателен во всех ответах.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "X-Instance-Id": { + "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "Retry-After": { + "description": "Положительное число секунд.", + "schema": { + "type": "integer", + "minimum": 1 + } + } + }, + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Error" + } + } + } + }, + "503": { + "description": "Зависимость недоступна", + "headers": { + "X-Request-Id": { + "description": "Непустой идентификатор запроса; обязателен во всех ответах.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "X-Instance-Id": { + "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "schema": { + "type": "string", + "minLength": 1 + } + } + }, + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Error" + } + } + } + }, + "500": { + "description": "Неожиданная ошибка без внутренних деталей", + "headers": { + "X-Request-Id": { + "description": "Непустой идентификатор запроса; обязателен во всех ответах.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "X-Instance-Id": { + "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "schema": { + "type": "string", + "minLength": 1 + } + } + }, + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Error" + } + } + } + } + }, + "requestBody": { + "required": true, + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/CreateChat" + } + } + } + } + }, + "get": { + "operationId": "listChats", + "summary": "Чаты текущего пользователя", + "responses": { + "200": { + "description": "Успех", + "headers": { + "X-Request-Id": { + "description": "Непустой идентификатор запроса; обязателен во всех ответах.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "X-Instance-Id": { + "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "schema": { + "type": "string", + "minLength": 1 + } + } + }, + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/ChatPage" + } + } + } + }, + "400": { + "description": "Неверный запрос", + "headers": { + "X-Request-Id": { + "description": "Непустой идентификатор запроса; обязателен во всех ответах.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "X-Instance-Id": { + "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "schema": { + "type": "string", + "minLength": 1 + } + } + }, + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Error" + } + } + } + }, + "401": { + "description": "Нет действительной аутентификации", + "headers": { + "X-Request-Id": { + "description": "Непустой идентификатор запроса; обязателен во всех ответах.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "X-Instance-Id": { + "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "WWW-Authenticate": { + "description": "Basic realm=\"messenger\", charset=\"UTF-8\" для входа; Bearer realm=\"messenger\" для защищённого API lab2+.", + "schema": { + "type": "string" + } + } + }, + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Error" + } + } + } + }, + "404": { + "description": "Объект отсутствует или недоступен", + "headers": { + "X-Request-Id": { + "description": "Непустой идентификатор запроса; обязателен во всех ответах.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "X-Instance-Id": { + "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "schema": { + "type": "string", + "minLength": 1 + } + } + }, + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Error" + } + } + } + }, + "409": { + "description": "Конфликт", + "headers": { + "X-Request-Id": { + "description": "Непустой идентификатор запроса; обязателен во всех ответах.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "X-Instance-Id": { + "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "schema": { + "type": "string", + "minLength": 1 + } + } + }, + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Error" + } + } + } + }, + "429": { + "description": "Ограничение частоты запросов", + "headers": { + "X-Request-Id": { + "description": "Непустой идентификатор запроса; обязателен во всех ответах.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "X-Instance-Id": { + "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "Retry-After": { + "description": "Положительное число секунд.", + "schema": { + "type": "integer", + "minimum": 1 + } + } + }, + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Error" + } + } + } + }, + "503": { + "description": "Зависимость недоступна", + "headers": { + "X-Request-Id": { + "description": "Непустой идентификатор запроса; обязателен во всех ответах.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "X-Instance-Id": { + "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "schema": { + "type": "string", + "minLength": 1 + } + } + }, + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Error" + } + } + } + }, + "500": { + "description": "Неожиданная ошибка без внутренних деталей", + "headers": { + "X-Request-Id": { + "description": "Непустой идентификатор запроса; обязателен во всех ответах.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "X-Instance-Id": { + "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "schema": { + "type": "string", + "minLength": 1 + } + } + }, + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Error" + } + } + } + } + }, + "parameters": [ + { + "name": "limit", + "in": "query", + "schema": { + "type": "integer", + "minimum": 1, + "maximum": 100, + "default": 50 + } + }, + { + "name": "cursor", + "in": "query", + "schema": { + "type": "string", + "minLength": 1 + } + } + ] + } + }, + "/api/v1/chats/{chat_id}": { + "get": { + "operationId": "getChat", + "summary": "Получить доступный чат", + "responses": { + "200": { + "description": "Успех", + "headers": { + "X-Request-Id": { + "description": "Непустой идентификатор запроса; обязателен во всех ответах.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "X-Instance-Id": { + "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "schema": { + "type": "string", + "minLength": 1 + } + } + }, + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Chat" + } + } + } + }, + "400": { + "description": "Неверный запрос", + "headers": { + "X-Request-Id": { + "description": "Непустой идентификатор запроса; обязателен во всех ответах.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "X-Instance-Id": { + "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "schema": { + "type": "string", + "minLength": 1 + } + } + }, + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Error" + } + } + } + }, + "401": { + "description": "Нет действительной аутентификации", + "headers": { + "X-Request-Id": { + "description": "Непустой идентификатор запроса; обязателен во всех ответах.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "X-Instance-Id": { + "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "WWW-Authenticate": { + "description": "Basic realm=\"messenger\", charset=\"UTF-8\" для входа; Bearer realm=\"messenger\" для защищённого API lab2+.", + "schema": { + "type": "string" + } + } + }, + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Error" + } + } + } + }, + "404": { + "description": "Объект отсутствует или недоступен", + "headers": { + "X-Request-Id": { + "description": "Непустой идентификатор запроса; обязателен во всех ответах.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "X-Instance-Id": { + "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "schema": { + "type": "string", + "minLength": 1 + } + } + }, + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Error" + } + } + } + }, + "409": { + "description": "Конфликт", + "headers": { + "X-Request-Id": { + "description": "Непустой идентификатор запроса; обязателен во всех ответах.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "X-Instance-Id": { + "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "schema": { + "type": "string", + "minLength": 1 + } + } + }, + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Error" + } + } + } + }, + "429": { + "description": "Ограничение частоты запросов", + "headers": { + "X-Request-Id": { + "description": "Непустой идентификатор запроса; обязателен во всех ответах.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "X-Instance-Id": { + "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "Retry-After": { + "description": "Положительное число секунд.", + "schema": { + "type": "integer", + "minimum": 1 + } + } + }, + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Error" + } + } + } + }, + "503": { + "description": "Зависимость недоступна", + "headers": { + "X-Request-Id": { + "description": "Непустой идентификатор запроса; обязателен во всех ответах.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "X-Instance-Id": { + "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "schema": { + "type": "string", + "minLength": 1 + } + } + }, + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Error" + } + } + } + }, + "500": { + "description": "Неожиданная ошибка без внутренних деталей", + "headers": { + "X-Request-Id": { + "description": "Непустой идентификатор запроса; обязателен во всех ответах.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "X-Instance-Id": { + "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "schema": { + "type": "string", + "minLength": 1 + } + } + }, + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Error" + } + } + } + } + }, + "parameters": [ + { + "name": "chat_id", + "in": "path", + "required": true, + "schema": { + "type": "string", + "format": "uuid" + } + } + ] + } + }, + "/api/v1/chats/{chat_id}/messages": { + "post": { + "operationId": "createMessage", + "summary": "Отправить сообщение", + "responses": { + "201": { + "description": "Успех", + "headers": { + "X-Request-Id": { + "description": "Непустой идентификатор запроса; обязателен во всех ответах.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "X-Instance-Id": { + "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "schema": { + "type": "string", + "minLength": 1 + } + } + }, + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Message" + } + } + } + }, + "400": { + "description": "Неверный запрос", + "headers": { + "X-Request-Id": { + "description": "Непустой идентификатор запроса; обязателен во всех ответах.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "X-Instance-Id": { + "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "schema": { + "type": "string", + "minLength": 1 + } + } + }, + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Error" + } + } + } + }, + "401": { + "description": "Нет действительной аутентификации", + "headers": { + "X-Request-Id": { + "description": "Непустой идентификатор запроса; обязателен во всех ответах.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "X-Instance-Id": { + "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "WWW-Authenticate": { + "description": "Basic realm=\"messenger\", charset=\"UTF-8\" для входа; Bearer realm=\"messenger\" для защищённого API lab2+.", + "schema": { + "type": "string" + } + } + }, + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Error" + } + } + } + }, + "404": { + "description": "Объект отсутствует или недоступен", + "headers": { + "X-Request-Id": { + "description": "Непустой идентификатор запроса; обязателен во всех ответах.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "X-Instance-Id": { + "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "schema": { + "type": "string", + "minLength": 1 + } + } + }, + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Error" + } + } + } + }, + "409": { + "description": "Конфликт", + "headers": { + "X-Request-Id": { + "description": "Непустой идентификатор запроса; обязателен во всех ответах.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "X-Instance-Id": { + "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "schema": { + "type": "string", + "minLength": 1 + } + } + }, + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Error" + } + } + } + }, + "429": { + "description": "Ограничение частоты запросов", + "headers": { + "X-Request-Id": { + "description": "Непустой идентификатор запроса; обязателен во всех ответах.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "X-Instance-Id": { + "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "Retry-After": { + "description": "Положительное число секунд.", + "schema": { + "type": "integer", + "minimum": 1 + } + } + }, + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Error" + } + } + } + }, + "503": { + "description": "Зависимость недоступна", + "headers": { + "X-Request-Id": { + "description": "Непустой идентификатор запроса; обязателен во всех ответах.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "X-Instance-Id": { + "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "schema": { + "type": "string", + "minLength": 1 + } + } + }, + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Error" + } + } + } + }, + "500": { + "description": "Неожиданная ошибка без внутренних деталей", + "headers": { + "X-Request-Id": { + "description": "Непустой идентификатор запроса; обязателен во всех ответах.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "X-Instance-Id": { + "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "schema": { + "type": "string", + "minLength": 1 + } + } + }, + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Error" + } + } + } + } + }, + "parameters": [ + { + "name": "chat_id", + "in": "path", + "required": true, + "schema": { + "type": "string", + "format": "uuid" + } + }, + { + "name": "Idempotency-Key", + "in": "header", + "required": true, + "schema": { + "type": "string", + "minLength": 1, + "maxLength": 128, + "pattern": "^[A-Za-z0-9._:-]+$" + }, + "description": "Обязателен с lab3. Область: пользователь + метод + путь + ключ; хранить результат не менее 24 ч. Повтор того же JSON возвращает исходный 201 и то же сообщение, другой JSON — 409." + } + ], + "requestBody": { + "required": true, + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/CreateMessage" + } + } + } + } + }, + "get": { + "operationId": "listMessages", + "summary": "Сообщения по (created_at, id) по возрастанию", + "responses": { + "200": { + "description": "Успех", + "headers": { + "X-Request-Id": { + "description": "Непустой идентификатор запроса; обязателен во всех ответах.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "X-Instance-Id": { + "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "schema": { + "type": "string", + "minLength": 1 + } + } + }, + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/MessagePage" + } + } + } + }, + "400": { + "description": "Неверный запрос", + "headers": { + "X-Request-Id": { + "description": "Непустой идентификатор запроса; обязателен во всех ответах.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "X-Instance-Id": { + "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "schema": { + "type": "string", + "minLength": 1 + } + } + }, + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Error" + } + } + } + }, + "401": { + "description": "Нет действительной аутентификации", + "headers": { + "X-Request-Id": { + "description": "Непустой идентификатор запроса; обязателен во всех ответах.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "X-Instance-Id": { + "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "WWW-Authenticate": { + "description": "Basic realm=\"messenger\", charset=\"UTF-8\" для входа; Bearer realm=\"messenger\" для защищённого API lab2+.", + "schema": { + "type": "string" + } + } + }, + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Error" + } + } + } + }, + "404": { + "description": "Объект отсутствует или недоступен", + "headers": { + "X-Request-Id": { + "description": "Непустой идентификатор запроса; обязателен во всех ответах.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "X-Instance-Id": { + "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "schema": { + "type": "string", + "minLength": 1 + } + } + }, + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Error" + } + } + } + }, + "409": { + "description": "Конфликт", + "headers": { + "X-Request-Id": { + "description": "Непустой идентификатор запроса; обязателен во всех ответах.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "X-Instance-Id": { + "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "schema": { + "type": "string", + "minLength": 1 + } + } + }, + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Error" + } + } + } + }, + "429": { + "description": "Ограничение частоты запросов", + "headers": { + "X-Request-Id": { + "description": "Непустой идентификатор запроса; обязателен во всех ответах.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "X-Instance-Id": { + "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "Retry-After": { + "description": "Положительное число секунд.", + "schema": { + "type": "integer", + "minimum": 1 + } + } + }, + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Error" + } + } + } + }, + "503": { + "description": "Зависимость недоступна", + "headers": { + "X-Request-Id": { + "description": "Непустой идентификатор запроса; обязателен во всех ответах.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "X-Instance-Id": { + "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "schema": { + "type": "string", + "minLength": 1 + } + } + }, + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Error" + } + } + } + }, + "500": { + "description": "Неожиданная ошибка без внутренних деталей", + "headers": { + "X-Request-Id": { + "description": "Непустой идентификатор запроса; обязателен во всех ответах.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "X-Instance-Id": { + "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "schema": { + "type": "string", + "minLength": 1 + } + } + }, + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Error" + } + } + } + } + }, + "parameters": [ + { + "name": "chat_id", + "in": "path", + "required": true, + "schema": { + "type": "string", + "format": "uuid" + } + }, + { + "name": "limit", + "in": "query", + "schema": { + "type": "integer", + "minimum": 1, + "maximum": 100, + "default": 50 + } + }, + { + "name": "cursor", + "in": "query", + "schema": { + "type": "string", + "minLength": 1 + } + } + ] + } + }, + "/api/v1/auth/sessions": { + "post": { + "operationId": "createSession", + "summary": "Вход: Basic username:password; выдаёт сессию", + "responses": { + "201": { + "description": "Успех", + "headers": { + "X-Request-Id": { + "description": "Непустой идентификатор запроса; обязателен во всех ответах.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "X-Instance-Id": { + "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "schema": { + "type": "string", + "minLength": 1 + } + } + }, + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Session" + } + } + } + }, + "400": { + "description": "Неверный запрос", + "headers": { + "X-Request-Id": { + "description": "Непустой идентификатор запроса; обязателен во всех ответах.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "X-Instance-Id": { + "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "schema": { + "type": "string", + "minLength": 1 + } + } + }, + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Error" + } + } + } + }, + "401": { + "description": "Нет действительной аутентификации", + "headers": { + "X-Request-Id": { + "description": "Непустой идентификатор запроса; обязателен во всех ответах.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "X-Instance-Id": { + "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "WWW-Authenticate": { + "description": "Basic realm=\"messenger\", charset=\"UTF-8\" для входа; Bearer realm=\"messenger\" для защищённого API lab2+.", + "schema": { + "type": "string" + } + } + }, + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Error" + } + } + } + }, + "429": { + "description": "Ограничение частоты запросов", + "headers": { + "X-Request-Id": { + "description": "Непустой идентификатор запроса; обязателен во всех ответах.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "X-Instance-Id": { + "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "Retry-After": { + "description": "Положительное число секунд.", + "schema": { + "type": "integer", + "minimum": 1 + } + } + }, + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Error" + } + } + } + }, + "503": { + "description": "Зависимость недоступна", + "headers": { + "X-Request-Id": { + "description": "Непустой идентификатор запроса; обязателен во всех ответах.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "X-Instance-Id": { + "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "schema": { + "type": "string", + "minLength": 1 + } + } + }, + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Error" + } + } + } + }, + "500": { + "description": "Неожиданная ошибка без внутренних деталей", + "headers": { + "X-Request-Id": { + "description": "Непустой идентификатор запроса; обязателен во всех ответах.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "X-Instance-Id": { + "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "schema": { + "type": "string", + "minLength": 1 + } + } + }, + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Error" + } + } + } + } + }, + "security": [ + { + "basicAuth": [] + } + ] + } + }, + "/api/v1/auth/sessions/current": { + "delete": { + "operationId": "revokeSession", + "summary": "Отозвать текущую сессию", + "responses": { + "204": { + "description": "Успех", + "headers": { + "X-Request-Id": { + "description": "Непустой идентификатор запроса; обязателен во всех ответах.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "X-Instance-Id": { + "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "schema": { + "type": "string", + "minLength": 1 + } + } + } + }, + "401": { + "description": "Нет действительной аутентификации", + "headers": { + "X-Request-Id": { + "description": "Непустой идентификатор запроса; обязателен во всех ответах.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "X-Instance-Id": { + "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "WWW-Authenticate": { + "description": "Basic realm=\"messenger\", charset=\"UTF-8\" для входа; Bearer realm=\"messenger\" для защищённого API lab2+.", + "schema": { + "type": "string" + } + } + }, + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Error" + } + } + } + }, + "429": { + "description": "Ограничение частоты запросов", + "headers": { + "X-Request-Id": { + "description": "Непустой идентификатор запроса; обязателен во всех ответах.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "X-Instance-Id": { + "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "Retry-After": { + "description": "Положительное число секунд.", + "schema": { + "type": "integer", + "minimum": 1 + } + } + }, + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Error" + } + } + } + }, + "503": { + "description": "Зависимость недоступна", + "headers": { + "X-Request-Id": { + "description": "Непустой идентификатор запроса; обязателен во всех ответах.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "X-Instance-Id": { + "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "schema": { + "type": "string", + "minLength": 1 + } + } + }, + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Error" + } + } + } + }, + "500": { + "description": "Неожиданная ошибка без внутренних деталей", + "headers": { + "X-Request-Id": { + "description": "Непустой идентификатор запроса; обязателен во всех ответах.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "X-Instance-Id": { + "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "schema": { + "type": "string", + "minLength": 1 + } + } + }, + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Error" + } + } + } + } + } + } + }, + "/api/v1/chats/{chat_id}/attachments": { + "post": { + "operationId": "uploadAttachment", + "summary": "Принять один PNG/JPEG, до 10 MiB", + "responses": { + "201": { + "description": "Успех", + "headers": { + "X-Request-Id": { + "description": "Непустой идентификатор запроса; обязателен во всех ответах.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "X-Instance-Id": { + "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "schema": { + "type": "string", + "minLength": 1 + } + } + }, + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Attachment" + } + } + } + }, + "400": { + "description": "Неверный запрос", + "headers": { + "X-Request-Id": { + "description": "Непустой идентификатор запроса; обязателен во всех ответах.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "X-Instance-Id": { + "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "schema": { + "type": "string", + "minLength": 1 + } + } + }, + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Error" + } + } + } + }, + "401": { + "description": "Нет действительной аутентификации", + "headers": { + "X-Request-Id": { + "description": "Непустой идентификатор запроса; обязателен во всех ответах.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "X-Instance-Id": { + "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "WWW-Authenticate": { + "description": "Basic realm=\"messenger\", charset=\"UTF-8\" для входа; Bearer realm=\"messenger\" для защищённого API lab2+.", + "schema": { + "type": "string" + } + } + }, + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Error" + } + } + } + }, + "404": { + "description": "Объект отсутствует или недоступен", + "headers": { + "X-Request-Id": { + "description": "Непустой идентификатор запроса; обязателен во всех ответах.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "X-Instance-Id": { + "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "schema": { + "type": "string", + "minLength": 1 + } + } + }, + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Error" + } + } + } + }, + "413": { + "description": "Превышен лимит", + "headers": { + "X-Request-Id": { + "description": "Непустой идентификатор запроса; обязателен во всех ответах.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "X-Instance-Id": { + "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "schema": { + "type": "string", + "minLength": 1 + } + } + }, + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Error" + } + } + } + }, + "415": { + "description": "Неподдерживаемый тип", + "headers": { + "X-Request-Id": { + "description": "Непустой идентификатор запроса; обязателен во всех ответах.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "X-Instance-Id": { + "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "schema": { + "type": "string", + "minLength": 1 + } + } + }, + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Error" + } + } + } + }, + "429": { + "description": "Ограничение частоты запросов", + "headers": { + "X-Request-Id": { + "description": "Непустой идентификатор запроса; обязателен во всех ответах.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "X-Instance-Id": { + "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "Retry-After": { + "description": "Положительное число секунд.", + "schema": { + "type": "integer", + "minimum": 1 + } + } + }, + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Error" + } + } + } + }, + "503": { + "description": "Зависимость недоступна", + "headers": { + "X-Request-Id": { + "description": "Непустой идентификатор запроса; обязателен во всех ответах.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "X-Instance-Id": { + "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "schema": { + "type": "string", + "minLength": 1 + } + } + }, + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Error" + } + } + } + }, + "500": { + "description": "Неожиданная ошибка без внутренних деталей", + "headers": { + "X-Request-Id": { + "description": "Непустой идентификатор запроса; обязателен во всех ответах.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "X-Instance-Id": { + "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "schema": { + "type": "string", + "minLength": 1 + } + } + }, + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Error" + } + } + } + } + }, + "parameters": [ + { + "name": "chat_id", + "in": "path", + "required": true, + "schema": { + "type": "string", + "format": "uuid" + } + }, + { + "name": "filename", + "in": "query", + "required": true, + "schema": { + "type": "string", + "minLength": 1, + "maxLength": 255 + } + } + ], + "requestBody": { + "required": true, + "description": "Сырые байты изображения, НЕ multipart и НЕ JSON/base64.", + "content": { + "image/png": { + "schema": { + "type": "string", + "format": "binary" + } + }, + "image/jpeg": { + "schema": { + "type": "string", + "format": "binary" + } + } + } + } + } + }, + "/api/v1/attachments/{attachment_id}": { + "get": { + "operationId": "getAttachment", + "summary": "Метаданные вложения, доступ участникам чата", + "responses": { + "200": { + "description": "Успех", + "headers": { + "X-Request-Id": { + "description": "Непустой идентификатор запроса; обязателен во всех ответах.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "X-Instance-Id": { + "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "schema": { + "type": "string", + "minLength": 1 + } + } + }, + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Attachment" + } + } + } + }, + "400": { + "description": "Неверный запрос", + "headers": { + "X-Request-Id": { + "description": "Непустой идентификатор запроса; обязателен во всех ответах.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "X-Instance-Id": { + "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "schema": { + "type": "string", + "minLength": 1 + } + } + }, + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Error" + } + } + } + }, + "401": { + "description": "Нет действительной аутентификации", + "headers": { + "X-Request-Id": { + "description": "Непустой идентификатор запроса; обязателен во всех ответах.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "X-Instance-Id": { + "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "WWW-Authenticate": { + "description": "Basic realm=\"messenger\", charset=\"UTF-8\" для входа; Bearer realm=\"messenger\" для защищённого API lab2+.", + "schema": { + "type": "string" + } + } + }, + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Error" + } + } + } + }, + "404": { + "description": "Объект отсутствует или недоступен", + "headers": { + "X-Request-Id": { + "description": "Непустой идентификатор запроса; обязателен во всех ответах.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "X-Instance-Id": { + "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "schema": { + "type": "string", + "minLength": 1 + } + } + }, + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Error" + } + } + } + }, + "409": { + "description": "Конфликт", + "headers": { + "X-Request-Id": { + "description": "Непустой идентификатор запроса; обязателен во всех ответах.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "X-Instance-Id": { + "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "schema": { + "type": "string", + "minLength": 1 + } + } + }, + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Error" + } + } + } + }, + "429": { + "description": "Ограничение частоты запросов", + "headers": { + "X-Request-Id": { + "description": "Непустой идентификатор запроса; обязателен во всех ответах.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "X-Instance-Id": { + "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "Retry-After": { + "description": "Положительное число секунд.", + "schema": { + "type": "integer", + "minimum": 1 + } + } + }, + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Error" + } + } + } + }, + "503": { + "description": "Зависимость недоступна", + "headers": { + "X-Request-Id": { + "description": "Непустой идентификатор запроса; обязателен во всех ответах.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "X-Instance-Id": { + "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "schema": { + "type": "string", + "minLength": 1 + } + } + }, + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Error" + } + } + } + }, + "500": { + "description": "Неожиданная ошибка без внутренних деталей", + "headers": { + "X-Request-Id": { + "description": "Непустой идентификатор запроса; обязателен во всех ответах.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "X-Instance-Id": { + "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "schema": { + "type": "string", + "minLength": 1 + } + } + }, + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Error" + } + } + } + } + }, + "parameters": [ + { + "name": "attachment_id", + "in": "path", + "required": true, + "schema": { + "type": "string", + "format": "uuid" + } + } + ] + } + }, + "/api/v1/attachments/{attachment_id}/content": { + "get": { + "operationId": "downloadAttachment", + "summary": "Скачать байты через авторизованный API", + "responses": { + "200": { + "description": "Успех", + "headers": { + "X-Request-Id": { + "description": "Непустой идентификатор запроса; обязателен во всех ответах.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "X-Instance-Id": { + "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "schema": { + "type": "string", + "minLength": 1 + } + } + }, + "content": { + "image/png": { + "schema": { + "type": "string", + "format": "binary" + } + }, + "image/jpeg": { + "schema": { + "type": "string", + "format": "binary" + } + }, + "image/webp": { + "schema": { + "type": "string", + "format": "binary" + } + } + } + }, + "400": { + "description": "Неверный запрос", + "headers": { + "X-Request-Id": { + "description": "Непустой идентификатор запроса; обязателен во всех ответах.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "X-Instance-Id": { + "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "schema": { + "type": "string", + "minLength": 1 + } + } + }, + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Error" + } + } + } + }, + "401": { + "description": "Нет действительной аутентификации", + "headers": { + "X-Request-Id": { + "description": "Непустой идентификатор запроса; обязателен во всех ответах.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "X-Instance-Id": { + "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "WWW-Authenticate": { + "description": "Basic realm=\"messenger\", charset=\"UTF-8\" для входа; Bearer realm=\"messenger\" для защищённого API lab2+.", + "schema": { + "type": "string" + } + } + }, + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Error" + } + } + } + }, + "404": { + "description": "Объект отсутствует или недоступен", + "headers": { + "X-Request-Id": { + "description": "Непустой идентификатор запроса; обязателен во всех ответах.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "X-Instance-Id": { + "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "schema": { + "type": "string", + "minLength": 1 + } + } + }, + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Error" + } + } + } + }, + "409": { + "description": "Конфликт", + "headers": { + "X-Request-Id": { + "description": "Непустой идентификатор запроса; обязателен во всех ответах.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "X-Instance-Id": { + "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "schema": { + "type": "string", + "minLength": 1 + } + } + }, + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Error" + } + } + } + }, + "429": { + "description": "Ограничение частоты запросов", + "headers": { + "X-Request-Id": { + "description": "Непустой идентификатор запроса; обязателен во всех ответах.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "X-Instance-Id": { + "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "Retry-After": { + "description": "Положительное число секунд.", + "schema": { + "type": "integer", + "minimum": 1 + } + } + }, + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Error" + } + } + } + }, + "503": { + "description": "Зависимость недоступна", + "headers": { + "X-Request-Id": { + "description": "Непустой идентификатор запроса; обязателен во всех ответах.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "X-Instance-Id": { + "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "schema": { + "type": "string", + "minLength": 1 + } + } + }, + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Error" + } + } + } + }, + "500": { + "description": "Неожиданная ошибка без внутренних деталей", + "headers": { + "X-Request-Id": { + "description": "Непустой идентификатор запроса; обязателен во всех ответах.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "X-Instance-Id": { + "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "schema": { + "type": "string", + "minLength": 1 + } + } + }, + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Error" + } + } + } + } + }, + "parameters": [ + { + "name": "attachment_id", + "in": "path", + "required": true, + "schema": { + "type": "string", + "format": "uuid" + } + }, + { + "name": "variant", + "in": "query", + "schema": { + "type": "string", + "enum": [ + "original" + ], + "default": "original" + } + } + ] + } + } + }, + "components": { + "securitySchemes": { + "basicAuth": { + "type": "http", + "scheme": "basic" + }, + "bearerAuth": { + "type": "http", + "scheme": "bearer", + "bearerFormat": "opaque session token" + } + }, + "schemas": { + "Error": { + "type": "object", + "properties": { + "error": { + "type": "object", + "properties": { + "code": { + "type": "string", + "enum": [ + "invalid_request", + "unauthorized", + "not_found", + "conflict", + "payload_too_large", + "unsupported_media_type", + "rate_limited", + "unavailable", + "internal_error" + ] + }, + "message": { + "type": "string", + "minLength": 1 + }, + "request_id": { + "type": "string", + "minLength": 1 + } + }, + "required": [ + "code", + "message", + "request_id" + ], + "additionalProperties": false + } + }, + "required": [ + "error" + ], + "additionalProperties": false, + "examples": [ + { + "error": { + "code": "not_found", + "message": "Объект не найден", + "request_id": "example-request-id" + } + } + ] + }, + "Health": { + "type": "object", + "properties": { + "status": { + "type": "string", + "enum": [ + "ok" + ] + } + }, + "required": [ + "status" + ], + "additionalProperties": false + }, + "User": { + "type": "object", + "properties": { + "id": { + "type": "string", + "format": "uuid" + }, + "username": { + "type": "string", + "pattern": "^[a-z0-9_]{3,32}$" + }, + "display_name": { + "type": "string", + "minLength": 1, + "maxLength": 100 + }, + "created_at": { + "type": "string", + "format": "date-time" + } + }, + "required": [ + "id", + "username", + "display_name", + "created_at" + ], + "additionalProperties": false, + "examples": [ + { + "id": "11111111-1111-4111-8111-111111111111", + "username": "alice", + "display_name": "Алиса", + "created_at": "2026-09-01T12:00:00Z" + } + ] + }, + "CreateUser": { + "type": "object", + "properties": { + "username": { + "type": "string", + "pattern": "^[a-z0-9_]{3,32}$" + }, + "display_name": { + "type": "string", + "minLength": 1, + "maxLength": 100 + }, + "password": { + "type": "string", + "minLength": 12, + "maxLength": 128, + "writeOnly": true + } + }, + "required": [ + "username", + "display_name", + "password" + ], + "additionalProperties": false, + "examples": [ + { + "username": "alice", + "display_name": "Алиса", + "password": "Example-only-not-a-real-secret-123" + } + ] + }, + "Chat": { + "type": "object", + "properties": { + "id": { + "type": "string", + "format": "uuid" + }, + "title": { + "type": "string", + "minLength": 1, + "maxLength": 100 + }, + "member_ids": { + "type": "array", + "items": { + "type": "string", + "format": "uuid" + }, + "minItems": 1, + "maxItems": 100, + "uniqueItems": true + }, + "created_at": { + "type": "string", + "format": "date-time" + } + }, + "required": [ + "id", + "title", + "member_ids", + "created_at" + ], + "additionalProperties": false, + "examples": [ + { + "title": "Проект по ВНП", + "member_ids": [ + "11111111-1111-4111-8111-111111111111", + "22222222-2222-4222-8222-222222222222" + ], + "id": "33333333-3333-4333-8333-333333333333", + "created_at": "2026-09-01T12:00:00Z" + } + ] + }, + "CreateChat": { + "type": "object", + "properties": { + "title": { + "type": "string", + "minLength": 1, + "maxLength": 100 + }, + "member_ids": { + "type": "array", + "items": { + "type": "string", + "format": "uuid" + }, + "minItems": 1, + "maxItems": 100, + "uniqueItems": true + } + }, + "required": [ + "title", + "member_ids" + ], + "additionalProperties": false, + "examples": [ + { + "title": "Проект по ВНП", + "member_ids": [ + "11111111-1111-4111-8111-111111111111", + "22222222-2222-4222-8222-222222222222" + ] + } + ] + }, + "Message": { + "type": "object", + "properties": { + "id": { + "type": "string", + "format": "uuid" + }, + "chat_id": { + "type": "string", + "format": "uuid" + }, + "sender_id": { + "type": "string", + "format": "uuid" + }, + "text": { + "type": "string", + "maxLength": 4000 + }, + "created_at": { + "type": "string", + "format": "date-time" + }, + "attachment_ids": { + "type": "array", + "items": { + "type": "string", + "format": "uuid" + }, + "maxItems": 10, + "uniqueItems": true + } + }, + "required": [ + "id", + "chat_id", + "sender_id", + "text", + "created_at", + "attachment_ids" + ], + "additionalProperties": false, + "anyOf": [ + { + "properties": { + "text": { + "minLength": 1 + } + } + }, + { + "properties": { + "attachment_ids": { + "minItems": 1 + } + } + } + ], + "examples": [ + { + "id": "44444444-4444-4444-8444-444444444444", + "chat_id": "33333333-3333-4333-8333-333333333333", + "sender_id": "11111111-1111-4111-8111-111111111111", + "text": "Привет, Боб!", + "created_at": "2026-09-01T12:00:00Z", + "attachment_ids": [] + } + ] + }, + "CreateMessage": { + "type": "object", + "properties": { + "text": { + "type": "string", + "maxLength": 4000 + }, + "attachment_ids": { + "type": "array", + "items": { + "type": "string", + "format": "uuid" + }, + "maxItems": 10, + "uniqueItems": true, + "default": [] + } + }, + "required": [ + "text" + ], + "additionalProperties": false, + "anyOf": [ + { + "properties": { + "text": { + "minLength": 1 + } + } + }, + { + "properties": { + "attachment_ids": { + "minItems": 1 + } + }, + "required": [ + "attachment_ids" + ] + } + ], + "examples": [ + { + "text": "Привет, Боб!" + } + ] + }, + "ChatPage": { + "type": "object", + "properties": { + "items": { + "type": "array", + "items": { + "$ref": "#/components/schemas/Chat" + } + }, + "next_cursor": { + "type": [ + "string", + "null" + ] + } + }, + "required": [ + "items", + "next_cursor" + ], + "additionalProperties": false + }, + "MessagePage": { + "type": "object", + "properties": { + "items": { + "type": "array", + "items": { + "$ref": "#/components/schemas/Message" + } + }, + "next_cursor": { + "type": [ + "string", + "null" + ] + } + }, + "required": [ + "items", + "next_cursor" + ], + "additionalProperties": false, + "examples": [ + { + "items": [ + { + "id": "44444444-4444-4444-8444-444444444444", + "chat_id": "33333333-3333-4333-8333-333333333333", + "sender_id": "11111111-1111-4111-8111-111111111111", + "text": "Привет, Боб!", + "created_at": "2026-09-01T12:00:00Z", + "attachment_ids": [] + } + ], + "next_cursor": null + } + ] + }, + "Session": { + "type": "object", + "properties": { + "token": { + "type": "string", + "minLength": 32 + }, + "token_type": { + "type": "string", + "const": "Bearer" + }, + "expires_at": { + "type": "string", + "format": "date-time" + }, + "user": { + "$ref": "#/components/schemas/User" + } + }, + "required": [ + "token", + "token_type", + "expires_at", + "user" + ], + "additionalProperties": false + }, + "Variant": { + "type": "object", + "properties": { + "name": { + "type": "string", + "enum": [ + "thumbnail", + "crop", + "optimized" + ] + }, + "content_type": { + "type": "string", + "enum": [ + "image/jpeg", + "image/png", + "image/webp" + ] + }, + "size_bytes": { + "type": "integer", + "minimum": 1 + }, + "width": { + "type": "integer", + "minimum": 1 + }, + "height": { + "type": "integer", + "minimum": 1 + }, + "sha256": { + "type": "string", + "pattern": "^[a-f0-9]{64}$" + } + }, + "required": [ + "name", + "content_type", + "size_bytes", + "width", + "height", + "sha256" + ], + "additionalProperties": false + }, + "Attachment": { + "type": "object", + "properties": { + "id": { + "type": "string", + "format": "uuid" + }, + "chat_id": { + "type": "string", + "format": "uuid" + }, + "owner_id": { + "type": "string", + "format": "uuid" + }, + "filename": { + "type": "string", + "minLength": 1, + "maxLength": 255 + }, + "content_type": { + "type": "string", + "enum": [ + "image/png", + "image/jpeg" + ] + }, + "size_bytes": { + "type": "integer", + "minimum": 1, + "maximum": 10485760 + }, + "sha256": { + "type": "string", + "pattern": "^[a-f0-9]{64}$" + }, + "status": { + "type": "string", + "enum": [ + "ready" + ] + }, + "variants": { + "type": "array", + "items": { + "$ref": "#/components/schemas/Variant" + } + }, + "error_code": { + "type": [ + "string", + "null" + ] + }, + "created_at": { + "type": "string", + "format": "date-time" + }, + "updated_at": { + "type": "string", + "format": "date-time" + } + }, + "required": [ + "id", + "chat_id", + "owner_id", + "filename", + "content_type", + "size_bytes", + "sha256", + "status", + "variants", + "error_code", + "created_at", + "updated_at" + ], + "additionalProperties": false + } + } + } +} diff --git a/evidence/README.md.lab4 b/evidence/README.md.lab4 new file mode 100644 index 00000000..8df70015 --- /dev/null +++ b/evidence/README.md.lab4 @@ -0,0 +1,5 @@ +# Доказательства + +Храните небольшие обезличенные результаты: команды и stdout проверок, JSON/CSV агрегатов нагрузки, EXPLAIN, dashboard JSON, текст разбора отказа. Укажите дату, commit, аппаратные лимиты и профиль нагрузки. + +Не добавляйте .env, session tokens, cookies, приватные ключи, credentials, реальные сообщения, raw dumps и signed URLs. Для скриншотов скрывайте секреты. Большие логи/бинарные traces сохраняйте отдельно по правилам преподавателя; одного скриншота без методики недостаточно. diff --git a/evidence/attachment-failover.txt b/evidence/attachment-failover.txt new file mode 100644 index 00000000..c5a9ffc3 --- /dev/null +++ b/evidence/attachment-failover.txt @@ -0,0 +1,4 @@ +{"uploaded_by": "ad8141b5e17c", "attachment_id": "4395a996-db6d-4568-8bfe-805a926b54c8", "size_bytes": 111817} +stopping API replica ad8141b5e17c +{"downloaded_by": "2229551f45ac", "stopped_replica": "ad8141b5e17c", "attempts": 1, "transient_errors": 0, "size_bytes": 111817, "sha256": "99ea7533c441f79cd630d15ce4d58d1ec4e06f6f0efb3b77a5c088305cd1bd75"} +PASS: attachment survived the stop of the replica that accepted it diff --git a/evidence/attachments-negative.txt b/evidence/attachments-negative.txt new file mode 100644 index 00000000..48f2a749 --- /dev/null +++ b/evidence/attachments-negative.txt @@ -0,0 +1,2 @@ +python3 tests/custom/attachments.py --base-url "http://haproxy:8080" +PASS: graded negative cases (413/415/404/400), no 5xx, cross-member download checksum matches diff --git a/evidence/failover.txt b/evidence/failover.txt new file mode 100644 index 00000000..ef89b76a --- /dev/null +++ b/evidence/failover.txt @@ -0,0 +1,4 @@ +python3 tests/resilience.py --mode failover --action-script "scripts/stop-one.sh" --base-url "http://haproxy:8080" +Executing explicit failover hook; hook output suppressed to avoid leaking secrets. +{"observed_instances_before": 2, "successful_writes": 118, "failed_attempts": 1, "first_recovery_seconds": 2.557} +PASS: failover preserved confirmed data and existing session. Hook scope needs operator evidence. diff --git a/evidence/idempotency.txt b/evidence/idempotency.txt new file mode 100644 index 00000000..a1c73bf3 --- /dev/null +++ b/evidence/idempotency.txt @@ -0,0 +1,2 @@ +python3 tests/custom/idempotency.py --base-url "http://haproxy:8080" +PASS: 12 concurrent requests used 2 replicas and stored one message diff --git a/evidence/interrupted-upload.txt b/evidence/interrupted-upload.txt new file mode 100644 index 00000000..ba753d34 --- /dev/null +++ b/evidence/interrupted-upload.txt @@ -0,0 +1,4 @@ +attachments rows: 61 -> 61 +stored objects: 61 -> 61 +temp files left: 0 +PASS: interrupted upload left no row, object or temp file diff --git a/evidence/load.json b/evidence/load.json new file mode 100644 index 00000000..306c2f08 --- /dev/null +++ b/evidence/load.json @@ -0,0 +1,17 @@ +{ + "lab": 4, + "workload": "closed-loop POST messages; no automatic retries", + "requested_duration_seconds": 15.0, + "elapsed_seconds": 15.216, + "concurrency": 8, + "successful_requests": 6249, + "failed_requests": 0, + "error_ratio": 0.0, + "successful_rps": 410.689, + "successful_latency_ms": { + "p50": 18.104, + "p95": 31.058, + "p99": 46.769 + }, + "note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead." +} diff --git a/evidence/load.txt b/evidence/load.txt new file mode 100644 index 00000000..306c2f08 --- /dev/null +++ b/evidence/load.txt @@ -0,0 +1,17 @@ +{ + "lab": 4, + "workload": "closed-loop POST messages; no automatic retries", + "requested_duration_seconds": 15.0, + "elapsed_seconds": 15.216, + "concurrency": 8, + "successful_requests": 6249, + "failed_requests": 0, + "error_ratio": 0.0, + "successful_rps": 410.689, + "successful_latency_ms": { + "p50": 18.104, + "p95": 31.058, + "p99": 46.769 + }, + "note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead." +} diff --git a/evidence/persistence-containers.txt b/evidence/persistence-containers.txt new file mode 100644 index 00000000..53a33989 --- /dev/null +++ b/evidence/persistence-containers.txt @@ -0,0 +1,17 @@ +before: +/mtusi2026lab-api-1 started=2026-10-31T17:51:06.750192429Z +/mtusi2026lab-api-2 started=2026-10-31T17:51:49.665340634Z +/mtusi2026lab-haproxy-1 started=2026-10-31T17:48:59.281606934Z +/mtusi2026lab-postgres-1 started=2026-10-31T17:48:59.351033349Z +/mtusi2026lab-redis-1 started=2026-10-31T17:48:59.306542581Z +/mtusi2026lab-rustfs-1 started=2026-10-31T17:48:59.424216765Z +python3 tests/resilience.py --mode persistence --action-script "scripts/restart.sh" --base-url "http://haproxy: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. +after: +/mtusi2026lab-api-1 started=2026-10-31T17:52:30.339455795Z +/mtusi2026lab-api-2 started=2026-10-31T17:52:30.289832188Z +/mtusi2026lab-haproxy-1 started=2026-10-31T17:52:30.089676215Z +/mtusi2026lab-postgres-1 started=2026-10-31T17:52:30.233422161Z +/mtusi2026lab-redis-1 started=2026-10-31T17:52:30.195015955Z +/mtusi2026lab-rustfs-1 started=2026-10-31T17:52:29.875749955Z diff --git a/evidence/persistence.txt b/evidence/persistence.txt new file mode 100644 index 00000000..55326c38 --- /dev/null +++ b/evidence/persistence.txt @@ -0,0 +1,3 @@ +python3 tests/resilience.py --mode persistence --action-script "scripts/restart.sh" --base-url "http://haproxy:8080" +Executing explicit persistence hook; hook output suppressed to avoid leaking secrets. +PASS: persistence preserved confirmed data and existing session. Hook scope needs operator evidence. diff --git a/evidence/private-bucket.txt b/evidence/private-bucket.txt new file mode 100644 index 00000000..4ea2f4be --- /dev/null +++ b/evidence/private-bucket.txt @@ -0,0 +1,9 @@ +1/4 create a probe object with the root credential +2/4 anonymous object GET must be denied + anonymous GET -> 403 +3/4 scoped credential works inside attachments/ and is denied elsewhere + scoped PUT/HEAD inside attachments/ -> allowed + scoped PUT outside attachments/ -> denied + scoped CreateBucket -> denied +4/4 cleanup the probe object on exit +PASS: bucket is private and the API credential is limited to attachments/* diff --git a/evidence/readiness-postgres.txt b/evidence/readiness-postgres.txt new file mode 100644 index 00000000..ef09c1f6 --- /dev/null +++ b/evidence/readiness-postgres.txt @@ -0,0 +1,18 @@ +== before == + X-Instance-Id: 2229551f45ac + Connection: close + +== stopping postgres == + Container mtusi2026lab-postgres-1 Stopping + Container mtusi2026lab-postgres-1 Stopped + HTTP/1.1 503 Service Unavailable +wget: server returned error: HTTP/1.1 503 Service Unavailable +== starting postgres == + Container mtusi2026lab-postgres-1 Starting + Container mtusi2026lab-postgres-1 Started +== after == + X-Request-Id: SixnEMLrvZMdpzQ7KEnqWSq0qLmSnx02kMfwNT39fiA + X-Instance-Id: 2229551f45ac + Connection: close + +{"status":"ok"} \ No newline at end of file diff --git a/evidence/readiness-redis.txt b/evidence/readiness-redis.txt new file mode 100644 index 00000000..2b0b0d82 --- /dev/null +++ b/evidence/readiness-redis.txt @@ -0,0 +1,15 @@ +== before == + Connection: close + +{"status":"ok"}== stopping redis == + Container mtusi2026lab-redis-1 Stopping + Container mtusi2026lab-redis-1 Stopped + HTTP/1.1 503 Service Unavailable +wget: server returned error: HTTP/1.1 503 Service Unavailable +== starting redis == + Container mtusi2026lab-redis-1 Starting + Container mtusi2026lab-redis-1 Started +== after == + X-Instance-Id: 2229551f45ac + Connection: close + diff --git a/evidence/readiness-s3.txt b/evidence/readiness-s3.txt new file mode 100644 index 00000000..fc516bf0 --- /dev/null +++ b/evidence/readiness-s3.txt @@ -0,0 +1,21 @@ +== before stopping rustfs == + X-Request-Id: JlYsHSjQDyVkf9Sfo3ifA3L0_2k4DXB4ONl34YgKC40 + X-Instance-Id: 2229551f45ac + Connection: close + +{"status":"ok"}exit=0 +== stopping rustfs == + Container mtusi2026lab-rustfs-1 Stopping + Container mtusi2026lab-rustfs-1 Stopped + HTTP/1.1 503 Service Unavailable +wget: server returned error: HTTP/1.1 503 Service Unavailable +exit=0 +== starting rustfs == + Container mtusi2026lab-rustfs-1 Starting + Container mtusi2026lab-rustfs-1 Started +== after rustfs recovery == + X-Request-Id: KeusU6cz8m0fpFPz5NpDzvQU3XD0E6N-bLLo410ePG8 + X-Instance-Id: 2229551f45ac + Connection: close + +{"status":"ok"}exit=0 diff --git a/evidence/reconcile.txt b/evidence/reconcile.txt new file mode 100644 index 00000000..a068f429 --- /dev/null +++ b/evidence/reconcile.txt @@ -0,0 +1,24 @@ + Container mtusi2026lab-reconciler-1 Stopping + Container mtusi2026lab-reconciler-1 Stopped +1/4 old pending row is visible to dry-run but unchanged + dry-run reported it and changed neither store +2/4 old pending row and object are removed by a real pass + Container mtusi2026lab-reconciler-run-d207ba83e1d7 Creating + Container mtusi2026lab-reconciler-run-d207ba83e1d7 Created +2026/11/06 15:57:35 INFO reconcile pass complete dry_run=false stale_pending=1 orphan_objects=0 deleted_rows=1 deleted_objects=1 skipped=0 + row and object removed +3/4 fresh pending row survives the grace period + Container mtusi2026lab-reconciler-run-c71fa380c411 Creating + Container mtusi2026lab-reconciler-run-c71fa380c411 Created +2026/11/06 15:57:39 INFO reconcile pass complete dry_run=false stale_pending=0 orphan_objects=0 deleted_rows=0 deleted_objects=0 skipped=0 + fresh row and object kept + Container mtusi2026lab-reconciler-run-2666c645577b Creating + Container mtusi2026lab-reconciler-run-2666c645577b Created +2026/11/06 15:57:41 INFO reconcile pass complete dry_run=false stale_pending=0 orphan_objects=1 deleted_rows=0 deleted_objects=1 skipped=0 + row removed, orphan object cleaned up +4/4 ready attachment is not touched + Container mtusi2026lab-reconciler-run-5f830168e512 Creating + Container mtusi2026lab-reconciler-run-5f830168e512 Created +2026/11/06 15:57:47 INFO reconcile pass complete dry_run=false stale_pending=0 orphan_objects=0 deleted_rows=0 deleted_objects=0 skipped=0 + ready metadata and bytes kept +PASS: reconciler removed only abandoned data and respected the grace period diff --git a/evidence/s3-bootstrap.txt b/evidence/s3-bootstrap.txt new file mode 100644 index 00000000..7b511709 --- /dev/null +++ b/evidence/s3-bootstrap.txt @@ -0,0 +1,8 @@ +run 1: + Container mtusi2026lab-s3-init-run-6ff27dc7d5c8 Creating + Container mtusi2026lab-s3-init-run-6ff27dc7d5c8 Created +S3 bootstrap complete: bucket=messenger-attachments user=messenger-api policy=messenger-attachments-rw +run 2: + Container mtusi2026lab-s3-init-run-83790bb5cf48 Creating + Container mtusi2026lab-s3-init-run-83790bb5cf48 Created +S3 bootstrap complete: bucket=messenger-attachments user=messenger-api policy=messenger-attachments-rw diff --git a/evidence/smoke.txt b/evidence/smoke.txt new file mode 100644 index 00000000..45d0c032 --- /dev/null +++ b/evidence/smoke.txt @@ -0,0 +1,2 @@ +python3 tests/smoke.py --base-url "http://haproxy:8080" +PASS: lab4 public API scenario; infrastructure and grading evidence remain separate. diff --git a/evidence/upload-memory-run.txt b/evidence/upload-memory-run.txt new file mode 100644 index 00000000..43366225 --- /dev/null +++ b/evidence/upload-memory-run.txt @@ -0,0 +1,25 @@ +{ + "workload": { + "uploads": 10, + "width": 1800, + "height": 1400, + "image_bytes": 7563763, + "sha256": "e42f6283ac02b27d81700f861fc943a1bcb2b17a638edde130d37625315907f5", + "elapsed_seconds": 2.6, + "instances": [ + "2229551f45ac", + "ad8141b5e17c" + ], + "download_verified": true + }, + "sampling_interval_seconds": 0.05, + "samples": 121, + "wall_seconds": 6.884, + "baseline_total_mib": 14.7, + "peak_total_mib": 354.1, + "peak_per_container_mib": { + "mtusi2026lab-api-1": 174.1, + "mtusi2026lab-api-2": 180.1 + }, + "api_memory_limit_mib": 512.0 +} diff --git a/evidence/upload-memory-single-run.txt b/evidence/upload-memory-single-run.txt new file mode 100644 index 00000000..214639d8 --- /dev/null +++ b/evidence/upload-memory-single-run.txt @@ -0,0 +1,24 @@ +{ + "workload": { + "uploads": 1, + "width": 1800, + "height": 1400, + "image_bytes": 7563763, + "sha256": "be9606c01fdb972e9d60ffdacc03d0208cdca825a0797e727647639b14c0e35c", + "elapsed_seconds": 0.435, + "instances": [ + "2229551f45ac" + ], + "download_verified": true + }, + "sampling_interval_seconds": 0.05, + "samples": 82, + "wall_seconds": 4.616, + "baseline_total_mib": 16.7, + "peak_total_mib": 280.4, + "peak_per_container_mib": { + "mtusi2026lab-api-1": 142.9, + "mtusi2026lab-api-2": 137.5 + }, + "api_memory_limit_mib": 512.0 +} diff --git a/evidence/upload-memory-single.json b/evidence/upload-memory-single.json new file mode 100644 index 00000000..214639d8 --- /dev/null +++ b/evidence/upload-memory-single.json @@ -0,0 +1,24 @@ +{ + "workload": { + "uploads": 1, + "width": 1800, + "height": 1400, + "image_bytes": 7563763, + "sha256": "be9606c01fdb972e9d60ffdacc03d0208cdca825a0797e727647639b14c0e35c", + "elapsed_seconds": 0.435, + "instances": [ + "2229551f45ac" + ], + "download_verified": true + }, + "sampling_interval_seconds": 0.05, + "samples": 82, + "wall_seconds": 4.616, + "baseline_total_mib": 16.7, + "peak_total_mib": 280.4, + "peak_per_container_mib": { + "mtusi2026lab-api-1": 142.9, + "mtusi2026lab-api-2": 137.5 + }, + "api_memory_limit_mib": 512.0 +} diff --git a/evidence/upload-memory.json b/evidence/upload-memory.json new file mode 100644 index 00000000..43366225 --- /dev/null +++ b/evidence/upload-memory.json @@ -0,0 +1,25 @@ +{ + "workload": { + "uploads": 10, + "width": 1800, + "height": 1400, + "image_bytes": 7563763, + "sha256": "e42f6283ac02b27d81700f861fc943a1bcb2b17a638edde130d37625315907f5", + "elapsed_seconds": 2.6, + "instances": [ + "2229551f45ac", + "ad8141b5e17c" + ], + "download_verified": true + }, + "sampling_interval_seconds": 0.05, + "samples": 121, + "wall_seconds": 6.884, + "baseline_total_mib": 14.7, + "peak_total_mib": 354.1, + "peak_per_container_mib": { + "mtusi2026lab-api-1": 174.1, + "mtusi2026lab-api-2": 180.1 + }, + "api_memory_limit_mib": 512.0 +} diff --git a/examples/requests.http.lab4 b/examples/requests.http.lab4 new file mode 100644 index 00000000..052497b1 --- /dev/null +++ b/examples/requests.http.lab4 @@ -0,0 +1,53 @@ +# Optional HTTP-client worksheet; no extension is required by the course. +# Replace IDs/token locally from real responses; do not commit filled-in credentials. +# Canonical API: contracts/openapi.json and contracts/README.md. +@baseUrl = http://localhost:8080 +@aliceId = REPLACE_WITH_CREATED_ALICE_UUID +@bobId = REPLACE_WITH_CREATED_BOB_UUID +@chatId = REPLACE_WITH_CREATED_CHAT_UUID +@sessionToken = REPLACE_LOCALLY_DO_NOT_COMMIT + +### Liveness +GET {{baseUrl}}/health/live + +### Register Alice (repeat with username bob/display_name Боб) +POST {{baseUrl}}/api/v1/users +Content-Type: application/json + +{ + "username": "alice", + "display_name": "Алиса", + "password": "Example-only-not-a-real-secret-123" +} + +### Create session: generate the Base64 value locally, do not commit credentials +# Base64 of UTF-8 username:password; only this endpoint accepts Basic. +POST {{baseUrl}}/api/v1/auth/sessions +Authorization: Basic REPLACE_WITH_LOCAL_BASE64_CREDENTIAL + + +### Create chat; both UUIDs must be real registered users +POST {{baseUrl}}/api/v1/chats +Authorization: Bearer {{sessionToken}} +Content-Type: application/json + +{"title":"Проект по ВНП","member_ids":["{{aliceId}}","{{bobId}}"]} + +### Send message +POST {{baseUrl}}/api/v1/chats/{{chatId}}/messages +Authorization: Bearer {{sessionToken}} +Idempotency-Key: example-message-001 +Content-Type: application/json + +{"text":"Привет, Боб!"} + +### Read first page +GET {{baseUrl}}/api/v1/chats/{{chatId}}/messages?limit=50 +Authorization: Bearer {{sessionToken}} + +### Binary upload; HTTP file-client syntax for reading local fixture +POST {{baseUrl}}/api/v1/chats/{{chatId}}/attachments?filename=sample.png +Authorization: Bearer {{sessionToken}} +Content-Type: image/png + +< ../tests/fixtures/sample.png diff --git a/infra/README.md.lab4 b/infra/README.md.lab4 new file mode 100644 index 00000000..5a39e862 --- /dev/null +++ b/infra/README.md.lab4 @@ -0,0 +1,7 @@ +# Инфраструктура студента + +Здесь разместите конфигурации выбранных сервисов; основную топологию — в корневом compose.yaml/stack.yaml. `compose.example.yaml` — только worksheet ролей и намеренно не запускается. `contracts/infrastructure.json` задаёт минимальные роли, не готовые образы/пароли. + +В REPORT.md сопоставьте роль → service name → версия/digest → health/readiness → volume → сеть/порт → способ передачи credentials. Приведите bootstrap (schema, bucket, broker topology, а с lab6 provisioning) и безопасное повторение этих команд. + +Приложение строится из исходников. Не используйте заранее существующий образ преподавательского сервера вместо своей реализации. Restart hook перезапускает все соответствующие stores с volumes; failover hook останавливает только заданную API-реплику. diff --git a/infra/haproxy/haproxy.cfg b/infra/haproxy/haproxy.cfg new file mode 100644 index 00000000..21a70622 --- /dev/null +++ b/infra/haproxy/haproxy.cfg @@ -0,0 +1,41 @@ +global + log stdout format raw local0 + maxconn 4096 + +defaults + log global + mode http + option httplog + option dontlognull + timeout connect 2s + timeout client 30s + timeout server 30s + timeout http-request 10s + timeout http-keep-alive 30s + retries 2 + option redispatch + +resolvers docker + nameserver dns 127.0.0.11:53 + resolve_retries 3 + timeout resolve 1s + timeout retry 1s + +frontend api_frontend + bind *:8080 + default_backend api + +backend api + balance roundrobin + option httpchk GET /health/ready + http-check expect status 200 + default-server check inter 1s fastinter 500ms downinter 1s fall 2 rise 2 + # 16 slots is thus the upper bound for API_REPLICAS + server-template api 16 api:8080 resolvers docker init-addr last,libc,none + +frontend stats + bind *:8404 + http-request use-service prometheus-exporter if { path /metrics } + stats enable + stats uri /stats + stats refresh 5s diff --git a/infra/rustfs/api-policy.json.template b/infra/rustfs/api-policy.json.template new file mode 100644 index 00000000..42c837ca --- /dev/null +++ b/infra/rustfs/api-policy.json.template @@ -0,0 +1,26 @@ +{ + "Version": "2012-10-17", + "Statement": [ + { + "Effect": "Allow", + "Action": [ + "s3:GetBucketLocation", + "s3:ListBucket" + ], + "Resource": [ + "arn:aws:s3:::__BUCKET__" + ] + }, + { + "Effect": "Allow", + "Action": [ + "s3:GetObject", + "s3:PutObject", + "s3:DeleteObject" + ], + "Resource": [ + "arn:aws:s3:::__BUCKET__/attachments/*" + ] + } + ] +} diff --git a/infra/rustfs/bootstrap.sh b/infra/rustfs/bootstrap.sh new file mode 100644 index 00000000..550bb419 --- /dev/null +++ b/infra/rustfs/bootstrap.sh @@ -0,0 +1,52 @@ +#!/bin/sh +# Idempotent RustFS bootstrap: create the private bucket, a scoped IAM user and +# a policy that only allows object operations under the attachments/ prefix. +set -eu + +: "${S3_ENDPOINT:?S3_ENDPOINT is required}" +: "${S3_BUCKET:?S3_BUCKET is required}" +: "${S3_ROOT_ACCESS_KEY:?S3_ROOT_ACCESS_KEY is required}" +: "${S3_ROOT_SECRET_KEY:?S3_ROOT_SECRET_KEY is required}" +: "${S3_ACCESS_KEY_ID:?S3_ACCESS_KEY_ID is required}" +: "${S3_SECRET_ACCESS_KEY:?S3_SECRET_ACCESS_KEY is required}" +: "${S3_APP_USER:?S3_APP_USER is required}" +: "${S3_APP_POLICY:?S3_APP_POLICY is required}" +: "${S3_POLICY_TEMPLATE:?S3_POLICY_TEMPLATE is required}" + +# AGENT: rc stores its alias file in $HOME. The image runs as an unprivileged +# user, so point HOME at a writable temporary directory. +export HOME="${HOME:-/tmp}" +ALIAS=local + +rc alias set "$ALIAS" "$S3_ENDPOINT" "$S3_ROOT_ACCESS_KEY" "$S3_ROOT_SECRET_KEY" >/dev/null + +# Compose waits for the RustFS healthcheck, but the S3 API can become ready a +# moment later. A bounded wait keeps this service deterministic without hiding +# a genuinely broken deployment. +attempt=0 +until rc ready "$ALIAS" >/dev/null 2>&1; do + attempt=$((attempt + 1)) + if [ "$attempt" -ge 60 ]; then + echo "RustFS did not become ready within 60 seconds" >&2 + exit 1 + fi + sleep 1 +done + +rc bucket create -p "$ALIAS/$S3_BUCKET" >/dev/null +# Explicitly converge anonymous access to private. This is both a policy +# assertion and a guard against a bucket created public by a previous run. +rc bucket anonymous set private "$ALIAS/$S3_BUCKET" >/dev/null + +# The policy is rendered from a template because the bucket name is part of +# every resource ARN and is configurable in .env. +sed "s|__BUCKET__|$S3_BUCKET|g" "$S3_POLICY_TEMPLATE" > /tmp/api-policy.json + +# All three admin calls are replaces/updates, not creates that fail on a second +# run: policy create replaces the named document, user add updates the secret, +# and policy attach is idempotent. +rc admin policy create "$ALIAS" "$S3_APP_POLICY" /tmp/api-policy.json >/dev/null +rc admin user add "$ALIAS" "$S3_APP_USER" "$S3_SECRET_ACCESS_KEY" >/dev/null +rc admin policy attach "$ALIAS" "$S3_APP_POLICY" --user "$S3_APP_USER" >/dev/null + +echo "S3 bootstrap complete: bucket=$S3_BUCKET user=$S3_APP_USER policy=$S3_APP_POLICY" diff --git a/lab.json.lab4 b/lab.json.lab4 new file mode 100644 index 00000000..e9ffc9f7 --- /dev/null +++ b/lab.json.lab4 @@ -0,0 +1,8 @@ +{ + "number": 4, + "title": "S3-хранилище и вложения", + "contract_version": "1.4.0", + "default_base_url": "http://localhost:8080", + "grading": "3; 4 includes 3; 5 includes 4 plus one completed research track", + "scaffold_has_application": false +} diff --git a/load/messages.js b/load/messages.js new file mode 100644 index 00000000..5abd7929 --- /dev/null +++ b/load/messages.js @@ -0,0 +1,70 @@ +import http from 'k6/http'; +import encoding from 'k6/encoding'; +import { check } from 'k6'; + +const baseURL = __ENV.BASE_URL || 'http://localhost:8080'; +const vus = Number(__ENV.VUS || 10); +const duration = __ENV.DURATION || '60s'; + +export const options = { + scenarios: { + writes: { + executor: 'constant-vus', + vus, + duration, + }, + }, + summaryTrendStats: ['avg', 'min', 'med', 'p(50)', 'p(95)', 'p(99)', 'max'], +}; + +function headers(token) { + return { + Authorization: `Bearer ${token}`, + 'Content-Type': 'application/json', + Accept: 'application/json', + }; +} + +export function setup() { + const suffix = `${Date.now()}_${Math.floor(Math.random() * 1e6)}`; + const username = `u_load_${suffix}`.slice(0, 32); + const password = `Load-${suffix}-pass`; + + const registration = http.post( + `${baseURL}/api/v1/users`, + JSON.stringify({ username, display_name: 'Load test', password }), + { headers: { 'Content-Type': 'application/json', Accept: 'application/json' } }, + ); + check(registration, { 'register 201': (r) => r.status === 201 }); + const user = registration.json(); + + const basic = encoding.b64encode(`${username}:${password}`); + const login = http.post(`${baseURL}/api/v1/auth/sessions`, null, { + headers: { Authorization: `Basic ${basic}`, Accept: 'application/json' }, + }); + check(login, { 'login 201': (r) => r.status === 201 }); + const token = login.json('token'); + + const chat = http.post( + `${baseURL}/api/v1/chats`, + JSON.stringify({ title: 'Load profile', member_ids: [user.id] }), + { headers: headers(token) }, + ); + check(chat, { 'chat 201': (r) => r.status === 201 }); + + return { token, chatID: chat.json('id') }; +} + +export default function (data) { + const key = `${__VU}-${__ITER}-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`; + const response = http.post( + `${baseURL}/api/v1/chats/${data.chatID}/messages`, + JSON.stringify({ text: `load message ${key}` }), + { headers: { ...headers(data.token), 'Idempotency-Key': key } }, + ); + + check(response, { + 'message 201': (r) => r.status === 201, + 'instance header': (r) => Boolean(r.headers['X-Instance-Id'] || r.headers['x-instance-id']), + }); +} diff --git a/requirements-dev.txt.lab4 b/requirements-dev.txt.lab4 new file mode 100644 index 00000000..6b103ddb --- /dev/null +++ b/requirements-dev.txt.lab4 @@ -0,0 +1,2 @@ +# Only for full OpenAPI/JSON Schema validation; HTTP tests use stdlib. +openapi-spec-validator==0.7.2 diff --git a/scripts/check-private-bucket.sh b/scripts/check-private-bucket.sh new file mode 100755 index 00000000..5c2377b2 --- /dev/null +++ b/scripts/check-private-bucket.sh @@ -0,0 +1,104 @@ +#!/bin/sh +# Prove the bucket is private outside the API and that the API credential is +# scoped. The probe object is created with the RustFS root credential, fetched +# anonymously (must be 403), accessed with the scoped credential inside the +# allowed prefix, and removed again. +set -eu +cd "$(dirname "$0")/.." + +COMPOSE="docker compose --env-file .env" +TMP_DIR="$PWD/evidence/.tmp-private-bucket.$$" +mkdir -p "$TMP_DIR" +chmod 700 "$TMP_DIR" +TMP_ENV="$TMP_DIR/env" +umask 077 +# AGENT: only the S3 settings go into the temporary env file. The scoped +# credential is intentionally used for most checks; the root pair is needed +# only to create and remove the probe object. +cat > "$TMP_ENV" </dev/null 2>&1 && rc rm -f --quiet "root/$S3_BUCKET/$PROBE"' \ + >/dev/null 2>&1 || true + rm -rf "$TMP_DIR" +} +trap cleanup EXIT INT TERM + +network=$(docker inspect -f '{{range $k,$v := .NetworkSettings.Networks}}{{$k}}{{end}}' "$($COMPOSE ps -q haproxy)") +[ -n "$network" ] || { + echo "cannot determine the Compose network; is the stack running?" >&2 + exit 2 +} + +# shellcheck disable=SC1090 +. "$TMP_ENV" +PROBE="attachments/_privacy_probe_$$" +URL="http://rustfs:9000/$S3_BUCKET/$PROBE" + +echo "1/4 create a probe object with the root credential" +docker run --rm --network "$network" --env-file "$TMP_ENV" -e HOME=/tmp \ + -v "$PWD/tests/fixtures:/fixtures:ro" -e PROBE="$PROBE" \ + --entrypoint /bin/sh rustfs/rc:v0.1.36 -c \ + 'rc alias set root "$S3_ENDPOINT" "$S3_ROOT_ACCESS_KEY" "$S3_ROOT_SECRET_KEY" >/dev/null && + rc cp --quiet /fixtures/sample.png "root/$S3_BUCKET/$PROBE"' + +echo "2/4 anonymous object GET must be denied" +anon_code=$(docker run --rm --network "$network" -e URL="$URL" python:3.12-alpine \ + python3 -c ' +import os, sys, urllib.error, urllib.request +try: + with urllib.request.urlopen(os.environ["URL"], timeout=5) as response: + print(response.status) +except urllib.error.HTTPError as error: + print(error.code) +except Exception as error: + print(type(error).__name__, file=sys.stderr) + print(0) +') +[ "$anon_code" = "403" ] || { + echo "anonymous object GET returned $anon_code, expected 403" >&2 + exit 1 +} +echo " anonymous GET -> 403" + +echo "3/4 scoped credential works inside attachments/ and is denied elsewhere" +docker run --rm --network "$network" --env-file "$TMP_ENV" -e HOME=/tmp \ + -v "$PWD/tests/fixtures:/fixtures:ro" -e PROBE="$PROBE" \ + --entrypoint /bin/sh rustfs/rc:v0.1.36 -c \ + 'rc alias set app "$S3_ENDPOINT" "$S3_ACCESS_KEY_ID" "$S3_SECRET_ACCESS_KEY" >/dev/null && + rc cp --quiet /fixtures/sample.png "app/$S3_BUCKET/$PROBE" && + rc head --quiet "app/$S3_BUCKET/$PROBE" >/dev/null' +echo " scoped PUT/HEAD inside attachments/ -> allowed" + +if docker run --rm --network "$network" --env-file "$TMP_ENV" -e HOME=/tmp \ + -v "$PWD/tests/fixtures:/fixtures:ro" -e PROBE="$PROBE" \ + --entrypoint /bin/sh rustfs/rc:v0.1.36 -c \ + 'rc alias set app "$S3_ENDPOINT" "$S3_ACCESS_KEY_ID" "$S3_SECRET_ACCESS_KEY" >/dev/null && + rc cp --quiet /fixtures/sample.png "app/$S3_BUCKET/outside-$PROBE"' >/dev/null 2>&1; then + echo "scoped credential wrote outside attachments/, expected denial" >&2 + exit 1 +fi +echo " scoped PUT outside attachments/ -> denied" + +if docker run --rm --network "$network" --env-file "$TMP_ENV" -e HOME=/tmp \ + --entrypoint /bin/sh rustfs/rc:v0.1.36 -c \ + 'rc alias set app "$S3_ENDPOINT" "$S3_ACCESS_KEY_ID" "$S3_SECRET_ACCESS_KEY" >/dev/null && + rc bucket create -p "app/forbidden-'"$$"'"' >/dev/null 2>&1; then + echo "scoped credential created a bucket, expected denial" >&2 + exit 1 +fi +echo " scoped CreateBucket -> denied" + +echo "4/4 cleanup the probe object on exit" +chmod 700 "$TMP_DIR" 2>/dev/null || true +echo "PASS: bucket is private and the API credential is limited to attachments/*" diff --git a/scripts/check_contract.py.lab4 b/scripts/check_contract.py.lab4 new file mode 100644 index 00000000..dcb34dfa --- /dev/null +++ b/scripts/check_contract.py.lab4 @@ -0,0 +1,54 @@ +#!/usr/bin/env python3 +"""Offline structural checks; full schema validation is a separate target.""" +import ast +import json +from pathlib import Path +import re + +ROOT = Path(__file__).resolve().parents[1] + +def main(): + lab = json.loads((ROOT/'lab.json').read_text())['number'] + spec = json.loads((ROOT/'contracts/openapi.json').read_text()) + assert spec['openapi']=='3.1.0' + assert spec['info']['version']=='1.%d.0'%lab + operation_ids = set() + def walk(value): + if isinstance(value,dict): + if '$ref' in value: + pointer = value['$ref'] + assert pointer.startswith('#/'), 'unexpected external reference' + target = spec + for part in pointer[2:].split('/'): + target = target[part.replace('~1','/').replace('~0','~')] + for v in value.values(): + walk(v) + elif isinstance(value,list): + for v in value: + walk(v) + walk(spec) + for route,methods in spec['paths'].items(): + placeholders = set(re.findall(r'\{([^}]+)\}',route)) + for method,op in methods.items(): + assert op['operationId'] not in operation_ids, 'duplicate operationId' + operation_ids.add(op['operationId']) + params = {p['name'] for p in op.get('parameters',[]) if p['in']=='path' and p.get('required')} + assert placeholders==params, 'path parameter mismatch: '+route + for response in op['responses'].values(): + assert 'X-Request-Id' in response['headers'] + if lab>=3: + assert 'X-Instance-Id' in response['headers'] + assert ('/api/v1/auth/sessions' in spec['paths']) == (lab>=2) + assert ('/api/v1/attachments/{attachment_id}' in spec['paths']) == (lab>=4) + for file in list((ROOT/'tests').glob('*.py'))+list((ROOT/'scripts').glob('*.py')): + ast.parse(file.read_text(),filename=str(file)) + for file in ROOT.rglob('*.json'): + if any(part in {'.git','.venv','node_modules','vendor','output'} for part in file.parts): + continue + json.loads(file.read_text()) + for required in ('README.md','COURSE.md','CONTRIBUTING.md','REPORT.md','contracts/README.md','tests/smoke.py','tests/README.md'): + assert (ROOT/required).is_file(), 'missing '+required + print('PASS: offline skeleton structure, local refs and Python syntax; application NOT tested.') + +if __name__=='__main__': + main() diff --git a/scripts/measure-upload-memory.py b/scripts/measure-upload-memory.py new file mode 100755 index 00000000..9aff6e5c --- /dev/null +++ b/scripts/measure-upload-memory.py @@ -0,0 +1,174 @@ +#!/usr/bin/env python3 +"""Measure API container memory while concurrent uploads run. + +The HTTP workload runs in a throwaway Python container attached to the Compose +network. This host-side script samples the Docker Engine stats API at a short +interval, because the repository shell cannot always reach published ports and +`docker stats --no-stream` is too slow to catch a short upload burst. +""" +import argparse +import json +import socket +import subprocess +import sys +import threading +import time +from pathlib import Path + +ROOT = Path(__file__).resolve().parents[1] +DOCKER_SOCKET = "/var/run/docker.sock" + + +def docker_json(path): + request = f"GET {path} HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n" + with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as sock: + sock.connect(DOCKER_SOCKET) + sock.sendall(request.encode()) + data = bytearray() + while True: + part = sock.recv(65536) + if not part: + break + data.extend(part) + head, _, body = bytes(data).partition(b"\r\n\r\n") + if b"transfer-encoding: chunked" not in head.lower(): + return json.loads(body) + + # The Engine API uses chunked transfer encoding for stats and inspect. + decoded = bytearray() + position = 0 + while position < len(body): + line_end = body.find(b"\r\n", position) + if line_end < 0: + break + size = int(body[position:line_end].split(b";", 1)[0], 16) + if size == 0: + break + start = line_end + 2 + decoded.extend(body[start : start + size]) + position = start + size + 2 + return json.loads(bytes(decoded)) + + +def compose(*args): + return subprocess.check_output( + ["docker", "compose", "--env-file", ".env", *args], cwd=ROOT, text=True + ).strip() + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("--uploads", type=int, default=10) + parser.add_argument("--width", type=int, default=1800) + parser.add_argument("--height", type=int, default=1400) + parser.add_argument("--interval", type=float, default=0.05) + parser.add_argument("--output", type=Path, default=ROOT / "evidence/upload-memory.json") + args = parser.parse_args() + + api_ids = [line for line in compose("ps", "-q", "api").splitlines() if line] + if len(api_ids) < 2: + raise SystemExit("at least two running API replicas are required") + names = { + container_id: docker_json(f"/containers/{container_id}/json")["Name"].lstrip("/") + for container_id in api_ids + } + network = docker_json( + f"/containers/{compose('ps', '-q', 'haproxy')}/json" + )["NetworkSettings"]["Networks"] + network_name = next(iter(network)) + + stop = threading.Event() + samples = [] + + def sample(): + while not stop.is_set(): + per_container = {} + for container_id in api_ids: + stats = docker_json( + f"/containers/{container_id}/stats?stream=false&one-shot=true" + ) + per_container[names[container_id]] = stats.get("memory_stats", {}).get( + "usage", 0 + ) + samples.append( + { + "at": time.time(), + "total": sum(per_container.values()), + "per_container": per_container, + } + ) + time.sleep(args.interval) + + sampler = threading.Thread(target=sample, daemon=True) + sampler.start() + time.sleep(0.3) # capture an idle baseline before the burst + + command = [ + "docker", + "run", + "--rm", + "--network", + network_name, + "-v", + f"{ROOT}:/work:ro", + "-w", + "/work", + "python:3.12-alpine", + "python3", + "tests/custom/concurrent_uploads.py", + "--base-url", + "http://haproxy:8080", + "--uploads", + str(args.uploads), + "--width", + str(args.width), + "--height", + str(args.height), + ] + started = time.monotonic() + try: + output = subprocess.check_output(command, text=True) + finally: + stop.set() + sampler.join(timeout=2) + + workload = json.loads(output.strip().splitlines()[-1]) + baseline = samples[0]["total"] if samples else 0 + peak = max(samples, key=lambda item: item["total"]) if samples else {"total": 0} + report = { + "workload": workload, + "sampling_interval_seconds": args.interval, + "samples": len(samples), + "wall_seconds": round(time.monotonic() - started, 3), + "baseline_total_mib": round(baseline / (1024 * 1024), 1), + "peak_total_mib": round(peak["total"] / (1024 * 1024), 1), + "peak_per_container_mib": { + name: round(value / (1024 * 1024), 1) + for name, value in peak["per_container"].items() + }, + "api_memory_limit_mib": None, + } + # The limit is configured in .env; keep the evidence honest by reading the + # effective value from Compose rather than repeating a default. + configured = subprocess.check_output( + [ + "docker", + "inspect", + "-f", + "{{.HostConfig.Memory}}", + api_ids[0], + ], + text=True, + ).strip() + limit_bytes = int(configured or 0) + report["api_memory_limit_mib"] = ( + round(limit_bytes / (1024 * 1024), 1) if limit_bytes else None + ) + + args.output.parent.mkdir(parents=True, exist_ok=True) + args.output.write_text(json.dumps(report, indent=2) + "\n") + print(json.dumps(report, indent=2)) + + +if __name__ == "__main__": + main() diff --git a/scripts/restart.example.sh.lab4 b/scripts/restart.example.sh.lab4 new file mode 100755 index 00000000..967e9512 --- /dev/null +++ b/scripts/restart.example.sh.lab4 @@ -0,0 +1,10 @@ +#!/bin/sh +set -eu +# TODO: copy to restart.sh and implement using YOUR compose/stack services. +# Restart API and ALL used state stores (DB, sessions, S3, broker as applicable). +# Preserve persistent volumes and schema/data; never down -v / flush / re-seed. +# Allow operator unseal if required in lab7; do not print credentials. +# Exit 0 only after the intended action; test waits for API readiness separately. +printf '%s +' 'TODO: implement restart.sh for your infrastructure' >&2 +exit 2 diff --git a/scripts/restart.sh b/scripts/restart.sh new file mode 100755 index 00000000..38c4bc1d --- /dev/null +++ b/scripts/restart.sh @@ -0,0 +1,4 @@ +#!/bin/sh +set -eu +cd "$(dirname "$0")/.." +docker compose --env-file .env restart postgres redis rustfs haproxy api diff --git a/scripts/rolling-update.sh b/scripts/rolling-update.sh new file mode 100755 index 00000000..20392069 --- /dev/null +++ b/scripts/rolling-update.sh @@ -0,0 +1,42 @@ +#!/bin/sh +set -eu +cd "$(dirname "$0")/.." +COMPOSE="docker compose --env-file .env" +N=${1:-$($COMPOSE ps -q api | wc -l)} +[ "$N" -ge 2 ] || { + echo "need at least two API replicas; got $N" >&2 + exit 2 +} + +wait_healthy() { + attempts=0 + while [ "$attempts" -lt 90 ]; do + ids=$($COMPOSE ps -q api) + count=$(printf '%s\n' "$ids" | sed '/^$/d' | wc -l) + healthy=$(for id in $ids; do docker inspect -f '{{.State.Health.Status}}' "$id"; done | grep -c '^healthy$' || true) + if [ "$count" -eq "$N" ] && [ "$healthy" -eq "$N" ]; then + return 0 + fi + attempts=$((attempts + 1)) + sleep 1 + done + echo "API replicas did not become healthy in time" >&2 + return 1 +} + +$COMPOSE build api +i=0 +while [ "$i" -lt "$N" ]; do + target=$($COMPOSE ps -q api | head -n 1) + [ -n "$target" ] || { + echo "no API container to replace" >&2 + exit 2 + } + echo "replacing $target" + docker stop --time 10 "$target" >/dev/null + docker rm "$target" >/dev/null + $COMPOSE up -d --no-deps --no-recreate --scale api="$N" api + wait_healthy + i=$((i + 1)) +done +echo "rolling replacement complete for $N replicas" diff --git a/scripts/stop-one.example.sh.lab4 b/scripts/stop-one.example.sh.lab4 new file mode 100755 index 00000000..ae149e89 --- /dev/null +++ b/scripts/stop-one.example.sh.lab4 @@ -0,0 +1,10 @@ +#!/bin/sh +set -eu +# TODO: copy to stop-one.sh, map TARGET_INSTANCE_ID to ONE API container/task. +# Stop that instance and leave it stopped; no stop of DB/LB/volumes. +# Return quickly (within 10 s); the test sends requests during this script. +# Restore the replica explicitly after the test. Never print credentials. +: "${TARGET_INSTANCE_ID:?test provides target API instance alias}" +printf '%s +' 'TODO: implement stop-one.sh for your infrastructure' >&2 +exit 2 diff --git a/scripts/stop-one.sh b/scripts/stop-one.sh new file mode 100755 index 00000000..211a6ee7 --- /dev/null +++ b/scripts/stop-one.sh @@ -0,0 +1,20 @@ +#!/bin/sh +set -eu +: "${TARGET_INSTANCE_ID:?test provides target API instance alias}" +cd "$(dirname "$0")/.." + +target="" +for id in $(docker compose --env-file .env ps -q api); do + hostname=$(docker inspect -f '{{.Config.Hostname}}' "$id") + if [ "$hostname" = "$TARGET_INSTANCE_ID" ]; then + target="$id" + break + fi +done + +if [ -z "$target" ]; then + echo "no running API replica has instance id $TARGET_INSTANCE_ID" >&2 + exit 2 +fi + +docker stop --time 5 "$target" >/dev/null diff --git a/scripts/validate_openapi.py.lab4 b/scripts/validate_openapi.py.lab4 new file mode 100644 index 00000000..1dfe4a98 --- /dev/null +++ b/scripts/validate_openapi.py.lab4 @@ -0,0 +1,23 @@ +#!/usr/bin/env python3 +"""Full OpenAPI and JSON Schema checks; install requirements-dev.txt first.""" +import json +from pathlib import Path +from openapi_spec_validator import validate +from jsonschema import Draft202012Validator + +root = Path(__file__).resolve().parents[1] +spec = json.loads((root/'contracts/openapi.json').read_text()) +validate(spec) +from referencing import Registry, Resource +# The base URI resolves local #/components references inside examples. +resource = dict(spec, **{'$schema': 'https://json-schema.org/draft/2020-12/schema'}) +registry = Registry().with_resource('urn:mtusi:openapi', Resource.from_contents(resource)) +for name, example in json.loads((root/'contracts/examples.json').read_text()).items(): + schema = {'$ref': 'urn:mtusi:openapi#/components/schemas/' + name} + Draft202012Validator(schema, registry=registry).validate(example) +for path in (root/'contracts').glob('*.schema.json'): + schema = json.loads(path.read_text()) + Draft202012Validator.check_schema(schema) + for example in schema.get('examples',[]): + Draft202012Validator(schema).validate(example) +print('PASS: full OpenAPI 3.1 and JSON Schema validation; application NOT tested.') diff --git a/scripts/verify-attachment-failover.sh b/scripts/verify-attachment-failover.sh new file mode 100755 index 00000000..f02e2c6a --- /dev/null +++ b/scripts/verify-attachment-failover.sh @@ -0,0 +1,54 @@ +#!/bin/sh +# Verify the lab4 durability claim: upload through the load balancer, stop the +# API replica that served the upload, then download the same attachment through +# another replica and compare the SHA-256. The stopped replica is restored on +# exit so the stand is left as it was found. +set -eu +cd "$(dirname "$0")/.." + +COMPOSE="docker compose --env-file .env" +STATE_DIR="$PWD/evidence/.tmp-attachment-failover.$$" +mkdir -p "$STATE_DIR" +chmod 700 "$STATE_DIR" + +restore() { + rm -rf "$STATE_DIR" + $COMPOSE up -d --scale api=2 api >/dev/null 2>&1 || true + attempts=0 + while [ "$attempts" -lt 60 ]; do + ids=$($COMPOSE ps -q api) + count=$(printf '%s\n' "$ids" | sed '/^$/d' | wc -l) + healthy=$(for id in $ids; do docker inspect -f '{{.State.Health.Status}}' "$id"; done | grep -c '^healthy$' || true) + if [ "$count" -eq 2 ] && [ "$healthy" -eq 2 ]; then + return 0 + fi + attempts=$((attempts + 1)) + sleep 1 + done + echo "warning: API replicas did not return to two healthy instances" >&2 +} +trap restore EXIT INT TERM + +network=$(docker inspect -f '{{range $k,$v := .NetworkSettings.Networks}}{{$k}}{{end}}' "$($COMPOSE ps -q haproxy)") +[ -n "$network" ] || { + echo "cannot determine the Compose network; is the stack running?" >&2 + exit 2 +} + +run_python() { + docker run --rm --network "$network" \ + -v "$PWD:/work:ro" -v "$STATE_DIR:/state" -w /work \ + python:3.12-alpine \ + python3 tests/custom/attachment_flow.py --base-url http://haproxy:8080 "$@" +} + +upload_json=$(run_python --action upload --state /state/upload.json) +printf '%s\n' "$upload_json" +instance=$(printf '%s' "$upload_json" | python3 -c 'import json,sys; print(json.load(sys.stdin)["uploaded_by"])') + +echo "stopping API replica $instance" +TARGET_INSTANCE_ID="$instance" scripts/stop-one.sh + +run_python --action download --state /state/upload.json --timeout 30 + +echo "PASS: attachment survived the stop of the replica that accepted it" diff --git a/scripts/verify-interrupted-upload.sh b/scripts/verify-interrupted-upload.sh new file mode 100755 index 00000000..4ecb737a --- /dev/null +++ b/scripts/verify-interrupted-upload.sh @@ -0,0 +1,92 @@ +#!/bin/sh +# A client that declares a large body and disconnects halfway must not leave a +# pending metadata row, an S3 object or a temp file behind. The handler creates +# the pending row only after the whole bounded body has been copied, so this is +# the expected clean failure. +set -eu +cd "$(dirname "$0")/.." + +COMPOSE="docker compose --env-file .env" +network=$(docker inspect -f '{{range $k,$v := .NetworkSettings.Networks}}{{$k}}{{end}}' "$($COMPOSE ps -q haproxy)") +[ -n "$network" ] || { + echo "cannot determine the Compose network; is the stack running?" >&2 + exit 2 +} + +POSTGRES_USER=$(sed -n 's/^POSTGRES_USER=//p' .env) +POSTGRES_DB=$(sed -n 's/^POSTGRES_DB=//p' .env) +POSTGRES_USER=${POSTGRES_USER:-messenger} +POSTGRES_DB=${POSTGRES_DB:-messenger} + +count_rows() { + $COMPOSE exec -T postgres psql -U "$POSTGRES_USER" -d "$POSTGRES_DB" -tAc \ + "SELECT count(*) FROM attachments" | tr -d '\r' +} + +count_objects() { + docker run --rm --network "$network" --env-file .env -e HOME=/tmp \ + --entrypoint /bin/sh rustfs/rc:v0.1.36 -c \ + 'rc alias set app "$S3_ENDPOINT" "$S3_ACCESS_KEY_ID" "$S3_SECRET_ACCESS_KEY" >/dev/null && + rc bucket list -r --no-color "app/$S3_BUCKET/attachments/" | wc -l' | tr -d '\r ' +} + +rows_before=$(count_rows) +objects_before=$(count_objects) + +# The helper prints the chat id and token for this one short-lived probe. The +# token is passed through the environment, not written to disk or evidence. +state=$(docker run --rm --network "$network" -v "$PWD:/work:ro" -w /work \ + python:3.12-alpine python3 -c ' +import sys +sys.path.insert(0, "tests") +from client import Client +alice, _, _, chat = Client("http://haproxy:8080").bootstrap() +print(chat["id"] + " " + alice["token"]) +') +chat_id=$(printf '%s' "$state" | cut -d' ' -f1) +token=$(printf '%s' "$state" | cut -d' ' -f2) + +docker run --rm --network "$network" -e CHAT_ID="$chat_id" -e TOKEN="$token" \ + python:3.12-alpine python3 -c ' +import os, socket, time +host, port = "haproxy", 8080 +request = ( + "POST /api/v1/chats/" + os.environ["CHAT_ID"] + "/attachments?filename=cut.png HTTP/1.1\r\n" + "Host: haproxy\r\n" + "Authorization: Bearer " + os.environ["TOKEN"] + "\r\n" + "Content-Type: image/png\r\n" + "Content-Length: 5000000\r\n" + "Connection: close\r\n\r\n" +).encode() +with socket.create_connection((host, port), timeout=5) as sock: + sock.sendall(request) + sock.sendall(b"\x89PNG\r\n\x1a\n" + b"\x00" * 1024) + time.sleep(0.2) +# close without sending the declared body +' +sleep 2 + +rows_after=$(count_rows) +objects_after=$(count_objects) +temp_files=0 +for id in $($COMPOSE ps -q api); do + count=$(docker exec "$id" sh -c 'find /tmp/messenger-uploads -type f 2>/dev/null | wc -l' | tr -d '\r ') + temp_files=$((temp_files + count)) +done + +echo "attachments rows: $rows_before -> $rows_after" +echo "stored objects: $objects_before -> $objects_after" +echo "temp files left: $temp_files" +[ "$rows_before" = "$rows_after" ] || { + echo "interrupted upload left a metadata row" >&2 + exit 1 +} +[ "$objects_before" = "$objects_after" ] || { + echo "interrupted upload left an S3 object" >&2 + exit 1 +} +[ "$temp_files" = "0" ] || { + echo "interrupted upload left a temporary file" >&2 + exit 1 +} +echo "PASS: interrupted upload left no row, object or temp file" diff --git a/scripts/verify-reconcile.sh b/scripts/verify-reconcile.sh new file mode 100755 index 00000000..e402e597 --- /dev/null +++ b/scripts/verify-reconcile.sh @@ -0,0 +1,199 @@ +#!/bin/sh +# Exercise the two-store reconciler without breaking real data. +# +# 1. An old pending row plus its object must be removed; a dry run must not. +# 2. A fresh pending row must survive the grace period. +# 3. An object with no metadata row (orphan) must be removed. +# 4. A ready attachment referenced by PostgreSQL must never be removed. +set -eu +cd "$(dirname "$0")/.." + +COMPOSE="docker compose --env-file .env" +TMP_DIR="$PWD/evidence/.tmp-reconcile.$$" +mkdir -p "$TMP_DIR" +chmod 700 "$TMP_DIR" +TMP_ENV="$TMP_DIR/env" +umask 077 +cat > "$TMP_ENV" <&2 + exit 2 +} +# shellcheck disable=SC1090 +. "$TMP_ENV" + +POSTGRES_USER=$(sed -n 's/^POSTGRES_USER=//p' .env) +POSTGRES_DB=$(sed -n 's/^POSTGRES_DB=//p' .env) +POSTGRES_USER=${POSTGRES_USER:-messenger} +POSTGRES_DB=${POSTGRES_DB:-messenger} + +network=$(docker inspect -f '{{range $k,$v := .NetworkSettings.Networks}}{{$k}}{{end}}' "$($COMPOSE ps -q haproxy)") +[ -n "$network" ] || { + echo "cannot determine the Compose network; is the stack running?" >&2 + exit 2 +} + +# The long-running reconciler has a 900 s grace period. Pause it so the probe +# rows cannot be removed by a background pass while they are being asserted. +$COMPOSE stop reconciler >/dev/null + +psql_value() { + $COMPOSE exec -T postgres psql -U "$POSTGRES_USER" -d "$POSTGRES_DB" -tAc "$1" | tr -d '\r' +} + +rc_shell() { + docker run --rm --network "$network" --env-file "$TMP_ENV" -e HOME=/tmp \ + --entrypoint /bin/sh rustfs/rc:v0.1.36 -c \ + 'rc alias set app "$S3_ENDPOINT" "$S3_ACCESS_KEY_ID" "$S3_SECRET_ACCESS_KEY" >/dev/null && '"$1" +} + +object_exists() { + rc_shell "rc head --quiet \"app/\$S3_BUCKET/$1\" >/dev/null" >/dev/null 2>&1 +} + +put_object() { + docker run --rm --network "$network" --env-file "$TMP_ENV" -e HOME=/tmp \ + -v "$PWD/tests/fixtures:/fixtures:ro" -e KEY="$1" \ + --entrypoint /bin/sh rustfs/rc:v0.1.36 -c \ + 'rc alias set app "$S3_ENDPOINT" "$S3_ACCESS_KEY_ID" "$S3_SECRET_ACCESS_KEY" >/dev/null && + rc cp --quiet /fixtures/sample.png "app/$S3_BUCKET/$KEY"' +} + +remove_object() { + rc_shell "rc rm -f --quiet \"app/\$S3_BUCKET/$1\"" >/dev/null 2>&1 || true +} + +insert_pending() { + id=$1 + key=$2 + age_sql=$3 + psql_value "INSERT INTO attachments + (id, chat_id, owner_id, filename, content_type, size_bytes, sha256, + object_key, status, created_at, updated_at) + VALUES ('$id', '$CHAT_ID', '$OWNER_ID', 'probe.png', 'image/png', + $(wc -c < tests/fixtures/sample.png), decode('$(sha256sum tests/fixtures/sample.png | cut -d' ' -f1)', 'hex'), + '$key', 'pending', now() - $age_sql, now() - $age_sql)" >/dev/null +} + +run_reconciler() { + $COMPOSE run --rm --no-deps -T reconciler --once "$@" +} + +cleanup() { + for id in ${STALE_ID:-} ${FRESH_ID:-} ${READY_ID:-}; do + [ -n "$id" ] || continue + psql_value "DELETE FROM attachments WHERE id = '$id'" >/dev/null 2>&1 || true + done + for key in ${STALE_KEY:-} ${FRESH_KEY:-} ${READY_KEY:-}; do + [ -n "$key" ] || continue + remove_object "$key" + done + $COMPOSE start reconciler >/dev/null 2>&1 || true + rm -rf "$TMP_DIR" +} +trap cleanup EXIT INT TERM + +# A baseline chat/user pair makes the inserted rows valid without depending on +# data left by an earlier test run. +ids=$(docker run --rm --network "$network" -v "$PWD:/work:ro" -w /work \ + python:3.12-alpine python3 -c ' +import sys +sys.path.insert(0, "tests") +from client import Client +alice, _, _, chat = Client("http://haproxy:8080").bootstrap() +print(chat["id"], alice["id"]) +') +CHAT_ID=$(printf '%s' "$ids" | cut -d' ' -f1) +OWNER_ID=$(printf '%s' "$ids" | cut -d' ' -f2) + +echo "1/4 old pending row is visible to dry-run but unchanged" +STALE_ID=$(python3 -c 'import uuid; print(uuid.uuid4())') +STALE_KEY="attachments/$CHAT_ID/$STALE_ID" +put_object "$STALE_KEY" +insert_pending "$STALE_ID" "$STALE_KEY" "interval '1 hour'" +dry=$(run_reconciler --dry-run --grace=1s 2>&1) +printf '%s\n' "$dry" | grep -q "would remove stale pending attachment" || { + echo "dry run did not report the stale pending attachment" >&2 + exit 1 +} +[ "$(psql_value "SELECT count(*) FROM attachments WHERE id = '$STALE_ID'")" = "1" ] || { + echo "dry run deleted the pending row" >&2 + exit 1 +} +object_exists "$STALE_KEY" || { + echo "dry run deleted the pending object" >&2 + exit 1 +} +echo " dry-run reported it and changed neither store" + +echo "2/4 old pending row and object are removed by a real pass" +run_reconciler --grace=1s 2>&1 | tee "$TMP_DIR/stale.log" +[ "$(psql_value "SELECT count(*) FROM attachments WHERE id = '$STALE_ID'")" = "0" ] || { + echo "stale pending row survived" >&2 + exit 1 +} +if object_exists "$STALE_KEY"; then + echo "stale pending object survived" >&2 + exit 1 +fi +echo " row and object removed" + +echo "3/4 fresh pending row survives the grace period" +FRESH_ID=$(python3 -c 'import uuid; print(uuid.uuid4())') +FRESH_KEY="attachments/$CHAT_ID/$FRESH_ID" +put_object "$FRESH_KEY" +insert_pending "$FRESH_ID" "$FRESH_KEY" "interval '0 seconds'" +run_reconciler --grace=1s 2>&1 | tee "$TMP_DIR/fresh.log" +[ "$(psql_value "SELECT count(*) FROM attachments WHERE id = '$FRESH_ID'")" = "1" ] || { + echo "fresh pending row was removed" >&2 + exit 1 +} +object_exists "$FRESH_KEY" || { + echo "fresh pending object was removed" >&2 + exit 1 +} +echo " fresh row and object kept" + +# Remove the row only; the object now has no metadata row and becomes an +# orphan. A very small grace period makes the old object eligible immediately. +psql_value "DELETE FROM attachments WHERE id = '$FRESH_ID'" >/dev/null +run_reconciler --grace=1ns 2>&1 | tee "$TMP_DIR/orphan.log" +if object_exists "$FRESH_KEY"; then + echo "orphan object survived" >&2 + exit 1 +fi +echo " row removed, orphan object cleaned up" + +echo "4/4 ready attachment is not touched" +READY_ID=$(docker run --rm --network "$network" -v "$PWD:/work:ro" -w /work \ + python:3.12-alpine python3 -c ' +import sys +sys.path.insert(0, "tests") +from client import Client +c = Client("http://haproxy:8080") +alice, _, _, chat = c.bootstrap() +att, _ = c.attachment(alice, chat) +print(att["id"]) +') +READY_KEY=$(psql_value "SELECT object_key FROM attachments WHERE id = '$READY_ID'") +[ -n "$READY_KEY" ] || { + echo "could not read the ready object key" >&2 + exit 1 +} +run_reconciler --grace=1ns 2>&1 | tee "$TMP_DIR/ready.log" +[ "$(psql_value "SELECT count(*) FROM attachments WHERE id = '$READY_ID'")" = "1" ] || { + echo "ready attachment row was removed" >&2 + exit 1 +} +object_exists "$READY_KEY" || { + echo "ready attachment object was removed" >&2 + exit 1 +} +echo " ready metadata and bytes kept" + +echo "PASS: reconciler removed only abandoned data and respected the grace period" diff --git a/tests/README.md.lab4 b/tests/README.md.lab4 new file mode 100644 index 00000000..0dfa3f89 --- /dev/null +++ b/tests/README.md.lab4 @@ -0,0 +1,57 @@ +# Публичные проверки + +Python **3.9+**, стандартная библиотека. Сервер уже должен быть запущен; проверки не создают готовое приложение и сами не управляют Docker. `make check` можно выполнить на исходном skeleton, `make test` — только после реализации API. Полная OpenAPI-валидация отдельно: `make validate` после установки `requirements-dev.txt` в venv. + +```sh +make check +make test BASE_URL=http://localhost:8080 +# без make: +python3 tests/smoke.py --base-url http://localhost:8080 +``` + +`smoke.py` выполняет **один сквозной сценарий**, автоматически выбирая профиль из `lab.json`: health → три пользователя → вход с lab2 → чат → сообщения → пагинация → ошибки доступа → идемпотентность с lab3 → файл с lab4 → thumbnail с lab5 → logout. Он проверяет JSON по используемому подмножеству схемы, коды/headers, Unicode, SHA-256 оригинала и фактические JPEG-размеры thumbnail. Это публичный минимум, не полный fuzz/security/load suite. + +Каждый запуск создаёт уникальных синтетических пользователей, чат и сообщения. Данные автоматически не удаляются: в API курса нет delete. Для чистого повтора используйте отдельный тестовый стенд/volume и осознанный сброс своего окружения. Session tokens хранятся только в памяти процесса и не печатаются/не сохраняются. + +## Проверка сохранности, lab2+ + +Скопируйте `scripts/restart.example.sh` в `scripts/restart.sh`, реализуйте перезапуск **API и всех используемых хранилищ** с сохранением volumes и задайте executable bit. Скрипт может опираться на ваш compose.yaml или stack.yaml. Он не должен удалять volumes, повторно seed-ить БД или менять тестовые данные. + +```sh +chmod +x scripts/restart.sh +make test-persistence ACTION_SCRIPT=scripts/restart.sh +``` + +Тест создаёт данные и сессию, запускает указанный файл без shell interpolation, ожидает готовности и проверяет те же данные **с прежним токеном**. В lab4+ также проверяется исходный файл. Hook может работать до 90 секунд, готовность затем ожидается до 90 секунд. TTL сессии для этого теста — минимум 300 секунд. Успех не доказывает, что hook действительно перезапустил БД: приложите команды/состояния контейнеров до и после. + +## Проверка отказа, lab3+ + +Реализуйте `scripts/stop-one.sh` на основе примера. `TARGET_INSTANCE_ID` в окружении содержит alias обслужившей тест API-реплики: сопоставьте его своему контейнеру/Swarm task и остановите именно его. Скрипт должен быстро вернуть 0 и **оставить эту реплику остановленной**; восстановление выполняйте отдельно. Swarm может создать новую task с другим ID — это допустимо. + +```sh +chmod +x scripts/stop-one.sh +make test-failover ACTION_SCRIPT=scripts/stop-one.sh +``` + +Перед отказом тест наблюдает ≥2 API-реплики; 15 секунд отправляет сообщения параллельно выполнению hook, повторяет неопределённый POST с тем же ключом, проверяет восстановление записи за ≤10 секунд, отсутствие остановленной реплики в последних ответах, сохранность подтверждённых сообщений и отсутствие дублей. Ошибки переходного периода учитываются. Затем восстановите реплику и повторите для другой. Скрипт — проверка одного отказа API, не гарантия HA БД/LB/host. Вывод hook скрыт, чтобы не утекли секреты; отлаживайте его отдельно с безопасным выводом. + +## Нагрузка + +```sh +python3 tests/load.py --duration 30 --concurrency 4 --output evidence/load.json +``` + +Это простой **closed-loop** генератор POST сообщений; он ограничен производительностью клиента и страдает coordinated omission. Он выдаёт successful RPS, ошибки и p50/p95/p99 **только успешных запросов**. Во время нагрузки нет прозрачных retry; timeout мог скрыть совершённую запись, поэтому это throughput HTTP-подтверждений, не точный счётчик COMMIT. Изменение размера истории входит в профиль. Для серьёзного исследования используйте k6/Locust/wrk либо свой обоснованный генератор, а не выводите максимальную пропускную способность из одного запуска этого скрипта. Для нагрузки pipeline изображений нужен отдельный сценарий студента. + +## HTTPS, lab7 + +```sh +make test BASE_URL=https://localhost:8443 CA_FILE=/absolute/path/to/ca.crt +make test-security BASE_URL=https://localhost:8443 CA_FILE=/absolute/path/to/ca.crt +``` + +Все скрипты принимают `--ca-file`/`CA_FILE` и проверяют серверный сертификат и hostname. `--insecure` отсутствует. TLS-тест сначала проверяет рабочее доверенное соединение, затем намеренно пустой trust store и именно ошибку проверки сертификата; network timeout не засчитывается как правильный отказ. **Внутреннее mTLS, identity/authorization, OpenBao policy и hardening проверяют отдельные тесты студента** по trust matrix. Клиентский сертификат внешнему учебному REST-клиенту не требуется: mTLS находится на внутренних связях. + +## Что ещё остаётся доказать + +Smoke не доказывает persistence, число реальных контейнеров, durability брокера, outbox, отсутствие утечек/уязвимостей, полноту telemetry или выполнение уровня 4/5. Конкурентные/нагрузочные/негативные проверки своего решения добавляйте отдельно. Список защиты текущей лабы находится в README.md. Проверки рассчитаны на localhost-стенд, не на production. diff --git a/tests/client.py.lab4 b/tests/client.py.lab4 new file mode 100644 index 00000000..f0499a29 --- /dev/null +++ b/tests/client.py.lab4 @@ -0,0 +1,217 @@ +"""HTTP helpers for public black-box tests; Python 3.9+, standard library only.""" +import base64 +import datetime as dt +import hashlib +import json +import os +from pathlib import Path +import re +import ssl +import time +import urllib.error +import urllib.request +import uuid + +ROOT = Path(__file__).resolve().parents[1] +LAB = json.loads((ROOT / 'lab.json').read_text())['number'] +SPEC = json.loads((ROOT / 'contracts/openapi.json').read_text()) + +class Failure(AssertionError): + pass + +def check(condition, message): + if not condition: + raise Failure(message) + +def timestamp(value): + check(isinstance(value, str) and value.endswith('Z'), 'timestamp must be UTC ending in Z') + try: + return dt.datetime.fromisoformat(value[:-1] + '+00:00') + except ValueError as exc: + raise Failure('invalid RFC3339 timestamp') from exc + +def validate(value, schema, where='response'): + """Validate the JSON Schema subset used by the supplied contract, not arbitrary OpenAPI.""" + if '$ref' in schema: + target = SPEC + for part in schema['$ref'].split('/')[1:]: + target = target[part] + return validate(value, target, where) + kinds = schema.get('type', []) + if isinstance(kinds, str): + kinds = [kinds] + matches = {'null':value is None, 'boolean':isinstance(value,bool), 'integer':isinstance(value,int) and not isinstance(value,bool), 'number':isinstance(value,(int,float)) and not isinstance(value,bool), 'string':isinstance(value,str), 'array':isinstance(value,list), 'object':isinstance(value,dict)} + if kinds: + check(any(matches.get(k,False) for k in kinds), where + ': incorrect type') + if 'enum' in schema: + check(value in schema['enum'], where + ': invalid enum') + if 'const' in schema: + check(value == schema['const'], where + ': invalid const') + if isinstance(value,dict): + props = schema.get('properties',{}) + check(all(k in value for k in schema.get('required',[])), where + ': missing required fields') + if schema.get('additionalProperties') is False: + check(not set(value).difference(props), where + ': unexpected fields') + for key, sub in props.items(): + if key in value: + validate(value[key], sub, where + '.' + key) + if isinstance(value,list): + check(len(value) >= schema.get('minItems',0), where + ': too few items') + check(len(value) <= schema.get('maxItems',float('inf')), where + ': too many items') + if schema.get('uniqueItems'): + encoded = [json.dumps(x,sort_keys=True) for x in value] + check(len(encoded)==len(set(encoded)), where + ': duplicate items') + if 'items' in schema: + for item in value: + validate(item, schema['items'], where + '[]') + if isinstance(value,str): + check(len(value) >= schema.get('minLength',0), where + ': too short') + check(len(value) <= schema.get('maxLength',float('inf')), where + ': too long') + if 'pattern' in schema: + check(re.search(schema['pattern'], value) is not None, where + ': pattern mismatch') + if schema.get('format') == 'uuid': + try: + uuid.UUID(value) + except ValueError as exc: + raise Failure(where + ': invalid UUID') from exc + if schema.get('format') == 'date-time': + timestamp(value) + if isinstance(value,(int,float)) and not isinstance(value,bool): + check(value >= schema.get('minimum',-float('inf')), where + ': below minimum') + check(value <= schema.get('maximum',float('inf')), where + ': above maximum') + if 'anyOf' in schema: + for alternative in schema['anyOf']: + try: + validate(value, alternative, where) + break + except Failure: + pass + else: + raise Failure(where + ': no anyOf branch matches') + +def shape(value, name): + validate(value, SPEC['components']['schemas'][name]) + return value + +class NoRedirect(urllib.request.HTTPRedirectHandler): + def redirect_request(self, req, fp, code, msg, headers, newurl): + return None + +class Client: + def __init__(self, base_url=None, ca_file=None, timeout=10): + self.base = (base_url or os.environ.get('BASE_URL') or ('https://localhost:8443' if LAB==7 else 'http://localhost:8080')).rstrip('/') + check(self.base.startswith(('http://','https://')), 'BASE_URL must be HTTP(S)') + check(LAB != 7 or self.base.startswith('https://'), 'lab7 requires HTTPS') + self.timeout = timeout + context = ssl.create_default_context(cafile=ca_file or os.environ.get('CA_FILE') or None) + self.opener = urllib.request.build_opener(NoRedirect(), urllib.request.HTTPSHandler(context=context)) + + def call(self, method, path, expected=200, body=None, actor=None, headers=None, raw=None, schema=None): + h = {'Accept':'application/json'} + if actor: + h['X-User-Id' if LAB==1 else 'Authorization'] = actor['id'] if LAB==1 else 'Bearer ' + actor['token'] + if body is not None: + raw = json.dumps(body,ensure_ascii=False).encode('utf-8') + h['Content-Type'] = 'application/json' + h.update(headers or {}) + req = urllib.request.Request(self.base + path, data=raw, headers=h, method=method) + start = time.monotonic() + try: + response = self.opener.open(req, timeout=self.timeout) + except urllib.error.HTTPError as error: + response = error + except (urllib.error.URLError,TimeoutError,OSError) as exc: + # Do not print URL query, headers, request body, or credentials. + raise Failure(method + ' ' + path.split('?')[0] + ': connection/TLS failure (' + type(exc).__name__ + ')') from None + with response: + status = response.code + rh = response.headers + data = response.read(16*1024*1024 + 1) + check(len(data) <= 16*1024*1024, 'response exceeds test safety limit') + allowed = [expected] if isinstance(expected,int) else expected + check(status in allowed, method + ' ' + path.split('?')[0] + ': expected ' + str(allowed) + ', received ' + str(status)) + check(bool(rh.get('X-Request-Id')), 'missing X-Request-Id') + if LAB>=3: + check(bool(rh.get('X-Instance-Id')), 'missing X-Instance-Id') + payload = None + if schema or status>=400: + check(rh.get_content_type()=='application/json', 'expected application/json') + try: + payload = json.loads(data) + except (ValueError,UnicodeError): + raise Failure('invalid response JSON') from None + shape(payload, 'Error' if status>=400 else schema) + if status>=400: + check(payload['error']['request_id']==rh.get('X-Request-Id'),'error request_id differs from header') + if status==401 and LAB>=2: + scheme = 'Basic' if path=='/api/v1/auth/sessions' else 'Bearer' + check(rh.get('WWW-Authenticate','').lower().startswith(scheme.lower()), 'incorrect WWW-Authenticate challenge') + if status==204: + check(not data, '204 must not contain body') + return {'status':status,'headers':rh,'json':payload,'bytes':data,'elapsed':time.monotonic()-start} + + def user(self, label): + username = 'u_' + label + '_' + uuid.uuid4().hex[:12] + password = 'T3st-' + uuid.uuid4().hex + body = {'username':username,'display_name':'Студент ' + label} + if LAB>=2: + body['password'] = password + user = self.call('POST','/api/v1/users',201,body=body,schema='User')['json'] + check(user['username']==username,'username changed') + actor = {'id':user['id'],'user':user,'registration':body} + if LAB>=2: + credential = base64.b64encode((username+':'+password).encode()).decode() + session = self.call('POST','/api/v1/auth/sessions',201,headers={'Authorization':'Basic '+credential},schema='Session')['json'] + check(session['user']==user,'session user mismatch') + check(timestamp(session['expires_at']) > dt.datetime.now(dt.timezone.utc),'session already expired') + actor['token'] = session['token'] + return actor + + def bootstrap(self): + self.call('GET','/health/live',schema='Health') + self.call('GET','/health/ready',schema='Health') + alice, bob, eve = (self.user(label) for label in ('alice','bob','eve')) + chat = self.call('POST','/api/v1/chats',201,actor=alice,body={'title':'Контрактный тест','member_ids':[alice['id'],bob['id']]},schema='Chat')['json'] + check(set(chat['member_ids'])=={alice['id'],bob['id']},'chat member mismatch') + return alice,bob,eve,chat + + def message(self, actor, chat, text, key=None, attachments=None): + body = {'text':text} + if attachments is not None: + body['attachment_ids'] = attachments + headers = {'Idempotency-Key':key or uuid.uuid4().hex} if LAB>=3 else {} + return self.call('POST','/api/v1/chats/'+chat['id']+'/messages',201,actor=actor,body=body,headers=headers,schema='Message') + + def attachment(self, actor, chat): + content = (ROOT / 'tests/fixtures/sample.png').read_bytes() + att = self.call('POST','/api/v1/chats/'+chat['id']+'/attachments?filename=sample.png',201 if LAB==4 else 202,actor=actor,raw=content,headers={'Content-Type':'image/png'},schema='Attachment')['json'] + check(att['status']==('ready' if LAB==4 else 'queued'),'incorrect upload status') + check(att['chat_id']==chat['id'] and att['owner_id']==actor['id'],'attachment ownership mismatch') + check(att['sha256']==hashlib.sha256(content).hexdigest() and att['size_bytes']==len(content),'original checksum/size mismatch') + return att,content + + def wait_ready(self, actor, att, wait_seconds=60): + deadline = time.monotonic()+wait_seconds + while time.monotonic()