mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix(floci-core): remove aws sdk from floci project (#846)
This commit is contained in:
1 parent
cf0db6edc9
commit
b6619f8646
3 files changed
+231
-38
No files matched your search
+219
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user