mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 14:30:28 -05:00
fix(rds): fix PostgreSQL proxy ignoring requested database name (#1480)
* fix(rds): postgres proxy ignores requested database name * fix(rds): assert-auththread and backendthread are terminated * fix(rds): assert-auththread and backendthread are terminated again
This commit is contained in:
1 parent
7c04e8824c
commit
7b3c5b230d
2 files changed
+302
-3
No files matched your search
+29
-3
@@ -127,7 +127,7 @@ public class PostgresProtocolHandler {
|
||||
InputStream backendIn = backend.getInputStream();
|
||||
OutputStream backendOut = backend.getOutputStream();
|
||||
|
||||
String effectiveDbName = (dbName != null && !dbName.isBlank()) ? dbName : "postgres";
|
||||
String effectiveDbName = resolveEffectiveDbName(startup.database(), dbName);
|
||||
String backendUser = (isIam || isMaster) ? masterUsername : clientUsername;
|
||||
String backendPass = (isIam || isMaster) ? masterPassword : clientPassword;
|
||||
sendStartupToBackend(backendOut, backendUser, effectiveDbName);
|
||||
@@ -146,6 +146,16 @@ public class PostgresProtocolHandler {
|
||||
List<byte[]> bufferedMessages = readUntilReadyForQuery(backendIn);
|
||||
|
||||
// Phase 6: Send AuthenticationOK to client, forward buffered messages, then bridge
|
||||
if (endsWithErrorResponse(bufferedMessages)) {
|
||||
for (byte[] msg : bufferedMessages) {
|
||||
clientOut.write(msg);
|
||||
}
|
||||
clientOut.flush();
|
||||
closeQuietly(client);
|
||||
closeQuietly(backend);
|
||||
return;
|
||||
}
|
||||
|
||||
sendMessage(clientOut, 'R', intBytes(0)); // AuthenticationOK
|
||||
for (byte[] msg : bufferedMessages) {
|
||||
clientOut.write(msg);
|
||||
@@ -183,7 +193,9 @@ public class PostgresProtocolHandler {
|
||||
byte[] payload = new byte[length - 8];
|
||||
readFully(in, payload);
|
||||
Map<String, String> params = parseStartupParams(payload);
|
||||
return new StartupMessage(currentSocket, params.getOrDefault("user", "postgres"));
|
||||
return new StartupMessage(currentSocket,
|
||||
params.getOrDefault("user", "postgres"),
|
||||
params.get("database"));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -257,7 +269,21 @@ public class PostgresProtocolHandler {
|
||||
return context;
|
||||
}
|
||||
|
||||
private record StartupMessage(Socket socket, String username) {}
|
||||
private record StartupMessage(Socket socket, String username, String database) {}
|
||||
|
||||
static String resolveEffectiveDbName(String clientDatabase, String instanceDbName) {
|
||||
if (clientDatabase != null && !clientDatabase.isBlank()) {
|
||||
return clientDatabase;
|
||||
}
|
||||
if (instanceDbName != null && !instanceDbName.isBlank()) {
|
||||
return instanceDbName;
|
||||
}
|
||||
return "postgres";
|
||||
}
|
||||
|
||||
private static boolean endsWithErrorResponse(List<byte[]> messages) {
|
||||
return !messages.isEmpty() && messages.get(messages.size() - 1)[0] == 'E';
|
||||
}
|
||||
|
||||
private static Map<String, String> parseStartupParams(byte[] data) {
|
||||
Map<String, String> params = new HashMap<>();
|
||||
|
||||
Reference in new issue
Block a user