diff --git a/build.gradle.kts b/build.gradle.kts index 0a5b5720..52fda1bc 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -25,6 +25,7 @@ dependencies { implementation(ktorLibs.server.statusPages) implementation(ktorLibs.server.callLogging) implementation(ktorLibs.server.defaultHeaders) + implementation(ktorLibs.server.cors) implementation(ktorLibs.server.swagger) implementation(ktorLibs.serialization.kotlinx.json) @@ -67,7 +68,7 @@ ksp { } application { - mainClass = "io.ktor.server.netty.EngineMain" + mainClass = "com.mtusi.messenger.main.AppKt" applicationDefaultJvmArgs = listOf("-Dio.ktor.development=false") } diff --git a/compose.yaml b/compose.yaml index 42c56457..163a6261 100644 --- a/compose.yaml +++ b/compose.yaml @@ -4,7 +4,6 @@ services: postgres: image: postgres:17.6-alpine3.22 - container_name: mtusi-lab3-postgres # pg_stat_statements counts executed queries, track 1 uses it to measure the cache stampede. command: ["postgres", "-c", "shared_preload_libraries=pg_stat_statements", "-c", "pg_stat_statements.track=all"] environment: @@ -25,7 +24,6 @@ services: redis: image: redis:8.4.0-alpine - container_name: mtusi-lab3-redis # Session cache only, PostgreSQL is the source of truth: no RDB/AOF, a restart simply empties the cache. # Every key carries a TTL, so under memory pressure volatile-lru evicts the least used sessions. command: @@ -48,11 +46,36 @@ services: start_period: 5s restart: unless-stopped + # One-shot: applies Flyway migrations once and exits. Replicas start only after it succeeded, + # so they never race each other on the schema. + migrate: + build: . + image: mtusi-messenger:lab3 + command: ["migrate"] + healthcheck: + disable: true + depends_on: + postgres: + condition: service_healthy + environment: + DB_HOST: postgres + DB_PORT: "5432" + DB_NAME: messenger + DB_USER: messenger + DB_PASSWORD_FILE: /run/secrets/db_password + secrets: + - db_password + restart: "no" + api: build: . image: mtusi-messenger:lab3 - container_name: mtusi-lab3-api + # Interchangeable replicas: no container_name and no published port, traffic only through `lb`. + deploy: + replicas: ${API_REPLICAS:-2} depends_on: + migrate: + condition: service_completed_successfully postgres: condition: service_healthy redis: @@ -65,14 +88,27 @@ services: DB_NAME: messenger DB_USER: messenger DB_PASSWORD_FILE: /run/secrets/db_password + DB_MIGRATE_ON_START: "false" REDIS_HOST: redis REDIS_PORT: "6379" REDIS_PASSWORD_FILE: /run/secrets/redis_password SESSION_TTL_SECONDS: ${SESSION_TTL_SECONDS:-3600} SESSION_CACHE_SINGLE_FLIGHT: ${SESSION_CACHE_SINGLE_FLIGHT:-true} + IDEMPOTENCY_TTL_SECONDS: ${IDEMPOTENCY_TTL_SECONDS:-86400} secrets: - db_password - redis_password + # SIGTERM: Ktor stops accepting, finishes requests in flight (shutdownGracePeriod 3 s, shutdownTimeout 10 s). + stop_grace_period: 15s + restart: unless-stopped + + lb: + image: haproxy:3.2.4-alpine + volumes: + - ./infra/haproxy/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg:ro + depends_on: + api: + condition: service_healthy ports: - "127.0.0.1:8080:8080" restart: unless-stopped diff --git a/evidence/failover-api-1.txt b/evidence/failover-api-1.txt new file mode 100644 index 00000000..3ef92aef --- /dev/null +++ b/evidence/failover-api-1.txt @@ -0,0 +1,24 @@ +# before +NAME CONTAINER ID STATE STATUS +mtusi-messenger-lab3-api-1 236cb759e825 running Up About a minute (healthy) +mtusi-messenger-lab3-api-2 787a253aa6a6 running Up 8 seconds (healthy) + +$ make test-failover ACTION_SCRIPT=scripts/stop-one.sh +Executing explicit failover hook; hook output suppressed to avoid leaking secrets. +{"observed_instances_before": 2, "successful_writes": 119, "failed_attempts": 1, "first_recovery_seconds": 6.508} +PASS: failover preserved confirmed data and existing session. Hook scope needs operator evidence. + +# after: the stopped container is the replica whose id the test passed as TARGET_INSTANCE_ID +NAME CONTAINER ID STATE STATUS +mtusi-messenger-lab3-api-1 236cb759e825 exited Exited (143) 8 seconds ago +mtusi-messenger-lab3-api-2 787a253aa6a6 running Up 24 seconds (healthy) + +# HAProxy log around the failure (non-2xx and state changes) +[WARNING] (8) : Server api/api1 was DOWN and now enters maintenance (No IP for server ). +Server api/api1 ('api') is UP/READY (resolves again). +[WARNING] (8) : Server api/api1 ('api') is UP/READY (resolves again). +172.24.0.1:55062 [22/Oct/2026:17:44:22.431] http api/api2 0/0/0/-1/93 502 209 - - SH-- 1/1/0/0/0 0/0 "POST /api/v1/chats/43312e52-5c0b-41af-9981-2379d372b382/messages HTTP/1.1" +[WARNING] (8) : Server api/api2 is DOWN, reason: Layer4 connection problem, info: "Connection refused", check duration: 0ms. 1 active and 0 backup servers left. 0 sessions active, 0 requeued, 0 remaining in queue. +Server api/api2 is DOWN, reason: Layer4 connection problem, info: "Connection refused", check duration: 0ms. 1 active and 0 backup servers left. 0 sessions active, 0 requeued, 0 remaining in queue. +Server api/api2 was DOWN and now enters maintenance (No IP for server ). +[WARNING] (8) : Server api/api2 was DOWN and now enters maintenance (No IP for server ). diff --git a/evidence/failover-api-2.txt b/evidence/failover-api-2.txt new file mode 100644 index 00000000..232bad25 --- /dev/null +++ b/evidence/failover-api-2.txt @@ -0,0 +1,24 @@ +# before +NAME CONTAINER ID STATE STATUS +mtusi-messenger-lab3-api-1 236cb759e825 running Up 8 seconds (healthy) +mtusi-messenger-lab3-api-2 787a253aa6a6 running Up About a minute (healthy) + +$ make test-failover ACTION_SCRIPT=scripts/stop-one.sh +Executing explicit failover hook; hook output suppressed to avoid leaking secrets. +{"observed_instances_before": 2, "successful_writes": 120, "failed_attempts": 1, "first_recovery_seconds": 6.537} +PASS: failover preserved confirmed data and existing session. Hook scope needs operator evidence. + +# after: the stopped container is the replica whose id the test passed as TARGET_INSTANCE_ID +NAME CONTAINER ID STATE STATUS +mtusi-messenger-lab3-api-1 236cb759e825 running Up 24 seconds (healthy) +mtusi-messenger-lab3-api-2 787a253aa6a6 exited Exited (143) 8 seconds ago + +# HAProxy log around the failure (non-2xx and state changes) +[WARNING] (8) : Server api/api2 was DOWN and now enters maintenance (No IP for server ). +Server api/api2 ('api') is UP/READY (resolves again). +[WARNING] (8) : Server api/api2 ('api') is UP/READY (resolves again). +172.24.0.1:62054 [22/Oct/2026:17:45:16.133] http api/api1 0/0/0/-1/87 502 209 - - SH-- 1/1/0/0/0 0/0 "POST /api/v1/chats/43529cee-e036-4e88-a744-c3fd0e23ce65/messages HTTP/1.1" +Server api/api1 is DOWN, reason: Layer4 connection problem, info: "Connection refused", check duration: 0ms. 1 active and 0 backup servers left. 0 sessions active, 0 requeued, 0 remaining in queue. +[WARNING] (8) : Server api/api1 is DOWN, reason: Layer4 connection problem, info: "Connection refused", check duration: 0ms. 1 active and 0 backup servers left. 0 sessions active, 0 requeued, 0 remaining in queue. +[WARNING] (8) : Server api/api1 was DOWN and now enters maintenance (No IP for server ). +Server api/api1 was DOWN and now enters maintenance (No IP for server ). diff --git a/evidence/gradle-tests.txt b/evidence/gradle-tests.txt new file mode 100644 index 00000000..7411d123 --- /dev/null +++ b/evidence/gradle-tests.txt @@ -0,0 +1,84 @@ +# ./gradlew test (Testcontainers: postgres:17.6-alpine3.22, redis:8.4.0-alpine) +total: 66 tests, 0 failed + +infrastructure.controllers.authentication.AuthenticationControllerTest: 9 tests, 0 failed, 7.774 s + - logout revokes only the current token + - Bearer token cannot open a session + - password containing a colon and non-ASCII characters works + - unknown token returns 401 + - session expires after SESSION_TTL_SECONDS + - each login issues a different token + - valid Basic credentials open a session + - missing or malformed Basic header returns 401 + - wrong password and unknown user get the same 401 +infrastructure.controllers.chat.ChatControllerTest: 6 tests, 0 failed, 2.101 s + - member can read own chat and members keep their order + - unknown member is rejected before anything is written + - foreign chat returns 404 for non-member + - cursor issued to another user is rejected + - chat list contains only own chats and is paginated + - invalid member lists are rejected with 400 +infrastructure.controllers.health.HealthControllerTest: 2 tests, 0 failed, 0.134 s + - live returns 200 + - ready returns 200 when database is reachable +infrastructure.controllers.message.MessageConcurrencyTest: 1 tests, 0 failed, 0.335 s + - parallel senders produce exactly N unique messages +infrastructure.controllers.message.MessageControllerTest: 7 tests, 0 failed, 1.946 s + - foreign chat returns 404 when sending + - last page has null next_cursor + - pagination walks all messages exactly once + - foreign chat returns 404 when listing + - empty text returns 400 invalid_request + - history that is an exact multiple of limit ends with null next_cursor + - foreign cursor returns 400 +infrastructure.controllers.message.MessageIdempotencyTest: 7 tests, 0 failed, 3.266 s + - expired key can be used for a new message + - key scope is user and chat + - same key with another body is a conflict + - missing or malformed key is rejected before anything is written + - retry with the same key and body returns the original message + - retry by a former outsider is still checked for access + - concurrent retries create exactly one message +infrastructure.controllers.user.UserControllerTest: 11 tests, 0 failed, 1.638 s + - password shorter than 12 code points returns 400 + - concurrent registrations of one username create exactly one user + - X-User-Id alone gives no access + - create user returns 201 without password + - duplicate username returns 409 conflict + - missing token returns 401 with Bearer challenge + - invalid JSON body returns 400 invalid_request + - missing password returns 400 + - Basic credentials are rejected on business endpoints + - me returns the session owner + - X-User-Id does not override bearer identity +infrastructure.repositories.authentication.session.cache.RedisSessionCacheRepositoryTest: 3 tests, 0 failed, 0.102 s + - cached session round-trips and expires together with the session + - revoke replaces a cached session with a tombstone + - late cache fill cannot overwrite a tombstone +infrastructure.repositories.chat.ExposedChatRepositoryTest: 2 tests, 0 failed, 0.514 s + - chat row is rolled back when inserting members fails + - successful creation commits the chat and every member +infrastructure.services.authentication.SessionLookupSingleFlightTest: 4 tests, 0 failed, 1.120 s + - request arriving after logout does not join a lookup started before it + - without single-flight every concurrent miss queries the database + - with single-flight concurrent misses share one database lookup + - lookup that finished before a request arrived is not reused +infrastructure.services.hashing.DefaultHashingServiceTest: 4 tests, 0 failed, 0.421 s + - passwords differing only after 72 bytes are distinguished + - validates the right password and rejects a wrong one + - malformed hash is rejected instead of throwing + - hash is a salted Argon2id PHC string +plugins.DatabaseMigrationTest: 1 tests, 0 failed, 0.209 s + - re-running migrations applies nothing and keeps data +plugins.DependencyFailureTest: 5 tests, 0 failed, 13.326 s + - without PostgreSQL requests fail with a bounded 503 + - logout during a Redis outage survives an API restart + - logout during a Redis outage is not undone by a stale cached copy + - without Redis requests bypass the cache + - hanging Redis costs one command timeout and recovers +plugins.InstanceIdTest: 1 tests, 0 failed, 0.231 s + - every response names the replica +plugins.SessionCacheTest: 3 tests, 0 failed, 0.928 s + - emptied cache falls back to PostgreSQL + - first authenticated request fills the cache + - logout leaves a tombstone and the token is rejected diff --git a/evidence/idempotency-race.txt b/evidence/idempotency-race.txt new file mode 100644 index 00000000..2720d6fa --- /dev/null +++ b/evidence/idempotency-race.txt @@ -0,0 +1,19 @@ +$ python3 scripts/idempotency-race.py +{ + "rounds": 20, + "parallel_requests_per_round": 32, + "statuses": { + "201": 640 + }, + "requests_per_instance": { + "236cb759e825": 320, + "787a253aa6a6": 320 + }, + "rounds_with_more_than_one_message_id": 0, + "messages_in_history": 20, + "distinct_texts_in_history": 20 +} +PASS + +$ psql: keys without a message (must be 0) +0 diff --git a/evidence/instances.txt b/evidence/instances.txt new file mode 100644 index 00000000..440fc6ba --- /dev/null +++ b/evidence/instances.txt @@ -0,0 +1,21 @@ +$ docker compose ps -a +NAME IMAGE STATE STATUS PORTS +mtusi-messenger-lab3-api-1 mtusi-messenger:lab3 running Up 35 seconds (healthy) 8080/tcp +mtusi-messenger-lab3-api-2 mtusi-messenger:lab3 running Up 35 seconds (healthy) 8080/tcp +mtusi-messenger-lab3-lb-1 haproxy:3.2.4-alpine running Up 29 seconds 127.0.0.1:8080->8080/tcp +mtusi-messenger-lab3-migrate-1 mtusi-messenger:lab3 exited Exited (0) 36 seconds ago +mtusi-messenger-lab3-postgres-1 postgres:17.6-alpine3.22 running Up 42 seconds (healthy) 5432/tcp +mtusi-messenger-lab3-redis-1 redis:8.4.0-alpine running Up 42 seconds (healthy) 6379/tcp + +$ for i in 1..6: curl -sD - http://localhost:8080/health/ready | grep X-Instance-Id +x-instance-id: 236cb759e825 +x-instance-id: 787a253aa6a6 +x-instance-id: 236cb759e825 +x-instance-id: 787a253aa6a6 +x-instance-id: 236cb759e825 +x-instance-id: 787a253aa6a6 + +# X-Instance-Id = HOSTNAME = short container id of the replica +NAME CONTAINER ID STATE STATUS +mtusi-messenger-lab3-api-1 236cb759e825 running Up 35 seconds (healthy) +mtusi-messenger-lab3-api-2 787a253aa6a6 running Up 35 seconds (healthy) diff --git a/evidence/persistence.txt b/evidence/persistence.txt new file mode 100644 index 00000000..d33c8104 --- /dev/null +++ b/evidence/persistence.txt @@ -0,0 +1,24 @@ +# before +NAME STATE STATUS +mtusi-messenger-lab3-api-1 running Up 35 seconds (healthy) +mtusi-messenger-lab3-api-2 running Up 8 seconds (healthy) +mtusi-messenger-lab3-lb-1 running Up 2 minutes +mtusi-messenger-lab3-migrate-1 exited Exited (0) 7 seconds ago +mtusi-messenger-lab3-postgres-1 running Up 2 minutes (healthy) +mtusi-messenger-lab3-redis-1 running Up 2 minutes (healthy) +/mtusi-messenger-lab3-api-1 StartedAt=2026-10-22T17:45:06.396826175Z +/mtusi-messenger-lab3-api-2 StartedAt=2026-10-22T17:45:33.184696798Z +/mtusi-messenger-lab3-lb-1 StartedAt=2026-10-22T17:43:17.31840918Z +/mtusi-messenger-lab3-postgres-1 StartedAt=2026-10-22T17:43:04.928814758Z +/mtusi-messenger-lab3-redis-1 StartedAt=2026-10-22T17:43:04.927049216Z + +$ make test-persistence ACTION_SCRIPT=scripts/restart.sh +PASS: persistence preserved confirmed data and existing session. Hook scope needs operator evidence. + +# after: every container has a new StartedAt, volume postgres-data is kept +/mtusi-messenger-lab3-api-1 StartedAt=2026-10-22T17:45:53.47890296Z +/mtusi-messenger-lab3-api-2 StartedAt=2026-10-22T17:45:53.585460419Z +/mtusi-messenger-lab3-lb-1 StartedAt=2026-10-22T17:45:59.18815863Z +/mtusi-messenger-lab3-postgres-1 StartedAt=2026-10-22T17:45:46.762570055Z +/mtusi-messenger-lab3-redis-1 StartedAt=2026-10-22T17:45:46.760974013Z +mtusi-messenger-lab3_postgres-data diff --git a/evidence/smoke.txt b/evidence/smoke.txt new file mode 100644 index 00000000..b049e9a0 --- /dev/null +++ b/evidence/smoke.txt @@ -0,0 +1,2 @@ +$ make test +PASS: lab3 public API scenario; infrastructure and grading evidence remain separate. diff --git a/infra/haproxy/haproxy.cfg b/infra/haproxy/haproxy.cfg new file mode 100644 index 00000000..b3abf039 --- /dev/null +++ b/infra/haproxy/haproxy.cfg @@ -0,0 +1,47 @@ +# Single public entry point in front of the API replicas. +# Backends are discovered through Docker DNS (service name `api`) and receive traffic only while /health/ready is 200. + +global + log stdout format raw local0 info + +defaults + mode http + log global + option httplog + option dontlognull + option forwardfor + timeout connect 1s + timeout client 30s + timeout server 10s + timeout http-request 10s + timeout http-keep-alive 5s + # Retried only when the connection to a replica fails, i.e. before the request was sent: + # a POST that reached a replica is never repeated by the balancer. + retries 2 + option redispatch 1 + +resolvers docker + nameserver dns 127.0.0.11:53 + resolve_retries 3 + timeout resolve 1s + timeout retry 1s + hold valid 2s + hold other 2s + hold refused 2s + hold nx 2s + hold timeout 2s + hold obsolete 2s + accepted_payload_size 8192 + +frontend http + bind :8080 + default_backend api + +backend api + balance roundrobin + option httpchk GET /health/ready + http-check expect status 200 + # A replica leaves rotation after 2 failed checks 1 s apart and returns after 2 successful ones. + default-server check inter 1s fall 2 rise 2 resolvers docker init-addr none + # Up to 4 replicas; slots without a resolved address stay in maintenance. + server-template api 4 api:8080 diff --git a/run/_template/config/cors.conf b/run/_template/config/cors.conf new file mode 100644 index 00000000..4551a034 --- /dev/null +++ b/run/_template/config/cors.conf @@ -0,0 +1,23 @@ +cors { + # Defaults to false + #allowAnyHost = true + # Swagger UI sends "Try it out" to the contract server http://localhost:8080; pages opened elsewhere need these. + allowedHosts = [ + { host = "localhost:8080", schemes = ["http"] } + { host = "localhost:8081", schemes = ["http"] } + { host = "127.0.0.1:8080", schemes = ["http"] } + { host = "127.0.0.1:8081", schemes = ["http"] } + ] + + # Defaults to true + #allowAnyMethod = true + #allowedMethods = [GET, POST, PATCH, PUT, DELETE, OPTIONS] + + allowAnyHeader = true + #allowedHeaders = [] + + exposedHeaders = [X-Request-Id, X-Instance-Id, WWW-Authenticate] + + # Browsers send Origin on same-origin POST too, e.g. Swagger UI served by the API itself. + allowSameOrigin = true +} diff --git a/scripts/idempotency-race.py b/scripts/idempotency-race.py new file mode 100755 index 00000000..cdcc6af6 --- /dev/null +++ b/scripts/idempotency-race.py @@ -0,0 +1,84 @@ +#!/usr/bin/env python3 +"""Sends one message with the SAME Idempotency-Key many times in parallel through the balancer. + +Expects every response to be 201 with the same message, at least two replicas answering (X-Instance-Id) +and exactly one message in the chat history. Synthetic users only; the token stays in memory. +""" +import argparse +import base64 +import json +import threading +import urllib.error +import urllib.request +import uuid +from collections import Counter + +parser = argparse.ArgumentParser() +parser.add_argument('--base-url', default='http://localhost:8080') +parser.add_argument('--parallel', type=int, default=32) +parser.add_argument('--rounds', type=int, default=20) +args = parser.parse_args() + + +def call(method, path, body=None, headers=None): + data = json.dumps(body).encode() if body is not None else None + request = urllib.request.Request(args.base_url + path, data=data, method=method, headers={ + 'Content-Type': 'application/json', **(headers or {})}) + try: + with urllib.request.urlopen(request, timeout=10) as response: + return response.status, response.headers, json.loads(response.read() or b'null') + except urllib.error.HTTPError as error: + return error.code, error.headers, json.loads(error.read() or b'null') + + +username = 'race_' + uuid.uuid4().hex[:12] +password = 'race-password-' + uuid.uuid4().hex +status, _, user = call('POST', '/api/v1/users', {'username': username, 'display_name': 'race', 'password': password}) +assert status == 201, status +basic = base64.b64encode(f'{username}:{password}'.encode()).decode() +status, _, session = call('POST', '/api/v1/auth/sessions', headers={'Authorization': 'Basic ' + basic}) +assert status == 201, status +auth = {'Authorization': 'Bearer ' + session['token']} +status, _, chat = call('POST', '/api/v1/chats', {'title': 'race', 'member_ids': [user['id']]}, auth) +assert status == 201, status +path = f"/api/v1/chats/{chat['id']}/messages" + +statuses, instances, violations = Counter(), Counter(), 0 +for round_number in range(args.rounds): + key = uuid.uuid4().hex + results = [] + barrier = threading.Barrier(args.parallel) + + def send(): + barrier.wait() + results.append(call('POST', path, {'text': f'race {round_number}'}, {**auth, 'Idempotency-Key': key})) + + threads = [threading.Thread(target=send) for _ in range(args.parallel)] + for thread in threads: + thread.start() + for thread in threads: + thread.join() + + ids = {body['id'] for status, _, body in results if status == 201} + for status, headers, _ in results: + statuses[status] += 1 + instances[headers.get('X-Instance-Id', '-')] += 1 + if len(ids) != 1: + violations += 1 + +status, _, page = call('GET', path + '?limit=100', headers=auth) +texts = [m['text'] for m in page['items']] + +print(json.dumps({ + 'rounds': args.rounds, + 'parallel_requests_per_round': args.parallel, + 'statuses': dict(statuses), + 'requests_per_instance': dict(instances), + 'rounds_with_more_than_one_message_id': violations, + 'messages_in_history': len(texts), + 'distinct_texts_in_history': len(set(texts)), +}, indent=2)) + +ok = violations == 0 and len(texts) == args.rounds == len(set(texts)) and len(instances) >= 2 and set(statuses) == {201} +print('PASS' if ok else 'FAIL') +raise SystemExit(0 if ok else 1) diff --git a/scripts/restart.sh b/scripts/restart.sh index 76c2bb0c..97789e1d 100755 --- a/scripts/restart.sh +++ b/scripts/restart.sh @@ -1,7 +1,7 @@ #!/bin/sh -# Restarts the API, PostgreSQL and Redis, keeping the named volume (no `down -v`, no re-seed). +# Restarts the balancer, every API replica, PostgreSQL and Redis, keeping the named volume (no `down -v`, no re-seed). set -eu cd "$(dirname "$0")/.." -docker compose stop api redis postgres -docker compose up -d --wait postgres redis api +docker compose stop lb api redis postgres +docker compose up -d --wait diff --git a/scripts/stop-one.sh b/scripts/stop-one.sh new file mode 100755 index 00000000..b848e733 --- /dev/null +++ b/scripts/stop-one.sh @@ -0,0 +1,22 @@ +#!/bin/sh +# Stops the ONE API replica named by TARGET_INSTANCE_ID and leaves it stopped (no DB/LB/volume changes). +# The replica's X-Instance-Id is its container hostname, i.e. the short container id. +# Restore afterwards with: docker compose start api +set -eu +cd "$(dirname "$0")/.." + +: "${TARGET_INSTANCE_ID:?test provides target API instance alias}" + +case "$TARGET_INSTANCE_ID" in + *[!0-9a-f]* | "") echo "unexpected instance id" >&2; exit 2 ;; +esac + +# Only containers of the api service are candidates, so a wrong id can never stop the database or the balancer. +container=$(docker compose ps -q api | grep "^$TARGET_INSTANCE_ID" || true) + +if [ -z "$container" ] || [ "$(printf '%s\n' "$container" | wc -l)" -ne 1 ]; then + echo "no single api replica matches $TARGET_INSTANCE_ID" >&2 + exit 1 +fi + +docker stop --time 8 "$container" >/dev/null diff --git a/src/main/kotlin/com/mtusi/messenger/main/App.kt b/src/main/kotlin/com/mtusi/messenger/main/App.kt index 8ec647e2..4115fd77 100644 --- a/src/main/kotlin/com/mtusi/messenger/main/App.kt +++ b/src/main/kotlin/com/mtusi/messenger/main/App.kt @@ -3,17 +3,26 @@ package com.mtusi.messenger.main import com.mtusi.messenger.main.plugins.authentication.configureAuthentication import com.mtusi.messenger.main.plugins.configureCallLogging import com.mtusi.messenger.main.plugins.configureContentNegotiation +import com.mtusi.messenger.main.plugins.configureCors import com.mtusi.messenger.main.plugins.configureDatabase import com.mtusi.messenger.main.plugins.configureDefaultHeaders +import com.mtusi.messenger.main.plugins.configureInstanceId import com.mtusi.messenger.main.plugins.configureKoin import com.mtusi.messenger.main.plugins.configureRedis import com.mtusi.messenger.main.plugins.configureRequestId import com.mtusi.messenger.main.plugins.configureRouting import com.mtusi.messenger.main.plugins.configureStatusPages +import com.mtusi.messenger.main.plugins.migrateDatabase import io.ktor.server.application.Application import io.ktor.server.netty.EngineMain -fun main(args: Array): Unit = EngineMain.main(args) +fun main(args: Array) { + // `migrate` applies database migrations and exits; the API replicas start with migrations already in place. + if (args.firstOrNull() == "migrate") { + return migrateDatabase() + } + EngineMain.main(args) +} fun Application.module() { @@ -23,6 +32,8 @@ fun Application.module() { configureContentNegotiation() configureDefaultHeaders() + configureInstanceId() + configureCors() configureRequestId() configureCallLogging() configureStatusPages() diff --git a/src/main/kotlin/com/mtusi/messenger/main/compat/serializers/HttpMethodSerializer.kt b/src/main/kotlin/com/mtusi/messenger/main/compat/serializers/HttpMethodSerializer.kt new file mode 100644 index 00000000..59bc7bd8 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/compat/serializers/HttpMethodSerializer.kt @@ -0,0 +1,31 @@ +package com.mtusi.messenger.main.compat.serializers + +import io.ktor.http.HttpMethod +import kotlinx.serialization.KSerializer +import kotlinx.serialization.Serializable +import kotlinx.serialization.descriptors.PrimitiveKind +import kotlinx.serialization.descriptors.PrimitiveSerialDescriptor +import kotlinx.serialization.encoding.Decoder +import kotlinx.serialization.encoding.Encoder + +typealias SerialHttpMethod = @Serializable(HttpMethodSerializer::class) HttpMethod + +object HttpMethodSerializer : KSerializer { + + override val descriptor = PrimitiveSerialDescriptor( + serialName = "HttpMethod", + kind = PrimitiveKind.STRING + ) + + override fun serialize( + encoder: Encoder, + value: HttpMethod + ) { + encoder.encodeString(value.value) + } + + override fun deserialize(decoder: Decoder): HttpMethod { + return HttpMethod.parse(decoder.decodeString()) + } + +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/config/cors/CorsConfig.kt b/src/main/kotlin/com/mtusi/messenger/main/config/cors/CorsConfig.kt new file mode 100644 index 00000000..a6aa0185 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/config/cors/CorsConfig.kt @@ -0,0 +1,100 @@ +package com.mtusi.messenger.main.config.cors + +import com.mtusi.messenger.main.compat.serializers.SerialHttpMethod +import io.ktor.http.HttpMethod +import kotlinx.serialization.Serializable +import io.ktor.server.plugins.cors.CORSConfig as CorsPluginConfig + +interface CorsConfig { + + /** + * Whether any host should be allowed + * + * Must default to false + */ + val allowAnyHost: Boolean + val allowedHosts: List + + /** + * Whether any [HttpMethod][io.ktor.http.HttpMethod] should be allowed + * + * Must default to true + */ + val allowAnyMethod: Boolean + val allowedMethods: List + + /** + * Whether any header should be allowed + * + * Must default to true + */ + val allowAnyHeader: Boolean + val allowedHeaders: List + + /** + * Response headers a browser page may read, e.g. X-Request-Id and X-Instance-Id in Swagger UI + */ + val exposedHeaders: List + + /** + * Whether requests from the API's own origin should be allowed + * + * Must default to false + */ + val allowSameOrigin: Boolean + + /** + * Whether credentials should be allowed + * + * Must default to true + */ + val allowCredentials: Boolean + + /** + * Whether non-simple content types should be allowed + * + * Must default to true + * + * @see [io.ktor.server.plugins.cors.CORSConfig.allowNonSimpleContentTypes] + */ + val allowNonSimpleContentTypes: Boolean + + @Serializable + data class AllowedHost( + val host: String, + val schemes: List + ) + +} + +fun CorsPluginConfig.configure(config: CorsConfig) { + + if (config.allowAnyHost) { + anyHost() + } else { + + config.allowedHosts.forEach { (host, schemes) -> + allowHost(host, schemes) + } + + } + + if (config.allowAnyMethod) { + HttpMethod.DefaultMethods.forEach(::allowMethod) + } else { + config.allowedMethods.forEach(::allowMethod) + } + + if (config.allowAnyHeader) { + allowHeaders { true } + } else { + config.allowedHeaders.forEach(::allowHeader) + } + + config.exposedHeaders.forEach(::exposeHeader) + + allowSameOrigin = config.allowSameOrigin + allowCredentials = config.allowCredentials + allowNonSimpleContentTypes = config.allowNonSimpleContentTypes + +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/config/cors/DefaultCorsConfig.kt b/src/main/kotlin/com/mtusi/messenger/main/config/cors/DefaultCorsConfig.kt new file mode 100644 index 00000000..5dab8651 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/config/cors/DefaultCorsConfig.kt @@ -0,0 +1,11 @@ +package com.mtusi.messenger.main.config.cors + +import io.ktor.server.application.Application +import io.ktor.server.config.getAs +import org.koin.core.annotation.Provided +import org.koin.core.annotation.Single + +@Single +class DefaultCorsConfig( + @Provided app: Application +) : CorsConfig by app.environment.config.property("cors").getAs() diff --git a/src/main/kotlin/com/mtusi/messenger/main/config/cors/SerialCorsConfig.kt b/src/main/kotlin/com/mtusi/messenger/main/config/cors/SerialCorsConfig.kt new file mode 100644 index 00000000..93918a29 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/config/cors/SerialCorsConfig.kt @@ -0,0 +1,18 @@ +package com.mtusi.messenger.main.config.cors + +import com.mtusi.messenger.main.compat.serializers.SerialHttpMethod +import kotlinx.serialization.Serializable + +@Serializable +data class SerialCorsConfig( + override val allowAnyHost: Boolean = false, + override val allowedHosts: List = emptyList(), + override val allowAnyMethod: Boolean = true, + override val allowedMethods: List = emptyList(), + override val allowAnyHeader: Boolean = true, + override val allowedHeaders: List = emptyList(), + override val exposedHeaders: List = emptyList(), + override val allowSameOrigin: Boolean = false, + override val allowCredentials: Boolean = true, + override val allowNonSimpleContentTypes: Boolean = true +) : CorsConfig diff --git a/src/main/kotlin/com/mtusi/messenger/main/config/database/DatabaseConfig.kt b/src/main/kotlin/com/mtusi/messenger/main/config/database/DatabaseConfig.kt index 40d48c28..1a971655 100644 --- a/src/main/kotlin/com/mtusi/messenger/main/config/database/DatabaseConfig.kt +++ b/src/main/kotlin/com/mtusi/messenger/main/config/database/DatabaseConfig.kt @@ -14,6 +14,8 @@ interface DatabaseConfig { val connectionTimeoutMs: Long val statementTimeoutMs: Long + val migrateOnStart: Boolean + val jdbcConnectionUrl: String get() = "jdbc:postgresql://$host:$port/$name" diff --git a/src/main/kotlin/com/mtusi/messenger/main/config/database/SerialDatabaseConfig.kt b/src/main/kotlin/com/mtusi/messenger/main/config/database/SerialDatabaseConfig.kt index 00c4890e..1a2ebc92 100644 --- a/src/main/kotlin/com/mtusi/messenger/main/config/database/SerialDatabaseConfig.kt +++ b/src/main/kotlin/com/mtusi/messenger/main/config/database/SerialDatabaseConfig.kt @@ -12,5 +12,6 @@ data class SerialDatabaseConfig( override val passwordFile: String? = null, override val maxPoolSize: Int, override val connectionTimeoutMs: Long, - override val statementTimeoutMs: Long + override val statementTimeoutMs: Long, + override val migrateOnStart: Boolean = true ) : DatabaseConfig diff --git a/src/main/kotlin/com/mtusi/messenger/main/config/message/DefaultMessageConfig.kt b/src/main/kotlin/com/mtusi/messenger/main/config/message/DefaultMessageConfig.kt new file mode 100644 index 00000000..8a62d41d --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/config/message/DefaultMessageConfig.kt @@ -0,0 +1,11 @@ +package com.mtusi.messenger.main.config.message + +import io.ktor.server.application.Application +import io.ktor.server.config.getAs +import org.koin.core.annotation.Provided +import org.koin.core.annotation.Single + +@Single +class DefaultMessageConfig( + @Provided app: Application +) : MessageConfig by app.environment.config.propertyOrNull("messages")?.getAs() ?: SerialMessageConfig() diff --git a/src/main/kotlin/com/mtusi/messenger/main/config/message/MessageConfig.kt b/src/main/kotlin/com/mtusi/messenger/main/config/message/MessageConfig.kt new file mode 100644 index 00000000..ccf1808f --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/config/message/MessageConfig.kt @@ -0,0 +1,5 @@ +package com.mtusi.messenger.main.config.message + +interface MessageConfig { + val idempotencyTtlSeconds: Long +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/config/message/SerialMessageConfig.kt b/src/main/kotlin/com/mtusi/messenger/main/config/message/SerialMessageConfig.kt new file mode 100644 index 00000000..6c0752e7 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/config/message/SerialMessageConfig.kt @@ -0,0 +1,8 @@ +package com.mtusi.messenger.main.config.message + +import kotlinx.serialization.Serializable + +@Serializable +data class SerialMessageConfig( + override val idempotencyTtlSeconds: Long = 86400 +) : MessageConfig diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/controllers/message/MessageController.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/controllers/message/MessageController.kt index d1b02be5..c5c5594b 100644 --- a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/controllers/message/MessageController.kt +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/controllers/message/MessageController.kt @@ -11,6 +11,7 @@ import com.mtusi.messenger.main.infrastructure.services.message.results.MessageP import com.mtusi.messenger.main.infrastructure.types.ErrorCode import com.mtusi.messenger.main.plugins.authentication.SESSION_AUTH import com.mtusi.messenger.main.plugins.suspendedTransaction +import com.mtusi.messenger.main.utils.requireIdempotencyKey import com.mtusi.messenger.main.utils.requireNonBlankBounded import com.mtusi.messenger.main.utils.requireUuid import io.ktor.http.HttpStatusCode @@ -33,6 +34,7 @@ fun Routing.configureMessageRouting() = route("/api/v1/chats/{chat_id}/messages" val actor = call.currentUser val chatId = requireUuid(call.parameters["chat_id"], "chat_id") + val idempotencyKey = requireIdempotencyKey(call.request.headers["Idempotency-Key"]) val body = call.receive() requireNonBlankBounded( @@ -46,13 +48,16 @@ fun Routing.configureMessageRouting() = route("/api/v1/chats/{chat_id}/messages" messageService.send( chatId = chatId, senderId = actor.id, - text = body.text + text = body.text, + idempotencyKey = idempotencyKey ) } when (result) { is MessageCreationResult.ChatNotFound -> call.respondError(ErrorCode.NOT_FOUND, "chat not found") + is MessageCreationResult.IdempotencyKeyReused -> + call.respondError(ErrorCode.CONFLICT, "Idempotency-Key was used with a different message") is MessageCreationResult.Success -> call.respond(HttpStatusCode.Created, result.message) } diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/models/message/MessageIdempotencyKeyModel.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/models/message/MessageIdempotencyKeyModel.kt new file mode 100644 index 00000000..dd2698e2 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/models/message/MessageIdempotencyKeyModel.kt @@ -0,0 +1,25 @@ +package com.mtusi.messenger.main.infrastructure.models.message + +import com.mtusi.messenger.main.infrastructure.models.chat.ChatModel +import com.mtusi.messenger.main.infrastructure.models.user.UserModel +import org.jetbrains.exposed.v1.core.ReferenceOption +import org.jetbrains.exposed.v1.core.Table +import org.jetbrains.exposed.v1.datetime.timestamp + +const val MAX_IDEMPOTENCY_KEY_LENGTH = 128 + +object MessageIdempotencyKeyModel : Table("message_idempotency_keys") { + val userId = reference("user_id", UserModel, onDelete = ReferenceOption.CASCADE, onUpdate = ReferenceOption.NO_ACTION, fkName = "message_idempotency_keys_user_id_fkey") + val chatId = reference("chat_id", ChatModel, onDelete = ReferenceOption.CASCADE, onUpdate = ReferenceOption.NO_ACTION, fkName = "message_idempotency_keys_chat_id_fkey") + val key = varchar("key", MAX_IDEMPOTENCY_KEY_LENGTH) + val requestHash = varchar("request_hash", 64) + val messageId = optReference("message_id", MessageModel, onDelete = ReferenceOption.CASCADE, onUpdate = ReferenceOption.NO_ACTION, fkName = "message_idempotency_keys_message_id_fkey") + val createdAt = timestamp("created_at") + + override val primaryKey = PrimaryKey(userId, chatId, key, name = "message_idempotency_keys_pkey") + + init { + index("message_idempotency_keys_chat_id_idx", false, chatId) + index("message_idempotency_keys_message_id_idx", false, messageId) + } +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/authentication/session/cache/SessionCacheRepository.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/authentication/session/cache/SessionCacheRepository.kt index c244cc95..31ef6897 100644 --- a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/authentication/session/cache/SessionCacheRepository.kt +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/authentication/session/cache/SessionCacheRepository.kt @@ -4,28 +4,17 @@ import com.mtusi.messenger.main.infrastructure.dto.authentication.Session import com.mtusi.messenger.main.infrastructure.repositories.Repository import kotlin.time.Instant -/** - * Cache-aside copy of [com.mtusi.messenger.main.infrastructure.repositories.authentication.session.SessionRepository]. - * PostgreSQL stays the source of truth: losing every key only costs a database lookup. - */ interface SessionCacheRepository : Repository { suspend fun find( tokenHash: String ): CachedSession? - /** - * Stores the session until [cachedUntil] unless the key already exists, so a lookup racing with a logout - * cannot overwrite the tombstone written by [revoke]. - */ suspend fun putIfAbsent( session: Session, cachedUntil: Instant ) - /** - * Replaces the entry with a tombstone that lives until [expiresAt], when the session would have expired. - */ suspend fun revoke( tokenHash: String, expiresAt: Instant diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/authentication/session/revocation/SessionRevocationRepository.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/authentication/session/revocation/SessionRevocationRepository.kt index 8612e876..ab9d69b9 100644 --- a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/authentication/session/revocation/SessionRevocationRepository.kt +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/authentication/session/revocation/SessionRevocationRepository.kt @@ -4,18 +4,12 @@ import com.mtusi.messenger.main.infrastructure.dto.authentication.SessionRevocat import com.mtusi.messenger.main.infrastructure.repositories.Repository import kotlin.time.Instant -/** - * Logouts that still have to be written to the session cache as tombstones (outbox). - */ interface SessionRevocationRepository : Repository { fun add( revocation: SessionRevocation ) - /** - * Revocations of sessions that have not expired yet: an expired session is rejected even if its copy is cached. - */ fun findPending( now: Instant ): List diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/chat/ChatRepository.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/chat/ChatRepository.kt index 956e9dfc..4e49658c 100644 --- a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/chat/ChatRepository.kt +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/chat/ChatRepository.kt @@ -21,9 +21,6 @@ interface ChatRepository : Repository { userId: UUID ): Boolean - /** - * Chats of [userId] ordered by `(created_at, id)`, strictly after [after]. - */ fun findPageByMemberId( userId: UUID, after: PageKey?, diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/message/ExposedMessageRepository.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/message/ExposedMessageRepository.kt index 19dc19c4..f687fd95 100644 --- a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/message/ExposedMessageRepository.kt +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/message/ExposedMessageRepository.kt @@ -42,6 +42,16 @@ class ExposedMessageRepository : MessageRepository { ) } + override fun findById( + id: UUID + ): Message? { + return MessageModel + .selectAll() + .where(MessageModel.id eq id) + .firstOrNull() + ?.toMessage() + } + override fun findPageByChatId( chatId: UUID, after: PageKey?, diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/message/MessageRepository.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/message/MessageRepository.kt index 80a130d2..86bddb81 100644 --- a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/message/MessageRepository.kt +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/message/MessageRepository.kt @@ -13,6 +13,10 @@ interface MessageRepository : Repository { text: String ): Message + fun findById( + id: UUID + ): Message? + fun findPageByChatId( chatId: UUID, after: PageKey?, diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/message/idempotency/ExposedMessageIdempotencyRepository.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/message/idempotency/ExposedMessageIdempotencyRepository.kt new file mode 100644 index 00000000..0363fc04 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/message/idempotency/ExposedMessageIdempotencyRepository.kt @@ -0,0 +1,83 @@ +package com.mtusi.messenger.main.infrastructure.repositories.message.idempotency + +import com.mtusi.messenger.main.infrastructure.models.message.MessageIdempotencyKeyModel +import org.jetbrains.exposed.v1.core.and +import org.jetbrains.exposed.v1.core.eq +import org.jetbrains.exposed.v1.core.less +import org.jetbrains.exposed.v1.jdbc.deleteWhere +import org.jetbrains.exposed.v1.jdbc.insertIgnore +import org.jetbrains.exposed.v1.jdbc.selectAll +import org.jetbrains.exposed.v1.jdbc.update +import org.koin.core.annotation.Single +import java.util.UUID +import kotlin.time.Instant + +@Single +class ExposedMessageIdempotencyRepository : MessageIdempotencyRepository { + + override fun claim( + userId: UUID, + chatId: UUID, + key: String, + requestHash: String, + now: Instant + ): Boolean { + + // INSERT ... ON CONFLICT DO NOTHING + val statement = MessageIdempotencyKeyModel.insertIgnore { + it[this.userId] = userId + it[this.chatId] = chatId + it[this.key] = key + it[this.requestHash] = requestHash + it[this.createdAt] = now + } + + return statement.insertedCount > 0 + } + + override fun find( + userId: UUID, + chatId: UUID, + key: String + ): MessageIdempotencyRecord? { + return MessageIdempotencyKeyModel + .selectAll() + .where(scope(userId, chatId, key)) + .firstOrNull() + ?.let { + MessageIdempotencyRecord( + requestHash = it[MessageIdempotencyKeyModel.requestHash], + messageId = it[MessageIdempotencyKeyModel.messageId]?.value, + createdAt = it[MessageIdempotencyKeyModel.createdAt] + ) + } + } + + override fun attachMessage( + userId: UUID, + chatId: UUID, + key: String, + messageId: UUID + ) { + MessageIdempotencyKeyModel.update({ scope(userId, chatId, key) }) { + it[this.messageId] = messageId + } + } + + override fun deleteExpired( + userId: UUID, + chatId: UUID, + key: String, + createdBefore: Instant + ) { + MessageIdempotencyKeyModel.deleteWhere { + scope(userId, chatId, key) and (MessageIdempotencyKeyModel.createdAt less createdBefore) + } + } + + private fun scope(userId: UUID, chatId: UUID, key: String) = + (MessageIdempotencyKeyModel.userId eq userId) and + (MessageIdempotencyKeyModel.chatId eq chatId) and + (MessageIdempotencyKeyModel.key eq key) + +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/message/idempotency/MessageIdempotencyRepository.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/message/idempotency/MessageIdempotencyRepository.kt new file mode 100644 index 00000000..a3069dd0 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/repositories/message/idempotency/MessageIdempotencyRepository.kt @@ -0,0 +1,43 @@ +package com.mtusi.messenger.main.infrastructure.repositories.message.idempotency + +import com.mtusi.messenger.main.infrastructure.repositories.Repository +import java.util.UUID +import kotlin.time.Instant + +data class MessageIdempotencyRecord( + val requestHash: String, + val messageId: UUID?, + val createdAt: Instant, +) + +interface MessageIdempotencyRepository : Repository { + + fun claim( + userId: UUID, + chatId: UUID, + key: String, + requestHash: String, + now: Instant + ): Boolean + + fun find( + userId: UUID, + chatId: UUID, + key: String + ): MessageIdempotencyRecord? + + fun attachMessage( + userId: UUID, + chatId: UUID, + key: String, + messageId: UUID + ) + + fun deleteExpired( + userId: UUID, + chatId: UUID, + key: String, + createdBefore: Instant + ) + +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/authentication/AuthenticationService.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/authentication/AuthenticationService.kt index dd86fe0d..b3db613b 100644 --- a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/authentication/AuthenticationService.kt +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/authentication/AuthenticationService.kt @@ -16,17 +16,10 @@ interface AuthenticationService { userId: UUID ): IssuedSession - /** - * Opens its own transaction only on a cache miss, so it must not be called inside one. - */ suspend fun findSessionByToken( token: String ): Session? - /** - * Deletes the session in PostgreSQL, then replaces its cached copy with a tombstone. Without Redis only the - * database is updated: a copy cached before the outage can then live for at most the cache TTL. - */ suspend fun revokeSession( session: Session ): Boolean diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/authentication/DefaultAuthenticationService.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/authentication/DefaultAuthenticationService.kt index 9bfc3df9..c6a0fa14 100644 --- a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/authentication/DefaultAuthenticationService.kt +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/authentication/DefaultAuthenticationService.kt @@ -52,22 +52,10 @@ class DefaultAuthenticationService( private val random = SecureRandom() - /** - * Database lookups currently running per token hash. Concurrent cache misses for one token wait for the - * lookup already in flight instead of each sending their own query (cache stampede protection). - */ private val inFlightLookups = ConcurrentHashMap>() - /** - * Only availability changes are logged: with Redis down every request would otherwise add a warning. - */ private val isCacheAvailable = AtomicBoolean(true) - /** - * Number of failed cache operations. A failure may have lost a logout tombstone, so the cache is trusted only - * after the pending revocations were written to it with no failure since: [trustedAtFailures] equals this counter. - * A fresh process starts untrusted, as the previous one may have stopped with revocations still pending. - */ private val cacheFailures = AtomicLong() @Volatile @@ -206,11 +194,6 @@ class DefaultAuthenticationService( return withCache { sessionCacheRepository.find(tokenHash) } } - /** - * Writes the tombstones of logouts that happened while Redis was unavailable. Until that succeeds the cache - * is bypassed: a copy cached before the outage could otherwise outlive the logout. One request restores, - * the others go to the database meanwhile; a failed attempt is repeated after [CACHE_RESTORE_BACKOFF]. - */ private suspend fun restoreCache(): Boolean { if (!cacheRestoreMutex.tryLock()) { @@ -277,11 +260,6 @@ class DefaultAuthenticationService( } } - /** - * Runs a cache operation, treating an unavailable Redis as a miss: the database stays the source of truth. - * A command already written when Redis drops the connection fails with the raw [IOException] (connection - * reset), not with a [RedisException]; it means the same. - */ private suspend fun withCache( operation: suspend () -> T ): T? { diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/message/DefaultMessageService.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/message/DefaultMessageService.kt index 800af6fe..34b11a2c 100644 --- a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/message/DefaultMessageService.kt +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/message/DefaultMessageService.kt @@ -1,38 +1,85 @@ package com.mtusi.messenger.main.infrastructure.services.message +import com.mtusi.messenger.main.config.message.MessageConfig import com.mtusi.messenger.main.infrastructure.repositories.chat.ChatRepository import com.mtusi.messenger.main.infrastructure.repositories.message.MessageRepository +import com.mtusi.messenger.main.infrastructure.repositories.message.idempotency.MessageIdempotencyRepository import com.mtusi.messenger.main.infrastructure.services.message.results.MessageCreationResult import com.mtusi.messenger.main.infrastructure.services.message.results.MessagePageResult import com.mtusi.messenger.main.utils.PageKey +import com.mtusi.messenger.main.utils.computeSha256 import com.mtusi.messenger.main.utils.decodeCursor +import com.mtusi.messenger.main.utils.nowMicros import com.mtusi.messenger.main.utils.toPage import org.koin.core.annotation.Single import java.util.UUID +import kotlin.time.Clock +import kotlin.time.Duration.Companion.seconds + +// One attempt to claim a free key, plus one more if the stored key had expired and was removed +private const val CLAIM_ATTEMPTS = 2 @Single class DefaultMessageService( private val chatRepository: ChatRepository, - private val messageRepository: MessageRepository + private val messageRepository: MessageRepository, + private val idempotencyRepository: MessageIdempotencyRepository, + private val config: MessageConfig ) : MessageService { override fun send( chatId: UUID, senderId: UUID, - text: String + text: String, + idempotencyKey: String ): MessageCreationResult { + // Access is checked on every retry too: a replay must not outlive the membership. if (!chatRepository.isMember(chatId, senderId)) { return MessageCreationResult.ChatNotFound } - val message = messageRepository.create( - chatId = chatId, - senderId = senderId, - text = text - ) + // The body has a single field, so its hash is the hash of the text. + val requestHash = text.toByteArray().computeSha256() + val now = Clock.System.nowMicros() - return MessageCreationResult.Success(message) + repeat(CLAIM_ATTEMPTS) { + + if (idempotencyRepository.claim(senderId, chatId, idempotencyKey, requestHash, now)) { + + val message = messageRepository.create( + chatId = chatId, + senderId = senderId, + text = text + ) + idempotencyRepository.attachMessage(senderId, chatId, idempotencyKey, message.id) + + return MessageCreationResult.Success(message) + } + + // The key is taken by a committed transaction (claim waits for an uncommitted one). + val record = idempotencyRepository.find(senderId, chatId, idempotencyKey) + ?: return@repeat + + val expiredBefore = now - config.idempotencyTtlSeconds.seconds + + if (record.createdAt < expiredBefore) { + idempotencyRepository.deleteExpired(senderId, chatId, idempotencyKey, createdBefore = expiredBefore) + return@repeat + } + + if (record.requestHash != requestHash) { + return MessageCreationResult.IdempotencyKeyReused + } + + val messageId = checkNotNull(record.messageId) { "committed idempotency key without a message" } + + return MessageCreationResult.Success( + checkNotNull(messageRepository.findById(messageId)) { "message of an idempotency key is missing" } + ) + } + + error("Idempotency key could not be claimed") } override fun findPageByChatId( diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/message/MessageService.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/message/MessageService.kt index 075c35d7..5ec42822 100644 --- a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/message/MessageService.kt +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/message/MessageService.kt @@ -9,7 +9,8 @@ interface MessageService { fun send( chatId: UUID, senderId: UUID, - text: String + text: String, + idempotencyKey: String ): MessageCreationResult fun findPageByChatId( diff --git a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/message/results/MessageCreationResult.kt b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/message/results/MessageCreationResult.kt index d5ac9756..1f260a32 100644 --- a/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/message/results/MessageCreationResult.kt +++ b/src/main/kotlin/com/mtusi/messenger/main/infrastructure/services/message/results/MessageCreationResult.kt @@ -8,6 +8,8 @@ sealed class MessageCreationResult { data object ChatNotFound : Error() + data object IdempotencyKeyReused : Error() + data class Success( val message: Message ) : MessageCreationResult() diff --git a/src/main/kotlin/com/mtusi/messenger/main/plugins/Cors.kt b/src/main/kotlin/com/mtusi/messenger/main/plugins/Cors.kt new file mode 100644 index 00000000..de5c66b1 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/plugins/Cors.kt @@ -0,0 +1,13 @@ +package com.mtusi.messenger.main.plugins + +import com.mtusi.messenger.main.config.cors.CorsConfig +import com.mtusi.messenger.main.config.cors.configure +import io.ktor.server.application.Application +import io.ktor.server.application.install +import io.ktor.server.plugins.cors.routing.CORS +import org.koin.ktor.ext.inject + +fun Application.configureCors() = install(CORS) { + val config by this@configureCors.inject() + configure(config) +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/plugins/Database.kt b/src/main/kotlin/com/mtusi/messenger/main/plugins/Database.kt index 32001ed3..623f3dd0 100644 --- a/src/main/kotlin/com/mtusi/messenger/main/plugins/Database.kt +++ b/src/main/kotlin/com/mtusi/messenger/main/plugins/Database.kt @@ -6,6 +6,7 @@ import com.mtusi.messenger.main.infrastructure.models.authentication.SessionMode import com.mtusi.messenger.main.infrastructure.models.authentication.SessionRevocationModel import com.mtusi.messenger.main.infrastructure.models.chat.ChatMemberModel import com.mtusi.messenger.main.infrastructure.models.chat.ChatModel +import com.mtusi.messenger.main.infrastructure.models.message.MessageIdempotencyKeyModel import com.mtusi.messenger.main.infrastructure.models.message.MessageModel import com.mtusi.messenger.main.infrastructure.models.user.UserModel import com.mtusi.messenger.main.utils.resolveSecret @@ -46,7 +47,11 @@ fun Application.configureDatabase() { defaultMaxAttempts = 1 } ) - applyDatabaseMigrations(dataSource) + if (config.migrateOnStart) { + applyDatabaseMigrations(dataSource) + } else { + validateDatabaseMigrations(dataSource) + } monitor.subscribe(ApplicationStopped) { dataSource.close() @@ -69,18 +74,38 @@ private fun DatabaseConfig.createDataSource(): HikariDataSource { ) } +private fun flyway(dataSource: DataSource) = Flyway.configure() + .dataSource(dataSource) + .locations("classpath:migrations") + .load() + fun applyDatabaseMigrations( dataSource: DataSource ) { - - val flyway = Flyway.configure() - .dataSource(dataSource) - .locations("classpath:migrations") - .load() - + val flyway = flyway(dataSource) flyway.migrate() flyway.validate() +} +/** + * Fails if a migration is pending or differs from the applied one, so a replica never serves an outdated schema. + */ +fun validateDatabaseMigrations( + dataSource: DataSource +) { + flyway(dataSource).validate() +} + +/** + * Entry point of `java -jar messenger.jar migrate`: applies the migrations once and exits. + * Uses the `database` section of application.conf, environment variables included. + */ +fun migrateDatabase() { + val config = HoconApplicationConfig(ConfigFactory.load()) + .property("database") + .getAs() + + config.createDataSource().use(::applyDatabaseMigrations) } fun main(args: Array) { @@ -96,6 +121,7 @@ private fun generateMigration(description: String) { ChatModel, ChatMemberModel, MessageModel, + MessageIdempotencyKeyModel, SessionModel, SessionRevocationModel ) diff --git a/src/main/kotlin/com/mtusi/messenger/main/plugins/InstanceId.kt b/src/main/kotlin/com/mtusi/messenger/main/plugins/InstanceId.kt new file mode 100644 index 00000000..34066029 --- /dev/null +++ b/src/main/kotlin/com/mtusi/messenger/main/plugins/InstanceId.kt @@ -0,0 +1,28 @@ +package com.mtusi.messenger.main.plugins + +import io.ktor.server.application.Application +import io.ktor.server.application.createApplicationPlugin +import io.ktor.server.application.install +import io.ktor.server.application.log +import java.util.UUID + +/** + * Identifies the replica that produced the response. Stable for the life of the process: `INSTANCE_ID` if set, + * otherwise the container hostname (Docker sets it to the short container id), otherwise a random alias. + */ +fun Application.configureInstanceId() { + + val instanceId = environment.config.propertyOrNull("instance.id")?.getString()?.takeIf { it.isNotBlank() } + ?: System.getenv("HOSTNAME")?.takeIf { it.isNotBlank() } + ?: UUID.randomUUID().toString().take(12) + + log.info("X-Instance-Id of this replica: $instanceId") + + install( + createApplicationPlugin("InstanceId") { + onCall { call -> + call.response.headers.append("X-Instance-Id", instanceId, safeOnly = false) + } + } + ) +} diff --git a/src/main/kotlin/com/mtusi/messenger/main/utils/Pagination.kt b/src/main/kotlin/com/mtusi/messenger/main/utils/Pagination.kt index cd84ad28..bc605e09 100644 --- a/src/main/kotlin/com/mtusi/messenger/main/utils/Pagination.kt +++ b/src/main/kotlin/com/mtusi/messenger/main/utils/Pagination.kt @@ -10,9 +10,6 @@ import kotlin.time.Instant private val json = Json { ignoreUnknownKeys = false } -/** - * Position in a `(created_at, id)` ordered list: the next page starts strictly after it. - */ data class PageKey( val createdAt: Instant, val id: UUID, @@ -30,9 +27,6 @@ fun encodeCursor(scope: String, key: PageKey): String { return Base64.getUrlEncoder().withoutPadding().encodeToString(raw.toByteArray(Charsets.UTF_8)) } -/** - * @return page key, or null if the cursor is malformed or was issued for another resource - */ fun decodeCursor(raw: String, expectedScope: String): PageKey? { val cursor = try { val bytes = Base64.getUrlDecoder().decode(raw) @@ -48,9 +42,6 @@ fun decodeCursor(raw: String, expectedScope: String): PageKey? { return PageKey(cursor.createdAt, cursor.id) } -/** - * Builds a page from `limit + 1` rows fetched after the cursor: the extra row only signals that more data exists. - */ fun List.toPage( limit: Int, scope: String, diff --git a/src/main/kotlin/com/mtusi/messenger/main/utils/Secrets.kt b/src/main/kotlin/com/mtusi/messenger/main/utils/Secrets.kt index e0b30312..8288a810 100644 --- a/src/main/kotlin/com/mtusi/messenger/main/utils/Secrets.kt +++ b/src/main/kotlin/com/mtusi/messenger/main/utils/Secrets.kt @@ -2,9 +2,6 @@ package com.mtusi.messenger.main.utils import java.io.File -/** - * Secrets come either inline (local development) or as a mounted file (Docker secrets), the inline value wins. - */ fun resolveSecret( value: String?, file: String? diff --git a/src/main/kotlin/com/mtusi/messenger/main/utils/Time.kt b/src/main/kotlin/com/mtusi/messenger/main/utils/Time.kt index 7c8c8d43..86b8e3c7 100644 --- a/src/main/kotlin/com/mtusi/messenger/main/utils/Time.kt +++ b/src/main/kotlin/com/mtusi/messenger/main/utils/Time.kt @@ -5,10 +5,6 @@ import kotlin.time.Instant private const val NANOS_PER_MICRO = 1_000 -/** - * Current time truncated to microseconds: PostgreSQL timestamptz keeps exactly this precision, - * so the value returned to the client equals the one read back from the database. - */ fun Clock.nowMicros(): Instant { val now = now() return Instant.fromEpochSeconds( diff --git a/src/main/kotlin/com/mtusi/messenger/main/utils/Validation.kt b/src/main/kotlin/com/mtusi/messenger/main/utils/Validation.kt index 395e8fb6..aad25ee1 100644 --- a/src/main/kotlin/com/mtusi/messenger/main/utils/Validation.kt +++ b/src/main/kotlin/com/mtusi/messenger/main/utils/Validation.kt @@ -6,6 +6,8 @@ import java.util.UUID private val USERNAME = Regex("^[a-z0-9_]{3,32}$") +private val IDEMPOTENCY_KEY = Regex("^[A-Za-z0-9._:-]{1,128}$") + private val UUID_REGEX = Regex( "^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$" ) @@ -28,6 +30,13 @@ fun requireNonBlankBounded(value: String, min: Int, max: Int, field: String) { } } +fun requireIdempotencyKey(value: String?): String { + if (value == null || !IDEMPOTENCY_KEY.matches(value)) { + throw ApiException(ErrorCode.INVALID_REQUEST, "missing or invalid Idempotency-Key") + } + return value +} + fun requireUuid(value: String?, field: String): UUID { if (value == null || !UUID_REGEX.matches(value)) { throw ApiException(ErrorCode.INVALID_REQUEST, "invalid $field") diff --git a/src/main/resources/application.conf b/src/main/resources/application.conf index 0f2d4033..7460517c 100644 --- a/src/main/resources/application.conf +++ b/src/main/resources/application.conf @@ -28,6 +28,9 @@ database { maxPoolSize = ${?DB_MAX_POOL_SIZE} connectionTimeoutMs = 2000 statementTimeoutMs = 5000 + # Replicas run with false: compose applies migrations once in the `migrate` service. + migrateOnStart = true + migrateOnStart = ${?DB_MIGRATE_ON_START} } redis { @@ -41,6 +44,39 @@ redis { commandTimeoutMs = 500 } +cors { + # Defaults to false + #allowAnyHost = true + # Swagger UI sends "Try it out" to the contract server http://localhost:8080; pages opened elsewhere need these. + allowedHosts = [ + { host = "localhost:8080", schemes = ["http"] } + { host = "localhost:8081", schemes = ["http"] } + { host = "127.0.0.1:8080", schemes = ["http"] } + { host = "127.0.0.1:8081", schemes = ["http"] } + ] + + # Defaults to true + #allowAnyMethod = true + #allowedMethods = [GET, POST, PATCH, PUT, DELETE, OPTIONS] + + allowAnyHeader = true + #allowedHeaders = [] + + exposedHeaders = [X-Request-Id, X-Instance-Id, WWW-Authenticate] + + # Browsers send Origin on same-origin POST too, e.g. Swagger UI served by the API itself. + allowSameOrigin = true +} + +instance { + id = ${?INSTANCE_ID} +} + +messages { + idempotencyTtlSeconds = 86400 + idempotencyTtlSeconds = ${?IDEMPOTENCY_TTL_SECONDS} +} + authentication { realm = "messenger" sessionTtlSeconds = 3600 diff --git a/src/main/resources/migrations/V3__message_idempotency_keys.sql b/src/main/resources/migrations/V3__message_idempotency_keys.sql new file mode 100644 index 00000000..9ddedfee --- /dev/null +++ b/src/main/resources/migrations/V3__message_idempotency_keys.sql @@ -0,0 +1,19 @@ +-- Idempotency-Key of POST /chats/{chat_id}/messages. Scope: user + chat (method and path are fixed) + key. +-- The row is inserted in the same transaction as the message, so a key exists only together with its message. +-- message_id is NULL only inside that transaction: the key is claimed first, the message is written next. +CREATE TABLE message_idempotency_keys ( + user_id uuid NOT NULL, + chat_id uuid NOT NULL, + key varchar(128) NOT NULL, + request_hash varchar(64) NOT NULL, + message_id uuid NULL, + created_at timestamptz NOT NULL, + CONSTRAINT message_idempotency_keys_pkey PRIMARY KEY (user_id, chat_id, key), + CONSTRAINT message_idempotency_keys_user_id_fkey FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE, + CONSTRAINT message_idempotency_keys_chat_id_fkey FOREIGN KEY (chat_id) REFERENCES chats (id) ON DELETE CASCADE, + CONSTRAINT message_idempotency_keys_message_id_fkey FOREIGN KEY (message_id) REFERENCES messages (id) ON DELETE CASCADE +); + +-- Foreign keys are not indexed automatically; the cascades from chats and messages need these. +CREATE INDEX message_idempotency_keys_chat_id_idx ON message_idempotency_keys (chat_id); +CREATE INDEX message_idempotency_keys_message_id_idx ON message_idempotency_keys (message_id); diff --git a/src/test/kotlin/com/mtusi/messenger/main/infrastructure/controllers/message/MessageConcurrencyTest.kt b/src/test/kotlin/com/mtusi/messenger/main/infrastructure/controllers/message/MessageConcurrencyTest.kt index a91cb3d8..5cc3bc1b 100644 --- a/src/test/kotlin/com/mtusi/messenger/main/infrastructure/controllers/message/MessageConcurrencyTest.kt +++ b/src/test/kotlin/com/mtusi/messenger/main/infrastructure/controllers/message/MessageConcurrencyTest.kt @@ -5,6 +5,7 @@ import com.mtusi.messenger.main.infrastructure.dto.Message import com.mtusi.messenger.main.infrastructure.dto.PageResponse import com.mtusi.messenger.main.support.auth import com.mtusi.messenger.main.support.createChat +import com.mtusi.messenger.main.support.idempotencyKey import com.mtusi.messenger.main.support.register import com.mtusi.messenger.main.support.runApp import io.ktor.client.call.body @@ -37,6 +38,7 @@ class MessageConcurrencyTest { repeat(perWorker) { i -> val response = client.post("/api/v1/chats/${chat.id}/messages") { auth(alice) + idempotencyKey() contentType(ContentType.Application.Json) setBody(CreateMessageRequest(text = "w$w-i$i")) } diff --git a/src/test/kotlin/com/mtusi/messenger/main/infrastructure/controllers/message/MessageControllerTest.kt b/src/test/kotlin/com/mtusi/messenger/main/infrastructure/controllers/message/MessageControllerTest.kt index ac72e82c..61ce8052 100644 --- a/src/test/kotlin/com/mtusi/messenger/main/infrastructure/controllers/message/MessageControllerTest.kt +++ b/src/test/kotlin/com/mtusi/messenger/main/infrastructure/controllers/message/MessageControllerTest.kt @@ -6,6 +6,7 @@ import com.mtusi.messenger.main.infrastructure.dto.Message import com.mtusi.messenger.main.infrastructure.dto.PageResponse import com.mtusi.messenger.main.support.auth import com.mtusi.messenger.main.support.createChat +import com.mtusi.messenger.main.support.idempotencyKey import com.mtusi.messenger.main.support.register import com.mtusi.messenger.main.support.runApp import io.ktor.client.call.body @@ -29,6 +30,7 @@ class MessageControllerTest { val response = client.post("/api/v1/chats/${chat.id}/messages") { auth(alice) + idempotencyKey() contentType(ContentType.Application.Json) setBody(CreateMessageRequest(text = "")) } @@ -45,6 +47,7 @@ class MessageControllerTest { val response = client.post("/api/v1/chats/${chat.id}/messages") { auth(bob) + idempotencyKey() contentType(ContentType.Application.Json) setBody(CreateMessageRequest(text = "hi")) } @@ -75,6 +78,7 @@ class MessageControllerTest { repeat(total) { i -> val r = client.post("/api/v1/chats/${chat.id}/messages") { auth(alice) + idempotencyKey() contentType(ContentType.Application.Json) setBody(CreateMessageRequest(text = "m$i")) } @@ -115,6 +119,7 @@ class MessageControllerTest { repeat(20) { i -> client.post("/api/v1/chats/${chat.id}/messages") { auth(alice) + idempotencyKey() contentType(ContentType.Application.Json) setBody(CreateMessageRequest(text = "m$i")) } @@ -164,6 +169,7 @@ class MessageControllerTest { repeat(2) { i -> client.post("/api/v1/chats/${chatA.id}/messages") { auth(alice) + idempotencyKey() contentType(ContentType.Application.Json) setBody(CreateMessageRequest(text = "a$i")) } diff --git a/src/test/kotlin/com/mtusi/messenger/main/infrastructure/controllers/message/MessageIdempotencyTest.kt b/src/test/kotlin/com/mtusi/messenger/main/infrastructure/controllers/message/MessageIdempotencyTest.kt new file mode 100644 index 00000000..eb9728c5 --- /dev/null +++ b/src/test/kotlin/com/mtusi/messenger/main/infrastructure/controllers/message/MessageIdempotencyTest.kt @@ -0,0 +1,157 @@ +package com.mtusi.messenger.main.infrastructure.controllers.message + +import com.mtusi.messenger.main.extensions.ErrorBody +import com.mtusi.messenger.main.infrastructure.controllers.message.requests.CreateMessageRequest +import com.mtusi.messenger.main.infrastructure.dto.Chat +import com.mtusi.messenger.main.infrastructure.dto.Message +import com.mtusi.messenger.main.infrastructure.dto.PageResponse +import com.mtusi.messenger.main.support.TestUser +import com.mtusi.messenger.main.support.auth +import com.mtusi.messenger.main.support.createChat +import com.mtusi.messenger.main.support.idempotencyKey +import com.mtusi.messenger.main.support.register +import com.mtusi.messenger.main.support.runApp +import io.ktor.client.HttpClient +import io.ktor.client.call.body +import io.ktor.client.request.get +import io.ktor.client.request.post +import io.ktor.client.request.setBody +import io.ktor.client.statement.HttpResponse +import io.ktor.http.ContentType +import io.ktor.http.HttpStatusCode +import io.ktor.http.contentType +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.async +import kotlinx.coroutines.awaitAll +import kotlinx.coroutines.coroutineScope +import kotlinx.coroutines.delay +import kotlinx.coroutines.withContext +import java.util.UUID +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertNotEquals + +class MessageIdempotencyTest { + + private suspend fun HttpClient.send(user: TestUser, chat: Chat, text: String, key: String?): HttpResponse { + return post("/api/v1/chats/${chat.id}/messages") { + auth(user) + if (key != null) { + idempotencyKey(key) + } + contentType(ContentType.Application.Json) + setBody(CreateMessageRequest(text = text)) + } + } + + private suspend fun HttpClient.history(user: TestUser, chat: Chat): List { + return get("/api/v1/chats/${chat.id}/messages?limit=100") { auth(user) }.body>().items + } + + @Test + fun `retry with the same key and body returns the original message`() = runApp { client -> + val alice = client.register("alice") + val chat = client.createChat(alice, "retry", listOf(alice.id)) + val key = UUID.randomUUID().toString() + + val first = client.send(alice, chat, "hello", key) + val retry = client.send(alice, chat, "hello", key) + + assertEquals(HttpStatusCode.Created, first.status) + assertEquals(HttpStatusCode.Created, retry.status) + assertEquals(first.body(), retry.body()) + assertEquals(1, client.history(alice, chat).size) + } + + @Test + fun `same key with another body is a conflict`() = runApp { client -> + val alice = client.register("alice") + val chat = client.createChat(alice, "conflict", listOf(alice.id)) + val key = UUID.randomUUID().toString() + + client.send(alice, chat, "first", key) + val response = client.send(alice, chat, "second", key) + + assertEquals(HttpStatusCode.Conflict, response.status) + assertEquals("conflict", response.body().error.code) + assertEquals(listOf("first"), client.history(alice, chat).map { it.text }) + } + + @Test + fun `missing or malformed key is rejected before anything is written`() = runApp { client -> + val alice = client.register("alice") + val chat = client.createChat(alice, "keys", listOf(alice.id)) + + for (key in listOf(null, "", "has space", "кириллица", "a".repeat(129))) { + val response = client.send(alice, chat, "text", key) + assertEquals(HttpStatusCode.BadRequest, response.status, "key: $key") + assertEquals("invalid_request", response.body().error.code) + } + assertEquals(HttpStatusCode.Created, client.send(alice, chat, "ok", "A-z_0.9:" + "x".repeat(120)).status) + assertEquals(1, client.history(alice, chat).size) + } + + @Test + fun `key scope is user and chat`() = runApp { client -> + val alice = client.register("alice") + val bob = client.register("bob") + val first = client.createChat(alice, "one", listOf(alice.id, bob.id)) + val second = client.createChat(alice, "two", listOf(alice.id, bob.id)) + val key = UUID.randomUUID().toString() + + val ids = listOf( + client.send(alice, first, "same", key), + client.send(bob, first, "same", key), + client.send(alice, second, "same", key) + ).map { + assertEquals(HttpStatusCode.Created, it.status) + it.body().id + } + + assertEquals(3, ids.toSet().size) + } + + @Test + fun `retry by a former outsider is still checked for access`() = runApp { client -> + val alice = client.register("alice") + val eve = client.register("eve") + val chat = client.createChat(alice, "access", listOf(alice.id)) + val key = UUID.randomUUID().toString() + + client.send(alice, chat, "hello", key) + + assertEquals(HttpStatusCode.NotFound, client.send(eve, chat, "hello", key).status) + } + + @Test + fun `concurrent retries create exactly one message`() = runApp { client -> + val alice = client.register("alice") + val chat = client.createChat(alice, "concurrent", listOf(alice.id)) + val key = UUID.randomUUID().toString() + + val responses = withContext(Dispatchers.Default) { + coroutineScope { + (0 until 20).map { async { client.send(alice, chat, "once", key) } }.awaitAll() + } + } + + assertEquals(List(20) { HttpStatusCode.Created }, responses.map { it.status }) + assertEquals(1, responses.map { it.body() }.toSet().size) + assertEquals(1, client.history(alice, chat).size) + } + + @Test + fun `expired key can be used for a new message`() = runApp(idempotencyTtlSeconds = 1) { client -> + val alice = client.register("alice") + val chat = client.createChat(alice, "ttl", listOf(alice.id)) + val key = UUID.randomUUID().toString() + + val first = client.send(alice, chat, "hello", key).body() + delay(1500) + val second = client.send(alice, chat, "hello again", key) + + assertEquals(HttpStatusCode.Created, second.status) + assertNotEquals(first.id, second.body().id) + } + +} diff --git a/src/test/kotlin/com/mtusi/messenger/main/plugins/CorsTest.kt b/src/test/kotlin/com/mtusi/messenger/main/plugins/CorsTest.kt new file mode 100644 index 00000000..50fb3b11 --- /dev/null +++ b/src/test/kotlin/com/mtusi/messenger/main/plugins/CorsTest.kt @@ -0,0 +1,26 @@ +package com.mtusi.messenger.main.plugins + +import com.mtusi.messenger.main.support.runApp +import io.ktor.client.request.get +import io.ktor.client.request.header +import io.ktor.http.HttpStatusCode +import kotlin.test.Test +import kotlin.test.assertEquals + +class CorsTest { + + @Test + fun `page of the API itself may call it`() = runApp { client -> + val response = client.get("/health/ready") { header("Origin", "http://localhost") } + + assertEquals(HttpStatusCode.OK, response.status) + } + + @Test + fun `unknown origin is rejected`() = runApp { client -> + val response = client.get("/health/ready") { header("Origin", "http://evil.example") } + + assertEquals(HttpStatusCode.Forbidden, response.status) + } + +} diff --git a/src/test/kotlin/com/mtusi/messenger/main/plugins/DependencyFailureTest.kt b/src/test/kotlin/com/mtusi/messenger/main/plugins/DependencyFailureTest.kt index 2a171457..e221736d 100644 --- a/src/test/kotlin/com/mtusi/messenger/main/plugins/DependencyFailureTest.kt +++ b/src/test/kotlin/com/mtusi/messenger/main/plugins/DependencyFailureTest.kt @@ -8,6 +8,7 @@ import com.mtusi.messenger.main.support.TestRedis import com.mtusi.messenger.main.support.TestUser import com.mtusi.messenger.main.support.auth import com.mtusi.messenger.main.support.createChat +import com.mtusi.messenger.main.support.idempotencyKey import com.mtusi.messenger.main.support.register import com.mtusi.messenger.main.support.runApp import com.mtusi.messenger.main.utils.computeSha256 @@ -176,6 +177,7 @@ class DependencyFailureTest { HttpStatusCode.Unauthorized, client.post("/api/v1/chats/${chat.id}/messages") { auth(alice) + idempotencyKey() contentType(ContentType.Application.Json) setBody(CreateMessageRequest(text = "after logout")) }.status diff --git a/src/test/kotlin/com/mtusi/messenger/main/plugins/InstanceIdTest.kt b/src/test/kotlin/com/mtusi/messenger/main/plugins/InstanceIdTest.kt new file mode 100644 index 00000000..e3141f41 --- /dev/null +++ b/src/test/kotlin/com/mtusi/messenger/main/plugins/InstanceIdTest.kt @@ -0,0 +1,30 @@ +package com.mtusi.messenger.main.plugins + +import com.mtusi.messenger.main.support.register +import com.mtusi.messenger.main.support.runApp +import io.ktor.client.request.get +import io.ktor.client.request.header +import io.ktor.client.request.post +import kotlin.test.Test +import kotlin.test.assertEquals + +class InstanceIdTest { + + @Test + fun `every response names the replica`() = runApp { client -> + val alice = client.register("alice") + + val responses = listOf( + client.get("/health/live"), + client.get("/health/ready"), + client.get("/api/v1/users/me") { header("Authorization", "Bearer ${alice.token}") }, + client.get("/api/v1/users/me"), + client.get("/api/v1/chats/not-a-uuid") { header("Authorization", "Bearer ${alice.token}") }, + client.post("/api/v1/chats") { header("Authorization", "Bearer ${alice.token}") }, + client.get("/no/such/route") + ) + + assertEquals(List(responses.size) { "test-instance" }, responses.map { it.headers["X-Instance-Id"] }) + } + +} diff --git a/src/test/kotlin/com/mtusi/messenger/main/support/TestApp.kt b/src/test/kotlin/com/mtusi/messenger/main/support/TestApp.kt index c502993b..669b36c6 100644 --- a/src/test/kotlin/com/mtusi/messenger/main/support/TestApp.kt +++ b/src/test/kotlin/com/mtusi/messenger/main/support/TestApp.kt @@ -13,6 +13,7 @@ import io.ktor.client.plugins.contentnegotiation.ContentNegotiation import io.ktor.client.request.HttpRequestBuilder import io.ktor.client.request.basicAuth import io.ktor.client.request.bearerAuth +import io.ktor.client.request.header import io.ktor.client.request.post import io.ktor.client.request.setBody import io.ktor.http.ContentType @@ -56,6 +57,7 @@ private fun testConfig( database: PostgreSQLContainer<*>, databaseConnectionTimeoutMs: Long, redis: RedisEndpoint, + idempotencyTtlSeconds: Long, ) = HoconApplicationConfig( ConfigFactory.parseMap( mapOf( @@ -75,6 +77,9 @@ private fun testConfig( "authentication.realm" to "messenger", "authentication.sessionTtlSeconds" to sessionTtlSeconds, "authentication.sessionCacheTtlSeconds" to 60, + "messages.idempotencyTtlSeconds" to idempotencyTtlSeconds, + "instance.id" to "test-instance", + "cors.allowSameOrigin" to true, ) ) ) @@ -87,10 +92,11 @@ fun runApp( database: PostgreSQLContainer<*> = TestDatabase.container, databaseConnectionTimeoutMs: Long = 10000, redis: RedisEndpoint = TestRedis.endpoint(), + idempotencyTtlSeconds: Long = 86400, block: suspend ApplicationTestBuilder.(HttpClient) -> Unit ) = testApplication { environment { - config = testConfig(sessionTtlSeconds, database, databaseConnectionTimeoutMs, redis) + config = testConfig(sessionTtlSeconds, database, databaseConnectionTimeoutMs, redis, idempotencyTtlSeconds) } application { module() @@ -137,6 +143,11 @@ suspend fun HttpClient.register(prefix: String): TestUser { fun HttpRequestBuilder.auth(user: TestUser) = bearerAuth(user.token) +/** + * Every message POST needs its own key since lab3; pass the same key to model a retry. + */ +fun HttpRequestBuilder.idempotencyKey(key: String = UUID.randomUUID().toString()) = header("Idempotency-Key", key) + suspend fun HttpClient.createChat( creator: TestUser, title: String,