From 80719fc62abfd7ebb130ab66f91e456709a4d99d Mon Sep 17 00:00:00 2001 From: Andre Kurait Date: Tue, 13 Oct 2026 21:02:58 -0500 Subject: [PATCH] test: stabilize Cognito invitations and Postgres TLS fixtures (#5597) * test: stabilize Cognito invitations and Postgres TLS fixtures Signed-off-by: Andre Kurait * test: assert the TLS authentication thread stopped with assertFalse Signed-off-by: Priamo --------- Signed-off-by: Andre Kurait Signed-off-by: Priamo Co-authored-by: Priamo --- .../cognito/CognitoIntegrationTest.java | 43 ++++--- .../proxy/PostgresProtocolHandlerTest.java | 120 +++++------------- 2 files changed, 56 insertions(+), 107 deletions(-) diff --git a/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoIntegrationTest.java index 538f3a0476..e54dd4ba65 100644 --- a/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoIntegrationTest.java @@ -13,6 +13,8 @@ import org.junit.jupiter.api.TestMethodOrder; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; +import org.junit.jupiter.params.provider.NullSource; +import org.junit.jupiter.params.provider.ValueSource; import java.math.BigInteger; import java.nio.charset.StandardCharsets; @@ -22,7 +24,9 @@ import java.security.Signature; import java.security.spec.RSAPublicKeySpec; import java.util.ArrayList; import java.util.Base64; +import java.util.LinkedHashMap; import java.util.List; +import java.util.Map; import java.util.Spliterators; import java.util.UUID; import java.util.regex.Matcher; @@ -768,8 +772,10 @@ class CognitoIntegrationTest { equalTo("no-reply@verificationemail.com")); } - @Test - void adminCreateUserEmailsAnInvitationWithAWorkingTemporaryPassword() throws Exception { + @ParameterizedTest + @NullSource + @ValueSource(strings = "Inv\"ite\\123Aa!") + void adminCreateUserEmailsAnInvitationWithAWorkingTemporaryPassword(String temporaryPassword) throws Exception { String invitePoolId = cognitoJson("CreateUserPool", """ { "PoolName": "InvitationPool" } """).path("UserPool").path("Id").asText(); @@ -782,14 +788,15 @@ class CognitoIntegrationTest { """.formatted(invitePoolId)).path("UserPoolClient").path("ClientId").asText(); String invitee = "invited+" + UUID.randomUUID() + "@example.com"; - cognitoAction("AdminCreateUser", """ - { - "UserPoolId": "%s", - "Username": "%s", - "UserAttributes": [{ "Name": "email", "Value": "%s" }], - "DesiredDeliveryMediums": ["EMAIL"] - } - """.formatted(invitePoolId, invitee, invitee)) + Map createUser = new LinkedHashMap<>(); + createUser.put("UserPoolId", invitePoolId); + createUser.put("Username", invitee); + createUser.put("UserAttributes", List.of(Map.of("Name", "email", "Value", invitee))); + createUser.put("DesiredDeliveryMediums", List.of("EMAIL")); + if (temporaryPassword != null) { + createUser.put("TemporaryPassword", temporaryPassword); + } + cognitoAction("AdminCreateUser", OBJECT_MAPPER.writeValueAsString(createUser)) .then() .statusCode(200) .body("User.UserStatus", equalTo("FORCE_CHANGE_PASSWORD")); @@ -808,15 +815,15 @@ class CognitoIntegrationTest { .matcher(body); assertTrue(invitation.find(), body); assertEquals(invitee, invitation.group(1)); + if (temporaryPassword != null) { + assertEquals(temporaryPassword, invitation.group(2)); + } - cognitoAction("AdminInitiateAuth", """ - { - "UserPoolId": "%s", - "ClientId": "%s", - "AuthFlow": "ADMIN_USER_PASSWORD_AUTH", - "AuthParameters": { "USERNAME": "%s", "PASSWORD": "%s" } - } - """.formatted(invitePoolId, inviteClientId, invitee, invitation.group(2))) + cognitoAction("AdminInitiateAuth", OBJECT_MAPPER.writeValueAsString(Map.of( + "UserPoolId", invitePoolId, + "ClientId", inviteClientId, + "AuthFlow", "ADMIN_USER_PASSWORD_AUTH", + "AuthParameters", Map.of("USERNAME", invitee, "PASSWORD", invitation.group(2))))) .then() .statusCode(200) .body("ChallengeName", equalTo("NEW_PASSWORD_REQUIRED")); diff --git a/src/test/java/io/github/hectorvent/floci/services/rds/proxy/PostgresProtocolHandlerTest.java b/src/test/java/io/github/hectorvent/floci/services/rds/proxy/PostgresProtocolHandlerTest.java index bfbb2d65d4..7cd524c4dd 100644 --- a/src/test/java/io/github/hectorvent/floci/services/rds/proxy/PostgresProtocolHandlerTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/rds/proxy/PostgresProtocolHandlerTest.java @@ -40,6 +40,7 @@ import java.util.Map; import java.util.concurrent.atomic.AtomicReference; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNull; @@ -445,98 +446,32 @@ class PostgresProtocolHandlerTest { } } - @Test - void hostnameVerificationSucceedsForRegisteredAdvertisedHost() throws Exception { - try (ServerSocket backendServer = new ServerSocket(0); - ServerSocket clientServer = new ServerSocket(0)) { - - RdsProxyTlsCertificates tlsCertificates = testTlsCertificates(); - String advertisedHost = "172.17.0.5"; - tlsCertificates.ensureHost(advertisedHost); - - int backendPort = backendServer.getLocalPort(); - Thread backendThread = Thread.ofVirtual().start(() -> { - try { - mockBackendStartup(backendServer, new AtomicReference<>(), false); - } catch (IOException e) { - throw new RuntimeException(e); - } - }); - - try (Socket ourClient = new Socket("localhost", clientServer.getLocalPort())) { - Socket proxyClient = clientServer.accept(); - Socket backend = new Socket("localhost", backendPort); - - Thread authThread = Thread.ofVirtual().start(() -> { - try { - PostgresProtocolHandler.AuthenticatedSession session = - PostgresProtocolHandler.authenticate( - proxyClient, () -> backend, - "dbadmin", "adminpass", "postgres", - false, testSigV4Validator(), testBinding(), tlsCertificates, - (user, pass) -> PasswordValidator.AuthResult.MASTER_EQUIVALENT, 5000); - if (session != null) { - PostgresProtocolHandler.bridge(session); - } - } catch (IOException expected) { - // The test closes proxyClient once the handshake succeeds, which ends authenticate. - } - }); - - DataOutputStream clientOut = new DataOutputStream(ourClient.getOutputStream()); - DataInputStream clientIn = new DataInputStream(ourClient.getInputStream()); - - writeSslRequest(clientOut); - assertEquals('S', clientIn.readUnsignedByte()); - - Path caCertFile = tempDir.resolve("tls").resolve("rds-ca.crt"); - SSLSocket sslClient = verifyingClientSocket(ourClient, caCertFile, advertisedHost); - sslClient.startHandshake(); // must not throw — advertisedHost is in the cert's SAN - - proxyClient.close(); - authThread.join(5_000); - backendThread.join(5_000); - } - } - } - - @Test - void hostnameVerificationFailsForUnregisteredAdvertisedHost() throws Exception { - try (ServerSocket backendServer = new ServerSocket(0); - ServerSocket clientServer = new ServerSocket(0)) { + @ParameterizedTest + @CsvSource({"172.17.0.5, true", "never-registered.example.com, false"}) + void hostnameVerificationRequiresRegisteredAdvertisedHost(String advertisedHost, boolean registered) throws Exception { + try (ServerSocket clientServer = new ServerSocket(0); + Socket ourClient = new Socket("localhost", clientServer.getLocalPort()); + Socket proxyClient = clientServer.accept()) { RdsProxyTlsCertificates tlsCertificates = testTlsCertificates(); tlsCertificates.ensureHost("172.17.0.5"); - int backendPort = backendServer.getLocalPort(); - Thread backendThread = Thread.ofVirtual().start(() -> { - try { - mockBackendStartup(backendServer, new AtomicReference<>(), false); - } catch (IOException e) { - // Backend connection is torn down before completing startup — expected here. - } - }); - - try (Socket ourClient = new Socket("localhost", clientServer.getLocalPort())) { - Socket proxyClient = clientServer.accept(); - Socket backend = new Socket("localhost", backendPort); - - Thread authThread = Thread.ofVirtual().start(() -> { - try { - PostgresProtocolHandler.AuthenticatedSession session = - PostgresProtocolHandler.authenticate( - proxyClient, () -> backend, - "dbadmin", "adminpass", "postgres", - false, testSigV4Validator(), testBinding(), tlsCertificates, - (user, pass) -> PasswordValidator.AuthResult.MASTER_EQUIVALENT, 5000); - if (session != null) { - PostgresProtocolHandler.bridge(session); + AtomicReference authFailure = new AtomicReference<>(); + Thread authThread = Thread.ofVirtual() + .uncaughtExceptionHandler((thread, error) -> authFailure.set(error)) + .start(() -> { + try { + PostgresProtocolHandler.authenticate( + proxyClient, NEVER_CONNECT, + "dbadmin", "adminpass", "postgres", + false, testSigV4Validator(), testBinding(), tlsCertificates, + (user, pass) -> PasswordValidator.AuthResult.MASTER_EQUIVALENT, 5000); + } catch (IOException expected) { + // Closing or rejecting TLS ends authentication before PostgreSQL startup. } - } catch (IOException ignored) { - // Client aborts the handshake below — the handler observing that is expected. - } - }); + }); + try { DataOutputStream clientOut = new DataOutputStream(ourClient.getOutputStream()); DataInputStream clientIn = new DataInputStream(ourClient.getInputStream()); @@ -544,12 +479,19 @@ class PostgresProtocolHandlerTest { assertEquals('S', clientIn.readUnsignedByte()); Path caCertFile = tempDir.resolve("tls").resolve("rds-ca.crt"); - SSLSocket sslClient = verifyingClientSocket(ourClient, caCertFile, "never-registered.example.com"); - assertThrows(SSLHandshakeException.class, sslClient::startHandshake); - + try (SSLSocket sslClient = verifyingClientSocket(ourClient, caCertFile, advertisedHost)) { + if (registered) { + sslClient.startHandshake(); + } else { + assertThrows(SSLHandshakeException.class, sslClient::startHandshake); + } + } + } finally { proxyClient.close(); authThread.join(5_000); + assertFalse(authThread.isAlive(), "authThread did not terminate"); } + assertNull(authFailure.get(), "TLS authentication thread failed"); } }