mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -05:00
fix(rds): forward a PostgreSQL CancelRequest to the backend (#6127)
A client cancels a running query by opening a second connection and sending a CancelRequest: the backend's process id and secret key from the BackendKeyData it received at login. The RDS proxy only knew SSLRequest and StartupMessage, so it logged "Unexpected PostgreSQL startup protocol version: 80877102" and closed the connection. The cancel never reached PostgreSQL and the query kept running. The client already holds the backend's own key, because the proxy relays BackendKeyData unchanged. So the proxy now passes the 16 bytes to the backend on a connection of its own and closes, as the server does. A cancel of any other length is dropped with its own warning. Redshift clusters share this handler, so they forward cancels too. A DB proxy endpoint drops a cancel, because RDS Proxy does not support one. The RDS integration tests now share their Docker check and their CreateDBInstance and DeleteDBInstance calls in RdsRequests. The Docker check uses DockerAvailability, so only a missing daemon skips a test. Limits: a cancel still takes one of the proxy's connection permits, so a proxy at its connection limit refuses it, where PostgreSQL itself would not. And on an auto-paused Aurora backend, a cancel resumes it, as any other connection does. Co-authored-by: Robin Baeckman <27041454+RobinBaeckman@users.noreply.github.com>
This commit is contained in:
1 parent
686d0b5f02
commit
b806f76f1b
8 files changed
+343
-84
No files matched your search
+37
-3
@@ -127,7 +127,10 @@ public class PostgresProtocolHandler {
|
||||
handshakeTimeoutMillis, forwardStartupParameters, true);
|
||||
}
|
||||
|
||||
/** Standard DB Proxy IAM does not enroll the backend role in database-native IAM. */
|
||||
/**
|
||||
* {@code databaseIamAuthentication} is false on a standard DB Proxy endpoint, which neither
|
||||
* enrolls the backend role in database-native IAM nor forwards a client's CancelRequest.
|
||||
*/
|
||||
public static AuthenticatedSession authenticate(Socket client, BackendConnector backendConnector,
|
||||
String masterUsername, String masterPassword, String dbName,
|
||||
boolean iamEnabled, RdsSigV4Validator sigV4,
|
||||
@@ -140,7 +143,8 @@ public class PostgresProtocolHandler {
|
||||
client.setSoTimeout(handshakeTimeoutMillis);
|
||||
|
||||
// Phase 1: Read client startup message (possibly preceded by SSL request)
|
||||
StartupMessage startup = readStartupMessage(client, tlsCertificates, handshakeTimeoutMillis);
|
||||
StartupMessage startup = readStartupMessage(client, backendConnector, databaseIamAuthentication,
|
||||
tlsCertificates, handshakeTimeoutMillis);
|
||||
if (startup == null) {
|
||||
closeQuietly(client);
|
||||
return null;
|
||||
@@ -471,7 +475,8 @@ public class PostgresProtocolHandler {
|
||||
|
||||
// ── Startup ───────────────────────────────────────────────────────────────
|
||||
|
||||
private static StartupMessage readStartupMessage(Socket socket, RdsProxyTlsCertificates tlsCertificates,
|
||||
private static StartupMessage readStartupMessage(Socket socket, BackendConnector backendConnector,
|
||||
boolean forwardCancel, RdsProxyTlsCertificates tlsCertificates,
|
||||
int handshakeTimeoutMillis) throws IOException {
|
||||
Socket currentSocket = socket;
|
||||
while (true) {
|
||||
@@ -489,6 +494,19 @@ public class PostgresProtocolHandler {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (startup.code() == PostgresWire.CANCEL_REQUEST) {
|
||||
if (!forwardCancel) {
|
||||
// RDS Proxy does not support a CancelRequest from a client, so it drops one.
|
||||
LOG.debugv("Dropping a PostgreSQL CancelRequest sent to a DB proxy endpoint");
|
||||
} else if (startup.body().length == 8) {
|
||||
forwardCancelRequest(startup.body(), backendConnector);
|
||||
} else {
|
||||
LOG.warnv("Dropping a PostgreSQL CancelRequest of length {0}; it must be 16",
|
||||
startup.body().length + 8);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
if (startup.code() != PostgresWire.PROTOCOL_3) {
|
||||
LOG.warnv("Unexpected PostgreSQL startup protocol version: {0}", startup.code());
|
||||
return null;
|
||||
@@ -502,6 +520,22 @@ public class PostgresProtocolHandler {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Relays a CancelRequest to the backend on a connection of its own, as PostgreSQL expects.
|
||||
* The client holds the backend's own process id and key from its BackendKeyData, so the
|
||||
* request goes through unchanged. Nothing is answered: the server never replies to one.
|
||||
*/
|
||||
private static void forwardCancelRequest(byte[] key, BackendConnector backendConnector)
|
||||
throws IOException {
|
||||
try (Socket backend = backendConnector.connect()) {
|
||||
OutputStream out = backend.getOutputStream();
|
||||
PostgresWire.writeInt32(out, 16);
|
||||
PostgresWire.writeInt32(out, PostgresWire.CANCEL_REQUEST);
|
||||
out.write(key);
|
||||
out.flush();
|
||||
}
|
||||
}
|
||||
|
||||
static Socket acceptSsl(Socket socket, RdsProxyTlsCertificates tlsCertificates) throws IOException {
|
||||
try {
|
||||
SSLSocket sslSocket = (SSLSocket) tlsCertificates.sslContext().getSocketFactory()
|
||||
|
||||
Reference in new issue
Block a user