Skip to content

armgit / floci

Public Imported from GitHub
0

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

fix(rds): forward PostgreSQL startup parameters through the proxy (#5151)

jwoa951199f3a7902a
diff --git a/docs/services/rds.md b/docs/services/rds.md
index 0992e26f3e..a859705cd7 100644
--- a/docs/services/rds.md
+++ b/docs/services/rds.md
@@ -667,6 +667,8 @@ On PostgreSQL, the token names a database role (`DBUser`) and the session runs a
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.
+The parameters a PostgreSQL client sends when it connects (`options` such as `-c search_path=app`, `application_name`, and any other run-time parameter) apply to the session as they do on RDS, with one exception: an IAM session for a role other than the master starts without them. The proxy would have to apply them while the session is still the master's, and a `-c role=<master>` among them would outlive the handover. Set them with `SET` once connected instead.
+
On MySQL, `AWSAuthenticationPlugin` is proprietary to RDS and ships in no public MySQL build, so
`CREATE USER ... IDENTIFIED WITH AWSAuthenticationPlugin AS 'RDS'` would fail against the container
with `ERROR 1524 (HY000): Plugin 'AWSAuthenticationPlugin' is not loaded`. The proxy rewrites that
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 ab37beccfa..fa465eb559 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
@@ -21,6 +21,7 @@ import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.Base64;
import java.util.HashMap;
+import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;
@@ -85,6 +86,10 @@ public class PostgresProtocolHandler {
return new BackendLogin(clientUsername, clientPassword);
}
+ /**
+ * Authenticates the client, opening the backend session with only {@code user} and
+ * {@code database}: the client's other StartupMessage parameters are not forwarded.
+ */
public static AuthenticatedSession authenticate(Socket client, BackendConnector backendConnector,
String masterUsername, String masterPassword, String dbName,
boolean iamEnabled, RdsSigV4Validator sigV4,
@@ -92,6 +97,24 @@ public class PostgresProtocolHandler {
RdsProxyTlsCertificates tlsCertificates,
PasswordValidator passwordValidator,
int handshakeTimeoutMillis) throws IOException {
+ return authenticate(client, backendConnector, masterUsername, masterPassword, dbName,
+ iamEnabled, sigV4, binding, tlsCertificates, passwordValidator,
+ handshakeTimeoutMillis, false);
+ }
+
+ /**
+ * Authenticates the client. With {@code forwardStartupParameters}, the client's other
+ * StartupMessage parameters ({@code options}, {@code application_name} and any run-time
+ * parameter) open the backend session too, so they apply as they would on the server itself.
+ */
+ 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) throws IOException {
client.setSoTimeout(handshakeTimeoutMillis);
@@ -176,12 +199,31 @@ public class PostgresProtocolHandler {
masterUsername, masterPassword, clientUsername, clientPassword);
String backendUser = backendLogin.user();
String backendPass = backendLogin.password();
- sendStartupToBackend(backendOut, backendUser, effectiveDbName);
+ // An IAM login for any role but the master runs on a session opened as the master and
+ // handed over below, so PostgreSQL would apply the client's parameters with the
+ // master's privileges: a "-c role=<master>" would outlive the handover and RESET ROLE
+ // would regain the master. Those logins send only user and database.
+ Map<String, String> clientParameters = forwardStartupParameters && !(isIam && !isMaster)
+ ? startup.parameters() : Map.of();
+ sendStartupToBackend(backendOut, backendUser, effectiveDbName, clientParameters);
backendOut.flush();
- if (!authenticateWithBackend(backendIn, backendOut, backendUser, backendPass)) {
- sendErrorResponse(clientOut, "FATAL", "08006",
- "Backend database authentication failed");
+ boolean backendAuthenticated = false;
+ byte[] backendRejection = null;
+ try {
+ backendAuthenticated = authenticateWithBackend(backendIn, backendOut, backendUser, backendPass);
+ } catch (BackendRejectedException e) {
+ backendRejection = e.errorResponse();
+ }
+ if (!backendAuthenticated) {
+ // PostgreSQL's own refusal, such as a missing pg_hba.conf entry for a forwarded
+ // replication=true, says far more than the proxy's generic error.
+ if (backendRejection != null) {
+ clientOut.write(backendRejection);
+ } else {
+ sendErrorResponse(clientOut, "FATAL", "08006",
+ "Backend database authentication failed");
+ }
clientOut.flush();
closeQuietly(client);
closeQuietly(backend);
@@ -270,7 +312,8 @@ public class PostgresProtocolHandler {
Map<String, String> params = parseStartupParams(payload);
return new StartupMessage(currentSocket,
params.getOrDefault("user", "postgres"),
- params.get("database"));
+ params.get("database"),
+ params);
}
}
@@ -286,7 +329,8 @@ public class PostgresProtocolHandler {
}
}
- private record StartupMessage(Socket socket, String username, String database) {}
+ private record StartupMessage(Socket socket, String username, String database,
+ Map<String, String> parameters) {}
static String resolveEffectiveDbName(String clientDatabase, String instanceDbName) {
if (clientDatabase != null && !clientDatabase.isBlank()) {
@@ -302,8 +346,8 @@ public class PostgresProtocolHandler {
return !messages.isEmpty() && messages.get(messages.size() - 1)[0] == 'E';
}
- private static Map<String, String> parseStartupParams(byte[] data) {
- Map<String, String> params = new HashMap<>();
+ static Map<String, String> parseStartupParams(byte[] data) {
+ Map<String, String> params = new LinkedHashMap<>();
int i = 0;
while (i < data.length) {
int keyStart = i;
@@ -329,25 +373,35 @@ public class PostgresProtocolHandler {
return params;
}
- private static void sendStartupToBackend(OutputStream out, String username, String dbName)
- throws IOException {
- byte[] userKey = "user".getBytes(StandardCharsets.UTF_8);
- byte[] userVal = username.getBytes(StandardCharsets.UTF_8);
- byte[] dbKey = "database".getBytes(StandardCharsets.UTF_8);
- byte[] dbVal = dbName.getBytes(StandardCharsets.UTF_8);
-
- int length = 4 + 4
- + userKey.length + 1 + userVal.length + 1
- + dbKey.length + 1 + dbVal.length + 1
- + 1; // final null
+ /**
+ * Writes the backend StartupMessage: the proxy's own {@code user} and {@code database}, then
+ * the client's other parameters in the order the client sent them. {@code _pq_.} names request
+ * protocol extensions, which the proxy does not negotiate with the backend.
+ */
+ private static void sendStartupToBackend(OutputStream out, String username, String dbName,
+ Map<String, String> clientParameters) throws IOException {
+ ByteArrayOutputStream parameters = new ByteArrayOutputStream();
+ writeStartupParameter(parameters, "user", username);
+ writeStartupParameter(parameters, "database", dbName);
+ for (Map.Entry<String, String> parameter : clientParameters.entrySet()) {
+ String name = parameter.getKey();
+ if (!"user".equals(name) && !"database".equals(name) && !name.startsWith("_pq_.")) {
+ writeStartupParameter(parameters, name, parameter.getValue());
+ }
+ }
+ parameters.write(0); // final null
- writeInt32(out, length);
+ writeInt32(out, 4 + 4 + parameters.size());
writeInt32(out, STARTUP_PROTOCOL_VERSION);
- out.write(userKey); out.write(0);
- out.write(userVal); out.write(0);
- out.write(dbKey); out.write(0);
- out.write(dbVal); out.write(0);
- out.write(0); // final null
+ parameters.writeTo(out);
+ }
+
+ private static void writeStartupParameter(ByteArrayOutputStream out, String name, String value)
+ throws IOException {
+ out.write(name.getBytes(StandardCharsets.UTF_8));
+ out.write(0);
+ out.write(value.getBytes(StandardCharsets.UTF_8));
+ out.write(0);
}
// ── Client auth phase ─────────────────────────────────────────────────────
@@ -376,7 +430,7 @@ public class PostgresProtocolHandler {
private static boolean authenticateWithBackend(InputStream in, OutputStream out,
String username, String password) throws IOException {
- int type = in.read();
+ int type = readAuthenticationType(in);
if (type != 'R') {
LOG.warnv("Expected Authentication ('R') from backend, got type={0}", type);
return false;
@@ -422,6 +476,33 @@ public class PostgresProtocolHandler {
return false;
}
+ /**
+ * Reads the type byte of a message the handshake expects to be an Authentication one. When
+ * PostgreSQL refuses the login instead, the whole ErrorResponse is read and thrown so the
+ * client sees PostgreSQL's reason.
+ */
+ private static int readAuthenticationType(InputStream in) throws IOException {
+ int type = in.read();
+ if (type == 'E') {
+ throw new BackendRejectedException(readMessage(in, type));
+ }
+ return type;
+ }
+
+ /** PostgreSQL refused the backend login with {@code errorResponse}, a complete message. */
+ private static final class BackendRejectedException extends IOException {
+ private final byte[] errorResponse;
+
+ BackendRejectedException(byte[] errorResponse) {
+ super("Backend rejected the login: " + errorMessage(errorResponse, "no message"));
+ this.errorResponse = errorResponse;
+ }
+
+ byte[] errorResponse() {
+ return errorResponse;
+ }
+ }
+
// ── SCRAM-SHA-256 ─────────────────────────────────────────────────────────
private static boolean performScramSha256(InputStream in, OutputStream out,
@@ -445,7 +526,7 @@ public class PostgresProtocolHandler {
out.flush();
// Step 2: Read AuthenticationSASLContinue (authType=11)
- if (in.read() != 'R') {
+ if (readAuthenticationType(in) != 'R') {
return false;
}
int len2 = checkedPacketLength(readInt32(in), 8, "SASL continue message");
@@ -486,7 +567,7 @@ public class PostgresProtocolHandler {
out.flush();
// Step 4: Read AuthenticationSASLFinal (authType=12) — server signature (ignored)
- if (in.read() != 'R') {
+ if (readAuthenticationType(in) != 'R') {
return false;
}
int len3 = checkedPacketLength(readInt32(in), 8, "SASL final message");
@@ -560,7 +641,7 @@ public class PostgresProtocolHandler {
// ── MD5 password ──────────────────────────────────────────────────────────
private static boolean readAuthOk(InputStream in) throws IOException {
- int type = in.read();
+ int type = readAuthenticationType(in);
if (type != 'R') {
LOG.warnv("Expected AuthenticationOK from backend, got type={0}", type);
return false;
@@ -605,19 +686,7 @@ public class PostgresProtocolHandler {
if (type < 0) {
throw new EOFException("Connection closed before ReadyForQuery");
}
- int length = checkedPacketLength(readInt32(in), 4, "backend message");
- byte[] payload = new byte[length - 4];
- readFully(in, payload);
-
- // Reconstruct full message: type + length(4) + payload
- byte[] full = new byte[1 + 4 + payload.length];
- full[0] = (byte) type;
- full[1] = (byte) ((length >> 24) & 0xFF);
- full[2] = (byte) ((length >> 16) & 0xFF);
- full[3] = (byte) ((length >> 8) & 0xFF);
- full[4] = (byte) (length & 0xFF);
- System.arraycopy(payload, 0, full, 5, payload.length);
- messages.add(full);
+ messages.add(readMessage(in, type));
if (type == 'Z') { // ReadyForQuery
break;
@@ -630,6 +699,23 @@ public class PostgresProtocolHandler {
return messages;
}
+ /** Reads the rest of a backend message whose type byte was {@code type}, returning it whole. */
+ private static byte[] readMessage(InputStream in, int type) throws IOException {
+ int length = checkedPacketLength(readInt32(in), 4, "backend message");
+ byte[] payload = new byte[length - 4];
+ readFully(in, payload);
+
+ // Reconstruct full message: type + length(4) + payload
+ byte[] full = new byte[1 + 4 + payload.length];
+ full[0] = (byte) type;
+ full[1] = (byte) ((length >> 24) & 0xFF);
+ full[2] = (byte) ((length >> 16) & 0xFF);
+ full[3] = (byte) ((length >> 8) & 0xFF);
+ full[4] = (byte) (length & 0xFF);
+ System.arraycopy(payload, 0, full, 5, payload.length);
+ return full;
+ }
+
// ── IAM session role ──────────────────────────────────────────────────────
/**
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 dbe302b37e..efc619f77f 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
@@ -184,10 +184,12 @@ public class RdsAuthProxy {
switch (engine) {
case POSTGRES -> {
+ // forwardStartupParameters = true: an RDS for PostgreSQL endpoint is the
+ // server itself, so the client's options and run-time parameters apply.
session = PostgresProtocolHandler.authenticate(
client, connector, masterUsername, masterPassword, dbName,
iamEnabled, sigV4, binding, tlsCertificates, authAdapter,
- handshakeTimeoutMillis);
+ handshakeTimeoutMillis, true);
if (session != null) {
PostgresProtocolHandler.bridge(session);
}
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 7b61cae3c2..83d808cdd5 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
@@ -8,6 +8,7 @@ import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
+import org.junit.jupiter.params.provider.ValueSource;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
@@ -23,6 +24,8 @@ import java.nio.file.Path;
import java.security.KeyStore;
import java.time.Instant;
import java.util.HashMap;
+import java.util.LinkedHashMap;
+import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;
import javax.net.ssl.SSLContext;
@@ -163,6 +166,36 @@ class PostgresProtocolHandlerTest {
error.getMessage());
}
+ @ParameterizedTest
+ @ValueSource(booleans = {false, true})
+ void relaysPostgresRefusalOfTheBackendLogin(boolean afterPassword) throws Exception {
+ ByteArrayOutputStream backendInput = new ByteArrayOutputStream();
+ DataOutputStream backendOut = new DataOutputStream(backendInput);
+ if (afterPassword) {
+ backendOut.writeByte('R');
+ backendOut.writeInt(8);
+ backendOut.writeInt(3);
+ }
+ String reason = "no pg_hba.conf entry for replication connection from host \"172.17.0.1\", "
+ + "user \"dbadmin\", no encryption";
+ writeErrorResponse(backendOut, "FATAL", "28000", reason);
+ MemorySocket client = new MemorySocket(startupAndPassword());
+
+ assertNull(PostgresProtocolHandler.authenticate(
+ client, () -> new MemorySocket(backendInput.toByteArray()),
+ "dbadmin", "adminpass", "postgres",
+ false, testSigV4Validator(), testBinding(), testTlsCertificates(),
+ (user, pass) -> PasswordValidator.AuthResult.MASTER_EQUIVALENT, 5000));
+
+ DataInputStream clientIn = new DataInputStream(
+ new ByteArrayInputStream(client.getOutputStream().toByteArray()));
+ readCleartextPasswordChallenge(clientIn);
+ Map<Character, String> error = readErrorResponse(clientIn);
+ assertEquals("FATAL", error.get('S'));
+ assertEquals("28000", error.get('C'));
+ assertEquals(reason, error.get('M'));
+ }
+
@Test
void rejectsOversizedSaslContinuationMessage() throws Exception {
ByteArrayOutputStream backendInput = new ByteArrayOutputStream();
@@ -254,6 +287,39 @@ class PostgresProtocolHandlerTest {
}
}
+ @Test
+ void forwardsClientStartupParametersToBackendInOrder() throws Exception {
+ Map<String, String> clientStartup = new LinkedHashMap<>();
+ clientStartup.put("user", "dbadmin");
+ clientStartup.put("database", "auth_db");
+ clientStartup.put("application_name", "psql");
+ clientStartup.put("options", "-c search_path=foo -c work_mem=64MB");
+ clientStartup.put("_pq_.test_extension", "on");
+ clientStartup.put("DateStyle", "ISO");
+
+ Map<String, String> backendStartup = backendStartupFor(clientStartup, true);
+
+ Map<String, String> expected = new LinkedHashMap<>();
+ expected.put("user", "dbadmin");
+ expected.put("database", "auth_db");
+ expected.put("application_name", "psql");
+ expected.put("options", "-c search_path=foo -c work_mem=64MB");
+ expected.put("DateStyle", "ISO");
+ assertEquals(List.copyOf(expected.entrySet()), List.copyOf(backendStartup.entrySet()));
+ }
+
+ @Test
+ void sendsOnlyUserAndDatabaseWhenNotForwardingStartupParameters() throws Exception {
+ Map<String, String> clientStartup = new LinkedHashMap<>();
+ clientStartup.put("user", "dbadmin");
+ clientStartup.put("database", "auth_db");
+ clientStartup.put("client_protocol_version", "2");
+
+ Map<String, String> backendStartup = backendStartupFor(clientStartup, false);
+
+ assertEquals(List.of("user", "database"), List.copyOf(backendStartup.keySet()));
+ }
+
@Test
void doesNotSendAuthenticationOkWhenBackendStartupFails() throws Exception {
AtomicReference<String> backendDatabase = new AtomicReference<>();
@@ -542,7 +608,7 @@ class PostgresProtocolHandlerTest {
@Test
void iamSessionRunsAsTheRoleNamedInTheToken() throws Exception {
- AtomicReference<String> backendUser = new AtomicReference<>();
+ AtomicReference<Map<String, String>> backendStartup = new AtomicReference<>();
AtomicReference<String> backendQuery = new AtomicReference<>();
try (ServerSocket backendServer = new ServerSocket(0);
@@ -551,7 +617,7 @@ class PostgresProtocolHandlerTest {
int backendPort = backendServer.getLocalPort();
Thread backendThread = Thread.ofVirtual().start(() -> {
try {
- mockBackendRoleSwitch(backendServer, backendUser, backendQuery, "approle", true);
+ mockBackendRoleSwitch(backendServer, backendStartup, backendQuery, "approle", true);
} catch (IOException e) {
throw new RuntimeException(e);
}
@@ -568,7 +634,13 @@ class PostgresProtocolHandlerTest {
DataOutputStream clientOut = new DataOutputStream(ourClient.getOutputStream());
DataInputStream clientIn = new DataInputStream(ourClient.getInputStream());
- writeStartup(clientOut, "approle", "postgres");
+ // Applied while the backend session is still the master's, this would outlive the
+ // handover and let RESET ROLE regain the master.
+ Map<String, String> clientStartup = new LinkedHashMap<>();
+ clientStartup.put("user", "approle");
+ clientStartup.put("database", "postgres");
+ clientStartup.put("options", "-c role=dbadmin");
+ writeStartup(clientOut, clientStartup);
readCleartextPasswordChallenge(clientIn);
writePassword(clientOut, rdsToken("approle"));
readAuthenticationOk(clientIn);
@@ -585,14 +657,16 @@ class PostgresProtocolHandlerTest {
assertEquals(false, backendThread.isAlive(), "backendThread did not terminate");
}
- assertEquals("dbadmin", backendUser.get(), "backend connection is still opened as master");
+ assertEquals("dbadmin", backendStartup.get().get("user"), "backend connection is still opened as master");
+ assertEquals(List.of("user", "database"), List.copyOf(backendStartup.get().keySet()),
+ "client startup parameters are not applied to the master session");
assertEquals("SET SESSION AUTHORIZATION \"approle\"", backendQuery.get());
}
}
@Test
void iamSessionIsRejectedWhenTheTokenRoleDoesNotExist() throws Exception {
- AtomicReference<String> backendUser = new AtomicReference<>();
+ AtomicReference<Map<String, String>> backendStartup = new AtomicReference<>();
AtomicReference<String> backendQuery = new AtomicReference<>();
try (ServerSocket backendServer = new ServerSocket(0);
@@ -601,7 +675,7 @@ class PostgresProtocolHandlerTest {
int backendPort = backendServer.getLocalPort();
Thread backendThread = Thread.ofVirtual().start(() -> {
try {
- mockBackendRoleSwitch(backendServer, backendUser, backendQuery, "nosuchrole", false);
+ mockBackendRoleSwitch(backendServer, backendStartup, backendQuery, "nosuchrole", false);
} catch (IOException e) {
throw new RuntimeException(e);
}
@@ -925,14 +999,20 @@ class PostgresProtocolHandlerTest {
}
private Thread startIamAuth(Socket proxyClient, Socket backend) {
+ return startIamAuth(proxyClient, backend, true, true);
+ }
+
+ private Thread startIamAuth(Socket proxyClient, Socket backend,
+ boolean iamEnabled, boolean forwardStartupParameters) {
return Thread.ofVirtual().start(() -> {
try {
PostgresProtocolHandler.AuthenticatedSession session =
PostgresProtocolHandler.authenticate(
proxyClient, () -> backend,
"dbadmin", "adminpass", "postgres",
- true, testSigV4Validator(), testBinding(), testTlsCertificates(),
- (user, pass) -> PasswordValidator.AuthResult.MASTER_EQUIVALENT, 5000);
+ iamEnabled, testSigV4Validator(), testBinding(), testTlsCertificates(),
+ (user, pass) -> PasswordValidator.AuthResult.MASTER_EQUIVALENT, 5000,
+ forwardStartupParameters);
if (session != null) {
PostgresProtocolHandler.bridge(session);
}
@@ -942,6 +1022,54 @@ class PostgresProtocolHandlerTest {
});
}
+ /**
+ * Logs the master in through the proxy with {@code clientStartup} and returns the
+ * StartupMessage parameters the backend received, in the order it received them.
+ */
+ private Map<String, String> backendStartupFor(Map<String, String> clientStartup,
+ boolean forwardStartupParameters) throws Exception {
+ AtomicReference<Map<String, String>> backendStartup = 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, new AtomicReference<>(), backendStartup, 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, false, forwardStartupParameters);
+
+ DataOutputStream clientOut = new DataOutputStream(ourClient.getOutputStream());
+ DataInputStream clientIn = new DataInputStream(ourClient.getInputStream());
+
+ writeStartup(clientOut, clientStartup);
+ readCleartextPasswordChallenge(clientIn);
+ writePassword(clientOut, "adminpass");
+ 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");
+ }
+ }
+ return backendStartup.get();
+ }
+
private static String rdsToken(String dbUser) throws Exception {
return SigV4TokenTestHelper.createRdsToken("localhost", 7001, dbUser,
"AKIATEST", "secret", Instant.now(), 900);
@@ -952,13 +1080,15 @@ class PostgresProtocolHandlerTest {
* 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,
+ private static void mockBackendRoleSwitch(ServerSocket server,
+ AtomicReference<Map<String, String>> backendStartup,
AtomicReference<String> backendQuery,
String role, boolean roleExists) throws IOException {
- mockBackendRoleSwitch(server, backendUser, backendQuery, role, roleExists, null);
+ mockBackendRoleSwitch(server, backendStartup, backendQuery, role, roleExists, null);
}
- private static void mockBackendRoleSwitch(ServerSocket server, AtomicReference<String> backendUser,
+ private static void mockBackendRoleSwitch(ServerSocket server,
+ AtomicReference<Map<String, String>> backendStartup,
AtomicReference<String> backendQuery,
String role, boolean roleExists,
BackendScript afterHandover) throws IOException {
@@ -969,7 +1099,7 @@ class PostgresProtocolHandlerTest {
int length = in.readInt();
assertEquals(STARTUP_PROTOCOL_VERSION, in.readInt());
byte[] payload = in.readNBytes(length - 8);
- backendUser.set(parseStartupParams(payload).get("user"));
+ backendStartup.set(PostgresProtocolHandler.parseStartupParams(payload));
out.writeByte('R');
out.writeInt(8);
@@ -1185,6 +1315,12 @@ class PostgresProtocolHandlerTest {
private static void mockBackendStartup(ServerSocket server, AtomicReference<String> backendDatabase,
boolean failWithError) throws IOException {
+ mockBackendStartup(server, backendDatabase, new AtomicReference<>(), failWithError);
+ }
+
+ private static void mockBackendStartup(ServerSocket server, AtomicReference<String> backendDatabase,
+ AtomicReference<Map<String, String>> backendStartup,
+ boolean failWithError) throws IOException {
try (Socket socket = server.accept()) {
DataInputStream in = new DataInputStream(socket.getInputStream());
DataOutputStream out = new DataOutputStream(socket.getOutputStream());
@@ -1193,7 +1329,8 @@ class PostgresProtocolHandlerTest {
int proto = in.readInt();
assertEquals(STARTUP_PROTOCOL_VERSION, proto);
byte[] payload = in.readNBytes(length - 8);
- backendDatabase.set(parseStartupParams(payload).get("database"));
+ backendStartup.set(PostgresProtocolHandler.parseStartupParams(payload));
+ backendDatabase.set(backendStartup.get().get("database"));
out.writeByte('R');
out.writeInt(8);
@@ -1254,27 +1391,25 @@ class PostgresProtocolHandlerTest {
}
private static void writeStartup(DataOutputStream out, String user, String database) throws IOException {
- byte[] userKey = "user".getBytes(StandardCharsets.UTF_8);
- byte[] userVal = user.getBytes(StandardCharsets.UTF_8);
- byte[] dbKey = "database".getBytes(StandardCharsets.UTF_8);
- byte[] dbVal = database.getBytes(StandardCharsets.UTF_8);
+ Map<String, String> parameters = new LinkedHashMap<>();
+ parameters.put("user", user);
+ parameters.put("database", database);
+ writeStartup(out, parameters);
+ }
- int length = 4 + 4
- + userKey.length + 1 + userVal.length + 1
- + dbKey.length + 1 + dbVal.length + 1
- + 1;
+ private static void writeStartup(DataOutputStream out, Map<String, String> parameters) throws IOException {
+ ByteArrayOutputStream body = new ByteArrayOutputStream();
+ for (Map.Entry<String, String> parameter : parameters.entrySet()) {
+ body.write(parameter.getKey().getBytes(StandardCharsets.UTF_8));
+ body.write(0);
+ body.write(parameter.getValue().getBytes(StandardCharsets.UTF_8));
+ body.write(0);
+ }
+ body.write(0);
- out.writeInt(length);
+ out.writeInt(4 + 4 + body.size());
out.writeInt(STARTUP_PROTOCOL_VERSION);
- out.write(userKey);
- out.writeByte(0);
- out.write(userVal);
- out.writeByte(0);
- out.write(dbKey);
- out.writeByte(0);
- out.write(dbVal);
- out.writeByte(0);
- out.writeByte(0);
+ body.writeTo(out);
out.flush();
}
@@ -1369,33 +1504,6 @@ class PostgresProtocolHandlerTest {
out.flush();
}
- private static Map<String, String> parseStartupParams(byte[] data) {
- Map<String, String> params = new HashMap<>();
- int i = 0;
- while (i < data.length) {
- int keyStart = i;
- while (i < data.length && data[i] != 0) {
- i++;
- }
- if (i >= data.length) {
- break;
- }
- String key = new String(data, keyStart, i - keyStart, StandardCharsets.UTF_8);
- i++;
- if (key.isEmpty()) {
- break;
- }
- int valStart = i;
- while (i < data.length && data[i] != 0) {
- i++;
- }
- String value = new String(data, valStart, i - valStart, StandardCharsets.UTF_8);
- i++;
- params.put(key, value);
- }
- return params;
- }
-
private static final class MemorySocket extends Socket {
private final ByteArrayInputStream input;
private final ByteArrayOutputStream output = new ByteArrayOutputStream();
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
new file mode 100644
index 0000000000..154c911756
--- /dev/null
+++ b/src/test/java/io/github/hectorvent/floci/services/rds/proxy/RdsPostgresStartupParametersIntegrationTest.java
@@ -0,0 +1,99 @@
+package io.github.hectorvent.floci.services.rds.proxy;
+
+import com.github.dockerjava.api.DockerClient;
+import io.quarkus.test.junit.QuarkusTest;
+import io.restassured.specification.RequestSpecification;
+import jakarta.inject.Inject;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Tag;
+import org.junit.jupiter.api.Test;
+
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.util.Properties;
+
+import static io.restassured.RestAssured.given;
+import static io.restassured.http.ContentType.URLENC;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.equalTo;
+import static org.junit.jupiter.api.Assumptions.assumeTrue;
+
+/**
+ * The parameters a client puts in its PostgreSQL StartupMessage reach the instance through the
+ * proxy, as they reach an RDS for PostgreSQL endpoint: {@code options} and
+ * {@code application_name} here.
+ */
+@QuarkusTest
+@Tag("docker")
+class RdsPostgresStartupParametersIntegrationTest {
+
+ private static final String RDS_AUTH = "AWS4-HMAC-SHA256 Credential=test/20261006/us-east-1/rds/aws4_request";
+ private static final String MASTER_USER = "master";
+ private static final String MASTER_PASSWORD = "Master123!";
+
+ @Inject
+ DockerClient dockerClient;
+
+ @BeforeEach
+ void checkDocker() {
+ try {
+ dockerClient.pingCmd().exec();
+ } catch (Exception e) {
+ assumeTrue(false, "Docker is not available: " + e.getMessage());
+ }
+ }
+
+ @Test
+ void clientStartupParametersApplyToTheSession() throws Exception {
+ String dbId = "startup-params-" + Long.toString(System.nanoTime(), 36);
+ int port = rds("CreateDBInstance")
+ .formParam("DBInstanceIdentifier", dbId)
+ .formParam("Engine", "postgres")
+ .formParam("MasterUsername", MASTER_USER)
+ .formParam("MasterUserPassword", MASTER_PASSWORD)
+ .formParam("DBName", "appdb")
+ .formParam("AllocatedStorage", "20")
+ .formParam("DBInstanceClass", "db.t3.micro")
+ .when().post("/").then().statusCode(200)
+ .extract().xmlPath()
+ .getInt("CreateDBInstanceResponse.CreateDBInstanceResult.DBInstance.Endpoint.Port");
+ try {
+ Properties properties = new Properties();
+ properties.setProperty("user", MASTER_USER);
+ properties.setProperty("password", MASTER_PASSWORD);
+ properties.setProperty("sslmode", "disable");
+ properties.setProperty("options", "-c search_path=floci_opts -c work_mem=64MB");
+ properties.setProperty("ApplicationName", "floci-startup-params");
+
+ try (Connection connection = DriverManager.getConnection(
+ "jdbc:postgresql://127.0.0.1:" + port + "/appdb", properties)) {
+ assertThat(show(connection, "search_path"), equalTo("floci_opts"));
+ assertThat(show(connection, "work_mem"), equalTo("64MB"));
+ assertThat(show(connection, "application_name"), equalTo("floci-startup-params"));
+ }
+ } finally {
+ rds("DeleteDBInstance")
+ .formParam("DBInstanceIdentifier", dbId)
+ .formParam("SkipFinalSnapshot", "true")
+ .when().post("/");
+ }
+ }
+
+ private static String show(Connection connection, String parameter) throws SQLException {
+ try (Statement statement = connection.createStatement();
+ ResultSet rows = statement.executeQuery("SHOW " + parameter)) {
+ rows.next();
+ return rows.getString(1);
+ }
+ }
+
+ private static RequestSpecification rds(String action) {
+ return given().header("Authorization", RDS_AUTH)
+ .contentType(URLENC)
+ .formParam("Action", action)
+ .formParam("Version", "2014-10-31");
+ }
+}