Fix: reject revoked tokens on all replicas when Redis writes fail

This commit is contained in:
2026-10-23 22:33:12 +03:00
parent af184cabe3
commit 4abbe84847
42 changed files with 876 additions and 501 deletions
@@ -31,6 +31,11 @@ import kotlin.test.assertTrue
import kotlin.time.Duration.Companion.seconds
import kotlin.time.TimeSource
/**
* Short logout deadline: with Redis paused or down the tombstone cannot be written and the logout answers 503.
*/
private const val REVOKE_TIMEOUT_MS = 1000L
class DependencyFailureTest {
/**
@@ -54,7 +59,8 @@ class DependencyFailureTest {
@Test
fun `without Redis requests bypass the cache`() = runApp(
redis = TestRedis.unreachableEndpoint()
redis = TestRedis.unreachableEndpoint(),
sessionRevokeTimeoutMs = REVOKE_TIMEOUT_MS
) { client ->
assertEquals(HttpStatusCode.OK, client.get("/health/ready").status)
@@ -66,8 +72,9 @@ class DependencyFailureTest {
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)
// Without the tombstone another replica could still accept the token from the cache, so the logout is not
// confirmed (503). The session is gone from PostgreSQL, so this replica rejects the token anyway.
assertUnavailable(client.delete("/api/v1/auth/sessions/current") { auth(alice) })
assertEquals(HttpStatusCode.Unauthorized, client.get("/api/v1/users/me") { auth(alice) }.status)
}
@@ -77,7 +84,7 @@ class DependencyFailureTest {
val docker = redis.dockerClient
try {
runApp(redis = TestRedis.endpoint(redis)) { client ->
runApp(redis = TestRedis.endpoint(redis), sessionRevokeTimeoutMs = REVOKE_TIMEOUT_MS) { client ->
val alice = client.register("alice")
assertEquals(HttpStatusCode.OK, client.get("/api/v1/users/me") { auth(alice) }.status)
@@ -89,7 +96,7 @@ class DependencyFailureTest {
client.assertAnonymousRejected()
assertEquals(HttpStatusCode.NoContent, client.delete("/api/v1/auth/sessions/current") { auth(alice) }.status)
assertUnavailable(client.delete("/api/v1/auth/sessions/current") { auth(alice) })
assertEquals(HttpStatusCode.Unauthorized, client.get("/api/v1/users/me") { auth(alice) }.status)
docker.unpauseContainerCmd(redis.containerId).exec()
@@ -128,19 +135,25 @@ class DependencyFailureTest {
val docker = redis.dockerClient
try {
runApp(redis = TestRedis.endpoint(redis)) { client ->
runApp(redis = TestRedis.endpoint(redis), sessionRevokeTimeoutMs = REVOKE_TIMEOUT_MS) { client ->
val alice = client.register("alice")
assertEquals(HttpStatusCode.OK, client.get("/api/v1/users/me") { auth(alice) }.status)
val copy = cachedCopy(redis, alice)
docker.pauseContainerCmd(redis.containerId).exec()
assertEquals(HttpStatusCode.NoContent, client.delete("/api/v1/auth/sessions/current") { auth(alice) }.status)
assertUnavailable(client.delete("/api/v1/auth/sessions/current") { auth(alice) })
docker.unpauseContainerCmd(redis.containerId).exec()
loseTombstone(redis, alice, copy)
assertEquals(HttpStatusCode.Unauthorized, client.get("/api/v1/users/me") { auth(alice) }.status)
assertEquals("revoked", TestRedis.cli(redis, "GET", "session:${alice.token.toByteArray().computeSha256()}"))
// The background check writes the pending tombstone once Redis accepts writes (retry every 1 s).
val deadline = TimeSource.Monotonic.markNow() + 3.seconds
while (TestRedis.cli(redis, "GET", "session:${alice.token.toByteArray().computeSha256()}") != "revoked") {
assertTrue(deadline.hasNotPassedNow(), "pending tombstone was not written after recovery")
Thread.sleep(200)
}
}
} finally {
redis.stop()
@@ -157,20 +170,20 @@ class DependencyFailureTest {
lateinit var copy: String
try {
runApp(redis = TestRedis.endpoint(redis)) { client ->
runApp(redis = TestRedis.endpoint(redis), sessionRevokeTimeoutMs = REVOKE_TIMEOUT_MS) { client ->
alice = client.register("alice")
val bob = client.register("bob")
chat = client.createChat(alice, "restart", listOf(alice.id, bob.id))
copy = cachedCopy(redis, alice)
docker.pauseContainerCmd(redis.containerId).exec()
assertEquals(HttpStatusCode.NoContent, client.delete("/api/v1/auth/sessions/current") { auth(alice) }.status)
assertUnavailable(client.delete("/api/v1/auth/sessions/current") { auth(alice) })
}
docker.unpauseContainerCmd(redis.containerId).exec()
loseTombstone(redis, alice, copy)
runApp(redis = TestRedis.endpoint(redis)) { client ->
runApp(redis = TestRedis.endpoint(redis), sessionRevokeTimeoutMs = REVOKE_TIMEOUT_MS) { client ->
assertEquals(HttpStatusCode.Unauthorized, client.get("/api/v1/users/me") { auth(alice) }.status)
assertEquals(HttpStatusCode.Unauthorized, client.get("/api/v1/chats/${chat.id}/messages") { auth(alice) }.status)
assertEquals(