forked from mtusi2026/mtusi2026lab4
Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
90a689661b | ||
|
|
e06678b6a9 |
+11
-2
@@ -14,7 +14,16 @@ IDEMPOTENCY_TTL_SECONDS=86400
|
|||||||
S3_ENDPOINT=http://s3:9000
|
S3_ENDPOINT=http://s3:9000
|
||||||
S3_REGION=us-east-1
|
S3_REGION=us-east-1
|
||||||
S3_BUCKET=messenger-attachments
|
S3_BUCKET=messenger-attachments
|
||||||
S3_ACCESS_KEY_ID_FILE=/run/secrets/s3_access_key_id
|
S3_ACCESS_KEY_ID=messenger-api
|
||||||
S3_SECRET_ACCESS_KEY_FILE=/run/secrets/s3_secret_access_key
|
S3_SECRET_ACCESS_KEY=replace-with-a-long-random-api-secret
|
||||||
|
MINIO_ROOT_USER=minio-root
|
||||||
|
MINIO_ROOT_PASSWORD=replace-with-a-different-long-random-root-secret
|
||||||
MAX_UPLOAD_BYTES=10485760
|
MAX_UPLOAD_BYTES=10485760
|
||||||
MAX_IMAGE_PIXELS=40000000
|
MAX_IMAGE_PIXELS=40000000
|
||||||
|
MAX_CONCURRENT_UPLOADS=3
|
||||||
|
UPLOAD_PERMIT_WAIT_MILLIS=10000
|
||||||
|
# Orphans younger than this are never deleted: protects the S3 PUT -> DB COMMIT window.
|
||||||
|
ATTACHMENT_CLEANUP_GRACE_SECONDS=3600
|
||||||
|
ATTACHMENT_CLEANUP_DRY_RUN=false
|
||||||
|
ATTACHMENT_RECONCILE_INTERVAL_MILLIS=600000
|
||||||
|
ATTACHMENT_RECONCILE_INITIAL_DELAY_MILLIS=60000
|
||||||
|
|||||||
+37
@@ -0,0 +1,37 @@
|
|||||||
|
# Stage 1: Build
|
||||||
|
FROM eclipse-temurin:23-jdk AS build
|
||||||
|
|
||||||
|
WORKDIR /app
|
||||||
|
|
||||||
|
COPY gradlew .
|
||||||
|
COPY gradle gradle
|
||||||
|
COPY build.gradle settings.gradle ./
|
||||||
|
|
||||||
|
COPY src src
|
||||||
|
|
||||||
|
RUN --mount=type=cache,target=/root/.gradle \
|
||||||
|
chmod +x gradlew && ./gradlew bootJar --no-daemon
|
||||||
|
|
||||||
|
|
||||||
|
# Stage 2: Runtime
|
||||||
|
FROM eclipse-temurin:23-jre
|
||||||
|
|
||||||
|
WORKDIR /app
|
||||||
|
|
||||||
|
COPY --from=build /app/build/libs/*.jar app.jar
|
||||||
|
|
||||||
|
RUN apt-get update \
|
||||||
|
&& apt-get install --yes --no-install-recommends curl \
|
||||||
|
&& rm -rf /var/lib/apt/lists/* \
|
||||||
|
&& useradd --system --create-home messenger
|
||||||
|
|
||||||
|
USER messenger
|
||||||
|
|
||||||
|
EXPOSE 8080
|
||||||
|
|
||||||
|
STOPSIGNAL SIGTERM
|
||||||
|
|
||||||
|
HEALTHCHECK --interval=30s --timeout=5s --start-period=30s --retries=3 \
|
||||||
|
CMD curl --fail --silent --show-error http://localhost:8080/health/ready || exit 1
|
||||||
|
|
||||||
|
ENTRYPOINT ["java", "-jar", "app.jar"]
|
||||||
@@ -1,37 +1,70 @@
|
|||||||
# Отчёт — лабораторная 4
|
# Отчёт — лабораторная 4
|
||||||
|
|
||||||
- Автор, группа: TODO
|
- Автор, группа: Егор Антипин, БПИ2301
|
||||||
- Целевая оценка; выбранный трек 5, если нужен: TODO
|
- Целевая оценка; выбранный трек 5, если нужен: 4
|
||||||
- Commit/PR: TODO после публикации
|
- Commit/PR: TODO после публикации
|
||||||
- Использованные источники, библиотеки и помощь ИИ: TODO
|
- Использованные источники, библиотеки и помощь ИИ: контракт репозитория; MinIO Java SDK 8.6.0; документация MinIO/S3; помощь Codex при реализации и проверке.
|
||||||
|
|
||||||
## Запуск из чистого clone
|
## Запуск из чистого clone
|
||||||
|
|
||||||
TODO: версии runtime/образов, prerequisites, команды создания локальных secrets/.env, build, миграций, запуска, ожидания готовности, тестов и остановки. Указать необходимые действия оператора. Не вставлять секреты. Отдельно указать безопасную остановку и команду намеренного удаления учебных данных.
|
Требуются Docker Engine с Compose v2 и свободный локальный порт 8080. Java внутри образа — Eclipse Temurin 23. PostgreSQL `16.4-bookworm`, Redis `7.4-bookworm`, nginx `1.27.4-bookworm`. Точные MinIO tags и digests описаны в `infra/README.md`.
|
||||||
|
|
||||||
|
```sh
|
||||||
|
cp .env.example .env
|
||||||
|
# заменить DATABASE_PASSWORD, MINIO_ROOT_PASSWORD и S3_SECRET_ACCESS_KEY;
|
||||||
|
# root и API secrets должны быть разными
|
||||||
|
docker compose --env-file .env up -d --build --scale api=2
|
||||||
|
docker compose --env-file .env ps
|
||||||
|
python3 tests/smoke.py --base-url http://localhost:8080
|
||||||
|
python3 student_tests/lab4_negative.py --base-url http://localhost:8080
|
||||||
|
ENV_FILE=.env ./scripts/check-private-bucket.sh
|
||||||
|
python3 student_tests/concurrent_upload_memory.py --base-url http://localhost:8080 \
|
||||||
|
--compose-env-file .env --concurrency 12 --output evidence/upload-memory.json
|
||||||
|
```
|
||||||
|
|
||||||
|
`migrate` применяет Flyway V1–V4 один раз. `minio-init` идемпотентно создаёт приватный bucket и ограниченного API-user. Обычная безопасная остановка: `docker compose --env-file .env down` — named volumes сохраняются. Намеренное удаление только учебных данных: `docker compose --env-file .env down --volumes`.
|
||||||
|
|
||||||
## Архитектура и решения
|
## Архитектура и решения
|
||||||
|
|
||||||
TODO: схема процессов/хранилищ, источник истины, транзакционные границы, ограничения. Что перенесено из lab3, что изменено в этой работе. Соответствие переменных из .env.example реальной конфигурации.
|
Путь запроса: клиент → nginx → две API-реплики → PostgreSQL + Redis + MinIO. PostgreSQL — источник истины для users/chats/messages/attachments; MinIO хранит только оригинальные bytes. Redis остаётся необязательным cache.
|
||||||
|
|
||||||
|
`attachments` содержит chat/owner, display filename, истинный MIME, size, SHA-256 и непрозрачный object key вида `attachments/<server-uuid>/original`. `message_attachments` использует составные FK `(message_id, chat_id)` и `(attachment_id, chat_id)`, поэтому база не позволяет связать сообщение с вложением другого чата.
|
||||||
|
|
||||||
|
Upload читает raw body порциями по 8 KiB с жёсткой границей 10 MiB, проверяет filename, реальный PNG/JPEG через ImageIO, совпадение MIME и ≤40 млн пикселей. До чтения body захватывается один из трёх fair semaphore permits на реплику; ожидание ограничено 10 секундами и возвращает 429 с `Retry-After`. Nginx ограничивает body 11 MiB, ожидание тела 10 секундами, запрос к API 20 секундами; MinIO client имеет connect 2 с и общий call timeout 15 с. Поэтому число одновременно удерживаемых byte/decode buffers ограничено.
|
||||||
|
|
||||||
|
После успешного S3 PUT метаданные фиксируются отдельной DB-транзакцией. При ошибке DB выполняется компенсирующий DELETE объекта. Обрыв входного потока происходит до PUT и не создаёт object. Ответ 201 возвращается только после обеих записей. Ошибка MinIO преобразуется в JSON 503 без внутренних деталей.
|
||||||
|
|
||||||
|
Для аварии процесса между S3 PUT и DB COMMIT работает `AttachmentReconciler`: каждые 10 минут он сравнивает `attachments.object_key` с prefix `attachments/` в MinIO. Orphan удаляется только если `lastModified` старше grace period 3600 секунд; свежий объект не трогается, что защищает нормальный commit window. Missing object не удаляет метаданные автоматически, а пишет ERROR для ручного расследования. Есть `ATTACHMENT_CLEANUP_DRY_RUN=true`. Unit-тесты доказывают old/fresh orphan, dry-run и missing-object поведение.
|
||||||
|
|
||||||
|
Download сначала загружает метаданные и проверяет членство пользователя в чате, затем читает object через API. Redirect/presigned URL не используется. Filename участвует только в `Content-Disposition`.
|
||||||
|
|
||||||
|
Compose и Swarm используют отдельный persistent volume `minio-data`; S3/console не публикуются наружу. Все параметры из `.env.example` сопоставлены с `application.yml` и stack-файлами.
|
||||||
|
|
||||||
## Доказательства критериев
|
## Доказательства критериев
|
||||||
|
|
||||||
| Критерий README | Команда/сценарий | Наблюдаемый результат | Файл доказательства |
|
| Критерий README | Команда/сценарий | Наблюдаемый результат | Файл доказательства |
|
||||||
| --- | --- | --- | --- |
|
| --- | --- | --- | --- |
|
||||||
| Публичный smoke | make test | TODO | TODO |
|
| Публичный smoke | `python -X utf8 tests/smoke.py --base-url http://localhost:18084` | PASS, включая upload/message/download/ACL | `evidence/lab4-2026-11-11.md` |
|
||||||
| Остальные пункты уровня 3 | TODO | TODO | TODO |
|
| Private bucket/bootstrap | повторный `minio-init`; `scripts/check-private-bucket.cmd` | bootstrap повторяем; anonymous list запрещён; API credentials non-admin | `evidence/lab4-2026-11-11.md` |
|
||||||
| Все пункты уровня 4, если заявлен | TODO | TODO | TODO |
|
| Негативные файлы и чужой чат | `python -X utf8 student_tests/lab4_negative.py --base-url http://localhost:18084` | PASS | `evidence/lab4-2026-11-11.md` |
|
||||||
| Выбранный трек 5, если заявлен | TODO | TODO | TODO |
|
| Обрыв/DB failure/reconciler | `AttachmentServiceTest`, `AttachmentReconcilerTest` | PASS: нет object до PUT; compensating DELETE; grace/dry-run/missing | `evidence/lab4-2026-11-11.md` |
|
||||||
|
| 12 конкурентных upload + RAM | `student_tests/concurrent_upload_memory.py ... --concurrency 12` | 12/12; peak 839909375 B aggregate; +171442175 B | `evidence/upload-memory-2026-11-11.json` |
|
||||||
|
| Полный restart | `tests/resilience.py --mode persistence ...` | PASS; session/message/original preserved | `evidence/lab4-2026-11-11.md` |
|
||||||
|
| Отказ API-реплики | `tests/resilience.py --mode failover ...` | PASS; download через другую replica | `evidence/lab4-2026-11-11.md` |
|
||||||
|
| Недоступность S3 | stop MinIO + `lab4_negative.py --expect-s3-unavailable` | JSON 503 unavailable; после start smoke PASS | `evidence/lab4-2026-11-11.md` |
|
||||||
|
|
||||||
TODO: добавить отдельную строку для каждого заявленного критерия. PASS без команды и наблюдения не является доказательством. Проверка инфраструктурного hook требует подтверждения, какие реальные процессы/хранилища перезапущены.
|
Unit-набор `AttachmentServiceTest` отдельно проверяет server-generated key, MIME spoofing, ранний byte limit, ACL до download, чужой chat attachment, обрыв request body, отказ ObjectStorage и compensating DELETE после DB failure. `AttachmentReconcilerTest` проверяет grace period, dry-run и missing objects. Полный Gradle suite внутри Compose network: `BUILD SUCCESSFUL`.
|
||||||
|
|
||||||
## Эксперименты и отказы
|
## Эксперименты и отказы
|
||||||
|
|
||||||
TODO: гипотеза, hardware/лимиты, версии, объём данных, профиль нагрузки, concurrency/arrival rate, длительность/прогрев, повторы, успешные RPS, ошибки, p50/p95/p99, CPU/RAM. Сохранить raw summary без tokens и объяснить ограничения измерения.
|
Проверялась функциональная устойчивость и ограниченность памяти, не максимальная производительность. Стенд: Docker Desktop, 2 API replicas по 768 MiB, MinIO 512 MiB, PostgreSQL 768 MiB. Конкурентный эксперимент сгенерировал один валидный несжимаемый PNG 1800×1200, 6835833 bytes, и одновременно отправил его 12 раз. Все 12 upload завершились 201 за 10.156 с. По пяти `docker stats` samples aggregate API baseline был 668467200 B, peak 839909375 B, рост 171442175 B. Результат повторяемый, но sampling может пропустить краткий пик и не является профилированием JVM heap.
|
||||||
|
|
||||||
| Воздействие | Что ожидаем | Что наблюдали | Время восстановления / потеря данных |
|
| Воздействие | Что ожидаем | Что наблюдали | Время восстановления / потеря данных |
|
||||||
| --- | --- | --- | --- |
|
| --- | --- | --- | --- |
|
||||||
| TODO | TODO | TODO | TODO |
|
| Stop обслужившей API replica | Запросы продолжает вторая replica; общий MinIO сохраняет original | 2 instance до отказа, 102 успешные записи, 0 ошибок | 1.386 с до подтверждённого восстановления |
|
||||||
|
| Restart nginx/API/Redis/PostgreSQL/MinIO без удаления volumes | Сессия, message, attachment metadata и bytes сохранятся | PASS persistence | <90 с, потерь нет |
|
||||||
|
| Stop MinIO | Upload/download не превращаются в 500 | JSON 503 `unavailable`; после start публичный smoke снова PASS | Восстановление после healthcheck MinIO |
|
||||||
|
|
||||||
## Ограничения и следующие шаги
|
## Ограничения и следующие шаги
|
||||||
|
|
||||||
TODO: какие отказы выдерживаются и какие нет; что не реализовано; где учебное упрощение; какое улучшение подтверждено, а какое пока гипотеза.
|
Стенд выдерживает отказ одной API-реплики и перезапуск всех сервисов при сохранённых named volumes. MinIO развёрнут одной репликой: во время его отказа attachment upload/download недоступны с 503, но PostgreSQL API остаётся работоспособным. HA самого S3, multipart upload, presigned flow и thumbnail pipeline не реализованы. Reconciler автоматически удаляет только старые orphan objects; missing objects намеренно не чинятся автоматически, чтобы не скрывать потерю данных.
|
||||||
|
|||||||
@@ -0,0 +1,55 @@
|
|||||||
|
plugins {
|
||||||
|
id 'java'
|
||||||
|
id 'org.springframework.boot' version '4.1.1'
|
||||||
|
id 'io.spring.dependency-management' version '1.1.7'
|
||||||
|
}
|
||||||
|
|
||||||
|
group = 'com.example'
|
||||||
|
version = '0.0.1-SNAPSHOT'
|
||||||
|
|
||||||
|
java {
|
||||||
|
toolchain {
|
||||||
|
languageVersion = JavaLanguageVersion.of(23)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
repositories {
|
||||||
|
mavenCentral()
|
||||||
|
}
|
||||||
|
|
||||||
|
dependencies {
|
||||||
|
// implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
|
||||||
|
// implementation 'org.springframework.boot:spring-boot-starter-security'
|
||||||
|
implementation 'org.springframework.boot:spring-boot-starter-webmvc'
|
||||||
|
implementation 'org.springframework.boot:spring-boot-starter-validation'
|
||||||
|
implementation 'org.springframework.boot:spring-boot-starter-jdbc'
|
||||||
|
implementation 'org.springframework.boot:spring-boot-starter-data-redis'
|
||||||
|
implementation 'org.springframework.boot:spring-boot-flyway'
|
||||||
|
implementation 'org.flywaydb:flyway-core'
|
||||||
|
implementation 'org.flywaydb:flyway-database-postgresql'
|
||||||
|
implementation 'org.springframework.boot:spring-boot-starter-security'
|
||||||
|
implementation 'org.bouncycastle:bcprov-jdk18on:1.84'
|
||||||
|
implementation 'org.apache.commons:commons-pool2'
|
||||||
|
implementation 'io.minio:minio:8.6.0'
|
||||||
|
runtimeOnly 'org.postgresql:postgresql'
|
||||||
|
|
||||||
|
implementation 'com.fasterxml.jackson.datatype:jackson-datatype-jsr310'
|
||||||
|
implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.8.3'
|
||||||
|
compileOnly 'org.projectlombok:lombok'
|
||||||
|
developmentOnly 'org.springframework.boot:spring-boot-devtools'
|
||||||
|
annotationProcessor 'org.projectlombok:lombok'
|
||||||
|
// testImplementation 'org.springframework.boot:spring-boot-starter-data-jpa-test'
|
||||||
|
// testImplementation 'org.springframework.boot:spring-boot-starter-security-test'
|
||||||
|
testImplementation 'org.springframework.boot:spring-boot-starter-test'
|
||||||
|
testImplementation 'org.springframework.boot:spring-boot-starter-webmvc-test'
|
||||||
|
testCompileOnly 'org.projectlombok:lombok'
|
||||||
|
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
|
||||||
|
testAnnotationProcessor 'org.projectlombok:lombok'
|
||||||
|
|
||||||
|
implementation("com.github.f4b6a3:uuid-creator:6.1.1")
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
tasks.named('test') {
|
||||||
|
useJUnitPlatform()
|
||||||
|
}
|
||||||
+219
@@ -0,0 +1,219 @@
|
|||||||
|
name: hla-lab4
|
||||||
|
|
||||||
|
x-api-environment: &api-environment
|
||||||
|
APP_HOST: 0.0.0.0
|
||||||
|
APP_PORT: 8080
|
||||||
|
DATABASE_URL: jdbc:postgresql://postgres:5432/${DATABASE_NAME:-messenger}
|
||||||
|
DATABASE_USER: ${DATABASE_USER:-messenger}
|
||||||
|
DATABASE_PASSWORD: ${DATABASE_PASSWORD:?Set DATABASE_PASSWORD in .env}
|
||||||
|
DATABASE_POOL_SIZE: ${DATABASE_POOL_SIZE:-10}
|
||||||
|
DATABASE_MIN_IDLE: ${DATABASE_MIN_IDLE:-2}
|
||||||
|
DATABASE_QUERY_TIMEOUT_SECONDS: ${DATABASE_QUERY_TIMEOUT_SECONDS:-5}
|
||||||
|
DATABASE_TRANSACTION_TIMEOUT_SECONDS: ${DATABASE_TRANSACTION_TIMEOUT_SECONDS:-5}
|
||||||
|
REDIS_HOST: redis
|
||||||
|
REDIS_PORT: 6379
|
||||||
|
REDIS_CONNECT_TIMEOUT: ${REDIS_CONNECT_TIMEOUT:-1s}
|
||||||
|
REDIS_COMMAND_TIMEOUT: ${REDIS_COMMAND_TIMEOUT:-500ms}
|
||||||
|
REDIS_POOL_MAX_ACTIVE: ${REDIS_POOL_MAX_ACTIVE:-8}
|
||||||
|
REDIS_POOL_MAX_IDLE: ${REDIS_POOL_MAX_IDLE:-8}
|
||||||
|
REDIS_POOL_MIN_IDLE: ${REDIS_POOL_MIN_IDLE:-0}
|
||||||
|
REDIS_POOL_MAX_WAIT: ${REDIS_POOL_MAX_WAIT:-500ms}
|
||||||
|
USER_CACHE_TTL_SECONDS: ${USER_CACHE_TTL_SECONDS:-300}
|
||||||
|
SESSION_TTL_SECONDS: ${SESSION_TTL_SECONDS:-3600}
|
||||||
|
IDEMPOTENCY_TTL_SECONDS: ${IDEMPOTENCY_TTL_SECONDS:-86400}
|
||||||
|
S3_ENDPOINT: http://minio:9000
|
||||||
|
S3_REGION: ${S3_REGION:-us-east-1}
|
||||||
|
S3_BUCKET: ${S3_BUCKET:-messenger-attachments}
|
||||||
|
S3_ACCESS_KEY_ID: ${S3_ACCESS_KEY_ID:?Set S3_ACCESS_KEY_ID in .env}
|
||||||
|
S3_SECRET_ACCESS_KEY: ${S3_SECRET_ACCESS_KEY:?Set S3_SECRET_ACCESS_KEY in .env}
|
||||||
|
MAX_UPLOAD_BYTES: ${MAX_UPLOAD_BYTES:-10485760}
|
||||||
|
MAX_IMAGE_PIXELS: ${MAX_IMAGE_PIXELS:-40000000}
|
||||||
|
MAX_CONCURRENT_UPLOADS: ${MAX_CONCURRENT_UPLOADS:-3}
|
||||||
|
UPLOAD_PERMIT_WAIT_MILLIS: ${UPLOAD_PERMIT_WAIT_MILLIS:-10000}
|
||||||
|
ATTACHMENT_CLEANUP_GRACE_SECONDS: ${ATTACHMENT_CLEANUP_GRACE_SECONDS:-3600}
|
||||||
|
ATTACHMENT_CLEANUP_DRY_RUN: ${ATTACHMENT_CLEANUP_DRY_RUN:-false}
|
||||||
|
ATTACHMENT_RECONCILE_INTERVAL_MILLIS: ${ATTACHMENT_RECONCILE_INTERVAL_MILLIS:-600000}
|
||||||
|
ATTACHMENT_RECONCILE_INITIAL_DELAY_MILLIS: ${ATTACHMENT_RECONCILE_INITIAL_DELAY_MILLIS:-60000}
|
||||||
|
|
||||||
|
services:
|
||||||
|
postgres:
|
||||||
|
image: postgres:16.4-bookworm
|
||||||
|
environment:
|
||||||
|
POSTGRES_DB: ${DATABASE_NAME:-messenger}
|
||||||
|
POSTGRES_USER: ${DATABASE_USER:-messenger}
|
||||||
|
POSTGRES_PASSWORD: ${DATABASE_PASSWORD:?Set DATABASE_PASSWORD in .env}
|
||||||
|
volumes:
|
||||||
|
- postgres-data:/var/lib/postgresql/data
|
||||||
|
healthcheck:
|
||||||
|
test: ["CMD-SHELL", "pg_isready -U $${POSTGRES_USER} -d $${POSTGRES_DB}"]
|
||||||
|
interval: 2s
|
||||||
|
timeout: 2s
|
||||||
|
retries: 30
|
||||||
|
restart: unless-stopped
|
||||||
|
cpus: 1.0
|
||||||
|
mem_limit: 768m
|
||||||
|
networks: [backend]
|
||||||
|
|
||||||
|
redis:
|
||||||
|
image: redis:7.4-bookworm
|
||||||
|
command: ["redis-server", "--appendonly", "yes", "--appendfsync", "everysec"]
|
||||||
|
volumes:
|
||||||
|
- redis-data:/data
|
||||||
|
healthcheck:
|
||||||
|
test: ["CMD", "redis-cli", "ping"]
|
||||||
|
interval: 2s
|
||||||
|
timeout: 2s
|
||||||
|
retries: 30
|
||||||
|
restart: unless-stopped
|
||||||
|
cpus: 0.25
|
||||||
|
mem_limit: 256m
|
||||||
|
networks: [backend]
|
||||||
|
|
||||||
|
minio:
|
||||||
|
image: bitnamilegacy/minio:2025.4.22-debian-12-r1@sha256:d7cd0e172c4cc0870f4bdc3142018e2a37be9acf04d68f386600daad427e0cab
|
||||||
|
environment:
|
||||||
|
MINIO_ROOT_USER: ${MINIO_ROOT_USER:?Set MINIO_ROOT_USER in .env}
|
||||||
|
MINIO_ROOT_PASSWORD: ${MINIO_ROOT_PASSWORD:?Set MINIO_ROOT_PASSWORD in .env}
|
||||||
|
volumes:
|
||||||
|
- minio-data:/bitnami/minio/data
|
||||||
|
healthcheck:
|
||||||
|
test: ["CMD", "curl", "--fail", "--silent", "http://localhost:9000/minio/health/live"]
|
||||||
|
interval: 2s
|
||||||
|
timeout: 2s
|
||||||
|
retries: 30
|
||||||
|
restart: unless-stopped
|
||||||
|
cpus: 0.75
|
||||||
|
mem_limit: 512m
|
||||||
|
networks: [backend]
|
||||||
|
|
||||||
|
minio-init:
|
||||||
|
image: bitnamilegacy/minio-client:2025.4.16-debian-12-r0@sha256:28e38f52642c06a2344fa974cf55786b4c52601b6b9b0676c68d40ee0876a5bc
|
||||||
|
depends_on:
|
||||||
|
minio:
|
||||||
|
condition: service_healthy
|
||||||
|
environment:
|
||||||
|
MINIO_ROOT_USER: ${MINIO_ROOT_USER:?Set MINIO_ROOT_USER in .env}
|
||||||
|
MINIO_ROOT_PASSWORD: ${MINIO_ROOT_PASSWORD:?Set MINIO_ROOT_PASSWORD in .env}
|
||||||
|
S3_BUCKET: ${S3_BUCKET:-messenger-attachments}
|
||||||
|
S3_ACCESS_KEY_ID: ${S3_ACCESS_KEY_ID:?Set S3_ACCESS_KEY_ID in .env}
|
||||||
|
S3_SECRET_ACCESS_KEY: ${S3_SECRET_ACCESS_KEY:?Set S3_SECRET_ACCESS_KEY in .env}
|
||||||
|
volumes:
|
||||||
|
- ./infra/minio-api-policy.json:/policy/minio-api-policy.json:ro
|
||||||
|
entrypoint: ["/bin/sh", "-c"]
|
||||||
|
command:
|
||||||
|
- |
|
||||||
|
set -eu
|
||||||
|
mc alias set local http://minio:9000 "$$MINIO_ROOT_USER" "$$MINIO_ROOT_PASSWORD"
|
||||||
|
mc mb --ignore-existing "local/$$S3_BUCKET"
|
||||||
|
mc anonymous set none "local/$$S3_BUCKET"
|
||||||
|
sed "s/__S3_BUCKET__/$$S3_BUCKET/g" /policy/minio-api-policy.json > /tmp/minio-api-policy.json
|
||||||
|
mc admin user add local "$$S3_ACCESS_KEY_ID" "$$S3_SECRET_ACCESS_KEY" || true
|
||||||
|
mc admin policy create local messenger-attachments-rw /tmp/minio-api-policy.json || true
|
||||||
|
mc admin policy attach local messenger-attachments-rw --user "$$S3_ACCESS_KEY_ID"
|
||||||
|
restart: "no"
|
||||||
|
networks: [backend]
|
||||||
|
|
||||||
|
private-bucket-check:
|
||||||
|
profiles: ["verification"]
|
||||||
|
image: bitnamilegacy/minio-client:2025.4.16-debian-12-r0@sha256:28e38f52642c06a2344fa974cf55786b4c52601b6b9b0676c68d40ee0876a5bc
|
||||||
|
depends_on:
|
||||||
|
minio-init:
|
||||||
|
condition: service_completed_successfully
|
||||||
|
environment:
|
||||||
|
S3_BUCKET: ${S3_BUCKET:-messenger-attachments}
|
||||||
|
S3_ACCESS_KEY_ID: ${S3_ACCESS_KEY_ID:?Set S3_ACCESS_KEY_ID in .env}
|
||||||
|
S3_SECRET_ACCESS_KEY: ${S3_SECRET_ACCESS_KEY:?Set S3_SECRET_ACCESS_KEY in .env}
|
||||||
|
entrypoint: ["/bin/sh", "-c"]
|
||||||
|
command:
|
||||||
|
- |
|
||||||
|
set -eu
|
||||||
|
mc alias set anonymous http://minio:9000
|
||||||
|
if mc ls "anonymous/$$S3_BUCKET" >/dev/null 2>&1; then
|
||||||
|
echo "FAIL: anonymous bucket listing succeeded" >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
mc alias set restricted http://minio:9000 "$$S3_ACCESS_KEY_ID" "$$S3_SECRET_ACCESS_KEY"
|
||||||
|
mc ls "restricted/$$S3_BUCKET" >/dev/null
|
||||||
|
if mc admin info restricted >/dev/null 2>&1; then
|
||||||
|
echo "FAIL: API credentials have administrative access" >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
echo "PASS: bucket is private and API credentials are non-admin"
|
||||||
|
restart: "no"
|
||||||
|
networks: [backend]
|
||||||
|
|
||||||
|
# One-shot Flyway runner. It is invoked explicitly before API replicas.
|
||||||
|
migrate:
|
||||||
|
image: hla-lab4-api:local
|
||||||
|
build:
|
||||||
|
context: .
|
||||||
|
environment:
|
||||||
|
<<: *api-environment
|
||||||
|
FLYWAY_ENABLED: "true"
|
||||||
|
MIGRATE_ONLY: "true"
|
||||||
|
SPRING_MAIN_WEB_APPLICATION_TYPE: none
|
||||||
|
depends_on:
|
||||||
|
postgres:
|
||||||
|
condition: service_healthy
|
||||||
|
redis:
|
||||||
|
condition: service_healthy
|
||||||
|
restart: "no"
|
||||||
|
cpus: 0.75
|
||||||
|
mem_limit: 768m
|
||||||
|
networks: [backend]
|
||||||
|
|
||||||
|
api:
|
||||||
|
image: hla-lab4-api:local
|
||||||
|
build:
|
||||||
|
context: .
|
||||||
|
environment:
|
||||||
|
<<: *api-environment
|
||||||
|
FLYWAY_ENABLED: "false"
|
||||||
|
depends_on:
|
||||||
|
postgres:
|
||||||
|
condition: service_healthy
|
||||||
|
redis:
|
||||||
|
condition: service_healthy
|
||||||
|
minio-init:
|
||||||
|
condition: service_completed_successfully
|
||||||
|
healthcheck:
|
||||||
|
test: ["CMD", "curl", "--fail", "--silent", "http://localhost:8080/health/ready"]
|
||||||
|
interval: 2s
|
||||||
|
timeout: 2s
|
||||||
|
retries: 15
|
||||||
|
start_period: 45s
|
||||||
|
init: true
|
||||||
|
stop_grace_period: 12s
|
||||||
|
restart: unless-stopped
|
||||||
|
cpus: 0.75
|
||||||
|
mem_limit: 768m
|
||||||
|
networks: [backend]
|
||||||
|
|
||||||
|
nginx:
|
||||||
|
image: nginx:1.27.4-bookworm
|
||||||
|
ports:
|
||||||
|
- "127.0.0.1:${APP_PORT:-8080}:80"
|
||||||
|
volumes:
|
||||||
|
- ./infra/nginx.conf:/etc/nginx/nginx.conf:ro
|
||||||
|
depends_on:
|
||||||
|
api:
|
||||||
|
condition: service_healthy
|
||||||
|
healthcheck:
|
||||||
|
test: ["CMD", "nginx", "-t"]
|
||||||
|
interval: 2s
|
||||||
|
timeout: 2s
|
||||||
|
retries: 15
|
||||||
|
stop_grace_period: 10s
|
||||||
|
restart: unless-stopped
|
||||||
|
cpus: 0.25
|
||||||
|
mem_limit: 128m
|
||||||
|
networks: [backend]
|
||||||
|
|
||||||
|
networks:
|
||||||
|
backend:
|
||||||
|
driver: bridge
|
||||||
|
|
||||||
|
volumes:
|
||||||
|
postgres-data:
|
||||||
|
redis-data:
|
||||||
|
minio-data:
|
||||||
@@ -0,0 +1,79 @@
|
|||||||
|
# Lab4 evidence — 2026-11-11
|
||||||
|
|
||||||
|
Стенд: Docker Compose, nginx на `127.0.0.1:18084`, API ×2, PostgreSQL, Redis, MinIO. Секреты и session tokens в evidence не сохранялись.
|
||||||
|
|
||||||
|
## Bootstrap и миграции
|
||||||
|
|
||||||
|
- MinIO server image: `bitnamilegacy/minio:2025.4.22-debian-12-r1@sha256:d7cd…e0cab`.
|
||||||
|
- `minio-init`: bucket `messenger-attachments` создан, anonymous access установлен `private`, пользователь `messenger-api` создан, policy `messenger-attachments-rw` назначена.
|
||||||
|
- Flyway: успешно проверены и применены 4 миграции, итоговая версия schema `v4`.
|
||||||
|
- `docker compose ps`: две здоровые API-реплики; MinIO/PostgreSQL/Redis/nginx healthy; наружу опубликован только nginx.
|
||||||
|
|
||||||
|
## Проверки
|
||||||
|
|
||||||
|
```text
|
||||||
|
AttachmentServiceTest + AttachmentReconcilerTest: BUILD SUCCESSFUL, 10 tests.
|
||||||
|
tests/smoke.py: PASS: lab4 public API scenario.
|
||||||
|
student_tests/lab4_negative.py:
|
||||||
|
PASS: unknown/foreign attachment, foreign chat/user, MIME spoof, corruption,
|
||||||
|
byte limit and filename checks.
|
||||||
|
```
|
||||||
|
|
||||||
|
Persistence hook останавливал nginx, обе API, Redis, PostgreSQL и MinIO, затем запускал те же named volumes и повторял idempotent bucket bootstrap:
|
||||||
|
|
||||||
|
```text
|
||||||
|
PASS: persistence preserved confirmed data and existing session.
|
||||||
|
```
|
||||||
|
|
||||||
|
Failover hook остановил replica из `X-Instance-Id`, обслужившую тестовый запрос:
|
||||||
|
|
||||||
|
```json
|
||||||
|
{"observed_instances_before":2,"successful_writes":102,"failed_attempts":0,"first_recovery_seconds":1.386}
|
||||||
|
```
|
||||||
|
|
||||||
|
```text
|
||||||
|
PASS: failover preserved confirmed data and existing session.
|
||||||
|
```
|
||||||
|
|
||||||
|
При остановленном MinIO:
|
||||||
|
|
||||||
|
```text
|
||||||
|
PASS: unavailable S3 returns contract JSON 503
|
||||||
|
```
|
||||||
|
|
||||||
|
После запуска того же MinIO volume публичный smoke повторно завершился PASS, checksum скачанного original совпал.
|
||||||
|
|
||||||
|
## Level 4: consistency, memory and private S3
|
||||||
|
|
||||||
|
Unit tests inject an interrupted request stream before S3 PUT and a repository
|
||||||
|
failure after S3 PUT. The first leaves storage empty; the second verifies the
|
||||||
|
compensating DELETE. Reconciler tests prove that an orphan younger than the
|
||||||
|
one-hour grace period survives, an older orphan is deleted, dry-run never
|
||||||
|
deletes, and a missing object is reported.
|
||||||
|
|
||||||
|
Bootstrap was executed again against the existing bucket and completed with
|
||||||
|
exit code 0. The direct S3 probe (without application API) produced:
|
||||||
|
|
||||||
|
```text
|
||||||
|
PASS: bucket is private and API credentials are non-admin
|
||||||
|
```
|
||||||
|
|
||||||
|
Full Gradle suite using the live Compose PostgreSQL/Redis/MinIO network:
|
||||||
|
|
||||||
|
```text
|
||||||
|
BUILD SUCCESSFUL in 11m 47s
|
||||||
|
```
|
||||||
|
|
||||||
|
Concurrent memory experiment (`evidence/upload-memory-2026-11-11.json`):
|
||||||
|
|
||||||
|
```json
|
||||||
|
{"concurrency":12,"successful_uploads":12,"payload_bytes_each":6835833,
|
||||||
|
"duration_seconds":10.156,"api_replicas":2,"memory_samples":5,
|
||||||
|
"baseline_api_memory_bytes":668467200,"peak_api_memory_bytes":839909375,
|
||||||
|
"peak_increase_bytes":171442175,"configured_max_concurrent_uploads_per_replica":3}
|
||||||
|
```
|
||||||
|
|
||||||
|
The memory values are the aggregate of both API containers from `docker stats`.
|
||||||
|
Sampling can miss a shorter peak, so this is an operational bound observation,
|
||||||
|
not a JVM heap proof. The code-level bounds are 10 MiB/request, 40M pixels and
|
||||||
|
three active uploads per replica.
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
{
|
||||||
|
"concurrency": 12,
|
||||||
|
"successful_uploads": 12,
|
||||||
|
"payload_bytes_each": 6835833,
|
||||||
|
"duration_seconds": 10.156,
|
||||||
|
"api_replicas": 2,
|
||||||
|
"memory_samples": 5,
|
||||||
|
"baseline_api_memory_bytes": 668467200,
|
||||||
|
"peak_api_memory_bytes": 839909375,
|
||||||
|
"peak_increase_bytes": 171442175,
|
||||||
|
"configured_max_concurrent_uploads_per_replica": 3,
|
||||||
|
"configured_max_upload_bytes": 10485760,
|
||||||
|
"configured_max_image_pixels": 40000000
|
||||||
|
}
|
||||||
Vendored
BIN
Binary file not shown.
+9
@@ -0,0 +1,9 @@
|
|||||||
|
distributionBase=GRADLE_USER_HOME
|
||||||
|
distributionPath=wrapper/dists
|
||||||
|
distributionUrl=https\://services.gradle.org/distributions/gradle-9.7.1-bin.zip
|
||||||
|
networkTimeout=10000
|
||||||
|
retries=0
|
||||||
|
retryBackOffMs=500
|
||||||
|
validateDistributionUrl=true
|
||||||
|
zipStoreBase=GRADLE_USER_HOME
|
||||||
|
zipStorePath=wrapper/dists
|
||||||
@@ -0,0 +1,248 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
|
||||||
|
#
|
||||||
|
# Copyright © 2015 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
|
||||||
|
#
|
||||||
|
|
||||||
|
##############################################################################
|
||||||
|
#
|
||||||
|
# gradlew 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 gradlew
|
||||||
|
#
|
||||||
|
# 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/3d91ce3b8caaf77ad09f381f43615b715b53f72c/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
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# 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" )
|
||||||
|
|
||||||
|
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" \
|
||||||
|
-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" "$@"
|
||||||
Vendored
+82
@@ -0,0 +1,82 @@
|
|||||||
|
@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 gradlew startup script for Windows
|
||||||
|
@rem
|
||||||
|
@rem ##########################################################################
|
||||||
|
|
||||||
|
@rem Set local scope for the variables, and ensure extensions are enabled
|
||||||
|
setlocal EnableExtensions
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
"%COMSPEC%" /c exit 1
|
||||||
|
|
||||||
|
: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
|
||||||
|
|
||||||
|
"%COMSPEC%" /c exit 1
|
||||||
|
|
||||||
|
:execute
|
||||||
|
@rem Setup the command line
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@rem Execute gradlew
|
||||||
|
@rem endlocal doesn't take effect until after the line is parsed and variables are expanded
|
||||||
|
@rem which allows us to clear the local environment before executing the java command
|
||||||
|
endlocal & "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -jar "%APP_HOME%\gradle\wrapper\gradle-wrapper.jar" %* & call :exitWithErrorLevel
|
||||||
|
|
||||||
|
:exitWithErrorLevel
|
||||||
|
@rem Use "%COMSPEC%" /c exit to allow operators to work properly in scripts
|
||||||
|
"%COMSPEC%" /c exit %ERRORLEVEL%
|
||||||
@@ -5,3 +5,44 @@
|
|||||||
В REPORT.md сопоставьте роль → service name → версия/digest → health/readiness → volume → сеть/порт → способ передачи credentials. Приведите bootstrap (schema, bucket, broker topology, а с lab6 provisioning) и безопасное повторение этих команд.
|
В REPORT.md сопоставьте роль → service name → версия/digest → health/readiness → volume → сеть/порт → способ передачи credentials. Приведите bootstrap (schema, bucket, broker topology, а с lab6 provisioning) и безопасное повторение этих команд.
|
||||||
|
|
||||||
Приложение строится из исходников. Не используйте заранее существующий образ преподавательского сервера вместо своей реализации. Restart hook перезапускает все соответствующие stores с volumes; failover hook останавливает только заданную API-реплику.
|
Приложение строится из исходников. Не используйте заранее существующий образ преподавательского сервера вместо своей реализации. Restart hook перезапускает все соответствующие stores с volumes; failover hook останавливает только заданную API-реплику.
|
||||||
|
|
||||||
|
## MinIO в этом решении
|
||||||
|
|
||||||
|
- Сервер: `bitnamilegacy/minio:2025.4.22-debian-12-r1` с digest
|
||||||
|
`sha256:d7cd0e172c4cc0870f4bdc3142018e2a37be9acf04d68f386600daad427e0cab`.
|
||||||
|
- Bootstrap-клиент: `bitnamilegacy/minio-client:2025.4.16-debian-12-r0` с digest
|
||||||
|
`sha256:28e38f52642c06a2344fa974cf55786b4c52601b6b9b0676c68d40ee0876a5bc`.
|
||||||
|
- Bucket `messenger-attachments` создаёт одноразовый `minio-init`. Команда
|
||||||
|
`mc anonymous set none` явно оставляет bucket приватным.
|
||||||
|
- API не получает root credentials. Bootstrap создаёт отдельного пользователя и
|
||||||
|
назначает политику из `minio-api-policy.json`: только list/location bucket и
|
||||||
|
get/put/delete объектов под префиксом `attachments/`.
|
||||||
|
- Порт S3 и административная консоль наружу не публикуются. API обращается к
|
||||||
|
`http://minio:9000` в Compose или `http://hla-state_minio:9000` в Swarm.
|
||||||
|
- Байты лежат в named volume `minio-data`; PostgreSQL хранит только метаданные и
|
||||||
|
серверный object key. Клиентский filename никогда не используется как key.
|
||||||
|
|
||||||
|
Политика содержит placeholder `__S3_BUCKET__`; bootstrap подставляет фактическое
|
||||||
|
значение `S3_BUCKET` перед созданием policy. Повторный запуск безопасен: bucket,
|
||||||
|
user и policy переиспользуются, а private anonymous policy устанавливается снова.
|
||||||
|
|
||||||
|
Проверка вне API:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
ENV_FILE=.env ./scripts/check-private-bucket.sh
|
||||||
|
```
|
||||||
|
|
||||||
|
Одноразовый `private-bucket-check` подключается непосредственно к S3 внутри
|
||||||
|
backend network без HTTP API приложения. Он требует, чтобы anonymous list bucket
|
||||||
|
завершился отказом, проверяет list с ограниченными API credentials и требует,
|
||||||
|
чтобы `mc admin info` с этими credentials также завершился отказом. Сервис имеет
|
||||||
|
Compose profile `verification` и не работает постоянно.
|
||||||
|
|
||||||
|
## Сверка PostgreSQL и MinIO
|
||||||
|
|
||||||
|
API запускает `AttachmentReconciler` с prefix `attachments/`. Метаданные из
|
||||||
|
PostgreSQL сравниваются с объектами MinIO. Старый orphan удаляется после
|
||||||
|
`ATTACHMENT_CLEANUP_GRACE_SECONDS` (по умолчанию 3600), свежий orphan защищён от
|
||||||
|
гонки PUT/COMMIT. `ATTACHMENT_CLEANUP_DRY_RUN=true` только сообщает кандидатов.
|
||||||
|
Missing object логируется как ERROR, но metadata автоматически не удаляется.
|
||||||
|
Интервал и initial delay задаются через `.env.example`.
|
||||||
|
|||||||
@@ -0,0 +1,26 @@
|
|||||||
|
{
|
||||||
|
"Version": "2012-10-17",
|
||||||
|
"Statement": [
|
||||||
|
{
|
||||||
|
"Effect": "Allow",
|
||||||
|
"Action": [
|
||||||
|
"s3:GetBucketLocation",
|
||||||
|
"s3:ListBucket"
|
||||||
|
],
|
||||||
|
"Resource": [
|
||||||
|
"arn:aws:s3:::__S3_BUCKET__"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"Effect": "Allow",
|
||||||
|
"Action": [
|
||||||
|
"s3:GetObject",
|
||||||
|
"s3:PutObject",
|
||||||
|
"s3:DeleteObject"
|
||||||
|
],
|
||||||
|
"Resource": [
|
||||||
|
"arn:aws:s3:::__S3_BUCKET__/attachments/*"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
@@ -0,0 +1,103 @@
|
|||||||
|
worker_processes auto;
|
||||||
|
worker_shutdown_timeout 10s;
|
||||||
|
|
||||||
|
events {
|
||||||
|
worker_connections 1024;
|
||||||
|
}
|
||||||
|
|
||||||
|
http {
|
||||||
|
# Let the application enforce the exact 10 MiB contract and return JSON.
|
||||||
|
client_max_body_size 11m;
|
||||||
|
client_body_timeout 10s;
|
||||||
|
access_log /var/log/nginx/access.log;
|
||||||
|
error_log /var/log/nginx/error.log warn;
|
||||||
|
|
||||||
|
# Docker embedded DNS returns current Compose replicas. Nginx OSS 1.27.3+
|
||||||
|
# refreshes these addresses without a configuration reload.
|
||||||
|
resolver 127.0.0.11 valid=2s ipv6=off;
|
||||||
|
resolver_timeout 1s;
|
||||||
|
|
||||||
|
upstream api_backend {
|
||||||
|
zone api_backend 64k;
|
||||||
|
least_conn;
|
||||||
|
server api:8080 resolve max_fails=1 fail_timeout=2s;
|
||||||
|
keepalive 32;
|
||||||
|
}
|
||||||
|
|
||||||
|
server {
|
||||||
|
listen 80;
|
||||||
|
server_name _;
|
||||||
|
proxy_intercept_errors on;
|
||||||
|
error_page 502 503 504 = @no_ready_api;
|
||||||
|
|
||||||
|
location = /nginx-health {
|
||||||
|
access_log off;
|
||||||
|
default_type application/json;
|
||||||
|
return 200 '{"status":"ok"}';
|
||||||
|
}
|
||||||
|
|
||||||
|
# After both peers have refused traffic or become unreachable, Nginx
|
||||||
|
# would normally emit its own HTML 502. Keep the public API contract:
|
||||||
|
# no ready backend is a JSON 503 with the required tracing headers.
|
||||||
|
location @no_ready_api {
|
||||||
|
internal;
|
||||||
|
default_type application/json;
|
||||||
|
add_header X-Request-Id $request_id always;
|
||||||
|
add_header X-Instance-Id nginx always;
|
||||||
|
add_header Cache-Control "no-store" always;
|
||||||
|
return 503 '{"error":{"code":"unavailable","message":"No ready API backend","request_id":"$request_id"}}';
|
||||||
|
}
|
||||||
|
|
||||||
|
# Message POST is the only non-idempotent HTTP method that is safe to
|
||||||
|
# replay here: the API requires Idempotency-Key and commits the message
|
||||||
|
# together with its shared PostgreSQL idempotency record.
|
||||||
|
location ~ ^/api/v1/chats/[0-9a-fA-F-]+/messages$ {
|
||||||
|
proxy_pass http://api_backend;
|
||||||
|
proxy_http_version 1.1;
|
||||||
|
proxy_set_header Connection "";
|
||||||
|
proxy_set_header Host $host;
|
||||||
|
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||||
|
proxy_set_header X-Forwarded-Proto $scheme;
|
||||||
|
proxy_request_buffering on;
|
||||||
|
|
||||||
|
proxy_connect_timeout 500ms;
|
||||||
|
proxy_send_timeout 4s;
|
||||||
|
proxy_read_timeout 4s;
|
||||||
|
proxy_next_upstream error timeout http_502 http_503 http_504 non_idempotent;
|
||||||
|
proxy_next_upstream_tries 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
location ~ ^/api/v1/chats/[0-9a-fA-F-]+/attachments$ {
|
||||||
|
proxy_pass http://api_backend;
|
||||||
|
proxy_http_version 1.1;
|
||||||
|
proxy_set_header Connection "";
|
||||||
|
proxy_set_header Host $host;
|
||||||
|
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||||
|
proxy_set_header X-Forwarded-Proto $scheme;
|
||||||
|
proxy_request_buffering on;
|
||||||
|
|
||||||
|
proxy_connect_timeout 500ms;
|
||||||
|
proxy_send_timeout 20s;
|
||||||
|
proxy_read_timeout 20s;
|
||||||
|
# Upload is not replayed: it has no idempotency key. The client may retry explicitly.
|
||||||
|
proxy_next_upstream off;
|
||||||
|
}
|
||||||
|
|
||||||
|
location / {
|
||||||
|
proxy_pass http://api_backend;
|
||||||
|
proxy_http_version 1.1;
|
||||||
|
proxy_set_header Connection "";
|
||||||
|
proxy_set_header Host $host;
|
||||||
|
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||||
|
proxy_set_header X-Forwarded-Proto $scheme;
|
||||||
|
|
||||||
|
proxy_connect_timeout 500ms;
|
||||||
|
proxy_send_timeout 4s;
|
||||||
|
proxy_read_timeout 4s;
|
||||||
|
|
||||||
|
# No sticky sessions. Failed/unready peers are retried elsewhere.
|
||||||
|
proxy_next_upstream error timeout http_502 http_503 http_504;
|
||||||
|
proxy_next_upstream_tries 2;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
@echo off
|
||||||
|
setlocal EnableExtensions
|
||||||
|
cd /d "%~dp0\.."
|
||||||
|
if not defined ENV_FILE set "ENV_FILE=.env"
|
||||||
|
|
||||||
|
docker compose --env-file "%ENV_FILE%" --profile verification run --rm private-bucket-check
|
||||||
|
exit /b %ERRORLEVEL%
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
set -eu
|
||||||
|
|
||||||
|
SCRIPT_DIR=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
|
||||||
|
PROJECT_DIR=$(dirname -- "$SCRIPT_DIR")
|
||||||
|
cd "$PROJECT_DIR"
|
||||||
|
: "${ENV_FILE:=.env}"
|
||||||
|
|
||||||
|
docker compose --env-file "$ENV_FILE" --profile verification run --rm private-bucket-check
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
@echo off
|
||||||
|
setlocal EnableExtensions
|
||||||
|
cd /d "%~dp0\.."
|
||||||
|
if not defined ENV_FILE set "ENV_FILE=.env"
|
||||||
|
|
||||||
|
docker compose --env-file "%ENV_FILE%" stop nginx api redis postgres minio || exit /b 1
|
||||||
|
docker compose --env-file "%ENV_FILE%" up -d --wait --wait-timeout 90 postgres redis minio || exit /b 1
|
||||||
|
docker compose --env-file "%ENV_FILE%" run --rm minio-init || exit /b 1
|
||||||
|
docker compose --env-file "%ENV_FILE%" up -d --wait --wait-timeout 90 --no-deps --scale api=2 api || exit /b 1
|
||||||
|
docker compose --env-file "%ENV_FILE%" up -d --no-deps nginx || exit /b 1
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
set -eu
|
||||||
|
|
||||||
|
SCRIPT_DIR=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
|
||||||
|
PROJECT_DIR=$(dirname -- "$SCRIPT_DIR")
|
||||||
|
cd "$PROJECT_DIR"
|
||||||
|
: "${ENV_FILE:=.env}"
|
||||||
|
|
||||||
|
# Preserve named volumes and the network. The migration job is intentionally
|
||||||
|
# not re-run during a same-version restart.
|
||||||
|
docker compose --env-file "$ENV_FILE" stop nginx api redis postgres minio
|
||||||
|
docker compose --env-file "$ENV_FILE" up -d --wait --wait-timeout 90 postgres redis minio
|
||||||
|
docker compose --env-file "$ENV_FILE" run --rm minio-init
|
||||||
|
docker compose --env-file "$ENV_FILE" up -d --wait --wait-timeout 90 --no-deps --scale api=2 api
|
||||||
|
docker compose --env-file "$ENV_FILE" up -d --no-deps nginx
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
@echo off
|
||||||
|
setlocal EnableExtensions
|
||||||
|
cd /d "%~dp0\.."
|
||||||
|
if not defined ENV_FILE set "ENV_FILE=.env"
|
||||||
|
|
||||||
|
docker compose --env-file "%ENV_FILE%" up -d --wait --wait-timeout 90 --no-deps --scale api=2 api
|
||||||
|
exit /b %ERRORLEVEL%
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
set -eu
|
||||||
|
|
||||||
|
SCRIPT_DIR=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
|
||||||
|
PROJECT_DIR=$(dirname -- "$SCRIPT_DIR")
|
||||||
|
cd "$PROJECT_DIR"
|
||||||
|
: "${ENV_FILE:=.env}"
|
||||||
|
|
||||||
|
docker compose --env-file "$ENV_FILE" up -d --wait --wait-timeout 90 \
|
||||||
|
--no-deps --scale api=2 api
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
@echo off
|
||||||
|
setlocal EnableExtensions
|
||||||
|
cd /d "%~dp0\.."
|
||||||
|
|
||||||
|
set /a REPLICA_COUNT=0
|
||||||
|
for /f "usebackq delims=" %%C in (`docker compose --env-file .env ps -q api`) do set /a REPLICA_COUNT+=1
|
||||||
|
if %REPLICA_COUNT% LSS 2 exit /b 2
|
||||||
|
|
||||||
|
for /f "usebackq delims=" %%C in (`docker compose --env-file .env ps -q api`) do call :replace_one %%C
|
||||||
|
exit /b 0
|
||||||
|
|
||||||
|
:replace_one
|
||||||
|
docker stop --time 10 %1 >nul || exit /b 1
|
||||||
|
docker rm %1 >nul || exit /b 1
|
||||||
|
docker compose --env-file .env up -d --wait --wait-timeout 90 --no-deps --scale api=2 api >nul || exit /b 1
|
||||||
|
exit /b 0
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
set -eu
|
||||||
|
|
||||||
|
SCRIPT_DIR=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
|
||||||
|
PROJECT_DIR=$(dirname -- "$SCRIPT_DIR")
|
||||||
|
cd "$PROJECT_DIR"
|
||||||
|
|
||||||
|
original_ids=$(docker compose --env-file .env ps -q api)
|
||||||
|
count=$(printf '%s\n' "$original_ids" | sed '/^$/d' | wc -l | tr -d ' ')
|
||||||
|
if [ "$count" -lt 2 ]; then
|
||||||
|
printf '%s\n' 'Rolling update requires at least two API replicas' >&2
|
||||||
|
exit 2
|
||||||
|
fi
|
||||||
|
|
||||||
|
for container_id in $original_ids; do
|
||||||
|
# Replace exactly one original replica and wait for the replacement before
|
||||||
|
# touching the next one. Named volumes and state stores are not changed.
|
||||||
|
docker stop --time 10 "$container_id" >/dev/null
|
||||||
|
docker rm "$container_id" >/dev/null
|
||||||
|
docker compose --env-file .env up -d --wait --wait-timeout 90 \
|
||||||
|
--no-deps --scale api=2 api >/dev/null
|
||||||
|
done
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
@echo off
|
||||||
|
setlocal EnableExtensions EnableDelayedExpansion
|
||||||
|
|
||||||
|
if "%TARGET_INSTANCE_ID%"=="" exit /b 2
|
||||||
|
cd /d "%~dp0\.."
|
||||||
|
if not defined ENV_FILE set "ENV_FILE=.env"
|
||||||
|
|
||||||
|
set "TARGET_CONTAINER="
|
||||||
|
set /a MATCHES=0
|
||||||
|
for /f "usebackq delims=" %%C in (`docker compose --env-file "%ENV_FILE%" ps -q api`) do (
|
||||||
|
set "CONTAINER_ID=%%C"
|
||||||
|
set "SHORT_ID=!CONTAINER_ID:~0,12!"
|
||||||
|
set "HOSTNAME_VALUE="
|
||||||
|
for /f "usebackq delims=" %%H in (`docker inspect --format "{{.Config.Hostname}}" %%C`) do set "HOSTNAME_VALUE=%%H"
|
||||||
|
if "!HOSTNAME_VALUE!"=="%TARGET_INSTANCE_ID%" (
|
||||||
|
set "TARGET_CONTAINER=%%C"
|
||||||
|
set /a MATCHES+=1
|
||||||
|
) else if "!SHORT_ID!"=="%TARGET_INSTANCE_ID%" (
|
||||||
|
set "TARGET_CONTAINER=%%C"
|
||||||
|
set /a MATCHES+=1
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
if not "!MATCHES!"=="1" exit /b 2
|
||||||
|
docker stop --time 5 "!TARGET_CONTAINER!" >nul
|
||||||
|
exit /b %ERRORLEVEL%
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
set -eu
|
||||||
|
|
||||||
|
: "${TARGET_INSTANCE_ID:?test provides target API instance alias}"
|
||||||
|
|
||||||
|
SCRIPT_DIR=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
|
||||||
|
PROJECT_DIR=$(dirname -- "$SCRIPT_DIR")
|
||||||
|
cd "$PROJECT_DIR"
|
||||||
|
: "${ENV_FILE:=.env}"
|
||||||
|
|
||||||
|
target=""
|
||||||
|
matches=0
|
||||||
|
for container_id in $(docker compose --env-file "$ENV_FILE" ps -q api); do
|
||||||
|
hostname=$(docker inspect --format '{{.Config.Hostname}}' "$container_id")
|
||||||
|
short_id=$(printf '%s' "$container_id" | cut -c1-12)
|
||||||
|
if [ "$TARGET_INSTANCE_ID" = "$hostname" ] || [ "$TARGET_INSTANCE_ID" = "$short_id" ]; then
|
||||||
|
target=$container_id
|
||||||
|
matches=$((matches + 1))
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
|
||||||
|
if [ "$matches" -ne 1 ]; then
|
||||||
|
printf '%s\n' "Expected one API replica for X-Instance-Id, found $matches" >&2
|
||||||
|
exit 2
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Five seconds keeps the operator hook below the test's ten-second budget while
|
||||||
|
# still delivering SIGTERM and allowing Spring's graceful shutdown phase.
|
||||||
|
docker stop --time 5 "$target" >/dev/null
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
$ErrorActionPreference = 'Continue'
|
||||||
|
if (Test-Path variable:PSNativeCommandUseErrorActionPreference) {
|
||||||
|
$PSNativeCommandUseErrorActionPreference = $false
|
||||||
|
}
|
||||||
|
|
||||||
|
$nodes = @('hla-swarm-manager', 'hla-swarm-worker1', 'hla-swarm-worker2', 'hla-swarm-registry')
|
||||||
|
foreach ($node in $nodes) {
|
||||||
|
$existing = (& docker ps --all --filter "name=^/${node}$" --format '{{.Names}}')
|
||||||
|
if ($existing -eq $node) {
|
||||||
|
docker rm --force $node | Out-Null
|
||||||
|
if ($LASTEXITCODE -ne 0) { throw "Cannot remove $node" }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$existingNetwork = (& docker network ls --filter 'name=^hla-swarm-underlay$' --format '{{.Name}}')
|
||||||
|
if ($existingNetwork -eq 'hla-swarm-underlay') {
|
||||||
|
docker network rm hla-swarm-underlay | Out-Null
|
||||||
|
if ($LASTEXITCODE -ne 0) { throw 'Cannot remove hla-swarm-underlay' }
|
||||||
|
}
|
||||||
|
Write-Output 'Removed the disposable three-node Swarm lab and its nested volumes.'
|
||||||
@@ -0,0 +1,171 @@
|
|||||||
|
$ErrorActionPreference = 'Continue'
|
||||||
|
if (Test-Path variable:PSNativeCommandUseErrorActionPreference) {
|
||||||
|
$PSNativeCommandUseErrorActionPreference = $false
|
||||||
|
}
|
||||||
|
|
||||||
|
$projectDir = Split-Path -Parent $PSScriptRoot
|
||||||
|
Set-Location $projectDir
|
||||||
|
|
||||||
|
$manager = 'hla-swarm-manager'
|
||||||
|
$nodes = @($manager, 'hla-swarm-worker1', 'hla-swarm-worker2')
|
||||||
|
$network = 'hla-swarm-underlay'
|
||||||
|
$registry = 'hla-swarm-registry'
|
||||||
|
|
||||||
|
function Invoke-Checked {
|
||||||
|
param([Parameter(ValueFromRemainingArguments = $true)][string[]]$Command)
|
||||||
|
$executable = $Command[0]
|
||||||
|
$arguments = if ($Command.Length -gt 1) {
|
||||||
|
$Command[1..($Command.Length - 1)]
|
||||||
|
} else {
|
||||||
|
@()
|
||||||
|
}
|
||||||
|
& $executable @arguments
|
||||||
|
if ($LASTEXITCODE -ne 0) {
|
||||||
|
throw "Command failed: $($Command -join ' ')"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function Wait-InnerDocker([string]$Node) {
|
||||||
|
$deadline = (Get-Date).AddSeconds(90)
|
||||||
|
do {
|
||||||
|
& docker exec $Node docker info *> $null
|
||||||
|
if ($LASTEXITCODE -eq 0) { return }
|
||||||
|
Start-Sleep -Seconds 2
|
||||||
|
} while ((Get-Date) -lt $deadline)
|
||||||
|
throw "Docker daemon did not start in $Node"
|
||||||
|
}
|
||||||
|
|
||||||
|
function Wait-Service([string]$Service, [string]$Expected, [int]$Seconds = 180) {
|
||||||
|
$deadline = (Get-Date).AddSeconds($Seconds)
|
||||||
|
do {
|
||||||
|
$replicas = (& docker exec $manager docker service ls `
|
||||||
|
--filter "name=$Service" --format '{{.Replicas}}' 2>$null | Select-Object -First 1)
|
||||||
|
if ($replicas -like "$Expected*") { return }
|
||||||
|
Start-Sleep -Seconds 2
|
||||||
|
} while ((Get-Date) -lt $deadline)
|
||||||
|
throw "Service $Service did not reach $Expected replicas"
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ($node in $nodes) {
|
||||||
|
$existing = (& docker ps --all --filter "name=^/${node}$" --format '{{.Names}}')
|
||||||
|
if ($existing -eq $node) {
|
||||||
|
throw "Container $node already exists. Run scripts/swarm-lab-down.ps1 first."
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$existingRegistry = (& docker ps --all --filter "name=^/${registry}$" --format '{{.Names}}')
|
||||||
|
if ($existingRegistry -eq $registry) {
|
||||||
|
throw "Container $registry already exists. Run scripts/swarm-lab-down.ps1 first."
|
||||||
|
}
|
||||||
|
|
||||||
|
$envValues = @{}
|
||||||
|
Get-Content -LiteralPath '.env' | ForEach-Object {
|
||||||
|
if ($_ -match '^\s*([A-Za-z_][A-Za-z0-9_]*)\s*=\s*(.*)\s*$') {
|
||||||
|
$envValues[$matches[1]] = $matches[2].Trim('"', "'")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$password = $envValues['DATABASE_PASSWORD']
|
||||||
|
if ([string]::IsNullOrWhiteSpace($password)) { throw 'DATABASE_PASSWORD is missing from .env' }
|
||||||
|
$database = if ($envValues['DATABASE_NAME']) { $envValues['DATABASE_NAME'] } else { 'messenger' }
|
||||||
|
$user = if ($envValues['DATABASE_USER']) { $envValues['DATABASE_USER'] } else { 'messenger' }
|
||||||
|
$minioRootUser = $envValues['MINIO_ROOT_USER']
|
||||||
|
$minioRootPassword = $envValues['MINIO_ROOT_PASSWORD']
|
||||||
|
$s3AccessKey = $envValues['S3_ACCESS_KEY_ID']
|
||||||
|
$s3SecretKey = $envValues['S3_SECRET_ACCESS_KEY']
|
||||||
|
$s3Bucket = if ($envValues['S3_BUCKET']) { $envValues['S3_BUCKET'] } else { 'messenger-attachments' }
|
||||||
|
$s3Region = if ($envValues['S3_REGION']) { $envValues['S3_REGION'] } else { 'us-east-1' }
|
||||||
|
foreach ($required in @{
|
||||||
|
MINIO_ROOT_USER = $minioRootUser
|
||||||
|
MINIO_ROOT_PASSWORD = $minioRootPassword
|
||||||
|
S3_ACCESS_KEY_ID = $s3AccessKey
|
||||||
|
S3_SECRET_ACCESS_KEY = $s3SecretKey
|
||||||
|
}.GetEnumerator()) {
|
||||||
|
if ([string]::IsNullOrWhiteSpace($required.Value)) { throw "$($required.Key) is missing from .env" }
|
||||||
|
}
|
||||||
|
|
||||||
|
$existingNetwork = (& docker network ls --filter "name=^${network}$" --format '{{.Name}}')
|
||||||
|
if ($existingNetwork -ne $network) { Invoke-Checked docker network create $network }
|
||||||
|
|
||||||
|
Invoke-Checked docker run --detach --name $registry --hostname $registry `
|
||||||
|
--network $network --publish 127.0.0.1:15000:5000 registry:2
|
||||||
|
|
||||||
|
Invoke-Checked docker run --privileged --detach --name $manager --hostname $manager `
|
||||||
|
--network $network --publish 127.0.0.1:18080:8080 `
|
||||||
|
--volume "${projectDir}:/project:ro" --env DOCKER_TLS_CERTDIR= docker:28-dind `
|
||||||
|
--storage-driver=overlay2 --insecure-registry=${registry}:5000
|
||||||
|
foreach ($worker in $nodes[1..2]) {
|
||||||
|
Invoke-Checked docker run --privileged --detach --name $worker --hostname $worker `
|
||||||
|
--network $network --env DOCKER_TLS_CERTDIR= docker:28-dind `
|
||||||
|
--storage-driver=overlay2 --insecure-registry=${registry}:5000
|
||||||
|
}
|
||||||
|
foreach ($node in $nodes) { Wait-InnerDocker $node }
|
||||||
|
|
||||||
|
$managerIp = (& docker inspect --format `
|
||||||
|
'{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $manager).Trim()
|
||||||
|
Invoke-Checked docker exec $manager docker swarm init --advertise-addr $managerIp
|
||||||
|
$token = (& docker exec $manager docker swarm join-token --quiet worker).Trim()
|
||||||
|
foreach ($worker in $nodes[1..2]) {
|
||||||
|
Invoke-Checked docker exec $worker docker swarm join --token $token "$managerIp`:2377"
|
||||||
|
}
|
||||||
|
Invoke-Checked docker exec $manager docker node update `
|
||||||
|
--label-add hla.state=true --label-add hla.lb=true $manager
|
||||||
|
|
||||||
|
Invoke-Checked docker compose --env-file .env build api
|
||||||
|
Invoke-Checked docker tag hla-lab4-api:local localhost:15000/hla-lab4-api:swarm
|
||||||
|
Invoke-Checked docker build --file swarm/nginx.Dockerfile `
|
||||||
|
--tag localhost:15000/hla-lab4-nginx:swarm .
|
||||||
|
Invoke-Checked docker push localhost:15000/hla-lab4-api:swarm
|
||||||
|
Invoke-Checked docker push localhost:15000/hla-lab4-nginx:swarm
|
||||||
|
|
||||||
|
Invoke-Checked docker exec --env "DATABASE_PASSWORD=$password" `
|
||||||
|
--env "DATABASE_NAME=$database" --env "DATABASE_USER=$user" `
|
||||||
|
--env "MINIO_ROOT_USER=$minioRootUser" --env "MINIO_ROOT_PASSWORD=$minioRootPassword" `
|
||||||
|
--env "S3_ACCESS_KEY_ID=$s3AccessKey" --env "S3_SECRET_ACCESS_KEY=$s3SecretKey" `
|
||||||
|
--env "S3_BUCKET=$s3Bucket" `
|
||||||
|
$manager docker stack deploy --compose-file /project/swarm/stack.state.yaml hla-state
|
||||||
|
Wait-Service 'hla-state_postgres' '1/1'
|
||||||
|
Wait-Service 'hla-state_redis' '1/1'
|
||||||
|
Wait-Service 'hla-state_minio' '1/1'
|
||||||
|
|
||||||
|
$deadline = (Get-Date).AddSeconds(180)
|
||||||
|
do {
|
||||||
|
$minioInitState = (& docker exec $manager docker service ps hla-state_minio-init `
|
||||||
|
--no-trunc --format '{{.CurrentState}}' | Select-Object -First 1)
|
||||||
|
if ($minioInitState -like 'Complete*') { break }
|
||||||
|
Start-Sleep -Seconds 2
|
||||||
|
} while ((Get-Date) -lt $deadline)
|
||||||
|
if ($minioInitState -notlike 'Complete*') { throw 'MinIO bucket bootstrap did not complete' }
|
||||||
|
|
||||||
|
Invoke-Checked docker exec $manager docker service create --name hla-migrate `
|
||||||
|
--mode replicated-job --replicas 1 --max-concurrent 1 `
|
||||||
|
--network hla-state_backend --constraint node.labels.hla.state==true `
|
||||||
|
--restart-condition on-failure --restart-delay 3s `
|
||||||
|
--limit-cpu 0.75 --limit-memory 768M `
|
||||||
|
--env "DATABASE_URL=jdbc:postgresql://hla-state_postgres:5432/$database" `
|
||||||
|
--env "DATABASE_USER=$user" --env "DATABASE_PASSWORD=$password" `
|
||||||
|
--env FLYWAY_ENABLED=true --env MIGRATE_ONLY=true `
|
||||||
|
--env SPRING_MAIN_WEB_APPLICATION_TYPE=none `
|
||||||
|
--env "S3_ENDPOINT=http://hla-state_minio:9000" `
|
||||||
|
--env "S3_BUCKET=$s3Bucket" --env "S3_REGION=$s3Region" `
|
||||||
|
--env "S3_ACCESS_KEY_ID=$s3AccessKey" --env "S3_SECRET_ACCESS_KEY=$s3SecretKey" `
|
||||||
|
hla-swarm-registry:5000/hla-lab4-api:swarm
|
||||||
|
$deadline = (Get-Date).AddSeconds(180)
|
||||||
|
do {
|
||||||
|
$state = (& docker exec $manager docker service ps hla-migrate `
|
||||||
|
--no-trunc --format '{{.CurrentState}}' | Select-Object -First 1)
|
||||||
|
if ($state -like 'Complete*') { break }
|
||||||
|
Start-Sleep -Seconds 2
|
||||||
|
} while ((Get-Date) -lt $deadline)
|
||||||
|
if ($state -notlike 'Complete*') { throw 'Migration job did not complete' }
|
||||||
|
Invoke-Checked docker exec $manager docker service rm hla-migrate
|
||||||
|
|
||||||
|
Invoke-Checked docker exec --env "DATABASE_PASSWORD=$password" `
|
||||||
|
--env "DATABASE_NAME=$database" --env "DATABASE_USER=$user" `
|
||||||
|
--env "S3_BUCKET=$s3Bucket" --env "S3_REGION=$s3Region" `
|
||||||
|
--env "S3_ACCESS_KEY_ID=$s3AccessKey" --env "S3_SECRET_ACCESS_KEY=$s3SecretKey" `
|
||||||
|
$manager docker stack deploy --compose-file /project/swarm/stack.app.yaml hla-app
|
||||||
|
Wait-Service 'hla-app_api' '2/2'
|
||||||
|
Wait-Service 'hla-app_nginx' '1/1'
|
||||||
|
|
||||||
|
Write-Output 'Swarm lab is ready at http://localhost:18080'
|
||||||
|
Invoke-Checked docker exec $manager docker node ls
|
||||||
|
Invoke-Checked docker exec $manager docker service ps hla-app_api
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
rootProject.name = 'mtusi-messenger'
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
package com.example.mtusi_messenger;
|
||||||
|
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.springframework.context.ConfigurableApplicationContext;
|
||||||
|
import org.springframework.scheduling.annotation.EnableScheduling;
|
||||||
|
|
||||||
|
@SpringBootApplication
|
||||||
|
@EnableScheduling
|
||||||
|
public class MtusiMessengerApplication {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
ConfigurableApplicationContext context = SpringApplication.run(
|
||||||
|
MtusiMessengerApplication.class, args);
|
||||||
|
if (Boolean.parseBoolean(System.getenv("MIGRATE_ONLY"))) {
|
||||||
|
System.exit(SpringApplication.exit(context));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,83 @@
|
|||||||
|
package com.example.mtusi_messenger.cache;
|
||||||
|
|
||||||
|
import com.example.mtusi_messenger.model.User;
|
||||||
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.dao.DataAccessException;
|
||||||
|
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.time.Duration;
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class UserCache {
|
||||||
|
private static final Logger log = LoggerFactory.getLogger(UserCache.class);
|
||||||
|
private static final String KEY_PREFIX = "messenger:user:";
|
||||||
|
|
||||||
|
private final StringRedisTemplate redis;
|
||||||
|
private final ObjectMapper mapper;
|
||||||
|
private final Duration ttl;
|
||||||
|
|
||||||
|
public UserCache(
|
||||||
|
StringRedisTemplate redis,
|
||||||
|
ObjectMapper mapper,
|
||||||
|
@Value("${messenger.cache.user-ttl-seconds:300}") long ttlSeconds
|
||||||
|
) {
|
||||||
|
if (ttlSeconds <= 0) {
|
||||||
|
throw new IllegalArgumentException("User cache TTL must be positive");
|
||||||
|
}
|
||||||
|
this.redis = redis;
|
||||||
|
this.mapper = mapper;
|
||||||
|
this.ttl = Duration.ofSeconds(ttlSeconds);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Optional<User> find(UUID userId) {
|
||||||
|
try {
|
||||||
|
String value = redis.opsForValue().get(key(userId));
|
||||||
|
if (value == null) {
|
||||||
|
return Optional.empty();
|
||||||
|
}
|
||||||
|
User cachedUser = mapper.readValue(value, User.class);
|
||||||
|
if (cachedUser == null) {
|
||||||
|
log.warn("Ignoring null Redis user cache entry for {}", userId);
|
||||||
|
evict(userId);
|
||||||
|
return Optional.empty();
|
||||||
|
}
|
||||||
|
return Optional.of(cachedUser);
|
||||||
|
} catch (DataAccessException e) {
|
||||||
|
log.warn("Redis user cache is unavailable; using PostgreSQL fallback");
|
||||||
|
return Optional.empty();
|
||||||
|
} catch (JsonProcessingException e) {
|
||||||
|
log.warn("Ignoring malformed Redis user cache entry for {}", userId);
|
||||||
|
evict(userId);
|
||||||
|
return Optional.empty();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void put(User user) {
|
||||||
|
try {
|
||||||
|
redis.opsForValue().set(key(user.id()), mapper.writeValueAsString(user), ttl);
|
||||||
|
} catch (DataAccessException e) {
|
||||||
|
log.warn("Redis user cache is unavailable; user was not cached");
|
||||||
|
} catch (JsonProcessingException e) {
|
||||||
|
throw new IllegalStateException("Cannot serialize user cache entry", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void evict(UUID userId) {
|
||||||
|
try {
|
||||||
|
redis.delete(key(userId));
|
||||||
|
} catch (DataAccessException e) {
|
||||||
|
log.warn("Redis user cache is unavailable; cache eviction was skipped");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private String key(UUID userId) {
|
||||||
|
return KEY_PREFIX + userId;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
package com.example.mtusi_messenger.config;
|
||||||
|
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.boot.jackson.autoconfigure.JsonMapperBuilderCustomizer;
|
||||||
|
|
||||||
|
import tools.jackson.databind.cfg.CoercionAction;
|
||||||
|
import tools.jackson.databind.cfg.CoercionInputShape;
|
||||||
|
import tools.jackson.databind.type.LogicalType;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
public class JacksonConfig {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
JsonMapperBuilderCustomizer strictStringTypes() {
|
||||||
|
return builder -> builder.withCoercionConfig(
|
||||||
|
LogicalType.Textual,
|
||||||
|
config -> {
|
||||||
|
config.setCoercion(
|
||||||
|
CoercionInputShape.Integer, CoercionAction.Fail);
|
||||||
|
config.setCoercion(
|
||||||
|
CoercionInputShape.Float, CoercionAction.Fail);
|
||||||
|
config.setCoercion(
|
||||||
|
CoercionInputShape.Boolean, CoercionAction.Fail);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
package com.example.mtusi_messenger.config;
|
||||||
|
|
||||||
|
|
||||||
|
import io.swagger.v3.oas.annotations.enums.SecuritySchemeType;
|
||||||
|
import io.swagger.v3.oas.annotations.security.SecurityScheme;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
|
@SecurityScheme(
|
||||||
|
name = "basicAuth",
|
||||||
|
type = SecuritySchemeType.HTTP,
|
||||||
|
scheme = "basic"
|
||||||
|
)
|
||||||
|
@SecurityScheme(
|
||||||
|
name = "bearerAuth",
|
||||||
|
type = SecuritySchemeType.HTTP,
|
||||||
|
scheme = "bearer",
|
||||||
|
bearerFormat = "opaque session token"
|
||||||
|
)
|
||||||
|
@Configuration
|
||||||
|
public class OpenApiConfig {
|
||||||
|
}
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
package com.example.mtusi_messenger.config;
|
||||||
|
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.security.crypto.argon2.Argon2PasswordEncoder;
|
||||||
|
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
public class PasswordConfig {
|
||||||
|
@Bean
|
||||||
|
PasswordEncoder passwordEncoder() {
|
||||||
|
return Argon2PasswordEncoder.defaultsForSpringSecurity_v5_8();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,107 @@
|
|||||||
|
package com.example.mtusi_messenger.config;
|
||||||
|
|
||||||
|
import com.example.mtusi_messenger.security.ApiAuthenticationFilter;
|
||||||
|
import com.example.mtusi_messenger.security.DatabaseAuthenticationProvider;
|
||||||
|
import com.example.mtusi_messenger.security.RestAuthenticationEntryPoint;
|
||||||
|
import com.example.mtusi_messenger.service.AuthenticationService;
|
||||||
|
import com.example.mtusi_messenger.web.ReadinessGuardFilter;
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import org.springframework.boot.web.servlet.FilterRegistrationBean;
|
||||||
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.beans.factory.annotation.Qualifier;
|
||||||
|
import org.springframework.http.HttpMethod;
|
||||||
|
import org.springframework.security.authentication.AuthenticationManager;
|
||||||
|
import org.springframework.security.authentication.ProviderManager;
|
||||||
|
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
||||||
|
import org.springframework.security.config.http.SessionCreationPolicy;
|
||||||
|
import org.springframework.security.web.SecurityFilterChain;
|
||||||
|
import org.springframework.security.web.authentication.AnonymousAuthenticationFilter;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
|
||||||
|
public class SecurityConfig {
|
||||||
|
@Bean
|
||||||
|
AuthenticationManager authenticationManager(DatabaseAuthenticationProvider provider) {
|
||||||
|
return new ProviderManager(provider);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
RestAuthenticationEntryPoint basicAuthenticationEntryPoint(ObjectMapper mapper) {
|
||||||
|
return new RestAuthenticationEntryPoint(
|
||||||
|
mapper,
|
||||||
|
"Basic realm=\"messenger\", charset=\"UTF-8\"");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
RestAuthenticationEntryPoint bearerAuthenticationEntryPoint(ObjectMapper mapper) {
|
||||||
|
return new RestAuthenticationEntryPoint(mapper, "Bearer realm=\"messenger\"");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
ApiAuthenticationFilter apiAuthenticationFilter(
|
||||||
|
AuthenticationManager authenticationManager,
|
||||||
|
AuthenticationService authenticationService,
|
||||||
|
@Qualifier("basicAuthenticationEntryPoint") RestAuthenticationEntryPoint basicAuthenticationEntryPoint,
|
||||||
|
@Qualifier("bearerAuthenticationEntryPoint") RestAuthenticationEntryPoint bearerAuthenticationEntryPoint
|
||||||
|
) {
|
||||||
|
return new ApiAuthenticationFilter(
|
||||||
|
authenticationManager,
|
||||||
|
authenticationService,
|
||||||
|
basicAuthenticationEntryPoint,
|
||||||
|
bearerAuthenticationEntryPoint);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
FilterRegistrationBean<ApiAuthenticationFilter> disableServletFilterRegistration(
|
||||||
|
ApiAuthenticationFilter filter
|
||||||
|
) {
|
||||||
|
var registration = new FilterRegistrationBean<>(filter);
|
||||||
|
registration.setEnabled(false);
|
||||||
|
return registration;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
FilterRegistrationBean<ReadinessGuardFilter> disableReadinessGuardServletRegistration(
|
||||||
|
ReadinessGuardFilter filter
|
||||||
|
) {
|
||||||
|
var registration = new FilterRegistrationBean<>(filter);
|
||||||
|
registration.setEnabled(false);
|
||||||
|
return registration;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
SecurityFilterChain securityFilterChain(
|
||||||
|
HttpSecurity http,
|
||||||
|
ApiAuthenticationFilter apiAuthenticationFilter,
|
||||||
|
ReadinessGuardFilter readinessGuardFilter,
|
||||||
|
@Qualifier("bearerAuthenticationEntryPoint") RestAuthenticationEntryPoint bearerAuthenticationEntryPoint
|
||||||
|
) throws Exception {
|
||||||
|
http
|
||||||
|
.csrf(csrf -> csrf.disable())
|
||||||
|
.formLogin(form -> form.disable())
|
||||||
|
.httpBasic(basic -> basic.disable())
|
||||||
|
.logout(logout -> logout.disable())
|
||||||
|
.requestCache(cache -> cache.disable())
|
||||||
|
.sessionManagement(session -> session
|
||||||
|
.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
|
||||||
|
.exceptionHandling(errors -> errors
|
||||||
|
.authenticationEntryPoint(bearerAuthenticationEntryPoint))
|
||||||
|
.authorizeHttpRequests(authorize -> authorize
|
||||||
|
.requestMatchers(HttpMethod.GET, "/health/**").permitAll()
|
||||||
|
.requestMatchers(
|
||||||
|
"/swagger-ui.html",
|
||||||
|
"/swagger-ui/**",
|
||||||
|
"/v3/api-docs/**"
|
||||||
|
).permitAll()
|
||||||
|
.requestMatchers(HttpMethod.POST, "/api/v1/users").permitAll()
|
||||||
|
.anyRequest().authenticated())
|
||||||
|
// Refuse business traffic before authentication touches storage.
|
||||||
|
// Nginx retries this 503 on another peer and passively excludes
|
||||||
|
// the refusing replica according to max_fails/fail_timeout.
|
||||||
|
.addFilterBefore(readinessGuardFilter, AnonymousAuthenticationFilter.class)
|
||||||
|
.addFilterAfter(apiAuthenticationFilter, ReadinessGuardFilter.class);
|
||||||
|
return http.build();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
package com.example.mtusi_messenger.config;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.databind.DeserializationFeature;
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import com.fasterxml.jackson.databind.SerializationFeature;
|
||||||
|
import com.fasterxml.jackson.databind.json.JsonMapper;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
public class WebConfig {
|
||||||
|
@Bean
|
||||||
|
ObjectMapper objectMapper() {
|
||||||
|
return JsonMapper.builder().findAndAddModules()
|
||||||
|
.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false)
|
||||||
|
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, true)
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,78 @@
|
|||||||
|
package com.example.mtusi_messenger.controller;
|
||||||
|
|
||||||
|
import com.example.mtusi_messenger.dto.AttachmentResponse;
|
||||||
|
import com.example.mtusi_messenger.exception.ApiExceptions;
|
||||||
|
import com.example.mtusi_messenger.model.Attachment;
|
||||||
|
import com.example.mtusi_messenger.security.SessionPrincipal;
|
||||||
|
import com.example.mtusi_messenger.service.AttachmentService;
|
||||||
|
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
|
||||||
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.springframework.http.ContentDisposition;
|
||||||
|
import org.springframework.http.HttpHeaders;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.http.MediaType;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.security.core.annotation.AuthenticationPrincipal;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.PathVariable;
|
||||||
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestHeader;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
import static com.example.mtusi_messenger.service.UuidValidator.parse;
|
||||||
|
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/api/v1")
|
||||||
|
@SecurityRequirement(name = "bearerAuth")
|
||||||
|
public class AttachmentController {
|
||||||
|
private final AttachmentService attachments;
|
||||||
|
|
||||||
|
@PostMapping(value = "/chats/{chatId}/attachments",
|
||||||
|
consumes = {"image/png", "image/jpeg"},
|
||||||
|
produces = MediaType.APPLICATION_JSON_VALUE)
|
||||||
|
public ResponseEntity<AttachmentResponse> upload(
|
||||||
|
@AuthenticationPrincipal SessionPrincipal principal,
|
||||||
|
@PathVariable String chatId,
|
||||||
|
@RequestParam String filename,
|
||||||
|
@RequestHeader(HttpHeaders.CONTENT_TYPE) String contentType,
|
||||||
|
HttpServletRequest request
|
||||||
|
) throws IOException {
|
||||||
|
Attachment attachment = attachments.upload(parse(chatId), principal.user().id(), filename,
|
||||||
|
contentType, request.getContentLengthLong(), request.getInputStream());
|
||||||
|
return ResponseEntity.status(HttpStatus.CREATED).body(AttachmentResponse.from(attachment));
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/attachments/{attachmentId}")
|
||||||
|
public AttachmentResponse metadata(
|
||||||
|
@AuthenticationPrincipal SessionPrincipal principal,
|
||||||
|
@PathVariable String attachmentId
|
||||||
|
) {
|
||||||
|
return AttachmentResponse.from(attachments.getAccessible(parse(attachmentId), principal.user().id()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/attachments/{attachmentId}/content")
|
||||||
|
public ResponseEntity<byte[]> download(
|
||||||
|
@AuthenticationPrincipal SessionPrincipal principal,
|
||||||
|
@PathVariable String attachmentId,
|
||||||
|
@RequestParam(defaultValue = "original") String variant
|
||||||
|
) {
|
||||||
|
if (!"original".equals(variant)) {
|
||||||
|
throw ApiExceptions.invalid("Unsupported attachment variant");
|
||||||
|
}
|
||||||
|
AttachmentService.Download download = attachments.download(parse(attachmentId), principal.user().id());
|
||||||
|
return ResponseEntity.ok()
|
||||||
|
.contentType(MediaType.parseMediaType(download.attachment().contentType()))
|
||||||
|
.contentLength(download.bytes().length)
|
||||||
|
.header(HttpHeaders.CONTENT_DISPOSITION, ContentDisposition.inline()
|
||||||
|
.filename(download.attachment().filename(), StandardCharsets.UTF_8).build().toString())
|
||||||
|
.body(download.bytes());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,83 @@
|
|||||||
|
package com.example.mtusi_messenger.controller;
|
||||||
|
|
||||||
|
import com.example.mtusi_messenger.exception.ApiExceptions;
|
||||||
|
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
|
||||||
|
import jakarta.validation.Valid;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.security.core.annotation.AuthenticationPrincipal;
|
||||||
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
import com.example.mtusi_messenger.dto.*;
|
||||||
|
import com.example.mtusi_messenger.model.Chat;
|
||||||
|
import com.example.mtusi_messenger.service.ChatService;
|
||||||
|
import com.example.mtusi_messenger.security.SessionPrincipal;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
import static com.example.mtusi_messenger.service.UuidValidator.parse;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/api/v1/chats")
|
||||||
|
@SecurityRequirement(name = "bearerAuth")
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class ChatController {
|
||||||
|
|
||||||
|
private final ChatService chats;
|
||||||
|
|
||||||
|
@PostMapping
|
||||||
|
public ResponseEntity<ChatResponse> create(@AuthenticationPrincipal SessionPrincipal principal,
|
||||||
|
@Valid @RequestBody CreateChatRequest body
|
||||||
|
) {
|
||||||
|
UUID userId = principal.user().id();
|
||||||
|
Chat c = chats.create(body, userId);
|
||||||
|
return ResponseEntity.status(HttpStatus.CREATED).body(ChatResponse.from(c));
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping
|
||||||
|
public ChatPageResponse list(@AuthenticationPrincipal SessionPrincipal principal,
|
||||||
|
@RequestParam(required = false) String limit,
|
||||||
|
@RequestParam(required = false) String cursor
|
||||||
|
) {
|
||||||
|
UUID userId = principal.user().id();
|
||||||
|
int parsedLimit = validateLimit(limit);
|
||||||
|
validateCursorPresence(cursor);
|
||||||
|
List<Chat> page = chats.page(userId, parsedLimit, cursor);
|
||||||
|
boolean hasNext = page.size() > parsedLimit;
|
||||||
|
if (hasNext) page = page.subList(0, parsedLimit);
|
||||||
|
return new ChatPageResponse(page.stream().map(ChatResponse::from).toList(), page.isEmpty() ? null :
|
||||||
|
chats.nextCursor(userId, page.get(page.size() - 1), hasNext));
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/{chatId}")
|
||||||
|
public ChatResponse get(@AuthenticationPrincipal SessionPrincipal principal,
|
||||||
|
@PathVariable String chatId
|
||||||
|
) {
|
||||||
|
UUID userId = principal.user().id();
|
||||||
|
return ChatResponse.from(chats.getAccessible(parse(chatId), userId));
|
||||||
|
}
|
||||||
|
|
||||||
|
private static int validateLimit(String limit) {
|
||||||
|
if (limit == null) {
|
||||||
|
return 50;
|
||||||
|
}
|
||||||
|
if (!limit.matches("[0-9]+")) {
|
||||||
|
throw ApiExceptions.invalid("Invalid limit");
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
int value = Integer.parseInt(limit);
|
||||||
|
|
||||||
|
if (value < 1 || value > 100) {
|
||||||
|
throw ApiExceptions.invalid("Invalid limit");
|
||||||
|
}
|
||||||
|
|
||||||
|
return value;
|
||||||
|
} catch (NumberFormatException ex) {
|
||||||
|
throw ApiExceptions.invalid("Invalid limit");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void validateCursorPresence(String c) {
|
||||||
|
if (c != null && c.isBlank()) throw ApiExceptions.invalid("Invalid cursor");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
package com.example.mtusi_messenger.controller;
|
||||||
|
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
import com.example.mtusi_messenger.dto.HealthResponse;
|
||||||
|
import com.example.mtusi_messenger.exception.ApiExceptions;
|
||||||
|
import com.example.mtusi_messenger.service.ReadinessService;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
public class HealthController {
|
||||||
|
private final ReadinessService readiness;
|
||||||
|
|
||||||
|
public HealthController(ReadinessService readiness) {
|
||||||
|
this.readiness = readiness;
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/health/live")
|
||||||
|
public ResponseEntity<HealthResponse> live() {
|
||||||
|
return ResponseEntity.ok(new HealthResponse("ok"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/health/ready")
|
||||||
|
public ResponseEntity<HealthResponse> ready() {
|
||||||
|
// Always probe dependencies, even after a failure, so a restored schema
|
||||||
|
// can move this process back to ACCEPTING_TRAFFIC without a restart.
|
||||||
|
if (!readiness.probeDependencies() || !readiness.acceptsBusinessTraffic()) {
|
||||||
|
throw ApiExceptions.unavailable("Application is not accepting traffic");
|
||||||
|
}
|
||||||
|
return ResponseEntity.ok(new HealthResponse("ok"));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,79 @@
|
|||||||
|
package com.example.mtusi_messenger.controller;
|
||||||
|
|
||||||
|
import com.example.mtusi_messenger.exception.ApiExceptions;
|
||||||
|
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
|
||||||
|
import jakarta.validation.Valid;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.security.core.annotation.AuthenticationPrincipal;
|
||||||
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
import com.example.mtusi_messenger.dto.*;
|
||||||
|
import com.example.mtusi_messenger.model.Message;
|
||||||
|
import com.example.mtusi_messenger.service.MessageService;
|
||||||
|
import com.example.mtusi_messenger.security.SessionPrincipal;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
import static com.example.mtusi_messenger.service.UuidValidator.parse;
|
||||||
|
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/api/v1/chats/{chatId}/messages")
|
||||||
|
@SecurityRequirement(name = "bearerAuth")
|
||||||
|
public class MessageController {
|
||||||
|
private static final String IDEMPOTENCY_KEY_PATTERN = "[A-Za-z0-9._:-]{1,128}";
|
||||||
|
private final MessageService messages;
|
||||||
|
|
||||||
|
@PostMapping
|
||||||
|
public ResponseEntity<MessageResponse> create(@AuthenticationPrincipal SessionPrincipal principal,
|
||||||
|
@PathVariable String chatId,
|
||||||
|
@RequestHeader(name = "Idempotency-Key", required = false) String idempotencyKey,
|
||||||
|
@Valid @RequestBody CreateMessageRequest body
|
||||||
|
) {
|
||||||
|
if (idempotencyKey == null || !idempotencyKey.matches(IDEMPOTENCY_KEY_PATTERN)) {
|
||||||
|
throw ApiExceptions.invalid("Invalid or missing Idempotency-Key");
|
||||||
|
}
|
||||||
|
UUID userId = principal.user().id();
|
||||||
|
UUID parsedChatId = parse(chatId);
|
||||||
|
Message m = messages.create(parsedChatId, userId, body, idempotencyKey);
|
||||||
|
return ResponseEntity.status(HttpStatus.CREATED).body(MessageResponse.from(m));
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping
|
||||||
|
public MessagePageResponse list(@AuthenticationPrincipal SessionPrincipal principal,
|
||||||
|
@PathVariable String chatId,
|
||||||
|
@RequestParam(required = false) String limit,
|
||||||
|
@RequestParam(required=false) String cursor
|
||||||
|
) {
|
||||||
|
UUID userId = principal.user().id();
|
||||||
|
UUID parsedChatId = parse(chatId);
|
||||||
|
int parsedLimit = validateLimit(limit);
|
||||||
|
if (cursor != null && cursor.isBlank()) throw ApiExceptions.invalid("Invalid cursor");
|
||||||
|
List<Message> page = messages.page(parsedChatId, userId, parsedLimit, cursor);
|
||||||
|
boolean hasNext = page.size() > parsedLimit; if (hasNext) page = page.subList(0, parsedLimit);
|
||||||
|
return new MessagePageResponse(page.stream().map(MessageResponse::from).toList(),
|
||||||
|
page.isEmpty() ? null : messages.nextCursor(parsedChatId, page.get(page.size()-1), hasNext));
|
||||||
|
}
|
||||||
|
|
||||||
|
private static int validateLimit(String limit) {
|
||||||
|
if (limit == null) {
|
||||||
|
return 50;
|
||||||
|
}
|
||||||
|
if (!limit.matches("[0-9]+")) {
|
||||||
|
throw ApiExceptions.invalid("Invalid limit");
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
int value = Integer.parseInt(limit);
|
||||||
|
|
||||||
|
if (value < 1 || value > 100) {
|
||||||
|
throw ApiExceptions.invalid("Invalid limit");
|
||||||
|
}
|
||||||
|
|
||||||
|
return value;
|
||||||
|
} catch (NumberFormatException ex) {
|
||||||
|
throw ApiExceptions.invalid("Invalid limit");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,41 @@
|
|||||||
|
package com.example.mtusi_messenger.controller;
|
||||||
|
|
||||||
|
import com.example.mtusi_messenger.dto.SessionResponse;
|
||||||
|
import com.example.mtusi_messenger.dto.UserResponse;
|
||||||
|
import com.example.mtusi_messenger.service.AuthenticationService;
|
||||||
|
import com.example.mtusi_messenger.model.User;
|
||||||
|
import com.example.mtusi_messenger.security.SessionPrincipal;
|
||||||
|
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.security.core.annotation.AuthenticationPrincipal;
|
||||||
|
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||||
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/api/v1/auth/sessions")
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class SessionController {
|
||||||
|
private final AuthenticationService authentication;
|
||||||
|
|
||||||
|
@PostMapping
|
||||||
|
@SecurityRequirement(name = "basicAuth")
|
||||||
|
public ResponseEntity<SessionResponse> create(@AuthenticationPrincipal User user) {
|
||||||
|
var issued = authentication.issueSession(user);
|
||||||
|
return ResponseEntity.status(HttpStatus.CREATED).body(new SessionResponse(
|
||||||
|
issued.token(),
|
||||||
|
"Bearer",
|
||||||
|
issued.expiresAt(),
|
||||||
|
UserResponse.from(issued.user())));
|
||||||
|
}
|
||||||
|
|
||||||
|
@DeleteMapping("/current")
|
||||||
|
@SecurityRequirement(name = "bearerAuth")
|
||||||
|
public ResponseEntity<Void> revoke(@AuthenticationPrincipal SessionPrincipal principal) {
|
||||||
|
authentication.revoke(principal);
|
||||||
|
return ResponseEntity.noContent().build();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
package com.example.mtusi_messenger.controller;
|
||||||
|
|
||||||
|
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
|
||||||
|
import jakarta.validation.Valid;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.security.core.annotation.AuthenticationPrincipal;
|
||||||
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
import com.example.mtusi_messenger.dto.*;
|
||||||
|
import com.example.mtusi_messenger.service.UserService;
|
||||||
|
import com.example.mtusi_messenger.security.SessionPrincipal;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/api/v1/users")
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class UserController {
|
||||||
|
|
||||||
|
private final UserService users;
|
||||||
|
|
||||||
|
@PostMapping
|
||||||
|
public ResponseEntity<UserResponse> create(@Valid @RequestBody CreateUserRequest request) {
|
||||||
|
var user = users.create(request);
|
||||||
|
return ResponseEntity.status(HttpStatus.CREATED).body(UserResponse.from(user));
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/me")
|
||||||
|
@SecurityRequirement(name = "bearerAuth")
|
||||||
|
public UserResponse me(@AuthenticationPrincipal SessionPrincipal principal) {
|
||||||
|
return UserResponse.from(principal.user());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
package com.example.mtusi_messenger.dto;
|
||||||
|
|
||||||
|
import com.example.mtusi_messenger.model.Attachment;
|
||||||
|
|
||||||
|
import java.time.Instant;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
public record AttachmentResponse(UUID id,
|
||||||
|
UUID chat_id,
|
||||||
|
UUID owner_id,
|
||||||
|
String filename,
|
||||||
|
String content_type,
|
||||||
|
long size_bytes,
|
||||||
|
String sha256,
|
||||||
|
String status,
|
||||||
|
List<Object> variants,
|
||||||
|
String error_code,
|
||||||
|
Instant created_at,
|
||||||
|
Instant updated_at) {
|
||||||
|
public static AttachmentResponse from(Attachment attachment) {
|
||||||
|
return new AttachmentResponse(
|
||||||
|
attachment.id(),
|
||||||
|
attachment.chatId(),
|
||||||
|
attachment.ownerId(),
|
||||||
|
attachment.filename(),
|
||||||
|
attachment.contentType(),
|
||||||
|
attachment.sizeBytes(),
|
||||||
|
attachment.sha256(),
|
||||||
|
"ready",
|
||||||
|
List.of(),
|
||||||
|
null,
|
||||||
|
attachment.createdAt(),
|
||||||
|
attachment.updatedAt());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
package com.example.mtusi_messenger.dto;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public record ChatPageResponse(
|
||||||
|
List<ChatResponse> items,
|
||||||
|
String next_cursor
|
||||||
|
) {}
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
package com.example.mtusi_messenger.dto;
|
||||||
|
|
||||||
|
import com.example.mtusi_messenger.model.Chat;
|
||||||
|
import java.time.Instant;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
public record ChatResponse(UUID id,
|
||||||
|
String title,
|
||||||
|
List<UUID> member_ids,
|
||||||
|
Instant created_at) {
|
||||||
|
public static ChatResponse from(Chat c) {
|
||||||
|
return new ChatResponse(
|
||||||
|
c.id(),
|
||||||
|
c.title(),
|
||||||
|
c.memberIds(),
|
||||||
|
c.createdAt()); }
|
||||||
|
}
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
package com.example.mtusi_messenger.dto;
|
||||||
|
|
||||||
|
import com.example.mtusi_messenger.web.NotUnicodeBlank;
|
||||||
|
import com.example.mtusi_messenger.web.UnicodeSize;
|
||||||
|
import jakarta.validation.constraints.NotBlank;
|
||||||
|
import jakarta.validation.constraints.NotEmpty;
|
||||||
|
import jakarta.validation.constraints.Size;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
public record CreateChatRequest(
|
||||||
|
@NotUnicodeBlank
|
||||||
|
@UnicodeSize(min = 1, max = 100)
|
||||||
|
String title,
|
||||||
|
|
||||||
|
@NotEmpty
|
||||||
|
@Size(min = 1, max = 100)
|
||||||
|
List<UUID> member_ids
|
||||||
|
) {}
|
||||||
@@ -0,0 +1,31 @@
|
|||||||
|
package com.example.mtusi_messenger.dto;
|
||||||
|
|
||||||
|
import com.example.mtusi_messenger.web.UnicodeSize;
|
||||||
|
import jakarta.validation.constraints.AssertTrue;
|
||||||
|
import jakarta.validation.constraints.NotNull;
|
||||||
|
import jakarta.validation.constraints.Size;
|
||||||
|
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
public record CreateMessageRequest(
|
||||||
|
@NotNull @UnicodeSize(max = 4000) String text,
|
||||||
|
@Size(max = 10) List<UUID> attachment_ids
|
||||||
|
) {
|
||||||
|
public CreateMessageRequest {
|
||||||
|
attachment_ids = attachment_ids == null ? List.of() : List.copyOf(attachment_ids);
|
||||||
|
}
|
||||||
|
|
||||||
|
@AssertTrue(message = "text or attachment_ids must be present")
|
||||||
|
public boolean isContentPresent() {
|
||||||
|
return text != null && (!text.isBlank() || !attachment_ids.isEmpty());
|
||||||
|
}
|
||||||
|
|
||||||
|
@AssertTrue(message = "attachment_ids must be unique and non-null")
|
||||||
|
public boolean isAttachmentIdsValid() {
|
||||||
|
return attachment_ids != null
|
||||||
|
&& attachment_ids.stream().noneMatch(java.util.Objects::isNull)
|
||||||
|
&& new HashSet<>(attachment_ids).size() == attachment_ids.size();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
package com.example.mtusi_messenger.dto;
|
||||||
|
|
||||||
|
import com.example.mtusi_messenger.web.NotUnicodeBlank;
|
||||||
|
import com.example.mtusi_messenger.web.UnicodeSize;
|
||||||
|
import jakarta.validation.constraints.NotBlank;
|
||||||
|
import jakarta.validation.constraints.Pattern;
|
||||||
|
|
||||||
|
public record CreateUserRequest(
|
||||||
|
@NotBlank
|
||||||
|
@Pattern(regexp = "[a-z0-9_]{3,32}")
|
||||||
|
String username,
|
||||||
|
|
||||||
|
@NotUnicodeBlank
|
||||||
|
@UnicodeSize(min = 1, max = 100)
|
||||||
|
String display_name,
|
||||||
|
|
||||||
|
@NotUnicodeBlank
|
||||||
|
@UnicodeSize(min = 12, max = 128)
|
||||||
|
String password
|
||||||
|
) {}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
package com.example.mtusi_messenger.dto;
|
||||||
|
|
||||||
|
public record ErrorResponse(ErrorBody error) {
|
||||||
|
public record ErrorBody(String code,
|
||||||
|
String message,
|
||||||
|
String request_id)
|
||||||
|
{}
|
||||||
|
}
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
package com.example.mtusi_messenger.dto;
|
||||||
|
|
||||||
|
public record HealthResponse(String status) {}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
package com.example.mtusi_messenger.dto;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public record MessagePageResponse(
|
||||||
|
List<MessageResponse> items,
|
||||||
|
String next_cursor
|
||||||
|
) {}
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
package com.example.mtusi_messenger.dto;
|
||||||
|
|
||||||
|
import com.example.mtusi_messenger.model.Message;
|
||||||
|
import java.time.Instant;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
public record MessageResponse(UUID id,
|
||||||
|
UUID chat_id,
|
||||||
|
UUID sender_id,
|
||||||
|
String text,
|
||||||
|
Instant created_at,
|
||||||
|
List<UUID> attachment_ids) {
|
||||||
|
public static MessageResponse from(Message m) {
|
||||||
|
return new MessageResponse(m.id(),
|
||||||
|
m.chatId(),
|
||||||
|
m.senderId(),
|
||||||
|
m.text(),
|
||||||
|
m.createdAt(),
|
||||||
|
m.attachmentIds());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
package com.example.mtusi_messenger.dto;
|
||||||
|
|
||||||
|
import java.time.Instant;
|
||||||
|
|
||||||
|
public record SessionResponse(
|
||||||
|
String token,
|
||||||
|
String token_type,
|
||||||
|
Instant expires_at,
|
||||||
|
UserResponse user
|
||||||
|
) {}
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
package com.example.mtusi_messenger.dto;
|
||||||
|
|
||||||
|
import com.example.mtusi_messenger.model.User;
|
||||||
|
import java.time.Instant;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
public record UserResponse(UUID id,
|
||||||
|
String username,
|
||||||
|
String display_name,
|
||||||
|
Instant created_at) {
|
||||||
|
public static UserResponse from(User u) {
|
||||||
|
return new UserResponse(
|
||||||
|
u.id(),
|
||||||
|
u.username(),
|
||||||
|
u.displayName(),
|
||||||
|
u.createdAt());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
package com.example.mtusi_messenger.exception;
|
||||||
|
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
|
||||||
|
public class ApiException extends RuntimeException {
|
||||||
|
private final String code; private final HttpStatus status; private final String authenticate;
|
||||||
|
public ApiException(String code, HttpStatus status, String message) { this(code, status, message, null); }
|
||||||
|
public ApiException(String code, HttpStatus status, String message, String authenticate) {
|
||||||
|
super(message); this.code = code; this.status = status; this.authenticate = authenticate;
|
||||||
|
}
|
||||||
|
public String code() { return code; }
|
||||||
|
public HttpStatus status() { return status; }
|
||||||
|
public String authenticate() { return authenticate; }
|
||||||
|
}
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
package com.example.mtusi_messenger.exception;
|
||||||
|
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
|
||||||
|
public final class ApiExceptions {
|
||||||
|
private ApiExceptions() {}
|
||||||
|
public static ApiException invalid(String m) { return new ApiException("invalid_request", HttpStatus.BAD_REQUEST, m); }
|
||||||
|
public static ApiException unauthorized(String m) { return unauthorizedBearer(m); }
|
||||||
|
public static ApiException unauthorizedBasic(String m) { return new ApiException("unauthorized", HttpStatus.UNAUTHORIZED, m, "Basic realm=\"messenger\", charset=\"UTF-8\""); }
|
||||||
|
public static ApiException unauthorizedBearer(String m) { return new ApiException("unauthorized", HttpStatus.UNAUTHORIZED, m, "Bearer realm=\"messenger\""); }
|
||||||
|
public static ApiException notFound(String m) { return new ApiException("not_found", HttpStatus.NOT_FOUND, m); }
|
||||||
|
public static ApiException conflict(String m) { return new ApiException("conflict", HttpStatus.CONFLICT, m); }
|
||||||
|
public static ApiException payloadTooLarge(String m) { return new ApiException("payload_too_large", HttpStatus.PAYLOAD_TOO_LARGE, m); }
|
||||||
|
public static ApiException unsupportedMediaType(String m) { return new ApiException("unsupported_media_type", HttpStatus.UNSUPPORTED_MEDIA_TYPE, m); }
|
||||||
|
public static ApiException tooManyRequests(String m) { return new ApiException("too_many_requests", HttpStatus.TOO_MANY_REQUESTS, m); }
|
||||||
|
public static ApiException unavailable(String m) { return new ApiException("unavailable", HttpStatus.SERVICE_UNAVAILABLE, m); }
|
||||||
|
}
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
package com.example.mtusi_messenger.model;
|
||||||
|
|
||||||
|
import java.time.Instant;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
public record Attachment(UUID id,
|
||||||
|
UUID chatId,
|
||||||
|
UUID ownerId,
|
||||||
|
String filename,
|
||||||
|
String contentType,
|
||||||
|
long sizeBytes,
|
||||||
|
String sha256,
|
||||||
|
String objectKey,
|
||||||
|
Instant createdAt,
|
||||||
|
Instant updatedAt) {
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
package com.example.mtusi_messenger.model;
|
||||||
|
|
||||||
|
import java.time.Instant;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
public record Chat(UUID id, String title, List<UUID> memberIds, Instant createdAt) {
|
||||||
|
public Chat {
|
||||||
|
memberIds = List.copyOf(memberIds);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
package com.example.mtusi_messenger.model;
|
||||||
|
|
||||||
|
public record IdempotentMessage(byte[] requestHash, Message message) {}
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
package com.example.mtusi_messenger.model;
|
||||||
|
|
||||||
|
import java.time.Instant;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
public record Message(UUID id,
|
||||||
|
UUID chatId,
|
||||||
|
UUID senderId,
|
||||||
|
String text,
|
||||||
|
Instant createdAt,
|
||||||
|
List<UUID> attachmentIds) {
|
||||||
|
public Message {
|
||||||
|
attachmentIds = attachmentIds == null ? List.of() : List.copyOf(attachmentIds);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,35 @@
|
|||||||
|
package com.example.mtusi_messenger.model;
|
||||||
|
|
||||||
|
import java.time.Instant;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
public record MessageKey(Instant createdAt, UUID id)
|
||||||
|
implements Comparable<MessageKey> {
|
||||||
|
|
||||||
|
private static int compareUuidV7(UUID a, UUID b) {
|
||||||
|
int result = Long.compareUnsigned(
|
||||||
|
a.getMostSignificantBits(),
|
||||||
|
b.getMostSignificantBits()
|
||||||
|
);
|
||||||
|
|
||||||
|
if (result != 0) {
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
return Long.compareUnsigned(
|
||||||
|
a.getLeastSignificantBits(),
|
||||||
|
b.getLeastSignificantBits()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int compareTo(MessageKey other) {
|
||||||
|
int result = createdAt.compareTo(other.createdAt);
|
||||||
|
|
||||||
|
if (result != 0) {
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
return compareUuidV7(id, other.id);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
package com.example.mtusi_messenger.model;
|
||||||
|
|
||||||
|
import java.time.Instant;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
public record Session(UUID id, UUID userId, byte[] tokenHash, Instant createdAt, Instant expiresAt) {
|
||||||
|
public Session {
|
||||||
|
tokenHash = tokenHash.clone();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public byte[] tokenHash() {
|
||||||
|
return tokenHash.clone();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
package com.example.mtusi_messenger.model;
|
||||||
|
|
||||||
|
import java.time.Instant;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
public record User(UUID id, String username, String displayName, Instant createdAt) {}
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
package com.example.mtusi_messenger.repository;
|
||||||
|
|
||||||
|
import com.example.mtusi_messenger.model.Attachment;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.UUID;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
public interface AttachmentRepository {
|
||||||
|
Attachment save(Attachment attachment);
|
||||||
|
Optional<Attachment> findById(UUID id);
|
||||||
|
List<Attachment> findAllByIdsInChat(UUID chatId, List<UUID> ids);
|
||||||
|
Set<String> findAllObjectKeys();
|
||||||
|
}
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
package com.example.mtusi_messenger.repository;
|
||||||
|
|
||||||
|
import com.example.mtusi_messenger.model.Chat;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
public interface ChatRepository {
|
||||||
|
Chat save(Chat chat);
|
||||||
|
Optional<Chat> findById(UUID id);
|
||||||
|
boolean isMember(UUID chatId, UUID userId);
|
||||||
|
List<Chat> findAllForUser(UUID userId);
|
||||||
|
List<Chat> findPageForUser(UUID userId, UUID afterId, java.time.Instant afterCreatedAt, int limitPlusOne);
|
||||||
|
}
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
package com.example.mtusi_messenger.repository;
|
||||||
|
|
||||||
|
import com.example.mtusi_messenger.model.Message;
|
||||||
|
import java.time.Instant;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
public interface MessageRepository {
|
||||||
|
Message save(Message message);
|
||||||
|
void lockIdempotencyScope(UUID userId, String method, String path, String key);
|
||||||
|
void deleteExpiredIdempotency(UUID userId, String method, String path, String key, Instant expiresBefore);
|
||||||
|
Optional<com.example.mtusi_messenger.model.IdempotentMessage> findIdempotent(
|
||||||
|
UUID userId, String method, String path, String key);
|
||||||
|
void saveIdempotency(UUID userId, String method, String path, String key,
|
||||||
|
byte[] requestHash, UUID messageId, Instant createdAt);
|
||||||
|
List<Message> findPage(UUID chatId, Instant afterCreatedAt, UUID afterId, int limitPlusOne);
|
||||||
|
}
|
||||||
+82
@@ -0,0 +1,82 @@
|
|||||||
|
package com.example.mtusi_messenger.repository;
|
||||||
|
|
||||||
|
import com.example.mtusi_messenger.model.Attachment;
|
||||||
|
import org.springframework.jdbc.core.JdbcTemplate;
|
||||||
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
import java.sql.Timestamp;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.UUID;
|
||||||
|
import java.util.Set;
|
||||||
|
import java.util.HashSet;
|
||||||
|
|
||||||
|
@Repository
|
||||||
|
public class PostgresAttachmentRepository implements AttachmentRepository {
|
||||||
|
private static final String COLUMNS = """
|
||||||
|
id, chat_id, owner_id, filename, content_type, size_bytes,
|
||||||
|
sha256, object_key, created_at, updated_at
|
||||||
|
""";
|
||||||
|
private final JdbcTemplate jdbc;
|
||||||
|
|
||||||
|
public PostgresAttachmentRepository(JdbcTemplate jdbc) {
|
||||||
|
this.jdbc = jdbc;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Attachment save(Attachment attachment) {
|
||||||
|
jdbc.update("""
|
||||||
|
INSERT INTO attachments
|
||||||
|
(id, chat_id, owner_id, filename, content_type, size_bytes,
|
||||||
|
sha256, object_key, status, created_at, updated_at)
|
||||||
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, 'ready', ?, ?)
|
||||||
|
""", attachment.id(), attachment.chatId(), attachment.ownerId(),
|
||||||
|
attachment.filename(), attachment.contentType(), attachment.sizeBytes(),
|
||||||
|
attachment.sha256(), attachment.objectKey(), Timestamp.from(attachment.createdAt()),
|
||||||
|
Timestamp.from(attachment.updatedAt()));
|
||||||
|
return attachment;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Optional<Attachment> findById(UUID id) {
|
||||||
|
return jdbc.query("SELECT " + COLUMNS + " FROM attachments WHERE id = ?",
|
||||||
|
(rs, rowNum) -> map(rs), id).stream().findFirst();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<Attachment> findAllByIdsInChat(UUID chatId, List<UUID> ids) {
|
||||||
|
if (ids.isEmpty()) {
|
||||||
|
return List.of();
|
||||||
|
}
|
||||||
|
String placeholders = String.join(",", Collections.nCopies(ids.size(), "?"));
|
||||||
|
Object[] args = new Object[ids.size() + 1];
|
||||||
|
args[0] = chatId;
|
||||||
|
for (int i = 0; i < ids.size(); i++) {
|
||||||
|
args[i + 1] = ids.get(i);
|
||||||
|
}
|
||||||
|
return jdbc.query("SELECT " + COLUMNS + " FROM attachments WHERE chat_id = ? AND id IN ("
|
||||||
|
+ placeholders + ") AND status = 'ready'",
|
||||||
|
(rs, rowNum) -> map(rs), args);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Set<String> findAllObjectKeys() {
|
||||||
|
return new HashSet<>(jdbc.query("SELECT object_key FROM attachments",
|
||||||
|
(rs, rowNum) -> rs.getString("object_key")));
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Attachment map(java.sql.ResultSet rs) throws java.sql.SQLException {
|
||||||
|
return new Attachment(
|
||||||
|
rs.getObject("id", UUID.class),
|
||||||
|
rs.getObject("chat_id", UUID.class),
|
||||||
|
rs.getObject("owner_id", UUID.class),
|
||||||
|
rs.getString("filename"),
|
||||||
|
rs.getString("content_type"),
|
||||||
|
rs.getLong("size_bytes"),
|
||||||
|
rs.getString("sha256"),
|
||||||
|
rs.getString("object_key"),
|
||||||
|
rs.getTimestamp("created_at").toInstant(),
|
||||||
|
rs.getTimestamp("updated_at").toInstant());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,106 @@
|
|||||||
|
package com.example.mtusi_messenger.repository;
|
||||||
|
|
||||||
|
import com.example.mtusi_messenger.model.Chat;
|
||||||
|
import org.springframework.jdbc.core.JdbcTemplate;
|
||||||
|
import org.springframework.stereotype.Repository;
|
||||||
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
|
import java.sql.Timestamp;
|
||||||
|
import java.time.Instant;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
@Repository
|
||||||
|
public class PostgresChatRepository implements ChatRepository {
|
||||||
|
private final JdbcTemplate jdbc;
|
||||||
|
|
||||||
|
public PostgresChatRepository(JdbcTemplate jdbc) {
|
||||||
|
this.jdbc = jdbc;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@Transactional(timeout = 5)
|
||||||
|
public Chat save(Chat chat) {
|
||||||
|
jdbc.update("""
|
||||||
|
INSERT INTO chats (id, title, created_at)
|
||||||
|
VALUES (?, ?, ?)
|
||||||
|
""", chat.id(), chat.title(), Timestamp.from(chat.createdAt()));
|
||||||
|
|
||||||
|
for (UUID memberId : chat.memberIds()) {
|
||||||
|
jdbc.update("""
|
||||||
|
INSERT INTO chat_members (chat_id, user_id)
|
||||||
|
VALUES (?, ?)
|
||||||
|
""", chat.id(), memberId);
|
||||||
|
}
|
||||||
|
return chat;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Optional<Chat> findById(UUID id) {
|
||||||
|
return jdbc.query("""
|
||||||
|
SELECT id, title, created_at
|
||||||
|
FROM chats
|
||||||
|
WHERE id = ?
|
||||||
|
""", (rs, rowNum) -> new ChatRow(
|
||||||
|
rs.getObject("id", UUID.class),
|
||||||
|
rs.getString("title"),
|
||||||
|
rs.getTimestamp("created_at").toInstant()), id)
|
||||||
|
.stream().findFirst()
|
||||||
|
.map(this::toChat);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isMember(UUID chatId, UUID userId) {
|
||||||
|
return Boolean.TRUE.equals(jdbc.queryForObject("""
|
||||||
|
SELECT EXISTS (
|
||||||
|
SELECT 1 FROM chat_members
|
||||||
|
WHERE chat_id = ? AND user_id = ?
|
||||||
|
)
|
||||||
|
""", Boolean.class, chatId, userId));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<Chat> findAllForUser(UUID userId) {
|
||||||
|
return findPageForUser(userId, null, null, Integer.MAX_VALUE);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<Chat> findPageForUser(UUID userId, UUID afterId, Instant afterCreatedAt, int limitPlusOne) {
|
||||||
|
StringBuilder sql = new StringBuilder("""
|
||||||
|
SELECT c.id, c.title, c.created_at
|
||||||
|
FROM chats c
|
||||||
|
JOIN chat_members cm ON cm.chat_id = c.id
|
||||||
|
WHERE cm.user_id = ?
|
||||||
|
""");
|
||||||
|
List<Object> parameters = new ArrayList<>();
|
||||||
|
parameters.add(userId);
|
||||||
|
|
||||||
|
if (afterCreatedAt != null) {
|
||||||
|
sql.append(" AND (c.created_at, c.id) > (?, ?) ");
|
||||||
|
parameters.add(Timestamp.from(afterCreatedAt));
|
||||||
|
parameters.add(afterId);
|
||||||
|
}
|
||||||
|
sql.append(" ORDER BY c.created_at, c.id LIMIT ? ");
|
||||||
|
parameters.add(limitPlusOne);
|
||||||
|
|
||||||
|
return jdbc.query(sql.toString(), (rs, rowNum) -> new ChatRow(
|
||||||
|
rs.getObject("id", UUID.class),
|
||||||
|
rs.getString("title"),
|
||||||
|
rs.getTimestamp("created_at").toInstant()), parameters.toArray())
|
||||||
|
.stream().map(this::toChat).toList();
|
||||||
|
}
|
||||||
|
|
||||||
|
private Chat toChat(ChatRow row) {
|
||||||
|
List<UUID> members = jdbc.queryForList("""
|
||||||
|
SELECT user_id
|
||||||
|
FROM chat_members
|
||||||
|
WHERE chat_id = ?
|
||||||
|
ORDER BY joined_at, user_id
|
||||||
|
""", UUID.class, row.id());
|
||||||
|
return new Chat(row.id(), row.title(), members, row.createdAt());
|
||||||
|
}
|
||||||
|
|
||||||
|
private record ChatRow(UUID id, String title, Instant createdAt) {}
|
||||||
|
}
|
||||||
@@ -0,0 +1,172 @@
|
|||||||
|
package com.example.mtusi_messenger.repository;
|
||||||
|
|
||||||
|
import com.example.mtusi_messenger.model.Message;
|
||||||
|
import com.example.mtusi_messenger.model.IdempotentMessage;
|
||||||
|
import org.springframework.jdbc.core.JdbcTemplate;
|
||||||
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
import java.sql.Timestamp;
|
||||||
|
import java.time.Instant;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.UUID;
|
||||||
|
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
|
||||||
|
import java.sql.PreparedStatement;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
|
||||||
|
@Repository
|
||||||
|
public class PostgresMessageRepository implements MessageRepository {
|
||||||
|
private final JdbcTemplate jdbc;
|
||||||
|
|
||||||
|
public PostgresMessageRepository(JdbcTemplate jdbc) {
|
||||||
|
this.jdbc = jdbc;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Message save(Message message) {
|
||||||
|
jdbc.update("""
|
||||||
|
INSERT INTO messages (id, chat_id, sender_id, text, created_at)
|
||||||
|
VALUES (?, ?, ?, ?, ?)
|
||||||
|
""", message.id(), message.chatId(), message.senderId(), message.text(),
|
||||||
|
Timestamp.from(message.createdAt()));
|
||||||
|
if (!message.attachmentIds().isEmpty()) {
|
||||||
|
jdbc.batchUpdate("""
|
||||||
|
INSERT INTO message_attachments
|
||||||
|
(message_id, chat_id, attachment_id, position)
|
||||||
|
VALUES (?, ?, ?, ?)
|
||||||
|
""", new BatchPreparedStatementSetter() {
|
||||||
|
@Override
|
||||||
|
public void setValues(PreparedStatement statement, int index) throws SQLException {
|
||||||
|
statement.setObject(1, message.id());
|
||||||
|
statement.setObject(2, message.chatId());
|
||||||
|
statement.setObject(3, message.attachmentIds().get(index));
|
||||||
|
statement.setInt(4, index);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getBatchSize() {
|
||||||
|
return message.attachmentIds().size();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return message;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void lockIdempotencyScope(UUID userId, String method, String path, String key) {
|
||||||
|
String scope = userId + "\n" + method + "\n" + path + "\n" + key;
|
||||||
|
jdbc.query(
|
||||||
|
"SELECT pg_advisory_xact_lock(hashtextextended(?, 0))",
|
||||||
|
rs -> {
|
||||||
|
rs.next();
|
||||||
|
return null;
|
||||||
|
},
|
||||||
|
scope);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void deleteExpiredIdempotency(
|
||||||
|
UUID userId,
|
||||||
|
String method,
|
||||||
|
String path,
|
||||||
|
String key,
|
||||||
|
Instant expiresBefore
|
||||||
|
) {
|
||||||
|
jdbc.update("""
|
||||||
|
DELETE FROM message_idempotency
|
||||||
|
WHERE user_id = ?
|
||||||
|
AND method = ?
|
||||||
|
AND request_path = ?
|
||||||
|
AND idempotency_key = ?
|
||||||
|
AND created_at < ?
|
||||||
|
""", userId, method, path, key, Timestamp.from(expiresBefore));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Optional<IdempotentMessage> findIdempotent(
|
||||||
|
UUID userId,
|
||||||
|
String method,
|
||||||
|
String path,
|
||||||
|
String key
|
||||||
|
) {
|
||||||
|
Optional<IdempotentMessage> result = jdbc.query("""
|
||||||
|
SELECT i.request_hash,
|
||||||
|
m.id, m.chat_id, m.sender_id, m.text, m.created_at
|
||||||
|
FROM message_idempotency i
|
||||||
|
JOIN messages m ON m.id = i.message_id
|
||||||
|
WHERE i.user_id = ?
|
||||||
|
AND i.method = ?
|
||||||
|
AND i.request_path = ?
|
||||||
|
AND i.idempotency_key = ?
|
||||||
|
""", (rs, rowNum) -> new IdempotentMessage(
|
||||||
|
rs.getBytes("request_hash"),
|
||||||
|
new Message(
|
||||||
|
rs.getObject("id", UUID.class),
|
||||||
|
rs.getObject("chat_id", UUID.class),
|
||||||
|
rs.getObject("sender_id", UUID.class),
|
||||||
|
rs.getString("text"),
|
||||||
|
rs.getTimestamp("created_at").toInstant(),
|
||||||
|
List.of())),
|
||||||
|
userId, method, path, key).stream().findFirst();
|
||||||
|
return result.map(value -> new IdempotentMessage(
|
||||||
|
value.requestHash(), withAttachments(value.message())));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void saveIdempotency(
|
||||||
|
UUID userId,
|
||||||
|
String method,
|
||||||
|
String path,
|
||||||
|
String key,
|
||||||
|
byte[] requestHash,
|
||||||
|
UUID messageId,
|
||||||
|
Instant createdAt
|
||||||
|
) {
|
||||||
|
jdbc.update("""
|
||||||
|
INSERT INTO message_idempotency
|
||||||
|
(user_id, method, request_path, idempotency_key,
|
||||||
|
request_hash, message_id, created_at)
|
||||||
|
VALUES (?, ?, ?, ?, ?, ?, ?)
|
||||||
|
""", userId, method, path, key, requestHash, messageId,
|
||||||
|
Timestamp.from(createdAt));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<Message> findPage(UUID chatId, Instant afterCreatedAt, UUID afterId, int limitPlusOne) {
|
||||||
|
StringBuilder sql = new StringBuilder("""
|
||||||
|
SELECT id, chat_id, sender_id, text, created_at
|
||||||
|
FROM messages
|
||||||
|
WHERE chat_id = ?
|
||||||
|
""");
|
||||||
|
List<Object> parameters = new ArrayList<>();
|
||||||
|
parameters.add(chatId);
|
||||||
|
|
||||||
|
if (afterCreatedAt != null) {
|
||||||
|
sql.append(" AND (created_at, id) > (?, ?) ");
|
||||||
|
parameters.add(Timestamp.from(afterCreatedAt));
|
||||||
|
parameters.add(afterId);
|
||||||
|
}
|
||||||
|
sql.append(" ORDER BY created_at, id LIMIT ? ");
|
||||||
|
parameters.add(limitPlusOne);
|
||||||
|
|
||||||
|
return jdbc.query(sql.toString(), (rs, rowNum) -> new Message(
|
||||||
|
rs.getObject("id", UUID.class),
|
||||||
|
rs.getObject("chat_id", UUID.class),
|
||||||
|
rs.getObject("sender_id", UUID.class),
|
||||||
|
rs.getString("text"),
|
||||||
|
rs.getTimestamp("created_at").toInstant(),
|
||||||
|
List.of()), parameters.toArray()).stream().map(this::withAttachments).toList();
|
||||||
|
}
|
||||||
|
|
||||||
|
private Message withAttachments(Message message) {
|
||||||
|
List<UUID> attachmentIds = jdbc.query("""
|
||||||
|
SELECT attachment_id
|
||||||
|
FROM message_attachments
|
||||||
|
WHERE message_id = ?
|
||||||
|
ORDER BY position
|
||||||
|
""", (rs, rowNum) -> rs.getObject("attachment_id", UUID.class), message.id());
|
||||||
|
return new Message(message.id(), message.chatId(), message.senderId(), message.text(),
|
||||||
|
message.createdAt(), attachmentIds);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,51 @@
|
|||||||
|
package com.example.mtusi_messenger.repository;
|
||||||
|
|
||||||
|
import com.example.mtusi_messenger.model.Session;
|
||||||
|
import org.springframework.jdbc.core.JdbcTemplate;
|
||||||
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
import java.sql.Timestamp;
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
@Repository
|
||||||
|
public class PostgresSessionRepository implements SessionRepository {
|
||||||
|
private final JdbcTemplate jdbc;
|
||||||
|
|
||||||
|
public PostgresSessionRepository(JdbcTemplate jdbc) {
|
||||||
|
this.jdbc = jdbc;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Session save(Session session) {
|
||||||
|
jdbc.update("""
|
||||||
|
INSERT INTO sessions (id, user_id, token_hash, created_at, expires_at)
|
||||||
|
VALUES (?, ?, ?, ?, ?)
|
||||||
|
""", session.id(), session.userId(), session.tokenHash(),
|
||||||
|
Timestamp.from(session.createdAt()), Timestamp.from(session.expiresAt()));
|
||||||
|
return session;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Optional<UUID> findActiveUserId(byte[] tokenHash) {
|
||||||
|
return jdbc.query("""
|
||||||
|
SELECT user_id
|
||||||
|
FROM sessions
|
||||||
|
WHERE token_hash = ?
|
||||||
|
AND revoked_at IS NULL
|
||||||
|
AND expires_at > CURRENT_TIMESTAMP
|
||||||
|
""", (rs, rowNum) -> rs.getObject("user_id", UUID.class), tokenHash)
|
||||||
|
.stream().findFirst();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean revoke(byte[] tokenHash) {
|
||||||
|
return jdbc.update("""
|
||||||
|
UPDATE sessions
|
||||||
|
SET revoked_at = CURRENT_TIMESTAMP
|
||||||
|
WHERE token_hash = ?
|
||||||
|
AND revoked_at IS NULL
|
||||||
|
AND expires_at > CURRENT_TIMESTAMP
|
||||||
|
""", tokenHash) == 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,72 @@
|
|||||||
|
package com.example.mtusi_messenger.repository;
|
||||||
|
|
||||||
|
import com.example.mtusi_messenger.model.User;
|
||||||
|
import org.springframework.jdbc.core.JdbcTemplate;
|
||||||
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
import java.sql.Timestamp;
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
@Repository
|
||||||
|
public class PostgresUserRepository implements UserRepository {
|
||||||
|
private final JdbcTemplate jdbc;
|
||||||
|
|
||||||
|
public PostgresUserRepository(JdbcTemplate jdbc) {
|
||||||
|
this.jdbc = jdbc;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public User save(User user, String passwordHash) {
|
||||||
|
jdbc.update("""
|
||||||
|
INSERT INTO users (id, username, display_name, password_hash, created_at)
|
||||||
|
VALUES (?, ?, ?, ?, ?)
|
||||||
|
""", user.id(), user.username(), user.displayName(), passwordHash,
|
||||||
|
Timestamp.from(user.createdAt()));
|
||||||
|
return user;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Optional<User> findById(UUID id) {
|
||||||
|
return jdbc.query("""
|
||||||
|
SELECT id, username, display_name, created_at
|
||||||
|
FROM users
|
||||||
|
WHERE id = ?
|
||||||
|
""", (rs, rowNum) -> new User(
|
||||||
|
rs.getObject("id", UUID.class),
|
||||||
|
rs.getString("username"),
|
||||||
|
rs.getString("display_name"),
|
||||||
|
rs.getTimestamp("created_at").toInstant()), id)
|
||||||
|
.stream().findFirst();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Optional<User> findByUsername(String username) {
|
||||||
|
return jdbc.query("""
|
||||||
|
SELECT id, username, display_name, created_at
|
||||||
|
FROM users
|
||||||
|
WHERE username = ?
|
||||||
|
""", (rs, rowNum) -> new User(
|
||||||
|
rs.getObject("id", UUID.class),
|
||||||
|
rs.getString("username"),
|
||||||
|
rs.getString("display_name"),
|
||||||
|
rs.getTimestamp("created_at").toInstant()), username)
|
||||||
|
.stream().findFirst();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Optional<UserCredentials> findCredentialsByUsername(String username) {
|
||||||
|
return jdbc.query("""
|
||||||
|
SELECT id, username, display_name, password_hash, created_at
|
||||||
|
FROM users
|
||||||
|
WHERE username = ?
|
||||||
|
""", (rs, rowNum) -> new UserCredentials(
|
||||||
|
new User(
|
||||||
|
rs.getObject("id", UUID.class),
|
||||||
|
rs.getString("username"),
|
||||||
|
rs.getString("display_name"),
|
||||||
|
rs.getTimestamp("created_at").toInstant()),
|
||||||
|
rs.getString("password_hash")), username)
|
||||||
|
.stream().findFirst();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
package com.example.mtusi_messenger.repository;
|
||||||
|
|
||||||
|
import com.example.mtusi_messenger.model.Session;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
public interface SessionRepository {
|
||||||
|
Session save(Session session);
|
||||||
|
Optional<UUID> findActiveUserId(byte[] tokenHash);
|
||||||
|
boolean revoke(byte[] tokenHash);
|
||||||
|
}
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
package com.example.mtusi_messenger.repository;
|
||||||
|
|
||||||
|
import com.example.mtusi_messenger.model.User;
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
public interface UserRepository {
|
||||||
|
User save(User user, String passwordHash);
|
||||||
|
Optional<User> findById(UUID id);
|
||||||
|
Optional<User> findByUsername(String username);
|
||||||
|
Optional<UserCredentials> findCredentialsByUsername(String username);
|
||||||
|
|
||||||
|
record UserCredentials(User user, String passwordHash) {}
|
||||||
|
}
|
||||||
@@ -0,0 +1,130 @@
|
|||||||
|
package com.example.mtusi_messenger.security;
|
||||||
|
|
||||||
|
import com.example.mtusi_messenger.service.AuthenticationService;
|
||||||
|
import jakarta.servlet.FilterChain;
|
||||||
|
import jakarta.servlet.ServletException;
|
||||||
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
|
import jakarta.servlet.http.HttpServletResponse;
|
||||||
|
import org.springframework.http.HttpMethod;
|
||||||
|
import org.springframework.security.authentication.AuthenticationManager;
|
||||||
|
import org.springframework.security.authentication.BadCredentialsException;
|
||||||
|
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
||||||
|
import org.springframework.security.core.Authentication;
|
||||||
|
import org.springframework.security.core.AuthenticationException;
|
||||||
|
import org.springframework.security.core.context.SecurityContextHolder;
|
||||||
|
import org.springframework.web.filter.OncePerRequestFilter;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.util.Base64;
|
||||||
|
import java.util.Enumeration;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class ApiAuthenticationFilter extends OncePerRequestFilter {
|
||||||
|
private static final String AUTHORIZATION = "Authorization";
|
||||||
|
private static final String SESSION_PATH = "/api/v1/auth/sessions";
|
||||||
|
|
||||||
|
private final AuthenticationManager authenticationManager;
|
||||||
|
private final AuthenticationService sessions;
|
||||||
|
private final RestAuthenticationEntryPoint basicEntryPoint;
|
||||||
|
private final RestAuthenticationEntryPoint bearerEntryPoint;
|
||||||
|
|
||||||
|
public ApiAuthenticationFilter(
|
||||||
|
AuthenticationManager authenticationManager,
|
||||||
|
AuthenticationService sessions,
|
||||||
|
RestAuthenticationEntryPoint basicEntryPoint,
|
||||||
|
RestAuthenticationEntryPoint bearerEntryPoint
|
||||||
|
) {
|
||||||
|
this.authenticationManager = authenticationManager;
|
||||||
|
this.sessions = sessions;
|
||||||
|
this.basicEntryPoint = basicEntryPoint;
|
||||||
|
this.bearerEntryPoint = bearerEntryPoint;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void doFilterInternal(
|
||||||
|
HttpServletRequest request,
|
||||||
|
HttpServletResponse response,
|
||||||
|
FilterChain filterChain
|
||||||
|
) throws ServletException, IOException {
|
||||||
|
boolean basicLogin = HttpMethod.POST.matches(request.getMethod())
|
||||||
|
&& SESSION_PATH.equals(request.getRequestURI());
|
||||||
|
if (isPublicRequest(request) && !basicLogin) {
|
||||||
|
filterChain.doFilter(request, response);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
Authentication authentication = basicLogin
|
||||||
|
? authenticateBasic(request)
|
||||||
|
: authenticateBearer(request);
|
||||||
|
var context = SecurityContextHolder.createEmptyContext();
|
||||||
|
context.setAuthentication(authentication);
|
||||||
|
SecurityContextHolder.setContext(context);
|
||||||
|
filterChain.doFilter(request, response);
|
||||||
|
} catch (AuthenticationException e) {
|
||||||
|
SecurityContextHolder.clearContext();
|
||||||
|
(basicLogin ? basicEntryPoint : bearerEntryPoint)
|
||||||
|
.commence(request, response, e);
|
||||||
|
} finally {
|
||||||
|
SecurityContextHolder.clearContext();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private Authentication authenticateBasic(HttpServletRequest request) {
|
||||||
|
String header = singleAuthorizationHeader(request);
|
||||||
|
if (!header.startsWith("Basic ") || header.length() == 6) {
|
||||||
|
throw new BadCredentialsException("Invalid Basic credentials");
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
byte[] decoded = Base64.getDecoder().decode(header.substring(6));
|
||||||
|
String credentials = new String(decoded, StandardCharsets.UTF_8);
|
||||||
|
int separator = credentials.indexOf(':');
|
||||||
|
if (separator <= 0) {
|
||||||
|
throw new BadCredentialsException("Invalid Basic credentials");
|
||||||
|
}
|
||||||
|
return authenticationManager.authenticate(
|
||||||
|
UsernamePasswordAuthenticationToken.unauthenticated(
|
||||||
|
credentials.substring(0, separator),
|
||||||
|
credentials.substring(separator + 1)));
|
||||||
|
} catch (IllegalArgumentException e) {
|
||||||
|
throw new BadCredentialsException("Invalid Basic credentials", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private Authentication authenticateBearer(HttpServletRequest request) {
|
||||||
|
String header = singleAuthorizationHeader(request);
|
||||||
|
if (!header.startsWith("Bearer ") || header.length() == 7) {
|
||||||
|
throw new BadCredentialsException("Invalid Bearer token");
|
||||||
|
}
|
||||||
|
String token = header.substring(7);
|
||||||
|
if (!token.matches("[A-Za-z0-9_-]{43}")) {
|
||||||
|
throw new BadCredentialsException("Invalid Bearer token");
|
||||||
|
}
|
||||||
|
SessionPrincipal principal = sessions.authenticateBearer(token);
|
||||||
|
return UsernamePasswordAuthenticationToken.authenticated(principal, null, List.of());
|
||||||
|
}
|
||||||
|
|
||||||
|
private String singleAuthorizationHeader(HttpServletRequest request) {
|
||||||
|
Enumeration<String> values = request.getHeaders(AUTHORIZATION);
|
||||||
|
if (values == null || !values.hasMoreElements()) {
|
||||||
|
throw new BadCredentialsException("Authentication required");
|
||||||
|
}
|
||||||
|
String value = values.nextElement();
|
||||||
|
if (values.hasMoreElements()) {
|
||||||
|
throw new BadCredentialsException("Multiple Authorization headers");
|
||||||
|
}
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isPublicRequest(HttpServletRequest request) {
|
||||||
|
String path = request.getRequestURI();
|
||||||
|
return path.startsWith("/health/")
|
||||||
|
|| path.equals("/swagger-ui.html")
|
||||||
|
|| path.startsWith("/swagger-ui/")
|
||||||
|
|| path.equals("/v3/api-docs")
|
||||||
|
|| path.startsWith("/v3/api-docs/")
|
||||||
|
|| (HttpMethod.POST.matches(request.getMethod())
|
||||||
|
&& "/api/v1/users".equals(path));
|
||||||
|
}
|
||||||
|
}
|
||||||
+63
@@ -0,0 +1,63 @@
|
|||||||
|
package com.example.mtusi_messenger.security;
|
||||||
|
|
||||||
|
import com.example.mtusi_messenger.repository.UserRepository;
|
||||||
|
import org.springframework.security.authentication.AuthenticationProvider;
|
||||||
|
import org.springframework.security.authentication.AuthenticationServiceException;
|
||||||
|
import org.springframework.security.authentication.BadCredentialsException;
|
||||||
|
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
||||||
|
import org.springframework.security.core.Authentication;
|
||||||
|
import org.springframework.security.core.AuthenticationException;
|
||||||
|
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import org.springframework.dao.DataAccessException;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class DatabaseAuthenticationProvider implements AuthenticationProvider {
|
||||||
|
private final UserRepository users;
|
||||||
|
private final PasswordEncoder passwordEncoder;
|
||||||
|
private final String dummyPasswordHash;
|
||||||
|
|
||||||
|
public DatabaseAuthenticationProvider(UserRepository users, PasswordEncoder passwordEncoder) {
|
||||||
|
this.users = users;
|
||||||
|
this.passwordEncoder = passwordEncoder;
|
||||||
|
this.dummyPasswordHash = passwordEncoder.encode("dummy-password-used-only-for-timing");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
|
||||||
|
String username = authentication.getName();
|
||||||
|
String password = String.valueOf(authentication.getCredentials());
|
||||||
|
final java.util.Optional<UserRepository.UserCredentials> stored;
|
||||||
|
try {
|
||||||
|
stored = users.findCredentialsByUsername(username);
|
||||||
|
} catch (DataAccessException e) {
|
||||||
|
throw new AuthenticationServiceException("Authentication database is unavailable", e);
|
||||||
|
}
|
||||||
|
String passwordHash = stored.map(UserRepository.UserCredentials::passwordHash)
|
||||||
|
.filter(hash -> hash.startsWith("$argon2"))
|
||||||
|
.orElse(dummyPasswordHash);
|
||||||
|
|
||||||
|
boolean matches;
|
||||||
|
try {
|
||||||
|
matches = passwordEncoder.matches(password, passwordHash);
|
||||||
|
} catch (IllegalArgumentException e) {
|
||||||
|
matches = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (stored.isEmpty() || !matches) {
|
||||||
|
throw new BadCredentialsException("Invalid credentials");
|
||||||
|
}
|
||||||
|
|
||||||
|
return UsernamePasswordAuthenticationToken.authenticated(
|
||||||
|
stored.get().user(),
|
||||||
|
null,
|
||||||
|
List.of());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean supports(Class<?> authentication) {
|
||||||
|
return UsernamePasswordAuthenticationToken.class.isAssignableFrom(authentication);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,46 @@
|
|||||||
|
package com.example.mtusi_messenger.security;
|
||||||
|
|
||||||
|
import com.example.mtusi_messenger.dto.ErrorResponse;
|
||||||
|
import com.example.mtusi_messenger.web.RequestIdFilter;
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
|
import jakarta.servlet.http.HttpServletResponse;
|
||||||
|
import org.springframework.http.MediaType;
|
||||||
|
import org.springframework.security.core.AuthenticationException;
|
||||||
|
import org.springframework.security.authentication.AuthenticationServiceException;
|
||||||
|
import org.springframework.security.web.AuthenticationEntryPoint;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
public class RestAuthenticationEntryPoint implements AuthenticationEntryPoint {
|
||||||
|
private final ObjectMapper mapper;
|
||||||
|
private final String challenge;
|
||||||
|
|
||||||
|
public RestAuthenticationEntryPoint(ObjectMapper mapper, String challenge) {
|
||||||
|
this.mapper = mapper;
|
||||||
|
this.challenge = challenge;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void commence(
|
||||||
|
HttpServletRequest request,
|
||||||
|
HttpServletResponse response,
|
||||||
|
AuthenticationException exception
|
||||||
|
) throws IOException {
|
||||||
|
String requestId = (String) request.getAttribute(RequestIdFilter.ATTRIBUTE);
|
||||||
|
boolean unavailable = exception instanceof AuthenticationServiceException;
|
||||||
|
response.setStatus(unavailable
|
||||||
|
? HttpServletResponse.SC_SERVICE_UNAVAILABLE
|
||||||
|
: HttpServletResponse.SC_UNAUTHORIZED);
|
||||||
|
response.setContentType(MediaType.APPLICATION_JSON_VALUE);
|
||||||
|
response.setCharacterEncoding("UTF-8");
|
||||||
|
if (!unavailable) {
|
||||||
|
response.setHeader("WWW-Authenticate", challenge);
|
||||||
|
}
|
||||||
|
mapper.writeValue(response.getOutputStream(), new ErrorResponse(
|
||||||
|
new ErrorResponse.ErrorBody(
|
||||||
|
unavailable ? "unavailable" : "unauthorized",
|
||||||
|
unavailable ? "Authentication service temporarily unavailable" : "Authentication required",
|
||||||
|
requestId)));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
package com.example.mtusi_messenger.security;
|
||||||
|
|
||||||
|
import com.example.mtusi_messenger.model.User;
|
||||||
|
|
||||||
|
public record SessionPrincipal(User user, byte[] tokenHash) {
|
||||||
|
public SessionPrincipal {
|
||||||
|
tokenHash = tokenHash.clone();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public byte[] tokenHash() {
|
||||||
|
return tokenHash.clone();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,104 @@
|
|||||||
|
package com.example.mtusi_messenger.service;
|
||||||
|
|
||||||
|
import com.example.mtusi_messenger.repository.AttachmentRepository;
|
||||||
|
import com.example.mtusi_messenger.storage.ObjectStorage;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.scheduling.annotation.Scheduled;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.time.Clock;
|
||||||
|
import java.time.Duration;
|
||||||
|
import java.time.Instant;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class AttachmentReconciler {
|
||||||
|
private static final Logger log = LoggerFactory.getLogger(AttachmentReconciler.class);
|
||||||
|
private static final String PREFIX = "attachments/";
|
||||||
|
private final AttachmentRepository attachments;
|
||||||
|
private final ObjectStorage storage;
|
||||||
|
private final Duration gracePeriod;
|
||||||
|
private final boolean dryRun;
|
||||||
|
private final Clock clock;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
public AttachmentReconciler(
|
||||||
|
AttachmentRepository attachments,
|
||||||
|
ObjectStorage storage,
|
||||||
|
@Value("${messenger.attachments.cleanup-grace-seconds:3600}") long graceSeconds,
|
||||||
|
@Value("${messenger.attachments.cleanup-dry-run:false}") boolean dryRun
|
||||||
|
) {
|
||||||
|
this(attachments, storage, Duration.ofSeconds(graceSeconds), dryRun, Clock.systemUTC());
|
||||||
|
}
|
||||||
|
|
||||||
|
AttachmentReconciler(AttachmentRepository attachments,
|
||||||
|
ObjectStorage storage,
|
||||||
|
Duration gracePeriod,
|
||||||
|
boolean dryRun,
|
||||||
|
Clock clock) {
|
||||||
|
if (gracePeriod.isNegative() || gracePeriod.isZero()) {
|
||||||
|
throw new IllegalArgumentException("Attachment cleanup grace period must be positive");
|
||||||
|
}
|
||||||
|
this.attachments = attachments;
|
||||||
|
this.storage = storage;
|
||||||
|
this.gracePeriod = gracePeriod;
|
||||||
|
this.dryRun = dryRun;
|
||||||
|
this.clock = clock;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Scheduled(
|
||||||
|
fixedDelayString = "${messenger.attachments.reconcile-interval-millis:600000}",
|
||||||
|
initialDelayString = "${messenger.attachments.reconcile-initial-delay-millis:60000}")
|
||||||
|
public void scheduledReconcile() {
|
||||||
|
try {
|
||||||
|
Report report = reconcile();
|
||||||
|
if (report.orphanObjects() > 0 || report.missingObjects() > 0) {
|
||||||
|
log.warn("Attachment reconciliation: {} orphan, {} deleted, {} missing, dryRun={}",
|
||||||
|
report.orphanObjects(), report.deletedObjects(), report.missingObjects(), dryRun);
|
||||||
|
}
|
||||||
|
} catch (RuntimeException error) {
|
||||||
|
log.warn("Attachment reconciliation postponed: {}", error.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public Report reconcile() {
|
||||||
|
Set<String> databaseKeys = attachments.findAllObjectKeys();
|
||||||
|
var storedObjects = storage.list(PREFIX);
|
||||||
|
Set<String> storageKeys = new HashSet<>();
|
||||||
|
Instant deleteBefore = clock.instant().minus(gracePeriod);
|
||||||
|
int orphans = 0;
|
||||||
|
int deleted = 0;
|
||||||
|
|
||||||
|
for (ObjectStorage.StoredObject object : storedObjects) {
|
||||||
|
storageKeys.add(object.objectKey());
|
||||||
|
if (!databaseKeys.contains(object.objectKey()) && object.lastModified().isBefore(deleteBefore)) {
|
||||||
|
orphans++;
|
||||||
|
if (!dryRun) {
|
||||||
|
storage.delete(object.objectKey());
|
||||||
|
deleted++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int missing = 0;
|
||||||
|
for (String databaseKey : databaseKeys) {
|
||||||
|
if (!storageKeys.contains(databaseKey)) {
|
||||||
|
missing++;
|
||||||
|
log.error("Attachment metadata references missing object {}", databaseKey);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return new Report(databaseKeys.size(), storedObjects.size(), orphans, deleted, missing, dryRun);
|
||||||
|
}
|
||||||
|
|
||||||
|
public record Report(int metadataObjects,
|
||||||
|
int storageObjects,
|
||||||
|
int orphanObjects,
|
||||||
|
int deletedObjects,
|
||||||
|
int missingObjects,
|
||||||
|
boolean dryRun) {
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,239 @@
|
|||||||
|
package com.example.mtusi_messenger.service;
|
||||||
|
|
||||||
|
import com.example.mtusi_messenger.exception.ApiExceptions;
|
||||||
|
import com.example.mtusi_messenger.model.Attachment;
|
||||||
|
import com.example.mtusi_messenger.repository.AttachmentRepository;
|
||||||
|
import com.example.mtusi_messenger.storage.ObjectStorage;
|
||||||
|
import com.example.mtusi_messenger.storage.ObjectStorageException;
|
||||||
|
import com.github.f4b6a3.uuid.UuidCreator;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.transaction.support.TransactionTemplate;
|
||||||
|
|
||||||
|
import javax.imageio.ImageIO;
|
||||||
|
import javax.imageio.ImageReader;
|
||||||
|
import java.io.ByteArrayInputStream;
|
||||||
|
import java.io.ByteArrayOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.security.MessageDigest;
|
||||||
|
import java.security.NoSuchAlgorithmException;
|
||||||
|
import java.time.Instant;
|
||||||
|
import java.time.temporal.ChronoUnit;
|
||||||
|
import java.util.HexFormat;
|
||||||
|
import java.util.Iterator;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Locale;
|
||||||
|
import java.util.UUID;
|
||||||
|
import java.util.concurrent.Semaphore;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class AttachmentService {
|
||||||
|
private static final List<String> ALLOWED_TYPES = List.of("image/png", "image/jpeg");
|
||||||
|
private final AttachmentRepository attachments;
|
||||||
|
private final ChatService chats;
|
||||||
|
private final ObjectStorage storage;
|
||||||
|
private final TransactionTemplate transaction;
|
||||||
|
private final long maxUploadBytes;
|
||||||
|
private final long maxImagePixels;
|
||||||
|
private final Semaphore uploadPermits;
|
||||||
|
private final long uploadPermitWaitMillis;
|
||||||
|
|
||||||
|
public AttachmentService(
|
||||||
|
AttachmentRepository attachments,
|
||||||
|
ChatService chats,
|
||||||
|
ObjectStorage storage,
|
||||||
|
TransactionTemplate transaction,
|
||||||
|
@Value("${messenger.attachments.max-upload-bytes:10485760}") long maxUploadBytes,
|
||||||
|
@Value("${messenger.attachments.max-image-pixels:40000000}") long maxImagePixels,
|
||||||
|
@Value("${messenger.attachments.max-concurrent-uploads:3}") int maxConcurrentUploads,
|
||||||
|
@Value("${messenger.attachments.upload-permit-wait-millis:10000}") long uploadPermitWaitMillis
|
||||||
|
) {
|
||||||
|
this.attachments = attachments;
|
||||||
|
this.chats = chats;
|
||||||
|
this.storage = storage;
|
||||||
|
this.transaction = transaction;
|
||||||
|
this.maxUploadBytes = maxUploadBytes;
|
||||||
|
this.maxImagePixels = maxImagePixels;
|
||||||
|
if (maxConcurrentUploads < 1 || uploadPermitWaitMillis < 1) {
|
||||||
|
throw new IllegalArgumentException("Upload concurrency limits must be positive");
|
||||||
|
}
|
||||||
|
this.uploadPermits = new Semaphore(maxConcurrentUploads, true);
|
||||||
|
this.uploadPermitWaitMillis = uploadPermitWaitMillis;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Attachment upload(UUID chatId,
|
||||||
|
UUID ownerId,
|
||||||
|
String filename,
|
||||||
|
String declaredContentType,
|
||||||
|
long declaredLength,
|
||||||
|
InputStream input) {
|
||||||
|
chats.getAccessible(chatId, ownerId);
|
||||||
|
String safeFilename = validateFilename(filename);
|
||||||
|
String contentType = normalizeContentType(declaredContentType);
|
||||||
|
if (declaredLength > maxUploadBytes) {
|
||||||
|
throw ApiExceptions.payloadTooLarge("Attachment exceeds 10 MiB");
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
try {
|
||||||
|
if (!uploadPermits.tryAcquire(uploadPermitWaitMillis, TimeUnit.MILLISECONDS)) {
|
||||||
|
throw ApiExceptions.tooManyRequests("Upload capacity is temporarily exhausted");
|
||||||
|
}
|
||||||
|
} catch (InterruptedException interrupted) {
|
||||||
|
Thread.currentThread().interrupt();
|
||||||
|
throw ApiExceptions.unavailable("Upload was interrupted");
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
return storeValidated(chatId, ownerId, safeFilename, contentType, input);
|
||||||
|
} finally {
|
||||||
|
uploadPermits.release();
|
||||||
|
}
|
||||||
|
} catch (com.example.mtusi_messenger.exception.ApiException error) {
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private Attachment storeValidated(UUID chatId,
|
||||||
|
UUID ownerId,
|
||||||
|
String safeFilename,
|
||||||
|
String contentType,
|
||||||
|
InputStream input) {
|
||||||
|
byte[] bytes = readBounded(input);
|
||||||
|
if (bytes.length == 0) {
|
||||||
|
throw ApiExceptions.unsupportedMediaType("Image is empty");
|
||||||
|
}
|
||||||
|
validateImage(bytes, contentType);
|
||||||
|
|
||||||
|
UUID id = UuidCreator.getTimeOrderedEpochPlus1();
|
||||||
|
String objectKey = "attachments/" + id + "/original";
|
||||||
|
Instant now = Instant.now().truncatedTo(ChronoUnit.MICROS);
|
||||||
|
Attachment attachment = new Attachment(id, chatId, ownerId, safeFilename, contentType,
|
||||||
|
bytes.length, sha256(bytes), objectKey, now, now);
|
||||||
|
|
||||||
|
storage.put(objectKey, bytes, contentType);
|
||||||
|
try {
|
||||||
|
return transaction.execute(status -> attachments.save(attachment));
|
||||||
|
} catch (RuntimeException databaseFailure) {
|
||||||
|
try {
|
||||||
|
storage.delete(objectKey);
|
||||||
|
} catch (RuntimeException cleanupFailure) {
|
||||||
|
databaseFailure.addSuppressed(cleanupFailure);
|
||||||
|
}
|
||||||
|
throw databaseFailure;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public Attachment getAccessible(UUID attachmentId, UUID userId) {
|
||||||
|
Attachment attachment = attachments.findById(attachmentId)
|
||||||
|
.orElseThrow(() -> ApiExceptions.notFound("Attachment not found"));
|
||||||
|
chats.getAccessible(attachment.chatId(), userId);
|
||||||
|
return attachment;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Download download(UUID attachmentId, UUID userId) {
|
||||||
|
Attachment attachment = getAccessible(attachmentId, userId);
|
||||||
|
return new Download(attachment, storage.get(attachment.objectKey()));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void validateForMessage(UUID chatId, List<UUID> attachmentIds) {
|
||||||
|
if (attachmentIds.isEmpty()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (attachments.findAllByIdsInChat(chatId, attachmentIds).size() != attachmentIds.size()) {
|
||||||
|
throw ApiExceptions.notFound("Attachment not found");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private byte[] readBounded(InputStream input) {
|
||||||
|
try {
|
||||||
|
ByteArrayOutputStream output = new ByteArrayOutputStream(
|
||||||
|
(int) Math.min(maxUploadBytes, 1024 * 1024));
|
||||||
|
byte[] chunk = new byte[8192];
|
||||||
|
long total = 0;
|
||||||
|
int read;
|
||||||
|
while ((read = input.read(chunk)) != -1) {
|
||||||
|
total += read;
|
||||||
|
if (total > maxUploadBytes) {
|
||||||
|
throw ApiExceptions.payloadTooLarge("Attachment exceeds 10 MiB");
|
||||||
|
}
|
||||||
|
output.write(chunk, 0, read);
|
||||||
|
}
|
||||||
|
return output.toByteArray();
|
||||||
|
} catch (IOException error) {
|
||||||
|
throw ApiExceptions.invalid("Cannot read request body");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void validateImage(byte[] bytes, String declaredContentType) {
|
||||||
|
ImageIO.setUseCache(false);
|
||||||
|
try (var imageInput = ImageIO.createImageInputStream(new ByteArrayInputStream(bytes))) {
|
||||||
|
if (imageInput == null) {
|
||||||
|
throw ApiExceptions.unsupportedMediaType("Invalid image");
|
||||||
|
}
|
||||||
|
Iterator<ImageReader> readers = ImageIO.getImageReaders(imageInput);
|
||||||
|
if (!readers.hasNext()) {
|
||||||
|
throw ApiExceptions.unsupportedMediaType("Invalid PNG/JPEG image");
|
||||||
|
}
|
||||||
|
ImageReader reader = readers.next();
|
||||||
|
try {
|
||||||
|
reader.setInput(imageInput, true, true);
|
||||||
|
String actualType = switch (reader.getFormatName().toLowerCase(Locale.ROOT)) {
|
||||||
|
case "png" -> "image/png";
|
||||||
|
case "jpeg", "jpg" -> "image/jpeg";
|
||||||
|
default -> throw ApiExceptions.unsupportedMediaType("Only PNG and JPEG are supported");
|
||||||
|
};
|
||||||
|
if (!actualType.equals(declaredContentType)) {
|
||||||
|
throw ApiExceptions.unsupportedMediaType("Content-Type does not match image bytes");
|
||||||
|
}
|
||||||
|
int width = reader.getWidth(0);
|
||||||
|
int height = reader.getHeight(0);
|
||||||
|
if (width <= 0 || height <= 0) {
|
||||||
|
throw ApiExceptions.unsupportedMediaType("Invalid image dimensions");
|
||||||
|
}
|
||||||
|
if ((long) width * height > maxImagePixels) {
|
||||||
|
throw ApiExceptions.payloadTooLarge("Image exceeds 40 million pixels");
|
||||||
|
}
|
||||||
|
if (reader.read(0) == null) {
|
||||||
|
throw ApiExceptions.unsupportedMediaType("Image cannot be decoded");
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
reader.dispose();
|
||||||
|
}
|
||||||
|
} catch (com.example.mtusi_messenger.exception.ApiException error) {
|
||||||
|
throw error;
|
||||||
|
} catch (IOException | RuntimeException error) {
|
||||||
|
throw ApiExceptions.unsupportedMediaType("Corrupted PNG/JPEG image");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String validateFilename(String filename) {
|
||||||
|
if (filename == null || filename.isBlank() || filename.length() > 255
|
||||||
|
|| filename.equals(".") || filename.equals("..")
|
||||||
|
|| filename.indexOf('/') >= 0 || filename.indexOf('\\') >= 0
|
||||||
|
|| filename.codePoints().anyMatch(Character::isISOControl)) {
|
||||||
|
throw ApiExceptions.invalid("Invalid filename");
|
||||||
|
}
|
||||||
|
return filename;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String normalizeContentType(String contentType) {
|
||||||
|
String normalized = contentType == null ? "" : contentType.split(";", 2)[0].trim().toLowerCase(Locale.ROOT);
|
||||||
|
if (!ALLOWED_TYPES.contains(normalized)) {
|
||||||
|
throw ApiExceptions.unsupportedMediaType("Only image/png and image/jpeg are supported");
|
||||||
|
}
|
||||||
|
return normalized;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String sha256(byte[] bytes) {
|
||||||
|
try {
|
||||||
|
return HexFormat.of().formatHex(MessageDigest.getInstance("SHA-256").digest(bytes));
|
||||||
|
} catch (NoSuchAlgorithmException impossible) {
|
||||||
|
throw new IllegalStateException("SHA-256 is unavailable", impossible);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public record Download(Attachment attachment, byte[] bytes) {
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,104 @@
|
|||||||
|
package com.example.mtusi_messenger.service;
|
||||||
|
|
||||||
|
import com.example.mtusi_messenger.cache.UserCache;
|
||||||
|
import com.example.mtusi_messenger.model.Session;
|
||||||
|
import com.example.mtusi_messenger.model.User;
|
||||||
|
import com.example.mtusi_messenger.repository.SessionRepository;
|
||||||
|
import com.example.mtusi_messenger.repository.UserRepository;
|
||||||
|
import com.example.mtusi_messenger.security.SessionPrincipal;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.dao.DataAccessException;
|
||||||
|
import org.springframework.security.authentication.AuthenticationServiceException;
|
||||||
|
import org.springframework.security.authentication.BadCredentialsException;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.security.MessageDigest;
|
||||||
|
import java.security.NoSuchAlgorithmException;
|
||||||
|
import java.security.SecureRandom;
|
||||||
|
import java.time.Instant;
|
||||||
|
import java.time.temporal.ChronoUnit;
|
||||||
|
import java.util.Base64;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class AuthenticationService {
|
||||||
|
private static final SecureRandom SECURE_RANDOM = new SecureRandom();
|
||||||
|
|
||||||
|
private final UserRepository users;
|
||||||
|
private final SessionRepository sessions;
|
||||||
|
private final UserCache userCache;
|
||||||
|
private final long ttlSeconds;
|
||||||
|
|
||||||
|
public AuthenticationService(
|
||||||
|
UserRepository users,
|
||||||
|
SessionRepository sessions,
|
||||||
|
UserCache userCache,
|
||||||
|
@Value("${messenger.session.ttl-seconds:3600}") long ttlSeconds
|
||||||
|
) {
|
||||||
|
if (ttlSeconds <= 0) {
|
||||||
|
throw new IllegalArgumentException("Session TTL must be positive");
|
||||||
|
}
|
||||||
|
this.users = users;
|
||||||
|
this.sessions = sessions;
|
||||||
|
this.userCache = userCache;
|
||||||
|
this.ttlSeconds = ttlSeconds;
|
||||||
|
}
|
||||||
|
|
||||||
|
public IssuedSession issueSession(User user) {
|
||||||
|
String token = newToken();
|
||||||
|
Instant createdAt = Instant.now().truncatedTo(ChronoUnit.MICROS);
|
||||||
|
Instant expiresAt = createdAt.plusSeconds(ttlSeconds);
|
||||||
|
sessions.save(new Session(
|
||||||
|
UUID.randomUUID(),
|
||||||
|
user.id(),
|
||||||
|
hashToken(token),
|
||||||
|
createdAt,
|
||||||
|
expiresAt));
|
||||||
|
return new IssuedSession(token, expiresAt, user);
|
||||||
|
}
|
||||||
|
|
||||||
|
public SessionPrincipal authenticateBearer(String token) {
|
||||||
|
byte[] tokenHash = hashToken(token);
|
||||||
|
try {
|
||||||
|
UUID userId = sessions.findActiveUserId(tokenHash)
|
||||||
|
.orElseThrow(() -> new BadCredentialsException("Invalid or expired session"));
|
||||||
|
User user = userCache.find(userId).orElseGet(() -> {
|
||||||
|
User loaded = users.findById(userId)
|
||||||
|
.orElseThrow(() -> new BadCredentialsException("Invalid or expired session"));
|
||||||
|
userCache.put(loaded);
|
||||||
|
return loaded;
|
||||||
|
});
|
||||||
|
return new SessionPrincipal(user, tokenHash);
|
||||||
|
} catch (DataAccessException e) {
|
||||||
|
throw new AuthenticationServiceException("Authentication database is unavailable", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void revoke(SessionPrincipal principal) {
|
||||||
|
try {
|
||||||
|
if (!sessions.revoke(principal.tokenHash())) {
|
||||||
|
throw new BadCredentialsException("Invalid or expired session");
|
||||||
|
}
|
||||||
|
} catch (DataAccessException e) {
|
||||||
|
throw new AuthenticationServiceException("Authentication database is unavailable", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private String newToken() {
|
||||||
|
byte[] bytes = new byte[32];
|
||||||
|
SECURE_RANDOM.nextBytes(bytes);
|
||||||
|
return Base64.getUrlEncoder().withoutPadding().encodeToString(bytes);
|
||||||
|
}
|
||||||
|
|
||||||
|
private byte[] hashToken(String token) {
|
||||||
|
try {
|
||||||
|
return MessageDigest.getInstance("SHA-256")
|
||||||
|
.digest(token.getBytes(StandardCharsets.US_ASCII));
|
||||||
|
} catch (NoSuchAlgorithmException e) {
|
||||||
|
throw new IllegalStateException("SHA-256 is unavailable", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public record IssuedSession(String token, Instant expiresAt, User user) {}
|
||||||
|
}
|
||||||
@@ -0,0 +1,38 @@
|
|||||||
|
package com.example.mtusi_messenger.service;
|
||||||
|
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import com.example.mtusi_messenger.dto.CreateChatRequest;
|
||||||
|
import com.example.mtusi_messenger.exception.ApiExceptions;
|
||||||
|
import com.example.mtusi_messenger.model.Chat;
|
||||||
|
import com.example.mtusi_messenger.repository.ChatRepository;
|
||||||
|
import java.time.Instant;
|
||||||
|
import java.time.temporal.ChronoUnit;
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class ChatService {
|
||||||
|
private final ChatRepository chats; private final UserService users; private final CursorService cursors;
|
||||||
|
public ChatService(ChatRepository chats, UserService users, CursorService cursors) { this.chats=chats; this.users=users; this.cursors=cursors; }
|
||||||
|
public Chat create(CreateChatRequest r, UUID caller) {
|
||||||
|
Set<UUID> ids = new LinkedHashSet<>(r.member_ids());
|
||||||
|
if (ids.size() != r.member_ids().size() || !ids.contains(caller)) throw ApiExceptions.invalid("Invalid member_ids");
|
||||||
|
for (UUID id : ids) if (id == null || !users.exists(id)) throw ApiExceptions.invalid("Invalid member_ids");
|
||||||
|
return chats.save(new Chat(UUID.randomUUID(), r.title(), List.copyOf(ids),
|
||||||
|
Instant.now().truncatedTo(ChronoUnit.MICROS)));
|
||||||
|
}
|
||||||
|
public Chat getAccessible(UUID chatId, UUID userId) {
|
||||||
|
users.get(userId);
|
||||||
|
if (!chats.isMember(chatId, userId)) throw ApiExceptions.notFound("Chat not found");
|
||||||
|
return chats.findById(chatId).orElseThrow(() -> ApiExceptions.notFound("Chat not found"));
|
||||||
|
}
|
||||||
|
public List<Chat> page(UUID userId, int limit, String cursor) {
|
||||||
|
CursorService.Cursor c = cursor == null ? null : cursors.decode(cursor, "chats", userId);
|
||||||
|
return chats.findPageForUser(userId, c == null ? null : c.id(), c == null ? null : c.createdAt(), limit + 1);
|
||||||
|
}
|
||||||
|
public String nextCursor(UUID userId,
|
||||||
|
Chat last,
|
||||||
|
boolean hasNext
|
||||||
|
) {
|
||||||
|
return hasNext ? cursors.encode("chats", userId, last.createdAt(), last.id()) : null;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,40 @@
|
|||||||
|
package com.example.mtusi_messenger.service;
|
||||||
|
|
||||||
|
import com.example.mtusi_messenger.exception.ApiException;
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import com.example.mtusi_messenger.exception.ApiExceptions;
|
||||||
|
import java.time.Instant;
|
||||||
|
import java.util.Base64;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class CursorService {
|
||||||
|
private final ObjectMapper mapper;
|
||||||
|
public CursorService(ObjectMapper mapper) { this.mapper = mapper; }
|
||||||
|
|
||||||
|
public String encode(String resource, UUID resourceId, Instant createdAt, UUID id) {
|
||||||
|
try {
|
||||||
|
byte[] json = mapper.writeValueAsBytes(new CursorPayload(1, resource, resourceId, createdAt.toString(), id));
|
||||||
|
return Base64.getUrlEncoder().withoutPadding().encodeToString(json);
|
||||||
|
} catch (Exception e) { throw new IllegalStateException(e); }
|
||||||
|
}
|
||||||
|
public Cursor decode(String raw, String resource, UUID resourceId) {
|
||||||
|
if (raw == null || raw.isBlank()) throw ApiExceptions.invalid("Invalid cursor");
|
||||||
|
try {
|
||||||
|
CursorPayload p = mapper.readValue(Base64.getUrlDecoder().decode(raw), CursorPayload.class);
|
||||||
|
if (p.v != 1
|
||||||
|
|| !resource.equals(p.resource)
|
||||||
|
|| !Objects.equals(resourceId, p.resourceId)
|
||||||
|
|| p.createdAt() == null
|
||||||
|
|| p.createdAt().isBlank()
|
||||||
|
|| p.id() == null
|
||||||
|
) throw ApiExceptions.invalid("Invalid cursor");
|
||||||
|
return new Cursor(Instant.parse(p.createdAt), p.id);
|
||||||
|
} catch (ApiException e) { throw e; }
|
||||||
|
catch (Exception e) { throw ApiExceptions.invalid("Invalid cursor"); }
|
||||||
|
}
|
||||||
|
public record Cursor(Instant createdAt, UUID id) {}
|
||||||
|
private record CursorPayload(int v, String resource, UUID resourceId, String createdAt, UUID id) {}
|
||||||
|
private static final class Objects { static boolean equals(Object a, Object b) { return java.util.Objects.equals(a,b); } }
|
||||||
|
}
|
||||||
@@ -0,0 +1,102 @@
|
|||||||
|
package com.example.mtusi_messenger.service;
|
||||||
|
|
||||||
|
import com.github.f4b6a3.uuid.UuidCreator;
|
||||||
|
import com.example.mtusi_messenger.exception.ApiExceptions;
|
||||||
|
import com.example.mtusi_messenger.model.IdempotentMessage;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
import com.example.mtusi_messenger.dto.CreateMessageRequest;
|
||||||
|
import com.example.mtusi_messenger.model.Message;
|
||||||
|
import com.example.mtusi_messenger.repository.MessageRepository;
|
||||||
|
import java.time.Instant;
|
||||||
|
import java.time.temporal.ChronoUnit;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.security.MessageDigest;
|
||||||
|
import java.security.NoSuchAlgorithmException;
|
||||||
|
import java.time.Duration;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class MessageService {
|
||||||
|
private static final String METHOD = "POST";
|
||||||
|
private final MessageRepository messages;
|
||||||
|
private final ChatService chats;
|
||||||
|
private final CursorService cursors;
|
||||||
|
private final AttachmentService attachments;
|
||||||
|
private final Duration idempotencyTtl;
|
||||||
|
|
||||||
|
public MessageService(
|
||||||
|
MessageRepository messages,
|
||||||
|
ChatService chats,
|
||||||
|
CursorService cursors,
|
||||||
|
AttachmentService attachments,
|
||||||
|
@Value("${messenger.idempotency.ttl-seconds:86400}") long idempotencyTtlSeconds
|
||||||
|
) {
|
||||||
|
if (idempotencyTtlSeconds < 86400) {
|
||||||
|
throw new IllegalArgumentException("Idempotency TTL must be at least 24 hours");
|
||||||
|
}
|
||||||
|
this.messages = messages;
|
||||||
|
this.chats = chats;
|
||||||
|
this.cursors = cursors;
|
||||||
|
this.attachments = attachments;
|
||||||
|
this.idempotencyTtl = Duration.ofSeconds(idempotencyTtlSeconds);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public Message create(UUID chatId, UUID senderId, CreateMessageRequest r, String idempotencyKey) {
|
||||||
|
chats.getAccessible(chatId, senderId);
|
||||||
|
attachments.validateForMessage(chatId, r.attachment_ids());
|
||||||
|
String path = "/api/v1/chats/" + chatId + "/messages";
|
||||||
|
byte[] requestHash = hash(r.text(), r.attachment_ids());
|
||||||
|
Instant now = Instant.now().truncatedTo(ChronoUnit.MICROS);
|
||||||
|
|
||||||
|
// The transaction-scoped PostgreSQL advisory lock serializes the same
|
||||||
|
// idempotency scope across every API replica. A hash collision only
|
||||||
|
// causes harmless extra serialization; the full scope is still checked.
|
||||||
|
messages.lockIdempotencyScope(senderId, METHOD, path, idempotencyKey);
|
||||||
|
messages.deleteExpiredIdempotency(
|
||||||
|
senderId, METHOD, path, idempotencyKey, now.minus(idempotencyTtl));
|
||||||
|
|
||||||
|
var existing = messages.findIdempotent(senderId, METHOD, path, idempotencyKey);
|
||||||
|
if (existing.isPresent()) {
|
||||||
|
IdempotentMessage value = existing.get();
|
||||||
|
if (!Arrays.equals(value.requestHash(), requestHash)) {
|
||||||
|
throw ApiExceptions.conflict("Idempotency-Key was already used with another request body");
|
||||||
|
}
|
||||||
|
return value.message();
|
||||||
|
}
|
||||||
|
|
||||||
|
Message message = messages.save(new Message(
|
||||||
|
UuidCreator.getTimeOrderedEpochPlus1(), chatId, senderId, r.text(), now,
|
||||||
|
r.attachment_ids()));
|
||||||
|
messages.saveIdempotency(
|
||||||
|
senderId, METHOD, path, idempotencyKey, requestHash, message.id(), now);
|
||||||
|
return message;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static byte[] hash(String text, List<UUID> attachmentIds) {
|
||||||
|
try {
|
||||||
|
MessageDigest digest = MessageDigest.getInstance("SHA-256");
|
||||||
|
byte[] textBytes = text.getBytes(StandardCharsets.UTF_8);
|
||||||
|
digest.update(java.nio.ByteBuffer.allocate(Integer.BYTES).putInt(textBytes.length).array());
|
||||||
|
digest.update(textBytes);
|
||||||
|
for (UUID id : attachmentIds) {
|
||||||
|
digest.update(java.nio.ByteBuffer.allocate(16)
|
||||||
|
.putLong(id.getMostSignificantBits())
|
||||||
|
.putLong(id.getLeastSignificantBits()).array());
|
||||||
|
}
|
||||||
|
return digest.digest();
|
||||||
|
} catch (NoSuchAlgorithmException e) {
|
||||||
|
throw new IllegalStateException("SHA-256 is unavailable", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public List<Message> page(UUID chatId, UUID userId, int limit, String cursor) {
|
||||||
|
chats.getAccessible(chatId, userId);
|
||||||
|
CursorService.Cursor c = cursor == null ? null : cursors.decode(cursor, "messages", chatId);
|
||||||
|
return messages.findPage(chatId, c == null ? null : c.createdAt(), c == null ? null : c.id(), limit + 1);
|
||||||
|
}
|
||||||
|
public String nextCursor(UUID chatId, Message last, boolean hasNext) { return hasNext ? cursors.encode("messages", chatId, last.createdAt(), last.id()) : null; }
|
||||||
|
}
|
||||||
@@ -0,0 +1,81 @@
|
|||||||
|
package com.example.mtusi_messenger.service;
|
||||||
|
|
||||||
|
import org.springframework.boot.availability.ApplicationAvailability;
|
||||||
|
import org.springframework.boot.availability.AvailabilityChangeEvent;
|
||||||
|
import org.springframework.boot.availability.ReadinessState;
|
||||||
|
import org.springframework.context.ApplicationEventPublisher;
|
||||||
|
import org.springframework.context.event.ContextClosedEvent;
|
||||||
|
import org.springframework.context.event.EventListener;
|
||||||
|
import org.springframework.dao.DataAccessException;
|
||||||
|
import org.springframework.data.redis.core.RedisCallback;
|
||||||
|
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||||
|
import org.springframework.jdbc.core.JdbcTemplate;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.util.concurrent.atomic.AtomicBoolean;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class ReadinessService {
|
||||||
|
private final JdbcTemplate jdbc;
|
||||||
|
private final StringRedisTemplate redis;
|
||||||
|
private final ApplicationAvailability availability;
|
||||||
|
private final ApplicationEventPublisher events;
|
||||||
|
private final AtomicBoolean dependencyUnavailable = new AtomicBoolean(false);
|
||||||
|
private final AtomicBoolean shuttingDown = new AtomicBoolean(false);
|
||||||
|
|
||||||
|
public ReadinessService(
|
||||||
|
JdbcTemplate jdbc,
|
||||||
|
StringRedisTemplate redis,
|
||||||
|
ApplicationAvailability availability,
|
||||||
|
ApplicationEventPublisher events
|
||||||
|
) {
|
||||||
|
this.jdbc = jdbc;
|
||||||
|
this.redis = redis;
|
||||||
|
this.availability = availability;
|
||||||
|
this.events = events;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean probeDependencies() {
|
||||||
|
try {
|
||||||
|
// Redis is an optional cache. Ping only warms the lazy connection;
|
||||||
|
// its failure must not make the authoritative API unavailable.
|
||||||
|
redis.execute((RedisCallback<String>) connection -> connection.ping());
|
||||||
|
} catch (DataAccessException ignored) {
|
||||||
|
// PostgreSQL remains the source of truth.
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
// The schema-level probe also proves that the separate lab4 migration
|
||||||
|
// completed before this replica is allowed to serve business traffic.
|
||||||
|
jdbc.queryForObject("SELECT count(*) FROM attachments", Long.class);
|
||||||
|
markDependenciesAvailable();
|
||||||
|
return true;
|
||||||
|
} catch (DataAccessException e) {
|
||||||
|
markDependenciesUnavailable();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean acceptsBusinessTraffic() {
|
||||||
|
return !shuttingDown.get()
|
||||||
|
&& !dependencyUnavailable.get()
|
||||||
|
&& availability.getReadinessState() == ReadinessState.ACCEPTING_TRAFFIC;
|
||||||
|
}
|
||||||
|
|
||||||
|
@EventListener(ContextClosedEvent.class)
|
||||||
|
void onContextClosed() {
|
||||||
|
shuttingDown.set(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void markDependenciesUnavailable() {
|
||||||
|
dependencyUnavailable.set(true);
|
||||||
|
AvailabilityChangeEvent.publish(events, this, ReadinessState.REFUSING_TRAFFIC);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void markDependenciesAvailable() {
|
||||||
|
boolean recovered = dependencyUnavailable.getAndSet(false);
|
||||||
|
if (recovered && !shuttingDown.get()) {
|
||||||
|
AvailabilityChangeEvent.publish(events, this, ReadinessState.ACCEPTING_TRAFFIC);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,34 @@
|
|||||||
|
package com.example.mtusi_messenger.service;
|
||||||
|
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.dao.DuplicateKeyException;
|
||||||
|
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||||
|
import com.example.mtusi_messenger.dto.CreateUserRequest;
|
||||||
|
import com.example.mtusi_messenger.exception.ApiExceptions;
|
||||||
|
import com.example.mtusi_messenger.model.User;
|
||||||
|
import com.example.mtusi_messenger.repository.UserRepository;
|
||||||
|
import java.time.Instant;
|
||||||
|
import java.time.temporal.ChronoUnit;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class UserService {
|
||||||
|
private final UserRepository users;
|
||||||
|
private final PasswordEncoder passwordEncoder;
|
||||||
|
|
||||||
|
public User create(CreateUserRequest r) {
|
||||||
|
String username = r.username(); String display = r.display_name();
|
||||||
|
if (users.findByUsername(username).isPresent()) throw ApiExceptions.conflict("Username is already taken");
|
||||||
|
String passwordHash = passwordEncoder.encode(r.password());
|
||||||
|
try {
|
||||||
|
return users.save(new User(UUID.randomUUID(), username, display,
|
||||||
|
Instant.now().truncatedTo(ChronoUnit.MICROS)), passwordHash);
|
||||||
|
} catch (DuplicateKeyException e) {
|
||||||
|
throw ApiExceptions.conflict("Username is already taken");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public User get(UUID id) { return users.findById(id).orElseThrow(() -> ApiExceptions.unauthorized("Unknown user identity")); }
|
||||||
|
public boolean exists(UUID id) { return users.findById(id).isPresent(); }
|
||||||
|
}
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
package com.example.mtusi_messenger.service;
|
||||||
|
|
||||||
|
import com.example.mtusi_messenger.exception.ApiExceptions;
|
||||||
|
|
||||||
|
import java.util.UUID;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
|
public final class UuidValidator {
|
||||||
|
|
||||||
|
private static final Pattern UUID_PATTERN = Pattern.compile(
|
||||||
|
"^[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}$"
|
||||||
|
);
|
||||||
|
|
||||||
|
private UuidValidator() {}
|
||||||
|
|
||||||
|
public static UUID parse(String value) {
|
||||||
|
if (value == null || !UUID_PATTERN.matcher(value).matches()) {
|
||||||
|
throw ApiExceptions.invalid("Invalid UUID");
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
return UUID.fromString(value);
|
||||||
|
} catch (IllegalArgumentException e) {
|
||||||
|
throw ApiExceptions.invalid("Invalid UUID");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,92 @@
|
|||||||
|
package com.example.mtusi_messenger.storage;
|
||||||
|
|
||||||
|
import io.minio.GetObjectArgs;
|
||||||
|
import io.minio.MinioClient;
|
||||||
|
import io.minio.ListObjectsArgs;
|
||||||
|
import io.minio.PutObjectArgs;
|
||||||
|
import io.minio.RemoveObjectArgs;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import okhttp3.OkHttpClient;
|
||||||
|
|
||||||
|
import java.io.ByteArrayInputStream;
|
||||||
|
import java.time.Duration;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class MinioObjectStorage implements ObjectStorage {
|
||||||
|
private final MinioClient client;
|
||||||
|
private final String bucket;
|
||||||
|
|
||||||
|
public MinioObjectStorage(
|
||||||
|
@Value("${messenger.s3.endpoint}") String endpoint,
|
||||||
|
@Value("${messenger.s3.access-key}") String accessKey,
|
||||||
|
@Value("${messenger.s3.secret-key}") String secretKey,
|
||||||
|
@Value("${messenger.s3.bucket}") String bucket,
|
||||||
|
@Value("${messenger.s3.region:us-east-1}") String region
|
||||||
|
) {
|
||||||
|
OkHttpClient httpClient = new OkHttpClient.Builder()
|
||||||
|
.connectTimeout(Duration.ofSeconds(2))
|
||||||
|
.readTimeout(Duration.ofSeconds(10))
|
||||||
|
.writeTimeout(Duration.ofSeconds(10))
|
||||||
|
.callTimeout(Duration.ofSeconds(15))
|
||||||
|
.build();
|
||||||
|
this.client = MinioClient.builder()
|
||||||
|
.endpoint(endpoint)
|
||||||
|
.credentials(accessKey, secretKey)
|
||||||
|
.region(region)
|
||||||
|
.httpClient(httpClient)
|
||||||
|
.build();
|
||||||
|
this.bucket = bucket;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void put(String objectKey, byte[] bytes, String contentType) {
|
||||||
|
try (var stream = new ByteArrayInputStream(bytes)) {
|
||||||
|
client.putObject(PutObjectArgs.builder()
|
||||||
|
.bucket(bucket)
|
||||||
|
.object(objectKey)
|
||||||
|
.contentType(contentType)
|
||||||
|
.stream(stream, bytes.length, -1)
|
||||||
|
.build());
|
||||||
|
} catch (Exception error) {
|
||||||
|
throw new ObjectStorageException("Cannot write attachment object", error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public byte[] get(String objectKey) {
|
||||||
|
try (var stream = client.getObject(GetObjectArgs.builder()
|
||||||
|
.bucket(bucket).object(objectKey).build())) {
|
||||||
|
return stream.readAllBytes();
|
||||||
|
} catch (Exception error) {
|
||||||
|
throw new ObjectStorageException("Cannot read attachment object", error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void delete(String objectKey) {
|
||||||
|
try {
|
||||||
|
client.removeObject(RemoveObjectArgs.builder()
|
||||||
|
.bucket(bucket).object(objectKey).build());
|
||||||
|
} catch (Exception error) {
|
||||||
|
throw new ObjectStorageException("Cannot remove attachment object", error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<StoredObject> list(String prefix) {
|
||||||
|
try {
|
||||||
|
List<StoredObject> objects = new ArrayList<>();
|
||||||
|
for (var result : client.listObjects(ListObjectsArgs.builder()
|
||||||
|
.bucket(bucket).prefix(prefix).recursive(true).build())) {
|
||||||
|
var item = result.get();
|
||||||
|
objects.add(new StoredObject(item.objectName(), item.lastModified().toInstant(), item.size()));
|
||||||
|
}
|
||||||
|
return List.copyOf(objects);
|
||||||
|
} catch (Exception error) {
|
||||||
|
throw new ObjectStorageException("Cannot list attachment objects", error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
package com.example.mtusi_messenger.storage;
|
||||||
|
|
||||||
|
import java.time.Instant;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public interface ObjectStorage {
|
||||||
|
void put(String objectKey, byte[] bytes, String contentType);
|
||||||
|
byte[] get(String objectKey);
|
||||||
|
void delete(String objectKey);
|
||||||
|
List<StoredObject> list(String prefix);
|
||||||
|
|
||||||
|
record StoredObject(String objectKey, Instant lastModified, long sizeBytes) {
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
package com.example.mtusi_messenger.storage;
|
||||||
|
|
||||||
|
public class ObjectStorageException extends RuntimeException {
|
||||||
|
public ObjectStorageException(String message, Throwable cause) {
|
||||||
|
super(message, cause);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,56 @@
|
|||||||
|
package com.example.mtusi_messenger.web;
|
||||||
|
|
||||||
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.http.converter.HttpMessageNotReadableException;
|
||||||
|
import org.springframework.dao.DataAccessException;
|
||||||
|
import org.springframework.web.HttpMediaTypeNotSupportedException;
|
||||||
|
import org.springframework.web.bind.MethodArgumentNotValidException;
|
||||||
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||||
|
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||||
|
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
|
||||||
|
import com.example.mtusi_messenger.dto.ErrorResponse;
|
||||||
|
import com.example.mtusi_messenger.exception.ApiException;
|
||||||
|
import com.example.mtusi_messenger.storage.ObjectStorageException;
|
||||||
|
|
||||||
|
@RestControllerAdvice
|
||||||
|
public class GlobalExceptionHandler {
|
||||||
|
@ExceptionHandler(ApiException.class)
|
||||||
|
ResponseEntity<ErrorResponse> api(ApiException e, HttpServletRequest req) {
|
||||||
|
var body = body(e.code(), e.getMessage(), req);
|
||||||
|
var builder = ResponseEntity.status(e.status());
|
||||||
|
if (e.status() == HttpStatus.UNAUTHORIZED && e.authenticate() != null) {
|
||||||
|
builder.header("WWW-Authenticate", e.authenticate());
|
||||||
|
}
|
||||||
|
if (e.status() == HttpStatus.TOO_MANY_REQUESTS) {
|
||||||
|
builder.header("Retry-After", "1");
|
||||||
|
}
|
||||||
|
return builder.body(body);
|
||||||
|
}
|
||||||
|
@ExceptionHandler({MethodArgumentNotValidException.class, MethodArgumentTypeMismatchException.class, HttpMessageNotReadableException.class})
|
||||||
|
ResponseEntity<ErrorResponse> bad(Exception e, HttpServletRequest req) { return ResponseEntity.badRequest().body(body("invalid_request", "Invalid request", req)); }
|
||||||
|
@ExceptionHandler(HttpMediaTypeNotSupportedException.class)
|
||||||
|
ResponseEntity<ErrorResponse> unsupported(HttpServletRequest req) { return ResponseEntity.status(415).body(body("unsupported_media_type", "Unsupported media type", req)); }
|
||||||
|
@ExceptionHandler(DataAccessException.class)
|
||||||
|
ResponseEntity<ErrorResponse> unavailable(DataAccessException e, HttpServletRequest req) {
|
||||||
|
return ResponseEntity.status(503).body(body("unavailable", "Storage temporarily unavailable", req));
|
||||||
|
}
|
||||||
|
@ExceptionHandler(ObjectStorageException.class)
|
||||||
|
ResponseEntity<ErrorResponse> storageUnavailable(ObjectStorageException e, HttpServletRequest req) {
|
||||||
|
return ResponseEntity.status(503).body(body("unavailable", "Object storage temporarily unavailable", req));
|
||||||
|
}
|
||||||
|
@ExceptionHandler(Exception.class)
|
||||||
|
ResponseEntity<ErrorResponse> internal(Exception e, HttpServletRequest req) { return ResponseEntity.status(500).body(body("internal_error", "Internal server error", req)); }
|
||||||
|
private ErrorResponse body(
|
||||||
|
String code,
|
||||||
|
String message,
|
||||||
|
HttpServletRequest req
|
||||||
|
) {
|
||||||
|
String id = (String) req.getAttribute(RequestIdFilter.ATTRIBUTE);
|
||||||
|
|
||||||
|
return new ErrorResponse(
|
||||||
|
new ErrorResponse.ErrorBody(code, message, id)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,47 @@
|
|||||||
|
package com.example.mtusi_messenger.web;
|
||||||
|
|
||||||
|
import jakarta.servlet.FilterChain;
|
||||||
|
import jakarta.servlet.ServletException;
|
||||||
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
|
import jakarta.servlet.http.HttpServletResponse;
|
||||||
|
import org.springframework.core.Ordered;
|
||||||
|
import org.springframework.core.annotation.Order;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import org.springframework.web.filter.OncePerRequestFilter;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.net.InetAddress;
|
||||||
|
import java.net.UnknownHostException;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
@Order(Ordered.HIGHEST_PRECEDENCE + 1)
|
||||||
|
public class InstanceIdFilter extends OncePerRequestFilter {
|
||||||
|
public static final String HEADER = "X-Instance-Id";
|
||||||
|
private final String instanceId;
|
||||||
|
|
||||||
|
public InstanceIdFilter() {
|
||||||
|
String configured = System.getenv("INSTANCE_ID");
|
||||||
|
this.instanceId = configured == null || configured.isBlank()
|
||||||
|
? localHostOrRandom()
|
||||||
|
: configured;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void doFilterInternal(
|
||||||
|
HttpServletRequest request,
|
||||||
|
HttpServletResponse response,
|
||||||
|
FilterChain filterChain
|
||||||
|
) throws ServletException, IOException {
|
||||||
|
response.setHeader(HEADER, instanceId);
|
||||||
|
filterChain.doFilter(request, response);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String localHostOrRandom() {
|
||||||
|
try {
|
||||||
|
return InetAddress.getLocalHost().getHostName();
|
||||||
|
} catch (UnknownHostException e) {
|
||||||
|
return "api-" + UUID.randomUUID();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
package com.example.mtusi_messenger.web;
|
||||||
|
|
||||||
|
import jakarta.validation.Constraint;
|
||||||
|
import jakarta.validation.Payload;
|
||||||
|
|
||||||
|
import java.lang.annotation.Documented;
|
||||||
|
import java.lang.annotation.Retention;
|
||||||
|
import java.lang.annotation.Target;
|
||||||
|
|
||||||
|
import static java.lang.annotation.ElementType.*;
|
||||||
|
import static java.lang.annotation.RetentionPolicy.RUNTIME;
|
||||||
|
|
||||||
|
@Documented
|
||||||
|
@Constraint(validatedBy = UnicodeBlankValidator.class)
|
||||||
|
@Target({ FIELD, PARAMETER, RECORD_COMPONENT, ANNOTATION_TYPE })
|
||||||
|
@Retention(RUNTIME)
|
||||||
|
public @interface NotUnicodeBlank {
|
||||||
|
String message() default "must not be blank";
|
||||||
|
Class<?>[] groups() default {};
|
||||||
|
Class<? extends Payload>[] payload() default {};
|
||||||
|
}
|
||||||
@@ -0,0 +1,56 @@
|
|||||||
|
package com.example.mtusi_messenger.web;
|
||||||
|
|
||||||
|
import com.example.mtusi_messenger.dto.ErrorResponse;
|
||||||
|
import com.example.mtusi_messenger.service.ReadinessService;
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import jakarta.servlet.FilterChain;
|
||||||
|
import jakarta.servlet.ServletException;
|
||||||
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
|
import jakarta.servlet.http.HttpServletResponse;
|
||||||
|
import org.springframework.http.MediaType;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import org.springframework.web.filter.OncePerRequestFilter;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class ReadinessGuardFilter extends OncePerRequestFilter {
|
||||||
|
private final ReadinessService readiness;
|
||||||
|
private final ObjectMapper mapper;
|
||||||
|
|
||||||
|
public ReadinessGuardFilter(ReadinessService readiness, ObjectMapper mapper) {
|
||||||
|
this.readiness = readiness;
|
||||||
|
this.mapper = mapper;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected boolean shouldNotFilter(HttpServletRequest request) {
|
||||||
|
String path = request.getRequestURI();
|
||||||
|
return path.startsWith("/health/")
|
||||||
|
|| path.equals("/swagger-ui.html")
|
||||||
|
|| path.startsWith("/swagger-ui/")
|
||||||
|
|| path.startsWith("/v3/api-docs/")
|
||||||
|
|| path.equals("/v3/api-docs");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void doFilterInternal(
|
||||||
|
HttpServletRequest request,
|
||||||
|
HttpServletResponse response,
|
||||||
|
FilterChain filterChain
|
||||||
|
) throws ServletException, IOException {
|
||||||
|
if (readiness.acceptsBusinessTraffic()) {
|
||||||
|
filterChain.doFilter(request, response);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
response.setStatus(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
|
||||||
|
response.setContentType(MediaType.APPLICATION_JSON_VALUE);
|
||||||
|
String requestId = (String) request.getAttribute(RequestIdFilter.ATTRIBUTE);
|
||||||
|
mapper.writeValue(response.getOutputStream(), new ErrorResponse(
|
||||||
|
new ErrorResponse.ErrorBody(
|
||||||
|
"unavailable",
|
||||||
|
"Application is not ready to serve business traffic",
|
||||||
|
requestId)));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,40 @@
|
|||||||
|
package com.example.mtusi_messenger.web;
|
||||||
|
|
||||||
|
import jakarta.servlet.FilterChain;
|
||||||
|
import jakarta.servlet.ServletException;
|
||||||
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
|
import jakarta.servlet.http.HttpServletResponse;
|
||||||
|
import org.slf4j.MDC;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import org.springframework.core.Ordered;
|
||||||
|
import org.springframework.core.annotation.Order;
|
||||||
|
import org.springframework.web.filter.OncePerRequestFilter;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
@Order(Ordered.HIGHEST_PRECEDENCE)
|
||||||
|
public class RequestIdFilter extends OncePerRequestFilter {
|
||||||
|
|
||||||
|
public static final String HEADER = "X-Request-Id";
|
||||||
|
public static final String ATTRIBUTE = "request_id";
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void doFilterInternal(
|
||||||
|
HttpServletRequest req,
|
||||||
|
HttpServletResponse res,
|
||||||
|
FilterChain chain
|
||||||
|
) throws ServletException, IOException {
|
||||||
|
|
||||||
|
String id = UUID.randomUUID().toString();
|
||||||
|
|
||||||
|
req.setAttribute(ATTRIBUTE, id);
|
||||||
|
res.setHeader(HEADER, id);
|
||||||
|
|
||||||
|
try {
|
||||||
|
chain.doFilter(req, res);
|
||||||
|
} finally {
|
||||||
|
MDC.remove(ATTRIBUTE);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
package com.example.mtusi_messenger.web;
|
||||||
|
|
||||||
|
import jakarta.validation.ConstraintValidator;
|
||||||
|
import jakarta.validation.ConstraintValidatorContext;
|
||||||
|
|
||||||
|
public class UnicodeBlankValidator
|
||||||
|
implements ConstraintValidator<NotUnicodeBlank, String> {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isValid(
|
||||||
|
String value,
|
||||||
|
ConstraintValidatorContext context) {
|
||||||
|
|
||||||
|
if (value == null || value.isEmpty()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return value.codePoints().anyMatch(codePoint ->
|
||||||
|
!Character.isWhitespace(codePoint)
|
||||||
|
&& !Character.isSpaceChar(codePoint)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,35 @@
|
|||||||
|
package com.example.mtusi_messenger.web;
|
||||||
|
import jakarta.validation.Constraint;
|
||||||
|
import jakarta.validation.Payload;
|
||||||
|
|
||||||
|
import java.lang.annotation.Documented;
|
||||||
|
import java.lang.annotation.Retention;
|
||||||
|
import java.lang.annotation.Target;
|
||||||
|
|
||||||
|
import static java.lang.annotation.ElementType.ANNOTATION_TYPE;
|
||||||
|
import static java.lang.annotation.ElementType.FIELD;
|
||||||
|
import static java.lang.annotation.ElementType.METHOD;
|
||||||
|
import static java.lang.annotation.ElementType.PARAMETER;
|
||||||
|
import static java.lang.annotation.RetentionPolicy.RUNTIME;
|
||||||
|
|
||||||
|
@Documented
|
||||||
|
@Constraint(validatedBy = UnicodeSizeValidator.class)
|
||||||
|
@Target({
|
||||||
|
FIELD,
|
||||||
|
METHOD,
|
||||||
|
PARAMETER,
|
||||||
|
ANNOTATION_TYPE
|
||||||
|
})
|
||||||
|
@Retention(RUNTIME)
|
||||||
|
public @interface UnicodeSize {
|
||||||
|
|
||||||
|
String message() default "size must be between {min} and {max} Unicode code points";
|
||||||
|
|
||||||
|
int min() default 0;
|
||||||
|
|
||||||
|
int max() default Integer.MAX_VALUE;
|
||||||
|
|
||||||
|
Class<?>[] groups() default {};
|
||||||
|
|
||||||
|
Class<? extends Payload>[] payload() default {};
|
||||||
|
}
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
package com.example.mtusi_messenger.web;
|
||||||
|
|
||||||
|
import jakarta.validation.ConstraintValidator;
|
||||||
|
import jakarta.validation.ConstraintValidatorContext;
|
||||||
|
|
||||||
|
public class UnicodeSizeValidator
|
||||||
|
implements ConstraintValidator<UnicodeSize, String> {
|
||||||
|
|
||||||
|
private int min;
|
||||||
|
private int max;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void initialize(UnicodeSize annotation) {
|
||||||
|
this.min = annotation.min();
|
||||||
|
this.max = annotation.max();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isValid(
|
||||||
|
String value,
|
||||||
|
ConstraintValidatorContext context
|
||||||
|
) {
|
||||||
|
if (value == null) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
int codePointCount = value.codePointCount(0, value.length());
|
||||||
|
|
||||||
|
return codePointCount >= min
|
||||||
|
&& codePointCount <= max;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,77 @@
|
|||||||
|
spring:
|
||||||
|
application:
|
||||||
|
name: mtusi-messenger
|
||||||
|
|
||||||
|
datasource:
|
||||||
|
url: ${DATABASE_URL:jdbc:postgresql://localhost:5432/messenger}
|
||||||
|
username: ${DATABASE_USER:messenger}
|
||||||
|
password: ${DATABASE_PASSWORD:messenger}
|
||||||
|
hikari:
|
||||||
|
maximum-pool-size: ${DATABASE_POOL_SIZE:10}
|
||||||
|
minimum-idle: ${DATABASE_MIN_IDLE:2}
|
||||||
|
connection-timeout: 5000
|
||||||
|
validation-timeout: 2000
|
||||||
|
idle-timeout: 600000
|
||||||
|
max-lifetime: 1800000
|
||||||
|
|
||||||
|
data:
|
||||||
|
redis:
|
||||||
|
host: ${REDIS_HOST:localhost}
|
||||||
|
port: ${REDIS_PORT:6379}
|
||||||
|
connect-timeout: ${REDIS_CONNECT_TIMEOUT:1s}
|
||||||
|
timeout: ${REDIS_COMMAND_TIMEOUT:500ms}
|
||||||
|
lettuce:
|
||||||
|
pool:
|
||||||
|
max-active: ${REDIS_POOL_MAX_ACTIVE:8}
|
||||||
|
max-idle: ${REDIS_POOL_MAX_IDLE:8}
|
||||||
|
min-idle: ${REDIS_POOL_MIN_IDLE:0}
|
||||||
|
max-wait: ${REDIS_POOL_MAX_WAIT:500ms}
|
||||||
|
|
||||||
|
jdbc:
|
||||||
|
template:
|
||||||
|
query-timeout: ${DATABASE_QUERY_TIMEOUT_SECONDS:5}
|
||||||
|
|
||||||
|
transaction:
|
||||||
|
default-timeout: ${DATABASE_TRANSACTION_TIMEOUT_SECONDS:5}
|
||||||
|
|
||||||
|
flyway:
|
||||||
|
# API replicas never race one another for migrations. Compose runs the
|
||||||
|
# same image once as the dedicated migrate service with this overridden.
|
||||||
|
enabled: ${FLYWAY_ENABLED:false}
|
||||||
|
locations: classpath:db/migration
|
||||||
|
|
||||||
|
jackson:
|
||||||
|
deserialization:
|
||||||
|
fail-on-unknown-properties: true
|
||||||
|
|
||||||
|
lifecycle:
|
||||||
|
timeout-per-shutdown-phase: 10s
|
||||||
|
|
||||||
|
server:
|
||||||
|
address: ${APP_HOST:0.0.0.0}
|
||||||
|
port: ${APP_PORT:8080}
|
||||||
|
shutdown: graceful
|
||||||
|
|
||||||
|
messenger:
|
||||||
|
storage: postgres
|
||||||
|
session:
|
||||||
|
ttl-seconds: ${SESSION_TTL_SECONDS:3600}
|
||||||
|
cache:
|
||||||
|
user-ttl-seconds: ${USER_CACHE_TTL_SECONDS:300}
|
||||||
|
idempotency:
|
||||||
|
ttl-seconds: ${IDEMPOTENCY_TTL_SECONDS:86400}
|
||||||
|
s3:
|
||||||
|
endpoint: ${S3_ENDPOINT:http://localhost:9000}
|
||||||
|
region: ${S3_REGION:us-east-1}
|
||||||
|
bucket: ${S3_BUCKET:messenger-attachments}
|
||||||
|
access-key: ${S3_ACCESS_KEY_ID:messenger-api}
|
||||||
|
secret-key: ${S3_SECRET_ACCESS_KEY:messenger-api-local-only}
|
||||||
|
attachments:
|
||||||
|
max-upload-bytes: ${MAX_UPLOAD_BYTES:10485760}
|
||||||
|
max-image-pixels: ${MAX_IMAGE_PIXELS:40000000}
|
||||||
|
max-concurrent-uploads: ${MAX_CONCURRENT_UPLOADS:3}
|
||||||
|
upload-permit-wait-millis: ${UPLOAD_PERMIT_WAIT_MILLIS:10000}
|
||||||
|
cleanup-grace-seconds: ${ATTACHMENT_CLEANUP_GRACE_SECONDS:3600}
|
||||||
|
cleanup-dry-run: ${ATTACHMENT_CLEANUP_DRY_RUN:false}
|
||||||
|
reconcile-interval-millis: ${ATTACHMENT_RECONCILE_INTERVAL_MILLIS:600000}
|
||||||
|
reconcile-initial-delay-millis: ${ATTACHMENT_RECONCILE_INITIAL_DELAY_MILLIS:60000}
|
||||||
@@ -0,0 +1,60 @@
|
|||||||
|
CREATE TABLE users (
|
||||||
|
id uuid PRIMARY KEY,
|
||||||
|
username varchar(32) NOT NULL,
|
||||||
|
display_name text NOT NULL,
|
||||||
|
created_at timestamptz NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
|
||||||
|
CONSTRAINT users_username_uk UNIQUE (username),
|
||||||
|
CONSTRAINT users_username_format_chk
|
||||||
|
CHECK (username ~ '^[a-z0-9_]{3,32}$'),
|
||||||
|
CONSTRAINT users_display_name_length_chk
|
||||||
|
CHECK (char_length(display_name) BETWEEN 1 AND 100)
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE TABLE chats (
|
||||||
|
id uuid PRIMARY KEY,
|
||||||
|
title text NOT NULL,
|
||||||
|
created_at timestamptz NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
|
||||||
|
CONSTRAINT chats_title_length_chk
|
||||||
|
CHECK (char_length(title) BETWEEN 1 AND 100)
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE TABLE chat_members (
|
||||||
|
chat_id uuid NOT NULL,
|
||||||
|
user_id uuid NOT NULL,
|
||||||
|
joined_at timestamptz NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
|
||||||
|
CONSTRAINT chat_members_pk PRIMARY KEY (chat_id, user_id),
|
||||||
|
CONSTRAINT chat_members_chat_fk
|
||||||
|
FOREIGN KEY (chat_id) REFERENCES chats(id) ON DELETE CASCADE,
|
||||||
|
CONSTRAINT chat_members_user_fk
|
||||||
|
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE RESTRICT
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE TABLE messages (
|
||||||
|
id uuid PRIMARY KEY,
|
||||||
|
chat_id uuid NOT NULL,
|
||||||
|
sender_id uuid NOT NULL,
|
||||||
|
text text NOT NULL,
|
||||||
|
created_at timestamptz NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
|
||||||
|
CONSTRAINT messages_text_length_chk
|
||||||
|
CHECK (char_length(text) BETWEEN 1 AND 4000),
|
||||||
|
CONSTRAINT messages_sender_membership_fk
|
||||||
|
FOREIGN KEY (chat_id, sender_id)
|
||||||
|
REFERENCES chat_members(chat_id, user_id)
|
||||||
|
ON DELETE RESTRICT
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE INDEX chat_members_user_chat_idx
|
||||||
|
ON chat_members (user_id, chat_id);
|
||||||
|
|
||||||
|
CREATE INDEX chats_created_id_idx
|
||||||
|
ON chats (created_at, id);
|
||||||
|
|
||||||
|
CREATE INDEX messages_chat_created_id_idx
|
||||||
|
ON messages (chat_id, created_at, id);
|
||||||
|
|
||||||
|
CREATE INDEX messages_sender_created_idx
|
||||||
|
ON messages (sender_id, created_at);
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
ALTER TABLE users
|
||||||
|
ADD COLUMN password_hash text;
|
||||||
|
|
||||||
|
ALTER TABLE users
|
||||||
|
ALTER COLUMN password_hash SET NOT NULL;
|
||||||
|
|
||||||
|
CREATE TABLE sessions (
|
||||||
|
id uuid PRIMARY KEY,
|
||||||
|
user_id uuid NOT NULL,
|
||||||
|
token_hash bytea NOT NULL,
|
||||||
|
created_at timestamptz NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
expires_at timestamptz NOT NULL,
|
||||||
|
revoked_at timestamptz,
|
||||||
|
|
||||||
|
CONSTRAINT sessions_user_fk
|
||||||
|
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE,
|
||||||
|
CONSTRAINT sessions_token_hash_uk UNIQUE (token_hash),
|
||||||
|
CONSTRAINT sessions_token_hash_length_chk
|
||||||
|
CHECK (octet_length(token_hash) = 32),
|
||||||
|
CONSTRAINT sessions_expiry_chk
|
||||||
|
CHECK (expires_at > created_at),
|
||||||
|
CONSTRAINT sessions_revocation_chk
|
||||||
|
CHECK (revoked_at IS NULL OR revoked_at >= created_at)
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE INDEX sessions_active_expiry_idx
|
||||||
|
ON sessions (expires_at)
|
||||||
|
WHERE revoked_at IS NULL;
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
CREATE TABLE message_idempotency (
|
||||||
|
user_id uuid NOT NULL,
|
||||||
|
method varchar(8) NOT NULL,
|
||||||
|
request_path text NOT NULL,
|
||||||
|
idempotency_key varchar(128) NOT NULL,
|
||||||
|
request_hash bytea NOT NULL,
|
||||||
|
message_id uuid NOT NULL,
|
||||||
|
created_at timestamptz NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
|
||||||
|
CONSTRAINT message_idempotency_pk
|
||||||
|
PRIMARY KEY (user_id, method, request_path, idempotency_key),
|
||||||
|
CONSTRAINT message_idempotency_user_fk
|
||||||
|
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE,
|
||||||
|
CONSTRAINT message_idempotency_message_fk
|
||||||
|
FOREIGN KEY (message_id) REFERENCES messages(id) ON DELETE CASCADE,
|
||||||
|
CONSTRAINT message_idempotency_method_chk
|
||||||
|
CHECK (method = 'POST'),
|
||||||
|
CONSTRAINT message_idempotency_key_format_chk
|
||||||
|
CHECK (idempotency_key ~ '^[A-Za-z0-9._:-]{1,128}$'),
|
||||||
|
CONSTRAINT message_idempotency_hash_length_chk
|
||||||
|
CHECK (octet_length(request_hash) = 32)
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE INDEX message_idempotency_created_idx
|
||||||
|
ON message_idempotency (created_at);
|
||||||
@@ -0,0 +1,53 @@
|
|||||||
|
ALTER TABLE messages DROP CONSTRAINT messages_text_length_chk;
|
||||||
|
ALTER TABLE messages ADD CONSTRAINT messages_text_length_chk
|
||||||
|
CHECK (char_length(text) BETWEEN 0 AND 4000);
|
||||||
|
ALTER TABLE messages ADD CONSTRAINT messages_id_chat_uk UNIQUE (id, chat_id);
|
||||||
|
|
||||||
|
CREATE TABLE attachments (
|
||||||
|
id uuid PRIMARY KEY,
|
||||||
|
chat_id uuid NOT NULL,
|
||||||
|
owner_id uuid NOT NULL,
|
||||||
|
filename varchar(255) NOT NULL,
|
||||||
|
content_type varchar(32) NOT NULL,
|
||||||
|
size_bytes bigint NOT NULL,
|
||||||
|
sha256 char(64) NOT NULL,
|
||||||
|
object_key text NOT NULL,
|
||||||
|
status varchar(16) NOT NULL DEFAULT 'ready',
|
||||||
|
created_at timestamptz NOT NULL,
|
||||||
|
updated_at timestamptz NOT NULL,
|
||||||
|
|
||||||
|
CONSTRAINT attachments_id_chat_uk UNIQUE (id, chat_id),
|
||||||
|
CONSTRAINT attachments_object_key_uk UNIQUE (object_key),
|
||||||
|
CONSTRAINT attachments_owner_membership_fk
|
||||||
|
FOREIGN KEY (chat_id, owner_id)
|
||||||
|
REFERENCES chat_members(chat_id, user_id) ON DELETE RESTRICT,
|
||||||
|
CONSTRAINT attachments_content_type_chk
|
||||||
|
CHECK (content_type IN ('image/png', 'image/jpeg')),
|
||||||
|
CONSTRAINT attachments_size_chk
|
||||||
|
CHECK (size_bytes BETWEEN 1 AND 10485760),
|
||||||
|
CONSTRAINT attachments_sha256_chk
|
||||||
|
CHECK (sha256 ~ '^[a-f0-9]{64}$'),
|
||||||
|
CONSTRAINT attachments_status_chk CHECK (status = 'ready'),
|
||||||
|
CONSTRAINT attachments_filename_chk
|
||||||
|
CHECK (char_length(filename) BETWEEN 1 AND 255)
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE TABLE message_attachments (
|
||||||
|
message_id uuid NOT NULL,
|
||||||
|
chat_id uuid NOT NULL,
|
||||||
|
attachment_id uuid NOT NULL,
|
||||||
|
position smallint NOT NULL,
|
||||||
|
|
||||||
|
CONSTRAINT message_attachments_pk PRIMARY KEY (message_id, attachment_id),
|
||||||
|
CONSTRAINT message_attachments_position_uk UNIQUE (message_id, position),
|
||||||
|
CONSTRAINT message_attachments_message_fk
|
||||||
|
FOREIGN KEY (message_id, chat_id)
|
||||||
|
REFERENCES messages(id, chat_id) ON DELETE CASCADE,
|
||||||
|
CONSTRAINT message_attachments_attachment_fk
|
||||||
|
FOREIGN KEY (attachment_id, chat_id)
|
||||||
|
REFERENCES attachments(id, chat_id) ON DELETE RESTRICT,
|
||||||
|
CONSTRAINT message_attachments_position_chk CHECK (position BETWEEN 0 AND 9)
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE INDEX attachments_chat_created_idx ON attachments (chat_id, created_at, id);
|
||||||
|
CREATE INDEX message_attachments_attachment_idx ON message_attachments (attachment_id);
|
||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user