diff --git a/.dockerignore b/.dockerignore index bc16e556..28532a6d 100644 --- a/.dockerignore +++ b/.dockerignore @@ -18,3 +18,23 @@ __pycache__ node_modules evidence .DS_Store +.gradle +.kotlin +build +.idea +*.iml +tests +scripts +examples +run +COURSE.md +CONTRIBUTING.md +MAINTAINERS.md +REPORT.md +README.md +requirements-dev.txt +Dockerfile.example +compose.example.yaml +Makefile + +archive diff --git a/.env.example b/.env.example index d99d26a2..1a29d8a9 100644 --- a/.env.example +++ b/.env.example @@ -14,7 +14,7 @@ IDEMPOTENCY_TTL_SECONDS=86400 S3_ENDPOINT=http://s3:9000 S3_REGION=us-east-1 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 +S3_ACCESS_KEY_ID_FILE=/run/secrets/s3_api_access_key_id +S3_SECRET_ACCESS_KEY_FILE=/run/secrets/s3_api_secret_access_key MAX_UPLOAD_BYTES=10485760 MAX_IMAGE_PIXELS=40000000 diff --git a/.gitignore b/.gitignore index b5a71896..6137e904 100644 --- a/.gitignore +++ b/.gitignore @@ -28,5 +28,9 @@ coverage/ .DS_Store .idea/ .vscode/ +.gradle/ +.kotlin/ +!gradle/wrapper/ +/run/development/ # Keep small, sanitized evidence summaries in evidence/ under version control. # Inspect git diff --cached: ignore rules are not a secret scanner. diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..2f60fd5f --- /dev/null +++ b/Dockerfile @@ -0,0 +1,23 @@ +FROM gradle:8.14-jdk21 AS build +WORKDIR /src +COPY settings.gradle.kts build.gradle.kts gradle.properties ./ +COPY gradle ./gradle +COPY gradlew ./ +COPY src ./src +COPY contracts ./contracts +RUN --mount=type=cache,target=/home/gradle/.gradle gradle --no-daemon shadowJar + +FROM eclipse-temurin:21-jre-alpine +# Distinguishes builds of the same code, e.g. the image rolled out by scripts/rolling-update.sh. +ARG BUILD_ID=dev +LABEL org.opencontainers.image.revision=$BUILD_ID +RUN apk add --no-cache wget && addgroup -S app && adduser -S -G app app +WORKDIR /app +COPY --from=build /src/build/libs/messenger.jar /app/messenger.jar +USER app +ENV APP_HOST=0.0.0.0 APP_PORT=8080 +EXPOSE 8080 +# Docker's own view of the replica (compose depends_on, rolling update). The balancer runs its own checks every 1 s. +HEALTHCHECK --interval=10s --timeout=3s --start-period=30s --start-interval=1s --retries=3 \ + CMD wget -qO- http://127.0.0.1:8080/health/ready || exit 1 +ENTRYPOINT ["java","-XX:MaxRAMPercentage=75.0","-jar","/app/messenger.jar"] diff --git a/REPORT.md b/REPORT.md index 9ad2b84f..e8e94daa 100644 --- a/REPORT.md +++ b/REPORT.md @@ -1,37 +1,134 @@ # Отчёт — лабораторная 4 -- Автор, группа: TODO -- Целевая оценка; выбранный трек 5, если нужен: TODO -- Commit/PR: TODO после публикации -- Использованные источники, библиотеки и помощь ИИ: TODO +- Автор, группа: Кучерявенко Максим, БВТ2303 +- Целевая оценка: **4** +- Commit/PR: после публикации. +- Основа: собственный lab3; структура конфигураций, controllers → services → repositories. Для S3 используется AWS SDK for Kotlin 1.6.41; изображения проверяет JDK ImageIO. ИИ использован как парный разработчик. +- Источники: [AWS SDK for Kotlin](https://github.com/awslabs/aws-sdk-kotlin), [релиз MinIO](https://github.com/minio/minio/releases/tag/RELEASE.2025-09-07T16-13-09Z), [ImageReader](https://docs.oracle.com/en/java/javase/21/docs/api/java.desktop/javax/imageio/ImageReader.html), [S3 uploads](https://docs.aws.amazon.com/AmazonS3/latest/userguide/upload-objects.html). ## Запуск из чистого clone -TODO: версии runtime/образов, prerequisites, команды создания локальных secrets/.env, build, миграций, запуска, ожидания готовности, тестов и остановки. Указать необходимые действия оператора. Не вставлять секреты. Отдельно указать безопасную остановку и команду намеренного удаления учебных данных. +Нужны Docker с Compose v2, Python 3.9+, openssl. Для JVM-тестов нужны JDK 21+ и Docker (Testcontainers самостоятельно поднимает PostgreSQL, Redis и MinIO). + +```sh +./scripts/init-secrets.sh + +# Если 8080 занят lab3, как при проверке этой работы: +export LAB_HTTP_PORT=8084 +export BASE_URL=http://localhost:8084 +# Для свободного стандартного порта можно использовать 8080 в обеих переменных. + +docker compose config --quiet +docker compose up -d --build --wait +make check +make test +./gradlew test + +# Остановка именно реплики, принявшей upload; скрипт сам возвращает её в работу. +python3 scripts/attachment-failover.py + +# Проверка lab3: запись сообщений во время отказа, идемпотентность, чтение файла. +make test-failover ACTION_SCRIPT=scripts/stop-one.sh +docker compose start api + +# Перезапуск LB, обеих API-реплик, PostgreSQL, Redis и MinIO без удаления volumes. +make test-persistence ACTION_SCRIPT=scripts/restart.sh + +# Проверки уровня 4: +scripts/check-s3-permissions.sh +python3 scripts/attachment-interrupt.py +python3 scripts/attachment-load.py > evidence/attachment-load.json + +# Безопасная остановка, данные сохраняются: +docker compose down +# Только для намеренного удаления всех учебных данных: +# docker compose down -v +``` + +Compose собирает общий образ `mtusi-messenger:lab4` через `api`; одноразовый `migrate` использует тот же образ и применяет Flyway V1–V5 до старта реплик. `s3-init` ждёт MinIO, создаёт bucket через `mc mb --ignore-existing` и закрывает анонимный доступ через `mc anonymous set none`. Повторный запуск сохраняет bucket и объекты. Секреты создаются локально, существующие файлы не перезаписываются; `.env`, `secrets/` и `run/development/` не входят в Git. + +MinIO RELEASE.2025-09-07T16-13-09Z и mc RELEASE.2025-08-13T08-35-41Z закреплены digest в Compose. PostgreSQL 17.6, Redis 8.4.0, HAProxy 3.2.4 — из lab3. API использует Kotlin 2.3.20, Ktor 3.3.0, JDK 21. Проверено на macOS arm64 с Docker 28.4.0. MinIO S3 и console не публикуются на host; наружу доступен только LB на loopback. + +### Настройки + +`.env` необязателен. В Compose есть значения по умолчанию; `docker compose --env-file .env.example config --quiet` также проходит. Переменные из skeleton сопоставлены так: + +- `API_REPLICAS`, `SESSION_TTL_SECONDS`, `IDEMPOTENCY_TTL_SECONDS`, `S3_BUCKET` используются в Compose. Дополнительно `LAB_HTTP_PORT` задаёт опубликованный порт LB. +- `S3_ENDPOINT=http://s3:9000`, `S3_ACCESS_KEY_ID_FILE`, `S3_SECRET_ACCESS_KEY_FILE` явно передаются API в Compose; в локальном JVM-процессе читаются из environment или HOCON `storage`. +- `S3_REGION=us-east-1` закреплён в SDK. `MAX_UPLOAD_BYTES=10485760` и `MAX_IMAGE_PIXELS=40000000` закреплены в валидаторе по обязательному контракту; переменными окружения не увеличиваются. +- Вместо предложенных skeleton `DATABASE_URL_FILE` и `CACHE_URL_FILE` используются `DB_HOST/PORT/NAME/USER/PASSWORD_FILE` и `REDIS_HOST/PORT/PASSWORD_FILE`. PostgreSQL всегда источник истины для сессий; `SESSION_STORE` не переключает реализацию. `APP_ENV` не используется. +- `APP_HOST/PORT` задаются внутри Compose, порт приложения — 8080. `LAB_HTTP_PORT` меняет только внешний порт. + +Для отдельного `./gradlew run` задача создаёт недостающие файлы `run/development/config` из `_template`, сохраняя существующие настройки. Нужно настроить доступные локальному JVM PostgreSQL, Redis и S3; изолированные адреса Compose с host не доступны. Полностью воспроизводимый путь проверки выше использует Compose. ## Архитектура и решения -TODO: схема процессов/хранилищ, источник истины, транзакционные границы, ограничения. Что перенесено из lab3, что изменено в этой работе. Соответствие переменных из .env.example реальной конфигурации. +Клиент → HAProxy → две Ktor API-реплики → PostgreSQL (метаданные, связи, сессии) и MinIO (байты). Redis остаётся кэшем сессий с fallback в PostgreSQL. API-реплики взаимозаменяемы, sticky sessions нет. + +`attachments` содержит ID, chat_id, owner_id, отображаемое имя, MIME, размер, SHA-256 и время создания. `message_attachments` хранит порядок вложений сообщения, уникальность и внешние ключи. S3 key — `originals/`; filename никогда не используется как путь. В API нет ключей S3 и credentials. В lab4 все опубликованные метаданные уже ready, updated_at равен created_at, variants=[], error_code=null. + +Upload: Bearer-аутентификация → проверка членства → filename/MIME/размер → ограниченное чтение тела → проверка изображения → COMMIT записи журнала → S3 PUT → INSERT метаданных и удаление записи журнала в одной транзакции → 201. Нет долгой DB-транзакции во время HTTP upload или S3 PUT. Оригинальные bytes не перекодируются. Download: аутентификация → доступ через chat_id в PostgreSQL → S3 GET → bytes с истинным MIME через API без redirect. Посторонний и неизвестный ID получают одинаковый 404. + +Сообщение допускает пустой текст с 1–10 уникальными вложениями этого чата. Член чата может прикрепить загруженный другим участником файл. Создание сообщения, связей и результата Idempotency-Key выполняется в одной транзакции. Отсутствующий attachment_ids и [] равнозначны; повтор возвращает тот же Message. Hash дополнен вложениями, при replay дополнительно сравниваются исходный текст и список ID. Старые текстовые ключи lab3 остаются пригодны для replay. Сохраняются keyset-пагинация, отзыв сессий между репликами и graceful shutdown. Вложения всей страницы выбираются одним дополнительным DSL-запросом и группируются по message_id; тест страницы из 100 сообщений проверяет ровно два SQL-запроса и сохранение порядка вложений. + +### Ограничения входных данных + +- Тело читается из канала порциями до 8192 bytes, с проверкой лимита до записи во временный файл. Превышение Content-Length отсекается сразу; при отсутствии длины применяется тот же счётчик bytes. Максимум — 10 MiB, превышение — 413; приём тела ограничен 15 секундами. +- ImageIO определяет реальный формат, допускаются PNG/JPEG и совпадение MIME. Размеры считываются из заголовка до `reader.read`; произведение считается в Long, более 40 млн пикселей — 413. +- Затем выполняется полное декодирование, чтобы одного корректного заголовка было недостаточно. Ошибки/предупреждения декодера — 415. Для PNG дополнительно проверяются границы chunks, CRC и IEND, для JPEG — завершающий EOI. ImageIO использует `ignoreMetadata=true`: сжатые zTXt/iTXt не накапливаются. Регрессионный тест покрывает 5000 текстовых chunks. +- Filename: 1–255 Unicode code points, не только пробелы; `/`, `\`, NUL и управляющие символы — 400. +- Приём HTTP и S3 PUT потоковые: тело поступает в уникальный временный файл, AWS SDK читает его через `asByteStream()`. `withTemporaryUpload` создаёт файл и выполняет весь жизненный цикл внутри `Dispatchers.IO`; `finally` удаляет файл при успехе, ошибке и отмене, без потери ресурса при переключении контекста. В Compose `/tmp` — tmpfs 32 MiB, очищаемый при restart; это не постоянное хранилище оригиналов. Семафор допускает одну загрузку на реплику, остальные ждут без собственного буфера файла. Валидация всё ещё использует ограниченный массив ≤10 MiB и raster ≤40 млн пикселей; это честная граница потоковой обработки, а не обещание zero-copy. API ограничен 1 GiB и 1 CPU. У download также есть предел 10 MiB. +- S3-вызов ограничен coroutine timeout в 8 с, включая получение тела ответа. HAProxy server timeout увеличен до 30 с для синхронной загрузки. Автоматическое повторение POST после передачи запроса не включается. ## Доказательства критериев -| Критерий README | Команда/сценарий | Наблюдаемый результат | Файл доказательства | +Проверки выполнены 05.11.2026, через `http://localhost:8084`. Ниже актуальные результаты lab4; материалы в `archive/lab3` исторические. + +| Критерий | Команда/сценарий | Наблюдение | Доказательство | | --- | --- | --- | --- | -| Публичный smoke | make test | TODO | TODO | -| Остальные пункты уровня 3 | TODO | TODO | TODO | -| Все пункты уровня 4, если заявлен | TODO | TODO | TODO | -| Выбранный трек 5, если заявлен | TODO | TODO | TODO | +| Контракт, 201 ready, checksum, сообщение с вложением, ACL | `make test` | PASS публичного сценария lab4 | [smoke.txt](evidence/smoke.txt) | +| Возможности lab3 и новые проверки | `./gradlew test` | 80 тестов, 0 failures/errors/skipped | [gradle-tests.txt](evidence/gradle-tests.txt) | +| PNG/JPEG, MIME, опасное имя, лимиты bytes/pixels до декодирования | AttachmentControllerTest, ImageValidatorTest | Оба формата скачиваются побайтно; 400/413/415 на неверный ввод | [gradle-tests.txt](evidence/gradle-tests.txt) | +| Приватное S3, чужой чат, отказ именно загрузившей реплики | `python3 scripts/attachment-failover.py` | 403 для anonymous S3 GET известного объекта; 404 постороннему; иной X-Instance-Id и те же bytes после отказа | [attachment-failover.txt](evidence/attachment-failover.txt) | +| Сохранность после restart всех компонентов | `make test-persistence ACTION_SCRIPT=scripts/restart.sh` | PASS, прежняя сессия, метаданные и оригинал доступны; StartedAt всех процессов изменился | [persistence.txt](evidence/persistence.txt) | +| Запись сообщений во время отказа API | `make test-failover ACTION_SCRIPT=scripts/stop-one.sh` | Проверка восстановления записи, отсутствия дублей и сохранности данных | [failover.txt](evidence/failover.txt) | +| Ошибочный Content-Type и пробельное имя | AttachmentControllerTest | 400 вместо 500/201 | [gradle-tests.txt](evidence/gradle-tests.txt) | +| Недоступность S3 | AttachmentControllerTest: pause MinIO | Upload/content/ready → 503, live → 200; после unpause исходные bytes доступны | [gradle-tests.txt](evidence/gradle-tests.txt) | +| Негативные запросы уровня 4 | AttachmentControllerTest | 413 для большого файла, 415 для повреждения/MIME, 404 для чужого ID и другого чата | [gradle-tests.txt](evidence/gradle-tests.txt) | +| Сбой между S3 и БД, grace и повторная очистка | AttachmentCleanupTest | Свежие/готовые объекты сохранены, orphan и поздний PUT удалены, публикация abandoned запрещена | [gradle-tests.txt](evidence/gradle-tests.txt) | +| Обрыв клиента | `python3 scripts/attachment-interrupt.py` | Нет опубликованных метаданных и временных файлов после закрытия соединения | [attachment-interrupt.txt](evidence/attachment-interrupt.txt) | +| 10 одновременных загрузок, память | `python3 scripts/attachment-load.py` | 10 успешных upload/download, sampled peak ниже лимита реплик | [attachment-load.json](evidence/attachment-load.json) | +| Отдельные права и повторяемый bootstrap | `scripts/check-s3-permissions.sh` | Запрещённые операции отклонены, console недоступна из общей сети | [s3-permissions.txt](evidence/s3-permissions.txt) | -TODO: добавить отдельную строку для каждого заявленного критерия. PASS без команды и наблюдения не является доказательством. Проверка инфраструктурного hook требует подтверждения, какие реальные процессы/хранилища перезапущены. +## Согласованность и очистка уровня 4 -## Эксперименты и отказы +Миграция V5 добавляет `attachment_uploads`. Перед S3 PUT в PostgreSQL фиксируется ID будущего объекта и срок действия (время БД + 1 час). После PUT публикация берёт строку `FOR UPDATE`, проверяет срок и отсутствие признака `abandoned`, затем атомарно создаёт Attachment и удаляет запись журнала. Поэтому подтверждённый объект не попадает в очистку. -TODO: гипотеза, hardware/лимиты, версии, объём данных, профиль нагрузки, concurrency/arrival rate, длительность/прогрев, повторы, успешные RPS, ошибки, p50/p95/p99, CPU/RAM. Сохранить raw summary без tokens и объяснить ограничения измерения. +Каждая реплика раз в 30 секунд выбирает до 100 просроченных записей через `FOR UPDATE SKIP LOCKED`, помечает их abandoned и переносит следующую попытку на минуту вперёд. После COMMIT, без долгой DB-транзакции, удаляет соответствующие S3-объекты. Ошибка S3 оставляет запись для повтора. Публикация и очистка сериализуются блокировкой одной строки: либо метаданные опубликованы, либо загрузка окончательно abandoned. -| Воздействие | Что ожидаем | Что наблюдали | Время восстановления / потеря данных | -| --- | --- | --- | --- | -| TODO | TODO | TODO | TODO | +Записи abandoned сохраняются и периодически обрабатываются повторно даже после успешного DELETE. Это намеренно: процесс, замороженный дольше grace period, может поздно закончить PUT. Опубликовать метаданные он уже не сможет, а повторная очистка удалит поздний объект. Без сохранения такой записи первая уборка могла бы пропустить позднюю запись навсегда. Плата — рост журнала неудачных загрузок и повторные DELETE отсутствующих объектов; политика удаления tombstone требует отдельного ограничения максимального времени жизни загрузчика. Успешные загрузки строки журнала не накапливают. -## Ограничения и следующие шаги +Тест `AttachmentCleanupTest` воспроизводит сбой после фактического PUT до записи метаданных, сохраняет свежий объект внутри grace, сдвигает срок записи для проверки уборки без часового ожидания, моделирует отказ DELETE и повтор, поздний PUT и отказ публикации. Подтверждённый объект остаётся доступен. Глобального обхода всего bucket и восстановления вручную удалённых объектов нет — это отдельный исследовательский трек 5. -TODO: какие отказы выдерживаются и какие нет; что не реализовано; где учебное упрощение; какое улучшение подтверждено, а какое пока гипотеза. +## S3-права + +`secrets/s3_access_key_id` и `s3_secret_access_key` используются только MinIO и bootstrap. API получает отдельные `s3_api_access_key_id` и `s3_api_secret_access_key`, административные файлы в него не монтируются. Прикладная policy разрешает `ListBucket` для рабочего bucket (нужен HeadBucket/readiness) и только Get/Put/DeleteObject для `originals/*`. API не может управлять пользователями, политиками, создавать buckets или записывать другие prefixes. Console слушает только `127.0.0.1:9001` внутри контейнера MinIO, поэтому недоступна даже из общей сети Compose. + +Bootstrap повторно создаёт/обновляет пользователя и policy с теми же локальными credentials и не трогает объекты. `check-s3-permissions.sh` запускает bootstrap дважды и проверяет реальные отказы запрещённых операций. Анонимный GET известного существующего объекта отдельно получает 403 в `attachment-failover.py`. + +## Эксперимент с параллельными загрузками + +`attachment-load.py` синхронно выпускает 10 клиентов через Barrier. Каждый отправляет реальный PNG 1600×1600, 7 684 003 bytes (псевдослучайные RGB-пиксели, seed 42), затем скачивает оригинал вторым участником и проверяет SHA-256. Профиль — один burst без повторов, две API-реплики, по одной активной обработке на реплику; очередь включена во время HTTP-ответа. Docker 28.4.0, macOS arm64, лимиты каждой API: 1 CPU / 1 GiB. Это функциональная проверка ограничения памяти, не определение предельного throughput. + +Память измеряется через `memory.current` cgroup v2 каждой API, с паузой 0,2 с между обходами (плюс время docker exec). Включает JVM/native и tmpfs, не только heap. Замер дискретный, поэтому краткие пики между отсчётами могут быть пропущены. Raw-отсчёты, исходные значения и результаты всех запросов: [attachment-load.json](evidence/attachment-load.json). + +Все 10 upload вернули 201, все 10 download совпали по SHA-256. Наблюдаемые пики по репликам: 335579ab19d6: 282.4 MiB, a8ee56f6ec22: 259.0 MiB. Время upload с ожиданием очереди — от 1.030 до 2.973 с; ошибок не было. + +## Ограничения + + +201 подтверждается только после успешных S3 PUT и COMMIT PostgreSQL. Между этими хранилищами нет общей транзакции. При падении после PUT, до COMMIT, возможен объект без метаданных. Такой объект учтён в журнале и удаляется после grace period описанным выше механизмом. При потере ответа upload его повтор может создать ещё одно вложение, как разрешено контрактом. + +Вход и S3 PUT ограничены по bytes и времени; валидация допускает до 40 млн пикселей и только один декодер на реплику. Проведённый burst не доказывает работу при любом количестве соединений, любой комбинации размеров и намеренном истощении ресурсов. Очередь не обещает фиксированное время ответа при перегрузке; HAProxy сохраняет свои таймауты. Случайные orphan-объекты, созданные вручную или прежней версией до журнала V5, автоматическая очистка журнала не обнаруживает. + +Readiness требует PostgreSQL и S3, Redis допускает fallback. При отказе S3 HAProxy выведет обе реплики из ротации; LB может вернуть собственный 503 вместо JSON API. Прямые обращения к API при отказе S3 проверены тестом. Одноузловые PostgreSQL, MinIO и LB остаются точками отказа: volumes переживают restart, но не заменяют репликацию и backup. Сохраняются ограничения конкурентной keyset-пагинации и часов из [отчёта lab3](archive/lab3/REPORT.md). \ No newline at end of file diff --git a/build.gradle.kts b/build.gradle.kts new file mode 100644 index 00000000..97d10253 --- /dev/null +++ b/build.gradle.kts @@ -0,0 +1,125 @@ +plugins { + alias(libs.plugins.kotlin) + alias(libs.plugins.kotlin.serialization) + alias(libs.plugins.ksp) + alias(libs.plugins.shadow) + id("application") +} + +group = "com.mtusi.messenger" +version = "2.0.0" + +repositories { + mavenCentral() +} + +dependencies { + implementation(libs.kotlinx.serialization.json) + implementation(libs.kotlinx.serialization.hocon) + implementation(libs.logback.classic) + + implementation(ktorLibs.server.core) + implementation(ktorLibs.server.netty) + implementation(ktorLibs.server.auth) + implementation(ktorLibs.server.contentNegotiation) + implementation(ktorLibs.server.statusPages) + implementation(ktorLibs.server.callLogging) + implementation(ktorLibs.server.defaultHeaders) + implementation(ktorLibs.server.cors) + implementation(ktorLibs.server.swagger) + implementation(ktorLibs.serialization.kotlinx.json) + + implementation(libs.koin.core) + implementation(libs.koin.ktor) + implementation(libs.koin.annotations) + ksp(libs.koin.compiler) + + implementation(libs.exposed.core) + implementation(libs.exposed.jdbc) + implementation(libs.exposed.kotlin.datetime) + implementation(libs.exposed.migration.core) + implementation(libs.exposed.migration.jdbc) + + implementation(libs.postgres) + implementation(libs.hikaricp) + implementation(libs.flyway) + implementation(libs.flyway.postgresql) + + implementation(libs.lettuce) + implementation(awssdk.services.s3) + + implementation(libs.bouncycastle) + + testImplementation(kotlin("test")) + testImplementation(ktorLibs.server.testHost) + testImplementation(ktorLibs.client.contentNegotiation) + testImplementation(libs.kotlinx.coroutines.test) + testImplementation(libs.koin.test) + testImplementation(libs.testContainers) + testImplementation(libs.testContainers.postgresql) +} + +kotlin { + jvmToolchain(21) +} + +ksp { + arg("KOIN_CONFIG_CHECK", "true") + arg("KOIN_DEFAULT_MODULE", "false") +} + +application { + mainClass = "com.mtusi.messenger.main.AppKt" + applicationDefaultJvmArgs = listOf("-Dio.ktor.development=false") +} + +sourceSets { + named("main") { + resources { + srcDir(layout.projectDirectory.dir("contracts")) + } + } +} + +tasks { + test { + useJUnitPlatform() + } + + shadowJar { + archiveBaseName = "messenger" + archiveClassifier = "" + archiveVersion = "" + mergeServiceFiles() + } + + getByName("run") { + + workingDir = file("run/development") + + doFirst { + val configDir = file("run/development/config") + configDir.mkdirs() + file("run/_template/config").listFiles() + ?.filter { it.isFile && it.extension == "conf" } + ?.forEach { template -> + val target = configDir.resolve(template.name) + if (!target.exists()) template.copyTo(target) + } + + args = configDir.listFiles() + ?.filter { it.extension == "conf" } + ?.sortedBy { it.name } + ?.map { "-config=config/${it.name}" } + ?: emptyList() + } + } + + register("generateMigration") { + group = "database" + description = "Generates a new migration" + + classpath = sourceSets["main"].runtimeClasspath + mainClass.set("com.mtusi.messenger.main.plugins.DatabaseKt") + } +} diff --git a/compose.yaml b/compose.yaml new file mode 100644 index 00000000..f79fa590 --- /dev/null +++ b/compose.yaml @@ -0,0 +1,216 @@ +name: mtusi-messenger-lab4 + +services: + + postgres: + image: postgres:17.6-alpine3.22 + command: ["postgres", "-c", "shared_preload_libraries=pg_stat_statements", "-c", "pg_stat_statements.track=all"] + environment: + POSTGRES_DB: messenger + POSTGRES_USER: messenger + POSTGRES_PASSWORD_FILE: /run/secrets/db_password + secrets: + - db_password + volumes: + - postgres-data:/var/lib/postgresql/data + healthcheck: + test: ["CMD-SHELL", "pg_isready -U messenger -d messenger"] + interval: 5s + timeout: 3s + retries: 10 + start_period: 10s + deploy: + resources: + limits: + cpus: "1.0" + memory: 1g + restart: unless-stopped + + redis: + image: redis:8.4.0-alpine + # Session cache only, PostgreSQL is the source of truth: no RDB/AOF, a restart simply empties the cache. + # Every key carries a TTL, so under memory pressure volatile-lru evicts the least used sessions. + command: + - sh + - -c + - >- + exec docker-entrypoint.sh redis-server + --requirepass "$$(cat /run/secrets/redis_password)" + --save "" + --appendonly no + --maxmemory 64mb + --maxmemory-policy volatile-lru + secrets: + - redis_password + healthcheck: + test: ["CMD-SHELL", "REDISCLI_AUTH=\"$$(cat /run/secrets/redis_password)\" redis-cli ping | grep -q PONG"] + interval: 5s + timeout: 3s + retries: 10 + start_period: 5s + deploy: + resources: + limits: + cpus: "0.5" + memory: 128m + restart: unless-stopped + + s3: + # MinIO RELEASE.2025-09-07T16-13-09Z, pinned multi-platform image. + image: quay.io/minio/minio@sha256:14cea493d9a34af32f524e538b8346cf79f3321eff8e708c1e2960462bd8936e + command: ["server", "/data", "--console-address", "127.0.0.1:9001"] + environment: + MINIO_ROOT_USER_FILE: /run/secrets/s3_access_key_id + MINIO_ROOT_PASSWORD_FILE: /run/secrets/s3_secret_access_key + secrets: + - s3_access_key_id + - s3_secret_access_key + volumes: + - s3-data:/data + healthcheck: + test: ["CMD", "curl", "-f", "http://localhost:9000/minio/health/live"] + interval: 5s + timeout: 3s + retries: 10 + deploy: + resources: + limits: + cpus: "1.0" + memory: 512m + restart: unless-stopped + + s3-init: + # mc RELEASE.2025-08-13T08-35-41Z + image: quay.io/minio/mc@sha256:a7fe349ef4bd8521fb8497f55c6042871b2ae640607cf99d9bede5e9bdf11727 + depends_on: + s3: + condition: service_healthy + entrypoint: ["/bin/sh", "/bootstrap.sh"] + environment: + S3_BUCKET: ${S3_BUCKET:-messenger-attachments} + secrets: + - s3_access_key_id + - s3_secret_access_key + - s3_api_access_key_id + - s3_api_secret_access_key + volumes: + - ./infra/s3/bootstrap.sh:/bootstrap.sh:ro + restart: "no" + + # One-shot: applies Flyway migrations once and exits. Replicas start only after it succeeded, + # so they never race each other on the schema. + migrate: + image: mtusi-messenger:lab4 + command: ["migrate"] + healthcheck: + test: ["NONE"] + depends_on: + postgres: + condition: service_healthy + environment: + DB_HOST: postgres + DB_PORT: "5432" + DB_NAME: messenger + DB_USER: messenger + DB_PASSWORD_FILE: /run/secrets/db_password + secrets: + - db_password + deploy: + resources: + limits: + cpus: "0.5" + memory: 384m + restart: "no" + + api: + build: . + image: mtusi-messenger:lab4 + # Interchangeable replicas: no container_name and no published port, traffic only through `lb`. + deploy: + replicas: ${API_REPLICAS:-2} + resources: + limits: + cpus: "1.0" + memory: 1g + depends_on: + migrate: + condition: service_completed_successfully + postgres: + condition: service_healthy + redis: + condition: service_healthy + s3-init: + condition: service_completed_successfully + environment: + APP_HOST: "0.0.0.0" + APP_PORT: "8080" + DB_HOST: postgres + DB_PORT: "5432" + DB_NAME: messenger + DB_USER: messenger + DB_PASSWORD_FILE: /run/secrets/db_password + DB_MIGRATE_ON_START: "false" + REDIS_HOST: redis + REDIS_PORT: "6379" + REDIS_PASSWORD_FILE: /run/secrets/redis_password + SESSION_TTL_SECONDS: ${SESSION_TTL_SECONDS:-3600} + SESSION_CACHE_SINGLE_FLIGHT: ${SESSION_CACHE_SINGLE_FLIGHT:-true} + IDEMPOTENCY_TTL_SECONDS: ${IDEMPOTENCY_TTL_SECONDS:-86400} + SHUTDOWN_DRAIN_DELAY_MS: "3000" + S3_ENDPOINT: http://s3:9000 + S3_BUCKET: ${S3_BUCKET:-messenger-attachments} + S3_ACCESS_KEY_ID_FILE: /run/secrets/s3_api_access_key_id + S3_SECRET_ACCESS_KEY_FILE: /run/secrets/s3_api_secret_access_key + secrets: + - db_password + - redis_password + - s3_api_access_key_id + - s3_api_secret_access_key + # SIGTERM: readiness 503 and 3 s drain (HAProxy needs <= 2 s to notice), then Ktor closes the socket and + # finishes requests in flight within 5 s. 3 + 5 = 8 s < 10 s before Docker sends SIGKILL. + tmpfs: + - /tmp:size=32m,mode=1777 + stop_grace_period: 10s + restart: unless-stopped + + lb: + image: haproxy:3.2.4-alpine + volumes: + - ./infra/haproxy/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg:ro + depends_on: + api: + condition: service_healthy + ports: + - "127.0.0.1:${LAB_HTTP_PORT:-8080}:8080" + # Port 8404 (health and stats) stays inside the compose network. + healthcheck: + test: ["CMD", "wget", "-qO-", "http://127.0.0.1:8404/healthz"] + interval: 5s + timeout: 2s + retries: 3 + deploy: + resources: + limits: + cpus: "0.5" + memory: 128m + restart: unless-stopped + +secrets: + db_password: + file: ./secrets/db_password + redis_password: + file: ./secrets/redis_password + + s3_access_key_id: + file: ./secrets/s3_access_key_id + s3_secret_access_key: + file: ./secrets/s3_secret_access_key + + s3_api_access_key_id: + file: ./secrets/s3_api_access_key_id + s3_api_secret_access_key: + file: ./secrets/s3_api_secret_access_key + +volumes: + postgres-data: + s3-data: diff --git a/evidence/attachment-failover.txt b/evidence/attachment-failover.txt new file mode 100644 index 00000000..d88491c8 --- /dev/null +++ b/evidence/attachment-failover.txt @@ -0,0 +1,6 @@ +Upload: 201 ready; instance=335579ab19d6 +Anonymous S3 GET of known object: 403 +Outsider metadata/content: 404 +Download: 200; instance=a8ee56f6ec22 +SHA-256: 99ea7533c441f79cd630d15ce4d58d1ec4e06f6f0efb3b77a5c088305cd1bd75 +PASS: uploading replica stopped; original served by another replica; stopped replica restored diff --git a/evidence/attachment-interrupt.txt b/evidence/attachment-interrupt.txt new file mode 100644 index 00000000..a0be3184 --- /dev/null +++ b/evidence/attachment-interrupt.txt @@ -0,0 +1 @@ +PASS: connection closed mid-body; temporary files removed on both replicas; metadata absent diff --git a/evidence/attachment-load.json b/evidence/attachment-load.json new file mode 100644 index 00000000..57f5eec8 --- /dev/null +++ b/evidence/attachment-load.json @@ -0,0 +1,131 @@ +{ + "concurrency": 10, + "file_bytes": 7684003, + "pixels": 2560000, + "sha256": "de56c853868dc1ee1d4b0046023ed484ecd1e4d2ce2d403fbb661d81a414995b", + "baseline_bytes": { + "335579ab19d6": 193679360, + "a8ee56f6ec22": 204886016 + }, + "sampled_peak_bytes": { + "335579ab19d6": 296071168, + "a8ee56f6ec22": 271552512 + }, + "results": [ + { + "upload": 0, + "seconds": 2.591, + "instance": "a8ee56f6ec22" + }, + { + "upload": 1, + "seconds": 2.834, + "instance": "335579ab19d6" + }, + { + "upload": 2, + "seconds": 2.52, + "instance": "335579ab19d6" + }, + { + "upload": 3, + "seconds": 1.634, + "instance": "a8ee56f6ec22" + }, + { + "upload": 4, + "seconds": 1.03, + "instance": "335579ab19d6" + }, + { + "upload": 5, + "seconds": 2.973, + "instance": "a8ee56f6ec22" + }, + { + "upload": 6, + "seconds": 2.087, + "instance": "335579ab19d6" + }, + { + "upload": 7, + "seconds": 1.056, + "instance": "a8ee56f6ec22" + }, + { + "upload": 8, + "seconds": 1.593, + "instance": "335579ab19d6" + }, + { + "upload": 9, + "seconds": 2.096, + "instance": "a8ee56f6ec22" + } + ], + "samples": [ + { + "seconds": 0.0, + "bytes": { + "335579ab19d6": 196050944, + "a8ee56f6ec22": 210128896 + } + }, + { + "seconds": 0.404, + "bytes": { + "335579ab19d6": 212848640, + "a8ee56f6ec22": 223256576 + } + }, + { + "seconds": 0.749, + "bytes": { + "335579ab19d6": 241385472, + "a8ee56f6ec22": 242810880 + } + }, + { + "seconds": 1.075, + "bytes": { + "335579ab19d6": 248864768, + "a8ee56f6ec22": 259260416 + } + }, + { + "seconds": 1.489, + "bytes": { + "335579ab19d6": 266833920, + "a8ee56f6ec22": 249815040 + } + }, + { + "seconds": 1.838, + "bytes": { + "335579ab19d6": 290893824, + "a8ee56f6ec22": 257802240 + } + }, + { + "seconds": 2.244, + "bytes": { + "335579ab19d6": 293588992, + "a8ee56f6ec22": 271036416 + } + }, + { + "seconds": 2.607, + "bytes": { + "335579ab19d6": 296071168, + "a8ee56f6ec22": 271552512 + } + }, + { + "seconds": 3.007, + "bytes": { + "335579ab19d6": 284782592, + "a8ee56f6ec22": 263704576 + } + } + ] +} diff --git a/evidence/failover.txt b/evidence/failover.txt new file mode 100644 index 00000000..0ca07235 --- /dev/null +++ b/evidence/failover.txt @@ -0,0 +1,4 @@ +python3 tests/resilience.py --mode failover --action-script "scripts/stop-one.sh" --base-url "http://localhost:8084" +Executing explicit failover hook; hook output suppressed to avoid leaking secrets. +{"observed_instances_before": 2, "successful_writes": 123, "failed_attempts": 0, "first_recovery_seconds": 5.537} +PASS: failover preserved confirmed data and existing session. Hook scope needs operator evidence. diff --git a/evidence/gradle-tests.txt b/evidence/gradle-tests.txt new file mode 100644 index 00000000..62ca73ed --- /dev/null +++ b/evidence/gradle-tests.txt @@ -0,0 +1,35 @@ +Command: ./gradlew test +BUILD SUCCESSFUL +Tests: 80; failures: 0; errors: 0; skipped: 0 +com.mtusi.messenger.main.infrastructure.controllers.attachment.AttachmentControllerTest: tests=3 failures=0 + upload originals ACL message links and idempotency() + invalid images filenames and byte limit return client errors() + S3 outage fails upload download and readiness without losing the original() +com.mtusi.messenger.main.infrastructure.controllers.authentication.AuthenticationControllerTest: tests=9 failures=0 +com.mtusi.messenger.main.infrastructure.controllers.chat.ChatControllerTest: tests=6 failures=0 +com.mtusi.messenger.main.infrastructure.controllers.health.HealthControllerTest: tests=2 failures=0 +com.mtusi.messenger.main.infrastructure.controllers.message.MessageConcurrencyTest: tests=1 failures=0 +com.mtusi.messenger.main.infrastructure.controllers.message.MessageControllerTest: tests=7 failures=0 +com.mtusi.messenger.main.infrastructure.controllers.message.MessageIdempotencyTest: tests=7 failures=0 +com.mtusi.messenger.main.infrastructure.controllers.user.UserControllerTest: tests=11 failures=0 +com.mtusi.messenger.main.infrastructure.repositories.authentication.session.cache.RedisSessionCacheRepositoryTest: tests=3 failures=0 +com.mtusi.messenger.main.infrastructure.repositories.chat.ExposedChatRepositoryTest: tests=2 failures=0 +com.mtusi.messenger.main.infrastructure.repositories.message.MessageAttachmentsQueryTest: tests=1 failures=0 + history loads ordered attachment links in one query for the entire page() +com.mtusi.messenger.main.infrastructure.services.attachment.AttachmentCleanupTest: tests=1 failures=0 + orphan cleanup respects grace and fences late publishers() +com.mtusi.messenger.main.infrastructure.services.attachment.ImageValidatorTest: tests=3 failures=0 + compressed text metadata does not consume the decoded image memory budget() + corrupted CRC and missing end chunk are rejected() + pixel limit is enforced on header before decoding() +com.mtusi.messenger.main.infrastructure.services.authentication.CrossReplicaRevocationTest: tests=2 failures=0 +com.mtusi.messenger.main.infrastructure.services.authentication.SessionLookupSingleFlightTest: tests=4 failures=0 +com.mtusi.messenger.main.infrastructure.services.hashing.DefaultHashingServiceTest: tests=4 failures=0 +com.mtusi.messenger.main.plugins.CorsTest: tests=2 failures=0 +com.mtusi.messenger.main.plugins.DatabaseMigrationTest: tests=1 failures=0 +com.mtusi.messenger.main.plugins.DependencyFailureTest: tests=5 failures=0 +com.mtusi.messenger.main.plugins.InstanceIdTest: tests=1 failures=0 +com.mtusi.messenger.main.plugins.SessionCacheTest: tests=3 failures=0 +com.mtusi.messenger.main.utils.TemporaryUploadTest: tests=2 failures=0 + temporary file is removed when upload is cancelled() + temporary file is removed on success and failure() diff --git a/evidence/persistence.txt b/evidence/persistence.txt new file mode 100644 index 00000000..729af165 --- /dev/null +++ b/evidence/persistence.txt @@ -0,0 +1,17 @@ +Before restart: +/mtusi-messenger-lab4-api-1 started=2026-11-05T11:21:33.808216129Z +/mtusi-messenger-lab4-api-2 started=2026-11-05T11:19:40.744427424Z +/mtusi-messenger-lab4-lb-1 started=2026-11-04T23:04:59.215310882Z +/mtusi-messenger-lab4-postgres-1 started=2026-11-04T23:04:47.265519668Z +/mtusi-messenger-lab4-redis-1 started=2026-11-04T23:04:47.259546585Z +/mtusi-messenger-lab4-s3-1 started=2026-11-05T11:16:50.746732095Z +python3 tests/resilience.py --mode persistence --action-script "scripts/restart.sh" --base-url "http://localhost:8084" +Executing explicit persistence hook; hook output suppressed to avoid leaking secrets. +PASS: persistence preserved confirmed data and existing session. Hook scope needs operator evidence. +After restart: +/mtusi-messenger-lab4-api-1 started=2026-11-05T11:22:10.858998716Z +/mtusi-messenger-lab4-api-2 started=2026-11-05T11:22:10.784659257Z +/mtusi-messenger-lab4-lb-1 started=2026-11-05T11:22:13.944573717Z +/mtusi-messenger-lab4-postgres-1 started=2026-11-05T11:22:01.053742252Z +/mtusi-messenger-lab4-redis-1 started=2026-11-05T11:22:01.049022586Z +/mtusi-messenger-lab4-s3-1 started=2026-11-05T11:22:01.056079919Z diff --git a/evidence/s3-permissions.txt b/evidence/s3-permissions.txt new file mode 100644 index 00000000..48d1a15b --- /dev/null +++ b/evidence/s3-permissions.txt @@ -0,0 +1,10 @@ + Container mtusi-messenger-lab4-s3-1 Running +Bucket created successfully `local/messenger-attachments`. +Access permission for `local/messenger-attachments` is set to `private` + Container mtusi-messenger-lab4-s3-1 Running +Bucket created successfully `local/messenger-attachments`. +Access permission for `local/messenger-attachments` is set to `private` + Container mtusi-messenger-lab4-s3-1 Running +PASS: app cannot administer MinIO, create buckets, change policy or write outside originals/ +wget: can't connect to remote host (172.25.0.2): Connection refused +PASS: console unavailable from shared network diff --git a/evidence/smoke.txt b/evidence/smoke.txt new file mode 100644 index 00000000..fac765a8 --- /dev/null +++ b/evidence/smoke.txt @@ -0,0 +1,2 @@ +python3 tests/smoke.py --base-url "http://localhost:8084" +PASS: lab4 public API scenario; infrastructure and grading evidence remain separate. diff --git a/gradle.properties b/gradle.properties new file mode 100644 index 00000000..7fc6f1ff --- /dev/null +++ b/gradle.properties @@ -0,0 +1 @@ +kotlin.code.style=official diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml new file mode 100644 index 00000000..ec020b4d --- /dev/null +++ b/gradle/libs.versions.toml @@ -0,0 +1,54 @@ +[versions] +kotlin = "2.3.20" +ksp = "2.3.0" +kotlinx-serialization = "1.10.0" +kotlinx-coroutines = "1.9.0" +logback = "1.5.13" +shadow = "8.3.5" +koin = "4.1.0-RC1" +koin-ktor = "4.1.0-Beta8" +koin-compiler = "2.0.0" +exposed = "1.0.0" +postgres = "42.7.3" +hikaricp = "6.3.0" +flyway = "11.9.0" +lettuce = "6.7.1.RELEASE" +bouncycastle = "1.81" +test-containers = "1.21.0" + +[plugins] +kotlin = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin" } +kotlin-serialization = { id = "org.jetbrains.kotlin.plugin.serialization", version.ref = "kotlin" } +ksp = { id = "com.google.devtools.ksp", version.ref = "ksp" } +shadow = { id = "com.gradleup.shadow", version.ref = "shadow" } + +[libraries] +kotlinx-serialization-json = { module = "org.jetbrains.kotlinx:kotlinx-serialization-json", version.ref = "kotlinx-serialization" } +kotlinx-serialization-hocon = { module = "org.jetbrains.kotlinx:kotlinx-serialization-hocon", version.ref = "kotlinx-serialization" } +kotlinx-coroutines-test = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-test", version.ref = "kotlinx-coroutines" } + +logback-classic = { module = "ch.qos.logback:logback-classic", version.ref = "logback" } + +koin-core = { module = "io.insert-koin:koin-core", version.ref = "koin" } +koin-ktor = { module = "io.insert-koin:koin-ktor3", version.ref = "koin-ktor" } +koin-annotations = { module = "io.insert-koin:koin-annotations", version.ref = "koin-compiler" } +koin-compiler = { module = "io.insert-koin:koin-ksp-compiler", version.ref = "koin-compiler" } +koin-test = { module = "io.insert-koin:koin-test", version.ref = "koin" } + +exposed-core = { module = "org.jetbrains.exposed:exposed-core", version.ref = "exposed" } +exposed-jdbc = { module = "org.jetbrains.exposed:exposed-jdbc", version.ref = "exposed" } +exposed-kotlin-datetime = { module = "org.jetbrains.exposed:exposed-kotlin-datetime", version.ref = "exposed" } +exposed-migration-core = { module = "org.jetbrains.exposed:exposed-migration-core", version.ref = "exposed" } +exposed-migration-jdbc = { module = "org.jetbrains.exposed:exposed-migration-jdbc", version.ref = "exposed" } + +postgres = { module = "org.postgresql:postgresql", version.ref = "postgres" } +hikaricp = { module = "com.zaxxer:HikariCP", version.ref = "hikaricp" } +flyway = { module = "org.flywaydb:flyway-core", version.ref = "flyway" } +flyway-postgresql = { module = "org.flywaydb:flyway-database-postgresql", version.ref = "flyway" } + +lettuce = { module = "io.lettuce:lettuce-core", version.ref = "lettuce" } + +bouncycastle = { module = "org.bouncycastle:bcprov-jdk18on", version.ref = "bouncycastle" } + +testContainers = { module = "org.testcontainers:testcontainers", version.ref = "test-containers" } +testContainers-postgresql = { module = "org.testcontainers:postgresql", version.ref = "test-containers" } diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 00000000..1b33c55b Binary files /dev/null and b/gradle/wrapper/gradle-wrapper.jar differ diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 00000000..ca025c83 --- /dev/null +++ b/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,7 @@ +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-8.14-bin.zip +networkTimeout=10000 +validateDistributionUrl=true +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists diff --git a/gradlew b/gradlew new file mode 100755 index 00000000..23d15a93 --- /dev/null +++ b/gradlew @@ -0,0 +1,251 @@ +#!/bin/sh + +# +# Copyright © 2015-2021 the original authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# SPDX-License-Identifier: Apache-2.0 +# + +############################################################################## +# +# Gradle start up script for POSIX generated by Gradle. +# +# Important for running: +# +# (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is +# noncompliant, but you have some other compliant shell such as ksh or +# bash, then to run this script, type that shell name before the whole +# command line, like: +# +# ksh Gradle +# +# Busybox and similar reduced shells will NOT work, because this script +# requires all of these POSIX shell features: +# * functions; +# * expansions «$var», «${var}», «${var:-default}», «${var+SET}», +# «${var#prefix}», «${var%suffix}», and «$( cmd )»; +# * compound commands having a testable exit status, especially «case»; +# * various built-in commands including «command», «set», and «ulimit». +# +# Important for patching: +# +# (2) This script targets any POSIX shell, so it avoids extensions provided +# by Bash, Ksh, etc; in particular arrays are avoided. +# +# The "traditional" practice of packing multiple parameters into a +# space-separated string is a well documented source of bugs and security +# problems, so this is (mostly) avoided, by progressively accumulating +# options in "$@", and eventually passing that to Java. +# +# Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS, +# and GRADLE_OPTS) rely on word-splitting, this is performed explicitly; +# see the in-line comments for details. +# +# There are tweaks for specific operating systems such as AIX, CygWin, +# Darwin, MinGW, and NonStop. +# +# (3) This script is generated from the Groovy template +# https://github.com/gradle/gradle/blob/HEAD/platforms/jvm/plugins-application/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt +# within the Gradle project. +# +# You can find Gradle at https://github.com/gradle/gradle/. +# +############################################################################## + +# Attempt to set APP_HOME + +# Resolve links: $0 may be a link +app_path=$0 + +# Need this for daisy-chained symlinks. +while + APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path + [ -h "$app_path" ] +do + ls=$( ls -ld "$app_path" ) + link=${ls#*' -> '} + case $link in #( + /*) app_path=$link ;; #( + *) app_path=$APP_HOME$link ;; + esac +done + +# This is normally unused +# shellcheck disable=SC2034 +APP_BASE_NAME=${0##*/} +# Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036) +APP_HOME=$( cd -P "${APP_HOME:-./}" > /dev/null && printf '%s\n' "$PWD" ) || exit + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD=maximum + +warn () { + echo "$*" +} >&2 + +die () { + echo + echo "$*" + echo + exit 1 +} >&2 + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "$( uname )" in #( + CYGWIN* ) cygwin=true ;; #( + Darwin* ) darwin=true ;; #( + MSYS* | MINGW* ) msys=true ;; #( + NONSTOP* ) nonstop=true ;; +esac + +CLASSPATH="\\\"\\\"" + + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD=$JAVA_HOME/jre/sh/java + else + JAVACMD=$JAVA_HOME/bin/java + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD=java + if ! command -v java >/dev/null 2>&1 + then + die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +fi + +# Increase the maximum file descriptors if we can. +if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then + case $MAX_FD in #( + max*) + # In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC2039,SC3045 + MAX_FD=$( ulimit -H -n ) || + warn "Could not query maximum file descriptor limit" + esac + case $MAX_FD in #( + '' | soft) :;; #( + *) + # In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC2039,SC3045 + ulimit -n "$MAX_FD" || + warn "Could not set maximum file descriptor limit to $MAX_FD" + esac +fi + +# Collect all arguments for the java command, stacking in reverse order: +# * args from the command line +# * the main class name +# * -classpath +# * -D...appname settings +# * --module-path (only if needed) +# * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables. + +# For Cygwin or MSYS, switch paths to Windows format before running java +if "$cygwin" || "$msys" ; then + APP_HOME=$( cygpath --path --mixed "$APP_HOME" ) + CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" ) + + JAVACMD=$( cygpath --unix "$JAVACMD" ) + + # Now convert the arguments - kludge to limit ourselves to /bin/sh + for arg do + if + case $arg in #( + -*) false ;; # don't mess with options #( + /?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath + [ -e "$t" ] ;; #( + *) false ;; + esac + then + arg=$( cygpath --path --ignore --mixed "$arg" ) + fi + # Roll the args list around exactly as many times as the number of + # args, so each arg winds up back in the position where it started, but + # possibly modified. + # + # NB: a `for` loop captures its iteration list before it begins, so + # changing the positional parameters here affects neither the number of + # iterations, nor the values presented in `arg`. + shift # remove old arg + set -- "$@" "$arg" # push replacement arg + done +fi + + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' + +# Collect all arguments for the java command: +# * DEFAULT_JVM_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments, +# and any embedded shellness will be escaped. +# * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be +# treated as '${Hostname}' itself on the command line. + +set -- \ + "-Dorg.gradle.appname=$APP_BASE_NAME" \ + -classpath "$CLASSPATH" \ + -jar "$APP_HOME/gradle/wrapper/gradle-wrapper.jar" \ + "$@" + +# Stop when "xargs" is not available. +if ! command -v xargs >/dev/null 2>&1 +then + die "xargs is not available" +fi + +# Use "xargs" to parse quoted args. +# +# With -n1 it outputs one arg per line, with the quotes and backslashes removed. +# +# In Bash we could simply go: +# +# readarray ARGS < <( xargs -n1 <<<"$var" ) && +# set -- "${ARGS[@]}" "$@" +# +# but POSIX shell has neither arrays nor command substitution, so instead we +# post-process each arg (as a line of input to sed) to backslash-escape any +# character that might be a shell metacharacter, then use eval to reverse +# that process (while maintaining the separation between arguments), and wrap +# the whole thing up as a single "set" statement. +# +# This will of course break if any of these variables contains a newline or +# an unmatched quote. +# + +eval "set -- $( + printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" | + xargs -n1 | + sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' | + tr '\n' ' ' + )" '"$@"' + +exec "$JAVACMD" "$@" diff --git a/gradlew.bat b/gradlew.bat new file mode 100644 index 00000000..db3a6ac2 --- /dev/null +++ b/gradlew.bat @@ -0,0 +1,94 @@ +@rem +@rem Copyright 2015 the original author or authors. +@rem +@rem Licensed under the Apache License, Version 2.0 (the "License"); +@rem you may not use this file except in compliance with the License. +@rem You may obtain a copy of the License at +@rem +@rem https://www.apache.org/licenses/LICENSE-2.0 +@rem +@rem Unless required by applicable law or agreed to in writing, software +@rem distributed under the License is distributed on an "AS IS" BASIS, +@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +@rem See the License for the specific language governing permissions and +@rem limitations under the License. +@rem +@rem SPDX-License-Identifier: Apache-2.0 +@rem + +@if "%DEBUG%"=="" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%"=="" set DIRNAME=. +@rem This is normally unused +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Resolve any "." and ".." in APP_HOME to make it shorter. +for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if %ERRORLEVEL% equ 0 goto execute + +echo. 1>&2 +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 1>&2 +echo. 1>&2 +echo Please set the JAVA_HOME variable in your environment to match the 1>&2 +echo location of your Java installation. 1>&2 + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto execute + +echo. 1>&2 +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2 +echo. 1>&2 +echo Please set the JAVA_HOME variable in your environment to match the 1>&2 +echo location of your Java installation. 1>&2 + +goto fail + +:execute +@rem Setup the command line + +set CLASSPATH= + + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" -jar "%APP_HOME%\gradle\wrapper\gradle-wrapper.jar" %* + +:end +@rem End local scope for the variables with windows NT shell +if %ERRORLEVEL% equ 0 goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +set EXIT_CODE=%ERRORLEVEL% +if %EXIT_CODE% equ 0 set EXIT_CODE=1 +if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE% +exit /b %EXIT_CODE% + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/infra/haproxy/haproxy.cfg b/infra/haproxy/haproxy.cfg new file mode 100644 index 00000000..98071432 --- /dev/null +++ b/infra/haproxy/haproxy.cfg @@ -0,0 +1,57 @@ +# Single public entry point in front of the API replicas. +# Backends are discovered through Docker DNS (service name `api`) and receive traffic only while /health/ready is 200. + +global + log stdout format raw local0 info + # Without it HAProxy sizes buffers from the file descriptor limit and uses ~100 MB. + maxconn 4096 + +defaults + mode http + log global + option httplog + option dontlognull + option forwardfor + timeout connect 1s + timeout client 30s + timeout server 30s + timeout http-request 10s + timeout http-keep-alive 5s + # Retried only when the connection to a replica fails, i.e. before the request was sent: + # a POST that reached a replica is never repeated by the balancer. + retries 2 + option redispatch 1 + +resolvers docker + nameserver dns 127.0.0.11:53 + resolve_retries 3 + timeout resolve 1s + timeout retry 1s + hold valid 2s + hold other 2s + hold refused 2s + hold nx 2s + hold timeout 2s + hold obsolete 2s + accepted_payload_size 8192 + +frontend http + bind :8080 + default_backend api + +# Not published: container healthcheck and the backend table (wget -qO- "http://127.0.0.1:8404/stats;csv"). +frontend internal + bind :8404 + monitor-uri /healthz + stats enable + stats uri /stats + stats refresh 5s + +backend api + balance roundrobin + option httpchk GET /health/ready + http-check expect status 200 + # A replica leaves rotation after 2 failed checks 1 s apart and returns after 2 successful ones. + default-server check inter 1s fall 2 rise 2 resolvers docker init-addr none + # Up to 4 replicas; slots without a resolved address stay in maintenance. + server-template api 4 api:8080 diff --git a/infra/s3/bootstrap.sh b/infra/s3/bootstrap.sh new file mode 100644 index 00000000..5ca311de --- /dev/null +++ b/infra/s3/bootstrap.sh @@ -0,0 +1,20 @@ +#!/bin/sh +set -eu +# No tracing: credentials must not appear in logs. +mc alias set local http://s3:9000 "$(cat /run/secrets/s3_access_key_id)" "$(cat /run/secrets/s3_secret_access_key)" >/dev/null +mc mb --ignore-existing "local/$S3_BUCKET" +mc anonymous set none "local/$S3_BUCKET" + +api_user=$(cat /run/secrets/s3_api_access_key_id) +mc admin user add local "$api_user" "$(cat /run/secrets/s3_api_secret_access_key)" >/dev/null +cat > /tmp/attachment-policy.json </dev/null +mc admin policy attach local attachments-api --user "$api_user" >/dev/null diff --git a/run/_template/config/application.conf b/run/_template/config/application.conf new file mode 100644 index 00000000..d9373003 --- /dev/null +++ b/run/_template/config/application.conf @@ -0,0 +1,12 @@ +ktor { + + application { + modules = [com.mtusi.messenger.main.AppKt.module] + } + + deployment { + host = "127.0.0.1" + port = 8080 + } + +} diff --git a/run/_template/config/authentication.conf b/run/_template/config/authentication.conf new file mode 100644 index 00000000..44d5a6d7 --- /dev/null +++ b/run/_template/config/authentication.conf @@ -0,0 +1,9 @@ +authentication { + realm = "messenger" + sessionTtlSeconds = 3600 + sessionCacheTtlSeconds = 60 + # Collapse concurrent cache misses for one token into a single database lookup + sessionCacheSingleFlight = true + # A logout answers 204 only after its tombstone is in Redis; retries up to this long, then 503 + sessionRevokeTimeoutMs = 8000 +} diff --git a/run/_template/config/cors.conf b/run/_template/config/cors.conf new file mode 100644 index 00000000..4551a034 --- /dev/null +++ b/run/_template/config/cors.conf @@ -0,0 +1,23 @@ +cors { + # Defaults to false + #allowAnyHost = true + # Swagger UI sends "Try it out" to the contract server http://localhost:8080; pages opened elsewhere need these. + allowedHosts = [ + { host = "localhost:8080", schemes = ["http"] } + { host = "localhost:8081", schemes = ["http"] } + { host = "127.0.0.1:8080", schemes = ["http"] } + { host = "127.0.0.1:8081", schemes = ["http"] } + ] + + # Defaults to true + #allowAnyMethod = true + #allowedMethods = [GET, POST, PATCH, PUT, DELETE, OPTIONS] + + allowAnyHeader = true + #allowedHeaders = [] + + exposedHeaders = [X-Request-Id, X-Instance-Id, WWW-Authenticate] + + # Browsers send Origin on same-origin POST too, e.g. Swagger UI served by the API itself. + allowSameOrigin = true +} diff --git a/run/_template/config/database.conf b/run/_template/config/database.conf new file mode 100644 index 00000000..2f27e6f8 --- /dev/null +++ b/run/_template/config/database.conf @@ -0,0 +1,12 @@ +database { + host = "localhost" + port = 5432 + name = "messenger" + username = "username" + password = "password" + maxPoolSize = 10 + connectionTimeoutMs = 2000 + statementTimeoutMs = 5000 + # A single local process migrates itself; replicas in compose leave it to the `migrate` service + migrateOnStart = true +} diff --git a/run/_template/config/instance.conf b/run/_template/config/instance.conf new file mode 100644 index 00000000..1029f35d --- /dev/null +++ b/run/_template/config/instance.conf @@ -0,0 +1,4 @@ +instance { + # X-Instance-Id of this process. Defaults to HOSTNAME (the short container id in Docker), else a random alias + #id = "dev" +} diff --git a/run/_template/config/messages.conf b/run/_template/config/messages.conf new file mode 100644 index 00000000..ced68696 --- /dev/null +++ b/run/_template/config/messages.conf @@ -0,0 +1,4 @@ +messages { + # How long a stored Idempotency-Key answers a retry with the original message (contract: at least 24 h) + idempotencyTtlSeconds = 86400 +} diff --git a/run/_template/config/redis.conf b/run/_template/config/redis.conf new file mode 100644 index 00000000..d2f835f0 --- /dev/null +++ b/run/_template/config/redis.conf @@ -0,0 +1,8 @@ + redis { + host = "localhost" + port = 6379 + # Only if the local Redis requires AUTH + #password = "" + connectTimeoutMs = 500 + commandTimeoutMs = 500 +} diff --git a/run/_template/config/storage.conf b/run/_template/config/storage.conf new file mode 100644 index 00000000..2fb4828d --- /dev/null +++ b/run/_template/config/storage.conf @@ -0,0 +1,6 @@ +storage { + endpoint = "http://localhost:9000" + bucket = "messenger-attachments" + accessKeyFile = "../../secrets/s3_api_access_key_id" + secretKeyFile = "../../secrets/s3_api_secret_access_key" +} diff --git a/scripts/attachment-failover.py b/scripts/attachment-failover.py new file mode 100644 index 00000000..de8cdd7d --- /dev/null +++ b/scripts/attachment-failover.py @@ -0,0 +1,59 @@ +#!/usr/bin/env python3 +"""Stop the exact API instance which accepted the upload, then verify the original. +Creates synthetic data; restores the stopped replica in finally. No credentials in output. +""" +import hashlib +import os +from pathlib import Path +import subprocess +import sys +import time + +ROOT = Path(__file__).resolve().parents[1] +sys.path.insert(0, str(ROOT / 'tests')) +from client import Client, Failure, check + + +def main(): + c = Client(timeout=10) + alice, bob, eve, chat = c.bootstrap() + content = (ROOT / 'tests/fixtures/sample.png').read_bytes() + result = c.call('POST', '/api/v1/chats/' + chat['id'] + '/attachments?filename=sample.png', + 201, actor=alice, raw=content, headers={'Content-Type': 'image/png'}, schema='Attachment') + attachment = result['json'] + target = result['headers']['X-Instance-Id'] + c.message(alice, chat, '', attachments=[attachment['id']]) + print('Upload: 201 ready; instance=' + target, flush=True) + # Probe the real known object anonymously from the internal network, not an unknown key. + probe = subprocess.run(['docker', 'compose', 'exec', '-T', 'lb', 'wget', '-S', '-O', '/dev/null', + 'http://s3:9000/' + os.environ.get('S3_BUCKET', 'messenger-attachments') + '/originals/' + attachment['id']], + cwd=ROOT, capture_output=True, text=True) + check(probe.returncode != 0 and '403' in probe.stderr, 'private bucket did not reject anonymous GET') + print('Anonymous S3 GET of known object: 403', flush=True) + path = '/api/v1/attachments/' + attachment['id'] + c.call('GET', path, 404, actor=eve) + c.call('GET', path + '/content', 404, actor=eve) + print('Outsider metadata/content: 404', flush=True) + try: + subprocess.run([str(ROOT / 'scripts/stop-one.sh')], cwd=ROOT, + env=dict(os.environ, TARGET_INSTANCE_ID=target), check=True, capture_output=True) + deadline = time.monotonic() + 15 + while True: + try: + download = c.call('GET', path + '/content', actor=bob) + check(download['headers']['X-Instance-Id'] != target, 'stopped instance returned content') + check(download['bytes'] == content, 'download differs from original') + print('Download: 200; instance=' + download['headers']['X-Instance-Id'], flush=True) + print('SHA-256: ' + hashlib.sha256(content).hexdigest(), flush=True) + break + except Failure: + if time.monotonic() >= deadline: + raise + time.sleep(0.2) + finally: + subprocess.run(['docker', 'compose', 'start', 'api'], cwd=ROOT, check=True, capture_output=True) + print('PASS: uploading replica stopped; original served by another replica; stopped replica restored') + + +if __name__ == '__main__': + main() diff --git a/scripts/attachment-interrupt.py b/scripts/attachment-interrupt.py new file mode 100644 index 00000000..f95385cf --- /dev/null +++ b/scripts/attachment-interrupt.py @@ -0,0 +1,39 @@ +#!/usr/bin/env python3 +"""Abort a binary request mid-body and verify temporary files and metadata are absent.""" +import os +from pathlib import Path +import socket +import subprocess +import sys +import time +import urllib.parse +import uuid + +ROOT = Path(__file__).resolve().parents[1] +sys.path.insert(0, str(ROOT / 'tests')) +from client import Client, check + +c = Client() +alice, bob, eve, chat = c.bootstrap() +name = 'interrupted-' + uuid.uuid4().hex + '.png' +base = urllib.parse.urlsplit(c.base) +body = (ROOT / 'tests/fixtures/sample.png').read_bytes() +headers = ('POST /api/v1/chats/' + chat['id'] + '/attachments?filename=' + name + ' HTTP/1.1\r\n' + 'Host: localhost\r\nAuthorization: Bearer ' + alice['token'] + '\r\n' + 'Content-Type: image/png\r\nContent-Length: 1048576\r\n\r\n') +with socket.create_connection((base.hostname, base.port or 80)) as connection: + connection.sendall(headers.encode() + body[:len(body)//2]) + time.sleep(0.5) +# Let the server observe EOF, or the bounded receive timeout. +ids = subprocess.check_output(['docker', 'compose', 'ps', '-q', 'api'], cwd=ROOT, text=True).split() +deadline = time.monotonic() + 20 +while True: + files = [subprocess.check_output(['docker', 'exec', cid, 'find', '/tmp', '-name', 'messenger-upload-*'], text=True).strip() for cid in ids] + if not any(files): + break + check(time.monotonic() < deadline, 'temporary upload file survived interruption/timeout') + time.sleep(0.2) +count = subprocess.check_output(['docker', 'compose', 'exec', '-T', 'postgres', 'psql', '-U', 'messenger', '-d', 'messenger', '-Atc', + "SELECT count(*) FROM attachments WHERE filename = '" + name + "'"], cwd=ROOT, text=True).strip() +check(count == '0', 'interrupted upload published metadata') +print('PASS: connection closed mid-body; temporary files removed on both replicas; metadata absent') diff --git a/scripts/attachment-load.py b/scripts/attachment-load.py new file mode 100644 index 00000000..ff021fbe --- /dev/null +++ b/scripts/attachment-load.py @@ -0,0 +1,84 @@ +#!/usr/bin/env python3 +"""Ten simultaneous binary uploads and sampled API cgroup memory, without tokens in output.""" +import concurrent.futures +import hashlib +import json +import os +from pathlib import Path +import random +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, check + + +def chunk(kind, data): + return struct.pack('!I', len(data)) + kind + data + struct.pack('!I', zlib.crc32(kind + data)) + + +def fixture(): + width = height = 1600 + rng = random.Random(42) + pixels = b''.join(b'\0' + rng.randbytes(width * 3) for _ in range(height)) + return (b'\x89PNG\r\n\x1a\n' + chunk(b'IHDR', struct.pack('!IIBBBBB', width, height, 8, 2, 0, 0, 0)) + + chunk(b'IDAT', zlib.compress(pixels, 1)) + chunk(b'IEND', b'')) + + +def memory(ids): + result = {} + for container in ids: + value = subprocess.check_output(['docker', 'exec', container, 'cat', '/sys/fs/cgroup/memory.current'], text=True) + result[container[:12]] = int(value) + return result + + +def main(): + c = Client(timeout=60) + alice, bob, eve, chat = c.bootstrap() + data = fixture() + expected = hashlib.sha256(data).hexdigest() + ids = subprocess.check_output(['docker', 'compose', 'ps', '-q', 'api'], cwd=ROOT, text=True).split() + baseline = memory(ids) + samples = [] + stop = threading.Event() + start = time.monotonic() + + def sample(): + while not stop.is_set(): + samples.append({'seconds': round(time.monotonic() - start, 3), 'bytes': memory(ids)}) + stop.wait(0.2) + + barrier = threading.Barrier(10) + + def upload(index): + client = Client(timeout=60) + barrier.wait(timeout=30) + result = client.call('POST', '/api/v1/chats/' + chat['id'] + '/attachments?filename=load.png', + 201, actor=alice, raw=data, headers={'Content-Type': 'image/png'}, schema='Attachment') + check(result['json']['sha256'] == expected, 'uploaded checksum mismatch') + downloaded = client.call('GET', '/api/v1/attachments/' + result['json']['id'] + '/content', actor=bob) + check(hashlib.sha256(downloaded['bytes']).hexdigest() == expected, 'download checksum mismatch') + return {'upload': index, 'seconds': round(result['elapsed'], 3), 'instance': result['headers']['X-Instance-Id']} + + sampler = threading.Thread(target=sample) + sampler.start() + try: + with concurrent.futures.ThreadPoolExecutor(max_workers=10) as pool: + results = list(pool.map(upload, range(10))) + finally: + stop.set() + sampler.join() + peak = {key: max([baseline[key]] + [s['bytes'][key] for s in samples]) for key in baseline} + print(json.dumps({'concurrency': 10, 'file_bytes': len(data), 'pixels': 1600 * 1600, + 'sha256': expected, 'baseline_bytes': baseline, 'sampled_peak_bytes': peak, + 'results': results, 'samples': samples}, indent=2)) + + +if __name__ == '__main__': + main() diff --git a/scripts/check-s3-permissions.sh b/scripts/check-s3-permissions.sh new file mode 100755 index 00000000..416bdeb3 --- /dev/null +++ b/scripts/check-s3-permissions.sh @@ -0,0 +1,21 @@ +#!/bin/sh +set -eu +cd "$(dirname "$0")/.." +# Bootstrap is deliberately repeated; existing objects must survive both runs. +docker compose run --rm s3-init +docker compose run --rm s3-init +docker compose run --rm --entrypoint /bin/sh s3-init -ec ' +mc alias set app http://s3:9000 "$(cat /run/secrets/s3_api_access_key_id)" "$(cat /run/secrets/s3_api_secret_access_key)" >/dev/null +if mc admin info app >/dev/null 2>&1; then exit 1; fi +if mc mb app/forbidden-bucket >/dev/null 2>&1; then exit 1; fi +if mc anonymous set download "app/$S3_BUCKET" >/dev/null 2>&1; then exit 1; fi +printf test > /tmp/test-object +if mc cp /tmp/test-object "app/$S3_BUCKET/forbidden-prefix/object" >/dev/null 2>&1; then exit 1; fi +mc stat "app/$S3_BUCKET" >/dev/null +printf "PASS: app cannot administer MinIO, create buckets, change policy or write outside originals/\n" +' +if docker compose exec -T lb wget -q -T 2 -O /dev/null http://s3:9001; then + echo 'FAIL: console reachable from shared network' + exit 1 +fi +echo 'PASS: console unavailable from shared network' diff --git a/scripts/explain-history.sh b/scripts/explain-history.sh new file mode 100755 index 00000000..fa0c07e0 --- /dev/null +++ b/scripts/explain-history.sh @@ -0,0 +1,10 @@ +#!/bin/sh +# Runs EXPLAIN (ANALYZE, BUFFERS) of the chat history query against the Compose PostgreSQL on 70 000 synthetic +# messages. The data is inserted and rolled back in one transaction; the plans are saved to evidence/. +set -eu +cd "$(dirname "$0")/.." + +mkdir -p evidence + +docker compose exec -T postgres psql -U messenger -d messenger -X -q < scripts/sql/explain-history.sql \ + | tee evidence/explain-history.txt diff --git a/scripts/idempotency-race.py b/scripts/idempotency-race.py new file mode 100755 index 00000000..cdcc6af6 --- /dev/null +++ b/scripts/idempotency-race.py @@ -0,0 +1,84 @@ +#!/usr/bin/env python3 +"""Sends one message with the SAME Idempotency-Key many times in parallel through the balancer. + +Expects every response to be 201 with the same message, at least two replicas answering (X-Instance-Id) +and exactly one message in the chat history. Synthetic users only; the token stays in memory. +""" +import argparse +import base64 +import json +import threading +import urllib.error +import urllib.request +import uuid +from collections import Counter + +parser = argparse.ArgumentParser() +parser.add_argument('--base-url', default='http://localhost:8080') +parser.add_argument('--parallel', type=int, default=32) +parser.add_argument('--rounds', type=int, default=20) +args = parser.parse_args() + + +def call(method, path, body=None, headers=None): + data = json.dumps(body).encode() if body is not None else None + request = urllib.request.Request(args.base_url + path, data=data, method=method, headers={ + 'Content-Type': 'application/json', **(headers or {})}) + try: + with urllib.request.urlopen(request, timeout=10) as response: + return response.status, response.headers, json.loads(response.read() or b'null') + except urllib.error.HTTPError as error: + return error.code, error.headers, json.loads(error.read() or b'null') + + +username = 'race_' + uuid.uuid4().hex[:12] +password = 'race-password-' + uuid.uuid4().hex +status, _, user = call('POST', '/api/v1/users', {'username': username, 'display_name': 'race', 'password': password}) +assert status == 201, status +basic = base64.b64encode(f'{username}:{password}'.encode()).decode() +status, _, session = call('POST', '/api/v1/auth/sessions', headers={'Authorization': 'Basic ' + basic}) +assert status == 201, status +auth = {'Authorization': 'Bearer ' + session['token']} +status, _, chat = call('POST', '/api/v1/chats', {'title': 'race', 'member_ids': [user['id']]}, auth) +assert status == 201, status +path = f"/api/v1/chats/{chat['id']}/messages" + +statuses, instances, violations = Counter(), Counter(), 0 +for round_number in range(args.rounds): + key = uuid.uuid4().hex + results = [] + barrier = threading.Barrier(args.parallel) + + def send(): + barrier.wait() + results.append(call('POST', path, {'text': f'race {round_number}'}, {**auth, 'Idempotency-Key': key})) + + threads = [threading.Thread(target=send) for _ in range(args.parallel)] + for thread in threads: + thread.start() + for thread in threads: + thread.join() + + ids = {body['id'] for status, _, body in results if status == 201} + for status, headers, _ in results: + statuses[status] += 1 + instances[headers.get('X-Instance-Id', '-')] += 1 + if len(ids) != 1: + violations += 1 + +status, _, page = call('GET', path + '?limit=100', headers=auth) +texts = [m['text'] for m in page['items']] + +print(json.dumps({ + 'rounds': args.rounds, + 'parallel_requests_per_round': args.parallel, + 'statuses': dict(statuses), + 'requests_per_instance': dict(instances), + 'rounds_with_more_than_one_message_id': violations, + 'messages_in_history': len(texts), + 'distinct_texts_in_history': len(set(texts)), +}, indent=2)) + +ok = violations == 0 and len(texts) == args.rounds == len(set(texts)) and len(instances) >= 2 and set(statuses) == {201} +print('PASS' if ok else 'FAIL') +raise SystemExit(0 if ok else 1) diff --git a/scripts/init-secrets.sh b/scripts/init-secrets.sh new file mode 100755 index 00000000..7a4f6848 --- /dev/null +++ b/scripts/init-secrets.sh @@ -0,0 +1,19 @@ +#!/bin/sh +# Creates local secrets for the Compose stack. Never overwrites an existing secret: +# the PostgreSQL volume is initialised with the first password and keeps it. +set -eu +cd "$(dirname "$0")/.." + +mkdir -p secrets + +# Readable by the non-root users inside the containers (bind-mounted as is). +umask 022 + +for name in db_password redis_password s3_access_key_id s3_secret_access_key s3_api_access_key_id s3_api_secret_access_key; do + if [ ! -s "secrets/$name" ]; then + openssl rand -hex 24 > "secrets/$name" + echo "created secrets/$name" + else + echo "secrets/$name already exists, keeping it" + fi +done diff --git a/scripts/load/failover-under-load.sh b/scripts/load/failover-under-load.sh new file mode 100755 index 00000000..df654990 --- /dev/null +++ b/scripts/load/failover-under-load.sh @@ -0,0 +1,55 @@ +#!/bin/sh +# One replica fault under constant load through the balancer, then its restoration. +# MODE=stop docker stop (SIGTERM: drain, graceful shutdown) MODE=kill docker kill (SIGKILL: crash) +# TARGET=api-1|api-2 RATE=150 req/s DURATION=45 s fault at 10 s, restore at 25 s +# Output: evidence/load/failover--/{k6.txt,events.json,haproxy.log,per-second.csv,summary.json} +set -eu +cd "$(dirname "$0")/../.." + +MODE=${MODE:-stop} +TARGET=${TARGET:-api-1} +RATE=${RATE:-150} +DURATION=${DURATION:-45} +FAULT_AT=${FAULT_AT:-10} +RESTORE_AT=${RESTORE_AT:-25} + +out=evidence/load/failover-$MODE-$TARGET +rm -rf "$out" && mkdir -p "$out" +now() { python3 -c 'import time; print(int(time.time() * 1000))'; } +# Sleeps until after the load started, so the schedule does not drift with slow docker commands. +sleep_until() { python3 -c "import time; time.sleep(max(0, $start / 1000 + $1 - time.time()))"; } +container=mtusi-messenger-lab4-$TARGET +target_instance=$(docker inspect -f '{{.Config.Hostname}}' "$container") + +since=$(date -u +%Y-%m-%dT%H:%M:%SZ) +K6_CSV_TIME_FORMAT=unix_milli k6 run --quiet --out csv="$out/requests.csv" \ + -e RATE="$RATE" -e DURATION="${DURATION}s" scripts/load/mixed.js > "$out/k6.txt" 2>&1 & +k6_pid=$! + +# Load starts after k6 setup (users, sessions, chat): wait for the first request of the scenario. +until grep -q ',load,' "$out/requests.csv" 2>/dev/null; do sleep 0.2; done +start=$(now) + +sleep_until "$FAULT_AT" +fault=$(now) +if [ "$MODE" = kill ]; then docker kill "$container" >/dev/null; else docker stop --time 9 "$container" >/dev/null; fi +fault_done=$(now) + +sleep_until "$RESTORE_AT" +restore_begin=$(now) +docker start "$container" >/dev/null +until [ "$(docker inspect -f '{{.State.Health.Status}}' "$container")" = healthy ]; do sleep 0.2; done +restore_healthy=$(now) +restored_instance=$(docker inspect -f '{{.Config.Hostname}}' "$container") + +wait "$k6_pid" || true +docker compose logs --timestamps --no-log-prefix --since "$since" lb 2>/dev/null | grep -E 'Server api/|SH--|5[0-9][0-9] ' > "$out/haproxy.log" || true + +cat > "$out/events.json" < /dev/null +# Raw per-request data stays out of Git (evidence keeps aggregates only); KEEP_RAW=1 keeps it for analysis. +[ "${KEEP_RAW:-0}" = 1 ] || rm -f "$out/requests.csv" +cat "$out/summary.json" diff --git a/scripts/load/mixed.js b/scripts/load/mixed.js new file mode 100644 index 00000000..1cdf57dc --- /dev/null +++ b/scripts/load/mixed.js @@ -0,0 +1,67 @@ +// Open-model load through the balancer: RATE requests per second regardless of response time. +// 70 % GET /users/me (session lookup), 30 % POST message with a unique Idempotency-Key. +// Every response adds a `served_by` sample tagged with X-Instance-Id and status, so the summary can show +// which replica answered each second. Tokens live only in this process. +import http from 'k6/http'; +import encoding from 'k6/encoding'; +import { Counter } from 'k6/metrics'; + +const BASE = __ENV.BASE_URL || 'http://localhost:8080'; +const RATE = Number(__ENV.RATE || 150); +const DURATION = __ENV.DURATION || '40s'; +const USERS = 10; +const PASSWORD = 'load-test-password'; + +const servedBy = new Counter('served_by'); + +export const options = { + scenarios: { + load: { + executor: 'constant-arrival-rate', + rate: RATE, + timeUnit: '1s', + duration: DURATION, + preAllocatedVUs: 50, + maxVUs: 300, + }, + }, + summaryTrendStats: ['avg', 'med', 'p(95)', 'p(99)', 'max'], +}; + +export function setup() { + const json = { headers: { 'Content-Type': 'application/json' } }; + const users = []; + for (let i = 0; i < USERS; i++) { + const username = `load_${Date.now().toString(36)}_${i}`; + const user = http.post(`${BASE}/api/v1/users`, JSON.stringify({ username, display_name: 'load', password: PASSWORD }), json).json(); + const basic = encoding.b64encode(`${username}:${PASSWORD}`); + const session = http.post(`${BASE}/api/v1/auth/sessions`, null, { headers: { Authorization: `Basic ${basic}` } }).json(); + users.push({ id: user.id, token: session.token }); + } + const chat = http.post(`${BASE}/api/v1/chats`, JSON.stringify({ title: 'load', member_ids: users.map((u) => u.id) }), { + headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${users[0].token}` }, + }).json(); + return { users, chatId: chat.id }; +} + +export default function (data) { + const user = data.users[Math.floor(Math.random() * data.users.length)]; + const auth = { Authorization: `Bearer ${user.token}` }; + let response; + + if (Math.random() < 0.7) { + response = http.get(`${BASE}/api/v1/users/me`, { headers: auth, tags: { name: 'me' }, timeout: '5s' }); + } else { + response = http.post( + `${BASE}/api/v1/chats/${data.chatId}/messages`, + JSON.stringify({ text: `load ${__VU}-${__ITER}` }), + { + headers: { ...auth, 'Content-Type': 'application/json', 'Idempotency-Key': `load-${__VU}-${__ITER}-${Date.now()}` }, + tags: { name: 'send' }, + timeout: '5s', + }, + ); + } + + servedBy.add(1, { instance: response.headers['X-Instance-Id'] || 'none', code: String(response.status) }); +} diff --git a/scripts/load/rolling-under-load.sh b/scripts/load/rolling-under-load.sh new file mode 100755 index 00000000..b326bb04 --- /dev/null +++ b/scripts/load/rolling-under-load.sh @@ -0,0 +1,34 @@ +#!/bin/sh +# scripts/rolling-update.sh while k6 keeps RATE req/s through the balancer. +# Output: evidence/load/rolling-update/{k6.txt,rolling.log,events.json,per-second.csv,summary.json} +set -eu +cd "$(dirname "$0")/../.." + +RATE=${RATE:-150} +DURATION=${DURATION:-70} +out=evidence/load/rolling-update +rm -rf "$out" && mkdir -p "$out" +now() { python3 -c 'import time; print(int(time.time() * 1000))'; } + +# Build outside the measured window: compiling would compete with the API for CPU. +docker compose build --quiet api + +{ echo "# before"; docker compose ps api --format '{{.Name}} {{.ID}} {{.Image}}'; } > "$out/rolling.log" +K6_CSV_TIME_FORMAT=unix_milli k6 run --quiet --out csv="$out/requests.csv" \ + -e RATE="$RATE" -e DURATION="${DURATION}s" scripts/load/mixed.js > "$out/k6.txt" 2>&1 & +k6_pid=$! +until grep -q ',load,' "$out/requests.csv" 2>/dev/null; do sleep 0.2; done +start=$(now) + +sleep 10 +begin=$(now) +scripts/rolling-update.sh >> "$out/rolling.log" 2>&1 +end=$(now) +wait "$k6_pid" || true + +{ echo "# after"; docker compose ps api --format '{{.Name}} {{.ID}} {{.Image}}'; } >> "$out/rolling.log" +echo "{\"load_start\": $start, \"rolling_begin\": $begin, \"rolling_end\": $end}" > "$out/events.json" +python3 scripts/load/summarize.py "$out" > /dev/null +# Raw per-request data stays out of Git (evidence keeps aggregates only); KEEP_RAW=1 keeps it for analysis. +[ "${KEEP_RAW:-0}" = 1 ] || rm -f "$out/requests.csv" +cat "$out/rolling.log" "$out/summary.json" diff --git a/scripts/load/summarize.py b/scripts/load/summarize.py new file mode 100755 index 00000000..a3a2d04b --- /dev/null +++ b/scripts/load/summarize.py @@ -0,0 +1,110 @@ +#!/usr/bin/env python3 +"""Per-second view of one k6 run (requests.csv) plus the events of the experiment (events.json). + +Writes per-second.csv and summary.json next to the input. An error is any response that is not 2xx +(including k6 status 0: connection reset or timeout). Times are seconds from the start of the load. +""" +import csv +import json +import math +import sys +from collections import Counter, defaultdict +from pathlib import Path +from urllib.parse import parse_qsl + +run = Path(sys.argv[1]) +events = json.loads((run / 'events.json').read_text()) if (run / 'events.json').exists() else {} + +durations = [] # (t_ms, duration_ms, status) +served = [] # (t_ms, instance, status) +with open(run / 'requests.csv') as f: + for row in csv.DictReader(f): + if row['metric_name'] == 'http_req_duration' and row['scenario'] == 'load': + durations.append((int(row['timestamp']), float(row['metric_value']), int(row['status'] or 0))) + elif row['metric_name'] == 'served_by': + tags = dict(parse_qsl(row['extra_tags'])) + served.append((int(row['timestamp']), tags.get('instance', 'none'), int(tags.get('code', 0)))) + +t0 = min(t for t, _, _ in durations) +rel = lambda ms: (ms - t0) / 1000 + + +def percentile(values, p): + if not values: + return None + values = sorted(values) + return values[min(len(values) - 1, math.ceil(p / 100 * len(values)) - 1)] + + +per_second = defaultdict(lambda: {'ok': 0, 'errors': 0, 'durations': [], 'instances': Counter()}) +for t, d, status in durations: + second = per_second[math.floor(rel(t))] + second['durations'].append(d) + if 200 <= status < 300: + second['ok'] += 1 + else: + second['errors'] += 1 +for t, instance, status in served: + if 200 <= status < 300: + per_second[math.floor(rel(t))]['instances'][instance] += 1 + +instances = sorted({i for s in per_second.values() for i in s['instances']}) +with open(run / 'per-second.csv', 'w', newline='') as f: + writer = csv.writer(f) + writer.writerow(['second', 'ok', 'errors', 'p99_ms', 'max_ms'] + instances) + for second in sorted(per_second): + s = per_second[second] + writer.writerow([second, s['ok'], s['errors'], round(percentile(s['durations'], 99), 1), round(max(s['durations']), 1)] + + [s['instances'].get(i, 0) for i in instances]) + +errors = sorted(rel(t) for t, _, status in durations if not 200 <= status < 300) +seconds = max(per_second) + 1 +summary = { + 'requests': len(durations), + 'ok': len(durations) - len(errors), + 'errors': len(errors), + 'error_codes': dict(Counter(str(status) for _, _, status in durations if not 200 <= status < 300)), + 'ok_rps': round((len(durations) - len(errors)) / seconds, 1), + 'p50_ms': round(percentile([d for _, d, _ in durations], 50), 1), + 'p95_ms': round(percentile([d for _, d, _ in durations], 95), 1), + 'p99_ms': round(percentile([d for _, d, _ in durations], 99), 1), + 'max_ms': round(max(d for _, d, _ in durations), 1), + 'requests_per_instance': dict(Counter(i for _, i, status in served if 200 <= status < 300)), +} +for name, value in events.items(): + if isinstance(value, (int, float)): + summary[f'event_{name}_s'] = round(rel(value), 2) +if 'fault' in events: + fault = rel(events['fault']) + after = [e - fault for e in errors if e >= fault - 0.5] + summary['errors_after_fault'] = len(after) + summary['last_error_after_fault_s'] = round(max(after), 2) if after else None + target = events.get('target_instance') + end = rel(events['restore_begin']) if 'restore_begin' in events else float('inf') + last = [rel(t) - fault for t, i, status in served if i == target and fault <= rel(t) < end] + summary['target_last_response_after_fault_s'] = round(max(last), 2) if last else None +if 'restore_begin' in events and events.get('restored_instance'): + begin = rel(events['restore_begin']) + first = [rel(t) - begin for t, i, status in served + if i == events['restored_instance'] and 200 <= status < 300 and rel(t) >= begin] + summary['restored_replica_first_response_after_start_s'] = round(min(first), 2) if first else None + +# HAProxy state changes (docker log timestamps): when the balancer took the target out and put it back. +log = run / 'haproxy.log' +if log.exists() and 'fault' in events: + from datetime import datetime + + def at(line): + stamp = line.split()[0].rstrip('Z') + whole, _, fraction = stamp.partition('.') + return datetime.fromisoformat(whole + '+00:00').timestamp() * 1000 + float('0.' + (fraction or '0')) * 1000 + + changes = [(at(line), line) for line in log.read_text().splitlines() if 'Server api/' in line and '[WARNING]' not in line] + down = [t for t, line in changes if ('is DOWN' in line or 'going DOWN' in line) and t >= events['fault']] + summary['lb_marked_down_after_fault_s'] = round((min(down) - events['fault']) / 1000, 2) if down else None + if 'restore_begin' in events: + up = [t for t, line in changes if 'is UP, reason' in line and t >= events['restore_begin']] + summary['lb_marked_up_after_start_s'] = round((min(up) - events['restore_begin']) / 1000, 2) if up else None + +(run / 'summary.json').write_text(json.dumps(summary, indent=2, ensure_ascii=False)) +print(json.dumps(summary, indent=2, ensure_ascii=False)) diff --git a/scripts/restart.sh b/scripts/restart.sh new file mode 100755 index 00000000..88dbc5d9 --- /dev/null +++ b/scripts/restart.sh @@ -0,0 +1,7 @@ +#!/bin/sh +# Restarts the balancer, every API replica, PostgreSQL, Redis and S3, keeping the named volume (no `down -v`, no re-seed). +set -eu +cd "$(dirname "$0")/.." + +docker compose stop lb api redis postgres s3 +docker compose up -d --wait diff --git a/scripts/revocation-pause.sh b/scripts/revocation-pause.sh new file mode 100755 index 00000000..9c3a5880 --- /dev/null +++ b/scripts/revocation-pause.sh @@ -0,0 +1,44 @@ +#!/usr/bin/env bash +# Reproduces the review scenario on the running stack, everything through the balancer: +# session + message, warm the session on both replicas, CLIENT PAUSE WRITE on Redis (reads keep working), +# logout, then read and repeat the original message with the old token and key. +# Expected: logout is 204 only once the tombstone is in Redis; afterwards every replica answers 401. +# PAUSE_MS=5000 (default): the logout waits for the pause and answers 204. +# PAUSE_MS=12000: longer than the logout deadline (8 s): the logout answers 503 and every replica still rejects the token. +set -euo pipefail +cd "$(dirname "$0")/.." + +PAUSE_MS=${PAUSE_MS:-5000} +B=${BASE_URL:-http://localhost:8080}/api/v1 +U=pause_$RANDOM$RANDOM +PW=pause-test-password +KEY=pause-$RANDOM$RANDOM +redis() { docker compose exec -T redis redis-cli --no-auth-warning -a "$(cat secrets/redis_password)" "$@"; } +now() { python3 -c 'import time; print(time.time())'; } +status() { curl -s -o /dev/null -w '%{http_code} %header{x-instance-id}' "$@"; } + +curl -s -o /dev/null -X POST "$B/users" -H 'Content-Type: application/json' \ + -d "{\"username\":\"$U\",\"display_name\":\"pause\",\"password\":\"$PW\"}" +T=$(curl -s -X POST "$B/auth/sessions" -u "$U:$PW" | python3 -c 'import sys,json;print(json.load(sys.stdin)["token"])') +ME=$(curl -s "$B/users/me" -H "Authorization: Bearer $T" | python3 -c 'import sys,json;print(json.load(sys.stdin)["id"])') +CHAT=$(curl -s -X POST "$B/chats" -H "Authorization: Bearer $T" -H 'Content-Type: application/json' \ + -d "{\"title\":\"pause\",\"member_ids\":[\"$ME\"]}" | python3 -c 'import sys,json;print(json.load(sys.stdin)["id"])') +send() { status -X POST "$B/chats/$CHAT/messages" -H "Authorization: Bearer $T" -H 'Content-Type: application/json' \ + -H "Idempotency-Key: $KEY" -d '{"text":"original message"}'; } + +echo "send message with Idempotency-Key: $(send)" +echo "warm the session on both replicas:" +for i in 1 2 3 4; do echo " GET /users/me -> $(status "$B/users/me" -H "Authorization: Bearer $T")"; done + +echo "redis: CLIENT PAUSE $PAUSE_MS WRITE -> $(redis CLIENT PAUSE "$PAUSE_MS" WRITE)" +start=$(now) +echo "DELETE /auth/sessions/current -> $(status -X DELETE "$B/auth/sessions/current" -H "Authorization: Bearer $T") after $(python3 -c "print(round($(now) - $start, 2))") s" +sleep 1.5 # replicas check pending revocations once a second; only matters for the unconfirmed (503) case + +echo "old token after logout (redis writes still paused: $(python3 -c "print('yes' if $(now) - $start < $PAUSE_MS / 1000 else 'no')")):" +for i in 1 2 3 4; do echo " GET /users/me -> $(status "$B/users/me" -H "Authorization: Bearer $T")"; done +for i in 1 2; do echo " POST message, same key -> $(send)"; done + +python3 -c "import time; time.sleep(max(0, $start + $PAUSE_MS / 1000 + 2 - time.time()))" +echo "after the pause: session key in redis = $(redis GET "session:$(printf %s "$T" | shasum -a 256 | cut -d' ' -f1)")" +for i in 1 2; do echo " GET /users/me -> $(status "$B/users/me" -H "Authorization: Bearer $T")"; done diff --git a/scripts/rolling-update.sh b/scripts/rolling-update.sh new file mode 100755 index 00000000..7952b143 --- /dev/null +++ b/scripts/rolling-update.sh @@ -0,0 +1,40 @@ +#!/usr/bin/env bash +# Replaces the API replicas one by one with a freshly built image while the balancer keeps serving. +# 1. build the new image, 2. apply migrations once (they must be compatible with the running version), +# 3. for every old replica: start one new replica, wait until HAProxy has it UP, then stop (drain) and remove the old. +# The number of serving replicas never drops below the configured count. +set -euo pipefail +cd "$(dirname "$0")/.." + +build_id=${BUILD_ID:-$(git rev-parse --short HEAD)-$(date +%s)} +log() { echo "$(date -u +%H:%M:%S) $*"; } + +up_in_lb() { + docker compose exec -T lb wget -qO- 'http://127.0.0.1:8404/stats;csv' | awk -F, '$1 == "api" && $2 != "BACKEND" && $18 == "UP"' | wc -l +} + +log "build image (BUILD_ID=$build_id)" +docker compose build --quiet --build-arg BUILD_ID="$build_id" api + +log "migrate" +docker compose run --rm --no-deps migrate > /dev/null + +old=$(docker compose ps -q api) +count=$(echo "$old" | wc -l | tr -d ' ') + +for container in $old; do + before=$(docker compose ps -q api | sort) + docker compose up -d --no-deps --no-recreate --scale api=$((count + 1)) api 2> /dev/null + new=$(comm -13 <(echo "$before") <(docker compose ps -q api | sort)) + log "started $(docker inspect -f '{{.Name}}' "$new") ($(echo "$new" | cut -c1-12))" + + until [ "$(docker inspect -f '{{.State.Health.Status}}' "$new")" = healthy ]; do sleep 0.5; done + until [ "$(up_in_lb)" -ge $((count + 1)) ]; do sleep 0.5; done + log "new replica is UP in HAProxy" + + docker stop --time 9 "$container" > /dev/null + docker rm "$container" > /dev/null + log "stopped and removed $(echo "$container" | cut -c1-12)" +done + +log "done: $(docker compose ps -q api | while read -r c; do docker inspect -f '{{.Name}} {{index .Config.Labels "org.opencontainers.image.revision"}}' "$c"; done | tr '\n' ' ')" diff --git a/scripts/sql/explain-history.sql b/scripts/sql/explain-history.sql new file mode 100644 index 00000000..8467e137 --- /dev/null +++ b/scripts/sql/explain-history.sql @@ -0,0 +1,100 @@ +-- EXPLAIN (ANALYZE, BUFFERS) of the chat history query on synthetic data. +-- Everything runs in one transaction that is rolled back: the database is left untouched. +-- The queries are the ones ExposedMessageRepository.findPageByChatId generates (page size 50 + 1 look-ahead row). + +\set ON_ERROR_STOP on +\pset pager off + +BEGIN; + +INSERT INTO users (id, username, display_name, password_hash, created_at) +VALUES ('00000000-0000-0000-0000-000000000001', 'explain_alice', 'Alice', 'not-a-hash', now()), + ('00000000-0000-0000-0000-000000000002', 'explain_bob', 'Bob', 'not-a-hash', now()); + +-- One large chat (20 000 messages) and 50 neighbours with 1 000 messages each: 70 000 rows in total, +-- so the plan has to skip foreign chats instead of reading a table that only holds the target chat. +INSERT INTO chats (id, title, created_at) +SELECT ('00000000-0000-0000-0001-' || lpad(n::text, 12, '0'))::uuid, 'explain chat ' || n, now() +FROM generate_series(0, 50) AS n; + +INSERT INTO chat_members (chat_id, user_id, position) +SELECT c.id, u.id, u.position +FROM chats c +CROSS JOIN (VALUES ('00000000-0000-0000-0000-000000000001'::uuid, 0), + ('00000000-0000-0000-0000-000000000002'::uuid, 1)) AS u (id, position) +WHERE c.title LIKE 'explain chat %'; + +INSERT INTO messages (id, chat_id, sender_id, text, created_at) +SELECT gen_random_uuid(), + '00000000-0000-0000-0001-000000000000', + CASE WHEN n % 2 = 0 THEN '00000000-0000-0000-0000-000000000001'::uuid + ELSE '00000000-0000-0000-0000-000000000002'::uuid END, + 'message #' || n, + timestamptz '2026-01-01 00:00:00+00' + n * interval '1 second' +FROM generate_series(1, 20000) AS n; + +INSERT INTO messages (id, chat_id, sender_id, text, created_at) +SELECT gen_random_uuid(), + ('00000000-0000-0000-0001-' || lpad(c::text, 12, '0'))::uuid, + '00000000-0000-0000-0000-000000000001', + 'neighbour message #' || n, + timestamptz '2026-01-01 00:00:00+00' + n * interval '1 second' +FROM generate_series(1, 50) AS c, generate_series(1, 1000) AS n; + +ANALYZE users, chats, chat_members, messages; + +\echo +\echo '== Data volume' +SELECT count(*) AS total_messages, + count(*) FILTER (WHERE chat_id = '00000000-0000-0000-0001-000000000000') AS target_chat_messages +FROM messages; + +-- Cursor of the 15 000th message: a deep page, where OFFSET pagination would have to skip 15 000 rows. +SELECT created_at AS cursor_created_at, id AS cursor_id +FROM messages +WHERE chat_id = '00000000-0000-0000-0001-000000000000' +ORDER BY created_at, id +OFFSET 14999 LIMIT 1 \gset + +\echo +\echo '== 1. First page: WHERE chat_id = ? ORDER BY created_at, id LIMIT 51' +EXPLAIN (ANALYZE, BUFFERS, COSTS OFF) +SELECT messages.id, messages.chat_id, messages.sender_id, messages.text, messages.created_at +FROM messages +WHERE messages.chat_id = '00000000-0000-0000-0001-000000000000' +ORDER BY messages.created_at ASC, messages.id ASC +LIMIT 51; + +\echo +\echo '== 2. Deep page after the 15 000th message: (created_at, id) > (?, ?)' +EXPLAIN (ANALYZE, BUFFERS, COSTS OFF) +SELECT messages.id, messages.chat_id, messages.sender_id, messages.text, messages.created_at +FROM messages +WHERE (messages.chat_id = '00000000-0000-0000-0001-000000000000') + AND ((messages.created_at, messages.id) > (:'cursor_created_at', :'cursor_id')) +ORDER BY messages.created_at ASC, messages.id ASC +LIMIT 51; + +\echo +\echo '== 3. Same deep page with OFFSET instead of a cursor (for comparison, not used by the API)' +EXPLAIN (ANALYZE, BUFFERS, COSTS OFF) +SELECT messages.id, messages.chat_id, messages.sender_id, messages.text, messages.created_at +FROM messages +WHERE messages.chat_id = '00000000-0000-0000-0001-000000000000' +ORDER BY messages.created_at ASC, messages.id ASC +LIMIT 51 OFFSET 15000; + +\echo +\echo '== 4. Deep page without messages_chat_id_created_at_id_idx (index scans disabled for this query only)' +SET LOCAL enable_indexscan = off; +SET LOCAL enable_bitmapscan = off; +SET LOCAL enable_indexonlyscan = off; +EXPLAIN (ANALYZE, BUFFERS, COSTS OFF) +SELECT messages.id, messages.chat_id, messages.sender_id, messages.text, messages.created_at +FROM messages +WHERE (messages.chat_id = '00000000-0000-0000-0001-000000000000') + AND ((messages.created_at, messages.id) > (:'cursor_created_at', :'cursor_id')) +ORDER BY messages.created_at ASC, messages.id ASC +LIMIT 51; + +ROLLBACK; diff --git a/scripts/stop-one.sh b/scripts/stop-one.sh new file mode 100755 index 00000000..f8e65527 --- /dev/null +++ b/scripts/stop-one.sh @@ -0,0 +1,22 @@ +#!/bin/sh +# Stops the ONE API replica named by TARGET_INSTANCE_ID and leaves it stopped (no DB/LB/volume changes). +# The replica's X-Instance-Id is its container hostname, i.e. the short container id. +# Restore afterwards with: docker compose start api +set -eu +cd "$(dirname "$0")/.." + +: "${TARGET_INSTANCE_ID:?test provides target API instance alias}" + +case "$TARGET_INSTANCE_ID" in + *[!0-9a-f]* | "") echo "unexpected instance id" >&2; exit 2 ;; +esac + +# Only containers of the api service are candidates, so a wrong id can never stop the database or the balancer. +container=$(docker compose ps -q api | grep "^$TARGET_INSTANCE_ID" || true) + +if [ -z "$container" ] || [ "$(printf '%s\n' "$container" | wc -l)" -ne 1 ]; then + echo "no single api replica matches $TARGET_INSTANCE_ID" >&2 + exit 1 +fi + +docker stop --time 9 "$container" >/dev/null diff --git a/settings.gradle.kts b/settings.gradle.kts new file mode 100644 index 00000000..7b2834cc --- /dev/null +++ b/settings.gradle.kts @@ -0,0 +1,21 @@ +dependencyResolutionManagement { + repositories { + mavenCentral() + } + + versionCatalogs { + create("awssdk") { + from("aws.sdk.kotlin:version-catalog:1.6.41") + } + + create("ktorLibs") { + from("io.ktor:ktor-version-catalog:3.3.0") + } + } +} + +plugins { + id("org.gradle.toolchains.foojay-resolver-convention") version "1.0.0" +} + +rootProject.name = "mtusi-messenger-lab4" diff --git a/src/main/kotlin/com/mtusi/messenger/main/App.kt b/src/main/kotlin/com/mtusi/messenger/main/App.kt new file mode 100644 index 00000000..d2214cd9 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/App.kt @@ -0,0 +1,50 @@ +package com.mtusi.messenger.main + +import com.mtusi.messenger.main.plugins.authentication.configureAuthentication +import com.mtusi.messenger.main.plugins.configureAttachmentCleanup +import com.mtusi.messenger.main.plugins.configureCallLogging +import com.mtusi.messenger.main.plugins.configureContentNegotiation +import com.mtusi.messenger.main.plugins.configureCors +import com.mtusi.messenger.main.plugins.configureDatabase +import com.mtusi.messenger.main.plugins.configureDefaultHeaders +import com.mtusi.messenger.main.plugins.configureGracefulShutdown +import com.mtusi.messenger.main.plugins.configureInstanceId +import com.mtusi.messenger.main.plugins.configureKoin +import com.mtusi.messenger.main.plugins.configureRedis +import com.mtusi.messenger.main.plugins.configureRequestId +import com.mtusi.messenger.main.plugins.configureSessionRevocationSync +import com.mtusi.messenger.main.plugins.configureRouting +import com.mtusi.messenger.main.plugins.configureStatusPages +import com.mtusi.messenger.main.plugins.migrateDatabase +import io.ktor.server.application.Application +import io.ktor.server.netty.EngineMain + +fun main(args: Array) { + // `migrate` applies database migrations and exits; the API replicas start with migrations already in place. + if (args.firstOrNull() == "migrate") { + return migrateDatabase() + } + EngineMain.main(args) +} + +fun Application.module() { + + configureKoin() + configureDatabase() + configureRedis() + configureGracefulShutdown() + configureSessionRevocationSync() + configureAttachmentCleanup() + + configureContentNegotiation() + configureDefaultHeaders() + configureInstanceId() + configureCors() + configureRequestId() + configureCallLogging() + configureStatusPages() + + configureAuthentication() + configureRouting() + +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/compat/serializers/HttpMethodSerializer.kt b/src/main/kotlin/com/mtusi/messenger/main/compat/serializers/HttpMethodSerializer.kt new file mode 100644 index 00000000..59bc7bd8 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/compat/serializers/HttpMethodSerializer.kt @@ -0,0 +1,31 @@ +package com.mtusi.messenger.main.compat.serializers + +import io.ktor.http.HttpMethod +import kotlinx.serialization.KSerializer +import kotlinx.serialization.Serializable +import kotlinx.serialization.descriptors.PrimitiveKind +import kotlinx.serialization.descriptors.PrimitiveSerialDescriptor +import kotlinx.serialization.encoding.Decoder +import kotlinx.serialization.encoding.Encoder + +typealias SerialHttpMethod = @Serializable(HttpMethodSerializer::class) HttpMethod + +object HttpMethodSerializer : KSerializer { + + override val descriptor = PrimitiveSerialDescriptor( + serialName = "HttpMethod", + kind = PrimitiveKind.STRING + ) + + override fun serialize( + encoder: Encoder, + value: HttpMethod + ) { + encoder.encodeString(value.value) + } + + override fun deserialize(decoder: Decoder): HttpMethod { + return HttpMethod.parse(decoder.decodeString()) + } + +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/compat/serializers/UUIDSerializer.kt b/src/main/kotlin/com/mtusi/messenger/main/compat/serializers/UUIDSerializer.kt new file mode 100644 index 00000000..5a1ccff8 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/compat/serializers/UUIDSerializer.kt @@ -0,0 +1,31 @@ +package com.mtusi.messenger.main.compat.serializers + +import kotlinx.serialization.KSerializer +import kotlinx.serialization.Serializable +import kotlinx.serialization.descriptors.PrimitiveKind +import kotlinx.serialization.descriptors.PrimitiveSerialDescriptor +import kotlinx.serialization.encoding.Decoder +import kotlinx.serialization.encoding.Encoder +import java.util.UUID + +typealias SerialUUID = @Serializable(UUIDSerializer::class) UUID + +object UUIDSerializer : KSerializer { + + override val descriptor = PrimitiveSerialDescriptor( + serialName = "UUID", + kind = PrimitiveKind.STRING + ) + + override fun deserialize(decoder: Decoder): UUID { + return decoder.decodeString().run(UUID::fromString) + } + + override fun serialize( + encoder: Encoder, + value: UUID + ) { + encoder.encodeString(value.toString()) + } + +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/config/ConfigModule.kt b/src/main/kotlin/com/mtusi/messenger/main/config/ConfigModule.kt new file mode 100644 index 00000000..e4763008 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/config/ConfigModule.kt @@ -0,0 +1,8 @@ +package com.mtusi.messenger.main.config + +import org.koin.core.annotation.ComponentScan +import org.koin.core.annotation.Module + +@Module(createdAtStart = true) +@ComponentScan +class ConfigModule diff --git a/src/main/kotlin/com/mtusi/messenger/main/config/authentication/AuthenticationConfig.kt b/src/main/kotlin/com/mtusi/messenger/main/config/authentication/AuthenticationConfig.kt new file mode 100644 index 00000000..ce78889c --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/config/authentication/AuthenticationConfig.kt @@ -0,0 +1,15 @@ +package com.mtusi.messenger.main.config.authentication + +interface AuthenticationConfig { + + val realm: String + val sessionTtlSeconds: Long + val sessionCacheTtlSeconds: Long + val sessionCacheSingleFlight: Boolean + + /** + * How long a logout keeps trying to write its tombstone to Redis before it answers 503 instead of 204. + */ + val sessionRevokeTimeoutMs: Long + +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/config/authentication/DefaultAuthenticationConfig.kt b/src/main/kotlin/com/mtusi/messenger/main/config/authentication/DefaultAuthenticationConfig.kt new file mode 100644 index 00000000..7be2ab3f --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/config/authentication/DefaultAuthenticationConfig.kt @@ -0,0 +1,11 @@ +package com.mtusi.messenger.main.config.authentication + +import io.ktor.server.application.Application +import io.ktor.server.config.getAs +import org.koin.core.annotation.Provided +import org.koin.core.annotation.Single + +@Single +class DefaultAuthenticationConfig( + @Provided app: Application +) : AuthenticationConfig by app.environment.config.property("authentication").getAs() diff --git a/src/main/kotlin/com/mtusi/messenger/main/config/authentication/SerialAuthenticationConfig.kt b/src/main/kotlin/com/mtusi/messenger/main/config/authentication/SerialAuthenticationConfig.kt new file mode 100644 index 00000000..7ba9c360 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/config/authentication/SerialAuthenticationConfig.kt @@ -0,0 +1,12 @@ +package com.mtusi.messenger.main.config.authentication + +import kotlinx.serialization.Serializable + +@Serializable +data class SerialAuthenticationConfig( + override val realm: String, + override val sessionTtlSeconds: Long, + override val sessionCacheTtlSeconds: Long, + override val sessionCacheSingleFlight: Boolean = true, + override val sessionRevokeTimeoutMs: Long = 8000 +) : AuthenticationConfig diff --git a/src/main/kotlin/com/mtusi/messenger/main/config/cors/CorsConfig.kt b/src/main/kotlin/com/mtusi/messenger/main/config/cors/CorsConfig.kt new file mode 100644 index 00000000..a6aa0185 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/config/cors/CorsConfig.kt @@ -0,0 +1,100 @@ +package com.mtusi.messenger.main.config.cors + +import com.mtusi.messenger.main.compat.serializers.SerialHttpMethod +import io.ktor.http.HttpMethod +import kotlinx.serialization.Serializable +import io.ktor.server.plugins.cors.CORSConfig as CorsPluginConfig + +interface CorsConfig { + + /** + * Whether any host should be allowed + * + * Must default to false + */ + val allowAnyHost: Boolean + val allowedHosts: List + + /** + * Whether any [HttpMethod][io.ktor.http.HttpMethod] should be allowed + * + * Must default to true + */ + val allowAnyMethod: Boolean + val allowedMethods: List + + /** + * Whether any header should be allowed + * + * Must default to true + */ + val allowAnyHeader: Boolean + val allowedHeaders: List + + /** + * Response headers a browser page may read, e.g. X-Request-Id and X-Instance-Id in Swagger UI + */ + val exposedHeaders: List + + /** + * Whether requests from the API's own origin should be allowed + * + * Must default to false + */ + val allowSameOrigin: Boolean + + /** + * Whether credentials should be allowed + * + * Must default to true + */ + val allowCredentials: Boolean + + /** + * Whether non-simple content types should be allowed + * + * Must default to true + * + * @see [io.ktor.server.plugins.cors.CORSConfig.allowNonSimpleContentTypes] + */ + val allowNonSimpleContentTypes: Boolean + + @Serializable + data class AllowedHost( + val host: String, + val schemes: List + ) + +} + +fun CorsPluginConfig.configure(config: CorsConfig) { + + if (config.allowAnyHost) { + anyHost() + } else { + + config.allowedHosts.forEach { (host, schemes) -> + allowHost(host, schemes) + } + + } + + if (config.allowAnyMethod) { + HttpMethod.DefaultMethods.forEach(::allowMethod) + } else { + config.allowedMethods.forEach(::allowMethod) + } + + if (config.allowAnyHeader) { + allowHeaders { true } + } else { + config.allowedHeaders.forEach(::allowHeader) + } + + config.exposedHeaders.forEach(::exposeHeader) + + allowSameOrigin = config.allowSameOrigin + allowCredentials = config.allowCredentials + allowNonSimpleContentTypes = config.allowNonSimpleContentTypes + +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/config/cors/DefaultCorsConfig.kt b/src/main/kotlin/com/mtusi/messenger/main/config/cors/DefaultCorsConfig.kt new file mode 100644 index 00000000..5dab8651 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/config/cors/DefaultCorsConfig.kt @@ -0,0 +1,11 @@ +package com.mtusi.messenger.main.config.cors + +import io.ktor.server.application.Application +import io.ktor.server.config.getAs +import org.koin.core.annotation.Provided +import org.koin.core.annotation.Single + +@Single +class DefaultCorsConfig( + @Provided app: Application +) : CorsConfig by app.environment.config.property("cors").getAs() diff --git a/src/main/kotlin/com/mtusi/messenger/main/config/cors/SerialCorsConfig.kt b/src/main/kotlin/com/mtusi/messenger/main/config/cors/SerialCorsConfig.kt new file mode 100644 index 00000000..93918a29 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/config/cors/SerialCorsConfig.kt @@ -0,0 +1,18 @@ +package com.mtusi.messenger.main.config.cors + +import com.mtusi.messenger.main.compat.serializers.SerialHttpMethod +import kotlinx.serialization.Serializable + +@Serializable +data class SerialCorsConfig( + override val allowAnyHost: Boolean = false, + override val allowedHosts: List = emptyList(), + override val allowAnyMethod: Boolean = true, + override val allowedMethods: List = emptyList(), + override val allowAnyHeader: Boolean = true, + override val allowedHeaders: List = emptyList(), + override val exposedHeaders: List = emptyList(), + override val allowSameOrigin: Boolean = false, + override val allowCredentials: Boolean = true, + override val allowNonSimpleContentTypes: Boolean = true +) : CorsConfig diff --git a/src/main/kotlin/com/mtusi/messenger/main/config/database/DatabaseConfig.kt b/src/main/kotlin/com/mtusi/messenger/main/config/database/DatabaseConfig.kt new file mode 100644 index 00000000..1a971655 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/config/database/DatabaseConfig.kt @@ -0,0 +1,22 @@ +package com.mtusi.messenger.main.config.database + +interface DatabaseConfig { + + val host: String + val port: Int + val name: String + + val username: String + val password: String? + val passwordFile: String? + + val maxPoolSize: Int + val connectionTimeoutMs: Long + val statementTimeoutMs: Long + + val migrateOnStart: Boolean + + val jdbcConnectionUrl: String + get() = "jdbc:postgresql://$host:$port/$name" + +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/config/database/DefaultDatabaseConfig.kt b/src/main/kotlin/com/mtusi/messenger/main/config/database/DefaultDatabaseConfig.kt new file mode 100644 index 00000000..72a6c044 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/config/database/DefaultDatabaseConfig.kt @@ -0,0 +1,11 @@ +package com.mtusi.messenger.main.config.database + +import io.ktor.server.application.Application +import io.ktor.server.config.getAs +import org.koin.core.annotation.Provided +import org.koin.core.annotation.Single + +@Single +class DefaultDatabaseConfig( + @Provided app: Application +) : DatabaseConfig by app.environment.config.property("database").getAs() diff --git a/src/main/kotlin/com/mtusi/messenger/main/config/database/SerialDatabaseConfig.kt b/src/main/kotlin/com/mtusi/messenger/main/config/database/SerialDatabaseConfig.kt new file mode 100644 index 00000000..1a2ebc92 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/config/database/SerialDatabaseConfig.kt @@ -0,0 +1,17 @@ +package com.mtusi.messenger.main.config.database + +import kotlinx.serialization.Serializable + +@Serializable +data class SerialDatabaseConfig( + override val host: String, + override val port: Int, + override val name: String, + override val username: String, + override val password: String? = null, + override val passwordFile: String? = null, + override val maxPoolSize: Int, + override val connectionTimeoutMs: Long, + override val statementTimeoutMs: Long, + override val migrateOnStart: Boolean = true +) : DatabaseConfig diff --git a/src/main/kotlin/com/mtusi/messenger/main/config/message/DefaultMessageConfig.kt b/src/main/kotlin/com/mtusi/messenger/main/config/message/DefaultMessageConfig.kt new file mode 100644 index 00000000..8a62d41d --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/config/message/DefaultMessageConfig.kt @@ -0,0 +1,11 @@ +package com.mtusi.messenger.main.config.message + +import io.ktor.server.application.Application +import io.ktor.server.config.getAs +import org.koin.core.annotation.Provided +import org.koin.core.annotation.Single + +@Single +class DefaultMessageConfig( + @Provided app: Application +) : MessageConfig by app.environment.config.propertyOrNull("messages")?.getAs() ?: SerialMessageConfig() diff --git a/src/main/kotlin/com/mtusi/messenger/main/config/message/MessageConfig.kt b/src/main/kotlin/com/mtusi/messenger/main/config/message/MessageConfig.kt new file mode 100644 index 00000000..ccf1808f --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/config/message/MessageConfig.kt @@ -0,0 +1,5 @@ +package com.mtusi.messenger.main.config.message + +interface MessageConfig { + val idempotencyTtlSeconds: Long +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/config/message/SerialMessageConfig.kt b/src/main/kotlin/com/mtusi/messenger/main/config/message/SerialMessageConfig.kt new file mode 100644 index 00000000..6c0752e7 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/config/message/SerialMessageConfig.kt @@ -0,0 +1,8 @@ +package com.mtusi.messenger.main.config.message + +import kotlinx.serialization.Serializable + +@Serializable +data class SerialMessageConfig( + override val idempotencyTtlSeconds: Long = 86400 +) : MessageConfig diff --git a/src/main/kotlin/com/mtusi/messenger/main/config/redis/DefaultRedisConfig.kt b/src/main/kotlin/com/mtusi/messenger/main/config/redis/DefaultRedisConfig.kt new file mode 100644 index 00000000..9b7b168e --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/config/redis/DefaultRedisConfig.kt @@ -0,0 +1,11 @@ +package com.mtusi.messenger.main.config.redis + +import io.ktor.server.application.Application +import io.ktor.server.config.getAs +import org.koin.core.annotation.Provided +import org.koin.core.annotation.Single + +@Single +class DefaultRedisConfig( + @Provided app: Application +) : RedisConfig by app.environment.config.property("redis").getAs() diff --git a/src/main/kotlin/com/mtusi/messenger/main/config/redis/RedisConfig.kt b/src/main/kotlin/com/mtusi/messenger/main/config/redis/RedisConfig.kt new file mode 100644 index 00000000..4393403d --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/config/redis/RedisConfig.kt @@ -0,0 +1,15 @@ +package com.mtusi.messenger.main.config.redis + +interface RedisConfig { + + val host: String + val port: Int + + val username: String? + val password: String? + val passwordFile: String? + + val connectTimeoutMs: Long + val commandTimeoutMs: Long + +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/config/redis/SerialRedisConfig.kt b/src/main/kotlin/com/mtusi/messenger/main/config/redis/SerialRedisConfig.kt new file mode 100644 index 00000000..fddb8ed2 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/config/redis/SerialRedisConfig.kt @@ -0,0 +1,14 @@ +package com.mtusi.messenger.main.config.redis + +import kotlinx.serialization.Serializable + +@Serializable +data class SerialRedisConfig( + override val host: String, + override val port: Int, + override val username: String? = null, + override val password: String? = null, + override val passwordFile: String? = null, + override val connectTimeoutMs: Long, + override val commandTimeoutMs: Long +) : RedisConfig diff --git a/src/main/kotlin/com/mtusi/messenger/main/config/storage/DefaultStorageConfig.kt b/src/main/kotlin/com/mtusi/messenger/main/config/storage/DefaultStorageConfig.kt new file mode 100644 index 00000000..f3fd2456 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/config/storage/DefaultStorageConfig.kt @@ -0,0 +1,12 @@ +package com.mtusi.messenger.main.config.storage + +import io.ktor.server.application.Application +import io.ktor.server.config.getAs +import org.koin.core.annotation.Provided +import org.koin.core.annotation.Single + +@Single +class DefaultStorageConfig( + @Provided app: Application +) : StorageConfig by + (app.environment.config.propertyOrNull("storage")?.getAs() ?: SerialStorageConfig()) diff --git a/src/main/kotlin/com/mtusi/messenger/main/config/storage/SerialStorageConfig.kt b/src/main/kotlin/com/mtusi/messenger/main/config/storage/SerialStorageConfig.kt new file mode 100644 index 00000000..93c3a1ab --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/config/storage/SerialStorageConfig.kt @@ -0,0 +1,13 @@ +package com.mtusi.messenger.main.config.storage + +import kotlinx.serialization.Serializable + +@Serializable +data class SerialStorageConfig( + override val endpoint: String = "http://localhost:9000", + override val bucket: String = "attachments", + override val accessKey: String? = null, + override val secretKey: String? = null, + override val accessKeyFile: String? = null, + override val secretKeyFile: String? = null, +) : StorageConfig diff --git a/src/main/kotlin/com/mtusi/messenger/main/config/storage/StorageConfig.kt b/src/main/kotlin/com/mtusi/messenger/main/config/storage/StorageConfig.kt new file mode 100644 index 00000000..c79f9746 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/config/storage/StorageConfig.kt @@ -0,0 +1,10 @@ +package com.mtusi.messenger.main.config.storage + +interface StorageConfig { + val endpoint: String + val bucket: String + val accessKey: String? + val secretKey: String? + val accessKeyFile: String? + val secretKeyFile: String? +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/extensions/ApplicationCall.kt b/src/main/kotlin/com/mtusi/messenger/main/extensions/ApplicationCall.kt new file mode 100644 index 00000000..0fc40818 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/extensions/ApplicationCall.kt @@ -0,0 +1,37 @@ +package com.mtusi.messenger.main.extensions + +import com.mtusi.messenger.main.infrastructure.types.AuthChallenge +import com.mtusi.messenger.main.infrastructure.types.ErrorCode +import com.mtusi.messenger.main.plugins.RequestIdKey +import com.mtusi.messenger.main.plugins.authentication.UserPrincipal +import io.ktor.http.HttpHeaders +import io.ktor.server.application.ApplicationCall +import io.ktor.server.auth.principal +import io.ktor.server.response.header +import io.ktor.server.response.respond +import kotlinx.serialization.Serializable + +@Serializable +data class ErrorBody(val error: ErrorDetails) + +@Serializable +data class ErrorDetails( + val code: String, + val message: String, + val requestId: String, +) + +suspend fun ApplicationCall.respondError( + code: ErrorCode, + message: String, + challenge: String = AuthChallenge.BEARER, +) { + val requestId = attributes[RequestIdKey] + if (code == ErrorCode.UNAUTHORIZED) { + response.header(HttpHeaders.WWWAuthenticate, challenge) + } + respond(code.status, ErrorBody(ErrorDetails(code.wire, message, requestId))) +} + +val ApplicationCall.currentUser: UserPrincipal + get() = principal()!! diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/controllers/_common/parameters/PageParameters.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/controllers/_common/parameters/PageParameters.kt new file mode 100644 index 00000000..043b686e --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/controllers/_common/parameters/PageParameters.kt @@ -0,0 +1,31 @@ +package com.mtusi.messenger.main.infrastructure.controllers._common.parameters + +import com.mtusi.messenger.main.infrastructure.types.ApiException +import com.mtusi.messenger.main.infrastructure.types.ErrorCode +import io.ktor.server.application.ApplicationCall + +private const val DEFAULT_LIMIT = 50 +private const val MAX_LIMIT = 100 + +data class PageParameters( + val limit: Int, + val cursor: String? +) + +fun ApplicationCall.pageParameters(): PageParameters { + + val query = request.queryParameters + + val limit = query["limit"] + ?.let { it.toIntOrNull() ?: throw ApiException(ErrorCode.INVALID_REQUEST, "invalid limit") } + ?: DEFAULT_LIMIT + + if (limit !in 1..MAX_LIMIT) { + throw ApiException(ErrorCode.INVALID_REQUEST, "limit out of range") + } + + val cursor = query["cursor"] + ?.takeIf { it.isNotEmpty() } + + return PageParameters(limit, cursor) +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/controllers/attachment/AttachmentController.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/controllers/attachment/AttachmentController.kt new file mode 100644 index 00000000..6ae1f5a8 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/controllers/attachment/AttachmentController.kt @@ -0,0 +1,106 @@ +package com.mtusi.messenger.main.infrastructure.controllers.attachment + +import com.mtusi.messenger.main.extensions.currentUser +import com.mtusi.messenger.main.infrastructure.services.attachment.AttachmentService +import com.mtusi.messenger.main.infrastructure.services.attachment.MAX_ATTACHMENT_BYTES +import com.mtusi.messenger.main.infrastructure.types.ApiException +import com.mtusi.messenger.main.infrastructure.types.ErrorCode +import com.mtusi.messenger.main.plugins.authentication.SESSION_AUTH +import com.mtusi.messenger.main.utils.requireUuid +import io.ktor.http.* +import io.ktor.server.auth.authenticate +import io.ktor.server.request.* +import io.ktor.server.response.* +import io.ktor.server.routing.* +import io.ktor.utils.io.readAvailable +import kotlinx.coroutines.withTimeoutOrNull +import org.koin.ktor.ext.inject +import com.mtusi.messenger.main.utils.withTemporaryUpload +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.withContext +import kotlinx.coroutines.sync.Semaphore +import kotlinx.coroutines.sync.withPermit +import kotlin.time.Duration.Companion.milliseconds + +fun Routing.configureAttachmentRouting() { + + val service by inject() + val uploads = Semaphore(1) + + authenticate(SESSION_AUTH) { + + post("/api/v1/chats/{chat_id}/attachments") { + + val actor = call.currentUser + val chatId = requireUuid(call.parameters["chat_id"], "chat_id") + + service.requireMember(chatId, actor.id) + + val filename = call.request.queryParameters["filename"] + ?: throw ApiException(ErrorCode.INVALID_REQUEST, "filename is required") + + if (filename.isBlank() || filename.codePointCount(0, filename.length) !in 1..255 || filename.any { it == '/' || it == '\\' || it.isISOControl() }) { + throw ApiException(ErrorCode.INVALID_REQUEST, "invalid filename") + } + + val type = try { + call.request.contentType().withoutParameters().toString().lowercase() + } catch (_: BadContentTypeFormatException) { + throw ApiException(ErrorCode.INVALID_REQUEST, "invalid Content-Type") + } + if (type !in setOf("image/png", "image/jpeg")) { + throw ApiException(ErrorCode.UNSUPPORTED_MEDIA_TYPE, "expected PNG or JPEG") + } + + if ((call.request.contentLength() ?: 0) > MAX_ATTACHMENT_BYTES) { + throw ApiException(ErrorCode.PAYLOAD_TOO_LARGE, "file exceeds 10 MiB") + } + + uploads.withPermit { + withTemporaryUpload { file -> + withTimeoutOrNull(15_000.milliseconds) { + withContext(Dispatchers.IO) { + val input = call.receiveChannel() + val buffer = ByteArray(8192) + var size = 0L + file.outputStream().use { output -> + while (true) { + val count = input.readAvailable(buffer) + if (count == -1) break + size += count + if (size > MAX_ATTACHMENT_BYTES) { + throw ApiException(ErrorCode.PAYLOAD_TOO_LARGE, "file exceeds 10 MiB") + } + output.write(buffer, 0, count) + } + } + } + } ?: throw ApiException(ErrorCode.INVALID_REQUEST, "upload timed out") + + val attachment = service.upload(chatId, actor.id, filename, type, file) + call.respond(HttpStatusCode.Created, attachment) + } + } + + } + + get("/api/v1/attachments/{id}") { + call.respond(service.find(requireUuid(call.parameters["id"], "id"), call.currentUser.id)) + } + + get("/api/v1/attachments/{id}/content") { + + val attachment = service.find(requireUuid(call.parameters["id"], "id"), call.currentUser.id) + + if ((call.request.queryParameters["variant"] ?: "original") != "original") { + throw ApiException(ErrorCode.NOT_FOUND, "variant not found") + } + + call.response.header(HttpHeaders.CacheControl, "private, no-store") + call.response.header("X-Content-Type-Options", "nosniff") + + call.respondBytes(service.content(attachment), ContentType.parse(attachment.contentType)) + } + + } +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/controllers/authentication/AuthenticationController.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/controllers/authentication/AuthenticationController.kt new file mode 100644 index 00000000..757f629b --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/controllers/authentication/AuthenticationController.kt @@ -0,0 +1,53 @@ +package com.mtusi.messenger.main.infrastructure.controllers.authentication + +import com.mtusi.messenger.main.extensions.currentUser +import com.mtusi.messenger.main.infrastructure.controllers.authentication.responses.SessionResponse +import com.mtusi.messenger.main.infrastructure.services.authentication.AuthenticationService +import com.mtusi.messenger.main.plugins.authentication.CREDENTIALS_AUTH +import com.mtusi.messenger.main.plugins.authentication.CredentialsPrincipal +import com.mtusi.messenger.main.plugins.authentication.SESSION_AUTH +import com.mtusi.messenger.main.plugins.suspendedTransaction +import io.ktor.http.HttpStatusCode +import io.ktor.server.auth.authenticate +import io.ktor.server.auth.principal +import io.ktor.server.response.respond +import io.ktor.server.routing.Routing +import io.ktor.server.routing.delete +import io.ktor.server.routing.post +import io.ktor.server.routing.route +import org.koin.ktor.ext.inject + +fun Routing.configureAuthenticationRouting() = route("/api/v1/auth/sessions") { + + val authenticationService by inject() + + authenticate(CREDENTIALS_AUTH) { + post { + + val (user) = call.principal()!! + + val issued = suspendedTransaction { + authenticationService.createSession(user.id) + } + + call.respond( + HttpStatusCode.Created, + SessionResponse( + token = issued.token, + expiresAt = issued.session.expiresAt, + user = user + ) + ) + } + } + + authenticate(SESSION_AUTH) { + delete("current") { + + authenticationService.revokeSession(call.currentUser.session) + + call.respond(HttpStatusCode.NoContent) + } + } + +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/controllers/authentication/responses/SessionResponse.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/controllers/authentication/responses/SessionResponse.kt new file mode 100644 index 00000000..f9c4e343 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/controllers/authentication/responses/SessionResponse.kt @@ -0,0 +1,13 @@ +package com.mtusi.messenger.main.infrastructure.controllers.authentication.responses + +import com.mtusi.messenger.main.infrastructure.dto.User +import kotlinx.serialization.Serializable +import kotlin.time.Instant + +@Serializable +data class SessionResponse( + val token: String, + val tokenType: String = "Bearer", + val expiresAt: Instant, + val user: User, +) diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/controllers/chat/ChatController.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/controllers/chat/ChatController.kt new file mode 100644 index 00000000..e2fd839a --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/controllers/chat/ChatController.kt @@ -0,0 +1,112 @@ +package com.mtusi.messenger.main.infrastructure.controllers.chat + +import com.mtusi.messenger.main.extensions.currentUser +import com.mtusi.messenger.main.extensions.respondError +import com.mtusi.messenger.main.infrastructure.controllers._common.parameters.pageParameters +import com.mtusi.messenger.main.infrastructure.controllers.chat.requests.CreateChatRequest +import com.mtusi.messenger.main.infrastructure.models.chat.MAX_CHAT_MEMBERS +import com.mtusi.messenger.main.infrastructure.models.chat.MAX_CHAT_TITLE_LENGTH +import com.mtusi.messenger.main.infrastructure.services.chat.ChatService +import com.mtusi.messenger.main.infrastructure.services.chat.results.ChatCreationResult +import com.mtusi.messenger.main.infrastructure.services.chat.results.ChatPageResult +import com.mtusi.messenger.main.infrastructure.types.ApiException +import com.mtusi.messenger.main.infrastructure.types.ErrorCode +import com.mtusi.messenger.main.plugins.authentication.SESSION_AUTH +import com.mtusi.messenger.main.plugins.suspendedTransaction +import com.mtusi.messenger.main.utils.requireNonBlankBounded +import com.mtusi.messenger.main.utils.requireUuid +import io.ktor.http.HttpStatusCode +import io.ktor.server.auth.authenticate +import io.ktor.server.request.receive +import io.ktor.server.response.respond +import io.ktor.server.routing.Routing +import io.ktor.server.routing.get +import io.ktor.server.routing.post +import io.ktor.server.routing.route +import org.koin.ktor.ext.inject + +fun Routing.configureChatRouting() = route("/api/v1/chats") { + + val chatService by inject() + + authenticate(SESSION_AUTH) { + + post { + + val actor = call.currentUser + val body = call.receive() + + requireNonBlankBounded( + body.title, + min = 1, + max = MAX_CHAT_TITLE_LENGTH, + field = "title" + ) + + if (body.memberIds.isEmpty() || body.memberIds.size > MAX_CHAT_MEMBERS) { + throw ApiException(ErrorCode.INVALID_REQUEST, "member_ids size out of range") + } + + val memberIds = body.memberIds.map { requireUuid(it, "member_ids") } + + val result = suspendedTransaction { + chatService.create( + creatorId = actor.id, + title = body.title, + memberIds = memberIds + ) + } + + when (result) { + is ChatCreationResult.DuplicateMembers -> + call.respondError(ErrorCode.INVALID_REQUEST, "member_ids must be unique") + is ChatCreationResult.CreatorNotMember -> + call.respondError(ErrorCode.INVALID_REQUEST, "creator must be in member ids") + is ChatCreationResult.UnknownMembers -> + call.respondError(ErrorCode.INVALID_REQUEST, "unknown member") + is ChatCreationResult.Success -> + call.respond(HttpStatusCode.Created, result.chat) + } + } + + get { + + val actor = call.currentUser + val (limit, cursor) = call.pageParameters() + + val result = suspendedTransaction { + chatService.findPageAccessible( + userId = actor.id, + limit = limit, + cursor = cursor + ) + } + + when (result) { + is ChatPageResult.InvalidCursor -> + call.respondError(ErrorCode.INVALID_REQUEST, "invalid cursor") + is ChatPageResult.Success -> + call.respond(HttpStatusCode.OK, result.page) + } + } + + get("{chat_id}") { + + val actor = call.currentUser + val chatId = requireUuid(call.parameters["chat_id"], "chat_id") + + val chat = suspendedTransaction { + chatService.findAccessible(actor.id, chatId) + } + + if (chat == null) { + call.respondError(ErrorCode.NOT_FOUND, "chat not found") + return@get + } + + call.respond(HttpStatusCode.OK, chat) + } + + } + +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/controllers/chat/requests/CreateChatRequest.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/controllers/chat/requests/CreateChatRequest.kt new file mode 100644 index 00000000..d0505b9d --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/controllers/chat/requests/CreateChatRequest.kt @@ -0,0 +1,9 @@ +package com.mtusi.messenger.main.infrastructure.controllers.chat.requests + +import kotlinx.serialization.Serializable + +@Serializable +data class CreateChatRequest( + val title: String, + val memberIds: List, +) diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/controllers/health/HealthController.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/controllers/health/HealthController.kt new file mode 100644 index 00000000..d17fd116 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/controllers/health/HealthController.kt @@ -0,0 +1,67 @@ +package com.mtusi.messenger.main.infrastructure.controllers.health + +import com.mtusi.messenger.main.extensions.respondError +import com.mtusi.messenger.main.infrastructure.controllers.health.responses.HealthResponse +import com.mtusi.messenger.main.infrastructure.types.ErrorCode +import com.mtusi.messenger.main.plugins.isDraining +import com.mtusi.messenger.main.plugins.suspendedTransaction +import com.mtusi.messenger.main.infrastructure.repositories.attachment.storage.ObjectStorageRepository +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.withContext +import org.koin.ktor.ext.inject +import io.ktor.http.HttpStatusCode +import io.ktor.server.application.log +import io.ktor.server.response.respond +import io.ktor.server.routing.Routing +import io.ktor.server.routing.get +import io.ktor.server.routing.route +import kotlinx.coroutines.CancellationException +import kotlinx.coroutines.currentCoroutineContext +import kotlinx.coroutines.ensureActive + +fun Routing.configureHealthRouting() = route("/health") { + + get("live") { + call.respond(HttpStatusCode.OK, HealthResponse()) + } + + val storage by inject() + + // PostgreSQL and S3 are dependencies of the synchronous path; Redis has a DB fallback. + get("ready") { + + // SIGTERM received: leave the balancer's rotation while requests in flight are still being finished. + if (call.application.isDraining) { + call.respondError(ErrorCode.UNAVAILABLE, "shutting down") + return@get + } + + val isDatabaseReachable = try { + suspendedTransaction { + exec("SELECT 1") + } + true + } catch (cause: Exception) { + + if (cause is CancellationException) { + currentCoroutineContext().ensureActive() + } + + call.application.log.warn("Readiness check failed: ${cause.message}") + false + } + + if (!isDatabaseReachable) { + call.respondError(ErrorCode.UNAVAILABLE, "database unavailable") + return@get + } + + if (!withContext(Dispatchers.IO) { storage.ready() }) { + call.respondError(ErrorCode.UNAVAILABLE, "object storage unavailable") + return@get + } + + call.respond(HttpStatusCode.OK, HealthResponse()) + } + +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/controllers/health/responses/HealthResponse.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/controllers/health/responses/HealthResponse.kt new file mode 100644 index 00000000..a6b56097 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/controllers/health/responses/HealthResponse.kt @@ -0,0 +1,6 @@ +package com.mtusi.messenger.main.infrastructure.controllers.health.responses + +import kotlinx.serialization.Serializable + +@Serializable +data class HealthResponse(val status: String = "ok") diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/controllers/message/MessageController.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/controllers/message/MessageController.kt new file mode 100644 index 00000000..0fd89ada --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/controllers/message/MessageController.kt @@ -0,0 +1,94 @@ +package com.mtusi.messenger.main.infrastructure.controllers.message + +import com.mtusi.messenger.main.infrastructure.types.ApiException +import com.mtusi.messenger.main.extensions.currentUser +import com.mtusi.messenger.main.extensions.respondError +import com.mtusi.messenger.main.infrastructure.controllers._common.parameters.pageParameters +import com.mtusi.messenger.main.infrastructure.controllers.message.requests.CreateMessageRequest +import com.mtusi.messenger.main.infrastructure.models.message.MAX_MESSAGE_TEXT_LENGTH +import com.mtusi.messenger.main.infrastructure.services.message.MessageService +import com.mtusi.messenger.main.infrastructure.services.message.results.MessageCreationResult +import com.mtusi.messenger.main.infrastructure.services.message.results.MessagePageResult +import com.mtusi.messenger.main.infrastructure.types.ErrorCode +import com.mtusi.messenger.main.plugins.authentication.SESSION_AUTH +import com.mtusi.messenger.main.plugins.suspendedTransaction +import com.mtusi.messenger.main.utils.requireIdempotencyKey +import com.mtusi.messenger.main.utils.requireUuid +import io.ktor.http.HttpStatusCode +import io.ktor.server.auth.authenticate +import io.ktor.server.request.receive +import io.ktor.server.response.respond +import io.ktor.server.routing.Routing +import io.ktor.server.routing.get +import io.ktor.server.routing.post +import io.ktor.server.routing.route +import org.koin.ktor.ext.inject + +fun Routing.configureMessageRouting() = route("/api/v1/chats/{chat_id}/messages") { + + val messageService by inject() + + authenticate(SESSION_AUTH) { + + post { + + val actor = call.currentUser + val chatId = requireUuid(call.parameters["chat_id"], "chat_id") + val idempotencyKey = requireIdempotencyKey(call.request.headers["Idempotency-Key"]) + val body = call.receive() + + val attachmentIds = body.attachmentIds.map { requireUuid(it, "attachment_ids") } + if (attachmentIds.size > 10 || attachmentIds.distinct().size != attachmentIds.size || + body.text.codePointCount(0, body.text.length) > MAX_MESSAGE_TEXT_LENGTH || + (body.text.isBlank() && attachmentIds.isEmpty())) { + throw ApiException(ErrorCode.INVALID_REQUEST, "invalid message") + } + + val result = suspendedTransaction { + messageService.send( + chatId = chatId, + senderId = actor.id, + text = body.text, + idempotencyKey = idempotencyKey, + attachmentIds = attachmentIds + ) + } + + when (result) { + is MessageCreationResult.ChatNotFound -> + call.respondError(ErrorCode.NOT_FOUND, "chat not found") + is MessageCreationResult.IdempotencyKeyReused -> + call.respondError(ErrorCode.CONFLICT, "Idempotency-Key was used with a different message") + is MessageCreationResult.Success -> + call.respond(HttpStatusCode.Created, result.message) + } + } + + get { + + val actor = call.currentUser + val chatId = requireUuid(call.parameters["chat_id"], "chat_id") + val (limit, cursor) = call.pageParameters() + + val result = suspendedTransaction { + messageService.findPageByChatId( + chatId = chatId, + actorId = actor.id, + limit = limit, + cursor = cursor + ) + } + + when (result) { + is MessagePageResult.ChatNotFound -> + call.respondError(ErrorCode.NOT_FOUND, "chat not found") + is MessagePageResult.InvalidCursor -> + call.respondError(ErrorCode.INVALID_REQUEST, "invalid cursor") + is MessagePageResult.Success -> + call.respond(HttpStatusCode.OK, result.page) + } + } + + } + +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/controllers/message/requests/CreateMessageRequest.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/controllers/message/requests/CreateMessageRequest.kt new file mode 100644 index 00000000..417ec578 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/controllers/message/requests/CreateMessageRequest.kt @@ -0,0 +1,9 @@ +package com.mtusi.messenger.main.infrastructure.controllers.message.requests + +import kotlinx.serialization.Serializable + +@Serializable +data class CreateMessageRequest( + val text: String, + val attachmentIds: List = emptyList(), +) diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/controllers/user/UserController.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/controllers/user/UserController.kt new file mode 100644 index 00000000..d49bc9a9 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/controllers/user/UserController.kt @@ -0,0 +1,83 @@ +package com.mtusi.messenger.main.infrastructure.controllers.user + +import com.mtusi.messenger.main.extensions.currentUser +import com.mtusi.messenger.main.extensions.respondError +import com.mtusi.messenger.main.infrastructure.controllers.user.requests.CreateUserRequest +import com.mtusi.messenger.main.infrastructure.models.user.MAX_DISPLAY_NAME_LENGTH +import com.mtusi.messenger.main.infrastructure.services.user.UserService +import com.mtusi.messenger.main.infrastructure.services.user.results.UserCreationResult +import com.mtusi.messenger.main.infrastructure.types.ErrorCode +import com.mtusi.messenger.main.plugins.authentication.SESSION_AUTH +import com.mtusi.messenger.main.plugins.suspendedTransaction +import com.mtusi.messenger.main.utils.requireNonBlankBounded +import com.mtusi.messenger.main.utils.requireUsername +import io.ktor.http.HttpStatusCode +import io.ktor.server.auth.authenticate +import io.ktor.server.request.receive +import io.ktor.server.response.respond +import io.ktor.server.routing.Routing +import io.ktor.server.routing.get +import io.ktor.server.routing.post +import io.ktor.server.routing.route +import org.koin.ktor.ext.inject + +private const val PASSWORD_MIN = 12 +private const val PASSWORD_MAX = 128 + +fun Routing.configureUserRouting() = route("/api/v1/users") { + + val userService by inject() + + post { + + val body = call.receive() + + requireUsername(body.username) + + requireNonBlankBounded( + body.displayName, + min = 1, + max = MAX_DISPLAY_NAME_LENGTH, + field = "display_name" + ) + + requireNonBlankBounded( + body.password, + min = PASSWORD_MIN, + max = PASSWORD_MAX, + field = "password" + ) + + val result = suspendedTransaction { + userService.create( + username = body.username, + displayName = body.displayName, + password = body.password + ) + } + + when (result) { + is UserCreationResult.UsernameTaken -> + call.respondError(ErrorCode.CONFLICT, "username already taken") + is UserCreationResult.Success -> + call.respond(HttpStatusCode.Created, result.user) + } + } + + authenticate(SESSION_AUTH) { + get("me") { + + val user = suspendedTransaction { + userService.findById(call.currentUser.id) + } + + if (user == null) { + call.respondError(ErrorCode.UNAUTHORIZED, "unknown user") + return@get + } + + call.respond(HttpStatusCode.OK, user) + } + } + +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/controllers/user/requests/CreateUserRequest.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/controllers/user/requests/CreateUserRequest.kt new file mode 100644 index 00000000..0e7bb113 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/controllers/user/requests/CreateUserRequest.kt @@ -0,0 +1,10 @@ +package com.mtusi.messenger.main.infrastructure.controllers.user.requests + +import kotlinx.serialization.Serializable + +@Serializable +data class CreateUserRequest( + val username: String, + val displayName: String, + val password: String, +) diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/dto/Attachment.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/dto/Attachment.kt new file mode 100644 index 00000000..e65ac4cd --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/dto/Attachment.kt @@ -0,0 +1,21 @@ +package com.mtusi.messenger.main.infrastructure.dto + +import com.mtusi.messenger.main.compat.serializers.SerialUUID +import kotlinx.serialization.Serializable +import kotlin.time.Instant + +@Serializable +data class Attachment( + val id: SerialUUID, + val chatId: SerialUUID, + val ownerId: SerialUUID, + val filename: String, + val contentType: String, + val sizeBytes: Long, + val sha256: String, + val createdAt: Instant, + val updatedAt: Instant = createdAt, + val status: String = "ready", + val variants: List = emptyList(), + val errorCode: String? = null, +) diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/dto/Chat.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/dto/Chat.kt new file mode 100644 index 00000000..846d6c82 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/dto/Chat.kt @@ -0,0 +1,13 @@ +package com.mtusi.messenger.main.infrastructure.dto + +import com.mtusi.messenger.main.compat.serializers.SerialUUID +import kotlinx.serialization.Serializable +import kotlin.time.Instant + +@Serializable +data class Chat( + val id: SerialUUID, + val title: String, + val memberIds: List, + val createdAt: Instant, +) diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/dto/Message.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/dto/Message.kt new file mode 100644 index 00000000..01b2ced1 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/dto/Message.kt @@ -0,0 +1,15 @@ +package com.mtusi.messenger.main.infrastructure.dto + +import com.mtusi.messenger.main.compat.serializers.SerialUUID +import kotlinx.serialization.Serializable +import kotlin.time.Instant + +@Serializable +data class Message( + val id: SerialUUID, + val chatId: SerialUUID, + val senderId: SerialUUID, + val text: String, + val createdAt: Instant, + val attachmentIds: List = emptyList(), +) diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/dto/PageResponse.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/dto/PageResponse.kt new file mode 100644 index 00000000..28fa6dce --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/dto/PageResponse.kt @@ -0,0 +1,9 @@ +package com.mtusi.messenger.main.infrastructure.dto + +import kotlinx.serialization.Serializable + +@Serializable +data class PageResponse( + val items: List, + val nextCursor: String?, +) diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/dto/User.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/dto/User.kt new file mode 100644 index 00000000..4f768822 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/dto/User.kt @@ -0,0 +1,13 @@ +package com.mtusi.messenger.main.infrastructure.dto + +import com.mtusi.messenger.main.compat.serializers.SerialUUID +import kotlinx.serialization.Serializable +import kotlin.time.Instant + +@Serializable +data class User( + val id: SerialUUID, + val username: String, + val displayName: String, + val createdAt: Instant, +) diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/dto/authentication/IssuedSession.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/dto/authentication/IssuedSession.kt new file mode 100644 index 00000000..480607bf --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/dto/authentication/IssuedSession.kt @@ -0,0 +1,6 @@ +package com.mtusi.messenger.main.infrastructure.dto.authentication + +data class IssuedSession( + val session: Session, + val token: String, +) diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/dto/authentication/Session.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/dto/authentication/Session.kt new file mode 100644 index 00000000..a56336f7 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/dto/authentication/Session.kt @@ -0,0 +1,12 @@ +package com.mtusi.messenger.main.infrastructure.dto.authentication + +import java.util.UUID +import kotlin.time.Instant + +data class Session( + val id: UUID, + val userId: UUID, + val tokenHash: String, + val createdAt: Instant, + val expiresAt: Instant, +) diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/dto/authentication/SessionRevocation.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/dto/authentication/SessionRevocation.kt new file mode 100644 index 00000000..a02cf062 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/dto/authentication/SessionRevocation.kt @@ -0,0 +1,8 @@ +package com.mtusi.messenger.main.infrastructure.dto.authentication + +import kotlin.time.Instant + +data class SessionRevocation( + val tokenHash: String, + val expiresAt: Instant, +) diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/dto/authentication/UserCredentials.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/dto/authentication/UserCredentials.kt new file mode 100644 index 00000000..2533ff71 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/dto/authentication/UserCredentials.kt @@ -0,0 +1,8 @@ +package com.mtusi.messenger.main.infrastructure.dto.authentication + +import com.mtusi.messenger.main.infrastructure.dto.User + +data class UserCredentials( + val user: User, + val passwordHash: String, +) diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/models/attachment/AttachmentModel.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/models/attachment/AttachmentModel.kt new file mode 100644 index 00000000..2a26edd0 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/models/attachment/AttachmentModel.kt @@ -0,0 +1,16 @@ +package com.mtusi.messenger.main.infrastructure.models.attachment + +import com.mtusi.messenger.main.infrastructure.models.chat.ChatModel +import com.mtusi.messenger.main.infrastructure.models.user.UserModel +import org.jetbrains.exposed.v1.core.dao.id.java.UUIDTable +import org.jetbrains.exposed.v1.datetime.timestamp + +object AttachmentModel : UUIDTable("attachments") { + val chatId = reference("chat_id", ChatModel) + val ownerId = reference("owner_id", UserModel) + val filename = varchar("filename", 255) + val contentType = varchar("content_type", 32) + val sizeBytes = long("size_bytes") + val sha256 = varchar("sha256", 64) + val createdAt = timestamp("created_at") +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/models/attachment/MessageAttachmentModel.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/models/attachment/MessageAttachmentModel.kt new file mode 100644 index 00000000..507fe240 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/models/attachment/MessageAttachmentModel.kt @@ -0,0 +1,11 @@ +package com.mtusi.messenger.main.infrastructure.models.attachment + +import com.mtusi.messenger.main.infrastructure.models.message.MessageModel +import org.jetbrains.exposed.v1.core.Table + +object MessageAttachmentModel : Table("message_attachments") { + val messageId = reference("message_id", MessageModel) + val attachmentId = reference("attachment_id", AttachmentModel) + val position = integer("position") + override val primaryKey = PrimaryKey(messageId, attachmentId) +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/models/attachment/UploadJournalModel.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/models/attachment/UploadJournalModel.kt new file mode 100644 index 00000000..ae55f759 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/models/attachment/UploadJournalModel.kt @@ -0,0 +1,18 @@ +package com.mtusi.messenger.main.infrastructure.models.attachment + +import com.mtusi.messenger.main.utils.exposed.operations.plus +import org.jetbrains.exposed.v1.core.dao.id.java.UUIDTable +import org.jetbrains.exposed.v1.core.longLiteral +import org.jetbrains.exposed.v1.datetime.CurrentTimestamp +import org.jetbrains.exposed.v1.datetime.timestamp + +object UploadJournalModel : UUIDTable("attachment_uploads") { + + val expiresAt = timestamp("expires_at").defaultExpression(CurrentTimestamp + longLiteral(3600)) + val abandoned = bool("abandoned").default(false) + val nextAttemptAt = timestamp("next_attempt_at").defaultExpression(CurrentTimestamp + longLiteral(3600)) + + init { + index("attachment_uploads_next_attempt_idx", false, nextAttemptAt) + } +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/models/authentication/SessionModel.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/models/authentication/SessionModel.kt new file mode 100644 index 00000000..6b847e59 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/models/authentication/SessionModel.kt @@ -0,0 +1,17 @@ +package com.mtusi.messenger.main.infrastructure.models.authentication + +import com.mtusi.messenger.main.infrastructure.models.user.UserModel +import org.jetbrains.exposed.v1.core.ReferenceOption +import org.jetbrains.exposed.v1.core.dao.id.java.UUIDTable +import org.jetbrains.exposed.v1.datetime.timestamp + +object SessionModel : UUIDTable("sessions") { + val userId = reference("user_id", UserModel, onDelete = ReferenceOption.CASCADE, onUpdate = ReferenceOption.NO_ACTION, fkName = "sessions_user_id_fkey") + val tokenHash = varchar("token_hash", 64).uniqueIndex("sessions_token_hash_key") + val createdAt = timestamp("created_at") + val expiresAt = timestamp("expires_at") + + init { + index("sessions_user_id_idx", false, userId) + } +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/models/authentication/SessionRevocationModel.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/models/authentication/SessionRevocationModel.kt new file mode 100644 index 00000000..13e791dc --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/models/authentication/SessionRevocationModel.kt @@ -0,0 +1,11 @@ +package com.mtusi.messenger.main.infrastructure.models.authentication + +import org.jetbrains.exposed.v1.core.Table +import org.jetbrains.exposed.v1.datetime.timestamp + +object SessionRevocationModel : Table("session_revocations") { + val tokenHash = varchar("token_hash", 64) + val expiresAt = timestamp("expires_at") + + override val primaryKey = PrimaryKey(tokenHash, name = "session_revocations_pkey") +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/models/chat/ChatMemberModel.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/models/chat/ChatMemberModel.kt new file mode 100644 index 00000000..cd48d84d --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/models/chat/ChatMemberModel.kt @@ -0,0 +1,17 @@ +package com.mtusi.messenger.main.infrastructure.models.chat + +import com.mtusi.messenger.main.infrastructure.models.user.UserModel +import org.jetbrains.exposed.v1.core.ReferenceOption +import org.jetbrains.exposed.v1.core.Table + +object ChatMemberModel : Table("chat_members") { + val chatId = reference("chat_id", ChatModel, onDelete = ReferenceOption.CASCADE, onUpdate = ReferenceOption.NO_ACTION, fkName = "chat_members_chat_id_fkey") + val userId = reference("user_id", UserModel, onDelete = ReferenceOption.CASCADE, onUpdate = ReferenceOption.NO_ACTION, fkName = "chat_members_user_id_fkey") + val position = integer("position") + + override val primaryKey = PrimaryKey(chatId, userId) + + init { + index("chat_members_user_id_idx", false, userId) + } +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/models/chat/ChatModel.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/models/chat/ChatModel.kt new file mode 100644 index 00000000..984fbfd8 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/models/chat/ChatModel.kt @@ -0,0 +1,12 @@ +package com.mtusi.messenger.main.infrastructure.models.chat + +import org.jetbrains.exposed.v1.core.dao.id.java.UUIDTable +import org.jetbrains.exposed.v1.datetime.timestamp + +const val MAX_CHAT_TITLE_LENGTH = 100 +const val MAX_CHAT_MEMBERS = 100 + +object ChatModel : UUIDTable("chats") { + val title = varchar("title", MAX_CHAT_TITLE_LENGTH) + val createdAt = timestamp("created_at") +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/models/message/MessageIdempotencyKeyModel.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/models/message/MessageIdempotencyKeyModel.kt new file mode 100644 index 00000000..dd2698e2 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/models/message/MessageIdempotencyKeyModel.kt @@ -0,0 +1,25 @@ +package com.mtusi.messenger.main.infrastructure.models.message + +import com.mtusi.messenger.main.infrastructure.models.chat.ChatModel +import com.mtusi.messenger.main.infrastructure.models.user.UserModel +import org.jetbrains.exposed.v1.core.ReferenceOption +import org.jetbrains.exposed.v1.core.Table +import org.jetbrains.exposed.v1.datetime.timestamp + +const val MAX_IDEMPOTENCY_KEY_LENGTH = 128 + +object MessageIdempotencyKeyModel : Table("message_idempotency_keys") { + val userId = reference("user_id", UserModel, onDelete = ReferenceOption.CASCADE, onUpdate = ReferenceOption.NO_ACTION, fkName = "message_idempotency_keys_user_id_fkey") + val chatId = reference("chat_id", ChatModel, onDelete = ReferenceOption.CASCADE, onUpdate = ReferenceOption.NO_ACTION, fkName = "message_idempotency_keys_chat_id_fkey") + val key = varchar("key", MAX_IDEMPOTENCY_KEY_LENGTH) + val requestHash = varchar("request_hash", 64) + val messageId = optReference("message_id", MessageModel, onDelete = ReferenceOption.CASCADE, onUpdate = ReferenceOption.NO_ACTION, fkName = "message_idempotency_keys_message_id_fkey") + val createdAt = timestamp("created_at") + + override val primaryKey = PrimaryKey(userId, chatId, key, name = "message_idempotency_keys_pkey") + + init { + index("message_idempotency_keys_chat_id_idx", false, chatId) + index("message_idempotency_keys_message_id_idx", false, messageId) + } +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/models/message/MessageModel.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/models/message/MessageModel.kt new file mode 100644 index 00000000..301c59f6 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/models/message/MessageModel.kt @@ -0,0 +1,20 @@ +package com.mtusi.messenger.main.infrastructure.models.message + +import com.mtusi.messenger.main.infrastructure.models.chat.ChatModel +import com.mtusi.messenger.main.infrastructure.models.user.UserModel +import org.jetbrains.exposed.v1.core.ReferenceOption +import org.jetbrains.exposed.v1.core.dao.id.java.UUIDTable +import org.jetbrains.exposed.v1.datetime.timestamp + +const val MAX_MESSAGE_TEXT_LENGTH = 4000 + +object MessageModel : UUIDTable("messages") { + val chatId = reference("chat_id", ChatModel, onDelete = ReferenceOption.CASCADE, onUpdate = ReferenceOption.NO_ACTION, fkName = "messages_chat_id_fkey") + val senderId = reference("sender_id", UserModel, onDelete = ReferenceOption.CASCADE, onUpdate = ReferenceOption.NO_ACTION, fkName = "messages_sender_id_fkey") + val text = varchar("text", MAX_MESSAGE_TEXT_LENGTH) + val createdAt = timestamp("created_at") + + init { + index("messages_chat_id_created_at_id_idx", false, chatId, createdAt, id) + } +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/models/user/UserModel.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/models/user/UserModel.kt new file mode 100644 index 00000000..8dfa1664 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/models/user/UserModel.kt @@ -0,0 +1,14 @@ +package com.mtusi.messenger.main.infrastructure.models.user + +import org.jetbrains.exposed.v1.core.dao.id.java.UUIDTable +import org.jetbrains.exposed.v1.datetime.timestamp + +const val MAX_USERNAME_LENGTH = 32 +const val MAX_DISPLAY_NAME_LENGTH = 100 + +object UserModel : UUIDTable("users") { + val username = varchar("username", MAX_USERNAME_LENGTH).uniqueIndex("users_username_key") + val displayName = varchar("display_name", MAX_DISPLAY_NAME_LENGTH) + val passwordHash = varchar("password_hash", 255) + val createdAt = timestamp("created_at") +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/RepositoriesModule.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/RepositoriesModule.kt new file mode 100644 index 00000000..b5d76471 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/RepositoriesModule.kt @@ -0,0 +1,8 @@ +package com.mtusi.messenger.main.infrastructure.repositories + +import org.koin.core.annotation.ComponentScan +import org.koin.core.annotation.Module + +@Module +@ComponentScan +class RepositoriesModule diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/Repository.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/Repository.kt new file mode 100644 index 00000000..7d841e3e --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/Repository.kt @@ -0,0 +1,3 @@ +package com.mtusi.messenger.main.infrastructure.repositories + +interface Repository diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/_common/RedisConnection.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/_common/RedisConnection.kt new file mode 100644 index 00000000..03d38375 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/_common/RedisConnection.kt @@ -0,0 +1,88 @@ +package com.mtusi.messenger.main.infrastructure.repositories._common + +import com.mtusi.messenger.main.config.redis.RedisConfig +import com.mtusi.messenger.main.utils.resolveSecret +import io.lettuce.core.ClientOptions +import io.lettuce.core.RedisClient +import io.lettuce.core.RedisConnectionException +import io.lettuce.core.RedisURI +import io.lettuce.core.SocketOptions +import io.lettuce.core.TimeoutOptions +import io.lettuce.core.api.StatefulRedisConnection +import io.lettuce.core.api.async.RedisAsyncCommands +import io.lettuce.core.codec.StringCodec +import kotlinx.coroutines.future.await +import kotlinx.coroutines.sync.Mutex +import kotlinx.coroutines.sync.withLock +import org.koin.core.annotation.Single +import java.time.Duration +import kotlin.time.Duration.Companion.seconds +import kotlin.time.TimeSource + +private val RECONNECT_BACKOFF = 1.seconds + +/** + * Lazily connected Redis client. The application must start and serve requests without Redis, so the connection + * is opened on first use, a failed attempt is not retried for [RECONNECT_BACKOFF], and commands issued while + * disconnected fail immediately instead of queueing behind the reconnect. + */ +@Single +class RedisConnection( + config: RedisConfig +) : AutoCloseable { + + private val uri = RedisURI.builder() + .withHost(config.host) + .withPort(config.port) + .withTimeout(Duration.ofMillis(config.commandTimeoutMs)) + .apply { + val password = resolveSecret(config.password, config.passwordFile) + if (password != null) { + if (config.username != null) withAuthentication(config.username, password) else withPassword(password.toCharArray()) + } + } + .build() + + private val client = RedisClient.create().apply { + options = ClientOptions.builder() + .autoReconnect(true) + .disconnectedBehavior(ClientOptions.DisconnectedBehavior.REJECT_COMMANDS) + .timeoutOptions(TimeoutOptions.enabled(Duration.ofMillis(config.commandTimeoutMs))) + .socketOptions(SocketOptions.builder().connectTimeout(Duration.ofMillis(config.connectTimeoutMs)).build()) + .build() + } + + private val mutex = Mutex() + + @Volatile + private var connection: StatefulRedisConnection? = null + + @Volatile + private var nextAttempt = TimeSource.Monotonic.markNow() + + suspend fun commands(): RedisAsyncCommands { + return (connection ?: connect()).async() + } + + private suspend fun connect(): StatefulRedisConnection = mutex.withLock { + + connection?.let { return it } + + if (nextAttempt.hasNotPassedNow()) { + throw RedisConnectionException("Redis is unavailable, next connection attempt is postponed") + } + + return try { + client.connectAsync(StringCodec.UTF8, uri).await().also { connection = it } + } catch (e: Exception) { + nextAttempt = TimeSource.Monotonic.markNow() + RECONNECT_BACKOFF + throw e as? RedisConnectionException ?: RedisConnectionException("Unable to connect to Redis", e) + } + } + + override fun close() { + connection?.close() + client.shutdown() + } + +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/attachment/AttachmentRepository.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/attachment/AttachmentRepository.kt new file mode 100644 index 00000000..1487d761 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/attachment/AttachmentRepository.kt @@ -0,0 +1,20 @@ +package com.mtusi.messenger.main.infrastructure.repositories.attachment + +import com.mtusi.messenger.main.infrastructure.dto.Attachment +import java.util.UUID + +interface AttachmentRepository { + + fun create(attachment: Attachment) + + fun findById(id: UUID): Attachment? + + fun attach( + messageId: UUID, + ids: List + ) + + fun idsForMessage(messageId: UUID): List + + fun idsForMessages(messageIds: List): Map> +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/attachment/ExposedAttachmentRepository.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/attachment/ExposedAttachmentRepository.kt new file mode 100644 index 00000000..024c6330 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/attachment/ExposedAttachmentRepository.kt @@ -0,0 +1,88 @@ +package com.mtusi.messenger.main.infrastructure.repositories.attachment + +import com.mtusi.messenger.main.infrastructure.dto.Attachment +import com.mtusi.messenger.main.infrastructure.models.attachment.AttachmentModel +import com.mtusi.messenger.main.infrastructure.models.attachment.MessageAttachmentModel +import org.jetbrains.exposed.v1.jdbc.insert +import org.jetbrains.exposed.v1.jdbc.selectAll +import org.jetbrains.exposed.v1.core.ResultRow +import org.jetbrains.exposed.v1.core.inList +import org.jetbrains.exposed.v1.core.eq +import org.jetbrains.exposed.v1.core.SortOrder +import org.koin.core.annotation.Single +import java.util.UUID + +@Single +class ExposedAttachmentRepository : AttachmentRepository { + + override fun create(attachment: Attachment) { + AttachmentModel.insert { + it[id] = attachment.id + it[chatId] = attachment.chatId + it[ownerId] = attachment.ownerId + it[filename] = attachment.filename + it[contentType] = attachment.contentType + it[sizeBytes] = attachment.sizeBytes + it[sha256] = attachment.sha256 + it[createdAt] = attachment.createdAt + } + } + + override fun findById( + id: UUID + ): Attachment? { + return AttachmentModel + .selectAll() + .where(AttachmentModel.id eq id) + .firstOrNull() + ?.toAttachment() + } + + override fun attach( + messageId: UUID, + ids: List + ) { + ids.forEachIndexed { index, id -> + MessageAttachmentModel.insert { + it[this.messageId] = messageId + it[attachmentId] = id + it[position] = index + } + } + } + + override fun idsForMessage( + messageId: UUID + ): List { + return idsForMessages(listOf(messageId))[messageId].orEmpty() + } + + override fun idsForMessages( + messageIds: List + ): Map> { + if (messageIds.isEmpty()) { + return emptyMap() + } + + return MessageAttachmentModel + .selectAll() + .where(MessageAttachmentModel.messageId inList messageIds) + .orderBy(MessageAttachmentModel.position to SortOrder.ASC) + .groupBy( + keySelector = { it[MessageAttachmentModel.messageId].value }, + valueTransform = { it[MessageAttachmentModel.attachmentId].value } + ) + } + + private fun ResultRow.toAttachment() = Attachment( + id = this[AttachmentModel.id].value, + chatId = this[AttachmentModel.chatId].value, + ownerId = this[AttachmentModel.ownerId].value, + filename = this[AttachmentModel.filename], + contentType = this[AttachmentModel.contentType], + sizeBytes = this[AttachmentModel.sizeBytes], + sha256 = this[AttachmentModel.sha256], + createdAt = this[AttachmentModel.createdAt] + ) + +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/attachment/ExposedUploadJournalRepository.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/attachment/ExposedUploadJournalRepository.kt new file mode 100644 index 00000000..edd2ef92 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/attachment/ExposedUploadJournalRepository.kt @@ -0,0 +1,77 @@ +package com.mtusi.messenger.main.infrastructure.repositories.attachment + +import com.mtusi.messenger.main.infrastructure.models.attachment.UploadJournalModel +import com.mtusi.messenger.main.utils.exposed.operations.plus +import org.jetbrains.exposed.v1.core.SortOrder +import org.jetbrains.exposed.v1.core.and +import org.jetbrains.exposed.v1.core.eq +import org.jetbrains.exposed.v1.core.greater +import org.jetbrains.exposed.v1.core.inList +import org.jetbrains.exposed.v1.core.lessEq +import org.jetbrains.exposed.v1.core.longLiteral +import org.jetbrains.exposed.v1.core.vendors.ForUpdateOption +import org.jetbrains.exposed.v1.datetime.CurrentTimestamp +import org.jetbrains.exposed.v1.jdbc.deleteWhere +import org.jetbrains.exposed.v1.jdbc.insert +import org.jetbrains.exposed.v1.jdbc.select +import org.jetbrains.exposed.v1.jdbc.selectAll +import org.jetbrains.exposed.v1.jdbc.update +import org.koin.core.annotation.Single +import java.util.UUID + +@Single +class ExposedUploadJournalRepository : UploadJournalRepository { + + override fun register( + id: UUID + ) { + UploadJournalModel.insert { + it[this.id] = id + } + } + + override fun lockForCompletion( + id: UUID + ): Boolean { + val canComplete = (UploadJournalModel.abandoned eq false) and + (UploadJournalModel.expiresAt greater CurrentTimestamp) + + return UploadJournalModel + .select(canComplete) + .where(UploadJournalModel.id eq id) + .forUpdate() + .firstOrNull() + ?.get(canComplete) + ?: false + } + + override fun complete( + id: UUID + ) { + UploadJournalModel.deleteWhere { + UploadJournalModel.id eq id + } + } + + override fun claimExpired(): List { + + val ids = UploadJournalModel + .selectAll() + .where(UploadJournalModel.nextAttemptAt lessEq CurrentTimestamp) + .orderBy(UploadJournalModel.nextAttemptAt to SortOrder.ASC) + .limit(100) + .forUpdate(ForUpdateOption.PostgreSQL.ForUpdate(ForUpdateOption.PostgreSQL.MODE.SKIP_LOCKED)) + .map { + it[UploadJournalModel.id].value + } + + if (ids.isNotEmpty()) { + UploadJournalModel.update({ UploadJournalModel.id inList ids }) { + it[abandoned] = true + it[nextAttemptAt] = CurrentTimestamp + longLiteral(60) + } + } + + return ids + } +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/attachment/UploadJournalRepository.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/attachment/UploadJournalRepository.kt new file mode 100644 index 00000000..09fff459 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/attachment/UploadJournalRepository.kt @@ -0,0 +1,13 @@ +package com.mtusi.messenger.main.infrastructure.repositories.attachment + +import java.util.UUID + +interface UploadJournalRepository { + fun register(id: UUID) + + fun lockForCompletion(id: UUID): Boolean + + fun complete(id: UUID) + + fun claimExpired(): List +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/attachment/storage/ObjectStorageRepository.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/attachment/storage/ObjectStorageRepository.kt new file mode 100644 index 00000000..42154b81 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/attachment/storage/ObjectStorageRepository.kt @@ -0,0 +1,19 @@ +package com.mtusi.messenger.main.infrastructure.repositories.attachment.storage + +import java.io.File +import java.util.UUID + +interface ObjectStorageRepository { + + suspend fun put( + id: UUID, + file: File, + contentType: String + ) + + suspend fun get(id: UUID): ByteArray + + suspend fun delete(id: UUID) + + suspend fun ready(): Boolean +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/attachment/storage/S3ObjectStorageRepository.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/attachment/storage/S3ObjectStorageRepository.kt new file mode 100644 index 00000000..72afac5f --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/attachment/storage/S3ObjectStorageRepository.kt @@ -0,0 +1,116 @@ +package com.mtusi.messenger.main.infrastructure.repositories.attachment.storage + +import aws.sdk.kotlin.runtime.auth.credentials.StaticCredentialsProvider +import aws.sdk.kotlin.services.s3.S3Client +import aws.sdk.kotlin.services.s3.deleteObject +import aws.sdk.kotlin.services.s3.model.GetObjectRequest +import aws.sdk.kotlin.services.s3.headBucket +import aws.sdk.kotlin.services.s3.putObject +import aws.smithy.kotlin.runtime.content.asByteStream +import aws.smithy.kotlin.runtime.content.toByteArray +import aws.smithy.kotlin.runtime.net.url.Url +import com.mtusi.messenger.main.config.storage.StorageConfig +import com.mtusi.messenger.main.infrastructure.services.attachment.MAX_ATTACHMENT_BYTES +import com.mtusi.messenger.main.infrastructure.types.ApiException +import com.mtusi.messenger.main.infrastructure.types.ErrorCode +import com.mtusi.messenger.main.utils.resolveSecret +import kotlinx.coroutines.CancellationException +import kotlinx.coroutines.withTimeoutOrNull +import org.koin.core.annotation.Single +import java.io.File +import java.util.UUID +import kotlin.time.Duration.Companion.milliseconds + +@Single +class S3ObjectStorageRepository( + private val config: StorageConfig +) : ObjectStorageRepository { + + private val client by lazy { + S3Client { + endpointUrl = Url.parse(config.endpoint) + region = "us-east-1" + forcePathStyle = true + + credentialsProvider = StaticCredentialsProvider { + accessKeyId = requireNotNull(resolveSecret(config.accessKey, config.accessKeyFile)) + secretAccessKey = requireNotNull(resolveSecret(config.secretKey, config.secretKeyFile)) + } + } + } + + override suspend fun put( + id: UUID, + file: File, + contentType: String + ) { + request { + client.putObject { + bucket = config.bucket + key = getObjectKey(id) + body = file.asByteStream() + this.contentType = contentType + contentLength = file.length() + } + } + } + + override suspend fun get( + id: UUID + ): ByteArray = request { + client.getObject(GetObjectRequest { + bucket = config.bucket + key = getObjectKey(id) + }) { response -> + + val contentLength = requireNotNull(response.contentLength) + + check(contentLength >= 1L && contentLength <= MAX_ATTACHMENT_BYTES.toLong()) + + val bytes = requireNotNull(response.body?.toByteArray()) + + check(bytes.size <= MAX_ATTACHMENT_BYTES) + + bytes + } + } + + override suspend fun delete( + id: UUID + ) { + request { + client.deleteObject { + bucket = config.bucket + key = getObjectKey(id) + } + } + } + + override suspend fun ready(): Boolean { + return try { + request { + client.headBucket { + bucket = config.bucket + } + } + true + } catch (_: ApiException) { + false + } + } + + private fun getObjectKey(id: UUID) = "originals/$id" + + private suspend fun request(block: suspend () -> T): T { + return try { + withTimeoutOrNull(8000.milliseconds) { + block() + } ?: throw ApiException(ErrorCode.UNAVAILABLE, "object storage unavailable") + } catch (cause: CancellationException) { + throw cause + } catch (_: Exception) { + throw ApiException(ErrorCode.UNAVAILABLE, "object storage unavailable") + } + } + +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/authentication/session/ExposedSessionRepository.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/authentication/session/ExposedSessionRepository.kt new file mode 100644 index 00000000..080b14e3 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/authentication/session/ExposedSessionRepository.kt @@ -0,0 +1,73 @@ +package com.mtusi.messenger.main.infrastructure.repositories.authentication.session + +import com.mtusi.messenger.main.infrastructure.dto.authentication.Session +import com.mtusi.messenger.main.infrastructure.models.authentication.SessionModel +import com.mtusi.messenger.main.utils.nowMicros +import org.jetbrains.exposed.v1.core.ResultRow +import org.jetbrains.exposed.v1.core.and +import org.jetbrains.exposed.v1.core.eq +import org.jetbrains.exposed.v1.core.greater +import org.jetbrains.exposed.v1.jdbc.deleteWhere +import org.jetbrains.exposed.v1.jdbc.insert +import org.jetbrains.exposed.v1.jdbc.selectAll +import org.koin.core.annotation.Single +import java.util.UUID +import kotlin.time.Clock +import kotlin.time.Duration +import kotlin.time.Instant + +@Single +class ExposedSessionRepository : SessionRepository { + + override fun create( + userId: UUID, + tokenHash: String, + ttl: Duration + ): Session { + + val createdAt = Clock.System.nowMicros() + val expiresAt = createdAt + ttl + + val insertedRow = SessionModel.insert { + it[this.userId] = userId + it[this.tokenHash] = tokenHash + it[this.createdAt] = createdAt + it[this.expiresAt] = expiresAt + } + + return Session( + id = insertedRow[SessionModel.id].value, + userId = userId, + tokenHash = tokenHash, + createdAt = createdAt, + expiresAt = expiresAt + ) + } + + override fun findActiveByTokenHash( + tokenHash: String, + now: Instant + ): Session? { + return SessionModel + .selectAll() + .where((SessionModel.tokenHash eq tokenHash) and (SessionModel.expiresAt greater now)) + .firstOrNull() + ?.toSession() + } + + override fun deleteById( + id: UUID + ): Boolean { + val deletedRows = SessionModel.deleteWhere { SessionModel.id eq id } + return deletedRows > 0 + } + + private fun ResultRow.toSession() = Session( + id = this[SessionModel.id].value, + userId = this[SessionModel.userId].value, + tokenHash = this[SessionModel.tokenHash], + createdAt = this[SessionModel.createdAt], + expiresAt = this[SessionModel.expiresAt] + ) + +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/authentication/session/SessionRepository.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/authentication/session/SessionRepository.kt new file mode 100644 index 00000000..e693c339 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/authentication/session/SessionRepository.kt @@ -0,0 +1,26 @@ +package com.mtusi.messenger.main.infrastructure.repositories.authentication.session + +import com.mtusi.messenger.main.infrastructure.dto.authentication.Session +import com.mtusi.messenger.main.infrastructure.repositories.Repository +import java.util.UUID +import kotlin.time.Duration +import kotlin.time.Instant + +interface SessionRepository : Repository { + + fun create( + userId: UUID, + tokenHash: String, + ttl: Duration + ): Session + + fun findActiveByTokenHash( + tokenHash: String, + now: Instant + ): Session? + + fun deleteById( + id: UUID + ): Boolean + +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/authentication/session/cache/CachedSession.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/authentication/session/cache/CachedSession.kt new file mode 100644 index 00000000..86be49d5 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/authentication/session/cache/CachedSession.kt @@ -0,0 +1,11 @@ +package com.mtusi.messenger.main.infrastructure.repositories.authentication.session.cache + +import com.mtusi.messenger.main.infrastructure.dto.authentication.Session + +sealed class CachedSession { + + data class Active(val session: Session) : CachedSession() + + data object Revoked : CachedSession() + +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/authentication/session/cache/RedisSessionCacheRepository.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/authentication/session/cache/RedisSessionCacheRepository.kt new file mode 100644 index 00000000..5cd22fc3 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/authentication/session/cache/RedisSessionCacheRepository.kt @@ -0,0 +1,87 @@ +package com.mtusi.messenger.main.infrastructure.repositories.authentication.session.cache + +import com.mtusi.messenger.main.compat.serializers.SerialUUID +import com.mtusi.messenger.main.infrastructure.dto.authentication.Session +import com.mtusi.messenger.main.infrastructure.repositories._common.RedisConnection +import io.lettuce.core.SetArgs +import kotlinx.coroutines.future.await +import kotlinx.serialization.Serializable +import kotlinx.serialization.json.Json +import org.koin.core.annotation.Single +import kotlin.time.Instant + +private const val KEY_PREFIX = "session:" +private const val REVOKED = "revoked" + +@Single +class RedisSessionCacheRepository( + private val redis: RedisConnection +) : SessionCacheRepository { + + override suspend fun find( + tokenHash: String + ): CachedSession? { + + val value = redis.commands().get(key(tokenHash)).await() ?: return null + + if (value == REVOKED) { + return CachedSession.Revoked + } + + return CachedSession.Active( + Json.decodeFromString(value).toSession(tokenHash) + ) + } + + override suspend fun putIfAbsent( + session: Session, + cachedUntil: Instant + ) { + redis.commands().set( + key(session.tokenHash), + Json.encodeToString(SessionEntry.from(session)), + SetArgs.Builder.nx().pxAt(cachedUntil.toEpochMilliseconds()) + ).await() + } + + override suspend fun revoke( + tokenHash: String, + expiresAt: Instant + ) { + redis.commands().set( + key(tokenHash), + REVOKED, + SetArgs.Builder.pxAt(expiresAt.toEpochMilliseconds()) + ).await() + } + + private fun key(tokenHash: String) = KEY_PREFIX + tokenHash + + @Serializable + private data class SessionEntry( + val id: SerialUUID, + val userId: SerialUUID, + val createdAt: Instant, + val expiresAt: Instant + ) { + + fun toSession(tokenHash: String) = Session( + id = id, + userId = userId, + tokenHash = tokenHash, + createdAt = createdAt, + expiresAt = expiresAt + ) + + companion object { + fun from(session: Session) = SessionEntry( + id = session.id, + userId = session.userId, + createdAt = session.createdAt, + expiresAt = session.expiresAt + ) + } + + } + +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/authentication/session/cache/SessionCacheRepository.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/authentication/session/cache/SessionCacheRepository.kt new file mode 100644 index 00000000..31ef6897 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/authentication/session/cache/SessionCacheRepository.kt @@ -0,0 +1,23 @@ +package com.mtusi.messenger.main.infrastructure.repositories.authentication.session.cache + +import com.mtusi.messenger.main.infrastructure.dto.authentication.Session +import com.mtusi.messenger.main.infrastructure.repositories.Repository +import kotlin.time.Instant + +interface SessionCacheRepository : Repository { + + suspend fun find( + tokenHash: String + ): CachedSession? + + suspend fun putIfAbsent( + session: Session, + cachedUntil: Instant + ) + + suspend fun revoke( + tokenHash: String, + expiresAt: Instant + ) + +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/authentication/session/revocation/ExposedSessionRevocationRepository.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/authentication/session/revocation/ExposedSessionRevocationRepository.kt new file mode 100644 index 00000000..9ca4227a --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/authentication/session/revocation/ExposedSessionRevocationRepository.kt @@ -0,0 +1,52 @@ +package com.mtusi.messenger.main.infrastructure.repositories.authentication.session.revocation + +import com.mtusi.messenger.main.infrastructure.dto.authentication.SessionRevocation +import com.mtusi.messenger.main.infrastructure.models.authentication.SessionRevocationModel +import org.jetbrains.exposed.v1.core.eq +import org.jetbrains.exposed.v1.core.greater +import org.jetbrains.exposed.v1.core.lessEq +import org.jetbrains.exposed.v1.jdbc.deleteWhere +import org.jetbrains.exposed.v1.jdbc.insertIgnore +import org.jetbrains.exposed.v1.jdbc.selectAll +import org.koin.core.annotation.Single +import kotlin.time.Instant + +@Single +class ExposedSessionRevocationRepository : SessionRevocationRepository { + + override fun add( + revocation: SessionRevocation + ) { + SessionRevocationModel.insertIgnore { + it[tokenHash] = revocation.tokenHash + it[expiresAt] = revocation.expiresAt + } + } + + override fun findPending( + now: Instant + ): List { + return SessionRevocationModel + .selectAll() + .where(SessionRevocationModel.expiresAt greater now) + .map { + SessionRevocation( + tokenHash = it[SessionRevocationModel.tokenHash], + expiresAt = it[SessionRevocationModel.expiresAt] + ) + } + } + + override fun delete( + tokenHash: String + ) { + SessionRevocationModel.deleteWhere { SessionRevocationModel.tokenHash eq tokenHash } + } + + override fun deleteExpired( + now: Instant + ) { + SessionRevocationModel.deleteWhere { SessionRevocationModel.expiresAt lessEq now } + } + +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/authentication/session/revocation/SessionRevocationRepository.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/authentication/session/revocation/SessionRevocationRepository.kt new file mode 100644 index 00000000..ab9d69b9 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/authentication/session/revocation/SessionRevocationRepository.kt @@ -0,0 +1,25 @@ +package com.mtusi.messenger.main.infrastructure.repositories.authentication.session.revocation + +import com.mtusi.messenger.main.infrastructure.dto.authentication.SessionRevocation +import com.mtusi.messenger.main.infrastructure.repositories.Repository +import kotlin.time.Instant + +interface SessionRevocationRepository : Repository { + + fun add( + revocation: SessionRevocation + ) + + fun findPending( + now: Instant + ): List + + fun delete( + tokenHash: String + ) + + fun deleteExpired( + now: Instant + ) + +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/chat/ChatRepository.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/chat/ChatRepository.kt new file mode 100644 index 00000000..4e49658c --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/chat/ChatRepository.kt @@ -0,0 +1,30 @@ +package com.mtusi.messenger.main.infrastructure.repositories.chat + +import com.mtusi.messenger.main.infrastructure.dto.Chat +import com.mtusi.messenger.main.infrastructure.repositories.Repository +import com.mtusi.messenger.main.utils.PageKey +import java.util.UUID + +interface ChatRepository : Repository { + + fun create( + title: String, + memberIds: List + ): Chat + + fun findById( + id: UUID + ): Chat? + + fun isMember( + chatId: UUID, + userId: UUID + ): Boolean + + fun findPageByMemberId( + userId: UUID, + after: PageKey?, + limit: Int + ): List + +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/chat/ExposedChatRepository.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/chat/ExposedChatRepository.kt new file mode 100644 index 00000000..2afe352d --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/chat/ExposedChatRepository.kt @@ -0,0 +1,134 @@ +package com.mtusi.messenger.main.infrastructure.repositories.chat + +import com.mtusi.messenger.main.infrastructure.dto.Chat +import com.mtusi.messenger.main.infrastructure.models.chat.ChatMemberModel +import com.mtusi.messenger.main.infrastructure.models.chat.ChatModel +import com.mtusi.messenger.main.utils.PageKey +import com.mtusi.messenger.main.utils.exposed.operations.RowGreater +import com.mtusi.messenger.main.utils.nowMicros +import org.jetbrains.exposed.v1.core.QueryParameter +import org.jetbrains.exposed.v1.core.ResultRow +import org.jetbrains.exposed.v1.core.SortOrder +import org.jetbrains.exposed.v1.core.and +import org.jetbrains.exposed.v1.core.dao.id.EntityID +import org.jetbrains.exposed.v1.core.eq +import org.jetbrains.exposed.v1.core.inList +import org.jetbrains.exposed.v1.jdbc.batchInsert +import org.jetbrains.exposed.v1.jdbc.insert +import org.jetbrains.exposed.v1.jdbc.selectAll +import org.koin.core.annotation.Single +import java.util.UUID +import kotlin.time.Clock + +@Single +class ExposedChatRepository : ChatRepository { + + override fun create( + title: String, + memberIds: List + ): Chat { + + val insertedRow = ChatModel.insert { + it[this.title] = title + it[this.createdAt] = Clock.System.nowMicros() + } + + val chatId = insertedRow[ChatModel.id].value + + ChatMemberModel.batchInsert(memberIds.withIndex()) { (position, userId) -> + this[ChatMemberModel.chatId] = chatId + this[ChatMemberModel.userId] = userId + this[ChatMemberModel.position] = position + } + + return Chat( + id = chatId, + title = title, + memberIds = memberIds, + createdAt = insertedRow[ChatModel.createdAt] + ) + } + + override fun findById( + id: UUID + ): Chat? { + + val row = ChatModel + .selectAll() + .where(ChatModel.id eq id) + .firstOrNull() + ?: return null + + return row.toChat(findMemberIds(listOf(id))[id].orEmpty()) + } + + override fun isMember( + chatId: UUID, + userId: UUID + ): Boolean { + return ChatMemberModel + .selectAll() + .where((ChatMemberModel.chatId eq chatId) and (ChatMemberModel.userId eq userId)) + .empty() + .not() + } + + override fun findPageByMemberId( + userId: UUID, + after: PageKey?, + limit: Int + ): List { + + var condition = ChatMemberModel.userId eq userId + + if (after != null) { + condition = condition and RowGreater( + left = listOf(ChatModel.createdAt, ChatModel.id), + right = listOf( + QueryParameter(after.createdAt, ChatModel.createdAt.columnType), + QueryParameter(EntityID(after.id, ChatModel), ChatModel.id.columnType) + ) + ) + } + + val rows = ChatModel + .innerJoin(ChatMemberModel) + .selectAll() + .where(condition) + .orderBy(ChatModel.createdAt to SortOrder.ASC, ChatModel.id to SortOrder.ASC) + .limit(limit) + .toList() + + val memberIds = findMemberIds(rows.map { it[ChatModel.id].value }) + + return rows.map { + it.toChat(memberIds[it[ChatModel.id].value].orEmpty()) + } + } + + private fun findMemberIds( + chatIds: List + ): Map> { + + if (chatIds.isEmpty()) { + return emptyMap() + } + + return ChatMemberModel + .selectAll() + .where(ChatMemberModel.chatId inList chatIds) + .orderBy(ChatMemberModel.position to SortOrder.ASC) + .groupBy( + keySelector = { it[ChatMemberModel.chatId].value }, + valueTransform = { it[ChatMemberModel.userId].value } + ) + } + + private fun ResultRow.toChat(memberIds: List) = Chat( + id = this[ChatModel.id].value, + title = this[ChatModel.title], + memberIds = memberIds, + createdAt = this[ChatModel.createdAt] + ) + +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/message/ExposedMessageRepository.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/message/ExposedMessageRepository.kt new file mode 100644 index 00000000..0595efa7 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/message/ExposedMessageRepository.kt @@ -0,0 +1,99 @@ +package com.mtusi.messenger.main.infrastructure.repositories.message + +import com.mtusi.messenger.main.infrastructure.repositories.attachment.AttachmentRepository +import com.mtusi.messenger.main.infrastructure.dto.Message +import com.mtusi.messenger.main.infrastructure.models.message.MessageModel +import com.mtusi.messenger.main.utils.PageKey +import com.mtusi.messenger.main.utils.exposed.operations.RowGreater +import com.mtusi.messenger.main.utils.nowMicros +import org.jetbrains.exposed.v1.core.QueryParameter +import org.jetbrains.exposed.v1.core.ResultRow +import org.jetbrains.exposed.v1.core.SortOrder +import org.jetbrains.exposed.v1.core.and +import org.jetbrains.exposed.v1.core.dao.id.EntityID +import org.jetbrains.exposed.v1.core.eq +import org.jetbrains.exposed.v1.jdbc.insert +import org.jetbrains.exposed.v1.jdbc.selectAll +import org.koin.core.annotation.Single +import java.util.UUID +import kotlin.time.Clock + +@Single +class ExposedMessageRepository( + private val attachments: AttachmentRepository +) : MessageRepository { + + override fun create( + chatId: UUID, + senderId: UUID, + text: String + ): Message { + + val insertedRow = MessageModel.insert { + it[this.chatId] = chatId + it[this.senderId] = senderId + it[this.text] = text + it[this.createdAt] = Clock.System.nowMicros() + } + + return Message( + id = insertedRow[MessageModel.id].value, + chatId = chatId, + senderId = senderId, + text = text, + createdAt = insertedRow[MessageModel.createdAt] + ) + } + + override fun findById( + id: UUID + ): Message? { + return MessageModel + .selectAll() + .where(MessageModel.id eq id) + .firstOrNull() + ?.toMessage(attachments.idsForMessage(id)) + } + + override fun findPageByChatId( + chatId: UUID, + after: PageKey?, + limit: Int + ): List { + + var condition = MessageModel.chatId eq chatId + + if (after != null) { + condition = condition and RowGreater( + left = listOf(MessageModel.createdAt, MessageModel.id), + right = listOf( + QueryParameter(after.createdAt, MessageModel.createdAt.columnType), + QueryParameter(EntityID(after.id, MessageModel), MessageModel.id.columnType) + ) + ) + } + + val rows = MessageModel + .selectAll() + .where(condition) + .orderBy(MessageModel.createdAt to SortOrder.ASC, MessageModel.id to SortOrder.ASC) + .limit(limit) + .toList() + + val attachmentIds = attachments.idsForMessages(rows.map { it[MessageModel.id].value }) + + return rows.map { + it.toMessage(attachmentIds[it[MessageModel.id].value].orEmpty()) + } + } + + private fun ResultRow.toMessage(attachmentIds: List) = Message( + id = this[MessageModel.id].value, + chatId = this[MessageModel.chatId].value, + senderId = this[MessageModel.senderId].value, + text = this[MessageModel.text], + attachmentIds = attachmentIds, + createdAt = this[MessageModel.createdAt] + ) + +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/message/MessageRepository.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/message/MessageRepository.kt new file mode 100644 index 00000000..86bddb81 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/message/MessageRepository.kt @@ -0,0 +1,26 @@ +package com.mtusi.messenger.main.infrastructure.repositories.message + +import com.mtusi.messenger.main.infrastructure.dto.Message +import com.mtusi.messenger.main.infrastructure.repositories.Repository +import com.mtusi.messenger.main.utils.PageKey +import java.util.UUID + +interface MessageRepository : Repository { + + fun create( + chatId: UUID, + senderId: UUID, + text: String + ): Message + + fun findById( + id: UUID + ): Message? + + fun findPageByChatId( + chatId: UUID, + after: PageKey?, + limit: Int + ): List + +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/message/idempotency/ExposedMessageIdempotencyRepository.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/message/idempotency/ExposedMessageIdempotencyRepository.kt new file mode 100644 index 00000000..0363fc04 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/message/idempotency/ExposedMessageIdempotencyRepository.kt @@ -0,0 +1,83 @@ +package com.mtusi.messenger.main.infrastructure.repositories.message.idempotency + +import com.mtusi.messenger.main.infrastructure.models.message.MessageIdempotencyKeyModel +import org.jetbrains.exposed.v1.core.and +import org.jetbrains.exposed.v1.core.eq +import org.jetbrains.exposed.v1.core.less +import org.jetbrains.exposed.v1.jdbc.deleteWhere +import org.jetbrains.exposed.v1.jdbc.insertIgnore +import org.jetbrains.exposed.v1.jdbc.selectAll +import org.jetbrains.exposed.v1.jdbc.update +import org.koin.core.annotation.Single +import java.util.UUID +import kotlin.time.Instant + +@Single +class ExposedMessageIdempotencyRepository : MessageIdempotencyRepository { + + override fun claim( + userId: UUID, + chatId: UUID, + key: String, + requestHash: String, + now: Instant + ): Boolean { + + // INSERT ... ON CONFLICT DO NOTHING + val statement = MessageIdempotencyKeyModel.insertIgnore { + it[this.userId] = userId + it[this.chatId] = chatId + it[this.key] = key + it[this.requestHash] = requestHash + it[this.createdAt] = now + } + + return statement.insertedCount > 0 + } + + override fun find( + userId: UUID, + chatId: UUID, + key: String + ): MessageIdempotencyRecord? { + return MessageIdempotencyKeyModel + .selectAll() + .where(scope(userId, chatId, key)) + .firstOrNull() + ?.let { + MessageIdempotencyRecord( + requestHash = it[MessageIdempotencyKeyModel.requestHash], + messageId = it[MessageIdempotencyKeyModel.messageId]?.value, + createdAt = it[MessageIdempotencyKeyModel.createdAt] + ) + } + } + + override fun attachMessage( + userId: UUID, + chatId: UUID, + key: String, + messageId: UUID + ) { + MessageIdempotencyKeyModel.update({ scope(userId, chatId, key) }) { + it[this.messageId] = messageId + } + } + + override fun deleteExpired( + userId: UUID, + chatId: UUID, + key: String, + createdBefore: Instant + ) { + MessageIdempotencyKeyModel.deleteWhere { + scope(userId, chatId, key) and (MessageIdempotencyKeyModel.createdAt less createdBefore) + } + } + + private fun scope(userId: UUID, chatId: UUID, key: String) = + (MessageIdempotencyKeyModel.userId eq userId) and + (MessageIdempotencyKeyModel.chatId eq chatId) and + (MessageIdempotencyKeyModel.key eq key) + +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/message/idempotency/MessageIdempotencyRepository.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/message/idempotency/MessageIdempotencyRepository.kt new file mode 100644 index 00000000..a3069dd0 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/message/idempotency/MessageIdempotencyRepository.kt @@ -0,0 +1,43 @@ +package com.mtusi.messenger.main.infrastructure.repositories.message.idempotency + +import com.mtusi.messenger.main.infrastructure.repositories.Repository +import java.util.UUID +import kotlin.time.Instant + +data class MessageIdempotencyRecord( + val requestHash: String, + val messageId: UUID?, + val createdAt: Instant, +) + +interface MessageIdempotencyRepository : Repository { + + fun claim( + userId: UUID, + chatId: UUID, + key: String, + requestHash: String, + now: Instant + ): Boolean + + fun find( + userId: UUID, + chatId: UUID, + key: String + ): MessageIdempotencyRecord? + + fun attachMessage( + userId: UUID, + chatId: UUID, + key: String, + messageId: UUID + ) + + fun deleteExpired( + userId: UUID, + chatId: UUID, + key: String, + createdBefore: Instant + ) + +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/user/ExposedUserRepository.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/user/ExposedUserRepository.kt new file mode 100644 index 00000000..404a333f --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/user/ExposedUserRepository.kt @@ -0,0 +1,82 @@ +package com.mtusi.messenger.main.infrastructure.repositories.user + +import com.mtusi.messenger.main.infrastructure.dto.User +import com.mtusi.messenger.main.infrastructure.dto.authentication.UserCredentials +import com.mtusi.messenger.main.infrastructure.models.user.UserModel +import com.mtusi.messenger.main.utils.nowMicros +import org.jetbrains.exposed.v1.core.ResultRow +import org.jetbrains.exposed.v1.core.eq +import org.jetbrains.exposed.v1.core.inList +import org.jetbrains.exposed.v1.jdbc.insertIgnore +import org.jetbrains.exposed.v1.jdbc.selectAll +import org.koin.core.annotation.Single +import java.util.UUID +import kotlin.time.Clock + +@Single +class ExposedUserRepository : UserRepository { + + override fun create( + username: String, + displayName: String, + passwordHash: String + ): User? { + + val statement = UserModel.insertIgnore { + it[this.username] = username + it[this.displayName] = displayName + it[this.passwordHash] = passwordHash + it[this.createdAt] = Clock.System.nowMicros() + } + + if (statement.insertedCount == 0) { + return null + } + + return statement.resultedValues + ?.singleOrNull() + ?.toUser() + } + + override fun findById( + id: UUID + ): User? { + return UserModel + .selectAll() + .where(UserModel.id eq id) + .firstOrNull() + ?.toUser() + } + + override fun findCredentialsByUsername( + username: String + ): UserCredentials? { + return UserModel + .selectAll() + .where(UserModel.username eq username) + .firstOrNull() + ?.let { + UserCredentials( + user = it.toUser(), + passwordHash = it[UserModel.passwordHash] + ) + } + } + + override fun countByIds( + ids: Collection + ): Long { + return UserModel + .selectAll() + .where(UserModel.id inList ids) + .count() + } + + private fun ResultRow.toUser() = User( + id = this[UserModel.id].value, + username = this[UserModel.username], + displayName = this[UserModel.displayName], + createdAt = this[UserModel.createdAt] + ) + +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/user/UserRepository.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/user/UserRepository.kt new file mode 100644 index 00000000..0773d071 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/user/UserRepository.kt @@ -0,0 +1,28 @@ +package com.mtusi.messenger.main.infrastructure.repositories.user + +import com.mtusi.messenger.main.infrastructure.dto.User +import com.mtusi.messenger.main.infrastructure.dto.authentication.UserCredentials +import com.mtusi.messenger.main.infrastructure.repositories.Repository +import java.util.UUID + +interface UserRepository : Repository { + + fun create( + username: String, + displayName: String, + passwordHash: String + ): User? + + fun findById( + id: UUID + ): User? + + fun findCredentialsByUsername( + username: String + ): UserCredentials? + + fun countByIds( + ids: Collection + ): Long + +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/ServicesModule.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/ServicesModule.kt new file mode 100644 index 00000000..d7d75fc9 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/ServicesModule.kt @@ -0,0 +1,8 @@ +package com.mtusi.messenger.main.infrastructure.services + +import org.koin.core.annotation.ComponentScan +import org.koin.core.annotation.Module + +@Module +@ComponentScan +class ServicesModule diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/AttachmentCleanupService.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/AttachmentCleanupService.kt new file mode 100644 index 00000000..125ac525 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/AttachmentCleanupService.kt @@ -0,0 +1,34 @@ +package com.mtusi.messenger.main.infrastructure.services.attachment + +import com.mtusi.messenger.main.infrastructure.repositories.attachment.UploadJournalRepository +import com.mtusi.messenger.main.infrastructure.repositories.attachment.storage.ObjectStorageRepository +import com.mtusi.messenger.main.plugins.suspendedTransaction +import kotlinx.coroutines.CancellationException +import org.koin.core.annotation.Single + +@Single +class AttachmentCleanupService( + private val journalRepository: UploadJournalRepository, + private val objectStorageRepository: ObjectStorageRepository +) { + suspend fun cleanup(): Int { + + val ids = suspendedTransaction { + journalRepository.claimExpired() + } + + var deleted = 0 + ids.forEach { id -> + try { + objectStorageRepository.delete(id) + deleted++ + } catch (cause: CancellationException) { + throw cause + } catch (_: Exception) { + // The durable tombstone remains eligible for the next retry. + } + } + + return deleted + } +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/AttachmentService.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/AttachmentService.kt new file mode 100644 index 00000000..b4fffe56 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/AttachmentService.kt @@ -0,0 +1,28 @@ +package com.mtusi.messenger.main.infrastructure.services.attachment + +import com.mtusi.messenger.main.infrastructure.dto.Attachment +import java.io.File +import java.util.UUID + +interface AttachmentService { + + suspend fun requireMember( + chatId: UUID, + actorId: UUID + ) + + suspend fun upload( + chatId: UUID, + actorId: UUID, + filename: String, + contentType: String, + file: File + ): Attachment + + suspend fun find( + id: UUID, + actorId: UUID + ): Attachment + + suspend fun content(attachment: Attachment): ByteArray +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/DefaultAttachmentService.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/DefaultAttachmentService.kt new file mode 100644 index 00000000..ea95a32c --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/DefaultAttachmentService.kt @@ -0,0 +1,94 @@ +package com.mtusi.messenger.main.infrastructure.services.attachment + +import com.mtusi.messenger.main.infrastructure.dto.Attachment +import com.mtusi.messenger.main.infrastructure.repositories.attachment.UploadJournalRepository +import com.mtusi.messenger.main.infrastructure.repositories.attachment.AttachmentRepository +import com.mtusi.messenger.main.infrastructure.repositories.attachment.storage.ObjectStorageRepository +import com.mtusi.messenger.main.infrastructure.repositories.chat.ChatRepository +import com.mtusi.messenger.main.infrastructure.types.ApiException +import com.mtusi.messenger.main.infrastructure.types.ErrorCode +import com.mtusi.messenger.main.plugins.suspendedTransaction +import com.mtusi.messenger.main.utils.computeSha256 +import com.mtusi.messenger.main.utils.nowMicros +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.withContext +import org.koin.core.annotation.Single +import java.io.File +import java.util.UUID +import kotlin.time.Clock + +@Single +class DefaultAttachmentService( + private val journalRepository: UploadJournalRepository, + private val attachmentRepository: AttachmentRepository, + private val chatRepository: ChatRepository, + private val objectStorageRepository: ObjectStorageRepository, +) : AttachmentService { + + override suspend fun requireMember( + chatId: UUID, + actorId: UUID + ) { + if (!suspendedTransaction { chatRepository.isMember(chatId, actorId) }) { + notFound() + } + } + + override suspend fun upload( + chatId: UUID, + actorId: UUID, + filename: String, + contentType: String, + file: File + ): Attachment { + + requireMember(chatId, actorId) + + val (type, hash) = withContext(Dispatchers.IO) { + val bytes = file.readBytes() + ImageValidator.validate(bytes, contentType) to bytes.computeSha256() + } + + val attachment = Attachment( + id = UUID.randomUUID(), + chatId = chatId, + ownerId = actorId, + filename = filename, + contentType = type, + sizeBytes = file.length(), + sha256 = hash, + createdAt = Clock.System.nowMicros() + ) + + suspendedTransaction { + journalRepository.register(attachment.id) + } + objectStorageRepository.put(attachment.id, file, type) + + suspendedTransaction { + if (!journalRepository.lockForCompletion(attachment.id)) { + throw ApiException(ErrorCode.UNAVAILABLE, "upload expired") + } + attachmentRepository.create(attachment) + journalRepository.complete(attachment.id) + } + return attachment + } + + override suspend fun find( + id: UUID, + actorId: UUID + ): Attachment = suspendedTransaction { + val attachment = attachmentRepository.findById(id) ?: notFound() + if (!chatRepository.isMember(attachment.chatId, actorId)) { + notFound() + } + attachment + } + + override suspend fun content(attachment: Attachment): ByteArray = withContext(Dispatchers.IO) { + objectStorageRepository.get(attachment.id) + } + + private fun notFound(): Nothing = throw ApiException(ErrorCode.NOT_FOUND, "attachment or chat not found") +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/ImageValidator.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/ImageValidator.kt new file mode 100644 index 00000000..c693f2c2 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/ImageValidator.kt @@ -0,0 +1,127 @@ +package com.mtusi.messenger.main.infrastructure.services.attachment + +import com.mtusi.messenger.main.infrastructure.types.ApiException +import com.mtusi.messenger.main.infrastructure.types.ErrorCode +import java.nio.ByteBuffer +import java.util.zip.CRC32 +import java.io.ByteArrayInputStream +import javax.imageio.ImageIO +import javax.imageio.stream.MemoryCacheImageInputStream + +const val MAX_ATTACHMENT_BYTES = 10 * 1024 * 1024 +private const val MAX_PIXELS = 40_000_000L + +object ImageValidator { + + fun validate( + bytes: ByteArray, + declaredType: String + ): String { + + if (bytes.size > MAX_ATTACHMENT_BYTES) { + throw ApiException(ErrorCode.PAYLOAD_TOO_LARGE, "file exceeds 10 MiB") + } + + try { + MemoryCacheImageInputStream(ByteArrayInputStream(bytes)).use { input -> + val readers = ImageIO.getImageReaders(input) + if (!readers.hasNext()) { + invalid() + } + + val reader = readers.next() + + try { + // Ignore optional compressed text metadata to bound memory usage. + reader.setInput(input, false, true) + + val type = when (reader.formatName.lowercase()) { + "png" -> "image/png" + "jpeg", "jpg" -> "image/jpeg" + else -> invalid() + } + + if (declaredType != type) { + invalid() + } + + if (type == "image/png") { + validatePngChunks(bytes) + } + + if (type == "image/jpeg" && ( + bytes.size < 4 || + bytes[bytes.lastIndex - 1] != 0xff.toByte() || + bytes.last() != 0xd9.toByte() + )) { + invalid() + } + + val width = reader.getWidth(0) + val height = reader.getHeight(0) + + if (width <= 0 || height <= 0) { + invalid() + } + + if (width.toLong() * height > MAX_PIXELS) { + throw ApiException(ErrorCode.PAYLOAD_TOO_LARGE, "image exceeds 40 million pixels") + } + + var warning = false + reader.addIIOReadWarningListener { _, _ -> warning = true } + + val image = reader.read(0) ?: invalid() + image.flush() + + if (warning) { + invalid() + } + + return type + } finally { + reader.dispose() + } + } + } catch (cause: ApiException) { + throw cause + } catch (_: Exception) { + invalid() + } + + } + + private fun validatePngChunks(bytes: ByteArray) { + + var offset = 8 + + while (offset + 12 <= bytes.size) { + val length = ByteBuffer.wrap(bytes, offset, 4).int + + if (length < 0 || length.toLong() + offset + 12 > bytes.size) { + invalid() + } + + val type = String(bytes, offset + 4, 4, Charsets.US_ASCII) + + val crc = CRC32().apply { update(bytes, offset + 4, length + 4) }.value + val expected = ByteBuffer.wrap(bytes, offset + 8 + length, 4).int.toLong() and 0xffffffffL + if (crc != expected) { + invalid() + } + + offset += length + 12 + if (type == "IEND") { + if (length != 0 || offset != bytes.size) { + invalid() + } + return + } + } + invalid() + } + + private fun invalid(): Nothing = throw ApiException( + ErrorCode.UNSUPPORTED_MEDIA_TYPE, "invalid PNG/JPEG or MIME mismatch" + ) +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/authentication/AuthenticationService.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/authentication/AuthenticationService.kt new file mode 100644 index 00000000..26d91bab --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/authentication/AuthenticationService.kt @@ -0,0 +1,38 @@ +package com.mtusi.messenger.main.infrastructure.services.authentication + +import com.mtusi.messenger.main.infrastructure.dto.User +import com.mtusi.messenger.main.infrastructure.dto.authentication.IssuedSession +import com.mtusi.messenger.main.infrastructure.dto.authentication.Session +import java.util.UUID + +interface AuthenticationService { + + fun validateCredentials( + username: String, + password: String + ): User? + + fun createSession( + userId: UUID + ): IssuedSession + + suspend fun findSessionByToken( + token: String + ): Session? + + /** + * Deletes the session and confirms the revocation in the shared session cache, so that no replica accepts the + * token afterwards. Throws [com.mtusi.messenger.main.infrastructure.types.ApiException] (503) if the cache did + * not accept the tombstone in time: the logout is then not confirmed. + */ + suspend fun revokeSession( + session: Session + ): Boolean + + /** + * Called periodically on every replica: a revocation still waiting for its tombstone (written by any replica) + * means cached sessions cannot be trusted until it is written. + */ + suspend fun syncPendingRevocations() + +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/authentication/DefaultAuthenticationService.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/authentication/DefaultAuthenticationService.kt new file mode 100644 index 00000000..c8953367 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/authentication/DefaultAuthenticationService.kt @@ -0,0 +1,323 @@ +package com.mtusi.messenger.main.infrastructure.services.authentication + +import com.mtusi.messenger.main.config.authentication.AuthenticationConfig +import com.mtusi.messenger.main.infrastructure.dto.User +import com.mtusi.messenger.main.infrastructure.dto.authentication.IssuedSession +import com.mtusi.messenger.main.infrastructure.dto.authentication.Session +import com.mtusi.messenger.main.infrastructure.dto.authentication.SessionRevocation +import com.mtusi.messenger.main.infrastructure.repositories.authentication.session.SessionRepository +import com.mtusi.messenger.main.infrastructure.repositories.authentication.session.cache.CachedSession +import com.mtusi.messenger.main.infrastructure.repositories.authentication.session.cache.SessionCacheRepository +import com.mtusi.messenger.main.infrastructure.repositories.authentication.session.revocation.SessionRevocationRepository +import com.mtusi.messenger.main.infrastructure.repositories.user.UserRepository +import com.mtusi.messenger.main.infrastructure.services.hashing.HashingService +import com.mtusi.messenger.main.infrastructure.services.hashing.types.HashingAlgorithm +import com.mtusi.messenger.main.infrastructure.types.ApiException +import com.mtusi.messenger.main.infrastructure.types.ErrorCode +import com.mtusi.messenger.main.plugins.suspendedTransaction +import com.mtusi.messenger.main.utils.computeSha256 +import io.lettuce.core.RedisException +import kotlinx.coroutines.CancellationException +import kotlinx.coroutines.CompletableDeferred +import kotlinx.coroutines.NonCancellable +import kotlinx.coroutines.delay +import kotlinx.coroutines.sync.Mutex +import kotlinx.coroutines.withContext +import org.koin.core.annotation.Single +import org.slf4j.LoggerFactory +import java.io.IOException +import java.security.SecureRandom +import java.util.Base64 +import java.util.UUID +import java.util.concurrent.ConcurrentHashMap +import java.util.concurrent.atomic.AtomicBoolean +import java.util.concurrent.atomic.AtomicLong +import kotlin.time.Clock +import kotlin.time.Instant +import kotlin.time.Duration.Companion.milliseconds +import kotlin.time.Duration.Companion.seconds +import kotlin.time.TimeSource + +private const val TOKEN_BYTES = 32 + +private val CACHE_RESTORE_BACKOFF = 1.seconds + +private val REVOKE_RETRY_DELAY = 250.milliseconds + +@Single +class DefaultAuthenticationService( + private val userRepository: UserRepository, + private val sessionRepository: SessionRepository, + private val sessionCacheRepository: SessionCacheRepository, + private val sessionRevocationRepository: SessionRevocationRepository, + private val hashingService: HashingService, + private val config: AuthenticationConfig +) : AuthenticationService { + + private val logger = LoggerFactory.getLogger(DefaultAuthenticationService::class.java) + + private val random = SecureRandom() + + private val inFlightLookups = ConcurrentHashMap>() + + private val isCacheAvailable = AtomicBoolean(true) + + private val cacheFailures = AtomicLong() + + @Volatile + private var trustedAtFailures = -1L + + private val cacheRestoreMutex = Mutex() + + @Volatile + private var nextCacheRestoreAttempt = TimeSource.Monotonic.markNow() + + private val dummyPasswordHash by lazy { + hashingService.computeHash(HashingAlgorithm.Argon2id, "dummy-password-for-timing") + } + + override fun validateCredentials( + username: String, + password: String + ): User? { + + val credentials = userRepository.findCredentialsByUsername(username) + + val isPasswordValid = hashingService.validateHash( + algorithm = HashingAlgorithm.Argon2id, + input = password, + hashed = credentials?.passwordHash ?: dummyPasswordHash + ) + + if (credentials == null || !isPasswordValid) { + return null + } + + return credentials.user + } + + override fun createSession( + userId: UUID + ): IssuedSession { + + val token = ByteArray(TOKEN_BYTES) + .also(random::nextBytes) + .let(Base64.getUrlEncoder().withoutPadding()::encodeToString) + + val session = sessionRepository.create( + userId = userId, + tokenHash = token.toByteArray().computeSha256(), + ttl = config.sessionTtlSeconds.seconds + ) + + return IssuedSession( + session = session, + token = token + ) + } + + override suspend fun findSessionByToken( + token: String + ): Session? { + + val tokenHash = token.toByteArray().computeSha256() + val now = Clock.System.now() + + when (val cached = findCachedSession(tokenHash)) { + is CachedSession.Revoked -> return null + is CachedSession.Active -> return cached.session.takeIf { it.expiresAt > now } + null -> Unit + } + + if (!config.sessionCacheSingleFlight) { + return selectSession(tokenHash, now)?.also { cacheSession(it, now) } + } + + val flight = CompletableDeferred() + + inFlightLookups.putIfAbsent(tokenHash, flight)?.let { leader -> + return leader.await() + } + + try { + // The query is shared with the followers, so the leader's own cancellation must not abort it. + val session = withContext(NonCancellable) { + selectSession(tokenHash, now) + } + + flight.complete(session) + + // Requests arriving until the cache write lands reuse this result instead of starting a new lookup. + // A logout drops the flight first (see revokeSession), so a revoked session is never handed out here. + session?.let { cacheSession(it, now) } + + return session + } catch (e: Throwable) { + flight.completeExceptionally(e) + throw e + } finally { + inFlightLookups.remove(tokenHash, flight) + } + } + + private suspend fun selectSession( + tokenHash: String, + now: Instant + ): Session? { + return suspendedTransaction { + sessionRepository.findActiveByTokenHash(tokenHash, now) + } + } + + override suspend fun revokeSession( + session: Session + ): Boolean { + + // The revocation is kept in PostgreSQL until its tombstone is in Redis: if Redis is unavailable now, + // the tombstone is written when the cache is trusted again (see restoreCache), even after a restart. + val isDeleted = suspendedTransaction { + sessionRevocationRepository.add(SessionRevocation(session.tokenHash, session.expiresAt)) + sessionRepository.deleteById(session.id) + } + + // A lookup that started before the delete must not be joined by requests arriving after the logout. + inFlightLookups.remove(session.tokenHash) + + // Other replicas may hold the session in the shared cache: 204 is only allowed once the tombstone is there. + // Redis that rejects or delays writes (CLIENT PAUSE WRITE, reconnect) gets retried until the deadline. + val deadline = TimeSource.Monotonic.markNow() + config.sessionRevokeTimeoutMs.milliseconds + + while (withCache { sessionCacheRepository.revoke(session.tokenHash, session.expiresAt) } == null) { + if (deadline.hasPassedNow()) { + // The row stays in session_revocations: every replica stops trusting the cache within a second + // (syncPendingRevocations) and the tombstone is written as soon as Redis accepts writes again. + throw ApiException(ErrorCode.UNAVAILABLE, "logout is not confirmed: session cache is unavailable") + } + delay(REVOKE_RETRY_DELAY) + } + + // Best effort: a leftover row only costs one more tombstone write on the next restore. + runCatchingDatabase { sessionRevocationRepository.delete(session.tokenHash) } + + return isDeleted + } + + override suspend fun syncPendingRevocations() { + + val hasPending = runCatchingDatabase { + sessionRevocationRepository.findPending(Clock.System.now()).isNotEmpty() + } ?: return + + if (hasPending && trustedAtFailures == cacheFailures.get()) { + // Reuses the failure counter: an untrusted cache is bypassed until restoreCache writes the tombstones. + cacheFailures.incrementAndGet() + logger.warn("Pending session revocations found, cached sessions are not trusted until they are written") + } + + if (hasPending) { + restoreCache() + } + } + + private suspend fun findCachedSession( + tokenHash: String + ): CachedSession? { + if (trustedAtFailures != cacheFailures.get() && !restoreCache()) { + return null + } + return withCache { sessionCacheRepository.find(tokenHash) } + } + + private suspend fun restoreCache(): Boolean { + + if (!cacheRestoreMutex.tryLock()) { + return false + } + + try { + val failures = cacheFailures.get() + + if (trustedAtFailures == failures) { + return true + } + if (nextCacheRestoreAttempt.hasNotPassedNow()) { + return false + } + nextCacheRestoreAttempt = TimeSource.Monotonic.markNow() + CACHE_RESTORE_BACKOFF + + val now = Clock.System.now() + + val pending = runCatchingDatabase { + sessionRevocationRepository.deleteExpired(now) + sessionRevocationRepository.findPending(now) + } ?: return false + + for (revocation in pending) { + withCache { sessionCacheRepository.revoke(revocation.tokenHash, revocation.expiresAt) } ?: return false + } + + if (pending.isNotEmpty()) { + runCatchingDatabase { pending.forEach { sessionRevocationRepository.delete(it.tokenHash) } } + logger.info("Wrote ${pending.size} pending session revocations to the cache") + } + + // A failure while restoring changed the counter, so the cache stays untrusted. + trustedAtFailures = failures + return trustedAtFailures == cacheFailures.get() + } finally { + cacheRestoreMutex.unlock() + } + } + + private suspend fun runCatchingDatabase( + statement: () -> T + ): T? { + return try { + suspendedTransaction { statement() } + } catch (e: CancellationException) { + throw e + } catch (e: Exception) { + logger.warn("Unable to update pending session revocations: ${e.message}") + null + } + } + + private suspend fun cacheSession( + session: Session, + now: Instant + ) { + withCache { + sessionCacheRepository.putIfAbsent( + session = session, + cachedUntil = minOf(session.expiresAt, now + config.sessionCacheTtlSeconds.seconds) + ) + } + } + + private suspend fun withCache( + operation: suspend () -> T + ): T? { + return try { + operation().also { + if (isCacheAvailable.compareAndSet(false, true)) { + logger.info("Session cache is available again") + } + } + } catch (e: RedisException) { + onCacheUnavailable(e) + } catch (e: IOException) { + onCacheUnavailable(e) + } + } + + private fun onCacheUnavailable( + cause: Exception + ): Nothing? { + cacheFailures.incrementAndGet() + if (isCacheAvailable.compareAndSet(true, false)) { + logger.warn("Session cache is unavailable, sessions are read from the database: ${cause.message}") + } + return null + } + +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/chat/ChatService.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/chat/ChatService.kt new file mode 100644 index 00000000..603abc9f --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/chat/ChatService.kt @@ -0,0 +1,27 @@ +package com.mtusi.messenger.main.infrastructure.services.chat + +import com.mtusi.messenger.main.infrastructure.dto.Chat +import com.mtusi.messenger.main.infrastructure.services.chat.results.ChatCreationResult +import com.mtusi.messenger.main.infrastructure.services.chat.results.ChatPageResult +import java.util.UUID + +interface ChatService { + + fun create( + creatorId: UUID, + title: String, + memberIds: List + ): ChatCreationResult + + fun findAccessible( + userId: UUID, + chatId: UUID + ): Chat? + + fun findPageAccessible( + userId: UUID, + limit: Int, + cursor: String? + ): ChatPageResult + +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/chat/DefaultChatService.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/chat/DefaultChatService.kt new file mode 100644 index 00000000..b38620c5 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/chat/DefaultChatService.kt @@ -0,0 +1,79 @@ +package com.mtusi.messenger.main.infrastructure.services.chat + +import com.mtusi.messenger.main.infrastructure.dto.Chat +import com.mtusi.messenger.main.infrastructure.repositories.chat.ChatRepository +import com.mtusi.messenger.main.infrastructure.repositories.user.UserRepository +import com.mtusi.messenger.main.infrastructure.services.chat.results.ChatCreationResult +import com.mtusi.messenger.main.infrastructure.services.chat.results.ChatPageResult +import com.mtusi.messenger.main.utils.PageKey +import com.mtusi.messenger.main.utils.decodeCursor +import com.mtusi.messenger.main.utils.toPage +import org.koin.core.annotation.Single +import java.util.UUID + +@Single +class DefaultChatService( + private val chatRepository: ChatRepository, + private val userRepository: UserRepository +) : ChatService { + + override fun create( + creatorId: UUID, + title: String, + memberIds: List + ): ChatCreationResult { + + if (memberIds.toSet().size != memberIds.size) { + return ChatCreationResult.DuplicateMembers + } + + if (creatorId !in memberIds) { + return ChatCreationResult.CreatorNotMember + } + + if (userRepository.countByIds(memberIds) != memberIds.size.toLong()) { + return ChatCreationResult.UnknownMembers + } + + val chat = chatRepository.create( + title = title, + memberIds = memberIds + ) + + return ChatCreationResult.Success(chat) + } + + override fun findAccessible( + userId: UUID, + chatId: UUID + ): Chat? { + return chatRepository.findById(chatId) + ?.takeIf { userId in it.memberIds } + } + + override fun findPageAccessible( + userId: UUID, + limit: Int, + cursor: String? + ): ChatPageResult { + + val scope = "chats:$userId" + + val after = cursor?.let { + decodeCursor(it, scope) ?: return ChatPageResult.InvalidCursor + } + + val page = chatRepository + .findPageByMemberId( + userId = userId, + after = after, + limit = limit + 1 + ) + .toPage(limit, scope) { + PageKey(it.createdAt, it.id) + } + + return ChatPageResult.Success(page) + } + +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/chat/results/ChatCreationResult.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/chat/results/ChatCreationResult.kt new file mode 100644 index 00000000..61c35511 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/chat/results/ChatCreationResult.kt @@ -0,0 +1,17 @@ +package com.mtusi.messenger.main.infrastructure.services.chat.results + +import com.mtusi.messenger.main.infrastructure.dto.Chat + +sealed class ChatCreationResult { + + sealed class Error : ChatCreationResult() + + data object DuplicateMembers : Error() + data object CreatorNotMember : Error() + data object UnknownMembers : Error() + + data class Success( + val chat: Chat + ) : ChatCreationResult() + +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/chat/results/ChatPageResult.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/chat/results/ChatPageResult.kt new file mode 100644 index 00000000..d347bad6 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/chat/results/ChatPageResult.kt @@ -0,0 +1,16 @@ +package com.mtusi.messenger.main.infrastructure.services.chat.results + +import com.mtusi.messenger.main.infrastructure.dto.Chat +import com.mtusi.messenger.main.infrastructure.dto.PageResponse + +sealed class ChatPageResult { + + sealed class Error : ChatPageResult() + + data object InvalidCursor : Error() + + data class Success( + val page: PageResponse + ) : ChatPageResult() + +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/hashing/DefaultHashingService.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/hashing/DefaultHashingService.kt new file mode 100644 index 00000000..d18110ac --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/hashing/DefaultHashingService.kt @@ -0,0 +1,123 @@ +package com.mtusi.messenger.main.infrastructure.services.hashing + +import com.mtusi.messenger.main.infrastructure.services.hashing.types.HashingAlgorithm +import org.bouncycastle.crypto.generators.Argon2BytesGenerator +import org.bouncycastle.crypto.params.Argon2Parameters +import org.koin.core.annotation.Single +import java.security.MessageDigest +import java.security.SecureRandom +import java.util.Base64 + +// TODO: Move to config later +private const val ARGON2_MEMORY_KIB = 19 * 1024 +private const val ARGON2_ITERATIONS = 2 +private const val ARGON2_PARALLELISM = 1 +private const val ARGON2_SALT_LENGTH = 16 +private const val ARGON2_HASH_LENGTH = 32 + +@Single +class DefaultHashingService : HashingService { + + private val random = SecureRandom() + private val encoder = Base64.getEncoder().withoutPadding() + private val decoder = Base64.getDecoder() + + override fun computeHash( + algorithm: HashingAlgorithm, + input: String + ): String { + return when (algorithm) { + HashingAlgorithm.Argon2id -> computeHashArgon2id(input) + } + } + + private fun computeHashArgon2id(input: String): String { + + val salt = ByteArray(ARGON2_SALT_LENGTH).also(random::nextBytes) + + val hash = argon2id( + input = input, + salt = salt, + memoryKib = ARGON2_MEMORY_KIB, + iterations = ARGON2_ITERATIONS, + parallelism = ARGON2_PARALLELISM, + length = ARGON2_HASH_LENGTH + ) + + return "\$argon2id\$v=19\$m=$ARGON2_MEMORY_KIB,t=$ARGON2_ITERATIONS,p=$ARGON2_PARALLELISM" + + "\$${encoder.encodeToString(salt)}\$${encoder.encodeToString(hash)}" + } + + override fun validateHash( + algorithm: HashingAlgorithm, + input: String, + hashed: String + ): Boolean { + return when (algorithm) { + HashingAlgorithm.Argon2id -> validateHashArgon2id(input, hashed) + } + } + + private fun validateHashArgon2id( + input: String, + hashed: String + ): Boolean { + return try { + + val parts = hashed.split('$') + if (parts.size != 6 || parts[1] != "argon2id" || parts[2] != "v=19") { + return false + } + + val parameters = parts[3] + .split(',') + .associate { + val (key, value) = it.split('=') + key to value.toInt() + } + + val salt = decoder.decode(parts[4]) + val expected = decoder.decode(parts[5]) + + val actual = argon2id( + input = input, + salt = salt, + memoryKib = parameters.getValue("m"), + iterations = parameters.getValue("t"), + parallelism = parameters.getValue("p"), + length = expected.size + ) + + MessageDigest.isEqual(expected, actual) + } catch (_: Exception) { + false + } + } + + private fun argon2id( + input: String, + salt: ByteArray, + memoryKib: Int, + iterations: Int, + parallelism: Int, + length: Int + ): ByteArray { + + val parameters = Argon2Parameters.Builder(Argon2Parameters.ARGON2_id) + .withVersion(Argon2Parameters.ARGON2_VERSION_13) + .withMemoryAsKB(memoryKib) + .withIterations(iterations) + .withParallelism(parallelism) + .withSalt(salt) + .build() + + val generator = Argon2BytesGenerator().apply { + init(parameters) + } + + return ByteArray(length).also { + generator.generateBytes(input.toByteArray(Charsets.UTF_8), it) + } + } + +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/hashing/HashingService.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/hashing/HashingService.kt new file mode 100644 index 00000000..0de59beb --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/hashing/HashingService.kt @@ -0,0 +1,18 @@ +package com.mtusi.messenger.main.infrastructure.services.hashing + +import com.mtusi.messenger.main.infrastructure.services.hashing.types.HashingAlgorithm + +interface HashingService { + + fun computeHash( + algorithm: HashingAlgorithm, + input: String + ): String + + fun validateHash( + algorithm: HashingAlgorithm, + input: String, + hashed: String + ): Boolean + +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/hashing/types/HashingAlgorithm.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/hashing/types/HashingAlgorithm.kt new file mode 100644 index 00000000..8f39402b --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/hashing/types/HashingAlgorithm.kt @@ -0,0 +1,5 @@ +package com.mtusi.messenger.main.infrastructure.services.hashing.types + +sealed class HashingAlgorithm { + data object Argon2id : HashingAlgorithm() +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/message/DefaultMessageService.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/message/DefaultMessageService.kt new file mode 100644 index 00000000..c91ca0ae --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/message/DefaultMessageService.kt @@ -0,0 +1,136 @@ +package com.mtusi.messenger.main.infrastructure.services.message + +import com.mtusi.messenger.main.infrastructure.types.ErrorCode +import com.mtusi.messenger.main.infrastructure.types.ApiException +import com.mtusi.messenger.main.infrastructure.repositories.attachment.AttachmentRepository +import com.mtusi.messenger.main.config.message.MessageConfig +import com.mtusi.messenger.main.infrastructure.repositories.chat.ChatRepository +import com.mtusi.messenger.main.infrastructure.repositories.message.MessageRepository +import com.mtusi.messenger.main.infrastructure.repositories.message.idempotency.MessageIdempotencyRepository +import com.mtusi.messenger.main.infrastructure.services.message.results.MessageCreationResult +import com.mtusi.messenger.main.infrastructure.services.message.results.MessagePageResult +import com.mtusi.messenger.main.utils.PageKey +import com.mtusi.messenger.main.utils.computeSha256 +import com.mtusi.messenger.main.utils.decodeCursor +import com.mtusi.messenger.main.utils.nowMicros +import com.mtusi.messenger.main.utils.toPage +import org.koin.core.annotation.Single +import java.util.UUID +import kotlin.time.Clock +import kotlin.time.Duration.Companion.seconds + +// One attempt to claim a free key, plus one more if the stored key had expired and was removed +private const val CLAIM_ATTEMPTS = 2 + +@Single +class DefaultMessageService( + private val chatRepository: ChatRepository, + private val messageRepository: MessageRepository, + private val idempotencyRepository: MessageIdempotencyRepository, + private val attachments: AttachmentRepository, + private val config: MessageConfig +) : MessageService { + + override fun send( + chatId: UUID, + senderId: UUID, + text: String, + idempotencyKey: String, + attachmentIds: List + ): MessageCreationResult { + + // Access is checked on every retry too: a replay must not outlive the membership. + if (!chatRepository.isMember(chatId, senderId)) { + return MessageCreationResult.ChatNotFound + } + + attachmentIds.forEach { id -> + if (attachments.findById(id)?.chatId != chatId) + throw ApiException( + ErrorCode.NOT_FOUND, "attachment not found") + } + + val requestHash = (text + if (attachmentIds.isEmpty()) "" else "\u0000attachments:" + attachmentIds.joinToString(",")) + .toByteArray().computeSha256() + + val now = Clock.System.nowMicros() + + repeat(CLAIM_ATTEMPTS) { + + if (idempotencyRepository.claim(senderId, chatId, idempotencyKey, requestHash, now)) { + + val message = messageRepository.create( + chatId = chatId, + senderId = senderId, + text = text + ) + attachments.attach(message.id, attachmentIds) + idempotencyRepository.attachMessage(senderId, chatId, idempotencyKey, message.id) + + return MessageCreationResult.Success(message.copy(attachmentIds = attachmentIds)) + } + + // The key is taken by a committed transaction (claim waits for an uncommitted one). + val record = idempotencyRepository.find(senderId, chatId, idempotencyKey) + ?: return@repeat + + val expiredBefore = now - config.idempotencyTtlSeconds.seconds + + if (record.createdAt < expiredBefore) { + idempotencyRepository.deleteExpired(senderId, chatId, idempotencyKey, createdBefore = expiredBefore) + return@repeat + } + + if (record.requestHash != requestHash) { + return MessageCreationResult.IdempotencyKeyReused + } + + val messageId = checkNotNull(record.messageId) { + "committed idempotency key without a message" + } + + val original = checkNotNull(messageRepository.findById(messageId)) { + "message of an idempotency key is missing" + } + + if (original.text != text || original.attachmentIds != attachmentIds) { + return MessageCreationResult.IdempotencyKeyReused + } + + return MessageCreationResult.Success(original) + } + + error("Idempotency key could not be claimed") + } + + override fun findPageByChatId( + chatId: UUID, + actorId: UUID, + limit: Int, + cursor: String? + ): MessagePageResult { + + if (!chatRepository.isMember(chatId, actorId)) { + return MessagePageResult.ChatNotFound + } + + val scope = "messages:$chatId" + + val after = cursor?.let { + decodeCursor(it, scope) ?: return MessagePageResult.InvalidCursor + } + + val page = messageRepository + .findPageByChatId( + chatId = chatId, + after = after, + limit = limit + 1 + ) + .toPage(limit, scope) { + PageKey(it.createdAt, it.id) + } + + return MessagePageResult.Success(page) + } + +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/message/MessageService.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/message/MessageService.kt new file mode 100644 index 00000000..17fdabc2 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/message/MessageService.kt @@ -0,0 +1,24 @@ +package com.mtusi.messenger.main.infrastructure.services.message + +import com.mtusi.messenger.main.infrastructure.services.message.results.MessageCreationResult +import com.mtusi.messenger.main.infrastructure.services.message.results.MessagePageResult +import java.util.UUID + +interface MessageService { + + fun send( + chatId: UUID, + senderId: UUID, + text: String, + idempotencyKey: String, + attachmentIds: List = emptyList() + ): MessageCreationResult + + fun findPageByChatId( + chatId: UUID, + actorId: UUID, + limit: Int, + cursor: String? + ): MessagePageResult + +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/message/results/MessageCreationResult.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/message/results/MessageCreationResult.kt new file mode 100644 index 00000000..1f260a32 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/message/results/MessageCreationResult.kt @@ -0,0 +1,17 @@ +package com.mtusi.messenger.main.infrastructure.services.message.results + +import com.mtusi.messenger.main.infrastructure.dto.Message + +sealed class MessageCreationResult { + + sealed class Error : MessageCreationResult() + + data object ChatNotFound : Error() + + data object IdempotencyKeyReused : Error() + + data class Success( + val message: Message + ) : MessageCreationResult() + +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/message/results/MessagePageResult.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/message/results/MessagePageResult.kt new file mode 100644 index 00000000..254b1d52 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/message/results/MessagePageResult.kt @@ -0,0 +1,17 @@ +package com.mtusi.messenger.main.infrastructure.services.message.results + +import com.mtusi.messenger.main.infrastructure.dto.Message +import com.mtusi.messenger.main.infrastructure.dto.PageResponse + +sealed class MessagePageResult { + + sealed class Error : MessagePageResult() + + data object ChatNotFound : Error() + data object InvalidCursor : Error() + + data class Success( + val page: PageResponse + ) : MessagePageResult() + +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/user/DefaultUserService.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/user/DefaultUserService.kt new file mode 100644 index 00000000..618ca129 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/user/DefaultUserService.kt @@ -0,0 +1,43 @@ +package com.mtusi.messenger.main.infrastructure.services.user + +import com.mtusi.messenger.main.infrastructure.dto.User +import com.mtusi.messenger.main.infrastructure.repositories.user.UserRepository +import com.mtusi.messenger.main.infrastructure.services.hashing.HashingService +import com.mtusi.messenger.main.infrastructure.services.hashing.types.HashingAlgorithm +import com.mtusi.messenger.main.infrastructure.services.user.results.UserCreationResult +import org.koin.core.annotation.Single +import java.util.UUID + +@Single +class DefaultUserService( + private val userRepository: UserRepository, + private val hashingService: HashingService +) : UserService { + + override fun create( + username: String, + displayName: String, + password: String + ): UserCreationResult { + + val passwordHash = hashingService.computeHash( + algorithm = HashingAlgorithm.Argon2id, + input = password + ) + + val user = userRepository.create( + username = username, + displayName = displayName, + passwordHash = passwordHash + ) ?: return UserCreationResult.UsernameTaken + + return UserCreationResult.Success(user) + } + + override fun findById( + id: UUID + ): User? { + return userRepository.findById(id) + } + +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/user/UserService.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/user/UserService.kt new file mode 100644 index 00000000..da9528ed --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/user/UserService.kt @@ -0,0 +1,19 @@ +package com.mtusi.messenger.main.infrastructure.services.user + +import com.mtusi.messenger.main.infrastructure.dto.User +import com.mtusi.messenger.main.infrastructure.services.user.results.UserCreationResult +import java.util.UUID + +interface UserService { + + fun create( + username: String, + displayName: String, + password: String + ): UserCreationResult + + fun findById( + id: UUID + ): User? + +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/user/results/UserCreationResult.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/user/results/UserCreationResult.kt new file mode 100644 index 00000000..ae51bf5d --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/user/results/UserCreationResult.kt @@ -0,0 +1,15 @@ +package com.mtusi.messenger.main.infrastructure.services.user.results + +import com.mtusi.messenger.main.infrastructure.dto.User + +sealed class UserCreationResult { + + sealed class Error : UserCreationResult() + + data object UsernameTaken : Error() + + data class Success( + val user: User + ) : UserCreationResult() + +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/types/ApiException.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/types/ApiException.kt new file mode 100644 index 00000000..90021402 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/types/ApiException.kt @@ -0,0 +1,3 @@ +package com.mtusi.messenger.main.infrastructure.types + +class ApiException(val code: ErrorCode, message: String) : RuntimeException(message) diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/types/AuthChallenge.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/types/AuthChallenge.kt new file mode 100644 index 00000000..d8f23b56 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/types/AuthChallenge.kt @@ -0,0 +1,6 @@ +package com.mtusi.messenger.main.infrastructure.types + +object AuthChallenge { + const val BASIC = "Basic realm=\"messenger\", charset=\"UTF-8\"" + const val BEARER = "Bearer realm=\"messenger\"" +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/types/ErrorCode.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/types/ErrorCode.kt new file mode 100644 index 00000000..d71aef95 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/types/ErrorCode.kt @@ -0,0 +1,15 @@ +package com.mtusi.messenger.main.infrastructure.types + +import io.ktor.http.HttpStatusCode + +enum class ErrorCode(val wire: String, val status: HttpStatusCode) { + INVALID_REQUEST("invalid_request", HttpStatusCode.BadRequest), + UNAUTHORIZED("unauthorized", HttpStatusCode.Unauthorized), + NOT_FOUND("not_found", HttpStatusCode.NotFound), + CONFLICT("conflict", HttpStatusCode.Conflict), + PAYLOAD_TOO_LARGE("payload_too_large", HttpStatusCode.PayloadTooLarge), + UNSUPPORTED_MEDIA_TYPE("unsupported_media_type", HttpStatusCode.UnsupportedMediaType), + RATE_LIMITED("rate_limited", HttpStatusCode.TooManyRequests), + UNAVAILABLE("unavailable", HttpStatusCode.ServiceUnavailable), + INTERNAL_ERROR("internal_error", HttpStatusCode.InternalServerError), +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/plugins/AttachmentCleanup.kt b/src/main/kotlin/com/mtusi/messenger/main/plugins/AttachmentCleanup.kt new file mode 100644 index 00000000..76f69853 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/plugins/AttachmentCleanup.kt @@ -0,0 +1,29 @@ +package com.mtusi.messenger.main.plugins + +import com.mtusi.messenger.main.infrastructure.services.attachment.AttachmentCleanupService +import io.ktor.server.application.Application +import io.ktor.server.application.log +import kotlinx.coroutines.CancellationException +import kotlinx.coroutines.delay +import kotlinx.coroutines.isActive +import kotlinx.coroutines.launch +import org.koin.ktor.ext.inject +import kotlin.time.Duration.Companion.seconds + +fun Application.configureAttachmentCleanup() { + + val cleanupService by inject() + + launch { + while (isActive) { + delay(30.seconds) + try { + cleanupService.cleanup() + } catch (cause: CancellationException) { + throw cause + } catch (_: Exception) { + log.warn("Attachment cleanup journal is unavailable") + } + } + } +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/plugins/CallLogging.kt b/src/main/kotlin/com/mtusi/messenger/main/plugins/CallLogging.kt new file mode 100644 index 00000000..c9148e6b --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/plugins/CallLogging.kt @@ -0,0 +1,14 @@ +package com.mtusi.messenger.main.plugins + +import io.ktor.server.application.Application +import io.ktor.server.application.install +import io.ktor.server.plugins.calllogging.CallLogging +import org.slf4j.event.Level + +fun Application.configureCallLogging() = install(CallLogging) { + level = Level.INFO + filter { call -> + val uri = call.request.local.uri + uri.startsWith("/api") || uri.startsWith("/health") + } +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/plugins/ContentNegotiation.kt b/src/main/kotlin/com/mtusi/messenger/main/plugins/ContentNegotiation.kt new file mode 100644 index 00000000..beb9fffb --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/plugins/ContentNegotiation.kt @@ -0,0 +1,21 @@ +package com.mtusi.messenger.main.plugins + +import io.ktor.serialization.kotlinx.json.json +import io.ktor.server.application.Application +import io.ktor.server.application.install +import io.ktor.server.plugins.contentnegotiation.ContentNegotiation +import kotlinx.serialization.ExperimentalSerializationApi +import kotlinx.serialization.json.Json +import kotlinx.serialization.json.JsonNamingStrategy + +@OptIn(ExperimentalSerializationApi::class) +val json = Json { + ignoreUnknownKeys = false + explicitNulls = true + encodeDefaults = true + namingStrategy = JsonNamingStrategy.SnakeCase +} + +fun Application.configureContentNegotiation() = install(ContentNegotiation) { + json(json) +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/plugins/Cors.kt b/src/main/kotlin/com/mtusi/messenger/main/plugins/Cors.kt new file mode 100644 index 00000000..de5c66b1 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/plugins/Cors.kt @@ -0,0 +1,13 @@ +package com.mtusi.messenger.main.plugins + +import com.mtusi.messenger.main.config.cors.CorsConfig +import com.mtusi.messenger.main.config.cors.configure +import io.ktor.server.application.Application +import io.ktor.server.application.install +import io.ktor.server.plugins.cors.routing.CORS +import org.koin.ktor.ext.inject + +fun Application.configureCors() = install(CORS) { + val config by this@configureCors.inject() + configure(config) +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/plugins/Database.kt b/src/main/kotlin/com/mtusi/messenger/main/plugins/Database.kt new file mode 100644 index 00000000..d54e0b83 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/plugins/Database.kt @@ -0,0 +1,181 @@ +package com.mtusi.messenger.main.plugins + +import com.mtusi.messenger.main.infrastructure.models.attachment.MessageAttachmentModel +import com.mtusi.messenger.main.infrastructure.models.attachment.UploadJournalModel +import com.mtusi.messenger.main.infrastructure.models.attachment.AttachmentModel +import com.mtusi.messenger.main.config.database.DatabaseConfig +import com.mtusi.messenger.main.config.database.SerialDatabaseConfig +import com.mtusi.messenger.main.infrastructure.models.authentication.SessionModel +import com.mtusi.messenger.main.infrastructure.models.authentication.SessionRevocationModel +import com.mtusi.messenger.main.infrastructure.models.chat.ChatMemberModel +import com.mtusi.messenger.main.infrastructure.models.chat.ChatModel +import com.mtusi.messenger.main.infrastructure.models.message.MessageIdempotencyKeyModel +import com.mtusi.messenger.main.infrastructure.models.message.MessageModel +import com.mtusi.messenger.main.infrastructure.models.user.UserModel +import com.mtusi.messenger.main.utils.resolveSecret +import com.typesafe.config.ConfigFactory +import com.zaxxer.hikari.HikariConfig +import com.zaxxer.hikari.HikariDataSource +import io.ktor.server.application.Application +import io.ktor.server.application.ApplicationStopped +import io.ktor.server.config.HoconApplicationConfig +import io.ktor.server.config.getAs +import kotlinx.coroutines.Dispatchers +import org.flywaydb.core.Flyway +import org.jetbrains.exposed.v1.core.ExperimentalDatabaseMigrationApi +import org.jetbrains.exposed.v1.core.Table +import org.jetbrains.exposed.v1.core.DatabaseConfig as ExposedDatabaseConfig +import org.jetbrains.exposed.v1.jdbc.Database +import org.jetbrains.exposed.v1.jdbc.JdbcTransaction +import org.jetbrains.exposed.v1.jdbc.transactions.experimental.newSuspendedTransaction +import org.jetbrains.exposed.v1.jdbc.transactions.transaction +import org.jetbrains.exposed.v1.migration.jdbc.MigrationUtils +import org.koin.ktor.ext.inject +import java.io.File +import javax.sql.DataSource + +private const val MIGRATIONS_DIRECTORY = "src/main/resources/migrations" +private const val DEVELOPMENT_DATABASE_CONFIG = "run/development/config/database.conf" +private const val VALIDATION_TIMEOUT_MS = 1000L + +fun Application.configureDatabase() { + + val config by inject() + + val dataSource = config.createDataSource() + + Database.connect( + datasource = dataSource, + databaseConfig = ExposedDatabaseConfig { + defaultMaxAttempts = 1 + } + ) + if (config.migrateOnStart) { + applyDatabaseMigrations(dataSource) + } else { + validateDatabaseMigrations(dataSource) + } + + monitor.subscribe(ApplicationStopped) { + dataSource.close() + } + +} + +private fun DatabaseConfig.createDataSource(): HikariDataSource { + return HikariDataSource( + HikariConfig().apply { + poolName = "messenger" + jdbcUrl = jdbcConnectionUrl + username = this@createDataSource.username + password = resolveSecret(this@createDataSource.password, passwordFile).orEmpty() + maximumPoolSize = maxPoolSize + connectionTimeout = connectionTimeoutMs + validationTimeout = minOf(VALIDATION_TIMEOUT_MS, connectionTimeoutMs - 1) + addDataSourceProperty("options", "-c statement_timeout=$statementTimeoutMs") + } + ) +} + +private fun flyway(dataSource: DataSource) = Flyway.configure() + .dataSource(dataSource) + .locations("classpath:migrations") + .load() + +fun applyDatabaseMigrations( + dataSource: DataSource +) { + val flyway = flyway(dataSource) + flyway.migrate() + flyway.validate() +} + +/** + * Fails if a migration is pending or differs from the applied one, so a replica never serves an outdated schema. + */ +fun validateDatabaseMigrations( + dataSource: DataSource +) { + flyway(dataSource).validate() +} + +/** + * Entry point of `java -jar messenger.jar migrate`: applies the migrations once and exits. + * Uses the `database` section of application.conf, environment variables included. + */ +fun migrateDatabase() { + val config = HoconApplicationConfig(ConfigFactory.load()) + .property("database") + .getAs() + + config.createDataSource().use(::applyDatabaseMigrations) +} + +fun main(args: Array) { + generateMigration(args.joinToString("_")) +} + +@OptIn(ExperimentalDatabaseMigrationApi::class) +private fun generateMigration(description: String) { + require(description.isNotEmpty()) { "Description cannot be empty" } + + val tables: Array = arrayOf( + AttachmentModel, + UploadJournalModel, + MessageAttachmentModel, + UserModel, + ChatModel, + ChatMemberModel, + MessageModel, + MessageIdempotencyKeyModel, + SessionModel, + SessionRevocationModel + ) + + val config = HoconApplicationConfig(ConfigFactory.parseFile(File(DEVELOPMENT_DATABASE_CONFIG)).resolve()) + .property("database") + .getAs() + + config.createDataSource().use { dataSource -> + + Database.connect(dataSource) + applyDatabaseMigrations(dataSource) + + val migrations = File(MIGRATIONS_DIRECTORY).listFiles { it.extension == "sql" }.orEmpty() + + val latestVersion = migrations.maxOfOrNull { + it.name + .substringBefore("__") + .drop(1) + .toInt() + } + + val version = latestVersion?.inc() ?: 1 + + transaction { + + val statements = MigrationUtils.statementsRequiredForDatabaseMigration(*tables) + + if (statements.isEmpty()) { + println("Database schema is up to date, no migration generated") + return@transaction + } + + val script = MigrationUtils.generateMigrationScript( + tables = tables, + scriptDirectory = MIGRATIONS_DIRECTORY, + scriptName = "V${version}__$description" + ) + + println("Generated migration ${script.path}") + + } + + } + +} + +suspend fun suspendedTransaction(statement: JdbcTransaction.() -> R) = newSuspendedTransaction( + context = Dispatchers.IO, + statement = statement +) diff --git a/src/main/kotlin/com/mtusi/messenger/main/plugins/DefaultHeaders.kt b/src/main/kotlin/com/mtusi/messenger/main/plugins/DefaultHeaders.kt new file mode 100644 index 00000000..e7be1a56 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/plugins/DefaultHeaders.kt @@ -0,0 +1,7 @@ +package com.mtusi.messenger.main.plugins + +import io.ktor.server.application.Application +import io.ktor.server.application.install +import io.ktor.server.plugins.defaultheaders.DefaultHeaders + +fun Application.configureDefaultHeaders() = install(DefaultHeaders) diff --git a/src/main/kotlin/com/mtusi/messenger/main/plugins/GracefulShutdown.kt b/src/main/kotlin/com/mtusi/messenger/main/plugins/GracefulShutdown.kt new file mode 100644 index 00000000..9e6eec03 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/plugins/GracefulShutdown.kt @@ -0,0 +1,38 @@ +package com.mtusi.messenger.main.plugins + +import io.ktor.server.application.Application +import io.ktor.server.application.ApplicationStopPreparing +import io.ktor.server.application.log +import io.ktor.util.AttributeKey +import java.util.concurrent.atomic.AtomicBoolean + +private val DrainingKey = AttributeKey("Draining") + +/** + * True once the application received SIGTERM: /health/ready answers 503 from then on. + */ +val Application.isDraining: Boolean + get() = attributes.getOrNull(DrainingKey)?.get() == true + +/** + * On SIGTERM the replica first fails its readiness check and keeps serving for `shutdown.drainDelayMs`, so the + * balancer takes it out of rotation while it still answers. Only then Ktor closes the listening socket and waits + * for requests in flight (`shutdownGracePeriod` / `shutdownTimeout`). + * + * ApplicationStopPreparing is raised synchronously before the engine closes its channels, so blocking here delays + * the close itself. + */ +fun Application.configureGracefulShutdown() { + + val drainDelayMs = environment.config.propertyOrNull("shutdown.drainDelayMs")?.getString()?.toLong() ?: 0 + val draining = AtomicBoolean(false) + + attributes.put(DrainingKey, draining) + + monitor.subscribe(ApplicationStopPreparing) { + draining.set(true) + log.info("Shutting down: readiness is 503, still serving for $drainDelayMs ms") + Thread.sleep(drainDelayMs) + log.info("Drain delay is over, closing the listening socket") + } +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/plugins/InstanceId.kt b/src/main/kotlin/com/mtusi/messenger/main/plugins/InstanceId.kt new file mode 100644 index 00000000..34066029 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/plugins/InstanceId.kt @@ -0,0 +1,28 @@ +package com.mtusi.messenger.main.plugins + +import io.ktor.server.application.Application +import io.ktor.server.application.createApplicationPlugin +import io.ktor.server.application.install +import io.ktor.server.application.log +import java.util.UUID + +/** + * Identifies the replica that produced the response. Stable for the life of the process: `INSTANCE_ID` if set, + * otherwise the container hostname (Docker sets it to the short container id), otherwise a random alias. + */ +fun Application.configureInstanceId() { + + val instanceId = environment.config.propertyOrNull("instance.id")?.getString()?.takeIf { it.isNotBlank() } + ?: System.getenv("HOSTNAME")?.takeIf { it.isNotBlank() } + ?: UUID.randomUUID().toString().take(12) + + log.info("X-Instance-Id of this replica: $instanceId") + + install( + createApplicationPlugin("InstanceId") { + onCall { call -> + call.response.headers.append("X-Instance-Id", instanceId, safeOnly = false) + } + } + ) +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/plugins/Koin.kt b/src/main/kotlin/com/mtusi/messenger/main/plugins/Koin.kt new file mode 100644 index 00000000..6702609f --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/plugins/Koin.kt @@ -0,0 +1,27 @@ +package com.mtusi.messenger.main.plugins + +import com.mtusi.messenger.main.config.ConfigModule +import com.mtusi.messenger.main.infrastructure.repositories.RepositoriesModule +import com.mtusi.messenger.main.infrastructure.services.ServicesModule +import io.ktor.server.application.Application +import io.ktor.server.application.install +import org.koin.dsl.module +import org.koin.ksp.generated.module +import org.koin.ktor.plugin.Koin + +fun Application.configureKoin() { + + val applicationModule = module { + single { this@configureKoin } + } + + install(Koin) { + modules( + applicationModule, + ConfigModule().module, + RepositoriesModule().module, + ServicesModule().module + ) + } + +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/plugins/Redis.kt b/src/main/kotlin/com/mtusi/messenger/main/plugins/Redis.kt new file mode 100644 index 00000000..d0aaea25 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/plugins/Redis.kt @@ -0,0 +1,16 @@ +package com.mtusi.messenger.main.plugins + +import com.mtusi.messenger.main.infrastructure.repositories._common.RedisConnection +import io.ktor.server.application.Application +import io.ktor.server.application.ApplicationStopped +import org.koin.ktor.ext.get + +fun Application.configureRedis() { + + val redisConnection = get() + + monitor.subscribe(ApplicationStopped) { + redisConnection.close() + } + +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/plugins/RequestId.kt b/src/main/kotlin/com/mtusi/messenger/main/plugins/RequestId.kt new file mode 100644 index 00000000..e22ef712 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/plugins/RequestId.kt @@ -0,0 +1,20 @@ +package com.mtusi.messenger.main.plugins + +import io.ktor.server.application.Application +import io.ktor.server.application.createApplicationPlugin +import io.ktor.server.application.install +import io.ktor.util.AttributeKey +import java.util.UUID + +val RequestIdKey: AttributeKey = AttributeKey("X-Request-Id") + +private val RequestIdPlugin = createApplicationPlugin("RequestId") { + onCall { call -> + val incoming = call.request.headers["X-Request-Id"]?.takeIf { it.isNotBlank() } + val id = incoming ?: UUID.randomUUID().toString() + call.attributes.put(RequestIdKey, id) + call.response.headers.append("X-Request-Id", id, safeOnly = false) + } +} + +fun Application.configureRequestId() = install(RequestIdPlugin) diff --git a/src/main/kotlin/com/mtusi/messenger/main/plugins/Routing.kt b/src/main/kotlin/com/mtusi/messenger/main/plugins/Routing.kt new file mode 100644 index 00000000..437b6ad7 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/plugins/Routing.kt @@ -0,0 +1,20 @@ +package com.mtusi.messenger.main.plugins + +import com.mtusi.messenger.main.infrastructure.controllers.authentication.configureAuthenticationRouting +import com.mtusi.messenger.main.infrastructure.controllers.chat.configureChatRouting +import com.mtusi.messenger.main.infrastructure.controllers.health.configureHealthRouting +import com.mtusi.messenger.main.infrastructure.controllers.message.configureMessageRouting +import com.mtusi.messenger.main.infrastructure.controllers.user.configureUserRouting +import com.mtusi.messenger.main.infrastructure.controllers.attachment.configureAttachmentRouting +import io.ktor.server.application.Application +import io.ktor.server.routing.routing + +fun Application.configureRouting() = routing { + configureSwaggerRouting() + configureHealthRouting() + configureUserRouting() + configureAuthenticationRouting() + configureChatRouting() + configureMessageRouting() + configureAttachmentRouting() +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/plugins/SessionRevocationSync.kt b/src/main/kotlin/com/mtusi/messenger/main/plugins/SessionRevocationSync.kt new file mode 100644 index 00000000..b0c7343e --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/plugins/SessionRevocationSync.kt @@ -0,0 +1,36 @@ +package com.mtusi.messenger.main.plugins + +import com.mtusi.messenger.main.infrastructure.services.authentication.AuthenticationService +import io.ktor.server.application.Application +import io.ktor.server.application.log +import kotlinx.coroutines.CancellationException +import kotlinx.coroutines.delay +import kotlinx.coroutines.isActive +import kotlinx.coroutines.launch +import org.koin.ktor.ext.inject +import kotlin.time.Duration.Companion.seconds + +private val SYNC_INTERVAL = 1.seconds + +/** + * Shared signal between replicas: a logout that could not write its tombstone leaves a row in session_revocations + * (PostgreSQL). Every replica checks for such rows once a second and stops trusting cached sessions until the + * tombstones are written, so a replica that never saw the Redis failure itself does not keep accepting the token. + */ +fun Application.configureSessionRevocationSync() { + + val authenticationService by inject() + + launch { + while (isActive) { + delay(SYNC_INTERVAL) + try { + authenticationService.syncPendingRevocations() + } catch (e: CancellationException) { + throw e + } catch (e: Exception) { + log.warn("Pending revocation check failed: ${e.message}") + } + } + } +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/plugins/StatusPages.kt b/src/main/kotlin/com/mtusi/messenger/main/plugins/StatusPages.kt new file mode 100644 index 00000000..3b39bc99 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/plugins/StatusPages.kt @@ -0,0 +1,78 @@ +package com.mtusi.messenger.main.plugins + +import com.mtusi.messenger.main.extensions.respondError +import com.mtusi.messenger.main.infrastructure.types.ApiException +import com.mtusi.messenger.main.infrastructure.types.ErrorCode +import io.ktor.http.HttpStatusCode +import io.ktor.serialization.JsonConvertException +import io.ktor.server.application.Application +import io.ktor.server.application.install +import io.ktor.server.application.log +import io.ktor.server.plugins.BadRequestException +import io.ktor.server.plugins.NotFoundException +import io.ktor.server.plugins.UnsupportedMediaTypeException +import io.ktor.server.plugins.statuspages.StatusPages +import io.lettuce.core.RedisException +import kotlinx.serialization.SerializationException +import java.sql.SQLException +import java.sql.SQLTransientConnectionException + +private const val CONNECTION_ERROR_SQL_STATE_CLASS = "08" + +// admin_shutdown, crash_shutdown, cannot_connect_now: the server is going down or still starting. +private const val SERVER_SHUTDOWN_SQL_STATE_PREFIX = "57P" + +fun Application.configureStatusPages() = install(StatusPages) { + exception { call, cause -> + call.respondError(cause.code, cause.message ?: cause.code.wire) + } + exception { call, _ -> + call.respondError(ErrorCode.INVALID_REQUEST, "invalid JSON body") + } + exception { call, _ -> + call.respondError(ErrorCode.INVALID_REQUEST, "invalid JSON body") + } + exception { call, _ -> + call.respondError(ErrorCode.INVALID_REQUEST, "invalid request") + } + exception { call, _ -> + call.respondError(ErrorCode.NOT_FOUND, "not found") + } + exception { call, _ -> + call.respondError(ErrorCode.UNSUPPORTED_MEDIA_TYPE, "unsupported media type") + } + exception { call, cause -> + if (cause.isConnectionFailure()) { + call.application.log.warn("Database unavailable: ${cause.message}") + call.respondError(ErrorCode.UNAVAILABLE, "service temporarily unavailable") + } else { + call.application.log.error("Database error", cause) + call.respondError(ErrorCode.INTERNAL_ERROR, "internal error") + } + } + exception { call, cause -> + call.application.log.warn("Redis unavailable: ${cause.message}") + call.respondError(ErrorCode.UNAVAILABLE, "service temporarily unavailable") + } + exception { call, cause -> + call.application.log.error("Unhandled error", cause) + call.respondError(ErrorCode.INTERNAL_ERROR, "internal error") + } + status(HttpStatusCode.NotFound) { call, _ -> + call.respondError(ErrorCode.NOT_FOUND, "not found") + } + status(HttpStatusCode.MethodNotAllowed) { call, _ -> + call.respondError(ErrorCode.NOT_FOUND, "not found") + } +} + +private fun SQLException.isConnectionFailure(): Boolean { + return generateSequence(this) { it.cause }.any { + it is SQLTransientConnectionException || + (it is SQLException && it.sqlState.isUnavailableState()) + } +} + +private fun String?.isUnavailableState(): Boolean { + return this != null && (startsWith(CONNECTION_ERROR_SQL_STATE_CLASS) || startsWith(SERVER_SHUTDOWN_SQL_STATE_PREFIX)) +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/plugins/Swagger.kt b/src/main/kotlin/com/mtusi/messenger/main/plugins/Swagger.kt new file mode 100644 index 00000000..8f1d4f61 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/plugins/Swagger.kt @@ -0,0 +1,8 @@ +package com.mtusi.messenger.main.plugins + +import io.ktor.server.plugins.swagger.swaggerUI +import io.ktor.server.routing.Routing + +fun Routing.configureSwaggerRouting() { + swaggerUI(path = "docs", swaggerFile = "openapi.json") +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/plugins/authentication/Authentication.kt b/src/main/kotlin/com/mtusi/messenger/main/plugins/authentication/Authentication.kt new file mode 100644 index 00000000..378524eb --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/plugins/authentication/Authentication.kt @@ -0,0 +1,43 @@ +package com.mtusi.messenger.main.plugins.authentication + +import com.mtusi.messenger.main.infrastructure.services.authentication.AuthenticationService +import com.mtusi.messenger.main.plugins.authentication.providers.basicCredentials +import com.mtusi.messenger.main.plugins.authentication.providers.bearerSession +import com.mtusi.messenger.main.plugins.suspendedTransaction +import io.ktor.server.application.Application +import io.ktor.server.auth.authentication +import org.koin.ktor.ext.inject + +const val CREDENTIALS_AUTH = "credentials" +const val SESSION_AUTH = "session" + +fun Application.configureAuthentication() { + + val authenticationService by inject() + + authentication { + + basicCredentials(CREDENTIALS_AUTH) { + validate { credential -> + val user = suspendedTransaction { + authenticationService.validateCredentials( + username = credential.name, + password = credential.password + ) + } ?: return@validate null + + return@validate CredentialsPrincipal(user) + } + } + + bearerSession(SESSION_AUTH) { + authenticate { token -> + val session = authenticationService.findSessionByToken(token) ?: return@authenticate null + + return@authenticate UserPrincipal(session) + } + } + + } + +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/plugins/authentication/CredentialsPrincipal.kt b/src/main/kotlin/com/mtusi/messenger/main/plugins/authentication/CredentialsPrincipal.kt new file mode 100644 index 00000000..9a5684a3 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/plugins/authentication/CredentialsPrincipal.kt @@ -0,0 +1,7 @@ +package com.mtusi.messenger.main.plugins.authentication + +import com.mtusi.messenger.main.infrastructure.dto.User + +data class CredentialsPrincipal( + val user: User +) diff --git a/src/main/kotlin/com/mtusi/messenger/main/plugins/authentication/UserPrincipal.kt b/src/main/kotlin/com/mtusi/messenger/main/plugins/authentication/UserPrincipal.kt new file mode 100644 index 00000000..4ac42fa8 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/plugins/authentication/UserPrincipal.kt @@ -0,0 +1,13 @@ +package com.mtusi.messenger.main.plugins.authentication + +import com.mtusi.messenger.main.infrastructure.dto.authentication.Session +import java.util.UUID + +data class UserPrincipal( + val session: Session +) { + + val id: UUID + get() = session.userId + +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/plugins/authentication/providers/BasicCredentialsProvider.kt b/src/main/kotlin/com/mtusi/messenger/main/plugins/authentication/providers/BasicCredentialsProvider.kt new file mode 100644 index 00000000..f8ab4179 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/plugins/authentication/providers/BasicCredentialsProvider.kt @@ -0,0 +1,66 @@ +package com.mtusi.messenger.main.plugins.authentication.providers + +import com.mtusi.messenger.main.extensions.respondError +import com.mtusi.messenger.main.infrastructure.types.AuthChallenge +import com.mtusi.messenger.main.infrastructure.types.ErrorCode +import io.ktor.server.application.ApplicationCall +import io.ktor.server.auth.AuthenticationConfig +import io.ktor.server.auth.AuthenticationContext +import io.ktor.server.auth.AuthenticationFailedCause +import io.ktor.server.auth.AuthenticationProvider +import io.ktor.server.auth.UserPasswordCredential +import io.ktor.server.auth.basicAuthenticationCredentials +import io.ktor.server.plugins.BadRequestException + +private const val CHALLENGE_KEY = "BasicCredentials" + +class BasicCredentialsProvider(config: Config) : AuthenticationProvider(config) { + + private val validate = config.validate + + override suspend fun onAuthenticate(context: AuthenticationContext) { + + val credential = try { + context.call.request.basicAuthenticationCredentials(Charsets.UTF_8) + } catch (_: BadRequestException) { + null + } + + val principal = credential?.let { validate(context.call, it) } + + if (principal == null) { + + val cause = if (credential == null) { + AuthenticationFailedCause.NoCredentials + } else { + AuthenticationFailedCause.InvalidCredentials + } + + context.challenge(CHALLENGE_KEY, cause) { challenge, call -> + call.respondError(ErrorCode.UNAUTHORIZED, "invalid credentials", AuthChallenge.BASIC) + challenge.complete() + } + return + } + + context.principal(principal) + } + + class Config(name: String?) : AuthenticationProvider.Config(name) { + + internal var validate: suspend ApplicationCall.(UserPasswordCredential) -> Any? = { null } + + fun validate(body: suspend ApplicationCall.(UserPasswordCredential) -> Any?) { + validate = body + } + + } + +} + +fun AuthenticationConfig.basicCredentials( + name: String, + configure: BasicCredentialsProvider.Config.() -> Unit +) { + register(BasicCredentialsProvider(BasicCredentialsProvider.Config(name).apply(configure))) +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/plugins/authentication/providers/BearerSessionProvider.kt b/src/main/kotlin/com/mtusi/messenger/main/plugins/authentication/providers/BearerSessionProvider.kt new file mode 100644 index 00000000..9d3ad728 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/plugins/authentication/providers/BearerSessionProvider.kt @@ -0,0 +1,71 @@ +package com.mtusi.messenger.main.plugins.authentication.providers + +import com.mtusi.messenger.main.extensions.respondError +import com.mtusi.messenger.main.infrastructure.types.AuthChallenge +import com.mtusi.messenger.main.infrastructure.types.ErrorCode +import io.ktor.http.auth.HttpAuthHeader +import io.ktor.server.application.ApplicationCall +import io.ktor.server.auth.AuthenticationConfig +import io.ktor.server.auth.AuthenticationContext +import io.ktor.server.auth.AuthenticationFailedCause +import io.ktor.server.auth.AuthenticationProvider +import io.ktor.server.auth.parseAuthorizationHeader +import io.ktor.server.plugins.BadRequestException + +private const val CHALLENGE_KEY = "BearerSession" +private const val BEARER_SCHEME = "Bearer" + +class BearerSessionProvider(config: Config) : AuthenticationProvider(config) { + + private val authenticate = config.authenticate + + override suspend fun onAuthenticate(context: AuthenticationContext) { + + val header = try { + context.call.request.parseAuthorizationHeader() + } catch (_: BadRequestException) { + null + } + + val token = (header as? HttpAuthHeader.Single) + ?.takeIf { it.authScheme.equals(BEARER_SCHEME, ignoreCase = true) } + ?.blob + + val principal = token?.let { authenticate(context.call, it) } + + if (principal == null) { + + val cause = if (token == null) { + AuthenticationFailedCause.NoCredentials + } else { + AuthenticationFailedCause.InvalidCredentials + } + + context.challenge(CHALLENGE_KEY, cause) { challenge, call -> + call.respondError(ErrorCode.UNAUTHORIZED, "authentication required", AuthChallenge.BEARER) + challenge.complete() + } + return + } + + context.principal(principal) + } + + class Config(name: String?) : AuthenticationProvider.Config(name) { + + internal var authenticate: suspend ApplicationCall.(String) -> Any? = { null } + + fun authenticate(body: suspend ApplicationCall.(String) -> Any?) { + authenticate = body + } + + } + +} + +fun AuthenticationConfig.bearerSession( + name: String, + configure: BearerSessionProvider.Config.() -> Unit +) { + register(BearerSessionProvider(BearerSessionProvider.Config(name).apply(configure))) +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/utils/Hashing.kt b/src/main/kotlin/com/mtusi/messenger/main/utils/Hashing.kt new file mode 100644 index 00000000..824b9059 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/utils/Hashing.kt @@ -0,0 +1,10 @@ +package com.mtusi.messenger.main.utils + +import java.security.MessageDigest + +@OptIn(ExperimentalStdlibApi::class) +fun ByteArray.computeSha256(): String { + return MessageDigest.getInstance("SHA-256") + .digest(this) + .toHexString() +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/utils/Pagination.kt b/src/main/kotlin/com/mtusi/messenger/main/utils/Pagination.kt new file mode 100644 index 00000000..bc605e09 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/utils/Pagination.kt @@ -0,0 +1,55 @@ +package com.mtusi.messenger.main.utils + +import com.mtusi.messenger.main.compat.serializers.SerialUUID +import com.mtusi.messenger.main.infrastructure.dto.PageResponse +import kotlinx.serialization.Serializable +import kotlinx.serialization.json.Json +import java.util.Base64 +import java.util.UUID +import kotlin.time.Instant + +private val json = Json { ignoreUnknownKeys = false } + +data class PageKey( + val createdAt: Instant, + val id: UUID, +) + +@Serializable +private data class Cursor( + val scope: String, + val createdAt: Instant, + val id: SerialUUID, +) + +fun encodeCursor(scope: String, key: PageKey): String { + val raw = json.encodeToString(Cursor.serializer(), Cursor(scope, key.createdAt, key.id)) + return Base64.getUrlEncoder().withoutPadding().encodeToString(raw.toByteArray(Charsets.UTF_8)) +} + +fun decodeCursor(raw: String, expectedScope: String): PageKey? { + val cursor = try { + val bytes = Base64.getUrlDecoder().decode(raw) + json.decodeFromString(Cursor.serializer(), String(bytes, Charsets.UTF_8)) + } catch (_: Exception) { + return null + } + + if (cursor.scope != expectedScope) { + return null + } + + return PageKey(cursor.createdAt, cursor.id) +} + +fun List.toPage( + limit: Int, + scope: String, + key: (T) -> PageKey, +): PageResponse { + + val items = take(limit) + val nextCursor = if (size > limit) encodeCursor(scope, key(items.last())) else null + + return PageResponse(items, nextCursor) +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/utils/Secrets.kt b/src/main/kotlin/com/mtusi/messenger/main/utils/Secrets.kt new file mode 100644 index 00000000..8288a810 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/utils/Secrets.kt @@ -0,0 +1,10 @@ +package com.mtusi.messenger.main.utils + +import java.io.File + +fun resolveSecret( + value: String?, + file: String? +): String? { + return value ?: file?.let { File(it).readText().trim() } +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/utils/TemporaryUpload.kt b/src/main/kotlin/com/mtusi/messenger/main/utils/TemporaryUpload.kt new file mode 100644 index 00000000..9bee2e7e --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/utils/TemporaryUpload.kt @@ -0,0 +1,17 @@ +package com.mtusi.messenger.main.utils + +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.withContext +import java.io.File +import java.nio.file.Files + +suspend fun withTemporaryUpload(block: suspend (File) -> T): T = withContext(Dispatchers.IO) { + // Own creation and cleanup in the same context: cancellation during dispatch back + // must not lose a newly created file before a caller can enter its finally block. + val file = Files.createTempFile("messenger-upload-", ".bin").toFile() + try { + block(file) + } finally { + Files.deleteIfExists(file.toPath()) + } +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/utils/Time.kt b/src/main/kotlin/com/mtusi/messenger/main/utils/Time.kt new file mode 100644 index 00000000..86b8e3c7 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/utils/Time.kt @@ -0,0 +1,14 @@ +package com.mtusi.messenger.main.utils + +import kotlin.time.Clock +import kotlin.time.Instant + +private const val NANOS_PER_MICRO = 1_000 + +fun Clock.nowMicros(): Instant { + val now = now() + return Instant.fromEpochSeconds( + epochSeconds = now.epochSeconds, + nanosecondAdjustment = now.nanosecondsOfSecond / NANOS_PER_MICRO * NANOS_PER_MICRO + ) +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/utils/Validation.kt b/src/main/kotlin/com/mtusi/messenger/main/utils/Validation.kt new file mode 100644 index 00000000..aad25ee1 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/utils/Validation.kt @@ -0,0 +1,45 @@ +package com.mtusi.messenger.main.utils + +import com.mtusi.messenger.main.infrastructure.types.ApiException +import com.mtusi.messenger.main.infrastructure.types.ErrorCode +import java.util.UUID + +private val USERNAME = Regex("^[a-z0-9_]{3,32}$") + +private val IDEMPOTENCY_KEY = Regex("^[A-Za-z0-9._:-]{1,128}$") + +private val UUID_REGEX = Regex( + "^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$" +) + +fun codePoints(value: String): Int = value.codePointCount(0, value.length) + +fun requireUsername(value: String) { + if (!USERNAME.matches(value)) { + throw ApiException(ErrorCode.INVALID_REQUEST, "invalid username") + } +} + +fun requireNonBlankBounded(value: String, min: Int, max: Int, field: String) { + if (value.isBlank()) { + throw ApiException(ErrorCode.INVALID_REQUEST, "$field must not be blank") + } + val n = codePoints(value) + if (n !in min..max) { + throw ApiException(ErrorCode.INVALID_REQUEST, "$field length out of range") + } +} + +fun requireIdempotencyKey(value: String?): String { + if (value == null || !IDEMPOTENCY_KEY.matches(value)) { + throw ApiException(ErrorCode.INVALID_REQUEST, "missing or invalid Idempotency-Key") + } + return value +} + +fun requireUuid(value: String?, field: String): UUID { + if (value == null || !UUID_REGEX.matches(value)) { + throw ApiException(ErrorCode.INVALID_REQUEST, "invalid $field") + } + return UUID.fromString(value) +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/utils/exposed/operations/InstantPlusSeconds.kt b/src/main/kotlin/com/mtusi/messenger/main/utils/exposed/operations/InstantPlusSeconds.kt new file mode 100644 index 00000000..4c0abd5d --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/utils/exposed/operations/InstantPlusSeconds.kt @@ -0,0 +1,25 @@ +package com.mtusi.messenger.main.utils.exposed.operations + +import org.jetbrains.exposed.v1.core.Expression +import org.jetbrains.exposed.v1.core.ExpressionWithColumnType +import org.jetbrains.exposed.v1.core.QueryBuilder +import org.jetbrains.exposed.v1.datetime.KotlinInstantColumnType +import kotlin.time.Instant + +class InstantPlusSeconds( + private val instant: Expression, + private val seconds: Expression +) : ExpressionWithColumnType() { + + override val columnType = KotlinInstantColumnType() + + override fun toQueryBuilder(queryBuilder: QueryBuilder) = queryBuilder { + append("(") + append(instant) + append(" + (") + append(seconds) + append(" * INTERVAL '1 second'))") + } +} + +operator fun Expression.plus(seconds: Expression) = InstantPlusSeconds(this, seconds) diff --git a/src/main/kotlin/com/mtusi/messenger/main/utils/exposed/operations/RowGreater.kt b/src/main/kotlin/com/mtusi/messenger/main/utils/exposed/operations/RowGreater.kt new file mode 100644 index 00000000..190d93fa --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/utils/exposed/operations/RowGreater.kt @@ -0,0 +1,32 @@ +package com.mtusi.messenger.main.utils.exposed.operations + +import org.jetbrains.exposed.v1.core.Expression +import org.jetbrains.exposed.v1.core.Op +import org.jetbrains.exposed.v1.core.QueryBuilder + +class RowGreater( + private val left: List>, + private val right: List> +) : Op() { + + init { + require(left.size == right.size) { "Rows must have the same arity" } + } + + override fun toQueryBuilder(queryBuilder: QueryBuilder) = queryBuilder { + appendRow(left) + append(" > ") + appendRow(right) + } + + private fun QueryBuilder.appendRow(expressions: List>) { + append("(") + expressions.forEachIndexed { index, expression -> + if (index > 0) { + append(", ") + } + append(expression) + } + append(")") + } +} diff --git a/src/main/resources/application.conf b/src/main/resources/application.conf new file mode 100644 index 00000000..4d51dca4 --- /dev/null +++ b/src/main/resources/application.conf @@ -0,0 +1,112 @@ +ktor { + application { + modules = [com.mtusi.messenger.main.AppKt.module] + } + + deployment { + host = "0.0.0.0" + host = ${?APP_HOST} + port = 8080 + port = ${?APP_PORT} + # After the drain (see `shutdown`): Netty waits for 1 s without new activity, at most 5 s in total. + shutdownGracePeriod = 1000 + shutdownTimeout = 5000 + } +} + +shutdown { + # Time between SIGTERM and closing the socket while /health/ready is already 503. + # Behind HAProxy (check every 1 s, 2 failures) 3 s is enough to leave rotation; a single local process needs none. + drainDelayMs = 0 + drainDelayMs = ${?SHUTDOWN_DRAIN_DELAY_MS} +} + +database { + host = "localhost" + host = ${?DB_HOST} + port = 5432 + port = ${?DB_PORT} + name = "messenger" + name = ${?DB_NAME} + username = "messenger" + username = ${?DB_USER} + password = ${?DB_PASSWORD} + passwordFile = ${?DB_PASSWORD_FILE} + maxPoolSize = 10 + maxPoolSize = ${?DB_MAX_POOL_SIZE} + connectionTimeoutMs = 2000 + statementTimeoutMs = 5000 + # Replicas run with false: compose applies migrations once in the `migrate` service. + migrateOnStart = true + migrateOnStart = ${?DB_MIGRATE_ON_START} +} + +redis { + host = "localhost" + host = ${?REDIS_HOST} + port = 6379 + port = ${?REDIS_PORT} + password = ${?REDIS_PASSWORD} + passwordFile = ${?REDIS_PASSWORD_FILE} + connectTimeoutMs = 500 + commandTimeoutMs = 500 +} + +cors { + # Defaults to false + #allowAnyHost = true + # Swagger UI sends "Try it out" to the contract server http://localhost:8080; pages opened elsewhere need these. + allowedHosts = [ + { host = "localhost:8080", schemes = ["http"] } + { host = "localhost:8081", schemes = ["http"] } + { host = "127.0.0.1:8080", schemes = ["http"] } + { host = "127.0.0.1:8081", schemes = ["http"] } + ] + + # Defaults to true + #allowAnyMethod = true + #allowedMethods = [GET, POST, PATCH, PUT, DELETE, OPTIONS] + + allowAnyHeader = true + #allowedHeaders = [] + + exposedHeaders = [X-Request-Id, X-Instance-Id, WWW-Authenticate] + + # Browsers send Origin on same-origin POST too, e.g. Swagger UI served by the API itself. + allowSameOrigin = true +} + +instance { + id = ${?INSTANCE_ID} +} + +messages { + idempotencyTtlSeconds = 86400 + idempotencyTtlSeconds = ${?IDEMPOTENCY_TTL_SECONDS} +} + +authentication { + realm = "messenger" + sessionTtlSeconds = 3600 + sessionTtlSeconds = ${?SESSION_TTL_SECONDS} + sessionCacheTtlSeconds = 60 + # Collapse concurrent cache misses for one token into a single database lookup (track 1 compares on/off). + sessionCacheSingleFlight = true + sessionCacheSingleFlight = ${?SESSION_CACHE_SINGLE_FLIGHT} + # A logout answers 204 only after its tombstone is in Redis; it retries up to this long, then answers 503. + # Below HAProxy `timeout server` (10 s). + sessionRevokeTimeoutMs = 8000 + sessionRevokeTimeoutMs = ${?SESSION_REVOKE_TIMEOUT_MS} +} + +# S3 credentials are local files, never source-controlled values. +storage { + endpoint = "http://localhost:9000" + endpoint = ${?S3_ENDPOINT} + bucket = "messenger-attachments" + bucket = ${?S3_BUCKET} + accessKey = ${?S3_ACCESS_KEY_ID} + secretKey = ${?S3_SECRET_ACCESS_KEY} + accessKeyFile = ${?S3_ACCESS_KEY_ID_FILE} + secretKeyFile = ${?S3_SECRET_ACCESS_KEY_FILE} +} diff --git a/src/main/resources/logback.xml b/src/main/resources/logback.xml new file mode 100644 index 00000000..5416c44d --- /dev/null +++ b/src/main/resources/logback.xml @@ -0,0 +1,12 @@ + + + + %d{yyyy-MM-dd'T'HH:mm:ss.SSSXXX} %-5level [%thread] %logger{36} - %msg%n + + + + + + + + diff --git a/src/main/resources/migrations/V1__init.sql b/src/main/resources/migrations/V1__init.sql new file mode 100644 index 00000000..b580eefd --- /dev/null +++ b/src/main/resources/migrations/V1__init.sql @@ -0,0 +1,50 @@ +-- Users, chats, membership, messages and sessions. PostgreSQL is the source of truth. +-- Timestamps are timestamptz: an absolute instant, independent of server/JVM time zone. + +CREATE TABLE users ( + id uuid PRIMARY KEY, + username varchar(32) NOT NULL, + display_name varchar(100) NOT NULL, + password_hash varchar(255) NOT NULL, + created_at timestamptz NOT NULL, + CONSTRAINT users_username_key UNIQUE (username), + CONSTRAINT users_username_format CHECK (username ~ '^[a-z0-9_]{3,32}$') +); + +CREATE TABLE chats ( + id uuid PRIMARY KEY, + title varchar(100) NOT NULL, + created_at timestamptz NOT NULL +); + +CREATE TABLE chat_members ( + chat_id uuid NOT NULL REFERENCES chats (id) ON DELETE CASCADE, + user_id uuid NOT NULL REFERENCES users (id) ON DELETE CASCADE, + position integer NOT NULL, + PRIMARY KEY (chat_id, user_id) +); + +-- "Chats of a user": the PK leads with chat_id, so the reverse lookup needs its own index. +CREATE INDEX chat_members_user_id_idx ON chat_members (user_id); + +CREATE TABLE messages ( + id uuid PRIMARY KEY, + chat_id uuid NOT NULL REFERENCES chats (id) ON DELETE CASCADE, + sender_id uuid NOT NULL REFERENCES users (id) ON DELETE CASCADE, + text varchar(4000) NOT NULL, + created_at timestamptz NOT NULL +); + +-- Keyset pagination of a chat history: WHERE chat_id = ? AND (created_at, id) > (?, ?) ORDER BY created_at, id. +CREATE INDEX messages_chat_id_created_at_id_idx ON messages (chat_id, created_at, id); + +CREATE TABLE sessions ( + id uuid PRIMARY KEY, + user_id uuid NOT NULL REFERENCES users (id) ON DELETE CASCADE, + token_hash varchar(64) NOT NULL, + created_at timestamptz NOT NULL, + expires_at timestamptz NOT NULL, + CONSTRAINT sessions_token_hash_key UNIQUE (token_hash) +); + +CREATE INDEX sessions_user_id_idx ON sessions (user_id); diff --git a/src/main/resources/migrations/V2__session_revocations.sql b/src/main/resources/migrations/V2__session_revocations.sql new file mode 100644 index 00000000..4b15c00c --- /dev/null +++ b/src/main/resources/migrations/V2__session_revocations.sql @@ -0,0 +1,6 @@ +-- Logouts whose tombstone has not reached Redis yet. Written in the same transaction as the session delete and +-- removed once the tombstone is stored, so a copy cached before an outage cannot outlive the logout. +CREATE TABLE session_revocations ( + token_hash varchar(64) PRIMARY KEY, + expires_at timestamptz NOT NULL +); diff --git a/src/main/resources/migrations/V3__message_idempotency_keys.sql b/src/main/resources/migrations/V3__message_idempotency_keys.sql new file mode 100644 index 00000000..9ddedfee --- /dev/null +++ b/src/main/resources/migrations/V3__message_idempotency_keys.sql @@ -0,0 +1,19 @@ +-- Idempotency-Key of POST /chats/{chat_id}/messages. Scope: user + chat (method and path are fixed) + key. +-- The row is inserted in the same transaction as the message, so a key exists only together with its message. +-- message_id is NULL only inside that transaction: the key is claimed first, the message is written next. +CREATE TABLE message_idempotency_keys ( + user_id uuid NOT NULL, + chat_id uuid NOT NULL, + key varchar(128) NOT NULL, + request_hash varchar(64) NOT NULL, + message_id uuid NULL, + created_at timestamptz NOT NULL, + CONSTRAINT message_idempotency_keys_pkey PRIMARY KEY (user_id, chat_id, key), + CONSTRAINT message_idempotency_keys_user_id_fkey FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE, + CONSTRAINT message_idempotency_keys_chat_id_fkey FOREIGN KEY (chat_id) REFERENCES chats (id) ON DELETE CASCADE, + CONSTRAINT message_idempotency_keys_message_id_fkey FOREIGN KEY (message_id) REFERENCES messages (id) ON DELETE CASCADE +); + +-- Foreign keys are not indexed automatically; the cascades from chats and messages need these. +CREATE INDEX message_idempotency_keys_chat_id_idx ON message_idempotency_keys (chat_id); +CREATE INDEX message_idempotency_keys_message_id_idx ON message_idempotency_keys (message_id); diff --git a/src/main/resources/migrations/V4__attachments.sql b/src/main/resources/migrations/V4__attachments.sql new file mode 100644 index 00000000..07b752ab --- /dev/null +++ b/src/main/resources/migrations/V4__attachments.sql @@ -0,0 +1,19 @@ +CREATE TABLE attachments ( + id UUID PRIMARY KEY, + chat_id UUID NOT NULL REFERENCES chats(id), + owner_id UUID NOT NULL REFERENCES users(id), + filename VARCHAR(255) NOT NULL, + content_type VARCHAR(32) NOT NULL CHECK (content_type IN ('image/png', 'image/jpeg')), + size_bytes BIGINT NOT NULL CHECK (size_bytes BETWEEN 1 AND 10485760), + sha256 VARCHAR(64) NOT NULL, + created_at TIMESTAMP WITH TIME ZONE NOT NULL +); +CREATE INDEX attachments_chat_id_idx ON attachments(chat_id); +CREATE TABLE message_attachments ( + message_id UUID NOT NULL REFERENCES messages(id) ON DELETE CASCADE, + attachment_id UUID NOT NULL REFERENCES attachments(id), + position INTEGER NOT NULL CHECK (position BETWEEN 0 AND 9), + PRIMARY KEY (message_id, attachment_id), + UNIQUE (message_id, position) +); +CREATE INDEX message_attachments_attachment_id_idx ON message_attachments(attachment_id); diff --git a/src/main/resources/migrations/V5__attachment_uploads.sql b/src/main/resources/migrations/V5__attachment_uploads.sql new file mode 100644 index 00000000..881c8711 --- /dev/null +++ b/src/main/resources/migrations/V5__attachment_uploads.sql @@ -0,0 +1,7 @@ +CREATE TABLE attachment_uploads ( + id UUID PRIMARY KEY, + expires_at TIMESTAMPTZ NOT NULL DEFAULT (CURRENT_TIMESTAMP + INTERVAL '1 hour'), + abandoned BOOLEAN NOT NULL DEFAULT FALSE, + next_attempt_at TIMESTAMPTZ NOT NULL DEFAULT (CURRENT_TIMESTAMP + INTERVAL '1 hour') +); +CREATE INDEX attachment_uploads_next_attempt_idx ON attachment_uploads(next_attempt_at); diff --git a/src/test/kotlin/com/mtusi/messenger/main/infrastructure/controllers/attachment/AttachmentControllerTest.kt b/src/test/kotlin/com/mtusi/messenger/main/infrastructure/controllers/attachment/AttachmentControllerTest.kt new file mode 100644 index 00000000..c8181d20 --- /dev/null +++ b/src/test/kotlin/com/mtusi/messenger/main/infrastructure/controllers/attachment/AttachmentControllerTest.kt @@ -0,0 +1,139 @@ +package com.mtusi.messenger.main.infrastructure.controllers.attachment + +import com.mtusi.messenger.main.infrastructure.dto.Attachment +import com.mtusi.messenger.main.infrastructure.dto.Message +import com.mtusi.messenger.main.infrastructure.controllers.message.requests.CreateMessageRequest +import com.mtusi.messenger.main.support.* +import io.ktor.client.call.body +import io.ktor.client.request.* +import io.ktor.http.* +import io.ktor.http.content.OutgoingContent +import java.awt.image.BufferedImage +import java.io.ByteArrayOutputStream +import javax.imageio.ImageIO +import kotlin.test.* + +class AttachmentControllerTest { + + @Test + + fun `upload originals ACL message links and idempotency`() = runApp { client -> + val alice = client.register("alice") + val bob = client.register("bob") + val outsider = client.register("outsider") + val chat = client.createChat(alice, "files", listOf(alice.id, bob.id)) + for (format in listOf("png", "jpeg")) { + val bytes = image(format) + val upload = client.post("/api/v1/chats/${chat.id}/attachments?filename=photo.$format") { + auth(alice) + contentType(ContentType.parse("image/$format")) + setBody(bytes) + } + assertEquals(HttpStatusCode.Created, upload.status) + val attachment = upload.body() + assertEquals("ready", attachment.status) + assertEquals(bytes.size.toLong(), attachment.sizeBytes) + val path = "/api/v1/attachments/${attachment.id}" + val download = client.get("$path/content") { auth(bob) } + assertEquals(HttpStatusCode.OK, download.status) + assertContentEquals(bytes, download.body()) + assertEquals(HttpStatusCode.NotFound, client.get(path) { auth(outsider) }.status) + assertEquals(HttpStatusCode.NotFound, client.get("$path/content") { auth(outsider) }.status) + assertEquals(HttpStatusCode.Unauthorized, client.get("$path/content").status) + assertEquals(HttpStatusCode.NotFound, client.get("$path/content?variant=thumbnail") { auth(bob) }.status) + val outsiderChat = client.createChat(outsider, "outsider", listOf(outsider.id)) + assertEquals(HttpStatusCode.NotFound, client.post("/api/v1/chats/${outsiderChat.id}/messages") { + auth(outsider) + idempotencyKey() + contentType(ContentType.Application.Json) + setBody(CreateMessageRequest("", listOf(attachment.id.toString()))) + }.status) + val key = "file-$format" + suspend fun send(ids: List) = client.post("/api/v1/chats/${chat.id}/messages") { + auth(bob) + idempotencyKey(key) + contentType(ContentType.Application.Json) + setBody(CreateMessageRequest("", ids)) + } + val first = send(listOf(attachment.id.toString())) + assertEquals(HttpStatusCode.Created, first.status) + val message = first.body() + assertEquals(listOf(attachment.id), message.attachmentIds) + assertEquals(message, send(listOf(attachment.id.toString())).body()) + val otherChat = client.createChat(bob, "other", listOf(bob.id)) + assertEquals(HttpStatusCode.NotFound, client.post("/api/v1/chats/${otherChat.id}/messages") { + auth(bob) + idempotencyKey() + contentType(ContentType.Application.Json) + setBody(CreateMessageRequest("", listOf(attachment.id.toString()))) + }.status) + } + } + + @Test + + fun `invalid images filenames and byte limit return client errors`() = runApp { client -> + val user = client.register("upload") + val chat = client.createChat(user, "files", listOf(user.id)) + suspend fun upload( + bytes: ByteArray, + type: String = "image/png", + name: String = "photo.png" + ) = + client.post("/api/v1/chats/${chat.id}/attachments") { + auth(user) + parameter("filename", name) + contentType(ContentType.parse(type)) + setBody(bytes) + } + val malformedMime = client.post("/api/v1/chats/${chat.id}/attachments?filename=photo.png") { + auth(user) + // Keep the invalid header in raw content so the client does not parse it first. + setBody(object : OutgoingContent.ByteArrayContent() { + override val headers = Headers.build { + append(HttpHeaders.ContentType, "image") + } + override fun bytes() = image("png") + }) + } + assertEquals(HttpStatusCode.BadRequest, malformedMime.status) + assertEquals(HttpStatusCode.UnsupportedMediaType, upload("not an image".toByteArray()).status) + assertEquals(HttpStatusCode.UnsupportedMediaType, upload(image("png"), "image/jpeg").status) + assertEquals(HttpStatusCode.UnsupportedMediaType, upload(image("png").copyOf(40)).status) + for (name in listOf("", " ", "\u2003", "../x.png", "a\\b.png", "a\n.png", "a\u0000.png", "x".repeat(256))) + assertEquals(HttpStatusCode.BadRequest, upload(image("png"), name = name).status) + assertEquals(HttpStatusCode.PayloadTooLarge, upload(ByteArray(10 * 1024 * 1024 + 1)).status) + } + + @Test + + fun `S3 outage fails upload download and readiness without losing the original`() = runApp { client -> + val user = client.register("outage") + val chat = client.createChat(user, "files", listOf(user.id)) + val bytes = image("png") + suspend fun upload() = client.post("/api/v1/chats/${chat.id}/attachments?filename=photo.png") { + auth(user) + contentType(ContentType.Image.PNG) + setBody(bytes) + } + val attachment = upload().body() + val path = "/api/v1/attachments/${attachment.id}/content" + val container = TestStorage.container + container.dockerClient.pauseContainerCmd(container.containerId).exec() + try { + assertEquals(HttpStatusCode.ServiceUnavailable, upload().status) + assertEquals(HttpStatusCode.ServiceUnavailable, client.get(path) { auth(user) }.status) + assertEquals(HttpStatusCode.ServiceUnavailable, client.get("/health/ready").status) + assertEquals(HttpStatusCode.OK, client.get("/health/live").status) + } finally { + container.dockerClient.unpauseContainerCmd(container.containerId).exec() + } + assertContentEquals(bytes, client.get(path) { auth(user) }.body()) + assertEquals(HttpStatusCode.OK, client.get("/health/ready").status) + } + + private fun image(format: String): ByteArray = ByteArrayOutputStream().use { + ImageIO.write(BufferedImage(32, 20, BufferedImage.TYPE_INT_RGB), format, it) + it.toByteArray() + } +} diff --git a/src/test/kotlin/com/mtusi/messenger/main/infrastructure/controllers/authentication/AuthenticationControllerTest.kt b/src/test/kotlin/com/mtusi/messenger/main/infrastructure/controllers/authentication/AuthenticationControllerTest.kt new file mode 100644 index 00000000..67734ad9 --- /dev/null +++ b/src/test/kotlin/com/mtusi/messenger/main/infrastructure/controllers/authentication/AuthenticationControllerTest.kt @@ -0,0 +1,151 @@ +package com.mtusi.messenger.main.infrastructure.controllers.authentication + +import com.mtusi.messenger.main.extensions.ErrorBody +import com.mtusi.messenger.main.infrastructure.controllers.authentication.responses.SessionResponse +import com.mtusi.messenger.main.support.TEST_PASSWORD +import com.mtusi.messenger.main.support.auth +import com.mtusi.messenger.main.support.createUser +import com.mtusi.messenger.main.support.register +import com.mtusi.messenger.main.support.runApp +import io.ktor.client.call.body +import io.ktor.client.request.basicAuth +import io.ktor.client.request.bearerAuth +import io.ktor.client.request.delete +import io.ktor.client.request.get +import io.ktor.client.request.header +import io.ktor.client.request.post +import io.ktor.http.HttpHeaders +import io.ktor.http.HttpStatusCode +import kotlinx.coroutines.delay +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertNotEquals +import kotlin.test.assertTrue +import kotlin.time.Clock +import kotlin.time.Duration.Companion.minutes +import kotlin.time.Duration.Companion.seconds + +private const val BASIC_CHALLENGE = "Basic realm=\"messenger\", charset=\"UTF-8\"" + +class AuthenticationControllerTest { + + @Test + fun `valid Basic credentials open a session`() = runApp { client -> + val user = client.createUser("alice") + + val response = client.post("/api/v1/auth/sessions") { + basicAuth(user.username, TEST_PASSWORD) + } + + assertEquals(HttpStatusCode.Created, response.status) + + val session = response.body() + assertEquals("Bearer", session.tokenType) + assertEquals(user, session.user) + assertTrue(session.token.length >= 43, "token must carry at least 256 bits") + assertTrue(session.expiresAt > Clock.System.now() + 59.minutes) + } + + @Test + fun `each login issues a different token`() = runApp { client -> + val user = client.createUser("alice") + + val first = client.post("/api/v1/auth/sessions") { basicAuth(user.username, TEST_PASSWORD) } + val second = client.post("/api/v1/auth/sessions") { basicAuth(user.username, TEST_PASSWORD) } + + assertNotEquals(first.body().token, second.body().token) + } + + @Test + fun `password containing a colon and non-ASCII characters works`() = runApp { client -> + val password = "пароль:с-двоеточием" + val user = client.createUser("colon", password = password) + + val response = client.post("/api/v1/auth/sessions") { + basicAuth(user.username, password) + } + + assertEquals(HttpStatusCode.Created, response.status) + } + + @Test + fun `wrong password and unknown user get the same 401`() = runApp { client -> + val user = client.createUser("alice") + + val wrongPassword = client.post("/api/v1/auth/sessions") { + basicAuth(user.username, "wrong-password-123") + } + val unknownUser = client.post("/api/v1/auth/sessions") { + basicAuth("nobody_${user.username}".take(32), TEST_PASSWORD) + } + + for (response in listOf(wrongPassword, unknownUser)) { + assertEquals(HttpStatusCode.Unauthorized, response.status) + assertEquals(BASIC_CHALLENGE, response.headers[HttpHeaders.WWWAuthenticate]) + assertEquals("unauthorized", response.body().error.code) + } + } + + @Test + fun `missing or malformed Basic header returns 401`() = runApp { client -> + val missing = client.post("/api/v1/auth/sessions") + val malformed = client.post("/api/v1/auth/sessions") { + header(HttpHeaders.Authorization, "Basic !!!not-base64") + } + + for (response in listOf(missing, malformed)) { + assertEquals(HttpStatusCode.Unauthorized, response.status) + assertEquals(BASIC_CHALLENGE, response.headers[HttpHeaders.WWWAuthenticate]) + } + } + + @Test + fun `Bearer token cannot open a session`() = runApp { client -> + val alice = client.register("alice") + + val response = client.post("/api/v1/auth/sessions") { + auth(alice) + } + + assertEquals(HttpStatusCode.Unauthorized, response.status) + } + + @Test + fun `logout revokes only the current token`() = runApp { client -> + val alice = client.register("alice") + val secondSession = client.post("/api/v1/auth/sessions") { + basicAuth(alice.user.username, alice.password) + }.body() + + val logout = client.delete("/api/v1/auth/sessions/current") { auth(alice) } + assertEquals(HttpStatusCode.NoContent, logout.status) + + assertEquals(HttpStatusCode.Unauthorized, client.get("/api/v1/users/me") { auth(alice) }.status) + assertEquals(HttpStatusCode.Unauthorized, client.delete("/api/v1/auth/sessions/current") { auth(alice) }.status) + + val other = client.get("/api/v1/users/me") { bearerAuth(secondSession.token) } + assertEquals(HttpStatusCode.OK, other.status) + } + + @Test + fun `unknown token returns 401`() = runApp { client -> + val response = client.get("/api/v1/users/me") { + bearerAuth("this-token-was-never-issued-by-the-server-at-all") + } + + assertEquals(HttpStatusCode.Unauthorized, response.status) + } + + @Test + fun `session expires after SESSION_TTL_SECONDS`() = runApp(sessionTtlSeconds = 2) { client -> + val alice = client.register("alice") + + assertEquals(HttpStatusCode.OK, client.get("/api/v1/users/me") { auth(alice) }.status) + + delay(2.5.seconds) + + val expired = client.get("/api/v1/users/me") { auth(alice) } + assertEquals(HttpStatusCode.Unauthorized, expired.status) + assertEquals("Bearer realm=\"messenger\"", expired.headers[HttpHeaders.WWWAuthenticate]) + } +} diff --git a/src/test/kotlin/com/mtusi/messenger/main/infrastructure/controllers/chat/ChatControllerTest.kt b/src/test/kotlin/com/mtusi/messenger/main/infrastructure/controllers/chat/ChatControllerTest.kt new file mode 100644 index 00000000..f58f9053 --- /dev/null +++ b/src/test/kotlin/com/mtusi/messenger/main/infrastructure/controllers/chat/ChatControllerTest.kt @@ -0,0 +1,152 @@ +package com.mtusi.messenger.main.infrastructure.controllers.chat + +import com.mtusi.messenger.main.extensions.ErrorBody +import com.mtusi.messenger.main.infrastructure.controllers.chat.requests.CreateChatRequest +import com.mtusi.messenger.main.infrastructure.dto.Chat +import com.mtusi.messenger.main.infrastructure.dto.PageResponse +import com.mtusi.messenger.main.infrastructure.models.chat.ChatModel +import com.mtusi.messenger.main.support.TestUser +import com.mtusi.messenger.main.support.auth +import com.mtusi.messenger.main.support.createChat +import com.mtusi.messenger.main.support.register +import com.mtusi.messenger.main.support.runApp +import io.ktor.client.HttpClient +import io.ktor.client.call.body +import io.ktor.client.request.get +import io.ktor.client.request.post +import io.ktor.client.request.setBody +import io.ktor.client.statement.HttpResponse +import io.ktor.http.ContentType +import io.ktor.http.HttpStatusCode +import io.ktor.http.contentType +import org.jetbrains.exposed.v1.core.eq +import org.jetbrains.exposed.v1.jdbc.selectAll +import org.jetbrains.exposed.v1.jdbc.transactions.transaction +import java.util.UUID +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertNull + +class ChatControllerTest { + + private suspend fun HttpClient.postChat(actor: TestUser, title: String, memberIds: List): HttpResponse = + post("/api/v1/chats") { + auth(actor) + contentType(ContentType.Application.Json) + setBody(CreateChatRequest(title = title, memberIds = memberIds)) + } + + @Test + fun `member can read own chat and members keep their order`() = runApp { client -> + val alice = client.register("alice") + val bob = client.register("bob") + val created = client.createChat(alice, title = "notes", memberIds = listOf(bob.id, alice.id)) + + val response = client.get("/api/v1/chats/${created.id}") { + auth(bob) + } + + assertEquals(HttpStatusCode.OK, response.status) + assertEquals(created, response.body()) + assertEquals(listOf(bob.id, alice.id), created.memberIds) + } + + @Test + fun `foreign chat returns 404 for non-member`() = runApp { client -> + val alice = client.register("alice") + val eve = client.register("eve") + val chat = client.createChat(alice, title = "secret", memberIds = listOf(alice.id)) + + val response = client.get("/api/v1/chats/${chat.id}") { + auth(eve) + } + + assertEquals(HttpStatusCode.NotFound, response.status) + assertEquals("not_found", response.body().error.code) + } + + @Test + fun `invalid member lists are rejected with 400`() = runApp { client -> + val alice = client.register("alice") + val bob = client.register("bob") + + val cases = listOf( + listOf(bob.id.toString()), // creator is not a member + listOf(alice.id.toString(), alice.id.toString()), // duplicates + listOf(alice.id.toString(), "not-a-uuid"), // malformed id + listOf(alice.id.toString(), UUID.randomUUID().toString()), // unknown user + emptyList(), + ) + + for (memberIds in cases) { + val response = client.postChat(alice, "bad", memberIds) + assertEquals(HttpStatusCode.BadRequest, response.status, "member_ids=$memberIds") + } + } + + @Test + fun `unknown member is rejected before anything is written`() = runApp { client -> + val alice = client.register("alice") + val title = "atomic-${UUID.randomUUID()}" + + val response = client.postChat(alice, title, listOf(alice.id.toString(), UUID.randomUUID().toString())) + assertEquals(HttpStatusCode.BadRequest, response.status) + + val chats = transaction { + ChatModel.selectAll().where(ChatModel.title eq title).count() + } + assertEquals(0, chats) + } + + @Test + fun `chat list contains only own chats and is paginated`() = runApp { client -> + val alice = client.register("alice") + val bob = client.register("bob") + val eve = client.register("eve") + + val created = (0 until 5).map { + client.createChat(alice, title = "c$it", memberIds = listOf(alice.id, bob.id)) + } + client.createChat(eve, title = "eve only", memberIds = listOf(eve.id)) + + val collected = mutableListOf() + var cursor: String? = null + + do { + val page = client.get("/api/v1/chats") { + auth(bob) + url { + parameters.append("limit", "2") + cursor?.let { parameters.append("cursor", it) } + } + }.body>() + + collected += page.items + cursor = page.nextCursor + } while (cursor != null) + + assertEquals(created, collected) + + val evePage = client.get("/api/v1/chats") { auth(eve) }.body>() + assertEquals(listOf("eve only"), evePage.items.map { it.title }) + assertNull(evePage.nextCursor) + } + + @Test + fun `cursor issued to another user is rejected`() = runApp { client -> + val alice = client.register("alice") + val bob = client.register("bob") + repeat(2) { client.createChat(alice, title = "c$it", memberIds = listOf(alice.id)) } + + val cursor = client.get("/api/v1/chats?limit=1") { auth(alice) } + .body>() + .nextCursor!! + + val response = client.get("/api/v1/chats") { + auth(bob) + url { parameters.append("cursor", cursor) } + } + + assertEquals(HttpStatusCode.BadRequest, response.status) + } +} diff --git a/src/test/kotlin/com/mtusi/messenger/main/infrastructure/controllers/health/HealthControllerTest.kt b/src/test/kotlin/com/mtusi/messenger/main/infrastructure/controllers/health/HealthControllerTest.kt new file mode 100644 index 00000000..66a36f3f --- /dev/null +++ b/src/test/kotlin/com/mtusi/messenger/main/infrastructure/controllers/health/HealthControllerTest.kt @@ -0,0 +1,22 @@ +package com.mtusi.messenger.main.infrastructure.controllers.health + +import com.mtusi.messenger.main.support.runApp +import io.ktor.client.request.get +import io.ktor.http.HttpStatusCode +import kotlin.test.Test +import kotlin.test.assertEquals + +class HealthControllerTest { + + @Test + fun `live returns 200`() = runApp { client -> + val response = client.get("/health/live") + assertEquals(HttpStatusCode.OK, response.status) + } + + @Test + fun `ready returns 200 when database is reachable`() = runApp { client -> + val response = client.get("/health/ready") + assertEquals(HttpStatusCode.OK, response.status) + } +} diff --git a/src/test/kotlin/com/mtusi/messenger/main/infrastructure/controllers/message/MessageConcurrencyTest.kt b/src/test/kotlin/com/mtusi/messenger/main/infrastructure/controllers/message/MessageConcurrencyTest.kt new file mode 100644 index 00000000..5cc3bc1b --- /dev/null +++ b/src/test/kotlin/com/mtusi/messenger/main/infrastructure/controllers/message/MessageConcurrencyTest.kt @@ -0,0 +1,77 @@ +package com.mtusi.messenger.main.infrastructure.controllers.message + +import com.mtusi.messenger.main.infrastructure.controllers.message.requests.CreateMessageRequest +import com.mtusi.messenger.main.infrastructure.dto.Message +import com.mtusi.messenger.main.infrastructure.dto.PageResponse +import com.mtusi.messenger.main.support.auth +import com.mtusi.messenger.main.support.createChat +import com.mtusi.messenger.main.support.idempotencyKey +import com.mtusi.messenger.main.support.register +import com.mtusi.messenger.main.support.runApp +import io.ktor.client.call.body +import io.ktor.client.request.get +import io.ktor.client.request.post +import io.ktor.client.request.setBody +import io.ktor.http.ContentType +import io.ktor.http.HttpStatusCode +import io.ktor.http.contentType +import kotlinx.coroutines.async +import kotlinx.coroutines.awaitAll +import kotlinx.coroutines.coroutineScope +import kotlin.test.Test +import kotlin.test.assertEquals + +class MessageConcurrencyTest { + + @Test + fun `parallel senders produce exactly N unique messages`() = runApp { client -> + val alice = client.register("alice") + val chat = client.createChat(alice, title = "load", memberIds = listOf(alice.id)) + + val workers = 10 + val perWorker = 10 + val total = workers * perWorker + + coroutineScope { + (0 until workers).map { w -> + async { + repeat(perWorker) { i -> + val response = client.post("/api/v1/chats/${chat.id}/messages") { + auth(alice) + idempotencyKey() + contentType(ContentType.Application.Json) + setBody(CreateMessageRequest(text = "w$w-i$i")) + } + assertEquals(HttpStatusCode.Created, response.status) + } + } + }.awaitAll() + } + + val collected = mutableListOf() + var cursor: String? = null + + do { + val response = client.get("/api/v1/chats/${chat.id}/messages") { + auth(alice) + url { + parameters.append("limit", "100") + if (cursor != null) parameters.append("cursor", cursor!!) + } + } + assertEquals(HttpStatusCode.OK, response.status) + + val page = response.body>() + collected += page.items.map { it.text } + cursor = page.nextCursor + } while (cursor != null) + + assertEquals(total, collected.size) + assertEquals(total, collected.toSet().size) + + val expected = buildSet { + for (w in 0 until workers) for (i in 0 until perWorker) add("w$w-i$i") + } + assertEquals(expected, collected.toSet()) + } +} diff --git a/src/test/kotlin/com/mtusi/messenger/main/infrastructure/controllers/message/MessageControllerTest.kt b/src/test/kotlin/com/mtusi/messenger/main/infrastructure/controllers/message/MessageControllerTest.kt new file mode 100644 index 00000000..61ce8052 --- /dev/null +++ b/src/test/kotlin/com/mtusi/messenger/main/infrastructure/controllers/message/MessageControllerTest.kt @@ -0,0 +1,197 @@ +package com.mtusi.messenger.main.infrastructure.controllers.message + +import com.mtusi.messenger.main.extensions.ErrorBody +import com.mtusi.messenger.main.infrastructure.controllers.message.requests.CreateMessageRequest +import com.mtusi.messenger.main.infrastructure.dto.Message +import com.mtusi.messenger.main.infrastructure.dto.PageResponse +import com.mtusi.messenger.main.support.auth +import com.mtusi.messenger.main.support.createChat +import com.mtusi.messenger.main.support.idempotencyKey +import com.mtusi.messenger.main.support.register +import com.mtusi.messenger.main.support.runApp +import io.ktor.client.call.body +import io.ktor.client.request.get +import io.ktor.client.request.post +import io.ktor.client.request.setBody +import io.ktor.http.ContentType +import io.ktor.http.HttpStatusCode +import io.ktor.http.contentType +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertNotNull +import kotlin.test.assertNull + +class MessageControllerTest { + + @Test + fun `empty text returns 400 invalid_request`() = runApp { client -> + val alice = client.register("alice") + val chat = client.createChat(alice, title = "t", memberIds = listOf(alice.id)) + + val response = client.post("/api/v1/chats/${chat.id}/messages") { + auth(alice) + idempotencyKey() + contentType(ContentType.Application.Json) + setBody(CreateMessageRequest(text = "")) + } + + assertEquals(HttpStatusCode.BadRequest, response.status) + assertEquals("invalid_request", response.body().error.code) + } + + @Test + fun `foreign chat returns 404 when sending`() = runApp { client -> + val alice = client.register("alice") + val bob = client.register("bob") + val chat = client.createChat(alice, title = "t", memberIds = listOf(alice.id)) + + val response = client.post("/api/v1/chats/${chat.id}/messages") { + auth(bob) + idempotencyKey() + contentType(ContentType.Application.Json) + setBody(CreateMessageRequest(text = "hi")) + } + + assertEquals(HttpStatusCode.NotFound, response.status) + assertEquals("not_found", response.body().error.code) + } + + @Test + fun `foreign chat returns 404 when listing`() = runApp { client -> + val alice = client.register("alice") + val bob = client.register("bob") + val chat = client.createChat(alice, title = "t", memberIds = listOf(alice.id)) + + val response = client.get("/api/v1/chats/${chat.id}/messages") { + auth(bob) + } + + assertEquals(HttpStatusCode.NotFound, response.status) + } + + @Test + fun `pagination walks all messages exactly once`() = runApp { client -> + val alice = client.register("alice") + val chat = client.createChat(alice, title = "t", memberIds = listOf(alice.id)) + + val total = 25 + repeat(total) { i -> + val r = client.post("/api/v1/chats/${chat.id}/messages") { + auth(alice) + idempotencyKey() + contentType(ContentType.Application.Json) + setBody(CreateMessageRequest(text = "m$i")) + } + assertEquals(HttpStatusCode.Created, r.status) + } + + val collected = mutableListOf() + var cursor: String? = null + var pages = 0 + + do { + val response = client.get("/api/v1/chats/${chat.id}/messages") { + auth(alice) + url { + parameters.append("limit", "10") + if (cursor != null) parameters.append("cursor", cursor!!) + } + } + assertEquals(HttpStatusCode.OK, response.status) + + val page = response.body>() + collected += page.items.map { it.text } + cursor = page.nextCursor + pages++ + } while (cursor != null) + + assertEquals(3, pages) + assertEquals(total, collected.size) + assertEquals(total, collected.toSet().size) + assertEquals((0 until total).map { "m$it" }, collected) + } + + @Test + fun `history that is an exact multiple of limit ends with null next_cursor`() = runApp { client -> + val alice = client.register("alice") + val chat = client.createChat(alice, title = "t", memberIds = listOf(alice.id)) + + repeat(20) { i -> + client.post("/api/v1/chats/${chat.id}/messages") { + auth(alice) + idempotencyKey() + contentType(ContentType.Application.Json) + setBody(CreateMessageRequest(text = "m$i")) + } + } + + val first = client.get("/api/v1/chats/${chat.id}/messages") { + auth(alice) + url { parameters.append("limit", "10") } + }.body>() + + val cursor = first.nextCursor + assertNotNull(cursor) + + val second = client.get("/api/v1/chats/${chat.id}/messages") { + auth(alice) + url { + parameters.append("limit", "10") + parameters.append("cursor", cursor) + } + }.body>() + + assertEquals((10 until 20).map { "m$it" }, second.items.map { it.text }) + assertNull(second.nextCursor) + } + + @Test + fun `last page has null next_cursor`() = runApp { client -> + val alice = client.register("alice") + val chat = client.createChat(alice, title = "t", memberIds = listOf(alice.id)) + + val response = client.get("/api/v1/chats/${chat.id}/messages") { + auth(alice) + } + + assertEquals(HttpStatusCode.OK, response.status) + val page = response.body>() + assertEquals(0, page.items.size) + assertNull(page.nextCursor) + } + + @Test + fun `foreign cursor returns 400`() = runApp { client -> + val alice = client.register("alice") + val chatA = client.createChat(alice, title = "a", memberIds = listOf(alice.id)) + val chatB = client.createChat(alice, title = "b", memberIds = listOf(alice.id)) + + repeat(2) { i -> + client.post("/api/v1/chats/${chatA.id}/messages") { + auth(alice) + idempotencyKey() + contentType(ContentType.Application.Json) + setBody(CreateMessageRequest(text = "a$i")) + } + } + + val firstA = client.get("/api/v1/chats/${chatA.id}/messages") { + auth(alice) + url { parameters.append("limit", "1") } + }.body>() + + val cursor = firstA.nextCursor + assertNotNull(cursor) + + val response = client.get("/api/v1/chats/${chatB.id}/messages") { + auth(alice) + url { + parameters.append("limit", "1") + parameters.append("cursor", cursor) + } + } + + assertEquals(HttpStatusCode.BadRequest, response.status) + assertEquals("invalid_request", response.body().error.code) + } +} diff --git a/src/test/kotlin/com/mtusi/messenger/main/infrastructure/controllers/message/MessageIdempotencyTest.kt b/src/test/kotlin/com/mtusi/messenger/main/infrastructure/controllers/message/MessageIdempotencyTest.kt new file mode 100644 index 00000000..eb9728c5 --- /dev/null +++ b/src/test/kotlin/com/mtusi/messenger/main/infrastructure/controllers/message/MessageIdempotencyTest.kt @@ -0,0 +1,157 @@ +package com.mtusi.messenger.main.infrastructure.controllers.message + +import com.mtusi.messenger.main.extensions.ErrorBody +import com.mtusi.messenger.main.infrastructure.controllers.message.requests.CreateMessageRequest +import com.mtusi.messenger.main.infrastructure.dto.Chat +import com.mtusi.messenger.main.infrastructure.dto.Message +import com.mtusi.messenger.main.infrastructure.dto.PageResponse +import com.mtusi.messenger.main.support.TestUser +import com.mtusi.messenger.main.support.auth +import com.mtusi.messenger.main.support.createChat +import com.mtusi.messenger.main.support.idempotencyKey +import com.mtusi.messenger.main.support.register +import com.mtusi.messenger.main.support.runApp +import io.ktor.client.HttpClient +import io.ktor.client.call.body +import io.ktor.client.request.get +import io.ktor.client.request.post +import io.ktor.client.request.setBody +import io.ktor.client.statement.HttpResponse +import io.ktor.http.ContentType +import io.ktor.http.HttpStatusCode +import io.ktor.http.contentType +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.async +import kotlinx.coroutines.awaitAll +import kotlinx.coroutines.coroutineScope +import kotlinx.coroutines.delay +import kotlinx.coroutines.withContext +import java.util.UUID +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertNotEquals + +class MessageIdempotencyTest { + + private suspend fun HttpClient.send(user: TestUser, chat: Chat, text: String, key: String?): HttpResponse { + return post("/api/v1/chats/${chat.id}/messages") { + auth(user) + if (key != null) { + idempotencyKey(key) + } + contentType(ContentType.Application.Json) + setBody(CreateMessageRequest(text = text)) + } + } + + private suspend fun HttpClient.history(user: TestUser, chat: Chat): List { + return get("/api/v1/chats/${chat.id}/messages?limit=100") { auth(user) }.body>().items + } + + @Test + fun `retry with the same key and body returns the original message`() = runApp { client -> + val alice = client.register("alice") + val chat = client.createChat(alice, "retry", listOf(alice.id)) + val key = UUID.randomUUID().toString() + + val first = client.send(alice, chat, "hello", key) + val retry = client.send(alice, chat, "hello", key) + + assertEquals(HttpStatusCode.Created, first.status) + assertEquals(HttpStatusCode.Created, retry.status) + assertEquals(first.body(), retry.body()) + assertEquals(1, client.history(alice, chat).size) + } + + @Test + fun `same key with another body is a conflict`() = runApp { client -> + val alice = client.register("alice") + val chat = client.createChat(alice, "conflict", listOf(alice.id)) + val key = UUID.randomUUID().toString() + + client.send(alice, chat, "first", key) + val response = client.send(alice, chat, "second", key) + + assertEquals(HttpStatusCode.Conflict, response.status) + assertEquals("conflict", response.body().error.code) + assertEquals(listOf("first"), client.history(alice, chat).map { it.text }) + } + + @Test + fun `missing or malformed key is rejected before anything is written`() = runApp { client -> + val alice = client.register("alice") + val chat = client.createChat(alice, "keys", listOf(alice.id)) + + for (key in listOf(null, "", "has space", "кириллица", "a".repeat(129))) { + val response = client.send(alice, chat, "text", key) + assertEquals(HttpStatusCode.BadRequest, response.status, "key: $key") + assertEquals("invalid_request", response.body().error.code) + } + assertEquals(HttpStatusCode.Created, client.send(alice, chat, "ok", "A-z_0.9:" + "x".repeat(120)).status) + assertEquals(1, client.history(alice, chat).size) + } + + @Test + fun `key scope is user and chat`() = runApp { client -> + val alice = client.register("alice") + val bob = client.register("bob") + val first = client.createChat(alice, "one", listOf(alice.id, bob.id)) + val second = client.createChat(alice, "two", listOf(alice.id, bob.id)) + val key = UUID.randomUUID().toString() + + val ids = listOf( + client.send(alice, first, "same", key), + client.send(bob, first, "same", key), + client.send(alice, second, "same", key) + ).map { + assertEquals(HttpStatusCode.Created, it.status) + it.body().id + } + + assertEquals(3, ids.toSet().size) + } + + @Test + fun `retry by a former outsider is still checked for access`() = runApp { client -> + val alice = client.register("alice") + val eve = client.register("eve") + val chat = client.createChat(alice, "access", listOf(alice.id)) + val key = UUID.randomUUID().toString() + + client.send(alice, chat, "hello", key) + + assertEquals(HttpStatusCode.NotFound, client.send(eve, chat, "hello", key).status) + } + + @Test + fun `concurrent retries create exactly one message`() = runApp { client -> + val alice = client.register("alice") + val chat = client.createChat(alice, "concurrent", listOf(alice.id)) + val key = UUID.randomUUID().toString() + + val responses = withContext(Dispatchers.Default) { + coroutineScope { + (0 until 20).map { async { client.send(alice, chat, "once", key) } }.awaitAll() + } + } + + assertEquals(List(20) { HttpStatusCode.Created }, responses.map { it.status }) + assertEquals(1, responses.map { it.body() }.toSet().size) + assertEquals(1, client.history(alice, chat).size) + } + + @Test + fun `expired key can be used for a new message`() = runApp(idempotencyTtlSeconds = 1) { client -> + val alice = client.register("alice") + val chat = client.createChat(alice, "ttl", listOf(alice.id)) + val key = UUID.randomUUID().toString() + + val first = client.send(alice, chat, "hello", key).body() + delay(1500) + val second = client.send(alice, chat, "hello again", key) + + assertEquals(HttpStatusCode.Created, second.status) + assertNotEquals(first.id, second.body().id) + } + +} diff --git a/src/test/kotlin/com/mtusi/messenger/main/infrastructure/controllers/user/UserControllerTest.kt b/src/test/kotlin/com/mtusi/messenger/main/infrastructure/controllers/user/UserControllerTest.kt new file mode 100644 index 00000000..b2d7dfea --- /dev/null +++ b/src/test/kotlin/com/mtusi/messenger/main/infrastructure/controllers/user/UserControllerTest.kt @@ -0,0 +1,180 @@ +package com.mtusi.messenger.main.infrastructure.controllers.user + +import com.mtusi.messenger.main.extensions.ErrorBody +import com.mtusi.messenger.main.infrastructure.controllers.user.requests.CreateUserRequest +import com.mtusi.messenger.main.infrastructure.dto.User +import com.mtusi.messenger.main.support.TEST_PASSWORD +import com.mtusi.messenger.main.support.auth +import com.mtusi.messenger.main.support.register +import com.mtusi.messenger.main.support.runApp +import com.mtusi.messenger.main.support.uniqueUsername +import io.ktor.client.call.body +import io.ktor.client.request.basicAuth +import io.ktor.client.request.get +import io.ktor.client.request.header +import io.ktor.client.request.post +import io.ktor.client.request.setBody +import io.ktor.client.statement.bodyAsText +import io.ktor.http.ContentType +import io.ktor.http.HttpHeaders +import io.ktor.http.HttpStatusCode +import io.ktor.http.contentType +import kotlinx.coroutines.async +import kotlinx.coroutines.awaitAll +import kotlinx.coroutines.coroutineScope +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertFalse +import kotlin.test.assertNotNull + +class UserControllerTest { + + @Test + fun `create user returns 201 without password`() = runApp { client -> + val username = uniqueUsername("alice") + + val response = client.post("/api/v1/users") { + contentType(ContentType.Application.Json) + setBody(CreateUserRequest(username = username, displayName = "Alice", password = TEST_PASSWORD)) + } + + assertEquals(HttpStatusCode.Created, response.status) + + val body = response.bodyAsText() + assertFalse("password" in body) + assertFalse(TEST_PASSWORD in body) + assertEquals(username, response.body().username) + } + + @Test + fun `invalid JSON body returns 400 invalid_request`() = runApp { client -> + val response = client.post("/api/v1/users") { + contentType(ContentType.Application.Json) + setBody("{not json") + } + + assertEquals(HttpStatusCode.BadRequest, response.status) + + val body = response.body() + assertEquals("invalid_request", body.error.code) + assertNotNull(body.error.requestId) + } + + @Test + fun `missing password returns 400`() = runApp { client -> + val response = client.post("/api/v1/users") { + contentType(ContentType.Application.Json) + setBody("""{"username":"${uniqueUsername("nopass")}","display_name":"x"}""") + } + + assertEquals(HttpStatusCode.BadRequest, response.status) + } + + @Test + fun `password shorter than 12 code points returns 400`() = runApp { client -> + val response = client.post("/api/v1/users") { + contentType(ContentType.Application.Json) + setBody(CreateUserRequest(username = uniqueUsername("short"), displayName = "x", password = "elevenchars")) + } + + assertEquals(HttpStatusCode.BadRequest, response.status) + } + + @Test + fun `duplicate username returns 409 conflict`() = runApp { client -> + val username = uniqueUsername("bob") + + val request = CreateUserRequest(username = username, displayName = "Bob", password = TEST_PASSWORD) + + val first = client.post("/api/v1/users") { + contentType(ContentType.Application.Json) + setBody(request) + } + assertEquals(HttpStatusCode.Created, first.status) + + val second = client.post("/api/v1/users") { + contentType(ContentType.Application.Json) + setBody(request.copy(displayName = "Bob 2")) + } + + assertEquals(HttpStatusCode.Conflict, second.status) + assertEquals("conflict", second.body().error.code) + } + + @Test + fun `concurrent registrations of one username create exactly one user`() = runApp { client -> + val username = uniqueUsername("race") + val attempts = 10 + + val statuses = coroutineScope { + (0 until attempts).map { + async { + client.post("/api/v1/users") { + contentType(ContentType.Application.Json) + setBody(CreateUserRequest(username = username, displayName = "r$it", password = TEST_PASSWORD)) + }.status + } + }.awaitAll() + } + + assertEquals(1, statuses.count { it == HttpStatusCode.Created }) + assertEquals(attempts - 1, statuses.count { it == HttpStatusCode.Conflict }) + } + + @Test + fun `me returns the session owner`() = runApp { client -> + val alice = client.register("alice") + + val response = client.get("/api/v1/users/me") { + auth(alice) + } + + assertEquals(HttpStatusCode.OK, response.status) + assertEquals(alice.user, response.body()) + } + + @Test + fun `missing token returns 401 with Bearer challenge`() = runApp { client -> + val response = client.get("/api/v1/users/me") + + assertEquals(HttpStatusCode.Unauthorized, response.status) + assertEquals("unauthorized", response.body().error.code) + assertEquals("Bearer realm=\"messenger\"", response.headers[HttpHeaders.WWWAuthenticate]) + } + + @Test + fun `X-User-Id alone gives no access`() = runApp { client -> + val alice = client.register("alice") + + val response = client.get("/api/v1/users/me") { + header("X-User-Id", alice.id.toString()) + } + + assertEquals(HttpStatusCode.Unauthorized, response.status) + } + + @Test + fun `X-User-Id does not override bearer identity`() = runApp { client -> + val alice = client.register("alice") + val eve = client.register("eve") + + val response = client.get("/api/v1/users/me") { + auth(alice) + header("X-User-Id", eve.id.toString()) + } + + assertEquals(alice.id, response.body().id) + } + + @Test + fun `Basic credentials are rejected on business endpoints`() = runApp { client -> + val alice = client.register("alice") + + val response = client.get("/api/v1/users/me") { + basicAuth(alice.user.username, alice.password) + } + + assertEquals(HttpStatusCode.Unauthorized, response.status) + assertEquals("Bearer realm=\"messenger\"", response.headers[HttpHeaders.WWWAuthenticate]) + } +} diff --git a/src/test/kotlin/com/mtusi/messenger/main/infrastructure/repositories/authentication/session/cache/RedisSessionCacheRepositoryTest.kt b/src/test/kotlin/com/mtusi/messenger/main/infrastructure/repositories/authentication/session/cache/RedisSessionCacheRepositoryTest.kt new file mode 100644 index 00000000..8f27e66f --- /dev/null +++ b/src/test/kotlin/com/mtusi/messenger/main/infrastructure/repositories/authentication/session/cache/RedisSessionCacheRepositoryTest.kt @@ -0,0 +1,83 @@ +package com.mtusi.messenger.main.infrastructure.repositories.authentication.session.cache + +import com.mtusi.messenger.main.config.redis.SerialRedisConfig +import com.mtusi.messenger.main.infrastructure.dto.authentication.Session +import com.mtusi.messenger.main.infrastructure.repositories._common.RedisConnection +import com.mtusi.messenger.main.support.TestRedis +import com.mtusi.messenger.main.utils.nowMicros +import kotlinx.coroutines.test.runTest +import java.util.UUID +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertNull +import kotlin.test.assertTrue +import kotlin.time.Clock +import kotlin.time.Duration +import kotlin.time.Duration.Companion.minutes + +class RedisSessionCacheRepositoryTest { + + private fun withRepository(block: suspend (RedisSessionCacheRepository) -> Unit) = runTest { + val endpoint = TestRedis.endpoint() + val connection = RedisConnection( + SerialRedisConfig( + host = endpoint.host, + port = endpoint.port, + password = TestRedis.PASSWORD, + connectTimeoutMs = 2000, + commandTimeoutMs = 2000 + ) + ) + connection.use { block(RedisSessionCacheRepository(it)) } + } + + private fun session(ttl: Duration = 10.minutes): Session { + val now = Clock.System.nowMicros() + return Session( + id = UUID.randomUUID(), + userId = UUID.randomUUID(), + tokenHash = UUID.randomUUID().toString().replace("-", ""), + createdAt = now, + expiresAt = now + ttl + ) + } + + private fun ttlSeconds(session: Session): Long { + return TestRedis.cli(TestRedis.container, "TTL", "session:${session.tokenHash}").toLong() + } + + @Test + fun `cached session round-trips and expires together with the session`() = withRepository { repository -> + val session = session() + + assertNull(repository.find(session.tokenHash)) + + repository.putIfAbsent(session, cachedUntil = session.expiresAt) + + assertEquals(CachedSession.Active(session), repository.find(session.tokenHash)) + assertTrue(ttlSeconds(session) in 590..600, "key must expire at cachedUntil") + } + + @Test + fun `revoke replaces a cached session with a tombstone`() = withRepository { repository -> + val session = session() + repository.putIfAbsent(session, cachedUntil = session.expiresAt) + + repository.revoke(session.tokenHash, session.expiresAt) + + assertEquals(CachedSession.Revoked, repository.find(session.tokenHash)) + assertTrue(ttlSeconds(session) in 590..600, "tombstone must live until the session would expire") + } + + @Test + fun `late cache fill cannot overwrite a tombstone`() = withRepository { repository -> + val session = session() + + // A lookup read the session from PostgreSQL before logout and stores it after the logout finished. + repository.revoke(session.tokenHash, session.expiresAt) + repository.putIfAbsent(session, cachedUntil = session.expiresAt) + + assertEquals(CachedSession.Revoked, repository.find(session.tokenHash)) + } + +} diff --git a/src/test/kotlin/com/mtusi/messenger/main/infrastructure/repositories/chat/ExposedChatRepositoryTest.kt b/src/test/kotlin/com/mtusi/messenger/main/infrastructure/repositories/chat/ExposedChatRepositoryTest.kt new file mode 100644 index 00000000..e6964807 --- /dev/null +++ b/src/test/kotlin/com/mtusi/messenger/main/infrastructure/repositories/chat/ExposedChatRepositoryTest.kt @@ -0,0 +1,62 @@ +package com.mtusi.messenger.main.infrastructure.repositories.chat + +import com.mtusi.messenger.main.infrastructure.models.chat.ChatMemberModel +import com.mtusi.messenger.main.infrastructure.models.chat.ChatModel +import com.mtusi.messenger.main.support.register +import com.mtusi.messenger.main.support.runApp +import org.jetbrains.exposed.v1.core.eq +import org.jetbrains.exposed.v1.exceptions.ExposedSQLException +import org.jetbrains.exposed.v1.jdbc.selectAll +import org.jetbrains.exposed.v1.jdbc.transactions.transaction +import java.util.UUID +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertFailsWith + +private const val FOREIGN_KEY_VIOLATION = "23503" + +/** + * The service validates members before writing, so the rollback is exercised here directly: the chat row is + * inserted, the membership batch then fails on the foreign key, and the whole transaction must disappear. + */ +class ExposedChatRepositoryTest { + + private val repository = ExposedChatRepository() + + @Test + fun `chat row is rolled back when inserting members fails`() = runApp { client -> + val alice = client.register("alice") + val title = "rollback-${UUID.randomUUID()}" + + val error = assertFailsWith { + transaction { + repository.create(title = title, memberIds = listOf(alice.id, UUID.randomUUID())) + } + } + assertEquals(FOREIGN_KEY_VIOLATION, error.sqlState) + + val (chats, memberships) = transaction { + val chats = ChatModel.selectAll().where(ChatModel.title eq title).count() + val memberships = ChatMemberModel.selectAll().where(ChatMemberModel.userId eq alice.id).count() + chats to memberships + } + assertEquals(0, chats) + assertEquals(0, memberships) + } + + @Test + fun `successful creation commits the chat and every member`() = runApp { client -> + val alice = client.register("alice") + val bob = client.register("bob") + + val chat = transaction { + repository.create(title = "committed", memberIds = listOf(alice.id, bob.id)) + } + + val memberships = transaction { + ChatMemberModel.selectAll().where(ChatMemberModel.chatId eq chat.id).count() + } + assertEquals(2, memberships) + } + +} diff --git a/src/test/kotlin/com/mtusi/messenger/main/infrastructure/repositories/message/MessageAttachmentsQueryTest.kt b/src/test/kotlin/com/mtusi/messenger/main/infrastructure/repositories/message/MessageAttachmentsQueryTest.kt new file mode 100644 index 00000000..306d23ff --- /dev/null +++ b/src/test/kotlin/com/mtusi/messenger/main/infrastructure/repositories/message/MessageAttachmentsQueryTest.kt @@ -0,0 +1,58 @@ +package com.mtusi.messenger.main.infrastructure.repositories.message + +import com.mtusi.messenger.main.infrastructure.dto.Attachment +import com.mtusi.messenger.main.infrastructure.repositories.attachment.ExposedAttachmentRepository +import com.mtusi.messenger.main.plugins.suspendedTransaction +import com.mtusi.messenger.main.support.createChat +import com.mtusi.messenger.main.support.register +import com.mtusi.messenger.main.support.runApp +import java.util.UUID +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.time.Clock + +class MessageAttachmentsQueryTest { + + @Test + fun `history loads ordered attachment links in one query for the entire page`() = runApp { client -> + val user = client.register("historyfiles") + val chat = client.createChat(user, "history", listOf(user.id)) + val attachments = ExposedAttachmentRepository() + val messages = ExposedMessageRepository(attachments) + + suspendedTransaction { + val ids = List(2) { + val id = UUID.randomUUID() + attachments.create(Attachment( + id = id, + chatId = chat.id, + ownerId = user.id, + filename = "file.png", + contentType = "image/png", + sizeBytes = 1, + sha256 = "0".repeat(64), + createdAt = Clock.System.now() + )) + id + } + val created = List(100) { index -> + val message = messages.create(chat.id, user.id, "message $index") + if (index % 2 == 0) { + attachments.attach(message.id, ids.reversed()) + } + message + } + val before = statementCount + val page = messages.findPageByChatId(chat.id, null, 100) + assertEquals(2, statementCount - before) + assertEquals(100, page.size) + val expected = created.mapIndexed { index, message -> + message.id to if (index % 2 == 0) ids.reversed() else emptyList() + }.toMap() + page.forEach { message -> + assertEquals(expected[message.id], message.attachmentIds) + } + assertEquals(ids.reversed(), messages.findById(created.first().id)?.attachmentIds) + } + } +} diff --git a/src/test/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/AttachmentCleanupTest.kt b/src/test/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/AttachmentCleanupTest.kt new file mode 100644 index 00000000..9f1a4eae --- /dev/null +++ b/src/test/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/AttachmentCleanupTest.kt @@ -0,0 +1,124 @@ +package com.mtusi.messenger.main.infrastructure.services.attachment + +import com.mtusi.messenger.main.config.storage.SerialStorageConfig +import com.mtusi.messenger.main.infrastructure.repositories.attachment.ExposedAttachmentRepository +import com.mtusi.messenger.main.infrastructure.repositories.attachment.ExposedUploadJournalRepository +import com.mtusi.messenger.main.infrastructure.repositories.attachment.storage.ObjectStorageRepository +import com.mtusi.messenger.main.infrastructure.repositories.attachment.storage.S3ObjectStorageRepository +import com.mtusi.messenger.main.infrastructure.repositories.chat.ExposedChatRepository +import com.mtusi.messenger.main.infrastructure.types.ApiException +import com.mtusi.messenger.main.plugins.suspendedTransaction +import com.mtusi.messenger.main.support.TestStorage +import com.mtusi.messenger.main.support.createChat +import com.mtusi.messenger.main.support.register +import com.mtusi.messenger.main.support.runApp +import com.mtusi.messenger.main.infrastructure.models.attachment.UploadJournalModel +import com.mtusi.messenger.main.utils.exposed.operations.plus +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.withContext +import org.jetbrains.exposed.v1.core.eq +import org.jetbrains.exposed.v1.core.longLiteral +import org.jetbrains.exposed.v1.datetime.CurrentTimestamp +import org.jetbrains.exposed.v1.jdbc.update +import java.awt.image.BufferedImage +import java.io.File +import java.nio.file.Files +import java.util.UUID +import javax.imageio.ImageIO +import kotlin.test.Test +import kotlin.test.assertContentEquals +import kotlin.test.assertEquals +import kotlin.test.assertFailsWith +import kotlin.test.assertFalse +import kotlin.test.assertTrue + +class AttachmentCleanupTest { + + @Test + fun `orphan cleanup respects grace and fences late publishers`() = runApp { client -> + + val user = client.register("cleanup") + val chat = client.createChat(user, "cleanup", listOf(user.id)) + val journal = ExposedUploadJournalRepository() + val attachments = ExposedAttachmentRepository() + val storage = S3ObjectStorageRepository(SerialStorageConfig( + endpoint = TestStorage.endpoint, + bucket = TestStorage.BUCKET, + accessKey = TestStorage.ACCESS_KEY, + secretKey = TestStorage.SECRET_KEY + )) + val cleanup = AttachmentCleanupService(journal, storage) + + val file = withContext(Dispatchers.IO) { + Files.createTempFile("cleanup-test-", ".png") + }.toFile() + + withContext(Dispatchers.IO) { + ImageIO.write(BufferedImage(20, 20, BufferedImage.TYPE_INT_RGB), "png", file) + } + + var orphanId: UUID? = null + val failingStorage = object : ObjectStorageRepository by storage { + override suspend fun put(id: UUID, file: File, contentType: String) { + storage.put(id, file, contentType) + orphanId = id + error("injected failure after PUT, before metadata COMMIT") + } + } + try { + val failingService = DefaultAttachmentService(journal, attachments, ExposedChatRepository(), failingStorage) + + assertFailsWith { + failingService.upload(chat.id, user.id, "file.png", "image/png", file) + } + + val id = requireNotNull(orphanId) + assertEquals(null, suspendedTransaction { attachments.findById(id) }) + assertContentEquals(file.readBytes(), storage.get(id)) + cleanup.cleanup() + assertContentEquals(file.readBytes(), storage.get(id), "fresh upload must survive grace") + + val service = DefaultAttachmentService(journal, attachments, ExposedChatRepository(), storage) + val published = service.upload(chat.id, user.id, "ready.png", "image/png", file) + suspendedTransaction { + UploadJournalModel.update({ UploadJournalModel.id eq id }) { + it[expiresAt] = CurrentTimestamp + longLiteral(-1) + it[nextAttemptAt] = CurrentTimestamp + longLiteral(-1) + } + } + + val unavailableStorage = object : ObjectStorageRepository by storage { + override suspend fun delete(id: UUID) { + error("injected S3 delete outage") + } + } + + assertEquals(0, AttachmentCleanupService(journal, unavailableStorage).cleanup()) + assertContentEquals(file.readBytes(), storage.get(id)) + suspendedTransaction { + UploadJournalModel.update({ UploadJournalModel.id eq id }) { + it[nextAttemptAt] = CurrentTimestamp + longLiteral(-1) + } + } + + assertTrue(cleanup.cleanup() >= 1) + assertFailsWith { storage.get(id) } + assertFalse(suspendedTransaction { journal.lockForCompletion(id) }) + assertContentEquals(file.readBytes(), storage.get(published.id)) + + // A process suspended longer than grace can complete a late PUT, but cannot publish metadata. + storage.put(id, file, "image/png") + suspendedTransaction { + UploadJournalModel.update({ UploadJournalModel.id eq id }) { + it[nextAttemptAt] = CurrentTimestamp + longLiteral(-1) + } + } + + assertTrue(cleanup.cleanup() >= 1) + assertFailsWith { storage.get(id) } + assertContentEquals(file.readBytes(), storage.get(published.id)) + } finally { + file.delete() + } + } +} diff --git a/src/test/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/ImageValidatorTest.kt b/src/test/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/ImageValidatorTest.kt new file mode 100644 index 00000000..9d627861 --- /dev/null +++ b/src/test/kotlin/com/mtusi/messenger/main/infrastructure/services/attachment/ImageValidatorTest.kt @@ -0,0 +1,84 @@ +package com.mtusi.messenger.main.infrastructure.services.attachment + +import com.mtusi.messenger.main.infrastructure.types.ApiException +import com.mtusi.messenger.main.infrastructure.types.ErrorCode +import java.awt.image.BufferedImage +import java.io.ByteArrayOutputStream +import java.io.DataOutputStream +import java.util.zip.DeflaterOutputStream +import java.nio.ByteBuffer +import java.util.zip.CRC32 +import javax.imageio.ImageIO +import kotlin.test.* + +class ImageValidatorTest { + + private fun png() = ByteArrayOutputStream().also { + ImageIO.write(BufferedImage(2, 2, BufferedImage.TYPE_INT_RGB), "png", it) + }.toByteArray() + + @Test + fun `pixel limit is enforced on header before decoding`() { + val bytes = png() + ByteBuffer.wrap(bytes, 16, 8).putInt(10000).putInt(4001) + val crc = CRC32().apply { update(bytes, 12, 17) }.value + ByteBuffer.wrap(bytes, 29, 4).putInt(crc.toInt()) + assertEquals(ErrorCode.PAYLOAD_TOO_LARGE, assertFailsWith { + ImageValidator.validate(bytes, "image/png") + }.code) + } + @Test + fun `corrupted CRC and missing end chunk are rejected`() { + val good = png() + for (bytes in listOf(good.copyOf(good.size - 12), good.clone().apply { this[29] = (this[29].toInt() xor 1).toByte() })) { + assertEquals(ErrorCode.UNSUPPORTED_MEDIA_TYPE, assertFailsWith { + ImageValidator.validate(bytes, "image/png") + }.code) + } + } + @Test + fun `compressed text metadata does not consume the decoded image memory budget`() { + val compressed = ByteArrayOutputStream().also { output -> + DeflaterOutputStream(output).use { + it.write(ByteArray(262144) { 65 }) + } + }.toByteArray() + + for (imageType in listOf(BufferedImage.TYPE_INT_RGB, BufferedImage.TYPE_BYTE_INDEXED)) { + val original = ByteArrayOutputStream().also { + ImageIO.write(BufferedImage(2, 2, imageType), "png", it) + }.toByteArray() + val text = "Comment".toByteArray() + byteArrayOf(0, 0) + compressed + val internationalText = "Comment".toByteArray() + byteArrayOf(0, 1, 0, 0, 0) + compressed + val bytes = ByteArrayOutputStream().also { output -> + output.write(original, 0, 33) + repeat(2500) { + output.write(chunk("zTXt", text)) + output.write(chunk("iTXt", internationalText)) + } + output.write(original, 33, original.size - 33) + }.toByteArray() + + // Less than 2 MiB on the wire, over 1 GiB if all text is retained after inflation. + assertTrue(bytes.size < MAX_ATTACHMENT_BYTES) + assertEquals("image/png", ImageValidator.validate(bytes, "image/png")) + } + } + + private fun chunk(type: String, data: ByteArray): ByteArray { + val name = type.toByteArray(Charsets.US_ASCII) + val crc = CRC32().apply { + update(name) + update(data) + } + return ByteArrayOutputStream().also { output -> + DataOutputStream(output).use { + it.writeInt(data.size) + it.write(name) + it.write(data) + it.writeInt(crc.value.toInt()) + } + }.toByteArray() + } + +} diff --git a/src/test/kotlin/com/mtusi/messenger/main/infrastructure/services/authentication/CrossReplicaRevocationTest.kt b/src/test/kotlin/com/mtusi/messenger/main/infrastructure/services/authentication/CrossReplicaRevocationTest.kt new file mode 100644 index 00000000..0024b4dc --- /dev/null +++ b/src/test/kotlin/com/mtusi/messenger/main/infrastructure/services/authentication/CrossReplicaRevocationTest.kt @@ -0,0 +1,152 @@ +package com.mtusi.messenger.main.infrastructure.services.authentication + +import com.mtusi.messenger.main.config.authentication.SerialAuthenticationConfig +import com.mtusi.messenger.main.config.redis.SerialRedisConfig +import com.mtusi.messenger.main.infrastructure.dto.authentication.IssuedSession +import com.mtusi.messenger.main.infrastructure.repositories._common.RedisConnection +import com.mtusi.messenger.main.infrastructure.repositories.authentication.session.ExposedSessionRepository +import com.mtusi.messenger.main.infrastructure.repositories.authentication.session.cache.RedisSessionCacheRepository +import com.mtusi.messenger.main.infrastructure.repositories.authentication.session.revocation.ExposedSessionRevocationRepository +import com.mtusi.messenger.main.infrastructure.repositories.user.ExposedUserRepository +import com.mtusi.messenger.main.infrastructure.services.hashing.DefaultHashingService +import com.mtusi.messenger.main.infrastructure.types.ApiException +import com.mtusi.messenger.main.infrastructure.types.ErrorCode +import com.mtusi.messenger.main.plugins.applyDatabaseMigrations +import com.mtusi.messenger.main.support.TestDatabase +import com.mtusi.messenger.main.support.TestRedis +import com.mtusi.messenger.main.support.uniqueUsername +import com.mtusi.messenger.main.utils.computeSha256 +import com.zaxxer.hikari.HikariDataSource +import kotlinx.coroutines.runBlocking +import org.jetbrains.exposed.v1.jdbc.Database +import org.jetbrains.exposed.v1.jdbc.transactions.transaction +import org.testcontainers.containers.GenericContainer +import kotlin.test.AfterTest +import kotlin.test.BeforeTest +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertFailsWith +import kotlin.test.assertNotNull +import kotlin.test.assertNull +import kotlin.test.assertTrue +import kotlin.time.Duration.Companion.seconds +import kotlin.time.TimeSource + +/** + * Two services sharing PostgreSQL and Redis model two API replicas. Redis keeps serving reads while writes are + * paused (CLIENT PAUSE ... WRITE), so the replica that did not handle the logout still finds the cached session. + */ +class CrossReplicaRevocationTest { + + private lateinit var redis: GenericContainer<*> + private val connections = mutableListOf() + + @BeforeTest + fun start() { + redis = TestRedis.create().also { it.start() } + HikariDataSource().apply { + jdbcUrl = TestDatabase.container.jdbcUrl + username = TestDatabase.USERNAME + password = TestDatabase.PASSWORD + }.use(::applyDatabaseMigrations) + TestDatabase.container.let { Database.connect(it.jdbcUrl, user = it.username, password = it.password) } + } + + @AfterTest + fun stop() { + connections.forEach { it.close() } + redis.stop() + } + + private fun replica(revokeTimeoutMs: Long): DefaultAuthenticationService { + val endpoint = TestRedis.endpoint(redis) + val connection = RedisConnection( + SerialRedisConfig( + host = endpoint.host, + port = endpoint.port, + password = TestRedis.PASSWORD, + connectTimeoutMs = 500, + commandTimeoutMs = 500 + ) + ).also(connections::add) + + return DefaultAuthenticationService( + userRepository = ExposedUserRepository(), + sessionRepository = ExposedSessionRepository(), + sessionCacheRepository = RedisSessionCacheRepository(connection), + sessionRevocationRepository = ExposedSessionRevocationRepository(), + hashingService = DefaultHashingService(), + config = SerialAuthenticationConfig( + realm = "messenger", + sessionTtlSeconds = 3600, + sessionCacheTtlSeconds = 60, + sessionRevokeTimeoutMs = revokeTimeoutMs + ) + ) + } + + private fun login(service: DefaultAuthenticationService): IssuedSession = transaction { + val user = checkNotNull(ExposedUserRepository().create(uniqueUsername("replica"), "replica", "not-a-real-hash")) + service.createSession(user.id) + } + + private fun cached(issued: IssuedSession): String { + return TestRedis.cli(redis, "GET", "session:${issued.token.toByteArray().computeSha256()}") + } + + private fun pauseWrites(ms: Long) { + assertEquals("OK", TestRedis.cli(redis, "CLIENT", "PAUSE", ms.toString(), "WRITE")) + } + + @Test + fun `logout waits for paused Redis writes and then no replica accepts the token`() = runBlocking { + val a = replica(revokeTimeoutMs = 8000) + val b = replica(revokeTimeoutMs = 8000) + val issued = login(a) + + // Both replicas have read the session, Redis holds the active copy. + assertNotNull(a.findSessionByToken(issued.token)) + assertNotNull(b.findSessionByToken(issued.token)) + assertTrue(cached(issued).contains(issued.session.id.toString())) + + pauseWrites(3000) + + val started = TimeSource.Monotonic.markNow() + a.revokeSession(issued.session) + assertTrue(started.elapsedNow() > 2.seconds, "the logout must wait for the tombstone, not answer early") + + assertEquals("revoked", cached(issued)) + assertNull(b.findSessionByToken(issued.token), "the other replica must reject the token") + assertNull(a.findSessionByToken(issued.token)) + } + + @Test + fun `unconfirmed logout makes every replica bypass the cache`() = runBlocking { + val a = replica(revokeTimeoutMs = 1000) + val b = replica(revokeTimeoutMs = 1000) + val issued = login(a) + + assertNotNull(a.findSessionByToken(issued.token)) + assertNotNull(b.findSessionByToken(issued.token)) + + pauseWrites(6000) + val paused = TimeSource.Monotonic.markNow() + + val error = assertFailsWith { a.revokeSession(issued.session) } + assertEquals(ErrorCode.UNAVAILABLE, error.code) + + // The periodic check (once a second on every replica) sees the pending revocation in PostgreSQL. + b.syncPendingRevocations() + assertNull(b.findSessionByToken(issued.token), "a replica that saw no Redis failure must not trust the cache") + assertNull(a.findSessionByToken(issued.token)) + + // Once Redis accepts writes again the tombstone is written and the cache is trusted again. + while (paused.elapsedNow() < 7.seconds) { + Thread.sleep(200) + } + b.syncPendingRevocations() + assertEquals("revoked", cached(issued)) + assertNull(b.findSessionByToken(issued.token)) + } + +} diff --git a/src/test/kotlin/com/mtusi/messenger/main/infrastructure/services/authentication/SessionLookupSingleFlightTest.kt b/src/test/kotlin/com/mtusi/messenger/main/infrastructure/services/authentication/SessionLookupSingleFlightTest.kt new file mode 100644 index 00000000..01df4eed --- /dev/null +++ b/src/test/kotlin/com/mtusi/messenger/main/infrastructure/services/authentication/SessionLookupSingleFlightTest.kt @@ -0,0 +1,174 @@ +package com.mtusi.messenger.main.infrastructure.services.authentication + +import com.mtusi.messenger.main.config.authentication.SerialAuthenticationConfig +import com.mtusi.messenger.main.infrastructure.dto.authentication.Session +import com.mtusi.messenger.main.infrastructure.dto.authentication.SessionRevocation +import com.mtusi.messenger.main.infrastructure.repositories.authentication.session.SessionRepository +import com.mtusi.messenger.main.infrastructure.repositories.authentication.session.cache.CachedSession +import com.mtusi.messenger.main.infrastructure.repositories.authentication.session.cache.SessionCacheRepository +import com.mtusi.messenger.main.infrastructure.repositories.authentication.session.revocation.SessionRevocationRepository +import com.mtusi.messenger.main.infrastructure.repositories.user.ExposedUserRepository +import com.mtusi.messenger.main.infrastructure.services.hashing.DefaultHashingService +import com.mtusi.messenger.main.support.TestDatabase +import com.mtusi.messenger.main.utils.computeSha256 +import com.mtusi.messenger.main.utils.nowMicros +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.async +import kotlinx.coroutines.awaitAll +import kotlinx.coroutines.delay +import kotlinx.coroutines.runBlocking +import org.jetbrains.exposed.v1.jdbc.Database +import java.util.UUID +import java.util.concurrent.atomic.AtomicInteger +import kotlin.test.BeforeTest +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertTrue +import kotlin.time.Clock +import kotlin.time.Duration +import kotlin.time.Duration.Companion.hours +import kotlin.time.Instant + +private const val CONCURRENT_REQUESTS = 50 +private const val SLOW_QUERY_MS = 200L + +/** + * Cold cache and a deliberately slow lookup, so every request is guaranteed to overlap with the first one. + */ +class SessionLookupSingleFlightTest { + + private val token = "token-${UUID.randomUUID()}" + private val tokenHash = token.toByteArray().computeSha256() + + private val session = Clock.System.nowMicros().let { + Session( + id = UUID.randomUUID(), + userId = UUID.randomUUID(), + tokenHash = tokenHash, + createdAt = it, + expiresAt = it + 1.hours + ) + } + + private class CountingSessionRepository(private val session: Session) : SessionRepository { + + val lookups = AtomicInteger() + + override fun findActiveByTokenHash(tokenHash: String, now: Instant): Session? { + lookups.incrementAndGet() + Thread.sleep(SLOW_QUERY_MS) + return session.takeIf { it.tokenHash == tokenHash } + } + + override fun create(userId: UUID, tokenHash: String, ttl: Duration) = error("not used") + + override fun deleteById(id: UUID) = true + + } + + private class ColdCache : SessionCacheRepository { + + val writes = AtomicInteger() + + override suspend fun find(tokenHash: String): CachedSession? = null + + override suspend fun putIfAbsent(session: Session, cachedUntil: Instant) { + writes.incrementAndGet() + } + + override suspend fun revoke(tokenHash: String, expiresAt: Instant) = Unit + + } + + private object NoRevocations : SessionRevocationRepository { + + override fun add(revocation: SessionRevocation) = Unit + + override fun findPending(now: Instant) = emptyList() + + override fun delete(tokenHash: String) = Unit + + override fun deleteExpired(now: Instant) = Unit + + } + + @BeforeTest + fun connect() { + // The fakes do not touch the database, but the service still opens a transaction around the lookup. + TestDatabase.container.let { Database.connect(it.jdbcUrl, user = it.username, password = it.password) } + } + + private fun service(singleFlight: Boolean, repository: SessionRepository, cache: SessionCacheRepository) = + DefaultAuthenticationService( + userRepository = ExposedUserRepository(), + sessionRepository = repository, + sessionCacheRepository = cache, + sessionRevocationRepository = NoRevocations, + hashingService = DefaultHashingService(), + config = SerialAuthenticationConfig( + realm = "messenger", + sessionTtlSeconds = 3600, + sessionCacheTtlSeconds = 60, + sessionCacheSingleFlight = singleFlight + ) + ) + + private fun stampede(service: AuthenticationService): List = runBlocking(Dispatchers.Default) { + (0 until CONCURRENT_REQUESTS).map { + async { service.findSessionByToken(token) } + }.awaitAll() + } + + @Test + fun `without single-flight every concurrent miss queries the database`() { + val repository = CountingSessionRepository(session) + val cache = ColdCache() + + val results = stampede(service(singleFlight = false, repository, cache)) + + assertTrue(results.all { it == session }) + assertEquals(CONCURRENT_REQUESTS, repository.lookups.get()) + assertEquals(CONCURRENT_REQUESTS, cache.writes.get()) + } + + @Test + fun `with single-flight concurrent misses share one database lookup`() { + val repository = CountingSessionRepository(session) + val cache = ColdCache() + + val results = stampede(service(singleFlight = true, repository, cache)) + + assertTrue(results.all { it == session }) + assertEquals(1, repository.lookups.get()) + assertEquals(1, cache.writes.get()) + } + + @Test + fun `request arriving after logout does not join a lookup started before it`() = runBlocking(Dispatchers.Default) { + val repository = CountingSessionRepository(session) + val service = service(singleFlight = true, repository, ColdCache()) + + // The leader's query is still running (SLOW_QUERY_MS) when the logout completes. + val leader = async { service.findSessionByToken(token) } + delay(SLOW_QUERY_MS / 4) + service.revokeSession(session) + + val afterLogout = async { service.findSessionByToken(token) } + leader.await() + afterLogout.await() + + assertEquals(2, repository.lookups.get(), "the request after logout must run its own lookup") + } + + @Test + fun `lookup that finished before a request arrived is not reused`() = runBlocking { + val repository = CountingSessionRepository(session) + val service = service(singleFlight = true, repository, ColdCache()) + + service.findSessionByToken(token) + service.findSessionByToken(token) + + assertEquals(2, repository.lookups.get()) + } + +} diff --git a/src/test/kotlin/com/mtusi/messenger/main/infrastructure/services/hashing/DefaultHashingServiceTest.kt b/src/test/kotlin/com/mtusi/messenger/main/infrastructure/services/hashing/DefaultHashingServiceTest.kt new file mode 100644 index 00000000..384e053c --- /dev/null +++ b/src/test/kotlin/com/mtusi/messenger/main/infrastructure/services/hashing/DefaultHashingServiceTest.kt @@ -0,0 +1,45 @@ +package com.mtusi.messenger.main.infrastructure.services.hashing + +import com.mtusi.messenger.main.infrastructure.services.hashing.types.HashingAlgorithm +import kotlin.test.Test +import kotlin.test.assertFalse +import kotlin.test.assertNotEquals +import kotlin.test.assertTrue + +class DefaultHashingServiceTest { + + private val service = DefaultHashingService() + + @Test + fun `hash is a salted Argon2id PHC string`() { + val first = service.computeHash(HashingAlgorithm.Argon2id, "correct-horse-battery") + val second = service.computeHash(HashingAlgorithm.Argon2id, "correct-horse-battery") + + assertTrue(first.startsWith("\$argon2id\$v=19\$m=19456,t=2,p=1\$")) + assertFalse("correct-horse-battery" in first) + assertNotEquals(first, second, "same password must produce different hashes (random salt)") + } + + @Test + fun `validates the right password and rejects a wrong one`() { + val hash = service.computeHash(HashingAlgorithm.Argon2id, "correct-horse-battery") + + assertTrue(service.validateHash(HashingAlgorithm.Argon2id, "correct-horse-battery", hash)) + assertFalse(service.validateHash(HashingAlgorithm.Argon2id, "correct-horse-batterY", hash)) + } + + @Test + fun `passwords differing only after 72 bytes are distinguished`() { + // bcrypt silently truncates input to 72 bytes; Argon2id must not. + val prefix = "ж".repeat(40) // 80 bytes in UTF-8 + val hash = service.computeHash(HashingAlgorithm.Argon2id, prefix + "a") + + assertFalse(service.validateHash(HashingAlgorithm.Argon2id, prefix + "b", hash)) + } + + @Test + fun `malformed hash is rejected instead of throwing`() { + assertFalse(service.validateHash(HashingAlgorithm.Argon2id, "whatever", "not-a-hash")) + assertFalse(service.validateHash(HashingAlgorithm.Argon2id, "whatever", "\$argon2id\$v=19\$m=x\$a\$b")) + } +} diff --git a/src/test/kotlin/com/mtusi/messenger/main/plugins/CorsTest.kt b/src/test/kotlin/com/mtusi/messenger/main/plugins/CorsTest.kt new file mode 100644 index 00000000..50fb3b11 --- /dev/null +++ b/src/test/kotlin/com/mtusi/messenger/main/plugins/CorsTest.kt @@ -0,0 +1,26 @@ +package com.mtusi.messenger.main.plugins + +import com.mtusi.messenger.main.support.runApp +import io.ktor.client.request.get +import io.ktor.client.request.header +import io.ktor.http.HttpStatusCode +import kotlin.test.Test +import kotlin.test.assertEquals + +class CorsTest { + + @Test + fun `page of the API itself may call it`() = runApp { client -> + val response = client.get("/health/ready") { header("Origin", "http://localhost") } + + assertEquals(HttpStatusCode.OK, response.status) + } + + @Test + fun `unknown origin is rejected`() = runApp { client -> + val response = client.get("/health/ready") { header("Origin", "http://evil.example") } + + assertEquals(HttpStatusCode.Forbidden, response.status) + } + +} diff --git a/src/test/kotlin/com/mtusi/messenger/main/plugins/DatabaseMigrationTest.kt b/src/test/kotlin/com/mtusi/messenger/main/plugins/DatabaseMigrationTest.kt new file mode 100644 index 00000000..93ccd43f --- /dev/null +++ b/src/test/kotlin/com/mtusi/messenger/main/plugins/DatabaseMigrationTest.kt @@ -0,0 +1,49 @@ +package com.mtusi.messenger.main.plugins + +import com.mtusi.messenger.main.support.TestDatabase +import com.mtusi.messenger.main.support.createUser +import com.mtusi.messenger.main.support.runApp +import com.zaxxer.hikari.HikariDataSource +import org.flywaydb.core.Flyway +import kotlin.test.Test +import kotlin.test.assertEquals + +class DatabaseMigrationTest { + + @Test + fun `re-running migrations applies nothing and keeps data`() = runApp { client -> + client.createUser("keep") + + HikariDataSource().apply { + jdbcUrl = TestDatabase.container.jdbcUrl + username = TestDatabase.USERNAME + password = TestDatabase.PASSWORD + }.use { dataSource -> + + val usersBefore = countUsers(dataSource) + + applyDatabaseMigrations(dataSource) + applyDatabaseMigrations(dataSource) + + val flyway = Flyway.configure() + .dataSource(dataSource) + .locations("classpath:migrations") + .load() + + assertEquals(0, flyway.info().pending().size) + assertEquals(flyway.info().applied().size, flyway.info().all().size) + assertEquals(usersBefore, countUsers(dataSource)) + } + } + + private fun countUsers(dataSource: HikariDataSource): Long { + return dataSource.connection.use { connection -> + connection.createStatement().use { statement -> + statement.executeQuery("SELECT count(*) FROM users").use { + it.next() + it.getLong(1) + } + } + } + } +} diff --git a/src/test/kotlin/com/mtusi/messenger/main/plugins/DependencyFailureTest.kt b/src/test/kotlin/com/mtusi/messenger/main/plugins/DependencyFailureTest.kt new file mode 100644 index 00000000..867d8e08 --- /dev/null +++ b/src/test/kotlin/com/mtusi/messenger/main/plugins/DependencyFailureTest.kt @@ -0,0 +1,233 @@ +package com.mtusi.messenger.main.plugins + +import com.mtusi.messenger.main.extensions.ErrorBody +import com.mtusi.messenger.main.infrastructure.controllers.message.requests.CreateMessageRequest +import com.mtusi.messenger.main.infrastructure.dto.Chat +import com.mtusi.messenger.main.support.TestDatabase +import com.mtusi.messenger.main.support.TestRedis +import com.mtusi.messenger.main.support.TestUser +import com.mtusi.messenger.main.support.auth +import com.mtusi.messenger.main.support.createChat +import com.mtusi.messenger.main.support.idempotencyKey +import com.mtusi.messenger.main.support.register +import com.mtusi.messenger.main.support.runApp +import com.mtusi.messenger.main.utils.computeSha256 +import io.ktor.client.HttpClient +import io.ktor.client.call.body +import io.ktor.client.request.basicAuth +import io.ktor.client.request.delete +import io.ktor.client.request.get +import io.ktor.client.request.header +import io.ktor.client.request.post +import io.ktor.client.request.setBody +import io.ktor.client.statement.HttpResponse +import io.ktor.http.ContentType +import io.ktor.http.HttpStatusCode +import io.ktor.http.contentType +import org.testcontainers.containers.GenericContainer +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertTrue +import kotlin.time.Duration.Companion.seconds +import kotlin.time.TimeSource + +/** + * Short logout deadline: with Redis paused or down the tombstone cannot be written and the logout answers 503. + */ +private const val REVOKE_TIMEOUT_MS = 1000L + +class DependencyFailureTest { + + /** + * An unknown token can only be checked against PostgreSQL, so without it the answer is 503, never a success. + */ + private suspend fun HttpClient.assertAnonymousRejected( + unknownTokenStatus: HttpStatusCode = HttpStatusCode.Unauthorized + ) { + assertEquals(HttpStatusCode.Unauthorized, get("/api/v1/users/me").status) + assertEquals(HttpStatusCode.Unauthorized, get("/api/v1/users/me") { header("X-User-Id", "1") }.status) + assertEquals( + unknownTokenStatus, + get("/api/v1/users/me") { header("Authorization", "Bearer never-issued-token") }.status + ) + } + + private suspend fun assertUnavailable(response: HttpResponse) { + assertEquals(HttpStatusCode.ServiceUnavailable, response.status) + assertEquals("unavailable", response.body().error.code) + } + + @Test + fun `without Redis requests bypass the cache`() = runApp( + redis = TestRedis.unreachableEndpoint(), + sessionRevokeTimeoutMs = REVOKE_TIMEOUT_MS + ) { client -> + assertEquals(HttpStatusCode.OK, client.get("/health/ready").status) + + val alice = client.register("alice") + val bob = client.register("bob") + + assertEquals(HttpStatusCode.OK, client.get("/api/v1/users/me") { auth(alice) }.status) + client.createChat(alice, "no redis", listOf(alice.id, bob.id)) + + client.assertAnonymousRejected() + + // Without the tombstone another replica could still accept the token from the cache, so the logout is not + // confirmed (503). The session is gone from PostgreSQL, so this replica rejects the token anyway. + assertUnavailable(client.delete("/api/v1/auth/sessions/current") { auth(alice) }) + assertEquals(HttpStatusCode.Unauthorized, client.get("/api/v1/users/me") { auth(alice) }.status) + } + + @Test + fun `hanging Redis costs one command timeout and recovers`() { + val redis = TestRedis.create().also { it.start() } + val docker = redis.dockerClient + + try { + runApp(redis = TestRedis.endpoint(redis), sessionRevokeTimeoutMs = REVOKE_TIMEOUT_MS) { client -> + val alice = client.register("alice") + assertEquals(HttpStatusCode.OK, client.get("/api/v1/users/me") { auth(alice) }.status) + + docker.pauseContainerCmd(redis.containerId).exec() + + val started = TimeSource.Monotonic.markNow() + assertEquals(HttpStatusCode.OK, client.get("/api/v1/users/me") { auth(alice) }.status) + assertTrue(started.elapsedNow() < 2.seconds, "cache lookup must be bounded by the command timeout") + + client.assertAnonymousRejected() + + assertUnavailable(client.delete("/api/v1/auth/sessions/current") { auth(alice) }) + assertEquals(HttpStatusCode.Unauthorized, client.get("/api/v1/users/me") { auth(alice) }.status) + + docker.unpauseContainerCmd(redis.containerId).exec() + + // The copy cached before the pause is replaced by the delayed tombstone, not resurrected. + assertEquals(HttpStatusCode.Unauthorized, client.get("/api/v1/users/me") { auth(alice) }.status) + + val bob = client.register("bob") + assertEquals(HttpStatusCode.OK, client.get("/api/v1/users/me") { auth(bob) }.status) + assertTrue( + TestRedis.cli(redis, "EXISTS", "session:${bob.token.toByteArray().computeSha256()}") == "1", + "cache is used again after recovery" + ) + } + } finally { + redis.stop() + } + } + + /** + * The tombstone written by a logout during an outage can be lost while the copy cached before it survives + * (here: put back by hand). The revocation kept in PostgreSQL must still win once Redis is reachable again. + */ + private fun loseTombstone(redis: GenericContainer<*>, user: TestUser, cachedCopy: String) { + TestRedis.cli(redis, "SET", "session:${user.token.toByteArray().computeSha256()}", cachedCopy, "PX", "60000") + } + + private fun cachedCopy(redis: GenericContainer<*>, user: TestUser): String { + return TestRedis.cli(redis, "GET", "session:${user.token.toByteArray().computeSha256()}") + .also { assertTrue(it.contains(user.id.toString()), "session must be cached before the outage") } + } + + @Test + fun `logout during a Redis outage is not undone by a stale cached copy`() { + val redis = TestRedis.create().also { it.start() } + val docker = redis.dockerClient + + try { + runApp(redis = TestRedis.endpoint(redis), sessionRevokeTimeoutMs = REVOKE_TIMEOUT_MS) { client -> + val alice = client.register("alice") + assertEquals(HttpStatusCode.OK, client.get("/api/v1/users/me") { auth(alice) }.status) + val copy = cachedCopy(redis, alice) + + docker.pauseContainerCmd(redis.containerId).exec() + assertUnavailable(client.delete("/api/v1/auth/sessions/current") { auth(alice) }) + docker.unpauseContainerCmd(redis.containerId).exec() + + loseTombstone(redis, alice, copy) + + assertEquals(HttpStatusCode.Unauthorized, client.get("/api/v1/users/me") { auth(alice) }.status) + + // The background check writes the pending tombstone once Redis accepts writes (retry every 1 s). + val deadline = TimeSource.Monotonic.markNow() + 3.seconds + while (TestRedis.cli(redis, "GET", "session:${alice.token.toByteArray().computeSha256()}") != "revoked") { + assertTrue(deadline.hasNotPassedNow(), "pending tombstone was not written after recovery") + Thread.sleep(200) + } + } + } finally { + redis.stop() + } + } + + @Test + fun `logout during a Redis outage survives an API restart`() { + val redis = TestRedis.create().also { it.start() } + val docker = redis.dockerClient + + lateinit var alice: TestUser + lateinit var chat: Chat + lateinit var copy: String + + try { + runApp(redis = TestRedis.endpoint(redis), sessionRevokeTimeoutMs = REVOKE_TIMEOUT_MS) { client -> + alice = client.register("alice") + val bob = client.register("bob") + chat = client.createChat(alice, "restart", listOf(alice.id, bob.id)) + copy = cachedCopy(redis, alice) + + docker.pauseContainerCmd(redis.containerId).exec() + assertUnavailable(client.delete("/api/v1/auth/sessions/current") { auth(alice) }) + } + + docker.unpauseContainerCmd(redis.containerId).exec() + loseTombstone(redis, alice, copy) + + runApp(redis = TestRedis.endpoint(redis), sessionRevokeTimeoutMs = REVOKE_TIMEOUT_MS) { client -> + assertEquals(HttpStatusCode.Unauthorized, client.get("/api/v1/users/me") { auth(alice) }.status) + assertEquals(HttpStatusCode.Unauthorized, client.get("/api/v1/chats/${chat.id}/messages") { auth(alice) }.status) + assertEquals( + HttpStatusCode.Unauthorized, + client.post("/api/v1/chats/${chat.id}/messages") { + auth(alice) + idempotencyKey() + contentType(ContentType.Application.Json) + setBody(CreateMessageRequest(text = "after logout")) + }.status + ) + } + } finally { + redis.stop() + } + } + + @Test + fun `without PostgreSQL requests fail with a bounded 503`() { + val database = TestDatabase.create().also { it.start() } + TestDatabase.awaitConnectable(database) + + try { + runApp(database = database, databaseConnectionTimeoutMs = 2000) { client -> + val alice = client.register("alice") + assertEquals(HttpStatusCode.OK, client.get("/api/v1/users/me") { auth(alice) }.status) + + database.dockerClient.stopContainerCmd(database.containerId).exec() + + val started = TimeSource.Monotonic.markNow() + + // The session itself is still served from Redis, the user lookup needs PostgreSQL. + assertUnavailable(client.get("/api/v1/users/me") { auth(alice) }) + assertUnavailable(client.post("/api/v1/auth/sessions") { basicAuth(alice.user.username, alice.password) }) + assertEquals(HttpStatusCode.ServiceUnavailable, client.get("/health/ready").status) + assertEquals(HttpStatusCode.OK, client.get("/health/live").status) + + assertTrue(started.elapsedNow() < 15.seconds, "every request must fail within the pool timeout") + + client.assertAnonymousRejected(unknownTokenStatus = HttpStatusCode.ServiceUnavailable) + } + } finally { + database.stop() + } + } + +} diff --git a/src/test/kotlin/com/mtusi/messenger/main/plugins/InstanceIdTest.kt b/src/test/kotlin/com/mtusi/messenger/main/plugins/InstanceIdTest.kt new file mode 100644 index 00000000..e3141f41 --- /dev/null +++ b/src/test/kotlin/com/mtusi/messenger/main/plugins/InstanceIdTest.kt @@ -0,0 +1,30 @@ +package com.mtusi.messenger.main.plugins + +import com.mtusi.messenger.main.support.register +import com.mtusi.messenger.main.support.runApp +import io.ktor.client.request.get +import io.ktor.client.request.header +import io.ktor.client.request.post +import kotlin.test.Test +import kotlin.test.assertEquals + +class InstanceIdTest { + + @Test + fun `every response names the replica`() = runApp { client -> + val alice = client.register("alice") + + val responses = listOf( + client.get("/health/live"), + client.get("/health/ready"), + client.get("/api/v1/users/me") { header("Authorization", "Bearer ${alice.token}") }, + client.get("/api/v1/users/me"), + client.get("/api/v1/chats/not-a-uuid") { header("Authorization", "Bearer ${alice.token}") }, + client.post("/api/v1/chats") { header("Authorization", "Bearer ${alice.token}") }, + client.get("/no/such/route") + ) + + assertEquals(List(responses.size) { "test-instance" }, responses.map { it.headers["X-Instance-Id"] }) + } + +} diff --git a/src/test/kotlin/com/mtusi/messenger/main/plugins/SessionCacheTest.kt b/src/test/kotlin/com/mtusi/messenger/main/plugins/SessionCacheTest.kt new file mode 100644 index 00000000..d4523641 --- /dev/null +++ b/src/test/kotlin/com/mtusi/messenger/main/plugins/SessionCacheTest.kt @@ -0,0 +1,65 @@ +package com.mtusi.messenger.main.plugins + +import com.mtusi.messenger.main.support.TestRedis +import com.mtusi.messenger.main.support.TestUser +import com.mtusi.messenger.main.support.auth +import com.mtusi.messenger.main.support.register +import com.mtusi.messenger.main.support.runApp +import com.mtusi.messenger.main.utils.computeSha256 +import io.ktor.client.request.delete +import io.ktor.client.request.get +import io.ktor.http.HttpStatusCode +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertTrue + +class SessionCacheTest { + + private val redis = TestRedis.container + + private fun cached(user: TestUser): String { + return TestRedis.cli(redis, "GET", "session:${user.token.toByteArray().computeSha256()}") + } + + @Test + fun `first authenticated request fills the cache`() = runApp { client -> + val alice = client.register("alice") + + assertEquals("", cached(alice), "login does not write to the cache") + + assertEquals(HttpStatusCode.OK, client.get("/api/v1/users/me") { auth(alice) }.status) + + assertTrue(cached(alice).contains(alice.id.toString())) + + // The session lives for an hour, its cached copy at most sessionCacheTtlSeconds. + val ttl = TestRedis.cli(redis, "TTL", "session:${alice.token.toByteArray().computeSha256()}").toLong() + assertTrue(ttl in 1..60, "cached copy TTL was $ttl") + } + + @Test + fun `logout leaves a tombstone and the token is rejected`() = runApp { client -> + val alice = client.register("alice") + client.get("/api/v1/users/me") { auth(alice) } + + assertEquals(HttpStatusCode.NoContent, client.delete("/api/v1/auth/sessions/current") { auth(alice) }.status) + + assertEquals("revoked", cached(alice)) + assertEquals(HttpStatusCode.Unauthorized, client.get("/api/v1/users/me") { auth(alice) }.status) + } + + @Test + fun `emptied cache falls back to PostgreSQL`() = runApp { client -> + val alice = client.register("alice") + val bob = client.register("bob") + client.get("/api/v1/users/me") { auth(alice) } + client.delete("/api/v1/auth/sessions/current") { auth(bob) } + + TestRedis.cli(redis, "FLUSHALL") + + // PostgreSQL is the source of truth: the live session survives, the revoked one stays revoked. + assertEquals(HttpStatusCode.OK, client.get("/api/v1/users/me") { auth(alice) }.status) + assertEquals(HttpStatusCode.Unauthorized, client.get("/api/v1/users/me") { auth(bob) }.status) + assertTrue(cached(alice).contains(alice.id.toString()), "cache is filled again") + } + +} diff --git a/src/test/kotlin/com/mtusi/messenger/main/support/TestApp.kt b/src/test/kotlin/com/mtusi/messenger/main/support/TestApp.kt new file mode 100644 index 00000000..0ba00322 --- /dev/null +++ b/src/test/kotlin/com/mtusi/messenger/main/support/TestApp.kt @@ -0,0 +1,170 @@ +package com.mtusi.messenger.main.support + +import com.mtusi.messenger.main.infrastructure.controllers.authentication.responses.SessionResponse +import com.mtusi.messenger.main.infrastructure.controllers.chat.requests.CreateChatRequest +import com.mtusi.messenger.main.infrastructure.controllers.user.requests.CreateUserRequest +import com.mtusi.messenger.main.infrastructure.dto.Chat +import com.mtusi.messenger.main.infrastructure.dto.User +import com.mtusi.messenger.main.module +import com.typesafe.config.ConfigFactory +import io.ktor.client.HttpClient +import io.ktor.client.call.body +import io.ktor.client.plugins.contentnegotiation.ContentNegotiation +import io.ktor.client.request.HttpRequestBuilder +import io.ktor.client.request.basicAuth +import io.ktor.client.request.bearerAuth +import io.ktor.client.request.header +import io.ktor.client.request.post +import io.ktor.client.request.setBody +import io.ktor.http.ContentType +import io.ktor.http.HttpStatusCode +import io.ktor.http.contentType +import io.ktor.serialization.kotlinx.json.json +import io.ktor.server.config.HoconApplicationConfig +import io.ktor.server.testing.ApplicationTestBuilder +import io.ktor.server.testing.testApplication +import kotlinx.serialization.ExperimentalSerializationApi +import kotlinx.serialization.json.Json +import kotlinx.serialization.json.JsonNamingStrategy +import org.testcontainers.containers.PostgreSQLContainer +import java.util.UUID + +@OptIn(ExperimentalSerializationApi::class) +val testJson = Json { + ignoreUnknownKeys = true + encodeDefaults = true + namingStrategy = JsonNamingStrategy.SnakeCase +} + +const val TEST_PASSWORD = "correct-horse-battery" + +data class TestUser( + val user: User, + val password: String, + val token: String, +) { + val id: UUID + get() = user.id +} + +data class RedisEndpoint( + val host: String, + val port: Int, +) + +private fun testConfig( + sessionTtlSeconds: Long, + database: PostgreSQLContainer<*>, + databaseConnectionTimeoutMs: Long, + redis: RedisEndpoint, + idempotencyTtlSeconds: Long, + sessionRevokeTimeoutMs: Long, +) = HoconApplicationConfig( + ConfigFactory.parseMap( + mapOf( + "storage.endpoint" to TestStorage.endpoint, + "storage.bucket" to TestStorage.BUCKET, + "storage.accessKey" to TestStorage.ACCESS_KEY, + "storage.secretKey" to TestStorage.SECRET_KEY, + "database.host" to database.host, + "database.port" to database.firstMappedPort, + "database.name" to database.databaseName, + "database.username" to database.username, + "database.password" to database.password, + "database.maxPoolSize" to 10, + "database.connectionTimeoutMs" to databaseConnectionTimeoutMs, + "database.statementTimeoutMs" to 5000, + "redis.host" to redis.host, + "redis.port" to redis.port, + "redis.password" to TestRedis.PASSWORD, + "redis.connectTimeoutMs" to 500, + "redis.commandTimeoutMs" to 500, + "authentication.realm" to "messenger", + "authentication.sessionTtlSeconds" to sessionTtlSeconds, + "authentication.sessionCacheTtlSeconds" to 60, + "authentication.sessionRevokeTimeoutMs" to sessionRevokeTimeoutMs, + "messages.idempotencyTtlSeconds" to idempotencyTtlSeconds, + "instance.id" to "test-instance", + "cors.allowSameOrigin" to true, + ) + ) +) + +/** + * Docker Desktop can be slow to accept the first connection, hence the generous default database timeout. + */ +fun runApp( + sessionTtlSeconds: Long = 3600, + database: PostgreSQLContainer<*> = TestDatabase.container, + databaseConnectionTimeoutMs: Long = 10000, + redis: RedisEndpoint = TestRedis.endpoint(), + idempotencyTtlSeconds: Long = 86400, + sessionRevokeTimeoutMs: Long = 8000, + block: suspend ApplicationTestBuilder.(HttpClient) -> Unit +) = testApplication { + environment { + config = testConfig(sessionTtlSeconds, database, databaseConnectionTimeoutMs, redis, idempotencyTtlSeconds, sessionRevokeTimeoutMs) + } + application { + module() + } + val client = createClient { + install(ContentNegotiation) { + json(testJson) + } + } + block(client) +} + +/** + * Usernames are unique per call: all tests share one database. + */ +fun uniqueUsername(prefix: String): String = "${prefix}_${UUID.randomUUID().toString().replace("-", "").take(12)}" + +suspend fun HttpClient.createUser(prefix: String, password: String = TEST_PASSWORD): User { + val username = uniqueUsername(prefix) + val response = post("/api/v1/users") { + contentType(ContentType.Application.Json) + setBody(CreateUserRequest(username = username, displayName = prefix, password = password)) + } + check(response.status == HttpStatusCode.Created) { "createUser failed: ${response.status}" } + return response.body() +} + +suspend fun HttpClient.login(username: String, password: String): SessionResponse { + val response = post("/api/v1/auth/sessions") { + basicAuth(username, password) + } + check(response.status == HttpStatusCode.Created) { "login failed: ${response.status}" } + return response.body() +} + +/** + * Registers a user and opens a session for it. + */ +suspend fun HttpClient.register(prefix: String): TestUser { + val user = createUser(prefix) + val session = login(user.username, TEST_PASSWORD) + return TestUser(user, TEST_PASSWORD, session.token) +} + +fun HttpRequestBuilder.auth(user: TestUser) = bearerAuth(user.token) + +/** + * Every message POST needs its own key since lab3; pass the same key to model a retry. + */ +fun HttpRequestBuilder.idempotencyKey(key: String = UUID.randomUUID().toString()) = header("Idempotency-Key", key) + +suspend fun HttpClient.createChat( + creator: TestUser, + title: String, + memberIds: List, +): Chat { + val response = post("/api/v1/chats") { + auth(creator) + contentType(ContentType.Application.Json) + setBody(CreateChatRequest(title = title, memberIds = memberIds.map(UUID::toString))) + } + check(response.status == HttpStatusCode.Created) { "createChat failed: ${response.status}" } + return response.body() +} diff --git a/src/test/kotlin/com/mtusi/messenger/main/support/TestDatabase.kt b/src/test/kotlin/com/mtusi/messenger/main/support/TestDatabase.kt new file mode 100644 index 00000000..9453a1ef --- /dev/null +++ b/src/test/kotlin/com/mtusi/messenger/main/support/TestDatabase.kt @@ -0,0 +1,48 @@ +package com.mtusi.messenger.main.support + +import org.testcontainers.containers.PostgreSQLContainer +import java.sql.DriverManager +import java.sql.SQLException +import org.testcontainers.utility.DockerImageName + +/** + * One PostgreSQL container per test JVM, same image as compose.yaml. + * Tests share it and stay independent by using unique usernames. + */ +object TestDatabase { + + const val NAME = "messenger" + const val USERNAME = "messenger" + const val PASSWORD = "messenger" + + val container: PostgreSQLContainer<*> by lazy { + create().also { it.start() } + } + + /** + * A separate, not yet started container for tests that stop the database. + */ + /** + * Docker Desktop may refuse the first connections through a fresh port mapping for a moment after the container + * reported ready; tests with a short pool timeout wait for a real connection first. + */ + fun awaitConnectable(container: PostgreSQLContainer<*>, timeoutMs: Long = 15000) { + val deadline = System.currentTimeMillis() + timeoutMs + while (true) { + try { + DriverManager.getConnection(container.jdbcUrl, container.username, container.password).use { return } + } catch (e: SQLException) { + if (System.currentTimeMillis() > deadline) throw e + Thread.sleep(200) + } + } + } + + fun create(): PostgreSQLContainer<*> { + return PostgreSQLContainer(DockerImageName.parse("postgres:17.6-alpine3.22").asCompatibleSubstituteFor("postgres")) + .withDatabaseName(NAME) + .withUsername(USERNAME) + .withPassword(PASSWORD) + } + +} diff --git a/src/test/kotlin/com/mtusi/messenger/main/support/TestRedis.kt b/src/test/kotlin/com/mtusi/messenger/main/support/TestRedis.kt new file mode 100644 index 00000000..d5a5b6ef --- /dev/null +++ b/src/test/kotlin/com/mtusi/messenger/main/support/TestRedis.kt @@ -0,0 +1,54 @@ +package com.mtusi.messenger.main.support + +import org.testcontainers.containers.GenericContainer +import org.testcontainers.utility.DockerImageName +import java.net.ServerSocket + +/** + * One Redis container per test JVM, same image and memory settings as compose.yaml. + */ +object TestRedis { + + const val PASSWORD = "messenger" + + private const val PORT = 6379 + + val container: GenericContainer<*> by lazy { + create().also { it.start() } + } + + /** + * A separate, not yet started container for tests that pause or flush Redis. + */ + fun create(): GenericContainer<*> { + return GenericContainer(DockerImageName.parse("redis:8.4.0-alpine")) + .withExposedPorts(PORT) + .withCommand( + "redis-server", + "--requirepass", PASSWORD, + "--save", "", + "--appendonly", "no", + "--maxmemory-policy", "volatile-lru" + ) + } + + fun endpoint(container: GenericContainer<*> = this.container) = RedisEndpoint( + host = container.host, + port = container.getMappedPort(PORT) + ) + + /** + * An endpoint nobody listens on: connections are refused immediately. + */ + fun unreachableEndpoint(): RedisEndpoint { + val port = ServerSocket(0).use { it.localPort } + return RedisEndpoint(host = "127.0.0.1", port = port) + } + + fun cli(container: GenericContainer<*>, vararg command: String): String { + val result = container.execInContainer("redis-cli", "--no-auth-warning", "-a", PASSWORD, *command) + check(result.exitCode == 0) { "redis-cli failed: ${result.stderr}" } + return result.stdout.trim() + } + +} diff --git a/src/test/kotlin/com/mtusi/messenger/main/support/TestStorage.kt b/src/test/kotlin/com/mtusi/messenger/main/support/TestStorage.kt new file mode 100644 index 00000000..710fdebe --- /dev/null +++ b/src/test/kotlin/com/mtusi/messenger/main/support/TestStorage.kt @@ -0,0 +1,45 @@ +package com.mtusi.messenger.main.support + +import aws.sdk.kotlin.runtime.auth.credentials.StaticCredentialsProvider +import aws.sdk.kotlin.services.s3.S3Client +import aws.sdk.kotlin.services.s3.createBucket +import aws.smithy.kotlin.runtime.net.url.Url +import kotlinx.coroutines.runBlocking +import org.testcontainers.containers.GenericContainer +import org.testcontainers.containers.wait.strategy.Wait + +object TestStorage { + const val ACCESS_KEY = "test-access-key" + const val SECRET_KEY = "test-secret-key-for-local-tests" + const val BUCKET = "test-attachments" + val container by lazy { + GenericContainer("quay.io/minio/minio@sha256:14cea493d9a34af32f524e538b8346cf79f3321eff8e708c1e2960462bd8936e") + .withEnv("MINIO_ROOT_USER", ACCESS_KEY) + .withEnv("MINIO_ROOT_PASSWORD", SECRET_KEY) + .withCommand("server", "/data") + .withExposedPorts(9000) + .waitingFor(Wait.forHttp("/minio/health/live").forPort(9000)) + .also { + it.start() + } + } + val endpoint: String by lazy { + val url = "http://${container.host}:${container.getMappedPort(9000)}" + runBlocking { + S3Client { + endpointUrl = Url.parse(url) + region = "us-east-1" + forcePathStyle = true + credentialsProvider = StaticCredentialsProvider { + accessKeyId = ACCESS_KEY + secretAccessKey = SECRET_KEY + } + }.use { client -> + client.createBucket { + bucket = BUCKET + } + } + } + url + } +} diff --git a/src/test/kotlin/com/mtusi/messenger/main/utils/TemporaryUploadTest.kt b/src/test/kotlin/com/mtusi/messenger/main/utils/TemporaryUploadTest.kt new file mode 100644 index 00000000..2f942b40 --- /dev/null +++ b/src/test/kotlin/com/mtusi/messenger/main/utils/TemporaryUploadTest.kt @@ -0,0 +1,43 @@ +package com.mtusi.messenger.main.utils + +import kotlinx.coroutines.CompletableDeferred +import kotlinx.coroutines.awaitCancellation +import kotlinx.coroutines.cancelAndJoin +import kotlinx.coroutines.launch +import kotlinx.coroutines.runBlocking +import java.io.File +import kotlin.test.Test +import kotlin.test.assertFalse +import kotlin.test.assertFailsWith + +class TemporaryUploadTest { + + @Test + fun `temporary file is removed when upload is cancelled`() = runBlocking { + val created = CompletableDeferred() + val job = launch { + withTemporaryUpload { file -> + created.complete(file) + awaitCancellation() + } + } + val file = created.await() + job.cancelAndJoin() + assertFalse(file.exists()) + } + + @Test + fun `temporary file is removed on success and failure`() = runBlocking { + val completed = withTemporaryUpload { it } + assertFalse(completed.exists()) + + val failure = assertFailsWith { + withTemporaryUpload { + throw UploadFailure(it) + } + } + assertFalse(failure.file.exists()) + } + + private class UploadFailure(val file: File) : IllegalStateException("injected failure") +}