diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 00000000..72e9c40e --- /dev/null +++ b/.gitattributes @@ -0,0 +1,2 @@ +*.sh text eol=lf +*.sql text eol=lf diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..ef92a383 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,38 @@ +# syntax=docker/dockerfile:1 + +# Stage 1: Build static Go binary +FROM golang:1.24-alpine AS builder +WORKDIR /src + +ENV GOPROXY=https://goproxy.io,direct +ENV GOSUMDB=off + +# Cache dependencies first +COPY go.mod go.sum ./ +RUN go mod download + +# Copy application source and migrations +COPY cmd/ ./cmd/ +COPY internal/ ./internal/ +COPY migrations/ ./migrations/ + +# Build stripped static binary +RUN CGO_ENABLED=0 GOOS=linux go build -trimpath -ldflags="-s -w" -o /out/messenger ./cmd/server + +# Stage 2: Minimal non-root runtime image +FROM alpine:3.21 +RUN addgroup -g 10001 -S appgroup && \ + adduser -u 10001 -S appuser -G appgroup + +WORKDIR /app +COPY --from=builder /out/messenger /app/messenger +COPY --from=builder /src/migrations /app/migrations +RUN chown -R appuser:appgroup /app + +USER 10001:10001 +EXPOSE 8080 + +HEALTHCHECK --interval=2s --timeout=2s --start-period=2s --retries=10 \ + CMD wget -q -O /dev/null http://127.0.0.1:8080/health/ready || exit 1 + +ENTRYPOINT ["/app/messenger"] diff --git a/Makefile b/Makefile index 5d86d171..944afc85 100644 --- a/Makefile +++ b/Makefile @@ -4,7 +4,7 @@ CA_FILE ?= ACTION_SCRIPT ?= TLS_ARGS = $(if $(CA_FILE),--ca-file "$(CA_FILE)",) -.PHONY: help check validate test load test-persistence test-failover +.PHONY: help check validate test load test-persistence test-failover test-grade4 test-track2 seed help: @echo "check: offline structure; validate: full OpenAPI (requirements-dev.txt); test: running API; load: synthetic writes" @@ -28,3 +28,13 @@ test-persistence: 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-grade4: + $(PYTHON) student-tests/test_grade4.py "$(BASE_URL)" + +test-track2: + $(PYTHON) student-tests/test_track2_reconciler.py "$(BASE_URL)" + +seed: + $(PYTHON) scripts/seed.py "$(BASE_URL)" diff --git a/REPORT.md b/REPORT.md index 9ad2b84f..95c510ab 100644 --- a/REPORT.md +++ b/REPORT.md @@ -1,37 +1,137 @@ # Отчёт — лабораторная 4 -- Автор, группа: TODO -- Целевая оценка; выбранный трек 5, если нужен: TODO -- Commit/PR: TODO после публикации -- Использованные источники, библиотеки и помощь ИИ: TODO +- **Автор, группа:** Заморский Павел, БВТ2302 +- **Целевая оценка; трек 5:** **5** (выполнены уровни 3, 4 и **Трек 2 «Согласованность двух хранилищ»**) +- **Commit/PR:** ветка `solution/bvt2302-zamorskiy` +- **Использованные источники, библиотеки и помощь ИИ:** Go 1.24 (`jackc/pgx/v5`, `redis/go-redis/v9`, `minio/minio-go/v7`, `golang.org/x/crypto/argon2`, `golang.org/x/sync/singleflight`, `google/uuid`), PostgreSQL 18 (`postgres:18-alpine`), Redis 7.4 (`redis:7.4-alpine`), MinIO (`minio/minio@sha256:14cea493...` `RELEASE.2025-09-07T16-13-09Z` + `minio/mc@sha256:a7fe349e...`), HAProxy 3.0 (`haproxy:3.0-alpine`), документация MinIO S3 API & IAM Policies. Помощь ИИ (Google Antigravity) при генерации тестовых/экспериментальных скриптов и оформлении отчёта. -## Запуск из чистого clone +--- -TODO: версии runtime/образов, prerequisites, команды создания локальных secrets/.env, build, миграций, запуска, ожидания готовности, тестов и остановки. Указать необходимые действия оператора. Не вставлять секреты. Отдельно указать безопасную остановку и команду намеренного удаления учебных данных. +## 1. Запуск из чистого clone -## Архитектура и решения +### 1.1. Версии runtime и контейнерных образов +- **Go runtime / builder:** `golang:1.24-alpine`, runtime-образ: `alpine:3.21` (non-root UID/GID `10001`). +- **PostgreSQL:** `postgres:18-alpine` (volume `mtusi_lab4_pgdata`, `PGDATA=/var/lib/postgresql/data/pgdata`). +- **Redis:** `redis:7.4-alpine` (AOF `appendonly yes`, volume `mtusi_lab4_redisdata`). +- **S3-хранилище (MinIO):** `minio/minio@sha256:14cea493d9a34af32f524e538b8346cf79f3321eff8e708c1e2960462bd8936e` (`RELEASE.2025-09-07T16-13-09Z`, volume `mtusi_lab4_s3data`, `MINIO_BROWSER="off"`, порты `9000/9001` наружу не публикуются). +- **MinIO Bootstrap Client (`s3-init`):** `minio/mc@sha256:a7fe349ef4bd8521fb8497f55c6042871b2ae640607cf99d9bede5e9bdf11727`. +- **Load Balancer:** `haproxy:3.0-alpine` (единственная публичная точка входа `8080->80`). -TODO: схема процессов/хранилищ, источник истины, транзакционные границы, ограничения. Что перенесено из lab3, что изменено в этой работе. Соответствие переменных из .env.example реальной конфигурации. +### 1.2. Команды запуска, проверки и сброса -## Доказательства критериев +```sh +# 1. Скопировать локальный .env (секреты не коммитятся в Git) +cp .env.example .env -| Критерий README | Команда/сценарий | Наблюдаемый результат | Файл доказательства | +# 2. Проверить Compose и контракт +docker compose --env-file .env config --quiet +make check + +# 3. Собрать и поднять стенд (Postgres + Redis + MinIO -> migrate + s3-init -> API x2 -> HAProxy LB) +docker compose --env-file .env up -d --build +docker compose --env-file .env ps + +# 4. Дождаться готовности и наполнить демо-данные с изображением (опционально для /docs) +curl -fsS http://localhost:8080/health/ready +make seed + +# 5. Обязательные проверки (уровень 3) +make test +make test-persistence ACTION_SCRIPT=scripts/restart.sh +make test-failover ACTION_SCRIPT=scripts/stop-one.sh +# Восстановить остановленную реплику после failover-теста: +docker compose --env-file .env up -d --no-deps api + +# 6. Проверки уровня 4 и эксперимент уровня 5 (Трек 2) +make test-grade4 +make test-track2 +``` + +- **Безопасная остановка (с сохранением volumes):** `docker compose --env-file .env down` +- **Намеренное удаление данных и томов:** `docker compose --env-file .env down -v` + +--- + +## 2. Архитектура и решения + +### 2.1. Перенос из ЛР3 и изменения ЛР4 + +Из ЛР3 перенесены: HAProxy 3.0 с активным `/health/ready`, 2 stateless API-реплики с `X-Instance-Id`, `Idempotency-Key`, общие PostgreSQL 18 + Redis 7.4, однократный init-сервис миграций `migrate`. + +**Новые компоненты ЛР4:** +1. **Приватный MinIO S3 и принцип наименьших привилегий ([`compose.yaml`](compose.yaml), [`infra/s3-bootstrap.sh`](infra/s3-bootstrap.sh), [`infra/minio-api-policy.json`](infra/minio-api-policy.json)):** + - Init-сервис `s3-init` идемпотентно создаёт bucket `messenger-attachments` (`mc mb --ignore-existing`), отключает анонимный доступ (`mc anonymous set none`), создаёт отдельного пользователя `messenger_api_key` с IAM-политикой строго на один bucket (`GetBucketLocation`, `ListBucket`, `GetObject`, `PutObject`, `DeleteObject` только на `arn:aws:s3:::messenger-attachments/*`). + - Админская консоль MinIO выключена (`MINIO_BROWSER="off"`), а порты S3/консоли наружу не публикуются; API не получает root-учёток MinIO. + - Серверные S3 object keys имеют формат `attachments//`, клиентский `filename` хранится исключительно как метаданные в PostgreSQL и проверяется на отсутствие `/`, `\`, `NUL` и управляющих символов. +2. **Потоковая валидация изображений без OOM (`internal/media/validate.go`):** + - Проверка `Bearer`, валидности `chat_id` и прав участника (`404`) происходит **до** чтения тела и обращения к S3. + - Запрос ограничивается `http.MaxBytesReader` и `io.LimitReader(..., MAX_UPLOAD_BYTES+1)` во временный буфер `os.CreateTemp`, удаляемый в `defer` (0 локальных файлов после завершения запроса). + - На лету вычисляется `SHA-256`; по первым 512 байтам проверяются magic bytes против `Content-Type` (подмена MIME / несовпадение -> `415`). + - Перед полной распаковкой вызывается `image.DecodeConfig` (~4 КБ RAM) и проверяется `width * height <= MAX_IMAGE_PIXELS` (`40 000 000`) -> decompression bomb отклоняется кодом `413` без выделения памяти под пиксели. + - Дополнительно проверяется структурная целостность контейнера (CRC32 всех PNG-чанков до `IEND` или структура JPEG-маркеров до `EOI`) -> повреждённые файлы отклоняются с `415`. +3. **Загрузка, привязка и скачивание (`internal/store/postgres.go`, `internal/handler/handler.go`, `migrations/003_attachments.sql`):** + - `201 Attachment` возвращается **только после** успешного `PutObject` в MinIO и `COMMIT` в PostgreSQL. При ошибке БД выполняется немедленная синхронная компенсация `RemoveObject` в S3. + - `CreateMessage` поддерживает `attachment_ids` (`0..10` уникальных UUID этого чата, пустой `text=""` разрешён при наличии вложения; чужие/неизвестные вложения -> `404`). Отсутствующий `attachment_ids` и `[]` нормализуются в `[]` для канонического хеша `Idempotency-Key`. + - `GET /api/v1/attachments/{id}/content` стримит байты из приватного S3 через API без редиректа только участникам чата; при недоступности S3 или PostgreSQL `/health/ready` и эндпоинты вложений быстро (`MaxRetries: 1`, dial `800ms`) возвращают `503 unavailable`. +4. **Согласованность двух хранилищ и Reconciler (Уровень 4 + Трек 2):** + - Реализован `Store.Reconcile` (запускается фоновым тикером под advisory-блокировкой PostgreSQL `pg_try_advisory_lock(20260401)`, а также через CLI `-reconcile-once [-dry-run] [-grace-seconds]`). + - В пределах безопасного `grace_period` (`RECONCILE_GRACE_SECONDS=300` в проде) свежие объекты не трогаются. Объекты S3 без строки БД старше grace удаляются как осиротевшие; отсутствующие в S3 строки `pending` удаляются, а повреждённые `ready` (используемые сообщениями) переводятся в `status='failed', error_code='missing_s3_object'` (`GET /content -> 409`). + +### 2.2. Соответствие `.env.example` + +| Переменная `.env.example` | Использование | Значение по умолчанию | +| --- | --- | --- | +| `S3_ENDPOINT`, `S3_REGION`, `S3_BUCKET` | Внутренний адрес MinIO и приватный бакет | `http://s3:9000`, `us-east-1`, `messenger-attachments` | +| `S3_ACCESS_KEY_ID_FILE`, `S3_SECRET_ACCESS_KEY_FILE` | Секретные ключи ограниченного сервисного пользователя API (c fallback на прямые env) | `/run/secrets/...` | +| `MAX_UPLOAD_BYTES`, `MAX_IMAGE_PIXELS` | Лимиты размера (`10 MiB`) и числа пикселей (`40 MP`) до полной распаковки | `10485760`, `40000000` | +| Остальные (`APP_*`, `SESSION_*`, `DATABASE_*`, `API_REPLICAS`, `IDEMPOTENCY_*`) | Унаследованы без изменений из ЛР3 | как в ЛР3 | + +--- + +## 3. Доказательства критериев + +| Критерий README | Команда / сценарий | Наблюдаемый результат | Файл доказательства | | --- | --- | --- | --- | -| Публичный smoke | make test | TODO | TODO | -| Остальные пункты уровня 3 | TODO | TODO | TODO | -| Все пункты уровня 4, если заявлен | TODO | TODO | TODO | -| Выбранный трек 5, если заявлен | TODO | TODO | TODO | +| Статический контракт и синтаксис | `make check` | `PASS: offline skeleton structure, local refs and Python syntax` | `scripts/check_contract.py` | +| Публичный smoke (уровень 3: upload, message с `attachment_ids`, SHA-256 download, `404` для `eve`) | `make test` | `PASS: lab4 public API scenario` | `tests/smoke.py` | +| Полный рестарт сохранность (включая MinIO S3 volume) | `make test-persistence ACTION_SCRIPT=scripts/restart.sh` | Перезапущены `postgres`, `redis`, `s3`, `api`, `lb`; прежний токен и бинарник вложения сохранены побайтно | `scripts/restart.sh` | +| Отказ одной API-реплики под записью | `make test-failover ACTION_SCRIPT=scripts/stop-one.sh` | `observed_instances_before=2`, `successful_writes=142`, `failed_attempts=0`, `first_recovery_seconds=2.906` | `scripts/stop-one.sh` | +| Оценка 4: негативные сценарии (`413`, `415`, `400`, `404`), никогда не `500` | `make test-grade4` (`negative_validation`) | `>10MiB -> 413`, pixel bomb `7000x7000 -> 413`, обрезанный PNG & JPEG `-> 415`, подмена MIME `-> 415`, опасные filename `-> 400`, чужой чат / посторонний `-> 404` | [`evidence/grade4_checks.json`](evidence/grade4_checks.json) | +| Оценка 4: загрузка на реплику A -> остановка A -> скачивание с реплики B + отсутствие локальных файлов | `make test-grade4` (`cross_replica_failover_download`) | Загрузка на `4f15587f6c21`, остановка, скачивание с `0238360ce86c` с совпадением `SHA-256`; `0` временных файлов на API | [`evidence/grade4_checks.json`](evidence/grade4_checks.json) | +| Оценка 4: приватный bucket вне API, IAM минимальных прав, закрытая консоль, идемпотентный `s3-init` | `make test-grade4` (`private_bucket_and_iam`) | Прямой `GET http://s3:9000/messenger-attachments/...` из сети без авторизации даёт `403 Forbidden`; порты S3/консоли наружу закрыты (`0`); API использует `messenger_api_key`; повтор `s3-init` -> код `0` | [`evidence/grade4_checks.json`](evidence/grade4_checks.json) | +| Оценка 4: отказ S3 без `500` | `make test-grade4` (`s3_outage_handling`) | При остановке `s3`: `/health/ready -> 503`, `POST upload -> 503`, `GET content -> 503` | [`evidence/grade4_checks.json`](evidence/grade4_checks.json) | +| Оценка 4: `>=10` одновременных загрузок и ограниченность RAM | `make test-grade4` (`concurrent_streaming_memory`) | 12 одновременных загрузок PNG по `4.41 MiB` (`52.89 MiB` суммарно за `3.251s`): память реплик осталась `43.64 -> 56.23 MiB` и `51.44 -> 52.38 MiB` из лимита `256 MiB` | [`evidence/grade4_checks.json`](evidence/grade4_checks.json) | +| Базовая нагрузка `tests/load.py` | `python3 tests/load.py --duration 15 --concurrency 4 --output evidence/load.json` | `10001` успешных POST за 15s, `666.386 RPS`, `0` ошибок, `p50=4.893ms`, `p95=10.807ms`, `p99=20.305ms` | [`evidence/load.json`](evidence/load.json) | +| Оценка 5 Трек 2: сверщик PostgreSQL <-> S3 | `make test-track2` | `dry-run` (79 S3 / 78 DB за `18.4ms`) -> `apply` удалил 3 просроченных orphan в S3 и stale `pending`, пометил 1 missing `ready` как `failed`, не тронул свежий объект в grace и сохранил все 25 здоровых; повторный прогон дал 0 изменений (`19.7ms`) | [`evidence/track2_reconciler.json`](evidence/track2_reconciler.json) | -TODO: добавить отдельную строку для каждого заявленного критерия. PASS без команды и наблюдения не является доказательством. Проверка инфраструктурного hook требует подтверждения, какие реальные процессы/хранилища перезапущены. +--- -## Эксперименты и отказы +## 4. Эксперименты и отказы -TODO: гипотеза, hardware/лимиты, версии, объём данных, профиль нагрузки, concurrency/arrival rate, длительность/прогрев, повторы, успешные RPS, ошибки, p50/p95/p99, CPU/RAM. Сохранить raw summary без tokens и объяснить ограничения измерения. +### 4.1. Матрица отказов | Воздействие | Что ожидаем | Что наблюдали | Время восстановления / потеря данных | | --- | --- | --- | --- | -| TODO | TODO | TODO | TODO | +| Остановка загрузившей API-реплики (`docker stop`) | HAProxy выводит её из пула, другая реплика отдаёт файл побайтно из MinIO | `0238360ce86c` отдал точный оригинал (SHA-256 совпал); `make test-failover`: `142` записи, `failed_attempts=0`, `2.906s` | `0` потерь данных, `0` ошибок клиентов | +| Остановка контейнера `s3` | API быстро переходит в `503 unavailable` без `500` и зависаний | `/health/ready`, upload и download сразу отдавали `503` (благодаря `MaxRetries: 1` и dial `800ms`) | Восстановление за <3s после старта `s3` | +| Полный рестарт `postgres + redis + s3 + api + lb` (`scripts/restart.sh`) | Сессия, метаданные и объекты MinIO сохраняются в volumes | Прежний Bearer-токен скачал исходный PNG с прежним SHA-256 | `0` потерь метаданных и файлов | -## Ограничения и следующие шаги +### 4.2. Трек 2 — Согласованность двух хранилищ (Reconciler) -TODO: какие отказы выдерживаются и какие нет; что не реализовано; где учебное упрощение; какое улучшение подтверждено, а какое пока гипотеза. +- **Гипотеза:** В двуххранилищной архитектуре (S3 + PostgreSQL без 2PC) возможны сбои между `PutObject` и `COMMIT` (либо внешняя потеря объекта S3). Повторяемый сверщик с безопасным `grace_period`, `dry-run` и PostgreSQL advisory-блокировкой устраняет оба класса рассинхронизации за `1` `ListObjects` + `O(N)` строк БД без удаления активных или ещё загружаемых вложений. +- **Методика (`student-tests/test_track2_reconciler.py`):** + 1. Загружено `25` здоровых вложений (10 из них привязаны к сообщениям). + 2. Внедрены аномалии с обеих сторон: **3** осиротевших S3-объекта (возраст > `grace=5s`), **1** `ready`-вложение с удалённым в MinIO объектом (используемое в сообщении), **1** устаревшая `pending`-строка в БД без объекта, а также **1** «свежий» S3-объект внутри окна `grace_period=5s` (имитация активной загрузки). +- **Наблюдаемые результаты ([`evidence/track2_reconciler.json`](evidence/track2_reconciler.json)):** + - **`--dry-run`:** просканировал `79` S3-объектов (`1` вызов `ListObjects`) и `78` строк БД (`2` SQL-запроса) за **`18.395 ms`**. Обнаружил все `3` просроченных orphan в S3 и обе missing-строки БД, пропустил свежий объект в окне `grace`, сохранил все `75` здоровых, мутаций `0`. + - **`--apply`:** выполнил за **`27.038 ms`** (`1` `ListObjects`, `3` `RemoveObject`, `4` SQL-запроса): удалил строго `3` старых S3 orphan и `1` `pending`-строку, перевёл missing `ready`-вложение в `status='failed'` (`error_code='missing_s3_object'`, после чего `GET /content -> 409`), свежий объект в окне `grace` и все здоровые вложения остались нетронутыми. + - **Повторный запуск (идемпотентность):** `76` S3 / `77` DB за **`19.697 ms`** — `0` удалений, `0` обновлений, все `25` контрольных вложений скачаны с исходным `SHA-256`. + +--- + +## 5. Ограничения и следующие шаги + +- **Ограничения:** + - Загрузка идёт синхронным proxy-потоком через API (`201 ready` без асинхронной очереди миниатюр); пик локального spooling в `/tmp` ограничен размером `MAX_UPLOAD_BYTES` (`10 MiB`) и удаляется в `defer` — при большом числе параллельных загрузок требуется достаточный объём ephemeral `/tmp` и лимит на конкурентность на уровне LB/API. + - На одном хосте MinIO S3 развёрнут одним процессом без erasure coding/репликации (учебное хранилище на одном volume). +- **Следующий шаг (ЛР5):** перейти к асинхронному ответу `202 Attachment` (`queued -> processing -> ready`), брокеру сообщений, staging и фоновому worker генерации `thumbnail` (и `crop`/`optimized`). diff --git a/cmd/server/main.go b/cmd/server/main.go new file mode 100644 index 00000000..9674f14b --- /dev/null +++ b/cmd/server/main.go @@ -0,0 +1,283 @@ +package main + +import ( + "context" + "encoding/json" + "errors" + "flag" + "fmt" + "log" + "net/http" + "os" + "os/signal" + "path/filepath" + "sort" + "strconv" + "strings" + "syscall" + "time" + + "github.com/google/uuid" + "messenger/internal/handler" + "messenger/internal/s3store" + "messenger/internal/store" +) + +func getEnv(key, defaultVal string) string { + if val := strings.TrimSpace(os.Getenv(key)); val != "" { + return val + } + return defaultVal +} + +func getEnvInt(key string, defaultVal int) int { + if val := strings.TrimSpace(os.Getenv(key)); val != "" { + if n, err := strconv.Atoi(val); err == nil { + return n + } + } + return defaultVal +} + +func getEnvInt64(key string, defaultVal int64) int64 { + if val := strings.TrimSpace(os.Getenv(key)); val != "" { + if n, err := strconv.ParseInt(val, 10, 64); err == nil { + return n + } + } + return defaultVal +} + +func getEnvBool(key string, defaultVal bool) bool { + if val := strings.TrimSpace(os.Getenv(key)); val != "" { + if b, err := strconv.ParseBool(val); err == nil { + return b + } + } + return defaultVal +} + +func resolveSecretOrEnv(fileEnvKey, directEnvKey, defaultVal string) string { + if filePath := strings.TrimSpace(os.Getenv(fileEnvKey)); filePath != "" { + if data, err := os.ReadFile(filePath); err == nil { + if trimmed := strings.TrimSpace(string(data)); trimmed != "" { + return trimmed + } + } + } + if direct := strings.TrimSpace(os.Getenv(directEnvKey)); direct != "" { + return direct + } + return defaultVal +} + +func resolveInstanceID() string { + if id := strings.TrimSpace(os.Getenv("INSTANCE_ID")); id != "" { + return id + } + if host, err := os.Hostname(); err == nil && strings.TrimSpace(host) != "" { + return strings.TrimSpace(host) + } + return "api-" + uuid.New().String()[:12] +} + +func loadMigrations(dir string) ([]store.MigrationFile, error) { + entries, err := os.ReadDir(dir) + if err != nil { + return nil, fmt.Errorf("read migrations dir %s: %w", dir, err) + } + var names []string + for _, entry := range entries { + if !entry.IsDir() && strings.HasSuffix(entry.Name(), ".sql") { + names = append(names, entry.Name()) + } + } + sort.Strings(names) + if len(names) == 0 { + return nil, fmt.Errorf("no .sql migration files found in %s", dir) + } + + migrations := make([]store.MigrationFile, 0, len(names)) + for _, name := range names { + raw, err := os.ReadFile(filepath.Join(dir, name)) + if err != nil { + return nil, fmt.Errorf("read migration %s: %w", name, err) + } + migrations = append(migrations, store.MigrationFile{ + Version: name, + SQL: string(raw), + }) + } + return migrations, nil +} + +func main() { + migrateOnly := flag.Bool("migrate-only", false, "Run database migrations and exit") + reconcileOnce := flag.Bool("reconcile-once", false, "Run two-store (DB <-> S3) reconciliation once and exit") + reconcileDryRun := flag.Bool("dry-run", false, "Dry-run mode for -reconcile-once") + reconcileGraceSec := flag.Int("grace-seconds", -1, "Override grace period in seconds for -reconcile-once") + flag.Parse() + + host := getEnv("APP_HOST", "0.0.0.0") + port := getEnv("APP_PORT", "8080") + appEnv := getEnv("APP_ENV", "lab") + sessionTTL := getEnvInt("SESSION_TTL_SECONDS", 3600) + idempotencyTTL := getEnvInt("IDEMPOTENCY_TTL_SECONDS", 86400) + maxConns := int32(getEnvInt("DB_MAX_CONNS", 12)) + minConns := int32(getEnvInt("DB_MIN_CONNS", 2)) + queryTimeoutMs := getEnvInt("DB_QUERY_TIMEOUT_MS", 5000) + drainDelayMs := getEnvInt("SHUTDOWN_DRAIN_DELAY_MS", 1500) + cacheMode := getEnv("CACHE_MODE", "write_through_versioned") + writeMode := getEnv("WRITE_MODE", "pipeline") + migrationsDir := getEnv("MIGRATIONS_DIR", "migrations") + runMigrations := *migrateOnly || getEnvBool("RUN_MIGRATIONS", true) + + maxUploadBytes := getEnvInt64("MAX_UPLOAD_BYTES", 10485760) + maxImagePixels := getEnvInt64("MAX_IMAGE_PIXELS", 40000000) + reconcileIntervalSec := getEnvInt("RECONCILE_INTERVAL_SECONDS", 60) + reconcileGraceEnv := getEnvInt("RECONCILE_GRACE_SECONDS", 300) + if *reconcileGraceSec >= 0 { + reconcileGraceEnv = *reconcileGraceSec + } + + instanceID := resolveInstanceID() + + dbURL := resolveSecretOrEnv("DATABASE_URL_FILE", "DATABASE_URL", "postgres://messenger:messenger_lab4_pass@localhost:5432/messenger?sslmode=disable") + cacheURL := resolveSecretOrEnv("CACHE_URL_FILE", "CACHE_URL", "redis://localhost:6379/0") + + ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM) + defer stop() + + st, err := store.NewPostgresStore(ctx, store.Config{ + DatabaseURL: dbURL, + CacheURL: cacheURL, + MaxConns: maxConns, + MinConns: minConns, + QueryTimeout: time.Duration(queryTimeoutMs) * time.Millisecond, + CacheMode: cacheMode, + WriteMode: writeMode, + }) + if err != nil { + log.Fatalf("Failed to initialize store: %v", err) + } + defer st.Close() + + if runMigrations && !*reconcileOnce { + migrations, err := loadMigrations(migrationsDir) + if err != nil { + log.Fatalf("Failed to load migrations: %v", err) + } + + var migErr error + for attempt := 1; attempt <= 30; attempt++ { + migErr = st.RunMigrations(ctx, migrations) + if migErr == nil { + break + } + log.Printf("Waiting for PostgreSQL to apply migrations (attempt %d/30): %v", attempt, migErr) + select { + case <-ctx.Done(): + log.Fatalf("Interrupted while waiting for database migrations") + case <-time.After(1 * time.Second): + } + } + if migErr != nil { + log.Fatalf("Could not apply database migrations after 30 attempts: %v", migErr) + } + } + + if *migrateOnly { + log.Println("Migrate-only flag set; exiting 0.") + return + } + + s3Endpoint := getEnv("S3_ENDPOINT", "http://s3:9000") + s3Region := getEnv("S3_REGION", "us-east-1") + s3Bucket := getEnv("S3_BUCKET", "messenger-attachments") + s3AccessKey := resolveSecretOrEnv("S3_ACCESS_KEY_ID_FILE", "S3_ACCESS_KEY_ID", "messenger_api_key") + s3SecretKey := resolveSecretOrEnv("S3_SECRET_ACCESS_KEY_FILE", "S3_SECRET_ACCESS_KEY", "messenger_api_secret_key_lab4") + + s3Client, err := s3store.NewClient(s3store.Config{ + Endpoint: s3Endpoint, + Region: s3Region, + Bucket: s3Bucket, + AccessKey: s3AccessKey, + SecretKey: s3SecretKey, + }) + if err != nil { + log.Fatalf("Failed to initialize S3 client: %v", err) + } + + if *reconcileOnce { + grace := time.Duration(reconcileGraceEnv) * time.Second + report, rErr := st.Reconcile(ctx, s3Client, grace, *reconcileDryRun) + if rErr != nil { + log.Fatalf("Reconcile failed: %v", rErr) + } + out, _ := json.MarshalIndent(report, "", " ") + fmt.Println(string(out)) + return + } + + // Background two-store reconciliation loop (only 1 replica acquires advisory lock per interval). + if reconcileIntervalSec > 0 { + go func() { + ticker := time.NewTicker(time.Duration(reconcileIntervalSec) * time.Second) + defer ticker.Stop() + grace := time.Duration(reconcileGraceEnv) * time.Second + for { + select { + case <-ctx.Done(): + return + case <-ticker.C: + rCtx, rCancel := context.WithTimeout(ctx, 25*time.Second) + rep, rErr := st.Reconcile(rCtx, s3Client, grace, false) + rCancel() + if rErr != nil { + log.Printf("WARN: background reconcile error: %v", rErr) + } else if rep.LockAcquired && (len(rep.OrphanS3Deleted) > 0 || len(rep.MissingS3MarkedFailed) > 0 || len(rep.MissingS3Deleted) > 0) { + b, _ := json.Marshal(rep) + log.Printf("Background reconcile summary: %s", string(b)) + } + } + } + }() + } + + h, apiHandler := handler.RegisterRoutes(st, s3Client, sessionTTL, idempotencyTTL, maxUploadBytes, maxImagePixels, instanceID) + + addr := fmt.Sprintf("%s:%s", host, port) + srv := &http.Server{ + Addr: addr, + Handler: h, + ReadHeaderTimeout: 5 * time.Second, + ReadTimeout: 35 * time.Second, + WriteTimeout: 35 * time.Second, + IdleTimeout: 60 * time.Second, + } + + go func() { + log.Printf("Starting messenger server (Lab 4, instance=%s) on %s (env=%s, s3=%s/%s, maxUpload=%dB, maxPixels=%d)", + instanceID, addr, appEnv, s3Endpoint, s3Bucket, maxUploadBytes, maxImagePixels) + if err := srv.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) { + log.Fatalf("ListenAndServe error: %v", err) + } + }() + + <-ctx.Done() + log.Printf("Instance %s received termination signal: marking /health/ready=503 and draining...", instanceID) + apiHandler.SetDraining(true) + + if drainDelayMs > 0 { + time.Sleep(time.Duration(drainDelayMs) * time.Millisecond) + } + + shutdownCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + defer cancel() + + if err := srv.Shutdown(shutdownCtx); err != nil { + log.Printf("Server shutdown error: %v", err) + } else { + log.Printf("Instance %s stopped gracefully", instanceID) + } +} diff --git a/compose.yaml b/compose.yaml new file mode 100644 index 00000000..354a185e --- /dev/null +++ b/compose.yaml @@ -0,0 +1,212 @@ +x-lab-number: 4 +x-required-roles: + - api + - postgres + - redis + - s3 + - load_balancer + +services: + postgres: + image: postgres:18-alpine + restart: unless-stopped + environment: + POSTGRES_USER: ${POSTGRES_USER:-messenger} + POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-messenger_lab4_pass} + POSTGRES_DB: ${POSTGRES_DB:-messenger} + PGDATA: /var/lib/postgresql/data/pgdata + volumes: + - pgdata:/var/lib/postgresql/data + healthcheck: + test: ["CMD-SHELL", "pg_isready -U $${POSTGRES_USER:-messenger} -d $${POSTGRES_DB:-messenger}"] + interval: 2s + timeout: 2s + retries: 15 + start_period: 2s + deploy: + resources: + limits: + cpus: "${DB_CPU_LIMIT:-1.0}" + memory: "${DB_MEM_LIMIT:-512M}" + networks: + - backend + + redis: + image: redis:7.4-alpine + restart: unless-stopped + command: ["redis-server", "/usr/local/etc/redis/redis.conf"] + volumes: + - redisdata:/data + - ./infra/redis.conf:/usr/local/etc/redis/redis.conf:ro + healthcheck: + test: ["CMD", "redis-cli", "ping"] + interval: 2s + timeout: 2s + retries: 15 + start_period: 2s + deploy: + resources: + limits: + cpus: "${REDIS_CPU_LIMIT:-0.5}" + memory: "${REDIS_MEM_LIMIT:-256M}" + networks: + - backend + + s3: + image: minio/minio@sha256:14cea493d9a34af32f524e538b8346cf79f3321eff8e708c1e2960462bd8936e + restart: unless-stopped + command: ["server", "/data", "--address", ":9000", "--console-address", ":9001"] + environment: + MINIO_ROOT_USER: ${MINIO_ROOT_USER:-minio_admin_local} + MINIO_ROOT_PASSWORD: ${MINIO_ROOT_PASSWORD:-minio_admin_secret_lab4} + MINIO_BROWSER: "off" + volumes: + - s3data:/data + healthcheck: + test: ["CMD", "curl", "-fsS", "http://127.0.0.1:9000/minio/health/live"] + interval: 2s + timeout: 2s + retries: 15 + start_period: 2s + deploy: + resources: + limits: + cpus: "${S3_CPU_LIMIT:-1.0}" + memory: "${S3_MEM_LIMIT:-512M}" + networks: + - backend + + s3-init: + image: minio/mc@sha256:a7fe349ef4bd8521fb8497f55c6042871b2ae640607cf99d9bede5e9bdf11727 + restart: "no" + entrypoint: ["/bin/sh", "/infra/s3-bootstrap.sh"] + environment: + MINIO_ROOT_USER: ${MINIO_ROOT_USER:-minio_admin_local} + MINIO_ROOT_PASSWORD: ${MINIO_ROOT_PASSWORD:-minio_admin_secret_lab4} + S3_BUCKET: ${S3_BUCKET:-messenger-attachments} + S3_ACCESS_KEY_ID: ${S3_ACCESS_KEY_ID:-messenger_api_key} + S3_SECRET_ACCESS_KEY: ${S3_SECRET_ACCESS_KEY:-messenger_api_secret_key_lab4} + volumes: + - ./infra/s3-bootstrap.sh:/infra/s3-bootstrap.sh:ro + - ./infra/minio-api-policy.json:/infra/minio-api-policy.json:ro + depends_on: + s3: + condition: service_healthy + networks: + - backend + + migrate: + build: + context: . + dockerfile: Dockerfile + image: mtusi-messenger:lab4 + entrypoint: ["/app/messenger", "-migrate-only"] + restart: "no" + environment: + DATABASE_URL_FILE: ${DATABASE_URL_FILE:-/run/secrets/database_url} + DATABASE_URL: ${DATABASE_URL:-postgres://messenger:messenger_lab4_pass@postgres:5432/messenger?sslmode=disable} + CACHE_URL_FILE: ${CACHE_URL_FILE:-/run/secrets/cache_url} + CACHE_URL: ${CACHE_URL:-redis://redis:6379/0} + MIGRATIONS_DIR: /app/migrations + depends_on: + postgres: + condition: service_healthy + deploy: + resources: + limits: + cpus: "0.5" + memory: "128M" + networks: + - backend + + api: + build: + context: . + dockerfile: Dockerfile + image: mtusi-messenger:lab4 + restart: unless-stopped + stop_grace_period: 10s + environment: + APP_HOST: 0.0.0.0 + APP_PORT: 8080 + APP_ENV: ${APP_ENV:-lab} + SESSION_TTL_SECONDS: ${SESSION_TTL_SECONDS:-3600} + IDEMPOTENCY_TTL_SECONDS: ${IDEMPOTENCY_TTL_SECONDS:-86400} + SESSION_STORE: ${SESSION_STORE:-redis} + RUN_MIGRATIONS: "false" + MIGRATIONS_DIR: /app/migrations + SHUTDOWN_DRAIN_DELAY_MS: ${SHUTDOWN_DRAIN_DELAY_MS:-1500} + DATABASE_URL_FILE: ${DATABASE_URL_FILE:-/run/secrets/database_url} + DATABASE_URL: ${DATABASE_URL:-postgres://messenger:messenger_lab4_pass@postgres:5432/messenger?sslmode=disable} + CACHE_URL_FILE: ${CACHE_URL_FILE:-/run/secrets/cache_url} + CACHE_URL: ${CACHE_URL:-redis://redis:6379/0} + S3_ENDPOINT: ${S3_ENDPOINT:-http://s3:9000} + S3_REGION: ${S3_REGION:-us-east-1} + S3_BUCKET: ${S3_BUCKET:-messenger-attachments} + S3_ACCESS_KEY_ID_FILE: ${S3_ACCESS_KEY_ID_FILE:-/run/secrets/s3_access_key_id} + S3_ACCESS_KEY_ID: ${S3_ACCESS_KEY_ID:-messenger_api_key} + S3_SECRET_ACCESS_KEY_FILE: ${S3_SECRET_ACCESS_KEY_FILE:-/run/secrets/s3_secret_access_key} + S3_SECRET_ACCESS_KEY: ${S3_SECRET_ACCESS_KEY:-messenger_api_secret_key_lab4} + MAX_UPLOAD_BYTES: ${MAX_UPLOAD_BYTES:-10485760} + MAX_IMAGE_PIXELS: ${MAX_IMAGE_PIXELS:-40000000} + RECONCILE_INTERVAL_SECONDS: ${RECONCILE_INTERVAL_SECONDS:-60} + RECONCILE_GRACE_SECONDS: ${RECONCILE_GRACE_SECONDS:-300} + DB_MAX_CONNS: ${DB_MAX_CONNS:-12} + DB_MIN_CONNS: ${DB_MIN_CONNS:-2} + DB_QUERY_TIMEOUT_MS: ${DB_QUERY_TIMEOUT_MS:-5000} + CACHE_MODE: ${CACHE_MODE:-write_through_versioned} + WRITE_MODE: ${WRITE_MODE:-pipeline} + depends_on: + postgres: + condition: service_healthy + redis: + condition: service_healthy + migrate: + condition: service_completed_successfully + s3-init: + condition: service_completed_successfully + deploy: + replicas: ${API_REPLICAS:-2} + resources: + limits: + cpus: "${API_CPU_LIMIT:-1.0}" + memory: "${API_MEM_LIMIT:-256M}" + networks: + - backend + + lb: + image: haproxy:3.0-alpine + restart: unless-stopped + ports: + - "${APP_PORT:-8080}:80" + volumes: + - ./infra/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg:ro + healthcheck: + test: ["CMD-SHELL", "haproxy -c -f /usr/local/etc/haproxy/haproxy.cfg && kill -0 1"] + interval: 2s + timeout: 2s + retries: 10 + start_period: 1s + depends_on: + api: + condition: service_healthy + deploy: + resources: + limits: + cpus: "${LB_CPU_LIMIT:-0.5}" + memory: "${LB_MEM_LIMIT:-128M}" + networks: + - backend + +volumes: + pgdata: + name: mtusi_lab4_pgdata + redisdata: + name: mtusi_lab4_redisdata + s3data: + name: mtusi_lab4_s3data + +networks: + backend: + name: mtusi_lab4_backend + driver: bridge diff --git a/evidence/grade4_checks.json b/evidence/grade4_checks.json new file mode 100644 index 00000000..c9ce0a71 --- /dev/null +++ b/evidence/grade4_checks.json @@ -0,0 +1,77 @@ +{ + "timestamp_utc": "2026-11-16T23:52:48Z", + "base_url": "http://localhost:8080", + "checks": { + "negative_validation": { + "status": "PASS", + "oversized_10mib_status": 413, + "pixel_bomb_49mp_status": 413, + "corrupted_png_status": 415, + "corrupted_jpg_status": 415, + "mime_spoof_status": 415, + "bad_filename_status": 400, + "outsider_and_cross_chat_status": 404 + }, + "cross_replica_failover_download": { + "status": "PASS", + "uploaded_by": "4f15587f6c21", + "downloaded_from_after_stop": "0238360ce86c", + "sha256_verified": true, + "api_temp_files_remaining": 0, + "object_key_internal": "attachments/d5a638fc-2400-4fab-9492-f1d36911298c/2ed2b132-7f96-4db7-80e8-9b68e0e2fdf7" + }, + "private_bucket_and_iam": { + "status": "PASS", + "s3_exposed_host_ports": 0, + "minio_console_disabled": true, + "api_uses_scoped_credentials": "messenger_api_key", + "direct_unauth_s3_get": "403 Forbidden", + "s3_init_idempotent_rerun": 0 + }, + "s3_outage_handling": { + "status": "PASS", + "ready_during_s3_outage": 503, + "upload_during_s3_outage": 503, + "download_during_s3_outage": 503 + }, + "concurrent_streaming_memory": { + "status": "PASS", + "concurrent_uploads": 12, + "file_size_bytes": 4621523, + "file_size_mib": 4.41, + "total_payload_mib": 52.89, + "elapsed_seconds": 3.251, + "api_memory_baseline": { + "lab4-api-1": "43.64MiB / 256MiB", + "lab4-api-2": "51.44MiB / 256MiB" + }, + "stats_during_concurrent_upload": { + "lab4-api-1": { + "mem": "56.23MiB / 256MiB", + "cpu": "0.79%" + }, + "lab4-api-2": { + "mem": "52.38MiB / 256MiB", + "cpu": "0.82%" + }, + "lab4-lb-1": { + "mem": "14.69MiB / 128MiB", + "cpu": "0.23%" + }, + "lab4-redis-1": { + "mem": "3.734MiB / 256MiB", + "cpu": "0.35%" + }, + "lab4-s3-1": { + "mem": "95.27MiB / 512MiB", + "cpu": "0.41%" + }, + "lab4-postgres-1": { + "mem": "51.04MiB / 512MiB", + "cpu": "0.11%" + } + }, + "api_temp_files_after": 0 + } + } +} diff --git a/evidence/load.json b/evidence/load.json new file mode 100644 index 00000000..bbb4fbea --- /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.008, + "concurrency": 4, + "successful_requests": 10001, + "failed_requests": 0, + "error_ratio": 0.0, + "successful_rps": 666.386, + "successful_latency_ms": { + "p50": 4.893, + "p95": 10.807, + "p99": 20.305 + }, + "note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead." +} diff --git a/evidence/track2_reconciler.json b/evidence/track2_reconciler.json new file mode 100644 index 00000000..9b579655 --- /dev/null +++ b/evidence/track2_reconciler.json @@ -0,0 +1,96 @@ +{ + "timestamp_utc": "2026-11-16T23:53:59Z", + "healthy_attachments_verified": 25, + "injected_faults": { + "aged_orphan_s3_objects": 3, + "fresh_orphan_s3_inside_grace": 1, + "missing_s3_ready_referenced": 1, + "missing_s3_pending_stale": 1 + }, + "dry_run_report": { + "dry_run": true, + "grace_period_seconds": 5, + "lock_acquired": true, + "s3_objects_scanned": 79, + "db_rows_scanned": 78, + "orphan_s3_found": [ + "attachments/c0ab7718-6287-4ab8-81b9-0d89b3ab0b06/orphan_4fd8f77f-64b1-470c-b973-1c1255a86352", + "attachments/c0ab7718-6287-4ab8-81b9-0d89b3ab0b06/orphan_de20049e-1906-4519-a652-ee54e9e13101", + "attachments/c0ab7718-6287-4ab8-81b9-0d89b3ab0b06/orphan_fe71cc09-110e-406f-96a0-b3ca52491453" + ], + "orphan_s3_deleted": [], + "orphan_s3_skipped_grace": [ + "attachments/c0ab7718-6287-4ab8-81b9-0d89b3ab0b06/fresh_af92437e-8430-4d64-b5b3-b90a6e92eb39" + ], + "missing_s3_found": [ + "a4d2ad97-ab03-4c84-b657-392221d850d6", + "f8aa5170-f723-448d-9fac-880bc06e4da0" + ], + "missing_s3_marked_failed": [], + "missing_s3_deleted": [], + "missing_s3_skipped_grace": [], + "healthy_preserved": 75, + "s3_list_calls": 1, + "s3_remove_calls": 0, + "db_queries": 2, + "duration_ms": 18.395 + }, + "apply_report": { + "dry_run": false, + "grace_period_seconds": 5, + "lock_acquired": true, + "s3_objects_scanned": 79, + "db_rows_scanned": 78, + "orphan_s3_found": [ + "attachments/c0ab7718-6287-4ab8-81b9-0d89b3ab0b06/orphan_4fd8f77f-64b1-470c-b973-1c1255a86352", + "attachments/c0ab7718-6287-4ab8-81b9-0d89b3ab0b06/orphan_de20049e-1906-4519-a652-ee54e9e13101", + "attachments/c0ab7718-6287-4ab8-81b9-0d89b3ab0b06/orphan_fe71cc09-110e-406f-96a0-b3ca52491453" + ], + "orphan_s3_deleted": [ + "attachments/c0ab7718-6287-4ab8-81b9-0d89b3ab0b06/orphan_4fd8f77f-64b1-470c-b973-1c1255a86352", + "attachments/c0ab7718-6287-4ab8-81b9-0d89b3ab0b06/orphan_de20049e-1906-4519-a652-ee54e9e13101", + "attachments/c0ab7718-6287-4ab8-81b9-0d89b3ab0b06/orphan_fe71cc09-110e-406f-96a0-b3ca52491453" + ], + "orphan_s3_skipped_grace": [ + "attachments/c0ab7718-6287-4ab8-81b9-0d89b3ab0b06/fresh_af92437e-8430-4d64-b5b3-b90a6e92eb39" + ], + "missing_s3_found": [ + "a4d2ad97-ab03-4c84-b657-392221d850d6", + "f8aa5170-f723-448d-9fac-880bc06e4da0" + ], + "missing_s3_marked_failed": [ + "a4d2ad97-ab03-4c84-b657-392221d850d6" + ], + "missing_s3_deleted": [ + "f8aa5170-f723-448d-9fac-880bc06e4da0" + ], + "missing_s3_skipped_grace": [], + "healthy_preserved": 75, + "s3_list_calls": 1, + "s3_remove_calls": 3, + "db_queries": 4, + "duration_ms": 27.038 + }, + "idempotent_rerun_report": { + "dry_run": false, + "grace_period_seconds": 5, + "lock_acquired": true, + "s3_objects_scanned": 76, + "db_rows_scanned": 77, + "orphan_s3_found": [], + "orphan_s3_deleted": [], + "orphan_s3_skipped_grace": [ + "attachments/c0ab7718-6287-4ab8-81b9-0d89b3ab0b06/fresh_af92437e-8430-4d64-b5b3-b90a6e92eb39" + ], + "missing_s3_found": [], + "missing_s3_marked_failed": [], + "missing_s3_deleted": [], + "missing_s3_skipped_grace": [], + "healthy_preserved": 75, + "s3_list_calls": 1, + "s3_remove_calls": 0, + "db_queries": 2, + "duration_ms": 19.697 + }, + "status": "PASS" +} diff --git a/go.mod b/go.mod new file mode 100644 index 00000000..3ee4166a --- /dev/null +++ b/go.mod @@ -0,0 +1,31 @@ +module messenger + +go 1.24 + +require ( + github.com/google/uuid v1.6.0 + github.com/jackc/pgx/v5 v5.7.2 + github.com/redis/go-redis/v9 v9.17.2 + golang.org/x/crypto v0.33.0 + golang.org/x/sync v0.11.0 +) + +require ( + github.com/cespare/xxhash/v2 v2.3.0 // indirect + github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect + github.com/dustin/go-humanize v1.0.1 // indirect + github.com/go-ini/ini v1.67.0 // indirect + github.com/goccy/go-json v0.10.5 // 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.17.11 // indirect + github.com/klauspost/cpuid/v2 v2.2.9 // indirect + github.com/minio/crc64nvme v1.0.1 // indirect + github.com/minio/md5-simd v1.1.2 // indirect + github.com/minio/minio-go/v7 v7.0.88 // indirect + github.com/rs/xid v1.6.0 // indirect + golang.org/x/net v0.35.0 // indirect + golang.org/x/sys v0.30.0 // indirect + golang.org/x/text v0.22.0 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 00000000..bbb3b3c8 --- /dev/null +++ b/go.sum @@ -0,0 +1,64 @@ +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/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78= +github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc= +github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY= +github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto= +github.com/go-ini/ini v1.67.0 h1:z6ZrTEZqSWOTyH2FlglNbNgARyHG8oLW9gMELqKr06A= +github.com/go-ini/ini v1.67.0/go.mod h1:ByCAeIL28uOIIG0E3PJtZPDL8WnHpFKFOtgjp+3Ies8= +github.com/goccy/go-json v0.10.5 h1:Fq85nIqj+gXn/S5ahsiTlK3TmC85qgirsdTP/+DeaC4= +github.com/goccy/go-json v0.10.5/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PULtXL6M= +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/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.7.2 h1:mLoDLV6sonKlvjIEsV56SkWNCnuNv531l94GaIzO+XI= +github.com/jackc/pgx/v5 v5.7.2/go.mod h1:ncY89UGWxg82EykZUwSpUKEfccBGGYq1xjrOpsbsfGQ= +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.17.11 h1:In6xLpyWOi1+C7tXUUWv2ot1QvBjxevKAaI6IXrJmUc= +github.com/klauspost/compress v1.17.11/go.mod h1:pMDklpSncoRMuLFrf1W9Ss9KT+0rH90U12bZKk7uwG0= +github.com/klauspost/cpuid/v2 v2.0.1/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg= +github.com/klauspost/cpuid/v2 v2.2.9 h1:66ze0taIn2H33fBvCkXuv9BmCwDfafmiIVpKV9kKGuY= +github.com/klauspost/cpuid/v2 v2.2.9/go.mod h1:rqkxqrZ1EhYM9G+hXH7YdowN5R5RGN6NK4QwQ3WMXF8= +github.com/minio/crc64nvme v1.0.1 h1:DHQPrYPdqK7jQG/Ls5CTBZWeex/2FMS3G5XGkycuFrY= +github.com/minio/crc64nvme v1.0.1/go.mod h1:eVfm2fAzLlxMdUGc0EEBGSMmPwmXD5XiNRpnu9J3bvg= +github.com/minio/md5-simd v1.1.2 h1:Gdi1DZK69+ZVMoNHRXJyNcxrMA4dSxoYHZSQbirFg34= +github.com/minio/md5-simd v1.1.2/go.mod h1:MzdKDxYpY2BT9XQFocsiZf/NKVtR7nkE4RoEpN+20RM= +github.com/minio/minio-go/v7 v7.0.88 h1:v8MoIJjwYxOkehp+eiLIuvXk87P2raUtoU5klrAAshs= +github.com/minio/minio-go/v7 v7.0.88/go.mod h1:33+O8h0tO7pCeCWwBVa07RhVVfB/3vS4kEX7rwYKmIg= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/redis/go-redis/v9 v9.17.2 h1:P2EGsA4qVIM3Pp+aPocCJ7DguDHhqrXNhVcEp4ViluI= +github.com/redis/go-redis/v9 v9.17.2/go.mod h1:u410H11HMLoB+TP67dz8rL9s6QW2j76l0//kSOd3370= +github.com/rs/xid v1.6.0 h1:fV591PaemRlL6JfRxGDEPl69wICngIQ3shQtzfy2gxU= +github.com/rs/xid v1.6.0/go.mod h1:7XoLgs4eV+QndskICGsho+ADou8ySMSjJKDIan90Nz0= +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.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk= +github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= +github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= +golang.org/x/crypto v0.33.0 h1:IOBPskki6Lysi0lo9qQvbxiQ+FvsCC/YWOecCHAixus= +golang.org/x/crypto v0.33.0/go.mod h1:bVdXmD7IV/4GdElGPozy6U7lWdRXA4qyRVGJV57uQ5M= +golang.org/x/net v0.35.0 h1:T5GQRQb2y08kTAByq9L4/bz8cipCdA8FbRTXewonqY8= +golang.org/x/net v0.35.0/go.mod h1:EglIi67kWsHKlRzzVMUD93VMSWGFOMSZgxFjparz1Qk= +golang.org/x/sync v0.11.0 h1:GGz8+XQP4FvTTrjZPzNKTMFtSXH80RAzG+5ghFPgK9w= +golang.org/x/sync v0.11.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= +golang.org/x/sys v0.30.0 h1:QjkSwP/36a20jFYWkSue1YwXzLmsV5Gfq7Eiy72C1uc= +golang.org/x/sys v0.30.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/text v0.22.0 h1:bofq7m3/HAFvbF51jz3Q9wLg3jkvSPuiZu/pD1XwgtM= +golang.org/x/text v0.22.0/go.mod h1:YRoo4H8PVmsu+E3Ou7cqLVH8oXWIHVoX0jqUWALQhfY= +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= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/infra/haproxy.cfg b/infra/haproxy.cfg new file mode 100644 index 00000000..5202e2f8 --- /dev/null +++ b/infra/haproxy.cfg @@ -0,0 +1,40 @@ +global + log stdout format raw local0 + maxconn 4096 + +defaults + mode http + log global + option httplog + option dontlognull + option redispatch + retries 2 + timeout connect 2s + timeout client 30s + timeout server 30s + timeout check 1500ms + +resolvers docker_dns + nameserver dns 127.0.0.11:53 + resolve_retries 3 + timeout resolve 1s + timeout retry 1s + hold other 2s + hold refused 2s + hold nx 2s + hold timeout 2s + hold valid 2s + hold obsolete 2s + +frontend http_front + bind *:80 + default_backend api_back + +backend api_back + balance roundrobin + http-reuse safe + option httpchk + http-check send meth GET uri /health/ready ver HTTP/1.1 hdr Host localhost + http-check expect status 200 + default-server inter 1s fastinter 500ms downinter 1s fall 2 rise 1 resolvers docker_dns resolve-prefer ipv4 init-addr none maxconn 256 + server-template api 1-8 api:8080 check diff --git a/infra/minio-api-policy.json b/infra/minio-api-policy.json new file mode 100644 index 00000000..6a6cb57d --- /dev/null +++ b/infra/minio-api-policy.json @@ -0,0 +1,26 @@ +{ + "Version": "2012-10-17", + "Statement": [ + { + "Effect": "Allow", + "Action": [ + "s3:GetBucketLocation", + "s3:ListBucket" + ], + "Resource": [ + "arn:aws:s3:::messenger-attachments" + ] + }, + { + "Effect": "Allow", + "Action": [ + "s3:GetObject", + "s3:PutObject", + "s3:DeleteObject" + ], + "Resource": [ + "arn:aws:s3:::messenger-attachments/*" + ] + } + ] +} diff --git a/infra/redis.conf b/infra/redis.conf new file mode 100644 index 00000000..5be985c3 --- /dev/null +++ b/infra/redis.conf @@ -0,0 +1,14 @@ +# Redis 7.4 configuration for MTUCI Lab 3 (Shared Sessions + History Cache) +bind 0.0.0.0 +port 6379 +protected-mode no + +# Durable persistence (AOF + RDB snapshot) so cached sessions survive container restarts +appendonly yes +appendfilename "appendonly.aof" +appendfsync everysec +save 60 1 + +# Memory limits +maxmemory 128mb +maxmemory-policy volatile-lru diff --git a/infra/s3-bootstrap.sh b/infra/s3-bootstrap.sh new file mode 100755 index 00000000..94e98eb8 --- /dev/null +++ b/infra/s3-bootstrap.sh @@ -0,0 +1,9 @@ +#!/bin/sh +set -eu + +mc alias set local http://s3:9000 "${MINIO_ROOT_USER}" "${MINIO_ROOT_PASSWORD}" +mc mb --ignore-existing "local/${S3_BUCKET}" +mc anonymous set none "local/${S3_BUCKET}" +mc admin user add local "${S3_ACCESS_KEY_ID}" "${S3_SECRET_ACCESS_KEY}" +mc admin policy create local messenger-api-policy /infra/minio-api-policy.json +mc admin policy attach local messenger-api-policy --user "${S3_ACCESS_KEY_ID}" diff --git a/internal/auth/password.go b/internal/auth/password.go new file mode 100644 index 00000000..93a39697 --- /dev/null +++ b/internal/auth/password.go @@ -0,0 +1,108 @@ +package auth + +import ( + "crypto/rand" + "crypto/sha256" + "crypto/subtle" + "encoding/base64" + "encoding/hex" + "errors" + "fmt" + "strings" + + "golang.org/x/crypto/argon2" +) + +const ( + argonTime uint32 = 1 + argonMemory uint32 = 19 * 1024 // 19 MiB (OWASP interactive recommendation) + argonThreads uint8 = 2 + argonKeyLen uint32 = 32 + argonSaltLen int = 16 +) + +var dummyHash string + +func init() { + h, err := HashPassword("dummy-password-for-constant-time-check") + if err == nil { + dummyHash = h + } +} + +// HashPassword hashes a UTF-8 password using Argon2id and returns a PHC-formatted string. +func HashPassword(password string) (string, error) { + salt := make([]byte, argonSaltLen) + if _, err := rand.Read(salt); err != nil { + return "", fmt.Errorf("failed to generate salt: %w", err) + } + + hash := argon2.IDKey([]byte(password), salt, argonTime, argonMemory, argonThreads, argonKeyLen) + + b64Salt := base64.RawStdEncoding.EncodeToString(salt) + b64Hash := base64.RawStdEncoding.EncodeToString(hash) + + encoded := fmt.Sprintf("$argon2id$v=%d$m=%d,t=%d,p=%d$%s$%s", + argon2.Version, argonMemory, argonTime, argonThreads, b64Salt, b64Hash) + return encoded, nil +} + +// VerifyPassword compares a UTF-8 password against an Argon2id PHC string in constant time. +func VerifyPassword(password, encodedHash string) (bool, error) { + parts := strings.Split(encodedHash, "$") + if len(parts) != 6 || parts[1] != "argon2id" { + return false, errors.New("invalid argon2id hash format") + } + + var version int + if _, err := fmt.Sscanf(parts[2], "v=%d", &version); err != nil { + return false, err + } + + var memory, timeCost uint32 + var threads uint8 + if _, err := fmt.Sscanf(parts[3], "m=%d,t=%d,p=%d", &memory, &timeCost, &threads); err != nil { + return false, err + } + + salt, err := base64.RawStdEncoding.DecodeString(parts[4]) + if err != nil { + return false, err + } + + expectedHash, err := base64.RawStdEncoding.DecodeString(parts[5]) + if err != nil { + return false, err + } + + computedHash := argon2.IDKey([]byte(password), salt, timeCost, memory, threads, uint32(len(expectedHash))) + if subtle.ConstantTimeCompare(computedHash, expectedHash) == 1 { + return true, nil + } + return false, nil +} + +// VerifyDummy runs an Argon2id verification against a dummy hash to prevent timing attacks on unknown usernames. +func VerifyDummy(password string) { + if dummyHash != "" { + _, _ = VerifyPassword(password, dummyHash) + } +} + +// GenerateSessionToken generates an opaque 256-bit (32-byte) random token encoded as 64 hex chars, +// along with its SHA-256 hex digest for storage. +func GenerateSessionToken() (rawToken string, tokenHash string, err error) { + buf := make([]byte, 32) // 256 bits of entropy + if _, err := rand.Read(buf); err != nil { + return "", "", fmt.Errorf("failed to generate random token: %w", err) + } + rawToken = hex.EncodeToString(buf) + tokenHash = HashToken(rawToken) + return rawToken, tokenHash, nil +} + +// HashToken returns the lowercase SHA-256 hex digest of an opaque session token. +func HashToken(rawToken string) string { + sum := sha256.Sum256([]byte(rawToken)) + return hex.EncodeToString(sum[:]) +} diff --git a/internal/auth/password_test.go b/internal/auth/password_test.go new file mode 100644 index 00000000..61e06519 --- /dev/null +++ b/internal/auth/password_test.go @@ -0,0 +1,43 @@ +package auth + +import ( + "strings" + "testing" +) + +func TestHashAndVerifyPassword(t *testing.T) { + pw := "Сложный:Пароль:С:Двоеточиями:2026!🚀" + hash, err := HashPassword(pw) + if err != nil { + t.Fatalf("HashPassword failed: %v", err) + } + if !strings.HasPrefix(hash, "$argon2id$v=19$") { + t.Fatalf("expected Argon2id PHC prefix, got: %s", hash) + } + + ok, err := VerifyPassword(pw, hash) + if err != nil || !ok { + t.Fatalf("expected password verification to succeed, err=%v, ok=%v", err, ok) + } + + okWrong, err := VerifyPassword("WrongPassword-12345!", hash) + if err != nil || okWrong { + t.Fatalf("expected wrong password to fail verification") + } +} + +func TestGenerateSessionToken(t *testing.T) { + raw, hash, err := GenerateSessionToken() + if err != nil { + t.Fatalf("GenerateSessionToken failed: %v", err) + } + if len(raw) != 64 { + t.Fatalf("expected 64-char hex raw token (256-bit), got len=%d", len(raw)) + } + if len(hash) != 64 { + t.Fatalf("expected 64-char SHA-256 hex digest, got len=%d", len(hash)) + } + if HashToken(raw) != hash { + t.Fatalf("HashToken mismatch") + } +} diff --git a/internal/handler/handler.go b/internal/handler/handler.go new file mode 100644 index 00000000..25a74714 --- /dev/null +++ b/internal/handler/handler.go @@ -0,0 +1,662 @@ +package handler + +import ( + "context" + _ "embed" + "encoding/base64" + "encoding/json" + "errors" + "fmt" + "io" + "net/http" + "strconv" + "strings" + "sync/atomic" + "time" + "unicode" + "unicode/utf8" + + "github.com/google/uuid" + "messenger/internal/media" + "messenger/internal/model" + "messenger/internal/s3store" + "messenger/internal/store" +) + +//go:embed openapi.json +var openAPISpec []byte + +const swaggerUIHTML = ` + + + + + MTUCI Messenger API (Lab 4) - Swagger UI + + + + +
+ + + +` + +type APIHandler struct { + store store.Store + s3 *s3store.Client + sessionTTL int + idempotencyTTL int + maxUploadBytes int64 + maxImagePixels int64 + instanceID string + draining atomic.Bool +} + +func NewAPIHandler(st store.Store, s3 *s3store.Client, sessionTTL, idempotencyTTL int, maxUploadBytes, maxImagePixels int64, instanceID string) *APIHandler { + if sessionTTL <= 0 { + sessionTTL = 3600 + } + if idempotencyTTL <= 0 { + idempotencyTTL = 86400 + } + if maxUploadBytes <= 0 { + maxUploadBytes = 10 * 1024 * 1024 + } + if maxImagePixels <= 0 { + maxImagePixels = 40_000_000 + } + return &APIHandler{ + store: st, + s3: s3, + sessionTTL: sessionTTL, + idempotencyTTL: idempotencyTTL, + maxUploadBytes: maxUploadBytes, + maxImagePixels: maxImagePixels, + instanceID: instanceID, + } +} + +func (h *APIHandler) SetDraining(v bool) { + h.draining.Store(v) +} + +func (h *APIHandler) IsDraining() bool { + return h.draining.Load() +} + +func decodeJSON(r *http.Request, dst interface{}) error { + if r.Body == nil { + return errors.New("request body is missing") + } + defer r.Body.Close() + + dec := json.NewDecoder(r.Body) + dec.DisallowUnknownFields() + + if err := dec.Decode(dst); err != nil { + if errors.Is(err, io.EOF) { + return errors.New("request body is empty") + } + var unmarshalTypeError *json.UnmarshalTypeError + if errors.As(err, &unmarshalTypeError) { + return fmt.Errorf("field '%s' has incorrect type (expected %s)", unmarshalTypeError.Field, unmarshalTypeError.Type) + } + return fmt.Errorf("malformed JSON or unexpected field: %v", err) + } + + if dec.More() { + return errors.New("request body contains multiple JSON values") + } + + return nil +} + +func writeStoreError(w http.ResponseWriter, r *http.Request, err error) { + switch { + case errors.Is(err, store.ErrConflict): + WriteError(w, r, http.StatusConflict, "conflict", "resource conflict") + case errors.Is(err, store.ErrNotFound): + WriteError(w, r, http.StatusNotFound, "not_found", "resource not found or access denied") + case errors.Is(err, store.ErrUnauthorized): + WriteBearerUnauthorized(w, r, "unauthorized") + case errors.Is(err, store.ErrUnavailable): + WriteError(w, r, http.StatusServiceUnavailable, "unavailable", "storage dependency is temporarily unavailable") + case errors.Is(err, store.ErrBadRequest): + WriteError(w, r, http.StatusBadRequest, "invalid_request", err.Error()) + default: + WriteError(w, r, http.StatusInternalServerError, "internal_error", "internal server error") + } +} + +func (h *APIHandler) HandleLiveness(w http.ResponseWriter, r *http.Request) { + WriteJSON(w, r, http.StatusOK, model.HealthResponse{Status: "ok"}) +} + +func (h *APIHandler) HandleReadiness(w http.ResponseWriter, r *http.Request) { + if h.IsDraining() { + WriteError(w, r, http.StatusServiceUnavailable, "unavailable", "instance is draining") + return + } + if err := h.store.Ready(r.Context()); err != nil { + WriteError(w, r, http.StatusServiceUnavailable, "unavailable", "database dependency is not ready") + return + } + if h.s3 == nil || h.s3.Ready(r.Context()) != nil { + WriteError(w, r, http.StatusServiceUnavailable, "unavailable", "object storage dependency is not ready") + return + } + WriteJSON(w, r, http.StatusOK, model.HealthResponse{Status: "ok"}) +} + +func (h *APIHandler) HandleRegisterUser(w http.ResponseWriter, r *http.Request) { + var req model.CreateUserRequest + if err := decodeJSON(r, &req); err != nil { + WriteError(w, r, http.StatusBadRequest, "invalid_request", err.Error()) + return + } + + user, err := h.store.CreateUser(r.Context(), req) + if err != nil { + writeStoreError(w, r, err) + return + } + + WriteJSON(w, r, http.StatusCreated, user) +} + +func (h *APIHandler) HandleCreateSession(w http.ResponseWriter, r *http.Request) { + // Contract: POST /auth/sessions accepts ONLY Authorization: Basic , without JSON body. + if r.Body != nil { + defer r.Body.Close() + var buf [1]byte + n, _ := r.Body.Read(buf[:]) + if n > 0 { + WriteError(w, r, http.StatusBadRequest, "invalid_request", "POST /api/v1/auth/sessions must not include a request body") + return + } + } + + authHeader := strings.TrimSpace(r.Header.Get("Authorization")) + if authHeader == "" { + WriteBasicUnauthorized(w, r, "invalid credentials") + return + } + + parts := strings.SplitN(authHeader, " ", 2) + if len(parts) != 2 || !strings.EqualFold(parts[0], "Basic") { + WriteBasicUnauthorized(w, r, "invalid credentials") + return + } + + b64Payload := strings.TrimSpace(parts[1]) + decoded, err := base64.StdEncoding.DecodeString(b64Payload) + if err != nil || !utf8.Valid(decoded) { + WriteBasicUnauthorized(w, r, "invalid credentials") + return + } + + credParts := strings.SplitN(string(decoded), ":", 2) + if len(credParts) != 2 { + WriteBasicUnauthorized(w, r, "invalid credentials") + return + } + + username := credParts[0] + password := credParts[1] + + sess, err := h.store.AuthenticateAndCreateSession(r.Context(), username, password, h.sessionTTL) + if err != nil { + if errors.Is(err, store.ErrUnavailable) { + WriteError(w, r, http.StatusServiceUnavailable, "unavailable", "storage dependency is temporarily unavailable") + return + } + WriteBasicUnauthorized(w, r, "invalid credentials") + return + } + + WriteJSON(w, r, http.StatusCreated, sess) +} + +func (h *APIHandler) HandleRevokeSession(w http.ResponseWriter, r *http.Request) { + rawToken := GetAuthToken(r.Context()) + if rawToken == "" { + WriteBearerUnauthorized(w, r, "unauthenticated") + return + } + + if err := h.store.RevokeSession(r.Context(), rawToken); err != nil { + if errors.Is(err, store.ErrUnavailable) { + WriteError(w, r, http.StatusServiceUnavailable, "unavailable", "storage dependency is temporarily unavailable") + return + } + WriteBearerUnauthorized(w, r, "session already revoked or expired") + return + } + + reqID := GetRequestID(r.Context()) + w.Header().Set("X-Request-Id", reqID) + w.WriteHeader(http.StatusNoContent) +} + +func (h *APIHandler) HandleCurrentUser(w http.ResponseWriter, r *http.Request) { + user := GetAuthUser(r.Context()) + if user == nil { + WriteBearerUnauthorized(w, r, "unauthenticated") + return + } + WriteJSON(w, r, http.StatusOK, user) +} + +func (h *APIHandler) HandleCreateChat(w http.ResponseWriter, r *http.Request) { + caller := GetAuthUser(r.Context()) + if caller == nil { + WriteBearerUnauthorized(w, r, "unauthenticated") + return + } + + var req model.CreateChatRequest + if err := decodeJSON(r, &req); err != nil { + WriteError(w, r, http.StatusBadRequest, "invalid_request", err.Error()) + return + } + + chat, err := h.store.CreateChat(r.Context(), caller.ID, req) + if err != nil { + writeStoreError(w, r, err) + return + } + + WriteJSON(w, r, http.StatusCreated, chat) +} + +func (h *APIHandler) HandleListChats(w http.ResponseWriter, r *http.Request) { + caller := GetAuthUser(r.Context()) + if caller == nil { + WriteBearerUnauthorized(w, r, "unauthenticated") + return + } + + limit := 50 + limitStr := r.URL.Query().Get("limit") + if limitStr != "" { + l, err := strconv.Atoi(limitStr) + if err != nil || l < 1 || l > 100 { + WriteError(w, r, http.StatusBadRequest, "invalid_request", "limit must be an integer between 1 and 100") + return + } + limit = l + } + + cursor := r.URL.Query().Get("cursor") + page, err := h.store.ListChats(r.Context(), caller.ID, limit, cursor) + if err != nil { + writeStoreError(w, r, err) + return + } + + WriteJSON(w, r, http.StatusOK, page) +} + +func (h *APIHandler) HandleGetChat(w http.ResponseWriter, r *http.Request) { + caller := GetAuthUser(r.Context()) + if caller == nil { + WriteBearerUnauthorized(w, r, "unauthenticated") + return + } + + chatID := r.PathValue("chat_id") + if _, err := uuid.Parse(chatID); err != nil { + WriteError(w, r, http.StatusBadRequest, "invalid_request", "invalid chat id UUID") + return + } + + chat, err := h.store.GetChat(r.Context(), caller.ID, chatID) + if err != nil { + writeStoreError(w, r, err) + return + } + + WriteJSON(w, r, http.StatusOK, chat) +} + +func (h *APIHandler) HandleCreateMessage(w http.ResponseWriter, r *http.Request) { + caller := GetAuthUser(r.Context()) + if caller == nil { + WriteBearerUnauthorized(w, r, "unauthenticated") + return + } + + chatID := r.PathValue("chat_id") + if _, err := uuid.Parse(chatID); err != nil { + WriteError(w, r, http.StatusBadRequest, "invalid_request", "invalid chat id UUID") + return + } + + idemKey := r.Header.Get("Idempotency-Key") + if !store.IdempotencyKeyRegex.MatchString(idemKey) { + WriteError(w, r, http.StatusBadRequest, "invalid_request", "Idempotency-Key header is required and must match ^[A-Za-z0-9._:-]{1,128}$") + return + } + + var req model.CreateMessageRequest + if err := decodeJSON(r, &req); err != nil { + WriteError(w, r, http.StatusBadRequest, "invalid_request", err.Error()) + return + } + + msg, err := h.store.CreateMessage(r.Context(), caller.ID, chatID, idemKey, h.idempotencyTTL, req) + if err != nil { + writeStoreError(w, r, err) + return + } + + WriteJSON(w, r, http.StatusCreated, msg) +} + +func (h *APIHandler) HandleListMessages(w http.ResponseWriter, r *http.Request) { + caller := GetAuthUser(r.Context()) + if caller == nil { + WriteBearerUnauthorized(w, r, "unauthenticated") + return + } + + chatID := r.PathValue("chat_id") + if _, err := uuid.Parse(chatID); err != nil { + WriteError(w, r, http.StatusBadRequest, "invalid_request", "invalid chat id UUID") + return + } + + limit := 50 + limitStr := r.URL.Query().Get("limit") + if limitStr != "" { + l, err := strconv.Atoi(limitStr) + if err != nil || l < 1 || l > 100 { + WriteError(w, r, http.StatusBadRequest, "invalid_request", "limit must be an integer between 1 and 100") + return + } + limit = l + } + + cursor := r.URL.Query().Get("cursor") + page, err := h.store.ListMessages(r.Context(), caller.ID, chatID, limit, cursor) + if err != nil { + writeStoreError(w, r, err) + return + } + + WriteJSON(w, r, http.StatusOK, page) +} + +func (h *APIHandler) HandleInternalStats(w http.ResponseWriter, r *http.Request) { + if r.URL.Query().Get("reset") == "1" { + h.store.ResetStats() + } + if mode := r.URL.Query().Get("mode"); mode != "" { + h.store.SetCacheMode(mode) + } + WriteJSON(w, r, http.StatusOK, h.store.Stats(r.Context())) +} + +func (h *APIHandler) HandleOpenAPISpec(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json; charset=utf-8") + w.Header().Set("X-Request-Id", GetRequestID(r.Context())) + w.WriteHeader(http.StatusOK) + _, _ = w.Write(openAPISpec) +} + +func (h *APIHandler) HandleSwaggerUI(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "text/html; charset=utf-8") + w.Header().Set("X-Request-Id", GetRequestID(r.Context())) + w.WriteHeader(http.StatusOK) + _, _ = w.Write([]byte(swaggerUIHTML)) +} + +func (h *APIHandler) HandleNotFound(w http.ResponseWriter, r *http.Request) { + WriteError(w, r, http.StatusNotFound, "not_found", fmt.Sprintf("route %s %s not found", r.Method, r.URL.Path)) +} + +func validateFilename(name string) bool { + if strings.TrimSpace(name) == "" { + return false + } + n := utf8.RuneCountInString(name) + if n < 1 || n > 255 { + return false + } + for _, r := range name { + if r == '/' || r == '\\' || r == 0 || unicode.IsControl(r) { + return false + } + } + return true +} + +func (h *APIHandler) HandleUploadAttachment(w http.ResponseWriter, r *http.Request) { + caller := GetAuthUser(r.Context()) + if caller == nil { + WriteBearerUnauthorized(w, r, "unauthenticated") + return + } + + chatID := r.PathValue("chat_id") + if _, err := uuid.Parse(chatID); err != nil { + WriteError(w, r, http.StatusBadRequest, "invalid_request", "invalid chat id UUID") + return + } + + filename := r.URL.Query().Get("filename") + if !validateFilename(filename) { + WriteError(w, r, http.StatusBadRequest, "invalid_request", "invalid or missing filename") + return + } + + contentTypeHeader := r.Header.Get("Content-Type") + if _, err := media.ParseAllowedContentType(contentTypeHeader); err != nil { + WriteError(w, r, http.StatusUnsupportedMediaType, "unsupported_media_type", "Content-Type must be image/png or image/jpeg") + return + } + + if r.ContentLength > h.maxUploadBytes { + WriteError(w, r, http.StatusRequestEntityTooLarge, "payload_too_large", "upload exceeds maximum allowed byte size") + return + } + + // Enforce chat membership BEFORE spooling bytes or touching S3 + isMember, err := h.store.IsChatMember(r.Context(), caller.ID, chatID) + if err != nil { + writeStoreError(w, r, err) + return + } + if !isMember { + WriteError(w, r, http.StatusNotFound, "not_found", "resource not found or access denied") + return + } + + if h.s3 == nil { + WriteError(w, r, http.StatusServiceUnavailable, "unavailable", "object storage dependency is unavailable") + return + } + + r.Body = http.MaxBytesReader(w, r.Body, h.maxUploadBytes+1024) + img, err := media.ValidateAndSpoolImage(r.Body, contentTypeHeader, h.maxUploadBytes, h.maxImagePixels) + if err != nil { + switch { + case errors.Is(err, media.ErrPayloadTooLarge): + WriteError(w, r, http.StatusRequestEntityTooLarge, "payload_too_large", "image exceeds byte or pixel limit") + case errors.Is(err, media.ErrUnsupportedMediaType): + WriteError(w, r, http.StatusUnsupportedMediaType, "unsupported_media_type", "payload is not a valid PNG or JPEG image matching Content-Type") + default: + WriteError(w, r, http.StatusBadRequest, "invalid_request", "failed to read upload stream") + } + return + } + defer img.Cleanup() + + attID := uuid.New().String() + objectKey := fmt.Sprintf("attachments/%s/%s", chatID, attID) + + if err := h.s3.PutObject(r.Context(), objectKey, img.File, img.SizeBytes, img.ContentType); err != nil { + WriteError(w, r, http.StatusServiceUnavailable, "unavailable", "object storage temporarily unavailable") + return + } + + att, err := h.store.CreateAttachment(r.Context(), model.Attachment{ + ID: attID, + ChatID: chatID, + OwnerID: caller.ID, + Filename: filename, + ContentType: img.ContentType, + SizeBytes: img.SizeBytes, + SHA256: img.SHA256, + ObjectKey: objectKey, + }) + if err != nil { + // Immediate synchronous compensation: delete the unreferenced S3 object if DB commit failed. + compCtx, cancel := context.WithTimeout(context.Background(), 3*time.Second) + _ = h.s3.RemoveObject(compCtx, objectKey) + cancel() + writeStoreError(w, r, err) + return + } + + WriteJSON(w, r, http.StatusCreated, att) +} + +func (h *APIHandler) HandleGetAttachment(w http.ResponseWriter, r *http.Request) { + caller := GetAuthUser(r.Context()) + if caller == nil { + WriteBearerUnauthorized(w, r, "unauthenticated") + return + } + + attID := r.PathValue("attachment_id") + if _, err := uuid.Parse(attID); err != nil { + WriteError(w, r, http.StatusBadRequest, "invalid_request", "invalid attachment id UUID") + return + } + + att, err := h.store.GetAttachment(r.Context(), caller.ID, attID) + if err != nil { + writeStoreError(w, r, err) + return + } + + WriteJSON(w, r, http.StatusOK, att) +} + +func (h *APIHandler) HandleDownloadAttachmentContent(w http.ResponseWriter, r *http.Request) { + caller := GetAuthUser(r.Context()) + if caller == nil { + WriteBearerUnauthorized(w, r, "unauthenticated") + return + } + + attID := r.PathValue("attachment_id") + if _, err := uuid.Parse(attID); err != nil { + WriteError(w, r, http.StatusBadRequest, "invalid_request", "invalid attachment id UUID") + return + } + + variant := r.URL.Query().Get("variant") + if variant != "" && variant != "original" { + if variant == "thumbnail" || variant == "crop" || variant == "optimized" { + WriteError(w, r, http.StatusNotFound, "not_found", "requested variant does not exist") + return + } + WriteError(w, r, http.StatusBadRequest, "invalid_request", "invalid variant query parameter") + return + } + + att, err := h.store.GetAttachment(r.Context(), caller.ID, attID) + if err != nil { + writeStoreError(w, r, err) + return + } + + if att.Status != "ready" { + WriteError(w, r, http.StatusConflict, "conflict", "attachment is not ready for download") + return + } + + if h.s3 == nil { + WriteError(w, r, http.StatusServiceUnavailable, "unavailable", "object storage temporarily unavailable") + return + } + + obj, objSize, err := h.s3.GetObject(r.Context(), att.ObjectKey) + if err != nil { + if errors.Is(err, s3store.ErrObjectNotFound) { + WriteError(w, r, http.StatusConflict, "conflict", "attachment binary object is unavailable") + return + } + WriteError(w, r, http.StatusServiceUnavailable, "unavailable", "object storage temporarily unavailable") + return + } + defer obj.Close() + + w.Header().Set("Content-Type", att.ContentType) + w.Header().Set("Content-Length", strconv.FormatInt(objSize, 10)) + w.Header().Set("X-Content-Type-Options", "nosniff") + w.Header().Set("X-Request-Id", GetRequestID(r.Context())) + w.WriteHeader(http.StatusOK) + _, _ = io.Copy(w, obj) +} + +func RegisterRoutes(st store.Store, s3 *s3store.Client, sessionTTL, idempotencyTTL int, maxUploadBytes, maxImagePixels int64, instanceID string) (http.Handler, *APIHandler) { + api := NewAPIHandler(st, s3, sessionTTL, idempotencyTTL, maxUploadBytes, maxImagePixels, instanceID) + mux := http.NewServeMux() + + // Documentation & schema + mux.HandleFunc("GET /docs", api.HandleSwaggerUI) + mux.HandleFunc("GET /docs/", api.HandleSwaggerUI) + mux.HandleFunc("GET /openapi.json", api.HandleOpenAPISpec) + + // Internal diagnostics for experiments + mux.HandleFunc("GET /internal/stats", api.HandleInternalStats) + mux.HandleFunc("POST /internal/stats", api.HandleInternalStats) + + // Public routes + mux.HandleFunc("GET /health/live", api.HandleLiveness) + mux.HandleFunc("GET /health/ready", api.HandleReadiness) + mux.HandleFunc("POST /api/v1/users", api.HandleRegisterUser) + mux.HandleFunc("POST /api/v1/auth/sessions", api.HandleCreateSession) + + // Protected Bearer routes + mux.HandleFunc("DELETE /api/v1/auth/sessions/current", RequireBearerAuth(st, api.HandleRevokeSession)) + mux.HandleFunc("GET /api/v1/users/me", RequireBearerAuth(st, api.HandleCurrentUser)) + mux.HandleFunc("POST /api/v1/chats", RequireBearerAuth(st, api.HandleCreateChat)) + mux.HandleFunc("GET /api/v1/chats", RequireBearerAuth(st, api.HandleListChats)) + mux.HandleFunc("GET /api/v1/chats/{chat_id}", RequireBearerAuth(st, api.HandleGetChat)) + mux.HandleFunc("POST /api/v1/chats/{chat_id}/messages", RequireBearerAuth(st, api.HandleCreateMessage)) + mux.HandleFunc("GET /api/v1/chats/{chat_id}/messages", RequireBearerAuth(st, api.HandleListMessages)) + mux.HandleFunc("POST /api/v1/chats/{chat_id}/attachments", RequireBearerAuth(st, api.HandleUploadAttachment)) + mux.HandleFunc("GET /api/v1/attachments/{attachment_id}", RequireBearerAuth(st, api.HandleGetAttachment)) + mux.HandleFunc("GET /api/v1/attachments/{attachment_id}/content", RequireBearerAuth(st, api.HandleDownloadAttachmentContent)) + + // Fallback 404 handler + mux.HandleFunc("/", api.HandleNotFound) + + var handler http.Handler = mux + handler = LoggingMiddleware(instanceID, handler) + handler = RequestIDMiddleware(handler) + handler = InstanceIDMiddleware(instanceID, handler) + + return handler, api +} diff --git a/internal/handler/middleware.go b/internal/handler/middleware.go new file mode 100644 index 00000000..e496fdd7 --- /dev/null +++ b/internal/handler/middleware.go @@ -0,0 +1,168 @@ +package handler + +import ( + "context" + "encoding/json" + "errors" + "fmt" + "net/http" + "os" + "strings" + "time" + + "github.com/google/uuid" + "messenger/internal/model" + "messenger/internal/store" +) + +type contextKey string + +const ( + requestIDContextKey contextKey = "request_id" + userContextKey contextKey = "user" + tokenContextKey contextKey = "token" +) + +type loggingResponseWriter struct { + http.ResponseWriter + statusCode int +} + +func (lrw *loggingResponseWriter) WriteHeader(code int) { + lrw.statusCode = code + lrw.ResponseWriter.WriteHeader(code) +} + +func InstanceIDMiddleware(instanceID string, next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("X-Instance-Id", instanceID) + next.ServeHTTP(w, r) + }) +} + +func RequestIDMiddleware(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + reqID := strings.TrimSpace(r.Header.Get("X-Request-Id")) + if reqID == "" { + reqID = uuid.New().String() + } + w.Header().Set("X-Request-Id", reqID) + ctx := context.WithValue(r.Context(), requestIDContextKey, reqID) + next.ServeHTTP(w, r.WithContext(ctx)) + }) +} + +func LoggingMiddleware(instanceID string, next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + start := time.Now() + lrw := &loggingResponseWriter{ResponseWriter: w, statusCode: http.StatusOK} + + next.ServeHTTP(lrw, r) + + reqID, _ := r.Context().Value(requestIDContextKey).(string) + duration := time.Since(start) + + logEntry := map[string]interface{}{ + "time": time.Now().UTC().Format(time.RFC3339Nano), + "instance_id": instanceID, + "method": r.Method, + "path": r.URL.Path, + "status": lrw.statusCode, + "duration_ms": float64(duration.Microseconds()) / 1000.0, + "request_id": reqID, + "remote_ip": r.RemoteAddr, + } + logBytes, _ := json.Marshal(logEntry) + fmt.Fprintln(os.Stdout, string(logBytes)) + }) +} + +func GetRequestID(ctx context.Context) string { + if val, ok := ctx.Value(requestIDContextKey).(string); ok && val != "" { + return val + } + return uuid.New().String() +} + +func WriteError(w http.ResponseWriter, r *http.Request, status int, code, message string) { + reqID := GetRequestID(r.Context()) + w.Header().Set("Content-Type", "application/json") + w.Header().Set("X-Request-Id", reqID) + w.WriteHeader(status) + _ = json.NewEncoder(w).Encode(model.ErrorResponse{ + Error: model.ErrorDetail{ + Code: code, + Message: message, + RequestID: reqID, + }, + }) +} + +func WriteBasicUnauthorized(w http.ResponseWriter, r *http.Request, message string) { + w.Header().Set("WWW-Authenticate", `Basic realm="messenger", charset="UTF-8"`) + WriteError(w, r, http.StatusUnauthorized, "unauthorized", message) +} + +func WriteBearerUnauthorized(w http.ResponseWriter, r *http.Request, message string) { + w.Header().Set("WWW-Authenticate", `Bearer realm="messenger"`) + WriteError(w, r, http.StatusUnauthorized, "unauthorized", message) +} + +func WriteJSON(w http.ResponseWriter, r *http.Request, status int, data interface{}) { + reqID := GetRequestID(r.Context()) + w.Header().Set("Content-Type", "application/json") + w.Header().Set("X-Request-Id", reqID) + w.WriteHeader(status) + _ = json.NewEncoder(w).Encode(data) +} + +func RequireBearerAuth(st store.Store, next http.HandlerFunc) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + // Note: X-User-Id is intentionally ignored in Lab 2+ per contract. + authHeader := strings.TrimSpace(r.Header.Get("Authorization")) + if authHeader == "" { + WriteBearerUnauthorized(w, r, "missing Authorization header") + return + } + + parts := strings.SplitN(authHeader, " ", 2) + if len(parts) != 2 || !strings.EqualFold(parts[0], "Bearer") { + WriteBearerUnauthorized(w, r, "Bearer authentication required") + return + } + + rawToken := strings.TrimSpace(parts[1]) + if rawToken == "" || strings.ContainsAny(rawToken, " \t\r\n") { + WriteBearerUnauthorized(w, r, "invalid Bearer token") + return + } + + user, err := st.GetUserByToken(r.Context(), rawToken) + if err != nil { + if errors.Is(err, store.ErrUnavailable) { + WriteError(w, r, http.StatusServiceUnavailable, "unavailable", "storage dependency is temporarily unavailable") + return + } + WriteBearerUnauthorized(w, r, "invalid, expired, or revoked session token") + return + } + + ctx := context.WithValue(r.Context(), userContextKey, user) + ctx = context.WithValue(ctx, tokenContextKey, rawToken) + next(w, r.WithContext(ctx)) + } +} + +func GetAuthUser(ctx context.Context) *model.User { + if user, ok := ctx.Value(userContextKey).(*model.User); ok { + return user + } + return nil +} + +func GetAuthToken(ctx context.Context) string { + if tok, ok := ctx.Value(tokenContextKey).(string); ok { + return tok + } + return "" +} diff --git a/internal/handler/openapi.json b/internal/handler/openapi.json new file mode 100644 index 00000000..138f07c0 --- /dev/null +++ b/internal/handler/openapi.json @@ -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/internal/media/validate.go b/internal/media/validate.go new file mode 100644 index 00000000..58d4f9d2 --- /dev/null +++ b/internal/media/validate.go @@ -0,0 +1,314 @@ +package media + +import ( + "bufio" + "bytes" + "crypto/sha256" + "encoding/binary" + "encoding/hex" + "errors" + "fmt" + "hash/crc32" + "image" + "image/jpeg" + "image/png" + "io" + "mime" + "net/http" + "os" + "strings" +) + +var ( + ErrPayloadTooLarge = errors.New("payload too large") + ErrUnsupportedMediaType = errors.New("unsupported or corrupted media") + ErrBadFilename = errors.New("invalid filename") +) + +var pngSignature = []byte{0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a} + +type ValidatedImage struct { + File *os.File + SizeBytes int64 + SHA256 string + ContentType string + Width int + Height int +} + +func (v *ValidatedImage) Cleanup() { + if v != nil && v.File != nil { + name := v.File.Name() + _ = v.File.Close() + _ = os.Remove(name) + v.File = nil + } +} + +func ParseAllowedContentType(headerVal string) (string, error) { + mediaType, _, err := mime.ParseMediaType(strings.TrimSpace(headerVal)) + if err != nil { + return "", ErrUnsupportedMediaType + } + mediaType = strings.ToLower(mediaType) + if mediaType != "image/png" && mediaType != "image/jpeg" { + return "", ErrUnsupportedMediaType + } + return mediaType, nil +} + +// ValidateAndSpoolImage streams upload bytes into an ephemeral temp file while enforcing +// maxBytes, verifying magic bytes against declared Content-Type, checking pixel limits +// before bitmap allocation, and checking PNG/JPEG container integrity in bounded RAM. +func ValidateAndSpoolImage(r io.Reader, declaredContentType string, maxBytes int64, maxPixels int64) (*ValidatedImage, error) { + ct, err := ParseAllowedContentType(declaredContentType) + if err != nil { + return nil, err + } + if maxBytes <= 0 { + maxBytes = 10 * 1024 * 1024 + } + if maxPixels <= 0 { + maxPixels = 40_000_000 + } + + tmpFile, err := os.CreateTemp("", "lab4-upload-*") + if err != nil { + return nil, fmt.Errorf("create temp spool file: %w", err) + } + cleanupTmp := true + defer func() { + if cleanupTmp && tmpFile != nil { + name := tmpFile.Name() + _ = tmpFile.Close() + _ = os.Remove(name) + } + }() + + hasher := sha256.New() + buf := make([]byte, 32*1024) + n, err := io.CopyBuffer(io.MultiWriter(tmpFile, hasher), io.LimitReader(r, maxBytes+1), buf) + if err != nil { + return nil, ErrBadFilename + } + if n == 0 { + return nil, ErrUnsupportedMediaType + } + if n > maxBytes { + return nil, ErrPayloadTooLarge + } + + if _, err := tmpFile.Seek(0, io.SeekStart); err != nil { + return nil, err + } + + // Verify content magic bytes match declared Content-Type (prevents MIME spoofing). + head := make([]byte, 512) + headLen, _ := io.ReadFull(tmpFile, head) + if headLen == 0 { + return nil, ErrUnsupportedMediaType + } + sniffed := http.DetectContentType(head[:headLen]) + if ct == "image/png" { + if headLen < len(pngSignature) || !bytes.Equal(head[:8], pngSignature) || sniffed != "image/png" { + return nil, ErrUnsupportedMediaType + } + } else { + if headLen < 3 || head[0] != 0xff || head[1] != 0xd8 || head[2] != 0xff || sniffed != "image/jpeg" { + return nil, ErrUnsupportedMediaType + } + } + + // Parse image header BEFORE full pixel decode to prevent decompression-bomb OOMs. + if _, err := tmpFile.Seek(0, io.SeekStart); err != nil { + return nil, err + } + cfg, format, err := image.DecodeConfig(tmpFile) + if err != nil { + return nil, ErrUnsupportedMediaType + } + if (ct == "image/png" && format != "png") || (ct == "image/jpeg" && format != "jpeg") { + return nil, ErrUnsupportedMediaType + } + if cfg.Width <= 0 || cfg.Height <= 0 { + return nil, ErrUnsupportedMediaType + } + if int64(cfg.Width) > maxPixels/int64(cfg.Height) { + return nil, ErrPayloadTooLarge + } + + // Verify structural integrity in bounded memory. + if _, err := tmpFile.Seek(0, io.SeekStart); err != nil { + return nil, err + } + if ct == "image/png" { + if err := verifyPNGChunks(tmpFile); err != nil { + return nil, ErrUnsupportedMediaType + } + if int64(cfg.Width)*int64(cfg.Height) <= 2_000_000 { + _, _ = tmpFile.Seek(0, io.SeekStart) + if _, err := png.Decode(tmpFile); err != nil { + return nil, ErrUnsupportedMediaType + } + } + } else { + if err := verifyJPEGStructure(tmpFile); err != nil { + return nil, ErrUnsupportedMediaType + } + if int64(cfg.Width)*int64(cfg.Height) <= 2_000_000 { + _, _ = tmpFile.Seek(0, io.SeekStart) + if _, err := jpeg.Decode(tmpFile); err != nil { + return nil, ErrUnsupportedMediaType + } + } + } + + if _, err := tmpFile.Seek(0, io.SeekStart); err != nil { + return nil, err + } + + cleanupTmp = false + return &ValidatedImage{ + File: tmpFile, + SizeBytes: n, + SHA256: hex.EncodeToString(hasher.Sum(nil)), + ContentType: ct, + Width: cfg.Width, + Height: cfg.Height, + }, nil +} + +func verifyPNGChunks(r io.Reader) error { + sig := make([]byte, 8) + if _, err := io.ReadFull(r, sig); err != nil || !bytes.Equal(sig, pngSignature) { + return errors.New("invalid PNG signature") + } + + header := make([]byte, 8) + crcBuf := make([]byte, 4) + scratch := make([]byte, 32*1024) + seenIHDR := false + seenIEND := false + + for { + _, err := io.ReadFull(r, header) + if err != nil { + return errors.New("truncated PNG chunk") + } + chunkLen := binary.BigEndian.Uint32(header[0:4]) + if chunkLen > 10*1024*1024 { + return errors.New("PNG chunk too large") + } + chunkType := string(header[4:8]) + if !seenIHDR && chunkType != "IHDR" { + return errors.New("first PNG chunk must be IHDR") + } + if chunkType == "IHDR" { + seenIHDR = true + } + + crc := crc32.NewIEEE() + _, _ = crc.Write(header[4:8]) + + remaining := int64(chunkLen) + for remaining > 0 { + toRead := int64(len(scratch)) + if remaining < toRead { + toRead = remaining + } + m, rErr := io.ReadFull(r, scratch[:toRead]) + if rErr != nil { + return errors.New("truncated PNG chunk data") + } + _, _ = crc.Write(scratch[:m]) + remaining -= int64(m) + } + + if _, err := io.ReadFull(r, crcBuf); err != nil { + return errors.New("missing PNG CRC") + } + expectedCRC := binary.BigEndian.Uint32(crcBuf) + if crc.Sum32() != expectedCRC { + return errors.New("PNG CRC mismatch") + } + + if chunkType == "IEND" { + seenIEND = true + break + } + } + + if !seenIHDR || !seenIEND { + return errors.New("incomplete PNG structure") + } + return nil +} + +func verifyJPEGStructure(r io.Reader) error { + br := bufio.NewReaderSize(r, 32*1024) + b0, err := br.ReadByte() + b1, err2 := br.ReadByte() + if err != nil || err2 != nil || b0 != 0xff || b1 != 0xd8 { + return errors.New("missing JPEG SOI") + } + + seenSOF := false + seenSOS := false + seenEOI := false + + for { + b, err := br.ReadByte() + if err != nil { + break + } + if b != 0xff { + if !seenSOS { + return errors.New("unexpected bytes before JPEG SOS") + } + continue + } + for { + b, err = br.ReadByte() + if err != nil { + return errors.New("truncated JPEG marker") + } + if b != 0xff { + break + } + } + marker := b + if marker == 0x00 || (marker >= 0xd0 && marker <= 0xd7) { + continue + } + if marker == 0xd9 { + seenEOI = true + break + } + if marker == 0x01 || marker == 0xd8 { + continue + } + + var lenBuf [2]byte + if _, err := io.ReadFull(br, lenBuf[:]); err != nil { + return errors.New("truncated JPEG segment length") + } + segLen := int(binary.BigEndian.Uint16(lenBuf[:])) + if segLen < 2 { + return errors.New("invalid JPEG segment length") + } + if _, err := io.CopyN(io.Discard, br, int64(segLen-2)); err != nil { + return errors.New("truncated JPEG segment payload") + } + if (marker >= 0xc0 && marker <= 0xc3) || (marker >= 0xc5 && marker <= 0xc7) || (marker >= 0xc9 && marker <= 0xcb) || (marker >= 0xcd && marker <= 0xcf) { + seenSOF = true + } + if marker == 0xda { + seenSOS = true + } + } + + if !seenSOF || !seenSOS || !seenEOI { + return errors.New("incomplete JPEG structure") + } + return nil +} diff --git a/internal/model/types.go b/internal/model/types.go new file mode 100644 index 00000000..fcf343cd --- /dev/null +++ b/internal/model/types.go @@ -0,0 +1,102 @@ +package model + +type User struct { + ID string `json:"id"` + Username string `json:"username"` + DisplayName string `json:"display_name"` + CreatedAt string `json:"created_at"` +} + +type CreateUserRequest struct { + Username string `json:"username"` + DisplayName string `json:"display_name"` + Password string `json:"password"` +} + +type Session struct { + Token string `json:"token"` + TokenType string `json:"token_type"` + ExpiresAt string `json:"expires_at"` + User User `json:"user"` +} + +type Chat struct { + ID string `json:"id"` + Title string `json:"title"` + MemberIDs []string `json:"member_ids"` + CreatedAt string `json:"created_at"` +} + +type CreateChatRequest struct { + Title string `json:"title"` + MemberIDs []string `json:"member_ids"` +} + +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 Attachment struct { + ID string `json:"id"` + ChatID string `json:"chat_id"` + OwnerID string `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 string `json:"created_at"` + UpdatedAt string `json:"updated_at"` + ObjectKey string `json:"-"` +} + +type Message struct { + ID string `json:"id"` + ChatID string `json:"chat_id"` + SenderID string `json:"sender_id"` + Text string `json:"text"` + CreatedAt string `json:"created_at"` + AttachmentIDs []string `json:"attachment_ids"` +} + +type CreateMessageRequest struct { + Text string `json:"text"` + AttachmentIDs []string `json:"attachment_ids,omitempty"` +} + +type ChatPage struct { + Items []Chat `json:"items"` + NextCursor *string `json:"next_cursor"` +} + +type MessagePage struct { + Items []Message `json:"items"` + NextCursor *string `json:"next_cursor"` +} + +type ErrorDetail struct { + Code string `json:"code"` + Message string `json:"message"` + RequestID string `json:"request_id"` +} + +type ErrorResponse struct { + Error ErrorDetail `json:"error"` +} + +type HealthResponse struct { + Status string `json:"status"` +} + +type CursorData struct { + Scope string `json:"s"` + Time string `json:"t"` + ID string `json:"i"` +} diff --git a/internal/s3store/s3.go b/internal/s3store/s3.go new file mode 100644 index 00000000..f7293ff1 --- /dev/null +++ b/internal/s3store/s3.go @@ -0,0 +1,173 @@ +package s3store + +import ( + "context" + "errors" + "fmt" + "io" + "net" + "net/http" + "net/url" + "strings" + "time" + + "github.com/minio/minio-go/v7" + "github.com/minio/minio-go/v7/pkg/credentials" +) + +var ( + ErrObjectNotFound = errors.New("s3 object not found") + ErrS3Unavailable = errors.New("s3 storage unavailable") +) + +type Config struct { + Endpoint string + Region string + Bucket string + AccessKey string + SecretKey string +} + +type ObjectSummary struct { + Key string + Size int64 + LastModified time.Time +} + +type Client struct { + mc *minio.Client + bucket string +} + +func NewClient(cfg Config) (*Client, error) { + u, err := url.Parse(strings.TrimSpace(cfg.Endpoint)) + if err != nil || u.Host == "" { + return nil, fmt.Errorf("invalid S3 endpoint %q", cfg.Endpoint) + } + useSSL := strings.EqualFold(u.Scheme, "https") + region := strings.TrimSpace(cfg.Region) + if region == "" { + region = "us-east-1" + } + + tr := &http.Transport{ + Proxy: http.ProxyFromEnvironment, + DialContext: (&net.Dialer{ + Timeout: 800 * time.Millisecond, + KeepAlive: 30 * time.Second, + }).DialContext, + MaxIdleConns: 100, + IdleConnTimeout: 90 * time.Second, + TLSHandshakeTimeout: 2 * time.Second, + ResponseHeaderTimeout: 10 * time.Second, + ExpectContinueTimeout: 1 * time.Second, + } + + mc, err := minio.New(u.Host, &minio.Options{ + Creds: credentials.NewStaticV4(cfg.AccessKey, cfg.SecretKey, ""), + Secure: useSSL, + Region: region, + BucketLookup: minio.BucketLookupPath, + Transport: tr, + MaxRetries: 1, + }) + if err != nil { + return nil, fmt.Errorf("create minio client: %w", err) + } + + return &Client{ + mc: mc, + bucket: cfg.Bucket, + }, nil +} + +func (c *Client) Bucket() string { + return c.bucket +} + +func (c *Client) Ready(ctx context.Context) error { + rCtx, cancel := context.WithTimeout(ctx, 2*time.Second) + defer cancel() + exists, err := c.mc.BucketExists(rCtx, c.bucket) + if err != nil || !exists { + return ErrS3Unavailable + } + return nil +} + +func (c *Client) PutObject(ctx context.Context, objectKey string, r io.Reader, size int64, contentType string) error { + pCtx, cancel := context.WithTimeout(ctx, 30*time.Second) + defer cancel() + + _, err := c.mc.PutObject(pCtx, c.bucket, objectKey, r, size, minio.PutObjectOptions{ + ContentType: contentType, + }) + if err != nil { + return fmt.Errorf("%w: %v", ErrS3Unavailable, err) + } + return nil +} + +func (c *Client) GetObject(ctx context.Context, objectKey string) (io.ReadCloser, int64, error) { + obj, err := c.mc.GetObject(ctx, c.bucket, objectKey, minio.GetObjectOptions{}) + if err != nil { + return nil, 0, fmt.Errorf("%w: %v", ErrS3Unavailable, err) + } + info, err := obj.Stat() + if err != nil { + _ = obj.Close() + errResp := minio.ToErrorResponse(err) + if errResp.Code == "NoSuchKey" || errResp.StatusCode == 404 { + return nil, 0, ErrObjectNotFound + } + return nil, 0, fmt.Errorf("%w: %v", ErrS3Unavailable, err) + } + return obj, info.Size, nil +} + +func (c *Client) StatObject(ctx context.Context, objectKey string) (bool, error) { + sCtx, cancel := context.WithTimeout(ctx, 3*time.Second) + defer cancel() + _, err := c.mc.StatObject(sCtx, c.bucket, objectKey, minio.StatObjectOptions{}) + if err != nil { + errResp := minio.ToErrorResponse(err) + if errResp.Code == "NoSuchKey" || errResp.StatusCode == 404 { + return false, nil + } + return false, fmt.Errorf("%w: %v", ErrS3Unavailable, err) + } + return true, nil +} + +func (c *Client) RemoveObject(ctx context.Context, objectKey string) error { + dCtx, cancel := context.WithTimeout(ctx, 5*time.Second) + defer cancel() + if err := c.mc.RemoveObject(dCtx, c.bucket, objectKey, minio.RemoveObjectOptions{}); err != nil { + errResp := minio.ToErrorResponse(err) + if errResp.Code == "NoSuchKey" || errResp.StatusCode == 404 { + return nil + } + return fmt.Errorf("%w: %v", ErrS3Unavailable, err) + } + return nil +} + +func (c *Client) ListObjects(ctx context.Context, prefix string) ([]ObjectSummary, error) { + lCtx, cancel := context.WithTimeout(ctx, 30*time.Second) + defer cancel() + var out []ObjectSummary + for obj := range c.mc.ListObjects(lCtx, c.bucket, minio.ListObjectsOptions{ + Prefix: prefix, + Recursive: true, + }) { + if obj.Err != nil { + return nil, fmt.Errorf("%w: %v", ErrS3Unavailable, obj.Err) + } + out = append(out, ObjectSummary{ + Key: obj.Key, + Size: obj.Size, + LastModified: obj.LastModified, + }) + } + return out, nil +} diff --git a/internal/store/postgres.go b/internal/store/postgres.go new file mode 100644 index 00000000..5837675d --- /dev/null +++ b/internal/store/postgres.go @@ -0,0 +1,1304 @@ +package store + +import ( + "context" + "crypto/sha256" + "encoding/hex" + "encoding/json" + "errors" + "fmt" + "log" + "strings" + "sync" + "sync/atomic" + "time" + "unicode/utf8" + + "github.com/google/uuid" + "github.com/jackc/pgx/v5" + "github.com/jackc/pgx/v5/pgconn" + "github.com/jackc/pgx/v5/pgxpool" + "github.com/redis/go-redis/v9" + "golang.org/x/sync/singleflight" + + "messenger/internal/auth" + "messenger/internal/model" + "messenger/internal/s3store" +) + +type cachedSession struct { + User model.User `json:"user"` + ExpiresAt time.Time `json:"expires_at"` +} + +type PostgresStore struct { + pool *pgxpool.Pool + rdb *redis.Client + queryTimeout time.Duration + + sfGroup singleflight.Group + + mu sync.RWMutex + cacheMode string + writeMode string + + sqlQueries atomic.Int64 + cacheHits atomic.Int64 + cacheMisses atomic.Int64 + singleflightShared atomic.Int64 +} + +type Config struct { + DatabaseURL string + CacheURL string + MaxConns int32 + MinConns int32 + QueryTimeout time.Duration + CacheMode string + WriteMode string +} + +func NewPostgresStore(ctx context.Context, cfg Config) (*PostgresStore, error) { + poolCfg, err := pgxpool.ParseConfig(cfg.DatabaseURL) + if err != nil { + return nil, fmt.Errorf("invalid database URL: %w", err) + } + + if cfg.MaxConns <= 0 { + cfg.MaxConns = 12 + } + if cfg.MinConns <= 0 { + cfg.MinConns = 2 + } + if cfg.QueryTimeout <= 0 { + cfg.QueryTimeout = 5 * time.Second + } + if cfg.CacheMode == "" { + cfg.CacheMode = "write_through_versioned" + } + if cfg.WriteMode == "" { + cfg.WriteMode = "pipeline" + } + + poolCfg.MaxConns = cfg.MaxConns + poolCfg.MinConns = cfg.MinConns + poolCfg.MaxConnLifetime = 30 * time.Minute + poolCfg.MaxConnIdleTime = 5 * time.Minute + poolCfg.HealthCheckPeriod = 15 * time.Second + poolCfg.ConnConfig.ConnectTimeout = 3 * time.Second + + pool, err := pgxpool.NewWithConfig(ctx, poolCfg) + if err != nil { + return nil, fmt.Errorf("failed to create pgx pool: %w", err) + } + + var rdb *redis.Client + if cfg.CacheURL != "" { + opt, err := redis.ParseURL(cfg.CacheURL) + if err != nil { + pool.Close() + return nil, fmt.Errorf("invalid cache URL: %w", err) + } + opt.DialTimeout = 500 * time.Millisecond + opt.ReadTimeout = 500 * time.Millisecond + opt.WriteTimeout = 500 * time.Millisecond + opt.PoolSize = 30 + opt.MaxRetries = 1 + rdb = redis.NewClient(opt) + } + + return &PostgresStore{ + pool: pool, + rdb: rdb, + queryTimeout: cfg.QueryTimeout, + cacheMode: cfg.CacheMode, + writeMode: cfg.WriteMode, + }, nil +} + +func (s *PostgresStore) RunMigrations(ctx context.Context, migrations []MigrationFile) error { + qCtx, cancel := context.WithTimeout(ctx, 20*time.Second) + defer cancel() + + tx, err := s.pool.Begin(qCtx) + if err != nil { + return fmt.Errorf("begin migration tx: %w", err) + } + defer func() { _ = tx.Rollback(qCtx) }() + + // Serialize concurrent migration runners via PostgreSQL advisory transaction lock. + if _, err := tx.Exec(qCtx, `SELECT pg_advisory_xact_lock(20260301)`); err != nil { + return fmt.Errorf("acquire migration advisory lock: %w", err) + } + + _, err = tx.Exec(qCtx, ` + CREATE TABLE IF NOT EXISTS schema_migrations ( + version VARCHAR(64) PRIMARY KEY, + applied_at TIMESTAMPTZ NOT NULL DEFAULT NOW() + ); + `) + if err != nil { + return fmt.Errorf("create schema_migrations: %w", err) + } + + for _, mig := range migrations { + var alreadyApplied bool + err = tx.QueryRow(qCtx, `SELECT EXISTS (SELECT 1 FROM schema_migrations WHERE version = $1)`, mig.Version).Scan(&alreadyApplied) + if err != nil { + return fmt.Errorf("check migration version %s: %w", mig.Version, err) + } + + if !alreadyApplied { + if _, err := tx.Exec(qCtx, mig.SQL); err != nil { + return fmt.Errorf("apply migration %s: %w", mig.Version, err) + } + if _, err := tx.Exec(qCtx, `INSERT INTO schema_migrations (version, applied_at) VALUES ($1, NOW())`, mig.Version); err != nil { + return fmt.Errorf("record migration %s: %w", mig.Version, err) + } + log.Printf("Migration %s applied successfully", mig.Version) + } else { + log.Printf("Migration %s already applied (idempotent skip)", mig.Version) + } + } + + return tx.Commit(qCtx) +} + +func (s *PostgresStore) Ready(ctx context.Context) error { + qCtx, cancel := context.WithTimeout(ctx, 2*time.Second) + defer cancel() + + var ok int + if err := s.pool.QueryRow(qCtx, `SELECT 1 FROM schema_migrations WHERE version = '003_attachments.sql' LIMIT 1`).Scan(&ok); err != nil { + return ErrUnavailable + } + return nil +} + +func (s *PostgresStore) Close() { + if s.rdb != nil { + _ = s.rdb.Close() + } + if s.pool != nil { + s.pool.Close() + } +} + +func (s *PostgresStore) getCacheMode() string { + s.mu.RLock() + defer s.mu.RUnlock() + return s.cacheMode +} + +func (s *PostgresStore) SetCacheMode(mode string) { + s.mu.Lock() + defer s.mu.Unlock() + s.cacheMode = mode +} + +func (s *PostgresStore) ResetStats() { + s.sqlQueries.Store(0) + s.cacheHits.Store(0) + s.cacheMisses.Store(0) + s.singleflightShared.Store(0) +} + +func (s *PostgresStore) Stats(ctx context.Context) StoreStats { + redisOK := false + if s.rdb != nil { + rCtx, cancel := context.WithTimeout(ctx, 300*time.Millisecond) + defer cancel() + if err := s.rdb.Ping(rCtx).Err(); err == nil { + redisOK = true + } + } + return StoreStats{ + SQLQueries: s.sqlQueries.Load(), + CacheHits: s.cacheHits.Load(), + CacheMisses: s.cacheMisses.Load(), + SingleflightShared: s.singleflightShared.Load(), + CacheMode: s.getCacheMode(), + WriteMode: s.writeMode, + RedisConnected: redisOK, + } +} + +func classifyDBError(err error) error { + if err == nil { + return nil + } + var pgErr *pgconn.PgError + if errors.As(err, &pgErr) { + switch pgErr.Code { + case "23505": // unique_violation + return fmt.Errorf("%w: resource already exists", ErrConflict) + case "23503": // foreign_key_violation + return fmt.Errorf("%w: referenced entity does not exist", ErrBadRequest) + case "23514": // check_violation + return fmt.Errorf("%w: check constraint failed", ErrBadRequest) + case "22P02": // invalid_text_representation (e.g. bad UUID) + return fmt.Errorf("%w: invalid identifier format", ErrBadRequest) + } + } + if errors.Is(err, pgx.ErrNoRows) { + return ErrNotFound + } + return fmt.Errorf("%w: database operation failed", ErrUnavailable) +} + +func (s *PostgresStore) CreateUser(ctx context.Context, req model.CreateUserRequest) (*model.User, error) { + if !usernameRegex.MatchString(req.Username) { + return nil, fmt.Errorf("%w: username must match ^[a-z0-9_]{3,32}$", ErrBadRequest) + } + dnLen := utf8.RuneCountInString(req.DisplayName) + if dnLen < 1 || dnLen > 100 || strings.TrimSpace(req.DisplayName) == "" { + return nil, fmt.Errorf("%w: display_name must be 1..100 characters and not only whitespace", ErrBadRequest) + } + pwLen := utf8.RuneCountInString(req.Password) + if pwLen < 12 || pwLen > 128 || strings.TrimSpace(req.Password) == "" { + return nil, fmt.Errorf("%w: password must be 12..128 characters and not only whitespace", ErrBadRequest) + } + + pwHash, err := auth.HashPassword(req.Password) + if err != nil { + return nil, fmt.Errorf("%w: failed to hash password", ErrUnavailable) + } + + qCtx, cancel := context.WithTimeout(ctx, s.queryTimeout) + defer cancel() + + id := uuid.New().String() + now := NowUTC() + + s.sqlQueries.Add(1) + _, err = s.pool.Exec(qCtx, ` + INSERT INTO users (id, username, display_name, password_hash, created_at) + VALUES ($1::uuid, $2, $3, $4, $5::timestamptz) + `, id, req.Username, req.DisplayName, pwHash, now) + if err != nil { + return nil, classifyDBError(err) + } + + return &model.User{ + ID: id, + Username: req.Username, + DisplayName: req.DisplayName, + CreatedAt: FormatTime(now), + }, nil +} + +func (s *PostgresStore) AuthenticateAndCreateSession(ctx context.Context, username, password string, ttlSeconds int) (*model.Session, error) { + if !usernameRegex.MatchString(username) { + auth.VerifyDummy(password) + return nil, ErrUnauthorized + } + pwLen := utf8.RuneCountInString(password) + if pwLen < 1 || pwLen > 256 { + auth.VerifyDummy(password) + return nil, ErrUnauthorized + } + + qCtx, cancel := context.WithTimeout(ctx, s.queryTimeout) + defer cancel() + + var user model.User + var pwHash string + var createdAt time.Time + + s.sqlQueries.Add(1) + err := s.pool.QueryRow(qCtx, ` + SELECT id::text, username, display_name, password_hash, created_at + FROM users + WHERE username = $1 + `, username).Scan(&user.ID, &user.Username, &user.DisplayName, &pwHash, &createdAt) + if err != nil { + if errors.Is(err, pgx.ErrNoRows) { + auth.VerifyDummy(password) + return nil, ErrUnauthorized + } + return nil, classifyDBError(err) + } + user.CreatedAt = FormatTime(createdAt) + + valid, err := auth.VerifyPassword(password, pwHash) + if err != nil || !valid { + return nil, ErrUnauthorized + } + + rawToken, tokenHash, err := auth.GenerateSessionToken() + if err != nil { + return nil, ErrUnavailable + } + + if ttlSeconds <= 0 { + ttlSeconds = 3600 + } + now := NowUTC() + expiresAt := now.Add(time.Duration(ttlSeconds) * time.Second) + + s.sqlQueries.Add(1) + _, err = s.pool.Exec(qCtx, ` + INSERT INTO sessions (token_hash, user_id, expires_at, created_at) + VALUES ($1, $2::uuid, $3::timestamptz, $4::timestamptz) + `, tokenHash, user.ID, expiresAt, now) + if err != nil { + return nil, classifyDBError(err) + } + + s.cacheSession(ctx, tokenHash, user, expiresAt) + + return &model.Session{ + Token: rawToken, + TokenType: "Bearer", + ExpiresAt: FormatTime(expiresAt), + User: user, + }, nil +} + +func (s *PostgresStore) cacheSession(ctx context.Context, tokenHash string, user model.User, expiresAt time.Time) { + if s.rdb == nil { + return + } + remaining := time.Until(expiresAt) + if remaining <= 0 { + return + } + payload, err := json.Marshal(cachedSession{ + User: user, + ExpiresAt: expiresAt, + }) + if err != nil { + return + } + rCtx, cancel := context.WithTimeout(ctx, 300*time.Millisecond) + defer cancel() + key := "sess:" + tokenHash + if err := s.rdb.Set(rCtx, key, payload, remaining).Err(); err != nil { + log.Printf("WARN: redis session write failed (graceful bypass): %v", err) + return + } + _ = s.rdb.ExpireAt(rCtx, key, expiresAt).Err() +} + +func (s *PostgresStore) GetUserByToken(ctx context.Context, rawToken string) (*model.User, error) { + if len(rawToken) < 32 || len(rawToken) > 256 { + return nil, ErrUnauthorized + } + tokenHash := auth.HashToken(rawToken) + now := NowUTC() + + // 1. Check Valkey/Redis session cache first + if s.rdb != nil { + rCtx, cancel := context.WithTimeout(ctx, 300*time.Millisecond) + val, err := s.rdb.Get(rCtx, "sess:"+tokenHash).Bytes() + cancel() + if err == nil { + var cs cachedSession + if jsonErr := json.Unmarshal(val, &cs); jsonErr == nil { + if now.Before(cs.ExpiresAt) { + s.cacheHits.Add(1) + return &cs.User, nil + } + } + } else if !errors.Is(err, redis.Nil) { + log.Printf("WARN: redis session read failed (falling back to PostgreSQL): %v", err) + } + s.cacheMisses.Add(1) + } + + // 2. Fallback to PostgreSQL source of truth + qCtx, cancel := context.WithTimeout(ctx, s.queryTimeout) + defer cancel() + + var user model.User + var userCreatedAt, expiresAt time.Time + + s.sqlQueries.Add(1) + err := s.pool.QueryRow(qCtx, ` + SELECT u.id::text, u.username, u.display_name, u.created_at, s.expires_at + FROM sessions s + JOIN users u ON u.id = s.user_id + WHERE s.token_hash = $1 + `, tokenHash).Scan(&user.ID, &user.Username, &user.DisplayName, &userCreatedAt, &expiresAt) + if err != nil { + if errors.Is(err, pgx.ErrNoRows) { + return nil, ErrUnauthorized + } + return nil, classifyDBError(err) + } + + expiresAt = expiresAt.UTC().Truncate(time.Microsecond) + if !now.Before(expiresAt) { + // Session expired: clean up lazily and reject + _, _ = s.pool.Exec(qCtx, `DELETE FROM sessions WHERE token_hash = $1`, tokenHash) + return nil, ErrUnauthorized + } + + user.CreatedAt = FormatTime(userCreatedAt) + // Repopulate Valkey/Redis with original expires_at (never extending TTL!) + s.cacheSession(ctx, tokenHash, user, expiresAt) + + return &user, nil +} + +func (s *PostgresStore) RevokeSession(ctx context.Context, rawToken string) error { + if len(rawToken) < 32 || len(rawToken) > 256 { + return ErrUnauthorized + } + tokenHash := auth.HashToken(rawToken) + + if s.rdb != nil { + rCtx, cancel := context.WithTimeout(ctx, 300*time.Millisecond) + if err := s.rdb.Del(rCtx, "sess:"+tokenHash).Err(); err != nil { + log.Printf("WARN: redis session delete failed: %v", err) + } + cancel() + } + + qCtx, cancel := context.WithTimeout(ctx, s.queryTimeout) + defer cancel() + + s.sqlQueries.Add(1) + tag, err := s.pool.Exec(qCtx, ` + DELETE FROM sessions + WHERE token_hash = $1 AND expires_at > $2::timestamptz + `, tokenHash, NowUTC()) + if err != nil { + return classifyDBError(err) + } + if tag.RowsAffected() == 0 { + return ErrUnauthorized + } + return nil +} + +func (s *PostgresStore) CreateChat(ctx context.Context, callerID string, req model.CreateChatRequest) (*model.Chat, error) { + titleLen := utf8.RuneCountInString(req.Title) + if titleLen < 1 || titleLen > 100 || strings.TrimSpace(req.Title) == "" { + return nil, fmt.Errorf("%w: title must be 1..100 characters and not only whitespace", ErrBadRequest) + } + if len(req.MemberIDs) < 1 || len(req.MemberIDs) > 100 { + return nil, fmt.Errorf("%w: member_ids must contain 1..100 items", ErrBadRequest) + } + + seen := make(map[string]struct{}, len(req.MemberIDs)) + callerIncluded := false + for _, mid := range req.MemberIDs { + if _, err := uuid.Parse(mid); err != nil { + return nil, fmt.Errorf("%w: invalid UUID in member_ids", ErrBadRequest) + } + if _, exists := seen[mid]; exists { + return nil, fmt.Errorf("%w: duplicate user in member_ids", ErrBadRequest) + } + seen[mid] = struct{}{} + if mid == callerID { + callerIncluded = true + } + } + if !callerIncluded { + return nil, fmt.Errorf("%w: caller must be included in member_ids", ErrBadRequest) + } + + qCtx, cancel := context.WithTimeout(ctx, s.queryTimeout) + defer cancel() + + // Composite atomic transaction: insert chat + insert all chat_members + tx, err := s.pool.Begin(qCtx) + if err != nil { + return nil, classifyDBError(err) + } + defer func() { _ = tx.Rollback(qCtx) }() + + chatID := uuid.New().String() + now := NowUTC() + + s.sqlQueries.Add(1) + _, err = tx.Exec(qCtx, ` + INSERT INTO chats (id, title, created_at) + VALUES ($1::uuid, $2, $3::timestamptz) + `, chatID, req.Title, now) + if err != nil { + return nil, classifyDBError(err) + } + + for idx, mid := range req.MemberIDs { + s.sqlQueries.Add(1) + _, err = tx.Exec(qCtx, ` + INSERT INTO chat_members (chat_id, user_id, ord) + VALUES ($1::uuid, $2::uuid, $3) + `, chatID, mid, idx) + if err != nil { + // If any member does not exist in users, FK constraint 23503 triggers and rolls back the entire chat creation! + return nil, classifyDBError(err) + } + } + + if err := tx.Commit(qCtx); err != nil { + return nil, classifyDBError(err) + } + + membersCopy := make([]string, len(req.MemberIDs)) + copy(membersCopy, req.MemberIDs) + + return &model.Chat{ + ID: chatID, + Title: req.Title, + MemberIDs: membersCopy, + CreatedAt: FormatTime(now), + }, nil +} + +func (s *PostgresStore) ListChats(ctx context.Context, callerID string, limit int, cursorStr string) (*model.ChatPage, error) { + if limit < 1 || limit > 100 { + return nil, fmt.Errorf("%w: limit must be between 1 and 100", ErrBadRequest) + } + + scope := "chats:" + callerID + var hasCursor bool + var cursorTime time.Time + var cursorID string + if cursorStr != "" { + var err error + cursorTime, cursorID, err = DecodeCursor(cursorStr, scope) + if err != nil { + return nil, err + } + hasCursor = true + } + + qCtx, cancel := context.WithTimeout(ctx, s.queryTimeout) + defer cancel() + + var rows pgx.Rows + var err error + s.sqlQueries.Add(1) + if !hasCursor { + rows, err = s.pool.Query(qCtx, ` + SELECT c.id::text, c.title, c.created_at, + array_agg(cm_all.user_id::text ORDER BY cm_all.ord ASC) AS member_ids + FROM chats c + JOIN chat_members cm_me ON cm_me.chat_id = c.id AND cm_me.user_id = $1::uuid + JOIN chat_members cm_all ON cm_all.chat_id = c.id + GROUP BY c.id, c.title, c.created_at + ORDER BY c.created_at ASC, c.id ASC + LIMIT $2 + `, callerID, limit+1) + } else { + rows, err = s.pool.Query(qCtx, ` + SELECT c.id::text, c.title, c.created_at, + array_agg(cm_all.user_id::text ORDER BY cm_all.ord ASC) AS member_ids + FROM chats c + JOIN chat_members cm_me ON cm_me.chat_id = c.id AND cm_me.user_id = $1::uuid + JOIN chat_members cm_all ON cm_all.chat_id = c.id + WHERE (c.created_at, c.id) > ($2::timestamptz, $3::uuid) + GROUP BY c.id, c.title, c.created_at + ORDER BY c.created_at ASC, c.id ASC + LIMIT $4 + `, callerID, cursorTime, cursorID, limit+1) + } + if err != nil { + return nil, classifyDBError(err) + } + defer rows.Close() + + items := make([]model.Chat, 0, limit) + for rows.Next() { + var ch model.Chat + var createdAt time.Time + if err := rows.Scan(&ch.ID, &ch.Title, &createdAt, &ch.MemberIDs); err != nil { + return nil, classifyDBError(err) + } + ch.CreatedAt = FormatTime(createdAt) + items = append(items, ch) + } + if err := rows.Err(); err != nil { + return nil, classifyDBError(err) + } + + var nextCursor *string + if len(items) > limit { + items = items[:limit] + last := items[limit-1] + c := EncodeCursor(scope, last.CreatedAt, last.ID) + nextCursor = &c + } + + return &model.ChatPage{ + Items: items, + NextCursor: nextCursor, + }, nil +} + +func (s *PostgresStore) GetChat(ctx context.Context, callerID, chatID string) (*model.Chat, error) { + qCtx, cancel := context.WithTimeout(ctx, s.queryTimeout) + defer cancel() + + var ch model.Chat + var createdAt time.Time + + s.sqlQueries.Add(1) + err := s.pool.QueryRow(qCtx, ` + SELECT c.id::text, c.title, c.created_at, + array_agg(cm.user_id::text ORDER BY cm.ord ASC) AS member_ids + FROM chats c + JOIN chat_members cm ON cm.chat_id = c.id + WHERE c.id = $1::uuid + AND EXISTS ( + SELECT 1 FROM chat_members WHERE chat_id = c.id AND user_id = $2::uuid + ) + GROUP BY c.id, c.title, c.created_at + `, chatID, callerID).Scan(&ch.ID, &ch.Title, &createdAt, &ch.MemberIDs) + if err != nil { + return nil, classifyDBError(err) + } + ch.CreatedAt = FormatTime(createdAt) + return &ch, nil +} + +func (s *PostgresStore) lookupIdempotency(ctx context.Context, callerID, chatID, idempotencyKey, reqHash string, now time.Time) (*model.Message, bool, error) { + qCtx, cancel := context.WithTimeout(ctx, s.queryTimeout) + defer cancel() + + var existingHash string + var msg model.Message + var createdAt time.Time + + s.sqlQueries.Add(1) + err := s.pool.QueryRow(qCtx, ` + SELECT ik.request_hash, m.id::text, m.chat_id::text, m.sender_id::text, m.text, m.created_at, + COALESCE(array_agg(ma.attachment_id::text ORDER BY ma.ord ASC) FILTER (WHERE ma.attachment_id IS NOT NULL), '{}') AS attachment_ids + FROM idempotency_keys ik + JOIN messages m ON m.id = ik.message_id + LEFT JOIN message_attachments ma ON ma.message_id = m.id + WHERE ik.user_id = $1::uuid + AND ik.chat_id = $2::uuid + AND ik.idempotency_key = $3 + AND ik.expires_at > $4::timestamptz + GROUP BY ik.request_hash, m.id, m.chat_id, m.sender_id, m.text, m.created_at + `, callerID, chatID, idempotencyKey, now).Scan(&existingHash, &msg.ID, &msg.ChatID, &msg.SenderID, &msg.Text, &createdAt, &msg.AttachmentIDs) + if err != nil { + if errors.Is(err, pgx.ErrNoRows) { + return nil, false, nil + } + return nil, false, classifyDBError(err) + } + if existingHash != reqHash { + return nil, true, fmt.Errorf("%w: idempotency key already used with different request body", ErrConflict) + } + if msg.AttachmentIDs == nil { + msg.AttachmentIDs = []string{} + } + msg.CreatedAt = FormatTime(createdAt) + return &msg, true, nil +} + +func canonicalMessageHash(text string, attachmentIDs []string) string { + normAtts := attachmentIDs + if normAtts == nil { + normAtts = []string{} + } + canonicalBytes, _ := json.Marshal(struct { + Text string `json:"text"` + AttachmentIDs []string `json:"attachment_ids"` + }{ + Text: text, + AttachmentIDs: normAtts, + }) + sum := sha256.Sum256(canonicalBytes) + return hex.EncodeToString(sum[:]) +} + +func (s *PostgresStore) IsChatMember(ctx context.Context, callerID, chatID string) (bool, error) { + return s.checkChatMembership(ctx, chatID, callerID, s.getCacheMode()) +} + +func (s *PostgresStore) CreateAttachment(ctx context.Context, att model.Attachment) (*model.Attachment, error) { + qCtx, cancel := context.WithTimeout(ctx, s.queryTimeout) + defer cancel() + + now := NowUTC() + s.sqlQueries.Add(1) + _, err := s.pool.Exec(qCtx, ` + 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::uuid, $2::uuid, $3::uuid, $4, $5, $6, $7, $8, 'ready', NULL, $9::timestamptz, $9::timestamptz) + `, att.ID, att.ChatID, att.OwnerID, att.Filename, att.ContentType, att.SizeBytes, att.SHA256, att.ObjectKey, now) + if err != nil { + return nil, classifyDBError(err) + } + + ts := FormatTime(now) + out := att + out.Status = "ready" + out.Variants = []model.Variant{} + out.ErrorCode = nil + out.CreatedAt = ts + out.UpdatedAt = ts + return &out, nil +} + +func (s *PostgresStore) GetAttachment(ctx context.Context, callerID, attachmentID string) (*model.Attachment, error) { + qCtx, cancel := context.WithTimeout(ctx, s.queryTimeout) + defer cancel() + + var att model.Attachment + var createdAt, updatedAt time.Time + s.sqlQueries.Add(1) + err := s.pool.QueryRow(qCtx, ` + SELECT a.id::text, a.chat_id::text, a.owner_id::text, a.filename, a.content_type, + a.size_bytes, a.sha256, a.object_key, a.status, a.error_code, a.created_at, a.updated_at + FROM attachments a + JOIN chat_members cm ON cm.chat_id = a.chat_id AND cm.user_id = $2::uuid + WHERE a.id = $1::uuid + `, attachmentID, callerID).Scan( + &att.ID, &att.ChatID, &att.OwnerID, &att.Filename, &att.ContentType, + &att.SizeBytes, &att.SHA256, &att.ObjectKey, &att.Status, &att.ErrorCode, &createdAt, &updatedAt, + ) + if err != nil { + return nil, classifyDBError(err) + } + att.Variants = []model.Variant{} + att.CreatedAt = FormatTime(createdAt) + att.UpdatedAt = FormatTime(updatedAt) + return &att, nil +} + +func (s *PostgresStore) CreateMessage(ctx context.Context, callerID, chatID, idempotencyKey string, idempotencyTTLSeconds int, req model.CreateMessageRequest) (*model.Message, error) { + if !IdempotencyKeyRegex.MatchString(idempotencyKey) { + return nil, fmt.Errorf("%w: invalid or missing Idempotency-Key header", ErrBadRequest) + } + if len(req.AttachmentIDs) > 10 { + return nil, fmt.Errorf("%w: attachment_ids must contain at most 10 items", ErrBadRequest) + } + seenAtts := make(map[string]struct{}, len(req.AttachmentIDs)) + normAtts := make([]string, 0, len(req.AttachmentIDs)) + for _, aid := range req.AttachmentIDs { + if _, err := uuid.Parse(aid); err != nil { + return nil, fmt.Errorf("%w: invalid UUID in attachment_ids", ErrBadRequest) + } + if _, dup := seenAtts[aid]; dup { + return nil, fmt.Errorf("%w: duplicate attachment_id in attachment_ids", ErrBadRequest) + } + seenAtts[aid] = struct{}{} + normAtts = append(normAtts, aid) + } + + textLen := utf8.RuneCountInString(req.Text) + if textLen > 4000 { + return nil, fmt.Errorf("%w: text must be at most 4000 characters", ErrBadRequest) + } + if strings.TrimSpace(req.Text) == "" && len(normAtts) == 0 { + return nil, fmt.Errorf("%w: text must not be empty/whitespace when attachment_ids is empty", ErrBadRequest) + } + if idempotencyTTLSeconds <= 0 { + idempotencyTTLSeconds = 86400 + } + + mode := s.getCacheMode() + // Always verify membership/chat existence first so outsiders get 404 without touching idempotency_keys. + isMember, err := s.checkChatMembership(ctx, chatID, callerID, mode) + if err != nil { + return nil, err + } + if !isMember { + return nil, ErrNotFound + } + + reqHash := canonicalMessageHash(req.Text, normAtts) + now := NowUTC() + + // Fast-path replay lookup + if existing, found, err := s.lookupIdempotency(ctx, callerID, chatID, idempotencyKey, reqHash, now); err != nil { + return nil, err + } else if found { + return existing, nil + } + + qCtx, cancel := context.WithTimeout(ctx, s.queryTimeout) + defer cancel() + + // Validate that all referenced attachments belong to this chat and are ready (foreign/unknown -> 404, failed -> 409). + if len(normAtts) > 0 { + s.sqlQueries.Add(1) + rows, aErr := s.pool.Query(qCtx, ` + SELECT id::text, status + FROM attachments + WHERE chat_id = $1::uuid AND id = ANY($2::uuid[]) + `, chatID, normAtts) + if aErr != nil { + return nil, classifyDBError(aErr) + } + foundStatuses := make(map[string]string, len(normAtts)) + for rows.Next() { + var aid, status string + if sErr := rows.Scan(&aid, &status); sErr != nil { + rows.Close() + return nil, classifyDBError(sErr) + } + foundStatuses[aid] = status + } + rErr := rows.Err() + rows.Close() + if rErr != nil { + return nil, classifyDBError(rErr) + } + if len(foundStatuses) != len(normAtts) { + return nil, ErrNotFound + } + for _, aid := range normAtts { + if foundStatuses[aid] != "ready" { + return nil, fmt.Errorf("%w: attachment is not ready", ErrConflict) + } + } + } + + tx, err := s.pool.Begin(qCtx) + if err != nil { + return nil, classifyDBError(err) + } + defer func() { _ = tx.Rollback(qCtx) }() + + msgID := uuid.New().String() + expiresAt := now.Add(time.Duration(idempotencyTTLSeconds) * time.Second) + + batch := &pgx.Batch{} + batch.Queue(` + DELETE FROM idempotency_keys + WHERE user_id = $1::uuid AND chat_id = $2::uuid AND idempotency_key = $3 AND expires_at <= $4::timestamptz + `, callerID, chatID, idempotencyKey, now) + batch.Queue(` + INSERT INTO messages (id, chat_id, sender_id, text, created_at) + VALUES ($1::uuid, $2::uuid, $3::uuid, $4, $5::timestamptz) + `, msgID, chatID, callerID, req.Text, now) + for idx, aid := range normAtts { + batch.Queue(` + INSERT INTO message_attachments (message_id, attachment_id, ord) + VALUES ($1::uuid, $2::uuid, $3) + `, msgID, aid, idx) + } + batch.Queue(` + INSERT INTO idempotency_keys (user_id, chat_id, idempotency_key, request_hash, message_id, created_at, expires_at) + VALUES ($1::uuid, $2::uuid, $3, $4, $5::uuid, $6::timestamptz, $7::timestamptz) + ON CONFLICT (user_id, chat_id, idempotency_key) DO NOTHING + `, callerID, chatID, idempotencyKey, reqHash, msgID, now, expiresAt) + + expectedCmds := 3 + len(normAtts) + s.sqlQueries.Add(int64(expectedCmds)) + br := tx.SendBatch(qCtx, batch) + var claimed int64 + for cmdIdx := 0; cmdIdx < expectedCmds; cmdIdx++ { + tag, bErr := br.Exec() + if bErr != nil { + _ = br.Close() + return nil, classifyDBError(bErr) + } + if cmdIdx == expectedCmds-1 { + claimed = tag.RowsAffected() + } + } + if cErr := br.Close(); cErr != nil { + return nil, classifyDBError(cErr) + } + + if claimed == 0 { + // Another replica won the concurrent idempotency race; roll back duplicate inserts and read winner. + _ = tx.Rollback(qCtx) + existing, found, lErr := s.lookupIdempotency(ctx, callerID, chatID, idempotencyKey, reqHash, now) + if lErr != nil { + return nil, lErr + } + if !found { + return nil, ErrUnavailable + } + return existing, nil + } + + if err := tx.Commit(qCtx); err != nil { + return nil, classifyDBError(err) + } + + // Invalidate chat history cache immediately on write so any replica reads fresh history (Read-After-Write). + if s.rdb != nil && mode == "write_through_versioned" { + rCtx, rCancel := context.WithTimeout(ctx, 300*time.Millisecond) + verKey := "chat_ver:" + chatID + if err := s.rdb.Incr(rCtx, verKey).Err(); err != nil { + log.Printf("WARN: redis cache version increment failed: %v", err) + } else { + _ = s.rdb.Expire(rCtx, verKey, 24*time.Hour).Err() + } + rCancel() + } + + return &model.Message{ + ID: msgID, + ChatID: chatID, + SenderID: callerID, + Text: req.Text, + CreatedAt: FormatTime(now), + AttachmentIDs: normAtts, + }, nil +} + +func (s *PostgresStore) checkChatMembership(ctx context.Context, chatID, callerID, mode string) (bool, error) { + memKey := fmt.Sprintf("chat_mem:%s:%s", chatID, callerID) + if s.rdb != nil && mode != "disabled" { + rCtx, rCancel := context.WithTimeout(ctx, 200*time.Millisecond) + val, err := s.rdb.Get(rCtx, memKey).Result() + rCancel() + if err == nil { + return val == "1", nil + } + } + + queryMembership := func() (bool, error) { + qCtx, cancel := context.WithTimeout(ctx, s.queryTimeout) + defer cancel() + var isMember bool + s.sqlQueries.Add(1) + err := s.pool.QueryRow(qCtx, ` + SELECT EXISTS ( + SELECT 1 FROM chat_members WHERE chat_id = $1::uuid AND user_id = $2::uuid + ) + `, chatID, callerID).Scan(&isMember) + if err != nil { + return false, classifyDBError(err) + } + if s.rdb != nil && mode != "disabled" { + rCtx, rCancel := context.WithTimeout(ctx, 200*time.Millisecond) + strVal := "0" + if isMember { + strVal = "1" + } + _ = s.rdb.Set(rCtx, memKey, strVal, 10*time.Minute).Err() + rCancel() + } + return isMember, nil + } + + if mode == "write_through_versioned" { + res, err, _ := s.sfGroup.Do(memKey, func() (interface{}, error) { + return queryMembership() + }) + if err != nil { + return false, err + } + return res.(bool), nil + } + return queryMembership() +} + +func (s *PostgresStore) ListMessages(ctx context.Context, callerID, chatID string, limit int, cursorStr string) (*model.MessagePage, error) { + if limit < 1 || limit > 100 { + return nil, fmt.Errorf("%w: limit must be between 1 and 100", ErrBadRequest) + } + + scope := "msgs:" + chatID + var hasCursor bool + var cursorTime time.Time + var cursorID string + if cursorStr != "" { + var err error + cursorTime, cursorID, err = DecodeCursor(cursorStr, scope) + if err != nil { + return nil, err + } + hasCursor = true + } + + mode := s.getCacheMode() + + // 1. Always enforce membership check before returning cached or DB history + isMember, err := s.checkChatMembership(ctx, chatID, callerID, mode) + if err != nil { + return nil, err + } + if !isMember { + return nil, ErrNotFound + } + + // 2. Determine cache key + var cacheKey string + if s.rdb != nil && mode != "disabled" { + if mode == "naive_ttl" { + cacheKey = fmt.Sprintf("msg_page:%s:naive:l%d:c%s", chatID, limit, cursorStr) + } else { + rCtx, rCancel := context.WithTimeout(ctx, 200*time.Millisecond) + ver, err := s.rdb.Get(rCtx, "chat_ver:"+chatID).Int64() + rCancel() + if err != nil && !errors.Is(err, redis.Nil) { + log.Printf("WARN: redis version read failed (bypassing cache): %v", err) + cacheKey = "" + } else { + cacheKey = fmt.Sprintf("msg_page:%s:v%d:l%d:c%s", chatID, ver, limit, cursorStr) + } + } + } + + // 3. Check cache + if cacheKey != "" { + rCtx, rCancel := context.WithTimeout(ctx, 200*time.Millisecond) + cachedBytes, err := s.rdb.Get(rCtx, cacheKey).Bytes() + rCancel() + if err == nil { + var page model.MessagePage + if json.Unmarshal(cachedBytes, &page) == nil { + s.cacheHits.Add(1) + return &page, nil + } + } else if !errors.Is(err, redis.Nil) { + log.Printf("WARN: redis page read failed (falling back to PostgreSQL): %v", err) + } + s.cacheMisses.Add(1) + } + + fetchFromDB := func() (*model.MessagePage, error) { + dbCtx, dbCancel := context.WithTimeout(ctx, s.queryTimeout) + defer dbCancel() + + var rows pgx.Rows + var qErr error + s.sqlQueries.Add(1) + if !hasCursor { + rows, qErr = s.pool.Query(dbCtx, ` + SELECT m.id::text, m.chat_id::text, m.sender_id::text, m.text, m.created_at, + COALESCE(array_agg(ma.attachment_id::text ORDER BY ma.ord ASC) FILTER (WHERE ma.attachment_id IS NOT NULL), '{}') AS attachment_ids + FROM messages m + LEFT JOIN message_attachments ma ON ma.message_id = m.id + WHERE m.chat_id = $1::uuid + GROUP BY m.id, m.chat_id, m.sender_id, m.text, m.created_at + ORDER BY m.created_at ASC, m.id ASC + LIMIT $2 + `, chatID, limit+1) + } else { + rows, qErr = s.pool.Query(dbCtx, ` + SELECT m.id::text, m.chat_id::text, m.sender_id::text, m.text, m.created_at, + COALESCE(array_agg(ma.attachment_id::text ORDER BY ma.ord ASC) FILTER (WHERE ma.attachment_id IS NOT NULL), '{}') AS attachment_ids + FROM messages m + LEFT JOIN message_attachments ma ON ma.message_id = m.id + WHERE m.chat_id = $1::uuid + AND (m.created_at, m.id) > ($2::timestamptz, $3::uuid) + GROUP BY m.id, m.chat_id, m.sender_id, m.text, m.created_at + ORDER BY m.created_at ASC, m.id ASC + LIMIT $4 + `, chatID, cursorTime, cursorID, limit+1) + } + if qErr != nil { + return nil, classifyDBError(qErr) + } + defer rows.Close() + + items := make([]model.Message, 0, limit) + for rows.Next() { + var m model.Message + var createdAt time.Time + if err := rows.Scan(&m.ID, &m.ChatID, &m.SenderID, &m.Text, &createdAt, &m.AttachmentIDs); err != nil { + return nil, classifyDBError(err) + } + if m.AttachmentIDs == nil { + m.AttachmentIDs = []string{} + } + m.CreatedAt = FormatTime(createdAt) + items = append(items, m) + } + if err := rows.Err(); err != nil { + return nil, classifyDBError(err) + } + + var nextCursor *string + if len(items) > limit { + items = items[:limit] + last := items[limit-1] + c := EncodeCursor(scope, last.CreatedAt, last.ID) + nextCursor = &c + } + + page := &model.MessagePage{ + Items: items, + NextCursor: nextCursor, + } + + if cacheKey != "" && s.rdb != nil { + if payload, mErr := json.Marshal(page); mErr == nil { + rCtx, rCancel := context.WithTimeout(ctx, 200*time.Millisecond) + ttl := 30 * time.Second + if mode == "naive_ttl" { + ttl = 5 * time.Second + } + _ = s.rdb.Set(rCtx, cacheKey, payload, ttl).Err() + rCancel() + } + } + + return page, nil + } + + // 4. Use singleflight in write_through_versioned mode to prevent cache stampede! + if mode == "write_through_versioned" && cacheKey != "" { + res, err, shared := s.sfGroup.Do(cacheKey, func() (interface{}, error) { + return fetchFromDB() + }) + if shared { + s.singleflightShared.Add(1) + } + if err != nil { + return nil, err + } + return res.(*model.MessagePage), nil + } + + return fetchFromDB() +} + +type dbAttachmentRow struct { + ID string + ObjectKey string + Status string + CreatedAt time.Time + Referenced bool +} + +func (s *PostgresStore) Reconcile(ctx context.Context, s3 *s3store.Client, gracePeriod time.Duration, dryRun bool) (*ReconcileReport, error) { + started := time.Now() + report := &ReconcileReport{ + DryRun: dryRun, + GracePeriodSeconds: int64(gracePeriod.Seconds()), + OrphanS3Found: []string{}, + OrphanS3Deleted: []string{}, + OrphanS3SkippedGrace: []string{}, + MissingS3Found: []string{}, + MissingS3MarkedFailed: []string{}, + MissingS3Deleted: []string{}, + MissingS3SkippedGrace: []string{}, + } + + if s3 == nil { + return nil, ErrUnavailable + } + + conn, err := s.pool.Acquire(ctx) + if err != nil { + return nil, classifyDBError(err) + } + defer conn.Release() + + var locked bool + report.DBQueries++ + if err := conn.QueryRow(ctx, `SELECT pg_try_advisory_lock(20260401)`).Scan(&locked); err != nil { + return nil, classifyDBError(err) + } + if !locked { + report.LockAcquired = false + report.DurationMs = float64(time.Since(started).Microseconds()) / 1000.0 + return report, nil + } + defer func() { + uCtx, cancel := context.WithTimeout(context.Background(), 2*time.Second) + _, _ = conn.Exec(uCtx, `SELECT pg_advisory_unlock(20260401)`) + cancel() + }() + report.LockAcquired = true + + // 1. Scan S3 objects under attachments/ + report.S3ListCalls++ + s3Objs, err := s3.ListObjects(ctx, "attachments/") + if err != nil { + return nil, ErrUnavailable + } + report.S3ObjectsScanned = len(s3Objs) + s3Map := make(map[string]s3store.ObjectSummary, len(s3Objs)) + for _, obj := range s3Objs { + s3Map[obj.Key] = obj + } + + // 2. Scan PostgreSQL attachments + reference state + qCtx, cancel := context.WithTimeout(ctx, 15*time.Second) + defer cancel() + report.DBQueries++ + rows, err := conn.Query(qCtx, ` + SELECT a.id::text, a.object_key, a.status, a.created_at, + EXISTS (SELECT 1 FROM message_attachments ma WHERE ma.attachment_id = a.id) AS referenced + FROM attachments a + `) + if err != nil { + return nil, classifyDBError(err) + } + var dbRows []dbAttachmentRow + dbKeyMap := make(map[string]dbAttachmentRow) + for rows.Next() { + var r dbAttachmentRow + if sErr := rows.Scan(&r.ID, &r.ObjectKey, &r.Status, &r.CreatedAt, &r.Referenced); sErr != nil { + rows.Close() + return nil, classifyDBError(sErr) + } + dbRows = append(dbRows, r) + dbKeyMap[r.ObjectKey] = r + } + if rErr := rows.Err(); rErr != nil { + rows.Close() + return nil, classifyDBError(rErr) + } + rows.Close() + report.DBRowsScanned = len(dbRows) + + now := time.Now().UTC() + + // 3. Detect & safely clean orphan S3 objects (present in S3, absent in DB) + for _, obj := range s3Objs { + if _, exists := dbKeyMap[obj.Key]; exists { + continue + } + age := now.Sub(obj.LastModified.UTC()) + if age <= gracePeriod { + report.OrphanS3SkippedGrace = append(report.OrphanS3SkippedGrace, obj.Key) + continue + } + report.OrphanS3Found = append(report.OrphanS3Found, obj.Key) + if !dryRun { + report.S3RemoveCalls++ + if rErr := s3.RemoveObject(ctx, obj.Key); rErr != nil { + return nil, ErrUnavailable + } + report.OrphanS3Deleted = append(report.OrphanS3Deleted, obj.Key) + } + } + + // 4. Detect & safely reconcile missing S3 objects (present in DB, absent in S3) + for _, row := range dbRows { + if _, exists := s3Map[row.ObjectKey]; exists { + if row.Status == "ready" { + report.HealthyPreserved++ + } + continue + } + if row.Status == "failed" { + continue + } + age := now.Sub(row.CreatedAt.UTC()) + if age <= gracePeriod { + report.MissingS3SkippedGrace = append(report.MissingS3SkippedGrace, row.ID) + continue + } + report.MissingS3Found = append(report.MissingS3Found, row.ID) + if !dryRun { + if row.Status == "pending" && !row.Referenced { + report.DBQueries++ + if _, dErr := conn.Exec(qCtx, `DELETE FROM attachments WHERE id = $1::uuid`, row.ID); dErr != nil { + return nil, classifyDBError(dErr) + } + report.MissingS3Deleted = append(report.MissingS3Deleted, row.ID) + } else { + report.DBQueries++ + if _, uErr := conn.Exec(qCtx, ` + UPDATE attachments + SET status = 'failed', error_code = 'missing_s3_object', updated_at = $2::timestamptz + WHERE id = $1::uuid + `, row.ID, NowUTC()); uErr != nil { + return nil, classifyDBError(uErr) + } + report.MissingS3MarkedFailed = append(report.MissingS3MarkedFailed, row.ID) + } + } + } + + report.DurationMs = float64(time.Since(started).Microseconds()) / 1000.0 + return report, nil +} diff --git a/internal/store/store.go b/internal/store/store.go new file mode 100644 index 00000000..45029d71 --- /dev/null +++ b/internal/store/store.go @@ -0,0 +1,130 @@ +package store + +import ( + "context" + "encoding/base64" + "encoding/json" + "errors" + "regexp" + "time" + + "github.com/google/uuid" + "messenger/internal/model" + "messenger/internal/s3store" +) + +var ( + ErrConflict = errors.New("conflict") + ErrNotFound = errors.New("not found") + ErrBadRequest = errors.New("bad request") + ErrUnauthorized = errors.New("unauthorized") + ErrUnavailable = errors.New("service unavailable") + + usernameRegex = regexp.MustCompile(`^[a-z0-9_]{3,32}$`) + IdempotencyKeyRegex = regexp.MustCompile(`^[A-Za-z0-9._:-]{1,128}$`) +) + +const TimestampFormat = "2006-01-02T15:04:05.000000Z" + +func FormatTime(t time.Time) string { + return t.UTC().Truncate(time.Microsecond).Format(TimestampFormat) +} + +func NowUTC() time.Time { + return time.Now().UTC().Truncate(time.Microsecond) +} + +type MigrationFile struct { + Version string + SQL string +} + +type ReconcileReport struct { + DryRun bool `json:"dry_run"` + GracePeriodSeconds int64 `json:"grace_period_seconds"` + LockAcquired bool `json:"lock_acquired"` + S3ObjectsScanned int `json:"s3_objects_scanned"` + DBRowsScanned int `json:"db_rows_scanned"` + OrphanS3Found []string `json:"orphan_s3_found"` + OrphanS3Deleted []string `json:"orphan_s3_deleted"` + OrphanS3SkippedGrace []string `json:"orphan_s3_skipped_grace"` + MissingS3Found []string `json:"missing_s3_found"` + MissingS3MarkedFailed []string `json:"missing_s3_marked_failed"` + MissingS3Deleted []string `json:"missing_s3_deleted"` + MissingS3SkippedGrace []string `json:"missing_s3_skipped_grace"` + HealthyPreserved int `json:"healthy_preserved"` + S3ListCalls int `json:"s3_list_calls"` + S3RemoveCalls int `json:"s3_remove_calls"` + DBQueries int `json:"db_queries"` + DurationMs float64 `json:"duration_ms"` +} + +type StoreStats struct { + SQLQueries int64 `json:"sql_queries"` + CacheHits int64 `json:"cache_hits"` + CacheMisses int64 `json:"cache_misses"` + SingleflightShared int64 `json:"singleflight_shared"` + CacheMode string `json:"cache_mode"` + WriteMode string `json:"write_mode"` + RedisConnected bool `json:"redis_connected"` +} + +type Store interface { + Ready(ctx context.Context) error + RunMigrations(ctx context.Context, migrations []MigrationFile) error + CreateUser(ctx context.Context, req model.CreateUserRequest) (*model.User, error) + AuthenticateAndCreateSession(ctx context.Context, username, password string, ttlSeconds int) (*model.Session, error) + GetUserByToken(ctx context.Context, rawToken string) (*model.User, error) + RevokeSession(ctx context.Context, rawToken string) error + CreateChat(ctx context.Context, callerID string, req model.CreateChatRequest) (*model.Chat, error) + ListChats(ctx context.Context, callerID string, limit int, cursorStr string) (*model.ChatPage, error) + GetChat(ctx context.Context, callerID, chatID string) (*model.Chat, error) + IsChatMember(ctx context.Context, callerID, chatID string) (bool, error) + CreateAttachment(ctx context.Context, att model.Attachment) (*model.Attachment, error) + GetAttachment(ctx context.Context, callerID, attachmentID string) (*model.Attachment, error) + Reconcile(ctx context.Context, s3 *s3store.Client, gracePeriod time.Duration, dryRun bool) (*ReconcileReport, error) + CreateMessage(ctx context.Context, callerID, chatID, idempotencyKey string, idempotencyTTLSeconds int, req model.CreateMessageRequest) (*model.Message, error) + ListMessages(ctx context.Context, callerID, chatID string, limit int, cursorStr string) (*model.MessagePage, error) + Stats(ctx context.Context) StoreStats + ResetStats() + SetCacheMode(mode string) + Close() +} + +func EncodeCursor(scope, timestamp, id string) string { + data := model.CursorData{ + Scope: scope, + Time: timestamp, + ID: id, + } + b, _ := json.Marshal(data) + return base64.RawURLEncoding.EncodeToString(b) +} + +func DecodeCursor(cursorStr, expectedScope string) (time.Time, string, error) { + b, err := base64.RawURLEncoding.DecodeString(cursorStr) + if err != nil { + b, err = base64.URLEncoding.DecodeString(cursorStr) + if err != nil { + b, err = base64.StdEncoding.DecodeString(cursorStr) + if err != nil { + return time.Time{}, "", ErrBadRequest + } + } + } + var data model.CursorData + if err := json.Unmarshal(b, &data); err != nil { + return time.Time{}, "", ErrBadRequest + } + if data.Scope != expectedScope || data.Time == "" || data.ID == "" { + return time.Time{}, "", ErrBadRequest + } + if _, err := uuid.Parse(data.ID); err != nil { + return time.Time{}, "", ErrBadRequest + } + t, err := time.Parse(time.RFC3339Nano, data.Time) + if err != nil { + return time.Time{}, "", ErrBadRequest + } + return t.UTC().Truncate(time.Microsecond), data.ID, nil +} diff --git a/migrations/001_init.sql b/migrations/001_init.sql new file mode 100644 index 00000000..cbdae46f --- /dev/null +++ b/migrations/001_init.sql @@ -0,0 +1,53 @@ +-- Migration 001: Initial schema for Lab 2 (PostgreSQL, Sessions, Chats, Messages) +-- Idempotent DDL with primary/foreign keys, constraints, and keyset pagination indexes. + +CREATE TABLE IF NOT EXISTS users ( + id UUID PRIMARY KEY, + username VARCHAR(32) NOT NULL UNIQUE + CONSTRAINT chk_users_username CHECK (username ~ '^[a-z0-9_]{3,32}$'), + display_name VARCHAR(100) NOT NULL + CONSTRAINT chk_users_display_name CHECK (char_length(btrim(display_name)) >= 1 AND char_length(display_name) <= 100), + password_hash TEXT NOT NULL, + created_at TIMESTAMPTZ NOT NULL +); + +CREATE TABLE IF NOT EXISTS sessions ( + token_hash CHAR(64) PRIMARY KEY, + user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE, + expires_at TIMESTAMPTZ NOT NULL, + created_at TIMESTAMPTZ NOT NULL +); + +CREATE INDEX IF NOT EXISTS idx_sessions_user_id ON sessions (user_id); +CREATE INDEX IF NOT EXISTS idx_sessions_expires_at ON sessions (expires_at); + +CREATE TABLE IF NOT EXISTS chats ( + id UUID PRIMARY KEY, + title VARCHAR(100) NOT NULL + CONSTRAINT chk_chats_title CHECK (char_length(btrim(title)) >= 1 AND char_length(title) <= 100), + created_at TIMESTAMPTZ NOT NULL +); + +CREATE INDEX IF NOT EXISTS idx_chats_created_id ON chats (created_at ASC, id ASC); + +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 CASCADE, + ord INT NOT NULL DEFAULT 0, + PRIMARY KEY (chat_id, user_id) +); + +CREATE INDEX IF NOT EXISTS idx_chat_members_user_chat 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 + CONSTRAINT chk_messages_text CHECK (char_length(btrim(text)) >= 1 AND char_length(text) <= 4000), + created_at TIMESTAMPTZ NOT NULL +); + +-- Composite B-Tree index supporting keyset pagination: +-- WHERE chat_id = $1 AND (created_at, id) > ($2, $3) ORDER BY created_at ASC, id ASC LIMIT $4 +CREATE INDEX IF NOT EXISTS idx_messages_chat_created_id ON messages (chat_id, created_at ASC, id ASC); diff --git a/migrations/002_idempotency.sql b/migrations/002_idempotency.sql new file mode 100644 index 00000000..fb692457 --- /dev/null +++ b/migrations/002_idempotency.sql @@ -0,0 +1,17 @@ +-- Migration 002: Idempotency keys for POST /api/v1/chats/{chat_id}/messages (Lab 3) +-- Scope: (user_id, chat_id, idempotency_key), TTL 24h by default. +-- Message creation and idempotency key registration happen atomically in one PostgreSQL transaction. + +CREATE TABLE IF NOT EXISTS idempotency_keys ( + user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE, + chat_id UUID NOT NULL REFERENCES chats(id) ON DELETE CASCADE, + idempotency_key VARCHAR(128) NOT NULL + CONSTRAINT chk_idempotency_key_format CHECK (idempotency_key ~ '^[A-Za-z0-9._:-]{1,128}$'), + request_hash CHAR(64) 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 (user_id, chat_id, idempotency_key) +); + +CREATE INDEX IF NOT EXISTS idx_idempotency_keys_expires_at ON idempotency_keys (expires_at); diff --git a/migrations/003_attachments.sql b/migrations/003_attachments.sql new file mode 100644 index 00000000..ddb96d57 --- /dev/null +++ b/migrations/003_attachments.sql @@ -0,0 +1,35 @@ +-- Migration 003: Attachments & Message-Attachment links for Lab 4 (S3 metadata in PostgreSQL) + +ALTER TABLE messages DROP CONSTRAINT IF EXISTS chk_messages_text; +ALTER TABLE messages + ADD CONSTRAINT chk_messages_text CHECK (char_length(text) <= 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 VARCHAR(255) NOT NULL, + content_type VARCHAR(32) NOT NULL + CONSTRAINT chk_attachments_content_type CHECK (content_type IN ('image/png', 'image/jpeg')), + size_bytes BIGINT NOT NULL + CONSTRAINT chk_attachments_size_bytes CHECK (size_bytes >= 1 AND size_bytes <= 10485760), + sha256 CHAR(64) NOT NULL, + object_key TEXT NOT NULL UNIQUE, + status VARCHAR(16) NOT NULL DEFAULT 'ready' + CONSTRAINT chk_attachments_status CHECK (status IN ('pending', 'ready', 'failed')), + error_code VARCHAR(64), + created_at TIMESTAMPTZ NOT NULL, + updated_at TIMESTAMPTZ NOT NULL +); + +CREATE INDEX IF NOT EXISTS idx_attachments_chat_id ON attachments (chat_id); +CREATE INDEX IF NOT EXISTS idx_attachments_status_created 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, + ord INT NOT NULL DEFAULT 0, + PRIMARY KEY (message_id, attachment_id) +); + +CREATE INDEX IF NOT EXISTS idx_message_attachments_att_id ON message_attachments (attachment_id); diff --git a/scripts/restart.sh b/scripts/restart.sh new file mode 100755 index 00000000..2791ac1a --- /dev/null +++ b/scripts/restart.sh @@ -0,0 +1,8 @@ +#!/bin/sh +set -eu + +SCRIPT_DIR=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd) +ROOT_DIR=$(CDPATH= cd -- "$SCRIPT_DIR/.." && pwd) +cd "$ROOT_DIR" + +docker compose --env-file .env restart postgres redis s3 api lb >/dev/null diff --git a/scripts/seed.py b/scripts/seed.py new file mode 100644 index 00000000..e93e7a57 --- /dev/null +++ b/scripts/seed.py @@ -0,0 +1,90 @@ +#!/usr/bin/env python3 +"""Helper script to populate synthetic demo data (including image attachment) for Lab 4 Swagger UI.""" +import base64 +import json +from pathlib import Path +import sys +import urllib.request +import uuid + +BASE_URL = sys.argv[1] if len(sys.argv) > 1 else "http://localhost:8080" +FIXTURE = Path(__file__).resolve().parents[1] / "tests" / "fixtures" / "sample.png" + + +def req(method, path, body=None, raw=None, headers=None): + h = {"Accept": "application/json"} + data = raw + if body is not None: + data = json.dumps(body).encode("utf-8") + h["Content-Type"] = "application/json" + if headers: + h.update(headers) + r = urllib.request.Request(BASE_URL + path, data=data, headers=h, method=method) + with urllib.request.urlopen(r, timeout=15) as resp: + payload = resp.read() + inst = resp.headers.get("X-Instance-Id", "") + return (json.loads(payload.decode("utf-8")) if payload else None), inst + + +def main(): + suffix = uuid.uuid4().hex[:6] + alice_username = f"alice_{suffix}" + bob_username = f"bob_{suffix}" + password = "DemoPassword-2026!" + + alice, inst1 = req("POST", "/api/v1/users", { + "username": alice_username, + "display_name": "Alice Zamorskaya", + "password": password, + }) + bob, inst2 = req("POST", "/api/v1/users", { + "username": bob_username, + "display_name": "Bob Bobrov", + "password": password, + }) + + alice_basic = base64.b64encode(f"{alice_username}:{password}".encode("utf-8")).decode("ascii") + bob_basic = base64.b64encode(f"{bob_username}:{password}".encode("utf-8")).decode("ascii") + + alice_sess, _ = req("POST", "/api/v1/auth/sessions", headers={"Authorization": f"Basic {alice_basic}"}) + bob_sess, _ = req("POST", "/api/v1/auth/sessions", headers={"Authorization": f"Basic {bob_basic}"}) + + alice_bearer = {"Authorization": f"Bearer {alice_sess['token']}"} + bob_bearer = {"Authorization": f"Bearer {bob_sess['token']}"} + + chat, _ = req("POST", "/api/v1/chats", { + "title": "Lab 4 Attachments Chat", + "member_ids": [alice["id"], bob["id"]], + }, headers=alice_bearer) + + img_bytes = FIXTURE.read_bytes() + att, inst3 = req( + "POST", + f"/api/v1/chats/{chat['id']}/attachments?filename=sample.png", + raw=img_bytes, + headers={**alice_bearer, "Content-Type": "image/png"}, + ) + + _, inst4 = req("POST", f"/api/v1/chats/{chat['id']}/messages", { + "text": "Photo stored in private MinIO S3!", + "attachment_ids": [att["id"]], + }, headers={**alice_bearer, "Idempotency-Key": f"seed-att-{suffix}"}) + + _, inst5 = req("POST", f"/api/v1/chats/{chat['id']}/messages", { + "text": "Got it via another API replica!", + }, headers={**bob_bearer, "Idempotency-Key": f"seed-reply-{suffix}"}) + + print("=== LAB 4 DEMO DATA CREATED ===") + print(f"Swagger UI: {BASE_URL}/docs") + print(f"Observed IDs: {sorted({x for x in [inst1, inst2, inst3, inst4, inst5] if x})}") + print(f"Password: {password}") + print(f"Alice Username: {alice_username}") + print(f"Alice Token: {alice_sess['token']}") + print(f"Bob Username: {bob_username}") + print(f"Bob Token: {bob_sess['token']}") + print(f"Chat ID: {chat['id']}") + print(f"Attachment ID: {att['id']}") + + +if __name__ == "__main__": + main() diff --git a/scripts/stop-one.sh b/scripts/stop-one.sh new file mode 100755 index 00000000..16db8ae4 --- /dev/null +++ b/scripts/stop-one.sh @@ -0,0 +1,25 @@ +#!/bin/sh +set -eu + +: "${TARGET_INSTANCE_ID:?test provides target API instance alias}" + +SCRIPT_DIR=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd) +ROOT_DIR=$(CDPATH= cd -- "$SCRIPT_DIR/.." && pwd) +cd "$ROOT_DIR" + +TARGET_CID="" +for cid in $(docker compose --env-file .env ps -q api); do + short_id=$(printf '%s' "$cid" | cut -c1-12) + hostname_val=$(docker inspect -f '{{.Config.Hostname}}' "$cid" 2>/dev/null || true) + if [ "$short_id" = "$TARGET_INSTANCE_ID" ] || [ "$cid" = "$TARGET_INSTANCE_ID" ] || [ "$hostname_val" = "$TARGET_INSTANCE_ID" ]; then + TARGET_CID="$cid" + break + fi +done + +if [ -z "$TARGET_CID" ]; then + printf 'API container matching X-Instance-Id=%s not found\n' "$TARGET_INSTANCE_ID" >&2 + exit 1 +fi + +docker stop -t 5 "$TARGET_CID" >/dev/null diff --git a/student-tests/test_grade4.py b/student-tests/test_grade4.py new file mode 100644 index 00000000..93626def --- /dev/null +++ b/student-tests/test_grade4.py @@ -0,0 +1,446 @@ +#!/usr/bin/env python3 +"""Grade 4 verification suite for Lab 4: +1. Validation & security negative tests (413 bytes, 413 >40M pixels bomb, 415 corrupted PNG/JPEG, 415 MIME spoof, 400 bad filenames, 404 cross-chat/foreign). +2. Cross-replica attachment download after stopping the uploading API replica + zero leftover temp files on API. +3. Private bucket & MinIO console isolation + least-privilege credentials + idempotent s3-init. +4. S3 unavailability graceful 503 handling (no 500). +5. >=10 concurrent streaming multi-MB uploads with API peak memory measurement. +""" +import base64 +from concurrent.futures import ThreadPoolExecutor +import datetime as dt +import hashlib +import json +from pathlib import Path +import struct +import subprocess +import sys +import time +import urllib.error +import urllib.request +import uuid +import zlib + +ROOT = Path(__file__).resolve().parents[1] +FIXTURE_PNG = ROOT / "tests" / "fixtures" / "sample.png" +EVIDENCE_PATH = ROOT / "evidence" / "grade4_checks.json" +BASE_URL = sys.argv[1] if len(sys.argv) > 1 else "http://localhost:8080" + + +def run_cmd(cmd, check=True): + return subprocess.run(cmd, cwd=str(ROOT), text=True, capture_output=True, check=check) + + +def http_call(method, path, body=None, raw_bytes=None, headers=None, timeout=15): + h = {"Accept": "application/json"} + data = raw_bytes + if body is not None: + data = json.dumps(body).encode("utf-8") + h["Content-Type"] = "application/json" + if headers: + h.update(headers) + req = urllib.request.Request(BASE_URL + path, data=data, headers=h, method=method) + start = time.monotonic() + try: + with urllib.request.urlopen(req, timeout=timeout) as resp: + raw = resp.read() + hdrs = {k.lower(): v for k, v in resp.headers.items()} + ct = hdrs.get("content-type", "") + parsed = json.loads(raw.decode("utf-8")) if (raw and "application/json" in ct) else None + return resp.code, hdrs, parsed, raw, time.monotonic() - start + except urllib.error.HTTPError as err: + raw = err.read() + hdrs = {k.lower(): v for k, v in err.headers.items()} + ct = hdrs.get("content-type", "") + parsed = json.loads(raw.decode("utf-8")) if (raw and "application/json" in ct) else None + return err.code, hdrs, parsed, raw, time.monotonic() - start + + +def wait_ready(timeout_s=45): + deadline = time.monotonic() + timeout_s + while time.monotonic() < deadline: + try: + st, _, _, _, _ = http_call("GET", "/health/ready", timeout=2) + if st == 200: + return + except Exception: + pass + time.sleep(0.5) + raise RuntimeError("LB/API did not become ready within timeout") + + +def register_and_login(label): + username = f"g4_{label}_{uuid.uuid4().hex[:8]}" + password = f"Grade4Pass-{uuid.uuid4().hex}" + st, _, user, _, _ = http_call("POST", "/api/v1/users", { + "username": username, + "display_name": f"Student {label}", + "password": password, + }) + assert st == 201 + basic = base64.b64encode(f"{username}:{password}".encode("utf-8")).decode("ascii") + st, _, sess, _, _ = http_call("POST", "/api/v1/auth/sessions", headers={"Authorization": f"Basic {basic}"}) + assert st == 201 + return {"id": user["id"], "headers": {"Authorization": f"Bearer {sess['token']}"}} + + +def png_chunk(chunk_type, data): + return struct.pack(">I", len(data)) + chunk_type + data + struct.pack(">I", zlib.crc32(chunk_type + data) & 0xFFFFFFFF) + + +def make_synthetic_png(width, height, compress_level=6, color=2): + # color=2 -> RGB (3 bytes/px) + ihdr = struct.pack(">IIBBBBB", width, height, 8, color, 0, 0, 0) + row = b"\x00" + (b"\x2a\x7f\xc1" * width) + raw = row * height + compressed = zlib.compress(raw, level=compress_level) + return b"\x89PNG\r\n\x1a\n" + png_chunk(b"IHDR", ihdr) + png_chunk(b"IDAT", compressed) + png_chunk(b"IEND", b"") + + +def make_pixel_bomb_png(width=7000, height=7000): + # Valid IHDR declaring 49M pixels (> 40M limit) with tiny valid IDAT so DecodeConfig catches it in <4KB RAM + ihdr = struct.pack(">IIBBBBB", width, height, 8, 0, 0, 0, 0) + tiny_idat = zlib.compress(b"\x00\x00", level=1) + return b"\x89PNG\r\n\x1a\n" + png_chunk(b"IHDR", ihdr) + png_chunk(b"IDAT", tiny_idat) + png_chunk(b"IEND", b"") + + +def get_api_containers(): + cids = run_cmd(["docker", "compose", "--env-file", ".env", "ps", "-q", "api"]).stdout.strip().splitlines() + out = [] + for cid in cids: + cid = cid.strip() + if not cid: + continue + info = json.loads(run_cmd(["docker", "inspect", cid]).stdout)[0] + out.append({ + "id": cid, + "short_id": cid[:12], + "name": info["Name"].lstrip("/"), + "env": info["Config"]["Env"], + "ports": info["NetworkSettings"]["Ports"], + }) + return out + + +def wait_two_instances(headers, timeout_s=30): + deadline = time.monotonic() + timeout_s + seen = set() + while time.monotonic() < deadline: + try: + st, hdrs, _, _, _ = http_call("GET", "/api/v1/users/me", headers=headers, timeout=2) + if st == 200 and hdrs.get("x-instance-id"): + seen.add(hdrs["x-instance-id"]) + if len(seen) >= 2: + return sorted(seen) + except Exception: + pass + time.sleep(0.2) + raise RuntimeError(f"Expected >=2 active instances, saw {seen}") + + +def test_negative_validation(): + alice = register_and_login("neg_a") + bob = register_and_login("neg_b") + eve = register_and_login("neg_e") + + st, _, chat1, _, _ = http_call("POST", "/api/v1/chats", { + "title": "Chat 1", + "member_ids": [alice["id"], bob["id"]], + }, headers=alice["headers"]) + assert st == 201 + st, _, chat2, _, _ = http_call("POST", "/api/v1/chats", { + "title": "Chat 2", + "member_ids": [alice["id"]], + }, headers=alice["headers"]) + assert st == 201 + + sample = FIXTURE_PNG.read_bytes() + + # 1. Oversized bytes (> 10 MiB) -> 413 + oversized = b"\x89PNG\r\n\x1a\n" + (b"\x00" * (10 * 1024 * 1024 + 128)) + st, _, err, _, _ = http_call("POST", f"/api/v1/chats/{chat1['id']}/attachments?filename=big.png", raw_bytes=oversized, headers={**alice["headers"], "Content-Type": "image/png"}) + assert st == 413 and err["error"]["code"] == "payload_too_large", f"Expected 413 for >10MiB, got {st}" + + # 2. Pixel bomb (> 40M pixels) -> 413 + bomb = make_pixel_bomb_png(7000, 7000) + st, _, err, _, _ = http_call("POST", f"/api/v1/chats/{chat1['id']}/attachments?filename=bomb.png", raw_bytes=bomb, headers={**alice["headers"], "Content-Type": "image/png"}) + assert st == 413 and err["error"]["code"] == "payload_too_large", f"Expected 413 for pixel bomb, got {st}" + + # 3. Corrupted PNG (truncated before IEND) -> 415 + corrupt_png = sample[:len(sample) // 2] + st, _, err, _, _ = http_call("POST", f"/api/v1/chats/{chat1['id']}/attachments?filename=bad.png", raw_bytes=corrupt_png, headers={**alice["headers"], "Content-Type": "image/png"}) + assert st == 415 and err["error"]["code"] == "unsupported_media_type", f"Expected 415 for corrupt PNG, got {st}" + + # 4. Corrupted JPEG -> 415 + corrupt_jpg = b"\xff\xd8\xff\xe0\x00\x10JFIF\x00garbage-truncated" + st, _, err, _, _ = http_call("POST", f"/api/v1/chats/{chat1['id']}/attachments?filename=bad.jpg", raw_bytes=corrupt_jpg, headers={**alice["headers"], "Content-Type": "image/jpeg"}) + assert st == 415 and err["error"]["code"] == "unsupported_media_type", f"Expected 415 for corrupt JPEG, got {st}" + + # 5. MIME spoof (HTML/text labeled as image/png + PNG bytes labeled as image/jpeg) -> 415 + st, _, _, _, _ = http_call("POST", f"/api/v1/chats/{chat1['id']}/attachments?filename=fake.png", raw_bytes=b"not-an-image", headers={**alice["headers"], "Content-Type": "image/png"}) + assert st == 415 + st, _, _, _, _ = http_call("POST", f"/api/v1/chats/{chat1['id']}/attachments?filename=wrong.jpg", raw_bytes=sample, headers={**alice["headers"], "Content-Type": "image/jpeg"}) + assert st == 415 + + # 6. Bad filenames -> 400 + for bad_name in ["../secret.png", "dir/file.png", "dir\\file.png", " "]: + from urllib.parse import quote + st, _, _, _, _ = http_call("POST", f"/api/v1/chats/{chat1['id']}/attachments?filename={quote(bad_name, safe='')}", raw_bytes=sample, headers={**alice["headers"], "Content-Type": "image/png"}) + assert st == 400, f"Expected 400 for filename {bad_name!r}, got {st}" + + # 7. Upload valid attachment in chat1, then test outsider & cross-chat attachment isolation + st, _, att1, _, _ = http_call("POST", f"/api/v1/chats/{chat1['id']}/attachments?filename=valid.png", raw_bytes=sample, headers={**alice["headers"], "Content-Type": "image/png"}) + assert st == 201 + + st, _, _, _, _ = http_call("GET", f"/api/v1/attachments/{att1['id']}", headers=eve["headers"]) + assert st == 404 + st, _, _, _, _ = http_call("GET", f"/api/v1/attachments/{att1['id']}/content", headers=eve["headers"]) + assert st == 404 + st, _, _, _, _ = http_call("POST", f"/api/v1/chats/{chat2['id']}/messages", { + "text": "stealing attachment from chat1", + "attachment_ids": [att1["id"]], + }, headers=alice["headers"], timeout=5) + # Must add Idempotency-Key! + st, _, _, _, _ = http_call("POST", f"/api/v1/chats/{chat2['id']}/messages", { + "text": "stealing attachment from chat1", + "attachment_ids": [att1["id"]], + }, headers={**alice["headers"], "Idempotency-Key": uuid.uuid4().hex}) + assert st == 404, f"Cross-chat attachment must return 404, got {st}" + + # Unknown attachment UUID -> 404 + st, _, _, _, _ = http_call("POST", f"/api/v1/chats/{chat1['id']}/messages", { + "text": "missing att", + "attachment_ids": [str(uuid.uuid4())], + }, headers={**alice["headers"], "Idempotency-Key": uuid.uuid4().hex}) + assert st == 404 + + return { + "status": "PASS", + "oversized_10mib_status": 413, + "pixel_bomb_49mp_status": 413, + "corrupted_png_status": 415, + "corrupted_jpg_status": 415, + "mime_spoof_status": 415, + "bad_filename_status": 400, + "outsider_and_cross_chat_status": 404, + "sample_attachment": att1, + } + + +def test_cross_replica_and_no_local_files(att): + bob = register_and_login("cross_b") + # Actually use a shared chat between uploader and downloader + alice = register_and_login("cross_a") + st, _, chat, _, _ = http_call("POST", "/api/v1/chats", { + "title": "Cross Replica Chat", + "member_ids": [alice["id"], bob["id"]], + }, headers=alice["headers"]) + assert st == 201 + wait_two_instances(alice["headers"]) + + sample = FIXTURE_PNG.read_bytes() + expected_sha = hashlib.sha256(sample).hexdigest() + st, hdrs, uploaded, _, _ = http_call( + "POST", + f"/api/v1/chats/{chat['id']}/attachments?filename=cross.png", + raw_bytes=sample, + headers={**alice["headers"], "Content-Type": "image/png"}, + ) + assert st == 201 + uploader_inst = hdrs["x-instance-id"] + + containers = get_api_containers() + uploader_c = next(c for c in containers if c["short_id"] == uploader_inst) + other_c = next(c for c in containers if c["short_id"] != uploader_inst) + + # Confirm NO leftover temp files on API containers + leftover = run_cmd(["docker", "exec", uploader_c["id"], "find", "/tmp", "-name", "lab4-upload-*"]).stdout.strip() + assert leftover == "", f"Leftover temp files found on API: {leftover}" + + # Stop the uploading API replica, then download via remaining replica! + run_cmd(["docker", "stop", "-t", "5", uploader_c["id"]]) + try: + time.sleep(2.2) + st, dl_hdrs, _, dl_bytes, _ = http_call("GET", f"/api/v1/attachments/{uploaded['id']}/content", headers=bob["headers"]) + assert st == 200, f"Download after stopping uploader failed: {st}" + assert dl_hdrs["x-instance-id"] == other_c["short_id"] + assert hashlib.sha256(dl_bytes).hexdigest() == expected_sha + assert dl_bytes == sample + finally: + run_cmd(["docker", "start", uploader_c["id"]]) + wait_two_instances(alice["headers"]) + + return { + "status": "PASS", + "uploaded_by": uploader_inst, + "downloaded_from_after_stop": other_c["short_id"], + "sha256_verified": True, + "api_temp_files_remaining": 0, + "object_key_internal": f"attachments/{chat['id']}/{uploaded['id']}", + } + + +def test_private_bucket_and_least_privilege(object_key): + containers = get_api_containers() + # Verify NO API container exposes ports and NO MinIO root credentials exist in API env + for c in containers: + for p, binds in (c["ports"] or {}).items(): + assert not binds, f"API exposes host port {p}: {binds}" + env_map = dict(item.split("=", 1) for item in c["env"] if "=" in item) + assert "MINIO_ROOT_USER" not in env_map and "MINIO_ROOT_PASSWORD" not in env_map, "API must not receive MinIO root creds" + assert env_map.get("S3_ACCESS_KEY_ID") == "messenger_api_key" + + s3_info = json.loads(run_cmd(["docker", "inspect", run_cmd(["docker", "compose", "--env-file", ".env", "ps", "-q", "s3"]).stdout.strip()]).stdout)[0] + for p, binds in (s3_info["NetworkSettings"]["Ports"] or {}).items(): + assert not binds, f"S3/console must not expose ports to host, got {p}: {binds}" + + # Direct unauthenticated HTTP GET to private S3 bucket inside network -> must return 403 + unauth = run_cmd( + ["docker", "exec", containers[0]["id"], "wget", "-q", "-S", "-O", "/dev/null", f"http://s3:9000/messenger-attachments/{object_key}"], + check=False, + ) + combined = (unauth.stdout + unauth.stderr) + assert unauth.returncode != 0 and ("403" in combined or "Forbidden" in combined), f"Expected 403 from private bucket, got: {combined}" + + # Verify idempotent re-run of s3-init exits 0 cleanly + run_cmd(["docker", "compose", "--env-file", ".env", "up", "--exit-code-from", "s3-init", "s3-init"]) + + return { + "status": "PASS", + "s3_exposed_host_ports": 0, + "minio_console_disabled": True, + "api_uses_scoped_credentials": "messenger_api_key", + "direct_unauth_s3_get": "403 Forbidden", + "s3_init_idempotent_rerun": 0, + } + + +def test_s3_outage_handling(att_id, headers): + s3_cid = run_cmd(["docker", "compose", "--env-file", ".env", "ps", "-q", "s3"]).stdout.strip() + sample = FIXTURE_PNG.read_bytes() + alice = register_and_login("s3out") + st, _, chat, _, _ = http_call("POST", "/api/v1/chats", {"title": "Outage", "member_ids": [alice["id"]]}, headers=alice["headers"]) + assert st == 201 + st, _, ok_att, _, _ = http_call("POST", f"/api/v1/chats/{chat['id']}/attachments?filename=ok.png", raw_bytes=sample, headers={**alice["headers"], "Content-Type": "image/png"}) + assert st == 201 + + run_cmd(["docker", "stop", "-t", "5", s3_cid]) + try: + # HAProxy /health/ready will notice 503 too, so query API directly or via LB expecting 503 (never 500!) + c0 = get_api_containers()[0] + direct_ready = run_cmd(["docker", "exec", c0["id"], "wget", "-q", "-S", "-O", "/dev/null", "http://127.0.0.1:8080/health/ready"], check=False).stderr + assert "503" in direct_ready, f"Expected 503 on /health/ready during S3 outage, got {direct_ready}" + + # Test upload/download during S3 outage via LB -> must return 503 (never 500) + st_up, _, err_up, _, _ = http_call("POST", f"/api/v1/chats/{chat['id']}/attachments?filename=down.png", raw_bytes=sample, headers={**alice["headers"], "Content-Type": "image/png"}) + assert st_up == 503 and (err_up is None or err_up["error"]["code"] == "unavailable"), f"Expected 503 on upload during S3 outage, got {st_up}" + + st_dl, _, err_dl, _, _ = http_call("GET", f"/api/v1/attachments/{ok_att['id']}/content", headers=alice["headers"]) + assert st_dl == 503 and (err_dl is None or err_dl["error"]["code"] == "unavailable"), f"Expected 503 on download during S3 outage, got {st_dl}" + finally: + run_cmd(["docker", "start", s3_cid]) + wait_ready(45) + + return {"status": "PASS", "ready_during_s3_outage": 503, "upload_during_s3_outage": 503, "download_during_s3_outage": 503} + + +def test_concurrent_streaming_memory(): + alice = register_and_login("conc") + st, _, chat, _, _ = http_call("POST", "/api/v1/chats", {"title": "Concurrent Uploads", "member_ids": [alice["id"]]}, headers=alice["headers"]) + assert st == 201 + + # Valid ~4.6 MiB uncompressed PNG (1400x1100 RGB = 1.54M pixels <= 40M limit) + big_png = make_synthetic_png(1400, 1100, compress_level=0) + expected_sha = hashlib.sha256(big_png).hexdigest() + size_mb = round(len(big_png) / (1024 * 1024), 2) + + containers = get_api_containers() + baseline_stats = {} + for line in run_cmd(["docker", "stats", "--no-stream", "--format", "{{.Name}}\t{{.MemUsage}}"]).stdout.strip().splitlines(): + n, m = line.split("\t") + if n.startswith("lab4-api"): + baseline_stats[n] = m.strip() + + peak_stats = {} + + def upload_one(idx): + return http_call( + "POST", + f"/api/v1/chats/{chat['id']}/attachments?filename=big_{idx}.png", + raw_bytes=big_png, + headers={**alice["headers"], "Content-Type": "image/png"}, + timeout=30, + ) + + def poll_stats(): + time.sleep(0.5) + for line in run_cmd(["docker", "stats", "--no-stream", "--format", "{{.Name}}\t{{.MemUsage}}\t{{.CPUPerc}}"]).stdout.strip().splitlines(): + parts = line.split("\t") + if len(parts) == 3 and parts[0].startswith("lab4-"): + peak_stats[parts[0]] = {"mem": parts[1].strip(), "cpu": parts[2].strip()} + + t0 = time.monotonic() + with ThreadPoolExecutor(max_workers=13) as pool: + stat_fut = pool.submit(poll_stats) + up_futs = [pool.submit(upload_one, i) for i in range(12)] + stat_fut.result() + results = [f.result() for f in up_futs] + elapsed = round(time.monotonic() - t0, 3) + + for st_c, _, att, _, _ in results: + assert st_c == 201, f"Concurrent upload failed with {st_c}" + assert att["size_bytes"] == len(big_png) + assert att["sha256"] == expected_sha + + for c in containers: + leftover = run_cmd(["docker", "exec", c["id"], "find", "/tmp", "-name", "lab4-upload-*"]).stdout.strip() + assert leftover == "", f"Leftover temp file on {c['name']}: {leftover}" + + return { + "status": "PASS", + "concurrent_uploads": 12, + "file_size_bytes": len(big_png), + "file_size_mib": size_mb, + "total_payload_mib": round(12 * len(big_png) / (1024 * 1024), 2), + "elapsed_seconds": elapsed, + "api_memory_baseline": baseline_stats, + "stats_during_concurrent_upload": peak_stats, + "api_temp_files_after": 0, + } + + +def main(): + wait_ready() + report = { + "timestamp_utc": dt.datetime.now(dt.timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ"), + "base_url": BASE_URL, + "checks": {}, + } + + neg = test_negative_validation() + report["checks"]["negative_validation"] = {k: v for k, v in neg.items() if k != "sample_attachment"} + print("PASS: negative_validation", flush=True) + + cross = test_cross_replica_and_no_local_files(neg["sample_attachment"]) + report["checks"]["cross_replica_failover_download"] = cross + print("PASS: cross_replica_failover_download", flush=True) + + priv = test_private_bucket_and_least_privilege(cross["object_key_internal"]) + report["checks"]["private_bucket_and_iam"] = priv + print("PASS: private_bucket_and_iam", flush=True) + + outage = test_s3_outage_handling(neg["sample_attachment"]["id"], {}) + report["checks"]["s3_outage_handling"] = outage + print("PASS: s3_outage_handling", flush=True) + + conc = test_concurrent_streaming_memory() + report["checks"]["concurrent_streaming_memory"] = conc + print("PASS: concurrent_streaming_memory", flush=True) + + EVIDENCE_PATH.parent.mkdir(parents=True, exist_ok=True) + EVIDENCE_PATH.write_text(json.dumps(report, indent=2) + "\n", encoding="utf-8") + print(f"PASS: all Grade 4 checks succeeded; evidence saved to {EVIDENCE_PATH}") + + +if __name__ == "__main__": + main() diff --git a/student-tests/test_track2_reconciler.py b/student-tests/test_track2_reconciler.py new file mode 100644 index 00000000..6e99ba43 --- /dev/null +++ b/student-tests/test_track2_reconciler.py @@ -0,0 +1,234 @@ +#!/usr/bin/env python3 +"""Grade 5 Track 2: Two-Store (PostgreSQL <-> S3) Reconciler experiment for Lab 4. +1. Seeds >=25 healthy active attachments (some referenced by messages). +2. Injects faults on BOTH sides: + - 3 aged orphan S3 objects (> grace) without DB rows + - 1 aged missing-S3 'ready' attachment (referenced in message; S3 object deleted out-of-band) + - 1 aged missing-S3 'pending' attachment in DB (never committed to S3) + - 1 fresh orphan S3 object INSIDE grace period (simulating in-flight upload) +3. Runs Reconciler in --dry-run (proves 0 mutations, detects exact anomalies, skips fresh grace). +4. Runs Reconciler in --apply (deletes aged orphans & stale pending row, marks missing ready attachment as failed, preserves fresh grace & all >=25 healthy objects). +5. Runs Reconciler a second time (proves strict idempotency: 0 mutations, 0 anomalies). +6. Verifies all >=25 healthy attachments remain downloadable with exact SHA-256 and captures traversal cost. +""" +import base64 +import datetime as dt +import hashlib +import json +from pathlib import Path +import subprocess +import sys +import time +import urllib.error +import urllib.request +import uuid + +ROOT = Path(__file__).resolve().parents[1] +FIXTURE_PNG = ROOT / "tests" / "fixtures" / "sample.png" +EVIDENCE_PATH = ROOT / "evidence" / "track2_reconciler.json" +BASE_URL = sys.argv[1] if len(sys.argv) > 1 else "http://localhost:8080" + + +def run_cmd(cmd, input_text=None): + return subprocess.run(cmd, cwd=str(ROOT), input=input_text, text=True, capture_output=True, check=True) + + +def http_call(method, path, body=None, raw_bytes=None, headers=None, timeout=15): + h = {"Accept": "application/json"} + data = raw_bytes + if body is not None: + data = json.dumps(body).encode("utf-8") + h["Content-Type"] = "application/json" + if headers: + h.update(headers) + req = urllib.request.Request(BASE_URL + path, data=data, headers=h, method=method) + try: + with urllib.request.urlopen(req, timeout=timeout) as resp: + raw = resp.read() + ct = resp.headers.get("Content-Type", "") + parsed = json.loads(raw.decode("utf-8")) if (raw and "application/json" in ct) else None + return resp.code, parsed, raw + except urllib.error.HTTPError as err: + raw = err.read() + ct = err.headers.get("Content-Type", "") + parsed = json.loads(raw.decode("utf-8")) if (raw and "application/json" in ct) else None + return err.code, parsed, raw + + +def register_and_login(): + uname = f"rec_{uuid.uuid4().hex[:8]}" + pw = f"Track2Pass-{uuid.uuid4().hex}" + st, u, _ = http_call("POST", "/api/v1/users", {"username": uname, "display_name": "Reconciler", "password": pw}) + assert st == 201 + basic = base64.b64encode(f"{uname}:{pw}".encode("utf-8")).decode("ascii") + st, sess, _ = http_call("POST", "/api/v1/auth/sessions", headers={"Authorization": f"Basic {basic}"}) + assert st == 201 + return {"id": u["id"], "headers": {"Authorization": f"Bearer {sess['token']}"}} + + +def mc_exec(s3_cid, script_body): + cmd = ["docker", "exec", "-i", s3_cid, "/bin/sh", "-e"] + return run_cmd(cmd, input_text=script_body).stdout + + +def s3_object_exists(s3_cid, obj_key): + res = subprocess.run( + ["docker", "exec", s3_cid, "/bin/sh", "-c", + f"mc alias set local http://127.0.0.1:9000 minio_admin_local minio_admin_secret_lab4 >/dev/null && mc stat local/messenger-attachments/{obj_key} >/dev/null 2>&1"], + cwd=str(ROOT), text=True, capture_output=True, + ) + return res.returncode == 0 + + +def main(): + api_cid = run_cmd(["docker", "compose", "--env-file", ".env", "ps", "-q", "api"]).stdout.strip().splitlines()[0].strip() + pg_cid = run_cmd(["docker", "compose", "--env-file", ".env", "ps", "-q", "postgres"]).stdout.strip() + s3_cid = run_cmd(["docker", "compose", "--env-file", ".env", "ps", "-q", "s3"]).stdout.strip() + + sample = FIXTURE_PNG.read_bytes() + expected_sha = hashlib.sha256(sample).hexdigest() + + actor = register_and_login() + st, chat, _ = http_call("POST", "/api/v1/chats", { + "title": "Track 2 Reconciler Chat", + "member_ids": [actor["id"]], + }, headers=actor["headers"]) + assert st == 201 + + # 1. Upload 25 healthy active attachments (and link first 10 to messages) + healthy = [] + for i in range(25): + st, att, _ = http_call( + "POST", + f"/api/v1/chats/{chat['id']}/attachments?filename=healthy_{i}.png", + raw_bytes=sample, + headers={**actor["headers"], "Content-Type": "image/png"}, + ) + assert st == 201 + healthy.append(att) + if i < 10: + st_m, _, _ = http_call("POST", f"/api/v1/chats/{chat['id']}/messages", { + "text": f"Attached #{i}", + "attachment_ids": [att["id"]], + }, headers={**actor["headers"], "Idempotency-Key": f"rec-msg-{i}-{uuid.uuid4().hex[:8]}"}) + assert st_m == 201 + + # 2. Upload 1 legitimate attachment that will suffer out-of-band S3 object loss (Fault C) + st, victim_att, _ = http_call( + "POST", + f"/api/v1/chats/{chat['id']}/attachments?filename=victim.png", + raw_bytes=sample, + headers={**actor["headers"], "Content-Type": "image/png"}, + ) + assert st == 201 + st_m, _, _ = http_call("POST", f"/api/v1/chats/{chat['id']}/messages", { + "text": "Message referencing S3-lost attachment", + "attachment_ids": [victim_att["id"]], + }, headers={**actor["headers"], "Idempotency-Key": f"rec-victim-{uuid.uuid4().hex[:8]}"}) + assert st_m == 201 + + victim_key = f"attachments/{chat['id']}/{victim_att['id']}" + + # Inject Fault A (3 orphan S3 objects) & remove victim_key out-of-band (Fault C) + orphan_keys = [f"attachments/{chat['id']}/orphan_{uuid.uuid4()}" for _ in range(3)] + mc_script = "mc alias set local http://127.0.0.1:9000 minio_admin_local minio_admin_secret_lab4 >/dev/null\n" + for k in orphan_keys: + mc_script += f"printf 'orphan-bytes-%s' '{k}' | mc pipe local/messenger-attachments/{k} >/dev/null\n" + mc_script += f"mc rm local/messenger-attachments/{victim_key} >/dev/null\n" + mc_exec(s3_cid, mc_script) + + # Inject Fault D: aged 'pending' DB row without S3 object + stale_pending_id = str(uuid.uuid4()) + run_cmd([ + "docker", "exec", "-i", pg_cid, "psql", "-v", "ON_ERROR_STOP=1", "-U", "messenger", "-d", "messenger", + ], input_text=f""" + INSERT INTO attachments (id, chat_id, owner_id, filename, content_type, size_bytes, sha256, object_key, status, error_code, created_at, updated_at) + VALUES ('{stale_pending_id}'::uuid, '{chat['id']}'::uuid, '{actor['id']}'::uuid, 'stale_pending.png', 'image/png', 100, '{expected_sha}', 'attachments/{chat['id']}/{stale_pending_id}', 'pending', NULL, NOW() - INTERVAL '20 seconds', NOW() - INTERVAL '20 seconds'); + """) + + # Wait 7 seconds so Faults A/C/D cross the 5s grace threshold + time.sleep(7.0) + + # Inject Fault B: FRESH orphan S3 object INSIDE grace period (age ~0s < 5s) + fresh_key = f"attachments/{chat['id']}/fresh_{uuid.uuid4()}" + mc_exec(s3_cid, f""" + mc alias set local http://127.0.0.1:9000 minio_admin_local minio_admin_secret_lab4 >/dev/null + printf 'inflight-upload' | mc pipe local/messenger-attachments/{fresh_key} >/dev/null + """) + + # 3. Run Reconciler in --dry-run (grace=5s) + dry_raw = run_cmd([ + "docker", "exec", api_cid, "/app/messenger", "-reconcile-once", "-dry-run", "-grace-seconds=5", + ]).stdout + dry_report = json.loads(dry_raw) + assert dry_report["lock_acquired"] is True + for k in orphan_keys: + assert k in dry_report["orphan_s3_found"], f"Dry-run missed orphan {k}" + assert fresh_key in dry_report["orphan_s3_skipped_grace"], "Dry-run must skip fresh in-flight S3 object inside grace" + assert victim_att["id"] in dry_report["missing_s3_found"] + assert stale_pending_id in dry_report["missing_s3_found"] + assert dry_report["orphan_s3_deleted"] == [] + assert dry_report["missing_s3_marked_failed"] == [] + assert dry_report["missing_s3_deleted"] == [] + assert dry_report["healthy_preserved"] >= 25 + print(f"PASS: dry-run scanned {dry_report['s3_objects_scanned']} S3 objs, {dry_report['db_rows_scanned']} DB rows in {dry_report['duration_ms']}ms") + + # 4. Run Reconciler in --apply (grace=5s) + apply_raw = run_cmd([ + "docker", "exec", api_cid, "/app/messenger", "-reconcile-once", "-grace-seconds=5", + ]).stdout + apply_report = json.loads(apply_raw) + assert set(orphan_keys).issubset(set(apply_report["orphan_s3_deleted"])) + assert fresh_key in apply_report["orphan_s3_skipped_grace"], "Apply must NOT delete fresh object inside grace!" + assert victim_att["id"] in apply_report["missing_s3_marked_failed"] + assert stale_pending_id in apply_report["missing_s3_deleted"] + assert apply_report["healthy_preserved"] >= 25 + + # Verify S3 state: aged orphans gone, fresh orphan STILL present! + for k in orphan_keys: + assert not s3_object_exists(s3_cid, k), f"Aged orphan {k} was not removed" + assert s3_object_exists(s3_cid, fresh_key), "Fresh object inside grace was wrongly deleted!" + + # Verify DB/API state: victim attachment marked failed & download returns 409; stale pending deleted! + st_v, _, _ = http_call("GET", f"/api/v1/attachments/{victim_att['id']}/content", headers=actor["headers"]) + assert st_v == 409, f"Expected 409 on missing_s3_object download, got {st_v}" + + # 5. Re-run Reconciler immediately (repeatability / idempotency check!) + rerun_raw = run_cmd([ + "docker", "exec", api_cid, "/app/messenger", "-reconcile-once", "-grace-seconds=5", + ]).stdout + rerun_report = json.loads(rerun_raw) + assert rerun_report["orphan_s3_deleted"] == [] + assert rerun_report["missing_s3_marked_failed"] == [] + assert rerun_report["missing_s3_deleted"] == [] + assert rerun_report["healthy_preserved"] >= 25 + + # 6. Verify ALL 25 healthy active attachments remain downloadable with exact SHA-256 + for att in healthy: + st_h, _, raw_h = http_call("GET", f"/api/v1/attachments/{att['id']}/content", headers=actor["headers"]) + assert st_h == 200 and hashlib.sha256(raw_h).hexdigest() == expected_sha + + # Cleanup our synthetic fresh_key test artifact in S3 so bucket stays tidy + mc_exec(s3_cid, f"mc alias set local http://127.0.0.1:9000 minio_admin_local minio_admin_secret_lab4 >/dev/null && mc rm local/messenger-attachments/{fresh_key} >/dev/null") + + out = { + "timestamp_utc": dt.datetime.now(dt.timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ"), + "healthy_attachments_verified": len(healthy), + "injected_faults": { + "aged_orphan_s3_objects": len(orphan_keys), + "fresh_orphan_s3_inside_grace": 1, + "missing_s3_ready_referenced": 1, + "missing_s3_pending_stale": 1, + }, + "dry_run_report": dry_report, + "apply_report": apply_report, + "idempotent_rerun_report": rerun_report, + "status": "PASS", + } + EVIDENCE_PATH.parent.mkdir(parents=True, exist_ok=True) + EVIDENCE_PATH.write_text(json.dumps(out, indent=2) + "\n", encoding="utf-8") + print(f"PASS: Track 2 two-store reconciler experiment verified; evidence saved to {EVIDENCE_PATH}") + + +if __name__ == "__main__": + main()