ЛР 4 — БПИ2301 — Антонов — на 4 #61
@@ -16,5 +16,9 @@ backups
|
||||
.venv
|
||||
__pycache__
|
||||
node_modules
|
||||
dist
|
||||
coverage
|
||||
evidence
|
||||
.DS_Store
|
||||
.idea
|
||||
.vscode
|
||||
|
||||
@@ -18,3 +18,5 @@ S3_ACCESS_KEY_ID_FILE=/run/secrets/s3_access_key_id
|
||||
S3_SECRET_ACCESS_KEY_FILE=/run/secrets/s3_secret_access_key
|
||||
MAX_UPLOAD_BYTES=10485760
|
||||
MAX_IMAGE_PIXELS=40000000
|
||||
RECONCILE_GRACE_SECONDS=3600
|
||||
RECONCILE_INTERVAL_SECONDS=300
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
* text=auto eol=lf
|
||||
*.png binary
|
||||
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"useTabs": true,
|
||||
"tabWidth": 4,
|
||||
"singleQuote": true,
|
||||
"trailingComma": "all"
|
||||
}
|
||||
+45
@@ -0,0 +1,45 @@
|
||||
# syntax=docker/dockerfile:1
|
||||
|
||||
FROM node:20.17.0-slim AS base
|
||||
RUN apt-get update \
|
||||
&& apt-get install -y --no-install-recommends openssl \
|
||||
&& rm -rf /var/lib/apt/lists/*
|
||||
WORKDIR /app
|
||||
|
||||
FROM base AS deps
|
||||
COPY package.json package-lock.json ./
|
||||
RUN npm ci
|
||||
|
||||
FROM deps AS build
|
||||
COPY prisma ./prisma
|
||||
RUN npx prisma generate
|
||||
COPY tsconfig.json tsconfig.build.json nest-cli.json ./
|
||||
COPY src ./src
|
||||
RUN npm run build
|
||||
|
||||
FROM build AS test
|
||||
COPY contracts/openapi.json ./contracts/openapi.json
|
||||
COPY tests/fixtures/sample.png ./tests/fixtures/sample.png
|
||||
COPY test ./test
|
||||
CMD ["npm", "test"]
|
||||
|
||||
FROM build AS migrate
|
||||
CMD ["sh", "-c", "DATABASE_URL=\"$(cat \"$DATABASE_URL_FILE\")\" exec npx prisma migrate deploy"]
|
||||
|
||||
FROM build AS prod-deps
|
||||
RUN npm prune --omit=dev
|
||||
|
||||
FROM base AS runtime
|
||||
ENV NODE_ENV=production
|
||||
COPY package.json ./
|
||||
COPY --from=prod-deps /app/node_modules ./node_modules
|
||||
COPY --from=build /app/dist ./dist
|
||||
COPY contracts/openapi.json ./contracts/openapi.json
|
||||
|
||||
USER node
|
||||
EXPOSE 8080
|
||||
|
||||
HEALTHCHECK --interval=5s --timeout=2s --start-period=5s --retries=3 \
|
||||
CMD node -e "require('http').get('http://127.0.0.1:8080/health/core',r=>process.exit(r.statusCode===200?0:1)).on('error',()=>process.exit(1))"
|
||||
|
||||
CMD ["node", "dist/main.js"]
|
||||
@@ -1,37 +1,292 @@
|
||||
# Отчёт — лабораторная 4
|
||||
|
||||
- Автор, группа: TODO
|
||||
- Целевая оценка; выбранный трек 5, если нужен: TODO
|
||||
- Commit/PR: TODO после публикации
|
||||
- Использованные источники, библиотеки и помощь ИИ: TODO
|
||||
- Автор, группа: Антонов Егор, БПИ2301
|
||||
- Целевая оценка; выбранный трек 5, если нужен: **4**, трек 5 не заявляется
|
||||
- Commit/PR: ветка `solution/БПИ2301-Antonov`; ссылка на PR — после публикации
|
||||
- Использованные источники, библиотеки и помощь ИИ: документация S3 API (PutObject, ListObjectsV2, политики), MinIO (`mc`, IAM), HAProxy 3.0 (`http-request set-timeout`, health checks), `sharp`/libvips (`failOn`, `limitInputPixels`, `sequentialRead`), Prisma, Node.js streams. Новые библиотеки: `@aws-sdk/client-s3` 3.700.0, `sharp` 0.33.5; остальные — как в lab3. Перед сдачей просмотрены замечания проверяющих к чужим PR этой лабораторной, и те же сценарии воспроизведены на своём стенде; код из чужих решений не использовался. ИИ-ассистент использовался как парный программист: код, тесты, скрипты экспериментов и текст отчёта по моим решениям с последующим ревью; проверки воспроизведены мной на своём стенде.
|
||||
|
||||
## Запуск из чистого clone
|
||||
|
||||
TODO: версии runtime/образов, prerequisites, команды создания локальных secrets/.env, build, миграций, запуска, ожидания готовности, тестов и остановки. Указать необходимые действия оператора. Не вставлять секреты. Отдельно указать безопасную остановку и команду намеренного удаления учебных данных.
|
||||
На хосте нужен **только Docker** (Engine + Compose v2); python/node/make/sh на хосте не требуются, команды одинаковы в PowerShell и bash. Проверено: Docker 29.3.1, Compose v5.1.1 (внутри `tools` — v2.31.0), Docker Desktop (WSL2), 12 vCPU, 8 GiB RAM. Из свежего `git clone` на commit `f8e41b1` проходят сборка, `make check`, `make test`, `make test-persistence`, `make test-failover`, `check-s3.sh` и собственные тесты.
|
||||
|
||||
| Компонент | Версия |
|
||||
| --- | --- |
|
||||
| API runtime | `node:20.17.0-slim` + openssl, NestJS 10 / Fastify, Prisma 6.1.0, `@aws-sdk/client-s3` 3.700.0, `sharp` 0.33.5 |
|
||||
| Балансировщик | `haproxy:3.0.6-alpine` |
|
||||
| PostgreSQL | `postgres:17.2-alpine` |
|
||||
| Redis | `redis:7.4.1-alpine` |
|
||||
| S3 | `pgsty/minio:RELEASE.2026-08-04T00-00-00Z`, закреплён digest |
|
||||
| Bootstrap S3 | `pgsty/mc:RELEASE.2026-10-26T00-00-00Z`, закреплён digest |
|
||||
| secrets-init | `alpine:3.20.3` |
|
||||
| tools (проверки) | `docker:27.3.1-cli` + python3, make, git |
|
||||
|
||||
```sh
|
||||
# 1. Запуск: secrets-init → postgres, redis, s3 → s3-init, migrate (один раз) → api ×2, reconciler → lb; ждёт healthcheck
|
||||
docker compose up -d --build --wait
|
||||
|
||||
# 2. Публичные проверки внутри контейнера tools (BASE_URL=http://lb:8080)
|
||||
docker compose run --rm --build tools make check
|
||||
docker compose run --rm tools make test
|
||||
docker compose run --rm tools make test-persistence ACTION_SCRIPT=scripts/restart.sh
|
||||
docker compose up -d --wait
|
||||
docker compose run --rm tools make test-failover ACTION_SCRIPT=scripts/stop-one.sh
|
||||
docker compose up -d --wait
|
||||
|
||||
# 3. Проверки вложений и эксперименты уровня 4
|
||||
docker compose run --rm tools sh scripts/check-s3.sh # приватность bucket и права пользователя API
|
||||
docker compose run --rm tools python3 scripts/attachment-scenario.py # защита, отказ реплики, обрыв, отказы БД
|
||||
docker compose run --rm tools sh scripts/s3-outage.sh stop # отказ S3: stop или pause
|
||||
docker compose run --rm tools sh scripts/reconcile-demo.sh # сверка: grace, dry-run, apply
|
||||
docker compose run --rm tools python3 scripts/upload-experiment.py # загрузки и пик памяти
|
||||
|
||||
# Эксперименты lab3 сохранены
|
||||
docker compose run --rm tools python3 scripts/experiment.py idempotency
|
||||
docker compose run --rm tools python3 scripts/experiment.py failover
|
||||
docker compose run --rm tools python3 scripts/experiment.py failover --kill
|
||||
|
||||
# 4. Собственные тесты: отдельные postgres, s3 (tmpfs) и redis
|
||||
docker compose -f compose.test.yaml run --rm --build tests
|
||||
docker compose -f compose.test.yaml down -v
|
||||
|
||||
# 5. Все доказательства в evidence/ одной командой (~15 минут)
|
||||
docker compose run --rm tools sh scripts/collect-evidence.sh
|
||||
|
||||
# Безопасная остановка (данные и секреты сохраняются)
|
||||
docker compose stop # или: docker compose down
|
||||
# Намеренное удаление учебных данных и сгенерированных секретов
|
||||
docker compose down -v
|
||||
```
|
||||
|
||||
- **Обновление работающего стенда.** `docker compose up` не пересоздаёт контейнер при изменении содержимого примонтированного `infra/haproxy/haproxy.cfg`. После изменения конфигурации LB: `docker compose up -d --force-recreate --no-deps lb`. Из чистого clone это не требуется.
|
||||
- **`.env` необязателен**: в `compose.yaml` те же значения по умолчанию. Для своих: `cp .env.example .env`, затем `docker compose --env-file .env up -d --build --wait`.
|
||||
- **Секреты** создаёт one-shot сервис `secrets-init` в именованном volume `secrets`, раздельно по сервисам (`volume.subpath`): пароли PostgreSQL и Redis, root-ключи S3, отдельные ключи S3 для API. Действий оператора не требуется; секреты не попадают в Git, образы, логи и evidence.
|
||||
- **Наружу опубликован только LB** на `127.0.0.1:8080`. API, PostgreSQL, Redis и S3 доступны только во внутренней сети compose.
|
||||
- **Swagger UI**: http://localhost:8080/docs — обязательный контракт `contracts/openapi.json` 1.4.0 без изменений. Служебное расширение описано отдельно: [contracts/extensions.openapi.json](contracts/extensions.openapi.json).
|
||||
|
||||
## Архитектура и решения
|
||||
|
||||
TODO: схема процессов/хранилищ, источник истины, транзакционные границы, ограничения. Что перенесено из lab3, что изменено в этой работе. Соответствие переменных из .env.example реальной конфигурации.
|
||||
```
|
||||
client ──HTTP──▶ lb (HAProxy, 127.0.0.1:8080) ──round robin, без sticky──▶ api ×2 (без опубликованных портов)
|
||||
│ проверка GET /health/core каждые 1 s │
|
||||
│ адреса реплик — Docker DNS ├──SQL──▶ postgres (метаданные) ── volume pgdata
|
||||
├──кэш сессий──▶ redis
|
||||
└──S3 API──▶ s3 (байты, приватный bucket) ── volume s3data
|
||||
▲
|
||||
reconciler (раз в 5 минут): листинг S3 + сверка с PostgreSQL, удаление сирот ───────────────┘
|
||||
s3-init (один раз): bucket, запрет анонимного доступа, политика и пользователь API
|
||||
secrets-init ──▶ volume secrets: postgres/ redis/ s3/ s3-init/ api/ migrate/ reconciler/ (каждому сервису — свой подкаталог, ro)
|
||||
```
|
||||
|
||||
| Роль | Service | Health/readiness | Volume | Сеть/порт | Credentials | Лимиты |
|
||||
| --- | --- | --- | --- | --- | --- | --- |
|
||||
| Балансировщик | `lb` | проверяет backends сам | — | `127.0.0.1:8080` | — | 0.25 CPU, 64M |
|
||||
| API ×2 | `api` | HAProxy и Docker: `/health/core`; контрактный `/health/ready` | — (stateless) | внутренняя, 8080 | `secrets/api`: БД, кэш, ключи S3 пользователя API | 0.5 CPU, 256M каждая |
|
||||
| БД | `postgres` | `pg_isready` | `pgdata` | внутренняя | `secrets/postgres` | 1 CPU, 512M |
|
||||
| Кэш сессий | `redis` | `redis-cli ping` | — | внутренняя | `secrets/redis` | 0.25 CPU, 128M |
|
||||
| S3 | `s3` | `/minio/health/live` | `s3data` | внутренняя, 9000; консоль выключена (`MINIO_BROWSER=off`) | `secrets/s3`: root-ключи | 0.5 CPU, 512M |
|
||||
| Bootstrap S3 | `s3-init` | exit 0 | — | внутренняя | `secrets/s3-init`: root-ключи и ключи API | 0.25 CPU, 256M |
|
||||
| Сверка | `reconciler` | healthcheck отключён, отчёт JSON в лог | — | внутренняя | `secrets/reconciler`: БД, ключи S3 пользователя API | 0.25 CPU, 128M |
|
||||
| Миграции | `migrate` | exit 0 | — | внутренняя | `secrets/migrate` | 0.5 CPU, 512M |
|
||||
| Bootstrap секретов | `secrets-init` | exit 0 | `secrets` | — | генерирует | 0.1 CPU, 32M |
|
||||
|
||||
**Выбор S3.** Выбран MinIO. Официальные образы и бинарные файлы больше не раздаются: `minio/minio` и `quay.io/minio/*` не скачиваются, `dl.min.io` отвечает `410 Gone` с сообщением, что open source проект заархивирован (проверено 2026-11-05). Поэтому используется сторонняя сборка `pgsty/minio` + `pgsty/mc`, версии закреплены тегом и digest. Приложение работает по протоколу S3 и не зависит от конкретного сервера.
|
||||
|
||||
**Bucket и права** (`s3-init` в `compose.yaml`). Запуск идемпотентен: `mc mb --ignore-existing`, `mc anonymous set none`, политика `messenger-api` перезаписывается, пользователь и привязка политики обновляются. Пользователь API получает только `ListBucket`, `GetBucketLocation`, `ListBucketMultipartUploads` на bucket и `GetObject`, `PutObject`, `DeleteObject`, `AbortMultipartUpload`, `ListMultipartUploadParts` на его объекты. Root-ключи есть только у `s3` и `s3-init`.
|
||||
|
||||
**Модель данных** (миграция `20261105000000_attachments`).
|
||||
- `attachments`: чат, владелец, `filename`, `content_type`, `size_bytes`, `sha256`, `object_key`, время. CHECK дублируют проверки API: допустимые MIME, размер 1–10 485 760, формат SHA-256.
|
||||
- `message_attachments`: сообщение, `position` 0–9, вложение. PK `(message_id, position)`, уникальность `(message_id, attachment_id)`. Порядок `attachment_ids` хранится явно, поэтому POST, история и повтор возвращают один и тот же список.
|
||||
- Object key назначает сервер: `attachments/<uuid>`. `filename` используется только для отображения и в заголовке `Content-Disposition`.
|
||||
- Отдельного статуса нет: в lab4 строка существует только у готового вложения. `status: ready`, `variants: []`, `error_code: null` формируются в ответе; состояния появятся миграцией в lab5.
|
||||
|
||||
**Загрузка** (`src/attachments/`). Порядок проверок:
|
||||
1. Токен → доступ к чату → `filename` → `Content-Type` → `Content-Length`. До этого тело не читается: хук `onRequest` на этом маршруте заменяет разбор тела сквозной передачей потока, поэтому аноним всегда получает `401`, посторонний `404`, а не ошибку формата.
|
||||
2. Лимит 16 одновременных загрузок на реплику, сверх него `503`.
|
||||
3. Тело пишется потоком во временный файл с учётом backpressure, одновременно считается SHA-256; при превышении 10 MiB — `413`, дедлайн на приём тела 60 s. Каталог удаляется в `finally`, остатки после аварийного падения — при старте процесса.
|
||||
4. Сигнатура файла и совпадение с `Content-Type`; для PNG — наличие `IEND` в конце файла. Несовпадение → `415`.
|
||||
5. `sharp.metadata()` читает размеры без декодирования: больше 40 млн пикселей → `413`.
|
||||
6. Полное декодирование `sharp(... { failOn: 'warning', sequentialRead: true, limitInputPixels }).stats()`, не более 2 одновременно на реплику. Ошибка → `415`.
|
||||
7. PUT файла в S3 с `ChecksumSHA256`: MinIO сам сверяет контрольную сумму.
|
||||
8. INSERT метаданных и ответ `201`.
|
||||
|
||||
Способ декодирования выбран разовым замером на PNG 8000×5000 при 0.5 CPU (в evidence не сохранён): `stats()` — пик 85 МиБ, 3,3 s; `raw().toBuffer()` — 184 МиБ, 0,1 s; `resize(1,1)` — 459 МиБ, 0,5 s. Выигрыш по памяти важнее скорости при лимите реплики 256 МиБ. Режим `failOn: 'warning'` выбран потому, что `error` и `truncated` пропускают JPEG с испорченными данными скана.
|
||||
|
||||
**Когда возвращается 201.** Только после подтверждения записи объекта в S3 и успешного INSERT. На API не остаётся копии: временный файл удаляется, байты читаются только из S3.
|
||||
|
||||
**Скачивание.** Метаданные и `/content` ищут вложение одним запросом с условием членства в чате, поэтому посторонний и неизвестный ID неразличимы (`404`). Байты передаются потоком через API, без redirect, с истинным `Content-Type`, `Content-Length`, `X-Content-Type-Options: nosniff`. `variant`: отсутствует или `original` — файл; `thumbnail`, `crop`, `optimized` — `404`; прочее, пустое или повторённое — `400`.
|
||||
|
||||
**Идемпотентность с вложениями.** В хеш запроса входит список ID в заданном порядке после приведения к нижнему регистру: отсутствующий `attachment_ids` и `[]` равны, перестановка при том же ключе — `409`. Доступ к чату и принадлежность вложений чату проверяются при каждом запросе, в том числе при повторе.
|
||||
|
||||
**Согласованность S3 и PostgreSQL.** Распределённой транзакции нет. Порядок записи: сначала S3, потом БД, поэтому объект без строки возможен, а видимая строка без объекта при нормальной работе — нет.
|
||||
|
||||
| Сбой | Что происходит | Итог |
|
||||
| --- | --- | --- |
|
||||
| Обрыв клиента или лимит до PUT | до S3 ничего не дошло | временный файл удалён |
|
||||
| Отказ PUT | одиночный PUT с известной длиной, multipart не используется | `503`, строки нет |
|
||||
| Явный отказ БД (известный код Prisma: ограничение, внешний ключ) | запись точно не состоялась | объект удаляется сразу, ответ `500` |
|
||||
| Неясный исход (потеря соединения, необработанная ошибка БД) | COMMIT мог пройти, поэтому API объект **не удаляет** | `503`; повтор создаст новое вложение; решает сверка |
|
||||
| Падение API между PUT и INSERT | объект без строки | решает сверка |
|
||||
| Отказ удаления при сверке | учитывается в `failed` | объект остаётся, следующий проход повторит |
|
||||
|
||||
**Сверка** (`src/attachments/reconciler.ts`, сервис `reconciler`). Каждые 5 минут постранично обходит префикс `attachments/`. Объект считается сиротой, только если он старше grace period (1 час, по `LastModified`) и в момент проверки на него нет строки. Grace намного больше максимального времени загрузки (приём 60 s + PUT 20 s), поэтому загрузка в процессе не считается сиротой. Запуск без `--apply` — dry-run, хранилища не меняются. При отказе PostgreSQL проход прерывается до удалений. Каждый проход пишет JSON-отчёт в лог. Ручной запуск: `docker compose run --rm --no-deps reconciler node dist/reconcile.js [--apply] [--grace-seconds=N]`.
|
||||
|
||||
### Критичные зависимости и проверка готовности
|
||||
|
||||
Контракт требует фиксировать критичные зависимости в отчёте ([contracts/README.md](contracts/README.md)):
|
||||
|
||||
| Endpoint | Что проверяет | Кто использует |
|
||||
| --- | --- | --- |
|
||||
| `/health/live` | жизнь процесса | контракт |
|
||||
| `/health/ready` | остановка процесса, PostgreSQL, S3 (`HeadBucket`, таймаут 1 s) | контракт: клиенты и оператор |
|
||||
| `/health/core` | остановка процесса, PostgreSQL | HAProxy и Docker healthcheck |
|
||||
|
||||
**Почему HAProxy проверяет не `/health/ready`.** S3 один и общий для всех реплик. Если бы LB проверял `/health/ready`, падение S3 одновременно вывело бы из балансировки все исправные реплики: перестали бы работать вход, история и текстовые сообщения, которым S3 не нужен, а клиент получал бы `503` от LB без `X-Instance-Id`. Исключение реплики имеет смысл, когда другая реплика может обслужить запрос; при отказе общего S3 такой реплики нет. Поэтому `/health/ready` честно сообщает о неполной готовности (`503`), а бизнес-запросы продолжают обслуживаться; недоступны только вложения. Это проверено: при остановленном S3 текстовое сообщение получает `201` ([evidence/s3.txt](evidence/s3.txt)).
|
||||
|
||||
**Основание и статус решения.** `/health/core` — дополнение, не описанное в обязательном контракте. Отдельного разрешения именно на служебный health-путь в материалах курса нет; решение опирается на общее правило `COURSE.md`: новые возможности добавляются обратно совместимо, в отдельном `contracts/extensions.openapi.json` или документе. Расширение описано в [contracts/extensions.openapi.json](contracts/extensions.openapi.json), обязательные endpoint не изменены. Требование `backend_readiness` из `contracts/infrastructure.json` выполняется по смыслу: LB активно проверяет готовность каждой реплики и исключает неготовую, что показано сценарием с закрытым доступом к БД ([evidence/failures.txt](evidence/failures.txt) §5). Если проверяющий требует проверки именно через `/health/ready`, возврат — две строки (`infra/haproxy/haproxy.cfg`, `Dockerfile`) ценой остановки всего чата при падении S3.
|
||||
|
||||
**Ограничение решения.** Реплика, потерявшая связь только с S3 при рабочем S3 у других, из LB не исключается и отвечает `503` на запросы вложений. Такой частичный отказ не проверялся.
|
||||
|
||||
Redis не критичен: как и в lab3, вход и бизнес-запросы идут через PostgreSQL, при недоступном Redis отвечает `503` только logout.
|
||||
|
||||
**Таймауты** (согласованы между собой):
|
||||
|
||||
| Параметр | Значение | Где |
|
||||
| --- | --- | --- |
|
||||
| Проверка backend | `inter 1s`, `fall 2`, `rise 2`, `timeout check 1s`, путь `/health/core` | `infra/haproxy/haproxy.cfg` |
|
||||
| Таймауты LB | connect 1 s, server 10 s, client 30 s; для `POST .../attachments` server 60 s | там же |
|
||||
| S3: соединение, простой | 1 s, 5 s; при скачивании 5 s ограничивают только ожидание начала ответа | `src/storage/s3.service.ts` |
|
||||
| S3: вся операция | 20 s | там же |
|
||||
| S3: проверка готовности | 1 s | там же |
|
||||
| Приём тела загрузки | 60 s (`UPLOAD_BODY_TIMEOUT_MS`) | `src/attachments/attachments.service.ts` |
|
||||
| Одновременные загрузки / декодирования | 16 (`MAX_CONCURRENT_UPLOADS`) / 2 на реплику | `attachments.service.ts`, `image-validation.ts` |
|
||||
| Остальное | как в lab3: `SHUTDOWN_DELAY_MS=3000`, `stop_grace_period: 10s`, БД `statement_timeout=5000` | `compose.yaml` |
|
||||
|
||||
HAProxy, в отличие от nginx, не ограничивает размер тела, поэтому отдельной настройки 10 MiB нет: файл ровно 10 MiB проходит через LB, а `413` формирует API в формате контракта с `X-Request-Id` и `X-Instance-Id`.
|
||||
|
||||
**Перенесено из lab3:** весь API, HAProxy, 2 реплики, идемпотентность, graceful shutdown, tombstone при logout, раздельные секреты, `migrate`, тесты, эксперименты. **Изменено:** проверка LB и Docker на `/health/core`; `/health/ready` учитывает S3; `Message` содержит настоящие `attachment_ids`; в хеш идемпотентности входит список вложений; `restart.sh` перезапускает и `s3`; `testTimeout` jest 30 s; `.gitattributes` с LF.
|
||||
|
||||
**Соответствие `.env.example`:**
|
||||
|
||||
| Переменная | Где используется |
|
||||
| --- | --- |
|
||||
| `APP_HOST`, `APP_PORT` | API слушает `0.0.0.0:8080` внутри сети; `APP_PORT` в `.env` — опубликованный порт LB на `127.0.0.1` |
|
||||
| `APP_ENV` | передаётся в API, поведение не меняет |
|
||||
| `SESSION_TTL_SECONDS`, `IDEMPOTENCY_TTL_SECONDS` | сроки жизни, по умолчанию 3600 и 86400 |
|
||||
| `DATABASE_URL_FILE`, `CACHE_URL_FILE` | файлы в `/run/secrets`; в тестах — `DATABASE_URL`, `CACHE_URL` |
|
||||
| `SESSION_STORE=postgres` | фиксировано: источник истины сессий — PostgreSQL |
|
||||
| `API_REPLICAS` | `deploy.replicas`, по умолчанию 2 |
|
||||
| `S3_ENDPOINT`, `S3_REGION`, `S3_BUCKET` | в `compose.yaml` endpoint задан как `http://s3:9000`, остальные настраиваются |
|
||||
| `S3_ACCESS_KEY_ID_FILE`, `S3_SECRET_ACCESS_KEY_FILE` | файлы в `/run/secrets`; в тестах — `S3_ACCESS_KEY_ID`, `S3_SECRET_ACCESS_KEY` |
|
||||
| `MAX_UPLOAD_BYTES`, `MAX_IMAGE_PIXELS` | лимиты загрузки, 10485760 и 40000000 |
|
||||
| `RECONCILE_GRACE_SECONDS`, `RECONCILE_INTERVAL_SECONDS` | параметры сервиса `reconciler`, 3600 и 300 (добавлены к примеру) |
|
||||
|
||||
## Доказательства критериев
|
||||
|
||||
Файлы собраны `docker compose run --rm tools sh scripts/collect-evidence.sh` на commit `f8e41b1` с чистых volumes; в заголовке каждого `.txt` — дата и commit, в `.json` — дата. `tests.txt` и `smoke.txt` обновлены на commit `487da7d`: он только расширяет проверку формата UUID (корректный по формату nil и v7 получают `404`, а не `400`) и добавляет тесты на это; остальные файлы с этим изменением не пересобирались.
|
||||
|
||||
| Критерий README | Команда/сценарий | Наблюдаемый результат | Файл доказательства |
|
||||
| --- | --- | --- | --- |
|
||||
| Публичный smoke | make test | TODO | TODO |
|
||||
| Остальные пункты уровня 3 | TODO | TODO | TODO |
|
||||
| Все пункты уровня 4, если заявлен | TODO | TODO | TODO |
|
||||
| Выбранный трек 5, если заявлен | TODO | TODO | TODO |
|
||||
| Публичный smoke | `make test` через LB | `PASS: lab4 public API scenario` | [evidence/smoke.txt](evidence/smoke.txt) |
|
||||
| Структура контракта | `make check` | `PASS: offline skeleton structure` | [evidence/smoke.txt](evidence/smoke.txt) |
|
||||
| **3**: минимум lab3 сохранён | smoke, `make test-failover`, `experiment.py`, собственные тесты | одна сессия чередует 2 реплики; failover и идемпотентность проходят | [evidence/smoke.txt](evidence/smoke.txt), [evidence/failover.txt](evidence/failover.txt), [evidence/idempotency.json](evidence/idempotency.json) |
|
||||
| **3**: S3 в общем стенде; метаданные в PostgreSQL, байты в приватном bucket | `docker compose ps`, `\d attachments`, анонимный GET | сервис `s3` с volume; в таблице вложений нет поля байт; анонимный GET объекта `403` | [evidence/container.txt](evidence/container.txt), [evidence/attachments.txt](evidence/attachments.txt) §1 |
|
||||
| **3**: POST → `201 ready` после сохранения; скачивание побайтно; Message читается; посторонний без доступа | `make test`; `attachment-scenario.py` §1 | `201 ready`, Bob скачал, SHA-256 совпал, `text=""` с вложением `201`; Eve `404` на метаданные и байты | [evidence/attachments.txt](evidence/attachments.txt) §1 |
|
||||
| **3**: реальные PNG/JPEG ≤10 MiB и ≤40 млн пикселей; MIME по содержимому; опасные filename; лимиты до чтения/декодирования | тесты `attachments.e2e-spec.ts` | ровно 10 MiB и ровно 40 млн пикселей `201`; 10 MiB + 1 и 8001×5000 `413`; `../`, `\`, NUL, DEL, U+0085, пробелы — `400` | [evidence/tests.txt](evidence/tests.txt) |
|
||||
| **3**: остановка обслужившей загрузку реплики → скачивание через другую | `attachment-scenario.py` §2 для обеих реплик | по 6 скачиваний через оставшуюся реплику, SHA-256 совпал, остановленная запросов не получала | [evidence/attachments.txt](evidence/attachments.txt) §2 |
|
||||
| **3**: restart с сохранением volumes сохраняет метаданные и оригинал | `make test-persistence ACTION_SCRIPT=scripts/restart.sh` | PASS с повторным чтением файла; `started=` сменился у postgres, redis, s3, обеих api, lb | [evidence/persistence.txt](evidence/persistence.txt) |
|
||||
| **3**: failover для обеих реплик | `make test-failover` | остановлены разные реплики; 0 ошибок; запись через 3,56–3,58 s; файл прочитан | [evidence/failover.txt](evidence/failover.txt) |
|
||||
| Защита: на API нет обязательного локального оригинала | поиск файла по размеру на оставшейся реплике; временные каталоги | файла нет; временных каталогов 0 | [evidence/attachments.txt](evidence/attachments.txt) §2–3 |
|
||||
| **4**: слишком большой файл, повреждённое изображение, подмена MIME, чужое `attachment_id`, файл другого чата; нет 500 | `attachments.e2e-spec.ts`, `attachment-access.e2e-spec.ts` | `413`; 11 видов битых PNG и 4 вида битых JPEG `415`; подмена в обе стороны `415`; неизвестный и другой чат — одинаковый `404` | [evidence/tests.txt](evidence/tests.txt) |
|
||||
| **4**: прерывание и отказ между S3 и БД не оставляют вечных неучтённых объектов; grace | `attachment-scenario.py` §3–5, `reconcile-demo.sh` | обрыв: 0 новых строк и объектов; реальный отказ БД: `503`, объект защищён grace, затем удалён только он; dry-run ничего не меняет; при повторе удалять нечего | [evidence/attachments.txt](evidence/attachments.txt), [evidence/s3.txt](evidence/s3.txt) |
|
||||
| **4**: лимиты, таймауты, потоковая обработка; ≥10 одновременных загрузок с пиком памяти | `upload-experiment.py` | см. «Эксперименты»; максимум `VmHWM` 163 МиБ при лимите 256 МиБ, OOM нет | [evidence/upload-memory.json](evidence/upload-memory.json) |
|
||||
| **4**: bootstrap идемпотентен | двойной запуск `s3-init` | оба завершились успешно, `bucket ... ready` | [evidence/s3.txt](evidence/s3.txt) |
|
||||
| **4**: отдельные S3 credentials с ограниченными правами | `check-s3.sh` | свой bucket разрешён; другой bucket, создание bucket, смена политики, админ-команды запрещены | [evidence/s3.txt](evidence/s3.txt) |
|
||||
| **4**: консоль не открыта; приватность проверена вне API | `check-s3.sh` | порт S3 не опубликован, `MINIO_BROWSER=off`; анонимные GET объекта, листинг и PUT — `403` | [evidence/s3.txt](evidence/s3.txt) |
|
||||
| Защита: недоступность S3 | `s3-outage.sh stop`, `pause` | `ready`, скачивание и загрузка `503` от API за 1–5 s; метаданные `200`; текст `201`; после восстановления файл цел | [evidence/s3.txt](evidence/s3.txt) |
|
||||
| lab3: LB исключает неготовую реплику; лимиты; секреты | `pg_hba reject` для одной реплики; `docker inspect`; `ls /run/secrets` | `live 200`, `core 503`, `is DOWN`, 10 из 10 запросов через вторую реплику; лимиты у всех контейнеров, включая завершившиеся; у каждого сервиса свои файлы | [evidence/failures.txt](evidence/failures.txt) §5, [evidence/container.txt](evidence/container.txt) |
|
||||
| lab2/3: logout, expiry, отказы Redis и БД | `probe.py` | как в lab3 | [evidence/failures.txt](evidence/failures.txt) |
|
||||
| Собственные тесты | `docker compose -f compose.test.yaml run --rm --build tests` | `Tests: 165 passed, 165 total` | [evidence/tests.txt](evidence/tests.txt) |
|
||||
|
||||
TODO: добавить отдельную строку для каждого заявленного критерия. PASS без команды и наблюдения не является доказательством. Проверка инфраструктурного hook требует подтверждения, какие реальные процессы/хранилища перезапущены.
|
||||
Реальность остановок видна по `docker inspect` в `failover.txt` и `persistence.txt`, а не только по заголовку `X-Instance-Id`.
|
||||
|
||||
## Эксперименты и отказы
|
||||
|
||||
TODO: гипотеза, hardware/лимиты, версии, объём данных, профиль нагрузки, concurrency/arrival rate, длительность/прогрев, повторы, успешные RPS, ошибки, p50/p95/p99, CPU/RAM. Сохранить raw summary без tokens и объяснить ограничения измерения.
|
||||
### Одновременные загрузки и пик памяти
|
||||
|
||||
**Гипотеза.** Потоковая запись тела на диск и ограничение числа декодирований не дают памяти реплики расти пропорционально данным в обработке.
|
||||
|
||||
**Методика.** `scripts/upload-experiment.py` в контейнере `tools`: для каждого профиля реплики API перезапускаются (чтобы `VmHWM` относился к этому профилю), затем 3 раунда по N одновременных POST через LB. Память: `VmRSS` процесса каждые 0,2 s и `VmHWM`. Стенд: 2 реплики по 0.5 CPU и 256 МиБ, Docker Desktop WSL2, 12 vCPU. Объём данных после эксперимента: 290 вложений, 264 сообщения. Прогрева нет: каждый профиль начинается сразу после готовности реплик. Каждый профиль выполнен один раз.
|
||||
|
||||
| Профиль | Запросов | Ответы | Успешных RPS | Доля ошибок | p50 / p95 / p99, s | Простой → пик `VmHWM`, МиБ (макс. реплика) | Данных в обработке на реплику, МиБ | Макс. CPU реплики |
|
||||
| --- | --- | --- | --- | --- | --- | --- | --- | --- |
|
||||
| `sample.png` 112 КиБ × 10 | 30 | 30 × 201 | 14,83 | 0 | 0,49 / 0,71 / 0,81 | 104 → 118 | 0,6 | 44 % |
|
||||
| PNG 10 МиБ × 1 | 3 | 3 × 201 | 1,67 | 0 | 0,69 / 0,74 / 0,74 | 105 → 122 | 10 | 25 % |
|
||||
| PNG 10 МиБ × 10 | 30 | 30 × 201 | 3,13 | 0 | 2,95 / 3,77 / 3,77 | 105 → 144 | 50 | 55 % |
|
||||
| PNG 10 МиБ × 20 | 60 | 60 × 201 | 3,61 | 0 | 4,45 / 6,51 / 6,59 | 106 → 151 | 100 | 55 % |
|
||||
| PNG 10 МиБ × 40 | 120 | 96 × 201, 24 × 503 | 3,90 | 0,20 | 7,86 / 8,37 / 8,40 | 104 → 156 | 160 (лимит 16 загрузок) | 53 % |
|
||||
| PNG 40 МП, 127 КиБ × 10 | 30 | 30 × 201 | 0,34 | 0 | 23,2 / 33,5 / 33,5 | 104 → 163 | 5 файлов, до 2 декодируются | 55 % |
|
||||
| JPEG 40 МП, 235 КиБ × 10 | 30 | 30 × 201 | 0,35 | 0 | 21,6 / 29,5 / 29,8 | 104 → 163 | 5 файлов, до 2 декодируются | 55 % |
|
||||
|
||||
Запросы распределялись между репликами поровну, поэтому на реплику приходится половина одновременных загрузок. Во всех профилях: `oom_killed=false`, 0 временных каталогов после прогона, SHA-256 в ответах и при контрольном скачивании совпал.
|
||||
|
||||
**Вывод.** В этих пределах гипотеза подтверждена: при росте данных в обработке на реплику с 10 до 160 МиБ пик вырос с 122 до 156 МиБ, то есть на 34 МиБ при 150 МиБ дополнительных данных, и остался ниже лимита 256 МиБ. Узкое место — CPU: реплики упираются в лимит 0.5 CPU, а RPS для файлов 10 МиБ почти не растёт от 10 к 40 загрузкам. Отказы при × 40 совпадают с расчётом: по 20 запросов на реплику при лимите 16 дают 4 отказа на реплику за раунд, всего 24.
|
||||
|
||||
**Как читать показатели памяти.** Основной показатель — RSS процесса. `memory.max_usage_in_bytes` cgroup в профилях 40 МП и × 40 достигает 256 МиБ, но по устройству этого счётчика в него входит кэш страниц, в том числе временных файлов; OOM при этом нет. `docker stats` показывает меньше RSS, поэтому не используется как оценка пика. Разброс между запусками: в более ранних прогонах того же сценария (в evidence не сохранены) максимум `VmHWM` доходил до 174 МиБ.
|
||||
|
||||
**Ограничения измерения.** Закрытая модель нагрузки (раунд ждёт завершения всех запросов), клиент на том же хосте, один прогон. Файлы 40 МП — худший случай по декодированию, но они однородные и хорошо сжимаются; изображение одновременно на 10 МиБ и 40 МП не проверялось. При 5 файлах 40 МП на реплику p99 уже около 33 s; при 16 возможно превышение таймаута LB 60 s и `504`.
|
||||
|
||||
### Устойчивость записи (сценарии lab3)
|
||||
|
||||
Методика как в lab3: `scripts/experiment.py`, 4 потока closed-loop `POST` сообщений, неудачный запрос повторяется с тем же ключом, окна 5 s до воздействия, 10 s после команды остановки, 5 s после восстановления. Один прогон каждого сценария. Объём данных: 265 → 17 944 сообщений. Ресурсы под нагрузкой: api 44–48 % CPU и 137–180 МиБ, postgres 26 % и 77 МиБ, lb 11 % и 14 МиБ, s3 2 % и 249 МиБ. Потерь и дублей подтверждённых сообщений 0 из 7011 (`docker stop`), 5365 (`docker kill`), 5303 (rolling).
|
||||
|
||||
| Сценарий, окно | Успешных RPS | Ошибки (доля) | p50 / p95 / p99, ms: до → отказ | Макс. пауза записи |
|
||||
| --- | --- | --- | --- | --- |
|
||||
| `docker stop` api-1: до / отказ / после | 134 / 107 / 155 | 0 / 0 / 0 | 12,7/87,9/101 → 13,7/90,9/99,1 | 0,11 s |
|
||||
| `docker stop` api-2: до / отказ / после | 155 / 115 / 158 | 0 / 0 / 0 | 11,4/80,2/107 → 12,6/90,6/149 | 0,10 s |
|
||||
| `docker kill` api-1: до / отказ / после | 158 / 79 / 157 | 0 / 2 (0,24 %) / 0 | 11,3/80,0/117 → 13,4/92,9/1012 | 0,92 s |
|
||||
| `docker kill` api-2: до / отказ / после | 158 / 64 / 143 | 0 / 2 (0,30 %) / 0 | 11,9/74,7/85,1 → 14,1/95,5/1011 | 0,99 s |
|
||||
| Rolling update, вся замена (33,7 s) | 128 | 0 | 15,2 / 83,5 / 111 | 0,22 s |
|
||||
|
||||
### Отказы
|
||||
|
||||
| Воздействие | Что ожидаем | Что наблюдали | Время восстановления / потеря данных |
|
||||
| --- | --- | --- | --- |
|
||||
| TODO | TODO | TODO | TODO |
|
||||
| Остановка реплики, обслужившей загрузку, для обеих реплик | другая реплика отдаёт файл | 6 из 6 скачиваний через оставшуюся реплику, SHA-256 совпал, локальной копии нет | сразу; потерь нет |
|
||||
| `make test-failover` ×2, разные реплики | ≤ 10 s, без дублей, файл читается | 0 ошибок, запись через 3,56–3,58 s | PASS |
|
||||
| `restart.sh`: postgres, redis, s3, api ×2, lb | данные, файл и токен сохраняются | PASS | потерь нет |
|
||||
| `docker compose stop s3` | вложения `503`, остальное работает | `ready` `503` за 1,01 s; скачивание `503` за 1,01 s; загрузка `503` за 1,03 s; метаданные `200`; текст `201` | после `start` `ready 200`, файл цел |
|
||||
| `docker compose pause s3` (S3 завис) | ответы ограничены таймаутами | `ready` `503` за 1,01 s; скачивание и загрузка `503` за 5,0 s; текст `201` | после `unpause` `ready 200`, файл цел |
|
||||
| Обрыв загрузки через LB | ничего не остаётся | строки 7 → 7, объекты 7 → 7, временных каталогов 0 | потерь нет |
|
||||
| PostgreSQL отклоняет INSERT после PUT (триггер `RAISE EXCEPTION`) | объект без строки не становится вечным | `503`, строки 7 → 7, объекты 7 → 8; grace 3600 s — не удалён; grace 1 s — удалён только он, 7 привязанных не тронуты | следующая загрузка `201` |
|
||||
| Потеря соединения с PostgreSQL при INSERT (триггер `pg_terminate_backend`) | объект не удаляется сразу | `503`, строки 8 → 8, объекты 8 → 9; grace 3600 s — не удалён; grace 1 s — удалён только он, 8 привязанных не тронуты | следующая загрузка `201` |
|
||||
| Объект создан напрямую в S3, строки нет | dry-run не удаляет, apply удаляет только его | dry-run: `orphans: 1, deleted: 0`; apply: `deleted: 1`, `referenced: 11`; повтор `orphans: 0` | — |
|
||||
| Сверка во время зависшего S3 | проход прерывается, сервис живёт | в логе `reconcile pass aborted`, контейнер продолжает работу | следующий проход штатный |
|
||||
| Живая реплика без доступа к PostgreSQL | LB исключает её | `live 200`, `core 503`, `is DOWN`, 10 из 10 запросов через вторую реплику | после возврата доступа снова в ротации |
|
||||
| `docker compose stop redis`, `stop postgres`, `stop api` | как в lab3 | logout `503` без отзыва; `503 database unavailable`; при остановке всех API — LB `503` в формате контракта | после `start` работает |
|
||||
|
||||
В `failures.txt` §3 при остановленном PostgreSQL анонимный запрос получает `401` от API: для ответа без токена БД не нужна, а реплики ещё не исключены из LB (≤ 3 s).
|
||||
|
||||
Отказ между записью в S3 и PostgreSQL внедрялся реальным триггером на таблице. Случай «COMMIT прошёл, ответ потерян» проверен только тестом с подменой ошибки Prisma: надёжно оборвать соединение точно после COMMIT на стенде не удаётся.
|
||||
|
||||
## Ограничения и следующие шаги
|
||||
|
||||
TODO: какие отказы выдерживаются и какие нет; что не реализовано; где учебное упрощение; какое улучшение подтверждено, а какое пока гипотеза.
|
||||
- **Отказ процесса, а не узла.** Все контейнеры на одном хосте. LB, PostgreSQL, Redis и S3 — по одному экземпляру, один диск MinIO без репликации: потеря тома `s3data` означает потерю файлов.
|
||||
- **Образ S3.** Сторонняя сборка заархивированного проекта; контейнер работает от root. Для учебного стенда с синтетическими данными это приемлемо, для реальной системы потребовалась бы своя сборка или другой S3-сервер.
|
||||
- **`/health/core`** не входит в обязательный контракт; основание и способ отката — в разделе «Критичные зависимости и проверка готовности».
|
||||
- **Строка есть, объекта нет.** Сверка находит только объекты без строк. Потеря объекта (например, удаление вручную) проявляется как `500` при скачивании с записью в логе. Обратная сверка — трек 5.2, не заявлен.
|
||||
- **Непривязанные вложения не удаляются.** Загруженный файл, на который не сослалось ни одно сообщение, учтён в БД и остаётся навсегда: API удаления нет.
|
||||
- **Строгая проверка изображений.** `failOn: 'warning'` может отклонить редкий настоящий файл с предупреждением декодера. PNG с более чем 64 КиБ данных после `IEND` тоже получит `415`.
|
||||
- **Цена проверки.** Полное декодирование 40 МП занимает секунды CPU. Лимит загрузок общий на реплику, а не на пользователя: один пользователь может занять все слоты; rate limiting не реализован.
|
||||
- **Лишние байты после `413`.** Тело больше лимита отклоняется сразу, но оставшиеся байты дочитываются и отбрасываются, чтобы клиент получил ответ. Память не растёт, трафик ограничен только таймаутами LB.
|
||||
- **Медленные клиенты.** Приём тела загрузки ограничен 60 s; скачивание обрывается LB, если клиент не читает данные 30 s.
|
||||
- **Права сверки.** `reconciler` использует того же S3-пользователя, что и API, включая `DeleteObject`; отдельная учётная запись не заведена.
|
||||
- **`filename`.** Запрещены разделители пути, управляющие символы и разделители строк. Символы форматирования Unicode (например, смена направления текста) разрешены, чтобы не отклонять эмодзи с соединителями; имя используется только для отображения.
|
||||
- HTTP без TLS — только для локального стенда (TLS — lab7). `X-Instance-Id` раскрывает короткий id контейнера. Grace рассчитан на единые часы одного хоста; на нескольких узлах он должен учитывать расхождение часов.
|
||||
- **Подтверждено измерением:** файл переживает остановку реплики и restart; память API не растёт пропорционально данным в указанных профилях; отказ S3 не останавливает текстовый чат; сироты удаляются только после grace. **Гипотеза, не измерена:** стоимость обхода сверкой при большом числе объектов (трек 5.2).
|
||||
|
||||
## Ответы на вопросы защиты
|
||||
|
||||
- **Почему файл в Docker volume одной реплики не равен объектному хранилищу?** Volume привязан к контейнеру и хосту: другая реплика его не видит, а при остановке реплики файл недоступен. S3 — отдельный общий сервис со своим API и правами, поэтому файл читает любая реплика.
|
||||
- **Когда можно честно вернуть 201?** Когда объект подтверждён S3, а метаданные зафиксированы в PostgreSQL.
|
||||
- **Что произойдёт, если S3 PUT прошёл, а COMMIT в PostgreSQL — нет?** Клиент получит ошибку, объект останется без строки и будет удалён сверкой после grace. Если исход неясен, API объект не удаляет, потому что COMMIT мог пройти.
|
||||
- **Почему случайный object key и приватный bucket не заменяют ACL приложения?** Bucket закрывает прямой доступ, но API читает объекты с правами на весь bucket, и участие в чате S3 неизвестно: его проверяет только приложение. Ключ также может утечь, например через логи.
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
name: messenger-lab4-test
|
||||
|
||||
services:
|
||||
postgres:
|
||||
image: postgres:17.2-alpine
|
||||
environment:
|
||||
POSTGRES_USER: test
|
||||
POSTGRES_PASSWORD: test
|
||||
POSTGRES_DB: messenger_test
|
||||
tmpfs:
|
||||
- /var/lib/postgresql/data
|
||||
healthcheck:
|
||||
test: ["CMD", "pg_isready", "-U", "test", "-d", "messenger_test"]
|
||||
interval: 2s
|
||||
timeout: 3s
|
||||
retries: 15
|
||||
|
||||
redis:
|
||||
image: redis:7.4.1-alpine
|
||||
command: ["redis-server", "--save", "", "--appendonly", "no"]
|
||||
|
||||
s3:
|
||||
image: pgsty/minio:RELEASE.2026-08-04T00-00-00Z@sha256:b6bfe7239bfc83fb90d31612d9704d86039dd714f7904b3f1ad68f211e602372
|
||||
command: ["server", "/data", "--address", ":9000"]
|
||||
environment:
|
||||
MINIO_ROOT_USER: test-root
|
||||
MINIO_ROOT_PASSWORD: test-root-password
|
||||
MINIO_BROWSER: "off"
|
||||
tmpfs:
|
||||
- /data
|
||||
healthcheck:
|
||||
test: ["CMD", "curl", "-fsS", "-o", "/dev/null", "http://127.0.0.1:9000/minio/health/live"]
|
||||
interval: 2s
|
||||
timeout: 3s
|
||||
retries: 15
|
||||
|
||||
tests:
|
||||
build:
|
||||
context: .
|
||||
target: test
|
||||
image: mtusi-messenger-lab4-test:dev
|
||||
environment:
|
||||
DATABASE_URL: postgresql://test:test@postgres:5432/messenger_test?connection_limit=20
|
||||
CACHE_URL: redis://redis:6379/0
|
||||
S3_ENDPOINT: http://s3:9000
|
||||
S3_BUCKET: messenger-test
|
||||
S3_ACCESS_KEY_ID: test-root
|
||||
S3_SECRET_ACCESS_KEY: test-root-password
|
||||
depends_on:
|
||||
postgres:
|
||||
condition: service_healthy
|
||||
redis:
|
||||
condition: service_started
|
||||
s3:
|
||||
condition: service_healthy
|
||||
+240
@@ -0,0 +1,240 @@
|
||||
name: messenger-lab4
|
||||
|
||||
services:
|
||||
secrets-init:
|
||||
image: alpine:3.20.3
|
||||
volumes:
|
||||
- secrets:/secrets
|
||||
command:
|
||||
- sh
|
||||
- -ec
|
||||
- |
|
||||
cd /secrets
|
||||
mkdir -p postgres redis api migrate s3 s3-init reconciler
|
||||
for entry in pg_password:postgres:24 redis_password:redis:24 s3_root_user:s3:10 s3_root_password:s3:20 s3_access_key_id:api:10 s3_secret_access_key:api:20; do
|
||||
file=$${entry%%:*}; rest=$${entry#*:}; dir=$${rest%%:*}; size=$${rest##*:}
|
||||
[ -s "$$dir/$$file" ] || head -c "$$size" /dev/urandom | od -An -tx1 | tr -d ' \n' > "$$dir/$$file"
|
||||
done
|
||||
rm -f s3-init/*
|
||||
cp s3/s3_root_user s3/s3_root_password api/s3_access_key_id api/s3_secret_access_key s3-init/
|
||||
printf 'postgresql://messenger:%s@postgres:5432/messenger?connection_limit=10&pool_timeout=5&connect_timeout=5&socket_timeout=10&application_name=api&options=-c%%20statement_timeout%%3D5000' "$$(cat postgres/pg_password)" > api/database_url
|
||||
printf 'redis://:%s@redis:6379/0' "$$(cat redis/redis_password)" > api/cache_url
|
||||
printf 'postgresql://messenger:%s@postgres:5432/messenger' "$$(cat postgres/pg_password)" > migrate/database_url
|
||||
cp api/database_url api/s3_access_key_id api/s3_secret_access_key reconciler/
|
||||
chmod 0444 postgres/* redis/* api/* migrate/* s3/* s3-init/* reconciler/*
|
||||
deploy:
|
||||
resources:
|
||||
limits: { cpus: "0.1", memory: 32M }
|
||||
|
||||
postgres:
|
||||
image: postgres:17.2-alpine
|
||||
environment:
|
||||
POSTGRES_USER: messenger
|
||||
POSTGRES_DB: messenger
|
||||
POSTGRES_PASSWORD_FILE: /run/secrets/pg_password
|
||||
volumes:
|
||||
- pgdata:/var/lib/postgresql/data
|
||||
- { type: volume, source: secrets, target: /run/secrets, read_only: true, volume: { subpath: postgres } }
|
||||
deploy:
|
||||
resources:
|
||||
limits: { cpus: "1.0", memory: 512M }
|
||||
healthcheck:
|
||||
test: ["CMD", "pg_isready", "-U", "messenger", "-d", "messenger"]
|
||||
interval: 5s
|
||||
timeout: 3s
|
||||
retries: 10
|
||||
depends_on:
|
||||
secrets-init:
|
||||
condition: service_completed_successfully
|
||||
restart: unless-stopped
|
||||
|
||||
redis:
|
||||
image: redis:7.4.1-alpine
|
||||
user: redis
|
||||
command:
|
||||
- sh
|
||||
- -c
|
||||
- exec redis-server --save '' --appendonly no --maxmemory 64mb --maxmemory-policy allkeys-lru --requirepass "$$(cat /run/secrets/redis_password)"
|
||||
volumes:
|
||||
- { type: volume, source: secrets, target: /run/secrets, read_only: true, volume: { subpath: redis } }
|
||||
deploy:
|
||||
resources:
|
||||
limits: { cpus: "0.25", memory: 128M }
|
||||
healthcheck:
|
||||
test: ["CMD-SHELL", "redis-cli --no-auth-warning -a \"$$(cat /run/secrets/redis_password)\" ping | grep -q PONG"]
|
||||
interval: 5s
|
||||
timeout: 3s
|
||||
retries: 10
|
||||
depends_on:
|
||||
secrets-init:
|
||||
condition: service_completed_successfully
|
||||
restart: unless-stopped
|
||||
|
||||
s3:
|
||||
image: pgsty/minio:RELEASE.2026-08-04T00-00-00Z@sha256:b6bfe7239bfc83fb90d31612d9704d86039dd714f7904b3f1ad68f211e602372
|
||||
command: ["server", "/data", "--address", ":9000"]
|
||||
environment:
|
||||
MINIO_ROOT_USER_FILE: /run/secrets/s3_root_user
|
||||
MINIO_ROOT_PASSWORD_FILE: /run/secrets/s3_root_password
|
||||
MINIO_BROWSER: "off"
|
||||
volumes:
|
||||
- s3data:/data
|
||||
- { type: volume, source: secrets, target: /run/secrets, read_only: true, volume: { subpath: s3 } }
|
||||
deploy:
|
||||
resources:
|
||||
limits: { cpus: "0.5", memory: 512M }
|
||||
healthcheck:
|
||||
test: ["CMD", "curl", "-fsS", "-o", "/dev/null", "http://127.0.0.1:9000/minio/health/live"]
|
||||
interval: 5s
|
||||
timeout: 3s
|
||||
retries: 10
|
||||
depends_on:
|
||||
secrets-init:
|
||||
condition: service_completed_successfully
|
||||
restart: unless-stopped
|
||||
|
||||
s3-init:
|
||||
image: pgsty/mc:RELEASE.2026-10-26T00-00-00Z@sha256:ce62c9053c69ce12a527a322345b5ad9f44b5f8a7b8189f82e516f2e3f30f1d8
|
||||
entrypoint: ["sh", "-ec"]
|
||||
command:
|
||||
- |
|
||||
cd /run/secrets
|
||||
export MC_HOST_root="http://$$(cat s3_root_user):$$(cat s3_root_password)@s3:9000"
|
||||
mc mb --ignore-existing "root/$$S3_BUCKET"
|
||||
mc anonymous set none "root/$$S3_BUCKET"
|
||||
cat > /tmp/policy.json <<POLICY
|
||||
{"Version":"2012-10-17","Statement":[
|
||||
{"Effect":"Allow","Action":["s3:ListBucket","s3:GetBucketLocation","s3:ListBucketMultipartUploads"],"Resource":["arn:aws:s3:::$$S3_BUCKET"]},
|
||||
{"Effect":"Allow","Action":["s3:GetObject","s3:PutObject","s3:DeleteObject","s3:AbortMultipartUpload","s3:ListMultipartUploadParts"],"Resource":["arn:aws:s3:::$$S3_BUCKET/*"]}
|
||||
]}
|
||||
POLICY
|
||||
mc admin policy create root messenger-api /tmp/policy.json
|
||||
mc admin user add root "$$(cat s3_access_key_id)" "$$(cat s3_secret_access_key)" > /dev/null
|
||||
mc admin policy attach root messenger-api --user "$$(cat s3_access_key_id)" > /dev/null
|
||||
echo "bucket $$S3_BUCKET ready"
|
||||
environment:
|
||||
S3_BUCKET: ${S3_BUCKET:-messenger-attachments}
|
||||
volumes:
|
||||
- { type: volume, source: secrets, target: /run/secrets, read_only: true, volume: { subpath: s3-init } }
|
||||
deploy:
|
||||
resources:
|
||||
limits: { cpus: "0.25", memory: 256M }
|
||||
depends_on:
|
||||
s3:
|
||||
condition: service_healthy
|
||||
|
||||
migrate:
|
||||
build:
|
||||
context: .
|
||||
target: migrate
|
||||
image: mtusi-messenger-lab4-migrate:dev
|
||||
environment:
|
||||
DATABASE_URL_FILE: /run/secrets/database_url
|
||||
volumes:
|
||||
- { type: volume, source: secrets, target: /run/secrets, read_only: true, volume: { subpath: migrate } }
|
||||
deploy:
|
||||
resources:
|
||||
limits: { cpus: "0.5", memory: 512M }
|
||||
depends_on:
|
||||
postgres:
|
||||
condition: service_healthy
|
||||
|
||||
api:
|
||||
build:
|
||||
context: .
|
||||
target: runtime
|
||||
image: mtusi-messenger-lab4-api:dev
|
||||
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}
|
||||
SHUTDOWN_DELAY_MS: 3000
|
||||
DATABASE_URL_FILE: /run/secrets/database_url
|
||||
CACHE_URL_FILE: /run/secrets/cache_url
|
||||
S3_ENDPOINT: http://s3:9000
|
||||
S3_REGION: ${S3_REGION:-us-east-1}
|
||||
S3_BUCKET: ${S3_BUCKET:-messenger-attachments}
|
||||
S3_ACCESS_KEY_ID_FILE: /run/secrets/s3_access_key_id
|
||||
S3_SECRET_ACCESS_KEY_FILE: /run/secrets/s3_secret_access_key
|
||||
MAX_UPLOAD_BYTES: ${MAX_UPLOAD_BYTES:-10485760}
|
||||
MAX_IMAGE_PIXELS: ${MAX_IMAGE_PIXELS:-40000000}
|
||||
volumes:
|
||||
- { type: volume, source: secrets, target: /run/secrets, read_only: true, volume: { subpath: api } }
|
||||
deploy:
|
||||
replicas: ${API_REPLICAS:-2}
|
||||
resources:
|
||||
limits: { cpus: "0.5", memory: 256M }
|
||||
stop_grace_period: 10s
|
||||
depends_on:
|
||||
migrate:
|
||||
condition: service_completed_successfully
|
||||
redis:
|
||||
condition: service_started
|
||||
s3-init:
|
||||
condition: service_completed_successfully
|
||||
restart: unless-stopped
|
||||
|
||||
reconciler:
|
||||
build:
|
||||
context: .
|
||||
target: runtime
|
||||
image: mtusi-messenger-lab4-api:dev
|
||||
command: ["node", "dist/reconcile.js", "--apply", "--loop", "--grace-seconds=${RECONCILE_GRACE_SECONDS:-3600}", "--interval-seconds=${RECONCILE_INTERVAL_SECONDS:-300}"]
|
||||
environment:
|
||||
DATABASE_URL_FILE: /run/secrets/database_url
|
||||
S3_ENDPOINT: http://s3:9000
|
||||
S3_REGION: ${S3_REGION:-us-east-1}
|
||||
S3_BUCKET: ${S3_BUCKET:-messenger-attachments}
|
||||
S3_ACCESS_KEY_ID_FILE: /run/secrets/s3_access_key_id
|
||||
S3_SECRET_ACCESS_KEY_FILE: /run/secrets/s3_secret_access_key
|
||||
volumes:
|
||||
- { type: volume, source: secrets, target: /run/secrets, read_only: true, volume: { subpath: reconciler } }
|
||||
deploy:
|
||||
resources:
|
||||
limits: { cpus: "0.25", memory: 128M }
|
||||
healthcheck:
|
||||
disable: true
|
||||
depends_on:
|
||||
migrate:
|
||||
condition: service_completed_successfully
|
||||
s3-init:
|
||||
condition: service_completed_successfully
|
||||
restart: unless-stopped
|
||||
|
||||
lb:
|
||||
image: haproxy:3.0.6-alpine
|
||||
ports:
|
||||
- "127.0.0.1:${APP_PORT:-8080}:8080"
|
||||
volumes:
|
||||
- ./infra/haproxy/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg:ro
|
||||
deploy:
|
||||
resources:
|
||||
limits: { cpus: "0.25", memory: 64M }
|
||||
depends_on:
|
||||
api:
|
||||
condition: service_healthy
|
||||
restart: unless-stopped
|
||||
|
||||
tools:
|
||||
profiles: [tools]
|
||||
build:
|
||||
dockerfile_inline: |
|
||||
FROM docker:27.3.1-cli
|
||||
RUN apk add --no-cache python3 make git && git config --global --add safe.directory /work
|
||||
WORKDIR /work
|
||||
image: mtusi-messenger-lab4-tools:dev
|
||||
environment:
|
||||
BASE_URL: http://lb:8080
|
||||
volumes:
|
||||
- .:/work
|
||||
- /var/run/docker.sock:/var/run/docker.sock
|
||||
deploy:
|
||||
resources:
|
||||
limits: { cpus: "1.0", memory: 256M }
|
||||
|
||||
volumes:
|
||||
pgdata:
|
||||
s3data:
|
||||
secrets:
|
||||
@@ -0,0 +1,36 @@
|
||||
{
|
||||
"openapi": "3.1.0",
|
||||
"info": {
|
||||
"title": "MTUSI Messenger — расширения лабораторной 4",
|
||||
"version": "1.4.0",
|
||||
"description": "Обратно совместимое дополнение к contracts/openapi.json (см. COURSE.md, раздел «Контракт и совместимость»). Обязательный контракт не меняется. Обоснование — REPORT.md, раздел «Критичные зависимости и проверка готовности»."
|
||||
},
|
||||
"paths": {
|
||||
"/health/core": {
|
||||
"get": {
|
||||
"operationId": "getCoreReadiness",
|
||||
"summary": "Служебная проверка для балансировщика и Docker healthcheck: может ли реплика обслуживать API без вложений",
|
||||
"description": "В отличие от /health/ready не учитывает S3. Проверяет PostgreSQL и то, что процесс не останавливается. Падение общего S3 не выводит реплики из балансировки: сообщения продолжают работать, вложения отвечают 503.",
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "Реплика готова",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"type": "object",
|
||||
"properties": { "status": { "const": "ok" } },
|
||||
"required": ["status"],
|
||||
"additionalProperties": false
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"503": {
|
||||
"description": "PostgreSQL недоступен или процесс останавливается; тело в общем формате Error",
|
||||
"content": { "application/json": { "schema": { "type": "object" } } }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
# Вложения: сценарий защиты, остановка загрузившей реплики, прерванная загрузка, отказ между S3 и БД
|
||||
# date: 2026-11-05T13:59:24Z
|
||||
# commit: f8e41b1
|
||||
|
||||
$ python3 scripts/attachment-scenario.py
|
||||
|
||||
## 1. Сценарий защиты: загрузка, сообщение, второй участник, посторонний, S3 мимо API
|
||||
POST /attachments -> 201 status=ready variants=[] error_code=None sha256=99ea7533c441f79cd630d15ce4d58d1ec4e06f6f0efb3b77a5c088305cd1bd75
|
||||
POST /messages text="" -> 201 attachment_ids совпадают: True
|
||||
Bob GET /content -> 200 image/png sha256 совпадает: True
|
||||
Eve GET /attachments/{id} -> 404
|
||||
Eve GET /attachments/{id}/content -> 404
|
||||
object_key в БД: attachments/<uuid>, filename в ключ не входит: True
|
||||
Анонимный GET объекта напрямую в S3 -> 403
|
||||
|
||||
## 2. Остановка реплики, обслужившей загрузку; скачивание через другую
|
||||
upload -> 201, обслужила реплика 0900a35b199d
|
||||
stop-one.sh: /messenger-lab4-api-2 exited finished=2026-11-05T13:59:28.482320827Z
|
||||
download -> 200 реплика=bb4424f819bb sha256 совпадает: True
|
||||
download -> 200 реплика=bb4424f819bb sha256 совпадает: True
|
||||
download -> 200 реплика=bb4424f819bb sha256 совпадает: True
|
||||
download -> 200 реплика=bb4424f819bb sha256 совпадает: True
|
||||
download -> 200 реплика=bb4424f819bb sha256 совпадает: True
|
||||
download -> 200 реплика=bb4424f819bb sha256 совпадает: True
|
||||
остановленная реплика среди обслуживших: False ; 0.05s
|
||||
Файл оригинала на диске обслужившей реплики (поиск по точному размеру 111817 байт):
|
||||
bb4424f819bb -> нет
|
||||
реплика 0900a35b199d возвращена
|
||||
upload -> 201, обслужила реплика bb4424f819bb
|
||||
stop-one.sh: /messenger-lab4-api-1 exited finished=2026-11-05T13:59:35.788929779Z
|
||||
download -> 200 реплика=0900a35b199d sha256 совпадает: True
|
||||
download -> 200 реплика=0900a35b199d sha256 совпадает: True
|
||||
download -> 200 реплика=0900a35b199d sha256 совпадает: True
|
||||
download -> 200 реплика=0900a35b199d sha256 совпадает: True
|
||||
download -> 200 реплика=0900a35b199d sha256 совпадает: True
|
||||
download -> 200 реплика=0900a35b199d sha256 совпадает: True
|
||||
остановленная реплика среди обслуживших: False ; 0.12s
|
||||
Файл оригинала на диске обслужившей реплики (поиск по точному размеру 111817 байт):
|
||||
0900a35b199d -> нет
|
||||
реплика bb4424f819bb возвращена
|
||||
|
||||
## 3. Прерванная загрузка через LB: ни строки, ни объекта, ни временного файла
|
||||
$ reconcile --grace-seconds=3600
|
||||
{"mode":"dry-run","grace_seconds":3600,"scanned":7,"fresh_skipped":7,"referenced":0,"orphans":0,"deleted":0,"failed":0,"orphan_keys":[],"duration_ms":207}
|
||||
$ reconcile --grace-seconds=3600
|
||||
{"mode":"dry-run","grace_seconds":3600,"scanned":7,"fresh_skipped":7,"referenced":0,"orphans":0,"deleted":0,"failed":0,"orphan_keys":[],"duration_ms":218}
|
||||
строк attachments: 7 -> 7; объектов: 7 -> 7
|
||||
временных каталогов в bb4424f819bb: 0
|
||||
временных каталогов в 0900a35b199d: 0
|
||||
|
||||
## 4. Реальный отказ PostgreSQL между PUT и INSERT: триггер отклоняет вставку
|
||||
$ reconcile --grace-seconds=3600
|
||||
{"mode":"dry-run","grace_seconds":3600,"scanned":7,"fresh_skipped":7,"referenced":0,"orphans":0,"deleted":0,"failed":0,"orphan_keys":[],"duration_ms":208}
|
||||
upload при отказе INSERT -> 503 unavailable
|
||||
строк attachments: 7 -> 7
|
||||
$ reconcile --apply --grace-seconds=3600
|
||||
{"mode":"apply","grace_seconds":3600,"scanned":8,"fresh_skipped":8,"referenced":0,"orphans":0,"deleted":0,"failed":0,"orphan_keys":[],"duration_ms":207}
|
||||
объектов было 7, стало 8; свежий объект защищён grace: удалено 0
|
||||
$ reconcile --apply --grace-seconds=1
|
||||
{"mode":"apply","grace_seconds":1,"scanned":8,"fresh_skipped":0,"referenced":7,"orphans":1,"deleted":1,"failed":0,"orphan_keys":["attachments/2e115516-6a91-4d6a-8b18-577663eebebe"],"duration_ms":300}
|
||||
после grace: сирот 1, удалено 1, привязанные объекты не тронуты: 7
|
||||
следующая загрузка без отказа -> 201
|
||||
|
||||
## 5. Потеря соединения с PostgreSQL между PUT и INSERT: исход неясен, объект остаётся до сверки
|
||||
$ reconcile --grace-seconds=3600
|
||||
{"mode":"dry-run","grace_seconds":3600,"scanned":8,"fresh_skipped":8,"referenced":0,"orphans":0,"deleted":0,"failed":0,"orphan_keys":[],"duration_ms":210}
|
||||
upload при отказе INSERT -> 503 unavailable
|
||||
строк attachments: 8 -> 8
|
||||
$ reconcile --apply --grace-seconds=3600
|
||||
{"mode":"apply","grace_seconds":3600,"scanned":9,"fresh_skipped":9,"referenced":0,"orphans":0,"deleted":0,"failed":0,"orphan_keys":[],"duration_ms":214}
|
||||
объектов было 8, стало 9; свежий объект защищён grace: удалено 0
|
||||
$ reconcile --apply --grace-seconds=1
|
||||
{"mode":"apply","grace_seconds":1,"scanned":9,"fresh_skipped":0,"referenced":8,"orphans":1,"deleted":1,"failed":0,"orphan_keys":["attachments/38b788ac-03f5-4bb4-88cf-813b8445c686"],"duration_ms":221}
|
||||
после grace: сирот 1, удалено 1, привязанные объекты не тронуты: 8
|
||||
следующая загрузка без отказа -> 201
|
||||
|
||||
@@ -0,0 +1,163 @@
|
||||
# Стенд: сервисы, порты, лимиты, секреты, миграции
|
||||
# date: 2026-11-05T13:56:38Z
|
||||
# commit: f8e41b1
|
||||
|
||||
$ docker compose ps -a --format table {{.Service}}\t{{.Name}}\t{{.Image}}\t{{.Status}}\t{{.Ports}}
|
||||
SERVICE NAME IMAGE STATUS PORTS
|
||||
api messenger-lab4-api-1 mtusi-messenger-lab4-api:dev Up 11 seconds (healthy) 8080/tcp
|
||||
api messenger-lab4-api-2 mtusi-messenger-lab4-api:dev Up 11 seconds (healthy) 8080/tcp
|
||||
lb messenger-lab4-lb-1 haproxy:3.0.6-alpine Up 5 seconds 127.0.0.1:8080->8080/tcp
|
||||
migrate messenger-lab4-migrate-1 mtusi-messenger-lab4-migrate:dev Exited (0) 12 seconds ago
|
||||
postgres messenger-lab4-postgres-1 postgres:17.2-alpine Up 21 seconds (healthy) 5432/tcp
|
||||
reconciler messenger-lab4-reconciler-1 mtusi-messenger-lab4-api:dev Up 11 seconds 8080/tcp
|
||||
redis messenger-lab4-redis-1 redis:7.4.1-alpine Up 21 seconds (healthy) 6379/tcp
|
||||
s3 messenger-lab4-s3-1 pgsty/minio:RELEASE.2026-08-04T00-00-00Z@sha256:b6bfe7239bfc83fb90d31612d9704d86039dd714f7904b3f1ad68f211e602372 Up 21 seconds (healthy) 9000/tcp
|
||||
s3-init messenger-lab4-s3-init-1 pgsty/mc:RELEASE.2026-10-26T00-00-00Z@sha256:ce62c9053c69ce12a527a322345b5ad9f44b5f8a7b8189f82e516f2e3f30f1d8 Exited (0) 12 seconds ago
|
||||
secrets-init messenger-lab4-secrets-init-1 alpine:3.20.3 Exited (0) 21 seconds ago
|
||||
tools messenger-lab4-tools-run-3ba5514e0d22 mtusi-messenger-lab4-tools:dev Up 4 seconds
|
||||
|
||||
## CPU/RAM-лимиты (NanoCpus: 1e9 = 1 CPU; Memory в байтах)
|
||||
$ sh -c docker inspect -f '{{.Name}} cpus={{.HostConfig.NanoCpus}} memory={{.HostConfig.Memory}}' $(docker compose ps -aq)
|
||||
/messenger-lab4-api-1 cpus=500000000 memory=268435456
|
||||
/messenger-lab4-api-2 cpus=500000000 memory=268435456
|
||||
/messenger-lab4-lb-1 cpus=250000000 memory=67108864
|
||||
/messenger-lab4-migrate-1 cpus=500000000 memory=536870912
|
||||
/messenger-lab4-postgres-1 cpus=1000000000 memory=536870912
|
||||
/messenger-lab4-reconciler-1 cpus=250000000 memory=134217728
|
||||
/messenger-lab4-redis-1 cpus=250000000 memory=134217728
|
||||
/messenger-lab4-s3-1 cpus=500000000 memory=536870912
|
||||
/messenger-lab4-s3-init-1 cpus=250000000 memory=268435456
|
||||
/messenger-lab4-secrets-init-1 cpus=100000000 memory=33554432
|
||||
/messenger-lab4-tools-run-3ba5514e0d22 cpus=1000000000 memory=268435456
|
||||
|
||||
## container_name не задан (одно имя сервиса масштабируется)
|
||||
$ sh -c docker compose config | grep -c container_name || true
|
||||
0
|
||||
|
||||
## Секреты: каждый сервис видит только свои файлы
|
||||
$ docker compose exec -T postgres ls /run/secrets
|
||||
pg_password
|
||||
|
||||
$ docker compose exec -T redis ls /run/secrets
|
||||
redis_password
|
||||
|
||||
$ docker compose exec -T s3 ls /run/secrets
|
||||
s3_root_password
|
||||
s3_root_user
|
||||
|
||||
$ docker compose exec -T api ls /run/secrets
|
||||
cache_url
|
||||
database_url
|
||||
s3_access_key_id
|
||||
s3_secret_access_key
|
||||
|
||||
$ docker compose exec -T reconciler ls /run/secrets
|
||||
database_url
|
||||
s3_access_key_id
|
||||
s3_secret_access_key
|
||||
|
||||
$ docker compose run --rm --no-deps -T --entrypoint ls s3-init /run/secrets
|
||||
s3_access_key_id
|
||||
s3_root_password
|
||||
s3_root_user
|
||||
s3_secret_access_key
|
||||
|
||||
$ docker compose exec -T lb sh -c ls /run/secrets 2>/dev/null || echo "no /run/secrets"
|
||||
no /run/secrets
|
||||
|
||||
$ docker compose exec -T lb haproxy -v
|
||||
HAProxy version 3.0.6-c2c0090 2024/11/07 - https://haproxy.org/
|
||||
Status: long-term supported branch - will stop receiving fixes around Q2 2029.
|
||||
Known bugs: http://www.haproxy.org/bugs/bugs-3.0.6.html
|
||||
Running on: Linux 5.15.153.1-microsoft-standard-WSL2 #1 SMP Fri Mar 29 23:14:13 UTC 2024 x86_64
|
||||
|
||||
$ docker compose exec -T postgres postgres --version
|
||||
postgres (PostgreSQL) 17.2
|
||||
|
||||
$ docker compose exec -T redis redis-server --version
|
||||
Redis server v=7.4.1 sha=00000000:0 malloc=jemalloc-5.3.0 bits=64 build=a93ce81042b3a6d7
|
||||
|
||||
$ docker compose exec -T s3 minio --version
|
||||
minio version RELEASE.2026-08-04T00-00-00Z (commit-id=d88f46ccee345a9c2fabe2d221d9a9e56bc11aec)
|
||||
Runtime: go1.26.5 linux/amd64
|
||||
License: GNU AGPLv3 - https://www.gnu.org/licenses/agpl-3.0.html
|
||||
Copyright: 2015-2026 MinIO, Inc.
|
||||
|
||||
## Таблицы вложений
|
||||
$ docker compose exec -T postgres psql -U messenger -d messenger -c \d attachments -c \d message_attachments
|
||||
Table "public.attachments"
|
||||
Column | Type | Collation | Nullable | Default
|
||||
--------------+-----------------------------+-----------+----------+-------------------
|
||||
id | uuid | | not null |
|
||||
chat_id | uuid | | not null |
|
||||
owner_id | uuid | | not null |
|
||||
filename | character varying(255) | | not null |
|
||||
content_type | character varying(32) | | not null |
|
||||
size_bytes | integer | | not null |
|
||||
sha256 | character(64) | | not null |
|
||||
object_key | character varying(128) | | not null |
|
||||
created_at | timestamp(3) with time zone | | not null | CURRENT_TIMESTAMP
|
||||
updated_at | timestamp(3) with time zone | | not null | CURRENT_TIMESTAMP
|
||||
Indexes:
|
||||
"attachments_pkey" PRIMARY KEY, btree (id)
|
||||
"attachments_chat_id_idx" btree (chat_id)
|
||||
"attachments_object_key_key" UNIQUE, btree (object_key)
|
||||
"attachments_owner_id_idx" btree (owner_id)
|
||||
Check constraints:
|
||||
"attachments_content_type_allowed" CHECK (content_type::text = ANY (ARRAY['image/png'::character varying, 'image/jpeg'::character varying]::text[]))
|
||||
"attachments_filename_not_blank" CHECK (btrim(filename::text) <> ''::text)
|
||||
"attachments_sha256_format" CHECK (sha256 ~ '^[a-f0-9]{64}$'::text)
|
||||
"attachments_size_range" CHECK (size_bytes >= 1 AND size_bytes <= 10485760)
|
||||
Foreign-key constraints:
|
||||
"attachments_chat_id_fkey" FOREIGN KEY (chat_id) REFERENCES chats(id) ON UPDATE CASCADE ON DELETE CASCADE
|
||||
"attachments_owner_id_fkey" FOREIGN KEY (owner_id) REFERENCES users(id) ON UPDATE CASCADE ON DELETE RESTRICT
|
||||
Referenced by:
|
||||
TABLE "message_attachments" CONSTRAINT "message_attachments_attachment_id_fkey" FOREIGN KEY (attachment_id) REFERENCES attachments(id) ON UPDATE CASCADE
|
||||
|
||||
Table "public.message_attachments"
|
||||
Column | Type | Collation | Nullable | Default
|
||||
---------------+----------+-----------+----------+---------
|
||||
message_id | uuid | | not null |
|
||||
position | smallint | | not null |
|
||||
attachment_id | uuid | | not null |
|
||||
Indexes:
|
||||
"message_attachments_pkey" PRIMARY KEY, btree (message_id, "position")
|
||||
"message_attachments_attachment_id_idx" btree (attachment_id)
|
||||
"message_attachments_message_id_attachment_id_key" UNIQUE, btree (message_id, attachment_id)
|
||||
Check constraints:
|
||||
"message_attachments_position_range" CHECK ("position" >= 0 AND "position" <= 9)
|
||||
Foreign-key constraints:
|
||||
"message_attachments_attachment_id_fkey" FOREIGN KEY (attachment_id) REFERENCES attachments(id) ON UPDATE CASCADE
|
||||
"message_attachments_message_id_fkey" FOREIGN KEY (message_id) REFERENCES messages(id) ON UPDATE CASCADE ON DELETE CASCADE
|
||||
|
||||
|
||||
## Миграции: отдельный one-shot сервис, повторный запуск ничего не применяет
|
||||
$ docker compose run --rm migrate
|
||||
Container messenger-lab4-secrets-init-1 Created
|
||||
Container messenger-lab4-postgres-1 Running
|
||||
Container messenger-lab4-secrets-init-1 Starting
|
||||
Container messenger-lab4-secrets-init-1 Started
|
||||
Container messenger-lab4-secrets-init-1 Waiting
|
||||
Container messenger-lab4-secrets-init-1 Exited
|
||||
Prisma schema loaded from prisma/schema.prisma
|
||||
Datasource "db": PostgreSQL database "messenger", schema "public" at "postgres:5432"
|
||||
|
||||
4 migrations found in prisma/migrations
|
||||
|
||||
|
||||
No pending migrations to apply.
|
||||
|
||||
$ docker compose run --rm --no-deps migrate sh -c export DATABASE_URL="$(cat "$DATABASE_URL_FILE")"; npx prisma migrate diff --from-url "$DATABASE_URL" --to-schema-datamodel prisma/schema.prisma --exit-code
|
||||
No difference detected.
|
||||
|
||||
|
||||
$ docker compose exec -T postgres psql -U messenger -d messenger -c SELECT migration_name, finished_at FROM _prisma_migrations ORDER BY started_at
|
||||
migration_name | finished_at
|
||||
---------------------------------+-------------------------------
|
||||
20261021000000_init | 2026-11-05 13:56:26.122879+00
|
||||
20261021010000_auth_sessions | 2026-11-05 13:56:26.136187+00
|
||||
20261101000000_idempotency_keys | 2026-11-05 13:56:26.145579+00
|
||||
20261105000000_attachments | 2026-11-05 13:56:26.159672+00
|
||||
(4 rows)
|
||||
|
||||
|
||||
@@ -0,0 +1,148 @@
|
||||
{
|
||||
"scenario": "sequential failover of each api replica",
|
||||
"resources_under_load": [
|
||||
"messenger-lab4-api-1 cpu=44.15% mem=105MiB / 256MiB",
|
||||
"messenger-lab4-api-2 cpu=49.51% mem=122.1MiB / 256MiB",
|
||||
"messenger-lab4-lb-1 cpu=13.85% mem=15.72MiB / 64MiB",
|
||||
"messenger-lab4-postgres-1 cpu=26.52% mem=65.75MiB / 512MiB",
|
||||
"messenger-lab4-reconciler-1 cpu=0.00% mem=32.57MiB / 128MiB",
|
||||
"messenger-lab4-redis-1 cpu=1.89% mem=8.105MiB / 128MiB",
|
||||
"messenger-lab4-s3-1 cpu=0.09% mem=248.8MiB / 512MiB"
|
||||
],
|
||||
"replicas": [
|
||||
{
|
||||
"container": "/messenger-lab4-api-1 running image=0387c3cfc941",
|
||||
"action": "docker kill (SIGKILL)",
|
||||
"before": {
|
||||
"seconds": 5.0,
|
||||
"successful_writes": 789,
|
||||
"failed_attempts": 0,
|
||||
"error_ratio": 0.0,
|
||||
"successful_rps": 157.8,
|
||||
"latency_ms_successful": {
|
||||
"p50": 11.3,
|
||||
"p95": 80.0,
|
||||
"p99": 117.2
|
||||
},
|
||||
"max_write_gap_seconds": 0.155,
|
||||
"instances": [
|
||||
"0900a35b199d",
|
||||
"bb4424f819bb"
|
||||
]
|
||||
},
|
||||
"stop_command_seconds": 0.444,
|
||||
"target_last_success_after_stop_seconds": null,
|
||||
"outage_window": {
|
||||
"seconds": 10.444,
|
||||
"successful_writes": 828,
|
||||
"failed_attempts": 2,
|
||||
"error_ratio": 0.0024,
|
||||
"successful_rps": 79.3,
|
||||
"latency_ms_successful": {
|
||||
"p50": 13.4,
|
||||
"p95": 92.9,
|
||||
"p99": 1012.3
|
||||
},
|
||||
"max_write_gap_seconds": 0.916,
|
||||
"instances": [
|
||||
"0900a35b199d"
|
||||
]
|
||||
},
|
||||
"restore_until_healthy_seconds": 5.742,
|
||||
"restore_until_serving_seconds": 3.928,
|
||||
"after_restore": {
|
||||
"seconds": 5.027,
|
||||
"successful_writes": 791,
|
||||
"failed_attempts": 0,
|
||||
"error_ratio": 0.0,
|
||||
"successful_rps": 157.4,
|
||||
"latency_ms_successful": {
|
||||
"p50": 11.9,
|
||||
"p95": 74.7,
|
||||
"p99": 85.1
|
||||
},
|
||||
"max_write_gap_seconds": 0.143,
|
||||
"instances": [
|
||||
"0900a35b199d",
|
||||
"bb4424f819bb"
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
"container": "/messenger-lab4-api-2 running image=0387c3cfc941",
|
||||
"action": "docker kill (SIGKILL)",
|
||||
"before": {
|
||||
"seconds": 5.0,
|
||||
"successful_writes": 790,
|
||||
"failed_attempts": 0,
|
||||
"error_ratio": 0.0,
|
||||
"successful_rps": 158.0,
|
||||
"latency_ms_successful": {
|
||||
"p50": 11.9,
|
||||
"p95": 74.7,
|
||||
"p99": 85.1
|
||||
},
|
||||
"max_write_gap_seconds": 0.143,
|
||||
"instances": [
|
||||
"0900a35b199d",
|
||||
"bb4424f819bb"
|
||||
]
|
||||
},
|
||||
"stop_command_seconds": 0.444,
|
||||
"target_last_success_after_stop_seconds": null,
|
||||
"outage_window": {
|
||||
"seconds": 10.444,
|
||||
"successful_writes": 670,
|
||||
"failed_attempts": 2,
|
||||
"error_ratio": 0.003,
|
||||
"successful_rps": 64.2,
|
||||
"latency_ms_successful": {
|
||||
"p50": 14.1,
|
||||
"p95": 95.5,
|
||||
"p99": 1010.9
|
||||
},
|
||||
"max_write_gap_seconds": 0.989,
|
||||
"instances": [
|
||||
"bb4424f819bb"
|
||||
]
|
||||
},
|
||||
"restore_until_healthy_seconds": 5.757,
|
||||
"restore_until_serving_seconds": 4.112,
|
||||
"after_restore": {
|
||||
"seconds": 5.032,
|
||||
"successful_writes": 720,
|
||||
"failed_attempts": 0,
|
||||
"error_ratio": 0.0,
|
||||
"successful_rps": 143.1,
|
||||
"latency_ms_successful": {
|
||||
"p50": 13.2,
|
||||
"p95": 77.0,
|
||||
"p99": 90.7
|
||||
},
|
||||
"max_write_gap_seconds": 0.082,
|
||||
"instances": [
|
||||
"0900a35b199d",
|
||||
"bb4424f819bb"
|
||||
]
|
||||
}
|
||||
}
|
||||
],
|
||||
"dataset": {
|
||||
"messages_in_db_before": 7276,
|
||||
"messages_in_db_after": 12641
|
||||
},
|
||||
"history": {
|
||||
"confirmed": 5365,
|
||||
"stored": 5365,
|
||||
"lost_confirmed": 0,
|
||||
"duplicates": 0
|
||||
},
|
||||
"load": {
|
||||
"workers": 4,
|
||||
"model": "closed loop, POST message",
|
||||
"warmup_seconds": 5,
|
||||
"client_timeout_seconds": 2,
|
||||
"retry": "same Idempotency-Key until success"
|
||||
},
|
||||
"date": "2026-11-05T14:08:00Z"
|
||||
}
|
||||
@@ -0,0 +1,150 @@
|
||||
{
|
||||
"scenario": "sequential failover of each api replica",
|
||||
"resources_under_load": [
|
||||
"messenger-lab4-api-1 cpu=44.28% mem=180MiB / 256MiB",
|
||||
"messenger-lab4-api-2 cpu=47.97% mem=137MiB / 256MiB",
|
||||
"messenger-lab4-lb-1 cpu=10.58% mem=13.88MiB / 64MiB",
|
||||
"messenger-lab4-postgres-1 cpu=25.75% mem=76.62MiB / 512MiB",
|
||||
"messenger-lab4-reconciler-1 cpu=0.00% mem=30.15MiB / 128MiB",
|
||||
"messenger-lab4-redis-1 cpu=1.84% mem=8.105MiB / 128MiB",
|
||||
"messenger-lab4-s3-1 cpu=2.26% mem=248.7MiB / 512MiB"
|
||||
],
|
||||
"replicas": [
|
||||
{
|
||||
"container": "/messenger-lab4-api-1 running image=0387c3cfc941",
|
||||
"action": "docker stop (SIGTERM, graceful)",
|
||||
"before": {
|
||||
"seconds": 5.0,
|
||||
"successful_writes": 669,
|
||||
"failed_attempts": 0,
|
||||
"error_ratio": 0.0,
|
||||
"successful_rps": 133.8,
|
||||
"latency_ms_successful": {
|
||||
"p50": 12.7,
|
||||
"p95": 87.9,
|
||||
"p99": 101.1
|
||||
},
|
||||
"max_write_gap_seconds": 0.085,
|
||||
"instances": [
|
||||
"0900a35b199d",
|
||||
"bb4424f819bb"
|
||||
]
|
||||
},
|
||||
"stop_command_seconds": 3.527,
|
||||
"target_last_success_after_stop_seconds": 1.476,
|
||||
"outage_window": {
|
||||
"seconds": 13.527,
|
||||
"successful_writes": 1452,
|
||||
"failed_attempts": 0,
|
||||
"error_ratio": 0.0,
|
||||
"successful_rps": 107.3,
|
||||
"latency_ms_successful": {
|
||||
"p50": 13.7,
|
||||
"p95": 90.9,
|
||||
"p99": 99.1
|
||||
},
|
||||
"max_write_gap_seconds": 0.106,
|
||||
"instances": [
|
||||
"0900a35b199d",
|
||||
"bb4424f819bb"
|
||||
]
|
||||
},
|
||||
"restore_until_healthy_seconds": 5.401,
|
||||
"restore_until_serving_seconds": 4.045,
|
||||
"after_restore": {
|
||||
"seconds": 5.025,
|
||||
"successful_writes": 781,
|
||||
"failed_attempts": 0,
|
||||
"error_ratio": 0.0,
|
||||
"successful_rps": 155.4,
|
||||
"latency_ms_successful": {
|
||||
"p50": 11.4,
|
||||
"p95": 80.3,
|
||||
"p99": 106.7
|
||||
},
|
||||
"max_write_gap_seconds": 0.091,
|
||||
"instances": [
|
||||
"0900a35b199d",
|
||||
"bb4424f819bb"
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
"container": "/messenger-lab4-api-2 running image=0387c3cfc941",
|
||||
"action": "docker stop (SIGTERM, graceful)",
|
||||
"before": {
|
||||
"seconds": 5.0,
|
||||
"successful_writes": 777,
|
||||
"failed_attempts": 0,
|
||||
"error_ratio": 0.0,
|
||||
"successful_rps": 155.4,
|
||||
"latency_ms_successful": {
|
||||
"p50": 11.4,
|
||||
"p95": 80.2,
|
||||
"p99": 106.7
|
||||
},
|
||||
"max_write_gap_seconds": 0.091,
|
||||
"instances": [
|
||||
"0900a35b199d",
|
||||
"bb4424f819bb"
|
||||
]
|
||||
},
|
||||
"stop_command_seconds": 3.588,
|
||||
"target_last_success_after_stop_seconds": 1.931,
|
||||
"outage_window": {
|
||||
"seconds": 13.588,
|
||||
"successful_writes": 1560,
|
||||
"failed_attempts": 0,
|
||||
"error_ratio": 0.0,
|
||||
"successful_rps": 114.8,
|
||||
"latency_ms_successful": {
|
||||
"p50": 12.6,
|
||||
"p95": 90.6,
|
||||
"p99": 149.4
|
||||
},
|
||||
"max_write_gap_seconds": 0.103,
|
||||
"instances": [
|
||||
"0900a35b199d",
|
||||
"bb4424f819bb"
|
||||
]
|
||||
},
|
||||
"restore_until_healthy_seconds": 5.974,
|
||||
"restore_until_serving_seconds": 3.539,
|
||||
"after_restore": {
|
||||
"seconds": 5.035,
|
||||
"successful_writes": 793,
|
||||
"failed_attempts": 0,
|
||||
"error_ratio": 0.0,
|
||||
"successful_rps": 157.5,
|
||||
"latency_ms_successful": {
|
||||
"p50": 11.1,
|
||||
"p95": 79.5,
|
||||
"p99": 94.0
|
||||
},
|
||||
"max_write_gap_seconds": 0.148,
|
||||
"instances": [
|
||||
"0900a35b199d",
|
||||
"bb4424f819bb"
|
||||
]
|
||||
}
|
||||
}
|
||||
],
|
||||
"dataset": {
|
||||
"messages_in_db_before": 265,
|
||||
"messages_in_db_after": 7276
|
||||
},
|
||||
"history": {
|
||||
"confirmed": 7011,
|
||||
"stored": 7011,
|
||||
"lost_confirmed": 0,
|
||||
"duplicates": 0
|
||||
},
|
||||
"load": {
|
||||
"workers": 4,
|
||||
"model": "closed loop, POST message",
|
||||
"warmup_seconds": 5,
|
||||
"client_timeout_seconds": 2,
|
||||
"retry": "same Idempotency-Key until success"
|
||||
},
|
||||
"date": "2026-11-05T14:07:08Z"
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
# make test-failover: stop-one.sh останавливает реплику, обслужившую тест; затем вторую
|
||||
# date: 2026-11-05T13:58:43Z
|
||||
# commit: f8e41b1
|
||||
|
||||
## Прогон 1
|
||||
### до
|
||||
/messenger-lab4-api-1 id=bb4424f819bb running exit=0 finished=2026-11-05T13:58:37.640782752Z
|
||||
/messenger-lab4-api-2 id=0900a35b199d running exit=0 finished=2026-11-05T13:58:37.58216166Z
|
||||
|
||||
$ make test-failover ACTION_SCRIPT=scripts/stop-one.sh
|
||||
python3 tests/resilience.py --mode failover --action-script "scripts/stop-one.sh" --base-url "http://lb:8080"
|
||||
Executing explicit failover hook; hook output suppressed to avoid leaking secrets.
|
||||
{"observed_instances_before": 2, "successful_writes": 125, "failed_attempts": 0, "first_recovery_seconds": 3.558}
|
||||
PASS: failover preserved confirmed data and existing session. Hook scope needs operator evidence.
|
||||
|
||||
### после: остановленная реплика Exited (0) — штатное завершение по SIGTERM
|
||||
/messenger-lab4-api-1 id=bb4424f819bb running exit=0 finished=2026-11-05T13:58:37.640782752Z
|
||||
/messenger-lab4-api-2 id=0900a35b199d exited exit=0 finished=2026-11-05T13:58:47.981063522Z
|
||||
|
||||
### журнал HAProxy
|
||||
Server api/api1 is DOWN, reason: Layer7 wrong status, code: 503, info: "Service Unavailable", check duration: 1ms. 1 active and 0 backup servers left. 0 sessions active, 0 requeued, 0 remaining in queue.
|
||||
Server api/api1 was DOWN and now enters maintenance (No IP for server ).
|
||||
|
||||
остановлена реплика: 0900a35b199d
|
||||
|
||||
## Прогон 2
|
||||
### до
|
||||
/messenger-lab4-api-1 id=bb4424f819bb running exit=0 finished=2026-11-05T13:58:37.640782752Z
|
||||
/messenger-lab4-api-2 id=0900a35b199d running exit=0 finished=2026-11-05T13:58:47.981063522Z
|
||||
|
||||
$ make test-failover ACTION_SCRIPT=scripts/stop-one.sh
|
||||
python3 tests/resilience.py --mode failover --action-script "scripts/stop-one.sh" --base-url "http://lb:8080"
|
||||
Executing explicit failover hook; hook output suppressed to avoid leaking secrets.
|
||||
{"observed_instances_before": 2, "successful_writes": 130, "failed_attempts": 0, "first_recovery_seconds": 3.575}
|
||||
PASS: failover preserved confirmed data and existing session. Hook scope needs operator evidence.
|
||||
|
||||
### после: остановленная реплика Exited (0) — штатное завершение по SIGTERM
|
||||
/messenger-lab4-api-1 id=bb4424f819bb exited exit=0 finished=2026-11-05T13:59:08.399118281Z
|
||||
/messenger-lab4-api-2 id=0900a35b199d running exit=0 finished=2026-11-05T13:58:47.981063522Z
|
||||
|
||||
### журнал HAProxy
|
||||
Server api/api1 is UP, reason: Layer7 check passed, code: 200, check duration: 2ms. 2 active and 0 backup servers online. 0 sessions requeued, 0 total in queue.
|
||||
Server api/api2 is DOWN, reason: Layer7 wrong status, code: 503, info: "Service Unavailable", check duration: 1ms. 1 active and 0 backup servers left. 0 sessions active, 0 requeued, 0 remaining in queue.
|
||||
Server api/api2 was DOWN and now enters maintenance (No IP for server ).
|
||||
|
||||
остановлена реплика: bb4424f819bb
|
||||
|
||||
## Проверка guard: stop-one.sh отказывается останавливать не api
|
||||
$ env TARGET_INSTANCE_ID=6285848df28a sh scripts/stop-one.sh
|
||||
refusing to stop 6285848df28a: not an api replica of messenger-lab4 (messenger-lab4/postgres)
|
||||
EXIT CODE 1
|
||||
|
||||
@@ -0,0 +1,143 @@
|
||||
# Отказы зависимостей, ошибки LB, expiry
|
||||
# date: 2026-11-05T14:08:50Z
|
||||
# commit: f8e41b1
|
||||
|
||||
## 1. Redis остановлен: вход и Bearer через PostgreSQL; logout — 503 без отзыва (токен остаётся действующим)
|
||||
$ docker compose stop redis
|
||||
Container messenger-lab4-redis-1 Stopping
|
||||
Container messenger-lab4-redis-1 Stopped
|
||||
|
||||
$ python3 scripts/probe.py logout
|
||||
register -> 201
|
||||
login -> 201 expires_at: 2026-11-05T15:08:51.406Z
|
||||
GET /api/v1/users/me -> 200
|
||||
DELETE /api/v1/auth/sessions/current -> 503 {"error": {"code": "unavailable", "message": "session cache unavailable", "request_id": "34e10540-6a85-41d2-a969-d375e95ede98"}}
|
||||
GET /api/v1/users/me -> 200
|
||||
|
||||
$ docker compose start redis
|
||||
Container messenger-lab4-secrets-init-1 Starting
|
||||
Container messenger-lab4-secrets-init-1 Started
|
||||
Container messenger-lab4-secrets-init-1 Waiting
|
||||
Container messenger-lab4-secrets-init-1 Exited
|
||||
Container messenger-lab4-redis-1 Starting
|
||||
Container messenger-lab4-redis-1 Started
|
||||
|
||||
## 2. Redis снова доступен: logout отзывает токен на всех репликах
|
||||
$ python3 scripts/probe.py logout
|
||||
register -> 201
|
||||
login -> 201 expires_at: 2026-11-05T15:08:56.699Z
|
||||
GET /api/v1/users/me -> 200
|
||||
DELETE /api/v1/auth/sessions/current -> 204
|
||||
GET /api/v1/users/me -> 401
|
||||
|
||||
## 3. PostgreSQL остановлен: live 200, запрос с токеном — 503 без внутренних деталей, аноним — 401
|
||||
$ docker compose stop postgres
|
||||
Container messenger-lab4-postgres-1 Stopping
|
||||
Container messenger-lab4-postgres-1 Stopped
|
||||
|
||||
$ python3 scripts/probe.py status GET /health/live
|
||||
GET /health/live (anonymous) -> 200 {"status": "ok"}
|
||||
|
||||
$ python3 scripts/probe.py status GET /api/v1/chats Bearer unknown-token
|
||||
GET /api/v1/chats (Bearer unknown-token) -> 503 {"error": {"code": "unavailable", "message": "database unavailable", "request_id": "1613a3ed-45d9-4d26-bdef-16d6a2a111fd"}}
|
||||
|
||||
$ python3 scripts/probe.py status GET /api/v1/users/me
|
||||
GET /api/v1/users/me (anonymous) -> 401 {"error": {"code": "unauthorized", "message": "missing or invalid session", "request_id": "947900bb-f8b7-4dc9-8f17-4b049c7747dc"}}
|
||||
|
||||
$ docker compose start postgres
|
||||
Container messenger-lab4-secrets-init-1 Starting
|
||||
Container messenger-lab4-secrets-init-1 Started
|
||||
Container messenger-lab4-secrets-init-1 Waiting
|
||||
Container messenger-lab4-secrets-init-1 Exited
|
||||
Container messenger-lab4-postgres-1 Starting
|
||||
Container messenger-lab4-postgres-1 Started
|
||||
|
||||
$ python3 scripts/probe.py ready
|
||||
ready
|
||||
|
||||
## 4. Все реплики API остановлены: LB отвечает 503 в формате ошибки контракта
|
||||
$ docker compose stop api
|
||||
Container messenger-lab4-api-3 Stopping
|
||||
Container messenger-lab4-api-4 Stopping
|
||||
Container messenger-lab4-api-4 Stopped
|
||||
Container messenger-lab4-api-3 Stopped
|
||||
|
||||
$ python3 scripts/probe.py status GET /api/v1/users/me
|
||||
GET /api/v1/users/me (anonymous) -> 503 {"error": {"code": "unavailable", "message": "no API replica available", "request_id": "483476c1-5e87-4bbe-8d77-3a172f3c8934"}}
|
||||
|
||||
$ docker compose start api
|
||||
Container messenger-lab4-secrets-init-1 Starting
|
||||
Container messenger-lab4-secrets-init-1 Started
|
||||
Container messenger-lab4-secrets-init-1 Waiting
|
||||
Container messenger-lab4-secrets-init-1 Waiting
|
||||
Container messenger-lab4-secrets-init-1 Waiting
|
||||
Container messenger-lab4-secrets-init-1 Exited
|
||||
Container messenger-lab4-secrets-init-1 Exited
|
||||
Container messenger-lab4-postgres-1 Waiting
|
||||
Container messenger-lab4-secrets-init-1 Exited
|
||||
Container messenger-lab4-s3-1 Waiting
|
||||
Container messenger-lab4-postgres-1 Healthy
|
||||
Container messenger-lab4-migrate-1 Starting
|
||||
Container messenger-lab4-s3-1 Healthy
|
||||
Container messenger-lab4-s3-init-1 Starting
|
||||
Container messenger-lab4-migrate-1 Started
|
||||
Container messenger-lab4-s3-init-1 Started
|
||||
Container messenger-lab4-s3-init-1 Waiting
|
||||
Container messenger-lab4-migrate-1 Waiting
|
||||
Container messenger-lab4-s3-init-1 Exited
|
||||
Container messenger-lab4-migrate-1 Exited
|
||||
Container messenger-lab4-api-4 Starting
|
||||
Container messenger-lab4-api-4 Started
|
||||
Container messenger-lab4-api-3 Starting
|
||||
Container messenger-lab4-api-3 Started
|
||||
|
||||
$ python3 scripts/probe.py ready
|
||||
ready
|
||||
|
||||
## 5. Живая, но неготовая реплика: PostgreSQL отклоняет подключения только от одной API (pg_hba reject)
|
||||
target=4f49dcd66a29 ip=172.27.0.7
|
||||
$ docker exec 4f49dcd66a29 node -e let n=0;const g=p=>require('http').get('http://127.0.0.1:8080'+p,r=>{console.log(p,r.statusCode);r.resume();if(++n===2)process.exit(0)});g('/health/live');g('/health/core')
|
||||
/health/live 200
|
||||
/health/core 503
|
||||
|
||||
$ python3 scripts/probe.py instances 10
|
||||
register -> 201
|
||||
login -> 201 expires_at: 2026-11-05T15:09:17.986Z
|
||||
GET /api/v1/users/me -> 200 X-Instance-Id: 984f0bba8d26
|
||||
GET /api/v1/users/me -> 200 X-Instance-Id: 984f0bba8d26
|
||||
GET /api/v1/users/me -> 200 X-Instance-Id: 984f0bba8d26
|
||||
GET /api/v1/users/me -> 200 X-Instance-Id: 984f0bba8d26
|
||||
GET /api/v1/users/me -> 200 X-Instance-Id: 984f0bba8d26
|
||||
GET /api/v1/users/me -> 200 X-Instance-Id: 984f0bba8d26
|
||||
GET /api/v1/users/me -> 200 X-Instance-Id: 984f0bba8d26
|
||||
GET /api/v1/users/me -> 200 X-Instance-Id: 984f0bba8d26
|
||||
GET /api/v1/users/me -> 200 X-Instance-Id: 984f0bba8d26
|
||||
GET /api/v1/users/me -> 200 X-Instance-Id: 984f0bba8d26
|
||||
|
||||
Server api/api1 is UP, reason: Layer7 check passed, code: 200, check duration: 2ms. 2 active and 0 backup servers online. 0 sessions requeued, 0 total in queue.
|
||||
Server api/api2 is DOWN, reason: Layer7 wrong status, code: 503, info: "Service Unavailable", check duration: 4ms. 1 active and 0 backup servers left. 0 sessions active, 0 requeued, 0 remaining in queue.
|
||||
|
||||
### доступ возвращён: реплика снова готова и получает запросы
|
||||
$ python3 scripts/probe.py instances 6
|
||||
register -> 201
|
||||
login -> 201 expires_at: 2026-11-05T15:09:22.744Z
|
||||
GET /api/v1/users/me -> 200 X-Instance-Id: 984f0bba8d26
|
||||
GET /api/v1/users/me -> 200 X-Instance-Id: 4f49dcd66a29
|
||||
GET /api/v1/users/me -> 200 X-Instance-Id: 984f0bba8d26
|
||||
GET /api/v1/users/me -> 200 X-Instance-Id: 4f49dcd66a29
|
||||
GET /api/v1/users/me -> 200 X-Instance-Id: 984f0bba8d26
|
||||
GET /api/v1/users/me -> 200 X-Instance-Id: 4f49dcd66a29
|
||||
|
||||
## 6. Expiry: реплики пересозданы с SESSION_TTL_SECONDS=2
|
||||
$ sh -c docker exec $(docker compose ps -q api | head -1) printenv SESSION_TTL_SECONDS
|
||||
2
|
||||
|
||||
$ python3 scripts/probe.py expiry 2
|
||||
register -> 201
|
||||
login -> 201 expires_at: 2026-11-05T14:09:35.576Z
|
||||
GET /api/v1/users/me (fresh) -> 200
|
||||
GET /api/v1/users/me (after 3s) -> 401
|
||||
|
||||
$ python3 scripts/probe.py ready
|
||||
ready
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"scenario": "parallel POST with one Idempotency-Key through LB",
|
||||
"requests": 20,
|
||||
"all_201": true,
|
||||
"distinct_message_ids": 1,
|
||||
"instances": [
|
||||
"0900a35b199d",
|
||||
"bb4424f819bb"
|
||||
],
|
||||
"messages_in_history": 1,
|
||||
"date": "2026-11-05T14:06:10Z"
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
# make test-persistence: restart.sh перезапускает postgres, redis, s3, api, lb с сохранением volumes
|
||||
# date: 2026-11-05T13:58:32Z
|
||||
# commit: f8e41b1
|
||||
|
||||
## до
|
||||
/messenger-lab4-api-1 running started=2026-11-05T13:56:27.073950909Z
|
||||
/messenger-lab4-api-2 running started=2026-11-05T13:56:27.359178642Z
|
||||
/messenger-lab4-lb-1 running started=2026-11-05T13:56:33.130552561Z
|
||||
/messenger-lab4-postgres-1 running started=2026-11-05T13:56:17.503936862Z
|
||||
/messenger-lab4-redis-1 running started=2026-11-05T13:56:17.512981001Z
|
||||
/messenger-lab4-s3-1 running started=2026-11-05T13:56:17.516358837Z
|
||||
|
||||
$ make test-persistence ACTION_SCRIPT=scripts/restart.sh
|
||||
python3 tests/resilience.py --mode persistence --action-script "scripts/restart.sh" --base-url "http://lb:8080"
|
||||
Executing explicit persistence hook; hook output suppressed to avoid leaking secrets.
|
||||
PASS: persistence preserved confirmed data and existing session. Hook scope needs operator evidence.
|
||||
|
||||
## после
|
||||
/messenger-lab4-api-1 running started=2026-11-05T13:58:38.339108415Z
|
||||
/messenger-lab4-api-2 running started=2026-11-05T13:58:38.170906453Z
|
||||
/messenger-lab4-lb-1 running started=2026-11-05T13:58:36.012363468Z
|
||||
/messenger-lab4-postgres-1 running started=2026-11-05T13:58:35.944741705Z
|
||||
/messenger-lab4-redis-1 running started=2026-11-05T13:58:35.661397483Z
|
||||
/messenger-lab4-s3-1 running started=2026-11-05T13:58:35.410755983Z
|
||||
@@ -0,0 +1,69 @@
|
||||
{
|
||||
"scenario": "rolling replacement: migrate once, then add new replica, wait healthy, stop old one",
|
||||
"resources_under_load": [
|
||||
"messenger-lab4-api-1 cpu=49.37% mem=104.1MiB / 256MiB",
|
||||
"messenger-lab4-api-2 cpu=50.09% mem=121.8MiB / 256MiB",
|
||||
"messenger-lab4-lb-1 cpu=10.88% mem=16.03MiB / 64MiB",
|
||||
"messenger-lab4-postgres-1 cpu=24.86% mem=68.67MiB / 512MiB",
|
||||
"messenger-lab4-reconciler-1 cpu=0.00% mem=32.57MiB / 128MiB",
|
||||
"messenger-lab4-redis-1 cpu=1.69% mem=8.105MiB / 128MiB",
|
||||
"messenger-lab4-s3-1 cpu=0.09% mem=248.8MiB / 512MiB"
|
||||
],
|
||||
"before": [
|
||||
"/messenger-lab4-api-1 running image=0387c3cfc941",
|
||||
"/messenger-lab4-api-2 running image=0387c3cfc941"
|
||||
],
|
||||
"after": [
|
||||
"/messenger-lab4-api-3 running image=b352fc8e08ea",
|
||||
"/messenger-lab4-api-4 running image=b352fc8e08ea"
|
||||
],
|
||||
"steps": [
|
||||
{
|
||||
"replaced": "bb4424f819bb",
|
||||
"new_replica_ready_seconds": 6.727,
|
||||
"step_seconds": 10.356
|
||||
},
|
||||
{
|
||||
"replaced": "0900a35b199d",
|
||||
"new_replica_ready_seconds": 6.775,
|
||||
"step_seconds": 10.466
|
||||
}
|
||||
],
|
||||
"during_update": {
|
||||
"seconds": 33.715,
|
||||
"successful_writes": 4320,
|
||||
"failed_attempts": 0,
|
||||
"error_ratio": 0.0,
|
||||
"successful_rps": 128.1,
|
||||
"latency_ms_successful": {
|
||||
"p50": 15.2,
|
||||
"p95": 83.5,
|
||||
"p99": 110.6
|
||||
},
|
||||
"max_write_gap_seconds": 0.215,
|
||||
"instances": [
|
||||
"0900a35b199d",
|
||||
"4f49dcd66a29",
|
||||
"984f0bba8d26",
|
||||
"bb4424f819bb"
|
||||
]
|
||||
},
|
||||
"dataset": {
|
||||
"messages_in_db_before": 12641,
|
||||
"messages_in_db_after": 17944
|
||||
},
|
||||
"history": {
|
||||
"confirmed": 5303,
|
||||
"stored": 5303,
|
||||
"lost_confirmed": 0,
|
||||
"duplicates": 0
|
||||
},
|
||||
"load": {
|
||||
"workers": 4,
|
||||
"model": "closed loop, POST message",
|
||||
"warmup_seconds": 5,
|
||||
"client_timeout_seconds": 2,
|
||||
"retry": "same Idempotency-Key until success"
|
||||
},
|
||||
"date": "2026-11-05T14:08:50Z"
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
# S3: идемпотентный bootstrap, приватность мимо API, отказ S3, сверка
|
||||
# date: 2026-11-05T14:00:11Z
|
||||
# commit: f8e41b1
|
||||
|
||||
## Повторный bootstrap ничего не ломает
|
||||
$ docker compose run --rm --no-deps -T s3-init
|
||||
Bucket created successfully `root/messenger-attachments`.
|
||||
Access permission for `root/messenger-attachments` is set to `private`
|
||||
Created policy `messenger-api` successfully.
|
||||
bucket messenger-attachments ready
|
||||
|
||||
$ docker compose run --rm --no-deps -T s3-init
|
||||
Bucket created successfully `root/messenger-attachments`.
|
||||
Access permission for `root/messenger-attachments` is set to `private`
|
||||
Created policy `messenger-api` successfully.
|
||||
bucket messenger-attachments ready
|
||||
|
||||
$ sh scripts/check-s3.sh
|
||||
## S3 не опубликован наружу, консоль выключена
|
||||
s3 ports=9000/tcp
|
||||
MINIO_BROWSER=off
|
||||
|
||||
## Права пользователя API: только свой bucket
|
||||
PUT в свой bucket -> allowed
|
||||
GET из своего bucket -> allowed
|
||||
создание другого bucket -> denied
|
||||
GET из другого bucket -> denied
|
||||
PUT в другой bucket -> denied
|
||||
изменение политики bucket -> denied
|
||||
администрирование пользователей -> denied
|
||||
|
||||
## Анонимный доступ напрямую к S3, мимо API
|
||||
GET messenger-attachments/<object> -> 403
|
||||
GET messenger-attachments/ -> 403
|
||||
PUT messenger-attachments/anon.txt -> 403
|
||||
|
||||
PASS: bucket приватный, у API нет прав вне своего bucket
|
||||
|
||||
## Отказ: контейнер S3 остановлен
|
||||
$ sh scripts/s3-outage.sh stop
|
||||
## S3: stop
|
||||
GET /health/live -> 200 0.01s X-Instance-Id=yes
|
||||
GET /health/ready -> 503 unavailable 1.01s X-Instance-Id=yes
|
||||
GET /attachments/{id} -> 200 0.01s X-Instance-Id=yes
|
||||
GET /attachments/{id}/content -> 503 unavailable 1.01s X-Instance-Id=yes
|
||||
POST /attachments -> 503 unavailable 1.03s X-Instance-Id=yes
|
||||
POST /messages (текст) -> 201 0.03s X-Instance-Id=yes
|
||||
## S3 восстановлен
|
||||
GET /health/ready -> 200 0.01s X-Instance-Id=yes
|
||||
GET /attachments/{id}/content -> 200 0.01s X-Instance-Id=yes
|
||||
sha256 совпадает: True
|
||||
|
||||
## Отказ: контейнер S3 завис (docker pause)
|
||||
$ sh scripts/s3-outage.sh pause
|
||||
## S3: pause
|
||||
GET /health/live -> 200 0.01s X-Instance-Id=yes
|
||||
GET /health/ready -> 503 unavailable 1.01s X-Instance-Id=yes
|
||||
GET /attachments/{id} -> 200 0.01s X-Instance-Id=yes
|
||||
GET /attachments/{id}/content -> 503 unavailable 5.01s X-Instance-Id=yes
|
||||
POST /attachments -> 503 unavailable 5.02s X-Instance-Id=yes
|
||||
POST /messages (текст) -> 201 0.01s X-Instance-Id=yes
|
||||
## S3 восстановлен
|
||||
GET /health/ready -> 200 0.01s X-Instance-Id=yes
|
||||
GET /attachments/{id}/content -> 200 0.01s X-Instance-Id=yes
|
||||
sha256 совпадает: True
|
||||
|
||||
## Сверка: объект без метаданных
|
||||
$ sh scripts/reconcile-demo.sh
|
||||
## Отказ между S3 и БД: объект есть, строки нет
|
||||
Name : orphan-1793887242
|
||||
|
||||
## Свежий объект, grace 3600 с, --apply: не удалён
|
||||
{"mode":"apply","grace_seconds":3600,"scanned":12,"fresh_skipped":12,"referenced":0,"orphans":0,"deleted":0,"failed":0,"orphan_keys":[],"duration_ms":294}
|
||||
объект на месте
|
||||
|
||||
## Dry-run, grace 1 с: найден, не удалён
|
||||
{"mode":"dry-run","grace_seconds":1,"scanned":12,"fresh_skipped":0,"referenced":11,"orphans":1,"deleted":0,"failed":0,"orphan_keys":["attachments/orphan-1793887242"],"duration_ms":299}
|
||||
объект на месте
|
||||
|
||||
## --apply, grace 1 с: удалён только он
|
||||
{"mode":"apply","grace_seconds":1,"scanned":12,"fresh_skipped":0,"referenced":11,"orphans":1,"deleted":1,"failed":0,"orphan_keys":["attachments/orphan-1793887242"],"duration_ms":305}
|
||||
объект удалён
|
||||
|
||||
## Повторный запуск: удалять нечего
|
||||
{"mode":"apply","grace_seconds":1,"scanned":11,"fresh_skipped":0,"referenced":11,"orphans":0,"deleted":0,"failed":0,"orphan_keys":[],"duration_ms":292}
|
||||
|
||||
## Журнал постоянного сервиса reconciler
|
||||
{"mode":"apply","grace_seconds":3600,"scanned":0,"fresh_skipped":0,"referenced":0,"orphans":0,"deleted":0,"failed":0,"orphan_keys":[],"duration_ms":219}
|
||||
@@ -0,0 +1,26 @@
|
||||
# Публичные проверки через LB; разные реплики обслуживают одну сессию
|
||||
# date: 2026-11-05T14:57:16Z
|
||||
# commit: 487da7d
|
||||
|
||||
$ make check
|
||||
python3 scripts/check_contract.py
|
||||
PASS: offline skeleton structure, local refs and Python syntax; application NOT tested.
|
||||
|
||||
$ make test
|
||||
python3 tests/smoke.py --base-url "http://lb:8080"
|
||||
PASS: lab4 public API scenario; infrastructure and grading evidence remain separate.
|
||||
|
||||
$ sh -c docker compose ps -q api | cut -c1-12
|
||||
f66e82a1b6ff
|
||||
f1c1c6ae7312
|
||||
|
||||
$ python3 scripts/probe.py instances 6
|
||||
register -> 201
|
||||
login -> 201 expires_at: 2026-11-05T15:57:19.502Z
|
||||
GET /api/v1/users/me -> 200 X-Instance-Id: f1c1c6ae7312
|
||||
GET /api/v1/users/me -> 200 X-Instance-Id: f66e82a1b6ff
|
||||
GET /api/v1/users/me -> 200 X-Instance-Id: f1c1c6ae7312
|
||||
GET /api/v1/users/me -> 200 X-Instance-Id: f66e82a1b6ff
|
||||
GET /api/v1/users/me -> 200 X-Instance-Id: f1c1c6ae7312
|
||||
GET /api/v1/users/me -> 200 X-Instance-Id: f66e82a1b6ff
|
||||
|
||||
@@ -0,0 +1,302 @@
|
||||
# Собственные e2e-тесты (PostgreSQL + Redis + MinIO из compose.test.yaml)
|
||||
# date: 2026-11-05T14:56:18Z
|
||||
# commit: 487da7d
|
||||
|
||||
$ docker compose -f compose.test.yaml run --rm tests npx jest --config ./test/jest-e2e.json --verbose
|
||||
Network messenger-lab4-test_default Creating
|
||||
Network messenger-lab4-test_default Created
|
||||
Container messenger-lab4-test-postgres-1 Creating
|
||||
Container messenger-lab4-test-redis-1 Creating
|
||||
Container messenger-lab4-test-s3-1 Creating
|
||||
Container messenger-lab4-test-postgres-1 Created
|
||||
Container messenger-lab4-test-s3-1 Created
|
||||
Container messenger-lab4-test-redis-1 Created
|
||||
Container messenger-lab4-test-s3-1 Starting
|
||||
Container messenger-lab4-test-redis-1 Starting
|
||||
Container messenger-lab4-test-postgres-1 Starting
|
||||
Container messenger-lab4-test-s3-1 Started
|
||||
Container messenger-lab4-test-postgres-1 Started
|
||||
Container messenger-lab4-test-redis-1 Started
|
||||
Prisma schema loaded from prisma/schema.prisma
|
||||
Datasource "db": PostgreSQL database "messenger_test", schema "public" at "postgres:5432"
|
||||
|
||||
4 migrations found in prisma/migrations
|
||||
|
||||
Applying migration `20261021000000_init`
|
||||
Applying migration `20261021010000_auth_sessions`
|
||||
Applying migration `20261101000000_idempotency_keys`
|
||||
Applying migration `20261105000000_attachments`
|
||||
|
||||
The following migration(s) have been applied:
|
||||
|
||||
migrations/
|
||||
└─ 20261021000000_init/
|
||||
└─ migration.sql
|
||||
└─ 20261021010000_auth_sessions/
|
||||
└─ migration.sql
|
||||
└─ 20261101000000_idempotency_keys/
|
||||
└─ migration.sql
|
||||
└─ 20261105000000_attachments/
|
||||
└─ migration.sql
|
||||
|
||||
All migrations have been successfully applied.
|
||||
PASS test/docs.e2e-spec.ts (22.593 s)
|
||||
Swagger UI и примеры (e2e)
|
||||
✓ отдаёт Swagger UI и документ контракта со всеми путями (701 ms)
|
||||
✓ каждый пример запроса и ответа проходит JSON Schema контракта (1328 ms)
|
||||
|
||||
[31m[Nest] 198 - [39m11/05/2026, 2:57:06 PM [31m ERROR[39m [38;5;3m[HttpExceptionFilter] [39m[31mDELETE /api/v1/auth/sessions/current -> 503: session cache unavailable[39m
|
||||
[31m[Nest] 185 - [39m11/05/2026, 2:57:06 PM [31m ERROR[39m [38;5;3m[HttpExceptionFilter] [39m[31mGET /health/ready -> 503: object storage unavailable[39m
|
||||
PASS test/concurrency.e2e-spec.ts (23.898 s)
|
||||
Безопасность при конкурентном доступе (e2e)
|
||||
✓ принимает 100 конкурентных сообщений от 10 клиентов и сохраняет ровно 100 уникальных (3698 ms)
|
||||
✓ регистрирует ровно одного пользователя при гонке 10 запросов за один и тот же username (337 ms)
|
||||
|
||||
PASS test/auth.e2e-spec.ts (23.971 s)
|
||||
Аутентификация и сессии (e2e)
|
||||
регистрация
|
||||
✓ требует password (118 ms)
|
||||
✓ отклоняет password короче 12 code points (149 ms)
|
||||
✓ хранит пароль как Argon2id-хеш, а не открытым текстом (1078 ms)
|
||||
POST /api/v1/auth/sessions
|
||||
✓ выдаёт непрозрачный Bearer-токен с expires_at и пользователем (1021 ms)
|
||||
✓ принимает пароль с двоеточием и не-ASCII символами (251 ms)
|
||||
✓ отвечает одинаковым 401 на неверный пароль и неизвестного пользователя (676 ms)
|
||||
✓ отклоняет JSON body при входе даже с верным Basic (156 ms)
|
||||
✓ отвечает 401 без заголовка Authorization (12 ms)
|
||||
защищённые endpoint
|
||||
✓ не принимают Basic (132 ms)
|
||||
✓ не принимают X-User-Id сам по себе (106 ms)
|
||||
✓ игнорируют X-User-Id рядом с Bearer (295 ms)
|
||||
✓ отвечают 401 на неизвестный токен (10 ms)
|
||||
DELETE /api/v1/auth/sessions/current
|
||||
✓ отзывает только текущий токен: 204, затем 401 на повтор (230 ms)
|
||||
|
||||
PASS test/reconciler.e2e-spec.ts (24.011 s)
|
||||
Сверка объектов S3 с метаданными (e2e)
|
||||
✓ dry-run сообщает о сироте и ничего не удаляет, apply удаляет, повторный запуск ничего не делает (996 ms)
|
||||
✓ не трогает свежий объект внутри grace period (179 ms)
|
||||
✓ не трогает старый объект, на который есть метаданные (125 ms)
|
||||
✓ при отказе PostgreSQL проход прерывается и ничего не удаляет (66 ms)
|
||||
✓ сбой удаления считается и повторяется на следующем проходе (91 ms)
|
||||
✓ обходит все страницы листинга (236 ms)
|
||||
✓ реальная загрузка остаётся доступной после сверки всего bucket префикса вложений (235 ms)
|
||||
Готовность при отказе S3 (e2e)
|
||||
✓ ready — 503 с заголовками за короткое время, live и core — 200 (126 ms)
|
||||
✓ при рабочем S3 ready — 200 (88 ms)
|
||||
|
||||
[31m[Nest] 198 - [39m11/05/2026, 2:57:06 PM [31m ERROR[39m [38;5;3m[HttpExceptionFilter] [39m[31mDELETE /api/v1/auth/sessions/current -> 503: session cache unavailable[39m
|
||||
[31m[Nest] 179 - [39m11/05/2026, 2:57:06 PM [31m ERROR[39m [38;5;3m[S3Service] [39m[31mget attachments/926af009-ea82-4673-a2b3-47519a444174 failed: Error[39m
|
||||
[31m[Nest] 179 - [39m11/05/2026, 2:57:06 PM [31m ERROR[39m [38;5;3m[HttpExceptionFilter] [39m[31mGET /api/v1/attachments/926af009-ea82-4673-a2b3-47519a444174/content -> 503: object storage unavailable[39m
|
||||
[31m[Nest] 179 - [39m11/05/2026, 2:57:06 PM [31m ERROR[39m [38;5;3m[S3Service] [39m[31mget attachments/31099a66-1b03-425e-9ab0-20aaf731ba8d failed: NoSuchKey[39m
|
||||
[31m[Nest] 179 - [39m11/05/2026, 2:57:06 PM [31m ERROR[39m [38;5;3m[HttpExceptionFilter] [39m[31mGET /api/v1/attachments/31099a66-1b03-425e-9ab0-20aaf731ba8d/content -> 500: Internal server error[39m
|
||||
[31m[Nest] 198 - [39m11/05/2026, 2:57:06 PM [31m ERROR[39m [38;5;3m[HttpExceptionFilter] [39m[31mGET /health/ready -> 503: database unavailable[39m
|
||||
[31m[Nest] 198 - [39m11/05/2026, 2:57:06 PM [31m ERROR[39m [38;5;3m[HttpExceptionFilter] [39m[31mGET /api/v1/chats -> 503: database unavailable[39m
|
||||
[31m[Nest] 198 - [39m11/05/2026, 2:57:06 PM [31m ERROR[39m [38;5;3m[HttpExceptionFilter] [39m[31mGET /api/v1/chats -> 503: database unavailable[39m
|
||||
[31m[Nest] 198 - [39m11/05/2026, 2:57:07 PM [31m ERROR[39m [38;5;3m[HttpExceptionFilter] [39m[31mPOST /api/v1/users -> 503: database unavailable[39m
|
||||
[31m[Nest] 198 - [39m11/05/2026, 2:57:07 PM [31m ERROR[39m [38;5;3m[HttpExceptionFilter] [39m[31mGET /api/v1/chats -> 500: Internal server error[39m
|
||||
PASS test/failures.e2e-spec.ts (25.184 s)
|
||||
Уровень 4: expiry, отказы зависимостей, rollback (e2e)
|
||||
✓ сессия истекает при коротком SESSION_TTL_SECONDS, в том числе из кэша (3886 ms)
|
||||
✓ без Redis вход и проверка сессии работают через PostgreSQL, logout — 503 без отзыва (512 ms)
|
||||
✓ logout через реплику без связи с Redis не создаёт расхождения с репликой, у которой сессия в кэше (533 ms)
|
||||
✓ без PostgreSQL отвечает ограниченным 503 и не пускает анонима (331 ms)
|
||||
✓ неожиданное исключение — 500 internal_error без деталей, с X-Request-Id и X-Instance-Id (131 ms)
|
||||
✓ откатывает создание чата целиком, если один из участников не существует (379 ms)
|
||||
|
||||
PASS test/idempotency.e2e-spec.ts (25.959 s)
|
||||
Идемпотентность отправки и X-Instance-Id (e2e)
|
||||
✓ требует Idempotency-Key правильного формата (1839 ms)
|
||||
✓ повтор с тем же ключом и телом возвращает тот же 201 и то же сообщение (828 ms)
|
||||
✓ тот же ключ с другим телом — 409, сообщение не создаётся (510 ms)
|
||||
✓ 20 параллельных запросов с одним ключом через два экземпляра создают ровно одно сообщение (662 ms)
|
||||
✓ область ключа — пользователь и чат: одинаковый ключ у разных пользователей и чатов независим (322 ms)
|
||||
✓ повтор заново проверяет сессию (122 ms)
|
||||
✓ после IDEMPOTENCY_TTL_SECONDS ключ можно использовать заново (1728 ms)
|
||||
✓ X-Instance-Id есть в успешных ответах, ошибках и health и стабилен в процессе (90 ms)
|
||||
|
||||
PASS test/attachment-access.e2e-spec.ts (26.189 s)
|
||||
Скачивание и привязка вложений (e2e)
|
||||
чтение
|
||||
✓ участник получает метаданные и оригинал побайтно (548 ms)
|
||||
✓ JPEG отдаётся с истинным Content-Type (108 ms)
|
||||
✓ посторонний и неизвестный ID получают одинаковый 404 (230 ms)
|
||||
✓ UUID nil корректного формата — 404, а не 400 (84 ms)
|
||||
✓ UUID v7 корректного формата — 404, а не 400 (120 ms)
|
||||
✓ без токена 401, неверный UUID 400 (100 ms)
|
||||
✓ variant: original явно, отсутствующий 404, неизвестный и повторённый 400 (177 ms)
|
||||
✓ нет доступа к S3 — метаданные 200, content 503 с заголовками (151 ms)
|
||||
✓ объект пропал из S3 — 500 без внутренних деталей (102 ms)
|
||||
attachment_ids в сообщениях
|
||||
✓ text="" с вложением принимается, Message читается вторым участником (101 ms)
|
||||
✓ порядок [B, A] сохраняется в ответе, истории и повторе; перестановка с тем же ключом — 409 (200 ms)
|
||||
✓ отсутствующий attachment_ids и [] равны при повторе, изменение списка — 409 (119 ms)
|
||||
✓ вложение другого чата и неизвестное — одинаковый 404, сообщение не создаётся (81 ms)
|
||||
✓ смесь своего и чужого вложения — 404 (109 ms)
|
||||
✓ посторонний не может отправить вложение в чужой чат (62 ms)
|
||||
✓ любой участник может сослаться на вложение чата, вложение можно использовать повторно (96 ms)
|
||||
✓ ровно 10 вложений принимается, 11 — 400 (405 ms)
|
||||
✓ UUID в другом регистре — тот же ID и дубль (112 ms)
|
||||
✓ text отсутствует — 400 (52 ms)
|
||||
✓ text null — 400 (46 ms)
|
||||
✓ attachment_ids null — 400 (75 ms)
|
||||
✓ attachment_ids строка — 400 (90 ms)
|
||||
✓ элемент не UUID — 400 (63 ms)
|
||||
✓ элемент число — 400 (76 ms)
|
||||
✓ дубликат — 400 (87 ms)
|
||||
✓ пустой текст без вложений — 400 (67 ms)
|
||||
✓ пробельный текст без вложений — 400 (84 ms)
|
||||
|
||||
PASS test/api.e2e-spec.ts (27.699 s)
|
||||
API мессенджера (e2e) — граничные случаи сверх публичного smoke
|
||||
POST /api/v1/users
|
||||
✓ отклоняет некорректный JSON с кодом 400 (147 ms)
|
||||
✓ отклоняет тело с лишними полями (107 ms)
|
||||
✓ отклоняет username с заглавными буквами (13 ms)
|
||||
✓ отклоняет display_name из одних пробелов (11 ms)
|
||||
✓ отклоняет дубликат username с кодом 409 (1905 ms)
|
||||
✓ никогда не возвращает поле пароля или его хэша (75 ms)
|
||||
✓ отклоняет username короче 3 символов (17 ms)
|
||||
✓ отклоняет username длиннее 32 символов (12 ms)
|
||||
✓ отклоняет username с недопустимыми символами (дефис) (10 ms)
|
||||
✓ принимает username ровно на границах длины 3 и 32 символа (217 ms)
|
||||
✓ отклоняет display_name длиннее 100 Unicode code points (12 ms)
|
||||
✓ принимает display_name ровно из 100 code points, считая эмодзи вне BMP за один символ (77 ms)
|
||||
✓ отклоняет display_name из 101 code point, состоящий из эмодзи вне BMP (29 ms)
|
||||
контроль доступа
|
||||
✓ возвращает 401 раньше 404, если и актор, и ресурс невалидны (31 ms)
|
||||
✓ возвращает 404 (не 403), когда чат запрашивает не его участник (816 ms)
|
||||
✓ отклоняет создание чата, если вызывающий не входит в member_ids (292 ms)
|
||||
✓ отклоняет создание чата с дублирующимися member_ids (137 ms)
|
||||
✓ считает один UUID в разном регистре дубликатом и узнаёт вызывающего в верхнем регистре (144 ms)
|
||||
✓ принимает ровно 100 участников и отклоняет 101 (213 ms)
|
||||
✓ отклоняет создание чата с неизвестным участником (108 ms)
|
||||
✓ отклоняет создание чата с пустым/пробельным title (103 ms)
|
||||
✓ возвращает 400 для синтаксически некорректного chat_id (не UUID) (98 ms)
|
||||
✓ возвращает 404 для корректного по формату, но несуществующего chat_id (96 ms)
|
||||
GET /api/v1/chats (пагинация)
|
||||
✓ использует limit=50 по умолчанию, если limit не передан (116 ms)
|
||||
✓ отклоняет нечисловой limit (101 ms)
|
||||
✓ отклоняет limit=0 (87 ms)
|
||||
✓ отклоняет повторённые limit и cursor (84 ms)
|
||||
✓ отклоняет cursor, не являющийся корректным base64url JSON (79 ms)
|
||||
✓ отклоняет корректно закодированный cursor с id чата, который никогда не существовал (79 ms)
|
||||
✓ обходит весь список чатов по страницам без пропусков и дублей (145 ms)
|
||||
сообщения
|
||||
✓ не принимает sender_id, переданный клиентом (180 ms)
|
||||
✓ считает attachment_ids: [] равным отсутствию поля, неизвестный ID — 404, неверный тип — 400 (229 ms)
|
||||
✓ отклоняет пустое сообщение (149 ms)
|
||||
✓ отклоняет сообщение из одних пробелов (177 ms)
|
||||
✓ отклоняет отправку в чат, где вызывающий не участник (288 ms)
|
||||
✓ отклоняет limit=0 при чтении истории (156 ms)
|
||||
✓ отклоняет limit=101 при чтении истории (120 ms)
|
||||
✓ отклоняет нечисловой limit (134 ms)
|
||||
✓ отклоняет текст сообщения длиннее 4000 Unicode code points (132 ms)
|
||||
✓ принимает текст сообщения ровно из 4000 Unicode code points (147 ms)
|
||||
✓ отклоняет текст сообщения свыше 4000 code points, состоящий из эмодзи вне BMP (129 ms)
|
||||
✓ отклоняет cursor, не являющийся корректным base64url JSON (128 ms)
|
||||
✓ отклоняет корректно закодированный cursor с id сообщения, которое никогда не существовало в этом чате (152 ms)
|
||||
✓ возвращает 400 для синтаксически некорректного chat_id при отправке сообщения (68 ms)
|
||||
✓ возвращает 400 для синтаксически некорректного chat_id при чтении истории (64 ms)
|
||||
✓ отклоняет cursor, взятый из другого чата (155 ms)
|
||||
✓ обходит всю историю по страницам без пропусков и дублей (180 ms)
|
||||
X-Request-Id
|
||||
✓ присутствует и уникален для независимых запросов (3 ms)
|
||||
✓ совпадает с request_id внутри тела ошибки (1 ms)
|
||||
|
||||
console.warn
|
||||
An error was encountered in a non-retryable streaming request.
|
||||
|
||||
150 | private async guard<T>(action: string, run: () => Promise<T>): Promise<T> {
|
||||
151 | try {
|
||||
> 152 | return await run();
|
||||
| ^
|
||||
153 | } catch (error) {
|
||||
154 | const reason = error instanceof Error ? error.name : 'unknown error';
|
||||
155 | this.logger.error(`${action} failed: ${reason}`);
|
||||
|
||||
at ../node_modules/@smithy/middleware-retry/dist-cjs/index.js:329:125
|
||||
at ../node_modules/@aws-sdk/middleware-flexible-checksums/dist-cjs/index.js:263:18
|
||||
at ../node_modules/@aws-sdk/middleware-sdk-s3/dist-cjs/index.js:110:22
|
||||
at ../node_modules/@aws-sdk/middleware-sdk-s3/dist-cjs/index.js:138:14
|
||||
at ../node_modules/@aws-sdk/middleware-logger/dist-cjs/index.js:34:22
|
||||
at S3Service.guard (../src/storage/s3.service.ts:152:11)
|
||||
at S3Service.putFile (../src/storage/s3.service.ts:64:3)
|
||||
at AttachmentsService.store (../src/attachments/attachments.service.ts:117:3)
|
||||
at AttachmentsService.upload (../src/attachments/attachments.service.ts:63:11)
|
||||
at AttachmentsController.upload (../src/attachments/attachments.controller.ts:36:22)
|
||||
at ../node_modules/@nestjs/core/router/router-execution-context.js:46:28
|
||||
at Object.<anonymous> (../node_modules/@nestjs/core/router/router-proxy.js:9:17)
|
||||
|
||||
[31m[Nest] 173 - [39m11/05/2026, 2:57:12 PM [31m ERROR[39m [38;5;3m[S3Service] [39m[31mput attachments/f46507e6-91ef-4f05-bf5a-f0e651cda999 failed: Error[39m
|
||||
[31m[Nest] 173 - [39m11/05/2026, 2:57:12 PM [31m ERROR[39m [38;5;3m[HttpExceptionFilter] [39m[31mPOST /api/v1/chats/1c1a88b5-7763-4bf7-a7d4-d2039f139c42/attachments?filename=a.png -> 503: object storage unavailable[39m
|
||||
[31m[Nest] 173 - [39m11/05/2026, 2:57:12 PM [31m ERROR[39m [38;5;3m[HttpExceptionFilter] [39m[31mPOST /api/v1/chats/1c1a88b5-7763-4bf7-a7d4-d2039f139c42/attachments?filename=sample.png -> 500: Internal server error[39m
|
||||
[31m[Nest] 173 - [39m11/05/2026, 2:57:12 PM [31m ERROR[39m [38;5;3m[AttachmentsService] [39m[31mmetadata insert failed for attachments/d29e41f3-cb2b-46a7-ba25-18865f8801ae[39m
|
||||
[31m[Nest] 173 - [39m11/05/2026, 2:57:12 PM [31m ERROR[39m [38;5;3m[HttpExceptionFilter] [39m[31mPOST /api/v1/chats/1c1a88b5-7763-4bf7-a7d4-d2039f139c42/attachments?filename=sample.png -> 503: database unavailable[39m
|
||||
[31m[Nest] 173 - [39m11/05/2026, 2:57:12 PM [31m ERROR[39m [38;5;3m[HttpExceptionFilter] [39m[31mPOST /api/v1/chats/1c1a88b5-7763-4bf7-a7d4-d2039f139c42/attachments?filename=sample.png -> 503: database unavailable[39m
|
||||
PASS test/attachments.e2e-spec.ts (29.982 s)
|
||||
Загрузка вложений (e2e)
|
||||
успешная загрузка
|
||||
✓ возвращает 201 Attachment по контракту и сохраняет метаданные (848 ms)
|
||||
✓ принимает реальный JPEG (118 ms)
|
||||
✓ игнорирует параметры и регистр Content-Type (112 ms)
|
||||
✓ принимает файл ровно 10 MiB, filename ровно 255 символов и PNG с байтами после IEND (1064 ms)
|
||||
✓ принимает ровно 40 млн пикселей (3730 ms)
|
||||
порядок проверок и доступ
|
||||
✓ без токена — 401 раньше любых проверок содержимого (17 ms)
|
||||
✓ посторонний и несуществующий чат — одинаковый 404, чужой файл не сохраняется (15 ms)
|
||||
✓ неверный UUID чата — 400 (3 ms)
|
||||
filename
|
||||
✓ отсутствует — 400 (5 ms)
|
||||
✓ пустой — 400 (6 ms)
|
||||
✓ только пробелы — 400 (6 ms)
|
||||
✓ 256 символов — 400 (4 ms)
|
||||
✓ путь через / — 400 (4 ms)
|
||||
✓ путь через \ — 400 (3 ms)
|
||||
✓ NUL — 400 (3 ms)
|
||||
✓ перевод строки — 400 (4 ms)
|
||||
✓ DEL — 400 (4 ms)
|
||||
✓ C1 U+0085 — 400 (3 ms)
|
||||
✓ разделитель строк U+2028 — 400 (3 ms)
|
||||
✓ повторённый параметр — 400 (3 ms)
|
||||
тип содержимого
|
||||
✓ Content-Type text/plain — 415 (3 ms)
|
||||
✓ Content-Type application/json — 415 (26 ms)
|
||||
✓ Content-Type application/octet-stream — 415 (4 ms)
|
||||
✓ Content-Type multipart/form-data; boundary=x — 415 (5 ms)
|
||||
✓ Content-Type image/gif — 415 (4 ms)
|
||||
✓ Content-Type image/webp — 415 (3 ms)
|
||||
✓ без Content-Type — 415 (3 ms)
|
||||
✓ подмена MIME в обе стороны и чужие форматы — 415 (28 ms)
|
||||
✓ пустое тело — 400 (8 ms)
|
||||
повреждённые изображения
|
||||
✓ PNG: первые 33 байта sample.png — 415 (5 ms)
|
||||
✓ PNG: половина файла — 415 (5 ms)
|
||||
✓ PNG: без последних 20 байт — 415 (5 ms)
|
||||
✓ PNG: без IEND — 415 (4 ms)
|
||||
✓ PNG: неверный CRC — 415 (8 ms)
|
||||
✓ PNG: фильтр строки 5 — 415 (5 ms)
|
||||
✓ PNG: фильтр строки 255 — 415 (6 ms)
|
||||
✓ PNG: неверный фильтр в последней строке — 415 (6 ms)
|
||||
✓ PNG: мусор в IDAT с верным CRC — 415 (5 ms)
|
||||
✓ PNG: данных меньше, чем строк — 415 (7 ms)
|
||||
✓ PNG: палитра на 257 записей — 415 (6 ms)
|
||||
✓ JPEG: все повреждения — 415 (34 ms)
|
||||
лимиты
|
||||
✓ 10 MiB + 1 байт — 413 и без чтения объявленного тела (250 ms)
|
||||
✓ превышение при chunked без Content-Length — 413 (251 ms)
|
||||
✓ превышение лимита пикселей — 413 до декодирования (18 ms)
|
||||
✓ оборванная загрузка не оставляет временных файлов и строк в БД (804 ms)
|
||||
отказы
|
||||
✓ S3 недоступен — 503 с заголовками, метаданные не создаются (109 ms)
|
||||
✓ явный отказ INSERT после PUT удаляет объект (31 ms)
|
||||
✓ необработанная ошибка PostgreSQL при INSERT — 503, объект не удаляется (22 ms)
|
||||
✓ при неясном исходе COMMIT объект не удаляется (23 ms)
|
||||
|
||||
A worker process has failed to exit gracefully and has been force exited. This is likely caused by tests leaking due to improper teardown. Try running with --detectOpenHandles to find leaks. Active timers can also cause this, ensure that .unref() was called on them.
|
||||
Test Suites: 9 passed, 9 total
|
||||
Tests: 165 passed, 165 total
|
||||
Snapshots: 0 total
|
||||
Time: 31.217 s
|
||||
Ran all test suites.
|
||||
|
||||
@@ -0,0 +1,536 @@
|
||||
{
|
||||
"date": "2026-11-05T14:06:08Z",
|
||||
"api_replica_limits": {
|
||||
"cpus": 0.5,
|
||||
"memory_mib": 256
|
||||
},
|
||||
"rows_after_attachments_messages": "290|264",
|
||||
"results": [
|
||||
{
|
||||
"profile": "sample 112 KiB x10",
|
||||
"body_bytes": 111817,
|
||||
"content_type": "image/png",
|
||||
"concurrency": 10,
|
||||
"rounds": 3,
|
||||
"requests": 30,
|
||||
"statuses": {
|
||||
"201": 30
|
||||
},
|
||||
"wall_seconds": 2.02,
|
||||
"successful_rps": 14.83,
|
||||
"error_rate": 0.0,
|
||||
"latency_seconds": {
|
||||
"p50": 0.488,
|
||||
"p95": 0.707,
|
||||
"p99": 0.811
|
||||
},
|
||||
"sha256_in_responses_ok": true,
|
||||
"download_sha256_ok": true,
|
||||
"replicas": {
|
||||
"bb4424f819bb": {
|
||||
"idle_rss_mib": 104,
|
||||
"sampled_peak_rss_mib": 118,
|
||||
"vm_hwm_mib": 118,
|
||||
"cgroup_max_usage_with_page_cache_mib": 73,
|
||||
"oom_killed": "false",
|
||||
"temp_dirs_left": 0,
|
||||
"served": 15
|
||||
},
|
||||
"0900a35b199d": {
|
||||
"idle_rss_mib": 103,
|
||||
"sampled_peak_rss_mib": 113,
|
||||
"vm_hwm_mib": 113,
|
||||
"cgroup_max_usage_with_page_cache_mib": 64,
|
||||
"oom_killed": "false",
|
||||
"temp_dirs_left": 0,
|
||||
"served": 15
|
||||
}
|
||||
},
|
||||
"docker_stats": {
|
||||
"messenger-lab4-tools-run-3ba5514e0d22": {
|
||||
"max_cpu_percent": 4.55,
|
||||
"last_mem": "121.2MiB"
|
||||
},
|
||||
"messenger-lab4-lb-1": {
|
||||
"max_cpu_percent": 1.91,
|
||||
"last_mem": "12.31MiB"
|
||||
},
|
||||
"messenger-lab4-api-1": {
|
||||
"max_cpu_percent": 40.14,
|
||||
"last_mem": "64.27MiB"
|
||||
},
|
||||
"messenger-lab4-api-2": {
|
||||
"max_cpu_percent": 43.73,
|
||||
"last_mem": "56.66MiB"
|
||||
},
|
||||
"messenger-lab4-reconciler-1": {
|
||||
"max_cpu_percent": 0,
|
||||
"last_mem": "29.14MiB"
|
||||
},
|
||||
"messenger-lab4-redis-1": {
|
||||
"max_cpu_percent": 0.55,
|
||||
"last_mem": "8.105MiB"
|
||||
},
|
||||
"messenger-lab4-postgres-1": {
|
||||
"max_cpu_percent": 5.16,
|
||||
"last_mem": "34.39MiB"
|
||||
},
|
||||
"messenger-lab4-s3-1": {
|
||||
"max_cpu_percent": 5.51,
|
||||
"last_mem": "68.39MiB"
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"profile": "png 10 MiB x1",
|
||||
"body_bytes": 10485760,
|
||||
"content_type": "image/png",
|
||||
"concurrency": 1,
|
||||
"rounds": 3,
|
||||
"requests": 3,
|
||||
"statuses": {
|
||||
"201": 3
|
||||
},
|
||||
"wall_seconds": 1.8,
|
||||
"successful_rps": 1.67,
|
||||
"error_rate": 0.0,
|
||||
"latency_seconds": {
|
||||
"p50": 0.69,
|
||||
"p95": 0.742,
|
||||
"p99": 0.742
|
||||
},
|
||||
"sha256_in_responses_ok": true,
|
||||
"download_sha256_ok": true,
|
||||
"replicas": {
|
||||
"bb4424f819bb": {
|
||||
"idle_rss_mib": 103,
|
||||
"sampled_peak_rss_mib": 112,
|
||||
"vm_hwm_mib": 121,
|
||||
"cgroup_max_usage_with_page_cache_mib": 74,
|
||||
"oom_killed": "false",
|
||||
"temp_dirs_left": 0,
|
||||
"served": 1
|
||||
},
|
||||
"0900a35b199d": {
|
||||
"idle_rss_mib": 105,
|
||||
"sampled_peak_rss_mib": 122,
|
||||
"vm_hwm_mib": 122,
|
||||
"cgroup_max_usage_with_page_cache_mib": 77,
|
||||
"oom_killed": "false",
|
||||
"temp_dirs_left": 0,
|
||||
"served": 2
|
||||
}
|
||||
},
|
||||
"docker_stats": {
|
||||
"messenger-lab4-tools-run-3ba5514e0d22": {
|
||||
"max_cpu_percent": 0.75,
|
||||
"last_mem": "119.3MiB"
|
||||
},
|
||||
"messenger-lab4-lb-1": {
|
||||
"max_cpu_percent": 0.55,
|
||||
"last_mem": "12.31MiB"
|
||||
},
|
||||
"messenger-lab4-api-1": {
|
||||
"max_cpu_percent": 12.44,
|
||||
"last_mem": "57.14MiB"
|
||||
},
|
||||
"messenger-lab4-api-2": {
|
||||
"max_cpu_percent": 24.71,
|
||||
"last_mem": "67.43MiB"
|
||||
},
|
||||
"messenger-lab4-reconciler-1": {
|
||||
"max_cpu_percent": 3.9,
|
||||
"last_mem": "30.21MiB"
|
||||
},
|
||||
"messenger-lab4-redis-1": {
|
||||
"max_cpu_percent": 0.47,
|
||||
"last_mem": "8.23MiB"
|
||||
},
|
||||
"messenger-lab4-postgres-1": {
|
||||
"max_cpu_percent": 1.02,
|
||||
"last_mem": "32MiB"
|
||||
},
|
||||
"messenger-lab4-s3-1": {
|
||||
"max_cpu_percent": 5.27,
|
||||
"last_mem": "87.09MiB"
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"profile": "png 10 MiB x10",
|
||||
"body_bytes": 10485760,
|
||||
"content_type": "image/png",
|
||||
"concurrency": 10,
|
||||
"rounds": 3,
|
||||
"requests": 30,
|
||||
"statuses": {
|
||||
"201": 30
|
||||
},
|
||||
"wall_seconds": 9.58,
|
||||
"successful_rps": 3.13,
|
||||
"error_rate": 0.0,
|
||||
"latency_seconds": {
|
||||
"p50": 2.945,
|
||||
"p95": 3.77,
|
||||
"p99": 3.771
|
||||
},
|
||||
"sha256_in_responses_ok": true,
|
||||
"download_sha256_ok": true,
|
||||
"replicas": {
|
||||
"bb4424f819bb": {
|
||||
"idle_rss_mib": 103,
|
||||
"sampled_peak_rss_mib": 142,
|
||||
"vm_hwm_mib": 142,
|
||||
"cgroup_max_usage_with_page_cache_mib": 145,
|
||||
"oom_killed": "false",
|
||||
"temp_dirs_left": 0,
|
||||
"served": 15
|
||||
},
|
||||
"0900a35b199d": {
|
||||
"idle_rss_mib": 105,
|
||||
"sampled_peak_rss_mib": 143,
|
||||
"vm_hwm_mib": 144,
|
||||
"cgroup_max_usage_with_page_cache_mib": 139,
|
||||
"oom_killed": "false",
|
||||
"temp_dirs_left": 0,
|
||||
"served": 15
|
||||
}
|
||||
},
|
||||
"docker_stats": {
|
||||
"messenger-lab4-tools-run-3ba5514e0d22": {
|
||||
"max_cpu_percent": 8.22,
|
||||
"last_mem": "103.6MiB"
|
||||
},
|
||||
"messenger-lab4-lb-1": {
|
||||
"max_cpu_percent": 9.11,
|
||||
"last_mem": "12.32MiB"
|
||||
},
|
||||
"messenger-lab4-api-1": {
|
||||
"max_cpu_percent": 54.38,
|
||||
"last_mem": "85.79MiB"
|
||||
},
|
||||
"messenger-lab4-api-2": {
|
||||
"max_cpu_percent": 54.51,
|
||||
"last_mem": "87.57MiB"
|
||||
},
|
||||
"messenger-lab4-reconciler-1": {
|
||||
"max_cpu_percent": 0,
|
||||
"last_mem": "30.12MiB"
|
||||
},
|
||||
"messenger-lab4-redis-1": {
|
||||
"max_cpu_percent": 3.06,
|
||||
"last_mem": "8.234MiB"
|
||||
},
|
||||
"messenger-lab4-postgres-1": {
|
||||
"max_cpu_percent": 5.81,
|
||||
"last_mem": "44.02MiB"
|
||||
},
|
||||
"messenger-lab4-s3-1": {
|
||||
"max_cpu_percent": 46.28,
|
||||
"last_mem": "147.5MiB"
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"profile": "png 40 MP x10",
|
||||
"body_bytes": 126786,
|
||||
"content_type": "image/png",
|
||||
"concurrency": 10,
|
||||
"rounds": 3,
|
||||
"requests": 30,
|
||||
"statuses": {
|
||||
"201": 30
|
||||
},
|
||||
"wall_seconds": 89.21,
|
||||
"successful_rps": 0.34,
|
||||
"error_rate": 0.0,
|
||||
"latency_seconds": {
|
||||
"p50": 23.187,
|
||||
"p95": 33.475,
|
||||
"p99": 33.514
|
||||
},
|
||||
"sha256_in_responses_ok": true,
|
||||
"download_sha256_ok": true,
|
||||
"replicas": {
|
||||
"bb4424f819bb": {
|
||||
"idle_rss_mib": 104,
|
||||
"sampled_peak_rss_mib": 152,
|
||||
"vm_hwm_mib": 153,
|
||||
"cgroup_max_usage_with_page_cache_mib": 256,
|
||||
"oom_killed": "false",
|
||||
"temp_dirs_left": 0,
|
||||
"served": 15
|
||||
},
|
||||
"0900a35b199d": {
|
||||
"idle_rss_mib": 104,
|
||||
"sampled_peak_rss_mib": 160,
|
||||
"vm_hwm_mib": 163,
|
||||
"cgroup_max_usage_with_page_cache_mib": 256,
|
||||
"oom_killed": "false",
|
||||
"temp_dirs_left": 0,
|
||||
"served": 15
|
||||
}
|
||||
},
|
||||
"docker_stats": {
|
||||
"messenger-lab4-tools-run-3ba5514e0d22": {
|
||||
"max_cpu_percent": 2.34,
|
||||
"last_mem": "111.8MiB"
|
||||
},
|
||||
"messenger-lab4-lb-1": {
|
||||
"max_cpu_percent": 1.26,
|
||||
"last_mem": "12.33MiB"
|
||||
},
|
||||
"messenger-lab4-api-1": {
|
||||
"max_cpu_percent": 55.41,
|
||||
"last_mem": "66.35MiB"
|
||||
},
|
||||
"messenger-lab4-api-2": {
|
||||
"max_cpu_percent": 54.7,
|
||||
"last_mem": "67.4MiB"
|
||||
},
|
||||
"messenger-lab4-reconciler-1": {
|
||||
"max_cpu_percent": 0,
|
||||
"last_mem": "30.14MiB"
|
||||
},
|
||||
"messenger-lab4-redis-1": {
|
||||
"max_cpu_percent": 8.95,
|
||||
"last_mem": "8.105MiB"
|
||||
},
|
||||
"messenger-lab4-postgres-1": {
|
||||
"max_cpu_percent": 5.1,
|
||||
"last_mem": "45MiB"
|
||||
},
|
||||
"messenger-lab4-s3-1": {
|
||||
"max_cpu_percent": 7.63,
|
||||
"last_mem": "160.1MiB"
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"profile": "jpeg 40 MP x10",
|
||||
"body_bytes": 235018,
|
||||
"content_type": "image/jpeg",
|
||||
"concurrency": 10,
|
||||
"rounds": 3,
|
||||
"requests": 30,
|
||||
"statuses": {
|
||||
"201": 30
|
||||
},
|
||||
"wall_seconds": 86.77,
|
||||
"successful_rps": 0.35,
|
||||
"error_rate": 0.0,
|
||||
"latency_seconds": {
|
||||
"p50": 21.567,
|
||||
"p95": 29.473,
|
||||
"p99": 29.817
|
||||
},
|
||||
"sha256_in_responses_ok": true,
|
||||
"download_sha256_ok": true,
|
||||
"replicas": {
|
||||
"bb4424f819bb": {
|
||||
"idle_rss_mib": 103,
|
||||
"sampled_peak_rss_mib": 150,
|
||||
"vm_hwm_mib": 158,
|
||||
"cgroup_max_usage_with_page_cache_mib": 256,
|
||||
"oom_killed": "false",
|
||||
"temp_dirs_left": 0,
|
||||
"served": 15
|
||||
},
|
||||
"0900a35b199d": {
|
||||
"idle_rss_mib": 104,
|
||||
"sampled_peak_rss_mib": 160,
|
||||
"vm_hwm_mib": 163,
|
||||
"cgroup_max_usage_with_page_cache_mib": 256,
|
||||
"oom_killed": "false",
|
||||
"temp_dirs_left": 0,
|
||||
"served": 15
|
||||
}
|
||||
},
|
||||
"docker_stats": {
|
||||
"messenger-lab4-tools-run-3ba5514e0d22": {
|
||||
"max_cpu_percent": 1.93,
|
||||
"last_mem": "102MiB"
|
||||
},
|
||||
"messenger-lab4-lb-1": {
|
||||
"max_cpu_percent": 0.61,
|
||||
"last_mem": "12.42MiB"
|
||||
},
|
||||
"messenger-lab4-api-1": {
|
||||
"max_cpu_percent": 54.66,
|
||||
"last_mem": "48.4MiB"
|
||||
},
|
||||
"messenger-lab4-api-2": {
|
||||
"max_cpu_percent": 55.35,
|
||||
"last_mem": "58.52MiB"
|
||||
},
|
||||
"messenger-lab4-reconciler-1": {
|
||||
"max_cpu_percent": 0,
|
||||
"last_mem": "30.15MiB"
|
||||
},
|
||||
"messenger-lab4-redis-1": {
|
||||
"max_cpu_percent": 3.8,
|
||||
"last_mem": "8.105MiB"
|
||||
},
|
||||
"messenger-lab4-postgres-1": {
|
||||
"max_cpu_percent": 3.13,
|
||||
"last_mem": "45.41MiB"
|
||||
},
|
||||
"messenger-lab4-s3-1": {
|
||||
"max_cpu_percent": 5.96,
|
||||
"last_mem": "161.7MiB"
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"profile": "png 10 MiB x20",
|
||||
"body_bytes": 10485760,
|
||||
"content_type": "image/png",
|
||||
"concurrency": 20,
|
||||
"rounds": 3,
|
||||
"requests": 60,
|
||||
"statuses": {
|
||||
"201": 60
|
||||
},
|
||||
"wall_seconds": 16.62,
|
||||
"successful_rps": 3.61,
|
||||
"error_rate": 0.0,
|
||||
"latency_seconds": {
|
||||
"p50": 4.453,
|
||||
"p95": 6.507,
|
||||
"p99": 6.588
|
||||
},
|
||||
"sha256_in_responses_ok": true,
|
||||
"download_sha256_ok": true,
|
||||
"replicas": {
|
||||
"bb4424f819bb": {
|
||||
"idle_rss_mib": 106,
|
||||
"sampled_peak_rss_mib": 151,
|
||||
"vm_hwm_mib": 151,
|
||||
"cgroup_max_usage_with_page_cache_mib": 199,
|
||||
"oom_killed": "false",
|
||||
"temp_dirs_left": 0,
|
||||
"served": 30
|
||||
},
|
||||
"0900a35b199d": {
|
||||
"idle_rss_mib": 104,
|
||||
"sampled_peak_rss_mib": 148,
|
||||
"vm_hwm_mib": 148,
|
||||
"cgroup_max_usage_with_page_cache_mib": 194,
|
||||
"oom_killed": "false",
|
||||
"temp_dirs_left": 0,
|
||||
"served": 30
|
||||
}
|
||||
},
|
||||
"docker_stats": {
|
||||
"messenger-lab4-tools-run-3ba5514e0d22": {
|
||||
"max_cpu_percent": 1.92,
|
||||
"last_mem": "104MiB"
|
||||
},
|
||||
"messenger-lab4-lb-1": {
|
||||
"max_cpu_percent": 12.15,
|
||||
"last_mem": "12.68MiB"
|
||||
},
|
||||
"messenger-lab4-api-1": {
|
||||
"max_cpu_percent": 54.25,
|
||||
"last_mem": "96.46MiB"
|
||||
},
|
||||
"messenger-lab4-api-2": {
|
||||
"max_cpu_percent": 54.62,
|
||||
"last_mem": "91.93MiB"
|
||||
},
|
||||
"messenger-lab4-reconciler-1": {
|
||||
"max_cpu_percent": 0,
|
||||
"last_mem": "30.15MiB"
|
||||
},
|
||||
"messenger-lab4-redis-1": {
|
||||
"max_cpu_percent": 3.07,
|
||||
"last_mem": "8.23MiB"
|
||||
},
|
||||
"messenger-lab4-postgres-1": {
|
||||
"max_cpu_percent": 4.49,
|
||||
"last_mem": "47.98MiB"
|
||||
},
|
||||
"messenger-lab4-s3-1": {
|
||||
"max_cpu_percent": 49.68,
|
||||
"last_mem": "174.5MiB"
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"profile": "png 10 MiB x40 (выше лимита 16 на реплику)",
|
||||
"body_bytes": 10485760,
|
||||
"content_type": "image/png",
|
||||
"concurrency": 40,
|
||||
"rounds": 3,
|
||||
"requests": 120,
|
||||
"statuses": {
|
||||
"503": 24,
|
||||
"201": 96
|
||||
},
|
||||
"wall_seconds": 24.63,
|
||||
"successful_rps": 3.9,
|
||||
"error_rate": 0.2,
|
||||
"latency_seconds": {
|
||||
"p50": 7.862,
|
||||
"p95": 8.374,
|
||||
"p99": 8.399
|
||||
},
|
||||
"sha256_in_responses_ok": true,
|
||||
"download_sha256_ok": true,
|
||||
"replicas": {
|
||||
"bb4424f819bb": {
|
||||
"idle_rss_mib": 104,
|
||||
"sampled_peak_rss_mib": 156,
|
||||
"vm_hwm_mib": 156,
|
||||
"cgroup_max_usage_with_page_cache_mib": 256,
|
||||
"oom_killed": "false",
|
||||
"temp_dirs_left": 0,
|
||||
"served": 60
|
||||
},
|
||||
"0900a35b199d": {
|
||||
"idle_rss_mib": 102,
|
||||
"sampled_peak_rss_mib": 150,
|
||||
"vm_hwm_mib": 150,
|
||||
"cgroup_max_usage_with_page_cache_mib": 256,
|
||||
"oom_killed": "false",
|
||||
"temp_dirs_left": 0,
|
||||
"served": 60
|
||||
}
|
||||
},
|
||||
"docker_stats": {
|
||||
"messenger-lab4-tools-run-3ba5514e0d22": {
|
||||
"max_cpu_percent": 15.56,
|
||||
"last_mem": "122.4MiB"
|
||||
},
|
||||
"messenger-lab4-lb-1": {
|
||||
"max_cpu_percent": 26.52,
|
||||
"last_mem": "13.5MiB"
|
||||
},
|
||||
"messenger-lab4-api-1": {
|
||||
"max_cpu_percent": 52.45,
|
||||
"last_mem": "98.35MiB"
|
||||
},
|
||||
"messenger-lab4-api-2": {
|
||||
"max_cpu_percent": 52.64,
|
||||
"last_mem": "91.67MiB"
|
||||
},
|
||||
"messenger-lab4-reconciler-1": {
|
||||
"max_cpu_percent": 0,
|
||||
"last_mem": "30.15MiB"
|
||||
},
|
||||
"messenger-lab4-redis-1": {
|
||||
"max_cpu_percent": 3.46,
|
||||
"last_mem": "8.105MiB"
|
||||
},
|
||||
"messenger-lab4-postgres-1": {
|
||||
"max_cpu_percent": 7.07,
|
||||
"last_mem": "65.05MiB"
|
||||
},
|
||||
"messenger-lab4-s3-1": {
|
||||
"max_cpu_percent": 49.36,
|
||||
"last_mem": "248.1MiB"
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
global
|
||||
log stdout format raw local0 info
|
||||
maxconn 2000
|
||||
|
||||
defaults
|
||||
mode http
|
||||
log global
|
||||
option httplog
|
||||
option dontlognull
|
||||
timeout connect 1s
|
||||
timeout client 30s
|
||||
timeout server 10s
|
||||
timeout http-request 10s
|
||||
timeout queue 5s
|
||||
timeout check 1s
|
||||
retries 2
|
||||
option redispatch 1
|
||||
retry-on conn-failure
|
||||
unique-id-format %[uuid()]
|
||||
http-error status 502 content-type application/json hdr X-Request-Id "%[unique-id]" lf-string "{\"error\":{\"code\":\"unavailable\",\"message\":\"bad gateway\",\"request_id\":\"%[unique-id]\"}}"
|
||||
http-error status 503 content-type application/json hdr X-Request-Id "%[unique-id]" lf-string "{\"error\":{\"code\":\"unavailable\",\"message\":\"no API replica available\",\"request_id\":\"%[unique-id]\"}}"
|
||||
http-error status 504 content-type application/json hdr X-Request-Id "%[unique-id]" lf-string "{\"error\":{\"code\":\"unavailable\",\"message\":\"gateway timeout\",\"request_id\":\"%[unique-id]\"}}"
|
||||
|
||||
resolvers docker
|
||||
nameserver dns 127.0.0.11:53
|
||||
resolve_retries 3
|
||||
timeout resolve 1s
|
||||
timeout retry 1s
|
||||
hold valid 5s
|
||||
hold obsolete 5s
|
||||
accepted_payload_size 8192
|
||||
|
||||
frontend public
|
||||
bind :8080
|
||||
default_backend api
|
||||
|
||||
backend api
|
||||
balance roundrobin
|
||||
http-request set-timeout server 60s if { method POST } { path_reg ^/api/v1/chats/[^/]+/attachments$ }
|
||||
option httpchk GET /health/core
|
||||
http-check expect status 200
|
||||
default-server check inter 1s fall 2 rise 2 resolvers docker resolve-prefer ipv4 init-addr none
|
||||
server-template api 1-4 api:8080
|
||||
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"$schema": "https://json.schemastore.org/nest-cli",
|
||||
"collection": "@nestjs/schematics",
|
||||
"sourceRoot": "src",
|
||||
"compilerOptions": {
|
||||
"deleteOutDir": true
|
||||
}
|
||||
}
|
||||
Generated
+12222
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,52 @@
|
||||
{
|
||||
"name": "mtusi-messenger-lab4",
|
||||
"version": "1.0.0",
|
||||
"description": "Lab 4: messenger API with S3 attachments (NestJS + Fastify)",
|
||||
"private": true,
|
||||
"license": "UNLICENSED",
|
||||
"scripts": {
|
||||
"build": "nest build",
|
||||
"start": "node dist/main.js",
|
||||
"start:dev": "nest start --watch",
|
||||
"lint": "eslint \"{src,test}/**/*.ts\"",
|
||||
"test": "jest --config ./test/jest-e2e.json",
|
||||
"test:watch": "jest --config ./test/jest-e2e.json --watch"
|
||||
},
|
||||
"dependencies": {
|
||||
"@aws-sdk/client-s3": "3.700.0",
|
||||
"@fastify/static": "7.0.4",
|
||||
"@nestjs/common": "^10.4.15",
|
||||
"@nestjs/core": "^10.4.15",
|
||||
"@nestjs/platform-fastify": "^10.4.15",
|
||||
"@nestjs/swagger": "7.4.2",
|
||||
"@prisma/client": "6.1.0",
|
||||
"argon2": "0.41.1",
|
||||
"class-transformer": "^0.5.1",
|
||||
"class-validator": "^0.14.1",
|
||||
"ioredis": "5.4.1",
|
||||
"reflect-metadata": "^0.2.2",
|
||||
"rxjs": "^7.8.1",
|
||||
"sharp": "0.33.5"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@nestjs/cli": "^10.4.9",
|
||||
"@nestjs/schematics": "^10.2.3",
|
||||
"@nestjs/testing": "^10.4.15",
|
||||
"@types/jest": "^29.5.14",
|
||||
"@types/node": "^20.17.9",
|
||||
"@types/supertest": "^6.0.2",
|
||||
"@typescript-eslint/eslint-plugin": "^8.18.1",
|
||||
"@typescript-eslint/parser": "^8.18.1",
|
||||
"ajv": "8.12.0",
|
||||
"eslint": "^8.57.1",
|
||||
"jest": "^29.7.0",
|
||||
"prisma": "6.1.0",
|
||||
"source-map-support": "^0.5.21",
|
||||
"supertest": "^7.0.0",
|
||||
"ts-jest": "^29.2.5",
|
||||
"ts-loader": "^9.5.1",
|
||||
"ts-node": "^10.9.2",
|
||||
"tsconfig-paths": "^4.2.0",
|
||||
"typescript": "^5.7.2"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
CREATE TABLE "users" (
|
||||
"id" UUID NOT NULL,
|
||||
"username" VARCHAR(32) NOT NULL,
|
||||
"display_name" VARCHAR(100) NOT NULL,
|
||||
"created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
|
||||
CONSTRAINT "users_pkey" PRIMARY KEY ("id"),
|
||||
CONSTRAINT "users_username_format" CHECK ("username" ~ '^[a-z0-9_]{3,32}$'),
|
||||
CONSTRAINT "users_display_name_not_blank" CHECK (btrim("display_name") <> '')
|
||||
);
|
||||
|
||||
CREATE TABLE "chats" (
|
||||
"id" UUID NOT NULL,
|
||||
"title" VARCHAR(100) NOT NULL,
|
||||
"created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
|
||||
CONSTRAINT "chats_pkey" PRIMARY KEY ("id"),
|
||||
CONSTRAINT "chats_title_not_blank" CHECK (btrim("title") <> '')
|
||||
);
|
||||
|
||||
CREATE TABLE "chat_members" (
|
||||
"chat_id" UUID NOT NULL,
|
||||
"user_id" UUID NOT NULL,
|
||||
|
||||
CONSTRAINT "chat_members_pkey" PRIMARY KEY ("chat_id","user_id")
|
||||
);
|
||||
|
||||
CREATE TABLE "messages" (
|
||||
"id" UUID NOT NULL,
|
||||
"chat_id" UUID NOT NULL,
|
||||
"sender_id" UUID NOT NULL,
|
||||
"text" VARCHAR(4000) NOT NULL,
|
||||
"created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
|
||||
CONSTRAINT "messages_pkey" PRIMARY KEY ("id")
|
||||
);
|
||||
|
||||
CREATE UNIQUE INDEX "users_username_key" ON "users"("username");
|
||||
|
||||
CREATE INDEX "chat_members_user_id_idx" ON "chat_members"("user_id");
|
||||
|
||||
CREATE INDEX "messages_chat_id_created_at_id_idx" ON "messages"("chat_id", "created_at", "id");
|
||||
|
||||
ALTER TABLE "chat_members" ADD CONSTRAINT "chat_members_chat_id_fkey" FOREIGN KEY ("chat_id") REFERENCES "chats"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
|
||||
ALTER TABLE "chat_members" ADD CONSTRAINT "chat_members_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
|
||||
|
||||
ALTER TABLE "messages" ADD CONSTRAINT "messages_chat_id_fkey" FOREIGN KEY ("chat_id") REFERENCES "chats"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
|
||||
ALTER TABLE "messages" ADD CONSTRAINT "messages_sender_id_fkey" FOREIGN KEY ("sender_id") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
|
||||
@@ -0,0 +1,14 @@
|
||||
ALTER TABLE "users" ADD COLUMN "password_hash" TEXT NOT NULL;
|
||||
|
||||
CREATE TABLE "sessions" (
|
||||
"token_hash" CHAR(64) NOT NULL,
|
||||
"user_id" UUID NOT NULL,
|
||||
"expires_at" TIMESTAMPTZ(3) NOT NULL,
|
||||
"created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
|
||||
CONSTRAINT "sessions_pkey" PRIMARY KEY ("token_hash")
|
||||
);
|
||||
|
||||
CREATE INDEX "sessions_user_id_idx" ON "sessions"("user_id");
|
||||
|
||||
ALTER TABLE "sessions" ADD CONSTRAINT "sessions_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
@@ -0,0 +1,13 @@
|
||||
CREATE TABLE "idempotency_keys" (
|
||||
"user_id" UUID NOT NULL,
|
||||
"chat_id" UUID NOT NULL,
|
||||
"key" VARCHAR(128) NOT NULL,
|
||||
"request_hash" CHAR(64) NOT NULL,
|
||||
"message_id" UUID NOT NULL,
|
||||
"created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
|
||||
CONSTRAINT "idempotency_keys_pkey" PRIMARY KEY ("user_id","chat_id","key"),
|
||||
CONSTRAINT "idempotency_keys_key_format" CHECK ("key" ~ '^[A-Za-z0-9._:-]{1,128}$')
|
||||
);
|
||||
|
||||
ALTER TABLE "idempotency_keys" ADD CONSTRAINT "idempotency_keys_message_id_fkey" FOREIGN KEY ("message_id") REFERENCES "messages"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
@@ -0,0 +1,45 @@
|
||||
CREATE TABLE "attachments" (
|
||||
"id" UUID NOT NULL,
|
||||
"chat_id" UUID NOT NULL,
|
||||
"owner_id" UUID NOT NULL,
|
||||
"filename" VARCHAR(255) NOT NULL,
|
||||
"content_type" VARCHAR(32) NOT NULL,
|
||||
"size_bytes" INTEGER NOT NULL,
|
||||
"sha256" CHAR(64) NOT NULL,
|
||||
"object_key" VARCHAR(128) NOT NULL,
|
||||
"created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
"updated_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
|
||||
CONSTRAINT "attachments_pkey" PRIMARY KEY ("id"),
|
||||
CONSTRAINT "attachments_filename_not_blank" CHECK (btrim("filename") <> ''),
|
||||
CONSTRAINT "attachments_content_type_allowed" CHECK ("content_type" IN ('image/png', 'image/jpeg')),
|
||||
CONSTRAINT "attachments_size_range" CHECK ("size_bytes" BETWEEN 1 AND 10485760),
|
||||
CONSTRAINT "attachments_sha256_format" CHECK ("sha256" ~ '^[a-f0-9]{64}$')
|
||||
);
|
||||
|
||||
CREATE TABLE "message_attachments" (
|
||||
"message_id" UUID NOT NULL,
|
||||
"position" SMALLINT NOT NULL,
|
||||
"attachment_id" UUID NOT NULL,
|
||||
|
||||
CONSTRAINT "message_attachments_pkey" PRIMARY KEY ("message_id","position"),
|
||||
CONSTRAINT "message_attachments_position_range" CHECK ("position" BETWEEN 0 AND 9)
|
||||
);
|
||||
|
||||
CREATE UNIQUE INDEX "attachments_object_key_key" ON "attachments"("object_key");
|
||||
|
||||
CREATE INDEX "attachments_chat_id_idx" ON "attachments"("chat_id");
|
||||
|
||||
CREATE INDEX "attachments_owner_id_idx" ON "attachments"("owner_id");
|
||||
|
||||
CREATE UNIQUE INDEX "message_attachments_message_id_attachment_id_key" ON "message_attachments"("message_id", "attachment_id");
|
||||
|
||||
CREATE INDEX "message_attachments_attachment_id_idx" ON "message_attachments"("attachment_id");
|
||||
|
||||
ALTER TABLE "attachments" ADD CONSTRAINT "attachments_chat_id_fkey" FOREIGN KEY ("chat_id") REFERENCES "chats"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
|
||||
ALTER TABLE "attachments" ADD CONSTRAINT "attachments_owner_id_fkey" FOREIGN KEY ("owner_id") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
|
||||
|
||||
ALTER TABLE "message_attachments" ADD CONSTRAINT "message_attachments_message_id_fkey" FOREIGN KEY ("message_id") REFERENCES "messages"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
|
||||
ALTER TABLE "message_attachments" ADD CONSTRAINT "message_attachments_attachment_id_fkey" FOREIGN KEY ("attachment_id") REFERENCES "attachments"("id") ON DELETE NO ACTION ON UPDATE CASCADE;
|
||||
@@ -0,0 +1,3 @@
|
||||
# Please do not edit this file manually
|
||||
# It should be added in your version-control system (i.e. Git)
|
||||
provider = "postgresql"
|
||||
@@ -0,0 +1,116 @@
|
||||
generator client {
|
||||
provider = "prisma-client-js"
|
||||
}
|
||||
|
||||
datasource db {
|
||||
provider = "postgresql"
|
||||
url = env("DATABASE_URL")
|
||||
}
|
||||
|
||||
model User {
|
||||
id String @id @default(uuid()) @db.Uuid
|
||||
username String @unique @db.VarChar(32)
|
||||
displayName String @map("display_name") @db.VarChar(100)
|
||||
passwordHash String @map("password_hash")
|
||||
createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz(3)
|
||||
memberships ChatMember[]
|
||||
messages Message[]
|
||||
sessions Session[]
|
||||
attachments Attachment[]
|
||||
|
||||
@@map("users")
|
||||
}
|
||||
|
||||
model Session {
|
||||
tokenHash String @id @map("token_hash") @db.Char(64)
|
||||
userId String @map("user_id") @db.Uuid
|
||||
expiresAt DateTime @map("expires_at") @db.Timestamptz(3)
|
||||
createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz(3)
|
||||
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
|
||||
|
||||
@@index([userId])
|
||||
@@map("sessions")
|
||||
}
|
||||
|
||||
model Chat {
|
||||
id String @id @default(uuid()) @db.Uuid
|
||||
title String @db.VarChar(100)
|
||||
createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz(3)
|
||||
members ChatMember[]
|
||||
messages Message[]
|
||||
attachments Attachment[]
|
||||
|
||||
@@map("chats")
|
||||
}
|
||||
|
||||
model ChatMember {
|
||||
chatId String @map("chat_id") @db.Uuid
|
||||
userId String @map("user_id") @db.Uuid
|
||||
chat Chat @relation(fields: [chatId], references: [id], onDelete: Cascade)
|
||||
user User @relation(fields: [userId], references: [id], onDelete: Restrict)
|
||||
|
||||
@@id([chatId, userId])
|
||||
@@index([userId])
|
||||
@@map("chat_members")
|
||||
}
|
||||
|
||||
model Message {
|
||||
id String @id @default(uuid()) @db.Uuid
|
||||
chatId String @map("chat_id") @db.Uuid
|
||||
senderId String @map("sender_id") @db.Uuid
|
||||
text String @db.VarChar(4000)
|
||||
createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz(3)
|
||||
chat Chat @relation(fields: [chatId], references: [id], onDelete: Cascade)
|
||||
sender User @relation(fields: [senderId], references: [id], onDelete: Restrict)
|
||||
idempotencyKeys IdempotencyKey[]
|
||||
attachments MessageAttachment[]
|
||||
|
||||
@@index([chatId, createdAt, id])
|
||||
@@map("messages")
|
||||
}
|
||||
|
||||
model Attachment {
|
||||
id String @id @default(uuid()) @db.Uuid
|
||||
chatId String @map("chat_id") @db.Uuid
|
||||
ownerId String @map("owner_id") @db.Uuid
|
||||
filename String @db.VarChar(255)
|
||||
contentType String @map("content_type") @db.VarChar(32)
|
||||
sizeBytes Int @map("size_bytes")
|
||||
sha256 String @db.Char(64)
|
||||
objectKey String @unique @map("object_key") @db.VarChar(128)
|
||||
createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz(3)
|
||||
updatedAt DateTime @default(now()) @updatedAt @map("updated_at") @db.Timestamptz(3)
|
||||
chat Chat @relation(fields: [chatId], references: [id], onDelete: Cascade)
|
||||
owner User @relation(fields: [ownerId], references: [id], onDelete: Restrict)
|
||||
messages MessageAttachment[]
|
||||
|
||||
@@index([chatId])
|
||||
@@index([ownerId])
|
||||
@@map("attachments")
|
||||
}
|
||||
|
||||
model MessageAttachment {
|
||||
messageId String @map("message_id") @db.Uuid
|
||||
position Int @db.SmallInt
|
||||
attachmentId String @map("attachment_id") @db.Uuid
|
||||
message Message @relation(fields: [messageId], references: [id], onDelete: Cascade)
|
||||
attachment Attachment @relation(fields: [attachmentId], references: [id], onDelete: NoAction)
|
||||
|
||||
@@id([messageId, position])
|
||||
@@unique([messageId, attachmentId])
|
||||
@@index([attachmentId])
|
||||
@@map("message_attachments")
|
||||
}
|
||||
|
||||
model IdempotencyKey {
|
||||
userId String @map("user_id") @db.Uuid
|
||||
chatId String @map("chat_id") @db.Uuid
|
||||
key String @db.VarChar(128)
|
||||
requestHash String @map("request_hash") @db.Char(64)
|
||||
messageId String @map("message_id") @db.Uuid
|
||||
createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz(3)
|
||||
message Message @relation(fields: [messageId], references: [id], onDelete: Cascade)
|
||||
|
||||
@@id([userId, chatId, key])
|
||||
@@map("idempotency_keys")
|
||||
}
|
||||
Executable
+190
@@ -0,0 +1,190 @@
|
||||
#!/usr/bin/env python3
|
||||
import hashlib
|
||||
import http.client
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import socket
|
||||
import subprocess
|
||||
import sys
|
||||
import time
|
||||
import urllib.error
|
||||
import urllib.request
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
sys.path.insert(0, str(ROOT / 'tests'))
|
||||
from client import Client
|
||||
|
||||
HOST = os.environ.get('LB_HOST', 'lb')
|
||||
PORT = int(os.environ.get('LB_PORT', '8080'))
|
||||
SAMPLE = (ROOT / 'tests/fixtures/sample.png').read_bytes()
|
||||
SAMPLE_SHA = hashlib.sha256(SAMPLE).hexdigest()
|
||||
BUCKET = os.environ.get('S3_BUCKET', 'messenger-attachments')
|
||||
FAULT_NAME = 'lab4_fault'
|
||||
|
||||
|
||||
def sh(*args, check=True):
|
||||
return subprocess.run(args, check=check, capture_output=True, text=True).stdout.strip()
|
||||
|
||||
|
||||
def sql(statement):
|
||||
return sh('docker', 'compose', 'exec', '-T', 'postgres', 'psql', '-U', 'messenger', '-d', 'messenger', '-tAc', statement)
|
||||
|
||||
|
||||
def request(method, path, token=None, body=None, content_type=None):
|
||||
headers = {}
|
||||
if token:
|
||||
headers['Authorization'] = 'Bearer ' + token
|
||||
if content_type:
|
||||
headers['Content-Type'] = content_type
|
||||
conn = http.client.HTTPConnection(HOST, PORT, timeout=60)
|
||||
conn.request(method, path, body=body, headers=headers)
|
||||
response = conn.getresponse()
|
||||
data = response.read()
|
||||
conn.close()
|
||||
return response, data
|
||||
|
||||
|
||||
def upload(token, chat):
|
||||
return request('POST', '/api/v1/chats/' + chat + '/attachments?filename=sample.png', token, SAMPLE, 'image/png')
|
||||
|
||||
|
||||
def download(token, attachment):
|
||||
return request('GET', '/api/v1/attachments/' + attachment + '/content', token)
|
||||
|
||||
|
||||
def reconcile(*flags):
|
||||
output = sh('docker', 'compose', 'run', '--rm', '--no-deps', '-T', 'reconciler', 'node', 'dist/reconcile.js', *flags)
|
||||
line = output.splitlines()[-1]
|
||||
print('$ reconcile', ' '.join(flags))
|
||||
print(line)
|
||||
return json.loads(line)
|
||||
|
||||
|
||||
def replicas():
|
||||
return [cid[:12] for cid in sh('docker', 'compose', 'ps', '-q', 'api').split()]
|
||||
|
||||
|
||||
def wait_ready():
|
||||
deadline = time.monotonic() + 90
|
||||
while time.monotonic() < deadline:
|
||||
try:
|
||||
response, _ = request('GET', '/health/ready')
|
||||
if response.status == 200:
|
||||
return
|
||||
except OSError:
|
||||
pass
|
||||
time.sleep(0.3)
|
||||
raise SystemExit('not ready')
|
||||
|
||||
|
||||
def header(text):
|
||||
print('\n## ' + text, flush=True)
|
||||
|
||||
|
||||
def defence(client):
|
||||
header('1. Сценарий защиты: загрузка, сообщение, второй участник, посторонний, S3 мимо API')
|
||||
alice, bob, eve, chat = client.bootstrap()
|
||||
attachment, _ = client.attachment(alice, chat)
|
||||
print('POST /attachments -> 201 status=%s variants=%s error_code=%s sha256=%s' % (attachment['status'], attachment['variants'], attachment['error_code'], attachment['sha256']))
|
||||
message = client.message(alice, chat, '', attachments=[attachment['id']])['json']
|
||||
print('POST /messages text="" -> 201 attachment_ids совпадают:', message['attachment_ids'] == [attachment['id']])
|
||||
response, data = download(bob['token'], attachment['id'])
|
||||
print('Bob GET /content -> %d %s sha256 совпадает: %s' % (response.status, response.getheader('Content-Type'), hashlib.sha256(data).hexdigest() == SAMPLE_SHA))
|
||||
for suffix in ('', '/content'):
|
||||
response, _ = request('GET', '/api/v1/attachments/' + attachment['id'] + suffix, eve['token'])
|
||||
print('Eve GET /attachments/{id}%s -> %d' % (suffix, response.status))
|
||||
key = sql("SELECT object_key FROM attachments WHERE id = '%s'" % attachment['id'])
|
||||
print('object_key в БД: attachments/<uuid>, filename в ключ не входит:', 'sample' not in key)
|
||||
try:
|
||||
code = urllib.request.urlopen('http://s3:9000/%s/%s' % (BUCKET, key), timeout=5).status
|
||||
except urllib.error.HTTPError as error:
|
||||
code = error.code
|
||||
print('Анонимный GET объекта напрямую в S3 ->', code)
|
||||
return alice, chat['id']
|
||||
|
||||
|
||||
def failover(alice, chat):
|
||||
header('2. Остановка реплики, обслужившей загрузку; скачивание через другую')
|
||||
token = alice['token']
|
||||
tested = set()
|
||||
while len(tested) < 2:
|
||||
response, data = upload(token, chat)
|
||||
target = response.getheader('X-Instance-Id')
|
||||
if target in tested:
|
||||
continue
|
||||
tested.add(target)
|
||||
attachment = json.loads(data)
|
||||
print('upload -> %d, обслужила реплика %s' % (response.status, target))
|
||||
result = subprocess.run(['sh', 'scripts/stop-one.sh'], env={**os.environ, 'TARGET_INSTANCE_ID': target}, capture_output=True, text=True)
|
||||
print('stop-one.sh:', result.stdout.strip())
|
||||
others = [cid for cid in replicas() if cid != target]
|
||||
started = time.monotonic()
|
||||
served = []
|
||||
for _ in range(6):
|
||||
response, data = download(token, attachment['id'])
|
||||
served.append(response.getheader('X-Instance-Id'))
|
||||
ok = response.status == 200 and hashlib.sha256(data).hexdigest() == SAMPLE_SHA
|
||||
print('download -> %d реплика=%s sha256 совпадает: %s' % (response.status, served[-1], ok))
|
||||
if not ok:
|
||||
raise SystemExit('download failed')
|
||||
print('остановленная реплика среди обслуживших:', target in served, '; %.2fs' % (time.monotonic() - started))
|
||||
print('Файл оригинала на диске обслужившей реплики (поиск по точному размеру %d байт):' % len(SAMPLE))
|
||||
for other in others:
|
||||
found = sh('docker', 'exec', other, 'sh', '-c', 'find /tmp /app /home -xdev -type f -size %dc 2>/dev/null | grep -v node_modules || true' % len(SAMPLE))
|
||||
print(' %s -> %s' % (other, found or 'нет'))
|
||||
sh('docker', 'start', target)
|
||||
wait_ready()
|
||||
time.sleep(3)
|
||||
print('реплика %s возвращена' % target)
|
||||
|
||||
|
||||
def interrupted(alice, chat):
|
||||
header('3. Прерванная загрузка через LB: ни строки, ни объекта, ни временного файла')
|
||||
before_rows = sql('SELECT count(*) FROM attachments')
|
||||
before = reconcile('--grace-seconds=3600')['scanned']
|
||||
connection = socket.create_connection((HOST, PORT))
|
||||
connection.sendall(('POST /api/v1/chats/%s/attachments?filename=cut.png HTTP/1.1\r\nHost: lb\r\nAuthorization: Bearer %s\r\nContent-Type: image/png\r\nContent-Length: %d\r\n\r\n' % (chat, alice['token'], len(SAMPLE))).encode())
|
||||
connection.sendall(SAMPLE[:20000])
|
||||
time.sleep(0.5)
|
||||
connection.close()
|
||||
time.sleep(1)
|
||||
after = reconcile('--grace-seconds=3600')['scanned']
|
||||
print('строк attachments: %s -> %s; объектов: %d -> %d' % (before_rows, sql('SELECT count(*) FROM attachments'), before, after))
|
||||
for cid in replicas():
|
||||
print('временных каталогов в %s: %s' % (cid, sh('docker', 'exec', cid, 'sh', '-c', 'ls /tmp | grep -c messenger-upload || true') or '0'))
|
||||
|
||||
|
||||
def inject(alice, chat, title, body):
|
||||
header(title)
|
||||
token = alice['token']
|
||||
before = reconcile('--grace-seconds=3600')['scanned']
|
||||
rows = sql('SELECT count(*) FROM attachments')
|
||||
sql("CREATE FUNCTION %s() RETURNS trigger AS $$ BEGIN %s RETURN NEW; END $$ LANGUAGE plpgsql; CREATE TRIGGER %s BEFORE INSERT ON attachments FOR EACH ROW EXECUTE FUNCTION %s();" % (FAULT_NAME, body, FAULT_NAME, FAULT_NAME))
|
||||
try:
|
||||
response, data = upload(token, chat)
|
||||
print('upload при отказе INSERT -> %d %s' % (response.status, json.loads(data)['error']['code']))
|
||||
finally:
|
||||
sql('DROP TRIGGER %s ON attachments; DROP FUNCTION %s();' % (FAULT_NAME, FAULT_NAME))
|
||||
print('строк attachments: %s -> %s' % (rows, sql('SELECT count(*) FROM attachments')))
|
||||
fresh = reconcile('--apply', '--grace-seconds=3600')
|
||||
print('объектов было %d, стало %d; свежий объект защищён grace: удалено %d' % (before, fresh['scanned'], fresh['deleted']))
|
||||
time.sleep(2)
|
||||
cleaned = reconcile('--apply', '--grace-seconds=1')
|
||||
print('после grace: сирот %d, удалено %d, привязанные объекты не тронуты: %d' % (cleaned['orphans'], cleaned['deleted'], cleaned['referenced']))
|
||||
response, data = upload(token, chat)
|
||||
print('следующая загрузка без отказа ->', response.status)
|
||||
wait_ready()
|
||||
|
||||
|
||||
def main():
|
||||
client = Client('http://%s:%d' % (HOST, PORT))
|
||||
alice, chat = defence(client)
|
||||
failover(alice, chat)
|
||||
interrupted(alice, chat)
|
||||
inject(alice, chat, '4. Реальный отказ PostgreSQL между PUT и INSERT: триггер отклоняет вставку', "RAISE EXCEPTION 'injected fault';")
|
||||
inject(alice, chat, '5. Потеря соединения с PostgreSQL между PUT и INSERT: исход неясен, объект остаётся до сверки', 'PERFORM pg_terminate_backend(pg_backend_pid());')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
Executable
+21
@@ -0,0 +1,21 @@
|
||||
#!/usr/bin/env python3
|
||||
import sys
|
||||
import urllib.error
|
||||
import urllib.request
|
||||
|
||||
bucket, key = sys.argv[1:3]
|
||||
base = 'http://s3:9000/' + bucket
|
||||
attempts = (
|
||||
('GET', '/<object>', base + '/' + key, None),
|
||||
('GET', '/', base + '/', None),
|
||||
('PUT', '/anon.txt', base + '/anon.txt', b'x'),
|
||||
)
|
||||
failed = False
|
||||
for method, label, url, data in attempts:
|
||||
try:
|
||||
code = urllib.request.urlopen(urllib.request.Request(url, data=data, method=method), timeout=5).status
|
||||
except urllib.error.HTTPError as error:
|
||||
code = error.code
|
||||
print(method, bucket + label, '->', code)
|
||||
failed = failed or code != 403
|
||||
sys.exit(1 if failed else 0)
|
||||
Executable
+46
@@ -0,0 +1,46 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
cd "$(dirname "$0")/.."
|
||||
bucket="${S3_BUCKET:-messenger-attachments}"
|
||||
object="check/$(date +%s)-$$.txt"
|
||||
|
||||
as_api() {
|
||||
docker compose run --rm --no-deps -T -e BUCKET="$bucket" -e OBJECT="$object" --entrypoint sh s3-init -c "$1"
|
||||
}
|
||||
|
||||
echo "## S3 не опубликован наружу, консоль выключена"
|
||||
docker compose ps s3 --format '{{.Service}} ports={{.Ports}}'
|
||||
echo "MINIO_BROWSER=$(docker compose exec -T s3 printenv MINIO_BROWSER)"
|
||||
|
||||
echo
|
||||
echo "## Права пользователя API: только свой bucket"
|
||||
as_api '
|
||||
set -e
|
||||
cd /run/secrets
|
||||
export MC_HOST_api="http://$(cat s3_access_key_id):$(cat s3_secret_access_key)@s3:9000"
|
||||
export MC_HOST_root="http://$(cat s3_root_user):$(cat s3_root_password)@s3:9000"
|
||||
expect() { want=$1; label=$2; shift 2; if "$@" >/dev/null 2>&1; then got=allowed; else got=denied; fi; echo "$label -> $got"; [ "$got" = "$want" ]; }
|
||||
echo probe > /tmp/probe.txt
|
||||
mc mb --ignore-existing root/check-other-bucket >/dev/null
|
||||
mc cp /tmp/probe.txt root/check-other-bucket/probe.txt >/dev/null
|
||||
trap "mc rb --force root/check-other-bucket >/dev/null" EXIT
|
||||
expect allowed "PUT в свой bucket" mc cp /tmp/probe.txt "api/$BUCKET/$OBJECT"
|
||||
expect allowed "GET из своего bucket" mc cat "api/$BUCKET/$OBJECT"
|
||||
expect denied "создание другого bucket" mc mb api/forbidden-bucket
|
||||
expect denied "GET из другого bucket" mc cat api/check-other-bucket/probe.txt
|
||||
expect denied "PUT в другой bucket" mc cp /tmp/probe.txt api/check-other-bucket/new.txt
|
||||
expect denied "изменение политики bucket" mc anonymous set public "api/$BUCKET"
|
||||
expect denied "администрирование пользователей" mc admin user list api
|
||||
'
|
||||
|
||||
echo
|
||||
echo "## Анонимный доступ напрямую к S3, мимо API"
|
||||
python3 scripts/check-s3-anonymous.py "$bucket" "$object"
|
||||
|
||||
as_api '
|
||||
cd /run/secrets
|
||||
export MC_HOST_api="http://$(cat s3_access_key_id):$(cat s3_secret_access_key)@s3:9000"
|
||||
mc rm "api/$BUCKET/$OBJECT" >/dev/null
|
||||
'
|
||||
echo
|
||||
echo "PASS: bucket приватный, у API нет прав вне своего bucket"
|
||||
Executable
+200
@@ -0,0 +1,200 @@
|
||||
#!/bin/sh
|
||||
set -u
|
||||
cd "$(dirname "$0")/.."
|
||||
mkdir -p evidence
|
||||
|
||||
COMMIT="$(git rev-parse --short HEAD)$(git diff --quiet HEAD -- src prisma test scripts infra compose.yaml Dockerfile || echo ' (dirty)')"
|
||||
|
||||
header() {
|
||||
printf '# %s\n# date: %s\n# commit: %s\n\n' "$1" "$(date -u +%Y-%m-%dT%H:%M:%SZ)" "$COMMIT"
|
||||
}
|
||||
|
||||
run() {
|
||||
echo "\$ $*"
|
||||
"$@" 2>&1 || echo "EXIT CODE $?"
|
||||
echo
|
||||
}
|
||||
|
||||
started() {
|
||||
docker inspect -f '{{.Name}} {{.State.Status}} started={{.State.StartedAt}}' $(docker compose ps -aq postgres redis s3 api lb)
|
||||
}
|
||||
|
||||
api_states() {
|
||||
docker inspect -f '{{.Name}} id={{slice .Id 0 12}} {{.State.Status}} exit={{.State.ExitCode}} finished={{.State.FinishedAt}}' $(docker compose ps -aq api)
|
||||
}
|
||||
|
||||
restore_api() {
|
||||
for id in $(docker compose ps -aq --status exited api); do
|
||||
docker start "$id" >/dev/null
|
||||
done
|
||||
python3 scripts/probe.py ready >/dev/null
|
||||
sleep 3
|
||||
}
|
||||
|
||||
echo 'container.txt'
|
||||
{
|
||||
header 'Стенд: сервисы, порты, лимиты, секреты, миграции'
|
||||
run docker compose ps -a --format 'table {{.Service}}\t{{.Name}}\t{{.Image}}\t{{.Status}}\t{{.Ports}}'
|
||||
echo '## CPU/RAM-лимиты (NanoCpus: 1e9 = 1 CPU; Memory в байтах)'
|
||||
run sh -c "docker inspect -f '{{.Name}} cpus={{.HostConfig.NanoCpus}} memory={{.HostConfig.Memory}}' \$(docker compose ps -aq)"
|
||||
echo '## container_name не задан (одно имя сервиса масштабируется)'
|
||||
run sh -c "docker compose config | grep -c container_name || true"
|
||||
echo '## Секреты: каждый сервис видит только свои файлы'
|
||||
for service in postgres redis s3 api reconciler; do
|
||||
run docker compose exec -T "$service" ls /run/secrets
|
||||
done
|
||||
run docker compose run --rm --no-deps -T --entrypoint ls s3-init /run/secrets
|
||||
run docker compose exec -T lb sh -c 'ls /run/secrets 2>/dev/null || echo "no /run/secrets"'
|
||||
run docker compose exec -T lb haproxy -v
|
||||
run docker compose exec -T postgres postgres --version
|
||||
run docker compose exec -T redis redis-server --version
|
||||
run docker compose exec -T s3 minio --version
|
||||
echo '## Таблицы вложений'
|
||||
run docker compose exec -T postgres psql -U messenger -d messenger -c '\d attachments' -c '\d message_attachments'
|
||||
echo '## Миграции: отдельный one-shot сервис, повторный запуск ничего не применяет'
|
||||
run docker compose run --rm migrate
|
||||
run docker compose run --rm --no-deps migrate sh -c 'export DATABASE_URL="$(cat "$DATABASE_URL_FILE")"; npx prisma migrate diff --from-url "$DATABASE_URL" --to-schema-datamodel prisma/schema.prisma --exit-code'
|
||||
run docker compose exec -T postgres psql -U messenger -d messenger -c 'SELECT migration_name, finished_at FROM _prisma_migrations ORDER BY started_at'
|
||||
} > evidence/container.txt
|
||||
|
||||
echo 'tests.txt'
|
||||
{
|
||||
header 'Собственные e2e-тесты (PostgreSQL + Redis + MinIO из compose.test.yaml)'
|
||||
docker compose -f compose.test.yaml build -q tests
|
||||
run docker compose -f compose.test.yaml run --rm tests npx jest --config ./test/jest-e2e.json --verbose
|
||||
docker compose -f compose.test.yaml down -v >/dev/null 2>&1
|
||||
} > evidence/tests.txt
|
||||
|
||||
echo 'smoke.txt'
|
||||
{
|
||||
header 'Публичные проверки через LB; разные реплики обслуживают одну сессию'
|
||||
run make check
|
||||
run make test
|
||||
run sh -c "docker compose ps -q api | cut -c1-12"
|
||||
run python3 scripts/probe.py instances 6
|
||||
} > evidence/smoke.txt
|
||||
|
||||
echo 'persistence.txt'
|
||||
{
|
||||
header 'make test-persistence: restart.sh перезапускает postgres, redis, s3, api, lb с сохранением volumes'
|
||||
echo '## до'
|
||||
started
|
||||
echo
|
||||
run make test-persistence ACTION_SCRIPT=scripts/restart.sh
|
||||
echo '## после'
|
||||
started
|
||||
} > evidence/persistence.txt
|
||||
|
||||
echo 'failover.txt'
|
||||
{
|
||||
header 'make test-failover: stop-one.sh останавливает реплику, обслужившую тест; затем вторую'
|
||||
first_stopped=''
|
||||
for round in 1 2 3 4 5; do
|
||||
echo "## Прогон $round"
|
||||
echo '### до'
|
||||
api_states
|
||||
echo
|
||||
since=$(date -u +%Y-%m-%dT%H:%M:%SZ)
|
||||
run make test-failover ACTION_SCRIPT=scripts/stop-one.sh
|
||||
echo '### после: остановленная реплика Exited (0) — штатное завершение по SIGTERM'
|
||||
api_states
|
||||
echo
|
||||
echo '### журнал HAProxy'
|
||||
docker compose logs --no-log-prefix --since "$since" lb 2>&1 | grep -E 'is DOWN|is UP|maintenance|READY' | grep -v '^\[WARNING\]'
|
||||
echo
|
||||
stopped=$(docker compose ps -aq --status exited api | cut -c1-12)
|
||||
echo "остановлена реплика: $stopped"
|
||||
echo
|
||||
restore_api
|
||||
if [ -n "$first_stopped" ] && [ "$stopped" != "$first_stopped" ]; then
|
||||
break
|
||||
fi
|
||||
[ -n "$first_stopped" ] || first_stopped=$stopped
|
||||
python3 scripts/probe.py status GET /health/live >/dev/null
|
||||
done
|
||||
echo '## Проверка guard: stop-one.sh отказывается останавливать не api'
|
||||
run env TARGET_INSTANCE_ID="$(docker compose ps -q postgres | cut -c1-12)" sh scripts/stop-one.sh
|
||||
} > evidence/failover.txt
|
||||
|
||||
echo 'attachments.txt'
|
||||
{
|
||||
header 'Вложения: сценарий защиты, остановка загрузившей реплики, прерванная загрузка, отказ между S3 и БД'
|
||||
run python3 scripts/attachment-scenario.py
|
||||
} > evidence/attachments.txt
|
||||
|
||||
echo 's3.txt'
|
||||
{
|
||||
header 'S3: идемпотентный bootstrap, приватность мимо API, отказ S3, сверка'
|
||||
echo '## Повторный bootstrap ничего не ломает'
|
||||
run docker compose run --rm --no-deps -T s3-init
|
||||
run docker compose run --rm --no-deps -T s3-init
|
||||
run sh scripts/check-s3.sh
|
||||
echo '## Отказ: контейнер S3 остановлен'
|
||||
run sh scripts/s3-outage.sh stop
|
||||
echo '## Отказ: контейнер S3 завис (docker pause)'
|
||||
run sh scripts/s3-outage.sh pause
|
||||
echo '## Сверка: объект без метаданных'
|
||||
run sh scripts/reconcile-demo.sh
|
||||
echo '## Журнал постоянного сервиса reconciler'
|
||||
docker compose logs --no-log-prefix --tail 3 reconciler 2>&1
|
||||
} > evidence/s3.txt
|
||||
|
||||
echo 'upload-memory.json'
|
||||
python3 scripts/upload-experiment.py --output evidence/upload-memory.json >/dev/null 2>&1
|
||||
|
||||
echo 'experiments: idempotency, failover, failover --kill, rolling'
|
||||
python3 scripts/experiment.py idempotency --output evidence/idempotency.json >/dev/null 2>&1
|
||||
python3 scripts/experiment.py failover --output evidence/failover-stop.json >/dev/null 2>&1
|
||||
python3 scripts/experiment.py failover --kill --output evidence/failover-kill.json >/dev/null 2>&1
|
||||
docker compose build -q api >/dev/null 2>&1
|
||||
python3 scripts/experiment.py rolling --output evidence/rolling.json >/dev/null 2>&1
|
||||
|
||||
echo 'failures.txt'
|
||||
{
|
||||
header 'Отказы зависимостей, ошибки LB, expiry'
|
||||
echo '## 1. Redis остановлен: вход и Bearer через PostgreSQL; logout — 503 без отзыва (токен остаётся действующим)'
|
||||
run docker compose stop redis
|
||||
run python3 scripts/probe.py logout
|
||||
run docker compose start redis
|
||||
sleep 3
|
||||
echo '## 2. Redis снова доступен: logout отзывает токен на всех репликах'
|
||||
run python3 scripts/probe.py logout
|
||||
echo '## 3. PostgreSQL остановлен: live 200, запрос с токеном — 503 без внутренних деталей, аноним — 401'
|
||||
run docker compose stop postgres
|
||||
run python3 scripts/probe.py status GET /health/live
|
||||
run python3 scripts/probe.py status GET /api/v1/chats 'Bearer unknown-token'
|
||||
run python3 scripts/probe.py status GET /api/v1/users/me
|
||||
run docker compose start postgres
|
||||
run python3 scripts/probe.py ready
|
||||
echo '## 4. Все реплики API остановлены: LB отвечает 503 в формате ошибки контракта'
|
||||
run docker compose stop api
|
||||
run python3 scripts/probe.py status GET /api/v1/users/me
|
||||
run docker compose start api
|
||||
run python3 scripts/probe.py ready
|
||||
echo '## 5. Живая, но неготовая реплика: PostgreSQL отклоняет подключения только от одной API (pg_hba reject)'
|
||||
target=$(docker compose ps -q api | head -1 | cut -c1-12)
|
||||
ip=$(docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' "$target")
|
||||
hba=/var/lib/postgresql/data/pg_hba.conf
|
||||
echo "target=$target ip=$ip"
|
||||
since=$(date -u +%Y-%m-%dT%H:%M:%SZ)
|
||||
docker compose exec -T postgres sh -c "cp $hba /tmp/pg_hba.bak && { echo 'host all all $ip/32 reject'; cat /tmp/pg_hba.bak; } > $hba && chown postgres:postgres $hba && chmod 600 $hba"
|
||||
docker compose exec -T postgres psql -U messenger -d messenger -tAc "SELECT pg_reload_conf(); SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE client_addr = '$ip'" >/dev/null
|
||||
sleep 4
|
||||
run docker exec "$target" node -e "let n=0;const g=p=>require('http').get('http://127.0.0.1:8080'+p,r=>{console.log(p,r.statusCode);r.resume();if(++n===2)process.exit(0)});g('/health/live');g('/health/core')"
|
||||
run python3 scripts/probe.py instances 10
|
||||
docker compose logs --no-log-prefix --since "$since" lb 2>&1 | grep -E 'is DOWN|is UP' | grep -v '^\[WARNING\]'
|
||||
echo
|
||||
docker compose exec -T postgres sh -c "cp /tmp/pg_hba.bak $hba && chown postgres:postgres $hba && chmod 600 $hba"
|
||||
docker compose exec -T postgres psql -U messenger -d messenger -tAc "SELECT pg_reload_conf()" >/dev/null
|
||||
sleep 4
|
||||
echo '### доступ возвращён: реплика снова готова и получает запросы'
|
||||
run python3 scripts/probe.py instances 6
|
||||
echo '## 6. Expiry: реплики пересозданы с SESSION_TTL_SECONDS=2'
|
||||
SESSION_TTL_SECONDS=2 docker compose up -d --no-deps --wait api >/dev/null 2>&1
|
||||
run sh -c "docker exec \$(docker compose ps -q api | head -1) printenv SESSION_TTL_SECONDS"
|
||||
run python3 scripts/probe.py expiry 2
|
||||
docker compose up -d --no-deps --wait api >/dev/null 2>&1
|
||||
run python3 scripts/probe.py ready
|
||||
} > evidence/failures.txt
|
||||
|
||||
echo 'done'
|
||||
Executable
+255
@@ -0,0 +1,255 @@
|
||||
#!/usr/bin/env python3
|
||||
import argparse
|
||||
import json
|
||||
import math
|
||||
import os
|
||||
import subprocess
|
||||
import sys
|
||||
import threading
|
||||
import time
|
||||
import uuid
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
from pathlib import Path
|
||||
from urllib.parse import quote
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
sys.path.insert(0, str(ROOT / 'tests'))
|
||||
from client import Client, Failure
|
||||
|
||||
BASE_URL = os.environ.get('BASE_URL', 'http://lb:8080')
|
||||
|
||||
|
||||
def docker(*args):
|
||||
return subprocess.run(['docker', *args], check=True, capture_output=True, text=True).stdout.strip()
|
||||
|
||||
|
||||
def api_containers():
|
||||
return [cid[:12] for cid in docker('compose', 'ps', '-q', 'api').split()]
|
||||
|
||||
|
||||
def describe(cid):
|
||||
return docker('inspect', '-f', '{{.Name}} {{.State.Status}} image={{slice .Image 7 19}}', cid)
|
||||
|
||||
|
||||
def wait_healthy(cid, timeout=60):
|
||||
deadline = time.monotonic() + timeout
|
||||
while time.monotonic() < deadline:
|
||||
if docker('inspect', '-f', '{{.State.Health.Status}}', cid) == 'healthy':
|
||||
return
|
||||
time.sleep(0.2)
|
||||
raise SystemExit(cid + ' did not become healthy')
|
||||
|
||||
|
||||
class Load:
|
||||
def __init__(self, client, actor, chat, workers):
|
||||
self.client, self.actor, self.chat = client, actor, chat
|
||||
self.workers = workers
|
||||
self.lock = threading.Lock()
|
||||
self.events = []
|
||||
self.confirmed = []
|
||||
self.stop_flag = threading.Event()
|
||||
self.threads = [threading.Thread(target=self.worker, args=(i,)) for i in range(workers)]
|
||||
self.started = None
|
||||
|
||||
def worker(self, index):
|
||||
local = Client(BASE_URL, timeout=2)
|
||||
key = uuid.uuid4().hex
|
||||
while not self.stop_flag.is_set():
|
||||
try:
|
||||
response = local.message(self.actor, self.chat, 'load ' + key, key=key)
|
||||
with self.lock:
|
||||
self.events.append((time.monotonic(), True, response['headers']['X-Instance-Id'], response['elapsed']))
|
||||
self.confirmed.append(response['json'])
|
||||
key = uuid.uuid4().hex
|
||||
except Failure:
|
||||
with self.lock:
|
||||
self.events.append((time.monotonic(), False, None, None))
|
||||
time.sleep(0.05)
|
||||
|
||||
def __enter__(self):
|
||||
self.started = time.monotonic()
|
||||
for thread in self.threads:
|
||||
thread.start()
|
||||
return self
|
||||
|
||||
def __exit__(self, *exc):
|
||||
self.stop_flag.set()
|
||||
for thread in self.threads:
|
||||
thread.join()
|
||||
|
||||
def window(self, start, end):
|
||||
with self.lock:
|
||||
events = [e for e in self.events if start <= e[0] < end]
|
||||
ok = sorted(e[0] for e in events if e[1])
|
||||
latencies = sorted(e[3] for e in events if e[1])
|
||||
points = [start, *ok, end]
|
||||
|
||||
def percentile(p):
|
||||
return round(1000 * latencies[max(0, math.ceil(p * len(latencies)) - 1)], 1) if latencies else None
|
||||
|
||||
return {
|
||||
'seconds': round(end - start, 3),
|
||||
'successful_writes': len(ok),
|
||||
'failed_attempts': len(events) - len(ok),
|
||||
'error_ratio': round((len(events) - len(ok)) / len(events), 4) if events else None,
|
||||
'successful_rps': round(len(ok) / (end - start), 1),
|
||||
'latency_ms_successful': {'p50': percentile(0.5), 'p95': percentile(0.95), 'p99': percentile(0.99)},
|
||||
'max_write_gap_seconds': round(max(b - a for a, b in zip(points, points[1:])), 3),
|
||||
'instances': sorted({e[2] for e in events if e[1]}),
|
||||
}
|
||||
|
||||
def last_seen(self, instance, start, end):
|
||||
with self.lock:
|
||||
times = [e[0] for e in self.events if e[1] and e[2] == instance and start <= e[0] < end]
|
||||
return max(times) if times else None
|
||||
|
||||
def first_seen_after(self, instance, after):
|
||||
with self.lock:
|
||||
times = [e[0] for e in self.events if e[1] and e[2] == instance and e[0] >= after]
|
||||
return min(times) if times else None
|
||||
|
||||
|
||||
def verify_history(client, actor, chat, confirmed):
|
||||
path = '/api/v1/chats/' + chat['id'] + '/messages?limit=100'
|
||||
items = []
|
||||
while True:
|
||||
page = client.call('GET', path, actor=actor, schema='MessagePage')['json']
|
||||
items.extend(page['items'])
|
||||
if page['next_cursor'] is None:
|
||||
break
|
||||
path = '/api/v1/chats/' + chat['id'] + '/messages?limit=100&cursor=' + quote(page['next_cursor'], safe='')
|
||||
texts = [m['text'] for m in items]
|
||||
stored = {m['id']: m for m in items}
|
||||
return {
|
||||
'confirmed': len(confirmed),
|
||||
'stored': len(items),
|
||||
'lost_confirmed': sum(1 for m in confirmed if stored.get(m['id']) != m),
|
||||
'duplicates': len(texts) - len(set(texts)),
|
||||
}
|
||||
|
||||
|
||||
def resources():
|
||||
ids = docker('compose', 'ps', '-q').split()
|
||||
return docker('stats', '--no-stream', '--format', '{{.Name}} cpu={{.CPUPerc}} mem={{.MemUsage}}', *ids).splitlines()
|
||||
|
||||
|
||||
def messages_in_db():
|
||||
return int(docker('compose', 'exec', '-T', 'postgres', 'psql', '-U', 'messenger', '-d', 'messenger', '-tAc', 'SELECT count(*) FROM messages'))
|
||||
|
||||
|
||||
def failover(load, args):
|
||||
results = []
|
||||
with load:
|
||||
time.sleep(args.warmup)
|
||||
usage = resources()
|
||||
for cid in api_containers():
|
||||
t0 = time.monotonic()
|
||||
docker('kill' if args.kill else 'stop', cid)
|
||||
stopped = time.monotonic()
|
||||
time.sleep(args.outage)
|
||||
outage = load.window(t0, time.monotonic())
|
||||
restore = time.monotonic()
|
||||
docker('start', cid)
|
||||
wait_healthy(cid)
|
||||
healthy = time.monotonic()
|
||||
time.sleep(args.warmup)
|
||||
rejoined = load.first_seen_after(cid, restore)
|
||||
last = load.last_seen(cid, t0, restore)
|
||||
results.append({
|
||||
'container': describe(cid),
|
||||
'action': 'docker kill (SIGKILL)' if args.kill else 'docker stop (SIGTERM, graceful)',
|
||||
'before': load.window(t0 - args.warmup, t0),
|
||||
'stop_command_seconds': round(stopped - t0, 3),
|
||||
'target_last_success_after_stop_seconds': round(last - t0, 3) if last else None,
|
||||
'outage_window': outage,
|
||||
'restore_until_healthy_seconds': round(healthy - restore, 3),
|
||||
'restore_until_serving_seconds': round(rejoined - restore, 3) if rejoined else None,
|
||||
'after_restore': load.window(healthy, time.monotonic()),
|
||||
})
|
||||
return {'scenario': 'sequential failover of each api replica', 'resources_under_load': usage, 'replicas': results}
|
||||
|
||||
|
||||
def rolling(load, args):
|
||||
old = api_containers()
|
||||
before = [describe(cid) for cid in old]
|
||||
steps = []
|
||||
with load:
|
||||
time.sleep(args.warmup)
|
||||
usage = resources()
|
||||
t_start = time.monotonic()
|
||||
docker('compose', 'run', '--rm', '--no-deps', 'migrate')
|
||||
for cid in old:
|
||||
t0 = time.monotonic()
|
||||
docker('compose', 'up', '-d', '--no-deps', '--no-recreate', '--scale', 'api=' + str(len(old) + 1), '--wait', 'api')
|
||||
added = time.monotonic()
|
||||
docker('stop', cid)
|
||||
docker('rm', cid)
|
||||
steps.append({'replaced': cid, 'new_replica_ready_seconds': round(added - t0, 3), 'step_seconds': round(time.monotonic() - t0, 3)})
|
||||
time.sleep(args.warmup)
|
||||
total = load.window(t_start, time.monotonic())
|
||||
return {
|
||||
'scenario': 'rolling replacement: migrate once, then add new replica, wait healthy, stop old one',
|
||||
'resources_under_load': usage,
|
||||
'before': before,
|
||||
'after': [describe(cid) for cid in api_containers()],
|
||||
'steps': steps,
|
||||
'during_update': total,
|
||||
}
|
||||
|
||||
|
||||
def idempotency(client, actor, chat, args):
|
||||
key = uuid.uuid4().hex
|
||||
text = 'parallel ' + key
|
||||
|
||||
def send(_):
|
||||
local = Client(BASE_URL, timeout=10)
|
||||
response = local.message(actor, chat, text, key=key)
|
||||
return response['json']['id'], response['headers']['X-Instance-Id']
|
||||
|
||||
with ThreadPoolExecutor(max_workers=args.parallel) as pool:
|
||||
results = list(pool.map(send, range(args.parallel)))
|
||||
path = '/api/v1/chats/' + chat['id'] + '/messages?limit=100'
|
||||
stored = [m for m in client.call('GET', path, actor=actor, schema='MessagePage')['json']['items'] if m['text'] == text]
|
||||
return {
|
||||
'scenario': 'parallel POST with one Idempotency-Key through LB',
|
||||
'requests': args.parallel,
|
||||
'all_201': True,
|
||||
'distinct_message_ids': len({r[0] for r in results}),
|
||||
'instances': sorted({r[1] for r in results}),
|
||||
'messages_in_history': len(stored),
|
||||
}
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description='Lab3 level-4 experiments under load through LB')
|
||||
parser.add_argument('scenario', choices=['failover', 'rolling', 'idempotency'])
|
||||
parser.add_argument('--workers', type=int, default=4)
|
||||
parser.add_argument('--warmup', type=float, default=5)
|
||||
parser.add_argument('--outage', type=float, default=10)
|
||||
parser.add_argument('--parallel', type=int, default=20)
|
||||
parser.add_argument('--kill', action='store_true')
|
||||
parser.add_argument('--output')
|
||||
args = parser.parse_args()
|
||||
|
||||
client = Client(BASE_URL)
|
||||
alice, bob, _, chat = client.bootstrap()
|
||||
scenarios = {'failover': failover, 'rolling': rolling}
|
||||
if args.scenario in scenarios:
|
||||
load = Load(client, alice, chat, args.workers)
|
||||
dataset = messages_in_db()
|
||||
report = scenarios[args.scenario](load, args)
|
||||
report['dataset'] = {'messages_in_db_before': dataset, 'messages_in_db_after': messages_in_db()}
|
||||
report['history'] = verify_history(client, bob, chat, load.confirmed)
|
||||
report['load'] = {'workers': args.workers, 'model': 'closed loop, POST message', 'warmup_seconds': args.warmup, 'client_timeout_seconds': 2, 'retry': 'same Idempotency-Key until success'}
|
||||
else:
|
||||
report = idempotency(client, alice, chat, args)
|
||||
report['date'] = time.strftime('%Y-%m-%dT%H:%M:%SZ', time.gmtime())
|
||||
text = json.dumps(report, ensure_ascii=False, indent=2)
|
||||
if args.output:
|
||||
Path(args.output).parent.mkdir(parents=True, exist_ok=True)
|
||||
Path(args.output).write_text(text + '\n')
|
||||
print(text)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
@@ -0,0 +1,88 @@
|
||||
#!/usr/bin/env python3
|
||||
import base64
|
||||
import json
|
||||
import os
|
||||
import sys
|
||||
import time
|
||||
import urllib.error
|
||||
import urllib.request
|
||||
import uuid
|
||||
|
||||
BASE = os.environ.get('BASE_URL', 'http://localhost:8080')
|
||||
|
||||
|
||||
def call(method, path, body=None, headers=None, with_headers=False):
|
||||
data = json.dumps(body).encode() if body is not None else None
|
||||
content_type = {'Content-Type': 'application/json'} if data else {}
|
||||
request = urllib.request.Request(BASE + path, data=data, method=method, headers={**content_type, **(headers or {})})
|
||||
try:
|
||||
with urllib.request.urlopen(request, timeout=15) as response:
|
||||
result = response.status, json.loads(response.read() or b'null'), response.headers
|
||||
except urllib.error.HTTPError as error:
|
||||
result = error.code, json.loads(error.read() or b'null'), error.headers
|
||||
except OSError as error:
|
||||
result = 0, str(error), {}
|
||||
return result if with_headers else result[:2]
|
||||
|
||||
|
||||
def login():
|
||||
username = 'probe_' + uuid.uuid4().hex[:8]
|
||||
password = 'Probe-check-' + uuid.uuid4().hex
|
||||
print('register ->', call('POST', '/api/v1/users', {'username': username, 'display_name': 'Probe', 'password': password})[0])
|
||||
credential = base64.b64encode((username + ':' + password).encode()).decode()
|
||||
code, session = call('POST', '/api/v1/auth/sessions', headers={'Authorization': 'Basic ' + credential})
|
||||
if code != 201:
|
||||
sys.exit('login failed: %s %s' % (code, session))
|
||||
print('login ->', code, 'expires_at:', session['expires_at'])
|
||||
return {'Authorization': 'Bearer ' + session['token']}
|
||||
|
||||
|
||||
def instances(count):
|
||||
auth = login()
|
||||
for _ in range(count):
|
||||
code, _, headers = call('GET', '/api/v1/users/me', headers=auth, with_headers=True)
|
||||
print('GET /api/v1/users/me ->', code, 'X-Instance-Id:', headers.get('X-Instance-Id'))
|
||||
|
||||
|
||||
def logout():
|
||||
auth = login()
|
||||
print('GET /api/v1/users/me ->', call('GET', '/api/v1/users/me', headers=auth)[0])
|
||||
code, body = call('DELETE', '/api/v1/auth/sessions/current', headers=auth)
|
||||
print('DELETE /api/v1/auth/sessions/current ->', code, json.dumps(body, ensure_ascii=False) if body else '')
|
||||
print('GET /api/v1/users/me ->', call('GET', '/api/v1/users/me', headers=auth)[0])
|
||||
|
||||
|
||||
def ready(timeout=90):
|
||||
deadline = time.monotonic() + timeout
|
||||
while time.monotonic() < deadline:
|
||||
if call('GET', '/health/ready')[0] == 200:
|
||||
print('ready')
|
||||
return
|
||||
time.sleep(0.5)
|
||||
sys.exit('not ready within %ds' % timeout)
|
||||
|
||||
|
||||
def status(method, path, authorization=None):
|
||||
code, body = call(method, path, headers={'Authorization': authorization} if authorization else None)
|
||||
print(method, path, '(%s)' % (authorization or 'anonymous'), '->', code, json.dumps(body, ensure_ascii=False))
|
||||
|
||||
|
||||
def expiry(ttl):
|
||||
auth = login()
|
||||
print('GET /api/v1/users/me (fresh) ->', call('GET', '/api/v1/users/me', headers=auth)[0])
|
||||
time.sleep(ttl + 1)
|
||||
print('GET /api/v1/users/me (after %ds) ->' % (ttl + 1), call('GET', '/api/v1/users/me', headers=auth)[0])
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
command = sys.argv[1]
|
||||
if command == 'ready':
|
||||
ready()
|
||||
elif command == 'status':
|
||||
status(*sys.argv[2:5])
|
||||
elif command == 'expiry':
|
||||
expiry(int(sys.argv[2]))
|
||||
elif command == 'instances':
|
||||
instances(int(sys.argv[2]))
|
||||
elif command == 'logout':
|
||||
logout()
|
||||
Executable
+39
@@ -0,0 +1,39 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
cd "$(dirname "$0")/.."
|
||||
bucket="${S3_BUCKET:-messenger-attachments}"
|
||||
key="attachments/orphan-$(date +%s)"
|
||||
|
||||
mc_api() {
|
||||
docker compose run --rm --no-deps -T -e BUCKET="$bucket" -e KEY="$key" --entrypoint sh s3-init -c "
|
||||
cd /run/secrets
|
||||
export MC_HOST_api=\"http://\$(cat s3_access_key_id):\$(cat s3_secret_access_key)@s3:9000\"
|
||||
$1"
|
||||
}
|
||||
|
||||
run() {
|
||||
docker compose run --rm --no-deps -T reconciler node dist/reconcile.js "$@"
|
||||
}
|
||||
|
||||
echo "## Отказ между S3 и БД: объект есть, строки нет"
|
||||
mc_api 'echo orphan > /tmp/o && mc cp -q /tmp/o "api/$BUCKET/$KEY" >/dev/null && mc stat "api/$BUCKET/$KEY" | head -1'
|
||||
|
||||
echo
|
||||
echo "## Свежий объект, grace 3600 с, --apply: не удалён"
|
||||
run --apply --grace-seconds=3600
|
||||
mc_api 'mc stat "api/$BUCKET/$KEY" >/dev/null && echo "объект на месте"'
|
||||
|
||||
sleep 3
|
||||
echo
|
||||
echo "## Dry-run, grace 1 с: найден, не удалён"
|
||||
run --grace-seconds=1
|
||||
mc_api 'mc stat "api/$BUCKET/$KEY" >/dev/null && echo "объект на месте"'
|
||||
|
||||
echo
|
||||
echo "## --apply, grace 1 с: удалён только он"
|
||||
run --apply --grace-seconds=1
|
||||
mc_api 'mc stat "api/$BUCKET/$KEY" >/dev/null 2>&1 && echo "объект всё ещё есть" || echo "объект удалён"'
|
||||
|
||||
echo
|
||||
echo "## Повторный запуск: удалять нечего"
|
||||
run --apply --grace-seconds=1
|
||||
Executable
+9
@@ -0,0 +1,9 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
cd "$(dirname "$0")/.."
|
||||
started() {
|
||||
docker inspect -f '{{.Name}} {{.State.Status}} started={{.State.StartedAt}}' $(docker compose ps -aq postgres redis s3 api lb)
|
||||
}
|
||||
started
|
||||
docker compose restart postgres redis s3 api lb
|
||||
started
|
||||
Executable
+79
@@ -0,0 +1,79 @@
|
||||
#!/usr/bin/env python3
|
||||
import hashlib
|
||||
import http.client
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import sys
|
||||
import time
|
||||
import uuid
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
sys.path.insert(0, str(ROOT / 'tests'))
|
||||
from client import Client
|
||||
|
||||
STATE = Path(os.environ.get('S3_OUTAGE_STATE', '/tmp/s3-outage-state.json'))
|
||||
SAMPLE = (ROOT / 'tests/fixtures/sample.png').read_bytes()
|
||||
HOST = os.environ.get('LB_HOST', 'lb')
|
||||
PORT = int(os.environ.get('LB_PORT', '8080'))
|
||||
|
||||
|
||||
def request(method, path, token=None, body=None, content_type=None, extra=None):
|
||||
headers = dict(extra or {})
|
||||
if token:
|
||||
headers['Authorization'] = 'Bearer ' + token
|
||||
if content_type:
|
||||
headers['Content-Type'] = content_type
|
||||
conn = http.client.HTTPConnection(HOST, PORT, timeout=30)
|
||||
started = time.monotonic()
|
||||
conn.request(method, path, body=body, headers=headers)
|
||||
response = conn.getresponse()
|
||||
data = response.read()
|
||||
elapsed = time.monotonic() - started
|
||||
conn.close()
|
||||
return response, data, elapsed
|
||||
|
||||
|
||||
def show(label, response, data, elapsed):
|
||||
code = ''
|
||||
if response.status >= 400:
|
||||
code = json.loads(data)['error']['code']
|
||||
print('%-32s -> %s %-12s %5.2fs X-Instance-Id=%s' % (label, response.status, code, elapsed, 'yes' if response.getheader('X-Instance-Id') else 'no'))
|
||||
|
||||
|
||||
def prepare():
|
||||
client = Client('http://%s:%d' % (HOST, PORT))
|
||||
alice = client.user('alice')
|
||||
chat = client.call('POST', '/api/v1/chats', 201, actor=alice, body={'title': 'outage', 'member_ids': [alice['id']]}, schema='Chat')['json']
|
||||
attachment, _ = client.attachment(alice, chat)
|
||||
STATE.write_text(json.dumps({'token': alice['token'], 'chat': chat['id'], 'attachment': attachment['id']}))
|
||||
|
||||
|
||||
def during():
|
||||
state = json.loads(STATE.read_text())
|
||||
token, chat, attachment = state['token'], state['chat'], state['attachment']
|
||||
show('GET /health/live', *request('GET', '/health/live'))
|
||||
show('GET /health/ready', *request('GET', '/health/ready'))
|
||||
show('GET /attachments/{id}', *request('GET', '/api/v1/attachments/' + attachment, token))
|
||||
show('GET /attachments/{id}/content', *request('GET', '/api/v1/attachments/' + attachment + '/content', token))
|
||||
show('POST /attachments', *request('POST', '/api/v1/chats/' + chat + '/attachments?filename=down.png', token, SAMPLE, 'image/png'))
|
||||
show('POST /messages (текст)', *request('POST', '/api/v1/chats/' + chat + '/messages', token, json.dumps({'text': 'пока S3 недоступен'}), 'application/json', {'Idempotency-Key': uuid.uuid4().hex}))
|
||||
|
||||
|
||||
def after():
|
||||
state = json.loads(STATE.read_text())
|
||||
token, attachment = state['token'], state['attachment']
|
||||
deadline = time.monotonic() + 30
|
||||
while True:
|
||||
response, data, elapsed = request('GET', '/health/ready')
|
||||
if response.status == 200 or time.monotonic() > deadline:
|
||||
break
|
||||
time.sleep(0.5)
|
||||
show('GET /health/ready', response, data, elapsed)
|
||||
response, data, elapsed = request('GET', '/api/v1/attachments/' + attachment + '/content', token)
|
||||
show('GET /attachments/{id}/content', response, data, elapsed)
|
||||
print('sha256 совпадает:', hashlib.sha256(data).hexdigest() == hashlib.sha256(SAMPLE).hexdigest())
|
||||
STATE.unlink()
|
||||
|
||||
|
||||
{'prepare': prepare, 'during': during, 'after': after}[sys.argv[1]]()
|
||||
Executable
+18
@@ -0,0 +1,18 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
cd "$(dirname "$0")/.."
|
||||
mode="${1:-stop}"
|
||||
python3 scripts/s3-outage.py prepare
|
||||
case "$mode" in
|
||||
stop) docker compose stop s3 2>&1 | grep -v Container || true ;;
|
||||
pause) docker compose pause s3 2>&1 | grep -v Container || true ;;
|
||||
*) echo "usage: s3-outage.sh stop|pause" >&2; exit 2 ;;
|
||||
esac
|
||||
echo "## S3: $mode"
|
||||
python3 scripts/s3-outage.py during
|
||||
case "$mode" in
|
||||
stop) docker compose start s3 2>&1 | grep -v Container || true ;;
|
||||
pause) docker compose unpause s3 2>&1 | grep -v Container || true ;;
|
||||
esac
|
||||
echo "## S3 восстановлен"
|
||||
python3 scripts/s3-outage.py after
|
||||
Executable
+11
@@ -0,0 +1,11 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
: "${TARGET_INSTANCE_ID:?test provides target API instance alias}"
|
||||
project="${COMPOSE_PROJECT_NAME:-messenger-lab4}"
|
||||
labels=$(docker inspect -f '{{index .Config.Labels "com.docker.compose.project"}}/{{index .Config.Labels "com.docker.compose.service"}}' "$TARGET_INSTANCE_ID")
|
||||
if [ "$labels" != "$project/api" ]; then
|
||||
echo "refusing to stop $TARGET_INSTANCE_ID: not an api replica of $project ($labels)" >&2
|
||||
exit 1
|
||||
fi
|
||||
docker stop -t 8 "$TARGET_INSTANCE_ID" >/dev/null
|
||||
docker inspect -f '{{.Name}} {{.State.Status}} finished={{.State.FinishedAt}}' "$TARGET_INSTANCE_ID"
|
||||
Executable
+264
@@ -0,0 +1,264 @@
|
||||
#!/usr/bin/env python3
|
||||
import argparse
|
||||
import datetime
|
||||
import hashlib
|
||||
import http.client
|
||||
import json
|
||||
import math
|
||||
import os
|
||||
from pathlib import Path
|
||||
import re
|
||||
import struct
|
||||
import subprocess
|
||||
import sys
|
||||
import threading
|
||||
import time
|
||||
import zlib
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
sys.path.insert(0, str(ROOT / 'tests'))
|
||||
from client import Client
|
||||
|
||||
HOST = os.environ.get('LB_HOST', 'lb')
|
||||
PORT = int(os.environ.get('LB_PORT', '8080'))
|
||||
SAMPLE = (ROOT / 'tests/fixtures/sample.png').read_bytes()
|
||||
MiB = 1024 * 1024
|
||||
|
||||
|
||||
def docker(*args, text=True):
|
||||
return subprocess.run(['docker', *args], check=True, capture_output=True, text=text).stdout
|
||||
|
||||
|
||||
def replicas():
|
||||
return [cid[:12] for cid in docker('compose', 'ps', '-q', 'api').split()]
|
||||
|
||||
|
||||
def chunk(kind, data):
|
||||
return struct.pack('>I', len(data)) + kind + data + struct.pack('>I', zlib.crc32(kind + data) & 0xffffffff)
|
||||
|
||||
|
||||
def padded_png(size):
|
||||
return SAMPLE[:33] + chunk(b'abCd', b'\x01' * (size - len(SAMPLE) - 12)) + SAMPLE[33:]
|
||||
|
||||
|
||||
def big_png(width, height):
|
||||
rows = (b'\0' + b'\x55' * (width * 3)) * height
|
||||
header = struct.pack('>IIBBBBB', width, height, 8, 2, 0, 0, 0)
|
||||
return b'\x89PNG\r\n\x1a\n' + chunk(b'IHDR', header) + chunk(b'IDAT', zlib.compress(rows, 9)) + chunk(b'IEND', b'')
|
||||
|
||||
|
||||
def big_jpeg(width, height):
|
||||
script = (
|
||||
"require('sharp')({create:{width:%d,height:%d,channels:3,background:'#336699'}})"
|
||||
".jpeg({quality:80}).toBuffer().then(b=>process.stdout.write(b))" % (width, height)
|
||||
)
|
||||
return docker('exec', replicas()[0], 'node', '-e', script, text=False)
|
||||
|
||||
|
||||
def cgroup_peak(cid):
|
||||
raw = docker('exec', cid, 'sh', '-c', 'cat /sys/fs/cgroup/memory/memory.max_usage_in_bytes 2>/dev/null || cat /sys/fs/cgroup/memory.peak 2>/dev/null || echo 0')
|
||||
return int(raw.strip()) // MiB
|
||||
|
||||
|
||||
def percentile(values, q):
|
||||
ordered = sorted(values)
|
||||
return round(ordered[max(0, math.ceil(q * len(ordered)) - 1)], 3)
|
||||
|
||||
|
||||
class Sampler:
|
||||
def __init__(self, cids):
|
||||
self.rss = {cid: 0 for cid in cids}
|
||||
self.cpu = {}
|
||||
self.stop_flag = threading.Event()
|
||||
self.threads = [threading.Thread(target=self.watch, args=(cid,)) for cid in cids]
|
||||
self.threads.append(threading.Thread(target=self.watch_cpu))
|
||||
|
||||
def watch(self, cid):
|
||||
script = 'while :; do grep VmRSS /proc/1/status; sleep 0.2; done'
|
||||
process = subprocess.Popen(['docker', 'exec', cid, 'sh', '-c', script], stdout=subprocess.PIPE, text=True)
|
||||
for line in process.stdout:
|
||||
if self.stop_flag.is_set():
|
||||
break
|
||||
match = re.match(r'VmRSS:\s+(\d+)', line)
|
||||
if match:
|
||||
self.rss[cid] = max(self.rss[cid], int(match.group(1)) // 1024)
|
||||
process.kill()
|
||||
process.wait()
|
||||
|
||||
def watch_cpu(self):
|
||||
while not self.stop_flag.is_set():
|
||||
lines = docker('stats', '--no-stream', '--format', '{{.Name}} {{.CPUPerc}} {{.MemUsage}}')
|
||||
for line in lines.splitlines():
|
||||
name, cpu, usage = line.split(' ', 2)
|
||||
value = float(cpu.rstrip('%'))
|
||||
entry = self.cpu.setdefault(name, {'max_cpu_percent': 0, 'last_mem': ''})
|
||||
entry['max_cpu_percent'] = max(entry['max_cpu_percent'], value)
|
||||
entry['last_mem'] = usage.split(' / ')[0]
|
||||
|
||||
def __enter__(self):
|
||||
[thread.start() for thread in self.threads]
|
||||
return self
|
||||
|
||||
def __exit__(self, *_):
|
||||
self.stop_flag.set()
|
||||
for thread in self.threads:
|
||||
thread.join(timeout=5)
|
||||
|
||||
|
||||
def restart_replicas():
|
||||
docker('compose', 'restart', 'api')
|
||||
deadline = time.monotonic() + 90
|
||||
expected = set(replicas())
|
||||
while time.monotonic() < deadline:
|
||||
seen = set()
|
||||
for _ in range(8):
|
||||
try:
|
||||
conn = http.client.HTTPConnection(HOST, PORT, timeout=3)
|
||||
conn.request('GET', '/health/ready')
|
||||
response = conn.getresponse()
|
||||
response.read()
|
||||
if response.status == 200:
|
||||
seen.add(response.getheader('X-Instance-Id'))
|
||||
conn.close()
|
||||
except OSError:
|
||||
pass
|
||||
if seen >= expected:
|
||||
return
|
||||
time.sleep(0.5)
|
||||
raise SystemExit('replicas did not return')
|
||||
|
||||
|
||||
def upload(path, token, body, content_type):
|
||||
conn = http.client.HTTPConnection(HOST, PORT, timeout=120)
|
||||
started = time.monotonic()
|
||||
conn.request('POST', path, body=body, headers={'Authorization': 'Bearer ' + token, 'Content-Type': content_type})
|
||||
response = conn.getresponse()
|
||||
data = response.read()
|
||||
elapsed = time.monotonic() - started
|
||||
conn.close()
|
||||
return response.status, elapsed, json.loads(data), response.getheader('X-Instance-Id')
|
||||
|
||||
|
||||
def download_sha(attachment_id, token):
|
||||
conn = http.client.HTTPConnection(HOST, PORT, timeout=60)
|
||||
conn.request('GET', '/api/v1/attachments/' + attachment_id + '/content', headers={'Authorization': 'Bearer ' + token})
|
||||
response = conn.getresponse()
|
||||
data = response.read()
|
||||
conn.close()
|
||||
return response.status, hashlib.sha256(data).hexdigest()
|
||||
|
||||
|
||||
def run_profile(name, body, content_type, concurrency, rounds, token, chat):
|
||||
restart_replicas()
|
||||
cids = replicas()
|
||||
idle = {cid: int(re.search(r'VmRSS:\s+(\d+)', docker('exec', cid, 'cat', '/proc/1/status')).group(1)) // 1024 for cid in cids}
|
||||
path = '/api/v1/chats/' + chat + '/attachments?filename=load.bin'
|
||||
expected_sha = hashlib.sha256(body).hexdigest()
|
||||
results = []
|
||||
lock = threading.Lock()
|
||||
|
||||
def worker():
|
||||
try:
|
||||
outcome = upload(path, token, body, content_type)
|
||||
except OSError as error:
|
||||
outcome = (0, 0.0, {'error': {'code': type(error).__name__}}, None)
|
||||
with lock:
|
||||
results.append(outcome)
|
||||
|
||||
with Sampler(cids) as sampler:
|
||||
started = time.monotonic()
|
||||
for _ in range(rounds):
|
||||
threads = [threading.Thread(target=worker) for _ in range(concurrency)]
|
||||
[thread.start() for thread in threads]
|
||||
[thread.join() for thread in threads]
|
||||
wall = time.monotonic() - started
|
||||
time.sleep(0.5)
|
||||
|
||||
created = [item for item in results if item[0] == 201]
|
||||
latencies = [item[1] for item in created]
|
||||
statuses = {}
|
||||
for item in results:
|
||||
statuses[str(item[0])] = statuses.get(str(item[0]), 0) + 1
|
||||
sha_ok = all(item[2]['sha256'] == expected_sha for item in created)
|
||||
stored_ok = None
|
||||
if created:
|
||||
status, sha = download_sha(created[0][2]['id'], token)
|
||||
stored_ok = status == 200 and sha == expected_sha
|
||||
per_replica = {}
|
||||
for cid in cids:
|
||||
info = docker('exec', cid, 'cat', '/proc/1/status')
|
||||
hwm = int(re.search(r'VmHWM:\s+(\d+)', info).group(1)) // 1024
|
||||
oom = docker('inspect', '-f', '{{.State.OOMKilled}}', cid).strip()
|
||||
temp = docker('exec', cid, 'sh', '-c', 'ls /tmp | grep -c messenger-upload || true').strip()
|
||||
per_replica[cid] = {
|
||||
'idle_rss_mib': idle[cid],
|
||||
'sampled_peak_rss_mib': sampler.rss[cid],
|
||||
'vm_hwm_mib': hwm,
|
||||
'cgroup_max_usage_with_page_cache_mib': cgroup_peak(cid),
|
||||
'oom_killed': oom,
|
||||
'temp_dirs_left': int(temp or 0),
|
||||
'served': sum(1 for item in results if item[3] == cid),
|
||||
}
|
||||
return {
|
||||
'profile': name,
|
||||
'body_bytes': len(body),
|
||||
'content_type': content_type,
|
||||
'concurrency': concurrency,
|
||||
'rounds': rounds,
|
||||
'requests': len(results),
|
||||
'statuses': statuses,
|
||||
'wall_seconds': round(wall, 2),
|
||||
'successful_rps': round(len(created) / wall, 2),
|
||||
'error_rate': round(1 - len(created) / len(results), 4),
|
||||
'latency_seconds': None if not latencies else {
|
||||
'p50': percentile(latencies, 0.5),
|
||||
'p95': percentile(latencies, 0.95),
|
||||
'p99': percentile(latencies, 0.99),
|
||||
},
|
||||
'sha256_in_responses_ok': sha_ok,
|
||||
'download_sha256_ok': stored_ok,
|
||||
'replicas': per_replica,
|
||||
'docker_stats': sampler.cpu,
|
||||
}
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument('--output')
|
||||
parser.add_argument('--rounds', type=int, default=3)
|
||||
args = parser.parse_args()
|
||||
client = Client('http://%s:%d' % (HOST, PORT))
|
||||
alice = client.user('upload_load')
|
||||
chat = client.call('POST', '/api/v1/chats', 201, actor=alice, body={'title': 'upload experiment', 'member_ids': [alice['id']]}, schema='Chat')['json']
|
||||
token, chat_id = alice['token'], chat['id']
|
||||
jpeg_40mp = big_jpeg(8000, 5000)
|
||||
png_40mp = big_png(8000, 5000)
|
||||
profiles = [
|
||||
('sample 112 KiB x10', SAMPLE, 'image/png', 10),
|
||||
('png 10 MiB x1', padded_png(10 * MiB), 'image/png', 1),
|
||||
('png 10 MiB x10', padded_png(10 * MiB), 'image/png', 10),
|
||||
('png 40 MP x10', png_40mp, 'image/png', 10),
|
||||
('jpeg 40 MP x10', jpeg_40mp, 'image/jpeg', 10),
|
||||
('png 10 MiB x20', padded_png(10 * MiB), 'image/png', 20),
|
||||
('png 10 MiB x40 (выше лимита 16 на реплику)', padded_png(10 * MiB), 'image/png', 40),
|
||||
]
|
||||
results = []
|
||||
for name, body, content_type, concurrency in profiles:
|
||||
print('profile:', name, flush=True)
|
||||
results.append(run_profile(name, body, content_type, concurrency, args.rounds, token, chat_id))
|
||||
count = docker('compose', 'exec', '-T', 'postgres', 'psql', '-U', 'messenger', '-d', 'messenger', '-tAc', 'SELECT (SELECT count(*) FROM attachments), (SELECT count(*) FROM messages)').strip()
|
||||
limits = docker('inspect', '-f', '{{.HostConfig.NanoCpus}} {{.HostConfig.Memory}}', replicas()[0]).split()
|
||||
report = {
|
||||
'date': datetime.datetime.now(datetime.timezone.utc).strftime('%Y-%m-%dT%H:%M:%SZ'),
|
||||
'api_replica_limits': {'cpus': int(limits[0]) / 1e9, 'memory_mib': int(limits[1]) // MiB},
|
||||
'rows_after_attachments_messages': count,
|
||||
'results': results,
|
||||
}
|
||||
text = json.dumps(report, ensure_ascii=False, indent=2)
|
||||
if args.output:
|
||||
Path(args.output).write_text(text + '\n', encoding='utf-8')
|
||||
print(text)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
@@ -0,0 +1,25 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { AttachmentsModule } from './attachments/attachments.module';
|
||||
import { AuthModule } from './auth/auth.module';
|
||||
import { CacheModule } from './cache/cache.module';
|
||||
import { ChatsModule } from './chats/chats.module';
|
||||
import { DbModule } from './db/db.module';
|
||||
import { HealthModule } from './health/health.module';
|
||||
import { MessagesModule } from './messages/messages.module';
|
||||
import { StorageModule } from './storage/storage.module';
|
||||
import { UsersModule } from './users/users.module';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
DbModule,
|
||||
CacheModule,
|
||||
StorageModule,
|
||||
AuthModule,
|
||||
HealthModule,
|
||||
UsersModule,
|
||||
ChatsModule,
|
||||
MessagesModule,
|
||||
AttachmentsModule,
|
||||
],
|
||||
})
|
||||
export class AppModule {}
|
||||
@@ -0,0 +1,79 @@
|
||||
import {
|
||||
Controller,
|
||||
Get,
|
||||
HttpCode,
|
||||
Param,
|
||||
Post,
|
||||
Query,
|
||||
Req,
|
||||
Res,
|
||||
UseGuards,
|
||||
} from '@nestjs/common';
|
||||
import { FastifyReply, FastifyRequest } from 'fastify';
|
||||
import { Readable } from 'stream';
|
||||
import { AuthGuard } from '../auth/auth.guard';
|
||||
import { CurrentUser } from '../auth/current-user.decorator';
|
||||
import { User } from '../common/domain';
|
||||
import { AttachmentsService } from './attachments.service';
|
||||
import {
|
||||
AttachmentResponse,
|
||||
toAttachmentResponse,
|
||||
} from './dto/attachment.response';
|
||||
|
||||
@Controller()
|
||||
@UseGuards(AuthGuard)
|
||||
export class AttachmentsController {
|
||||
constructor(private readonly attachments: AttachmentsService) {}
|
||||
|
||||
@Post('api/v1/chats/:chatId/attachments')
|
||||
@HttpCode(201)
|
||||
async upload(
|
||||
@Param('chatId') chatId: string,
|
||||
@Query('filename') filename: unknown,
|
||||
@Req() request: FastifyRequest,
|
||||
@CurrentUser() user: User,
|
||||
): Promise<AttachmentResponse> {
|
||||
const attachment = await this.attachments.upload({
|
||||
chatId,
|
||||
ownerId: user.id,
|
||||
filename,
|
||||
contentType: request.uploadContentType,
|
||||
contentLength: request.headers['content-length'],
|
||||
body: request.body as Readable | undefined,
|
||||
});
|
||||
return toAttachmentResponse(attachment);
|
||||
}
|
||||
|
||||
@Get('api/v1/attachments/:attachmentId')
|
||||
async metadata(
|
||||
@Param('attachmentId') attachmentId: string,
|
||||
@CurrentUser() user: User,
|
||||
): Promise<AttachmentResponse> {
|
||||
return toAttachmentResponse(
|
||||
await this.attachments.getVisible(attachmentId, user.id),
|
||||
);
|
||||
}
|
||||
|
||||
@Get('api/v1/attachments/:attachmentId/content')
|
||||
async content(
|
||||
@Param('attachmentId') attachmentId: string,
|
||||
@Query('variant') variant: unknown,
|
||||
@CurrentUser() user: User,
|
||||
@Res() reply: FastifyReply,
|
||||
): Promise<void> {
|
||||
const { attachment, content } = await this.attachments.openContent(
|
||||
attachmentId,
|
||||
user.id,
|
||||
variant,
|
||||
);
|
||||
await reply
|
||||
.header('content-type', attachment.contentType)
|
||||
.header('content-length', attachment.sizeBytes)
|
||||
.header('x-content-type-options', 'nosniff')
|
||||
.header(
|
||||
'content-disposition',
|
||||
`inline; filename*=UTF-8''${encodeURIComponent(attachment.filename)}`,
|
||||
)
|
||||
.send(content);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { AttachmentsController } from './attachments.controller';
|
||||
import { AttachmentsService } from './attachments.service';
|
||||
|
||||
@Module({
|
||||
controllers: [AttachmentsController],
|
||||
providers: [AttachmentsService],
|
||||
})
|
||||
export class AttachmentsModule {}
|
||||
@@ -0,0 +1,202 @@
|
||||
import { Injectable, Logger, OnModuleInit } from '@nestjs/common';
|
||||
import { Attachment, Prisma } from '@prisma/client';
|
||||
import { randomUUID } from 'crypto';
|
||||
import { mkdtemp, readdir, rm, stat } from 'fs/promises';
|
||||
import { tmpdir } from 'os';
|
||||
import { join } from 'path';
|
||||
import { Readable } from 'stream';
|
||||
import { assertChatMember } from '../chats/chat-access';
|
||||
import { codePointLength } from '../common/dto/code-point-length.validator';
|
||||
import { ApiError } from '../common/filters/api-error';
|
||||
import { validate as isUuid } from '../common/uuid';
|
||||
import { isDatabaseUnavailable, PrismaService } from '../db/prisma.service';
|
||||
import { S3Service } from '../storage/s3.service';
|
||||
import { assertValidImage, isSupportedContentType } from './image-validation';
|
||||
import { receiveBody } from './upload-body';
|
||||
|
||||
const TEMP_PREFIX = 'messenger-upload-';
|
||||
const STALE_TEMP_MS = 60 * 60 * 1000;
|
||||
const MAX_FILENAME_CODE_POINTS = 255;
|
||||
const FORBIDDEN_FILENAME = /[/\\\p{Cc}\p{Zl}\p{Zp}]/u;
|
||||
const KNOWN_VARIANTS = new Set(['thumbnail', 'crop', 'optimized']);
|
||||
|
||||
export interface UploadInput {
|
||||
chatId: string;
|
||||
ownerId: string;
|
||||
filename: unknown;
|
||||
contentType: string | undefined;
|
||||
contentLength: string | undefined;
|
||||
body: Readable | undefined;
|
||||
}
|
||||
|
||||
@Injectable()
|
||||
export class AttachmentsService implements OnModuleInit {
|
||||
private readonly logger = new Logger('AttachmentsService');
|
||||
private readonly maxBytes = Number(process.env.MAX_UPLOAD_BYTES ?? 10485760);
|
||||
private readonly maxPixels = Number(process.env.MAX_IMAGE_PIXELS ?? 40000000);
|
||||
private readonly maxConcurrent = Number(process.env.MAX_CONCURRENT_UPLOADS ?? 16);
|
||||
private readonly bodyTimeoutMs = Number(process.env.UPLOAD_BODY_TIMEOUT_MS ?? 60000);
|
||||
private activeUploads = 0;
|
||||
|
||||
constructor(
|
||||
private readonly prisma: PrismaService,
|
||||
private readonly s3: S3Service,
|
||||
) {}
|
||||
|
||||
async onModuleInit(): Promise<void> {
|
||||
await this.removeStaleTempDirs();
|
||||
}
|
||||
|
||||
async upload(input: UploadInput): Promise<Attachment> {
|
||||
await assertChatMember(this.prisma, input.chatId, input.ownerId);
|
||||
const filename = parseFilename(input.filename);
|
||||
const contentType = parseContentType(input.contentType);
|
||||
if (Number(input.contentLength) > this.maxBytes) {
|
||||
throw new ApiError('payload_too_large', 'attachment exceeds size limit');
|
||||
}
|
||||
if (this.activeUploads >= this.maxConcurrent) {
|
||||
throw new ApiError('unavailable', 'too many concurrent uploads');
|
||||
}
|
||||
this.activeUploads++;
|
||||
const dir = await mkdtemp(join(tmpdir(), TEMP_PREFIX));
|
||||
try {
|
||||
return await this.store(input, filename, contentType, join(dir, 'body'));
|
||||
} finally {
|
||||
this.activeUploads--;
|
||||
await rm(dir, { recursive: true, force: true });
|
||||
}
|
||||
}
|
||||
|
||||
async getVisible(attachmentId: string, userId: string): Promise<Attachment> {
|
||||
if (!isUuid(attachmentId)) {
|
||||
throw new ApiError('invalid_request', 'attachmentId must be a valid UUID');
|
||||
}
|
||||
const row = await this.prisma.attachment.findFirst({
|
||||
where: {
|
||||
id: attachmentId,
|
||||
chat: { members: { some: { userId } } },
|
||||
},
|
||||
});
|
||||
if (!row) {
|
||||
throw new ApiError('not_found', 'attachment not found');
|
||||
}
|
||||
return row;
|
||||
}
|
||||
|
||||
async openContent(
|
||||
attachmentId: string,
|
||||
userId: string,
|
||||
variant: unknown,
|
||||
): Promise<{ attachment: Attachment; content: Readable }> {
|
||||
const attachment = await this.getVisible(attachmentId, userId);
|
||||
assertOriginalVariant(variant);
|
||||
return { attachment, content: await this.s3.getStream(attachment.objectKey) };
|
||||
}
|
||||
|
||||
private async store(
|
||||
input: UploadInput,
|
||||
filename: string,
|
||||
contentType: string,
|
||||
path: string,
|
||||
): Promise<Attachment> {
|
||||
if (!input.body) {
|
||||
throw new ApiError('invalid_request', 'request body must not be empty');
|
||||
}
|
||||
const { size, sha256 } = await receiveBody(
|
||||
input.body,
|
||||
path,
|
||||
this.maxBytes,
|
||||
this.bodyTimeoutMs,
|
||||
);
|
||||
if (size === 0) {
|
||||
throw new ApiError('invalid_request', 'request body must not be empty');
|
||||
}
|
||||
await assertValidImage(path, size, contentType, this.maxPixels);
|
||||
const id = randomUUID();
|
||||
const objectKey = `attachments/${id}`;
|
||||
await this.s3.putFile({ key: objectKey, path, size, contentType, sha256 });
|
||||
const now = new Date();
|
||||
try {
|
||||
return await this.prisma.attachment.create({
|
||||
data: {
|
||||
id,
|
||||
chatId: input.chatId,
|
||||
ownerId: input.ownerId,
|
||||
filename,
|
||||
contentType,
|
||||
sizeBytes: size,
|
||||
sha256,
|
||||
objectKey,
|
||||
createdAt: now,
|
||||
updatedAt: now,
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
if (isDefiniteRejection(error)) {
|
||||
await this.s3.delete(objectKey).catch(() => {
|
||||
this.logger.warn(`compensation failed for ${objectKey}`);
|
||||
});
|
||||
}
|
||||
if (error instanceof Prisma.PrismaClientUnknownRequestError) {
|
||||
this.logger.error(`metadata insert failed for ${objectKey}`);
|
||||
throw new ApiError('unavailable', 'database unavailable');
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
private async removeStaleTempDirs(): Promise<void> {
|
||||
const root = tmpdir();
|
||||
const names = await readdir(root);
|
||||
for (const name of names.filter((n) => n.startsWith(TEMP_PREFIX))) {
|
||||
const path = join(root, name);
|
||||
const info = await stat(path).catch(() => null);
|
||||
if (info && Date.now() - info.mtimeMs > STALE_TEMP_MS) {
|
||||
await rm(path, { recursive: true, force: true });
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function assertOriginalVariant(raw: unknown): void {
|
||||
if (raw === undefined || raw === 'original') {
|
||||
return;
|
||||
}
|
||||
if (typeof raw === 'string' && KNOWN_VARIANTS.has(raw)) {
|
||||
throw new ApiError('not_found', 'attachment variant not found');
|
||||
}
|
||||
throw new ApiError('invalid_request', 'unknown attachment variant');
|
||||
}
|
||||
|
||||
function isDefiniteRejection(error: unknown): boolean {
|
||||
return (
|
||||
error instanceof Prisma.PrismaClientKnownRequestError &&
|
||||
!isDatabaseUnavailable(error)
|
||||
);
|
||||
}
|
||||
|
||||
function parseFilename(raw: unknown): string {
|
||||
if (
|
||||
typeof raw !== 'string' ||
|
||||
raw.trim() === '' ||
|
||||
codePointLength(raw) > MAX_FILENAME_CODE_POINTS ||
|
||||
FORBIDDEN_FILENAME.test(raw)
|
||||
) {
|
||||
throw new ApiError(
|
||||
'invalid_request',
|
||||
'filename must be 1-255 characters without path separators or control characters',
|
||||
);
|
||||
}
|
||||
return raw;
|
||||
}
|
||||
|
||||
function parseContentType(raw: string | undefined): string {
|
||||
const contentType = raw?.split(';')[0].trim().toLowerCase() ?? '';
|
||||
if (!isSupportedContentType(contentType)) {
|
||||
throw new ApiError(
|
||||
'unsupported_media_type',
|
||||
'Content-Type must be image/png or image/jpeg',
|
||||
);
|
||||
}
|
||||
return contentType;
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
import { Attachment } from '@prisma/client';
|
||||
|
||||
export interface AttachmentResponse {
|
||||
id: string;
|
||||
chat_id: string;
|
||||
owner_id: string;
|
||||
filename: string;
|
||||
content_type: string;
|
||||
size_bytes: number;
|
||||
sha256: string;
|
||||
status: 'ready';
|
||||
variants: never[];
|
||||
error_code: null;
|
||||
created_at: string;
|
||||
updated_at: string;
|
||||
}
|
||||
|
||||
export function toAttachmentResponse(row: Attachment): AttachmentResponse {
|
||||
return {
|
||||
id: row.id,
|
||||
chat_id: row.chatId,
|
||||
owner_id: row.ownerId,
|
||||
filename: row.filename,
|
||||
content_type: row.contentType,
|
||||
size_bytes: row.sizeBytes,
|
||||
sha256: row.sha256,
|
||||
status: 'ready',
|
||||
variants: [],
|
||||
error_code: null,
|
||||
created_at: row.createdAt.toISOString(),
|
||||
updated_at: row.updatedAt.toISOString(),
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,105 @@
|
||||
import { open } from 'fs/promises';
|
||||
import sharp from 'sharp';
|
||||
import { ApiError } from '../common/filters/api-error';
|
||||
|
||||
sharp.cache(false);
|
||||
sharp.concurrency(1);
|
||||
|
||||
const PNG_SIGNATURE = Buffer.from('89504e470d0a1a0a', 'hex');
|
||||
const JPEG_SIGNATURE = Buffer.from('ffd8ff', 'hex');
|
||||
const PNG_END_CHUNK = Buffer.from('0000000049454e44ae426082', 'hex');
|
||||
const PNG_TAIL_BYTES = 64 * 1024;
|
||||
const MAX_CONCURRENT_DECODES = 2;
|
||||
|
||||
const FORMAT_BY_CONTENT_TYPE: Record<string, string> = {
|
||||
'image/png': 'png',
|
||||
'image/jpeg': 'jpeg',
|
||||
};
|
||||
|
||||
let activeDecodes = 0;
|
||||
const decodeQueue: (() => void)[] = [];
|
||||
|
||||
export function isSupportedContentType(contentType: string): boolean {
|
||||
return contentType in FORMAT_BY_CONTENT_TYPE;
|
||||
}
|
||||
|
||||
export async function assertValidImage(
|
||||
path: string,
|
||||
size: number,
|
||||
contentType: string,
|
||||
maxPixels: number,
|
||||
): Promise<void> {
|
||||
const invalid = new ApiError(
|
||||
'unsupported_media_type',
|
||||
'file is not a valid image of the declared type',
|
||||
);
|
||||
const format = FORMAT_BY_CONTENT_TYPE[contentType];
|
||||
if (!(await hasExpectedFraming(path, size, format))) {
|
||||
throw invalid;
|
||||
}
|
||||
const { width, height, format: actual } = await sharp(path, {
|
||||
limitInputPixels: false,
|
||||
})
|
||||
.metadata()
|
||||
.catch(() => {
|
||||
throw invalid;
|
||||
});
|
||||
if (!width || !height || actual !== format) {
|
||||
throw invalid;
|
||||
}
|
||||
if (width * height > maxPixels) {
|
||||
throw new ApiError('payload_too_large', 'image exceeds pixel limit');
|
||||
}
|
||||
await acquireDecodeSlot();
|
||||
try {
|
||||
await sharp(path, {
|
||||
failOn: 'warning',
|
||||
sequentialRead: true,
|
||||
limitInputPixels: maxPixels,
|
||||
}).stats();
|
||||
} catch {
|
||||
throw invalid;
|
||||
} finally {
|
||||
releaseDecodeSlot();
|
||||
}
|
||||
}
|
||||
|
||||
async function hasExpectedFraming(
|
||||
path: string,
|
||||
size: number,
|
||||
format: string,
|
||||
): Promise<boolean> {
|
||||
const file = await open(path, 'r');
|
||||
try {
|
||||
const head = Buffer.alloc(PNG_SIGNATURE.length);
|
||||
await file.read(head, 0, head.length, 0);
|
||||
if (format === 'jpeg') {
|
||||
return head.subarray(0, JPEG_SIGNATURE.length).equals(JPEG_SIGNATURE);
|
||||
}
|
||||
if (!head.equals(PNG_SIGNATURE)) {
|
||||
return false;
|
||||
}
|
||||
const tail = Buffer.alloc(Math.min(size, PNG_TAIL_BYTES));
|
||||
await file.read(tail, 0, tail.length, size - tail.length);
|
||||
return tail.includes(PNG_END_CHUNK);
|
||||
} finally {
|
||||
await file.close();
|
||||
}
|
||||
}
|
||||
|
||||
function acquireDecodeSlot(): Promise<void> {
|
||||
if (activeDecodes < MAX_CONCURRENT_DECODES) {
|
||||
activeDecodes++;
|
||||
return Promise.resolve();
|
||||
}
|
||||
return new Promise((resolve) => decodeQueue.push(resolve));
|
||||
}
|
||||
|
||||
function releaseDecodeSlot(): void {
|
||||
const next = decodeQueue.shift();
|
||||
if (next) {
|
||||
next();
|
||||
} else {
|
||||
activeDecodes--;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
import { PrismaClient } from '@prisma/client';
|
||||
import { S3Service } from '../storage/s3.service';
|
||||
|
||||
const MAX_REPORTED_KEYS = 20;
|
||||
|
||||
export interface ReconcileOptions {
|
||||
apply: boolean;
|
||||
graceMs: number;
|
||||
prefix?: string;
|
||||
now?: Date;
|
||||
pageSize?: number;
|
||||
}
|
||||
|
||||
export interface ReconcileReport {
|
||||
mode: 'apply' | 'dry-run';
|
||||
grace_seconds: number;
|
||||
scanned: number;
|
||||
fresh_skipped: number;
|
||||
referenced: number;
|
||||
orphans: number;
|
||||
deleted: number;
|
||||
failed: number;
|
||||
orphan_keys: string[];
|
||||
duration_ms: number;
|
||||
}
|
||||
|
||||
export async function reconcile(
|
||||
prisma: Pick<PrismaClient, 'attachment'>,
|
||||
s3: S3Service,
|
||||
options: ReconcileOptions,
|
||||
): Promise<ReconcileReport> {
|
||||
const started = Date.now();
|
||||
const cutoff = (options.now ?? new Date()).getTime() - options.graceMs;
|
||||
const report: ReconcileReport = {
|
||||
mode: options.apply ? 'apply' : 'dry-run',
|
||||
grace_seconds: options.graceMs / 1000,
|
||||
scanned: 0,
|
||||
fresh_skipped: 0,
|
||||
referenced: 0,
|
||||
orphans: 0,
|
||||
deleted: 0,
|
||||
failed: 0,
|
||||
orphan_keys: [],
|
||||
duration_ms: 0,
|
||||
};
|
||||
let token: string | undefined;
|
||||
do {
|
||||
const page = await s3.listObjects(
|
||||
options.prefix ?? 'attachments/',
|
||||
options.pageSize ?? 500,
|
||||
token,
|
||||
);
|
||||
token = page.nextToken;
|
||||
report.scanned += page.objects.length;
|
||||
const old = page.objects.filter((object) => object.lastModified.getTime() < cutoff);
|
||||
report.fresh_skipped += page.objects.length - old.length;
|
||||
const rows = await prisma.attachment.findMany({
|
||||
where: { objectKey: { in: old.map((object) => object.key) } },
|
||||
select: { objectKey: true },
|
||||
});
|
||||
const known = new Set(rows.map((row) => row.objectKey));
|
||||
for (const { key } of old) {
|
||||
if (known.has(key)) {
|
||||
report.referenced++;
|
||||
continue;
|
||||
}
|
||||
report.orphans++;
|
||||
if (report.orphan_keys.length < MAX_REPORTED_KEYS) {
|
||||
report.orphan_keys.push(key);
|
||||
}
|
||||
if (options.apply) {
|
||||
await s3.delete(key).then(
|
||||
() => report.deleted++,
|
||||
() => report.failed++,
|
||||
);
|
||||
}
|
||||
}
|
||||
} while (token);
|
||||
report.duration_ms = Date.now() - started;
|
||||
return report;
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
import { createHash } from 'crypto';
|
||||
import { FastifyInstance, FastifyRequest } from 'fastify';
|
||||
import { createWriteStream } from 'fs';
|
||||
import { Readable } from 'stream';
|
||||
import { ApiError } from '../common/filters/api-error';
|
||||
|
||||
const UPLOAD_ROUTE = '/api/v1/chats/:chatId/attachments';
|
||||
const PASS_THROUGH_TYPE = 'application/x-messenger-upload';
|
||||
|
||||
declare module 'fastify' {
|
||||
interface FastifyRequest {
|
||||
uploadContentType?: string;
|
||||
}
|
||||
}
|
||||
|
||||
export function registerUploadBody(instance: FastifyInstance): void {
|
||||
instance.addContentTypeParser(PASS_THROUGH_TYPE, (_request, payload, done) => {
|
||||
done(null, payload);
|
||||
});
|
||||
instance.addHook('onRequest', (request: FastifyRequest, _reply, done) => {
|
||||
if (request.method === 'POST' && request.routeOptions.url === UPLOAD_ROUTE) {
|
||||
request.uploadContentType = request.headers['content-type'];
|
||||
request.headers['content-type'] = PASS_THROUGH_TYPE;
|
||||
}
|
||||
done();
|
||||
});
|
||||
}
|
||||
|
||||
export interface ReceivedBody {
|
||||
size: number;
|
||||
sha256: string;
|
||||
}
|
||||
|
||||
export function receiveBody(
|
||||
body: Readable,
|
||||
path: string,
|
||||
maxBytes: number,
|
||||
timeoutMs: number,
|
||||
): Promise<ReceivedBody> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const hash = createHash('sha256');
|
||||
const out = createWriteStream(path, { flags: 'wx', mode: 0o600 });
|
||||
let size = 0;
|
||||
let settled = false;
|
||||
|
||||
const timer = setTimeout(() => body.destroy(), timeoutMs);
|
||||
const settle = (done: () => void): void => {
|
||||
if (settled) {
|
||||
return;
|
||||
}
|
||||
settled = true;
|
||||
clearTimeout(timer);
|
||||
body.off('data', onData);
|
||||
body.off('end', onEnd);
|
||||
body.off('close', onClose);
|
||||
done();
|
||||
};
|
||||
const fail = (error: ApiError): void => {
|
||||
settle(() => {
|
||||
out.destroy();
|
||||
reject(error);
|
||||
});
|
||||
};
|
||||
const onData = (chunk: Buffer): void => {
|
||||
size += chunk.length;
|
||||
if (size > maxBytes) {
|
||||
fail(new ApiError('payload_too_large', 'attachment exceeds size limit'));
|
||||
return;
|
||||
}
|
||||
hash.update(chunk);
|
||||
if (!out.write(chunk)) {
|
||||
body.pause();
|
||||
out.once('drain', () => {
|
||||
if (!settled) {
|
||||
body.resume();
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
const onEnd = (): void => {
|
||||
settle(() => {
|
||||
out.end(() =>
|
||||
resolve({ size, sha256: hash.digest('hex') }),
|
||||
);
|
||||
});
|
||||
};
|
||||
const onClose = (): void => {
|
||||
fail(new ApiError('invalid_request', 'upload was interrupted'));
|
||||
};
|
||||
|
||||
out.on('error', () => {
|
||||
fail(new ApiError('unavailable', 'temporary storage unavailable'));
|
||||
});
|
||||
body.on('data', onData);
|
||||
body.on('end', onEnd);
|
||||
body.on('close', onClose);
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
import {
|
||||
Controller,
|
||||
Delete,
|
||||
Headers,
|
||||
HttpCode,
|
||||
Post,
|
||||
Req,
|
||||
UseGuards,
|
||||
} from '@nestjs/common';
|
||||
import { FastifyRequest } from 'fastify';
|
||||
import { ApiError } from '../common/filters/api-error';
|
||||
import { toUserResponse, UserResponse } from '../users/dto/user.response';
|
||||
import { AuthGuard } from './auth.guard';
|
||||
import { AuthService } from './auth.service';
|
||||
|
||||
interface SessionResponse {
|
||||
token: string;
|
||||
token_type: 'Bearer';
|
||||
expires_at: string;
|
||||
user: UserResponse;
|
||||
}
|
||||
|
||||
@Controller('api/v1/auth/sessions')
|
||||
export class AuthController {
|
||||
constructor(private readonly auth: AuthService) {}
|
||||
|
||||
@Post()
|
||||
@HttpCode(201)
|
||||
async create(
|
||||
@Headers('authorization') authorization: string | undefined,
|
||||
@Req() request: FastifyRequest,
|
||||
): Promise<SessionResponse> {
|
||||
if (request.body !== undefined) {
|
||||
throw new ApiError('invalid_request', 'request body is not allowed');
|
||||
}
|
||||
const session = await this.auth.login(authorization);
|
||||
return {
|
||||
token: session.token,
|
||||
token_type: 'Bearer',
|
||||
expires_at: session.expiresAt.toISOString(),
|
||||
user: toUserResponse(session.user),
|
||||
};
|
||||
}
|
||||
|
||||
@Delete('current')
|
||||
@HttpCode(204)
|
||||
@UseGuards(AuthGuard)
|
||||
async revoke(@Req() request: FastifyRequest): Promise<void> {
|
||||
await this.auth.revoke(request.sessionTokenHash!);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
import { CanActivate, ExecutionContext, Injectable } from '@nestjs/common';
|
||||
import { FastifyRequest } from 'fastify';
|
||||
import { ApiError } from '../common/filters/api-error';
|
||||
import { AuthService } from './auth.service';
|
||||
|
||||
const BEARER_CHALLENGE = 'Bearer realm="messenger"';
|
||||
|
||||
@Injectable()
|
||||
export class AuthGuard implements CanActivate {
|
||||
constructor(private readonly auth: AuthService) {}
|
||||
|
||||
async canActivate(context: ExecutionContext): Promise<boolean> {
|
||||
const request = context.switchToHttp().getRequest<FastifyRequest>();
|
||||
const match = /^Bearer (\S+)$/i.exec(request.headers.authorization ?? '');
|
||||
const session = match ? await this.auth.resolve(match[1]) : null;
|
||||
if (!session) {
|
||||
throw new ApiError('unauthorized', 'missing or invalid session', {
|
||||
'WWW-Authenticate': BEARER_CHALLENGE,
|
||||
});
|
||||
}
|
||||
request.currentUser = session.user;
|
||||
request.sessionTokenHash = session.tokenHash;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
import { Global, Module } from '@nestjs/common';
|
||||
import { AuthController } from './auth.controller';
|
||||
import { AuthGuard } from './auth.guard';
|
||||
import { AuthService } from './auth.service';
|
||||
|
||||
@Global()
|
||||
@Module({
|
||||
controllers: [AuthController],
|
||||
providers: [AuthService, AuthGuard],
|
||||
exports: [AuthService, AuthGuard],
|
||||
})
|
||||
export class AuthModule {}
|
||||
@@ -0,0 +1,155 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { createHash, randomBytes } from 'crypto';
|
||||
import { CacheService } from '../cache/cache.service';
|
||||
import { codePointLength } from '../common/dto/code-point-length.validator';
|
||||
import { User } from '../common/domain';
|
||||
import { ApiError } from '../common/filters/api-error';
|
||||
import { PrismaService } from '../db/prisma.service';
|
||||
import { toUser } from '../users/users.service';
|
||||
import { verifyPassword } from './password';
|
||||
|
||||
const BASIC_CHALLENGE = 'Basic realm="messenger", charset="UTF-8"';
|
||||
const USERNAME_PATTERN = /^[a-z0-9_]{3,32}$/;
|
||||
const CACHE_TTL_MS = 60_000;
|
||||
const REVOKED = 'revoked';
|
||||
|
||||
interface CachedSession {
|
||||
user: User;
|
||||
expiresAt: number;
|
||||
}
|
||||
|
||||
export interface CreatedSession {
|
||||
token: string;
|
||||
expiresAt: Date;
|
||||
user: User;
|
||||
}
|
||||
|
||||
export interface ResolvedSession {
|
||||
tokenHash: string;
|
||||
user: User;
|
||||
}
|
||||
|
||||
@Injectable()
|
||||
export class AuthService {
|
||||
private readonly ttlSeconds = readSessionTtl();
|
||||
|
||||
constructor(
|
||||
private readonly prisma: PrismaService,
|
||||
private readonly cache: CacheService,
|
||||
) {}
|
||||
|
||||
async login(authorization: string | undefined): Promise<CreatedSession> {
|
||||
const credentials = parseBasic(authorization);
|
||||
if (!credentials) {
|
||||
throw invalidCredentials();
|
||||
}
|
||||
const row = await this.prisma.user.findUnique({
|
||||
where: { username: credentials.username },
|
||||
});
|
||||
const valid = await verifyPassword(
|
||||
row?.passwordHash ?? null,
|
||||
credentials.password,
|
||||
);
|
||||
if (!row || !valid) {
|
||||
throw invalidCredentials();
|
||||
}
|
||||
const token = randomBytes(32).toString('base64url');
|
||||
const expiresAt = new Date(Date.now() + this.ttlSeconds * 1000);
|
||||
await this.prisma.session.create({
|
||||
data: { tokenHash: hashToken(token), userId: row.id, expiresAt },
|
||||
});
|
||||
return { token, expiresAt, user: toUser(row) };
|
||||
}
|
||||
|
||||
async resolve(token: string): Promise<ResolvedSession | null> {
|
||||
const tokenHash = hashToken(token);
|
||||
const key = cacheKey(tokenHash);
|
||||
const cached = await this.cache.get(key);
|
||||
if (cached === REVOKED) {
|
||||
return null;
|
||||
}
|
||||
if (cached) {
|
||||
const entry = JSON.parse(cached) as CachedSession;
|
||||
if (entry.expiresAt > Date.now()) {
|
||||
return { tokenHash, user: entry.user };
|
||||
}
|
||||
}
|
||||
const session = await this.prisma.session.findUnique({
|
||||
where: { tokenHash },
|
||||
include: { user: true },
|
||||
});
|
||||
const remainingMs = session
|
||||
? session.expiresAt.getTime() - Date.now()
|
||||
: 0;
|
||||
if (!session || remainingMs <= 0) {
|
||||
return null;
|
||||
}
|
||||
const user = toUser(session.user);
|
||||
const entry: CachedSession = {
|
||||
user,
|
||||
expiresAt: session.expiresAt.getTime(),
|
||||
};
|
||||
await this.cache.set(
|
||||
key,
|
||||
JSON.stringify(entry),
|
||||
Math.min(remainingMs, CACHE_TTL_MS),
|
||||
true,
|
||||
);
|
||||
return { tokenHash, user };
|
||||
}
|
||||
|
||||
async revoke(tokenHash: string): Promise<void> {
|
||||
const marked = await this.cache.setStrict(
|
||||
cacheKey(tokenHash),
|
||||
REVOKED,
|
||||
CACHE_TTL_MS,
|
||||
);
|
||||
if (!marked) {
|
||||
throw new ApiError('unavailable', 'session cache unavailable');
|
||||
}
|
||||
await this.prisma.session.deleteMany({ where: { tokenHash } });
|
||||
}
|
||||
}
|
||||
|
||||
function cacheKey(tokenHash: string): string {
|
||||
return `session:v1:${tokenHash}`;
|
||||
}
|
||||
|
||||
function parseBasic(
|
||||
header: string | undefined,
|
||||
): { username: string; password: string } | null {
|
||||
const match = /^Basic ([A-Za-z0-9+/]+={0,2})$/i.exec(header ?? '');
|
||||
if (!match) {
|
||||
return null;
|
||||
}
|
||||
const decoded = Buffer.from(match[1], 'base64').toString('utf8');
|
||||
const separator = decoded.indexOf(':');
|
||||
if (separator < 0) {
|
||||
return null;
|
||||
}
|
||||
const username = decoded.slice(0, separator);
|
||||
const password = decoded.slice(separator + 1);
|
||||
const length = codePointLength(password);
|
||||
if (!USERNAME_PATTERN.test(username) || length < 12 || length > 128) {
|
||||
return null;
|
||||
}
|
||||
return { username, password };
|
||||
}
|
||||
|
||||
function hashToken(token: string): string {
|
||||
return createHash('sha256').update(token).digest('hex');
|
||||
}
|
||||
|
||||
function invalidCredentials(): ApiError {
|
||||
return new ApiError('unauthorized', 'invalid credentials', {
|
||||
'WWW-Authenticate': BASIC_CHALLENGE,
|
||||
});
|
||||
}
|
||||
|
||||
function readSessionTtl(): number {
|
||||
const ttl = Number(process.env.SESSION_TTL_SECONDS ?? 3600);
|
||||
if (!Number.isInteger(ttl) || ttl <= 0) {
|
||||
throw new Error('SESSION_TTL_SECONDS must be a positive integer');
|
||||
}
|
||||
return ttl;
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
import { createParamDecorator, ExecutionContext } from '@nestjs/common';
|
||||
import { FastifyRequest } from 'fastify';
|
||||
import { User } from '../common/domain';
|
||||
|
||||
declare module 'fastify' {
|
||||
interface FastifyRequest {
|
||||
currentUser?: User;
|
||||
sessionTokenHash?: string;
|
||||
}
|
||||
}
|
||||
|
||||
export const CurrentUser = createParamDecorator(
|
||||
(_data: unknown, ctx: ExecutionContext): User => {
|
||||
const request = ctx.switchToHttp().getRequest<FastifyRequest>();
|
||||
if (!request.currentUser) {
|
||||
throw new Error('CurrentUser used without AuthGuard');
|
||||
}
|
||||
return request.currentUser;
|
||||
},
|
||||
);
|
||||
@@ -0,0 +1,17 @@
|
||||
import { argon2id, hash, verify } from 'argon2';
|
||||
|
||||
const OPTIONS = { type: argon2id, memoryCost: 19456, timeCost: 2, parallelism: 1 };
|
||||
|
||||
const dummyHash = hash('timing-equalizer-password', OPTIONS);
|
||||
|
||||
export function hashPassword(password: string): Promise<string> {
|
||||
return hash(password, OPTIONS);
|
||||
}
|
||||
|
||||
export async function verifyPassword(
|
||||
passwordHash: string | null,
|
||||
password: string,
|
||||
): Promise<boolean> {
|
||||
const valid = await verify(passwordHash ?? (await dummyHash), password);
|
||||
return passwordHash !== null && valid;
|
||||
}
|
||||
Vendored
+9
@@ -0,0 +1,9 @@
|
||||
import { Global, Module } from '@nestjs/common';
|
||||
import { CacheService } from './cache.service';
|
||||
|
||||
@Global()
|
||||
@Module({
|
||||
providers: [CacheService],
|
||||
exports: [CacheService],
|
||||
})
|
||||
export class CacheModule {}
|
||||
Vendored
+98
@@ -0,0 +1,98 @@
|
||||
import { Injectable, Logger, OnModuleDestroy } from '@nestjs/common';
|
||||
import { readFileSync } from 'fs';
|
||||
import Redis from 'ioredis';
|
||||
|
||||
@Injectable()
|
||||
export class CacheService implements OnModuleDestroy {
|
||||
private readonly logger = new Logger('CacheService');
|
||||
private readonly client: Redis | null;
|
||||
private trusted = false;
|
||||
|
||||
constructor() {
|
||||
const url = readCacheUrl();
|
||||
this.client = url
|
||||
? new Redis(url, {
|
||||
enableOfflineQueue: false,
|
||||
maxRetriesPerRequest: 0,
|
||||
connectTimeout: 1000,
|
||||
commandTimeout: 200,
|
||||
})
|
||||
: null;
|
||||
this.client?.on('error', (error) =>
|
||||
this.logger.warn(`redis unavailable: ${error.message}`),
|
||||
);
|
||||
this.client?.on('close', () => {
|
||||
this.trusted = false;
|
||||
});
|
||||
this.client?.on('ready', () => {
|
||||
this.client!.flushdb().then(
|
||||
() => {
|
||||
this.trusted = true;
|
||||
},
|
||||
() => this.distrust(),
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
async get(key: string): Promise<string | null> {
|
||||
if (!this.client || !this.trusted) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
return await this.client.get(key);
|
||||
} catch {
|
||||
this.distrust();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
async set(
|
||||
key: string,
|
||||
value: string,
|
||||
ttlMs: number,
|
||||
onlyIfAbsent = false,
|
||||
): Promise<void> {
|
||||
if (!this.client || !this.trusted) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
if (onlyIfAbsent) {
|
||||
await this.client.set(key, value, 'PX', ttlMs, 'NX');
|
||||
} else {
|
||||
await this.client.set(key, value, 'PX', ttlMs);
|
||||
}
|
||||
} catch {
|
||||
this.distrust();
|
||||
}
|
||||
}
|
||||
|
||||
async setStrict(key: string, value: string, ttlMs: number): Promise<boolean> {
|
||||
if (!this.client) {
|
||||
return true;
|
||||
}
|
||||
if (!this.trusted) {
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
await this.client.set(key, value, 'PX', ttlMs);
|
||||
return true;
|
||||
} catch {
|
||||
this.distrust();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
onModuleDestroy(): void {
|
||||
this.client?.disconnect();
|
||||
}
|
||||
|
||||
private distrust(): void {
|
||||
this.trusted = false;
|
||||
this.client?.disconnect(true);
|
||||
}
|
||||
}
|
||||
|
||||
function readCacheUrl(): string | undefined {
|
||||
const file = process.env.CACHE_URL_FILE;
|
||||
return file ? readFileSync(file, 'utf8').trim() : process.env.CACHE_URL;
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
import { ApiError } from '../common/filters/api-error';
|
||||
import { validate as isUuid } from '../common/uuid';
|
||||
import { PrismaService } from '../db/prisma.service';
|
||||
|
||||
export async function assertChatMember(
|
||||
prisma: PrismaService,
|
||||
chatId: string,
|
||||
userId: string,
|
||||
): Promise<void> {
|
||||
if (!isUuid(chatId)) {
|
||||
throw new ApiError('invalid_request', 'chatId must be a valid UUID');
|
||||
}
|
||||
const member = await prisma.chatMember.findUnique({
|
||||
where: { chatId_userId: { chatId, userId } },
|
||||
});
|
||||
if (!member) {
|
||||
throw new ApiError('not_found', 'chat not found');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
import {
|
||||
Body,
|
||||
Controller,
|
||||
Get,
|
||||
HttpCode,
|
||||
Param,
|
||||
Post,
|
||||
Query,
|
||||
UseGuards,
|
||||
} from '@nestjs/common';
|
||||
import { AuthGuard } from '../auth/auth.guard';
|
||||
import { CurrentUser } from '../auth/current-user.decorator';
|
||||
import { parseLimit, rawCursorParam } from '../common/dto/pagination-query.dto';
|
||||
import { User } from '../common/domain';
|
||||
import { ChatsService } from './chats.service';
|
||||
import {
|
||||
ChatPageResponse,
|
||||
ChatResponse,
|
||||
toChatResponse,
|
||||
} from './dto/chat.response';
|
||||
import { CreateChatDto } from './dto/create-chat.dto';
|
||||
|
||||
@Controller('api/v1/chats')
|
||||
@UseGuards(AuthGuard)
|
||||
export class ChatsController {
|
||||
constructor(private readonly chats: ChatsService) {}
|
||||
|
||||
@Post()
|
||||
@HttpCode(201)
|
||||
async create(
|
||||
@Body() dto: CreateChatDto,
|
||||
@CurrentUser() user: User,
|
||||
): Promise<ChatResponse> {
|
||||
const chat = await this.chats.create(dto.title, dto.member_ids, user.id);
|
||||
return toChatResponse(chat);
|
||||
}
|
||||
|
||||
@Get()
|
||||
async list(
|
||||
@CurrentUser() user: User,
|
||||
@Query('limit') limitRaw: unknown,
|
||||
@Query('cursor') cursorRaw: unknown,
|
||||
): Promise<ChatPageResponse> {
|
||||
const limit = parseLimit(limitRaw);
|
||||
const cursor = rawCursorParam(cursorRaw);
|
||||
return this.chats.listForMember(user.id, limit, cursor);
|
||||
}
|
||||
|
||||
@Get(':chatId')
|
||||
async getOne(
|
||||
@Param('chatId') chatId: string,
|
||||
@CurrentUser() user: User,
|
||||
): Promise<ChatResponse> {
|
||||
const chat = await this.chats.getForMember(chatId, user.id);
|
||||
return toChatResponse(chat);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { ChatsController } from './chats.controller';
|
||||
import { ChatsService } from './chats.service';
|
||||
|
||||
@Module({
|
||||
controllers: [ChatsController],
|
||||
providers: [ChatsService],
|
||||
exports: [ChatsService],
|
||||
})
|
||||
export class ChatsModule {}
|
||||
@@ -0,0 +1,131 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { Prisma } from '@prisma/client';
|
||||
import { decodeChatCursor, encodeChatCursor } from '../common/cursor';
|
||||
import { Chat } from '../common/domain';
|
||||
import { ApiError } from '../common/filters/api-error';
|
||||
import { validate as isUuid } from '../common/uuid';
|
||||
import { PrismaService, isForeignKeyViolation } from '../db/prisma.service';
|
||||
import { ChatPageResponse, toChatResponse } from './dto/chat.response';
|
||||
|
||||
interface ChatRow {
|
||||
id: string;
|
||||
title: string;
|
||||
createdAt: Date;
|
||||
memberIds: string[];
|
||||
}
|
||||
|
||||
@Injectable()
|
||||
export class ChatsService {
|
||||
constructor(private readonly prisma: PrismaService) {}
|
||||
|
||||
async create(
|
||||
title: string,
|
||||
rawMemberIds: string[],
|
||||
callerId: string,
|
||||
): Promise<Chat> {
|
||||
const invalid = rawMemberIds.find((id) => !isUuid(id));
|
||||
if (invalid) {
|
||||
throw new ApiError('invalid_request', `unknown member_id: ${invalid}`);
|
||||
}
|
||||
const memberIds = rawMemberIds.map((id) => id.toLowerCase());
|
||||
if (new Set(memberIds).size !== memberIds.length) {
|
||||
throw new ApiError(
|
||||
'invalid_request',
|
||||
"All member_ids's elements must be unique",
|
||||
);
|
||||
}
|
||||
if (!memberIds.includes(callerId)) {
|
||||
throw new ApiError(
|
||||
'invalid_request',
|
||||
'member_ids must include the caller',
|
||||
);
|
||||
}
|
||||
try {
|
||||
const chat = await this.prisma.$transaction(async (tx) => {
|
||||
const created = await tx.chat.create({
|
||||
data: { title: title.trim() },
|
||||
});
|
||||
await tx.chatMember.createMany({
|
||||
data: memberIds.map((userId) => ({
|
||||
chatId: created.id,
|
||||
userId,
|
||||
})),
|
||||
});
|
||||
return created;
|
||||
});
|
||||
return toChat({ ...chat, memberIds });
|
||||
} catch (error) {
|
||||
if (isForeignKeyViolation(error)) {
|
||||
throw new ApiError(
|
||||
'invalid_request',
|
||||
'member_ids contain an unknown user',
|
||||
);
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async getForMember(chatId: string, callerId: string): Promise<Chat> {
|
||||
if (!isUuid(chatId)) {
|
||||
throw new ApiError('invalid_request', 'chatId must be a valid UUID');
|
||||
}
|
||||
const chat = await this.prisma.chat.findFirst({
|
||||
where: { id: chatId, members: { some: { userId: callerId } } },
|
||||
include: { members: { select: { userId: true } } },
|
||||
});
|
||||
if (!chat) {
|
||||
throw new ApiError('not_found', 'chat not found');
|
||||
}
|
||||
return toChat({
|
||||
...chat,
|
||||
memberIds: chat.members.map((m) => m.userId),
|
||||
});
|
||||
}
|
||||
|
||||
async listForMember(
|
||||
callerId: string,
|
||||
limit: number,
|
||||
rawCursor: string | null,
|
||||
): Promise<ChatPageResponse> {
|
||||
let after = Prisma.empty;
|
||||
if (rawCursor !== null) {
|
||||
const cursor = decodeChatCursor(rawCursor);
|
||||
const member = cursor
|
||||
? await this.prisma.chatMember.findUnique({
|
||||
where: {
|
||||
chatId_userId: { chatId: cursor.id, userId: callerId },
|
||||
},
|
||||
include: { chat: { select: { createdAt: true } } },
|
||||
})
|
||||
: null;
|
||||
if (!member) {
|
||||
throw new ApiError('invalid_request', 'invalid cursor');
|
||||
}
|
||||
after = Prisma.sql`AND (c.created_at, c.id) > (${member.chat.createdAt}::timestamptz, ${member.chatId}::uuid)`;
|
||||
}
|
||||
const rows = await this.prisma.$queryRaw<ChatRow[]>`
|
||||
SELECT c.id, c.title, c.created_at AS "createdAt",
|
||||
ARRAY(SELECT m.user_id::text FROM chat_members m WHERE m.chat_id = c.id) AS "memberIds"
|
||||
FROM chat_members cm
|
||||
JOIN chats c ON c.id = cm.chat_id
|
||||
WHERE cm.user_id = ${callerId}::uuid ${after}
|
||||
ORDER BY c.created_at, c.id
|
||||
LIMIT ${limit + 1}`;
|
||||
const items = rows.slice(0, limit).map(toChat);
|
||||
const last = items[items.length - 1];
|
||||
return {
|
||||
items: items.map(toChatResponse),
|
||||
next_cursor:
|
||||
rows.length > limit ? encodeChatCursor({ id: last.id }) : null,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
function toChat(row: ChatRow): Chat {
|
||||
return {
|
||||
id: row.id,
|
||||
title: row.title,
|
||||
memberIds: row.memberIds,
|
||||
createdAt: row.createdAt.toISOString(),
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
import { Chat } from '../../common/domain';
|
||||
|
||||
export interface ChatResponse {
|
||||
id: string;
|
||||
title: string;
|
||||
member_ids: string[];
|
||||
created_at: string;
|
||||
}
|
||||
|
||||
export interface ChatPageResponse {
|
||||
items: ChatResponse[];
|
||||
next_cursor: string | null;
|
||||
}
|
||||
|
||||
export function toChatResponse(chat: Chat): ChatResponse {
|
||||
return {
|
||||
id: chat.id,
|
||||
title: chat.title,
|
||||
member_ids: chat.memberIds,
|
||||
created_at: chat.createdAt,
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
import {
|
||||
ArrayMaxSize,
|
||||
ArrayMinSize,
|
||||
ArrayUnique,
|
||||
IsArray,
|
||||
IsString,
|
||||
} from 'class-validator';
|
||||
import { CodePointLength } from '../../common/dto/code-point-length.validator';
|
||||
import { NotBlank } from '../../common/dto/not-blank.validator';
|
||||
|
||||
export class CreateChatDto {
|
||||
@NotBlank()
|
||||
@CodePointLength(1, 100)
|
||||
title!: string;
|
||||
|
||||
@IsArray()
|
||||
@ArrayMinSize(1)
|
||||
@ArrayMaxSize(100)
|
||||
@ArrayUnique()
|
||||
@IsString({ each: true })
|
||||
member_ids!: string[];
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
import { validate as isUuid } from './uuid';
|
||||
|
||||
export interface ChatCursor {
|
||||
id: string;
|
||||
}
|
||||
|
||||
export interface MessageCursor {
|
||||
chatId: string;
|
||||
id: string;
|
||||
}
|
||||
|
||||
export function encodeChatCursor(cursor: ChatCursor): string {
|
||||
return encode(cursor);
|
||||
}
|
||||
|
||||
export function decodeChatCursor(raw: string): ChatCursor | null {
|
||||
const decoded = decode(raw);
|
||||
return decoded && isUuidField(decoded.id) ? { id: decoded.id } : null;
|
||||
}
|
||||
|
||||
export function encodeMessageCursor(cursor: MessageCursor): string {
|
||||
return encode(cursor);
|
||||
}
|
||||
|
||||
export function decodeMessageCursor(raw: string): MessageCursor | null {
|
||||
const decoded = decode(raw);
|
||||
return decoded && isUuidField(decoded.chatId) && isUuidField(decoded.id)
|
||||
? { chatId: decoded.chatId, id: decoded.id }
|
||||
: null;
|
||||
}
|
||||
|
||||
function isUuidField(value: unknown): value is string {
|
||||
return typeof value === 'string' && isUuid(value);
|
||||
}
|
||||
|
||||
function encode(value: object): string {
|
||||
return Buffer.from(JSON.stringify(value), 'utf8').toString('base64url');
|
||||
}
|
||||
|
||||
function decode(raw: string): Record<string, unknown> | null {
|
||||
try {
|
||||
const parsed = JSON.parse(
|
||||
Buffer.from(raw, 'base64url').toString('utf8'),
|
||||
);
|
||||
return typeof parsed === 'object' && parsed !== null ? parsed : null;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
export interface User {
|
||||
id: string;
|
||||
username: string;
|
||||
displayName: string;
|
||||
createdAt: string;
|
||||
}
|
||||
|
||||
export interface Chat {
|
||||
id: string;
|
||||
title: string;
|
||||
memberIds: string[];
|
||||
createdAt: string;
|
||||
}
|
||||
|
||||
export interface Message {
|
||||
id: string;
|
||||
chatId: string;
|
||||
senderId: string;
|
||||
text: string;
|
||||
createdAt: string;
|
||||
attachmentIds: string[];
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
import { registerDecorator, ValidationOptions } from 'class-validator';
|
||||
|
||||
export function codePointLength(value: string): number {
|
||||
return [...value].length;
|
||||
}
|
||||
|
||||
export function CodePointLength(
|
||||
min: number,
|
||||
max: number,
|
||||
options?: ValidationOptions,
|
||||
) {
|
||||
return function (object: object, propertyName: string) {
|
||||
registerDecorator({
|
||||
name: 'codePointLength',
|
||||
target: object.constructor,
|
||||
propertyName,
|
||||
constraints: [min, max],
|
||||
options,
|
||||
validator: {
|
||||
validate(value: unknown): boolean {
|
||||
if (typeof value !== 'string') {
|
||||
return false;
|
||||
}
|
||||
const length = codePointLength(value);
|
||||
return length >= min && length <= max;
|
||||
},
|
||||
defaultMessage(): string {
|
||||
return `${propertyName} must be between ${min} and ${max} Unicode code points`;
|
||||
},
|
||||
},
|
||||
});
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
import { registerDecorator, ValidationOptions } from 'class-validator';
|
||||
|
||||
export function NotBlank(options?: ValidationOptions) {
|
||||
return function (object: object, propertyName: string) {
|
||||
registerDecorator({
|
||||
name: 'notBlank',
|
||||
target: object.constructor,
|
||||
propertyName,
|
||||
options,
|
||||
validator: {
|
||||
validate(value: unknown): boolean {
|
||||
return typeof value === 'string' && value.trim().length > 0;
|
||||
},
|
||||
defaultMessage(): string {
|
||||
return `${propertyName} must not be empty or whitespace-only`;
|
||||
},
|
||||
},
|
||||
});
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
import { ApiError } from '../filters/api-error';
|
||||
|
||||
const DEFAULT_LIMIT = 50;
|
||||
const MIN_LIMIT = 1;
|
||||
const MAX_LIMIT = 100;
|
||||
|
||||
export function parseLimit(raw: unknown): number {
|
||||
if (raw === undefined) {
|
||||
return DEFAULT_LIMIT;
|
||||
}
|
||||
if (typeof raw !== 'string' || !/^\d+$/.test(raw)) {
|
||||
throw new ApiError('invalid_request', 'limit must be an integer');
|
||||
}
|
||||
const limit = Number(raw);
|
||||
if (limit < MIN_LIMIT || limit > MAX_LIMIT) {
|
||||
throw new ApiError(
|
||||
'invalid_request',
|
||||
`limit must be between ${MIN_LIMIT} and ${MAX_LIMIT}`,
|
||||
);
|
||||
}
|
||||
return limit;
|
||||
}
|
||||
|
||||
export function rawCursorParam(raw: unknown): string | null {
|
||||
if (raw === undefined) {
|
||||
return null;
|
||||
}
|
||||
if (typeof raw !== 'string' || raw.length === 0) {
|
||||
throw new ApiError(
|
||||
'invalid_request',
|
||||
'cursor must be a non-empty string',
|
||||
);
|
||||
}
|
||||
return raw;
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
import { HttpException, HttpStatus } from '@nestjs/common';
|
||||
|
||||
export type ErrorCode =
|
||||
| 'invalid_request'
|
||||
| 'unauthorized'
|
||||
| 'not_found'
|
||||
| 'conflict'
|
||||
| 'payload_too_large'
|
||||
| 'unsupported_media_type'
|
||||
| 'rate_limited'
|
||||
| 'unavailable'
|
||||
| 'internal_error';
|
||||
|
||||
const STATUS_BY_CODE: Record<ErrorCode, number> = {
|
||||
invalid_request: HttpStatus.BAD_REQUEST,
|
||||
unauthorized: HttpStatus.UNAUTHORIZED,
|
||||
not_found: HttpStatus.NOT_FOUND,
|
||||
conflict: HttpStatus.CONFLICT,
|
||||
payload_too_large: HttpStatus.PAYLOAD_TOO_LARGE,
|
||||
unsupported_media_type: HttpStatus.UNSUPPORTED_MEDIA_TYPE,
|
||||
rate_limited: HttpStatus.TOO_MANY_REQUESTS,
|
||||
unavailable: HttpStatus.SERVICE_UNAVAILABLE,
|
||||
internal_error: HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
};
|
||||
|
||||
export class ApiError extends HttpException {
|
||||
constructor(
|
||||
public readonly code: ErrorCode,
|
||||
message: string,
|
||||
public readonly headers: Record<string, string> = {},
|
||||
) {
|
||||
super(message, STATUS_BY_CODE[code]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,115 @@
|
||||
import {
|
||||
ArgumentsHost,
|
||||
Catch,
|
||||
ExceptionFilter,
|
||||
HttpException,
|
||||
HttpStatus,
|
||||
Logger,
|
||||
} from '@nestjs/common';
|
||||
import { FastifyReply, FastifyRequest } from 'fastify';
|
||||
import { isDatabaseUnavailable } from '../../db/prisma.service';
|
||||
import { ApiError, ErrorCode } from './api-error';
|
||||
|
||||
const CODE_BY_STATUS: Partial<Record<number, ErrorCode>> = {
|
||||
[HttpStatus.BAD_REQUEST]: 'invalid_request',
|
||||
[HttpStatus.UNAUTHORIZED]: 'unauthorized',
|
||||
[HttpStatus.NOT_FOUND]: 'not_found',
|
||||
[HttpStatus.CONFLICT]: 'conflict',
|
||||
[HttpStatus.PAYLOAD_TOO_LARGE]: 'payload_too_large',
|
||||
[HttpStatus.UNSUPPORTED_MEDIA_TYPE]: 'unsupported_media_type',
|
||||
[HttpStatus.TOO_MANY_REQUESTS]: 'rate_limited',
|
||||
[HttpStatus.SERVICE_UNAVAILABLE]: 'unavailable',
|
||||
};
|
||||
|
||||
interface Resolved {
|
||||
status: number;
|
||||
code: ErrorCode;
|
||||
message: string;
|
||||
}
|
||||
|
||||
@Catch()
|
||||
export class HttpExceptionFilter implements ExceptionFilter {
|
||||
private readonly logger = new Logger('HttpExceptionFilter');
|
||||
|
||||
catch(exception: unknown, host: ArgumentsHost): void {
|
||||
const ctx = host.switchToHttp();
|
||||
const reply = ctx.getResponse<FastifyReply>();
|
||||
const request = ctx.getRequest<FastifyRequest>();
|
||||
const requestId = request.requestId;
|
||||
|
||||
const { status, code, message } = this.resolve(exception);
|
||||
if (status >= 500) {
|
||||
this.logger.error(
|
||||
`${request.method} ${request.url} -> ${status}: ${message}`,
|
||||
);
|
||||
}
|
||||
|
||||
if (exception instanceof ApiError) {
|
||||
reply.headers(exception.headers);
|
||||
}
|
||||
reply
|
||||
.code(status)
|
||||
.header('x-request-id', requestId)
|
||||
.send({ error: { code, message, request_id: requestId } });
|
||||
}
|
||||
|
||||
private resolve(exception: unknown): Resolved {
|
||||
if (exception instanceof ApiError) {
|
||||
return {
|
||||
status: exception.getStatus(),
|
||||
code: exception.code,
|
||||
message: exception.message,
|
||||
};
|
||||
}
|
||||
if (exception instanceof HttpException) {
|
||||
const status = exception.getStatus();
|
||||
const code = CODE_BY_STATUS[status] ?? 'internal_error';
|
||||
const message =
|
||||
extractMessage(exception.getResponse()) ?? exception.message;
|
||||
return { status, code, message };
|
||||
}
|
||||
const fastifyStatus = (exception as { statusCode?: unknown })
|
||||
?.statusCode;
|
||||
if (typeof fastifyStatus === 'number' && CODE_BY_STATUS[fastifyStatus]) {
|
||||
const message =
|
||||
exception instanceof Error ? exception.message : 'Invalid request';
|
||||
return {
|
||||
status: fastifyStatus,
|
||||
code: CODE_BY_STATUS[fastifyStatus]!,
|
||||
message,
|
||||
};
|
||||
}
|
||||
if (isDatabaseUnavailable(exception)) {
|
||||
return {
|
||||
status: 503,
|
||||
code: 'unavailable',
|
||||
message: 'database unavailable',
|
||||
};
|
||||
}
|
||||
return {
|
||||
status: 500,
|
||||
code: 'internal_error',
|
||||
message: 'Internal server error',
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
function extractMessage(response: unknown): string | null {
|
||||
if (typeof response === 'string') {
|
||||
return response;
|
||||
}
|
||||
if (
|
||||
typeof response === 'object' &&
|
||||
response !== null &&
|
||||
'message' in response
|
||||
) {
|
||||
const value = (response as { message: unknown }).message;
|
||||
if (typeof value === 'string') {
|
||||
return value;
|
||||
}
|
||||
if (Array.isArray(value) && value.every((v) => typeof v === 'string')) {
|
||||
return value.join('; ');
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
import { randomUUID } from 'crypto';
|
||||
import { FastifyInstance, FastifyRequest } from 'fastify';
|
||||
import { hostname } from 'os';
|
||||
|
||||
const REQUEST_ID_HEADER = 'x-request-id';
|
||||
const INSTANCE_ID_HEADER = 'x-instance-id';
|
||||
|
||||
export const INSTANCE_ID = process.env.INSTANCE_ID ?? hostname();
|
||||
|
||||
declare module 'fastify' {
|
||||
interface FastifyRequest {
|
||||
requestId: string;
|
||||
}
|
||||
}
|
||||
|
||||
export function registerRequestId(instance: FastifyInstance): void {
|
||||
instance.addHook('onRequest', (request: FastifyRequest, reply, done) => {
|
||||
request.requestId = randomUUID();
|
||||
reply.header(REQUEST_ID_HEADER, request.requestId);
|
||||
reply.header(INSTANCE_ID_HEADER, INSTANCE_ID);
|
||||
done();
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
const UUID_PATTERN =
|
||||
/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
||||
|
||||
export function validate(value: string): boolean {
|
||||
return UUID_PATTERN.test(value);
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
import { ValidationPipe } from '@nestjs/common';
|
||||
import { NestFastifyApplication } from '@nestjs/platform-fastify';
|
||||
import { registerUploadBody } from './attachments/upload-body';
|
||||
import { HttpExceptionFilter } from './common/filters/http-exception.filter';
|
||||
import { registerRequestId } from './common/interceptors/request-id';
|
||||
|
||||
export function configureApp(app: NestFastifyApplication): void {
|
||||
const instance = app.getHttpAdapter().getInstance();
|
||||
registerRequestId(instance);
|
||||
registerUploadBody(instance);
|
||||
|
||||
app.useGlobalPipes(
|
||||
new ValidationPipe({
|
||||
whitelist: true,
|
||||
forbidNonWhitelisted: true,
|
||||
transform: true,
|
||||
forbidUnknownValues: true,
|
||||
}),
|
||||
);
|
||||
app.useGlobalFilters(new HttpExceptionFilter());
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
import { Global, Module } from '@nestjs/common';
|
||||
import { PrismaService } from './prisma.service';
|
||||
|
||||
@Global()
|
||||
@Module({
|
||||
providers: [PrismaService],
|
||||
exports: [PrismaService],
|
||||
})
|
||||
export class DbModule {}
|
||||
@@ -0,0 +1,49 @@
|
||||
import { Injectable, OnModuleDestroy } from '@nestjs/common';
|
||||
import { Prisma, PrismaClient } from '@prisma/client';
|
||||
import { readFileSync } from 'fs';
|
||||
|
||||
const UNAVAILABLE_CODES = new Set(['P1001', 'P1002', 'P1008', 'P1017', 'P2024']);
|
||||
|
||||
@Injectable()
|
||||
export class PrismaService extends PrismaClient implements OnModuleDestroy {
|
||||
constructor() {
|
||||
super({ datasourceUrl: readDatabaseUrl() });
|
||||
}
|
||||
|
||||
async onModuleDestroy(): Promise<void> {
|
||||
await this.$disconnect();
|
||||
}
|
||||
}
|
||||
|
||||
function readDatabaseUrl(): string {
|
||||
const file = process.env.DATABASE_URL_FILE;
|
||||
const url = file ? readFileSync(file, 'utf8').trim() : process.env.DATABASE_URL;
|
||||
if (!url) {
|
||||
throw new Error('DATABASE_URL_FILE or DATABASE_URL must be set');
|
||||
}
|
||||
return url;
|
||||
}
|
||||
|
||||
export function isUniqueViolation(error: unknown): boolean {
|
||||
return (
|
||||
error instanceof Prisma.PrismaClientKnownRequestError &&
|
||||
error.code === 'P2002'
|
||||
);
|
||||
}
|
||||
|
||||
export function isForeignKeyViolation(error: unknown): boolean {
|
||||
return (
|
||||
error instanceof Prisma.PrismaClientKnownRequestError &&
|
||||
error.code === 'P2003'
|
||||
);
|
||||
}
|
||||
|
||||
export function isDatabaseUnavailable(error: unknown): boolean {
|
||||
if (error instanceof Prisma.PrismaClientInitializationError) {
|
||||
return true;
|
||||
}
|
||||
return (
|
||||
error instanceof Prisma.PrismaClientKnownRequestError &&
|
||||
UNAVAILABLE_CODES.has(error.code)
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,350 @@
|
||||
const ALICE_ID = '11111111-1111-4111-8111-111111111111';
|
||||
const BOB_ID = '22222222-2222-4222-8222-222222222222';
|
||||
const CHAT_ID = '33333333-3333-4333-8333-333333333333';
|
||||
const MESSAGE_ID = '44444444-4444-4444-8444-444444444444';
|
||||
const CURSOR =
|
||||
'eyJjaGF0SWQiOiIzMzMzMzMzMy0zMzMzLTQzMzMtODMzMy0zMzMzMzMzMzMzMzMiLCJpZCI6IjQ0NDQ0NDQ0LTQ0NDQtNDQ0NC04NDQ0LTQ0NDQ0NDQ0NDQ0NCJ9';
|
||||
|
||||
const ALICE = {
|
||||
id: ALICE_ID,
|
||||
username: 'alice',
|
||||
display_name: 'Алиса',
|
||||
created_at: '2026-09-01T12:00:00.000Z',
|
||||
};
|
||||
|
||||
const CHAT = {
|
||||
id: CHAT_ID,
|
||||
title: 'Проект по ВНП',
|
||||
member_ids: [ALICE_ID, BOB_ID],
|
||||
created_at: '2026-09-01T12:01:00.000Z',
|
||||
};
|
||||
|
||||
const MESSAGE = {
|
||||
id: MESSAGE_ID,
|
||||
chat_id: CHAT_ID,
|
||||
sender_id: ALICE_ID,
|
||||
text: 'Привет, Боб! 👋',
|
||||
created_at: '2026-09-01T12:02:00.000Z',
|
||||
attachment_ids: [],
|
||||
};
|
||||
|
||||
export interface Example {
|
||||
summary: string;
|
||||
value: unknown;
|
||||
}
|
||||
|
||||
export interface OperationExamples {
|
||||
request?: Record<string, Example>;
|
||||
responses: Record<string, Record<string, Example>>;
|
||||
}
|
||||
|
||||
function example(summary: string, value: unknown): Example {
|
||||
return { summary, value };
|
||||
}
|
||||
|
||||
function error(code: string, message: string): unknown {
|
||||
return {
|
||||
error: {
|
||||
code,
|
||||
message,
|
||||
request_id: '9b2c6f1e-5d4a-4c3b-8a2f-1e0d9c8b7a6f',
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
const UNAUTHORIZED = {
|
||||
missing: example(
|
||||
'Нет/неверный/истёкший/отозванный Bearer-токен',
|
||||
error('unauthorized', 'missing or invalid session'),
|
||||
),
|
||||
};
|
||||
|
||||
const UNAVAILABLE = {
|
||||
database: example(
|
||||
'PostgreSQL недоступен',
|
||||
error('unavailable', 'database unavailable'),
|
||||
),
|
||||
};
|
||||
|
||||
const CHAT_NOT_FOUND = {
|
||||
notFound: example(
|
||||
'Чата нет или вызывающий не участник (не 403)',
|
||||
error('not_found', 'chat not found'),
|
||||
),
|
||||
};
|
||||
|
||||
const INVALID_CHAT_ID = example(
|
||||
'chat_id не UUID',
|
||||
error('invalid_request', 'chatId must be a valid UUID'),
|
||||
);
|
||||
|
||||
export const GUIDE = `
|
||||
Учебный мессенджер, лабораторная 3: PostgreSQL, сессии, Redis-кэш сессий.
|
||||
Документ — обязательный контракт \`contracts/openapi.json\`, дополненный примерами запросов и ответов.
|
||||
|
||||
**Как пройти сценарий в Swagger UI**
|
||||
|
||||
1. \`POST /api/v1/users\` — зарегистрируйте пользователей (примеры alice, bob, eve в списке Examples). Повторная регистрация того же username — 409, меняйте username.
|
||||
2. **Authorize → basicAuth**: username и password из шага 1 → \`POST /api/v1/auth/sessions\` → скопируйте \`token\`.
|
||||
3. **Authorize → bearerAuth**: вставьте token (без слова Bearer). Basic отправляется только на вход, остальные запросы идут с Bearer.
|
||||
4. \`GET /api/v1/users/me\` — свой id; id второго участника — из ответа его регистрации.
|
||||
5. \`POST /api/v1/chats\` с \`member_ids\` = [свой id, id собеседника] → \`chat_id\`.
|
||||
6. \`POST /api/v1/chats/{chat_id}/messages\` с заголовком \`Idempotency-Key\` (любая строка из \`A-Za-z0-9._:-\`, до 128 символов). Повтор с тем же ключом и телом вернёт то же сообщение, с другим телом — 409. Затем \`GET …/messages?limit=1\` и следующая страница с \`cursor\` = \`next_cursor\`.
|
||||
7. Чтобы действовать от другого пользователя: Logout в Authorize, вход им и новый token.
|
||||
8. \`DELETE /api/v1/auth/sessions/current\` — logout; тот же token дальше даёт 401.
|
||||
|
||||
Сессия живёт \`SESSION_TTL_SECONDS\` (по умолчанию 3600 с). Пароль — 12–128 символов Unicode, двоеточие допустимо.
|
||||
`;
|
||||
|
||||
export const OPERATION_EXAMPLES: Record<string, OperationExamples> = {
|
||||
'GET /health/live': {
|
||||
responses: {
|
||||
'200': { ok: example('Процесс жив', { status: 'ok' }) },
|
||||
},
|
||||
},
|
||||
'GET /health/ready': {
|
||||
responses: {
|
||||
'200': {
|
||||
ok: example('PostgreSQL доступен (Redis не критичен)', {
|
||||
status: 'ok',
|
||||
}),
|
||||
},
|
||||
'503': UNAVAILABLE,
|
||||
},
|
||||
},
|
||||
'POST /api/v1/users': {
|
||||
request: {
|
||||
alice: example('Алиса', {
|
||||
username: 'alice',
|
||||
display_name: 'Алиса',
|
||||
password: 'Alice-password-123',
|
||||
}),
|
||||
bob: example('Боб', {
|
||||
username: 'bob',
|
||||
display_name: 'Боб',
|
||||
password: 'Bob-password-123',
|
||||
}),
|
||||
eve: example('Ева (посторонняя)', {
|
||||
username: 'eve',
|
||||
display_name: 'Ева',
|
||||
password: 'Eve-password-123',
|
||||
}),
|
||||
colon: example('Пароль с двоеточием и эмодзи', {
|
||||
username: 'carol',
|
||||
display_name: 'Кэрол 🌸',
|
||||
password: 'пароль:с:двоеточием🔑',
|
||||
}),
|
||||
},
|
||||
responses: {
|
||||
'201': { created: example('Пользователь создан', ALICE) },
|
||||
'400': {
|
||||
username: example(
|
||||
'username не соответствует [a-z0-9_]{3,32}',
|
||||
error('invalid_request', 'username must match [a-z0-9_]{3,32}'),
|
||||
),
|
||||
password: example(
|
||||
'password короче 12 символов',
|
||||
error(
|
||||
'invalid_request',
|
||||
'password must be between 12 and 128 Unicode code points',
|
||||
),
|
||||
),
|
||||
extra: example(
|
||||
'Лишнее поле в JSON',
|
||||
error('invalid_request', 'property extra should not exist'),
|
||||
),
|
||||
},
|
||||
'409': {
|
||||
taken: example(
|
||||
'username уже занят',
|
||||
error('conflict', 'username is already taken'),
|
||||
),
|
||||
},
|
||||
'503': UNAVAILABLE,
|
||||
},
|
||||
},
|
||||
'POST /api/v1/auth/sessions': {
|
||||
responses: {
|
||||
'201': {
|
||||
created: example('Сессия создана', {
|
||||
token: 'q8Hn3vXl0ZbJ7mWcY2sRkT5uPaE9dFgVhN1oLxI4w6A',
|
||||
token_type: 'Bearer',
|
||||
expires_at: '2026-09-01T13:00:00.000Z',
|
||||
user: ALICE,
|
||||
}),
|
||||
},
|
||||
'401': {
|
||||
invalid: example(
|
||||
'Неверный пароль, неизвестный пользователь или нет Basic — ответ одинаковый',
|
||||
error('unauthorized', 'invalid credentials'),
|
||||
),
|
||||
},
|
||||
'503': UNAVAILABLE,
|
||||
},
|
||||
},
|
||||
'DELETE /api/v1/auth/sessions/current': {
|
||||
responses: {
|
||||
'401': UNAUTHORIZED,
|
||||
'503': {
|
||||
...UNAVAILABLE,
|
||||
cache: example(
|
||||
'Redis недоступен — отзыв не выполнен, повторите позже',
|
||||
error('unavailable', 'session cache unavailable'),
|
||||
),
|
||||
},
|
||||
},
|
||||
},
|
||||
'GET /api/v1/users/me': {
|
||||
responses: {
|
||||
'200': { me: example('Текущий пользователь из сессии', ALICE) },
|
||||
'401': UNAUTHORIZED,
|
||||
'503': UNAVAILABLE,
|
||||
},
|
||||
},
|
||||
'POST /api/v1/chats': {
|
||||
request: {
|
||||
pair: example('Чат Алисы и Боба', {
|
||||
title: CHAT.title,
|
||||
member_ids: CHAT.member_ids,
|
||||
}),
|
||||
solo: example('Чат только с собой', {
|
||||
title: 'Заметки',
|
||||
member_ids: [ALICE_ID],
|
||||
}),
|
||||
},
|
||||
responses: {
|
||||
'201': { created: example('Чат создан атомарно', CHAT) },
|
||||
'400': {
|
||||
noCaller: example(
|
||||
'Вызывающий не в member_ids',
|
||||
error('invalid_request', 'member_ids must include the caller'),
|
||||
),
|
||||
unknown: example(
|
||||
'Неизвестный участник — транзакция откатывается',
|
||||
error('invalid_request', 'member_ids contain an unknown user'),
|
||||
),
|
||||
duplicate: example(
|
||||
'Дубликаты в member_ids',
|
||||
error(
|
||||
'invalid_request',
|
||||
"All member_ids's elements must be unique",
|
||||
),
|
||||
),
|
||||
},
|
||||
'401': UNAUTHORIZED,
|
||||
'503': UNAVAILABLE,
|
||||
},
|
||||
},
|
||||
'GET /api/v1/chats': {
|
||||
responses: {
|
||||
'200': {
|
||||
page: example('Страница чатов с продолжением', {
|
||||
items: [CHAT],
|
||||
next_cursor: 'eyJpZCI6IjMzMzMzMzMzLTMzMzMtNDMzMy04MzMzLTMzMzMzMzMzMzMzMyJ9',
|
||||
}),
|
||||
last: example('Последняя страница', {
|
||||
items: [CHAT],
|
||||
next_cursor: null,
|
||||
}),
|
||||
},
|
||||
'400': {
|
||||
limit: example(
|
||||
'limit вне 1–100',
|
||||
error('invalid_request', 'limit must be between 1 and 100'),
|
||||
),
|
||||
cursor: example(
|
||||
'Испорченный или чужой cursor',
|
||||
error('invalid_request', 'invalid cursor'),
|
||||
),
|
||||
},
|
||||
'401': UNAUTHORIZED,
|
||||
'503': UNAVAILABLE,
|
||||
},
|
||||
},
|
||||
'GET /api/v1/chats/{chat_id}': {
|
||||
responses: {
|
||||
'200': { chat: example('Чат, где вызывающий — участник', CHAT) },
|
||||
'400': { chatId: INVALID_CHAT_ID },
|
||||
'401': UNAUTHORIZED,
|
||||
'404': CHAT_NOT_FOUND,
|
||||
'503': UNAVAILABLE,
|
||||
},
|
||||
},
|
||||
'POST /api/v1/chats/{chat_id}/messages': {
|
||||
request: {
|
||||
text: example('Обычное сообщение', { text: MESSAGE.text }),
|
||||
long: example('Максимум — 4000 символов Unicode', {
|
||||
text: 'Длинный текст…',
|
||||
}),
|
||||
},
|
||||
responses: {
|
||||
'201': {
|
||||
created: example('Отправитель берётся из сессии', MESSAGE),
|
||||
},
|
||||
'400': {
|
||||
blank: example(
|
||||
'Пустой или пробельный текст',
|
||||
error(
|
||||
'invalid_request',
|
||||
'text must not be empty or whitespace-only',
|
||||
),
|
||||
),
|
||||
sender: example(
|
||||
'sender_id в теле не принимается',
|
||||
error('invalid_request', 'property sender_id should not exist'),
|
||||
),
|
||||
chatId: INVALID_CHAT_ID,
|
||||
key: example(
|
||||
'Нет Idempotency-Key или неверный формат',
|
||||
error(
|
||||
'invalid_request',
|
||||
'Idempotency-Key header must match [A-Za-z0-9._:-]{1,128}',
|
||||
),
|
||||
),
|
||||
},
|
||||
'401': UNAUTHORIZED,
|
||||
'404': CHAT_NOT_FOUND,
|
||||
'409': {
|
||||
keyReused: example(
|
||||
'Тот же Idempotency-Key с другим телом',
|
||||
error(
|
||||
'conflict',
|
||||
'Idempotency-Key was already used with a different request',
|
||||
),
|
||||
),
|
||||
},
|
||||
'503': UNAVAILABLE,
|
||||
},
|
||||
},
|
||||
'GET /api/v1/chats/{chat_id}/messages': {
|
||||
responses: {
|
||||
'200': {
|
||||
page: example('Страница истории (limit=1) с cursor', {
|
||||
items: [MESSAGE],
|
||||
next_cursor: CURSOR,
|
||||
}),
|
||||
last: example('Последняя страница', {
|
||||
items: [MESSAGE],
|
||||
next_cursor: null,
|
||||
}),
|
||||
},
|
||||
'400': {
|
||||
otherChat: example(
|
||||
'cursor от другого чата',
|
||||
error('invalid_request', 'cursor belongs to a different chat'),
|
||||
),
|
||||
cursor: example(
|
||||
'Испорченный cursor или cursor несуществующего сообщения',
|
||||
error('invalid_request', 'invalid cursor'),
|
||||
),
|
||||
limit: example(
|
||||
'limit вне 1–100',
|
||||
error('invalid_request', 'limit must be between 1 and 100'),
|
||||
),
|
||||
},
|
||||
'401': UNAUTHORIZED,
|
||||
'404': CHAT_NOT_FOUND,
|
||||
'503': UNAVAILABLE,
|
||||
},
|
||||
},
|
||||
};
|
||||
@@ -0,0 +1,43 @@
|
||||
import { NestFastifyApplication } from '@nestjs/platform-fastify';
|
||||
import { OpenAPIObject, SwaggerModule } from '@nestjs/swagger';
|
||||
import { readFileSync } from 'fs';
|
||||
import { join } from 'path';
|
||||
import { GUIDE, OPERATION_EXAMPLES } from './examples';
|
||||
|
||||
const CONTRACT_PATH = join(process.cwd(), 'contracts', 'openapi.json');
|
||||
|
||||
export function setupSwagger(app: NestFastifyApplication): void {
|
||||
SwaggerModule.setup('docs', app, loadDocument(), {
|
||||
customSiteTitle: 'Messenger lab4 API',
|
||||
swaggerOptions: {
|
||||
persistAuthorization: true,
|
||||
displayRequestDuration: true,
|
||||
tryItOutEnabled: true,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export function loadDocument(): OpenAPIObject {
|
||||
const document = JSON.parse(readFileSync(CONTRACT_PATH, 'utf8'));
|
||||
document.info.description = GUIDE;
|
||||
document.servers = [{ url: '/', description: 'Этот экземпляр API' }];
|
||||
for (const [key, examples] of Object.entries(OPERATION_EXAMPLES)) {
|
||||
const [method, path] = key.split(' ');
|
||||
const operation = document.paths[path]?.[method.toLowerCase()];
|
||||
if (!operation) {
|
||||
throw new Error(`examples for unknown operation ${key}`);
|
||||
}
|
||||
if (examples.request) {
|
||||
operation.requestBody.content['application/json'].examples =
|
||||
examples.request;
|
||||
}
|
||||
for (const [status, named] of Object.entries(examples.responses)) {
|
||||
const content = operation.responses[status]?.content;
|
||||
if (!content) {
|
||||
throw new Error(`examples for unknown response ${key} ${status}`);
|
||||
}
|
||||
content['application/json'].examples = named;
|
||||
}
|
||||
}
|
||||
return document;
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
import { Controller, Get } from '@nestjs/common';
|
||||
import { ApiError } from '../common/filters/api-error';
|
||||
import { PrismaService } from '../db/prisma.service';
|
||||
import { S3Service } from '../storage/s3.service';
|
||||
import { isDraining } from './shutdown';
|
||||
|
||||
@Controller('health')
|
||||
export class HealthController {
|
||||
constructor(
|
||||
private readonly prisma: PrismaService,
|
||||
private readonly s3: S3Service,
|
||||
) {}
|
||||
|
||||
@Get('live')
|
||||
live(): { status: 'ok' } {
|
||||
return { status: 'ok' };
|
||||
}
|
||||
|
||||
@Get('core')
|
||||
async core(): Promise<{ status: 'ok' }> {
|
||||
await this.assertCoreReady();
|
||||
return { status: 'ok' };
|
||||
}
|
||||
|
||||
@Get('ready')
|
||||
async ready(): Promise<{ status: 'ok' }> {
|
||||
await this.assertCoreReady();
|
||||
if (!(await this.s3.isAvailable())) {
|
||||
throw new ApiError('unavailable', 'object storage unavailable');
|
||||
}
|
||||
return { status: 'ok' };
|
||||
}
|
||||
|
||||
private async assertCoreReady(): Promise<void> {
|
||||
if (isDraining()) {
|
||||
throw new ApiError('unavailable', 'shutting down');
|
||||
}
|
||||
try {
|
||||
await this.prisma.$queryRaw`SELECT 1`;
|
||||
} catch {
|
||||
throw new ApiError('unavailable', 'database unavailable');
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { HealthController } from './health.controller';
|
||||
|
||||
@Module({
|
||||
controllers: [HealthController],
|
||||
})
|
||||
export class HealthModule {}
|
||||
@@ -0,0 +1,26 @@
|
||||
import { INestApplication, Logger } from '@nestjs/common';
|
||||
|
||||
let draining = false;
|
||||
|
||||
export function isDraining(): boolean {
|
||||
return draining;
|
||||
}
|
||||
|
||||
export function enableGracefulShutdown(app: INestApplication): void {
|
||||
const delayMs = Number(process.env.SHUTDOWN_DELAY_MS ?? 3000);
|
||||
const shutdown = (signal: string): void => {
|
||||
if (draining) {
|
||||
return;
|
||||
}
|
||||
draining = true;
|
||||
Logger.log(`${signal}: readiness → 503, closing in ${delayMs} ms`, 'Shutdown');
|
||||
setTimeout(() => {
|
||||
app.close().then(
|
||||
() => process.exit(0),
|
||||
() => process.exit(1),
|
||||
);
|
||||
}, delayMs);
|
||||
};
|
||||
process.once('SIGTERM', () => shutdown('SIGTERM'));
|
||||
process.once('SIGINT', () => shutdown('SIGINT'));
|
||||
}
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
import { Logger } from '@nestjs/common';
|
||||
import { NestFactory } from '@nestjs/core';
|
||||
import {
|
||||
FastifyAdapter,
|
||||
NestFastifyApplication,
|
||||
} from '@nestjs/platform-fastify';
|
||||
import { AppModule } from './app.module';
|
||||
import { configureApp } from './configure-app';
|
||||
import { setupSwagger } from './docs/swagger';
|
||||
import { enableGracefulShutdown } from './health/shutdown';
|
||||
|
||||
async function bootstrap(): Promise<void> {
|
||||
const app = await NestFactory.create<NestFastifyApplication>(
|
||||
AppModule,
|
||||
new FastifyAdapter(),
|
||||
{
|
||||
bufferLogs: true,
|
||||
},
|
||||
);
|
||||
|
||||
configureApp(app);
|
||||
setupSwagger(app);
|
||||
enableGracefulShutdown(app);
|
||||
|
||||
const host = process.env.APP_HOST ?? '0.0.0.0';
|
||||
const port = Number(process.env.APP_PORT ?? 8080);
|
||||
await app.listen(port, host);
|
||||
Logger.log(`Listening on http://${host}:${port}`, 'Bootstrap');
|
||||
}
|
||||
|
||||
bootstrap().catch((error) => {
|
||||
Logger.error('Fatal error during bootstrap', error);
|
||||
process.exit(1);
|
||||
});
|
||||
@@ -0,0 +1,41 @@
|
||||
import {
|
||||
ArrayMaxSize,
|
||||
IsArray,
|
||||
IsString,
|
||||
Validate,
|
||||
ValidateIf,
|
||||
ValidatorConstraint,
|
||||
ValidatorConstraintInterface,
|
||||
} from 'class-validator';
|
||||
import { CodePointLength } from '../../common/dto/code-point-length.validator';
|
||||
import { validate as isUuid } from '../../common/uuid';
|
||||
|
||||
@ValidatorConstraint({ name: 'uniqueUuidList' })
|
||||
class UniqueUuidList implements ValidatorConstraintInterface {
|
||||
validate(value: unknown): boolean {
|
||||
if (
|
||||
!Array.isArray(value) ||
|
||||
!value.every((id) => typeof id === 'string' && isUuid(id))
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
const normalized = (value as string[]).map((id) => id.toLowerCase());
|
||||
return new Set(normalized).size === normalized.length;
|
||||
}
|
||||
|
||||
defaultMessage(): string {
|
||||
return 'attachment_ids must contain unique UUIDs';
|
||||
}
|
||||
}
|
||||
|
||||
export class CreateMessageDto {
|
||||
@IsString()
|
||||
@CodePointLength(0, 4000)
|
||||
text!: string;
|
||||
|
||||
@ValidateIf((dto: CreateMessageDto) => dto.attachment_ids !== undefined)
|
||||
@IsArray()
|
||||
@ArrayMaxSize(10)
|
||||
@Validate(UniqueUuidList)
|
||||
attachment_ids?: string[];
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
import { Message } from '../../common/domain';
|
||||
|
||||
export interface MessageResponse {
|
||||
id: string;
|
||||
chat_id: string;
|
||||
sender_id: string;
|
||||
text: string;
|
||||
created_at: string;
|
||||
attachment_ids: string[];
|
||||
}
|
||||
|
||||
export interface MessagePageResponse {
|
||||
items: MessageResponse[];
|
||||
next_cursor: string | null;
|
||||
}
|
||||
|
||||
export function toMessageResponse(message: Message): MessageResponse {
|
||||
return {
|
||||
id: message.id,
|
||||
chat_id: message.chatId,
|
||||
sender_id: message.senderId,
|
||||
text: message.text,
|
||||
created_at: message.createdAt,
|
||||
attachment_ids: message.attachmentIds,
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
import {
|
||||
Body,
|
||||
Controller,
|
||||
Get,
|
||||
Headers,
|
||||
HttpCode,
|
||||
Param,
|
||||
Post,
|
||||
Query,
|
||||
UseGuards,
|
||||
} from '@nestjs/common';
|
||||
import { AuthGuard } from '../auth/auth.guard';
|
||||
import { CurrentUser } from '../auth/current-user.decorator';
|
||||
import { parseLimit, rawCursorParam } from '../common/dto/pagination-query.dto';
|
||||
import { User } from '../common/domain';
|
||||
import { ApiError } from '../common/filters/api-error';
|
||||
import { CreateMessageDto } from './dto/create-message.dto';
|
||||
import {
|
||||
MessagePageResponse,
|
||||
MessageResponse,
|
||||
toMessageResponse,
|
||||
} from './dto/message.response';
|
||||
import { MessagesService } from './messages.service';
|
||||
|
||||
@Controller('api/v1/chats/:chatId/messages')
|
||||
@UseGuards(AuthGuard)
|
||||
export class MessagesController {
|
||||
constructor(private readonly messages: MessagesService) {}
|
||||
|
||||
@Post()
|
||||
@HttpCode(201)
|
||||
async create(
|
||||
@Param('chatId') chatId: string,
|
||||
@Body() dto: CreateMessageDto,
|
||||
@Headers('idempotency-key') keyRaw: unknown,
|
||||
@CurrentUser() user: User,
|
||||
): Promise<MessageResponse> {
|
||||
const key = parseIdempotencyKey(keyRaw);
|
||||
const message = await this.messages.create(
|
||||
chatId,
|
||||
user.id,
|
||||
dto.text,
|
||||
(dto.attachment_ids ?? []).map((id) => id.toLowerCase()),
|
||||
key,
|
||||
);
|
||||
return toMessageResponse(message);
|
||||
}
|
||||
|
||||
@Get()
|
||||
async list(
|
||||
@Param('chatId') chatId: string,
|
||||
@CurrentUser() user: User,
|
||||
@Query('limit') limitRaw: unknown,
|
||||
@Query('cursor') cursorRaw: unknown,
|
||||
): Promise<MessagePageResponse> {
|
||||
const limit = parseLimit(limitRaw);
|
||||
const cursor = rawCursorParam(cursorRaw);
|
||||
return this.messages.list(chatId, user.id, limit, cursor);
|
||||
}
|
||||
}
|
||||
|
||||
function parseIdempotencyKey(raw: unknown): string {
|
||||
if (typeof raw !== 'string' || !/^[A-Za-z0-9._:-]{1,128}$/.test(raw)) {
|
||||
throw new ApiError(
|
||||
'invalid_request',
|
||||
'Idempotency-Key header must match [A-Za-z0-9._:-]{1,128}',
|
||||
);
|
||||
}
|
||||
return raw;
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { MessagesController } from './messages.controller';
|
||||
import { MessagesService } from './messages.service';
|
||||
|
||||
@Module({
|
||||
controllers: [MessagesController],
|
||||
providers: [MessagesService],
|
||||
})
|
||||
export class MessagesModule {}
|
||||
@@ -0,0 +1,194 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { Prisma } from '@prisma/client';
|
||||
import { createHash } from 'crypto';
|
||||
import { assertChatMember } from '../chats/chat-access';
|
||||
import { codePointLength } from '../common/dto/code-point-length.validator';
|
||||
import { decodeMessageCursor, encodeMessageCursor } from '../common/cursor';
|
||||
import { Message } from '../common/domain';
|
||||
import { ApiError } from '../common/filters/api-error';
|
||||
import { PrismaService } from '../db/prisma.service';
|
||||
import { MessagePageResponse, toMessageResponse } from './dto/message.response';
|
||||
|
||||
interface MessageRow {
|
||||
id: string;
|
||||
chatId: string;
|
||||
senderId: string;
|
||||
text: string;
|
||||
createdAt: Date;
|
||||
attachmentIds: string[];
|
||||
}
|
||||
|
||||
const ATTACHMENT_LINKS = {
|
||||
attachments: {
|
||||
orderBy: { position: 'asc' },
|
||||
select: { attachmentId: true },
|
||||
},
|
||||
} satisfies Prisma.MessageInclude;
|
||||
|
||||
type MessageWithLinks = Prisma.MessageGetPayload<{
|
||||
include: typeof ATTACHMENT_LINKS;
|
||||
}>;
|
||||
|
||||
class KeyAlreadyUsed extends Error {}
|
||||
|
||||
@Injectable()
|
||||
export class MessagesService {
|
||||
private readonly keyTtlMs =
|
||||
Number(process.env.IDEMPOTENCY_TTL_SECONDS ?? 86400) * 1000;
|
||||
|
||||
constructor(private readonly prisma: PrismaService) {}
|
||||
|
||||
async create(
|
||||
chatId: string,
|
||||
callerId: string,
|
||||
text: string,
|
||||
attachmentIds: string[],
|
||||
key: string,
|
||||
): Promise<Message> {
|
||||
await this.assertMember(chatId, callerId);
|
||||
if (codePointLength(text.trim()) === 0 && attachmentIds.length === 0) {
|
||||
throw new ApiError(
|
||||
'invalid_request',
|
||||
'text must not be empty or whitespace-only without attachments',
|
||||
);
|
||||
}
|
||||
await this.assertAttachmentsInChat(chatId, attachmentIds);
|
||||
const scope = { userId: callerId, chatId, key };
|
||||
const requestHash = createHash('sha256')
|
||||
.update(JSON.stringify({ text, attachment_ids: attachmentIds }))
|
||||
.digest('hex');
|
||||
try {
|
||||
return await this.prisma.$transaction(async (tx) => {
|
||||
await tx.idempotencyKey.deleteMany({
|
||||
where: {
|
||||
...scope,
|
||||
createdAt: { lt: new Date(Date.now() - this.keyTtlMs) },
|
||||
},
|
||||
});
|
||||
const row = await tx.message.create({
|
||||
data: {
|
||||
chatId,
|
||||
senderId: callerId,
|
||||
text,
|
||||
attachments: {
|
||||
create: attachmentIds.map((attachmentId, position) => ({
|
||||
attachmentId,
|
||||
position,
|
||||
})),
|
||||
},
|
||||
},
|
||||
include: ATTACHMENT_LINKS,
|
||||
});
|
||||
const { count } = await tx.idempotencyKey.createMany({
|
||||
data: [{ ...scope, requestHash, messageId: row.id }],
|
||||
skipDuplicates: true,
|
||||
});
|
||||
if (count === 0) {
|
||||
throw new KeyAlreadyUsed();
|
||||
}
|
||||
return linkedMessage(row);
|
||||
});
|
||||
} catch (error) {
|
||||
if (!(error instanceof KeyAlreadyUsed)) {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
const stored = await this.prisma.idempotencyKey.findUnique({
|
||||
where: { userId_chatId_key: scope },
|
||||
include: { message: { include: ATTACHMENT_LINKS } },
|
||||
});
|
||||
if (!stored || stored.requestHash !== requestHash) {
|
||||
throw new ApiError(
|
||||
'conflict',
|
||||
'Idempotency-Key was already used with a different request',
|
||||
);
|
||||
}
|
||||
return linkedMessage(stored.message);
|
||||
}
|
||||
|
||||
async list(
|
||||
chatId: string,
|
||||
callerId: string,
|
||||
limit: number,
|
||||
rawCursor: string | null,
|
||||
): Promise<MessagePageResponse> {
|
||||
await this.assertMember(chatId, callerId);
|
||||
let after = Prisma.empty;
|
||||
if (rawCursor !== null) {
|
||||
const cursor = decodeMessageCursor(rawCursor);
|
||||
if (cursor && cursor.chatId !== chatId) {
|
||||
throw new ApiError(
|
||||
'invalid_request',
|
||||
'cursor belongs to a different chat',
|
||||
);
|
||||
}
|
||||
const anchor = cursor
|
||||
? await this.prisma.message.findFirst({
|
||||
where: { id: cursor.id, chatId },
|
||||
select: { id: true, createdAt: true },
|
||||
})
|
||||
: null;
|
||||
if (!anchor) {
|
||||
throw new ApiError('invalid_request', 'invalid cursor');
|
||||
}
|
||||
after = Prisma.sql`AND (created_at, id) > (${anchor.createdAt}::timestamptz, ${anchor.id}::uuid)`;
|
||||
}
|
||||
const rows = await this.prisma.$queryRaw<MessageRow[]>`
|
||||
SELECT id, chat_id AS "chatId", sender_id AS "senderId", text, created_at AS "createdAt",
|
||||
ARRAY(
|
||||
SELECT attachment_id::text FROM message_attachments
|
||||
WHERE message_id = messages.id
|
||||
ORDER BY position
|
||||
) AS "attachmentIds"
|
||||
FROM messages
|
||||
WHERE chat_id = ${chatId}::uuid ${after}
|
||||
ORDER BY created_at, id
|
||||
LIMIT ${limit + 1}`;
|
||||
const items = rows.slice(0, limit).map(toMessage);
|
||||
const last = items[items.length - 1];
|
||||
return {
|
||||
items: items.map(toMessageResponse),
|
||||
next_cursor:
|
||||
rows.length > limit
|
||||
? encodeMessageCursor({ chatId, id: last.id })
|
||||
: null,
|
||||
};
|
||||
}
|
||||
|
||||
private assertMember(chatId: string, callerId: string): Promise<void> {
|
||||
return assertChatMember(this.prisma, chatId, callerId);
|
||||
}
|
||||
|
||||
private async assertAttachmentsInChat(
|
||||
chatId: string,
|
||||
attachmentIds: string[],
|
||||
): Promise<void> {
|
||||
if (attachmentIds.length === 0) {
|
||||
return;
|
||||
}
|
||||
const found = await this.prisma.attachment.count({
|
||||
where: { id: { in: attachmentIds }, chatId },
|
||||
});
|
||||
if (found !== attachmentIds.length) {
|
||||
throw new ApiError('not_found', 'attachment not found');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function linkedMessage(row: MessageWithLinks): Message {
|
||||
return toMessage({
|
||||
...row,
|
||||
attachmentIds: row.attachments.map((link) => link.attachmentId),
|
||||
});
|
||||
}
|
||||
|
||||
function toMessage(row: MessageRow): Message {
|
||||
return {
|
||||
id: row.id,
|
||||
chatId: row.chatId,
|
||||
senderId: row.senderId,
|
||||
text: row.text,
|
||||
createdAt: row.createdAt.toISOString(),
|
||||
attachmentIds: row.attachmentIds,
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
import { Logger } from '@nestjs/common';
|
||||
import { reconcile } from './attachments/reconciler';
|
||||
import { PrismaService } from './db/prisma.service';
|
||||
import { S3Service } from './storage/s3.service';
|
||||
|
||||
const DEFAULT_GRACE_SECONDS = 3600;
|
||||
const DEFAULT_INTERVAL_SECONDS = 300;
|
||||
|
||||
function option(name: string, fallback: number): number {
|
||||
const prefix = `--${name}=`;
|
||||
const raw = process.argv.find((arg) => arg.startsWith(prefix));
|
||||
const value = raw ? Number(raw.slice(prefix.length)) : fallback;
|
||||
if (!Number.isInteger(value) || value < 0) {
|
||||
throw new Error(`--${name} must be a non-negative integer`);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
async function main(): Promise<void> {
|
||||
process.once('SIGTERM', () => process.exit(0));
|
||||
process.once('SIGINT', () => process.exit(0));
|
||||
const apply = process.argv.includes('--apply');
|
||||
const loop = process.argv.includes('--loop');
|
||||
const graceSeconds = option('grace-seconds', DEFAULT_GRACE_SECONDS);
|
||||
const intervalSeconds = option('interval-seconds', DEFAULT_INTERVAL_SECONDS);
|
||||
const prisma = new PrismaService();
|
||||
const s3 = new S3Service();
|
||||
do {
|
||||
try {
|
||||
const report = await reconcile(prisma, s3, {
|
||||
apply,
|
||||
graceMs: graceSeconds * 1000,
|
||||
});
|
||||
process.stdout.write(`${JSON.stringify(report)}\n`);
|
||||
} catch (error) {
|
||||
const reason = error instanceof Error ? error.name : 'unknown error';
|
||||
Logger.error(`reconcile pass aborted: ${reason}`, 'Reconciler');
|
||||
process.exitCode = loop ? 0 : 1;
|
||||
}
|
||||
if (loop) {
|
||||
await new Promise((resolve) => setTimeout(resolve, intervalSeconds * 1000));
|
||||
}
|
||||
} while (loop);
|
||||
await prisma.$disconnect();
|
||||
s3.onModuleDestroy();
|
||||
}
|
||||
|
||||
main().catch((error) => {
|
||||
Logger.error(error instanceof Error ? error.message : 'fatal error', 'Reconciler');
|
||||
process.exit(1);
|
||||
});
|
||||
@@ -0,0 +1,168 @@
|
||||
import {
|
||||
DeleteObjectCommand,
|
||||
GetObjectCommand,
|
||||
HeadBucketCommand,
|
||||
ListObjectsV2Command,
|
||||
PutObjectCommand,
|
||||
S3Client,
|
||||
} from '@aws-sdk/client-s3';
|
||||
import { Injectable, Logger, OnModuleDestroy } from '@nestjs/common';
|
||||
import { createReadStream, readFileSync } from 'fs';
|
||||
import { Readable } from 'stream';
|
||||
import { ApiError } from '../common/filters/api-error';
|
||||
|
||||
const CONNECT_TIMEOUT_MS = 1000;
|
||||
const IDLE_TIMEOUT_MS = 5000;
|
||||
const OPERATION_TIMEOUT_MS = 20000;
|
||||
const HEALTH_TIMEOUT_MS = 1000;
|
||||
|
||||
export interface ObjectPage {
|
||||
objects: { key: string; lastModified: Date }[];
|
||||
nextToken?: string;
|
||||
}
|
||||
|
||||
export interface StoredFile {
|
||||
key: string;
|
||||
path: string;
|
||||
size: number;
|
||||
contentType: string;
|
||||
sha256: string;
|
||||
}
|
||||
|
||||
@Injectable()
|
||||
export class S3Service implements OnModuleDestroy {
|
||||
private readonly logger = new Logger('S3Service');
|
||||
private readonly client: S3Client;
|
||||
private readonly streamClient: S3Client;
|
||||
private readonly bucket = process.env.S3_BUCKET ?? 'messenger-attachments';
|
||||
|
||||
constructor() {
|
||||
const config = {
|
||||
endpoint: process.env.S3_ENDPOINT ?? 'http://s3:9000',
|
||||
region: process.env.S3_REGION ?? 'us-east-1',
|
||||
forcePathStyle: true,
|
||||
maxAttempts: 1,
|
||||
credentials: {
|
||||
accessKeyId: readSecret('S3_ACCESS_KEY_ID'),
|
||||
secretAccessKey: readSecret('S3_SECRET_ACCESS_KEY'),
|
||||
},
|
||||
};
|
||||
this.client = new S3Client({
|
||||
...config,
|
||||
requestHandler: {
|
||||
connectionTimeout: CONNECT_TIMEOUT_MS,
|
||||
requestTimeout: IDLE_TIMEOUT_MS,
|
||||
},
|
||||
});
|
||||
this.streamClient = new S3Client({
|
||||
...config,
|
||||
requestHandler: { connectionTimeout: CONNECT_TIMEOUT_MS },
|
||||
});
|
||||
}
|
||||
|
||||
async putFile(file: StoredFile): Promise<void> {
|
||||
await this.guard(`put ${file.key}`, () =>
|
||||
this.client.send(
|
||||
new PutObjectCommand({
|
||||
Bucket: this.bucket,
|
||||
Key: file.key,
|
||||
Body: createReadStream(file.path),
|
||||
ContentLength: file.size,
|
||||
ContentType: file.contentType,
|
||||
ChecksumSHA256: Buffer.from(file.sha256, 'hex').toString('base64'),
|
||||
}),
|
||||
{ abortSignal: AbortSignal.timeout(OPERATION_TIMEOUT_MS) },
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
async getStream(key: string): Promise<Readable> {
|
||||
const deadline = new AbortController();
|
||||
const timer = setTimeout(() => deadline.abort(), IDLE_TIMEOUT_MS);
|
||||
try {
|
||||
const { Body } = await this.streamClient.send(
|
||||
new GetObjectCommand({ Bucket: this.bucket, Key: key }),
|
||||
{ abortSignal: deadline.signal },
|
||||
);
|
||||
return Body as Readable;
|
||||
} catch (error) {
|
||||
const name = error instanceof Error ? error.name : 'unknown error';
|
||||
this.logger.error(`get ${key} failed: ${name}`);
|
||||
if (name === 'NoSuchKey') {
|
||||
throw new Error(`object ${key} is missing from storage`);
|
||||
}
|
||||
throw new ApiError('unavailable', 'object storage unavailable');
|
||||
} finally {
|
||||
clearTimeout(timer);
|
||||
}
|
||||
}
|
||||
|
||||
async isAvailable(): Promise<boolean> {
|
||||
try {
|
||||
await this.client.send(new HeadBucketCommand({ Bucket: this.bucket }), {
|
||||
abortSignal: AbortSignal.timeout(HEALTH_TIMEOUT_MS),
|
||||
});
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
async listObjects(
|
||||
prefix: string,
|
||||
pageSize: number,
|
||||
token?: string,
|
||||
): Promise<ObjectPage> {
|
||||
const page = await this.guard(`list ${prefix}`, () =>
|
||||
this.client.send(
|
||||
new ListObjectsV2Command({
|
||||
Bucket: this.bucket,
|
||||
Prefix: prefix,
|
||||
MaxKeys: pageSize,
|
||||
ContinuationToken: token,
|
||||
}),
|
||||
{ abortSignal: AbortSignal.timeout(OPERATION_TIMEOUT_MS) },
|
||||
),
|
||||
);
|
||||
return {
|
||||
objects: (page.Contents ?? []).map((object) => ({
|
||||
key: object.Key!,
|
||||
lastModified: object.LastModified!,
|
||||
})),
|
||||
nextToken: page.NextContinuationToken,
|
||||
};
|
||||
}
|
||||
|
||||
async delete(key: string): Promise<void> {
|
||||
await this.guard(`delete ${key}`, () =>
|
||||
this.client.send(
|
||||
new DeleteObjectCommand({ Bucket: this.bucket, Key: key }),
|
||||
{ abortSignal: AbortSignal.timeout(OPERATION_TIMEOUT_MS) },
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
onModuleDestroy(): void {
|
||||
this.client.destroy();
|
||||
this.streamClient.destroy();
|
||||
}
|
||||
|
||||
private async guard<T>(action: string, run: () => Promise<T>): Promise<T> {
|
||||
try {
|
||||
return await run();
|
||||
} catch (error) {
|
||||
const reason = error instanceof Error ? error.name : 'unknown error';
|
||||
this.logger.error(`${action} failed: ${reason}`);
|
||||
throw new ApiError('unavailable', 'object storage unavailable');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function readSecret(name: string): string {
|
||||
const file = process.env[`${name}_FILE`];
|
||||
const value = file ? readFileSync(file, 'utf8').trim() : process.env[name];
|
||||
if (!value) {
|
||||
throw new Error(`${name}_FILE or ${name} must be set`);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
import { Global, Module } from '@nestjs/common';
|
||||
import { S3Service } from './s3.service';
|
||||
|
||||
@Global()
|
||||
@Module({
|
||||
providers: [S3Service],
|
||||
exports: [S3Service],
|
||||
})
|
||||
export class StorageModule {}
|
||||
@@ -0,0 +1,18 @@
|
||||
import { Matches } from 'class-validator';
|
||||
import { CodePointLength } from '../../common/dto/code-point-length.validator';
|
||||
import { NotBlank } from '../../common/dto/not-blank.validator';
|
||||
|
||||
export class CreateUserDto {
|
||||
@Matches(/^[a-z0-9_]{3,32}$/, {
|
||||
message: 'username must match [a-z0-9_]{3,32}',
|
||||
})
|
||||
username!: string;
|
||||
|
||||
@NotBlank()
|
||||
@CodePointLength(1, 100)
|
||||
display_name!: string;
|
||||
|
||||
@NotBlank()
|
||||
@CodePointLength(12, 128)
|
||||
password!: string;
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
import { User } from '../../common/domain';
|
||||
|
||||
export interface UserResponse {
|
||||
id: string;
|
||||
username: string;
|
||||
display_name: string;
|
||||
created_at: string;
|
||||
}
|
||||
|
||||
export function toUserResponse(user: User): UserResponse {
|
||||
return {
|
||||
id: user.id,
|
||||
username: user.username,
|
||||
display_name: user.displayName,
|
||||
created_at: user.createdAt,
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
import {
|
||||
Body,
|
||||
Controller,
|
||||
Get,
|
||||
HttpCode,
|
||||
Post,
|
||||
UseGuards,
|
||||
} from '@nestjs/common';
|
||||
import { AuthGuard } from '../auth/auth.guard';
|
||||
import { CurrentUser } from '../auth/current-user.decorator';
|
||||
import { User } from '../common/domain';
|
||||
import { CreateUserDto } from './dto/create-user.dto';
|
||||
import { toUserResponse, UserResponse } from './dto/user.response';
|
||||
import { UsersService } from './users.service';
|
||||
|
||||
@Controller('api/v1/users')
|
||||
export class UsersController {
|
||||
constructor(private readonly users: UsersService) {}
|
||||
|
||||
@Post()
|
||||
@HttpCode(201)
|
||||
async create(@Body() dto: CreateUserDto): Promise<UserResponse> {
|
||||
const user = await this.users.register(
|
||||
dto.username,
|
||||
dto.display_name,
|
||||
dto.password,
|
||||
);
|
||||
return toUserResponse(user);
|
||||
}
|
||||
|
||||
@Get('me')
|
||||
@UseGuards(AuthGuard)
|
||||
me(@CurrentUser() user: User): UserResponse {
|
||||
return toUserResponse(user);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { UsersController } from './users.controller';
|
||||
import { UsersService } from './users.service';
|
||||
|
||||
@Module({
|
||||
controllers: [UsersController],
|
||||
providers: [UsersService],
|
||||
exports: [UsersService],
|
||||
})
|
||||
export class UsersModule {}
|
||||
@@ -0,0 +1,48 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { hashPassword } from '../auth/password';
|
||||
import { ApiError } from '../common/filters/api-error';
|
||||
import { User } from '../common/domain';
|
||||
import { PrismaService, isUniqueViolation } from '../db/prisma.service';
|
||||
|
||||
@Injectable()
|
||||
export class UsersService {
|
||||
constructor(private readonly prisma: PrismaService) {}
|
||||
|
||||
async register(
|
||||
username: string,
|
||||
displayName: string,
|
||||
password: string,
|
||||
): Promise<User> {
|
||||
const passwordHash = await hashPassword(password);
|
||||
try {
|
||||
const row = await this.prisma.user.create({
|
||||
data: { username, displayName: displayName.trim(), passwordHash },
|
||||
});
|
||||
return toUser(row);
|
||||
} catch (error) {
|
||||
if (isUniqueViolation(error)) {
|
||||
throw new ApiError('conflict', 'username is already taken');
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async findById(id: string): Promise<User | null> {
|
||||
const row = await this.prisma.user.findUnique({ where: { id } });
|
||||
return row ? toUser(row) : null;
|
||||
}
|
||||
}
|
||||
|
||||
export function toUser(row: {
|
||||
id: string;
|
||||
username: string;
|
||||
displayName: string;
|
||||
createdAt: Date;
|
||||
}): User {
|
||||
return {
|
||||
id: row.id,
|
||||
username: row.username,
|
||||
displayName: row.displayName,
|
||||
createdAt: row.createdAt.toISOString(),
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,686 @@
|
||||
import { INestApplication } from '@nestjs/common';
|
||||
import request from 'supertest';
|
||||
import { randomUUID } from 'crypto';
|
||||
import { PrismaService } from '../src/db/prisma.service';
|
||||
import {
|
||||
bearer,
|
||||
createTestApp,
|
||||
PASSWORD,
|
||||
registerUser,
|
||||
TestUser,
|
||||
} from './helpers';
|
||||
|
||||
describe('API мессенджера (e2e) — граничные случаи сверх публичного smoke', () => {
|
||||
let app: INestApplication;
|
||||
let server: any;
|
||||
|
||||
beforeAll(async () => {
|
||||
({ app, server } = await createTestApp());
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
await app.close();
|
||||
});
|
||||
|
||||
function createUser(label: string): Promise<TestUser> {
|
||||
return registerUser(server, label);
|
||||
}
|
||||
|
||||
describe('POST /api/v1/users', () => {
|
||||
it('отклоняет некорректный JSON с кодом 400', async () => {
|
||||
const res = await request(server)
|
||||
.post('/api/v1/users')
|
||||
.set('Content-Type', 'application/json')
|
||||
.send('{not valid json')
|
||||
.expect(400);
|
||||
expect(res.body.error.code).toBe('invalid_request');
|
||||
expect(res.body.error.request_id).toBeTruthy();
|
||||
});
|
||||
|
||||
it('отклоняет тело с лишними полями', async () => {
|
||||
const res = await request(server)
|
||||
.post('/api/v1/users')
|
||||
.send({
|
||||
username: 'validname1',
|
||||
display_name: 'Valid',
|
||||
password: PASSWORD,
|
||||
extra: 'nope',
|
||||
})
|
||||
.expect(400);
|
||||
expect(res.body.error.code).toBe('invalid_request');
|
||||
});
|
||||
|
||||
it('отклоняет username с заглавными буквами', async () => {
|
||||
await request(server)
|
||||
.post('/api/v1/users')
|
||||
.send({ username: 'InvalidName', display_name: 'X', password: PASSWORD })
|
||||
.expect(400);
|
||||
});
|
||||
|
||||
it('отклоняет display_name из одних пробелов', async () => {
|
||||
await request(server)
|
||||
.post('/api/v1/users')
|
||||
.send({ username: 'wsonlyname1', display_name: ' ', password: PASSWORD })
|
||||
.expect(400);
|
||||
});
|
||||
|
||||
it('отклоняет дубликат username с кодом 409', async () => {
|
||||
const user = await createUser('dup');
|
||||
const res = await request(server)
|
||||
.post('/api/v1/users')
|
||||
.send({ username: user.username, display_name: 'Someone else', password: PASSWORD })
|
||||
.expect(409);
|
||||
expect(res.body.error.code).toBe('conflict');
|
||||
});
|
||||
|
||||
it('никогда не возвращает поле пароля или его хэша', async () => {
|
||||
const res = await request(server)
|
||||
.post('/api/v1/users')
|
||||
.send({
|
||||
username: `nopass_${randomUUID().slice(0, 8)}`,
|
||||
display_name: 'X',
|
||||
password: PASSWORD,
|
||||
})
|
||||
.expect(201);
|
||||
expect(res.body).not.toHaveProperty('password');
|
||||
expect(res.body).not.toHaveProperty('password_hash');
|
||||
});
|
||||
|
||||
it('отклоняет username короче 3 символов', async () => {
|
||||
await request(server)
|
||||
.post('/api/v1/users')
|
||||
.send({ username: 'ab', display_name: 'X', password: PASSWORD })
|
||||
.expect(400);
|
||||
});
|
||||
|
||||
it('отклоняет username длиннее 32 символов', async () => {
|
||||
await request(server)
|
||||
.post('/api/v1/users')
|
||||
.send({ username: 'a'.repeat(33), display_name: 'X', password: PASSWORD })
|
||||
.expect(400);
|
||||
});
|
||||
|
||||
it('отклоняет username с недопустимыми символами (дефис)', async () => {
|
||||
await request(server)
|
||||
.post('/api/v1/users')
|
||||
.send({ username: 'has-hyphen', display_name: 'X', password: PASSWORD })
|
||||
.expect(400);
|
||||
});
|
||||
|
||||
it('принимает username ровно на границах длины 3 и 32 символа', async () => {
|
||||
await request(server)
|
||||
.post('/api/v1/users')
|
||||
.send({
|
||||
username: `a${randomUUID().slice(0, 2)}`,
|
||||
display_name: 'X',
|
||||
password: PASSWORD,
|
||||
})
|
||||
.expect(201);
|
||||
const longest = ('z' + randomUUID().replace(/-/g, '')).slice(0, 32);
|
||||
await request(server)
|
||||
.post('/api/v1/users')
|
||||
.send({ username: longest, display_name: 'X', password: PASSWORD })
|
||||
.expect(201);
|
||||
});
|
||||
|
||||
it('отклоняет display_name длиннее 100 Unicode code points', async () => {
|
||||
await request(server)
|
||||
.post('/api/v1/users')
|
||||
.send({
|
||||
username: `toolongname_${randomUUID().slice(0, 6)}`,
|
||||
display_name: 'x'.repeat(101),
|
||||
password: PASSWORD,
|
||||
})
|
||||
.expect(400);
|
||||
});
|
||||
|
||||
it('принимает display_name ровно из 100 code points, считая эмодзи вне BMP за один символ', async () => {
|
||||
const displayName = '👋'.repeat(100);
|
||||
expect([...displayName].length).toBe(100);
|
||||
await request(server)
|
||||
.post('/api/v1/users')
|
||||
.send({
|
||||
username: `emojiname_${randomUUID().slice(0, 6)}`,
|
||||
display_name: displayName,
|
||||
password: PASSWORD,
|
||||
})
|
||||
.expect(201);
|
||||
});
|
||||
|
||||
it('отклоняет display_name из 101 code point, состоящий из эмодзи вне BMP', async () => {
|
||||
const displayName = '👋'.repeat(100) + 'x';
|
||||
expect([...displayName].length).toBe(101);
|
||||
await request(server)
|
||||
.post('/api/v1/users')
|
||||
.send({
|
||||
username: `emojitoolong_${randomUUID().slice(0, 6)}`,
|
||||
display_name: displayName,
|
||||
password: PASSWORD,
|
||||
})
|
||||
.expect(400);
|
||||
});
|
||||
});
|
||||
|
||||
describe('контроль доступа', () => {
|
||||
it('возвращает 401 раньше 404, если и актор, и ресурс невалидны', async () => {
|
||||
const res = await request(server)
|
||||
.get(`/api/v1/chats/${randomUUID()}`)
|
||||
.set('Authorization', 'Bearer not-a-token')
|
||||
.expect(401);
|
||||
expect(res.body.error.code).toBe('unauthorized');
|
||||
});
|
||||
|
||||
it('возвращает 404 (не 403), когда чат запрашивает не его участник', async () => {
|
||||
const alice = await createUser('alice');
|
||||
const bob = await createUser('bob');
|
||||
const eve = await createUser('eve');
|
||||
const chat = await request(server)
|
||||
.post('/api/v1/chats')
|
||||
.set('Authorization', bearer(alice))
|
||||
.send({ title: 'Private', member_ids: [alice.id, bob.id] })
|
||||
.expect(201);
|
||||
|
||||
const res = await request(server)
|
||||
.get(`/api/v1/chats/${chat.body.id}`)
|
||||
.set('Authorization', bearer(eve))
|
||||
.expect(404);
|
||||
expect(res.body.error.code).toBe('not_found');
|
||||
});
|
||||
|
||||
it('отклоняет создание чата, если вызывающий не входит в member_ids', async () => {
|
||||
const alice = await createUser('alice2');
|
||||
const bob = await createUser('bob2');
|
||||
await request(server)
|
||||
.post('/api/v1/chats')
|
||||
.set('Authorization', bearer(alice))
|
||||
.send({ title: 'Missing caller', member_ids: [bob.id] })
|
||||
.expect(400);
|
||||
});
|
||||
|
||||
it('отклоняет создание чата с дублирующимися member_ids', async () => {
|
||||
const alice = await createUser('alice3');
|
||||
await request(server)
|
||||
.post('/api/v1/chats')
|
||||
.set('Authorization', bearer(alice))
|
||||
.send({
|
||||
title: 'Dup members',
|
||||
member_ids: [alice.id, alice.id],
|
||||
})
|
||||
.expect(400);
|
||||
});
|
||||
|
||||
it('считает один UUID в разном регистре дубликатом и узнаёт вызывающего в верхнем регистре', async () => {
|
||||
const alice = await createUser('alicecase');
|
||||
await request(server)
|
||||
.post('/api/v1/chats')
|
||||
.set('Authorization', bearer(alice))
|
||||
.send({ title: 'Case', member_ids: [alice.id, alice.id.toUpperCase()] })
|
||||
.expect(400);
|
||||
const res = await request(server)
|
||||
.post('/api/v1/chats')
|
||||
.set('Authorization', bearer(alice))
|
||||
.send({ title: 'Upper', member_ids: [alice.id.toUpperCase()] })
|
||||
.expect(201);
|
||||
expect(res.body.member_ids).toEqual([alice.id]);
|
||||
});
|
||||
|
||||
it('принимает ровно 100 участников и отклоняет 101', async () => {
|
||||
const alice = await createUser('max');
|
||||
const ids = Array.from({ length: 100 }, () => randomUUID());
|
||||
await app.get(PrismaService).user.createMany({
|
||||
data: ids.map((id) => ({
|
||||
id,
|
||||
username: `m_${id.replace(/-/g, '').slice(0, 30)}`,
|
||||
displayName: 'Member',
|
||||
passwordHash: 'not-a-real-hash',
|
||||
})),
|
||||
});
|
||||
const ok = await request(server)
|
||||
.post('/api/v1/chats')
|
||||
.set('Authorization', bearer(alice))
|
||||
.send({ title: 'Max', member_ids: [alice.id, ...ids.slice(0, 99)] })
|
||||
.expect(201);
|
||||
expect(ok.body.member_ids).toHaveLength(100);
|
||||
await request(server)
|
||||
.post('/api/v1/chats')
|
||||
.set('Authorization', bearer(alice))
|
||||
.send({ title: 'Too many', member_ids: [alice.id, ...ids] })
|
||||
.expect(400);
|
||||
});
|
||||
|
||||
it('отклоняет создание чата с неизвестным участником', async () => {
|
||||
const alice = await createUser('alice4');
|
||||
await request(server)
|
||||
.post('/api/v1/chats')
|
||||
.set('Authorization', bearer(alice))
|
||||
.send({
|
||||
title: 'Unknown member',
|
||||
member_ids: [alice.id, randomUUID()],
|
||||
})
|
||||
.expect(400);
|
||||
});
|
||||
|
||||
it('отклоняет создание чата с пустым/пробельным title', async () => {
|
||||
const alice = await createUser('alice4b');
|
||||
await request(server)
|
||||
.post('/api/v1/chats')
|
||||
.set('Authorization', bearer(alice))
|
||||
.send({ title: ' ', member_ids: [alice.id] })
|
||||
.expect(400);
|
||||
});
|
||||
|
||||
it('возвращает 400 для синтаксически некорректного chat_id (не UUID)', async () => {
|
||||
const alice = await createUser('alice4c');
|
||||
const res = await request(server)
|
||||
.get('/api/v1/chats/not-a-uuid')
|
||||
.set('Authorization', bearer(alice))
|
||||
.expect(400);
|
||||
expect(res.body.error.code).toBe('invalid_request');
|
||||
});
|
||||
|
||||
it('возвращает 404 для корректного по формату, но несуществующего chat_id', async () => {
|
||||
const alice = await createUser('alice4d');
|
||||
await request(server)
|
||||
.get(`/api/v1/chats/${randomUUID()}`)
|
||||
.set('Authorization', bearer(alice))
|
||||
.expect(404);
|
||||
});
|
||||
});
|
||||
|
||||
describe('GET /api/v1/chats (пагинация)', () => {
|
||||
it('использует limit=50 по умолчанию, если limit не передан', async () => {
|
||||
const alice = await createUser('pgalice1');
|
||||
await request(server)
|
||||
.post('/api/v1/chats')
|
||||
.set('Authorization', bearer(alice))
|
||||
.send({ title: 'Only chat', member_ids: [alice.id] })
|
||||
.expect(201);
|
||||
const res = await request(server)
|
||||
.get('/api/v1/chats')
|
||||
.set('Authorization', bearer(alice))
|
||||
.expect(200);
|
||||
expect(res.body.items.length).toBeGreaterThanOrEqual(1);
|
||||
expect(res.body.items.length).toBeLessThanOrEqual(50);
|
||||
});
|
||||
|
||||
it('отклоняет нечисловой limit', async () => {
|
||||
const alice = await createUser('pgalice2');
|
||||
await request(server)
|
||||
.get('/api/v1/chats?limit=abc')
|
||||
.set('Authorization', bearer(alice))
|
||||
.expect(400);
|
||||
});
|
||||
|
||||
it('отклоняет limit=0', async () => {
|
||||
const alice = await createUser('pgalice3');
|
||||
await request(server)
|
||||
.get('/api/v1/chats?limit=0')
|
||||
.set('Authorization', bearer(alice))
|
||||
.expect(400);
|
||||
});
|
||||
|
||||
it('отклоняет повторённые limit и cursor', async () => {
|
||||
const alice = await createUser('pgrepeat');
|
||||
for (const query of ['limit=1&limit=2', 'cursor=a&cursor=b']) {
|
||||
await request(server)
|
||||
.get(`/api/v1/chats?${query}`)
|
||||
.set('Authorization', bearer(alice))
|
||||
.expect(400);
|
||||
}
|
||||
});
|
||||
|
||||
it('отклоняет cursor, не являющийся корректным base64url JSON', async () => {
|
||||
const alice = await createUser('pgalice4');
|
||||
await request(server)
|
||||
.get('/api/v1/chats')
|
||||
.query({ cursor: 'not-a-real-cursor!!!' })
|
||||
.set('Authorization', bearer(alice))
|
||||
.expect(400);
|
||||
});
|
||||
|
||||
it('отклоняет корректно закодированный cursor с id чата, который никогда не существовал', async () => {
|
||||
const alice = await createUser('pgalice4b');
|
||||
const bogusCursor = Buffer.from(
|
||||
JSON.stringify({ id: randomUUID() }),
|
||||
'utf8',
|
||||
).toString('base64url');
|
||||
const res = await request(server)
|
||||
.get('/api/v1/chats')
|
||||
.query({ cursor: bogusCursor })
|
||||
.set('Authorization', bearer(alice))
|
||||
.expect(400);
|
||||
expect(res.body.error.code).toBe('invalid_request');
|
||||
});
|
||||
|
||||
it('обходит весь список чатов по страницам без пропусков и дублей', async () => {
|
||||
const alice = await createUser('pgalice5');
|
||||
const titles = ['c1', 'c2', 'c3', 'c4', 'c5'];
|
||||
for (const title of titles) {
|
||||
await request(server)
|
||||
.post('/api/v1/chats')
|
||||
.set('Authorization', bearer(alice))
|
||||
.send({ title, member_ids: [alice.id] })
|
||||
.expect(201);
|
||||
}
|
||||
|
||||
const seen: string[] = [];
|
||||
let cursor: string | null = null;
|
||||
do {
|
||||
const res: request.Response = await request(server)
|
||||
.get('/api/v1/chats')
|
||||
.query({ limit: 2, ...(cursor ? { cursor } : {}) })
|
||||
.set('Authorization', bearer(alice))
|
||||
.expect(200);
|
||||
seen.push(
|
||||
...res.body.items.map((c: { title: string }) => c.title),
|
||||
);
|
||||
cursor = res.body.next_cursor;
|
||||
} while (cursor);
|
||||
|
||||
expect(seen).toEqual(titles);
|
||||
});
|
||||
});
|
||||
|
||||
describe('сообщения', () => {
|
||||
async function createChat(alice: TestUser, bob: TestUser) {
|
||||
const res = await request(server)
|
||||
.post('/api/v1/chats')
|
||||
.set('Authorization', bearer(alice))
|
||||
.send({ title: 'Chat', member_ids: [alice.id, bob.id] })
|
||||
.expect(201);
|
||||
return res.body as { id: string };
|
||||
}
|
||||
|
||||
it('не принимает sender_id, переданный клиентом', async () => {
|
||||
const alice = await createUser('alice5');
|
||||
const bob = await createUser('bob5');
|
||||
const chat = await createChat(alice, bob);
|
||||
|
||||
const res = await request(server)
|
||||
.post(`/api/v1/chats/${chat.id}/messages`)
|
||||
.set('Idempotency-Key', randomUUID())
|
||||
.set('Authorization', bearer(alice))
|
||||
.send({ text: 'hi', sender_id: bob.id })
|
||||
.expect(400);
|
||||
expect(res.body.error.code).toBe('invalid_request');
|
||||
});
|
||||
|
||||
it('считает attachment_ids: [] равным отсутствию поля, неизвестный ID — 404, неверный тип — 400', async () => {
|
||||
const alice = await createUser('aliceatt');
|
||||
const bob = await createUser('bobatt');
|
||||
const chat = await createChat(alice, bob);
|
||||
const path = `/api/v1/chats/${chat.id}/messages`;
|
||||
const key = randomUUID();
|
||||
const first = await request(server)
|
||||
.post(path)
|
||||
.set('Idempotency-Key', key)
|
||||
.set('Authorization', bearer(alice))
|
||||
.send({ text: 'att' })
|
||||
.expect(201);
|
||||
const replay = await request(server)
|
||||
.post(path)
|
||||
.set('Idempotency-Key', key)
|
||||
.set('Authorization', bearer(alice))
|
||||
.send({ attachment_ids: [], text: 'att' })
|
||||
.expect(201);
|
||||
expect(replay.body).toEqual(first.body);
|
||||
await request(server)
|
||||
.post(path)
|
||||
.set('Idempotency-Key', randomUUID())
|
||||
.set('Authorization', bearer(alice))
|
||||
.send({ text: 'x', attachment_ids: [randomUUID()] })
|
||||
.expect(404);
|
||||
for (const attachment_ids of [null, 'x']) {
|
||||
await request(server)
|
||||
.post(path)
|
||||
.set('Idempotency-Key', randomUUID())
|
||||
.set('Authorization', bearer(alice))
|
||||
.send({ text: 'x', attachment_ids })
|
||||
.expect(400);
|
||||
}
|
||||
});
|
||||
|
||||
it('отклоняет пустое сообщение', async () => {
|
||||
const alice = await createUser('alice6');
|
||||
const bob = await createUser('bob6');
|
||||
const chat = await createChat(alice, bob);
|
||||
await request(server)
|
||||
.post(`/api/v1/chats/${chat.id}/messages`)
|
||||
.set('Idempotency-Key', randomUUID())
|
||||
.set('Authorization', bearer(alice))
|
||||
.send({ text: '' })
|
||||
.expect(400);
|
||||
});
|
||||
|
||||
it('отклоняет сообщение из одних пробелов', async () => {
|
||||
const alice = await createUser('alice7');
|
||||
const bob = await createUser('bob7');
|
||||
const chat = await createChat(alice, bob);
|
||||
await request(server)
|
||||
.post(`/api/v1/chats/${chat.id}/messages`)
|
||||
.set('Idempotency-Key', randomUUID())
|
||||
.set('Authorization', bearer(alice))
|
||||
.send({ text: ' \n\t ' })
|
||||
.expect(400);
|
||||
});
|
||||
|
||||
it('отклоняет отправку в чат, где вызывающий не участник', async () => {
|
||||
const alice = await createUser('alice8');
|
||||
const bob = await createUser('bob8');
|
||||
const eve = await createUser('eve8');
|
||||
const chat = await createChat(alice, bob);
|
||||
await request(server)
|
||||
.post(`/api/v1/chats/${chat.id}/messages`)
|
||||
.set('Idempotency-Key', randomUUID())
|
||||
.set('Authorization', bearer(eve))
|
||||
.send({ text: 'intrusion' })
|
||||
.expect(404);
|
||||
});
|
||||
|
||||
it('отклоняет limit=0 при чтении истории', async () => {
|
||||
const alice = await createUser('alice9');
|
||||
const bob = await createUser('bob9');
|
||||
const chat = await createChat(alice, bob);
|
||||
await request(server)
|
||||
.get(`/api/v1/chats/${chat.id}/messages?limit=0`)
|
||||
.set('Authorization', bearer(alice))
|
||||
.expect(400);
|
||||
});
|
||||
|
||||
it('отклоняет limit=101 при чтении истории', async () => {
|
||||
const alice = await createUser('alice10');
|
||||
const bob = await createUser('bob10');
|
||||
const chat = await createChat(alice, bob);
|
||||
await request(server)
|
||||
.get(`/api/v1/chats/${chat.id}/messages?limit=101`)
|
||||
.set('Authorization', bearer(alice))
|
||||
.expect(400);
|
||||
});
|
||||
|
||||
it('отклоняет нечисловой limit', async () => {
|
||||
const alice = await createUser('alice10b');
|
||||
const bob = await createUser('bob10b');
|
||||
const chat = await createChat(alice, bob);
|
||||
await request(server)
|
||||
.get(`/api/v1/chats/${chat.id}/messages?limit=abc`)
|
||||
.set('Authorization', bearer(alice))
|
||||
.expect(400);
|
||||
});
|
||||
|
||||
it('отклоняет текст сообщения длиннее 4000 Unicode code points', async () => {
|
||||
const alice = await createUser('alice10c');
|
||||
const bob = await createUser('bob10c');
|
||||
const chat = await createChat(alice, bob);
|
||||
await request(server)
|
||||
.post(`/api/v1/chats/${chat.id}/messages`)
|
||||
.set('Idempotency-Key', randomUUID())
|
||||
.set('Authorization', bearer(alice))
|
||||
.send({ text: 'x'.repeat(4001) })
|
||||
.expect(400);
|
||||
});
|
||||
|
||||
it('принимает текст сообщения ровно из 4000 Unicode code points', async () => {
|
||||
const alice = await createUser('alice10d');
|
||||
const bob = await createUser('bob10d');
|
||||
const chat = await createChat(alice, bob);
|
||||
await request(server)
|
||||
.post(`/api/v1/chats/${chat.id}/messages`)
|
||||
.set('Idempotency-Key', randomUUID())
|
||||
.set('Authorization', bearer(alice))
|
||||
.send({ text: 'x'.repeat(4000) })
|
||||
.expect(201);
|
||||
});
|
||||
|
||||
it('отклоняет текст сообщения свыше 4000 code points, состоящий из эмодзи вне BMP', async () => {
|
||||
const alice = await createUser('alice10e');
|
||||
const bob = await createUser('bob10e');
|
||||
const chat = await createChat(alice, bob);
|
||||
const text = '👋'.repeat(4001);
|
||||
expect([...text].length).toBe(4001);
|
||||
await request(server)
|
||||
.post(`/api/v1/chats/${chat.id}/messages`)
|
||||
.set('Idempotency-Key', randomUUID())
|
||||
.set('Authorization', bearer(alice))
|
||||
.send({ text })
|
||||
.expect(400);
|
||||
});
|
||||
|
||||
it('отклоняет cursor, не являющийся корректным base64url JSON', async () => {
|
||||
const alice = await createUser('alice10f');
|
||||
const bob = await createUser('bob10f');
|
||||
const chat = await createChat(alice, bob);
|
||||
await request(server)
|
||||
.get(`/api/v1/chats/${chat.id}/messages`)
|
||||
.query({ cursor: '!!!not-valid!!!' })
|
||||
.set('Authorization', bearer(alice))
|
||||
.expect(400);
|
||||
});
|
||||
|
||||
it('отклоняет корректно закодированный cursor с id сообщения, которое никогда не существовало в этом чате', async () => {
|
||||
const alice = await createUser('alice10i');
|
||||
const bob = await createUser('bob10i');
|
||||
const chat = await createChat(alice, bob);
|
||||
await request(server)
|
||||
.post(`/api/v1/chats/${chat.id}/messages`)
|
||||
.set('Idempotency-Key', randomUUID())
|
||||
.set('Authorization', bearer(alice))
|
||||
.send({ text: 'only message' })
|
||||
.expect(201);
|
||||
|
||||
const bogusCursor = Buffer.from(
|
||||
JSON.stringify({ chatId: chat.id, id: randomUUID() }),
|
||||
'utf8',
|
||||
).toString('base64url');
|
||||
const res = await request(server)
|
||||
.get(`/api/v1/chats/${chat.id}/messages`)
|
||||
.query({ cursor: bogusCursor })
|
||||
.set('Authorization', bearer(alice))
|
||||
.expect(400);
|
||||
expect(res.body.error.code).toBe('invalid_request');
|
||||
});
|
||||
|
||||
it('возвращает 400 для синтаксически некорректного chat_id при отправке сообщения', async () => {
|
||||
const alice = await createUser('alice10g');
|
||||
const res = await request(server)
|
||||
.post('/api/v1/chats/not-a-uuid/messages')
|
||||
.set('Authorization', bearer(alice))
|
||||
.send({ text: 'hi' })
|
||||
.expect(400);
|
||||
expect(res.body.error.code).toBe('invalid_request');
|
||||
});
|
||||
|
||||
it('возвращает 400 для синтаксически некорректного chat_id при чтении истории', async () => {
|
||||
const alice = await createUser('alice10h');
|
||||
const res = await request(server)
|
||||
.get('/api/v1/chats/not-a-uuid/messages')
|
||||
.set('Authorization', bearer(alice))
|
||||
.expect(400);
|
||||
expect(res.body.error.code).toBe('invalid_request');
|
||||
});
|
||||
|
||||
it('отклоняет cursor, взятый из другого чата', async () => {
|
||||
const alice = await createUser('alice11');
|
||||
const bob = await createUser('bob11');
|
||||
const chatA = await createChat(alice, bob);
|
||||
const chatB = await createChat(alice, bob);
|
||||
|
||||
await request(server)
|
||||
.post(`/api/v1/chats/${chatA.id}/messages`)
|
||||
.set('Idempotency-Key', randomUUID())
|
||||
.set('Authorization', bearer(alice))
|
||||
.send({ text: 'first' })
|
||||
.expect(201);
|
||||
await request(server)
|
||||
.post(`/api/v1/chats/${chatA.id}/messages`)
|
||||
.set('Idempotency-Key', randomUUID())
|
||||
.set('Authorization', bearer(alice))
|
||||
.send({ text: 'second' })
|
||||
.expect(201);
|
||||
|
||||
const page1 = await request(server)
|
||||
.get(`/api/v1/chats/${chatA.id}/messages?limit=1`)
|
||||
.set('Authorization', bearer(alice))
|
||||
.expect(200);
|
||||
expect(page1.body.next_cursor).toBeTruthy();
|
||||
|
||||
const res = await request(server)
|
||||
.get(`/api/v1/chats/${chatB.id}/messages`)
|
||||
.query({ cursor: page1.body.next_cursor })
|
||||
.set('Authorization', bearer(alice))
|
||||
.expect(400);
|
||||
expect(res.body.error.code).toBe('invalid_request');
|
||||
});
|
||||
|
||||
it('обходит всю историю по страницам без пропусков и дублей', async () => {
|
||||
const alice = await createUser('alice12');
|
||||
const bob = await createUser('bob12');
|
||||
const chat = await createChat(alice, bob);
|
||||
const sentTexts = ['m1', 'm2', 'm3', 'm4', 'm5'];
|
||||
for (const text of sentTexts) {
|
||||
await request(server)
|
||||
.post(`/api/v1/chats/${chat.id}/messages`)
|
||||
.set('Idempotency-Key', randomUUID())
|
||||
.set('Authorization', bearer(alice))
|
||||
.send({ text })
|
||||
.expect(201);
|
||||
}
|
||||
|
||||
const seen: string[] = [];
|
||||
let cursor: string | null = null;
|
||||
do {
|
||||
const res: request.Response = await request(server)
|
||||
.get(`/api/v1/chats/${chat.id}/messages`)
|
||||
.query({ limit: 2, ...(cursor ? { cursor } : {}) })
|
||||
.set('Authorization', bearer(alice))
|
||||
.expect(200);
|
||||
seen.push(
|
||||
...res.body.items.map((m: { text: string }) => m.text),
|
||||
);
|
||||
cursor = res.body.next_cursor;
|
||||
} while (cursor);
|
||||
|
||||
expect(seen).toEqual(sentTexts);
|
||||
});
|
||||
});
|
||||
|
||||
describe('X-Request-Id', () => {
|
||||
it('присутствует и уникален для независимых запросов', async () => {
|
||||
const first = await request(server).get('/health/live').expect(200);
|
||||
const second = await request(server)
|
||||
.get('/health/live')
|
||||
.expect(200);
|
||||
expect(first.headers['x-request-id']).toBeTruthy();
|
||||
expect(second.headers['x-request-id']).toBeTruthy();
|
||||
expect(first.headers['x-request-id']).not.toBe(
|
||||
second.headers['x-request-id'],
|
||||
);
|
||||
});
|
||||
|
||||
it('совпадает с request_id внутри тела ошибки', async () => {
|
||||
const res = await request(server)
|
||||
.get('/api/v1/users/me')
|
||||
.expect(401);
|
||||
expect(res.body.error.request_id).toBe(res.headers['x-request-id']);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,287 @@
|
||||
import { INestApplication } from '@nestjs/common';
|
||||
import { createHash, randomUUID } from 'crypto';
|
||||
import request from 'supertest';
|
||||
import { PrismaService } from '../src/db/prisma.service';
|
||||
import { S3Service } from '../src/storage/s3.service';
|
||||
import { bearer, createTestApp, registerUser, TestUser } from './helpers';
|
||||
import { jpeg, SAMPLE_PNG } from './images';
|
||||
|
||||
describe('Скачивание и привязка вложений (e2e)', () => {
|
||||
let app: INestApplication;
|
||||
let server: any;
|
||||
let alice: TestUser;
|
||||
let bob: TestUser;
|
||||
let eve: TestUser;
|
||||
let chatId: string;
|
||||
let otherChatId: string;
|
||||
|
||||
const createChat = async (owner: TestUser, members: TestUser[]) => {
|
||||
const res = await request(server)
|
||||
.post('/api/v1/chats')
|
||||
.set('Authorization', bearer(owner))
|
||||
.send({ title: 'Files', member_ids: members.map((m) => m.id) })
|
||||
.expect(201);
|
||||
return res.body.id as string;
|
||||
};
|
||||
|
||||
const upload = async (
|
||||
user = alice,
|
||||
chat = chatId,
|
||||
body: Buffer = SAMPLE_PNG,
|
||||
type = 'image/png',
|
||||
) => {
|
||||
const res = await request(server)
|
||||
.post(`/api/v1/chats/${chat}/attachments?filename=${encodeURIComponent('фото 1.png')}`)
|
||||
.set('Authorization', bearer(user))
|
||||
.set('Content-Type', type)
|
||||
.send(body)
|
||||
.expect(201);
|
||||
return res.body;
|
||||
};
|
||||
|
||||
const get = (path: string, user: TestUser | null = bob) => {
|
||||
const req = request(server).get(path).buffer(true).parse(binaryParser);
|
||||
return user ? req.set('Authorization', bearer(user)) : req;
|
||||
};
|
||||
|
||||
const send = (
|
||||
body: unknown,
|
||||
{ user = alice, chat = chatId, key = randomUUID() }: { user?: TestUser; chat?: string; key?: string } = {},
|
||||
) =>
|
||||
request(server)
|
||||
.post(`/api/v1/chats/${chat}/messages`)
|
||||
.set('Authorization', bearer(user))
|
||||
.set('Idempotency-Key', key)
|
||||
.send(body as object);
|
||||
|
||||
beforeAll(async () => {
|
||||
({ app, server } = await createTestApp());
|
||||
alice = await registerUser(server, 'acc_alice');
|
||||
bob = await registerUser(server, 'acc_bob');
|
||||
eve = await registerUser(server, 'acc_eve');
|
||||
chatId = await createChat(alice, [alice, bob]);
|
||||
otherChatId = await createChat(eve, [eve]);
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
await app.close();
|
||||
});
|
||||
|
||||
describe('чтение', () => {
|
||||
it('участник получает метаданные и оригинал побайтно', async () => {
|
||||
const attachment = await upload();
|
||||
const meta = await request(server)
|
||||
.get(`/api/v1/attachments/${attachment.id}`)
|
||||
.set('Authorization', bearer(bob))
|
||||
.expect(200);
|
||||
expect(meta.body).toEqual(attachment);
|
||||
const res = await get(`/api/v1/attachments/${attachment.id}/content`).expect(200);
|
||||
expect(res.headers['content-type']).toBe('image/png');
|
||||
expect(res.headers['content-length']).toBe(String(SAMPLE_PNG.length));
|
||||
expect(res.headers['x-content-type-options']).toBe('nosniff');
|
||||
expect(res.headers['x-request-id']).toBeTruthy();
|
||||
expect(res.headers['x-instance-id']).toBeTruthy();
|
||||
expect(res.headers['location']).toBeUndefined();
|
||||
expect(createHash('sha256').update(res.body).digest('hex')).toBe(attachment.sha256);
|
||||
});
|
||||
|
||||
it('JPEG отдаётся с истинным Content-Type', async () => {
|
||||
const body = await jpeg();
|
||||
const attachment = await upload(alice, chatId, body, 'image/jpeg');
|
||||
const res = await get(`/api/v1/attachments/${attachment.id}/content`).expect(200);
|
||||
expect(res.headers['content-type']).toBe('image/jpeg');
|
||||
expect(Buffer.compare(res.body, body)).toBe(0);
|
||||
});
|
||||
|
||||
it('посторонний и неизвестный ID получают одинаковый 404', async () => {
|
||||
const attachment = await upload();
|
||||
for (const suffix of ['', '/content']) {
|
||||
const foreign = await get(`/api/v1/attachments/${attachment.id}${suffix}`, eve);
|
||||
const unknown = await get(`/api/v1/attachments/${randomUUID()}${suffix}`, eve);
|
||||
expect(foreign.status).toBe(404);
|
||||
expect(unknown.status).toBe(404);
|
||||
const parsed = JSON.parse(foreign.body.toString());
|
||||
expect(parsed.error.code).toBe('not_found');
|
||||
expect(parsed.error.message).toBe(JSON.parse(unknown.body.toString()).error.message);
|
||||
}
|
||||
});
|
||||
|
||||
it.each([
|
||||
['nil', '00000000-0000-0000-0000-000000000000'],
|
||||
['v7', '018f6f3a-7b1c-7def-8abc-0123456789ab'],
|
||||
])('UUID %s корректного формата — 404, а не 400', async (_name, id) => {
|
||||
await get(`/api/v1/attachments/${id}`).expect(404);
|
||||
await get(`/api/v1/attachments/${id}/content`).expect(404);
|
||||
await get(`/api/v1/chats/${id}`, alice).expect(404);
|
||||
await send({ text: 'x', attachment_ids: [id] }).expect(404);
|
||||
await send({ text: 'x' }, { chat: id }).expect(404);
|
||||
await request(server)
|
||||
.post(`/api/v1/chats/${id}/attachments?filename=a.png`)
|
||||
.set('Authorization', bearer(alice))
|
||||
.set('Content-Type', 'image/png')
|
||||
.send(SAMPLE_PNG)
|
||||
.expect(404);
|
||||
});
|
||||
|
||||
it('без токена 401, неверный UUID 400', async () => {
|
||||
const attachment = await upload();
|
||||
for (const suffix of ['', '/content']) {
|
||||
await get(`/api/v1/attachments/${attachment.id}${suffix}`, null).expect(401);
|
||||
await get(`/api/v1/attachments/not-a-uuid${suffix}`).expect(400);
|
||||
}
|
||||
});
|
||||
|
||||
it('variant: original явно, отсутствующий 404, неизвестный и повторённый 400', async () => {
|
||||
const { id } = await upload();
|
||||
await get(`/api/v1/attachments/${id}/content?variant=original`).expect(200);
|
||||
await get(`/api/v1/attachments/${id}/content?variant=thumbnail`).expect(404);
|
||||
await get(`/api/v1/attachments/${id}/content?variant=nonsense`).expect(400);
|
||||
await get(`/api/v1/attachments/${id}/content?variant=`).expect(400);
|
||||
await get(`/api/v1/attachments/${id}/content?variant=original&variant=original`).expect(400);
|
||||
await get(`/api/v1/attachments/${id}/content?variant=thumbnail`, eve).expect(404);
|
||||
});
|
||||
|
||||
it('нет доступа к S3 — метаданные 200, content 503 с заголовками', async () => {
|
||||
const attachment = await upload();
|
||||
const { app: down, server: downServer } = await createTestApp({
|
||||
S3_ENDPOINT: 'http://127.0.0.1:9',
|
||||
});
|
||||
try {
|
||||
await request(downServer)
|
||||
.get(`/api/v1/attachments/${attachment.id}`)
|
||||
.set('Authorization', bearer(bob))
|
||||
.expect(200);
|
||||
const res = await request(downServer)
|
||||
.get(`/api/v1/attachments/${attachment.id}/content`)
|
||||
.set('Authorization', bearer(bob))
|
||||
.expect(503);
|
||||
expect(res.body.error.code).toBe('unavailable');
|
||||
expect(res.headers['x-instance-id']).toBeTruthy();
|
||||
} finally {
|
||||
await down.close();
|
||||
}
|
||||
});
|
||||
|
||||
it('объект пропал из S3 — 500 без внутренних деталей', async () => {
|
||||
const attachment = await upload();
|
||||
const row = await app.get(PrismaService).attachment.findUniqueOrThrow({
|
||||
where: { id: attachment.id },
|
||||
});
|
||||
await app.get(S3Service).delete(row.objectKey);
|
||||
const res = await request(server)
|
||||
.get(`/api/v1/attachments/${attachment.id}/content`)
|
||||
.set('Authorization', bearer(bob))
|
||||
.expect(500);
|
||||
expect(res.body.error.code).toBe('internal_error');
|
||||
expect(JSON.stringify(res.body)).not.toContain(row.objectKey);
|
||||
});
|
||||
});
|
||||
|
||||
describe('attachment_ids в сообщениях', () => {
|
||||
it('text="" с вложением принимается, Message читается вторым участником', async () => {
|
||||
const attachment = await upload();
|
||||
const created = await send({ text: '', attachment_ids: [attachment.id] }).expect(201);
|
||||
expect(created.body.attachment_ids).toEqual([attachment.id]);
|
||||
const page = await request(server)
|
||||
.get(`/api/v1/chats/${chatId}/messages?limit=100`)
|
||||
.set('Authorization', bearer(bob))
|
||||
.expect(200);
|
||||
expect(page.body.items).toContainEqual(created.body);
|
||||
});
|
||||
|
||||
it('порядок [B, A] сохраняется в ответе, истории и повторе; перестановка с тем же ключом — 409', async () => {
|
||||
const a = await upload();
|
||||
const b = await upload();
|
||||
const key = randomUUID();
|
||||
const body = { text: 'order', attachment_ids: [b.id, a.id] };
|
||||
const created = await send(body, { key }).expect(201);
|
||||
expect(created.body.attachment_ids).toEqual([b.id, a.id]);
|
||||
const replay = await send(body, { key }).expect(201);
|
||||
expect(replay.body).toEqual(created.body);
|
||||
const page = await request(server)
|
||||
.get(`/api/v1/chats/${chatId}/messages?limit=100`)
|
||||
.set('Authorization', bearer(alice))
|
||||
.expect(200);
|
||||
expect(page.body.items.find((m: any) => m.id === created.body.id)).toEqual(created.body);
|
||||
const conflict = await send({ text: 'order', attachment_ids: [a.id, b.id] }, { key }).expect(409);
|
||||
expect(conflict.body.error.code).toBe('conflict');
|
||||
});
|
||||
|
||||
it('отсутствующий attachment_ids и [] равны при повторе, изменение списка — 409', async () => {
|
||||
const attachment = await upload();
|
||||
const key = randomUUID();
|
||||
const first = await send({ text: 'same' }, { key }).expect(201);
|
||||
expect(first.body.attachment_ids).toEqual([]);
|
||||
const replay = await send({ attachment_ids: [], text: 'same' }, { key }).expect(201);
|
||||
expect(replay.body).toEqual(first.body);
|
||||
await send({ text: 'same', attachment_ids: [attachment.id] }, { key }).expect(409);
|
||||
});
|
||||
|
||||
it('вложение другого чата и неизвестное — одинаковый 404, сообщение не создаётся', async () => {
|
||||
const foreign = await upload(eve, otherChatId);
|
||||
const key = randomUUID();
|
||||
const first = await send({ text: 'x', attachment_ids: [foreign.id] }, { key }).expect(404);
|
||||
const second = await send({ text: 'x', attachment_ids: [randomUUID()] }).expect(404);
|
||||
expect(first.body.error.code).toBe('not_found');
|
||||
expect(first.body.error.message).toBe(second.body.error.message);
|
||||
await send({ text: 'x' }, { key }).expect(201);
|
||||
});
|
||||
|
||||
it('смесь своего и чужого вложения — 404', async () => {
|
||||
const own = await upload();
|
||||
const foreign = await upload(eve, otherChatId);
|
||||
await send({ text: 'x', attachment_ids: [own.id, foreign.id] }).expect(404);
|
||||
});
|
||||
|
||||
it('посторонний не может отправить вложение в чужой чат', async () => {
|
||||
const attachment = await upload();
|
||||
await send({ text: 'x', attachment_ids: [attachment.id] }, { user: eve }).expect(404);
|
||||
});
|
||||
|
||||
it('любой участник может сослаться на вложение чата, вложение можно использовать повторно', async () => {
|
||||
const attachment = await upload();
|
||||
await send({ text: 'first', attachment_ids: [attachment.id] }).expect(201);
|
||||
const res = await send({ text: 'second', attachment_ids: [attachment.id] }, { user: bob }).expect(201);
|
||||
expect(res.body.sender_id).toBe(bob.id);
|
||||
expect(res.body.attachment_ids).toEqual([attachment.id]);
|
||||
});
|
||||
|
||||
it('ровно 10 вложений принимается, 11 — 400', async () => {
|
||||
const attachment = await upload();
|
||||
const links = await Promise.all(Array.from({ length: 9 }, () => upload()));
|
||||
const ten = [attachment.id, ...links.map((l) => l.id)];
|
||||
const created = await send({ text: 'ten', attachment_ids: ten }).expect(201);
|
||||
expect(created.body.attachment_ids).toEqual(ten);
|
||||
await send({ text: 'eleven', attachment_ids: [...ten, randomUUID()] }).expect(400);
|
||||
});
|
||||
|
||||
it('UUID в другом регистре — тот же ID и дубль', async () => {
|
||||
const attachment = await upload();
|
||||
const upper = attachment.id.toUpperCase();
|
||||
const created = await send({ text: 'case', attachment_ids: [upper] }).expect(201);
|
||||
expect(created.body.attachment_ids).toEqual([attachment.id]);
|
||||
await send({ text: 'dup', attachment_ids: [attachment.id, upper] }).expect(400);
|
||||
});
|
||||
|
||||
it.each([
|
||||
['text отсутствует', (id: string) => ({ attachment_ids: [id] })],
|
||||
['text null', (id: string) => ({ text: null, attachment_ids: [id] })],
|
||||
['attachment_ids null', () => ({ text: 'x', attachment_ids: null })],
|
||||
['attachment_ids строка', (id: string) => ({ text: 'x', attachment_ids: id })],
|
||||
['элемент не UUID', () => ({ text: 'x', attachment_ids: ['nope'] })],
|
||||
['элемент число', () => ({ text: 'x', attachment_ids: [1] })],
|
||||
['дубликат', (id: string) => ({ text: 'x', attachment_ids: [id, id] })],
|
||||
['пустой текст без вложений', () => ({ text: '', attachment_ids: [] })],
|
||||
['пробельный текст без вложений', () => ({ text: ' ' })],
|
||||
])('%s — 400', async (_name, build) => {
|
||||
const attachment = await upload();
|
||||
await send(build(attachment.id)).expect(400);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
function binaryParser(res: any, done: (error: Error | null, body: Buffer) => void): void {
|
||||
const chunks: Buffer[] = [];
|
||||
res.on('data', (chunk: Buffer) => chunks.push(chunk));
|
||||
res.on('end', () => done(null, Buffer.concat(chunks)));
|
||||
}
|
||||
@@ -0,0 +1,376 @@
|
||||
import { INestApplication } from '@nestjs/common';
|
||||
import { Prisma } from '@prisma/client';
|
||||
import Ajv2020 from 'ajv/dist/2020';
|
||||
import { createHash, randomUUID } from 'crypto';
|
||||
import { readdirSync } from 'fs';
|
||||
import { connect } from 'net';
|
||||
import { tmpdir } from 'os';
|
||||
import request from 'supertest';
|
||||
import { PrismaService } from '../src/db/prisma.service';
|
||||
import { loadDocument } from '../src/docs/swagger';
|
||||
import { S3Service } from '../src/storage/s3.service';
|
||||
import { bearer, createTestApp, registerUser, TestUser } from './helpers';
|
||||
import {
|
||||
corruptedJpegs,
|
||||
corruptedPngs,
|
||||
jpeg,
|
||||
png,
|
||||
pngHeaderOnly,
|
||||
pngOfExactSize,
|
||||
SAMPLE_PNG,
|
||||
} from './images';
|
||||
|
||||
const MAX_BYTES = 10 * 1024 * 1024;
|
||||
|
||||
describe('Загрузка вложений (e2e)', () => {
|
||||
let app: INestApplication;
|
||||
let server: any;
|
||||
let alice: TestUser;
|
||||
let eve: TestUser;
|
||||
let chatId: string;
|
||||
|
||||
const upload = (
|
||||
body: Buffer | undefined,
|
||||
{
|
||||
type = 'image/png',
|
||||
filename = 'sample.png',
|
||||
user = alice,
|
||||
chat = chatId,
|
||||
}: {
|
||||
type?: string | null;
|
||||
filename?: string | null;
|
||||
user?: TestUser | null;
|
||||
chat?: string;
|
||||
} = {},
|
||||
) => {
|
||||
const query = filename === null ? '' : `?filename=${encodeURIComponent(filename)}`;
|
||||
const req = request(server).post(`/api/v1/chats/${chat}/attachments${query}`);
|
||||
if (user) {
|
||||
req.set('Authorization', bearer(user));
|
||||
}
|
||||
if (type) {
|
||||
req.set('Content-Type', type);
|
||||
}
|
||||
return body ? req.send(body) : req;
|
||||
};
|
||||
|
||||
const expectError = (res: request.Response, status: number, code: string) => {
|
||||
expect({ status: res.status, code: res.body.error?.code }).toEqual({ status, code });
|
||||
expect(res.headers['x-request-id']).toBeTruthy();
|
||||
expect(res.headers['x-instance-id']).toBeTruthy();
|
||||
expect(res.body.error.request_id).toBe(res.headers['x-request-id']);
|
||||
};
|
||||
|
||||
beforeAll(async () => {
|
||||
({ app, server } = await createTestApp());
|
||||
alice = await registerUser(server, 'att_alice');
|
||||
eve = await registerUser(server, 'att_eve');
|
||||
const chat = await request(server)
|
||||
.post('/api/v1/chats')
|
||||
.set('Authorization', bearer(alice))
|
||||
.send({ title: 'Files', member_ids: [alice.id] })
|
||||
.expect(201);
|
||||
chatId = chat.body.id;
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
await app.close();
|
||||
});
|
||||
|
||||
describe('успешная загрузка', () => {
|
||||
it('возвращает 201 Attachment по контракту и сохраняет метаданные', async () => {
|
||||
const res = await upload(SAMPLE_PNG).expect(201);
|
||||
const document: any = loadDocument();
|
||||
const ajv = new Ajv2020({ strict: false, validateFormats: false });
|
||||
ajv.addSchema(document, 'contract');
|
||||
const validate = ajv.compile({ $ref: 'contract#/components/schemas/Attachment' });
|
||||
expect({ valid: validate(res.body), errors: validate.errors }).toEqual({
|
||||
valid: true,
|
||||
errors: null,
|
||||
});
|
||||
expect(res.body).toMatchObject({
|
||||
chat_id: chatId,
|
||||
owner_id: alice.id,
|
||||
filename: 'sample.png',
|
||||
content_type: 'image/png',
|
||||
size_bytes: SAMPLE_PNG.length,
|
||||
sha256: createHash('sha256').update(SAMPLE_PNG).digest('hex'),
|
||||
status: 'ready',
|
||||
variants: [],
|
||||
error_code: null,
|
||||
});
|
||||
const row = await app.get(PrismaService).attachment.findUnique({
|
||||
where: { id: res.body.id },
|
||||
});
|
||||
expect(row?.objectKey).toBe(`attachments/${res.body.id}`);
|
||||
expect(JSON.stringify(res.body)).not.toContain('attachments/');
|
||||
});
|
||||
|
||||
it('принимает реальный JPEG', async () => {
|
||||
const res = await upload(await jpeg(), {
|
||||
type: 'image/jpeg',
|
||||
filename: 'фото 1.jpg',
|
||||
}).expect(201);
|
||||
expect(res.body).toMatchObject({
|
||||
content_type: 'image/jpeg',
|
||||
filename: 'фото 1.jpg',
|
||||
});
|
||||
});
|
||||
|
||||
it('игнорирует параметры и регистр Content-Type', async () => {
|
||||
await upload(SAMPLE_PNG, { type: 'IMAGE/PNG; charset=binary' }).expect(201);
|
||||
});
|
||||
|
||||
it('принимает файл ровно 10 MiB, filename ровно 255 символов и PNG с байтами после IEND', async () => {
|
||||
const exact = pngOfExactSize(MAX_BYTES);
|
||||
expect(exact.length).toBe(MAX_BYTES);
|
||||
const res = await upload(exact).expect(201);
|
||||
expect(res.body.size_bytes).toBe(MAX_BYTES);
|
||||
await upload(SAMPLE_PNG, { filename: `${'ф'.repeat(251)}.png` }).expect(201);
|
||||
await upload(Buffer.concat([png(4, 4), Buffer.from('tail')])).expect(201);
|
||||
});
|
||||
|
||||
it('принимает ровно 40 млн пикселей', async () => {
|
||||
const res = await upload(await bigPng(8000, 5000));
|
||||
expect(res.status).toBe(201);
|
||||
});
|
||||
});
|
||||
|
||||
describe('порядок проверок и доступ', () => {
|
||||
it('без токена — 401 раньше любых проверок содержимого', async () => {
|
||||
const cases: [string | null, string | null][] = [
|
||||
['image/png', 'a.png'],
|
||||
['text/plain', 'a.png'],
|
||||
['application/json', 'a.png'],
|
||||
['image/png', '../a.png'],
|
||||
[null, null],
|
||||
];
|
||||
for (const [type, filename] of cases) {
|
||||
const res = await upload(Buffer.from('{bad'), { type, filename, user: null });
|
||||
expectError(res, 401, 'unauthorized');
|
||||
expect(res.headers['www-authenticate']).toBe('Bearer realm="messenger"');
|
||||
}
|
||||
});
|
||||
|
||||
it('посторонний и несуществующий чат — одинаковый 404, чужой файл не сохраняется', async () => {
|
||||
const prisma = app.get(PrismaService);
|
||||
const before = await prisma.attachment.count();
|
||||
expectError(await upload(SAMPLE_PNG, { user: eve }), 404, 'not_found');
|
||||
expectError(await upload(SAMPLE_PNG, { chat: randomUUID() }), 404, 'not_found');
|
||||
expect(await prisma.attachment.count()).toBe(before);
|
||||
});
|
||||
|
||||
it('неверный UUID чата — 400', async () => {
|
||||
expectError(await upload(SAMPLE_PNG, { chat: 'not-a-uuid' }), 400, 'invalid_request');
|
||||
});
|
||||
});
|
||||
|
||||
describe('filename', () => {
|
||||
const invalid = {
|
||||
'отсутствует': null,
|
||||
'пустой': '',
|
||||
'только пробелы': ' ',
|
||||
'256 символов': 'x'.repeat(256),
|
||||
'путь через /': '../x.png',
|
||||
'путь через \\': 'a\\b.png',
|
||||
'NUL': 'a\u0000b.png',
|
||||
'перевод строки': 'a\nb.png',
|
||||
'DEL': 'bad\u007f.png',
|
||||
'C1 U+0085': 'c1\u0085.png',
|
||||
'разделитель строк U+2028': 'a
b.png',
|
||||
};
|
||||
it.each(Object.entries(invalid))('%s — 400', async (_name, filename) => {
|
||||
expectError(await upload(SAMPLE_PNG, { filename }), 400, 'invalid_request');
|
||||
});
|
||||
|
||||
it('повторённый параметр — 400', async () => {
|
||||
const res = await request(server)
|
||||
.post(`/api/v1/chats/${chatId}/attachments?filename=a.png&filename=b.png`)
|
||||
.set('Authorization', bearer(alice))
|
||||
.set('Content-Type', 'image/png')
|
||||
.send(SAMPLE_PNG);
|
||||
expectError(res, 400, 'invalid_request');
|
||||
});
|
||||
});
|
||||
|
||||
describe('тип содержимого', () => {
|
||||
it.each(['text/plain', 'application/json', 'application/octet-stream', 'multipart/form-data; boundary=x', 'image/gif', 'image/webp'])(
|
||||
'Content-Type %s — 415',
|
||||
async (type) => {
|
||||
expectError(await upload(SAMPLE_PNG, { type }), 415, 'unsupported_media_type');
|
||||
},
|
||||
);
|
||||
|
||||
it('без Content-Type — 415', async () => {
|
||||
expectError(await upload(SAMPLE_PNG, { type: null }), 415, 'unsupported_media_type');
|
||||
});
|
||||
|
||||
it('подмена MIME в обе стороны и чужие форматы — 415', async () => {
|
||||
const realJpeg = await jpeg();
|
||||
expectError(await upload(SAMPLE_PNG, { type: 'image/jpeg' }), 415, 'unsupported_media_type');
|
||||
expectError(await upload(realJpeg, { type: 'image/png' }), 415, 'unsupported_media_type');
|
||||
for (const body of [
|
||||
Buffer.from('просто текст'),
|
||||
Buffer.from('GIF89a\u0001\u0000\u0001\u0000'),
|
||||
Buffer.from('<svg xmlns="http://www.w3.org/2000/svg" width="1" height="1"/>'),
|
||||
]) {
|
||||
expectError(await upload(body), 415, 'unsupported_media_type');
|
||||
}
|
||||
});
|
||||
|
||||
it('пустое тело — 400', async () => {
|
||||
expectError(await upload(undefined), 400, 'invalid_request');
|
||||
expectError(await upload(Buffer.alloc(0)), 400, 'invalid_request');
|
||||
});
|
||||
});
|
||||
|
||||
describe('повреждённые изображения', () => {
|
||||
it.each(Object.entries(corruptedPngs()))('PNG: %s — 415', async (_name, body) => {
|
||||
expectError(await upload(body), 415, 'unsupported_media_type');
|
||||
});
|
||||
|
||||
it('JPEG: все повреждения — 415', async () => {
|
||||
for (const body of Object.values(corruptedJpegs(await jpeg()))) {
|
||||
expectError(await upload(body, { type: 'image/jpeg' }), 415, 'unsupported_media_type');
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe('лимиты', () => {
|
||||
it('10 MiB + 1 байт — 413 и без чтения объявленного тела', async () => {
|
||||
expectError(await upload(pngOfExactSize(MAX_BYTES + 1)), 413, 'payload_too_large');
|
||||
});
|
||||
|
||||
it('превышение при chunked без Content-Length — 413', async () => {
|
||||
const res = await new Promise<request.Response>((resolve, reject) => {
|
||||
const req = upload(undefined);
|
||||
req.set('Transfer-Encoding', 'chunked');
|
||||
req.write(pngOfExactSize(MAX_BYTES));
|
||||
req.write(Buffer.alloc(1024));
|
||||
req.end((error, response) => (response ? resolve(response) : reject(error)));
|
||||
});
|
||||
expectError(res, 413, 'payload_too_large');
|
||||
});
|
||||
|
||||
it('превышение лимита пикселей — 413 до декодирования', async () => {
|
||||
for (const body of [pngHeaderOnly(8001, 5000), pngHeaderOnly(20000, 10000)]) {
|
||||
expectError(await upload(body), 413, 'payload_too_large');
|
||||
}
|
||||
});
|
||||
|
||||
it('оборванная загрузка не оставляет временных файлов и строк в БД', async () => {
|
||||
const prisma = app.get(PrismaService);
|
||||
const before = await prisma.attachment.count();
|
||||
const { port } = server.address();
|
||||
await new Promise<void>((resolve, reject) => {
|
||||
const socket = connect(port, '127.0.0.1', () => {
|
||||
socket.write(
|
||||
`POST /api/v1/chats/${chatId}/attachments?filename=a.png HTTP/1.1\r\n` +
|
||||
`Host: test\r\nAuthorization: ${bearer(alice)}\r\n` +
|
||||
`Content-Type: image/png\r\nContent-Length: ${SAMPLE_PNG.length}\r\n\r\n`,
|
||||
);
|
||||
socket.write(SAMPLE_PNG.subarray(0, 1000));
|
||||
setTimeout(() => {
|
||||
socket.destroy();
|
||||
resolve();
|
||||
}, 300);
|
||||
});
|
||||
socket.on('error', reject);
|
||||
});
|
||||
await new Promise((resolve) => setTimeout(resolve, 500));
|
||||
expect(await prisma.attachment.count()).toBe(before);
|
||||
expect(activeTempDirs()).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('отказы', () => {
|
||||
it('S3 недоступен — 503 с заголовками, метаданные не создаются', async () => {
|
||||
const { app: down, server: downServer } = await createTestApp({
|
||||
S3_ENDPOINT: 'http://127.0.0.1:9',
|
||||
});
|
||||
try {
|
||||
const prisma = app.get(PrismaService);
|
||||
const before = await prisma.attachment.count();
|
||||
const started = Date.now();
|
||||
const res = await request(downServer)
|
||||
.post(`/api/v1/chats/${chatId}/attachments?filename=a.png`)
|
||||
.set('Authorization', bearer(alice))
|
||||
.set('Content-Type', 'image/png')
|
||||
.send(SAMPLE_PNG);
|
||||
expectError(res, 503, 'unavailable');
|
||||
expect(Date.now() - started).toBeLessThan(3000);
|
||||
expect(await prisma.attachment.count()).toBe(before);
|
||||
} finally {
|
||||
await down.close();
|
||||
}
|
||||
});
|
||||
|
||||
it('явный отказ INSERT после PUT удаляет объект', async () => {
|
||||
const prisma = app.get(PrismaService);
|
||||
const s3 = app.get(S3Service);
|
||||
const remove = jest.spyOn(s3, 'delete');
|
||||
const create = jest.spyOn(prisma.attachment, 'create').mockRejectedValueOnce(
|
||||
new Prisma.PrismaClientKnownRequestError('rejected', {
|
||||
code: 'P2003',
|
||||
clientVersion: 'test',
|
||||
}),
|
||||
);
|
||||
try {
|
||||
const res = await upload(SAMPLE_PNG);
|
||||
expect(res.status).toBe(500);
|
||||
expect(remove).toHaveBeenCalledTimes(1);
|
||||
} finally {
|
||||
create.mockRestore();
|
||||
remove.mockRestore();
|
||||
}
|
||||
});
|
||||
|
||||
it('необработанная ошибка PostgreSQL при INSERT — 503, объект не удаляется', async () => {
|
||||
const prisma = app.get(PrismaService);
|
||||
const s3 = app.get(S3Service);
|
||||
const remove = jest.spyOn(s3, 'delete');
|
||||
const create = jest.spyOn(prisma.attachment, 'create').mockRejectedValueOnce(
|
||||
new Prisma.PrismaClientUnknownRequestError('server closed the connection', {
|
||||
clientVersion: 'test',
|
||||
}),
|
||||
);
|
||||
try {
|
||||
expectError(await upload(SAMPLE_PNG), 503, 'unavailable');
|
||||
expect(remove).not.toHaveBeenCalled();
|
||||
} finally {
|
||||
create.mockRestore();
|
||||
remove.mockRestore();
|
||||
}
|
||||
});
|
||||
|
||||
it('при неясном исходе COMMIT объект не удаляется', async () => {
|
||||
const prisma = app.get(PrismaService);
|
||||
const s3 = app.get(S3Service);
|
||||
const remove = jest.spyOn(s3, 'delete');
|
||||
const create = jest.spyOn(prisma.attachment, 'create').mockRejectedValueOnce(
|
||||
new Prisma.PrismaClientKnownRequestError('connection lost', {
|
||||
code: 'P1017',
|
||||
clientVersion: 'test',
|
||||
}),
|
||||
);
|
||||
try {
|
||||
expectError(await upload(SAMPLE_PNG), 503, 'unavailable');
|
||||
expect(remove).not.toHaveBeenCalled();
|
||||
} finally {
|
||||
create.mockRestore();
|
||||
remove.mockRestore();
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
function activeTempDirs(): string[] {
|
||||
return readdirSync(tmpdir()).filter((name) => name.startsWith('messenger-upload-'));
|
||||
}
|
||||
|
||||
async function bigPng(width: number, height: number): Promise<Buffer> {
|
||||
const { default: sharp } = await import('sharp');
|
||||
return sharp({ create: { width, height, channels: 3, background: '#336699' } })
|
||||
.png({ compressionLevel: 9 })
|
||||
.toBuffer();
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user