Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

fix(rds): require database role grants for PostgreSQL IAM login (#5945)

David Phillips678c5b82a663
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
index 33c7f34a9f..701ae2b023 100644
--- 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
@@ -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");
}
diff --git a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/RdsJdbcCompatTest.java b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/RdsJdbcCompatTest.java
index 7d5976c1f2..b6bf89972b 100644
--- a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/RdsJdbcCompatTest.java
+++ b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/RdsJdbcCompatTest.java
@@ -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");
diff --git a/docs/services/rds.md b/docs/services/rds.md
index 20772a3ff7..2bc2969c05 100644
--- a/docs/services/rds.md
+++ b/docs/services/rds.md
@@ -843,7 +843,7 @@ FLOCI_STORAGE_HOST_PERSISTENT_PATH=/absolute/host/path/data
## Authentication
-The RDS auth proxy validates the master username and password at the proxy layer. All other database users are passed through directly to the backend engine — create them with standard SQL (`CREATE USER`) and connect as normal.
+The RDS auth proxy checks the master username and password at the proxy layer. Other database users authenticate against the backend engine. For PostgreSQL instances with IAM database authentication enabled, the database role's `rds_iam` membership also determines which authentication method it can use.
IAM database authentication is also supported. Set `--enable-iam-database-authentication` at instance creation time and use `aws rds generate-db-auth-token` to obtain a token.
@@ -881,7 +881,32 @@ arn:aws:rds-db:<region>:<account-id>:dbuser:<DbiResourceId>/<db-user-name>
Aurora cluster endpoints and their member instance endpoints use the cluster's `DbClusterResourceId`, including after member reboot or runtime recovery, and connections through an RDS Proxy use the proxy's `prx-…` resource id. A token that fails any of these checks is refused with the engine's ordinary authentication failure; the reason is written to Floci's log.
-On PostgreSQL, the token names a database role (`DBUser`) and the session runs as that role: `current_user` and `session_user` both report it, objects it creates are owned by it, and a token naming a role the database does not have is refused with `FATAL: role "..." does not exist`. Create the role first with `CREATE ROLE <name> WITH LOGIN` as the master user, and grant it whatever the application needs.
+On native PostgreSQL instance and cluster endpoints, the token names a database
+role (`DBUser`). The role must have its own `LOGIN` attribute and a direct or indirect grant of `rds_iam`, including when
+logging in as the master user. Standard RDS Proxy client IAM connections do not
+require that database IAM grant: the backend role still needs `LOGIN`, and the
+session remains bound to that role's identity. End-to-end DB Proxy IAM is not
+supported by the real database mode. Create an application role for a native
+database endpoint as the master user:
+
+```sql
+CREATE ROLE app_user LOGIN;
+GRANT rds_iam TO app_user;
+```
+
+On native database endpoints with IAM authentication enabled, membership in
+`rds_iam` makes IAM take
+precedence over the role's password. Roles without that membership continue to use
+passwords. These database-role checks apply independently of Floci's IAM policy
+enforcement setting. Disabling IAM database authentication restores ordinary
+password authentication for new connections. Password attempts refused because of that membership report
+`PAM authentication failed` with SQLSTATE `28000`.
+
+The IAM session runs as the token's role: `current_user` and `session_user` both
+report it, and objects it creates belong to it. A missing role or a role without
+`LOGIN` is refused with an authentication failure. Native database endpoints also
+require `rds_iam` membership. Grant the application any additional database
+privileges it needs.
Underneath, the proxy reaches the container as the master user and hands the session over to the token's role, so an IAM session that talks its way back to the master role, via `RESET SESSION AUTHORIZATION` and its variants, is terminated with `FATAL: permission denied to set session authorization` rather than being allowed to regain superuser. `SET ROLE` is untouched: PostgreSQL still permission-checks it against the token's role, exactly as on RDS. One difference from RDS: the proxy learns of the switch from PostgreSQL's own report, so when several statements are batched into a single query after the switch, their results are returned before the session is closed.
diff --git a/src/main/java/io/github/hectorvent/floci/services/rds/RdsService.java b/src/main/java/io/github/hectorvent/floci/services/rds/RdsService.java
index 66022f9eca..ff49d17364 100644
--- a/src/main/java/io/github/hectorvent/floci/services/rds/RdsService.java
+++ b/src/main/java/io/github/hectorvent/floci/services/rds/RdsService.java
@@ -61,6 +61,7 @@ import io.github.hectorvent.floci.services.rds.model.OptionGroupOption;
import io.github.hectorvent.floci.services.rds.model.PointInTimeRestoreRequest;
import io.github.hectorvent.floci.services.rds.model.RdsEvent;
import io.github.hectorvent.floci.services.rds.model.ReadReplicaRequest;
+import io.github.hectorvent.floci.services.rds.proxy.RdsAuthProxy;
import io.github.hectorvent.floci.services.rds.proxy.RdsProxyBinding;
import io.github.hectorvent.floci.services.rds.proxy.RdsProxyManager;
import io.github.hectorvent.floci.services.resourcegroupstagging.ResourceGroupsTaggingService;
@@ -5953,7 +5954,8 @@ public class RdsService implements Resettable, ResourceProvider {
: validateDbPasswordForScope(
proxyAccountId, targetRegion, targetId, user, pw),
proxyBinding(engine, proxy.getEndpointHost(), proxy.getProxyPort(),
- targetRegion, proxyAccountId, proxy.getDbProxyResourceId()));
+ targetRegion, proxyAccountId, proxy.getDbProxyResourceId()),
+ RdsAuthProxy.EndpointKind.STANDARD_DB_PROXY);
}
putTargetGroupForAccount(proxyAccountId, proxyKey, updatedTargetGroup);
if (updatedProxy != null) {
@@ -7997,7 +7999,8 @@ public class RdsService implements Resettable, ResourceProvider {
: validateDbPasswordForScope(
accountId, proxyRegion, targetId, user, password),
proxyBinding(engine, proxy.getEndpointHost(), proxy.getProxyPort(),
- proxyRegion, accountId, proxy.getDbProxyResourceId()));
+ proxyRegion, accountId, proxy.getDbProxyResourceId()),
+ RdsAuthProxy.EndpointKind.STANDARD_DB_PROXY);
}
private DbProxyTargetGroup copyProxyTargetGroup(DbProxyTargetGroup source) {
diff --git a/src/main/java/io/github/hectorvent/floci/services/rds/proxy/PostgresProtocolHandler.java b/src/main/java/io/github/hectorvent/floci/services/rds/proxy/PostgresProtocolHandler.java
index 498c29c883..284a93767d 100644
--- a/src/main/java/io/github/hectorvent/floci/services/rds/proxy/PostgresProtocolHandler.java
+++ b/src/main/java/io/github/hectorvent/floci/services/rds/proxy/PostgresProtocolHandler.java
@@ -1,6 +1,7 @@
package io.github.hectorvent.floci.services.rds.proxy;
import io.github.hectorvent.floci.core.common.Digests;
+import io.github.hectorvent.floci.core.common.auth.SigV4RequestValidator;
import org.jboss.logging.Logger;
import javax.crypto.Mac;
@@ -22,6 +23,7 @@ import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.Base64;
import java.util.HashMap;
+import java.util.HexFormat;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
@@ -121,6 +123,21 @@ public class PostgresProtocolHandler {
int handshakeTimeoutMillis,
boolean forwardStartupParameters) throws IOException {
+ return authenticate(client, backendConnector, masterUsername, masterPassword, dbName,
+ iamEnabled, sigV4, binding, tlsCertificates, passwordValidator,
+ handshakeTimeoutMillis, forwardStartupParameters, true);
+ }
+
+ /** Standard DB Proxy IAM does not enroll the backend role in database-native IAM. */
+ public static AuthenticatedSession authenticate(Socket client, BackendConnector backendConnector,
+ String masterUsername, String masterPassword, String dbName,
+ boolean iamEnabled, RdsSigV4Validator sigV4,
+ RdsProxyBinding binding,
+ RdsProxyTlsCertificates tlsCertificates,
+ PasswordValidator passwordValidator,
+ int handshakeTimeoutMillis, boolean forwardStartupParameters,
+ boolean databaseIamAuthentication) throws IOException {
+
client.setSoTimeout(handshakeTimeoutMillis);
// Phase 1: Read client startup message (possibly preceded by SSL request)
@@ -232,6 +249,24 @@ public class PostgresProtocolHandler {
// Buffer all backend messages until ReadyForQuery ('Z')
List<byte[]> bufferedMessages = readUntilReadyForQuery(backendIn);
+ // Native IAM-enabled PostgreSQL roles opt in through actual rds_iam membership. A
+ // superuser is not implicitly a member, and an opted-in role uses IAM rather
+ // than its password. Standard DB Proxy checks LOGIN without database IAM opt-in.
+ // Check before trying defaults or handing over the session.
+ DatabaseRole admittedRole = null;
+ if (iamEnabled && !endsWithErrorResponse(bufferedMessages)) {
+ admittedRole = readDatabaseRole(backendIn, backendOut, clientUsername, isIam, false, databaseIamAuthentication);
+ if (admittedRole == null || !admittedRole.allowed()) {
+ refuseDatabaseRole(clientOut, client, backend, clientUsername,
+ admittedRole != null && admittedRole.pamRequired(),
+ admittedRole == null ? "catalog lookup failed"
+ : !databaseIamAuthentication ? "LOGIN required"
+ : isIam ? "LOGIN and rds_iam membership required"
+ : "LOGIN without rds_iam membership required");
+ return null;
+ }
+ }
+
String iamRole = null;
List<SessionSetting> roleDefaultsAfterHandover = List.of();
List<byte[]> notices = new ArrayList<>();
@@ -370,8 +405,8 @@ public class PostgresProtocolHandler {
}
}
- // The role's client_encoding goes last, once the proxy sends no more SQL, and only
- // if the client did not choose an encoding itself.
+ // Apply the role's encoding only if the client did not choose one. The
+ // remaining role identity query is ASCII under either encoding.
if (roleEncoding != null && !clientChoseEncoding(startup.parameters())) {
DefaultOutcome outcome = applyDefault(backendIn, backendOut, roleEncoding, clientUsername);
if (outcome.failure() != null) {
@@ -391,6 +426,22 @@ public class PostgresProtocolHandler {
bufferedMessages = insertBeforeStartupTrailer(bufferedMessages, notices);
}
+ // Defaults can reopen the backend and handover resolves the role name again.
+ // Bind the final session to the admitted role OID, so dropping/recreating the
+ // name during login cannot hand a token to a different database role.
+ if (iamHandover && !endsWithErrorResponse(bufferedMessages)) {
+ DatabaseRole selectedRole = readDatabaseRole(backendIn, backendOut, clientUsername, true, true, databaseIamAuthentication);
+ if (selectedRole == null || !selectedRole.allowed() || admittedRole == null
+ || !selectedRole.oid().equals(admittedRole.oid())) {
+ refuseDatabaseRole(clientOut, client, backend, clientUsername, false,
+ selectedRole == null ? "final catalog lookup failed"
+ : !selectedRole.allowed() ? (databaseIamAuthentication
+ ? "final LOGIN and rds_iam membership required" : "final LOGIN required")
+ : "role identity changed during handover");
+ return null;
+ }
+ }
+
// Phase 6: Send AuthenticationOK to client, forward buffered messages, then bridge
if (endsWithErrorResponse(bufferedMessages)) {
for (byte[] msg : bufferedMessages) {
@@ -881,6 +932,89 @@ public class PostgresProtocolHandler {
return readUntilReadyForQuery(in);
}
+ /** A real PostgreSQL role identity and its choice of authentication method. */
+ private record DatabaseRole(String oid, boolean allowed, boolean pamRequired) {}
+
+ /**
+ * Follow catalog membership edges, including indirect grants. pg_has_role treats a
+ * superuser as privileged without a grant, which is not an IAM authentication opt-in.
+ * All SQL is ASCII even when the client selected a non-UTF8 encoding, and catalog
+ * objects and operators are qualified independently of the client's search_path.
+ */
+ static String databaseRoleQuery(String role, boolean iam, boolean sessionRole) {
+ return databaseRoleQuery(role, iam, sessionRole, true);
+ }
+
+ static String databaseRoleQuery(String role, boolean iam, boolean sessionRole,
+ boolean databaseIamAuthentication) {
+ String name = sessionRole ? "SESSION_USER" : "pg_catalog.convert_from(pg_catalog.decode('"
+ + HexFormat.of().formatHex(role.getBytes(StandardCharsets.UTF_8)) + "', 'hex'), 'UTF8')";
+ if (!databaseIamAuthentication) {
+ return "SELECT r.oid::pg_catalog.text, r.rolcanlogin FROM pg_catalog.pg_roles r "
+ + "WHERE r.rolname OPERATOR(pg_catalog.=) " + name;
+ }
+ String membership = "EXISTS (SELECT 1 FROM memberships m JOIN pg_catalog.pg_roles i "
+ + "ON i.oid OPERATOR(pg_catalog.=) m.oid "
+ + "WHERE i.rolname OPERATOR(pg_catalog.=) 'rds_iam')";
+ String admission = iam ? "r.rolcanlogin AND " + membership
+ : "CASE WHEN " + membership + " THEN 'pam' WHEN r.rolcanlogin THEN 't' ELSE 'f' END";
+ return "WITH RECURSIVE memberships(oid) AS ("
+ + "SELECT a.roleid FROM pg_catalog.pg_auth_members a JOIN pg_catalog.pg_roles r "
+ + "ON r.oid OPERATOR(pg_catalog.=) a.member WHERE r.rolname OPERATOR(pg_catalog.=) " + name
+ + " UNION SELECT a.roleid FROM pg_catalog.pg_auth_members a JOIN memberships m "
+ + "ON a.member OPERATOR(pg_catalog.=) m.oid) "
+ + "SELECT r.oid::pg_catalog.text, " + admission
+ + " FROM pg_catalog.pg_roles r WHERE r.rolname OPERATOR(pg_catalog.=) " + name;
+ }
+
+ private static DatabaseRole readDatabaseRole(InputStream in, OutputStream out, String role,
+ boolean iam, boolean sessionRole,
+ boolean databaseIamAuthentication) throws IOException {
+ List<byte[]> messages = runQuery(in, out,
+ databaseRoleQuery(role, iam, sessionRole, databaseIamAuthentication));
+ if (firstErrorResponse(messages) != null) {
+ logDatabaseRoleRefusal(role, "catalog query returned an error");
+ return null;
+ }
+ List<SessionSetting> rows = settingsFromDataRows(messages);
+ if (rows.size() != 1) {
+ logDatabaseRoleRefusal(role, rows.isEmpty() ? "database role not found" : "unexpected catalog row count");
+ return null;
+ }
+ SessionSetting row = rows.getFirst();
+ boolean pamRequired = databaseIamAuthentication && !iam && row.value().equals("pam");
+ if (!row.name().matches("[0-9]{1,10}")
+ || !(row.value().equals("t") || row.value().equals("f") || pamRequired)) {
+ logDatabaseRoleRefusal(role, "invalid catalog row format");
+ return null;
+ }
+ long oid = Long.parseLong(row.name());
+ if (oid == 0 || oid > 4_294_967_295L) {
+ logDatabaseRoleRefusal(role, "invalid database role OID");
+ return null;
+ }
+ return new DatabaseRole(row.name(), row.value().equals("t"), pamRequired);
+ }
+
+ private static void refuseDatabaseRole(OutputStream out, Socket client, Socket backend, String user,
+ boolean pamRequired, String reason)
+ throws IOException {
+ logDatabaseRoleRefusal(user, reason);
+ try {
+ sendErrorResponse(out, "FATAL", pamRequired ? "28000" : "28P01",
+ (pamRequired ? "PAM" : "password") + " authentication failed for user \"" + user + "\"");
+ out.flush();
+ } finally {
+ closeQuietly(client);
+ closeQuietly(backend);
+ }
+ }
+
+ private static void logDatabaseRoleRefusal(String user, String reason) {
+ LOG.infov("PostgreSQL role admission refused for user {0}: {1}",
+ SigV4RequestValidator.sanitizeForLog(user), reason);
+ }
+
static String quoteIdentifier(String identifier) {
return "\"" + identifier.replace("\"", "\"\"") + "\"";
}
diff --git a/src/main/java/io/github/hectorvent/floci/services/rds/proxy/RdsAuthProxy.java b/src/main/java/io/github/hectorvent/floci/services/rds/proxy/RdsAuthProxy.java
index 86397e1826..65ca18b19c 100644
--- a/src/main/java/io/github/hectorvent/floci/services/rds/proxy/RdsAuthProxy.java
+++ b/src/main/java/io/github/hectorvent/floci/services/rds/proxy/RdsAuthProxy.java
@@ -22,6 +22,11 @@ public class RdsAuthProxy {
private final int backendPort;
private volatile boolean iamEnabled;
+ private final EndpointKind endpointKind;
+
+ /** Native database IAM and standard client-to-proxy IAM have different role requirements. */
+ public enum EndpointKind { DATABASE, STANDARD_DB_PROXY }
+
private final String instanceId;
private final String backendHost;
private final String masterUsername;
@@ -78,11 +83,27 @@ public class RdsAuthProxy {
int maxConnections, RdsProxyBinding binding,
MySqlProtocolHandler.IamUserChecker iamUserChecker,
RdsBackendGate backendGate) {
+ this(instanceId, backendHost, backendPort, engine, iamEnabled, masterUsername, masterPassword,
+ dbName, sigV4, tlsCertificates, passwordValidator, handshakeTimeoutMillis,
+ backendConnectTimeoutMillis, maxConnections, binding, iamUserChecker, backendGate,
+ EndpointKind.DATABASE);
+ }
+
+ public RdsAuthProxy(String instanceId, String backendHost, int backendPort,
+ DatabaseEngine engine, boolean iamEnabled,
+ String masterUsername, String masterPassword, String dbName,
+ RdsSigV4Validator sigV4, RdsProxyTlsCertificates tlsCertificates,
+ MasterPasswordCheck passwordValidator,
+ int handshakeTimeoutMillis, int backendConnectTimeoutMillis,
+ int maxConnections, RdsProxyBinding binding,
+ MySqlProtocolHandler.IamUserChecker iamUserChecker,
+ RdsBackendGate backendGate, EndpointKind endpointKind) {
this.instanceId = instanceId;
this.backendHost = backendHost;
this.backendPort = backendPort;
this.engine = engine;
this.iamEnabled = iamEnabled;
+ this.endpointKind = endpointKind;
this.masterUsername = masterUsername;
this.masterPassword = masterPassword;
this.dbName = dbName;
@@ -210,7 +231,7 @@ public class RdsAuthProxy {
session = PostgresProtocolHandler.authenticate(
client, connector, masterUsername, masterPassword, dbName,
iamEnabled, sigV4, binding, tlsCertificates, authAdapter,
- handshakeTimeoutMillis, true);
+ handshakeTimeoutMillis, true, endpointKind == EndpointKind.DATABASE);
if (session != null) {
PostgresProtocolHandler.bridge(session);
}
diff --git a/src/main/java/io/github/hectorvent/floci/services/rds/proxy/RdsProxyManager.java b/src/main/java/io/github/hectorvent/floci/services/rds/proxy/RdsProxyManager.java
index ce65954e1d..1780786a85 100644
--- a/src/main/java/io/github/hectorvent/floci/services/rds/proxy/RdsProxyManager.java
+++ b/src/main/java/io/github/hectorvent/floci/services/rds/proxy/RdsProxyManager.java
@@ -44,13 +44,24 @@ public class RdsProxyManager {
String masterUsername, String masterPassword, String dbName,
RdsAuthProxy.MasterPasswordCheck passwordValidator,
RdsProxyBinding binding) {
+ startProxy(instanceId, engine, iamEnabled, proxyPort, backendHost, backendPort,
+ advertisedHost, masterUsername, masterPassword, dbName, passwordValidator,
+ binding, RdsAuthProxy.EndpointKind.DATABASE);
+ }
+
+ public synchronized void startProxy(String instanceId, DatabaseEngine engine, boolean iamEnabled,
+ int proxyPort, String backendHost, int backendPort,
+ String advertisedHost,
+ String masterUsername, String masterPassword, String dbName,
+ RdsAuthProxy.MasterPasswordCheck passwordValidator,
+ RdsProxyBinding binding, RdsAuthProxy.EndpointKind endpointKind) {
tlsCertificates.ensureHost(advertisedHost);
EmulatorConfig.RdsServiceConfig rdsConfig = config.services().rds();
RdsAuthProxy proxy = new RdsAuthProxy(
instanceId, backendHost, backendPort, engine, iamEnabled,
masterUsername, masterPassword, dbName, sigV4Validator, tlsCertificates, passwordValidator,
rdsConfig.proxyHandshakeTimeoutMillis(), rdsConfig.proxyBackendConnectTimeoutMillis(),
- rdsConfig.proxyMaxConnections(), binding, backendGate);
+ rdsConfig.proxyMaxConnections(), binding, username -> binding != null, backendGate, endpointKind);
try {
proxy.start(proxyPort);
} catch (IOException e) {
diff --git a/src/test/java/io/github/hectorvent/floci/services/rds/RdsIamAuthLambdaSessionIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/rds/RdsIamAuthLambdaSessionIntegrationTest.java
index 3294ef015e..333829c9e2 100644
--- a/src/test/java/io/github/hectorvent/floci/services/rds/RdsIamAuthLambdaSessionIntegrationTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/rds/RdsIamAuthLambdaSessionIntegrationTest.java
@@ -46,6 +46,7 @@ class RdsIamAuthLambdaSessionIntegrationTest {
private static final String ACCOUNT_ID = "000000000000";
private static final String MASTER_USER = "master";
+ private static final String IAM_USER = "iam_user";
private static final String MASTER_PASSWORD = "Master123!";
private static final String DATABASE = "appdb";
private static final String SESSION_SECRET = "lambda+session/secret=key";
@@ -89,13 +90,14 @@ class RdsIamAuthLambdaSessionIntegrationTest {
iamService.registerLambdaExecutionRoleSession(ACCOUNT_ID, accessKeyId, SESSION_SECRET, SESSION_TOKEN,
"arn:aws:iam::" + ACCOUNT_ID + ":role/connect-with-iam");
awaitPasswordReady(port);
+ createIamRole(port);
assertIamAuthentication(port, accessKeyId, true);
- String anotherSession = SigV4TokenTestHelper.createRdsToken("127.0.0.1", port, MASTER_USER,
+ String anotherSession = SigV4TokenTestHelper.createRdsToken("127.0.0.1", port, IAM_USER,
accessKeyId, SESSION_SECRET, Instant.now().minusSeconds(60), 900,
SESSION_TOKEN.replace('+', ' '));
- SQLException refused = assertThrows(SQLException.class, () -> connect(port, anotherSession).close());
+ SQLException refused = assertThrows(SQLException.class, () -> connect(port, IAM_USER, anotherSession).close());
assertTrue(refused.getMessage().contains("password authentication failed"), refused.getMessage());
} finally {
iamService.unregisterSession(ACCOUNT_ID, accessKeyId);
@@ -135,6 +137,9 @@ class RdsIamAuthLambdaSessionIntegrationTest {
.getInt("CreateDBInstanceResponse.CreateDBInstanceResult.DBInstance.Endpoint.Port");
iamService.registerLambdaExecutionRoleSession(ACCOUNT_ID, accessKeyId, SESSION_SECRET, SESSION_TOKEN,
"arn:aws:iam::" + ACCOUNT_ID + ":role/cluster-connect-with-iam");
+ // The cluster and member endpoints share the same PostgreSQL backend.
+ awaitPasswordReady(clusterPort);
+ createIamRole(clusterPort);
for (int port : new int[]{clusterPort, memberPort}) {
awaitPasswordReady(port);
assertIamAuthentication(port, accessKeyId, false);
@@ -189,6 +194,7 @@ class RdsIamAuthLambdaSessionIntegrationTest {
iamService.registerLambdaExecutionRoleSession(ACCOUNT_ID, accessKeyId, SESSION_SECRET, SESSION_TOKEN,
"arn:aws:iam::" + ACCOUNT_ID + ":role/member-connect-with-iam");
awaitPasswordReady(port);
+ createIamRole(port);
assertIamAuthentication(port, accessKeyId, enabled);
} finally {
iamService.unregisterSession(ACCOUNT_ID, accessKeyId);
@@ -233,15 +239,16 @@ class RdsIamAuthLambdaSessionIntegrationTest {
iamService.registerLambdaExecutionRoleSession(ACCOUNT_ID, accessKeyId, SESSION_SECRET, SESSION_TOKEN,
"arn:aws:iam::" + ACCOUNT_ID + ":role/connect-with-iam");
awaitPasswordReady(port);
- String token = SigV4TokenTestHelper.createRdsToken("127.0.0.1", port, MASTER_USER,
+ createIamRole(port);
+ String token = SigV4TokenTestHelper.createRdsToken("127.0.0.1", port, IAM_USER,
accessKeyId, SESSION_SECRET, Instant.now().minusSeconds(60), 900, SESSION_TOKEN);
- assertCurrentUser(port, token);
+ assertCurrentUser(port, IAM_USER, token);
rds("DescribeDBInstances").formParam("DBInstanceIdentifier", memberId)
.when().post("/").then().statusCode(200)
.body(containsString("<MasterUsername>" + MASTER_USER + "</MasterUsername>"));
rds("RebootDBInstance").formParam("DBInstanceIdentifier", memberId)
.when().post("/").then().statusCode(200);
- assertCurrentUser(port, token);
+ assertCurrentUser(port, IAM_USER, token);
} finally {
iamService.unregisterSession(ACCOUNT_ID, accessKeyId);
rds("DeleteDBInstance").formParam("DBInstanceIdentifier", memberId)
@@ -271,20 +278,21 @@ class RdsIamAuthLambdaSessionIntegrationTest {
iamService.registerLambdaExecutionRoleSession(ACCOUNT_ID, accessKey, SESSION_SECRET, SESSION_TOKEN,
"arn:aws:iam::" + ACCOUNT_ID + ":role/connect-with-iam");
awaitPasswordReady(port);
- String token = SigV4TokenTestHelper.createRdsToken("127.0.0.1", port, MASTER_USER,
+ createIamRole(port);
+ String token = SigV4TokenTestHelper.createRdsToken("127.0.0.1", port, IAM_USER,
accessKey, SESSION_SECRET, Instant.now().minusSeconds(60), 900, SESSION_TOKEN);
- assertCurrentUser(port, token);
+ assertCurrentUser(port, IAM_USER, token);
rds("ModifyDBInstance").formParam("DBInstanceIdentifier", member)
.formParam("MasterUserPassword", "MemberOnlyPassword!")
.formParam("PubliclyAccessible", "true").when().post("/").then().statusCode(200)
.body(containsString("<PubliclyAccessible>true</PubliclyAccessible>"));
assertCurrentUser(port, MASTER_PASSWORD);
- assertCurrentUser(port, token);
+ assertCurrentUser(port, IAM_USER, token);
assertThrows(SQLException.class, () -> connect(port, "MemberOnlyPassword!").close());
rds("RebootDBInstance").formParam("DBInstanceIdentifier", member)
.when().post("/").then().statusCode(200);
assertCurrentUser(port, MASTER_PASSWORD);
- assertCurrentUser(port, token);
+ assertCurrentUser(port, IAM_USER, token);
assertThrows(SQLException.class, () -> connect(port, "MemberOnlyPassword!").close());
} finally {
iamService.unregisterSession(ACCOUNT_ID, accessKey);
@@ -337,6 +345,14 @@ class RdsIamAuthLambdaSessionIntegrationTest {
awaitPasswordReady(port, MASTER_PASSWORD);
}
+ private static void createIamRole(int port) throws SQLException {
+ try (Connection connection = connect(port, MASTER_PASSWORD);
+ Statement statement = connection.createStatement()) {
+ statement.execute("CREATE ROLE " + IAM_USER + " LOGIN");
+ statement.execute("GRANT rds_iam TO " + IAM_USER);
+ }
+ }
+
private static void awaitPasswordReady(int port, String password) {
await().atMost(Duration.ofSeconds(60)).ignoreExceptions().until(() -> {
try (Connection connection = connect(port, password)) {
@@ -346,27 +362,35 @@ class RdsIamAuthLambdaSessionIntegrationTest {
}
private static void assertIamAuthentication(int port, String accessKeyId, boolean enabled) throws Exception {
- String token = SigV4TokenTestHelper.createRdsToken("127.0.0.1", port, MASTER_USER,
+ String token = SigV4TokenTestHelper.createRdsToken("127.0.0.1", port, IAM_USER,
accessKeyId, SESSION_SECRET, Instant.now().minusSeconds(60), 900, SESSION_TOKEN);
if (enabled) {
- assertCurrentUser(port, token);
+ assertCurrentUser(port, IAM_USER, token);
} else {
- assertThrows(SQLException.class, () -> connect(port, token).close());
+ assertThrows(SQLException.class, () -> connect(port, IAM_USER, token).close());
}
}
private static void assertCurrentUser(int port, String password) throws SQLException {
- try (Connection connection = connect(port, password);
+ assertCurrentUser(port, MASTER_USER, password);
+ }
+
+ private static void assertCurrentUser(int port, String user, String password) throws SQLException {
+ try (Connection connection = connect(port, user, password);
Statement statement = connection.createStatement();
ResultSet rows = statement.executeQuery("SELECT current_user")) {
assertTrue(rows.next());
- assertEquals(MASTER_USER, rows.getString(1));
+ assertEquals(user, rows.getString(1));
}
}
private static Connection connect(int port, String password) throws SQLException {
+ return connect(port, MASTER_USER, password);
+ }
+
+ private static Connection connect(int port, String user, String password) throws SQLException {
Properties properties = new Properties();
- properties.setProperty("user", MASTER_USER);
+ properties.setProperty("user", user);
properties.setProperty("password", password);
properties.setProperty("sslmode", "disable");
properties.setProperty("connectTimeout", "5");
diff --git a/src/test/java/io/github/hectorvent/floci/services/rds/RdsMemberIamResourceIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/rds/RdsMemberIamResourceIntegrationTest.java
index bc247efd43..c566989fc6 100644
--- a/src/test/java/io/github/hectorvent/floci/services/rds/RdsMemberIamResourceIntegrationTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/rds/RdsMemberIamResourceIntegrationTest.java
@@ -101,6 +101,10 @@ class RdsMemberIamResourceIntegrationTest {
return connection.isValid(5);
}
});
+ try (Connection connection = connect(port, MASTER_PASSWORD);
+ Statement statement = connection.createStatement()) {
+ statement.execute("GRANT rds_iam TO " + MASTER_USER);
+ }
String token = SigV4TokenTestHelper.createRdsToken("127.0.0.1", port, MASTER_USER,
accessKeyId, SESSION_SECRET, Instant.now().minusSeconds(60), 900, SESSION_TOKEN);
iamService.putRolePolicy(roleName, "connect", connectPolicy(resourceId));
diff --git a/src/test/java/io/github/hectorvent/floci/services/rds/RdsServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/rds/RdsServiceTest.java
index 7994994082..053a8a9205 100644
--- a/src/test/java/io/github/hectorvent/floci/services/rds/RdsServiceTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/rds/RdsServiceTest.java
@@ -1794,7 +1794,7 @@ class RdsServiceTest {
List.of(), List.of("mydb"), 90, 40);
ArgumentCaptor<RdsProxyBinding> binding = ArgumentCaptor.forClass(RdsProxyBinding.class);
verify(proxyManager).startProxy(any(), any(), anyBoolean(), eq(proxy.getProxyPort()), any(), anyInt(),
- any(), any(), any(), any(), any(), binding.capture());
+ any(), any(), any(), any(), any(), binding.capture(), eq(RdsAuthProxy.EndpointKind.STANDARD_DB_PROXY));
assertEquals(proxy.getEndpointHost(), proxy.getEndpoint());
assertEquals(new RdsProxyBinding(proxy.getEndpointHost(), proxy.getProxyPort(), "us-east-1",
"123456789012", proxy.getDbProxyResourceId(), true), binding.getValue());
@@ -1802,7 +1802,7 @@ class RdsServiceTest {
service.modifyDbProxy(proxy.getDbProxyName(), null, PROXY_AUTH, null, null, null,
null, null, null, "us-east-1");
verify(proxyManager).startProxy(any(), any(), eq(false), eq(proxy.getProxyPort()), any(), anyInt(),
- any(), any(), any(), any(), any(), binding.capture());
+ any(), any(), any(), any(), any(), binding.capture(), eq(RdsAuthProxy.EndpointKind.STANDARD_DB_PROXY));
assertEquals(new RdsProxyBinding(proxy.getEndpointHost(), proxy.getProxyPort(), "us-east-1",
"123456789012", proxy.getDbProxyResourceId(), true), binding.getValue());
clearInvocations(proxyManager);
@@ -6897,7 +6897,7 @@ class RdsServiceTest {
relayRunning.set(true);
return null;
}).when(proxyManager).startProxy(any(), any(), anyBoolean(), anyInt(), any(),
- anyInt(), any(), any(), any(), any(), any(), any());
+ anyInt(), any(), any(), any(), any(), any(), any(), eq(RdsAuthProxy.EndpointKind.STANDARD_DB_PROXY));
RdsService service = proxyStoreService(
regionResolver, config, proxies, targetGroups, instances, new InMemoryStorage<>());
DbProxyAuth requiredAuth = new DbProxyAuth(
@@ -6951,7 +6951,7 @@ class RdsServiceTest {
relayRunning.set(true);
return null;
}).when(proxyManager).startProxy(any(), any(), anyBoolean(), anyInt(), any(),
- anyInt(), any(), any(), any(), any(), any(), any());
+ anyInt(), any(), any(), any(), any(), any(), any(), eq(RdsAuthProxy.EndpointKind.STANDARD_DB_PROXY));
RdsService service = proxyStoreService(
regionResolver, config, proxies, targetGroups, instances, new InMemoryStorage<>());
DbProxyAuth requiredAuth = new DbProxyAuth(
@@ -7217,7 +7217,7 @@ class RdsServiceTest {
relayRunning.set(true);
return null;
}).when(proxyManager).startProxy(any(), any(), anyBoolean(), anyInt(), any(),
- anyInt(), any(), any(), any(), any(), any(), any());
+ anyInt(), any(), any(), any(), any(), any(), any(), eq(RdsAuthProxy.EndpointKind.STANDARD_DB_PROXY));
RdsService service = proxyStoreService(
regionResolver, config, proxies, targetGroups, instances, new InMemoryStorage<>());
@@ -7334,7 +7334,7 @@ class RdsServiceTest {
relayRunning.set(true);
return null;
}).when(proxyManager).startProxy(any(), any(), anyBoolean(), anyInt(), any(),
- anyInt(), any(), any(), any(), any(), any(), any());
+ anyInt(), any(), any(), any(), any(), any(), any(), eq(RdsAuthProxy.EndpointKind.STANDARD_DB_PROXY));
RdsService service = proxyStoreService(
regionResolver, config, proxies, targetGroups, instances, new InMemoryStorage<>());
@@ -7404,7 +7404,7 @@ class RdsServiceTest {
verify(proxyManager).startProxy(
eq("db-proxy:" + proxy.getDbProxyArn()), eq(DatabaseEngine.MYSQL),
anyBoolean(), eq(3306), eq("localhost"), eq(3306), any(),
- eq("admin"), eq("secret"), eq("app"), any(), any());
+ eq("admin"), eq("secret"), eq("app"), any(), any(), eq(RdsAuthProxy.EndpointKind.STANDARD_DB_PROXY));
verify(proxyManager).stopProxy("db-proxy:" + proxy.getDbProxyArn());
}
@@ -7433,7 +7433,7 @@ class RdsServiceTest {
relayRunning.set(true);
throw startupFailure;
}).when(proxyManager).startProxy(any(), any(), anyBoolean(), anyInt(), any(),
- anyInt(), any(), any(), any(), any(), any(), any());
+ anyInt(), any(), any(), any(), any(), any(), any(), eq(RdsAuthProxy.EndpointKind.STANDARD_DB_PROXY));
doAnswer(invocation -> {
relayRunning.set(false);
return null;
@@ -7944,7 +7944,7 @@ class RdsServiceTest {
relayRunning.set(true);
return null;
}).when(proxyManager).startProxy(any(), any(), anyBoolean(), anyInt(), any(),
- anyInt(), any(), any(), any(), any(), any(), any());
+ anyInt(), any(), any(), any(), any(), any(), any(), eq(RdsAuthProxy.EndpointKind.STANDARD_DB_PROXY));
IllegalStateException deleteFailure =
new IllegalStateException("simulated post-mutation target-group delete failure");
doAnswer(invocation -> {
@@ -8009,7 +8009,7 @@ class RdsServiceTest {
relayRunning.set(true);
return null;
}).when(proxyManager).startProxy(any(), any(), anyBoolean(), anyInt(), any(),
- anyInt(), any(), any(), any(), any(), any(), any());
+ anyInt(), any(), any(), any(), any(), any(), any(), eq(RdsAuthProxy.EndpointKind.STANDARD_DB_PROXY));
IllegalStateException deleteFailure =
new IllegalStateException("simulated post-mutation proxy delete failure");
doAnswer(invocation -> {
@@ -8395,7 +8395,7 @@ class RdsServiceTest {
assertEquals("insufficient-resource-limits", failed.getStatus());
verify(proxyManager, never()).startProxy(
eq("db-proxy:" + proxy.getDbProxyArn()), any(), anyBoolean(), anyInt(),
- any(), anyInt(), any(), any(), any(), any(), any(), any());
+ any(), anyInt(), any(), any(), any(), any(), any(), any(), eq(RdsAuthProxy.EndpointKind.STANDARD_DB_PROXY));
DbProxy another = service.createDbProxy(
"another-proxy", "MYSQL", true, false, "NONE", PROXY_ROLE_ARN,
@@ -8415,7 +8415,7 @@ class RdsServiceTest {
verify(proxyManager).startProxy(
eq("db-proxy:" + proxy.getDbProxyArn()), eq(DatabaseEngine.MYSQL),
anyBoolean(), eq(failed.getProxyPort()), any(), anyInt(),
- any(), any(), any(), any(), any(), any());
+ any(), any(), any(), any(), any(), any(), eq(RdsAuthProxy.EndpointKind.STANDARD_DB_PROXY));
}
@Test
@@ -8506,7 +8506,7 @@ class RdsServiceTest {
verify(restoredProxyManager).startProxy(eq("db-proxy:" + restored.getDbProxyArn()),
eq(DatabaseEngine.POSTGRES),
eq(false), anyInt(), eq("127.0.0.1"), eq(15432), any(),
- eq("admin"), eq("secret"), eq("app"), any(), any());
+ eq("admin"), eq("secret"), eq("app"), any(), any(), eq(RdsAuthProxy.EndpointKind.STANDARD_DB_PROXY));
}
@Test
@@ -8578,7 +8578,7 @@ class RdsServiceTest {
verify(restoredProxyManager).startProxy(eq("db-proxy:" + proxy.getDbProxyArn()),
eq(DatabaseEngine.POSTGRES),
eq(false), eq(5432), eq("127.0.0.1"), eq(15432), any(), eq("admin"),
- eq("target-secret"), eq("app"), validator.capture(), any());
+ eq("target-secret"), eq("app"), validator.capture(), any(), eq(RdsAuthProxy.EndpointKind.STANDARD_DB_PROXY));
assertTrue(validator.getValue().validate("admin", "target-secret"));
assertFalse(validator.getValue().validate("admin", "default-secret"));
assertTrue(rawProxies.get(targetAccount + "/us-east-1::app-proxy").isPresent());
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 c605481465..3bb7e4d645 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
@@ -4,6 +4,7 @@ import io.github.hectorvent.floci.config.FlociCertificateAuthority;
import io.github.hectorvent.floci.services.acm.CertificateGenerator;
import io.github.hectorvent.floci.services.acm.model.KeyAlgorithm;
import io.github.hectorvent.floci.testutil.IamServiceTestHelper;
+import io.github.hectorvent.floci.testutil.LogCapture;
import io.github.hectorvent.floci.testutil.SigV4TokenTestHelper;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
@@ -32,6 +33,7 @@ import java.nio.file.Path;
import java.security.KeyStore;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
+import java.text.MessageFormat;
import java.time.Instant;
import java.util.ArrayList;
import java.util.HashMap;
@@ -39,7 +41,9 @@ import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;
+import java.util.logging.LogRecord;
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
@@ -643,6 +647,7 @@ class PostgresProtocolHandlerTest {
writeParameterStatus(out, "application_name", "svc");
writeCommandComplete(out, "SELECT 1");
writeReadyForQuery(out);
+ answerRoleAdmission(in, out, "approle", true, true);
});
} catch (IOException e) {
throw new RuntimeException(e);
@@ -883,7 +888,8 @@ class PostgresProtocolHandlerTest {
writeCommandComplete(out, "SELECT 1");
writeReadyForQuery(out);
}
- });
+ answerRoleAdmission(in, out, "approle", true, true);
+ }, false);
} catch (IOException e) {
throw new RuntimeException(e);
}
@@ -944,7 +950,7 @@ class PostgresProtocolHandlerTest {
"SELECT pg_catalog.set_config(E'role', E'reader', false)",
"SELECT pg_catalog.set_config(E'application_name', E'svc', false)",
"SELECT pg_catalog.set_config(E'client_encoding', E'LATIN1', false)"),
- afterHandover, "role as the role, then the client's settings, then the role's encoding once no proxy SQL follows");
+ afterHandover, "role as the role, then the client's settings, then the role's encoding before the ASCII identity check");
}
@Test
@@ -1193,8 +1199,8 @@ class PostgresProtocolHandlerTest {
Map<Character, String> error = readErrorResponse(clientIn);
assertEquals("FATAL", error.get('S'));
- assertEquals("28000", error.get('C'));
- assertEquals("role \"nosuchrole\" does not exist", error.get('M'));
+ assertEquals("28P01", error.get('C'));
+ assertEquals("password authentication failed for user \"nosuchrole\"", error.get('M'));
authThread.join(5_000);
backendThread.join(5_000);
@@ -1202,7 +1208,7 @@ class PostgresProtocolHandlerTest {
assertEquals(false, backendThread.isAlive(), "backendThread did not terminate");
}
- assertEquals("SET SESSION AUTHORIZATION \"nosuchrole\"", backendQuery.get());
+ assertEquals(PostgresProtocolHandler.databaseRoleQuery("nosuchrole", true, false), backendQuery.get());
}
}
@@ -1240,7 +1246,8 @@ class PostgresProtocolHandlerTest {
int backendPort = backendServer.getLocalPort();
Thread backendThread = Thread.ofVirtual().start(() -> {
try {
- mockBackendStartup(backendServer, backendDatabase, false);
+ mockBackendMasterSession(backendServer, (in, out) ->
+ answerRoleAdmission(in, out, "dbadmin", true, false));
} catch (IOException e) {
throw new RuntimeException(e);
}
@@ -1260,8 +1267,8 @@ class PostgresProtocolHandlerTest {
writeStartup(clientOut, "dbadmin", "postgres");
readCleartextPasswordChallenge(clientIn);
writePassword(clientOut, rdsToken("dbadmin"));
- // The mock backend answers no query, so reaching ReadyForQuery proves no
- // SET SESSION AUTHORIZATION was issued for the master user.
+ // The backend answers only admission, so reaching ReadyForQuery proves
+ // no SET SESSION AUTHORIZATION was issued for the master user.
readAuthenticationOk(clientIn);
readReadyForQuery(clientIn);
@@ -1287,6 +1294,7 @@ class PostgresProtocolHandlerTest {
try {
mockBackendRoleSwitch(backendServer, new AtomicReference<>(), backendQuery,
"approle", true, (in, out) -> {
+ answerRoleAdmission(in, out, "approle", true, true);
readSimpleQuery(in);
// What PostgreSQL reports after RESET SESSION AUTHORIZATION and
// friends hand the session back to the connection's master role.
@@ -1345,6 +1353,7 @@ class PostgresProtocolHandlerTest {
try {
mockBackendRoleSwitch(backendServer, new AtomicReference<>(),
new AtomicReference<>(), "approle", true, (in, out) -> {
+ answerRoleAdmission(in, out, "approle", true, true);
readSimpleQuery(in);
writeParameterStatus(out, "session_authorization", "approle");
writeMessage(out, 'D', wideRow);
@@ -1402,6 +1411,7 @@ class PostgresProtocolHandlerTest {
Thread backendThread = Thread.ofVirtual().start(() -> {
try {
mockBackendMasterSession(backendServer, (in, out) -> {
+ answerRoleAdmission(in, out, "dbadmin", false, false);
readSimpleQuery(in);
writeParameterStatus(out, "session_authorization", "approle");
writeCommandComplete(out, "SET");
@@ -1493,6 +1503,147 @@ class PostgresProtocolHandlerTest {
}
}
+ @ParameterizedTest
+ @CsvSource({"true,replaced", "true,revoked", "true,missing", "true,malformed", "true,error",
+ "false,replaced", "false,revoked", "false,missing", "false,malformed", "false,error"})
+ void finalRoleAdmissionRefusesChangedOrUnverifiableSessionIdentity(boolean databaseIam, String fault) throws Exception {
+ assertAdmissionRefused(true, databaseIam, "approle", (in, out) -> {
+ assertEquals(PostgresProtocolHandler.databaseRoleQuery("approle", true, false, databaseIam), readSimpleQuery(in));
+ writeDataRow(out, "100", "t");
+ writeCommandComplete(out, "SELECT 1");
+ writeReadyForQuery(out);
+ assertEquals(true, readSimpleQuery(in).contains("pg_db_role_setting"));
+ writeCommandComplete(out, "SELECT 0");
+ writeReadyForQuery(out);
+ assertEquals("SET SESSION AUTHORIZATION \"approle\"", readSimpleQuery(in));
+ writeParameterStatus(out, "session_authorization", "approle");
+ writeCommandComplete(out, "SET");
+ writeReadyForQuery(out);
+ assertEquals(PostgresProtocolHandler.databaseRoleQuery("approle", true, true, databaseIam), readSimpleQuery(in));
+ switch (fault) {
+ case "replaced" -> writeDataRow(out, "101", "t");
+ case "revoked" -> writeDataRow(out, "100", "f");
+ case "malformed" -> writeDataRow(out, "not-an-oid", "t");
+ case "error" -> writeErrorResponse(out, "ERROR", "XX000", "catalog unavailable");
+ case "missing" -> { /* A role no longer present has no row. */ }
+ default -> throw new AssertionError(fault);
+ }
+ if (!fault.equals("error")) {
+ writeCommandComplete(out, fault.equals("missing") ? "SELECT 0" : "SELECT 1");
+ }
+ writeReadyForQuery(out);
+ assertEquals(-1, in.read(), "refusal must close the backend before any further query");
+ });
+ }
+
+ @ParameterizedTest
+ @ValueSource(booleans = {false, true})
+ void initialRoleRefusalPrecedesDefaultsAndHandover(boolean iam) throws Exception {
+ assertAdmissionRefused(iam, (in, out) -> {
+ assertEquals(PostgresProtocolHandler.databaseRoleQuery("approle", iam, false), readSimpleQuery(in));
+ writeDataRow(out, "100", "f");
+ writeCommandComplete(out, "SELECT 1");
+ writeReadyForQuery(out);
+ assertEquals(-1, in.read(), "no default, handover or startup query may follow initial refusal");
+ });
+ }
+
+ @ParameterizedTest
+ @CsvSource({"false,true,pam,28000", "true,true,pam,28P01", "false,false,pam,28P01",
+ "false,true,f,28P01", "false,true,unexpected,28P01"})
+ void pamRefusalIsConfinedToNativePasswordMembership(boolean iam, boolean databaseIam,
+ String admission, String code) throws Exception {
+ runAdmissionRefused(iam, databaseIam, "approle", (in, out) -> {
+ assertEquals(PostgresProtocolHandler.databaseRoleQuery("approle", iam, false, databaseIam), readSimpleQuery(in));
+ writeDataRow(out, "100", admission);
+ writeCommandComplete(out, "SELECT 1");
+ writeReadyForQuery(out);
+ assertEquals(-1, in.read(), "no defaults or handover may follow refusal");
+ }, code);
+ }
+
+ @Test
+ void roleRefusalLogsSanitizedIdentityWithoutBackendSecrets() throws Exception {
+ String user = "app\nrole\r";
+ List<LogRecord> records = LogCapture.capture(PostgresProtocolHandler.class, () ->
+ assertDoesNotThrow(() -> runAdmissionRefused(true, false, user, (in, out) -> {
+ assertEquals(PostgresProtocolHandler.databaseRoleQuery(user, true, false, false), readSimpleQuery(in));
+ writeErrorResponse(out, "ERROR", "XX000", "SECRET_SQL X-Amz-Credential=SECRET_TOKEN");
+ writeReadyForQuery(out);
+ assertEquals(-1, in.read());
+ })));
+ List<String> logs = records.stream().map(record -> MessageFormat.format(record.getMessage(), record.getParameters())).toList();
+ assertTrue(logs.stream().anyMatch(line -> line.contains("user approle: catalog query returned an error")));
+ assertFalse(logs.stream().anyMatch(line -> line.contains("SECRET_") || line.contains("X-Amz-")
+ || line.contains("\n") || line.contains("\r")));
+ }
+
+ private void assertAdmissionRefused(boolean iam, BackendScript script) throws Exception {
+ assertAdmissionRefused(iam, true, "approle", script);
+ }
+
+ private void assertAdmissionRefused(boolean iam, boolean databaseIam, String user, BackendScript script) {
+ List<LogRecord> records = LogCapture.capture(PostgresProtocolHandler.class, () ->
+ assertDoesNotThrow(() -> runAdmissionRefused(iam, databaseIam, user, script)));
+ assertTrue(records.stream().anyMatch(record -> record.getMessage().contains("PostgreSQL role admission refused")));
+ }
+
+ private void runAdmissionRefused(boolean iam, boolean databaseIam, String user, BackendScript script) throws Exception {
+ runAdmissionRefused(iam, databaseIam, user, script, "28P01");
+ }
+
+ private void runAdmissionRefused(boolean iam, boolean databaseIam, String user, BackendScript script,
+ String code) throws Exception {
+ AtomicReference<Throwable> backendFailure = new AtomicReference<>();
+ AtomicReference<Throwable> authFailure = new AtomicReference<>();
+ AtomicReference<PostgresProtocolHandler.AuthenticatedSession> result = new AtomicReference<>();
+ try (ServerSocket backendServer = new ServerSocket(0);
+ ServerSocket clientServer = new ServerSocket(0)) {
+ Thread backendThread = Thread.ofVirtual().start(() -> {
+ try {
+ mockBackendMasterSession(backendServer, script);
+ } catch (Throwable failure) {
+ backendFailure.set(failure);
+ }
+ });
+ try (Socket ourClient = new Socket("localhost", clientServer.getLocalPort());
+ Socket proxyClient = clientServer.accept();
+ Socket backend = new Socket("localhost", backendServer.getLocalPort())) {
+ ourClient.setSoTimeout(5_000);
+ Thread authThread = Thread.ofVirtual().start(() -> {
+ try {
+ result.set(PostgresProtocolHandler.authenticate(proxyClient, () -> backend,
+ "dbadmin", "adminpass", "postgres", true, testSigV4Validator(),
+ testBinding(), testTlsCertificates(),
+ (loginUser, pass) -> PasswordValidator.AuthResult.MASTER_EQUIVALENT, 5_000, true, databaseIam));
+ } catch (Throwable failure) {
+ authFailure.set(failure);
+ }
+ });
+ DataOutputStream out = new DataOutputStream(ourClient.getOutputStream());
+ DataInputStream in = new DataInputStream(ourClient.getInputStream());
+ writeStartup(out, user, "postgres");
+ readCleartextPasswordChallenge(in);
+ writePassword(out, iam ? rdsToken(user) : "adminpass");
+ Map<Character, String> refusal = readErrorResponse(in);
+ assertEquals("FATAL", refusal.get('S'));
+ assertEquals(code, refusal.get('C'));
+ assertEquals((code.equals("28000") ? "PAM" : "password")
+ + " authentication failed for user \"" + user + "\"", refusal.get('M'));
+ assertEquals(-1, in.read(), "neither AuthenticationOK nor ReadyForQuery may follow refusal");
+ authThread.join(5_000);
+ backendThread.join(5_000);
+ assertFalse(authThread.isAlive());
+ assertFalse(backendThread.isAlive());
+ assertNull(authFailure.get());
+ assertNull(backendFailure.get());
+ assertNull(result.get());
+ assertTrue(proxyClient.isClosed());
+ assertTrue(backend.isClosed());
+ }
+ }
+ }
+
private Thread startIamAuth(Socket proxyClient, Socket backend) {
return startIamAuth(proxyClient, backend, true, true);
}
@@ -1605,6 +1756,14 @@ class PostgresProtocolHandlerTest {
AtomicReference<String> backendQuery,
String role, boolean roleExists,
BackendScript afterHandover) throws IOException {
+ mockBackendRoleSwitch(server, backendStartup, backendQuery, role, roleExists, afterHandover, true);
+ }
+
+ private static void mockBackendRoleSwitch(ServerSocket server,
+ AtomicReference<Map<String, String>> backendStartup,
+ AtomicReference<String> backendQuery,
+ String role, boolean roleExists,
+ BackendScript afterHandover, boolean initialAdmission) throws IOException {
try (Socket socket = server.accept()) {
DataInputStream in = new DataInputStream(socket.getInputStream());
DataOutputStream out = new DataOutputStream(socket.getOutputStream());
@@ -1630,6 +1789,19 @@ class PostgresProtocolHandlerTest {
writeBackendKeyData(out);
writeReadyForQuery(out);
+ if (initialAdmission) {
+ String admission = readSimpleQuery(in);
+ assertEquals(PostgresProtocolHandler.databaseRoleQuery(role, true, false), admission);
+ if (!roleExists) {
+ backendQuery.set(admission);
+ writeCommandComplete(out, "SELECT 0");
+ writeReadyForQuery(out);
+ return;
+ }
+ writeDataRow(out, "100", "t");
+ writeCommandComplete(out, "SELECT 1");
+ writeReadyForQuery(out);
+ }
String query = readSimpleQuery(in);
if (query.contains("pg_db_role_setting")) {
// A role with no defaults: the proxy carries on with this session.
@@ -1652,6 +1824,8 @@ class PostgresProtocolHandlerTest {
if (afterHandover != null) {
afterHandover.run(in, out);
+ } else {
+ answerRoleAdmission(in, out, role, true, true);
}
}
}
@@ -1684,6 +1858,7 @@ class PostgresProtocolHandlerTest {
writeBackendKeyData(out);
writeReadyForQuery(out);
+ answerRoleAdmission(in, out, "approle", true, false);
assertEquals(true, readSimpleQuery(in).contains("pg_db_role_setting"));
for (String[] setting : roleDefaults) {
writeDataRow(out, setting[0], setting[1]);
@@ -1785,6 +1960,14 @@ class PostgresProtocolHandlerTest {
}
}
+ private static void answerRoleAdmission(DataInputStream in, DataOutputStream out, String role,
+ boolean iam, boolean sessionRole) throws IOException {
+ assertEquals(PostgresProtocolHandler.databaseRoleQuery(role, iam, sessionRole), readSimpleQuery(in));
+ writeDataRow(out, "100", "t");
+ writeCommandComplete(out, "SELECT 1");
+ writeReadyForQuery(out);
+ }
+
private static String readSimpleQuery(DataInputStream in) throws IOException {
assertEquals('Q', in.readByte());
byte[] query = in.readNBytes(in.readInt() - 4);
diff --git a/src/test/java/io/github/hectorvent/floci/services/rds/proxy/RdsPostgresIamRoleIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/rds/proxy/RdsPostgresIamRoleIntegrationTest.java
new file mode 100644
index 0000000000..b0cd292e0c
--- /dev/null
+++ b/src/test/java/io/github/hectorvent/floci/services/rds/proxy/RdsPostgresIamRoleIntegrationTest.java
@@ -0,0 +1,303 @@
+package io.github.hectorvent.floci.services.rds.proxy;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import io.github.hectorvent.floci.services.iam.IamService;
+import io.github.hectorvent.floci.services.rds.RdsService;
+import io.github.hectorvent.floci.testing.IamEnforcementProfile;
+import io.github.hectorvent.floci.testutil.SigV4TokenTestHelper;
+import io.quarkus.test.junit.QuarkusTest;
+import io.quarkus.test.junit.TestProfile;
+import jakarta.inject.Inject;
+import org.junit.jupiter.api.Tag;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.function.Executable;
+
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.time.Instant;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.Properties;
+
+import static io.github.hectorvent.floci.testing.Ec2Requests.ec2;
+import static io.github.hectorvent.floci.testing.RdsRequests.rds;
+import static io.restassured.RestAssured.given;
+import static org.junit.jupiter.api.Assertions.*;
+
+@QuarkusTest
+@Tag("docker")
+@TestProfile(IamEnforcementProfile.class)
+class RdsPostgresIamRoleIntegrationTest {
+ private static final String MASTER = "master";
+ private static final String PASSWORD = "Master123!";
+ private static final String APP_PASSWORD = "App123!";
+
+ @Inject
+ RdsService rdsService;
+
+ @Inject
+ IamService iamService;
+
+ @Test
+ void standardDbProxyAcceptsClientIamWithoutDatabaseIamMembership() throws Exception {
+ String id = uniqueId();
+ String proxy = id + "-proxy";
+ String role = id + "-policy";
+ List<Runnable> cleanup = new ArrayList<>();
+ try {
+ int databasePort = create(id);
+ cleanup.add(0, () -> delete(id));
+ asMaster(databasePort, "CREATE ROLE app_user LOGIN PASSWORD '" + APP_PASSWORD + "'");
+ String vpc = ec2("CreateVpc", "test", "us-east-1").formParam("CidrBlock", "10.97.0.0/16")
+ .post("/").then().statusCode(200).extract().xmlPath().getString("CreateVpcResponse.vpc.vpcId");
+ cleanup.add(0, () -> ec2("DeleteVpc", "test", "us-east-1").formParam("VpcId", vpc)
+ .post("/").then().statusCode(200));
+ List<String> subnets = new ArrayList<>();
+ for (int i = 0; i < 2; i++) {
+ String subnet = ec2("CreateSubnet", "test", "us-east-1").formParam("VpcId", vpc)
+ .formParam("CidrBlock", "10.97." + i + ".0/24")
+ .formParam("AvailabilityZone", i == 0 ? "us-east-1a" : "us-east-1b")
+ .post("/").then().statusCode(200).extract().xmlPath().getString("CreateSubnetResponse.subnet.subnetId");
+ subnets.add(subnet);
+ cleanup.add(0, () -> ec2("DeleteSubnet", "test", "us-east-1").formParam("SubnetId", subnet)
+ .post("/").then().statusCode(200));
+ }
+ String secret = given().header("Authorization", "AWS4-HMAC-SHA256 Credential=test/20261018/us-east-1/secretsmanager/aws4_request")
+ .header("X-Amz-Target", "secretsmanager.CreateSecret").contentType("application/x-amz-json-1.1")
+ .body(new ObjectMapper().writeValueAsBytes(Map.of("Name", id, "SecretString", "{\"username\":\"app_user\",\"password\":\"App123!\"}")))
+ .post("/").then().statusCode(200).extract().jsonPath().getString("ARN");
+ byte[] deleteSecret = new ObjectMapper().writeValueAsBytes(Map.of("SecretId", secret, "ForceDeleteWithoutRecovery", true));
+ cleanup.add(0, () -> given().header("Authorization", "AWS4-HMAC-SHA256 Credential=test/20261018/us-east-1/secretsmanager/aws4_request")
+ .header("X-Amz-Target", "secretsmanager.DeleteSecret").contentType("application/x-amz-json-1.1")
+ .body(deleteSecret).post("/").then().statusCode(200));
+ String roleArn = iamService.createRole(role, "/", "{}", null, 3600, Map.of()).getArn();
+ cleanup.add(0, () -> iamService.deleteRole(role));
+ rds("CreateDBProxy").formParam("DBProxyName", proxy).formParam("EngineFamily", "POSTGRESQL")
+ .formParam("RoleArn", roleArn).formParam("VpcSubnetIds.member.1", subnets.get(0))
+ .formParam("VpcSubnetIds.member.2", subnets.get(1)).formParam("Auth.member.1.AuthScheme", "SECRETS")
+ .formParam("Auth.member.1.SecretArn", secret).formParam("Auth.member.1.IAMAuth", "REQUIRED")
+ .post("/").then().statusCode(200);
+ cleanup.add(0, () -> rds("DeleteDBProxy").formParam("DBProxyName", proxy).post("/").then().statusCode(200));
+ rds("RegisterDBProxyTargets").formParam("DBProxyName", proxy).formParam("DBInstanceIdentifiers.member.1", id)
+ .post("/").then().statusCode(200);
+ int port = rdsService.getDbProxy(proxy).getProxyPort();
+ String resource = rdsService.getDbProxy(proxy).getDbProxyResourceId();
+ String accessKey = "ASIA" + Long.toString(System.nanoTime(), 36).toUpperCase();
+ String sessionSecret = "proxy-session-secret";
+ String sessionToken = "proxy-session-token";
+ iamService.registerLambdaExecutionRoleSession("000000000000", accessKey, sessionSecret, sessionToken, roleArn);
+ cleanup.add(0, () -> iamService.unregisterSession("000000000000", accessKey));
+ String policy = "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Action\":\"rds-db:connect\",\"Resource\":\"arn:aws:rds-db:us-east-1:000000000000:dbuser:" + resource + "/app_user\"}]}";
+ iamService.putRolePolicy(role, "connect", policy);
+ cleanup.add(0, () -> iamService.deleteRolePolicy(role, "connect"));
+ Properties iam = login("app_user", SigV4TokenTestHelper.createRdsToken("localhost", port, "app_user",
+ accessKey, sessionSecret, Instant.now(), 900, sessionToken));
+ assertUser(port, iam, "app_user");
+ refused(databasePort, token(databasePort, "app_user"));
+ asMaster(databasePort, "ALTER ROLE app_user NOLOGIN");
+ refused(port, iam);
+ asMaster(databasePort, "ALTER ROLE app_user LOGIN");
+ assertUser(port, iam, "app_user");
+ for (String mode : List.of("DISABLED", "REQUIRED")) {
+ rds("ModifyDBProxy").formParam("DBProxyName", proxy)
+ .formParam("Auth.member.1.AuthScheme", "SECRETS")
+ .formParam("Auth.member.1.SecretArn", secret).formParam("Auth.member.1.IAMAuth", mode)
+ .post("/").then().statusCode(200);
+ if (mode.equals("DISABLED")) {
+ refused(port, iam);
+ } else {
+ assertUser(port, iam, "app_user");
+ }
+ }
+ iamService.putRolePolicy(role, "connect", policy.replace(resource, "db-wrong-resource"));
+ refused(port, iam);
+ iamService.putRolePolicy(role, "connect", policy);
+ assertUser(port, iam, "app_user");
+ } finally {
+ assertAll(cleanup.stream().map(task -> (Executable) task::run));
+ }
+ }
+
+ @Test
+ void iamLoginRequiresMembershipAndTheRolesOwnLoginAttribute() throws Exception {
+ String id = uniqueId();
+ try {
+ int port = create(id);
+ asMaster(port, "CREATE ROLE app_user LOGIN PASSWORD '" + APP_PASSWORD + "'");
+ Properties token = token(port, "app_user");
+ refused(port, token);
+ asMaster(port, "GRANT rds_iam TO app_user");
+ assertUser(port, token, "app_user");
+ asMaster(port, "REVOKE rds_iam FROM app_user");
+ refused(port, token);
+ asMaster(port, "GRANT rds_iam TO app_user", "ALTER ROLE app_user NOLOGIN");
+ refused(port, token);
+ asMaster(port, "ALTER ROLE app_user LOGIN");
+ assertUser(port, token, "app_user");
+ } finally {
+ delete(id);
+ }
+ }
+
+ @Test
+ void iamMembershipTakesPrecedenceOverAnApplicationPassword() throws Exception {
+ String id = uniqueId();
+ try {
+ int port = create(id);
+ asMaster(port, "CREATE ROLE app_user LOGIN PASSWORD '" + APP_PASSWORD + "'");
+ Properties password = login("app_user", APP_PASSWORD);
+ assertUser(port, password, "app_user");
+ asMaster(port, "GRANT rds_iam TO app_user");
+ pamRefused(port, password);
+ assertUser(port, token(port, "app_user"), "app_user");
+ rds("ModifyDBInstance").formParam("DBInstanceIdentifier", id)
+ .formParam("EnableIAMDatabaseAuthentication", false).post("/").then().statusCode(200);
+ assertUser(port, password, "app_user");
+ rds("ModifyDBInstance").formParam("DBInstanceIdentifier", id)
+ .formParam("EnableIAMDatabaseAuthentication", true).post("/").then().statusCode(200);
+ pamRefused(port, password);
+ asMaster(port, "REVOKE rds_iam FROM app_user");
+ assertUser(port, password, "app_user");
+ } finally {
+ delete(id);
+ }
+ }
+
+ @Test
+ void theMasterNeedsAnActualGrantAndThenUsesIamInsteadOfItsPassword() throws Exception {
+ String id = uniqueId();
+ try {
+ int port = create(id);
+ Properties iam = token(port, MASTER);
+ refused(port, iam);
+ try (Connection admin = connect(port, login(MASTER, PASSWORD));
+ Statement sql = admin.createStatement()) {
+ sql.execute("GRANT rds_iam TO " + MASTER);
+ assertUser(port, iam, MASTER);
+ pamRefused(port, login(MASTER, PASSWORD));
+ // The established administrative session remains usable after a grant.
+ sql.execute("REVOKE rds_iam FROM " + MASTER);
+ }
+ assertUser(port, login(MASTER, PASSWORD), MASTER);
+ } finally {
+ delete(id);
+ }
+ }
+
+ @Test
+ void indirectMembershipWorksWithoutPrivilegeInheritance() throws Exception {
+ String id = uniqueId();
+ try {
+ int port = create(id);
+ asMaster(port, "CREATE ROLE iam_group NOLOGIN", "GRANT rds_iam TO iam_group",
+ "CREATE ROLE app_user LOGIN NOINHERIT PASSWORD '" + APP_PASSWORD + "'",
+ "GRANT iam_group TO app_user");
+ Properties iam = token(port, "app_user");
+ assertUser(port, iam, "app_user");
+ pamRefused(port, login("app_user", APP_PASSWORD));
+ asMaster(port, "REVOKE rds_iam FROM iam_group");
+ refused(port, iam);
+ assertUser(port, login("app_user", APP_PASSWORD), "app_user");
+ } finally {
+ delete(id);
+ }
+ }
+
+ @Test
+ void roleLookupIgnoresClientEncodingAndCatalogShadows() throws Exception {
+ String id = uniqueId();
+ String name = "café\"reader";
+ try {
+ int port = create(id);
+ asMaster(port, "CREATE ROLE \"café\"\"reader\" LOGIN PASSWORD '" + APP_PASSWORD + "'",
+ "GRANT rds_iam TO \"café\"\"reader\"", "CREATE SCHEMA shadow",
+ "CREATE TABLE shadow.pg_roles (oid oid, rolname name, rolcanlogin boolean)",
+ "CREATE TABLE shadow.pg_auth_members (roleid oid, member oid)",
+ "CREATE FUNCTION shadow.name_equals_text(name, text) RETURNS boolean "
+ + "LANGUAGE sql IMMUTABLE AS 'SELECT false'",
+ "CREATE OPERATOR shadow.= (LEFTARG = name, RIGHTARG = text, "
+ + "FUNCTION = shadow.name_equals_text)");
+ Properties password = login(name, APP_PASSWORD);
+ password.setProperty("options", "-c search_path=shadow,pg_catalog -c client_encoding=LATIN1");
+ pamRefused(port, password);
+ Properties iam = token(port, name);
+ iam.setProperty("options", "-c search_path=shadow,pg_catalog -c client_encoding=LATIN1");
+ iam.setProperty("allowEncodingChanges", "true");
+ assertUser(port, iam, name);
+ } finally {
+ delete(id);
+ }
+ }
+
+ private static void pamRefused(int port, Properties properties) {
+ SQLException exception = assertThrows(SQLException.class, () -> connect(port, properties).close());
+ assertAll(
+ () -> assertEquals("28000", exception.getSQLState()),
+ () -> assertEquals("FATAL: PAM authentication failed for user \""
+ + properties.getProperty("user") + "\"", exception.getMessage()));
+ }
+
+ private static void refused(int port, Properties properties) {
+ SQLException exception = assertThrows(SQLException.class, () -> connect(port, properties).close());
+ assertEquals("28P01", exception.getSQLState());
+ }
+
+ private static void assertUser(int port, Properties properties, String user) throws SQLException {
+ try (Connection connection = connect(port, properties);
+ Statement statement = connection.createStatement();
+ ResultSet rows = statement.executeQuery("SELECT session_user")) {
+ assertTrue(rows.next());
+ assertEquals(user, rows.getString(1));
+ assertFalse(rows.next());
+ }
+ }
+
+ private static void asMaster(int port, String... statements) throws SQLException {
+ try (Connection connection = connect(port, login(MASTER, PASSWORD));
+ Statement sql = connection.createStatement()) {
+ for (String statement : statements) {
+ sql.execute(statement);
+ }
+ }
+ }
+
+ private static Properties login(String user, String password) {
+ Properties properties = new Properties();
+ properties.setProperty("user", user);
+ properties.setProperty("password", password);
+ properties.setProperty("sslmode", "disable");
+ properties.setProperty("connectTimeout", "5");
+ return properties;
+ }
+
+ private static Properties token(int port, String user) throws Exception {
+ return login(user, SigV4TokenTestHelper.createRdsToken("localhost", port, user,
+ "test", "test", Instant.now(), 900));
+ }
+
+ private static Connection connect(int port, Properties properties) throws SQLException {
+ return DriverManager.getConnection("jdbc:postgresql://localhost:" + port + "/appdb", properties);
+ }
+
+ private static String uniqueId() {
+ return "iam-role-" + Long.toString(System.nanoTime(), 36);
+ }
+
+ private static int create(String id) {
+ return rds("CreateDBInstance").formParam("DBInstanceIdentifier", id).formParam("Engine", "postgres")
+ .formParam("MasterUsername", MASTER).formParam("MasterUserPassword", PASSWORD)
+ .formParam("DBName", "appdb").formParam("AllocatedStorage", 20)
+ .formParam("DBInstanceClass", "db.t3.micro").formParam("EnableIAMDatabaseAuthentication", true)
+ .post("/").then().statusCode(200).extract().xmlPath()
+ .getInt("CreateDBInstanceResponse.CreateDBInstanceResult.DBInstance.Endpoint.Port");
+ }
+
+ private static void delete(String id) {
+ rds("DeleteDBInstance").formParam("DBInstanceIdentifier", id).formParam("SkipFinalSnapshot", true)
+ .post("/").then().statusCode(200);
+ }
+}
diff --git a/src/test/java/io/github/hectorvent/floci/services/rds/proxy/RdsPostgresStartupParametersIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/rds/proxy/RdsPostgresStartupParametersIntegrationTest.java
index 5eba9fdaa2..16ed1df5ac 100644
--- a/src/test/java/io/github/hectorvent/floci/services/rds/proxy/RdsPostgresStartupParametersIntegrationTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/rds/proxy/RdsPostgresStartupParametersIntegrationTest.java
@@ -90,6 +90,7 @@ class RdsPostgresStartupParametersIntegrationTest {
try {
asMaster(port,
"CREATE ROLE app_user LOGIN",
+ "GRANT rds_iam TO app_user",
"CREATE SCHEMA floci_opts AUTHORIZATION app_user",
// A database that refuses \' in string literals must still accept the parameters.
"ALTER DATABASE appdb SET backslash_quote = off");
@@ -114,7 +115,7 @@ class RdsPostgresStartupParametersIntegrationTest {
String dbId = "startup-params-iam-escalation-" + Long.toString(System.nanoTime(), 36);
int port = createIamInstance(dbId);
try {
- asMaster(port, "CREATE ROLE app_user LOGIN");
+ asMaster(port, "CREATE ROLE app_user LOGIN", "GRANT rds_iam TO app_user");
for (String options : new String[] {
"-c session_authorization=" + MASTER_USER,
@@ -139,6 +140,7 @@ class RdsPostgresStartupParametersIntegrationTest {
asMaster(port,
"CREATE ROLE reader NOLOGIN",
"CREATE ROLE app_user LOGIN",
+ "GRANT rds_iam TO app_user",
"GRANT reader TO app_user",
"ALTER ROLE app_user SET work_mem = '32MB'",
"ALTER ROLE app_user IN DATABASE appdb SET work_mem = '48MB'",