mirror of
https://github.com/floci-io/floci.git
synced 2026-11-28 16:11:48 -05:00
fix(rds): require database role grants for PostgreSQL IAM login (#5945)
* fix(rds): require database role grants for PostgreSQL IAM login * fix(rds): preserve standard proxy IAM authentication * fix(rds): report PAM refusal for IAM member passwords
This commit is contained in:
1 parent
e9154fb928
commit
678c5b82a6
13 files changed
+817
-71
No files matched your search
+17
-8
@@ -46,6 +46,7 @@ class RdsIamTokenCompatTest {
|
||||
StaticCredentialsProvider.create(AwsBasicCredentials.create("test", "test"));
|
||||
private static final Region REGION = TestFixtures.region();
|
||||
private static final String USERNAME = "admin";
|
||||
private static final String IAM_USERNAME = "iam_user";
|
||||
private static final String PASSWORD = "secret-pass-123";
|
||||
private static final String DATABASE = "appdb";
|
||||
private static final int PROXY_PORT_MIN = 7000;
|
||||
@@ -57,7 +58,8 @@ class RdsIamTokenCompatTest {
|
||||
private static boolean instanceReady;
|
||||
|
||||
@BeforeAll
|
||||
static void setup() {
|
||||
static void setup() throws Exception {
|
||||
Assumptions.assumeFalse(TestFixtures.isRealAws(), "Local PostgreSQL role fixtures");
|
||||
rds = TestFixtures.rdsClient();
|
||||
instanceId = TestFixtures.uniqueName("rds-iam");
|
||||
try {
|
||||
@@ -76,6 +78,13 @@ class RdsIamTokenCompatTest {
|
||||
} catch (Exception e) {
|
||||
instanceReady = false;
|
||||
}
|
||||
if (instanceReady) {
|
||||
try (Connection admin = awaitPostgresConnection(USERNAME, PASSWORD);
|
||||
Statement statement = admin.createStatement()) {
|
||||
statement.execute("CREATE ROLE " + IAM_USERNAME + " LOGIN");
|
||||
statement.execute("GRANT rds_iam TO " + IAM_USERNAME);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@AfterAll
|
||||
@@ -102,12 +111,12 @@ class RdsIamTokenCompatTest {
|
||||
GenerateAuthenticationTokenRequest.builder()
|
||||
.hostname(TestFixtures.proxyHost())
|
||||
.port(proxyPort)
|
||||
.username(USERNAME)
|
||||
.username(IAM_USERNAME)
|
||||
.region(REGION)
|
||||
.credentialsProvider(CREDENTIALS)
|
||||
.build());
|
||||
|
||||
Connection conn = awaitPostgresConnection(USERNAME, token);
|
||||
Connection conn = awaitPostgresConnection(IAM_USERNAME, token);
|
||||
try {
|
||||
assertThat(selectOne(conn)).isEqualTo(1);
|
||||
} finally {
|
||||
@@ -130,7 +139,7 @@ class RdsIamTokenCompatTest {
|
||||
.credentialsProvider(CREDENTIALS)
|
||||
.build());
|
||||
|
||||
assertThatThrownBy(() -> openPostgresConnection(USERNAME, token))
|
||||
assertThatThrownBy(() -> openPostgresConnection(IAM_USERNAME, token))
|
||||
.isInstanceOf(SQLException.class)
|
||||
.hasMessageContaining("password authentication failed");
|
||||
}
|
||||
@@ -145,7 +154,7 @@ class RdsIamTokenCompatTest {
|
||||
GenerateAuthenticationTokenRequest.builder()
|
||||
.hostname(TestFixtures.proxyHost())
|
||||
.port(proxyPort)
|
||||
.username(USERNAME)
|
||||
.username(IAM_USERNAME)
|
||||
.region(REGION)
|
||||
.credentialsProvider(CREDENTIALS)
|
||||
.build());
|
||||
@@ -153,7 +162,7 @@ class RdsIamTokenCompatTest {
|
||||
String tampered = token.substring(0, token.length() - 1)
|
||||
+ (token.endsWith("a") ? "b" : "a");
|
||||
|
||||
assertThatThrownBy(() -> openPostgresConnection(USERNAME, tampered))
|
||||
assertThatThrownBy(() -> openPostgresConnection(IAM_USERNAME, tampered))
|
||||
.isInstanceOf(SQLException.class)
|
||||
.hasMessageContaining("password authentication failed");
|
||||
}
|
||||
@@ -168,14 +177,14 @@ class RdsIamTokenCompatTest {
|
||||
GenerateAuthenticationTokenRequest.builder()
|
||||
.hostname(TestFixtures.proxyHost())
|
||||
.port(proxyPort)
|
||||
.username(USERNAME)
|
||||
.username(IAM_USERNAME)
|
||||
.region(REGION)
|
||||
.credentialsProvider(CREDENTIALS)
|
||||
.build());
|
||||
|
||||
String withoutSignature = token.replaceFirst("&X-Amz-Signature=[0-9a-f]+", "");
|
||||
|
||||
assertThatThrownBy(() -> openPostgresConnection(USERNAME, withoutSignature))
|
||||
assertThatThrownBy(() -> openPostgresConnection(IAM_USERNAME, withoutSignature))
|
||||
.isInstanceOf(SQLException.class)
|
||||
.hasMessageContaining("password authentication failed");
|
||||
}
|
||||
|
||||
+44
-17
@@ -40,6 +40,7 @@ class RdsJdbcCompatTest {
|
||||
StaticCredentialsProvider.create(AwsBasicCredentials.create("test", "test"));
|
||||
private static final Region REGION = TestFixtures.region();
|
||||
private static final String USERNAME = "admin";
|
||||
private static final String IAM_USERNAME = "iam_user";
|
||||
private static final String PASSWORD = "secret123";
|
||||
private static final String DATABASE = "app";
|
||||
private static final int PROXY_PORT_MIN = 7000;
|
||||
@@ -95,6 +96,7 @@ class RdsJdbcCompatTest {
|
||||
Connection connection = awaitPostgresConnection(USERNAME, PASSWORD);
|
||||
try {
|
||||
assertThat(selectOne(connection)).isEqualTo(1);
|
||||
createIamRole(connection);
|
||||
} finally {
|
||||
connection.close();
|
||||
}
|
||||
@@ -106,15 +108,26 @@ class RdsJdbcCompatTest {
|
||||
void connectWithIamAuthToken() throws Exception {
|
||||
assumeInstanceCreated();
|
||||
|
||||
String ungrantedToken = rds.utilities().generateAuthenticationToken(GenerateAuthenticationTokenRequest.builder()
|
||||
.hostname(TestFixtures.proxyHost()).port(proxyPort).username(USERNAME).region(REGION)
|
||||
.credentialsProvider(CREDENTIALS).build());
|
||||
assertThatThrownBy(() -> openPostgresConnection(USERNAME, ungrantedToken))
|
||||
.isInstanceOf(SQLException.class).hasMessageContaining("password authentication failed");
|
||||
|
||||
assertThatThrownBy(() -> openPostgresConnection(IAM_USERNAME, PASSWORD))
|
||||
.isInstanceOfSatisfying(SQLException.class,
|
||||
exception -> assertThat(exception.getSQLState()).isEqualTo("28000"))
|
||||
.hasMessageContaining("PAM authentication failed for user \"" + IAM_USERNAME + "\"");
|
||||
|
||||
String token = rds.utilities().generateAuthenticationToken(GenerateAuthenticationTokenRequest.builder()
|
||||
.hostname(TestFixtures.proxyHost())
|
||||
.port(proxyPort)
|
||||
.username(USERNAME)
|
||||
.username(IAM_USERNAME)
|
||||
.region(REGION)
|
||||
.credentialsProvider(CREDENTIALS)
|
||||
.build());
|
||||
|
||||
Connection connection = awaitPostgresConnection(USERNAME, token);
|
||||
Connection connection = awaitPostgresConnection(IAM_USERNAME, token);
|
||||
try {
|
||||
assertThat(selectOne(connection)).isEqualTo(1);
|
||||
} finally {
|
||||
@@ -131,7 +144,7 @@ class RdsJdbcCompatTest {
|
||||
String token = rds.utilities().generateAuthenticationToken(GenerateAuthenticationTokenRequest.builder()
|
||||
.hostname(TestFixtures.proxyHost())
|
||||
.port(proxyPort)
|
||||
.username(USERNAME)
|
||||
.username(IAM_USERNAME)
|
||||
.region(REGION)
|
||||
.credentialsProvider(CREDENTIALS)
|
||||
.build());
|
||||
@@ -139,7 +152,7 @@ class RdsJdbcCompatTest {
|
||||
String tamperedToken = token.substring(0, token.length() - 1)
|
||||
+ (token.endsWith("a") ? "b" : "a");
|
||||
|
||||
assertThatThrownBy(() -> openPostgresConnection(USERNAME, tamperedToken))
|
||||
assertThatThrownBy(() -> openPostgresConnection(IAM_USERNAME, tamperedToken))
|
||||
.isInstanceOf(SQLException.class)
|
||||
.hasMessageContaining("password authentication failed");
|
||||
}
|
||||
@@ -155,12 +168,12 @@ class RdsJdbcCompatTest {
|
||||
String token = rds.utilities().generateAuthenticationToken(GenerateAuthenticationTokenRequest.builder()
|
||||
.hostname("other-instance.example.local")
|
||||
.port(proxyPort)
|
||||
.username(USERNAME)
|
||||
.username(IAM_USERNAME)
|
||||
.region(REGION)
|
||||
.credentialsProvider(CREDENTIALS)
|
||||
.build());
|
||||
|
||||
assertThatThrownBy(() -> openPostgresConnection(USERNAME, token))
|
||||
assertThatThrownBy(() -> openPostgresConnection(IAM_USERNAME, token))
|
||||
.isInstanceOf(SQLException.class)
|
||||
.hasMessageContaining("password authentication failed");
|
||||
}
|
||||
@@ -168,7 +181,7 @@ class RdsJdbcCompatTest {
|
||||
@Test
|
||||
@Order(5)
|
||||
@DisplayName("IAM auth rejected on instance created without IAM")
|
||||
void iamAuthRejectedWhenDisabledAtCreate() {
|
||||
void iamAuthRejectedWhenDisabledAtCreate() throws Exception {
|
||||
assumeInstanceCreated();
|
||||
|
||||
// Create a separate instance with IAM disabled
|
||||
@@ -186,11 +199,14 @@ class RdsJdbcCompatTest {
|
||||
.build());
|
||||
|
||||
Integer noIamPort = response.dbInstance().endpoint().port();
|
||||
try (Connection admin = awaitPostgresConnection(USERNAME, PASSWORD, noIamPort)) {
|
||||
createIamRole(admin);
|
||||
}
|
||||
|
||||
String token = rds.utilities().generateAuthenticationToken(GenerateAuthenticationTokenRequest.builder()
|
||||
.hostname(TestFixtures.proxyHost())
|
||||
.port(noIamPort)
|
||||
.username(USERNAME)
|
||||
.username(IAM_USERNAME)
|
||||
.region(REGION)
|
||||
.credentialsProvider(CREDENTIALS)
|
||||
.build());
|
||||
@@ -198,7 +214,7 @@ class RdsJdbcCompatTest {
|
||||
// Non-IAM instance rejects IAM tokens. The rejection may happen at the
|
||||
// PostgreSQL auth layer ("password authentication failed") or at the TCP
|
||||
// level if the proxy doesn't forward non-IAM connections ("connection attempt failed").
|
||||
assertThatThrownBy(() -> openPostgresConnection(USERNAME, token, noIamPort))
|
||||
assertThatThrownBy(() -> openPostgresConnection(IAM_USERNAME, token, noIamPort))
|
||||
.isInstanceOf(SQLException.class);
|
||||
} finally {
|
||||
try {
|
||||
@@ -232,16 +248,20 @@ class RdsJdbcCompatTest {
|
||||
|
||||
Integer togglePort = response.dbInstance().endpoint().port();
|
||||
|
||||
try (Connection admin = awaitPostgresConnection(USERNAME, PASSWORD, togglePort)) {
|
||||
createIamRole(admin);
|
||||
}
|
||||
|
||||
// Should reject IAM when disabled
|
||||
String token1 = rds.utilities().generateAuthenticationToken(GenerateAuthenticationTokenRequest.builder()
|
||||
.hostname(TestFixtures.proxyHost())
|
||||
.port(togglePort)
|
||||
.username(USERNAME)
|
||||
.username(IAM_USERNAME)
|
||||
.region(REGION)
|
||||
.credentialsProvider(CREDENTIALS)
|
||||
.build());
|
||||
|
||||
assertThatThrownBy(() -> openPostgresConnection(USERNAME, token1, togglePort))
|
||||
assertThatThrownBy(() -> openPostgresConnection(IAM_USERNAME, token1, togglePort))
|
||||
.isInstanceOf(SQLException.class);
|
||||
|
||||
// Enable IAM via modify
|
||||
@@ -255,12 +275,12 @@ class RdsJdbcCompatTest {
|
||||
String token2 = rds.utilities().generateAuthenticationToken(GenerateAuthenticationTokenRequest.builder()
|
||||
.hostname(TestFixtures.proxyHost())
|
||||
.port(togglePort)
|
||||
.username(USERNAME)
|
||||
.username(IAM_USERNAME)
|
||||
.region(REGION)
|
||||
.credentialsProvider(CREDENTIALS)
|
||||
.build());
|
||||
|
||||
Connection connection = awaitPostgresConnection(USERNAME, token2, togglePort);
|
||||
Connection connection = awaitPostgresConnection(IAM_USERNAME, token2, togglePort);
|
||||
try {
|
||||
assertThat(selectOne(connection)).isEqualTo(1);
|
||||
} finally {
|
||||
@@ -276,12 +296,12 @@ class RdsJdbcCompatTest {
|
||||
String token3 = rds.utilities().generateAuthenticationToken(GenerateAuthenticationTokenRequest.builder()
|
||||
.hostname(TestFixtures.proxyHost())
|
||||
.port(togglePort)
|
||||
.username(USERNAME)
|
||||
.username(IAM_USERNAME)
|
||||
.region(REGION)
|
||||
.credentialsProvider(CREDENTIALS)
|
||||
.build());
|
||||
|
||||
assertThatThrownBy(() -> openPostgresConnection(USERNAME, token3, togglePort))
|
||||
assertThatThrownBy(() -> openPostgresConnection(IAM_USERNAME, token3, togglePort))
|
||||
.isInstanceOf(SQLException.class);
|
||||
} finally {
|
||||
try {
|
||||
@@ -321,12 +341,12 @@ class RdsJdbcCompatTest {
|
||||
String token = rds.utilities().generateAuthenticationToken(GenerateAuthenticationTokenRequest.builder()
|
||||
.hostname(TestFixtures.proxyHost())
|
||||
.port(proxyPort)
|
||||
.username(USERNAME)
|
||||
.username(IAM_USERNAME)
|
||||
.region(REGION)
|
||||
.credentialsProvider(CREDENTIALS)
|
||||
.build());
|
||||
|
||||
Connection iamConnection = awaitPostgresConnection(USERNAME, token);
|
||||
Connection iamConnection = awaitPostgresConnection(IAM_USERNAME, token);
|
||||
try {
|
||||
assertThat(selectOne(iamConnection)).isEqualTo(1);
|
||||
} finally {
|
||||
@@ -377,6 +397,13 @@ class RdsJdbcCompatTest {
|
||||
}
|
||||
}
|
||||
|
||||
private static void createIamRole(Connection connection) throws SQLException {
|
||||
try (Statement statement = connection.createStatement()) {
|
||||
statement.execute("CREATE ROLE " + IAM_USERNAME + " LOGIN PASSWORD '" + PASSWORD + "'");
|
||||
statement.execute("GRANT rds_iam TO " + IAM_USERNAME);
|
||||
}
|
||||
}
|
||||
|
||||
private static void assumeInstanceCreated() {
|
||||
Assumptions.assumeTrue(instanceCreated && proxyPort != null,
|
||||
"RDS JDBC tests require a created DB instance from the first step");
|
||||
|
||||
Reference in new issue
Block a user