fix(rds): improve postgres proxy startup parity (#1599)

* fix(rds): close postgres proxy bridge on relay exit

* fix(rds): decline postgres ssl negotiation

* fix(rds): support postgres ssl startup
This commit is contained in:
Jason van Zyl authored and GitHub committed 2026-07-06 21:38:47 -05:00
1 parent 7586a01cf9
commit b8bb2ec6b3
2 files changed
+188 -62

No files matched your search

@@ -5,10 +5,6 @@ import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
@@ -16,13 +12,14 @@ import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.security.SecureRandom;
import java.security.cert.X509Certificate;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.security.cert.X509Certificate;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
@@ -154,49 +151,115 @@ class PostgresProtocolHandlerTest {
}
@Test
void acceptsPostgresSslRequestAndUpgradesSocket() throws Exception {
ArrayBlockingQueue<Integer> serverRead = new ArrayBlockingQueue<>(1);
void acceptsPostgresSslRequestAndContinuesStartup() throws Exception {
AtomicReference<String> backendDatabase = new AtomicReference<>();
try (ServerSocket server = new ServerSocket(0)) {
Thread.ofVirtual().start(() -> {
try (Socket accepted = server.accept()) {
DataInputStream in = new DataInputStream(accepted.getInputStream());
DataOutputStream out = new DataOutputStream(accepted.getOutputStream());
assertEquals(8, in.readInt());
assertEquals(SSL_REQUEST_CODE, in.readInt());
try (ServerSocket backendServer = new ServerSocket(0);
ServerSocket clientServer = new ServerSocket(0)) {
out.writeByte('S');
out.flush();
Socket sslSocket = PostgresProtocolHandler.acceptSsl(accepted);
serverRead.add(sslSocket.getInputStream().read());
sslSocket.getOutputStream().write(99);
sslSocket.getOutputStream().flush();
sslSocket.close();
} catch (Exception e) {
serverRead.add(-1);
int backendPort = backendServer.getLocalPort();
Thread backendThread = Thread.ofVirtual().start(() -> {
try {
mockBackendStartup(backendServer, backendDatabase, false);
} catch (IOException e) {
throw new RuntimeException(e);
}
});
try (Socket rawClient = new Socket("localhost", server.getLocalPort())) {
DataOutputStream out = new DataOutputStream(rawClient.getOutputStream());
DataInputStream in = new DataInputStream(rawClient.getInputStream());
out.writeInt(8);
out.writeInt(SSL_REQUEST_CODE);
out.flush();
assertEquals('S', in.readUnsignedByte());
Socket proxyClient;
try (Socket ourClient = new Socket("localhost", clientServer.getLocalPort())) {
proxyClient = clientServer.accept();
Socket backend = new Socket("localhost", backendPort);
SSLSocket sslClient = (SSLSocket) trustAllContext().getSocketFactory()
.createSocket(rawClient, "localhost", server.getLocalPort(), true);
sslClient.setUseClientMode(true);
Thread authThread = Thread.ofVirtual().start(() -> {
try {
PostgresProtocolHandler.handleAuth(
proxyClient, backend,
"dbadmin", "adminpass", "postgres",
false, testSigV4Validator(),
(user, pass) -> true);
} catch (IOException e) {
throw new RuntimeException(e);
}
});
DataOutputStream clientOut = new DataOutputStream(ourClient.getOutputStream());
DataInputStream clientIn = new DataInputStream(ourClient.getInputStream());
writeSslRequest(clientOut);
assertEquals('S', clientIn.readUnsignedByte());
SSLSocket sslClient = trustedClientSocket(ourClient);
sslClient.startHandshake();
sslClient.getOutputStream().write(42);
sslClient.getOutputStream().flush();
assertEquals(99, sslClient.getInputStream().read());
sslClient.close();
clientOut = new DataOutputStream(sslClient.getOutputStream());
clientIn = new DataInputStream(sslClient.getInputStream());
writeStartup(clientOut, "dbadmin", "auth_db");
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");
}
assertEquals("auth_db", backendDatabase.get());
}
}
@Test
void closesClientWhenBackendClosesDuringBridge() throws Exception {
try (ServerSocket backendServer = new ServerSocket(0);
ServerSocket clientServer = new ServerSocket(0)) {
Thread backendThread = Thread.ofVirtual().start(() -> {
try {
mockBackendClosesDuringBridge(backendServer);
} catch (IOException e) {
throw new RuntimeException(e);
}
});
try (Socket ourClient = new Socket("localhost", clientServer.getLocalPort())) {
ourClient.setSoTimeout(5_000);
Socket proxyClient = clientServer.accept();
Socket backend = new Socket("localhost", backendServer.getLocalPort());
Thread authThread = Thread.ofVirtual().start(() -> {
try {
PostgresProtocolHandler.handleAuth(
proxyClient, backend,
"dbadmin", "adminpass", "postgres",
false, testSigV4Validator(),
(user, pass) -> true);
} catch (IOException e) {
throw new RuntimeException(e);
}
});
DataOutputStream clientOut = new DataOutputStream(ourClient.getOutputStream());
DataInputStream clientIn = new DataInputStream(ourClient.getInputStream());
writeStartup(clientOut, "dbadmin", "auth_db");
readCleartextPasswordChallenge(clientIn);
writePassword(clientOut, "adminpass");
readAuthenticationOk(clientIn);
readReadyForQuery(clientIn);
writeSimpleQuery(clientOut, "select 1");
assertEquals(-1, clientIn.read(), "backend close must be visible to the client");
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(42, serverRead.poll(5, TimeUnit.SECONDS));
}
private static RdsSigV4Validator testSigV4Validator() {
@@ -240,6 +303,39 @@ class PostgresProtocolHandlerTest {
}
}
private static void mockBackendClosesDuringBridge(ServerSocket server) throws IOException {
try (Socket socket = server.accept()) {
DataInputStream in = new DataInputStream(socket.getInputStream());
DataOutputStream out = new DataOutputStream(socket.getOutputStream());
int length = in.readInt();
int proto = in.readInt();
assertEquals(STARTUP_PROTOCOL_VERSION, proto);
in.readNBytes(length - 8);
out.writeByte('R');
out.writeInt(8);
out.writeInt(3);
out.flush();
assertEquals('p', in.readByte());
int pwLength = in.readInt();
in.readNBytes(pwLength - 4);
out.writeByte('R');
out.writeInt(8);
out.writeInt(0);
out.writeByte('Z');
out.writeInt(5);
out.writeByte('I');
out.flush();
assertEquals('Q', in.readByte());
int queryLength = in.readInt();
in.readNBytes(queryLength - 4);
}
}
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);
@@ -265,6 +361,32 @@ class PostgresProtocolHandlerTest {
out.flush();
}
private static void writeSslRequest(DataOutputStream out) throws IOException {
out.writeInt(8);
out.writeInt(SSL_REQUEST_CODE);
out.flush();
}
private static SSLSocket trustedClientSocket(Socket socket) throws Exception {
SSLContext context = SSLContext.getInstance("TLS");
context.init(null, new TrustManager[] {new X509TrustManager() {
@Override
public void checkClientTrusted(X509Certificate[] chain, String authType) {}
@Override
public void checkServerTrusted(X509Certificate[] chain, String authType) {}
@Override
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[0];
}
}}, null);
SSLSocket sslSocket = (SSLSocket) context.getSocketFactory()
.createSocket(socket, socket.getInetAddress().getHostAddress(), socket.getPort(), true);
sslSocket.setUseClientMode(true);
return sslSocket;
}
private static void writePassword(DataOutputStream out, String password) throws IOException {
byte[] pw = password.getBytes(StandardCharsets.UTF_8);
out.writeByte('p');
@@ -274,6 +396,15 @@ class PostgresProtocolHandlerTest {
out.flush();
}
private static void writeSimpleQuery(DataOutputStream out, String query) throws IOException {
byte[] queryBytes = query.getBytes(StandardCharsets.UTF_8);
out.writeByte('Q');
out.writeInt(4 + queryBytes.length + 1);
out.write(queryBytes);
out.writeByte(0);
out.flush();
}
private static void readCleartextPasswordChallenge(DataInputStream in) throws IOException {
assertEquals('R', in.readByte());
assertEquals(8, in.readInt());
@@ -340,20 +471,4 @@ class PostgresProtocolHandlerTest {
return params;
}
private static SSLContext trustAllContext() throws Exception {
SSLContext context = SSLContext.getInstance("TLS");
context.init(null, new TrustManager[] {new X509TrustManager() {
@Override
public void checkClientTrusted(X509Certificate[] chain, String authType) {}
@Override
public void checkServerTrusted(X509Certificate[] chain, String authType) {}
@Override
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[0];
}
}}, new SecureRandom());
return context;
}
}