mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
refactor(rds): split PostgresProtocolHandler into authenticate and bridge primitives (#2913)
Extract handleAuth into authenticate(...), which returns the post-startup client Socket (or null when the handshake ended early), and a public bridge(...) that relays. RdsAuthProxy and RedshiftAuthProxy now call authenticate then bridge in sequence; RDS and Redshift relay behaviour is unchanged. This lets a later change replace the Redshift relay with an intercepting bridge without touching the handshake.
This commit is contained in:
1 parent
660e7613bf
commit
5b3c0e01e3
4 files changed
+51
-27
No files matched your search
+13
-15
@@ -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 {
|
||||
|
||||
@@ -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);
|
||||
|
||||
+6
-3
@@ -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);
|
||||
|
||||
Reference in new issue
Block a user