mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -05:00
fix(dsql): take TLS started with no SSLRequest, as DSQL does (#6296)
DSQL's own connectors, and libpq's and pgjdbc's sslnegotiation=direct, start TLS at once with no SSLRequest. The endpoint read their ClientHello as a plaintext startup packet and refused it as not TLS. The endpoint now reads a connection's first byte. A TLS handshake record goes straight to the handshake with that byte handed back, and anything else takes the SSLRequest path as before. The byte is TlsProxyServer's TLS_HANDSHAKE, now public, the one the TLS proxy already tells TLS apart by; its peek runs on a Vert.x socket, the endpoint's on a blocking one, so only the constant is shared. As DSQL does (measured, eu-west-1): the endpoint picks the ALPN protocol postgresql whenever a client offers it, after an SSLRequest too, and none otherwise; a direct handshake that does not end with postgresql picked completes and the connection closes without a message. DsqlWireClient opens TLS through one handshake helper, with an ALPN list, after an SSLRequest (tls) or at once (direct), and signs in separately.
This commit is contained in:
1 parent
d3359662e5
commit
1f25fd88cf
6 files changed
+155
-28
No files matched your search
@@ -56,7 +56,7 @@ public class TlsProxyServer {
|
||||
private static final Logger LOG = Logger.getLogger(TlsProxyServer.class);
|
||||
|
||||
/** TLS record content type for Handshake (ClientHello). */
|
||||
private static final byte TLS_HANDSHAKE = 0x16;
|
||||
public static final byte TLS_HANDSHAKE = 0x16;
|
||||
|
||||
private static final int HTTP_BACKEND_PORT = TlsConfigSource.HTTP_INTERNAL_PORT;
|
||||
private static final int HTTPS_BACKEND_PORT = TlsConfigSource.HTTPS_INTERNAL_PORT;
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package io.github.hectorvent.floci.services.dsql.dataplane;
|
||||
|
||||
import io.github.hectorvent.floci.config.TlsProxyServer;
|
||||
import io.github.hectorvent.floci.core.common.PostgresWire;
|
||||
import io.github.hectorvent.floci.services.dsql.DsqlIdentifiers;
|
||||
|
||||
@@ -11,9 +12,11 @@ import javax.net.ssl.SSLSocket;
|
||||
import javax.net.ssl.StandardConstants;
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.BufferedOutputStream;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.io.PushbackInputStream;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.Socket;
|
||||
import java.time.Duration;
|
||||
@@ -51,6 +54,9 @@ final class DsqlSession {
|
||||
private static final int MAX_DATABASE_MESSAGE = 64 * 1024 * 1024;
|
||||
private static final String INVALID_STARTUP_LENGTH = "invalid length of startup packet";
|
||||
private static final String INVALID_MESSAGE_LENGTH = "invalid message length";
|
||||
// DSQL's (measured): picked whenever a client offers it, and the one a client starting TLS at
|
||||
// once, with no SSLRequest, must offer.
|
||||
private static final String ALPN_POSTGRESQL = "postgresql";
|
||||
private static final String SERIALIZATION_FAILURE = "40001";
|
||||
private static final String QUERY_CANCELED = "57014";
|
||||
private static final String ACCESS_DENIED = "unable to accept connection, access denied";
|
||||
@@ -106,28 +112,37 @@ final class DsqlSession {
|
||||
void serve() throws IOException {
|
||||
socket.setTcpNoDelay(true);
|
||||
socket.setSoTimeout(endpoint.handshakeTimeoutMillis());
|
||||
InputStream rawIn = socket.getInputStream();
|
||||
PushbackInputStream rawIn = new PushbackInputStream(socket.getInputStream(), 1);
|
||||
OutputStream rawOut = socket.getOutputStream();
|
||||
PostgresWire.Startup first = readPlaintextPacket(rawIn);
|
||||
if (first.code() == PostgresWire.CANCEL_REQUEST) {
|
||||
endpoint.forwardCancel(first.body());
|
||||
return;
|
||||
}
|
||||
if (first.code() == PostgresWire.GSSENC_REQUEST) {
|
||||
rawOut.write('N');
|
||||
int firstByte = rawIn.read();
|
||||
boolean direct = firstByte == TlsProxyServer.TLS_HANDSHAKE;
|
||||
if (!direct) {
|
||||
if (firstByte >= 0) {
|
||||
rawIn.unread(firstByte);
|
||||
}
|
||||
PostgresWire.Startup first = readPlaintextPacket(rawIn);
|
||||
if (first.code() == PostgresWire.CANCEL_REQUEST) {
|
||||
endpoint.forwardCancel(first.body());
|
||||
return;
|
||||
}
|
||||
if (first.code() == PostgresWire.GSSENC_REQUEST) {
|
||||
rawOut.write('N');
|
||||
rawOut.flush();
|
||||
first = readPlaintextPacket(rawIn);
|
||||
}
|
||||
if (first.code() != PostgresWire.SSL_REQUEST) {
|
||||
refuse(rawOut, "08P01",
|
||||
"unable to accept connection, SSL is mandatory. AWS Authentication is required.", null);
|
||||
return;
|
||||
}
|
||||
rawOut.write('S');
|
||||
rawOut.flush();
|
||||
first = readPlaintextPacket(rawIn);
|
||||
}
|
||||
if (first.code() != PostgresWire.SSL_REQUEST) {
|
||||
refuse(rawOut, "08P01", "unable to accept connection, SSL is mandatory. AWS Authentication is required.",
|
||||
null);
|
||||
return;
|
||||
}
|
||||
rawOut.write('S');
|
||||
rawOut.flush();
|
||||
|
||||
String[] serverName = new String[1];
|
||||
SSLSocket tls = (SSLSocket) endpoint.sslContext().getSocketFactory().createSocket(socket, null, true);
|
||||
// A direct connection's first byte, read to tell it apart, is handed back to the TLS handshake.
|
||||
InputStream consumed = direct ? new ByteArrayInputStream(new byte[] {TlsProxyServer.TLS_HANDSHAKE}) : null;
|
||||
SSLSocket tls = (SSLSocket) endpoint.sslContext().getSocketFactory().createSocket(socket, consumed, true);
|
||||
SSLParameters parameters = tls.getSSLParameters();
|
||||
parameters.setSNIMatchers(List.of(new SNIMatcher(StandardConstants.SNI_HOST_NAME) {
|
||||
@Override
|
||||
@@ -140,7 +155,15 @@ final class DsqlSession {
|
||||
}));
|
||||
tls.setSSLParameters(parameters);
|
||||
tls.setUseClientMode(false);
|
||||
tls.setHandshakeApplicationProtocolSelector((handshaking, offered) ->
|
||||
offered.contains(ALPN_POSTGRESQL) ? ALPN_POSTGRESQL : "");
|
||||
tls.startHandshake();
|
||||
if (direct && !ALPN_POSTGRESQL.equals(tls.getApplicationProtocol())) {
|
||||
// DSQL completes a direct handshake without postgresql as its protocol, then closes the
|
||||
// connection without a word (measured).
|
||||
tls.close();
|
||||
return;
|
||||
}
|
||||
|
||||
InputStream in = new BufferedInputStream(tls.getInputStream());
|
||||
OutputStream out = new BufferedOutputStream(tls.getOutputStream());
|
||||
|
||||
Reference in new issue
Block a user