fix(rds): run IAM PostgreSQL sessions as the role named in the token (#3106)

* fix(rds): run IAM PostgreSQL sessions as the role named in the token

The proxy validated the token and then connected as master. It now hands
the session over with SET SESSION AUTHORIZATION, so the role's own
privileges apply and an unknown role is refused.

Closes #3087

Signed-off-by: Matheus Oliveira <matheus.6148@gmail.com>

* fix(rds): keep IAM PostgreSQL sessions from switching back to master

The backend connection is master underneath, so RESET SESSION AUTHORIZATION
regained superuser. The proxy now watches PostgreSQL's own
session_authorization report and terminates the session when it changes.

Signed-off-by: Matheus Oliveira <matheus.6148@gmail.com>

---------

Signed-off-by: Matheus Oliveira <matheus.6148@gmail.com>
This commit is contained in:
Matheus Oliveira authored and GitHub committed 2026-09-05 21:24:39 -04:00
1 parent 5719e65c3c
commit c22ded931a
5 files changed
+811 -33

No files matched your search

@@ -3,6 +3,7 @@ package io.github.hectorvent.floci.services.rds.proxy;
import io.github.hectorvent.floci.config.EmulatorConfig;
import io.github.hectorvent.floci.services.acm.CertificateGenerator;
import io.github.hectorvent.floci.testutil.IamServiceTestHelper;
import io.github.hectorvent.floci.testutil.SigV4TokenTestHelper;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.junit.jupiter.params.ParameterizedTest;
@@ -19,6 +20,7 @@ import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.security.KeyStore;
import java.time.Instant;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;
@@ -210,13 +212,14 @@ class PostgresProtocolHandlerTest {
Thread authThread = Thread.ofVirtual().start(() -> {
try {
Socket activeClient = PostgresProtocolHandler.authenticate(
PostgresProtocolHandler.AuthenticatedSession session =
PostgresProtocolHandler.authenticate(
proxyClient, backend,
"dbadmin", "adminpass", "postgres",
false, testSigV4Validator(), testTlsCertificates(),
(user, pass) -> true);
if (activeClient != null) {
PostgresProtocolHandler.bridge(activeClient, backend);
if (session != null) {
PostgresProtocolHandler.bridge(session, backend);
}
} catch (IOException e) {
throw new RuntimeException(e);
@@ -267,13 +270,14 @@ class PostgresProtocolHandlerTest {
Thread authThread = Thread.ofVirtual().start(() -> {
try {
Socket activeClient = PostgresProtocolHandler.authenticate(
PostgresProtocolHandler.AuthenticatedSession session =
PostgresProtocolHandler.authenticate(
proxyClient, backend,
"dbadmin", "adminpass", "postgres",
false, testSigV4Validator(), testTlsCertificates(),
(user, pass) -> true);
if (activeClient != null) {
PostgresProtocolHandler.bridge(activeClient, backend);
if (session != null) {
PostgresProtocolHandler.bridge(session, backend);
}
} catch (IOException e) {
throw new RuntimeException(e);
@@ -324,13 +328,14 @@ class PostgresProtocolHandlerTest {
Thread authThread = Thread.ofVirtual().start(() -> {
try {
Socket activeClient = PostgresProtocolHandler.authenticate(
PostgresProtocolHandler.AuthenticatedSession session =
PostgresProtocolHandler.authenticate(
proxyClient, backend,
"dbadmin", "adminpass", "postgres",
false, testSigV4Validator(), testTlsCertificates(),
(user, pass) -> true);
if (activeClient != null) {
PostgresProtocolHandler.bridge(activeClient, backend);
if (session != null) {
PostgresProtocolHandler.bridge(session, backend);
}
} catch (IOException e) {
throw new RuntimeException(e);
@@ -390,13 +395,14 @@ class PostgresProtocolHandlerTest {
Thread authThread = Thread.ofVirtual().start(() -> {
try {
Socket activeClient = PostgresProtocolHandler.authenticate(
PostgresProtocolHandler.AuthenticatedSession session =
PostgresProtocolHandler.authenticate(
proxyClient, backend,
"dbadmin", "adminpass", "postgres",
false, testSigV4Validator(), tlsCertificates,
(user, pass) -> true);
if (activeClient != null) {
PostgresProtocolHandler.bridge(activeClient, backend);
if (session != null) {
PostgresProtocolHandler.bridge(session, backend);
}
} catch (IOException e) {
throw new RuntimeException(e);
@@ -443,13 +449,14 @@ class PostgresProtocolHandlerTest {
Thread authThread = Thread.ofVirtual().start(() -> {
try {
Socket activeClient = PostgresProtocolHandler.authenticate(
PostgresProtocolHandler.AuthenticatedSession session =
PostgresProtocolHandler.authenticate(
proxyClient, backend,
"dbadmin", "adminpass", "postgres",
false, testSigV4Validator(), tlsCertificates,
(user, pass) -> true);
if (activeClient != null) {
PostgresProtocolHandler.bridge(activeClient, backend);
if (session != null) {
PostgresProtocolHandler.bridge(session, backend);
}
} catch (IOException ignored) {
// Client aborts the handshake below — the handler observing that is expected.
@@ -492,13 +499,14 @@ class PostgresProtocolHandlerTest {
Thread authThread = Thread.ofVirtual().start(() -> {
try {
Socket activeClient = PostgresProtocolHandler.authenticate(
PostgresProtocolHandler.AuthenticatedSession session =
PostgresProtocolHandler.authenticate(
proxyClient, backend,
"dbadmin", "adminpass", "postgres",
false, testSigV4Validator(), testTlsCertificates(),
(user, pass) -> true);
if (activeClient != null) {
PostgresProtocolHandler.bridge(activeClient, backend);
if (session != null) {
PostgresProtocolHandler.bridge(session, backend);
}
} catch (IOException e) {
throw new RuntimeException(e);
@@ -525,6 +533,533 @@ class PostgresProtocolHandlerTest {
}
}
@Test
void iamSessionRunsAsTheRoleNamedInTheToken() throws Exception {
AtomicReference<String> backendUser = new AtomicReference<>();
AtomicReference<String> backendQuery = new AtomicReference<>();
try (ServerSocket backendServer = new ServerSocket(0);
ServerSocket clientServer = new ServerSocket(0)) {
int backendPort = backendServer.getLocalPort();
Thread backendThread = Thread.ofVirtual().start(() -> {
try {
mockBackendRoleSwitch(backendServer, backendUser, backendQuery, "approle", true);
} catch (IOException e) {
throw new RuntimeException(e);
}
});
Socket proxyClient;
try (Socket ourClient = new Socket("localhost", clientServer.getLocalPort())) {
ourClient.setSoTimeout(5_000);
proxyClient = clientServer.accept();
Socket backend = new Socket("localhost", backendPort);
Thread authThread = startIamAuth(proxyClient, backend);
DataOutputStream clientOut = new DataOutputStream(ourClient.getOutputStream());
DataInputStream clientIn = new DataInputStream(ourClient.getInputStream());
writeStartup(clientOut, "approle", "postgres");
readCleartextPasswordChallenge(clientIn);
writePassword(clientOut, rdsToken("approle"));
readAuthenticationOk(clientIn);
Map<String, String> params = readParametersUntilReadyForQuery(clientIn);
assertEquals("approle", params.get("session_authorization"));
assertEquals("off", params.get("is_superuser"));
ourClient.close();
proxyClient.close();
authThread.join(5_000);
backendThread.join(5_000);
assertEquals(false, authThread.isAlive(), "authThread did not terminate");
assertEquals(false, backendThread.isAlive(), "backendThread did not terminate");
}
assertEquals("dbadmin", backendUser.get(), "backend connection is still opened as master");
assertEquals("SET SESSION AUTHORIZATION \"approle\"", backendQuery.get());
}
}
@Test
void iamSessionIsRejectedWhenTheTokenRoleDoesNotExist() throws Exception {
AtomicReference<String> backendUser = new AtomicReference<>();
AtomicReference<String> backendQuery = new AtomicReference<>();
try (ServerSocket backendServer = new ServerSocket(0);
ServerSocket clientServer = new ServerSocket(0)) {
int backendPort = backendServer.getLocalPort();
Thread backendThread = Thread.ofVirtual().start(() -> {
try {
mockBackendRoleSwitch(backendServer, backendUser, backendQuery, "nosuchrole", false);
} catch (IOException e) {
throw new RuntimeException(e);
}
});
Socket proxyClient;
try (Socket ourClient = new Socket("localhost", clientServer.getLocalPort())) {
ourClient.setSoTimeout(5_000);
proxyClient = clientServer.accept();
Socket backend = new Socket("localhost", backendPort);
Thread authThread = startIamAuth(proxyClient, backend);
DataOutputStream clientOut = new DataOutputStream(ourClient.getOutputStream());
DataInputStream clientIn = new DataInputStream(ourClient.getInputStream());
writeStartup(clientOut, "nosuchrole", "postgres");
readCleartextPasswordChallenge(clientIn);
writePassword(clientOut, rdsToken("nosuchrole"));
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'));
authThread.join(5_000);
backendThread.join(5_000);
assertEquals(false, authThread.isAlive(), "authThread did not terminate");
assertEquals(false, backendThread.isAlive(), "backendThread did not terminate");
}
assertEquals("SET SESSION AUTHORIZATION \"nosuchrole\"", backendQuery.get());
}
}
@Test
void iamSessionForTheMasterUserKeepsTheMasterSession() throws Exception {
AtomicReference<String> backendDatabase = new AtomicReference<>();
try (ServerSocket backendServer = new ServerSocket(0);
ServerSocket clientServer = new ServerSocket(0)) {
int backendPort = backendServer.getLocalPort();
Thread backendThread = Thread.ofVirtual().start(() -> {
try {
mockBackendStartup(backendServer, backendDatabase, false);
} catch (IOException e) {
throw new RuntimeException(e);
}
});
Socket proxyClient;
try (Socket ourClient = new Socket("localhost", clientServer.getLocalPort())) {
ourClient.setSoTimeout(5_000);
proxyClient = clientServer.accept();
Socket backend = new Socket("localhost", backendPort);
Thread authThread = startIamAuth(proxyClient, backend);
DataOutputStream clientOut = new DataOutputStream(ourClient.getOutputStream());
DataInputStream clientIn = new DataInputStream(ourClient.getInputStream());
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.
readAuthenticationOk(clientIn);
readReadyForQuery(clientIn);
ourClient.close();
proxyClient.close();
authThread.join(5_000);
backendThread.join(5_000);
assertEquals(false, authThread.isAlive(), "authThread did not terminate");
assertEquals(false, backendThread.isAlive(), "backendThread did not terminate");
}
}
}
@Test
void iamSessionIsTerminatedWhenPostgresReportsAnotherSessionRole() throws Exception {
AtomicReference<String> backendQuery = new AtomicReference<>();
try (ServerSocket backendServer = new ServerSocket(0);
ServerSocket clientServer = new ServerSocket(0)) {
int backendPort = backendServer.getLocalPort();
Thread backendThread = Thread.ofVirtual().start(() -> {
try {
mockBackendRoleSwitch(backendServer, new AtomicReference<>(), backendQuery,
"approle", true, (in, out) -> {
readSimpleQuery(in);
// What PostgreSQL reports after RESET SESSION AUTHORIZATION and
// friends hand the session back to the connection's master role.
writeParameterStatus(out, "session_authorization", "masteruser");
writeParameterStatus(out, "is_superuser", "on");
writeCommandComplete(out, "RESET");
writeReadyForQuery(out);
});
} catch (IOException e) {
throw new RuntimeException(e);
}
});
Socket proxyClient;
try (Socket ourClient = new Socket("localhost", clientServer.getLocalPort())) {
ourClient.setSoTimeout(5_000);
proxyClient = clientServer.accept();
Socket backend = new Socket("localhost", backendPort);
Thread authThread = startIamAuth(proxyClient, backend);
DataOutputStream clientOut = new DataOutputStream(ourClient.getOutputStream());
DataInputStream clientIn = new DataInputStream(ourClient.getInputStream());
writeStartup(clientOut, "approle", "postgres");
readCleartextPasswordChallenge(clientIn);
writePassword(clientOut, rdsToken("approle"));
readAuthenticationOk(clientIn);
readParametersUntilReadyForQuery(clientIn);
writeSimpleQuery(clientOut, "RESET SESSION AUTHORIZATION");
Map<Character, String> error = readErrorResponse(clientIn);
assertEquals("FATAL", error.get('S'));
assertEquals("42501", error.get('C'));
assertEquals("permission denied to set session authorization", error.get('M'));
assertEquals(-1, clientIn.read(), "session must be closed, not handed back as master");
authThread.join(5_000);
backendThread.join(5_000);
assertEquals(false, authThread.isAlive(), "authThread did not terminate");
}
}
}
@Test
void iamSessionKeepsRelayingWhenTheReportedRoleIsUnchanged() throws Exception {
byte[] wideRow = new byte[40_000];
java.util.Arrays.fill(wideRow, (byte) 'x');
try (ServerSocket backendServer = new ServerSocket(0);
ServerSocket clientServer = new ServerSocket(0)) {
int backendPort = backendServer.getLocalPort();
Thread backendThread = Thread.ofVirtual().start(() -> {
try {
mockBackendRoleSwitch(backendServer, new AtomicReference<>(),
new AtomicReference<>(), "approle", true, (in, out) -> {
readSimpleQuery(in);
writeParameterStatus(out, "session_authorization", "approle");
writeMessage(out, 'D', wideRow);
writeCommandComplete(out, "SELECT 1");
writeReadyForQuery(out);
});
} catch (IOException e) {
throw new RuntimeException(e);
}
});
Socket proxyClient;
try (Socket ourClient = new Socket("localhost", clientServer.getLocalPort())) {
ourClient.setSoTimeout(5_000);
proxyClient = clientServer.accept();
Socket backend = new Socket("localhost", backendPort);
Thread authThread = startIamAuth(proxyClient, backend);
DataOutputStream clientOut = new DataOutputStream(ourClient.getOutputStream());
DataInputStream clientIn = new DataInputStream(ourClient.getInputStream());
writeStartup(clientOut, "approle", "postgres");
readCleartextPasswordChallenge(clientIn);
writePassword(clientOut, rdsToken("approle"));
readAuthenticationOk(clientIn);
readParametersUntilReadyForQuery(clientIn);
writeSimpleQuery(clientOut, "SET SESSION AUTHORIZATION approle");
assertEquals('S', clientIn.readByte());
clientIn.readNBytes(clientIn.readInt() - 4);
assertEquals('D', clientIn.readByte());
// A message larger than the relay buffer must arrive whole.
assertEquals(wideRow.length, clientIn.readInt() - 4);
assertEquals(wideRow.length, clientIn.readNBytes(wideRow.length).length);
assertEquals('C', clientIn.readByte());
clientIn.readNBytes(clientIn.readInt() - 4);
readReadyForQuery(clientIn);
ourClient.close();
proxyClient.close();
authThread.join(5_000);
backendThread.join(5_000);
}
}
}
@Test
void nonIamSessionRelaysSessionAuthorizationChanges() throws Exception {
try (ServerSocket backendServer = new ServerSocket(0);
ServerSocket clientServer = new ServerSocket(0)) {
int backendPort = backendServer.getLocalPort();
Thread backendThread = Thread.ofVirtual().start(() -> {
try {
mockBackendMasterSession(backendServer, (in, out) -> {
readSimpleQuery(in);
writeParameterStatus(out, "session_authorization", "approle");
writeCommandComplete(out, "SET");
writeReadyForQuery(out);
});
} catch (IOException e) {
throw new RuntimeException(e);
}
});
Socket proxyClient;
try (Socket ourClient = new Socket("localhost", clientServer.getLocalPort())) {
ourClient.setSoTimeout(5_000);
proxyClient = clientServer.accept();
Socket backend = new Socket("localhost", backendPort);
Thread authThread = startIamAuth(proxyClient, backend);
DataOutputStream clientOut = new DataOutputStream(ourClient.getOutputStream());
DataInputStream clientIn = new DataInputStream(ourClient.getInputStream());
writeStartup(clientOut, "dbadmin", "postgres");
readCleartextPasswordChallenge(clientIn);
writePassword(clientOut, "adminpass");
readAuthenticationOk(clientIn);
readReadyForQuery(clientIn);
// The master session is the backend's own identity, so switching roles is its
// business. The guard applies only to IAM sessions.
writeSimpleQuery(clientOut, "SET SESSION AUTHORIZATION approle");
Map<String, String> params = readParametersUntilReadyForQuery(clientIn);
assertEquals("approle", params.get("session_authorization"));
ourClient.close();
proxyClient.close();
authThread.join(5_000);
backendThread.join(5_000);
}
}
}
@Test
void quotesRoleNamesContainingDoubleQuotes() {
assertEquals("\"app\"\"role\"", PostgresProtocolHandler.quoteIdentifier("app\"role"));
}
private Thread startIamAuth(Socket proxyClient, Socket backend) {
return Thread.ofVirtual().start(() -> {
try {
PostgresProtocolHandler.AuthenticatedSession session =
PostgresProtocolHandler.authenticate(
proxyClient, backend,
"dbadmin", "adminpass", "postgres",
true, testSigV4Validator(), testTlsCertificates(),
(user, pass) -> true);
if (session != null) {
PostgresProtocolHandler.bridge(session, backend);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
});
}
private static String rdsToken(String dbUser) throws Exception {
return SigV4TokenTestHelper.createRdsToken("localhost", 7001, dbUser,
"AKIATEST", "secret", Instant.now(), 900);
}
/**
* Mock backend that completes startup as master and then answers one simple query: either
* reporting the handover to {@code role}, or replying with the ErrorResponse PostgreSQL sends
* for a role the database does not have.
*/
private static void mockBackendRoleSwitch(ServerSocket server, AtomicReference<String> backendUser,
AtomicReference<String> backendQuery,
String role, boolean roleExists) throws IOException {
mockBackendRoleSwitch(server, backendUser, backendQuery, role, roleExists, null);
}
private static void mockBackendRoleSwitch(ServerSocket server, AtomicReference<String> backendUser,
AtomicReference<String> backendQuery,
String role, boolean roleExists,
BackendScript afterHandover) throws IOException {
try (Socket socket = server.accept()) {
DataInputStream in = new DataInputStream(socket.getInputStream());
DataOutputStream out = new DataOutputStream(socket.getOutputStream());
int length = in.readInt();
assertEquals(STARTUP_PROTOCOL_VERSION, in.readInt());
byte[] payload = in.readNBytes(length - 8);
backendUser.set(parseStartupParams(payload).get("user"));
out.writeByte('R');
out.writeInt(8);
out.writeInt(3);
out.flush();
assertEquals('p', in.readByte());
in.readNBytes(in.readInt() - 4);
out.writeByte('R');
out.writeInt(8);
out.writeInt(0);
writeParameterStatus(out, "session_authorization", "dbadmin");
writeParameterStatus(out, "is_superuser", "on");
writeBackendKeyData(out);
writeReadyForQuery(out);
assertEquals('Q', in.readByte());
byte[] query = in.readNBytes(in.readInt() - 4);
backendQuery.set(new String(query, 0, query.length - 1, StandardCharsets.UTF_8));
if (!roleExists) {
writeErrorResponse(out, "ERROR", "42704", "role \"" + role + "\" does not exist");
writeReadyForQuery(out);
return;
}
writeParameterStatus(out, "session_authorization", role);
writeParameterStatus(out, "is_superuser", "off");
writeCommandComplete(out, "SET");
writeReadyForQuery(out);
if (afterHandover != null) {
afterHandover.run(in, out);
}
}
}
/** Drives the backend side of a bridged session, once the client is past authentication. */
@FunctionalInterface
private interface BackendScript {
void run(DataInputStream in, DataOutputStream out) throws IOException;
}
/** Backend that completes a plain master startup and then runs {@code script}. */
private static void mockBackendMasterSession(ServerSocket server, BackendScript script)
throws IOException {
try (Socket socket = server.accept()) {
DataInputStream in = new DataInputStream(socket.getInputStream());
DataOutputStream out = new DataOutputStream(socket.getOutputStream());
int length = in.readInt();
assertEquals(STARTUP_PROTOCOL_VERSION, in.readInt());
in.readNBytes(length - 8);
out.writeByte('R');
out.writeInt(8);
out.writeInt(3);
out.flush();
assertEquals('p', in.readByte());
in.readNBytes(in.readInt() - 4);
out.writeByte('R');
out.writeInt(8);
out.writeInt(0);
writeReadyForQuery(out);
script.run(in, out);
}
}
private static String readSimpleQuery(DataInputStream in) throws IOException {
assertEquals('Q', in.readByte());
byte[] query = in.readNBytes(in.readInt() - 4);
return new String(query, 0, query.length - 1, StandardCharsets.UTF_8);
}
private static void writeMessage(DataOutputStream out, char type, byte[] payload)
throws IOException {
out.writeByte(type);
out.writeInt(4 + payload.length);
out.write(payload);
out.flush();
}
private static void writeParameterStatus(DataOutputStream out, String name, String value)
throws IOException {
byte[] nameBytes = name.getBytes(StandardCharsets.UTF_8);
byte[] valueBytes = value.getBytes(StandardCharsets.UTF_8);
out.writeByte('S');
out.writeInt(4 + nameBytes.length + 1 + valueBytes.length + 1);
out.write(nameBytes);
out.writeByte(0);
out.write(valueBytes);
out.writeByte(0);
out.flush();
}
private static void writeBackendKeyData(DataOutputStream out) throws IOException {
out.writeByte('K');
out.writeInt(12);
out.writeInt(4242);
out.writeInt(1234);
out.flush();
}
private static void writeCommandComplete(DataOutputStream out, String tag) throws IOException {
byte[] tagBytes = tag.getBytes(StandardCharsets.UTF_8);
out.writeByte('C');
out.writeInt(4 + tagBytes.length + 1);
out.write(tagBytes);
out.writeByte(0);
out.flush();
}
private static void writeReadyForQuery(DataOutputStream out) throws IOException {
out.writeByte('Z');
out.writeInt(5);
out.writeByte('I');
out.flush();
}
/** Collects ParameterStatus values the client is handed, stopping at ReadyForQuery. */
private static Map<String, String> readParametersUntilReadyForQuery(DataInputStream in)
throws IOException {
Map<String, String> params = new HashMap<>();
while (true) {
int type = in.readByte();
byte[] payload = in.readNBytes(in.readInt() - 4);
if (type == 'Z') {
return params;
}
if (type == 'S') {
int nameEnd = 0;
while (payload[nameEnd] != 0) {
nameEnd++;
}
int valueEnd = nameEnd + 1;
while (payload[valueEnd] != 0) {
valueEnd++;
}
params.put(new String(payload, 0, nameEnd, StandardCharsets.UTF_8),
new String(payload, nameEnd + 1, valueEnd - nameEnd - 1, StandardCharsets.UTF_8));
}
}
}
private static Map<Character, String> readErrorResponse(DataInputStream in) throws IOException {
assertEquals('E', in.readByte());
byte[] payload = in.readNBytes(in.readInt() - 4);
Map<Character, String> fields = new HashMap<>();
int i = 0;
while (i < payload.length && payload[i] != 0) {
char fieldType = (char) payload[i];
i++;
int start = i;
while (payload[i] != 0) {
i++;
}
fields.put(fieldType, new String(payload, start, i - start, StandardCharsets.UTF_8));
i++;
}
return fields;
}
private static RdsSigV4Validator testSigV4Validator() {
return new RdsSigV4Validator(IamServiceTestHelper.iamServiceWithAccessKey("AKIATEST", "secret"));
}