Skip to content

armgit / floci

Public Imported from GitHub
0

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

refactor(rds): split PostgresProtocolHandler into authenticate and bridge primitives (#2913)

Vũ Thế Huy5b3c0e01e319
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 662cb278ad..4536bc8f39 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
@@ -44,17 +44,17 @@ public class PostgresProtocolHandler {
private static final int SSL_REQUEST_CODE = 80877103;
private static final int STARTUP_PROTOCOL_VERSION = 196608; // v3.0
- public static void handleAuth(Socket client, Socket backend,
- String masterUsername, String masterPassword, String dbName,
- boolean iamEnabled, RdsSigV4Validator sigV4,
- RdsProxyTlsCertificates tlsCertificates,
- PasswordValidator passwordValidator) throws IOException {
+ public static Socket authenticate(Socket client, Socket backend,
+ String masterUsername, String masterPassword, String dbName,
+ boolean iamEnabled, RdsSigV4Validator sigV4,
+ RdsProxyTlsCertificates tlsCertificates,
+ PasswordValidator passwordValidator) throws IOException {
// Phase 1: Read client startup message (possibly preceded by SSL request)
StartupMessage startup = readStartupMessage(client, tlsCertificates);
if (startup == null) {
closeQuietly(client);
- return;
+ return null;
}
client = startup.socket();
String clientUsername = startup.username();
@@ -69,7 +69,7 @@ public class PostgresProtocolHandler {
String clientPassword = readPasswordMessage(clientIn);
if (clientPassword == null) {
closeQuietly(client);
- return;
+ return null;
}
// Phase 4: Validate credentials.
@@ -87,7 +87,7 @@ public class PostgresProtocolHandler {
clientOut.flush();
closeQuietly(client);
closeQuietly(backend);
- return;
+ return null;
}
} else if (isMaster) {
if (!passwordValidator.validate(clientUsername, clientPassword)) {
@@ -96,7 +96,7 @@ public class PostgresProtocolHandler {
clientOut.flush();
closeQuietly(client);
closeQuietly(backend);
- return;
+ return null;
}
}
@@ -119,7 +119,7 @@ public class PostgresProtocolHandler {
clientOut.flush();
closeQuietly(client);
closeQuietly(backend);
- return;
+ return null;
}
// Buffer all backend messages until ReadyForQuery ('Z')
@@ -133,7 +133,7 @@ public class PostgresProtocolHandler {
clientOut.flush();
closeQuietly(client);
closeQuietly(backend);
- return;
+ return null;
}
sendMessage(clientOut, 'R', intBytes(0)); // AuthenticationOK
@@ -142,7 +142,7 @@ public class PostgresProtocolHandler {
}
clientOut.flush();
- bridge(client, backend);
+ return client;
}
// ── Startup ───────────────────────────────────────────────────────────────
@@ -536,9 +536,7 @@ public class PostgresProtocolHandler {
return messages;
}
- // ── Bridge ────────────────────────────────────────────────────────────────
-
- private static void bridge(Socket client, Socket backend) {
+ public static void bridge(Socket client, Socket backend) {
InputStream clientIn, backendIn;
OutputStream clientOut, backendOut;
try {
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 a72a261419..b1bacb360a 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
@@ -100,9 +100,14 @@ public class RdsAuthProxy {
backend.setTcpNoDelay(true);
switch (engine) {
- case POSTGRES -> PostgresProtocolHandler.handleAuth(
- client, backend, masterUsername, masterPassword, dbName,
- iamEnabled, sigV4, tlsCertificates, passwordValidator::validate);
+ case POSTGRES -> {
+ Socket activeClient = PostgresProtocolHandler.authenticate(
+ client, backend, masterUsername, masterPassword, dbName,
+ iamEnabled, sigV4, tlsCertificates, passwordValidator::validate);
+ if (activeClient != null) {
+ PostgresProtocolHandler.bridge(activeClient, backend);
+ }
+ }
case MYSQL, MARIADB -> MySqlProtocolHandler.handleAuth(
client, backend, masterUsername, masterPassword,
iamEnabled, sigV4, tlsCertificates, passwordValidator::validate);
diff --git a/src/main/java/io/github/hectorvent/floci/services/redshift/proxy/RedshiftAuthProxy.java b/src/main/java/io/github/hectorvent/floci/services/redshift/proxy/RedshiftAuthProxy.java
index 4de297185e..1b8573857c 100644
--- a/src/main/java/io/github/hectorvent/floci/services/redshift/proxy/RedshiftAuthProxy.java
+++ b/src/main/java/io/github/hectorvent/floci/services/redshift/proxy/RedshiftAuthProxy.java
@@ -137,14 +137,17 @@ public class RedshiftAuthProxy {
client.setTcpNoDelay(true);
backend = new Socket(backendHost, backendPort);
backend.setTcpNoDelay(true);
- // iamEnabled = false: the SigV4 branch inside handleAuth is never taken.
- PostgresProtocolHandler.handleAuth(
+ // iamEnabled = false: the SigV4 branch inside authenticate is never taken.
+ Socket activeClient = PostgresProtocolHandler.authenticate(
client, backend, masterUsername, masterPassword, dbName,
false, sigV4, tlsCertificates, passwordValidator::validate);
+ if (activeClient != null) {
+ PostgresProtocolHandler.bridge(activeClient, backend);
+ }
} catch (Exception e) {
LOG.debugv("Redshift connection error for cluster {0}: {1}", clusterKey, e.getMessage());
} finally {
- // handleAuth's success path bridges then closes both sockets; every other path
+ // authenticate's success path returns the client to bridge; every other path
// (early return on a bare probe, auth failure, thrown IOException) can leave the
// backend connection to Postgres open. Closing here is idempotent.
closeQuietly(client);
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 9762f6ca08..2b89398ec6 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
@@ -81,11 +81,14 @@ class PostgresProtocolHandlerTest {
Thread authThread = Thread.ofVirtual().start(() -> {
try {
- PostgresProtocolHandler.handleAuth(
+ Socket activeClient = PostgresProtocolHandler.authenticate(
proxyClient, backend,
"dbadmin", "adminpass", "postgres",
false, testSigV4Validator(), testTlsCertificates(),
(user, pass) -> true);
+ if (activeClient != null) {
+ PostgresProtocolHandler.bridge(activeClient, backend);
+ }
} catch (IOException e) {
throw new RuntimeException(e);
}
@@ -135,11 +138,14 @@ class PostgresProtocolHandlerTest {
Thread authThread = Thread.ofVirtual().start(() -> {
try {
- PostgresProtocolHandler.handleAuth(
+ Socket activeClient = PostgresProtocolHandler.authenticate(
proxyClient, backend,
"dbadmin", "adminpass", "postgres",
false, testSigV4Validator(), testTlsCertificates(),
(user, pass) -> true);
+ if (activeClient != null) {
+ PostgresProtocolHandler.bridge(activeClient, backend);
+ }
} catch (IOException e) {
throw new RuntimeException(e);
}
@@ -189,11 +195,14 @@ class PostgresProtocolHandlerTest {
Thread authThread = Thread.ofVirtual().start(() -> {
try {
- PostgresProtocolHandler.handleAuth(
+ Socket activeClient = PostgresProtocolHandler.authenticate(
proxyClient, backend,
"dbadmin", "adminpass", "postgres",
false, testSigV4Validator(), testTlsCertificates(),
(user, pass) -> true);
+ if (activeClient != null) {
+ PostgresProtocolHandler.bridge(activeClient, backend);
+ }
} catch (IOException e) {
throw new RuntimeException(e);
}
@@ -252,11 +261,14 @@ class PostgresProtocolHandlerTest {
Thread authThread = Thread.ofVirtual().start(() -> {
try {
- PostgresProtocolHandler.handleAuth(
+ Socket activeClient = PostgresProtocolHandler.authenticate(
proxyClient, backend,
"dbadmin", "adminpass", "postgres",
false, testSigV4Validator(), tlsCertificates,
(user, pass) -> true);
+ if (activeClient != null) {
+ PostgresProtocolHandler.bridge(activeClient, backend);
+ }
} catch (IOException e) {
throw new RuntimeException(e);
}
@@ -302,11 +314,14 @@ class PostgresProtocolHandlerTest {
Thread authThread = Thread.ofVirtual().start(() -> {
try {
- PostgresProtocolHandler.handleAuth(
+ Socket activeClient = PostgresProtocolHandler.authenticate(
proxyClient, backend,
"dbadmin", "adminpass", "postgres",
false, testSigV4Validator(), tlsCertificates,
(user, pass) -> true);
+ if (activeClient != null) {
+ PostgresProtocolHandler.bridge(activeClient, backend);
+ }
} catch (IOException ignored) {
// Client aborts the handshake below — the handler observing that is expected.
}
@@ -348,11 +363,14 @@ class PostgresProtocolHandlerTest {
Thread authThread = Thread.ofVirtual().start(() -> {
try {
- PostgresProtocolHandler.handleAuth(
+ Socket activeClient = PostgresProtocolHandler.authenticate(
proxyClient, backend,
"dbadmin", "adminpass", "postgres",
false, testSigV4Validator(), testTlsCertificates(),
(user, pass) -> true);
+ if (activeClient != null) {
+ PostgresProtocolHandler.bridge(activeClient, backend);
+ }
} catch (IOException e) {
throw new RuntimeException(e);
}