ЛР 4 — БВТ2304 — ivanstorozhev — на 4 #67
+19
-8
@@ -1,20 +1,31 @@
|
||||
# Copy to .env for your LOCAL synthetic test environment; never commit .env.
|
||||
# These are logical interface suggestions; map actual framework names in REPORT.md.
|
||||
APP_HOST=0.0.0.0
|
||||
APP_PORT=8080
|
||||
APP_ENV=lab
|
||||
SESSION_TTL_SECONDS=3600
|
||||
# Choose credentials locally. File paths contain no secret values.
|
||||
DATABASE_URL_FILE=/run/secrets/database_url
|
||||
# Optional on 3, required use on 4 in lab2; Redis OR Valkey:
|
||||
SESSION_STORE=postgres
|
||||
CACHE_URL_FILE=/run/secrets/cache_url
|
||||
API_REPLICAS=2
|
||||
IDEMPOTENCY_TTL_SECONDS=86400
|
||||
S3_ENDPOINT=http://s3:9000
|
||||
POSTGRES_USER=messenger
|
||||
POSTGRES_PASSWORD=messenger
|
||||
POSTGRES_DB=messenger
|
||||
SPRING_DATASOURCE_URL=jdbc:postgresql://postgres:5432/messenger
|
||||
SPRING_DATASOURCE_USERNAME=messenger
|
||||
SPRING_DATASOURCE_PASSWORD=messenger
|
||||
SPRING_DATA_REDIS_HOST=redis
|
||||
SPRING_DATA_REDIS_PORT=6379
|
||||
S3_ENDPOINT=http://s3:8333
|
||||
S3_REGION=us-east-1
|
||||
S3_BUCKET=messenger-attachments
|
||||
S3_ACCESS_KEY_ID_FILE=/run/secrets/s3_access_key_id
|
||||
S3_SECRET_ACCESS_KEY_FILE=/run/secrets/s3_secret_access_key
|
||||
S3_ACCESS_KEY=messenger
|
||||
S3_SECRET_KEY=messenger-lab-secret
|
||||
S3_BOOTSTRAP_ACCESS_KEY=messenger-bootstrap
|
||||
S3_BOOTSTRAP_SECRET_KEY=messenger-bootstrap-secret
|
||||
S3_ORPHAN_GRACE=2m
|
||||
MAX_UPLOAD_BYTES=10485760
|
||||
MAX_IMAGE_PIXELS=40000000
|
||||
# Course names kept for the report mapping. This stand passes credentials as env vars, not files.
|
||||
DATABASE_URL_FILE=/run/secrets/database_url
|
||||
CACHE_URL_FILE=/run/secrets/cache_url
|
||||
S3_ACCESS_KEY_ID_FILE=/run/secrets/s3_access_key_id
|
||||
S3_SECRET_ACCESS_KEY_FILE=/run/secrets/s3_secret_access_key
|
||||
|
||||
@@ -0,0 +1,311 @@
|
||||
# Демонстрация лабораторной 4
|
||||
|
||||
Один терминал в корне репозитория. Блоки идут сверху вниз, в том же шелле: функции и переменные сохраняются. Пароль учебный, из `contracts/examples.json`. Токен появится на экране; в Git и в отчёт его не копировать.
|
||||
|
||||
Что сказать в начале: клиент ходит только в HAProxy на `127.0.0.1:8080`. За ним две реплики API. PostgreSQL хранит пользователей, чаты, сообщения, сессии, ключи идемпотентности и метаданные вложений. Байты лежат в SeaweedFS, один bucket на обе реплики. Redis только кэширует разбор Bearer. На диске API оригинала нет, sticky session нет.
|
||||
|
||||
## 1. Поднять стенд
|
||||
|
||||
```sh
|
||||
cp -n .env.example .env
|
||||
docker compose --env-file .env up -d --build
|
||||
until curl -fsS http://127.0.0.1:8080/health/ready >/dev/null; do sleep 1; done
|
||||
docker compose --env-file .env ps -a
|
||||
```
|
||||
|
||||
Ожидаем `api` в двух копиях (`healthy`), `lb`, `postgres`, `redis`, `s3` запущены. `migrate` уже завершился с кодом 0: схему он применил один раз и вышел. Порт на хосте есть только у `lb`: `127.0.0.1:8080`. Порты API и S3 наружу не опубликованы.
|
||||
|
||||
## 2. Оболочка запросов
|
||||
|
||||
Дальше в том же шелле.
|
||||
|
||||
```sh
|
||||
export PATH="/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/homebrew/bin:$HOME/.docker/bin"
|
||||
export LANG=en_US.UTF-8
|
||||
BASE=http://127.0.0.1:8080
|
||||
PASSWORD='Example-only-not-a-real-secret-123'
|
||||
SUFFIX=$(date +%H%M%S)
|
||||
basic() {
|
||||
python3 -c 'import base64,sys; print(base64.b64encode(sys.argv[1].encode()).decode())' "$1"
|
||||
}
|
||||
call() {
|
||||
method=$1
|
||||
url_path=$2
|
||||
shift 2
|
||||
echo
|
||||
echo "=== $method $url_path ==="
|
||||
curl -sS -D /tmp/demo.hdr -o /tmp/demo.body -X "$method" "$BASE$url_path" "$@"
|
||||
head -n 1 /tmp/demo.hdr
|
||||
grep -iE '^(X-Request-Id|X-Instance-Id|WWW-Authenticate|Content-Type):' /tmp/demo.hdr || true
|
||||
if [ -s /tmp/demo.body ]; then
|
||||
python3 -c 'import json,sys; sys.stdout.reconfigure(encoding="utf-8"); print(json.dumps(json.load(sys.stdin), ensure_ascii=False, indent=2))' < /tmp/demo.body
|
||||
fi
|
||||
}
|
||||
fetch() {
|
||||
method=$1
|
||||
url_path=$2
|
||||
shift 2
|
||||
echo
|
||||
echo "=== $method $url_path ==="
|
||||
curl -sS -D /tmp/demo.hdr -o /tmp/demo.body -X "$method" "$BASE$url_path" "$@"
|
||||
head -n 1 /tmp/demo.hdr
|
||||
grep -iE '^(X-Request-Id|X-Instance-Id|Content-Type|Content-Length):' /tmp/demo.hdr || true
|
||||
echo "байт: $(wc -c < /tmp/demo.body | tr -d ' ')"
|
||||
}
|
||||
restore_api() {
|
||||
# compose start заново гоняет migrate из-за depends_on. Здесь нужен только остановленный API.
|
||||
ids=$(docker ps -aq --filter label=com.docker.compose.project=messenger-lab4 --filter label=com.docker.compose.service=api --filter status=exited)
|
||||
if [ -n "$ids" ]; then
|
||||
docker start $ids
|
||||
fi
|
||||
until [ "$(docker inspect -f '{{.State.Health.Status}}' messenger-lab4-api-1)" = healthy ] \
|
||||
&& [ "$(docker inspect -f '{{.State.Health.Status}}' messenger-lab4-api-2)" = healthy ]
|
||||
do
|
||||
sleep 1
|
||||
done
|
||||
}
|
||||
```
|
||||
|
||||
`call` печатает JSON. `fetch` забирает байты файла и JSON не разбирает. На каждом ответе API есть `X-Request-Id` и `X-Instance-Id`. Ошибки соединения от самого HAProxy этого заголовка могут не иметь.
|
||||
|
||||
## 3. Пользователи, одна сессия, чат
|
||||
|
||||
```sh
|
||||
call POST /api/v1/users -H 'Content-Type: application/json' \
|
||||
-d "{\"username\":\"alice$SUFFIX\",\"display_name\":\"Алиса\",\"password\":\"$PASSWORD\"}"
|
||||
ALICE=$(python3 -c 'import json; print(json.load(open("/tmp/demo.body"))["id"])')
|
||||
|
||||
call POST /api/v1/users -H 'Content-Type: application/json' \
|
||||
-d "{\"username\":\"bob$SUFFIX\",\"display_name\":\"Боб\",\"password\":\"$PASSWORD\"}"
|
||||
BOB=$(python3 -c 'import json; print(json.load(open("/tmp/demo.body"))["id"])')
|
||||
|
||||
call POST /api/v1/users -H 'Content-Type: application/json' \
|
||||
-d "{\"username\":\"eve$SUFFIX\",\"display_name\":\"Ева\",\"password\":\"$PASSWORD\"}"
|
||||
EVE=$(python3 -c 'import json; print(json.load(open("/tmp/demo.body"))["id"])')
|
||||
|
||||
call POST /api/v1/auth/sessions -H "Authorization: Basic $(basic "alice${SUFFIX}:${PASSWORD}")"
|
||||
TOKEN=$(python3 -c 'import json; print(json.load(open("/tmp/demo.body"))["token"])')
|
||||
|
||||
call POST /api/v1/chats -H "Authorization: Bearer $TOKEN" -H 'Content-Type: application/json' \
|
||||
-d "{\"title\":\"Проект-$SUFFIX\",\"member_ids\":[\"$ALICE\",\"$BOB\"]}"
|
||||
CHAT=$(python3 -c 'import json; print(json.load(open("/tmp/demo.body"))["id"])')
|
||||
|
||||
call POST /api/v1/auth/sessions -H "Authorization: Basic $(basic "bob${SUFFIX}:${PASSWORD}")"
|
||||
BOB_TOKEN=$(python3 -c 'import json; print(json.load(open("/tmp/demo.body"))["token"])')
|
||||
|
||||
call POST /api/v1/auth/sessions -H "Authorization: Basic $(basic "eve${SUFFIX}:${PASSWORD}")"
|
||||
EVE_TOKEN=$(python3 -c 'import json; print(json.load(open("/tmp/demo.body"))["token"])')
|
||||
```
|
||||
|
||||
Регистрация — `201` без поля `password`. Вход — `201`, `token_type=Bearer`. Чат создаётся одним запросом, в нём Алиса и Боб. Ева в чат не входит.
|
||||
|
||||
## 4. Идемпотентность сообщения
|
||||
|
||||
Ключ обязателен. Повтор того же текста возвращает то же сообщение. Другой текст с тем же ключом — `409`. Без ключа — `400`.
|
||||
|
||||
```sh
|
||||
KEY="demo-$SUFFIX"
|
||||
call POST "/api/v1/chats/$CHAT/messages" \
|
||||
-H "Authorization: Bearer $TOKEN" \
|
||||
-H "Idempotency-Key: $KEY" \
|
||||
-H 'Content-Type: application/json' \
|
||||
-d '{"text":"Привет, мир"}'
|
||||
FIRST=$(python3 -c 'import json; print(json.load(open("/tmp/demo.body"))["id"])')
|
||||
|
||||
call POST "/api/v1/chats/$CHAT/messages" \
|
||||
-H "Authorization: Bearer $TOKEN" \
|
||||
-H "Idempotency-Key: $KEY" \
|
||||
-H 'Content-Type: application/json' \
|
||||
-d '{"text":"Привет, мир"}'
|
||||
SECOND=$(python3 -c 'import json; print(json.load(open("/tmp/demo.body"))["id"])')
|
||||
echo "ids: $FIRST и $SECOND"
|
||||
|
||||
call POST "/api/v1/chats/$CHAT/messages" \
|
||||
-H "Authorization: Bearer $TOKEN" \
|
||||
-H "Idempotency-Key: $KEY" \
|
||||
-H 'Content-Type: application/json' \
|
||||
-d '{"text":"другой текст"}'
|
||||
|
||||
call POST "/api/v1/chats/$CHAT/messages" \
|
||||
-H "Authorization: Bearer $TOKEN" \
|
||||
-H 'Content-Type: application/json' \
|
||||
-d '{"text":"без ключа"}'
|
||||
```
|
||||
|
||||
Два id совпадают, оба ответа `201`. Третий запрос — `409`. Четвёртый — `400`. Область ключа: этот пользователь, `POST`, путь этого чата и сам ключ.
|
||||
|
||||
## 5. Вложение, сообщение, чужой пользователь
|
||||
|
||||
Загрузка — сырые байты, не multipart. Ключ объекта назначает сервер, в ответе его нет. Пустой текст допустим, потому что у сообщения есть вложение.
|
||||
|
||||
```sh
|
||||
call POST "/api/v1/chats/$CHAT/attachments?filename=sample.png" \
|
||||
-H "Authorization: Bearer $TOKEN" \
|
||||
-H 'Content-Type: image/png' \
|
||||
--data-binary @tests/fixtures/sample.png
|
||||
ATT=$(python3 -c 'import json; print(json.load(open("/tmp/demo.body"))["id"])')
|
||||
echo "status=$(python3 -c 'import json; print(json.load(open("/tmp/demo.body"))["status"])')"
|
||||
python3 -c 'import json; d=json.load(open("/tmp/demo.body")); assert "object_key" not in d'
|
||||
|
||||
call POST "/api/v1/chats/$CHAT/messages" \
|
||||
-H "Authorization: Bearer $TOKEN" \
|
||||
-H "Idempotency-Key: file-$SUFFIX" \
|
||||
-H 'Content-Type: application/json' \
|
||||
-d "{\"text\":\"\",\"attachment_ids\":[\"$ATT\"]}"
|
||||
|
||||
fetch GET "/api/v1/attachments/$ATT/content" -H "Authorization: Bearer $BOB_TOKEN"
|
||||
cp /tmp/demo.body /tmp/demo.png
|
||||
python3 - <<'PY'
|
||||
import hashlib, pathlib
|
||||
got = hashlib.sha256(pathlib.Path("/tmp/demo.png").read_bytes()).hexdigest()
|
||||
src = hashlib.sha256(pathlib.Path("tests/fixtures/sample.png").read_bytes()).hexdigest()
|
||||
print(got)
|
||||
print("совпадает с fixture:", got == src)
|
||||
PY
|
||||
|
||||
call GET "/api/v1/attachments/$ATT" -H "Authorization: Bearer $EVE_TOKEN"
|
||||
fetch GET "/api/v1/attachments/$ATT/content" -H "Authorization: Bearer $EVE_TOKEN"
|
||||
|
||||
call POST "/api/v1/chats/$CHAT/attachments?filename=../secret.png" \
|
||||
-H "Authorization: Bearer $TOKEN" \
|
||||
-H 'Content-Type: image/png' \
|
||||
--data-binary @tests/fixtures/sample.png
|
||||
```
|
||||
|
||||
Загрузка — `201`, `status=ready`. Боб скачивает те же байты, `Content-Type: image/png`. Ева на метаданных и на байтах получает `404`. Имя `../secret.png` — `400`.
|
||||
|
||||
## 6. Одна сессия, две реплики
|
||||
|
||||
```sh
|
||||
rm -f /tmp/instances.txt
|
||||
i=0
|
||||
while [ "$i" -lt 16 ]; do
|
||||
call GET /api/v1/users/me -H "Authorization: Bearer $TOKEN" >/dev/null
|
||||
grep -i '^X-Instance-Id:' /tmp/demo.hdr | awk '{print $2}' | tr -d '\r' >> /tmp/instances.txt
|
||||
i=$((i + 1))
|
||||
done
|
||||
sort -u /tmp/instances.txt
|
||||
echo "уникальных реплик: $(sort -u /tmp/instances.txt | grep -c .)"
|
||||
docker inspect messenger-lab4-api-1 messenger-lab4-api-2 \
|
||||
--format '{{.Name}} hostname={{.Config.Hostname}} {{.State.Status}}'
|
||||
```
|
||||
|
||||
В файле два разных id. Они равны hostname контейнеров. Запросы Алисы идут по кругу, повторный вход не нужен.
|
||||
|
||||
Сохранить id для следующих шагов:
|
||||
|
||||
```sh
|
||||
sort -u /tmp/instances.txt > /tmp/instance-ids.txt
|
||||
FIRST_ID=$(sed -n '1p' /tmp/instance-ids.txt)
|
||||
SECOND_ID=$(sed -n '2p' /tmp/instance-ids.txt)
|
||||
echo "остановлю сначала $FIRST_ID, потом $SECOND_ID"
|
||||
```
|
||||
|
||||
## 7. Остановка первой реплики, тот же файл
|
||||
|
||||
```sh
|
||||
TARGET_INSTANCE_ID=$FIRST_ID ./scripts/stop-one.sh
|
||||
docker compose --env-file .env ps api
|
||||
rm -f /tmp/after-stop.txt
|
||||
i=0
|
||||
while [ "$i" -lt 8 ]; do
|
||||
call GET /api/v1/users/me -H "Authorization: Bearer $TOKEN" >/dev/null
|
||||
grep -i '^X-Instance-Id:' /tmp/demo.hdr | awk '{print $2}' | tr -d '\r' >> /tmp/after-stop.txt
|
||||
i=$((i + 1))
|
||||
done
|
||||
echo "отвечали: $(sort -u /tmp/after-stop.txt | tr '\n' ' ')"
|
||||
fetch GET "/api/v1/attachments/$ATT/content" -H "Authorization: Bearer $BOB_TOKEN"
|
||||
cmp -s /tmp/demo.body tests/fixtures/sample.png && echo "байты те же"
|
||||
```
|
||||
|
||||
Один контейнер `Exited`, код 143: это SIGTERM. В ответах остаётся только второй id. Боб тем же токеном получает тот же PNG. Файл не лежал на диске остановленной реплики: его отдал общий S3.
|
||||
|
||||
## 8. Вернуть первую и остановить вторую
|
||||
|
||||
```sh
|
||||
restore_api
|
||||
docker compose --env-file .env ps api
|
||||
|
||||
TARGET_INSTANCE_ID=$SECOND_ID ./scripts/stop-one.sh
|
||||
docker compose --env-file .env ps api
|
||||
fetch GET "/api/v1/attachments/$ATT/content" -H "Authorization: Bearer $BOB_TOKEN"
|
||||
cmp -s /tmp/demo.body tests/fixtures/sample.png && echo "байты те же"
|
||||
```
|
||||
|
||||
Теперь остановлена другая реплика. Скачивание проходит через оставшуюся. Скрипт не трогает PostgreSQL, Redis, SeaweedFS, HAProxy и volumes.
|
||||
|
||||
Поднять обе обратно:
|
||||
|
||||
```sh
|
||||
restore_api
|
||||
```
|
||||
|
||||
## 9. Официальный тест отказа
|
||||
|
||||
Тест сам создаёт данные, загружает fixture, 15 секунд пишет сообщения и в это время `stop-one.sh` гасит ту реплику, которая ответила на пробный запрос.
|
||||
|
||||
```sh
|
||||
make test-failover ACTION_SCRIPT=scripts/stop-one.sh
|
||||
docker compose --env-file .env ps api
|
||||
```
|
||||
|
||||
В строке JSON: не меньше 2 реплик до отказа, число успешных записей, число ошибок перехода и `first_recovery_seconds` не больше 10. Остановленный контейнер виден в `ps` как `Exited`. Вернуть его:
|
||||
|
||||
```sh
|
||||
restore_api
|
||||
curl -fsS "$BASE/health/ready"
|
||||
```
|
||||
|
||||
## 10. Рестарт всего стенда, тот же токен и тот же файл
|
||||
|
||||
```sh
|
||||
docker inspect messenger-lab4-api-1 messenger-lab4-postgres-1 messenger-lab4-redis-1 messenger-lab4-s3-1 \
|
||||
--format '{{.Name}} {{.State.StartedAt}}' > /tmp/started-before.txt
|
||||
./scripts/restart.sh
|
||||
until curl -fsS "$BASE/health/ready" >/dev/null; do sleep 1; done
|
||||
docker inspect messenger-lab4-api-1 messenger-lab4-postgres-1 messenger-lab4-redis-1 messenger-lab4-s3-1 \
|
||||
--format '{{.Name}} {{.State.StartedAt}}' > /tmp/started-after.txt
|
||||
echo '--- до ---'
|
||||
cat /tmp/started-before.txt
|
||||
echo '--- после ---'
|
||||
cat /tmp/started-after.txt
|
||||
docker volume ls --filter name=messenger-lab4
|
||||
fetch GET "/api/v1/attachments/$ATT/content" -H "Authorization: Bearer $BOB_TOKEN"
|
||||
cmp -s /tmp/demo.body tests/fixtures/sample.png && echo "после рестарта байты те же"
|
||||
```
|
||||
|
||||
`StartedAt` у API, PostgreSQL, Redis и S3 изменился. Имена volumes те же: `postgres-data`, `redis-data`, `s3-data`. Боб читает тот же файл тем же токеном, входить заново не нужно. `restart.sh` после готовности ждёт ещё 5 с: master SeaweedFS отвечает раньше, чем чтение объекта стабильно укладывается в таймаут клиента.
|
||||
|
||||
Официальные проверки:
|
||||
|
||||
```sh
|
||||
make test
|
||||
make test-persistence ACTION_SCRIPT=scripts/restart.sh
|
||||
```
|
||||
|
||||
`make test` создаёт новых пользователей и сверяет checksum fixture. Persistence перезапускает стенд и читает оригинал прежним токеном теста.
|
||||
|
||||
## 11. Миграции, лимиты, закрытый bucket, нет файла на API
|
||||
|
||||
```sh
|
||||
docker compose --env-file .env logs migrate | grep -E 'Run:|Previously run:'
|
||||
docker compose --env-file .env exec postgres \
|
||||
psql -U messenger -d messenger -c \
|
||||
'select id, dateexecuted from databasechangelog order by orderexecuted;'
|
||||
docker inspect messenger-lab4-api-1 messenger-lab4-api-2 messenger-lab4-lb-1 \
|
||||
messenger-lab4-postgres-1 messenger-lab4-redis-1 messenger-lab4-s3-1 \
|
||||
--format '{{.Name}} cpus_ns={{.HostConfig.NanoCpus}} memory={{.HostConfig.Memory}} ports={{json .HostConfig.PortBindings}} mounts={{json .Mounts}}'
|
||||
docker run --rm --network messenger-lab4_default curlimages/curl:8.11.1 \
|
||||
-sS -o /dev/null -w 'анонимный S3 %{http_code}\n' -m 5 \
|
||||
http://s3:8333/messenger-attachments/originals/does-not-matter
|
||||
```
|
||||
|
||||
У `migrate` в логе `Previously run: 6` и `Run: 0`. В `databasechangelog` шесть записей, последняя — вложения. У API `ports` пустые и `mounts` пустые: оригиналу некуда лечь на диск реплики. Наружу опубликован только `8080` у `lb`. Анонимный GET объекта — `403`. В `infra/s3.json` два пользователя: `messenger-api` без Admin и `messenger-bootstrap` только для создания bucket. Объект без строки в базе старше двух минут убирает фоновая очистка.
|
||||
|
||||
## 12. Остановить
|
||||
|
||||
```sh
|
||||
docker compose --env-file .env stop
|
||||
```
|
||||
|
||||
Данные на volumes остаются. Снять контейнеры, сохранив данные: `docker compose --env-file .env down`. Удалить учебные данные: `docker compose --env-file .env down -v`.
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
FROM maven:3.9.11-eclipse-temurin-21 AS build
|
||||
WORKDIR /src
|
||||
COPY backend/pom.xml backend/pom.xml
|
||||
COPY contracts contracts
|
||||
COPY backend/src backend/src
|
||||
RUN mvn -f backend/pom.xml -DskipTests package
|
||||
|
||||
FROM eclipse-temurin:21.0.8_9-jre-jammy
|
||||
RUN apt-get update \
|
||||
&& apt-get install -y --no-install-recommends curl \
|
||||
&& rm -rf /var/lib/apt/lists/* \
|
||||
&& useradd --uid 10001 --create-home --shell /usr/sbin/nologin app
|
||||
WORKDIR /app
|
||||
COPY --from=build /src/backend/target/messenger.jar /app/messenger.jar
|
||||
USER app
|
||||
EXPOSE 8080
|
||||
ENV LIQUIBASE_ANALYTICS_ENABLED=false
|
||||
HEALTHCHECK --interval=5s --timeout=3s --start-period=60s --retries=3 \
|
||||
CMD curl -fsS http://127.0.0.1:8080/health/ready || exit 1
|
||||
ENTRYPOINT ["java", "-jar", "/app/messenger.jar"]
|
||||
@@ -1,37 +1,100 @@
|
||||
# Отчёт — лабораторная 4
|
||||
|
||||
- Автор, группа: TODO
|
||||
- Целевая оценка; выбранный трек 5, если нужен: TODO
|
||||
- Автор, группа: ivanstorozhev, БВТ2304
|
||||
- Целевая оценка: 4
|
||||
- Трек 5: не выбран
|
||||
- Commit/PR: TODO после публикации
|
||||
- Использованные источники, библиотеки и помощь ИИ: TODO
|
||||
- Использованные источники, библиотеки и помощь ИИ: контракт `contracts/openapi.json` 1.4.0 и `contracts/README.md`; код lab3 (Spring Boot 4.1.1, Spring Data JPA, Liquibase, Redis-кэш сессий, HAProxy, Argon2id, MapStruct, OpenAPI Generator 7.17.0). Новое: AWS SDK for Java 2.31.50 (`s3`, `apache-client`), SeaweedFS 4.52 как S3. Помощь ИИ: реализация стенда и отчёта в Cursor.
|
||||
|
||||
## Запуск из чистого clone
|
||||
|
||||
TODO: версии runtime/образов, prerequisites, команды создания локальных secrets/.env, build, миграций, запуска, ожидания готовности, тестов и остановки. Указать необходимые действия оператора. Не вставлять секреты. Отдельно указать безопасную остановку и команду намеренного удаления учебных данных.
|
||||
Нужны Docker Desktop (проверено 28.5.1), Docker Compose и Python 3.9+. JDK 21 и Maven 3.9 нужны только для `mvn test` вне образа.
|
||||
|
||||
```sh
|
||||
cp .env.example .env
|
||||
# Перед общим стендом замените учебные пароли PostgreSQL и ключ S3 в .env.
|
||||
docker compose --env-file .env config --quiet
|
||||
docker compose --env-file .env up -d --build
|
||||
curl -fsS http://127.0.0.1:8080/health/ready
|
||||
make test
|
||||
make test-persistence ACTION_SCRIPT=scripts/restart.sh
|
||||
make test-failover ACTION_SCRIPT=scripts/stop-one.sh
|
||||
# Поднять остановленную реплику:
|
||||
docker compose --env-file .env start api
|
||||
```
|
||||
|
||||
Миграции выполняет одноразовый сервис `migrate`: тот же JAR с профилем `migrate`. Реплики `api` стартуют после него с `SPRING_LIQUIBASE_ENABLED=false`. Bucket `messenger-attachments` создаёт API при readiness (`headBucket`, при 404 — `createBucket`). Повторный старт тот же bucket не пересоздаёт.
|
||||
|
||||
Остановка без удаления данных: `docker compose --env-file .env stop`. Снятие контейнеров с сохранением volumes: `docker compose --env-file .env down`. Намеренное удаление учебных данных: `docker compose --env-file .env down -v`. Файл `.env` в Git не входит.
|
||||
|
||||
Свои проверки: `mvn -q test` в `backend/` (Testcontainers поднимает PostgreSQL, Redis и SeaweedFS).
|
||||
|
||||
## Архитектура и решения
|
||||
|
||||
TODO: схема процессов/хранилищ, источник истины, транзакционные границы, ограничения. Что перенесено из lab3, что изменено в этой работе. Соответствие переменных из .env.example реальной конфигурации.
|
||||
Клиент ходит только в HAProxy на `127.0.0.1:8080`. Две реплики `api` портов наружу не публикуют. Балансировка, сессии, идемпотентность и `X-Instance-Id` перенесены из lab3 без изменений смысла: PostgreSQL — источник истины пользователей, чатов, сообщений и сессий; Redis — только кэш разбора Bearer.
|
||||
|
||||
Байты вложения лежат в SeaweedFS (`chrislusf/seaweedfs:4.52`, команда `server -s3`), не в PostgreSQL и не на диске API. Образ MinIO с Docker Hub к этой дате снят, поэтому выбран SeaweedFS. Порты S3, master и filer наружу не опубликованы. Filer UI выключен (`-filer.exposeDirectoryData=false`). Анонимной личности в `infra/s3.json` нет.
|
||||
|
||||
Метаданные — таблица `attachments` (чат, владелец, отображаемое имя, MIME, размер, SHA-256, ключ объекта, статус `ready`). Связь с сообщением — `message_attachments` с порядком. Ключ объекта назначает сервер (`originals/{uuid}`) и в API не возвращается. Имя файла только для отображения: 1–255 кодовых точек, без `/`, `\` и управляющих символов.
|
||||
|
||||
`POST /api/v1/chats/{chat_id}/attachments` принимает сырые байты `image/png` или `image/jpeg`. Длина режется по `Content-Length` и по чтению не больше 10 MiB до разбора картинки. PNG и JPEG проверяются по сигнатуре и заголовкам (размеры, IEND/EOI), пиксели не распаковываются. Подмена MIME и мусор — 415, больше 10 MiB или 40 млн пикселей — 413. После PUT в S3 строка пишется в PostgreSQL. Если транзакция не прошла, этот же запрос удаляет только что записанный объект. Если процесс умер между PUT и COMMIT, объект остаётся без строки. Раз в минуту `OrphanSweeper` смотрит ключи `originals/` и удаляет те, у которых нет строки и `LastModified` старше `S3_ORPHAN_GRACE` (по умолчанию 2 минуты). Свежий объект не трогается: загрузка может ещё не закоммититься. Обрыв тела до конца чтения в S3 ничего не пишет. 201 возвращается со `status=ready`.
|
||||
|
||||
У API два ключа. `messenger-api` умеет Read/Write/List и ходит в PUT, GET, DELETE и список. `messenger-bootstrap` с Admin только создаёт bucket, если его ещё нет. Filer UI выключен.
|
||||
|
||||
Скачивание идёт через API (`GET /api/v1/attachments/{id}/content`), без редиректа. Участник чата получает те же байты и исходный `Content-Type`. Посторонний и вложение другого чата — 404. `variant` кроме `original` — 400.
|
||||
|
||||
Пустой текст сообщения допустим, только если есть хотя бы одно вложение. Нет поля `attachment_ids` и `[]` дают один и тот же ключ идемпотентности. Другой набор id при том же ключе — 409.
|
||||
|
||||
`/health/ready` включает процесс, PostgreSQL и S3. Redis в readiness не входит. `scripts/restart.sh` перезапускает lb, api, postgres, redis и s3, затем ждёт `/health/ready` и ещё 5 с: сразу после старта SeaweedFS master уже отвечает, а чтение объекта ещё может не успеть уложиться в таймаут клиента (2 с).
|
||||
|
||||
| Роль | Сервис | Образ | Готовность | Volume | Сеть | Credentials |
|
||||
| --- | --- | --- | --- | --- | --- | --- |
|
||||
| API | `api` × `API_REPLICAS` | `messenger-lab4:1.0.0`, сборка из `backend/` | `GET /health/ready` | нет локального файла оригинала | порт наружу не опубликован | env Spring и S3 |
|
||||
| Балансировщик | `lb` | `haproxy:3.2.6` | процесс слушает 8080 | нет | только `127.0.0.1:8080` | нет |
|
||||
| Метаданные | `postgres` | `postgres:16.10` | `pg_isready` | `postgres-data` | 5432 только внутри сети | `POSTGRES_*` из `.env` |
|
||||
| Кэш сессии | `redis` | `redis:7.4.6` | `redis-cli ping` | `redis-data` | 6379 только внутри сети | без пароля, сеть стенда |
|
||||
| Объекты | `s3` | `chrislusf/seaweedfs:4.52` | `GET :9333/cluster/status` | `s3-data` | 8333/9333 только внутри сети | API: Read/Write/List. Bootstrap: Admin, только создание bucket |
|
||||
| Миграции | `migrate` | тот же образ API | завершается с кодом 0 | нет | нет | тот же datasource |
|
||||
|
||||
Лимиты контейнеров: API 1 CPU и 1024 MiB на реплику, PostgreSQL 1 CPU и 512 MiB, Redis 0.25 CPU и 128 MiB, HAProxy 0.25 CPU и 128 MiB, S3 0.5 CPU и 512 MiB.
|
||||
|
||||
Соответствие `.env.example`: переменные lab3 те же. `S3_ENDPOINT` в Compose — `http://s3:8333`, регион `us-east-1`, bucket `messenger-attachments`. `MAX_UPLOAD_BYTES` и `MAX_IMAGE_PIXELS` задают лимиты загрузки. Имена `*_FILE` из курса оставлены комментарием: стенд передаёт доступ переменными окружения, не файлами секретов.
|
||||
|
||||
## Доказательства критериев
|
||||
|
||||
Стенд: Mac, arm64, 12 CPU, 24 GiB RAM, Docker 28.5.1. Прогон 5 ноября 2026.
|
||||
|
||||
| Критерий README | Команда/сценарий | Наблюдаемый результат | Файл доказательства |
|
||||
| --- | --- | --- | --- |
|
||||
| Публичный smoke | make test | TODO | TODO |
|
||||
| Остальные пункты уровня 3 | TODO | TODO | TODO |
|
||||
| Все пункты уровня 4, если заявлен | TODO | TODO | TODO |
|
||||
| Выбранный трек 5, если заявлен | TODO | TODO | TODO |
|
||||
|
||||
TODO: добавить отдельную строку для каждого заявленного критерия. PASS без команды и наблюдения не является доказательством. Проверка инфраструктурного hook требует подтверждения, какие реальные процессы/хранилища перезапущены.
|
||||
| Публичный smoke, checksum, чужой пользователь | `make test` | `PASS: lab4 public API scenario` | `evidence/results.txt` |
|
||||
| Две реплики, S3 и API без внешнего порта | `docker compose ps` и `docker inspect` | два API healthy, наружу только `lb` на `127.0.0.1:8080`; у `api` и `s3` host-порт `null` | `evidence/limits.txt`, `evidence/results.txt` |
|
||||
| Приватный bucket | анонимный GET объекта из контейнера в сети стенда | HTTP 403, ключей в запросе не было | `evidence/results.txt` |
|
||||
| Метаданные и оригинал переживают рестарт | `make test-persistence ACTION_SCRIPT=scripts/restart.sh` | `PASS: persistence preserved confirmed data and existing session.` Хук перезапускает lb, api, postgres, redis и s3, volumes не удаляет | `evidence/results.txt`, `scripts/restart.sh` |
|
||||
| Скачивание с другой реплики | `make test-failover ACTION_SCRIPT=scripts/stop-one.sh` на прогретом стенде | до отказа 2 instance id; 119 успешных записей, 1 ошибка на переходе, первая успешная запись через 1.665 с. `PASS`. В `ps` сразу после теста не было остановленной реплики `api-2`; `docker compose start api` вернул её в healthy | `evidence/results.txt` |
|
||||
| Слишком большой файл, порча, подмена MIME, чужой id, другой чат | `mvn -q test` | 10 МиБ+1 байт — 413 `payload_too_large`; битый PNG и JPEG под видом PNG — 415; `../secret.png` — 400; чужой id и вложение другого чата — 404 `not_found`; посторонний — 404. Ни один из этих ответов не 500 | `AttachmentIntegrationTest`, `ImageFilesTest` |
|
||||
| Сирота в S3 без строки | тот же прогон, `OrphanSweeper` | объект без строки удаляется, объект с строкой остаётся. Свежее `LastModified` правило не удаляет | `OrphanSweeperTest`, `AttachmentIntegrationTest` |
|
||||
| 10 одновременных загрузок, пик RAM | 10 POST по 8 МиБ параллельно, `docker stats` | все 10 ответов 201. До нагрузки около 389 MiB на реплику, пик 581.3 и 558.2 MiB при лимите 1024 MiB | `evidence/memory.txt` |
|
||||
| Возможности lab3 | те же тесты идемпотентности и стенд из lab3 | `IdempotencyIntegrationTest` входит в тот же прогон | `backend/src/test` |
|
||||
| Трек 5 | не заявлен | — | — |
|
||||
|
||||
## Эксперименты и отказы
|
||||
|
||||
TODO: гипотеза, hardware/лимиты, версии, объём данных, профиль нагрузки, concurrency/arrival rate, длительность/прогрев, повторы, успешные RPS, ошибки, p50/p95/p99, CPU/RAM. Сохранить raw summary без tokens и объяснить ограничения измерения.
|
||||
Память на загрузке ограничена 10 МиБ на запрос: тело читается кусками по 8 КиБ и обрывается на лишнем байте. Десять параллельных файлов по 8 МиБ дали пик 581.3 и 558.2 MiB на репликах при лимите 1024 MiB, все ответы 201. Клиент публичных тестов отказов обрывает запрос через 2 с.
|
||||
|
||||
| Воздействие | Что ожидаем | Что наблюдали | Время восстановления / потеря данных |
|
||||
| --- | --- | --- | --- |
|
||||
| TODO | TODO | TODO | TODO |
|
||||
| `scripts/restart.sh` перезапускает lb, api, postgres, redis, s3 | тот же Bearer читает сообщение и исходные байты | persistence-тест прошёл после паузы 5 с за `/health/ready` | подтверждённые метаданные и оригинал на месте |
|
||||
| Первый failover сразу после этого рестарта | загрузка fixture и затем остановка реплики | `TimeoutError` на POST вложения ещё до hook: чтение S3 не уложилось в 2 с | повтор на уже прогретом стенде прошёл |
|
||||
| `stop-one.sh` на прогретом стенде | другая реплика отдаёт тот же файл, остановленная сама не поднимается | 2 instance id до отказа, восстановление записи 1.665 с, `PASS`; остановленная реплика отсутствовала в `ps` | подтверждённые данные на месте, реплика поднята вручную `start api` |
|
||||
|
||||
## Ограничения и следующие шаги
|
||||
|
||||
TODO: какие отказы выдерживаются и какие нет; что не реализовано; где учебное упрощение; какое улучшение подтверждено, а какое пока гипотеза.
|
||||
Один Docker-host не переживает потерю этой машины. Единственные PostgreSQL, Redis, SeaweedFS и HAProxy остаются точками отказа.
|
||||
|
||||
Если процесс API умирает после успешного PUT и до COMMIT, объект живёт без строки не дольше grace period, затем его забирает sweeper. Обратная дыра, строка есть, а объекта нет, обнаруживается при скачивании как 404. Отдельного прохода, который чинит пропавшие объекты, нет: это трек 5.
|
||||
|
||||
Консоль наружу не открыта, анонимное чтение отвечает 403. Ключ Admin в процессе API есть, но им только создаётся bucket.
|
||||
|
||||
Тело больше примерно 12 MiB может обрезать Tomcat своим 413 до JSON приложения. Лимит контракта 10 MiB проверяется в приложении.
|
||||
|
||||
Пока HAProxy не снял backend, часть запросов попадает в останавливаемую реплику. Балансировщик POST не повторяет. Клиент может повторить тот же `Idempotency-Key`.
|
||||
|
||||
+199
@@ -0,0 +1,199 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>4.1.1</version>
|
||||
<relativePath/>
|
||||
</parent>
|
||||
<groupId>ru.mtuci</groupId>
|
||||
<artifactId>messenger</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<name>messenger</name>
|
||||
<description>MTUSI messenger lab 4</description>
|
||||
<properties>
|
||||
<java.version>21</java.version>
|
||||
<mapstruct.version>1.6.3</mapstruct.version>
|
||||
<openapi-generator.version>7.17.0</openapi-generator.version>
|
||||
<swagger-annotations.version>2.2.36</swagger-annotations.version>
|
||||
<aws.sdk.version>2.31.50</aws.sdk.version>
|
||||
</properties>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-webmvc</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-validation</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-liquibase</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-redis</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.security</groupId>
|
||||
<artifactId>spring-security-crypto</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.bouncycastle</groupId>
|
||||
<artifactId>bcprov-jdk18on</artifactId>
|
||||
<version>1.86</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-actuator</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.micrometer</groupId>
|
||||
<artifactId>micrometer-registry-prometheus</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.postgresql</groupId>
|
||||
<artifactId>postgresql</artifactId>
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.mapstruct</groupId>
|
||||
<artifactId>mapstruct</artifactId>
|
||||
<version>${mapstruct.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.swagger.core.v3</groupId>
|
||||
<artifactId>swagger-annotations-jakarta</artifactId>
|
||||
<version>${swagger-annotations.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>software.amazon.awssdk</groupId>
|
||||
<artifactId>s3</artifactId>
|
||||
<version>${aws.sdk.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>software.amazon.awssdk</groupId>
|
||||
<artifactId>apache-client</artifactId>
|
||||
<version>${aws.sdk.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-webmvc-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-jpa-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-testcontainers</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.testcontainers</groupId>
|
||||
<artifactId>testcontainers</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.testcontainers</groupId>
|
||||
<artifactId>testcontainers-postgresql</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<build>
|
||||
<finalName>messenger</finalName>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.openapitools</groupId>
|
||||
<artifactId>openapi-generator-maven-plugin</artifactId>
|
||||
<version>${openapi-generator.version}</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
<goal>generate</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<inputSpec>${project.basedir}/../contracts/openapi.json</inputSpec>
|
||||
<generatorName>spring</generatorName>
|
||||
<output>${project.build.directory}/generated-sources/openapi</output>
|
||||
<modelPackage>ru.mtuci.messenger.api.model</modelPackage>
|
||||
<apiPackage>ru.mtuci.messenger.api.generated</apiPackage>
|
||||
<generateApis>false</generateApis>
|
||||
<generateModels>true</generateModels>
|
||||
<generateSupportingFiles>false</generateSupportingFiles>
|
||||
<typeMappings>
|
||||
<typeMapping>set=List</typeMapping>
|
||||
</typeMappings>
|
||||
<instantiationTypes>
|
||||
<instantiationType>set=ArrayList</instantiationType>
|
||||
</instantiationTypes>
|
||||
<configOptions>
|
||||
<useSpringBoot3>true</useSpringBoot3>
|
||||
<useJakartaEe>true</useJakartaEe>
|
||||
<interfaceOnly>true</interfaceOnly>
|
||||
<dateLibrary>java8</dateLibrary>
|
||||
<openApiNullable>false</openApiNullable>
|
||||
<serializationLibrary>jackson</serializationLibrary>
|
||||
<hideGenerationTimestamp>true</hideGenerationTimestamp>
|
||||
<additionalModelTypeAnnotations>@com.fasterxml.jackson.annotation.JsonInclude(com.fasterxml.jackson.annotation.JsonInclude.Include.ALWAYS)</additionalModelTypeAnnotations>
|
||||
</configOptions>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-antrun-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>jackson3-model-annotations</id>
|
||||
<phase>generate-sources</phase>
|
||||
<goals>
|
||||
<goal>run</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<target>
|
||||
<replace dir="${project.build.directory}/generated-sources/openapi" token="import com.fasterxml.jackson.databind.annotation.JsonDeserialize;" value="">
|
||||
<include name="**/*.java"/>
|
||||
</replace>
|
||||
<replace dir="${project.build.directory}/generated-sources/openapi" token="@JsonDeserialize(as = LinkedHashSet.class)" value="">
|
||||
<include name="**/*.java"/>
|
||||
</replace>
|
||||
<!-- Generator builds a required-args constructor that drops attachment_ids. Jackson uses it, so copy the list back. -->
|
||||
<replace dir="${project.build.directory}/generated-sources/openapi" token="public CreateMessage(String text, Object attachmentIds) {${line.separator} this.text = text;${line.separator} }" value="public CreateMessage(String text, Object attachmentIds) {${line.separator} this.text = text;${line.separator} if (attachmentIds instanceof java.util.List<?> ids) {${line.separator} this.attachmentIds = new java.util.ArrayList<>();${line.separator} for (Object id : ids) {${line.separator} this.attachmentIds.add(java.util.UUID.fromString(String.valueOf(id)));${line.separator} }${line.separator} }${line.separator} }">
|
||||
<include name="**/CreateMessage.java"/>
|
||||
</replace>
|
||||
</target>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<configuration>
|
||||
<annotationProcessorPaths>
|
||||
<path>
|
||||
<groupId>org.mapstruct</groupId>
|
||||
<artifactId>mapstruct-processor</artifactId>
|
||||
<version>${mapstruct.version}</version>
|
||||
</path>
|
||||
</annotationProcessorPaths>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
@@ -0,0 +1,20 @@
|
||||
package ru.mtuci.messenger;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.security.crypto.argon2.Argon2PasswordEncoder;
|
||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
|
||||
@SpringBootApplication
|
||||
public class MessengerApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(MessengerApplication.class, args);
|
||||
}
|
||||
|
||||
@Bean
|
||||
PasswordEncoder passwordEncoder() {
|
||||
return Argon2PasswordEncoder.defaultsForSpringSecurity_v5_8();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
package ru.mtuci.messenger.api.attachment;
|
||||
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import org.springframework.core.io.InputStreamResource;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
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.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import ru.mtuci.messenger.api.model.Attachment;
|
||||
import ru.mtuci.messenger.domain.AttachmentEntity;
|
||||
import ru.mtuci.messenger.service.AttachmentService;
|
||||
import ru.mtuci.messenger.web.BearerAuthFilter;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.UUID;
|
||||
|
||||
@RestController
|
||||
public class AttachmentController {
|
||||
|
||||
private final AttachmentService attachments;
|
||||
|
||||
public AttachmentController(AttachmentService attachments) {
|
||||
this.attachments = attachments;
|
||||
}
|
||||
|
||||
@PostMapping(value = "/api/v1/chats/{chatId}/attachments", consumes = {"image/png", "image/jpeg"})
|
||||
public ResponseEntity<Attachment> upload(HttpServletRequest request, @PathVariable UUID chatId,
|
||||
@RequestParam("filename") String filename) throws IOException {
|
||||
long length = request.getContentLengthLong();
|
||||
Attachment created = attachments.upload(
|
||||
BearerAuthFilter.userId(request), chatId, filename, request.getContentType(), length, request.getInputStream());
|
||||
return ResponseEntity.status(HttpStatus.CREATED).body(created);
|
||||
}
|
||||
|
||||
@GetMapping("/api/v1/attachments/{attachmentId}")
|
||||
public Attachment get(HttpServletRequest request, @PathVariable UUID attachmentId) {
|
||||
return attachments.get(BearerAuthFilter.userId(request), attachmentId);
|
||||
}
|
||||
|
||||
@GetMapping("/api/v1/attachments/{attachmentId}/content")
|
||||
public ResponseEntity<InputStreamResource> content(HttpServletRequest request, @PathVariable UUID attachmentId,
|
||||
@RequestParam(required = false) String variant) {
|
||||
AttachmentEntity attachment = attachments.content(BearerAuthFilter.userId(request), attachmentId, variant);
|
||||
return ResponseEntity.ok()
|
||||
.contentType(MediaType.parseMediaType(attachment.getContentType()))
|
||||
.contentLength(attachment.getSizeBytes())
|
||||
.body(new InputStreamResource(attachments.open(attachment)));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
package ru.mtuci.messenger.api.chat;
|
||||
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.validation.Valid;
|
||||
import jakarta.validation.constraints.Max;
|
||||
import jakarta.validation.constraints.Min;
|
||||
import jakarta.validation.constraints.Size;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
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.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import ru.mtuci.messenger.api.model.Chat;
|
||||
import ru.mtuci.messenger.api.model.ChatPage;
|
||||
import ru.mtuci.messenger.api.model.CreateChat;
|
||||
import ru.mtuci.messenger.service.ChatService;
|
||||
import ru.mtuci.messenger.web.BearerAuthFilter;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
@Validated
|
||||
@RestController
|
||||
public class ChatController {
|
||||
|
||||
private final ChatService chats;
|
||||
|
||||
public ChatController(ChatService chats) {
|
||||
this.chats = chats;
|
||||
}
|
||||
|
||||
@PostMapping("/api/v1/chats")
|
||||
public ResponseEntity<Chat> create(HttpServletRequest request, @Valid @RequestBody CreateChat body) {
|
||||
return ResponseEntity.status(HttpStatus.CREATED).body(chats.create(BearerAuthFilter.userId(request), body));
|
||||
}
|
||||
|
||||
@GetMapping("/api/v1/chats")
|
||||
public ChatPage list(HttpServletRequest request,
|
||||
@RequestParam(required = false) @Min(1) @Max(100) Integer limit,
|
||||
@RequestParam(required = false) @Size(min = 1) String cursor) {
|
||||
return chats.list(BearerAuthFilter.userId(request), limit, cursor);
|
||||
}
|
||||
|
||||
@GetMapping("/api/v1/chats/{chatId}")
|
||||
public Chat get(HttpServletRequest request, @PathVariable UUID chatId) {
|
||||
return chats.get(BearerAuthFilter.userId(request), chatId);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package ru.mtuci.messenger.api.health;
|
||||
|
||||
import org.springframework.boot.health.actuate.endpoint.HealthDescriptor;
|
||||
import org.springframework.boot.health.actuate.endpoint.HealthEndpoint;
|
||||
import org.springframework.boot.health.contributor.Status;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import ru.mtuci.messenger.api.model.Health;
|
||||
import ru.mtuci.messenger.exception.UnavailableException;
|
||||
|
||||
@RestController
|
||||
public class HealthController {
|
||||
|
||||
private final HealthEndpoint healthEndpoint;
|
||||
|
||||
public HealthController(HealthEndpoint healthEndpoint) {
|
||||
this.healthEndpoint = healthEndpoint;
|
||||
}
|
||||
|
||||
@GetMapping("/health/live")
|
||||
public Health live() {
|
||||
if (!up("liveness")) {
|
||||
throw new IllegalStateException("liveness");
|
||||
}
|
||||
return ok();
|
||||
}
|
||||
|
||||
@GetMapping("/health/ready")
|
||||
public Health ready() {
|
||||
if (!up("readiness")) {
|
||||
throw new UnavailableException("Сервис не готов");
|
||||
}
|
||||
return ok();
|
||||
}
|
||||
|
||||
private boolean up(String path) {
|
||||
HealthDescriptor descriptor = healthEndpoint.healthForPath(path);
|
||||
return descriptor != null && Status.UP.equals(descriptor.getStatus());
|
||||
}
|
||||
|
||||
private Health ok() {
|
||||
return new Health(Health.StatusEnum.OK);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
package ru.mtuci.messenger.api.message;
|
||||
|
||||
import jakarta.validation.Valid;
|
||||
import jakarta.validation.constraints.Max;
|
||||
import jakarta.validation.constraints.Min;
|
||||
import jakarta.validation.constraints.Size;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
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.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestHeader;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import ru.mtuci.messenger.api.model.CreateMessage;
|
||||
import ru.mtuci.messenger.api.model.Message;
|
||||
import ru.mtuci.messenger.api.model.MessagePage;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import ru.mtuci.messenger.service.MessageService;
|
||||
import ru.mtuci.messenger.web.BearerAuthFilter;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
@Validated
|
||||
@RestController
|
||||
public class MessageController {
|
||||
|
||||
private final MessageService messageService;
|
||||
|
||||
public MessageController(MessageService messageService) {
|
||||
this.messageService = messageService;
|
||||
}
|
||||
|
||||
@PostMapping("/api/v1/chats/{chatId}/messages")
|
||||
public ResponseEntity<Message> create(HttpServletRequest request, @PathVariable UUID chatId,
|
||||
@RequestHeader(value = "Idempotency-Key", required = false) String idempotencyKey,
|
||||
@Valid @RequestBody CreateMessage body) {
|
||||
return ResponseEntity.status(HttpStatus.CREATED)
|
||||
.body(messageService.create(BearerAuthFilter.userId(request), chatId, idempotencyKey, body));
|
||||
}
|
||||
|
||||
@GetMapping("/api/v1/chats/{chatId}/messages")
|
||||
public MessagePage list(HttpServletRequest request, @PathVariable UUID chatId,
|
||||
@RequestParam(required = false) @Min(1) @Max(100) Integer limit,
|
||||
@RequestParam(required = false) @Size(min = 1) String cursor) {
|
||||
return messageService.list(BearerAuthFilter.userId(request), chatId, limit, cursor);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package ru.mtuci.messenger.api.user;
|
||||
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.validation.Valid;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import ru.mtuci.messenger.api.model.CreateUser;
|
||||
import ru.mtuci.messenger.api.model.Session;
|
||||
import ru.mtuci.messenger.api.model.User;
|
||||
import ru.mtuci.messenger.service.UserService;
|
||||
import ru.mtuci.messenger.web.BearerAuthFilter;
|
||||
|
||||
@RestController
|
||||
public class UserController {
|
||||
|
||||
private final UserService users;
|
||||
|
||||
public UserController(UserService users) {
|
||||
this.users = users;
|
||||
}
|
||||
|
||||
@PostMapping("/api/v1/users")
|
||||
public ResponseEntity<User> register(@Valid @RequestBody CreateUser request) {
|
||||
return ResponseEntity.status(HttpStatus.CREATED).body(users.register(request));
|
||||
}
|
||||
|
||||
@GetMapping("/api/v1/users/me")
|
||||
public User me(HttpServletRequest request) {
|
||||
return users.current(BearerAuthFilter.userId(request));
|
||||
}
|
||||
|
||||
@PostMapping("/api/v1/auth/sessions")
|
||||
public ResponseEntity<Session> login(HttpServletRequest request) {
|
||||
return ResponseEntity.status(HttpStatus.CREATED).body(users.login(request.getHeader("Authorization")));
|
||||
}
|
||||
|
||||
@DeleteMapping("/api/v1/auth/sessions/current")
|
||||
public ResponseEntity<Void> logout(HttpServletRequest request) {
|
||||
users.revoke((String) request.getAttribute(BearerAuthFilter.TOKEN));
|
||||
return ResponseEntity.noContent().build();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
package ru.mtuci.messenger.cache;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.dao.DataAccessException;
|
||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* Кэш Bearer-сессий: session:{sha256(token)} -> userId, TTL = остаток жизни сессии.
|
||||
* Источник истины — таблица sessions; при ошибке Redis вызывающий идёт в PostgreSQL.
|
||||
*/
|
||||
@Component
|
||||
public class SessionCache {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(SessionCache.class);
|
||||
|
||||
private final StringRedisTemplate redis;
|
||||
|
||||
public SessionCache(StringRedisTemplate redis) {
|
||||
this.redis = redis;
|
||||
}
|
||||
|
||||
public Optional<UUID> find(String tokenHash) {
|
||||
try {
|
||||
return Optional.ofNullable(redis.opsForValue().get(key(tokenHash))).map(UUID::fromString);
|
||||
} catch (DataAccessException exception) {
|
||||
log.warn("session cache bypass: {}", exception.getClass().getSimpleName());
|
||||
return Optional.empty();
|
||||
}
|
||||
}
|
||||
|
||||
public void put(String tokenHash, UUID userId, Instant expiresAt) {
|
||||
Duration ttl = Duration.between(Instant.now(), expiresAt);
|
||||
if (ttl.isNegative() || ttl.isZero()) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
redis.opsForValue().set(key(tokenHash), userId.toString(), ttl);
|
||||
} catch (DataAccessException exception) {
|
||||
log.warn("session cache write skipped: {}", exception.getClass().getSimpleName());
|
||||
}
|
||||
}
|
||||
|
||||
public void evict(String tokenHash) {
|
||||
try {
|
||||
redis.delete(key(tokenHash));
|
||||
} catch (DataAccessException exception) {
|
||||
log.warn("session cache evict skipped: {}", exception.getClass().getSimpleName());
|
||||
}
|
||||
}
|
||||
|
||||
private static String key(String tokenHash) {
|
||||
return "session:" + tokenHash;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,111 @@
|
||||
package ru.mtuci.messenger.domain;
|
||||
|
||||
import jakarta.persistence.Column;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.Id;
|
||||
import jakarta.persistence.Table;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.UUID;
|
||||
|
||||
@Entity
|
||||
@Table(name = "attachments")
|
||||
public class AttachmentEntity {
|
||||
|
||||
@Id
|
||||
private UUID id;
|
||||
|
||||
@Column(name = "chat_id", nullable = false)
|
||||
private UUID chatId;
|
||||
|
||||
@Column(name = "owner_id", nullable = false)
|
||||
private UUID ownerId;
|
||||
|
||||
@Column(nullable = false, length = 255)
|
||||
private String filename;
|
||||
|
||||
@Column(name = "content_type", nullable = false, length = 32)
|
||||
private String contentType;
|
||||
|
||||
@Column(name = "size_bytes", nullable = false)
|
||||
private long sizeBytes;
|
||||
|
||||
@Column(nullable = false, length = 64)
|
||||
private String sha256;
|
||||
|
||||
@Column(name = "object_key", nullable = false, length = 80)
|
||||
private String objectKey;
|
||||
|
||||
@Column(nullable = false, length = 16)
|
||||
private String status;
|
||||
|
||||
@Column(name = "created_at", nullable = false)
|
||||
private Instant createdAt;
|
||||
|
||||
@Column(name = "updated_at", nullable = false)
|
||||
private Instant updatedAt;
|
||||
|
||||
protected AttachmentEntity() {
|
||||
}
|
||||
|
||||
public AttachmentEntity(UUID id, UUID chatId, UUID ownerId, String filename, String contentType, long sizeBytes,
|
||||
String sha256, String objectKey) {
|
||||
Instant now = Instant.now().truncatedTo(ChronoUnit.MICROS);
|
||||
this.id = id;
|
||||
this.chatId = chatId;
|
||||
this.ownerId = ownerId;
|
||||
this.filename = filename;
|
||||
this.contentType = contentType;
|
||||
this.sizeBytes = sizeBytes;
|
||||
this.sha256 = sha256;
|
||||
this.objectKey = objectKey;
|
||||
this.status = "ready";
|
||||
this.createdAt = now;
|
||||
this.updatedAt = now;
|
||||
}
|
||||
|
||||
public UUID getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public UUID getChatId() {
|
||||
return chatId;
|
||||
}
|
||||
|
||||
public UUID getOwnerId() {
|
||||
return ownerId;
|
||||
}
|
||||
|
||||
public String getFilename() {
|
||||
return filename;
|
||||
}
|
||||
|
||||
public String getContentType() {
|
||||
return contentType;
|
||||
}
|
||||
|
||||
public long getSizeBytes() {
|
||||
return sizeBytes;
|
||||
}
|
||||
|
||||
public String getSha256() {
|
||||
return sha256;
|
||||
}
|
||||
|
||||
public String getObjectKey() {
|
||||
return objectKey;
|
||||
}
|
||||
|
||||
public String getStatus() {
|
||||
return status;
|
||||
}
|
||||
|
||||
public Instant getCreatedAt() {
|
||||
return createdAt;
|
||||
}
|
||||
|
||||
public Instant getUpdatedAt() {
|
||||
return updatedAt;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package ru.mtuci.messenger.domain;
|
||||
|
||||
import jakarta.persistence.Column;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.Id;
|
||||
import jakarta.persistence.Table;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.UUID;
|
||||
|
||||
@Entity
|
||||
@Table(name = "chats")
|
||||
public class ChatEntity {
|
||||
|
||||
@Id
|
||||
private UUID id;
|
||||
|
||||
@Column(nullable = false, length = 100)
|
||||
private String title;
|
||||
|
||||
@Column(name = "created_at", nullable = false)
|
||||
private Instant createdAt;
|
||||
|
||||
protected ChatEntity() {
|
||||
}
|
||||
|
||||
public ChatEntity(String title) {
|
||||
this.id = UUID.randomUUID();
|
||||
this.title = title;
|
||||
this.createdAt = Instant.now().truncatedTo(ChronoUnit.MICROS);
|
||||
}
|
||||
|
||||
public UUID getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public Instant getCreatedAt() {
|
||||
return createdAt;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package ru.mtuci.messenger.domain;
|
||||
|
||||
import jakarta.persistence.Column;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.Id;
|
||||
import jakarta.persistence.IdClass;
|
||||
import jakarta.persistence.Table;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
@Entity
|
||||
@Table(name = "chat_members")
|
||||
@IdClass(ChatMemberId.class)
|
||||
public class ChatMemberEntity {
|
||||
|
||||
@Id
|
||||
@Column(name = "chat_id", nullable = false)
|
||||
private UUID chatId;
|
||||
|
||||
@Id
|
||||
@Column(name = "user_id", nullable = false)
|
||||
private UUID userId;
|
||||
|
||||
@Column(name = "ordinal", nullable = false)
|
||||
private int ordinal;
|
||||
|
||||
protected ChatMemberEntity() {
|
||||
}
|
||||
|
||||
public ChatMemberEntity(UUID chatId, UUID userId, int ordinal) {
|
||||
this.chatId = chatId;
|
||||
this.userId = userId;
|
||||
this.ordinal = ordinal;
|
||||
}
|
||||
|
||||
public UUID getChatId() {
|
||||
return chatId;
|
||||
}
|
||||
|
||||
public UUID getUserId() {
|
||||
return userId;
|
||||
}
|
||||
|
||||
public int getOrdinal() {
|
||||
return ordinal;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package ru.mtuci.messenger.domain;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.UUID;
|
||||
|
||||
public class ChatMemberId implements Serializable {
|
||||
|
||||
private UUID chatId;
|
||||
private UUID userId;
|
||||
|
||||
public ChatMemberId() {
|
||||
}
|
||||
|
||||
public ChatMemberId(UUID chatId, UUID userId) {
|
||||
this.chatId = chatId;
|
||||
this.userId = userId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object other) {
|
||||
if (this == other) {
|
||||
return true;
|
||||
}
|
||||
if (!(other instanceof ChatMemberId that)) {
|
||||
return false;
|
||||
}
|
||||
return chatId.equals(that.chatId) && userId.equals(that.userId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return chatId.hashCode() * 31 + userId.hashCode();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
package ru.mtuci.messenger.domain;
|
||||
|
||||
import jakarta.persistence.Column;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.Id;
|
||||
import jakarta.persistence.Table;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.UUID;
|
||||
|
||||
@Entity
|
||||
@Table(name = "idempotency_records")
|
||||
public class IdempotencyRecord {
|
||||
|
||||
@Id
|
||||
private UUID id;
|
||||
|
||||
@Column(name = "user_id", nullable = false)
|
||||
private UUID userId;
|
||||
|
||||
@Column(name = "http_method", nullable = false, length = 8)
|
||||
private String httpMethod;
|
||||
|
||||
@Column(nullable = false, length = 256)
|
||||
private String path;
|
||||
|
||||
@Column(name = "idempotency_key", nullable = false, length = 128)
|
||||
private String idempotencyKey;
|
||||
|
||||
@Column(name = "request_hash", nullable = false, length = 64)
|
||||
private String requestHash;
|
||||
|
||||
@Column(name = "message_id", nullable = false)
|
||||
private UUID messageId;
|
||||
|
||||
@Column(name = "created_at", nullable = false)
|
||||
private Instant createdAt;
|
||||
|
||||
@Column(name = "expires_at", nullable = false)
|
||||
private Instant expiresAt;
|
||||
|
||||
protected IdempotencyRecord() {
|
||||
}
|
||||
|
||||
public IdempotencyRecord(UUID userId, String httpMethod, String path, String idempotencyKey, String requestHash,
|
||||
UUID messageId, Instant createdAt, Instant expiresAt) {
|
||||
this.id = UUID.randomUUID();
|
||||
this.userId = userId;
|
||||
this.httpMethod = httpMethod;
|
||||
this.path = path;
|
||||
this.idempotencyKey = idempotencyKey;
|
||||
this.requestHash = requestHash;
|
||||
this.messageId = messageId;
|
||||
this.createdAt = createdAt;
|
||||
this.expiresAt = expiresAt;
|
||||
}
|
||||
|
||||
public UUID getMessageId() {
|
||||
return messageId;
|
||||
}
|
||||
|
||||
public String getRequestHash() {
|
||||
return requestHash;
|
||||
}
|
||||
|
||||
public Instant getExpiresAt() {
|
||||
return expiresAt;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
package ru.mtuci.messenger.domain;
|
||||
|
||||
import jakarta.persistence.CollectionTable;
|
||||
import jakarta.persistence.Column;
|
||||
import jakarta.persistence.ElementCollection;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.FetchType;
|
||||
import jakarta.persistence.Id;
|
||||
import jakarta.persistence.JoinColumn;
|
||||
import jakarta.persistence.OrderColumn;
|
||||
import jakarta.persistence.Table;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
@Entity
|
||||
@Table(name = "messages")
|
||||
public class MessageEntity {
|
||||
|
||||
@Id
|
||||
private UUID id;
|
||||
|
||||
@Column(name = "chat_id", nullable = false)
|
||||
private UUID chatId;
|
||||
|
||||
@Column(name = "sender_id", nullable = false)
|
||||
private UUID senderId;
|
||||
|
||||
@Column(nullable = false, length = 4000)
|
||||
private String text;
|
||||
|
||||
@Column(name = "created_at", nullable = false)
|
||||
private Instant createdAt;
|
||||
|
||||
@ElementCollection(fetch = FetchType.EAGER)
|
||||
@CollectionTable(name = "message_attachments", joinColumns = @JoinColumn(name = "message_id"))
|
||||
@OrderColumn(name = "sort_order")
|
||||
@Column(name = "attachment_id", nullable = false)
|
||||
private List<UUID> attachmentIds = new ArrayList<>();
|
||||
|
||||
protected MessageEntity() {
|
||||
}
|
||||
|
||||
public MessageEntity(UUID chatId, UUID senderId, String text, List<UUID> attachmentIds) {
|
||||
this.id = UUID.randomUUID();
|
||||
this.chatId = chatId;
|
||||
this.senderId = senderId;
|
||||
this.text = text;
|
||||
this.createdAt = Instant.now().truncatedTo(ChronoUnit.MICROS);
|
||||
this.attachmentIds = new ArrayList<>(attachmentIds);
|
||||
}
|
||||
|
||||
public UUID getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public UUID getChatId() {
|
||||
return chatId;
|
||||
}
|
||||
|
||||
public UUID getSenderId() {
|
||||
return senderId;
|
||||
}
|
||||
|
||||
public String getText() {
|
||||
return text;
|
||||
}
|
||||
|
||||
public Instant getCreatedAt() {
|
||||
return createdAt;
|
||||
}
|
||||
|
||||
public List<UUID> getAttachmentIds() {
|
||||
return attachmentIds;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
package ru.mtuci.messenger.domain;
|
||||
|
||||
import jakarta.persistence.Column;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.Id;
|
||||
import jakarta.persistence.Table;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.UUID;
|
||||
|
||||
@Entity
|
||||
@Table(name = "sessions")
|
||||
public class SessionEntity {
|
||||
|
||||
@Id
|
||||
private UUID id;
|
||||
|
||||
@Column(name = "token_hash", nullable = false, unique = true, length = 64)
|
||||
private String tokenHash;
|
||||
|
||||
@Column(name = "user_id", nullable = false)
|
||||
private UUID userId;
|
||||
|
||||
@Column(name = "expires_at", nullable = false)
|
||||
private Instant expiresAt;
|
||||
|
||||
@Column(name = "revoked_at")
|
||||
private Instant revokedAt;
|
||||
|
||||
@Column(name = "created_at", nullable = false)
|
||||
private Instant createdAt;
|
||||
|
||||
protected SessionEntity() {
|
||||
}
|
||||
|
||||
public SessionEntity(String tokenHash, UUID userId, Duration ttl) {
|
||||
this.id = UUID.randomUUID();
|
||||
this.tokenHash = tokenHash;
|
||||
this.userId = userId;
|
||||
this.createdAt = Instant.now().truncatedTo(ChronoUnit.MICROS);
|
||||
this.expiresAt = createdAt.plus(ttl);
|
||||
}
|
||||
|
||||
public UUID getUserId() {
|
||||
return userId;
|
||||
}
|
||||
|
||||
public Instant getExpiresAt() {
|
||||
return expiresAt;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
package ru.mtuci.messenger.domain;
|
||||
|
||||
import jakarta.persistence.Column;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.Id;
|
||||
import jakarta.persistence.Table;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.UUID;
|
||||
|
||||
@Entity
|
||||
@Table(name = "users")
|
||||
public class UserAccount {
|
||||
|
||||
@Id
|
||||
private UUID id;
|
||||
|
||||
@Column(nullable = false, unique = true, length = 32)
|
||||
private String username;
|
||||
|
||||
@Column(name = "display_name", nullable = false, length = 100)
|
||||
private String displayName;
|
||||
|
||||
@Column(name = "password_hash", nullable = false, length = 255)
|
||||
private String passwordHash;
|
||||
|
||||
@Column(name = "created_at", nullable = false)
|
||||
private Instant createdAt;
|
||||
|
||||
protected UserAccount() {
|
||||
}
|
||||
|
||||
public UserAccount(String username, String displayName, String passwordHash) {
|
||||
this.id = UUID.randomUUID();
|
||||
this.username = username;
|
||||
this.displayName = displayName;
|
||||
this.passwordHash = passwordHash;
|
||||
this.createdAt = Instant.now().truncatedTo(ChronoUnit.MICROS);
|
||||
}
|
||||
|
||||
public UUID getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
|
||||
public String getDisplayName() {
|
||||
return displayName;
|
||||
}
|
||||
|
||||
public String getPasswordHash() {
|
||||
return passwordHash;
|
||||
}
|
||||
|
||||
public Instant getCreatedAt() {
|
||||
return createdAt;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package ru.mtuci.messenger.exception;
|
||||
|
||||
import org.springframework.http.HttpStatus;
|
||||
import ru.mtuci.messenger.api.model.ErrorError;
|
||||
|
||||
public class ApiException extends RuntimeException {
|
||||
|
||||
private final HttpStatus status;
|
||||
private final ErrorError.CodeEnum code;
|
||||
|
||||
public ApiException(HttpStatus status, ErrorError.CodeEnum code, String message) {
|
||||
super(message);
|
||||
this.status = status;
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
public HttpStatus status() {
|
||||
return status;
|
||||
}
|
||||
|
||||
public ErrorError.CodeEnum code() {
|
||||
return code;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package ru.mtuci.messenger.exception;
|
||||
|
||||
import org.springframework.http.HttpStatus;
|
||||
import ru.mtuci.messenger.api.model.ErrorError;
|
||||
|
||||
public class ConflictException extends ApiException {
|
||||
|
||||
public ConflictException(String message) {
|
||||
super(HttpStatus.CONFLICT, ErrorError.CodeEnum.CONFLICT, message);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package ru.mtuci.messenger.exception;
|
||||
|
||||
import org.springframework.http.HttpStatus;
|
||||
import ru.mtuci.messenger.api.model.ErrorError;
|
||||
|
||||
public class InvalidRequestException extends ApiException {
|
||||
|
||||
public InvalidRequestException(String message) {
|
||||
super(HttpStatus.BAD_REQUEST, ErrorError.CodeEnum.INVALID_REQUEST, message);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package ru.mtuci.messenger.exception;
|
||||
|
||||
import org.springframework.http.HttpStatus;
|
||||
import ru.mtuci.messenger.api.model.ErrorError;
|
||||
|
||||
public class NotFoundException extends ApiException {
|
||||
|
||||
public NotFoundException() {
|
||||
super(HttpStatus.NOT_FOUND, ErrorError.CodeEnum.NOT_FOUND, "Объект не найден");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package ru.mtuci.messenger.exception;
|
||||
|
||||
import org.springframework.http.HttpStatus;
|
||||
import ru.mtuci.messenger.api.model.ErrorError;
|
||||
|
||||
public class PayloadTooLargeException extends ApiException {
|
||||
|
||||
public PayloadTooLargeException() {
|
||||
super(HttpStatus.PAYLOAD_TOO_LARGE, ErrorError.CodeEnum.PAYLOAD_TOO_LARGE, "Файл слишком большой");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package ru.mtuci.messenger.exception;
|
||||
|
||||
import org.springframework.http.HttpStatus;
|
||||
import ru.mtuci.messenger.api.model.ErrorError;
|
||||
|
||||
public class UnauthorizedException extends ApiException {
|
||||
|
||||
private final String challenge;
|
||||
|
||||
private UnauthorizedException(String challenge) {
|
||||
super(HttpStatus.UNAUTHORIZED, ErrorError.CodeEnum.UNAUTHORIZED, "Требуется аутентификация");
|
||||
this.challenge = challenge;
|
||||
}
|
||||
|
||||
public static UnauthorizedException basic() {
|
||||
return new UnauthorizedException("Basic realm=\"messenger\", charset=\"UTF-8\"");
|
||||
}
|
||||
|
||||
public static UnauthorizedException bearer() {
|
||||
return new UnauthorizedException("Bearer realm=\"messenger\"");
|
||||
}
|
||||
|
||||
public String challenge() {
|
||||
return challenge;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package ru.mtuci.messenger.exception;
|
||||
|
||||
import org.springframework.http.HttpStatus;
|
||||
import ru.mtuci.messenger.api.model.ErrorError;
|
||||
|
||||
public class UnavailableException extends ApiException {
|
||||
|
||||
public UnavailableException(String message) {
|
||||
super(HttpStatus.SERVICE_UNAVAILABLE, ErrorError.CodeEnum.UNAVAILABLE, message);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package ru.mtuci.messenger.exception;
|
||||
|
||||
import org.springframework.http.HttpStatus;
|
||||
import ru.mtuci.messenger.api.model.ErrorError;
|
||||
|
||||
public class UnsupportedMediaException extends ApiException {
|
||||
|
||||
public UnsupportedMediaException() {
|
||||
super(HttpStatus.UNSUPPORTED_MEDIA_TYPE, ErrorError.CodeEnum.UNSUPPORTED_MEDIA_TYPE, "Неподдерживаемый тип содержимого");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package ru.mtuci.messenger.mapper;
|
||||
|
||||
import org.mapstruct.Mapper;
|
||||
import org.mapstruct.Mapping;
|
||||
import org.mapstruct.ReportingPolicy;
|
||||
import ru.mtuci.messenger.api.model.Chat;
|
||||
import ru.mtuci.messenger.domain.ChatEntity;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
@Mapper(componentModel = "spring", uses = TimeMapper.class, unmappedTargetPolicy = ReportingPolicy.ERROR)
|
||||
public interface ChatMapper {
|
||||
|
||||
@Mapping(target = "id", source = "chat.id")
|
||||
@Mapping(target = "title", source = "chat.title")
|
||||
@Mapping(target = "createdAt", source = "chat.createdAt")
|
||||
@Mapping(target = "memberIds", source = "memberIds")
|
||||
Chat toModel(ChatEntity chat, List<UUID> memberIds);
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package ru.mtuci.messenger.mapper;
|
||||
|
||||
import org.mapstruct.Mapper;
|
||||
import org.mapstruct.ReportingPolicy;
|
||||
import ru.mtuci.messenger.api.model.Message;
|
||||
import ru.mtuci.messenger.domain.MessageEntity;
|
||||
|
||||
@Mapper(componentModel = "spring", uses = TimeMapper.class, unmappedTargetPolicy = ReportingPolicy.ERROR)
|
||||
public interface MessageMapper {
|
||||
|
||||
Message toModel(MessageEntity message);
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package ru.mtuci.messenger.mapper;
|
||||
|
||||
import org.mapstruct.Mapper;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.time.OffsetDateTime;
|
||||
import java.time.ZoneOffset;
|
||||
|
||||
@Mapper(componentModel = "spring")
|
||||
public interface TimeMapper {
|
||||
|
||||
default OffsetDateTime map(Instant instant) {
|
||||
return instant == null ? null : instant.atOffset(ZoneOffset.UTC);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package ru.mtuci.messenger.mapper;
|
||||
|
||||
import org.mapstruct.Mapper;
|
||||
import org.mapstruct.Mapping;
|
||||
import org.mapstruct.ReportingPolicy;
|
||||
import ru.mtuci.messenger.api.model.Session;
|
||||
import ru.mtuci.messenger.api.model.User;
|
||||
import ru.mtuci.messenger.domain.UserAccount;
|
||||
|
||||
import java.time.Instant;
|
||||
|
||||
@Mapper(componentModel = "spring", uses = TimeMapper.class, unmappedTargetPolicy = ReportingPolicy.ERROR)
|
||||
public interface UserMapper {
|
||||
|
||||
User toModel(UserAccount user);
|
||||
|
||||
@Mapping(target = "tokenType", expression = "java(ru.mtuci.messenger.api.model.Session.TokenTypeEnum.BEARER)")
|
||||
Session toSession(String token, Instant expiresAt, UserAccount user);
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package ru.mtuci.messenger.migrate;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.ApplicationArguments;
|
||||
import org.springframework.boot.ApplicationRunner;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.context.annotation.Profile;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
@Profile("migrate")
|
||||
public class MigrateExit implements ApplicationRunner {
|
||||
|
||||
private final ConfigurableApplicationContext context;
|
||||
|
||||
public MigrateExit(ConfigurableApplicationContext context) {
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run(ApplicationArguments args) {
|
||||
Thread exit = new Thread(() -> {
|
||||
try {
|
||||
Thread.sleep(500);
|
||||
} catch (InterruptedException interrupted) {
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
int code = SpringApplication.exit(context, () -> 0);
|
||||
System.exit(code);
|
||||
}, "migrate-exit");
|
||||
exit.setDaemon(false);
|
||||
exit.start();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package ru.mtuci.messenger.repository;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import ru.mtuci.messenger.domain.AttachmentEntity;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
public interface AttachmentRepository extends JpaRepository<AttachmentEntity, UUID> {
|
||||
|
||||
boolean existsByObjectKey(String objectKey);
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package ru.mtuci.messenger.repository;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import ru.mtuci.messenger.domain.ChatMemberEntity;
|
||||
import ru.mtuci.messenger.domain.ChatMemberId;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
public interface ChatMemberRepository extends JpaRepository<ChatMemberEntity, ChatMemberId> {
|
||||
|
||||
boolean existsByChatIdAndUserId(UUID chatId, UUID userId);
|
||||
|
||||
List<ChatMemberEntity> findByChatIdOrderByOrdinalAsc(UUID chatId);
|
||||
|
||||
List<ChatMemberEntity> findByChatIdInOrderByOrdinalAsc(Collection<UUID> chatIds);
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package ru.mtuci.messenger.repository;
|
||||
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import ru.mtuci.messenger.domain.ChatEntity;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
public interface ChatRepository extends JpaRepository<ChatEntity, UUID> {
|
||||
|
||||
@Query("""
|
||||
select c from ChatEntity c
|
||||
where c.id in (select m.chatId from ChatMemberEntity m where m.userId = :userId)
|
||||
order by c.createdAt asc, c.id asc
|
||||
""")
|
||||
List<ChatEntity> findFirst(UUID userId, Pageable pageable);
|
||||
|
||||
@Query(value = """
|
||||
select c.* from chats c
|
||||
where c.id in (select m.chat_id from chat_members m where m.user_id = :userId)
|
||||
and (c.created_at, c.id) > (:createdAt, :id)
|
||||
order by c.created_at asc, c.id asc
|
||||
""", nativeQuery = true)
|
||||
List<ChatEntity> findAfter(UUID userId, Instant createdAt, UUID id, Pageable pageable);
|
||||
|
||||
long countByTitle(String title);
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package ru.mtuci.messenger.repository;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import ru.mtuci.messenger.domain.IdempotencyRecord;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
||||
public interface IdempotencyRepository extends JpaRepository<IdempotencyRecord, UUID> {
|
||||
|
||||
Optional<IdempotencyRecord> findByUserIdAndHttpMethodAndPathAndIdempotencyKey(
|
||||
UUID userId, String httpMethod, String path, String idempotencyKey);
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package ru.mtuci.messenger.repository;
|
||||
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import ru.mtuci.messenger.domain.MessageEntity;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
public interface MessageRepository extends JpaRepository<MessageEntity, UUID> {
|
||||
|
||||
long countByChatId(UUID chatId);
|
||||
|
||||
|
||||
@Query("""
|
||||
select m from MessageEntity m
|
||||
where m.chatId = :chatId
|
||||
order by m.createdAt asc, m.id asc
|
||||
""")
|
||||
List<MessageEntity> findFirst(UUID chatId, Pageable pageable);
|
||||
|
||||
@Query(value = """
|
||||
select * from messages
|
||||
where chat_id = :chatId
|
||||
and (created_at, id) > (:createdAt, :id)
|
||||
order by created_at asc, id asc
|
||||
""", nativeQuery = true)
|
||||
List<MessageEntity> findAfter(UUID chatId, Instant createdAt, UUID id, Pageable pageable);
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package ru.mtuci.messenger.repository;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.Modifying;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import ru.mtuci.messenger.domain.SessionEntity;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
||||
public interface SessionRepository extends JpaRepository<SessionEntity, UUID> {
|
||||
|
||||
@Query("""
|
||||
select s from SessionEntity s
|
||||
where s.tokenHash = :tokenHash and s.revokedAt is null and s.expiresAt > :now
|
||||
""")
|
||||
Optional<SessionEntity> findActive(String tokenHash, Instant now);
|
||||
|
||||
@Transactional
|
||||
@Modifying
|
||||
@Query("""
|
||||
update SessionEntity s set s.revokedAt = :now
|
||||
where s.tokenHash = :tokenHash and s.revokedAt is null and s.expiresAt > :now
|
||||
""")
|
||||
int revoke(String tokenHash, Instant now);
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package ru.mtuci.messenger.repository;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import ru.mtuci.messenger.domain.UserAccount;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
||||
public interface UserRepository extends JpaRepository<UserAccount, UUID> {
|
||||
|
||||
Optional<UserAccount> findByUsername(String username);
|
||||
|
||||
long countByIdIn(Collection<UUID> ids);
|
||||
}
|
||||
@@ -0,0 +1,173 @@
|
||||
package ru.mtuci.messenger.service;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.http.InvalidMediaTypeException;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.PlatformTransactionManager;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.transaction.support.TransactionTemplate;
|
||||
import ru.mtuci.messenger.api.model.Attachment;
|
||||
import ru.mtuci.messenger.domain.AttachmentEntity;
|
||||
import ru.mtuci.messenger.exception.InvalidRequestException;
|
||||
import ru.mtuci.messenger.exception.NotFoundException;
|
||||
import ru.mtuci.messenger.exception.PayloadTooLargeException;
|
||||
import ru.mtuci.messenger.exception.UnsupportedMediaException;
|
||||
import ru.mtuci.messenger.repository.AttachmentRepository;
|
||||
import ru.mtuci.messenger.repository.ChatMemberRepository;
|
||||
import ru.mtuci.messenger.storage.ObjectStorage;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HexFormat;
|
||||
import java.util.UUID;
|
||||
|
||||
@Service
|
||||
public class AttachmentService {
|
||||
|
||||
private final AttachmentRepository attachments;
|
||||
private final ChatMemberRepository members;
|
||||
private final ObjectStorage storage;
|
||||
private final TransactionTemplate transactions;
|
||||
private final int maxBytes;
|
||||
private final long maxPixels;
|
||||
|
||||
public AttachmentService(AttachmentRepository attachments, ChatMemberRepository members, ObjectStorage storage,
|
||||
PlatformTransactionManager transactionManager,
|
||||
@Value("${app.upload.max-bytes:10485760}") int maxBytes,
|
||||
@Value("${app.upload.max-pixels:40000000}") long maxPixels) {
|
||||
this.attachments = attachments;
|
||||
this.members = members;
|
||||
this.storage = storage;
|
||||
this.transactions = new TransactionTemplate(transactionManager);
|
||||
this.maxBytes = maxBytes;
|
||||
this.maxPixels = maxPixels;
|
||||
}
|
||||
|
||||
public Attachment upload(UUID actorId, UUID chatId, String filename, String contentType, long contentLength,
|
||||
InputStream body) {
|
||||
requireMember(chatId, actorId);
|
||||
String displayName = filename(filename);
|
||||
String declared = declaredType(contentType);
|
||||
if (contentLength > maxBytes) {
|
||||
throw new PayloadTooLargeException();
|
||||
}
|
||||
byte[] bytes;
|
||||
try {
|
||||
bytes = ImageFiles.readLimited(body, maxBytes);
|
||||
} catch (PayloadTooLargeException tooLarge) {
|
||||
throw tooLarge;
|
||||
} catch (java.io.IOException exception) {
|
||||
throw new ru.mtuci.messenger.exception.UnavailableException("Хранилище временно недоступно");
|
||||
}
|
||||
ImageFiles.Image image = ImageFiles.inspect(bytes, declared, maxPixels);
|
||||
UUID id = UUID.randomUUID();
|
||||
String key = "originals/" + id;
|
||||
storage.ensureBucket();
|
||||
storage.put(key, bytes, image.contentType());
|
||||
try {
|
||||
AttachmentEntity saved = transactions.execute(status -> attachments.saveAndFlush(new AttachmentEntity(
|
||||
id, chatId, actorId, displayName, image.contentType(), bytes.length, sha256(bytes), key)));
|
||||
return toModel(saved);
|
||||
} catch (RuntimeException exception) {
|
||||
storage.deleteQuietly(key);
|
||||
throw exception;
|
||||
}
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public Attachment get(UUID actorId, UUID attachmentId) {
|
||||
return toModel(visible(actorId, attachmentId));
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public AttachmentEntity content(UUID actorId, UUID attachmentId, String variant) {
|
||||
if (variant != null && !"original".equals(variant)) {
|
||||
throw new InvalidRequestException("Некорректный запрос");
|
||||
}
|
||||
return visible(actorId, attachmentId);
|
||||
}
|
||||
|
||||
public InputStream open(AttachmentEntity attachment) {
|
||||
return storage.get(attachment.getObjectKey());
|
||||
}
|
||||
|
||||
private AttachmentEntity visible(UUID actorId, UUID attachmentId) {
|
||||
AttachmentEntity attachment = attachments.findById(attachmentId).orElseThrow(NotFoundException::new);
|
||||
if (!members.existsByChatIdAndUserId(attachment.getChatId(), actorId)) {
|
||||
throw new NotFoundException();
|
||||
}
|
||||
return attachment;
|
||||
}
|
||||
|
||||
private void requireMember(UUID chatId, UUID actorId) {
|
||||
if (!members.existsByChatIdAndUserId(chatId, actorId)) {
|
||||
throw new NotFoundException();
|
||||
}
|
||||
}
|
||||
|
||||
private static String filename(String value) {
|
||||
if (value == null) {
|
||||
throw new InvalidRequestException("Некорректный запрос");
|
||||
}
|
||||
int codePoints = value.codePointCount(0, value.length());
|
||||
if (codePoints < 1 || codePoints > 255) {
|
||||
throw new InvalidRequestException("Некорректный запрос");
|
||||
}
|
||||
int offset = 0;
|
||||
while (offset < value.length()) {
|
||||
int codePoint = value.codePointAt(offset);
|
||||
if (codePoint == '/' || codePoint == '\\' || Character.isISOControl(codePoint)) {
|
||||
throw new InvalidRequestException("Некорректный запрос");
|
||||
}
|
||||
offset += Character.charCount(codePoint);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
private static String declaredType(String header) {
|
||||
if (header == null || header.isBlank()) {
|
||||
throw new UnsupportedMediaException();
|
||||
}
|
||||
MediaType media;
|
||||
try {
|
||||
media = MediaType.parseMediaType(header);
|
||||
} catch (InvalidMediaTypeException exception) {
|
||||
throw new UnsupportedMediaException();
|
||||
}
|
||||
if (media.equalsTypeAndSubtype(MediaType.IMAGE_PNG)) {
|
||||
return "image/png";
|
||||
}
|
||||
if (media.equalsTypeAndSubtype(MediaType.parseMediaType("image/jpeg"))) {
|
||||
return "image/jpeg";
|
||||
}
|
||||
throw new UnsupportedMediaException();
|
||||
}
|
||||
|
||||
private static String sha256(byte[] bytes) {
|
||||
try {
|
||||
return HexFormat.of().formatHex(MessageDigest.getInstance("SHA-256").digest(bytes));
|
||||
} catch (NoSuchAlgorithmException exception) {
|
||||
throw new IllegalStateException(exception);
|
||||
}
|
||||
}
|
||||
|
||||
private static Attachment toModel(AttachmentEntity entity) {
|
||||
Attachment model = new Attachment();
|
||||
model.setId(entity.getId());
|
||||
model.setChatId(entity.getChatId());
|
||||
model.setOwnerId(entity.getOwnerId());
|
||||
model.setFilename(entity.getFilename());
|
||||
model.setContentType(Attachment.ContentTypeEnum.fromValue(entity.getContentType()));
|
||||
model.setSizeBytes((int) entity.getSizeBytes());
|
||||
model.setSha256(entity.getSha256());
|
||||
model.setStatus(Attachment.StatusEnum.READY);
|
||||
model.setVariants(new ArrayList<>());
|
||||
model.setErrorCode(null);
|
||||
model.setCreatedAt(entity.getCreatedAt().atOffset(java.time.ZoneOffset.UTC));
|
||||
model.setUpdatedAt(entity.getUpdatedAt().atOffset(java.time.ZoneOffset.UTC));
|
||||
return model;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
package ru.mtuci.messenger.service;
|
||||
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import ru.mtuci.messenger.api.model.Chat;
|
||||
import ru.mtuci.messenger.api.model.ChatPage;
|
||||
import ru.mtuci.messenger.api.model.CreateChat;
|
||||
import ru.mtuci.messenger.domain.ChatEntity;
|
||||
import ru.mtuci.messenger.domain.ChatMemberEntity;
|
||||
import ru.mtuci.messenger.exception.InvalidRequestException;
|
||||
import ru.mtuci.messenger.exception.NotFoundException;
|
||||
import ru.mtuci.messenger.mapper.ChatMapper;
|
||||
import ru.mtuci.messenger.repository.ChatMemberRepository;
|
||||
import ru.mtuci.messenger.repository.ChatRepository;
|
||||
import ru.mtuci.messenger.repository.UserRepository;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
@Service
|
||||
public class ChatService {
|
||||
|
||||
private final ChatRepository chats;
|
||||
private final ChatMemberRepository members;
|
||||
private final UserRepository users;
|
||||
private final ChatMapper mapper;
|
||||
|
||||
public ChatService(ChatRepository chats, ChatMemberRepository members, UserRepository users, ChatMapper mapper) {
|
||||
this.chats = chats;
|
||||
this.members = members;
|
||||
this.users = users;
|
||||
this.mapper = mapper;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Chat create(UUID actorId, CreateChat request) {
|
||||
TextRules.title(request.getTitle());
|
||||
List<UUID> memberIds = request.getMemberIds();
|
||||
if (memberIds == null || new HashSet<>(memberIds).size() != memberIds.size() || !memberIds.contains(actorId)) {
|
||||
throw new InvalidRequestException("Некорректный запрос");
|
||||
}
|
||||
ChatEntity chat = chats.save(new ChatEntity(request.getTitle()));
|
||||
if (users.countByIdIn(memberIds) != memberIds.size()) {
|
||||
throw new InvalidRequestException("Некорректный запрос");
|
||||
}
|
||||
for (int index = 0; index < memberIds.size(); index++) {
|
||||
members.save(new ChatMemberEntity(chat.getId(), memberIds.get(index), index));
|
||||
}
|
||||
return mapper.toModel(chat, memberIds);
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public ChatPage list(UUID actorId, Integer limit, String cursor) {
|
||||
int pageSize = limit == null ? 50 : limit;
|
||||
CursorCodec.Position position = cursor == null ? null : CursorCodec.decode(cursor, null);
|
||||
List<ChatEntity> rows = position == null
|
||||
? chats.findFirst(actorId, PageRequest.of(0, pageSize + 1))
|
||||
: chats.findAfter(actorId, position.createdAt(), position.id(), PageRequest.of(0, pageSize + 1));
|
||||
boolean more = rows.size() > pageSize;
|
||||
List<ChatEntity> visible = more ? rows.subList(0, pageSize) : rows;
|
||||
Map<UUID, List<UUID>> byChat = memberIds(visible);
|
||||
List<Chat> items = visible.stream()
|
||||
.map(chat -> mapper.toModel(chat, byChat.getOrDefault(chat.getId(), List.of())))
|
||||
.toList();
|
||||
String next = null;
|
||||
if (more) {
|
||||
ChatEntity last = visible.get(visible.size() - 1);
|
||||
next = CursorCodec.encode(null, last.getCreatedAt(), last.getId());
|
||||
}
|
||||
return new ChatPage(items, next);
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public Chat get(UUID actorId, UUID chatId) {
|
||||
if (!members.existsByChatIdAndUserId(chatId, actorId)) {
|
||||
throw new NotFoundException();
|
||||
}
|
||||
ChatEntity chat = chats.findById(chatId).orElseThrow(NotFoundException::new);
|
||||
List<UUID> memberIds = members.findByChatIdOrderByOrdinalAsc(chatId).stream().map(ChatMemberEntity::getUserId).toList();
|
||||
return mapper.toModel(chat, memberIds);
|
||||
}
|
||||
|
||||
private Map<UUID, List<UUID>> memberIds(List<ChatEntity> chats) {
|
||||
Map<UUID, List<UUID>> result = new LinkedHashMap<>();
|
||||
if (chats.isEmpty()) {
|
||||
return result;
|
||||
}
|
||||
List<UUID> ids = chats.stream().map(ChatEntity::getId).toList();
|
||||
for (ChatMemberEntity member : members.findByChatIdInOrderByOrdinalAsc(ids)) {
|
||||
result.computeIfAbsent(member.getChatId(), ignored -> new ArrayList<>()).add(member.getUserId());
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package ru.mtuci.messenger.service;
|
||||
|
||||
import ru.mtuci.messenger.exception.InvalidRequestException;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.time.Instant;
|
||||
import java.util.Base64;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* Непрозрачная позиция keyset-страницы: клиент получает её как next_cursor
|
||||
* и возвращает в ?cursor=, чтобы следующая страница началась после (created_at, id).
|
||||
* Для истории в курсор входит chatId: курсор другого чата отклоняется.
|
||||
*/
|
||||
public final class CursorCodec {
|
||||
|
||||
private CursorCodec() {
|
||||
}
|
||||
|
||||
public static String encode(UUID scope, Instant createdAt, UUID id) {
|
||||
String raw = scope == null ? createdAt + "|" + id : scope + "|" + createdAt + "|" + id;
|
||||
return Base64.getUrlEncoder().withoutPadding().encodeToString(raw.getBytes(StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
public static Position decode(String cursor, UUID scope) {
|
||||
String raw;
|
||||
try {
|
||||
raw = new String(Base64.getUrlDecoder().decode(cursor), StandardCharsets.UTF_8);
|
||||
} catch (IllegalArgumentException exception) {
|
||||
throw new InvalidRequestException("Некорректный курсор");
|
||||
}
|
||||
String[] parts = raw.split("\\|", -1);
|
||||
try {
|
||||
if (scope == null && parts.length == 2) {
|
||||
return new Position(Instant.parse(parts[0]), UUID.fromString(parts[1]));
|
||||
}
|
||||
if (scope != null && parts.length == 3 && scope.equals(UUID.fromString(parts[0]))) {
|
||||
return new Position(Instant.parse(parts[1]), UUID.fromString(parts[2]));
|
||||
}
|
||||
} catch (RuntimeException exception) {
|
||||
throw new InvalidRequestException("Некорректный курсор");
|
||||
}
|
||||
throw new InvalidRequestException("Некорректный курсор");
|
||||
}
|
||||
|
||||
public record Position(Instant createdAt, UUID id) {
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,171 @@
|
||||
package ru.mtuci.messenger.service;
|
||||
|
||||
import ru.mtuci.messenger.exception.PayloadTooLargeException;
|
||||
import ru.mtuci.messenger.exception.UnsupportedMediaException;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
public final class ImageFiles {
|
||||
|
||||
private static final byte[] PNG_SIGNATURE = new byte[] {(byte) 137, 80, 78, 71, 13, 10, 26, 10};
|
||||
|
||||
private ImageFiles() {
|
||||
}
|
||||
|
||||
public record Image(String contentType, long width, long height) {
|
||||
}
|
||||
|
||||
public static byte[] readLimited(InputStream input, int maxBytes) throws IOException {
|
||||
byte[] buffer = new byte[8192];
|
||||
ByteArrayOutputStream body = new ByteArrayOutputStream(Math.min(maxBytes, 64 * 1024));
|
||||
int total = 0;
|
||||
int read;
|
||||
while ((read = input.read(buffer)) >= 0) {
|
||||
if (read == 0) {
|
||||
continue;
|
||||
}
|
||||
if (total > maxBytes - read) {
|
||||
throw new PayloadTooLargeException();
|
||||
}
|
||||
body.write(buffer, 0, read);
|
||||
total += read;
|
||||
}
|
||||
return body.toByteArray();
|
||||
}
|
||||
|
||||
public static Image inspect(byte[] data, String declaredType, long maxPixels) {
|
||||
if (data.length == 0) {
|
||||
throw new UnsupportedMediaException();
|
||||
}
|
||||
Image image = "image/png".equals(declaredType) ? png(data) : jpeg(data);
|
||||
if (image == null || !declaredType.equals(image.contentType()) || image.width() <= 0 || image.height() <= 0) {
|
||||
throw new UnsupportedMediaException();
|
||||
}
|
||||
if (image.width() > maxPixels || image.height() > maxPixels || image.width() * image.height() > maxPixels) {
|
||||
throw new PayloadTooLargeException();
|
||||
}
|
||||
return image;
|
||||
}
|
||||
|
||||
private static Image png(byte[] data) {
|
||||
if (data.length < 33 || !startsWith(data, PNG_SIGNATURE)) {
|
||||
return null;
|
||||
}
|
||||
int offset = 8;
|
||||
Long width = null;
|
||||
Long height = null;
|
||||
boolean end = false;
|
||||
while (offset + 12 <= data.length) {
|
||||
long length = u32(data, offset);
|
||||
if (length > data.length || offset + 12L + length > data.length) {
|
||||
return null;
|
||||
}
|
||||
String type = new String(data, offset + 4, 4, StandardCharsets.US_ASCII);
|
||||
if (!type.chars().allMatch(Character::isLetter)) {
|
||||
return null;
|
||||
}
|
||||
if ("IHDR".equals(type)) {
|
||||
if (length != 13 || width != null || !validPngHeader(data, offset + 8)) {
|
||||
return null;
|
||||
}
|
||||
width = u32(data, offset + 8);
|
||||
height = u32(data, offset + 12);
|
||||
}
|
||||
offset += 12 + (int) length;
|
||||
if ("IEND".equals(type)) {
|
||||
end = length == 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!end || width == null || offset != data.length) {
|
||||
return null;
|
||||
}
|
||||
return new Image("image/png", width, height);
|
||||
}
|
||||
|
||||
private static boolean validPngHeader(byte[] data, int offset) {
|
||||
int bitDepth = data[offset + 8] & 0xff;
|
||||
int color = data[offset + 9] & 0xff;
|
||||
int compression = data[offset + 10] & 0xff;
|
||||
int filter = data[offset + 11] & 0xff;
|
||||
int interlace = data[offset + 12] & 0xff;
|
||||
if (compression != 0 || filter != 0 || (interlace != 0 && interlace != 1)) {
|
||||
return false;
|
||||
}
|
||||
return switch (color) {
|
||||
case 0 -> bitDepth == 1 || bitDepth == 2 || bitDepth == 4 || bitDepth == 8 || bitDepth == 16;
|
||||
case 3 -> bitDepth == 1 || bitDepth == 2 || bitDepth == 4 || bitDepth == 8;
|
||||
case 2, 4, 6 -> bitDepth == 8 || bitDepth == 16;
|
||||
default -> false;
|
||||
};
|
||||
}
|
||||
|
||||
private static Image jpeg(byte[] data) {
|
||||
if (data.length < 4 || (data[0] & 0xff) != 0xff || (data[1] & 0xff) != 0xd8) {
|
||||
return null;
|
||||
}
|
||||
int offset = 2;
|
||||
Long width = null;
|
||||
Long height = null;
|
||||
while (offset + 1 < data.length) {
|
||||
if ((data[offset] & 0xff) != 0xff) {
|
||||
return null;
|
||||
}
|
||||
while (offset < data.length && (data[offset] & 0xff) == 0xff) {
|
||||
offset++;
|
||||
}
|
||||
if (offset >= data.length) {
|
||||
return null;
|
||||
}
|
||||
int marker = data[offset++] & 0xff;
|
||||
if (marker == 0xd9) {
|
||||
return width == null ? null : new Image("image/jpeg", width, height);
|
||||
}
|
||||
if (marker == 0xd8 || marker == 0x01 || (marker >= 0xd0 && marker <= 0xd7)) {
|
||||
continue;
|
||||
}
|
||||
if (offset + 1 >= data.length) {
|
||||
return null;
|
||||
}
|
||||
int length = ((data[offset] & 0xff) << 8) | (data[offset + 1] & 0xff);
|
||||
if (length < 2 || offset + length > data.length) {
|
||||
return null;
|
||||
}
|
||||
if (isStartOfFrame(marker)) {
|
||||
if (length < 7) {
|
||||
return null;
|
||||
}
|
||||
height = (long) ((data[offset + 3] & 0xff) << 8) | (data[offset + 4] & 0xff);
|
||||
width = (long) ((data[offset + 5] & 0xff) << 8) | (data[offset + 6] & 0xff);
|
||||
}
|
||||
offset += length;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private static boolean isStartOfFrame(int marker) {
|
||||
return marker >= 0xc0 && marker <= 0xcf && marker != 0xc4 && marker != 0xc8 && marker != 0xcc;
|
||||
}
|
||||
|
||||
private static boolean startsWith(byte[] data, byte[] prefix) {
|
||||
if (data.length < prefix.length) {
|
||||
return false;
|
||||
}
|
||||
for (int i = 0; i < prefix.length; i++) {
|
||||
if (data[i] != prefix[i]) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private static long u32(byte[] data, int offset) {
|
||||
return ((data[offset] & 0xffL) << 24)
|
||||
| ((data[offset + 1] & 0xffL) << 16)
|
||||
| ((data[offset + 2] & 0xffL) << 8)
|
||||
| (data[offset + 3] & 0xffL);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,199 @@
|
||||
package ru.mtuci.messenger.service;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import ru.mtuci.messenger.api.model.CreateMessage;
|
||||
import ru.mtuci.messenger.api.model.Message;
|
||||
import ru.mtuci.messenger.api.model.MessagePage;
|
||||
import ru.mtuci.messenger.domain.AttachmentEntity;
|
||||
import ru.mtuci.messenger.domain.IdempotencyRecord;
|
||||
import ru.mtuci.messenger.domain.MessageEntity;
|
||||
import ru.mtuci.messenger.exception.ConflictException;
|
||||
import ru.mtuci.messenger.exception.InvalidRequestException;
|
||||
import ru.mtuci.messenger.exception.NotFoundException;
|
||||
import ru.mtuci.messenger.mapper.MessageMapper;
|
||||
import ru.mtuci.messenger.repository.AttachmentRepository;
|
||||
import ru.mtuci.messenger.repository.ChatMemberRepository;
|
||||
import ru.mtuci.messenger.repository.IdempotencyRepository;
|
||||
import ru.mtuci.messenger.repository.MessageRepository;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.sql.Connection;
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HexFormat;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
@Service
|
||||
public class MessageService {
|
||||
|
||||
private static final Pattern IDEMPOTENCY_KEY = Pattern.compile("^[A-Za-z0-9._:-]{1,128}$");
|
||||
private static final String METHOD = "POST";
|
||||
|
||||
private final MessageRepository messages;
|
||||
private final AttachmentRepository attachments;
|
||||
private final IdempotencyRepository idempotency;
|
||||
private final ChatMemberRepository members;
|
||||
private final MessageMapper mapper;
|
||||
private final JdbcTemplate jdbc;
|
||||
private final Duration idempotencyTtl;
|
||||
|
||||
public MessageService(MessageRepository messages, AttachmentRepository attachments, IdempotencyRepository idempotency,
|
||||
ChatMemberRepository members, MessageMapper mapper, JdbcTemplate jdbc,
|
||||
@Value("${app.idempotency-ttl:PT24H}") Duration idempotencyTtl) {
|
||||
this.messages = messages;
|
||||
this.attachments = attachments;
|
||||
this.idempotency = idempotency;
|
||||
this.members = members;
|
||||
this.mapper = mapper;
|
||||
this.jdbc = jdbc;
|
||||
this.idempotencyTtl = idempotencyTtl;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Message create(UUID actorId, UUID chatId, String idempotencyKey, CreateMessage request) {
|
||||
requireMember(chatId, actorId);
|
||||
String key = requireKey(idempotencyKey);
|
||||
List<UUID> attachmentIds = attachmentIds(request);
|
||||
requireText(request.getText(), !attachmentIds.isEmpty());
|
||||
requireAttachments(chatId, attachmentIds);
|
||||
String path = "/api/v1/chats/" + chatId + "/messages";
|
||||
lock(actorId + "\n" + METHOD + "\n" + path + "\n" + key);
|
||||
String hash = requestHash(request.getText(), attachmentIds);
|
||||
Optional<IdempotencyRecord> existing = idempotency.findByUserIdAndHttpMethodAndPathAndIdempotencyKey(
|
||||
actorId, METHOD, path, key);
|
||||
if (existing.isPresent()) {
|
||||
IdempotencyRecord record = existing.get();
|
||||
if (record.getExpiresAt().isAfter(Instant.now())) {
|
||||
if (!record.getRequestHash().equals(hash)) {
|
||||
throw new ConflictException("Ключ идемпотентности уже использован с другим телом");
|
||||
}
|
||||
return mapper.toModel(messages.findById(record.getMessageId()).orElseThrow(NotFoundException::new));
|
||||
}
|
||||
idempotency.delete(record);
|
||||
idempotency.flush();
|
||||
}
|
||||
Instant now = Instant.now();
|
||||
MessageEntity saved = messages.saveAndFlush(new MessageEntity(chatId, actorId, request.getText(), attachmentIds));
|
||||
idempotency.saveAndFlush(new IdempotencyRecord(
|
||||
actorId, METHOD, path, key, hash, saved.getId(), now, now.plus(idempotencyTtl)));
|
||||
return mapper.toModel(messages.findById(saved.getId()).orElseThrow(NotFoundException::new));
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public MessagePage list(UUID actorId, UUID chatId, Integer limit, String cursor) {
|
||||
requireMember(chatId, actorId);
|
||||
int pageSize = limit == null ? 50 : limit;
|
||||
CursorCodec.Position position = cursor == null ? null : CursorCodec.decode(cursor, chatId);
|
||||
List<MessageEntity> rows = position == null
|
||||
? messages.findFirst(chatId, PageRequest.of(0, pageSize + 1))
|
||||
: messages.findAfter(chatId, position.createdAt(), position.id(), PageRequest.of(0, pageSize + 1));
|
||||
boolean more = rows.size() > pageSize;
|
||||
List<MessageEntity> visible = more ? rows.subList(0, pageSize) : rows;
|
||||
String next = null;
|
||||
if (more) {
|
||||
MessageEntity last = visible.get(visible.size() - 1);
|
||||
next = CursorCodec.encode(chatId, last.getCreatedAt(), last.getId());
|
||||
}
|
||||
return new MessagePage(visible.stream().map(mapper::toModel).toList(), next);
|
||||
}
|
||||
|
||||
private void lock(String scope) {
|
||||
byte[] digest = digest(scope);
|
||||
long key = 0;
|
||||
for (int i = 0; i < 8; i++) {
|
||||
key = (key << 8) | (digest[i] & 0xffL);
|
||||
}
|
||||
long lockKey = key;
|
||||
jdbc.execute((Connection connection) -> {
|
||||
try (var statement = connection.prepareStatement("select pg_advisory_xact_lock(?)")) {
|
||||
statement.setLong(1, lockKey);
|
||||
statement.execute();
|
||||
}
|
||||
return null;
|
||||
});
|
||||
}
|
||||
|
||||
private static String requireKey(String key) {
|
||||
if (key == null || !IDEMPOTENCY_KEY.matcher(key).matches()) {
|
||||
throw new InvalidRequestException("Некорректный запрос");
|
||||
}
|
||||
return key;
|
||||
}
|
||||
|
||||
private void requireAttachments(UUID chatId, List<UUID> attachmentIds) {
|
||||
if (attachmentIds.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
List<AttachmentEntity> found = attachments.findAllById(attachmentIds);
|
||||
if (found.size() != attachmentIds.size()) {
|
||||
throw new NotFoundException();
|
||||
}
|
||||
for (AttachmentEntity attachment : found) {
|
||||
if (!chatId.equals(attachment.getChatId()) || !"ready".equals(attachment.getStatus())) {
|
||||
throw new NotFoundException();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static List<UUID> attachmentIds(CreateMessage request) {
|
||||
List<UUID> ids = request.getAttachmentIds();
|
||||
if (ids == null || ids.isEmpty()) {
|
||||
return List.of();
|
||||
}
|
||||
if (ids.size() > 10 || ids.stream().anyMatch(Objects::isNull) || new LinkedHashSet<>(ids).size() != ids.size()) {
|
||||
throw new InvalidRequestException("Некорректный запрос");
|
||||
}
|
||||
return new ArrayList<>(ids);
|
||||
}
|
||||
|
||||
private static void requireText(String text, boolean hasAttachments) {
|
||||
if (text == null) {
|
||||
throw new InvalidRequestException("Некорректный запрос");
|
||||
}
|
||||
int codePoints = text.codePointCount(0, text.length());
|
||||
if (codePoints > 4000 || (text.isBlank() && !hasAttachments)) {
|
||||
throw new InvalidRequestException("Некорректный запрос");
|
||||
}
|
||||
}
|
||||
|
||||
private static String requestHash(String text, List<UUID> attachmentIds) {
|
||||
StringBuilder canonical = new StringBuilder(text == null ? "" : text).append('\n');
|
||||
for (int i = 0; i < attachmentIds.size(); i++) {
|
||||
if (i > 0) {
|
||||
canonical.append(',');
|
||||
}
|
||||
canonical.append(attachmentIds.get(i));
|
||||
}
|
||||
return sha256(canonical.toString());
|
||||
}
|
||||
|
||||
private static String sha256(String value) {
|
||||
return HexFormat.of().formatHex(digest(value));
|
||||
}
|
||||
|
||||
private static byte[] digest(String value) {
|
||||
try {
|
||||
return MessageDigest.getInstance("SHA-256").digest(value.getBytes(StandardCharsets.UTF_8));
|
||||
} catch (NoSuchAlgorithmException exception) {
|
||||
throw new IllegalStateException(exception);
|
||||
}
|
||||
}
|
||||
|
||||
private void requireMember(UUID chatId, UUID actorId) {
|
||||
if (!members.existsByChatIdAndUserId(chatId, actorId)) {
|
||||
throw new NotFoundException();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package ru.mtuci.messenger.service;
|
||||
|
||||
import ru.mtuci.messenger.exception.InvalidRequestException;
|
||||
|
||||
public final class TextRules {
|
||||
|
||||
private TextRules() {
|
||||
}
|
||||
|
||||
public static void displayName(String value) {
|
||||
text(value, 1, 100);
|
||||
}
|
||||
|
||||
public static void title(String value) {
|
||||
text(value, 1, 100);
|
||||
}
|
||||
|
||||
public static void message(String value) {
|
||||
text(value, 1, 4000);
|
||||
}
|
||||
|
||||
public static void password(String value) {
|
||||
text(value, 12, 128);
|
||||
}
|
||||
|
||||
private static void text(String value, int minCodePoints, int maxCodePoints) {
|
||||
if (value == null || value.isBlank()) {
|
||||
throw new InvalidRequestException("Некорректный запрос");
|
||||
}
|
||||
int codePoints = value.codePointCount(0, value.length());
|
||||
if (codePoints < minCodePoints || codePoints > maxCodePoints) {
|
||||
throw new InvalidRequestException("Некорректный запрос");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,147 @@
|
||||
package ru.mtuci.messenger.service;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.dao.DataIntegrityViolationException;
|
||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.support.TransactionTemplate;
|
||||
import ru.mtuci.messenger.api.model.CreateUser;
|
||||
import ru.mtuci.messenger.api.model.Session;
|
||||
import ru.mtuci.messenger.api.model.User;
|
||||
import ru.mtuci.messenger.cache.SessionCache;
|
||||
import ru.mtuci.messenger.domain.SessionEntity;
|
||||
import ru.mtuci.messenger.domain.UserAccount;
|
||||
import ru.mtuci.messenger.exception.ConflictException;
|
||||
import ru.mtuci.messenger.exception.NotFoundException;
|
||||
import ru.mtuci.messenger.exception.UnauthorizedException;
|
||||
import ru.mtuci.messenger.mapper.UserMapper;
|
||||
import ru.mtuci.messenger.repository.SessionRepository;
|
||||
import ru.mtuci.messenger.repository.UserRepository;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.security.SecureRandom;
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.util.Base64;
|
||||
import java.util.HexFormat;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
||||
@Service
|
||||
public class UserService {
|
||||
|
||||
private final UserRepository users;
|
||||
private final SessionRepository sessions;
|
||||
private final SessionCache cache;
|
||||
private final UserMapper mapper;
|
||||
private final PasswordEncoder passwords;
|
||||
private final TransactionTemplate transactions;
|
||||
private final Duration sessionTtl;
|
||||
private final SecureRandom random = new SecureRandom();
|
||||
private volatile String dummyHash;
|
||||
|
||||
public UserService(UserRepository users, SessionRepository sessions, SessionCache cache, UserMapper mapper,
|
||||
PasswordEncoder passwords, TransactionTemplate transactions,
|
||||
@Value("${app.session-ttl}") Duration sessionTtl) {
|
||||
this.users = users;
|
||||
this.sessions = sessions;
|
||||
this.cache = cache;
|
||||
this.mapper = mapper;
|
||||
this.passwords = passwords;
|
||||
this.transactions = transactions;
|
||||
this.sessionTtl = sessionTtl;
|
||||
}
|
||||
|
||||
public User register(CreateUser request) {
|
||||
TextRules.displayName(request.getDisplayName());
|
||||
TextRules.password(request.getPassword());
|
||||
try {
|
||||
return transactions.execute(status -> mapper.toModel(users.saveAndFlush(new UserAccount(
|
||||
request.getUsername(),
|
||||
request.getDisplayName(),
|
||||
passwords.encode(request.getPassword())
|
||||
))));
|
||||
} catch (DataIntegrityViolationException exception) {
|
||||
throw new ConflictException("Имя пользователя занято");
|
||||
}
|
||||
}
|
||||
|
||||
public User current(UUID userId) {
|
||||
return users.findById(userId).map(mapper::toModel).orElseThrow(NotFoundException::new);
|
||||
}
|
||||
|
||||
public Session login(String authorization) {
|
||||
String[] credentials = basic(authorization);
|
||||
UserAccount user = users.findByUsername(credentials[0]).orElse(null);
|
||||
String hash = user == null ? dummyHash() : user.getPasswordHash();
|
||||
if (user == null || !passwords.matches(credentials[1], hash)) {
|
||||
throw UnauthorizedException.basic();
|
||||
}
|
||||
String token = newToken();
|
||||
SessionEntity session = sessions.saveAndFlush(new SessionEntity(tokenHash(token), user.getId(), sessionTtl));
|
||||
cache.put(tokenHash(token), user.getId(), session.getExpiresAt());
|
||||
return mapper.toSession(token, session.getExpiresAt(), user);
|
||||
}
|
||||
|
||||
public Optional<UUID> authenticate(String token) {
|
||||
String hash = tokenHash(token);
|
||||
Optional<UUID> cached = cache.find(hash);
|
||||
if (cached.isPresent()) {
|
||||
return cached;
|
||||
}
|
||||
return sessions.findActive(hash, Instant.now()).map(session -> {
|
||||
cache.put(hash, session.getUserId(), session.getExpiresAt());
|
||||
return session.getUserId();
|
||||
});
|
||||
}
|
||||
|
||||
public void revoke(String token) {
|
||||
String hash = tokenHash(token);
|
||||
if (sessions.revoke(hash, Instant.now()) == 0) {
|
||||
throw UnauthorizedException.bearer();
|
||||
}
|
||||
cache.evict(hash);
|
||||
}
|
||||
|
||||
public static String tokenHash(String token) {
|
||||
try {
|
||||
return HexFormat.of().formatHex(MessageDigest.getInstance("SHA-256").digest(token.getBytes(StandardCharsets.UTF_8)));
|
||||
} catch (NoSuchAlgorithmException exception) {
|
||||
throw new IllegalStateException(exception);
|
||||
}
|
||||
}
|
||||
|
||||
private String newToken() {
|
||||
byte[] bytes = new byte[32];
|
||||
random.nextBytes(bytes);
|
||||
return Base64.getUrlEncoder().withoutPadding().encodeToString(bytes);
|
||||
}
|
||||
|
||||
private String dummyHash() {
|
||||
String current = dummyHash;
|
||||
if (current == null) {
|
||||
current = passwords.encode("not-a-real-password");
|
||||
dummyHash = current;
|
||||
}
|
||||
return current;
|
||||
}
|
||||
|
||||
private static String[] basic(String header) {
|
||||
if (header == null || !header.regionMatches(true, 0, "Basic ", 0, 6)) {
|
||||
throw UnauthorizedException.basic();
|
||||
}
|
||||
String decoded;
|
||||
try {
|
||||
decoded = new String(Base64.getDecoder().decode(header.substring(6).trim()), StandardCharsets.UTF_8);
|
||||
} catch (IllegalArgumentException exception) {
|
||||
throw UnauthorizedException.basic();
|
||||
}
|
||||
int colon = decoded.indexOf(':');
|
||||
if (colon <= 0) {
|
||||
throw UnauthorizedException.basic();
|
||||
}
|
||||
return new String[]{decoded.substring(0, colon), decoded.substring(colon + 1)};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,149 @@
|
||||
package ru.mtuci.messenger.storage;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Component;
|
||||
import ru.mtuci.messenger.exception.NotFoundException;
|
||||
import ru.mtuci.messenger.exception.UnavailableException;
|
||||
import software.amazon.awssdk.core.sync.RequestBody;
|
||||
import software.amazon.awssdk.services.s3.S3Client;
|
||||
import software.amazon.awssdk.services.s3.model.BucketAlreadyExistsException;
|
||||
import software.amazon.awssdk.services.s3.model.BucketAlreadyOwnedByYouException;
|
||||
import software.amazon.awssdk.services.s3.model.CreateBucketRequest;
|
||||
import software.amazon.awssdk.services.s3.model.DeleteObjectRequest;
|
||||
import software.amazon.awssdk.services.s3.model.GetObjectRequest;
|
||||
import software.amazon.awssdk.services.s3.model.HeadBucketRequest;
|
||||
import software.amazon.awssdk.services.s3.model.ListObjectsV2Request;
|
||||
import software.amazon.awssdk.services.s3.model.ListObjectsV2Response;
|
||||
import software.amazon.awssdk.services.s3.model.NoSuchBucketException;
|
||||
import software.amazon.awssdk.services.s3.model.NoSuchKeyException;
|
||||
import software.amazon.awssdk.services.s3.model.PutObjectRequest;
|
||||
import software.amazon.awssdk.services.s3.model.S3Exception;
|
||||
import software.amazon.awssdk.services.s3.model.S3Object;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.time.Instant;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Component
|
||||
public class ObjectStorage {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(ObjectStorage.class);
|
||||
|
||||
private final S3Client data;
|
||||
private final S3Client bootstrap;
|
||||
private final String bucket;
|
||||
|
||||
public ObjectStorage(@Qualifier("s3Data") S3Client data,
|
||||
@Qualifier("s3Bootstrap") S3Client bootstrap,
|
||||
@Value("${app.s3.bucket}") String bucket) {
|
||||
this.data = data;
|
||||
this.bootstrap = bootstrap;
|
||||
this.bucket = bucket;
|
||||
}
|
||||
|
||||
public record StoredObject(String key, Instant modified) {
|
||||
}
|
||||
|
||||
public void ensureBucket() {
|
||||
try {
|
||||
bootstrap.headBucket(HeadBucketRequest.builder().bucket(bucket).build());
|
||||
} catch (NoSuchBucketException missing) {
|
||||
createBucket();
|
||||
} catch (S3Exception exception) {
|
||||
if (exception.statusCode() == 404) {
|
||||
createBucket();
|
||||
return;
|
||||
}
|
||||
throw storageFailure(exception);
|
||||
} catch (RuntimeException exception) {
|
||||
throw storageFailure(exception);
|
||||
}
|
||||
}
|
||||
|
||||
public void put(String key, byte[] body, String contentType) {
|
||||
try {
|
||||
data.putObject(PutObjectRequest.builder()
|
||||
.bucket(bucket)
|
||||
.key(key)
|
||||
.contentType(contentType)
|
||||
.contentLength((long) body.length)
|
||||
.build(), RequestBody.fromBytes(body));
|
||||
} catch (RuntimeException exception) {
|
||||
throw storageFailure(exception);
|
||||
}
|
||||
}
|
||||
|
||||
public InputStream get(String key) {
|
||||
try {
|
||||
return data.getObject(GetObjectRequest.builder().bucket(bucket).key(key).build());
|
||||
} catch (NoSuchKeyException missing) {
|
||||
throw new NotFoundException();
|
||||
} catch (S3Exception exception) {
|
||||
if (exception.statusCode() == 404) {
|
||||
throw new NotFoundException();
|
||||
}
|
||||
throw storageFailure(exception);
|
||||
} catch (RuntimeException exception) {
|
||||
throw storageFailure(exception);
|
||||
}
|
||||
}
|
||||
|
||||
public List<StoredObject> list(String prefix) {
|
||||
List<StoredObject> objects = new ArrayList<>();
|
||||
String token = null;
|
||||
do {
|
||||
ListObjectsV2Request.Builder request = ListObjectsV2Request.builder()
|
||||
.bucket(bucket)
|
||||
.prefix(prefix);
|
||||
if (token != null) {
|
||||
request.continuationToken(token);
|
||||
}
|
||||
ListObjectsV2Response page;
|
||||
try {
|
||||
page = data.listObjectsV2(request.build());
|
||||
} catch (RuntimeException exception) {
|
||||
throw storageFailure(exception);
|
||||
}
|
||||
if (page.contents() != null) {
|
||||
for (S3Object object : page.contents()) {
|
||||
Instant modified = object.lastModified() == null ? Instant.EPOCH : object.lastModified();
|
||||
objects.add(new StoredObject(object.key(), modified));
|
||||
}
|
||||
}
|
||||
token = page.nextContinuationToken();
|
||||
} while (token != null);
|
||||
return objects;
|
||||
}
|
||||
|
||||
public void deleteQuietly(String key) {
|
||||
try {
|
||||
data.deleteObject(DeleteObjectRequest.builder().bucket(bucket).key(key).build());
|
||||
} catch (RuntimeException exception) {
|
||||
log.warn("s3 delete failed status={}", exception.getClass().getSimpleName());
|
||||
}
|
||||
}
|
||||
|
||||
private void createBucket() {
|
||||
try {
|
||||
bootstrap.createBucket(CreateBucketRequest.builder().bucket(bucket).build());
|
||||
} catch (BucketAlreadyExistsException | BucketAlreadyOwnedByYouException ignored) {
|
||||
// another replica created the bucket
|
||||
} catch (S3Exception exception) {
|
||||
if (exception.statusCode() == 409) {
|
||||
return;
|
||||
}
|
||||
throw storageFailure(exception);
|
||||
} catch (RuntimeException exception) {
|
||||
throw storageFailure(exception);
|
||||
}
|
||||
}
|
||||
|
||||
private static UnavailableException storageFailure(RuntimeException exception) {
|
||||
log.warn("s3 failure type={}", exception.getClass().getSimpleName());
|
||||
return new UnavailableException("Хранилище временно недоступно");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
package ru.mtuci.messenger.storage;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.context.annotation.Profile;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.stereotype.Component;
|
||||
import ru.mtuci.messenger.repository.AttachmentRepository;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
|
||||
@Component
|
||||
@Profile("!migrate")
|
||||
@ConditionalOnProperty(name = "app.s3.sweep-enabled", havingValue = "true", matchIfMissing = true)
|
||||
public class OrphanSweeper {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(OrphanSweeper.class);
|
||||
|
||||
private final ObjectStorage storage;
|
||||
private final AttachmentRepository attachments;
|
||||
private final Duration grace;
|
||||
|
||||
public OrphanSweeper(ObjectStorage storage, AttachmentRepository attachments,
|
||||
@Value("${app.s3.orphan-grace:2m}") Duration grace) {
|
||||
this.storage = storage;
|
||||
this.attachments = attachments;
|
||||
this.grace = grace;
|
||||
}
|
||||
|
||||
@Scheduled(fixedDelayString = "${app.s3.sweep-interval-ms:60000}",
|
||||
initialDelayString = "${app.s3.sweep-initial-delay-ms:60000}")
|
||||
public int sweep() {
|
||||
return sweepOlderThan(Instant.now().minus(grace));
|
||||
}
|
||||
|
||||
public int sweepOlderThan(Instant cutoff) {
|
||||
int removed = 0;
|
||||
for (ObjectStorage.StoredObject object : storage.list("originals/")) {
|
||||
if (!expiredUnreferenced(object.modified(), cutoff, attachments.existsByObjectKey(object.key()))) {
|
||||
continue;
|
||||
}
|
||||
storage.deleteQuietly(object.key());
|
||||
removed++;
|
||||
}
|
||||
if (removed > 0) {
|
||||
log.info("removed {} orphan objects older than {}", removed, cutoff);
|
||||
}
|
||||
return removed;
|
||||
}
|
||||
|
||||
static boolean expiredUnreferenced(Instant modified, Instant cutoff, boolean referenced) {
|
||||
return !referenced && !modified.isAfter(cutoff);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
package ru.mtuci.messenger.storage;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
|
||||
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
|
||||
import software.amazon.awssdk.http.apache.ApacheHttpClient;
|
||||
import software.amazon.awssdk.regions.Region;
|
||||
import software.amazon.awssdk.services.s3.S3Client;
|
||||
import software.amazon.awssdk.services.s3.S3Configuration;
|
||||
|
||||
import java.net.URI;
|
||||
import java.time.Duration;
|
||||
|
||||
@Configuration
|
||||
public class S3Config {
|
||||
|
||||
@Bean(name = "s3Data", destroyMethod = "close")
|
||||
S3Client s3Data(@Value("${app.s3.endpoint}") String endpoint,
|
||||
@Value("${app.s3.region}") String region,
|
||||
@Value("${app.s3.access-key}") String accessKey,
|
||||
@Value("${app.s3.secret-key}") String secretKey) {
|
||||
return client(endpoint, region, accessKey, secretKey);
|
||||
}
|
||||
|
||||
@Bean(name = "s3Bootstrap", destroyMethod = "close")
|
||||
S3Client s3Bootstrap(@Value("${app.s3.endpoint}") String endpoint,
|
||||
@Value("${app.s3.region}") String region,
|
||||
@Value("${app.s3.bootstrap-access-key}") String accessKey,
|
||||
@Value("${app.s3.bootstrap-secret-key}") String secretKey) {
|
||||
return client(endpoint, region, accessKey, secretKey);
|
||||
}
|
||||
|
||||
private static S3Client client(String endpoint, String region, String accessKey, String secretKey) {
|
||||
return S3Client.builder()
|
||||
.endpointOverride(URI.create(endpoint))
|
||||
.region(Region.of(region))
|
||||
.credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create(accessKey, secretKey)))
|
||||
.serviceConfiguration(S3Configuration.builder().pathStyleAccessEnabled(true).build())
|
||||
.httpClientBuilder(ApacheHttpClient.builder()
|
||||
.connectionTimeout(Duration.ofSeconds(2))
|
||||
.socketTimeout(Duration.ofSeconds(8)))
|
||||
.overrideConfiguration(config -> config
|
||||
.apiCallTimeout(Duration.ofSeconds(10))
|
||||
.apiCallAttemptTimeout(Duration.ofSeconds(8)))
|
||||
.build();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package ru.mtuci.messenger.storage;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.boot.health.contributor.Health;
|
||||
import org.springframework.boot.health.contributor.HealthIndicator;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class S3HealthIndicator implements HealthIndicator {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(S3HealthIndicator.class);
|
||||
|
||||
private final ObjectStorage storage;
|
||||
|
||||
public S3HealthIndicator(ObjectStorage storage) {
|
||||
this.storage = storage;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Health health() {
|
||||
try {
|
||||
storage.ensureBucket();
|
||||
return Health.up().build();
|
||||
} catch (RuntimeException exception) {
|
||||
log.info("s3 readiness failed type={}", exception.getClass().getSimpleName());
|
||||
return Health.down().build();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package ru.mtuci.messenger.storage;
|
||||
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Profile;
|
||||
import org.springframework.scheduling.annotation.EnableScheduling;
|
||||
|
||||
@Configuration
|
||||
@EnableScheduling
|
||||
@Profile("!migrate")
|
||||
public class SweepConfig {
|
||||
}
|
||||
@@ -0,0 +1,97 @@
|
||||
package ru.mtuci.messenger.web;
|
||||
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import jakarta.validation.ConstraintViolationException;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.dao.DataAccessException;
|
||||
import org.springframework.transaction.CannotCreateTransactionException;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.http.converter.HttpMessageNotReadableException;
|
||||
import org.springframework.web.HttpMediaTypeNotSupportedException;
|
||||
import org.springframework.web.HttpRequestMethodNotSupportedException;
|
||||
import org.springframework.web.bind.MethodArgumentNotValidException;
|
||||
import org.springframework.web.bind.MissingServletRequestParameterException;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||
import org.springframework.web.method.annotation.HandlerMethodValidationException;
|
||||
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
|
||||
import org.springframework.web.servlet.NoHandlerFoundException;
|
||||
import org.springframework.web.servlet.resource.NoResourceFoundException;
|
||||
import ru.mtuci.messenger.api.model.Error;
|
||||
import ru.mtuci.messenger.api.model.ErrorError;
|
||||
import ru.mtuci.messenger.exception.ApiException;
|
||||
import ru.mtuci.messenger.exception.UnauthorizedException;
|
||||
|
||||
@RestControllerAdvice
|
||||
public class ApiAdvice {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(ApiAdvice.class);
|
||||
|
||||
private final InstanceIdentity instanceId;
|
||||
|
||||
public ApiAdvice(InstanceIdentity instanceId) {
|
||||
this.instanceId = instanceId;
|
||||
}
|
||||
|
||||
@ExceptionHandler(ApiException.class)
|
||||
public ResponseEntity<Error> handleApi(HttpServletRequest request, HttpServletResponse response, ApiException exception) {
|
||||
log.info("api error code={}", exception.code().getValue());
|
||||
String challenge = exception instanceof UnauthorizedException unauthorized ? unauthorized.challenge() : null;
|
||||
return response(request, response, exception.status(), exception.code(), exception.getMessage(), challenge);
|
||||
}
|
||||
|
||||
@ExceptionHandler({
|
||||
HttpMessageNotReadableException.class,
|
||||
MethodArgumentNotValidException.class,
|
||||
ConstraintViolationException.class,
|
||||
HandlerMethodValidationException.class,
|
||||
MethodArgumentTypeMismatchException.class,
|
||||
MissingServletRequestParameterException.class,
|
||||
HttpRequestMethodNotSupportedException.class
|
||||
})
|
||||
public ResponseEntity<Error> handleInvalid(HttpServletRequest request, HttpServletResponse response, Exception exception) {
|
||||
log.info("invalid request type={}", exception.getClass().getSimpleName());
|
||||
return response(request, response, HttpStatus.BAD_REQUEST, ErrorError.CodeEnum.INVALID_REQUEST, "Некорректный запрос", null);
|
||||
}
|
||||
|
||||
@ExceptionHandler(HttpMediaTypeNotSupportedException.class)
|
||||
public ResponseEntity<Error> handleMedia(HttpServletRequest request, HttpServletResponse response) {
|
||||
return response(request, response, HttpStatus.UNSUPPORTED_MEDIA_TYPE, ErrorError.CodeEnum.UNSUPPORTED_MEDIA_TYPE,
|
||||
"Неподдерживаемый тип содержимого", null);
|
||||
}
|
||||
|
||||
@ExceptionHandler({NoResourceFoundException.class, NoHandlerFoundException.class})
|
||||
public ResponseEntity<Error> handleMissing(HttpServletRequest request, HttpServletResponse response) {
|
||||
return response(request, response, HttpStatus.NOT_FOUND, ErrorError.CodeEnum.NOT_FOUND, "Объект не найден", null);
|
||||
}
|
||||
|
||||
@ExceptionHandler({DataAccessException.class, CannotCreateTransactionException.class})
|
||||
public ResponseEntity<Error> handleDataAccess(HttpServletRequest request, HttpServletResponse response, Exception exception) {
|
||||
log.warn("data access failure type={}", exception.getClass().getSimpleName());
|
||||
return response(request, response, HttpStatus.SERVICE_UNAVAILABLE, ErrorError.CodeEnum.UNAVAILABLE,
|
||||
"Сервис временно недоступен", null);
|
||||
}
|
||||
|
||||
@ExceptionHandler(Exception.class)
|
||||
public ResponseEntity<Error> handleUnexpected(HttpServletRequest request, HttpServletResponse response, Exception exception) {
|
||||
log.error("unexpected failure type={}", exception.getClass().getName(), exception);
|
||||
return response(request, response, HttpStatus.INTERNAL_SERVER_ERROR, ErrorError.CodeEnum.INTERNAL_ERROR,
|
||||
"Внутренняя ошибка", null);
|
||||
}
|
||||
|
||||
private ResponseEntity<Error> response(HttpServletRequest request, HttpServletResponse response, HttpStatus status,
|
||||
ErrorError.CodeEnum code, String message, String challenge) {
|
||||
String requestId = RequestIdFilter.id(request);
|
||||
response.setHeader(RequestIdFilter.HEADER, requestId);
|
||||
response.setHeader(InstanceIdFilter.HEADER, instanceId.value());
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
if (challenge != null) {
|
||||
headers.set(HttpHeaders.WWW_AUTHENTICATE, challenge);
|
||||
}
|
||||
return new ResponseEntity<>(new Error(new ErrorError(code, message, requestId)), headers, status);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
package ru.mtuci.messenger.web;
|
||||
|
||||
import jakarta.servlet.FilterChain;
|
||||
import jakarta.servlet.ServletException;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.core.Ordered;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.filter.OncePerRequestFilter;
|
||||
import org.springframework.web.servlet.HandlerExceptionResolver;
|
||||
import ru.mtuci.messenger.exception.UnauthorizedException;
|
||||
import ru.mtuci.messenger.service.UserService;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.UUID;
|
||||
|
||||
@Component
|
||||
@Order(Ordered.HIGHEST_PRECEDENCE + 10)
|
||||
public class BearerAuthFilter extends OncePerRequestFilter {
|
||||
|
||||
public static final String TOKEN = "token";
|
||||
|
||||
private final UserService users;
|
||||
private final HandlerExceptionResolver errors;
|
||||
|
||||
public BearerAuthFilter(UserService users, @Qualifier("handlerExceptionResolver") HandlerExceptionResolver errors) {
|
||||
this.users = users;
|
||||
this.errors = errors;
|
||||
}
|
||||
|
||||
public static UUID userId(HttpServletRequest request) {
|
||||
return (UUID) request.getAttribute("userId");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
|
||||
throws ServletException, IOException {
|
||||
if (isPublic(request)) {
|
||||
chain.doFilter(request, response);
|
||||
return;
|
||||
}
|
||||
try {
|
||||
String header = request.getHeader("Authorization");
|
||||
if (header == null || !header.regionMatches(true, 0, "Bearer ", 0, 7)) {
|
||||
throw UnauthorizedException.bearer();
|
||||
}
|
||||
String token = header.substring(7).trim();
|
||||
UUID userId = users.authenticate(token).orElseThrow(UnauthorizedException::bearer);
|
||||
request.setAttribute("userId", userId);
|
||||
request.setAttribute(TOKEN, token);
|
||||
} catch (RuntimeException exception) {
|
||||
errors.resolveException(request, response, null, exception);
|
||||
return;
|
||||
}
|
||||
chain.doFilter(request, response);
|
||||
}
|
||||
|
||||
private static boolean isPublic(HttpServletRequest request) {
|
||||
String path = request.getRequestURI();
|
||||
if (HttpMethod.GET.matches(request.getMethod()) && (path.equals("/health/live") || path.equals("/health/ready")
|
||||
|| path.startsWith("/actuator/"))) {
|
||||
return true;
|
||||
}
|
||||
return HttpMethod.POST.matches(request.getMethod())
|
||||
&& (path.equals("/api/v1/users") || path.equals("/api/v1/auth/sessions"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package ru.mtuci.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;
|
||||
|
||||
@Component
|
||||
@Order(Ordered.HIGHEST_PRECEDENCE)
|
||||
public class InstanceIdFilter extends OncePerRequestFilter {
|
||||
|
||||
static final String HEADER = "X-Instance-Id";
|
||||
|
||||
private final InstanceIdentity instanceId;
|
||||
|
||||
public InstanceIdFilter(InstanceIdentity instanceId) {
|
||||
this.instanceId = instanceId;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
|
||||
throws ServletException, IOException {
|
||||
response.setHeader(HEADER, instanceId.value());
|
||||
filterChain.doFilter(request, response);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package ru.mtuci.messenger.web;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class InstanceIdentity {
|
||||
|
||||
private final String value;
|
||||
|
||||
public InstanceIdentity(@Value("${app.instance-id:local}") String configured) {
|
||||
String trimmed = configured == null ? "" : configured.trim();
|
||||
this.value = trimmed.isEmpty() ? "local" : trimmed;
|
||||
}
|
||||
|
||||
public String value() {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package ru.mtuci.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.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.util.UUID;
|
||||
|
||||
@Component
|
||||
@Order(Ordered.HIGHEST_PRECEDENCE)
|
||||
public class RequestIdFilter extends OncePerRequestFilter {
|
||||
|
||||
static final String HEADER = "X-Request-Id";
|
||||
|
||||
static String id(HttpServletRequest request) {
|
||||
Object existing = request.getAttribute(HEADER);
|
||||
if (existing != null) {
|
||||
return existing.toString();
|
||||
}
|
||||
String header = request.getHeader(HEADER);
|
||||
String id = header == null || header.isBlank() ? UUID.randomUUID().toString() : header;
|
||||
request.setAttribute(HEADER, id);
|
||||
return id;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
|
||||
throws ServletException, IOException {
|
||||
String requestId = id(request);
|
||||
response.setHeader(HEADER, requestId);
|
||||
MDC.put("requestId", requestId);
|
||||
try {
|
||||
filterChain.doFilter(request, response);
|
||||
} finally {
|
||||
MDC.remove("requestId");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package ru.mtuci.messenger.web;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||
import org.springframework.boot.jackson.autoconfigure.JsonMapperBuilderCustomizer;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.Ordered;
|
||||
import org.springframework.core.annotation.Order;
|
||||
|
||||
import java.time.OffsetDateTime;
|
||||
|
||||
@Configuration
|
||||
public class TimeFormatConfig {
|
||||
|
||||
/**
|
||||
* Python 3.9 {@code fromisoformat} принимает только 0, 3 или 6 знаков дробной секунды.
|
||||
* Jackson по умолчанию отбрасывает хвостовые нули, и время вроде {@code .38078Z} ломает публичные тесты.
|
||||
*/
|
||||
@Bean
|
||||
@Order(Ordered.LOWEST_PRECEDENCE)
|
||||
JsonMapperBuilderCustomizer utcMicroseconds() {
|
||||
JsonFormat.Value format = JsonFormat.Value.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSSSSSXXX");
|
||||
return builder -> builder.withConfigOverride(OffsetDateTime.class, override -> override.setFormat(format));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,126 @@
|
||||
server:
|
||||
address: ${APP_HOST:0.0.0.0}
|
||||
port: ${APP_PORT:8080}
|
||||
shutdown: graceful
|
||||
tomcat:
|
||||
connection-timeout: 3s
|
||||
max-http-form-post-size: 12MB
|
||||
max-swallow-size: 12MB
|
||||
error:
|
||||
include-message: never
|
||||
include-stacktrace: never
|
||||
include-binding-errors: never
|
||||
whitelabel:
|
||||
enabled: false
|
||||
|
||||
spring:
|
||||
application:
|
||||
name: messenger
|
||||
lifecycle:
|
||||
timeout-per-shutdown-phase: 4s
|
||||
threads:
|
||||
virtual:
|
||||
enabled: false
|
||||
mvc:
|
||||
problemdetails:
|
||||
enabled: false
|
||||
throw-exception-if-no-handler-found: true
|
||||
web:
|
||||
resources:
|
||||
add-mappings: false
|
||||
jackson:
|
||||
use-jackson2-defaults: false
|
||||
property-naming-strategy: SNAKE_CASE
|
||||
time-zone: UTC
|
||||
default-property-inclusion: always
|
||||
deserialization:
|
||||
fail-on-unknown-properties: true
|
||||
datatype:
|
||||
datetime:
|
||||
write-dates-as-timestamps: false
|
||||
write-utc-as-offset: false
|
||||
datasource:
|
||||
url: ${SPRING_DATASOURCE_URL:jdbc:postgresql://localhost:5432/messenger}
|
||||
username: ${SPRING_DATASOURCE_USERNAME:messenger}
|
||||
password: ${SPRING_DATASOURCE_PASSWORD:messenger}
|
||||
hikari:
|
||||
maximum-pool-size: 10
|
||||
minimum-idle: 1
|
||||
connection-timeout: 3000
|
||||
validation-timeout: 1000
|
||||
jpa:
|
||||
open-in-view: false
|
||||
hibernate:
|
||||
ddl-auto: validate
|
||||
properties:
|
||||
hibernate.jdbc.time_zone: UTC
|
||||
liquibase:
|
||||
change-log: classpath:db/changelog/db.changelog-master.yaml
|
||||
data:
|
||||
redis:
|
||||
host: ${SPRING_DATA_REDIS_HOST:localhost}
|
||||
port: ${SPRING_DATA_REDIS_PORT:6379}
|
||||
timeout: 1s
|
||||
connect-timeout: 1s
|
||||
repositories:
|
||||
enabled: false
|
||||
|
||||
management:
|
||||
endpoints:
|
||||
access:
|
||||
default: READ_ONLY
|
||||
web:
|
||||
exposure:
|
||||
include:
|
||||
- health
|
||||
- info
|
||||
- prometheus
|
||||
endpoint:
|
||||
health:
|
||||
probes:
|
||||
enabled: true
|
||||
show-details: always
|
||||
show-components: always
|
||||
cache:
|
||||
time-to-live: 0s
|
||||
group:
|
||||
readiness:
|
||||
include:
|
||||
- readinessState
|
||||
- db
|
||||
- s3
|
||||
health:
|
||||
redis:
|
||||
enabled: false
|
||||
prometheus:
|
||||
metrics:
|
||||
export:
|
||||
enabled: true
|
||||
|
||||
info:
|
||||
app:
|
||||
name: messenger
|
||||
lab: 4
|
||||
|
||||
app:
|
||||
session-ttl: ${SESSION_TTL_SECONDS:3600}s
|
||||
idempotency-ttl: ${IDEMPOTENCY_TTL_SECONDS:86400}s
|
||||
instance-id: ${HOSTNAME:local}
|
||||
upload:
|
||||
max-bytes: ${MAX_UPLOAD_BYTES:10485760}
|
||||
max-pixels: ${MAX_IMAGE_PIXELS:40000000}
|
||||
s3:
|
||||
endpoint: ${S3_ENDPOINT:http://127.0.0.1:8333}
|
||||
region: ${S3_REGION:us-east-1}
|
||||
bucket: ${S3_BUCKET:messenger-attachments}
|
||||
access-key: ${S3_ACCESS_KEY:messenger}
|
||||
secret-key: ${S3_SECRET_KEY:messenger}
|
||||
bootstrap-access-key: ${S3_BOOTSTRAP_ACCESS_KEY:messenger-bootstrap}
|
||||
bootstrap-secret-key: ${S3_BOOTSTRAP_SECRET_KEY:messenger-bootstrap}
|
||||
orphan-grace: ${S3_ORPHAN_GRACE:2m}
|
||||
sweep-interval-ms: ${S3_SWEEP_INTERVAL_MS:60000}
|
||||
sweep-initial-delay-ms: ${S3_SWEEP_INITIAL_DELAY_MS:60000}
|
||||
|
||||
logging:
|
||||
pattern:
|
||||
console: "%d{yyyy-MM-dd'T'HH:mm:ss.SSSXXX} %-5level [%X{requestId}] %msg%n"
|
||||
@@ -0,0 +1,41 @@
|
||||
databaseChangeLog:
|
||||
- changeSet:
|
||||
id: 001-users
|
||||
author: lab2
|
||||
changes:
|
||||
- createTable:
|
||||
tableName: users
|
||||
columns:
|
||||
- column:
|
||||
name: id
|
||||
type: uuid
|
||||
constraints:
|
||||
primaryKey: true
|
||||
nullable: false
|
||||
primaryKeyName: users_pkey
|
||||
- column:
|
||||
name: username
|
||||
type: varchar(32)
|
||||
constraints:
|
||||
nullable: false
|
||||
unique: true
|
||||
uniqueConstraintName: users_username_key
|
||||
- column:
|
||||
name: display_name
|
||||
type: varchar(100)
|
||||
constraints:
|
||||
nullable: false
|
||||
- column:
|
||||
name: password_hash
|
||||
type: varchar(255)
|
||||
constraints:
|
||||
nullable: false
|
||||
- column:
|
||||
name: created_at
|
||||
type: timestamp with time zone
|
||||
constraints:
|
||||
nullable: false
|
||||
- sql:
|
||||
sql: alter table users add constraint users_username_pattern check (username ~ '^[a-z0-9_]{3,32}$')
|
||||
- sql:
|
||||
sql: alter table users add constraint users_display_name_text check (char_length(btrim(display_name)) > 0)
|
||||
@@ -0,0 +1,47 @@
|
||||
databaseChangeLog:
|
||||
- changeSet:
|
||||
id: 002-sessions
|
||||
author: lab2
|
||||
changes:
|
||||
- createTable:
|
||||
tableName: sessions
|
||||
columns:
|
||||
- column:
|
||||
name: id
|
||||
type: uuid
|
||||
constraints:
|
||||
primaryKey: true
|
||||
nullable: false
|
||||
primaryKeyName: sessions_pkey
|
||||
- column:
|
||||
name: token_hash
|
||||
type: varchar(64)
|
||||
constraints:
|
||||
nullable: false
|
||||
unique: true
|
||||
uniqueConstraintName: sessions_token_hash_key
|
||||
- column:
|
||||
name: user_id
|
||||
type: uuid
|
||||
constraints:
|
||||
nullable: false
|
||||
- column:
|
||||
name: expires_at
|
||||
type: timestamp with time zone
|
||||
constraints:
|
||||
nullable: false
|
||||
- column:
|
||||
name: revoked_at
|
||||
type: timestamp with time zone
|
||||
- column:
|
||||
name: created_at
|
||||
type: timestamp with time zone
|
||||
constraints:
|
||||
nullable: false
|
||||
- addForeignKeyConstraint:
|
||||
baseTableName: sessions
|
||||
baseColumnNames: user_id
|
||||
referencedTableName: users
|
||||
referencedColumnNames: id
|
||||
constraintName: sessions_user_fk
|
||||
onDelete: RESTRICT
|
||||
@@ -0,0 +1,77 @@
|
||||
databaseChangeLog:
|
||||
- changeSet:
|
||||
id: 003-chats
|
||||
author: lab2
|
||||
changes:
|
||||
- createTable:
|
||||
tableName: chats
|
||||
columns:
|
||||
- column:
|
||||
name: id
|
||||
type: uuid
|
||||
constraints:
|
||||
primaryKey: true
|
||||
nullable: false
|
||||
primaryKeyName: chats_pkey
|
||||
- column:
|
||||
name: title
|
||||
type: varchar(100)
|
||||
constraints:
|
||||
nullable: false
|
||||
- column:
|
||||
name: created_at
|
||||
type: timestamp with time zone
|
||||
constraints:
|
||||
nullable: false
|
||||
- sql:
|
||||
sql: alter table chats add constraint chats_title_text check (char_length(btrim(title)) > 0)
|
||||
- createTable:
|
||||
tableName: chat_members
|
||||
columns:
|
||||
- column:
|
||||
name: chat_id
|
||||
type: uuid
|
||||
constraints:
|
||||
nullable: false
|
||||
- column:
|
||||
name: user_id
|
||||
type: uuid
|
||||
constraints:
|
||||
nullable: false
|
||||
- column:
|
||||
name: ordinal
|
||||
type: int
|
||||
constraints:
|
||||
nullable: false
|
||||
- addPrimaryKey:
|
||||
tableName: chat_members
|
||||
columnNames: chat_id, user_id
|
||||
constraintName: chat_members_pkey
|
||||
- addForeignKeyConstraint:
|
||||
baseTableName: chat_members
|
||||
baseColumnNames: chat_id
|
||||
referencedTableName: chats
|
||||
referencedColumnNames: id
|
||||
constraintName: chat_members_chat_fk
|
||||
onDelete: RESTRICT
|
||||
- addForeignKeyConstraint:
|
||||
baseTableName: chat_members
|
||||
baseColumnNames: user_id
|
||||
referencedTableName: users
|
||||
referencedColumnNames: id
|
||||
constraintName: chat_members_user_fk
|
||||
onDelete: RESTRICT
|
||||
- createIndex:
|
||||
indexName: chat_members_user_idx
|
||||
tableName: chat_members
|
||||
columns:
|
||||
- column:
|
||||
name: user_id
|
||||
- createIndex:
|
||||
indexName: chats_history_idx
|
||||
tableName: chats
|
||||
columns:
|
||||
- column:
|
||||
name: created_at
|
||||
- column:
|
||||
name: id
|
||||
@@ -0,0 +1,61 @@
|
||||
databaseChangeLog:
|
||||
- changeSet:
|
||||
id: 004-messages
|
||||
author: lab2
|
||||
changes:
|
||||
- createTable:
|
||||
tableName: messages
|
||||
columns:
|
||||
- column:
|
||||
name: id
|
||||
type: uuid
|
||||
constraints:
|
||||
primaryKey: true
|
||||
nullable: false
|
||||
primaryKeyName: messages_pkey
|
||||
- column:
|
||||
name: chat_id
|
||||
type: uuid
|
||||
constraints:
|
||||
nullable: false
|
||||
- column:
|
||||
name: sender_id
|
||||
type: uuid
|
||||
constraints:
|
||||
nullable: false
|
||||
- column:
|
||||
name: text
|
||||
type: varchar(4000)
|
||||
constraints:
|
||||
nullable: false
|
||||
- column:
|
||||
name: created_at
|
||||
type: timestamp with time zone
|
||||
constraints:
|
||||
nullable: false
|
||||
- addForeignKeyConstraint:
|
||||
baseTableName: messages
|
||||
baseColumnNames: chat_id
|
||||
referencedTableName: chats
|
||||
referencedColumnNames: id
|
||||
constraintName: messages_chat_fk
|
||||
onDelete: RESTRICT
|
||||
- addForeignKeyConstraint:
|
||||
baseTableName: messages
|
||||
baseColumnNames: sender_id
|
||||
referencedTableName: users
|
||||
referencedColumnNames: id
|
||||
constraintName: messages_sender_fk
|
||||
onDelete: RESTRICT
|
||||
- sql:
|
||||
sql: alter table messages add constraint messages_text_not_blank check (char_length(btrim(text)) > 0 and char_length(text) <= 4000)
|
||||
- createIndex:
|
||||
indexName: messages_history_idx
|
||||
tableName: messages
|
||||
columns:
|
||||
- column:
|
||||
name: chat_id
|
||||
- column:
|
||||
name: created_at
|
||||
- column:
|
||||
name: id
|
||||
@@ -0,0 +1,73 @@
|
||||
databaseChangeLog:
|
||||
- changeSet:
|
||||
id: 005-idempotency
|
||||
author: lab3
|
||||
changes:
|
||||
- createTable:
|
||||
tableName: idempotency_records
|
||||
columns:
|
||||
- column:
|
||||
name: id
|
||||
type: uuid
|
||||
constraints:
|
||||
primaryKey: true
|
||||
nullable: false
|
||||
primaryKeyName: idempotency_records_pkey
|
||||
- column:
|
||||
name: user_id
|
||||
type: uuid
|
||||
constraints:
|
||||
nullable: false
|
||||
- column:
|
||||
name: http_method
|
||||
type: varchar(8)
|
||||
constraints:
|
||||
nullable: false
|
||||
- column:
|
||||
name: path
|
||||
type: varchar(256)
|
||||
constraints:
|
||||
nullable: false
|
||||
- column:
|
||||
name: idempotency_key
|
||||
type: varchar(128)
|
||||
constraints:
|
||||
nullable: false
|
||||
- column:
|
||||
name: request_hash
|
||||
type: varchar(64)
|
||||
constraints:
|
||||
nullable: false
|
||||
- column:
|
||||
name: message_id
|
||||
type: uuid
|
||||
constraints:
|
||||
nullable: false
|
||||
- column:
|
||||
name: created_at
|
||||
type: timestamp with time zone
|
||||
constraints:
|
||||
nullable: false
|
||||
- column:
|
||||
name: expires_at
|
||||
type: timestamp with time zone
|
||||
constraints:
|
||||
nullable: false
|
||||
- addUniqueConstraint:
|
||||
tableName: idempotency_records
|
||||
columnNames: user_id, http_method, path, idempotency_key
|
||||
constraintName: idempotency_scope_uq
|
||||
- addForeignKeyConstraint:
|
||||
baseTableName: idempotency_records
|
||||
baseColumnNames: user_id
|
||||
referencedTableName: users
|
||||
referencedColumnNames: id
|
||||
constraintName: idempotency_user_fk
|
||||
onDelete: RESTRICT
|
||||
- addForeignKeyConstraint:
|
||||
baseTableName: idempotency_records
|
||||
baseColumnNames: message_id
|
||||
referencedTableName: messages
|
||||
referencedColumnNames: id
|
||||
constraintName: idempotency_message_fk
|
||||
onDelete: RESTRICT
|
||||
@@ -0,0 +1,127 @@
|
||||
databaseChangeLog:
|
||||
- changeSet:
|
||||
id: 006-attachments
|
||||
author: lab4
|
||||
changes:
|
||||
- sql:
|
||||
sql: alter table messages drop constraint messages_text_not_blank
|
||||
- createTable:
|
||||
tableName: attachments
|
||||
columns:
|
||||
- column:
|
||||
name: id
|
||||
type: uuid
|
||||
constraints:
|
||||
primaryKey: true
|
||||
nullable: false
|
||||
primaryKeyName: attachments_pkey
|
||||
- column:
|
||||
name: chat_id
|
||||
type: uuid
|
||||
constraints:
|
||||
nullable: false
|
||||
- column:
|
||||
name: owner_id
|
||||
type: uuid
|
||||
constraints:
|
||||
nullable: false
|
||||
- column:
|
||||
name: filename
|
||||
type: varchar(255)
|
||||
constraints:
|
||||
nullable: false
|
||||
- column:
|
||||
name: content_type
|
||||
type: varchar(32)
|
||||
constraints:
|
||||
nullable: false
|
||||
- column:
|
||||
name: size_bytes
|
||||
type: bigint
|
||||
constraints:
|
||||
nullable: false
|
||||
- column:
|
||||
name: sha256
|
||||
type: varchar(64)
|
||||
constraints:
|
||||
nullable: false
|
||||
- column:
|
||||
name: object_key
|
||||
type: varchar(80)
|
||||
constraints:
|
||||
nullable: false
|
||||
- column:
|
||||
name: status
|
||||
type: varchar(16)
|
||||
constraints:
|
||||
nullable: false
|
||||
- column:
|
||||
name: created_at
|
||||
type: timestamp with time zone
|
||||
constraints:
|
||||
nullable: false
|
||||
- column:
|
||||
name: updated_at
|
||||
type: timestamp with time zone
|
||||
constraints:
|
||||
nullable: false
|
||||
- addUniqueConstraint:
|
||||
tableName: attachments
|
||||
columnNames: object_key
|
||||
constraintName: attachments_object_key_uq
|
||||
- addForeignKeyConstraint:
|
||||
baseTableName: attachments
|
||||
baseColumnNames: chat_id
|
||||
referencedTableName: chats
|
||||
referencedColumnNames: id
|
||||
constraintName: attachments_chat_fk
|
||||
onDelete: RESTRICT
|
||||
- addForeignKeyConstraint:
|
||||
baseTableName: attachments
|
||||
baseColumnNames: owner_id
|
||||
referencedTableName: users
|
||||
referencedColumnNames: id
|
||||
constraintName: attachments_owner_fk
|
||||
onDelete: RESTRICT
|
||||
- createIndex:
|
||||
indexName: attachments_chat_idx
|
||||
tableName: attachments
|
||||
columns:
|
||||
- column:
|
||||
name: chat_id
|
||||
- createTable:
|
||||
tableName: message_attachments
|
||||
columns:
|
||||
- column:
|
||||
name: message_id
|
||||
type: uuid
|
||||
constraints:
|
||||
nullable: false
|
||||
- column:
|
||||
name: sort_order
|
||||
type: int
|
||||
constraints:
|
||||
nullable: false
|
||||
- column:
|
||||
name: attachment_id
|
||||
type: uuid
|
||||
constraints:
|
||||
nullable: false
|
||||
- addPrimaryKey:
|
||||
tableName: message_attachments
|
||||
columnNames: message_id, sort_order
|
||||
constraintName: message_attachments_pkey
|
||||
- addForeignKeyConstraint:
|
||||
baseTableName: message_attachments
|
||||
baseColumnNames: message_id
|
||||
referencedTableName: messages
|
||||
referencedColumnNames: id
|
||||
constraintName: message_attachments_message_fk
|
||||
onDelete: RESTRICT
|
||||
- addForeignKeyConstraint:
|
||||
baseTableName: message_attachments
|
||||
baseColumnNames: attachment_id
|
||||
referencedTableName: attachments
|
||||
referencedColumnNames: id
|
||||
constraintName: message_attachments_attachment_fk
|
||||
onDelete: RESTRICT
|
||||
@@ -0,0 +1,19 @@
|
||||
databaseChangeLog:
|
||||
- include:
|
||||
file: 001-users.yaml
|
||||
relativeToChangelogFile: true
|
||||
- include:
|
||||
file: 002-sessions.yaml
|
||||
relativeToChangelogFile: true
|
||||
- include:
|
||||
file: 003-chats.yaml
|
||||
relativeToChangelogFile: true
|
||||
- include:
|
||||
file: 004-messages.yaml
|
||||
relativeToChangelogFile: true
|
||||
- include:
|
||||
file: 005-idempotency.yaml
|
||||
relativeToChangelogFile: true
|
||||
- include:
|
||||
file: 006-attachments.yaml
|
||||
relativeToChangelogFile: true
|
||||
@@ -0,0 +1,281 @@
|
||||
package ru.mtuci.messenger;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.test.web.server.LocalServerPort;
|
||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||
import org.springframework.test.context.DynamicPropertyRegistry;
|
||||
import org.springframework.test.context.DynamicPropertySource;
|
||||
import ru.mtuci.messenger.repository.ChatRepository;
|
||||
import ru.mtuci.messenger.service.UserService;
|
||||
import ru.mtuci.messenger.support.ApiClient;
|
||||
import ru.mtuci.messenger.support.LabContainers;
|
||||
import tools.jackson.databind.JsonNode;
|
||||
import tools.jackson.databind.json.JsonMapper;
|
||||
|
||||
import java.util.Base64;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.Future;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
||||
class ApiIntegrationTest {
|
||||
|
||||
@LocalServerPort
|
||||
int port;
|
||||
|
||||
@Autowired
|
||||
JsonMapper jsonMapper;
|
||||
|
||||
@Autowired
|
||||
ChatRepository chatRepository;
|
||||
|
||||
@Autowired
|
||||
StringRedisTemplate redis;
|
||||
|
||||
private ApiClient api;
|
||||
|
||||
@DynamicPropertySource
|
||||
static void containers(DynamicPropertyRegistry registry) {
|
||||
LabContainers.register(registry);
|
||||
}
|
||||
|
||||
@BeforeEach
|
||||
void client() {
|
||||
api = new ApiClient(port, jsonMapper);
|
||||
}
|
||||
|
||||
@Test
|
||||
void healthAndActuatorReportProcessAndDatabase() {
|
||||
ApiClient.Result live = api.get("/health/live", null);
|
||||
assertEquals(200, live.status());
|
||||
assertEquals("ok", live.body().get("status").asString());
|
||||
assertFalse(live.requestId().isBlank());
|
||||
ApiClient.Result ready = api.get("/health/ready", null);
|
||||
assertEquals(200, ready.status());
|
||||
assertEquals("ok", ready.body().get("status").asString());
|
||||
ApiClient.Result actuator = api.get("/actuator/health/readiness", null);
|
||||
assertEquals(200, actuator.status());
|
||||
assertEquals("UP", actuator.body().get("status").asString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void registrationConflictValidationAndRace() throws Exception {
|
||||
String username = name("reg");
|
||||
ApiClient.Result created = api.post("/api/v1/users", api.user(username, "Алиса", "Example-only-not-a-real-secret-123"));
|
||||
assertEquals(201, created.status());
|
||||
assertEquals(username, created.body().get("username").asString());
|
||||
assertTrue(created.body().get("created_at").asString().endsWith("Z"));
|
||||
assertFalse(created.raw().contains("password"));
|
||||
assertEquals(created.requestId(), created.requestId());
|
||||
|
||||
ApiClient.Result conflict = api.post("/api/v1/users", api.user(username, "Алиса", "Example-only-not-a-real-secret-123"));
|
||||
assertEquals(409, conflict.status());
|
||||
assertEquals("conflict", conflict.body().get("error").get("code").asString());
|
||||
assertEquals(conflict.requestId(), conflict.body().get("error").get("request_id").asString());
|
||||
|
||||
ApiClient.Result blank = api.post("/api/v1/users", api.user(name("blank"), " ", "Example-only-not-a-real-secret-123"));
|
||||
assertEquals(400, blank.status());
|
||||
ApiClient.Result extra = api.post("/api/v1/users", Map.of("username", name("extra"), "display_name", "Имя", "password", "Example-only-not-a-real-secret-123", "role", "admin"));
|
||||
assertEquals(400, extra.status());
|
||||
|
||||
String raced = name("race");
|
||||
CountDownLatch latch = new CountDownLatch(1);
|
||||
try (ExecutorService pool = Executors.newFixedThreadPool(2)) {
|
||||
List<Future<Integer>> results = List.of(
|
||||
pool.submit(() -> once(latch, raced)),
|
||||
pool.submit(() -> once(latch, raced))
|
||||
);
|
||||
latch.countDown();
|
||||
int createdCount = 0;
|
||||
int conflictCount = 0;
|
||||
for (Future<Integer> result : results) {
|
||||
int status = result.get();
|
||||
if (status == 201) {
|
||||
createdCount++;
|
||||
} else if (status == 409) {
|
||||
conflictCount++;
|
||||
}
|
||||
}
|
||||
assertEquals(1, createdCount);
|
||||
assertEquals(1, conflictCount);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void sessionLoginLogoutAndIgnoredUserHeader() {
|
||||
String username = name("sess");
|
||||
String password = "Example-only-not-a-real-secret-123";
|
||||
JsonNode user = api.post("/api/v1/users", api.user(username, "Алиса", password)).body();
|
||||
JsonNode other = api.post("/api/v1/users", api.user(name("oth"), "Боб", password)).body();
|
||||
String basic = Base64.getEncoder().encodeToString((username + ":" + password).getBytes());
|
||||
ApiClient.Result bad = api.postBasic("/api/v1/auth/sessions", Base64.getEncoder().encodeToString((username + ":wrong-password-xx").getBytes()));
|
||||
assertEquals(401, bad.status());
|
||||
assertTrue(bad.authenticate().toLowerCase().startsWith("basic"));
|
||||
|
||||
ApiClient.Result session = api.postBasic("/api/v1/auth/sessions", basic);
|
||||
assertEquals(201, session.status());
|
||||
assertEquals("Bearer", session.body().get("token_type").asString());
|
||||
assertTrue(session.body().get("token").asString().length() >= 32);
|
||||
assertNotEquals(user.get("id").asString(), session.body().get("token").asString());
|
||||
assertEquals(user.get("id").asString(), session.body().get("user").get("id").asString());
|
||||
String token = session.body().get("token").asString();
|
||||
|
||||
ApiClient.Result basicOnApi = api.postBasic("/api/v1/users/me", basic);
|
||||
assertEquals(401, basicOnApi.status());
|
||||
assertTrue(basicOnApi.authenticate().toLowerCase().startsWith("bearer"));
|
||||
ApiClient.Result headerOnly = api.get("/api/v1/users/me", null, Map.of("X-User-Id", user.get("id").asString()));
|
||||
assertEquals(401, headerOnly.status());
|
||||
ApiClient.Result me = api.get("/api/v1/users/me", token, Map.of("X-User-Id", other.get("id").asString()));
|
||||
assertEquals(200, me.status());
|
||||
assertEquals(user.get("id").asString(), me.body().get("id").asString());
|
||||
|
||||
assertTrue(Boolean.TRUE.equals(redis.hasKey("session:" + UserService.tokenHash(token))));
|
||||
ApiClient.Result logout = api.delete("/api/v1/auth/sessions/current", token);
|
||||
assertEquals(204, logout.status());
|
||||
assertTrue(logout.raw().isBlank());
|
||||
assertFalse(Boolean.TRUE.equals(redis.hasKey("session:" + UserService.tokenHash(token))));
|
||||
assertEquals(401, api.get("/api/v1/users/me", token).status());
|
||||
assertEquals(401, api.delete("/api/v1/auth/sessions/current", token).status());
|
||||
}
|
||||
|
||||
@Test
|
||||
void chatsAreAtomicAndHiddenFromOutsiders() {
|
||||
String password = "Example-only-not-a-real-secret-123";
|
||||
JsonNode alice = register(name("alice"), "Алиса", password);
|
||||
JsonNode bob = register(name("bob"), "Боб", password);
|
||||
String aliceToken = login(alice.get("username").asString(), password);
|
||||
String bobToken = login(bob.get("username").asString(), password);
|
||||
String eveToken = login(register(name("eve"), "Ева", password).get("username").asString(), password);
|
||||
|
||||
String title = "rollback-" + UUID.randomUUID();
|
||||
ApiClient.Result unknown = api.post("/api/v1/chats", aliceToken, Map.of(
|
||||
"title", title,
|
||||
"member_ids", List.of(alice.get("id").asString(), UUID.randomUUID().toString())
|
||||
));
|
||||
assertEquals(400, unknown.status());
|
||||
assertEquals(0, chatRepository.countByTitle(title));
|
||||
|
||||
ApiClient.Result duplicates = api.post("/api/v1/chats", aliceToken, Map.of(
|
||||
"title", "dup-" + UUID.randomUUID(),
|
||||
"member_ids", List.of(alice.get("id").asString(), alice.get("id").asString())
|
||||
));
|
||||
assertEquals(400, duplicates.status());
|
||||
ApiClient.Result withoutSelf = api.post("/api/v1/chats", aliceToken, Map.of(
|
||||
"title", "self-" + UUID.randomUUID(),
|
||||
"member_ids", List.of(bob.get("id").asString())
|
||||
));
|
||||
assertEquals(400, withoutSelf.status());
|
||||
|
||||
ApiClient.Result created = api.post("/api/v1/chats", aliceToken, Map.of(
|
||||
"title", "Контрактный тест",
|
||||
"member_ids", List.of(alice.get("id").asString(), bob.get("id").asString())
|
||||
));
|
||||
assertEquals(201, created.status());
|
||||
String chatId = created.body().get("id").asString();
|
||||
assertEquals(404, api.get("/api/v1/chats/" + chatId, eveToken).status());
|
||||
JsonNode bobList = api.get("/api/v1/chats?limit=100", bobToken).body();
|
||||
assertTrue(containsId(bobList.get("items"), chatId));
|
||||
JsonNode eveList = api.get("/api/v1/chats?limit=100", eveToken).body();
|
||||
assertFalse(containsId(eveList.get("items"), chatId));
|
||||
|
||||
ApiClient.Result second = api.post("/api/v1/chats", aliceToken, Map.of(
|
||||
"title", "Второй",
|
||||
"member_ids", List.of(alice.get("id").asString())
|
||||
));
|
||||
assertEquals(201, second.status());
|
||||
JsonNode page = api.get("/api/v1/chats?limit=1", aliceToken).body();
|
||||
assertEquals(1, page.get("items").size());
|
||||
assertFalse(page.get("next_cursor").isNull());
|
||||
JsonNode next = api.get("/api/v1/chats?limit=1&cursor=" + page.get("next_cursor").asString(), aliceToken).body();
|
||||
assertEquals(1, next.get("items").size());
|
||||
assertNotEquals(page.get("items").get(0).get("id").asString(), next.get("items").get(0).get("id").asString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void messageHistoryUsesKeysetCursor() {
|
||||
String password = "Example-only-not-a-real-secret-123";
|
||||
JsonNode alice = register(name("ma"), "Алиса", password);
|
||||
JsonNode bob = register(name("mb"), "Боб", password);
|
||||
String aliceToken = login(alice.get("username").asString(), password);
|
||||
String bobToken = login(bob.get("username").asString(), password);
|
||||
String eveToken = login(register(name("me"), "Ева", password).get("username").asString(), password);
|
||||
String chatId = api.post("/api/v1/chats", aliceToken, Map.of(
|
||||
"title", "История",
|
||||
"member_ids", List.of(alice.get("id").asString(), bob.get("id").asString())
|
||||
)).body().get("id").asString();
|
||||
String otherChat = api.post("/api/v1/chats", aliceToken, Map.of(
|
||||
"title", "Другой",
|
||||
"member_ids", List.of(alice.get("id").asString())
|
||||
)).body().get("id").asString();
|
||||
|
||||
ApiClient.Result first = sendMessage(aliceToken, chatId, "Привет, мир");
|
||||
ApiClient.Result second = sendMessage(bobToken, chatId, "Сообщение Боба");
|
||||
assertEquals(201, first.status());
|
||||
assertTrue(first.body().get("created_at").asString().matches("\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}\\.\\d{6}Z"));
|
||||
assertFalse(first.instanceId().isBlank());
|
||||
assertEquals(first.instanceId(), api.get("/api/v1/users/me", aliceToken).instanceId());
|
||||
assertEquals(alice.get("id").asString(), first.body().get("sender_id").asString());
|
||||
assertEquals(201, second.status());
|
||||
assertEquals(400, sendMessage(aliceToken, chatId, "").status());
|
||||
assertEquals(400, sendMessage(aliceToken, chatId, " ").status());
|
||||
assertEquals(404, sendMessage(eveToken, chatId, "чужое").status());
|
||||
assertEquals(400, api.get("/api/v1/chats/" + chatId + "/messages?limit=0", aliceToken).status());
|
||||
|
||||
JsonNode page = api.get("/api/v1/chats/" + chatId + "/messages?limit=1", bobToken).body();
|
||||
assertEquals(1, page.get("items").size());
|
||||
assertEquals(first.body().get("id").asString(), page.get("items").get(0).get("id").asString());
|
||||
String cursor = page.get("next_cursor").asString();
|
||||
JsonNode rest = api.get("/api/v1/chats/" + chatId + "/messages?limit=1&cursor=" + cursor, aliceToken).body();
|
||||
assertEquals(second.body().get("id").asString(), rest.get("items").get(0).get("id").asString());
|
||||
assertTrue(rest.get("next_cursor").isNull());
|
||||
assertEquals(400, api.get("/api/v1/chats/" + otherChat + "/messages?cursor=" + cursor, aliceToken).status());
|
||||
}
|
||||
|
||||
private int once(CountDownLatch latch, String username) throws Exception {
|
||||
latch.await();
|
||||
return api.post("/api/v1/users", api.user(username, "Гонка", "Example-only-not-a-real-secret-123")).status();
|
||||
}
|
||||
|
||||
private JsonNode register(String username, String displayName, String password) {
|
||||
ApiClient.Result result = api.post("/api/v1/users", api.user(username, displayName, password));
|
||||
assertEquals(201, result.status());
|
||||
return result.body();
|
||||
}
|
||||
|
||||
private String login(String username, String password) {
|
||||
String basic = Base64.getEncoder().encodeToString((username + ":" + password).getBytes());
|
||||
ApiClient.Result session = api.postBasic("/api/v1/auth/sessions", basic);
|
||||
assertEquals(201, session.status());
|
||||
return session.body().get("token").asString();
|
||||
}
|
||||
|
||||
private static boolean containsId(JsonNode items, String id) {
|
||||
for (JsonNode item : items) {
|
||||
if (id.equals(item.get("id").asString())) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private ApiClient.Result sendMessage(String token, String chatId, String text) {
|
||||
return api.post("/api/v1/chats/" + chatId + "/messages", token, Map.of("text", text),
|
||||
Map.of("Idempotency-Key", UUID.randomUUID().toString()));
|
||||
}
|
||||
|
||||
private static String name(String prefix) {
|
||||
return (prefix + UUID.randomUUID().toString().replace("-", "")).substring(0, 20);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,194 @@
|
||||
package ru.mtuci.messenger;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import ru.mtuci.messenger.storage.ObjectStorage;
|
||||
import ru.mtuci.messenger.storage.OrphanSweeper;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.test.web.server.LocalServerPort;
|
||||
import org.springframework.test.context.DynamicPropertyRegistry;
|
||||
import org.springframework.test.context.DynamicPropertySource;
|
||||
import ru.mtuci.messenger.support.ApiClient;
|
||||
import ru.mtuci.messenger.support.LabContainers;
|
||||
import tools.jackson.databind.JsonNode;
|
||||
import tools.jackson.databind.json.JsonMapper;
|
||||
|
||||
import java.net.URI;
|
||||
import java.net.URLEncoder;
|
||||
import java.net.http.HttpClient;
|
||||
import java.net.http.HttpRequest;
|
||||
import java.net.http.HttpResponse;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.security.MessageDigest;
|
||||
import java.util.Base64;
|
||||
import java.util.HexFormat;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.time.Instant;
|
||||
import java.util.UUID;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
||||
class AttachmentIntegrationTest {
|
||||
|
||||
@LocalServerPort
|
||||
int port;
|
||||
|
||||
@Autowired
|
||||
JsonMapper jsonMapper;
|
||||
|
||||
@Autowired
|
||||
ObjectStorage storage;
|
||||
|
||||
@Autowired
|
||||
OrphanSweeper sweeper;
|
||||
|
||||
private ApiClient api;
|
||||
private final HttpClient http = HttpClient.newHttpClient();
|
||||
|
||||
@DynamicPropertySource
|
||||
static void containers(DynamicPropertyRegistry registry) {
|
||||
LabContainers.register(registry);
|
||||
}
|
||||
|
||||
@BeforeEach
|
||||
void client() {
|
||||
api = new ApiClient(port, jsonMapper);
|
||||
}
|
||||
|
||||
@Test
|
||||
void uploadIsPrivateAndReadableByAnotherMember() throws Exception {
|
||||
String password = "Example-only-not-a-real-secret-123";
|
||||
JsonNode alice = register("at", password);
|
||||
JsonNode bob = register("bt", password);
|
||||
JsonNode eve = register("et", password);
|
||||
String aliceToken = login(alice.get("username").asString(), password);
|
||||
String bobToken = login(bob.get("username").asString(), password);
|
||||
String eveToken = login(eve.get("username").asString(), password);
|
||||
String chatId = chat(aliceToken, List.of(alice.get("id").asString(), bob.get("id").asString()));
|
||||
String otherChat = chat(aliceToken, List.of(alice.get("id").asString()));
|
||||
byte[] png = Files.readAllBytes(Path.of("../tests/fixtures/sample.png"));
|
||||
|
||||
HttpResponse<String> created = upload(aliceToken, chatId, "sample.png", "image/png", png);
|
||||
assertEquals(201, created.statusCode());
|
||||
JsonNode attachment = jsonMapper.readTree(created.body());
|
||||
assertEquals("ready", attachment.get("status").asString());
|
||||
assertEquals(sha256(png), attachment.get("sha256").asString());
|
||||
assertEquals(png.length, attachment.get("size_bytes").asInt());
|
||||
assertEquals(false, attachment.has("object_key"));
|
||||
|
||||
HttpResponse<byte[]> download = download(bobToken, attachment.get("id").asString());
|
||||
assertEquals(200, download.statusCode());
|
||||
assertEquals("image/png", download.headers().firstValue("Content-Type").orElse(""));
|
||||
assertArrayEquals(png, download.body());
|
||||
assertEquals(404, api.get("/api/v1/attachments/" + attachment.get("id").asString(), eveToken).status());
|
||||
assertEquals(404, download(eveToken, attachment.get("id").asString()).statusCode());
|
||||
|
||||
ApiClient.Result message = api.post("/api/v1/chats/" + chatId + "/messages", aliceToken,
|
||||
Map.of("text", "", "attachment_ids", List.of(attachment.get("id").asString())),
|
||||
Map.of("Idempotency-Key", UUID.randomUUID().toString()));
|
||||
assertEquals(201, message.status());
|
||||
assertEquals(attachment.get("id").asString(), message.body().get("attachment_ids").get(0).asString());
|
||||
|
||||
HttpResponse<String> badName = upload(aliceToken, chatId, "../secret.png", "image/png", png);
|
||||
assertEquals(400, badName.statusCode());
|
||||
HttpResponse<String> spoofed = upload(aliceToken, chatId, "photo.png", "image/png", new byte[] {1, 2, 3, 4});
|
||||
assertEquals(415, spoofed.statusCode());
|
||||
ApiClient.Result foreign = api.post("/api/v1/chats/" + otherChat + "/messages", aliceToken,
|
||||
Map.of("text", "", "attachment_ids", List.of(attachment.get("id").asString())),
|
||||
Map.of("Idempotency-Key", UUID.randomUUID().toString()));
|
||||
assertEquals(404, foreign.status());
|
||||
assertEquals("not_found", foreign.body().get("error").get("code").asString());
|
||||
|
||||
ApiClient.Result missing = api.post("/api/v1/chats/" + chatId + "/messages", aliceToken,
|
||||
Map.of("text", "", "attachment_ids", List.of(UUID.randomUUID().toString())),
|
||||
Map.of("Idempotency-Key", UUID.randomUUID().toString()));
|
||||
assertEquals(404, missing.status());
|
||||
|
||||
HttpResponse<String> corrupt = upload(aliceToken, chatId, "broken.png", "image/png",
|
||||
new byte[] {(byte) 137, 80, 78, 71, 13, 10, 26, 10, 0, 1, 2});
|
||||
assertEquals(415, corrupt.statusCode());
|
||||
assertEquals("unsupported_media_type", jsonMapper.readTree(corrupt.body()).get("error").get("code").asString());
|
||||
|
||||
byte[] jpeg = new byte[] {
|
||||
(byte) 0xff, (byte) 0xd8,
|
||||
(byte) 0xff, (byte) 0xc0, 0x00, 0x0b, 0x08, 0x00, 0x01, 0x00, 0x01, 0x01, 0x01, 0x11, 0x00,
|
||||
(byte) 0xff, (byte) 0xd9
|
||||
};
|
||||
HttpResponse<String> mime = upload(aliceToken, chatId, "photo.png", "image/png", jpeg);
|
||||
assertEquals(415, mime.statusCode());
|
||||
|
||||
HttpResponse<String> tooBig = upload(aliceToken, chatId, "big.png", "image/png", new byte[10_485_761]);
|
||||
assertEquals(413, tooBig.statusCode());
|
||||
assertEquals("payload_too_large", jsonMapper.readTree(tooBig.body()).get("error").get("code").asString());
|
||||
assertTrue(tooBig.statusCode() < 500);
|
||||
}
|
||||
|
||||
@Test
|
||||
void sweepDropsOrphanAndKeepsReferencedObject() throws Exception {
|
||||
String password = "Example-only-not-a-real-secret-123";
|
||||
JsonNode alice = register("sw", password);
|
||||
String token = login(alice.get("username").asString(), password);
|
||||
String chatId = chat(token, List.of(alice.get("id").asString()));
|
||||
byte[] png = Files.readAllBytes(Path.of("../tests/fixtures/sample.png"));
|
||||
HttpResponse<String> created = upload(token, chatId, "sample.png", "image/png", png);
|
||||
assertEquals(201, created.statusCode());
|
||||
String id = jsonMapper.readTree(created.body()).get("id").asString();
|
||||
|
||||
storage.put("originals/orphan-" + UUID.randomUUID(), new byte[] {9, 9, 9}, "application/octet-stream");
|
||||
int removed = sweeper.sweepOlderThan(Instant.now().plusSeconds(120));
|
||||
assertTrue(removed >= 1);
|
||||
assertEquals(200, download(token, id).statusCode());
|
||||
assertTrue(storage.list("originals/").stream().noneMatch(object -> object.key().contains("orphan-")));
|
||||
}
|
||||
|
||||
private HttpResponse<String> upload(String token, String chatId, String filename, String type, byte[] body) throws Exception {
|
||||
String name = URLEncoder.encode(filename, StandardCharsets.UTF_8);
|
||||
HttpRequest request = HttpRequest.newBuilder(URI.create(base() + "/api/v1/chats/" + chatId + "/attachments?filename=" + name))
|
||||
.header("Authorization", "Bearer " + token)
|
||||
.header("Content-Type", type)
|
||||
.POST(HttpRequest.BodyPublishers.ofByteArray(body))
|
||||
.build();
|
||||
return http.send(request, HttpResponse.BodyHandlers.ofString(StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
private HttpResponse<byte[]> download(String token, String attachmentId) throws Exception {
|
||||
HttpRequest request = HttpRequest.newBuilder(URI.create(base() + "/api/v1/attachments/" + attachmentId + "/content"))
|
||||
.header("Authorization", "Bearer " + token)
|
||||
.GET()
|
||||
.build();
|
||||
return http.send(request, HttpResponse.BodyHandlers.ofByteArray());
|
||||
}
|
||||
|
||||
private String base() {
|
||||
return "http://127.0.0.1:" + port;
|
||||
}
|
||||
|
||||
private String chat(String token, List<String> members) {
|
||||
return api.post("/api/v1/chats", token, Map.of("title", "Вложения", "member_ids", members)).body().get("id").asString();
|
||||
}
|
||||
|
||||
private JsonNode register(String prefix, String password) {
|
||||
String username = (prefix + UUID.randomUUID().toString().replace("-", "")).substring(0, 20);
|
||||
ApiClient.Result result = api.post("/api/v1/users", api.user(username, "Алиса", password));
|
||||
assertEquals(201, result.status());
|
||||
return result.body();
|
||||
}
|
||||
|
||||
private String login(String username, String password) {
|
||||
String basic = Base64.getEncoder().encodeToString((username + ":" + password).getBytes(StandardCharsets.UTF_8));
|
||||
ApiClient.Result session = api.postBasic("/api/v1/auth/sessions", basic);
|
||||
assertEquals(201, session.status());
|
||||
return session.body().get("token").asString();
|
||||
}
|
||||
|
||||
private static String sha256(byte[] bytes) throws Exception {
|
||||
return HexFormat.of().formatHex(MessageDigest.getInstance("SHA-256").digest(bytes));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
package ru.mtuci.messenger;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.Timeout;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.test.web.server.LocalServerPort;
|
||||
import org.springframework.test.annotation.DirtiesContext;
|
||||
import org.springframework.test.context.DynamicPropertyRegistry;
|
||||
import org.springframework.test.context.DynamicPropertySource;
|
||||
import org.testcontainers.postgresql.PostgreSQLContainer;
|
||||
import ru.mtuci.messenger.support.ApiClient;
|
||||
import ru.mtuci.messenger.support.LabContainers;
|
||||
import tools.jackson.databind.json.JsonMapper;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, properties = {
|
||||
"spring.data.redis.host=127.0.0.1",
|
||||
"spring.data.redis.port=1",
|
||||
"spring.data.redis.timeout=200ms",
|
||||
"spring.data.redis.connect-timeout=200ms",
|
||||
"spring.datasource.hikari.connection-timeout=1000",
|
||||
"spring.datasource.hikari.validation-timeout=500",
|
||||
"spring.datasource.hikari.maximum-pool-size=2"
|
||||
})
|
||||
@DirtiesContext
|
||||
class DatabaseOutageIntegrationTest {
|
||||
|
||||
private static final PostgreSQLContainer POSTGRES = new PostgreSQLContainer("postgres:16.10");
|
||||
|
||||
static {
|
||||
POSTGRES.start();
|
||||
}
|
||||
|
||||
@LocalServerPort
|
||||
int port;
|
||||
|
||||
@Autowired
|
||||
JsonMapper jsonMapper;
|
||||
|
||||
private ApiClient api;
|
||||
|
||||
@DynamicPropertySource
|
||||
static void database(DynamicPropertyRegistry registry) {
|
||||
registry.add("spring.datasource.url", POSTGRES::getJdbcUrl);
|
||||
registry.add("spring.datasource.username", POSTGRES::getUsername);
|
||||
registry.add("spring.datasource.password", POSTGRES::getPassword);
|
||||
LabContainers.registerS3(registry);
|
||||
}
|
||||
|
||||
@BeforeEach
|
||||
void client() {
|
||||
api = new ApiClient(port, jsonMapper);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Timeout(40)
|
||||
void readinessAndWritesFailWhenDatabaseStops() {
|
||||
assertEquals(200, api.get("/health/ready", null).status());
|
||||
POSTGRES.stop();
|
||||
ApiClient.Result ready = api.get("/health/ready", null);
|
||||
assertEquals(503, ready.status());
|
||||
assertEquals("unavailable", ready.body().get("error").get("code").asString());
|
||||
ApiClient.Result register = api.post("/api/v1/users", api.user(
|
||||
("db" + UUID.randomUUID().toString().replace("-", "")).substring(0, 16),
|
||||
"База",
|
||||
"Example-only-not-a-real-secret-123"
|
||||
));
|
||||
assertEquals(503, register.status());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
package ru.mtuci.messenger;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.test.context.DynamicPropertyRegistry;
|
||||
import org.springframework.test.context.DynamicPropertySource;
|
||||
import ru.mtuci.messenger.support.LabContainers;
|
||||
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.sql.Timestamp;
|
||||
import java.time.Instant;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
@SpringBootTest
|
||||
class HistoryIndexIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
JdbcTemplate jdbc;
|
||||
|
||||
@DynamicPropertySource
|
||||
static void containers(DynamicPropertyRegistry registry) {
|
||||
LabContainers.register(registry);
|
||||
}
|
||||
|
||||
@Test
|
||||
void historyPageUsesCompositeIndex() throws Exception {
|
||||
UUID userId = UUID.randomUUID();
|
||||
UUID chatId = UUID.randomUUID();
|
||||
Instant created = Instant.parse("2026-01-01T00:00:00Z");
|
||||
jdbc.update("insert into users (id, username, display_name, password_hash, created_at) values (?, ?, ?, ?, ?)",
|
||||
userId, ("idx" + userId.toString().replace("-", "")).substring(0, 16), "Индекс", "hash", Timestamp.from(created));
|
||||
jdbc.update("insert into chats (id, title, created_at) values (?, ?, ?)", chatId, "Индекс", Timestamp.from(created));
|
||||
jdbc.update("insert into chat_members (chat_id, user_id, ordinal) values (?, ?, 0)", chatId, userId);
|
||||
|
||||
List<Object[]> batch = new ArrayList<>();
|
||||
for (int index = 0; index < 10_000; index++) {
|
||||
batch.add(new Object[]{UUID.randomUUID(), chatId, userId, "m" + index, Timestamp.from(created.plusMillis(index))});
|
||||
}
|
||||
jdbc.batchUpdate("insert into messages (id, chat_id, sender_id, text, created_at) values (?, ?, ?, ?, ?)", batch);
|
||||
jdbc.execute("analyze messages");
|
||||
String plan = String.join("\n", jdbc.query("""
|
||||
explain (analyze, buffers)
|
||||
select id from messages
|
||||
where chat_id = ?
|
||||
and (created_at, id) > (?, ?)
|
||||
order by created_at asc, id asc
|
||||
limit 50
|
||||
""", (rs, row) -> rs.getString(1), chatId, Timestamp.from(created.plusMillis(5000)), UUID.randomUUID()));
|
||||
Path evidence = Files.isDirectory(Path.of("evidence")) ? Path.of("evidence") : Path.of("..", "evidence");
|
||||
Files.createDirectories(evidence);
|
||||
Files.writeString(evidence.resolve("explain-messages.txt"), plan + "\n");
|
||||
assertTrue(plan.contains("messages_history_idx"), plan);
|
||||
assertTrue(plan.contains("Index Cond"), plan);
|
||||
assertFalse(plan.contains("Seq Scan on messages"), plan);
|
||||
assertFalse(plan.contains("Rows Removed by Filter"), plan);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,164 @@
|
||||
package ru.mtuci.messenger;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.test.web.server.LocalServerPort;
|
||||
import org.springframework.test.context.DynamicPropertyRegistry;
|
||||
import org.springframework.test.context.DynamicPropertySource;
|
||||
import ru.mtuci.messenger.repository.MessageRepository;
|
||||
import ru.mtuci.messenger.support.ApiClient;
|
||||
import ru.mtuci.messenger.support.LabContainers;
|
||||
import tools.jackson.databind.JsonNode;
|
||||
import tools.jackson.databind.json.JsonMapper;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Base64;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.Future;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
||||
|
||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
||||
class IdempotencyIntegrationTest {
|
||||
|
||||
@LocalServerPort
|
||||
int port;
|
||||
|
||||
@Autowired
|
||||
JsonMapper jsonMapper;
|
||||
|
||||
@Autowired
|
||||
MessageRepository messages;
|
||||
|
||||
private ApiClient api;
|
||||
|
||||
@DynamicPropertySource
|
||||
static void containers(DynamicPropertyRegistry registry) {
|
||||
LabContainers.register(registry);
|
||||
}
|
||||
|
||||
@BeforeEach
|
||||
void client() {
|
||||
api = new ApiClient(port, jsonMapper);
|
||||
}
|
||||
|
||||
@Test
|
||||
void replayConflictAndRejectedKey() {
|
||||
String password = "Example-only-not-a-real-secret-123";
|
||||
JsonNode alice = register(name("ia"), password);
|
||||
String token = login(alice.get("username").asString(), password);
|
||||
String chatId = chat(token, List.of(alice.get("id").asString()));
|
||||
String key = "same-" + UUID.randomUUID();
|
||||
|
||||
ApiClient.Result rejected = send(token, chatId, "ещё не записано", key.replace("same", "bad key"));
|
||||
assertEquals(400, rejected.status());
|
||||
assertFalse(rejected.instanceId().isBlank());
|
||||
assertEquals(400, api.post("/api/v1/chats/" + chatId + "/messages", token, Map.of("text", "без ключа")).status());
|
||||
|
||||
String recover = "recover-" + UUID.randomUUID();
|
||||
assertEquals(400, send(token, chatId, "", recover).status());
|
||||
ApiClient.Result first = send(token, chatId, "Привет, мир", recover);
|
||||
assertEquals(201, first.status());
|
||||
ApiClient.Result replay = send(token, chatId, "Привет, мир", recover);
|
||||
assertEquals(201, replay.status());
|
||||
assertEquals(first.body(), replay.body());
|
||||
assertEquals(409, send(token, chatId, "другой body", recover).status());
|
||||
assertEquals(1, messages.countByChatId(UUID.fromString(chatId)));
|
||||
|
||||
ApiClient.Result fresh = send(token, chatId, "Привет, мир", key);
|
||||
assertEquals(201, fresh.status());
|
||||
assertNotEquals(first.body().get("id").asString(), fresh.body().get("id").asString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void sameKeyIsIndependentAcrossUsersAndChats() {
|
||||
String password = "Example-only-not-a-real-secret-123";
|
||||
JsonNode alice = register(name("ib"), password);
|
||||
JsonNode bob = register(name("ic"), password);
|
||||
String aliceToken = login(alice.get("username").asString(), password);
|
||||
String bobToken = login(bob.get("username").asString(), password);
|
||||
String shared = chat(aliceToken, List.of(alice.get("id").asString(), bob.get("id").asString()));
|
||||
String other = chat(aliceToken, List.of(alice.get("id").asString()));
|
||||
String key = "shared-" + UUID.randomUUID();
|
||||
|
||||
ApiClient.Result aliceMessage = send(aliceToken, shared, "общий ключ", key);
|
||||
ApiClient.Result bobMessage = send(bobToken, shared, "общий ключ", key);
|
||||
ApiClient.Result otherChat = send(aliceToken, other, "общий ключ", key);
|
||||
assertEquals(201, aliceMessage.status());
|
||||
assertEquals(201, bobMessage.status());
|
||||
assertEquals(201, otherChat.status());
|
||||
assertNotEquals(aliceMessage.body().get("id").asString(), bobMessage.body().get("id").asString());
|
||||
assertNotEquals(aliceMessage.body().get("id").asString(), otherChat.body().get("id").asString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void parallelSameKeyCreatesOneMessage() throws Exception {
|
||||
String password = "Example-only-not-a-real-secret-123";
|
||||
JsonNode alice = register(name("ip"), password);
|
||||
String token = login(alice.get("username").asString(), password);
|
||||
String chatId = chat(token, List.of(alice.get("id").asString()));
|
||||
String key = "parallel-" + UUID.randomUUID();
|
||||
int threads = 8;
|
||||
ExecutorService pool = Executors.newFixedThreadPool(threads);
|
||||
CountDownLatch start = new CountDownLatch(1);
|
||||
try {
|
||||
List<Future<ApiClient.Result>> futures = new ArrayList<>();
|
||||
for (int i = 0; i < threads; i++) {
|
||||
futures.add(pool.submit(() -> {
|
||||
start.await();
|
||||
return send(token, chatId, "одно сообщение", key);
|
||||
}));
|
||||
}
|
||||
start.countDown();
|
||||
JsonNode body = null;
|
||||
for (Future<ApiClient.Result> future : futures) {
|
||||
ApiClient.Result result = future.get();
|
||||
assertEquals(201, result.status());
|
||||
if (body == null) {
|
||||
body = result.body();
|
||||
} else {
|
||||
assertEquals(body, result.body());
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
pool.shutdownNow();
|
||||
}
|
||||
assertEquals(1, messages.countByChatId(UUID.fromString(chatId)));
|
||||
}
|
||||
|
||||
private ApiClient.Result send(String token, String chatId, String text, String key) {
|
||||
return api.post("/api/v1/chats/" + chatId + "/messages", token, Map.of("text", text),
|
||||
Map.of("Idempotency-Key", key));
|
||||
}
|
||||
|
||||
private String chat(String token, List<String> members) {
|
||||
return api.post("/api/v1/chats", token, Map.of("title", "Идемпотентность", "member_ids", members))
|
||||
.body().get("id").asString();
|
||||
}
|
||||
|
||||
private JsonNode register(String username, String password) {
|
||||
ApiClient.Result result = api.post("/api/v1/users", api.user(username, "Алиса", password));
|
||||
assertEquals(201, result.status());
|
||||
return result.body();
|
||||
}
|
||||
|
||||
private String login(String username, String password) {
|
||||
String basic = Base64.getEncoder().encodeToString((username + ":" + password).getBytes());
|
||||
ApiClient.Result session = api.postBasic("/api/v1/auth/sessions", basic);
|
||||
assertEquals(201, session.status());
|
||||
return session.body().get("token").asString();
|
||||
}
|
||||
|
||||
private static String name(String prefix) {
|
||||
return (prefix + UUID.randomUUID().toString().replace("-", "")).substring(0, 20);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
package ru.mtuci.messenger;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.test.web.server.LocalServerPort;
|
||||
import org.springframework.test.context.DynamicPropertyRegistry;
|
||||
import org.springframework.test.context.DynamicPropertySource;
|
||||
import ru.mtuci.messenger.support.ApiClient;
|
||||
import ru.mtuci.messenger.support.LabContainers;
|
||||
import tools.jackson.databind.json.JsonMapper;
|
||||
|
||||
import java.util.Base64;
|
||||
import java.util.UUID;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, properties = {
|
||||
"spring.data.redis.host=127.0.0.1",
|
||||
"spring.data.redis.port=1",
|
||||
"spring.data.redis.timeout=200ms",
|
||||
"spring.data.redis.connect-timeout=200ms"
|
||||
})
|
||||
class RedisOutageIntegrationTest {
|
||||
|
||||
@LocalServerPort
|
||||
int port;
|
||||
|
||||
@Autowired
|
||||
JsonMapper jsonMapper;
|
||||
|
||||
private ApiClient api;
|
||||
|
||||
@DynamicPropertySource
|
||||
static void database(DynamicPropertyRegistry registry) {
|
||||
registry.add("spring.datasource.url", LabContainers.POSTGRES::getJdbcUrl);
|
||||
registry.add("spring.datasource.username", LabContainers.POSTGRES::getUsername);
|
||||
registry.add("spring.datasource.password", LabContainers.POSTGRES::getPassword);
|
||||
LabContainers.registerS3(registry);
|
||||
}
|
||||
|
||||
@BeforeEach
|
||||
void client() {
|
||||
api = new ApiClient(port, jsonMapper);
|
||||
}
|
||||
|
||||
@Test
|
||||
void databaseSessionWorksWhenRedisIsDown() {
|
||||
assertEquals(200, api.get("/health/ready", null).status());
|
||||
String username = ("red" + UUID.randomUUID().toString().replace("-", "")).substring(0, 16);
|
||||
String password = "Example-only-not-a-real-secret-123";
|
||||
assertEquals(201, api.post("/api/v1/users", api.user(username, "Кэш", password)).status());
|
||||
String basic = Base64.getEncoder().encodeToString((username + ":" + password).getBytes());
|
||||
ApiClient.Result session = api.postBasic("/api/v1/auth/sessions", basic);
|
||||
assertEquals(201, session.status());
|
||||
ApiClient.Result me = api.get("/api/v1/users/me", session.body().get("token").asString());
|
||||
assertEquals(200, me.status());
|
||||
assertEquals(username, me.body().get("username").asString());
|
||||
assertEquals(200, api.get("/actuator/health", null).status());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
package ru.mtuci.messenger;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.test.web.server.LocalServerPort;
|
||||
import org.springframework.test.context.DynamicPropertyRegistry;
|
||||
import org.springframework.test.context.DynamicPropertySource;
|
||||
import ru.mtuci.messenger.support.ApiClient;
|
||||
import ru.mtuci.messenger.support.LabContainers;
|
||||
import tools.jackson.databind.json.JsonMapper;
|
||||
|
||||
import java.util.Base64;
|
||||
import java.util.UUID;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, properties = "app.session-ttl=2s")
|
||||
class SessionExpiryIntegrationTest {
|
||||
|
||||
@LocalServerPort
|
||||
int port;
|
||||
|
||||
@Autowired
|
||||
JsonMapper jsonMapper;
|
||||
|
||||
private ApiClient api;
|
||||
|
||||
@DynamicPropertySource
|
||||
static void containers(DynamicPropertyRegistry registry) {
|
||||
LabContainers.register(registry);
|
||||
}
|
||||
|
||||
@BeforeEach
|
||||
void client() {
|
||||
api = new ApiClient(port, jsonMapper);
|
||||
}
|
||||
|
||||
@Test
|
||||
void expiredTokenIsRejected() throws Exception {
|
||||
String username = ("exp" + UUID.randomUUID().toString().replace("-", "")).substring(0, 16);
|
||||
String password = "Example-only-not-a-real-secret-123";
|
||||
assertEquals(201, api.post("/api/v1/users", api.user(username, "Срок", password)).status());
|
||||
String basic = Base64.getEncoder().encodeToString((username + ":" + password).getBytes());
|
||||
String token = api.postBasic("/api/v1/auth/sessions", basic).body().get("token").asString();
|
||||
assertEquals(200, api.get("/api/v1/users/me", token).status());
|
||||
Thread.sleep(2500);
|
||||
ApiClient.Result expired = api.get("/api/v1/users/me", token);
|
||||
assertEquals(401, expired.status());
|
||||
assertEquals("unauthorized", expired.body().get("error").get("code").asString());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
package ru.mtuci.messenger.service;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import ru.mtuci.messenger.exception.PayloadTooLargeException;
|
||||
import ru.mtuci.messenger.exception.UnsupportedMediaException;
|
||||
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
class ImageFilesTest {
|
||||
|
||||
@Test
|
||||
void readsFixtureWithoutDecodingPixels() throws Exception {
|
||||
byte[] png = Files.readAllBytes(Path.of("../tests/fixtures/sample.png"));
|
||||
ImageFiles.Image image = ImageFiles.inspect(png, "image/png", 40_000_000);
|
||||
assertEquals("image/png", image.contentType());
|
||||
assertEquals(320, image.width());
|
||||
assertEquals(200, image.height());
|
||||
assertArrayEquals(png, ImageFiles.readLimited(new java.io.ByteArrayInputStream(png), 10_485_760));
|
||||
}
|
||||
|
||||
@Test
|
||||
void rejectsSpoofedMimeCorruptAndPixelBomb() {
|
||||
byte[] jpeg = new byte[] {
|
||||
(byte) 0xff, (byte) 0xd8,
|
||||
(byte) 0xff, (byte) 0xc0, 0x00, 0x0b, 0x08, 0x00, 0x01, 0x00, 0x01, 0x01, 0x01, 0x11, 0x00,
|
||||
(byte) 0xff, (byte) 0xd9
|
||||
};
|
||||
assertEquals(1, ImageFiles.inspect(jpeg, "image/jpeg", 40_000_000).width());
|
||||
assertThrows(UnsupportedMediaException.class, () -> ImageFiles.inspect(jpeg, "image/png", 40_000_000));
|
||||
assertThrows(UnsupportedMediaException.class, () -> ImageFiles.inspect(new byte[] {1, 2, 3}, "image/png", 40_000_000));
|
||||
byte[] bomb = png(8_000, 8_000);
|
||||
assertThrows(PayloadTooLargeException.class, () -> ImageFiles.inspect(bomb, "image/png", 40_000_000));
|
||||
byte[] huge = new byte[8];
|
||||
assertThrows(PayloadTooLargeException.class, () -> ImageFiles.readLimited(new java.io.ByteArrayInputStream(huge), 7));
|
||||
}
|
||||
|
||||
private static byte[] png(int width, int height) {
|
||||
byte[] data = new byte[8 + 12 + 13 + 12];
|
||||
byte[] signature = new byte[] {(byte) 137, 80, 78, 71, 13, 10, 26, 10};
|
||||
System.arraycopy(signature, 0, data, 0, 8);
|
||||
data[11] = 13;
|
||||
data[12] = 'I';
|
||||
data[13] = 'H';
|
||||
data[14] = 'D';
|
||||
data[15] = 'R';
|
||||
putInt(data, 16, width);
|
||||
putInt(data, 20, height);
|
||||
data[24] = 8;
|
||||
data[25] = 2;
|
||||
data[37] = 'I';
|
||||
data[38] = 'E';
|
||||
data[39] = 'N';
|
||||
data[40] = 'D';
|
||||
return data;
|
||||
}
|
||||
|
||||
private static void putInt(byte[] data, int offset, int value) {
|
||||
data[offset] = (byte) (value >>> 24);
|
||||
data[offset + 1] = (byte) (value >>> 16);
|
||||
data[offset + 2] = (byte) (value >>> 8);
|
||||
data[offset + 3] = (byte) value;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package ru.mtuci.messenger.service;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import ru.mtuci.messenger.exception.InvalidRequestException;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
class TextRulesTest {
|
||||
|
||||
@Test
|
||||
void rejectsBlankAndTooShortPassword() {
|
||||
assertThrows(InvalidRequestException.class, () -> TextRules.displayName(" "));
|
||||
assertThrows(InvalidRequestException.class, () -> TextRules.message(" \n "));
|
||||
assertThrows(InvalidRequestException.class, () -> TextRules.password("short"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package ru.mtuci.messenger.storage;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.time.Instant;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
class OrphanSweeperTest {
|
||||
|
||||
@Test
|
||||
void graceKeepsFreshObjectAndReferencedRow() {
|
||||
Instant cutoff = Instant.parse("2026-11-06T07:00:00Z");
|
||||
assertFalse(OrphanSweeper.expiredUnreferenced(cutoff.plusSeconds(1), cutoff, false));
|
||||
assertFalse(OrphanSweeper.expiredUnreferenced(cutoff.minusSeconds(30), cutoff, true));
|
||||
assertTrue(OrphanSweeper.expiredUnreferenced(cutoff.minusSeconds(30), cutoff, false));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
package ru.mtuci.messenger.support;
|
||||
|
||||
import tools.jackson.databind.JsonNode;
|
||||
import tools.jackson.databind.json.JsonMapper;
|
||||
|
||||
import java.net.URI;
|
||||
import java.net.http.HttpClient;
|
||||
import java.net.http.HttpRequest;
|
||||
import java.net.http.HttpResponse;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.time.Duration;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public final class ApiClient {
|
||||
|
||||
private final HttpClient http = HttpClient.newBuilder().connectTimeout(Duration.ofSeconds(5)).build();
|
||||
private final String base;
|
||||
private final JsonMapper json;
|
||||
|
||||
public ApiClient(int port, JsonMapper json) {
|
||||
this.base = "http://127.0.0.1:" + port;
|
||||
this.json = json;
|
||||
}
|
||||
|
||||
public Result get(String path, String token) {
|
||||
return send("GET", path, token, null, null, null);
|
||||
}
|
||||
|
||||
public Result get(String path, String token, Map<String, String> headers) {
|
||||
return send("GET", path, token, null, null, headers);
|
||||
}
|
||||
|
||||
public Result post(String path, Object body) {
|
||||
return send("POST", path, null, null, body, null);
|
||||
}
|
||||
|
||||
public Result post(String path, String token, Object body) {
|
||||
return send("POST", path, token, null, body, null);
|
||||
}
|
||||
|
||||
public Result post(String path, String token, Object body, Map<String, String> headers) {
|
||||
return send("POST", path, token, null, body, headers);
|
||||
}
|
||||
|
||||
public Result postBasic(String path, String basic) {
|
||||
return send("POST", path, null, basic, null, null);
|
||||
}
|
||||
|
||||
public Result delete(String path, String token) {
|
||||
return send("DELETE", path, token, null, null, null);
|
||||
}
|
||||
|
||||
private Result send(String method, String path, String token, String basic, Object body, Map<String, String> extra) {
|
||||
HttpRequest.Builder builder = HttpRequest.newBuilder(URI.create(base + path)).timeout(Duration.ofSeconds(20));
|
||||
if (body == null) {
|
||||
builder.method(method, HttpRequest.BodyPublishers.noBody());
|
||||
} else {
|
||||
builder.method(method, HttpRequest.BodyPublishers.ofString(json.writeValueAsString(body), StandardCharsets.UTF_8));
|
||||
builder.header("Content-Type", "application/json");
|
||||
}
|
||||
if (token != null) {
|
||||
builder.header("Authorization", "Bearer " + token);
|
||||
}
|
||||
if (basic != null) {
|
||||
builder.header("Authorization", "Basic " + basic);
|
||||
}
|
||||
if (extra != null) {
|
||||
extra.forEach(builder::header);
|
||||
}
|
||||
try {
|
||||
HttpResponse<String> response = http.send(builder.build(), HttpResponse.BodyHandlers.ofString(StandardCharsets.UTF_8));
|
||||
String raw = response.body() == null ? "" : response.body();
|
||||
JsonNode tree = raw.isBlank() ? null : json.readTree(raw);
|
||||
return new Result(response.statusCode(), response.headers().firstValue("X-Request-Id").orElse(""),
|
||||
response.headers().firstValue("X-Instance-Id").orElse(""),
|
||||
response.headers().firstValue("WWW-Authenticate").orElse(""), tree, raw);
|
||||
} catch (Exception exception) {
|
||||
throw new IllegalStateException(method + " " + path, exception);
|
||||
}
|
||||
}
|
||||
|
||||
public Map<String, Object> user(String username, String displayName, String password) {
|
||||
Map<String, Object> body = new LinkedHashMap<>();
|
||||
body.put("username", username);
|
||||
body.put("display_name", displayName);
|
||||
body.put("password", password);
|
||||
return body;
|
||||
}
|
||||
|
||||
public record Result(int status, String requestId, String instanceId, String authenticate, JsonNode body, String raw) {
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
package ru.mtuci.messenger.support;
|
||||
|
||||
import org.springframework.test.context.DynamicPropertyRegistry;
|
||||
import org.testcontainers.containers.GenericContainer;
|
||||
import org.testcontainers.containers.wait.strategy.Wait;
|
||||
import org.testcontainers.postgresql.PostgreSQLContainer;
|
||||
import org.testcontainers.utility.MountableFile;
|
||||
|
||||
import java.time.Duration;
|
||||
|
||||
public final class LabContainers {
|
||||
|
||||
public static final String S3_ACCESS_KEY = "messenger-test";
|
||||
public static final String S3_SECRET_KEY = "messenger-test-secret";
|
||||
public static final String S3_BOOTSTRAP_ACCESS_KEY = "messenger-bootstrap";
|
||||
public static final String S3_BOOTSTRAP_SECRET_KEY = "messenger-bootstrap-secret";
|
||||
public static final String S3_BUCKET = "messenger-attachments";
|
||||
|
||||
public static final PostgreSQLContainer POSTGRES = new PostgreSQLContainer("postgres:16.10");
|
||||
public static final GenericContainer<?> REDIS = new GenericContainer<>("redis:7.4.6").withExposedPorts(6379);
|
||||
public static final GenericContainer<?> S3 = new GenericContainer<>("chrislusf/seaweedfs:4.52")
|
||||
.withCopyToContainer(MountableFile.forClasspathResource("s3.json"), "/etc/seaweedfs/s3.json")
|
||||
.withCommand("server", "-ip.bind=0.0.0.0", "-s3", "-s3.config=/etc/seaweedfs/s3.json",
|
||||
"-filer.exposeDirectoryData=false")
|
||||
.withExposedPorts(8333)
|
||||
.waitingFor(Wait.forListeningPort().withStartupTimeout(Duration.ofSeconds(120)));
|
||||
|
||||
static {
|
||||
POSTGRES.start();
|
||||
REDIS.start();
|
||||
S3.start();
|
||||
}
|
||||
|
||||
private LabContainers() {
|
||||
}
|
||||
|
||||
public static void register(DynamicPropertyRegistry registry) {
|
||||
registry.add("spring.datasource.url", POSTGRES::getJdbcUrl);
|
||||
registry.add("spring.datasource.username", POSTGRES::getUsername);
|
||||
registry.add("spring.datasource.password", POSTGRES::getPassword);
|
||||
registry.add("spring.data.redis.host", REDIS::getHost);
|
||||
registry.add("spring.data.redis.port", () -> REDIS.getMappedPort(6379));
|
||||
registerS3(registry);
|
||||
}
|
||||
|
||||
public static void registerS3(DynamicPropertyRegistry registry) {
|
||||
registry.add("app.s3.endpoint", () -> "http://" + S3.getHost() + ":" + S3.getMappedPort(8333));
|
||||
registry.add("app.s3.region", () -> "us-east-1");
|
||||
registry.add("app.s3.bucket", () -> S3_BUCKET);
|
||||
registry.add("app.s3.access-key", () -> S3_ACCESS_KEY);
|
||||
registry.add("app.s3.secret-key", () -> S3_SECRET_KEY);
|
||||
registry.add("app.s3.bootstrap-access-key", () -> S3_BOOTSTRAP_ACCESS_KEY);
|
||||
registry.add("app.s3.bootstrap-secret-key", () -> S3_BOOTSTRAP_SECRET_KEY);
|
||||
registry.add("app.s3.sweep-initial-delay-ms", () -> "3600000");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
{
|
||||
"identities": [
|
||||
{
|
||||
"name": "messenger-api",
|
||||
"credentials": [
|
||||
{
|
||||
"accessKey": "messenger-test",
|
||||
"secretKey": "messenger-test-secret"
|
||||
}
|
||||
],
|
||||
"actions": ["Read", "Write", "List"]
|
||||
},
|
||||
{
|
||||
"name": "messenger-bootstrap",
|
||||
"credentials": [
|
||||
{
|
||||
"accessKey": "messenger-bootstrap",
|
||||
"secretKey": "messenger-bootstrap-secret"
|
||||
}
|
||||
],
|
||||
"actions": ["Admin"]
|
||||
}
|
||||
]
|
||||
}
|
||||
+166
@@ -0,0 +1,166 @@
|
||||
name: messenger-lab4
|
||||
|
||||
x-app-image: &app-image
|
||||
image: messenger-lab4:1.0.0
|
||||
|
||||
x-datasource: &datasource
|
||||
SPRING_DATASOURCE_URL: jdbc:postgresql://postgres:5432/${POSTGRES_DB:-messenger}
|
||||
SPRING_DATASOURCE_USERNAME: ${POSTGRES_USER:-messenger}
|
||||
SPRING_DATASOURCE_PASSWORD: ${POSTGRES_PASSWORD:-messenger}
|
||||
SPRING_DATA_REDIS_HOST: redis
|
||||
SPRING_DATA_REDIS_PORT: "6379"
|
||||
S3_ENDPOINT: http://s3:8333
|
||||
S3_REGION: ${S3_REGION:-us-east-1}
|
||||
S3_BUCKET: ${S3_BUCKET:-messenger-attachments}
|
||||
S3_ACCESS_KEY: ${S3_ACCESS_KEY:?set S3_ACCESS_KEY in .env}
|
||||
S3_SECRET_KEY: ${S3_SECRET_KEY:?set S3_SECRET_KEY in .env}
|
||||
S3_BOOTSTRAP_ACCESS_KEY: ${S3_BOOTSTRAP_ACCESS_KEY:?set S3_BOOTSTRAP_ACCESS_KEY in .env}
|
||||
S3_BOOTSTRAP_SECRET_KEY: ${S3_BOOTSTRAP_SECRET_KEY:?set S3_BOOTSTRAP_SECRET_KEY in .env}
|
||||
S3_ORPHAN_GRACE: ${S3_ORPHAN_GRACE:-2m}
|
||||
MAX_UPLOAD_BYTES: ${MAX_UPLOAD_BYTES:-10485760}
|
||||
MAX_IMAGE_PIXELS: ${MAX_IMAGE_PIXELS:-40000000}
|
||||
LIQUIBASE_ANALYTICS_ENABLED: "false"
|
||||
JAVA_TOOL_OPTIONS: -XX:MaxRAMPercentage=75.0
|
||||
|
||||
services:
|
||||
postgres:
|
||||
image: postgres:16.10
|
||||
environment:
|
||||
POSTGRES_USER: ${POSTGRES_USER:-messenger}
|
||||
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-messenger}
|
||||
POSTGRES_DB: ${POSTGRES_DB:-messenger}
|
||||
volumes:
|
||||
- postgres-data:/var/lib/postgresql/data
|
||||
healthcheck:
|
||||
test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER:-messenger} -d ${POSTGRES_DB:-messenger}"]
|
||||
interval: 5s
|
||||
timeout: 3s
|
||||
retries: 20
|
||||
shm_size: 128mb
|
||||
restart: unless-stopped
|
||||
deploy:
|
||||
resources:
|
||||
limits:
|
||||
cpus: "1.0"
|
||||
memory: 512M
|
||||
|
||||
redis:
|
||||
image: redis:7.4.6
|
||||
command: ["redis-server", "--appendonly", "yes"]
|
||||
volumes:
|
||||
- redis-data:/data
|
||||
healthcheck:
|
||||
test: ["CMD", "redis-cli", "ping"]
|
||||
interval: 5s
|
||||
timeout: 3s
|
||||
retries: 20
|
||||
restart: unless-stopped
|
||||
deploy:
|
||||
resources:
|
||||
limits:
|
||||
cpus: "0.25"
|
||||
memory: 128M
|
||||
|
||||
s3:
|
||||
image: chrislusf/seaweedfs:4.52
|
||||
command:
|
||||
- server
|
||||
- -ip.bind=0.0.0.0
|
||||
- -s3
|
||||
- -s3.config=/etc/seaweedfs/s3.json
|
||||
- -filer.exposeDirectoryData=false
|
||||
environment:
|
||||
S3_ACCESS_KEY: ${S3_ACCESS_KEY:?set S3_ACCESS_KEY in .env}
|
||||
S3_SECRET_KEY: ${S3_SECRET_KEY:?set S3_SECRET_KEY in .env}
|
||||
S3_BOOTSTRAP_ACCESS_KEY: ${S3_BOOTSTRAP_ACCESS_KEY:?set S3_BOOTSTRAP_ACCESS_KEY in .env}
|
||||
S3_BOOTSTRAP_SECRET_KEY: ${S3_BOOTSTRAP_SECRET_KEY:?set S3_BOOTSTRAP_SECRET_KEY in .env}
|
||||
volumes:
|
||||
- s3-data:/data
|
||||
- ./infra/s3.json:/etc/seaweedfs/s3.json:ro
|
||||
healthcheck:
|
||||
test: ["CMD", "wget", "-q", "-O", "/dev/null", "http://127.0.0.1:9333/cluster/status"]
|
||||
interval: 5s
|
||||
timeout: 3s
|
||||
retries: 20
|
||||
start_period: 20s
|
||||
restart: unless-stopped
|
||||
deploy:
|
||||
resources:
|
||||
limits:
|
||||
cpus: "0.50"
|
||||
memory: 512M
|
||||
|
||||
migrate:
|
||||
<<: *app-image
|
||||
build:
|
||||
context: .
|
||||
dockerfile: Dockerfile
|
||||
command: ["--spring.profiles.active=migrate"]
|
||||
environment:
|
||||
<<: *datasource
|
||||
SPRING_LIQUIBASE_ENABLED: "true"
|
||||
SESSION_TTL_SECONDS: ${SESSION_TTL_SECONDS:-3600}
|
||||
IDEMPOTENCY_TTL_SECONDS: ${IDEMPOTENCY_TTL_SECONDS:-86400}
|
||||
depends_on:
|
||||
postgres:
|
||||
condition: service_healthy
|
||||
redis:
|
||||
condition: service_healthy
|
||||
healthcheck:
|
||||
disable: true
|
||||
restart: "no"
|
||||
deploy:
|
||||
resources:
|
||||
limits:
|
||||
cpus: "0.50"
|
||||
memory: 1024M
|
||||
|
||||
api:
|
||||
<<: *app-image
|
||||
environment:
|
||||
<<: *datasource
|
||||
APP_HOST: 0.0.0.0
|
||||
APP_PORT: "8080"
|
||||
APP_ENV: ${APP_ENV:-lab}
|
||||
SESSION_TTL_SECONDS: ${SESSION_TTL_SECONDS:-3600}
|
||||
IDEMPOTENCY_TTL_SECONDS: ${IDEMPOTENCY_TTL_SECONDS:-86400}
|
||||
SPRING_LIQUIBASE_ENABLED: "false"
|
||||
depends_on:
|
||||
migrate:
|
||||
condition: service_completed_successfully
|
||||
postgres:
|
||||
condition: service_healthy
|
||||
redis:
|
||||
condition: service_healthy
|
||||
s3:
|
||||
condition: service_healthy
|
||||
stop_grace_period: 12s
|
||||
init: true
|
||||
restart: unless-stopped
|
||||
deploy:
|
||||
replicas: ${API_REPLICAS:-2}
|
||||
resources:
|
||||
limits:
|
||||
cpus: "1.0"
|
||||
memory: 1024M
|
||||
|
||||
lb:
|
||||
image: haproxy:3.2.6
|
||||
volumes:
|
||||
- ./infra/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg:ro
|
||||
ports:
|
||||
- "127.0.0.1:8080:8080"
|
||||
depends_on:
|
||||
api:
|
||||
condition: service_healthy
|
||||
restart: unless-stopped
|
||||
deploy:
|
||||
resources:
|
||||
limits:
|
||||
cpus: "0.25"
|
||||
memory: 128M
|
||||
|
||||
volumes:
|
||||
postgres-data:
|
||||
redis-data:
|
||||
s3-data:
|
||||
@@ -0,0 +1,10 @@
|
||||
Limit (cost=0.29..5.12 rows=50 width=24) (actual time=0.009..0.018 rows=50 loops=1)
|
||||
Buffers: shared hit=5
|
||||
-> Index Only Scan using messages_history_idx on messages (cost=0.29..484.01 rows=5004 width=24) (actual time=0.009..0.016 rows=50 loops=1)
|
||||
Index Cond: ((chat_id = '04dcff1d-2954-4718-bbcb-34e28670812a'::uuid) AND (ROW(created_at, id) > ROW('2026-01-01 03:00:05+03'::timestamp with time zone, 'd83da0d1-8e48-405c-a04f-e77621eec78f'::uuid)))
|
||||
Heap Fetches: 50
|
||||
Buffers: shared hit=5
|
||||
Planning:
|
||||
Buffers: shared hit=33
|
||||
Planning Time: 0.157 ms
|
||||
Execution Time: 0.035 ms
|
||||
@@ -0,0 +1,8 @@
|
||||
before failover 2026-11-05T18:47:27Z
|
||||
NAME IMAGE COMMAND SERVICE CREATED STATUS PORTS
|
||||
messenger-lab4-api-1 messenger-lab4:1.0.0 "java -jar /app/mess…" api 4 minutes ago Up 21 seconds (healthy) 8080/tcp
|
||||
messenger-lab4-api-2 messenger-lab4:1.0.0 "java -jar /app/mess…" api 4 minutes ago Up 21 seconds (healthy) 8080/tcp
|
||||
messenger-lab4-lb-1 haproxy:3.2.6 "docker-entrypoint.s…" lb 4 minutes ago Up 21 seconds 127.0.0.1:8080->8080/tcp
|
||||
messenger-lab4-postgres-1 postgres:16.10 "docker-entrypoint.s…" postgres 4 minutes ago Up 22 seconds (healthy) 5432/tcp
|
||||
messenger-lab4-redis-1 redis:7.4.6 "docker-entrypoint.s…" redis 4 minutes ago Up 22 seconds (healthy) 6379/tcp
|
||||
messenger-lab4-s3-1 chrislusf/seaweedfs:4.52 "/entrypoint.sh serv…" s3 2 minutes ago Up 21 seconds (healthy) 7333/tcp, 8080/tcp, 8333/tcp, 8888/tcp, 9333/tcp, 18080/tcp, 18888/tcp, 19333/tcp
|
||||
@@ -0,0 +1,16 @@
|
||||
before 2026-11-05T18:45:57Z
|
||||
NAME IMAGE COMMAND SERVICE CREATED STATUS PORTS
|
||||
messenger-lab4-api-1 messenger-lab4:1.0.0 "java -jar /app/mess…" api 3 minutes ago Up 31 seconds (healthy) 8080/tcp
|
||||
messenger-lab4-api-2 messenger-lab4:1.0.0 "java -jar /app/mess…" api 3 minutes ago Up 31 seconds (healthy) 8080/tcp
|
||||
messenger-lab4-lb-1 haproxy:3.2.6 "docker-entrypoint.s…" lb 3 minutes ago Up 20 seconds 127.0.0.1:8080->8080/tcp
|
||||
messenger-lab4-postgres-1 postgres:16.10 "docker-entrypoint.s…" postgres 3 minutes ago Up 3 minutes (healthy) 5432/tcp
|
||||
messenger-lab4-redis-1 redis:7.4.6 "docker-entrypoint.s…" redis 3 minutes ago Up 3 minutes (healthy) 6379/tcp
|
||||
messenger-lab4-s3-1 chrislusf/seaweedfs:4.52 "/entrypoint.sh serv…" s3 59 seconds ago Up 57 seconds (healthy) 7333/tcp, 8080/tcp, 8333/tcp, 8888/tcp, 9333/tcp, 18080/tcp, 18888/tcp, 19333/tcp
|
||||
--- instances ---
|
||||
4 3d288957ae72
|
||||
4 cd0fc7f96b95
|
||||
--- ports ---
|
||||
/messenger-lab4-api-1 ports={"8080/tcp":null}
|
||||
/messenger-lab4-api-2 ports={"8080/tcp":null}
|
||||
/messenger-lab4-lb-1 ports={"8080/tcp":[{"HostIp":"127.0.0.1","HostPort":"8080"}]}
|
||||
/messenger-lab4-s3-1 ports={"18080/tcp":null,"18888/tcp":null,"19333/tcp":null,"7333/tcp":null,"8080/tcp":null,"8333/tcp":null,"8888/tcp":null,"9333/tcp":null}
|
||||
@@ -0,0 +1,8 @@
|
||||
10 одновременных POST image/png по 8 МиБ. Файл структурно валидный, 1×1 пиксель, IDAT не распаковывается.
|
||||
Ответы: 201 201 201 201 201 201 201 201 201 201.
|
||||
Длительность запросов, с: 3.779, 3.906, 1.739, 3.905, 3.907, 3.857, 3.856, 3.856, 3.855, 3.854.
|
||||
docker stats, лимит контейнера 1024 MiB.
|
||||
До нагрузки обе реплики около 389 MiB.
|
||||
Пик messenger-lab4-api-1: 581.3 MiB.
|
||||
Пик messenger-lab4-api-2: 558.2 MiB.
|
||||
Лимит чтения одного запроса 10 МиБ, поэтому десять параллельных тел держат не больше примерно 100 МиБ сверх самой JVM.
|
||||
@@ -0,0 +1,17 @@
|
||||
Стенд: arm64, 12 CPU, 24 GiB RAM, Docker 28.5.1. Прогон 5 ноября 2026.
|
||||
Публичные порты: только lb 127.0.0.1:8080. api и s3 наружу не опубликованы (ports host = null).
|
||||
|
||||
make test
|
||||
PASS: lab4 public API scenario
|
||||
|
||||
make test-persistence ACTION_SCRIPT=scripts/restart.sh
|
||||
PASS: persistence preserved confirmed data and existing session.
|
||||
|
||||
make test-failover ACTION_SCRIPT=scripts/stop-one.sh
|
||||
{"observed_instances_before": 2, "successful_writes": 119, "failed_attempts": 1, "first_recovery_seconds": 1.665}
|
||||
PASS: failover preserved confirmed data and existing session. Hook scope needs operator evidence.
|
||||
После hook в ps не было api-2 (остановлена). docker compose start api поднял её обратно, статус healthy.
|
||||
|
||||
Анонимный GET http://s3:8333/messenger-attachments/originals/does-not-matter из контейнера в сети стенда, без ключей: HTTP 403.
|
||||
|
||||
mvn -q test в backend/: 16 тестов, 0 ошибок, 0 падений.
|
||||
@@ -0,0 +1,39 @@
|
||||
global
|
||||
maxconn 200
|
||||
log stdout format raw local0
|
||||
|
||||
defaults
|
||||
mode http
|
||||
log global
|
||||
option httplog
|
||||
timeout connect 1s
|
||||
timeout client 10s
|
||||
timeout server 5s
|
||||
timeout http-request 10s
|
||||
timeout check 1s
|
||||
retries 0
|
||||
|
||||
resolvers docker
|
||||
nameserver dns 127.0.0.11:53
|
||||
resolve_retries 3
|
||||
timeout resolve 1s
|
||||
timeout retry 1s
|
||||
hold valid 1s
|
||||
hold other 1s
|
||||
hold refused 1s
|
||||
hold nx 1s
|
||||
hold timeout 1s
|
||||
hold obsolete 1s
|
||||
accepted_payload_size 8192
|
||||
|
||||
frontend http
|
||||
bind *:8080
|
||||
default_backend api
|
||||
|
||||
backend api
|
||||
balance roundrobin
|
||||
option httpchk
|
||||
http-check send meth GET uri /health/ready ver HTTP/1.1 hdr host api
|
||||
http-check expect status 200
|
||||
default-server inter 1s fall 2 rise 1 on-marked-down shutdown-sessions
|
||||
server-template api 8 api:8080 check resolvers docker init-addr none resolve-prefer ipv4
|
||||
@@ -0,0 +1,24 @@
|
||||
{
|
||||
"identities": [
|
||||
{
|
||||
"name": "messenger-api",
|
||||
"credentials": [
|
||||
{
|
||||
"accessKey": "${S3_ACCESS_KEY}",
|
||||
"secretKey": "${S3_SECRET_KEY}"
|
||||
}
|
||||
],
|
||||
"actions": ["Read", "Write", "List"]
|
||||
},
|
||||
{
|
||||
"name": "messenger-bootstrap",
|
||||
"credentials": [
|
||||
{
|
||||
"accessKey": "${S3_BOOTSTRAP_ACCESS_KEY}",
|
||||
"secretKey": "${S3_BOOTSTRAP_SECRET_KEY}"
|
||||
}
|
||||
],
|
||||
"actions": ["Admin"]
|
||||
}
|
||||
]
|
||||
}
|
||||
Executable
+22
@@ -0,0 +1,22 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
cd "$(dirname "$0")/.."
|
||||
if [ -f .env ]; then
|
||||
docker compose --env-file .env restart lb api postgres redis s3
|
||||
else
|
||||
docker compose restart lb api postgres redis s3
|
||||
fi
|
||||
|
||||
i=0
|
||||
while [ "$i" -lt 60 ]; do
|
||||
if curl -fsS -m 2 http://127.0.0.1:8080/health/ready >/dev/null 2>&1; then
|
||||
# SeaweedFS answers master status before object reads are stable.
|
||||
sleep 5
|
||||
curl -fsS -m 2 http://127.0.0.1:8080/health/ready >/dev/null 2>&1
|
||||
exit 0
|
||||
fi
|
||||
i=$((i + 1))
|
||||
sleep 1
|
||||
done
|
||||
echo "stack did not become ready" >&2
|
||||
exit 1
|
||||
Executable
+24
@@ -0,0 +1,24 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
cd "$(dirname "$0")/.."
|
||||
: "${TARGET_INSTANCE_ID:?test provides target API instance alias}"
|
||||
|
||||
cid=""
|
||||
for id in $(docker ps -q --filter "label=com.docker.compose.project=messenger-lab4" --filter "label=com.docker.compose.service=api"); do
|
||||
full=$(docker inspect -f '{{.Id}}' "$id")
|
||||
host=$(docker inspect -f '{{.Config.Hostname}}' "$id")
|
||||
name=$(docker inspect -f '{{.Name}}' "$id")
|
||||
name=${name#/}
|
||||
short=$(printf '%s' "$full" | cut -c1-12)
|
||||
if [ "$host" = "$TARGET_INSTANCE_ID" ] || [ "$short" = "$TARGET_INSTANCE_ID" ] || [ "$name" = "$TARGET_INSTANCE_ID" ] || [ "$full" = "$TARGET_INSTANCE_ID" ]; then
|
||||
cid=$id
|
||||
break
|
||||
fi
|
||||
done
|
||||
|
||||
if [ -z "$cid" ]; then
|
||||
echo "no running api replica matches TARGET_INSTANCE_ID" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
docker stop -t 6 "$cid" >/dev/null
|
||||
Reference in New Issue
Block a user