mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
test: stabilize Cognito invitations and Postgres TLS fixtures (#5597)
* test: stabilize Cognito invitations and Postgres TLS fixtures Signed-off-by: Andre Kurait <akurait@amazon.com> * test: assert the TLS authentication thread stopped with assertFalse Signed-off-by: Priamo <pgermosen@gmail.com> --------- Signed-off-by: Andre Kurait <akurait@amazon.com> Signed-off-by: Priamo <pgermosen@gmail.com> Co-authored-by: Priamo <pgermosen@gmail.com>
This commit is contained in:
1 parent
2aca536bec
commit
80719fc62a
2 files changed
+56
-107
No files matched your search
+25
-18
@@ -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<String, Object> 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"));
|
||||
|
||||
+31
-89
@@ -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<Throwable> 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");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user