Completed for a grade of four
This commit is contained in:
@@ -44,6 +44,8 @@ dependencies {
|
||||
implementation(libs.flyway)
|
||||
implementation(libs.flyway.postgresql)
|
||||
|
||||
implementation(libs.lettuce)
|
||||
|
||||
implementation(libs.bouncycastle)
|
||||
|
||||
testImplementation(kotlin("test"))
|
||||
|
||||
@@ -21,6 +21,31 @@ services:
|
||||
start_period: 10s
|
||||
restart: unless-stopped
|
||||
|
||||
redis:
|
||||
image: redis:8.4.0-alpine
|
||||
container_name: mtusi-lab2-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:
|
||||
- sh
|
||||
- -c
|
||||
- >-
|
||||
exec docker-entrypoint.sh redis-server
|
||||
--requirepass "$$(cat /run/secrets/redis_password)"
|
||||
--save ""
|
||||
--appendonly no
|
||||
--maxmemory 64mb
|
||||
--maxmemory-policy volatile-lru
|
||||
secrets:
|
||||
- redis_password
|
||||
healthcheck:
|
||||
test: ["CMD-SHELL", "REDISCLI_AUTH=\"$$(cat /run/secrets/redis_password)\" redis-cli ping | grep -q PONG"]
|
||||
interval: 5s
|
||||
timeout: 3s
|
||||
retries: 10
|
||||
start_period: 5s
|
||||
restart: unless-stopped
|
||||
|
||||
api:
|
||||
build: .
|
||||
image: mtusi-messenger:lab2
|
||||
@@ -28,6 +53,8 @@ services:
|
||||
depends_on:
|
||||
postgres:
|
||||
condition: service_healthy
|
||||
redis:
|
||||
condition: service_healthy
|
||||
environment:
|
||||
APP_HOST: "0.0.0.0"
|
||||
APP_PORT: "8080"
|
||||
@@ -36,9 +63,13 @@ services:
|
||||
DB_NAME: messenger
|
||||
DB_USER: messenger
|
||||
DB_PASSWORD_FILE: /run/secrets/db_password
|
||||
REDIS_HOST: redis
|
||||
REDIS_PORT: "6379"
|
||||
REDIS_PASSWORD_FILE: /run/secrets/redis_password
|
||||
SESSION_TTL_SECONDS: ${SESSION_TTL_SECONDS:-3600}
|
||||
secrets:
|
||||
- db_password
|
||||
- redis_password
|
||||
ports:
|
||||
- "127.0.0.1:8080:8080"
|
||||
restart: unless-stopped
|
||||
@@ -46,6 +77,8 @@ services:
|
||||
secrets:
|
||||
db_password:
|
||||
file: ./secrets/db_password
|
||||
redis_password:
|
||||
file: ./secrets/redis_password
|
||||
|
||||
volumes:
|
||||
postgres-data:
|
||||
|
||||
@@ -0,0 +1,66 @@
|
||||
|
||||
== Data volume
|
||||
total_messages | target_chat_messages
|
||||
----------------+----------------------
|
||||
70006 | 20000
|
||||
(1 row)
|
||||
|
||||
|
||||
== 1. First page: WHERE chat_id = ? ORDER BY created_at, id LIMIT 51
|
||||
QUERY PLAN
|
||||
------------------------------------------------------------------------------------------------------------------
|
||||
Limit (actual time=0.005..0.022 rows=51 loops=1)
|
||||
Buffers: shared hit=4
|
||||
-> Index Scan using messages_chat_id_created_at_id_idx on messages (actual time=0.005..0.020 rows=51 loops=1)
|
||||
Index Cond: (chat_id = '00000000-0000-0000-0001-000000000000'::uuid)
|
||||
Buffers: shared hit=4
|
||||
Planning:
|
||||
Buffers: shared hit=6
|
||||
Planning Time: 0.045 ms
|
||||
Execution Time: 0.029 ms
|
||||
(9 rows)
|
||||
|
||||
|
||||
== 2. Deep page after the 15 000th message: (created_at, id) > (?, ?)
|
||||
QUERY PLAN
|
||||
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
Limit (actual time=0.005..0.010 rows=51 loops=1)
|
||||
Buffers: shared hit=4
|
||||
-> Index Scan using messages_chat_id_created_at_id_idx on messages (actual time=0.005..0.007 rows=51 loops=1)
|
||||
Index Cond: ((chat_id = '00000000-0000-0000-0001-000000000000'::uuid) AND (ROW(created_at, id) > ROW('2026-01-01 04:10:00+00'::timestamp with time zone, '5ec4eb0a-04cf-487e-9ec0-16b7367e29df'::uuid)))
|
||||
Buffers: shared hit=4
|
||||
Planning Time: 0.027 ms
|
||||
Execution Time: 0.025 ms
|
||||
(7 rows)
|
||||
|
||||
|
||||
== 3. Same deep page with OFFSET instead of a cursor (for comparison, not used by the API)
|
||||
QUERY PLAN
|
||||
---------------------------------------------------------------------------------------------------------------------
|
||||
Limit (actual time=1.076..1.080 rows=51 loops=1)
|
||||
Buffers: shared hit=296
|
||||
-> Index Scan using messages_chat_id_created_at_id_idx on messages (actual time=0.007..0.800 rows=15051 loops=1)
|
||||
Index Cond: (chat_id = '00000000-0000-0000-0001-000000000000'::uuid)
|
||||
Buffers: shared hit=296
|
||||
Planning Time: 0.025 ms
|
||||
Execution Time: 1.093 ms
|
||||
(7 rows)
|
||||
|
||||
|
||||
== 4. Deep page without messages_chat_id_created_at_id_idx (index scans disabled for this query only)
|
||||
QUERY PLAN
|
||||
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
Limit (actual time=2.151..2.154 rows=51 loops=1)
|
||||
Buffers: shared hit=914
|
||||
-> Sort (actual time=2.151..2.152 rows=51 loops=1)
|
||||
Sort Key: created_at, id
|
||||
Sort Method: top-N heapsort Memory: 31kB
|
||||
Buffers: shared hit=914
|
||||
-> Seq Scan on messages (actual time=0.420..1.969 rows=5000 loops=1)
|
||||
Filter: ((chat_id = '00000000-0000-0000-0001-000000000000'::uuid) AND (ROW(created_at, id) > ROW('2026-01-01 04:10:00+00'::timestamp with time zone, '5ec4eb0a-04cf-487e-9ec0-16b7367e29df'::uuid)))
|
||||
Rows Removed by Filter: 65006
|
||||
Buffers: shared hit=914
|
||||
Planning Time: 0.021 ms
|
||||
Execution Time: 2.162 ms
|
||||
(12 rows)
|
||||
|
||||
@@ -12,6 +12,7 @@ exposed = "1.0.0"
|
||||
postgres = "42.7.3"
|
||||
hikaricp = "6.3.0"
|
||||
flyway = "11.9.0"
|
||||
lettuce = "6.7.1.RELEASE"
|
||||
bouncycastle = "1.81"
|
||||
test-containers = "1.21.0"
|
||||
|
||||
@@ -45,6 +46,8 @@ hikaricp = { module = "com.zaxxer:HikariCP", version.ref = "hikaricp" }
|
||||
flyway = { module = "org.flywaydb:flyway-core", version.ref = "flyway" }
|
||||
flyway-postgresql = { module = "org.flywaydb:flyway-database-postgresql", version.ref = "flyway" }
|
||||
|
||||
lettuce = { module = "io.lettuce:lettuce-core", version.ref = "lettuce" }
|
||||
|
||||
bouncycastle = { module = "org.bouncycastle:bcprov-jdk18on", version.ref = "bouncycastle" }
|
||||
|
||||
testContainers = { module = "org.testcontainers:testcontainers", version.ref = "test-containers" }
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
redis {
|
||||
host = "localhost"
|
||||
port = 6379
|
||||
connectTimeoutMs = 500
|
||||
commandTimeoutMs = 500
|
||||
}
|
||||
Executable
+10
@@ -0,0 +1,10 @@
|
||||
#!/bin/sh
|
||||
# Runs EXPLAIN (ANALYZE, BUFFERS) of the chat history query against the Compose PostgreSQL on 70 000 synthetic
|
||||
# messages. The data is inserted and rolled back in one transaction; the plans are saved to evidence/.
|
||||
set -eu
|
||||
cd "$(dirname "$0")/.."
|
||||
|
||||
mkdir -p evidence
|
||||
|
||||
docker compose exec -T postgres psql -U messenger -d messenger -X -q < scripts/sql/explain-history.sql \
|
||||
| tee evidence/explain-history.txt
|
||||
+11
-8
@@ -6,11 +6,14 @@ cd "$(dirname "$0")/.."
|
||||
|
||||
mkdir -p secrets
|
||||
|
||||
if [ ! -s secrets/db_password ]; then
|
||||
# Readable by the non-root users inside the api/postgres containers (bind-mounted as is).
|
||||
umask 022
|
||||
openssl rand -hex 24 > secrets/db_password
|
||||
echo "created secrets/db_password"
|
||||
else
|
||||
echo "secrets/db_password already exists, keeping it"
|
||||
fi
|
||||
# Readable by the non-root users inside the containers (bind-mounted as is).
|
||||
umask 022
|
||||
|
||||
for name in db_password redis_password; do
|
||||
if [ ! -s "secrets/$name" ]; then
|
||||
openssl rand -hex 24 > "secrets/$name"
|
||||
echo "created secrets/$name"
|
||||
else
|
||||
echo "secrets/$name already exists, keeping it"
|
||||
fi
|
||||
done
|
||||
|
||||
+3
-3
@@ -1,7 +1,7 @@
|
||||
#!/bin/sh
|
||||
# Restarts the API and PostgreSQL, keeping the named volume (no `down -v`, no re-seed).
|
||||
# Restarts the API, PostgreSQL and Redis, keeping the named volume (no `down -v`, no re-seed).
|
||||
set -eu
|
||||
cd "$(dirname "$0")/.."
|
||||
|
||||
docker compose stop api postgres
|
||||
docker compose up -d --wait postgres api
|
||||
docker compose stop api redis postgres
|
||||
docker compose up -d --wait postgres redis api
|
||||
|
||||
@@ -0,0 +1,100 @@
|
||||
-- EXPLAIN (ANALYZE, BUFFERS) of the chat history query on synthetic data.
|
||||
-- Everything runs in one transaction that is rolled back: the database is left untouched.
|
||||
-- The queries are the ones ExposedMessageRepository.findPageByChatId generates (page size 50 + 1 look-ahead row).
|
||||
|
||||
\set ON_ERROR_STOP on
|
||||
\pset pager off
|
||||
|
||||
BEGIN;
|
||||
|
||||
INSERT INTO users (id, username, display_name, password_hash, created_at)
|
||||
VALUES ('00000000-0000-0000-0000-000000000001', 'explain_alice', 'Alice', 'not-a-hash', now()),
|
||||
('00000000-0000-0000-0000-000000000002', 'explain_bob', 'Bob', 'not-a-hash', now());
|
||||
|
||||
-- One large chat (20 000 messages) and 50 neighbours with 1 000 messages each: 70 000 rows in total,
|
||||
-- so the plan has to skip foreign chats instead of reading a table that only holds the target chat.
|
||||
INSERT INTO chats (id, title, created_at)
|
||||
SELECT ('00000000-0000-0000-0001-' || lpad(n::text, 12, '0'))::uuid, 'explain chat ' || n, now()
|
||||
FROM generate_series(0, 50) AS n;
|
||||
|
||||
INSERT INTO chat_members (chat_id, user_id, position)
|
||||
SELECT c.id, u.id, u.position
|
||||
FROM chats c
|
||||
CROSS JOIN (VALUES ('00000000-0000-0000-0000-000000000001'::uuid, 0),
|
||||
('00000000-0000-0000-0000-000000000002'::uuid, 1)) AS u (id, position)
|
||||
WHERE c.title LIKE 'explain chat %';
|
||||
|
||||
INSERT INTO messages (id, chat_id, sender_id, text, created_at)
|
||||
SELECT gen_random_uuid(),
|
||||
'00000000-0000-0000-0001-000000000000',
|
||||
CASE WHEN n % 2 = 0 THEN '00000000-0000-0000-0000-000000000001'::uuid
|
||||
ELSE '00000000-0000-0000-0000-000000000002'::uuid END,
|
||||
'message #' || n,
|
||||
timestamptz '2026-01-01 00:00:00+00' + n * interval '1 second'
|
||||
FROM generate_series(1, 20000) AS n;
|
||||
|
||||
INSERT INTO messages (id, chat_id, sender_id, text, created_at)
|
||||
SELECT gen_random_uuid(),
|
||||
('00000000-0000-0000-0001-' || lpad(c::text, 12, '0'))::uuid,
|
||||
'00000000-0000-0000-0000-000000000001',
|
||||
'neighbour message #' || n,
|
||||
timestamptz '2026-01-01 00:00:00+00' + n * interval '1 second'
|
||||
FROM generate_series(1, 50) AS c, generate_series(1, 1000) AS n;
|
||||
|
||||
ANALYZE users, chats, chat_members, messages;
|
||||
|
||||
\echo
|
||||
\echo '== Data volume'
|
||||
SELECT count(*) AS total_messages,
|
||||
count(*) FILTER (WHERE chat_id = '00000000-0000-0000-0001-000000000000') AS target_chat_messages
|
||||
FROM messages;
|
||||
|
||||
-- Cursor of the 15 000th message: a deep page, where OFFSET pagination would have to skip 15 000 rows.
|
||||
SELECT created_at AS cursor_created_at, id AS cursor_id
|
||||
FROM messages
|
||||
WHERE chat_id = '00000000-0000-0000-0001-000000000000'
|
||||
ORDER BY created_at, id
|
||||
OFFSET 14999 LIMIT 1 \gset
|
||||
|
||||
\echo
|
||||
\echo '== 1. First page: WHERE chat_id = ? ORDER BY created_at, id LIMIT 51'
|
||||
EXPLAIN (ANALYZE, BUFFERS, COSTS OFF)
|
||||
SELECT messages.id, messages.chat_id, messages.sender_id, messages.text, messages.created_at
|
||||
FROM messages
|
||||
WHERE messages.chat_id = '00000000-0000-0000-0001-000000000000'
|
||||
ORDER BY messages.created_at ASC, messages.id ASC
|
||||
LIMIT 51;
|
||||
|
||||
\echo
|
||||
\echo '== 2. Deep page after the 15 000th message: (created_at, id) > (?, ?)'
|
||||
EXPLAIN (ANALYZE, BUFFERS, COSTS OFF)
|
||||
SELECT messages.id, messages.chat_id, messages.sender_id, messages.text, messages.created_at
|
||||
FROM messages
|
||||
WHERE (messages.chat_id = '00000000-0000-0000-0001-000000000000')
|
||||
AND ((messages.created_at, messages.id) > (:'cursor_created_at', :'cursor_id'))
|
||||
ORDER BY messages.created_at ASC, messages.id ASC
|
||||
LIMIT 51;
|
||||
|
||||
\echo
|
||||
\echo '== 3. Same deep page with OFFSET instead of a cursor (for comparison, not used by the API)'
|
||||
EXPLAIN (ANALYZE, BUFFERS, COSTS OFF)
|
||||
SELECT messages.id, messages.chat_id, messages.sender_id, messages.text, messages.created_at
|
||||
FROM messages
|
||||
WHERE messages.chat_id = '00000000-0000-0000-0001-000000000000'
|
||||
ORDER BY messages.created_at ASC, messages.id ASC
|
||||
LIMIT 51 OFFSET 15000;
|
||||
|
||||
\echo
|
||||
\echo '== 4. Deep page without messages_chat_id_created_at_id_idx (index scans disabled for this query only)'
|
||||
SET LOCAL enable_indexscan = off;
|
||||
SET LOCAL enable_bitmapscan = off;
|
||||
SET LOCAL enable_indexonlyscan = off;
|
||||
EXPLAIN (ANALYZE, BUFFERS, COSTS OFF)
|
||||
SELECT messages.id, messages.chat_id, messages.sender_id, messages.text, messages.created_at
|
||||
FROM messages
|
||||
WHERE (messages.chat_id = '00000000-0000-0000-0001-000000000000')
|
||||
AND ((messages.created_at, messages.id) > (:'cursor_created_at', :'cursor_id'))
|
||||
ORDER BY messages.created_at ASC, messages.id ASC
|
||||
LIMIT 51;
|
||||
|
||||
ROLLBACK;
|
||||
@@ -6,6 +6,7 @@ import com.mtusi.messenger.main.plugins.configureContentNegotiation
|
||||
import com.mtusi.messenger.main.plugins.configureDatabase
|
||||
import com.mtusi.messenger.main.plugins.configureDefaultHeaders
|
||||
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
|
||||
@@ -18,6 +19,7 @@ fun Application.module() {
|
||||
|
||||
configureKoin()
|
||||
configureDatabase()
|
||||
configureRedis()
|
||||
|
||||
configureContentNegotiation()
|
||||
configureDefaultHeaders()
|
||||
|
||||
@@ -4,5 +4,6 @@ interface AuthenticationConfig {
|
||||
|
||||
val realm: String
|
||||
val sessionTtlSeconds: Long
|
||||
val sessionCacheTtlSeconds: Long
|
||||
|
||||
}
|
||||
|
||||
+2
-1
@@ -5,5 +5,6 @@ import kotlinx.serialization.Serializable
|
||||
@Serializable
|
||||
data class SerialAuthenticationConfig(
|
||||
override val realm: String,
|
||||
override val sessionTtlSeconds: Long
|
||||
override val sessionTtlSeconds: Long,
|
||||
override val sessionCacheTtlSeconds: Long
|
||||
) : AuthenticationConfig
|
||||
|
||||
@@ -12,6 +12,7 @@ interface DatabaseConfig {
|
||||
|
||||
val maxPoolSize: Int
|
||||
val connectionTimeoutMs: Long
|
||||
val statementTimeoutMs: Long
|
||||
|
||||
val jdbcConnectionUrl: String
|
||||
get() = "jdbc:postgresql://$host:$port/$name"
|
||||
|
||||
@@ -11,5 +11,6 @@ data class SerialDatabaseConfig(
|
||||
override val password: String? = null,
|
||||
override val passwordFile: String? = null,
|
||||
override val maxPoolSize: Int,
|
||||
override val connectionTimeoutMs: Long
|
||||
override val connectionTimeoutMs: Long,
|
||||
override val statementTimeoutMs: Long
|
||||
) : DatabaseConfig
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
package com.mtusi.messenger.main.config.redis
|
||||
|
||||
import io.ktor.server.application.Application
|
||||
import io.ktor.server.config.getAs
|
||||
import org.koin.core.annotation.Provided
|
||||
import org.koin.core.annotation.Single
|
||||
|
||||
@Single
|
||||
class DefaultRedisConfig(
|
||||
@Provided app: Application
|
||||
) : RedisConfig by app.environment.config.property("redis").getAs<SerialRedisConfig>()
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.mtusi.messenger.main.config.redis
|
||||
|
||||
interface RedisConfig {
|
||||
|
||||
val host: String
|
||||
val port: Int
|
||||
|
||||
val username: String?
|
||||
val password: String?
|
||||
val passwordFile: String?
|
||||
|
||||
val connectTimeoutMs: Long
|
||||
val commandTimeoutMs: Long
|
||||
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.mtusi.messenger.main.config.redis
|
||||
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
@Serializable
|
||||
data class SerialRedisConfig(
|
||||
override val host: String,
|
||||
override val port: Int,
|
||||
override val username: String? = null,
|
||||
override val password: String? = null,
|
||||
override val passwordFile: String? = null,
|
||||
override val connectTimeoutMs: Long,
|
||||
override val commandTimeoutMs: Long
|
||||
) : RedisConfig
|
||||
+1
-3
@@ -44,9 +44,7 @@ fun Routing.configureAuthenticationRouting() = route("/api/v1/auth/sessions") {
|
||||
authenticate(SESSION_AUTH) {
|
||||
delete("current") {
|
||||
|
||||
suspendedTransaction {
|
||||
authenticationService.revokeSession(call.currentUser.sessionId)
|
||||
}
|
||||
authenticationService.revokeSession(call.currentUser.session)
|
||||
|
||||
call.respond(HttpStatusCode.NoContent)
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import kotlin.time.Instant
|
||||
data class Session(
|
||||
val id: UUID,
|
||||
val userId: UUID,
|
||||
val tokenHash: String,
|
||||
val createdAt: Instant,
|
||||
val expiresAt: Instant,
|
||||
)
|
||||
|
||||
+88
@@ -0,0 +1,88 @@
|
||||
package com.mtusi.messenger.main.infrastructure.repositories._common
|
||||
|
||||
import com.mtusi.messenger.main.config.redis.RedisConfig
|
||||
import com.mtusi.messenger.main.utils.resolveSecret
|
||||
import io.lettuce.core.ClientOptions
|
||||
import io.lettuce.core.RedisClient
|
||||
import io.lettuce.core.RedisConnectionException
|
||||
import io.lettuce.core.RedisURI
|
||||
import io.lettuce.core.SocketOptions
|
||||
import io.lettuce.core.TimeoutOptions
|
||||
import io.lettuce.core.api.StatefulRedisConnection
|
||||
import io.lettuce.core.api.async.RedisAsyncCommands
|
||||
import io.lettuce.core.codec.StringCodec
|
||||
import kotlinx.coroutines.future.await
|
||||
import kotlinx.coroutines.sync.Mutex
|
||||
import kotlinx.coroutines.sync.withLock
|
||||
import org.koin.core.annotation.Single
|
||||
import java.time.Duration
|
||||
import kotlin.time.Duration.Companion.seconds
|
||||
import kotlin.time.TimeSource
|
||||
|
||||
private val RECONNECT_BACKOFF = 1.seconds
|
||||
|
||||
/**
|
||||
* Lazily connected Redis client. The application must start and serve requests without Redis, so the connection
|
||||
* is opened on first use, a failed attempt is not retried for [RECONNECT_BACKOFF], and commands issued while
|
||||
* disconnected fail immediately instead of queueing behind the reconnect.
|
||||
*/
|
||||
@Single
|
||||
class RedisConnection(
|
||||
config: RedisConfig
|
||||
) : AutoCloseable {
|
||||
|
||||
private val uri = RedisURI.builder()
|
||||
.withHost(config.host)
|
||||
.withPort(config.port)
|
||||
.withTimeout(Duration.ofMillis(config.commandTimeoutMs))
|
||||
.apply {
|
||||
val password = resolveSecret(config.password, config.passwordFile)
|
||||
if (password != null) {
|
||||
if (config.username != null) withAuthentication(config.username, password) else withPassword(password.toCharArray())
|
||||
}
|
||||
}
|
||||
.build()
|
||||
|
||||
private val client = RedisClient.create().apply {
|
||||
options = ClientOptions.builder()
|
||||
.autoReconnect(true)
|
||||
.disconnectedBehavior(ClientOptions.DisconnectedBehavior.REJECT_COMMANDS)
|
||||
.timeoutOptions(TimeoutOptions.enabled(Duration.ofMillis(config.commandTimeoutMs)))
|
||||
.socketOptions(SocketOptions.builder().connectTimeout(Duration.ofMillis(config.connectTimeoutMs)).build())
|
||||
.build()
|
||||
}
|
||||
|
||||
private val mutex = Mutex()
|
||||
|
||||
@Volatile
|
||||
private var connection: StatefulRedisConnection<String, String>? = null
|
||||
|
||||
@Volatile
|
||||
private var nextAttempt = TimeSource.Monotonic.markNow()
|
||||
|
||||
suspend fun commands(): RedisAsyncCommands<String, String> {
|
||||
return (connection ?: connect()).async()
|
||||
}
|
||||
|
||||
private suspend fun connect(): StatefulRedisConnection<String, String> = mutex.withLock {
|
||||
|
||||
connection?.let { return it }
|
||||
|
||||
if (nextAttempt.hasNotPassedNow()) {
|
||||
throw RedisConnectionException("Redis is unavailable, next connection attempt is postponed")
|
||||
}
|
||||
|
||||
return try {
|
||||
client.connectAsync(StringCodec.UTF8, uri).await().also { connection = it }
|
||||
} catch (e: Exception) {
|
||||
nextAttempt = TimeSource.Monotonic.markNow() + RECONNECT_BACKOFF
|
||||
throw e as? RedisConnectionException ?: RedisConnectionException("Unable to connect to Redis", e)
|
||||
}
|
||||
}
|
||||
|
||||
override fun close() {
|
||||
connection?.close()
|
||||
client.shutdown()
|
||||
}
|
||||
|
||||
}
|
||||
+9
-3
@@ -13,6 +13,7 @@ import org.jetbrains.exposed.v1.jdbc.selectAll
|
||||
import org.koin.core.annotation.Single
|
||||
import java.util.UUID
|
||||
import kotlin.time.Clock
|
||||
import kotlin.time.Duration
|
||||
import kotlin.time.Instant
|
||||
|
||||
@Single
|
||||
@@ -21,20 +22,24 @@ class ExposedSessionRepository : SessionRepository {
|
||||
override fun create(
|
||||
userId: UUID,
|
||||
tokenHash: String,
|
||||
expiresAt: Instant
|
||||
ttl: Duration
|
||||
): Session {
|
||||
|
||||
val createdAt = Clock.System.nowMicros()
|
||||
val expiresAt = createdAt + ttl
|
||||
|
||||
val insertedRow = SessionModel.insert {
|
||||
it[this.userId] = userId
|
||||
it[this.tokenHash] = tokenHash
|
||||
it[this.createdAt] = Clock.System.nowMicros()
|
||||
it[this.createdAt] = createdAt
|
||||
it[this.expiresAt] = expiresAt
|
||||
}
|
||||
|
||||
return Session(
|
||||
id = insertedRow[SessionModel.id].value,
|
||||
userId = userId,
|
||||
createdAt = insertedRow[SessionModel.createdAt],
|
||||
tokenHash = tokenHash,
|
||||
createdAt = createdAt,
|
||||
expiresAt = expiresAt
|
||||
)
|
||||
}
|
||||
@@ -60,6 +65,7 @@ class ExposedSessionRepository : SessionRepository {
|
||||
private fun ResultRow.toSession() = Session(
|
||||
id = this[SessionModel.id].value,
|
||||
userId = this[SessionModel.userId].value,
|
||||
tokenHash = this[SessionModel.tokenHash],
|
||||
createdAt = this[SessionModel.createdAt],
|
||||
expiresAt = this[SessionModel.expiresAt]
|
||||
)
|
||||
|
||||
+2
-1
@@ -3,6 +3,7 @@ package com.mtusi.messenger.main.infrastructure.repositories.authentication.sess
|
||||
import com.mtusi.messenger.main.infrastructure.dto.authentication.Session
|
||||
import com.mtusi.messenger.main.infrastructure.repositories.Repository
|
||||
import java.util.UUID
|
||||
import kotlin.time.Duration
|
||||
import kotlin.time.Instant
|
||||
|
||||
interface SessionRepository : Repository<UUID, Session> {
|
||||
@@ -10,7 +11,7 @@ interface SessionRepository : Repository<UUID, Session> {
|
||||
fun create(
|
||||
userId: UUID,
|
||||
tokenHash: String,
|
||||
expiresAt: Instant
|
||||
ttl: Duration
|
||||
): Session
|
||||
|
||||
fun findActiveByTokenHash(
|
||||
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
package com.mtusi.messenger.main.infrastructure.repositories.authentication.session.cache
|
||||
|
||||
import com.mtusi.messenger.main.infrastructure.dto.authentication.Session
|
||||
|
||||
sealed class CachedSession {
|
||||
|
||||
data class Active(val session: Session) : CachedSession()
|
||||
|
||||
data object Revoked : CachedSession()
|
||||
|
||||
}
|
||||
+86
@@ -0,0 +1,86 @@
|
||||
package com.mtusi.messenger.main.infrastructure.repositories.authentication.session.cache
|
||||
|
||||
import com.mtusi.messenger.main.compat.serializers.SerialUUID
|
||||
import com.mtusi.messenger.main.infrastructure.dto.authentication.Session
|
||||
import com.mtusi.messenger.main.infrastructure.repositories._common.RedisConnection
|
||||
import io.lettuce.core.SetArgs
|
||||
import kotlinx.coroutines.future.await
|
||||
import kotlinx.serialization.Serializable
|
||||
import kotlinx.serialization.json.Json
|
||||
import org.koin.core.annotation.Single
|
||||
import kotlin.time.Instant
|
||||
|
||||
private const val KEY_PREFIX = "session:"
|
||||
private const val REVOKED = "revoked"
|
||||
|
||||
@Single
|
||||
class RedisSessionCacheRepository(
|
||||
private val redis: RedisConnection
|
||||
) : SessionCacheRepository {
|
||||
|
||||
override suspend fun find(
|
||||
tokenHash: String
|
||||
): CachedSession? {
|
||||
|
||||
val value = redis.commands().get(key(tokenHash)).await() ?: return null
|
||||
|
||||
if (value == REVOKED) {
|
||||
return CachedSession.Revoked
|
||||
}
|
||||
|
||||
return CachedSession.Active(
|
||||
Json.decodeFromString<SessionEntry>(value).toSession(tokenHash)
|
||||
)
|
||||
}
|
||||
|
||||
override suspend fun putIfAbsent(
|
||||
session: Session,
|
||||
cachedUntil: Instant
|
||||
) {
|
||||
redis.commands().set(
|
||||
key(session.tokenHash),
|
||||
Json.encodeToString(SessionEntry.from(session)),
|
||||
SetArgs.Builder.nx().pxAt(cachedUntil.toEpochMilliseconds())
|
||||
).await()
|
||||
}
|
||||
|
||||
override suspend fun revoke(
|
||||
session: Session
|
||||
) {
|
||||
redis.commands().set(
|
||||
key(session.tokenHash),
|
||||
REVOKED,
|
||||
SetArgs.Builder.pxAt(session.expiresAt.toEpochMilliseconds())
|
||||
).await()
|
||||
}
|
||||
|
||||
private fun key(tokenHash: String) = KEY_PREFIX + tokenHash
|
||||
|
||||
@Serializable
|
||||
private data class SessionEntry(
|
||||
val id: SerialUUID,
|
||||
val userId: SerialUUID,
|
||||
val createdAt: Instant,
|
||||
val expiresAt: Instant
|
||||
) {
|
||||
|
||||
fun toSession(tokenHash: String) = Session(
|
||||
id = id,
|
||||
userId = userId,
|
||||
tokenHash = tokenHash,
|
||||
createdAt = createdAt,
|
||||
expiresAt = expiresAt
|
||||
)
|
||||
|
||||
companion object {
|
||||
fun from(session: Session) = SessionEntry(
|
||||
id = session.id,
|
||||
userId = session.userId,
|
||||
createdAt = session.createdAt,
|
||||
expiresAt = session.expiresAt
|
||||
)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
package com.mtusi.messenger.main.infrastructure.repositories.authentication.session.cache
|
||||
|
||||
import com.mtusi.messenger.main.infrastructure.dto.authentication.Session
|
||||
import com.mtusi.messenger.main.infrastructure.repositories.Repository
|
||||
import kotlin.time.Instant
|
||||
|
||||
/**
|
||||
* 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<String, Session> {
|
||||
|
||||
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 the session would have expired.
|
||||
*/
|
||||
suspend fun revoke(
|
||||
session: Session
|
||||
)
|
||||
|
||||
}
|
||||
+10
-3
@@ -16,12 +16,19 @@ interface AuthenticationService {
|
||||
userId: UUID
|
||||
): IssuedSession
|
||||
|
||||
fun findSessionByToken(
|
||||
/**
|
||||
* Opens its own transaction only on a cache miss, so it must not be called inside one.
|
||||
*/
|
||||
suspend fun findSessionByToken(
|
||||
token: String
|
||||
): Session?
|
||||
|
||||
fun revokeSession(
|
||||
sessionId: UUID
|
||||
/**
|
||||
* 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
|
||||
|
||||
}
|
||||
|
||||
+67
-10
@@ -5,16 +5,21 @@ import com.mtusi.messenger.main.infrastructure.dto.User
|
||||
import com.mtusi.messenger.main.infrastructure.dto.authentication.IssuedSession
|
||||
import com.mtusi.messenger.main.infrastructure.dto.authentication.Session
|
||||
import com.mtusi.messenger.main.infrastructure.repositories.authentication.session.SessionRepository
|
||||
import com.mtusi.messenger.main.infrastructure.repositories.authentication.session.cache.CachedSession
|
||||
import com.mtusi.messenger.main.infrastructure.repositories.authentication.session.cache.SessionCacheRepository
|
||||
import com.mtusi.messenger.main.infrastructure.repositories.user.UserRepository
|
||||
import com.mtusi.messenger.main.infrastructure.services.hashing.HashingService
|
||||
import com.mtusi.messenger.main.infrastructure.services.hashing.types.HashingAlgorithm
|
||||
import com.mtusi.messenger.main.plugins.suspendedTransaction
|
||||
import com.mtusi.messenger.main.utils.computeSha256
|
||||
import com.mtusi.messenger.main.utils.nowMicros
|
||||
import io.lettuce.core.RedisException
|
||||
import org.koin.core.annotation.Single
|
||||
import org.slf4j.LoggerFactory
|
||||
import java.security.SecureRandom
|
||||
import java.util.Base64
|
||||
import java.util.UUID
|
||||
import kotlin.time.Clock
|
||||
import kotlin.time.Instant
|
||||
import kotlin.time.Duration.Companion.seconds
|
||||
|
||||
private const val TOKEN_BYTES = 32
|
||||
@@ -23,10 +28,13 @@ private const val TOKEN_BYTES = 32
|
||||
class DefaultAuthenticationService(
|
||||
private val userRepository: UserRepository,
|
||||
private val sessionRepository: SessionRepository,
|
||||
private val sessionCacheRepository: SessionCacheRepository,
|
||||
private val hashingService: HashingService,
|
||||
private val config: AuthenticationConfig
|
||||
) : AuthenticationService {
|
||||
|
||||
private val logger = LoggerFactory.getLogger(DefaultAuthenticationService::class.java)
|
||||
|
||||
private val random = SecureRandom()
|
||||
|
||||
private val dummyPasswordHash by lazy {
|
||||
@@ -64,7 +72,7 @@ class DefaultAuthenticationService(
|
||||
val session = sessionRepository.create(
|
||||
userId = userId,
|
||||
tokenHash = token.toByteArray().computeSha256(),
|
||||
expiresAt = Clock.System.nowMicros() + config.sessionTtlSeconds.seconds
|
||||
ttl = config.sessionTtlSeconds.seconds
|
||||
)
|
||||
|
||||
return IssuedSession(
|
||||
@@ -73,19 +81,68 @@ class DefaultAuthenticationService(
|
||||
)
|
||||
}
|
||||
|
||||
override fun findSessionByToken(
|
||||
override suspend fun findSessionByToken(
|
||||
token: String
|
||||
): Session? {
|
||||
return sessionRepository.findActiveByTokenHash(
|
||||
tokenHash = token.toByteArray().computeSha256(),
|
||||
now = Clock.System.now()
|
||||
)
|
||||
|
||||
val tokenHash = token.toByteArray().computeSha256()
|
||||
val now = Clock.System.now()
|
||||
|
||||
when (val cached = findCachedSession(tokenHash)) {
|
||||
is CachedSession.Revoked -> return null
|
||||
is CachedSession.Active -> return cached.session.takeIf { it.expiresAt > now }
|
||||
null -> Unit
|
||||
}
|
||||
|
||||
val session = suspendedTransaction {
|
||||
sessionRepository.findActiveByTokenHash(tokenHash, now)
|
||||
} ?: return null
|
||||
|
||||
cacheSession(session, now)
|
||||
|
||||
return session
|
||||
}
|
||||
|
||||
override fun revokeSession(
|
||||
sessionId: UUID
|
||||
override suspend fun revokeSession(
|
||||
session: Session
|
||||
): Boolean {
|
||||
return sessionRepository.deleteById(sessionId)
|
||||
|
||||
val isDeleted = suspendedTransaction {
|
||||
sessionRepository.deleteById(session.id)
|
||||
}
|
||||
|
||||
try {
|
||||
sessionCacheRepository.revoke(session)
|
||||
} catch (e: RedisException) {
|
||||
logger.warn("Session cache is unavailable, revoked session is not tombstoned: ${e.message}")
|
||||
}
|
||||
|
||||
return isDeleted
|
||||
}
|
||||
|
||||
private suspend fun findCachedSession(
|
||||
tokenHash: String
|
||||
): CachedSession? {
|
||||
return try {
|
||||
sessionCacheRepository.find(tokenHash)
|
||||
} catch (e: RedisException) {
|
||||
logger.warn("Session cache is unavailable, reading from the database: ${e.message}")
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun cacheSession(
|
||||
session: Session,
|
||||
now: Instant
|
||||
) {
|
||||
try {
|
||||
sessionCacheRepository.putIfAbsent(
|
||||
session = session,
|
||||
cachedUntil = minOf(session.expiresAt, now + config.sessionCacheTtlSeconds.seconds)
|
||||
)
|
||||
} catch (e: RedisException) {
|
||||
logger.warn("Session cache is unavailable, session is not cached: ${e.message}")
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ 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.MessageModel
|
||||
import com.mtusi.messenger.main.infrastructure.models.user.UserModel
|
||||
import com.mtusi.messenger.main.utils.resolveSecret
|
||||
import com.typesafe.config.ConfigFactory
|
||||
import com.zaxxer.hikari.HikariConfig
|
||||
import com.zaxxer.hikari.HikariDataSource
|
||||
@@ -30,6 +31,7 @@ import javax.sql.DataSource
|
||||
|
||||
private const val MIGRATIONS_DIRECTORY = "src/main/resources/migrations"
|
||||
private const val DEVELOPMENT_DATABASE_CONFIG = "run/development/config/database.conf"
|
||||
private const val VALIDATION_TIMEOUT_MS = 1000L
|
||||
|
||||
fun Application.configureDatabase() {
|
||||
|
||||
@@ -57,19 +59,15 @@ private fun DatabaseConfig.createDataSource(): HikariDataSource {
|
||||
poolName = "messenger"
|
||||
jdbcUrl = jdbcConnectionUrl
|
||||
username = this@createDataSource.username
|
||||
password = resolvePassword()
|
||||
password = resolveSecret(this@createDataSource.password, passwordFile).orEmpty()
|
||||
maximumPoolSize = maxPoolSize
|
||||
connectionTimeout = connectionTimeoutMs
|
||||
validationTimeout = minOf(VALIDATION_TIMEOUT_MS, connectionTimeoutMs - 1)
|
||||
addDataSourceProperty("options", "-c statement_timeout=$statementTimeoutMs")
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
private fun DatabaseConfig.resolvePassword(): String {
|
||||
return password
|
||||
?: passwordFile?.let { File(it).readText().trim() }
|
||||
?: ""
|
||||
}
|
||||
|
||||
fun applyDatabaseMigrations(
|
||||
dataSource: DataSource
|
||||
) {
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.mtusi.messenger.main.plugins
|
||||
|
||||
import com.mtusi.messenger.main.infrastructure.repositories._common.RedisConnection
|
||||
import io.ktor.server.application.Application
|
||||
import io.ktor.server.application.ApplicationStopped
|
||||
import org.koin.ktor.ext.get
|
||||
|
||||
fun Application.configureRedis() {
|
||||
|
||||
val redisConnection = get<RedisConnection>()
|
||||
|
||||
monitor.subscribe(ApplicationStopped) {
|
||||
redisConnection.close()
|
||||
}
|
||||
|
||||
}
|
||||
@@ -12,12 +12,16 @@ import io.ktor.server.plugins.BadRequestException
|
||||
import io.ktor.server.plugins.NotFoundException
|
||||
import io.ktor.server.plugins.UnsupportedMediaTypeException
|
||||
import io.ktor.server.plugins.statuspages.StatusPages
|
||||
import io.lettuce.core.RedisException
|
||||
import kotlinx.serialization.SerializationException
|
||||
import java.sql.SQLException
|
||||
import java.sql.SQLTransientConnectionException
|
||||
|
||||
private const val CONNECTION_ERROR_SQL_STATE_CLASS = "08"
|
||||
|
||||
// admin_shutdown, crash_shutdown, cannot_connect_now: the server is going down or still starting.
|
||||
private const val SERVER_SHUTDOWN_SQL_STATE_PREFIX = "57P"
|
||||
|
||||
fun Application.configureStatusPages() = install(StatusPages) {
|
||||
exception<ApiException> { call, cause ->
|
||||
call.respondError(cause.code, cause.message ?: cause.code.wire)
|
||||
@@ -46,6 +50,10 @@ fun Application.configureStatusPages() = install(StatusPages) {
|
||||
call.respondError(ErrorCode.INTERNAL_ERROR, "internal error")
|
||||
}
|
||||
}
|
||||
exception<RedisException> { call, cause ->
|
||||
call.application.log.warn("Redis unavailable: ${cause.message}")
|
||||
call.respondError(ErrorCode.UNAVAILABLE, "service temporarily unavailable")
|
||||
}
|
||||
exception<Throwable> { call, cause ->
|
||||
call.application.log.error("Unhandled error", cause)
|
||||
call.respondError(ErrorCode.INTERNAL_ERROR, "internal error")
|
||||
@@ -61,6 +69,10 @@ fun Application.configureStatusPages() = install(StatusPages) {
|
||||
private fun SQLException.isConnectionFailure(): Boolean {
|
||||
return generateSequence<Throwable>(this) { it.cause }.any {
|
||||
it is SQLTransientConnectionException ||
|
||||
(it is SQLException && it.sqlState?.startsWith(CONNECTION_ERROR_SQL_STATE_CLASS) == true)
|
||||
(it is SQLException && it.sqlState.isUnavailableState())
|
||||
}
|
||||
}
|
||||
|
||||
private fun String?.isUnavailableState(): Boolean {
|
||||
return this != null && (startsWith(CONNECTION_ERROR_SQL_STATE_CLASS) || startsWith(SERVER_SHUTDOWN_SQL_STATE_PREFIX))
|
||||
}
|
||||
|
||||
@@ -32,14 +32,9 @@ fun Application.configureAuthentication() {
|
||||
|
||||
bearerSession(SESSION_AUTH) {
|
||||
authenticate { token ->
|
||||
val session = suspendedTransaction {
|
||||
authenticationService.findSessionByToken(token)
|
||||
} ?: return@authenticate null
|
||||
val session = authenticationService.findSessionByToken(token) ?: return@authenticate null
|
||||
|
||||
return@authenticate UserPrincipal(
|
||||
id = session.userId,
|
||||
sessionId = session.id
|
||||
)
|
||||
return@authenticate UserPrincipal(session)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,8 +1,13 @@
|
||||
package com.mtusi.messenger.main.plugins.authentication
|
||||
|
||||
import com.mtusi.messenger.main.infrastructure.dto.authentication.Session
|
||||
import java.util.UUID
|
||||
|
||||
data class UserPrincipal(
|
||||
val id: UUID,
|
||||
val sessionId: UUID
|
||||
)
|
||||
val session: Session
|
||||
) {
|
||||
|
||||
val id: UUID
|
||||
get() = session.userId
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
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?
|
||||
): String? {
|
||||
return value ?: file?.let { File(it).readText().trim() }
|
||||
}
|
||||
@@ -27,10 +27,23 @@ database {
|
||||
maxPoolSize = 10
|
||||
maxPoolSize = ${?DB_MAX_POOL_SIZE}
|
||||
connectionTimeoutMs = 2000
|
||||
statementTimeoutMs = 5000
|
||||
}
|
||||
|
||||
redis {
|
||||
host = "localhost"
|
||||
host = ${?REDIS_HOST}
|
||||
port = 6379
|
||||
port = ${?REDIS_PORT}
|
||||
password = ${?REDIS_PASSWORD}
|
||||
passwordFile = ${?REDIS_PASSWORD_FILE}
|
||||
connectTimeoutMs = 500
|
||||
commandTimeoutMs = 500
|
||||
}
|
||||
|
||||
authentication {
|
||||
realm = "messenger"
|
||||
sessionTtlSeconds = 3600
|
||||
sessionTtlSeconds = ${?SESSION_TTL_SECONDS}
|
||||
sessionCacheTtlSeconds = 60
|
||||
}
|
||||
|
||||
+83
@@ -0,0 +1,83 @@
|
||||
package com.mtusi.messenger.main.infrastructure.repositories.authentication.session.cache
|
||||
|
||||
import com.mtusi.messenger.main.config.redis.SerialRedisConfig
|
||||
import com.mtusi.messenger.main.infrastructure.dto.authentication.Session
|
||||
import com.mtusi.messenger.main.infrastructure.repositories._common.RedisConnection
|
||||
import com.mtusi.messenger.main.support.TestRedis
|
||||
import com.mtusi.messenger.main.utils.nowMicros
|
||||
import kotlinx.coroutines.test.runTest
|
||||
import java.util.UUID
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertNull
|
||||
import kotlin.test.assertTrue
|
||||
import kotlin.time.Clock
|
||||
import kotlin.time.Duration
|
||||
import kotlin.time.Duration.Companion.minutes
|
||||
|
||||
class RedisSessionCacheRepositoryTest {
|
||||
|
||||
private fun withRepository(block: suspend (RedisSessionCacheRepository) -> Unit) = runTest {
|
||||
val endpoint = TestRedis.endpoint()
|
||||
val connection = RedisConnection(
|
||||
SerialRedisConfig(
|
||||
host = endpoint.host,
|
||||
port = endpoint.port,
|
||||
password = TestRedis.PASSWORD,
|
||||
connectTimeoutMs = 2000,
|
||||
commandTimeoutMs = 2000
|
||||
)
|
||||
)
|
||||
connection.use { block(RedisSessionCacheRepository(it)) }
|
||||
}
|
||||
|
||||
private fun session(ttl: Duration = 10.minutes): Session {
|
||||
val now = Clock.System.nowMicros()
|
||||
return Session(
|
||||
id = UUID.randomUUID(),
|
||||
userId = UUID.randomUUID(),
|
||||
tokenHash = UUID.randomUUID().toString().replace("-", ""),
|
||||
createdAt = now,
|
||||
expiresAt = now + ttl
|
||||
)
|
||||
}
|
||||
|
||||
private fun ttlSeconds(session: Session): Long {
|
||||
return TestRedis.cli(TestRedis.container, "TTL", "session:${session.tokenHash}").toLong()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `cached session round-trips and expires together with the session`() = withRepository { repository ->
|
||||
val session = session()
|
||||
|
||||
assertNull(repository.find(session.tokenHash))
|
||||
|
||||
repository.putIfAbsent(session, cachedUntil = session.expiresAt)
|
||||
|
||||
assertEquals(CachedSession.Active(session), repository.find(session.tokenHash))
|
||||
assertTrue(ttlSeconds(session) in 590..600, "key must expire at cachedUntil")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `revoke replaces a cached session with a tombstone`() = withRepository { repository ->
|
||||
val session = session()
|
||||
repository.putIfAbsent(session, cachedUntil = session.expiresAt)
|
||||
|
||||
repository.revoke(session)
|
||||
|
||||
assertEquals(CachedSession.Revoked, repository.find(session.tokenHash))
|
||||
assertTrue(ttlSeconds(session) in 590..600, "tombstone must live until the session would expire")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `late cache fill cannot overwrite a tombstone`() = withRepository { repository ->
|
||||
val session = session()
|
||||
|
||||
// A lookup read the session from PostgreSQL before logout and stores it after the logout finished.
|
||||
repository.revoke(session)
|
||||
repository.putIfAbsent(session, cachedUntil = session.expiresAt)
|
||||
|
||||
assertEquals(CachedSession.Revoked, repository.find(session.tokenHash))
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,132 @@
|
||||
package com.mtusi.messenger.main.plugins
|
||||
|
||||
import com.mtusi.messenger.main.extensions.ErrorBody
|
||||
import com.mtusi.messenger.main.support.TestDatabase
|
||||
import com.mtusi.messenger.main.support.TestRedis
|
||||
import com.mtusi.messenger.main.support.auth
|
||||
import com.mtusi.messenger.main.support.createChat
|
||||
import com.mtusi.messenger.main.support.register
|
||||
import com.mtusi.messenger.main.support.runApp
|
||||
import com.mtusi.messenger.main.utils.computeSha256
|
||||
import io.ktor.client.HttpClient
|
||||
import io.ktor.client.call.body
|
||||
import io.ktor.client.request.basicAuth
|
||||
import io.ktor.client.request.delete
|
||||
import io.ktor.client.request.get
|
||||
import io.ktor.client.request.header
|
||||
import io.ktor.client.request.post
|
||||
import io.ktor.client.statement.HttpResponse
|
||||
import io.ktor.http.HttpStatusCode
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertTrue
|
||||
import kotlin.time.Duration.Companion.seconds
|
||||
import kotlin.time.TimeSource
|
||||
|
||||
class DependencyFailureTest {
|
||||
|
||||
/**
|
||||
* An unknown token can only be checked against PostgreSQL, so without it the answer is 503, never a success.
|
||||
*/
|
||||
private suspend fun HttpClient.assertAnonymousRejected(
|
||||
unknownTokenStatus: HttpStatusCode = HttpStatusCode.Unauthorized
|
||||
) {
|
||||
assertEquals(HttpStatusCode.Unauthorized, get("/api/v1/users/me").status)
|
||||
assertEquals(HttpStatusCode.Unauthorized, get("/api/v1/users/me") { header("X-User-Id", "1") }.status)
|
||||
assertEquals(
|
||||
unknownTokenStatus,
|
||||
get("/api/v1/users/me") { header("Authorization", "Bearer never-issued-token") }.status
|
||||
)
|
||||
}
|
||||
|
||||
private suspend fun assertUnavailable(response: HttpResponse) {
|
||||
assertEquals(HttpStatusCode.ServiceUnavailable, response.status)
|
||||
assertEquals("unavailable", response.body<ErrorBody>().error.code)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `without Redis requests bypass the cache`() = runApp(
|
||||
redis = TestRedis.unreachableEndpoint()
|
||||
) { client ->
|
||||
assertEquals(HttpStatusCode.OK, client.get("/health/ready").status)
|
||||
|
||||
val alice = client.register("alice")
|
||||
val bob = client.register("bob")
|
||||
|
||||
assertEquals(HttpStatusCode.OK, client.get("/api/v1/users/me") { auth(alice) }.status)
|
||||
client.createChat(alice, "no redis", listOf(alice.id, bob.id))
|
||||
|
||||
client.assertAnonymousRejected()
|
||||
|
||||
// PostgreSQL is the source of truth: logout still works and is visible immediately.
|
||||
assertEquals(HttpStatusCode.NoContent, client.delete("/api/v1/auth/sessions/current") { auth(alice) }.status)
|
||||
assertEquals(HttpStatusCode.Unauthorized, client.get("/api/v1/users/me") { auth(alice) }.status)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `hanging Redis costs one command timeout and recovers`() {
|
||||
val redis = TestRedis.create().also { it.start() }
|
||||
val docker = redis.dockerClient
|
||||
|
||||
try {
|
||||
runApp(redis = TestRedis.endpoint(redis)) { client ->
|
||||
val alice = client.register("alice")
|
||||
assertEquals(HttpStatusCode.OK, client.get("/api/v1/users/me") { auth(alice) }.status)
|
||||
|
||||
docker.pauseContainerCmd(redis.containerId).exec()
|
||||
|
||||
val started = TimeSource.Monotonic.markNow()
|
||||
assertEquals(HttpStatusCode.OK, client.get("/api/v1/users/me") { auth(alice) }.status)
|
||||
assertTrue(started.elapsedNow() < 2.seconds, "cache lookup must be bounded by the command timeout")
|
||||
|
||||
client.assertAnonymousRejected()
|
||||
|
||||
assertEquals(HttpStatusCode.NoContent, client.delete("/api/v1/auth/sessions/current") { auth(alice) }.status)
|
||||
assertEquals(HttpStatusCode.Unauthorized, client.get("/api/v1/users/me") { auth(alice) }.status)
|
||||
|
||||
docker.unpauseContainerCmd(redis.containerId).exec()
|
||||
|
||||
// The copy cached before the pause is replaced by the delayed tombstone, not resurrected.
|
||||
assertEquals(HttpStatusCode.Unauthorized, client.get("/api/v1/users/me") { auth(alice) }.status)
|
||||
|
||||
val bob = client.register("bob")
|
||||
assertEquals(HttpStatusCode.OK, client.get("/api/v1/users/me") { auth(bob) }.status)
|
||||
assertTrue(
|
||||
TestRedis.cli(redis, "EXISTS", "session:${bob.token.toByteArray().computeSha256()}") == "1",
|
||||
"cache is used again after recovery"
|
||||
)
|
||||
}
|
||||
} finally {
|
||||
redis.stop()
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `without PostgreSQL requests fail with a bounded 503`() {
|
||||
val database = TestDatabase.create().also { it.start() }
|
||||
|
||||
try {
|
||||
runApp(database = database, databaseConnectionTimeoutMs = 2000) { client ->
|
||||
val alice = client.register("alice")
|
||||
assertEquals(HttpStatusCode.OK, client.get("/api/v1/users/me") { auth(alice) }.status)
|
||||
|
||||
database.dockerClient.stopContainerCmd(database.containerId).exec()
|
||||
|
||||
val started = TimeSource.Monotonic.markNow()
|
||||
|
||||
// The session itself is still served from Redis, the user lookup needs PostgreSQL.
|
||||
assertUnavailable(client.get("/api/v1/users/me") { auth(alice) })
|
||||
assertUnavailable(client.post("/api/v1/auth/sessions") { basicAuth(alice.user.username, alice.password) })
|
||||
assertEquals(HttpStatusCode.ServiceUnavailable, client.get("/health/ready").status)
|
||||
assertEquals(HttpStatusCode.OK, client.get("/health/live").status)
|
||||
|
||||
assertTrue(started.elapsedNow() < 15.seconds, "every request must fail within the pool timeout")
|
||||
|
||||
client.assertAnonymousRejected(unknownTokenStatus = HttpStatusCode.ServiceUnavailable)
|
||||
}
|
||||
} finally {
|
||||
database.stop()
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
package com.mtusi.messenger.main.plugins
|
||||
|
||||
import com.mtusi.messenger.main.support.TestRedis
|
||||
import com.mtusi.messenger.main.support.TestUser
|
||||
import com.mtusi.messenger.main.support.auth
|
||||
import com.mtusi.messenger.main.support.register
|
||||
import com.mtusi.messenger.main.support.runApp
|
||||
import com.mtusi.messenger.main.utils.computeSha256
|
||||
import io.ktor.client.request.delete
|
||||
import io.ktor.client.request.get
|
||||
import io.ktor.http.HttpStatusCode
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertTrue
|
||||
|
||||
class SessionCacheTest {
|
||||
|
||||
private val redis = TestRedis.container
|
||||
|
||||
private fun cached(user: TestUser): String {
|
||||
return TestRedis.cli(redis, "GET", "session:${user.token.toByteArray().computeSha256()}")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `first authenticated request fills the cache`() = runApp { client ->
|
||||
val alice = client.register("alice")
|
||||
|
||||
assertEquals("", cached(alice), "login does not write to the cache")
|
||||
|
||||
assertEquals(HttpStatusCode.OK, client.get("/api/v1/users/me") { auth(alice) }.status)
|
||||
|
||||
assertTrue(cached(alice).contains(alice.id.toString()))
|
||||
|
||||
// The session lives for an hour, its cached copy at most sessionCacheTtlSeconds.
|
||||
val ttl = TestRedis.cli(redis, "TTL", "session:${alice.token.toByteArray().computeSha256()}").toLong()
|
||||
assertTrue(ttl in 1..60, "cached copy TTL was $ttl")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `logout leaves a tombstone and the token is rejected`() = runApp { client ->
|
||||
val alice = client.register("alice")
|
||||
client.get("/api/v1/users/me") { auth(alice) }
|
||||
|
||||
assertEquals(HttpStatusCode.NoContent, client.delete("/api/v1/auth/sessions/current") { auth(alice) }.status)
|
||||
|
||||
assertEquals("revoked", cached(alice))
|
||||
assertEquals(HttpStatusCode.Unauthorized, client.get("/api/v1/users/me") { auth(alice) }.status)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `emptied cache falls back to PostgreSQL`() = runApp { client ->
|
||||
val alice = client.register("alice")
|
||||
val bob = client.register("bob")
|
||||
client.get("/api/v1/users/me") { auth(alice) }
|
||||
client.delete("/api/v1/auth/sessions/current") { auth(bob) }
|
||||
|
||||
TestRedis.cli(redis, "FLUSHALL")
|
||||
|
||||
// PostgreSQL is the source of truth: the live session survives, the revoked one stays revoked.
|
||||
assertEquals(HttpStatusCode.OK, client.get("/api/v1/users/me") { auth(alice) }.status)
|
||||
assertEquals(HttpStatusCode.Unauthorized, client.get("/api/v1/users/me") { auth(bob) }.status)
|
||||
assertTrue(cached(alice).contains(alice.id.toString()), "cache is filled again")
|
||||
}
|
||||
|
||||
}
|
||||
@@ -25,6 +25,7 @@ import io.ktor.server.testing.testApplication
|
||||
import kotlinx.serialization.ExperimentalSerializationApi
|
||||
import kotlinx.serialization.json.Json
|
||||
import kotlinx.serialization.json.JsonNamingStrategy
|
||||
import org.testcontainers.containers.PostgreSQLContainer
|
||||
import java.util.UUID
|
||||
|
||||
@OptIn(ExperimentalSerializationApi::class)
|
||||
@@ -45,28 +46,51 @@ data class TestUser(
|
||||
get() = user.id
|
||||
}
|
||||
|
||||
private fun testConfig(sessionTtlSeconds: Long) = HoconApplicationConfig(
|
||||
data class RedisEndpoint(
|
||||
val host: String,
|
||||
val port: Int,
|
||||
)
|
||||
|
||||
private fun testConfig(
|
||||
sessionTtlSeconds: Long,
|
||||
database: PostgreSQLContainer<*>,
|
||||
databaseConnectionTimeoutMs: Long,
|
||||
redis: RedisEndpoint,
|
||||
) = HoconApplicationConfig(
|
||||
ConfigFactory.parseMap(
|
||||
mapOf(
|
||||
"database.host" to TestDatabase.container.host,
|
||||
"database.port" to TestDatabase.container.firstMappedPort,
|
||||
"database.name" to TestDatabase.NAME,
|
||||
"database.username" to TestDatabase.USERNAME,
|
||||
"database.password" to TestDatabase.PASSWORD,
|
||||
"database.host" to database.host,
|
||||
"database.port" to database.firstMappedPort,
|
||||
"database.name" to database.databaseName,
|
||||
"database.username" to database.username,
|
||||
"database.password" to database.password,
|
||||
"database.maxPoolSize" to 10,
|
||||
"database.connectionTimeoutMs" to 10000,
|
||||
"database.connectionTimeoutMs" to databaseConnectionTimeoutMs,
|
||||
"database.statementTimeoutMs" to 5000,
|
||||
"redis.host" to redis.host,
|
||||
"redis.port" to redis.port,
|
||||
"redis.password" to TestRedis.PASSWORD,
|
||||
"redis.connectTimeoutMs" to 500,
|
||||
"redis.commandTimeoutMs" to 500,
|
||||
"authentication.realm" to "messenger",
|
||||
"authentication.sessionTtlSeconds" to sessionTtlSeconds,
|
||||
"authentication.sessionCacheTtlSeconds" to 60,
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
/**
|
||||
* Docker Desktop can be slow to accept the first connection, hence the generous default database timeout.
|
||||
*/
|
||||
fun runApp(
|
||||
sessionTtlSeconds: Long = 3600,
|
||||
database: PostgreSQLContainer<*> = TestDatabase.container,
|
||||
databaseConnectionTimeoutMs: Long = 10000,
|
||||
redis: RedisEndpoint = TestRedis.endpoint(),
|
||||
block: suspend ApplicationTestBuilder.(HttpClient) -> Unit
|
||||
) = testApplication {
|
||||
environment {
|
||||
config = testConfig(sessionTtlSeconds)
|
||||
config = testConfig(sessionTtlSeconds, database, databaseConnectionTimeoutMs, redis)
|
||||
}
|
||||
application {
|
||||
module()
|
||||
|
||||
@@ -14,11 +14,17 @@ object TestDatabase {
|
||||
const val PASSWORD = "messenger"
|
||||
|
||||
val container: PostgreSQLContainer<*> by lazy {
|
||||
PostgreSQLContainer(DockerImageName.parse("postgres:17.6-alpine3.22").asCompatibleSubstituteFor("postgres"))
|
||||
create().also { it.start() }
|
||||
}
|
||||
|
||||
/**
|
||||
* A separate, not yet started container for tests that stop the database.
|
||||
*/
|
||||
fun create(): PostgreSQLContainer<*> {
|
||||
return PostgreSQLContainer(DockerImageName.parse("postgres:17.6-alpine3.22").asCompatibleSubstituteFor("postgres"))
|
||||
.withDatabaseName(NAME)
|
||||
.withUsername(USERNAME)
|
||||
.withPassword(PASSWORD)
|
||||
.also { it.start() }
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
package com.mtusi.messenger.main.support
|
||||
|
||||
import org.testcontainers.containers.GenericContainer
|
||||
import org.testcontainers.utility.DockerImageName
|
||||
import java.net.ServerSocket
|
||||
|
||||
/**
|
||||
* One Redis container per test JVM, same image and memory settings as compose.yaml.
|
||||
*/
|
||||
object TestRedis {
|
||||
|
||||
const val PASSWORD = "messenger"
|
||||
|
||||
private const val PORT = 6379
|
||||
|
||||
val container: GenericContainer<*> by lazy {
|
||||
create().also { it.start() }
|
||||
}
|
||||
|
||||
/**
|
||||
* A separate, not yet started container for tests that pause or flush Redis.
|
||||
*/
|
||||
fun create(): GenericContainer<*> {
|
||||
return GenericContainer(DockerImageName.parse("redis:8.4.0-alpine"))
|
||||
.withExposedPorts(PORT)
|
||||
.withCommand(
|
||||
"redis-server",
|
||||
"--requirepass", PASSWORD,
|
||||
"--save", "",
|
||||
"--appendonly", "no",
|
||||
"--maxmemory-policy", "volatile-lru"
|
||||
)
|
||||
}
|
||||
|
||||
fun endpoint(container: GenericContainer<*> = this.container) = RedisEndpoint(
|
||||
host = container.host,
|
||||
port = container.getMappedPort(PORT)
|
||||
)
|
||||
|
||||
/**
|
||||
* An endpoint nobody listens on: connections are refused immediately.
|
||||
*/
|
||||
fun unreachableEndpoint(): RedisEndpoint {
|
||||
val port = ServerSocket(0).use { it.localPort }
|
||||
return RedisEndpoint(host = "127.0.0.1", port = port)
|
||||
}
|
||||
|
||||
fun cli(container: GenericContainer<*>, vararg command: String): String {
|
||||
val result = container.execInContainer("redis-cli", "--no-auth-warning", "-a", PASSWORD, *command)
|
||||
check(result.exitCode == 0) { "redis-cli failed: ${result.stderr}" }
|
||||
return result.stdout.trim()
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user