Skip to content

armgit / floci

Public Imported from GitHub
0

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

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

Jason van Zylb8bb2ec6b314
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 d95fd65303..158417ff03 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
@@ -42,6 +42,7 @@ import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
+import java.util.concurrent.atomic.AtomicBoolean;
/**
* Handles the PostgreSQL wire protocol auth intercept.
@@ -628,10 +629,17 @@ public class PostgresProtocolHandler {
closeQuietly(backend);
return;
}
+ AtomicBoolean closed = new AtomicBoolean();
+ Runnable closeBoth = () -> {
+ if (closed.compareAndSet(false, true)) {
+ closeQuietly(client);
+ closeQuietly(backend);
+ }
+ };
Thread t1 = Thread.ofVirtual().name("rds-pg-c2b")
- .start(() -> relay(clientIn, backendOut));
+ .start(() -> relay(clientIn, backendOut, closeBoth));
Thread t2 = Thread.ofVirtual().name("rds-pg-b2c")
- .start(() -> relay(backendIn, clientOut));
+ .start(() -> relay(backendIn, clientOut, closeBoth));
try {
t1.join();
t2.join();
@@ -643,7 +651,7 @@ public class PostgresProtocolHandler {
}
}
- private static void relay(InputStream from, OutputStream to) {
+ private static void relay(InputStream from, OutputStream to, Runnable onDone) {
try {
byte[] buf = new byte[8192];
int n;
@@ -651,7 +659,10 @@ public class PostgresProtocolHandler {
to.write(buf, 0, n);
to.flush();
}
- } catch (IOException ignored) {}
+ } catch (IOException ignored) {
+ } finally {
+ onDone.run();
+ }
}
// ── Error response ────────────────────────────────────────────────────────
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 a5967b7d06..6b2fe076a8 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
@@ -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);
-
- 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());
-
- 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);
+ void acceptsPostgresSslRequestAndContinuesStartup() 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);
}
});
- 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);
+
+ 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);
+ }
+ });
- SSLSocket sslClient = (SSLSocket) trustAllContext().getSocketFactory()
- .createSocket(rawClient, "localhost", server.getLocalPort(), true);
- sslClient.setUseClientMode(true);
+ 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);
- assertEquals(42, serverRead.poll(5, TimeUnit.SECONDS));
+ 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");
+ }
+ }
}
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;
- }
}