diff --git a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/RdsIamTokenCompatTest.java b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/RdsIamTokenCompatTest.java new file mode 100644 index 0000000000..ce04f5e968 --- /dev/null +++ b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/RdsIamTokenCompatTest.java @@ -0,0 +1,219 @@ +package com.floci.test; + +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.Assumptions; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.MethodOrderer; +import org.junit.jupiter.api.Order; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestMethodOrder; +import software.amazon.awssdk.auth.credentials.AwsBasicCredentials; +import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider; +import software.amazon.awssdk.regions.Region; +import software.amazon.awssdk.services.rds.RdsClient; +import software.amazon.awssdk.services.rds.model.CreateDbInstanceRequest; +import software.amazon.awssdk.services.rds.model.DeleteDbInstanceRequest; +import software.amazon.awssdk.services.rds.model.GenerateAuthenticationTokenRequest; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import java.time.Duration; +import java.time.Instant; +import java.util.Properties; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +/** + * End-to-end IAM token validation tests for the RDS JDBC proxy. + * + * Uses the real AWS SDK RdsUtilities presigner as an oracle — if these tests pass, + * tokens generated by actual AWS SDK clients are correctly accepted or rejected by + * Floci's proxy, not just by the hand-rolled test helper used in unit tests. + * + * Requires Docker (a real Postgres container runs behind the Floci proxy). + */ +@DisplayName("RDS IAM Token Scenarios") +@TestMethodOrder(MethodOrderer.OrderAnnotation.class) +class RdsIamTokenCompatTest { + + private static final StaticCredentialsProvider CREDENTIALS = + StaticCredentialsProvider.create(AwsBasicCredentials.create("test", "test")); + private static final Region REGION = Region.US_EAST_1; + private static final String USERNAME = "admin"; + private static final String PASSWORD = "secret-pass-123"; + private static final String DATABASE = "appdb"; + private static final int PROXY_PORT_MIN = 7000; + private static final int PROXY_PORT_MAX = 7099; + + private static RdsClient rds; + private static String instanceId; + private static int proxyPort; + private static boolean instanceReady; + + @BeforeAll + static void setup() { + rds = TestFixtures.rdsClient(); + instanceId = TestFixtures.uniqueName("rds-iam"); + try { + var response = rds.createDBInstance(CreateDbInstanceRequest.builder() + .dbInstanceIdentifier(instanceId) + .dbInstanceClass("db.t3.micro") + .engine("postgres") + .masterUsername(USERNAME) + .masterUserPassword(PASSWORD) + .dbName(DATABASE) + .allocatedStorage(20) + .enableIAMDatabaseAuthentication(true) + .build()); + proxyPort = response.dbInstance().endpoint().port(); + instanceReady = true; + } catch (Exception e) { + instanceReady = false; + } + } + + @AfterAll + static void cleanup() { + if (rds != null && instanceReady && instanceId != null) { + try { + rds.deleteDBInstance(DeleteDbInstanceRequest.builder() + .dbInstanceIdentifier(instanceId) + .skipFinalSnapshot(true) + .build()); + } catch (Exception ignored) { + } + rds.close(); + } + } + + @Test + @Order(1) + @DisplayName("SDK-generated IAM token is accepted by the proxy") + void sdkGeneratedTokenIsAccepted() throws Exception { + assumeInstanceReady(); + + String token = rds.utilities().generateAuthenticationToken( + GenerateAuthenticationTokenRequest.builder() + .hostname(TestFixtures.proxyHost()) + .port(proxyPort) + .username(USERNAME) + .region(REGION) + .credentialsProvider(CREDENTIALS) + .build()); + + Connection conn = awaitPostgresConnection(USERNAME, token); + try { + assertThat(selectOne(conn)).isEqualTo(1); + } finally { + conn.close(); + } + } + + @Test + @Order(2) + @DisplayName("Token signed for a different user is rejected") + void tokenForDifferentUserIsRejected() { + assumeInstanceReady(); + + String token = rds.utilities().generateAuthenticationToken( + GenerateAuthenticationTokenRequest.builder() + .hostname(TestFixtures.proxyHost()) + .port(proxyPort) + .username("other-user") + .region(REGION) + .credentialsProvider(CREDENTIALS) + .build()); + + assertThatThrownBy(() -> openPostgresConnection(USERNAME, token)) + .isInstanceOf(SQLException.class) + .hasMessageContaining("password authentication failed"); + } + + @Test + @Order(3) + @DisplayName("Token with tampered signature is rejected") + void tokenWithTamperedSignatureIsRejected() { + assumeInstanceReady(); + + String token = rds.utilities().generateAuthenticationToken( + GenerateAuthenticationTokenRequest.builder() + .hostname(TestFixtures.proxyHost()) + .port(proxyPort) + .username(USERNAME) + .region(REGION) + .credentialsProvider(CREDENTIALS) + .build()); + + String tampered = token.substring(0, token.length() - 1) + + (token.endsWith("a") ? "b" : "a"); + + assertThatThrownBy(() -> openPostgresConnection(USERNAME, tampered)) + .isInstanceOf(SQLException.class) + .hasMessageContaining("password authentication failed"); + } + + @Test + @Order(4) + @DisplayName("Token missing X-Amz-Signature parameter is rejected") + void tokenMissingSignatureIsRejected() { + assumeInstanceReady(); + + String token = rds.utilities().generateAuthenticationToken( + GenerateAuthenticationTokenRequest.builder() + .hostname(TestFixtures.proxyHost()) + .port(proxyPort) + .username(USERNAME) + .region(REGION) + .credentialsProvider(CREDENTIALS) + .build()); + + String withoutSignature = token.replaceFirst("&X-Amz-Signature=[0-9a-f]+", ""); + + assertThatThrownBy(() -> openPostgresConnection(USERNAME, withoutSignature)) + .isInstanceOf(SQLException.class) + .hasMessageContaining("password authentication failed"); + } + + private static void assumeInstanceReady() { + Assumptions.assumeTrue(instanceReady && proxyPort > 0, + "RDS instance not available in this environment"); + } + + private static Connection awaitPostgresConnection(String username, String password) throws Exception { + Instant deadline = Instant.now().plus(Duration.ofSeconds(60)); + SQLException last = null; + while (Instant.now().isBefore(deadline)) { + try { + return openPostgresConnection(username, password); + } catch (SQLException e) { + last = e; + Thread.sleep(1000); + } + } + throw last != null ? last : new SQLException("Timed out waiting for RDS proxy connection"); + } + + private static Connection openPostgresConnection(String username, String password) throws SQLException { + Properties props = new Properties(); + props.setProperty("user", username); + props.setProperty("password", password); + props.setProperty("sslmode", "disable"); + props.setProperty("connectTimeout", "5"); + return DriverManager.getConnection( + "jdbc:postgresql://" + TestFixtures.proxyHost() + ":" + proxyPort + "/" + DATABASE, + props); + } + + private static int selectOne(Connection conn) throws SQLException { + try (Statement st = conn.createStatement(); + ResultSet rs = st.executeQuery("select 1")) { + assertThat(rs.next()).isTrue(); + return rs.getInt(1); + } + } +} diff --git a/pom.xml b/pom.xml index 5657c9b308..c16f333f6c 100644 --- a/pom.xml +++ b/pom.xml @@ -28,13 +28,6 @@ pom import - - software.amazon.awssdk - bom - 2.42.41 - pom - import - com.google.protobuf protobuf-java @@ -225,12 +218,6 @@ rest-assured test - - - software.amazon.awssdk - rds - test - com.fasterxml.jackson.core jackson-annotations diff --git a/src/test/java/io/github/hectorvent/floci/services/rds/proxy/RdsSigV4ValidatorTest.java b/src/test/java/io/github/hectorvent/floci/services/rds/proxy/RdsSigV4ValidatorTest.java index 9a1074e9bc..5b705208f3 100644 --- a/src/test/java/io/github/hectorvent/floci/services/rds/proxy/RdsSigV4ValidatorTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/rds/proxy/RdsSigV4ValidatorTest.java @@ -4,11 +4,6 @@ import io.github.hectorvent.floci.services.iam.IamService; import io.github.hectorvent.floci.testutil.IamServiceTestHelper; import io.github.hectorvent.floci.testutil.SigV4TokenTestHelper; import org.junit.jupiter.api.Test; -import software.amazon.awssdk.auth.credentials.AwsBasicCredentials; -import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider; -import software.amazon.awssdk.regions.Region; -import software.amazon.awssdk.services.rds.RdsUtilities; -import software.amazon.awssdk.services.rds.model.GenerateAuthenticationTokenRequest; import java.time.Instant; @@ -17,34 +12,26 @@ import static org.junit.jupiter.api.Assertions.assertTrue; class RdsSigV4ValidatorTest { - /** - * Uses the real AWS SDK RDS presigner as an independent SigV4 oracle. - * If this test passes, the validator is compatible with tokens generated - * by actual AWS SDK clients, not just our hand-rolled test helper. - */ @Test - void validateAcceptsTokenFromAwsSdkPresigner() throws Exception { + void validateAcceptsTokenSignedByStandardSigV4() throws Exception { String accessKeyId = "AKIAORACLETEST"; String secretAccessKey = "oracle-secret-key-value"; IamService iamService = IamServiceTestHelper.iamServiceWithAccessKey(accessKeyId, secretAccessKey); RdsSigV4Validator validator = new RdsSigV4Validator(iamService); - RdsUtilities utilities = RdsUtilities.builder() - .credentialsProvider(StaticCredentialsProvider.create( - AwsBasicCredentials.create(accessKeyId, secretAccessKey))) - .region(Region.US_EAST_1) - .build(); + String token = SigV4TokenTestHelper.createRdsToken( + "db.oracle-test.local", + 5432, + "testuser", + accessKeyId, + secretAccessKey, + Instant.now().minusSeconds(60), + 900 + ); - String sdkToken = utilities.generateAuthenticationToken( - GenerateAuthenticationTokenRequest.builder() - .hostname("db.oracle-test.local") - .port(5432) - .username("testuser") - .build()); - - assertTrue(validator.validate(sdkToken, "testuser"), - "Validator must accept tokens generated by the official AWS SDK RDS presigner"); + assertTrue(validator.validate(token, "testuser"), + "Validator must accept a well-formed SigV4 RDS authentication token"); } @Test