mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -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
+43
@@ -19,6 +19,7 @@ import org.junit.jupiter.api.extension.RegisterExtension;
|
||||
import org.postgresql.util.PSQLException;
|
||||
import org.postgresql.util.PSQLWarning;
|
||||
|
||||
import java.io.EOFException;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
@@ -28,6 +29,7 @@ import java.sql.DriverManager;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
@@ -45,6 +47,7 @@ import static io.github.hectorvent.floci.services.dsql.DsqlWireClient.sync;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTimeoutPreemptively;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
/**
|
||||
@@ -225,6 +228,46 @@ class DsqlDataPlaneIntegrationTest {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* DSQL takes TLS started at once, with no SSLRequest, from a client offering the ALPN protocol
|
||||
* postgresql, as libpq's and pgjdbc's direct negotiation do, and picks postgresql whenever a
|
||||
* client offers it; a direct handshake without it completes and the connection closes without a
|
||||
* word (measured).
|
||||
*/
|
||||
@Test
|
||||
void tlsWithNoSslRequestFirstIsTakenForTheAlpnProtocolPostgresql() throws Exception {
|
||||
Properties properties = cluster.properties("admin", cluster.adminToken());
|
||||
properties.setProperty("sslNegotiation", "direct");
|
||||
try (Connection connection = DriverManager.getConnection(cluster.url(), properties);
|
||||
Statement statement = connection.createStatement();
|
||||
ResultSet row = statement.executeQuery("SELECT current_user")) {
|
||||
assertTrue(row.next());
|
||||
assertEquals("admin", row.getString(1));
|
||||
}
|
||||
try (DsqlWireClient client = DsqlWireClient.direct(endpoint.port(), cluster.host(), "http/1.1", "postgresql")) {
|
||||
assertEquals("postgresql", client.applicationProtocol());
|
||||
client.signIn("admin", cluster.adminToken());
|
||||
client.send(query("SELECT 1"));
|
||||
assertEquals(List.of("SELECT 1"), client.untilReady());
|
||||
}
|
||||
for (String[] alpn : new String[][] {{}, {"http/1.1"}}) {
|
||||
try (DsqlWireClient client = DsqlWireClient.direct(endpoint.port(), cluster.host(), alpn)) {
|
||||
assertEquals("", client.applicationProtocol(), List.of(alpn).toString());
|
||||
// The endpoint's 10 s handshake timeout would close it too, later: only a close at once is this quick.
|
||||
assertThrows(EOFException.class, () -> assertTimeoutPreemptively(Duration.ofSeconds(5), client::next),
|
||||
List.of(alpn).toString());
|
||||
}
|
||||
}
|
||||
try (DsqlWireClient client = DsqlWireClient.tls(endpoint.port(), cluster.host(), "postgresql")) {
|
||||
assertEquals("postgresql", client.applicationProtocol());
|
||||
client.signIn("admin", cluster.adminToken());
|
||||
}
|
||||
try (DsqlWireClient client = DsqlWireClient.tls(endpoint.port(), cluster.host(), "http/1.1")) {
|
||||
assertEquals("", client.applicationProtocol());
|
||||
client.signIn("admin", cluster.adminToken());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void aMessageOfTenMibIsTaken() throws Exception {
|
||||
try (DsqlWireClient client = cluster.wireClient("admin", cluster.adminToken())) {
|
||||
|
||||
@@ -3,6 +3,8 @@ package io.github.hectorvent.floci.services.dsql;
|
||||
import io.github.hectorvent.floci.core.common.PostgresWire;
|
||||
import org.postgresql.ssl.NonValidatingFactory;
|
||||
|
||||
import javax.net.ssl.SSLParameters;
|
||||
import javax.net.ssl.SSLSocket;
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
@@ -22,13 +24,42 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
*/
|
||||
public final class DsqlWireClient implements AutoCloseable {
|
||||
|
||||
private final Socket socket;
|
||||
private final SSLSocket socket;
|
||||
private final InputStream in;
|
||||
private final OutputStream out;
|
||||
|
||||
/** Signs in to the endpoint on {@code port} as {@code user}, naming {@code host} as the server. */
|
||||
public DsqlWireClient(int port, String host, String user, String password) throws Exception {
|
||||
this(port, host);
|
||||
this(handshake(afterSslRequest(port), host, port));
|
||||
signIn(user, password);
|
||||
}
|
||||
|
||||
private DsqlWireClient(SSLSocket tls) throws IOException {
|
||||
socket = tls;
|
||||
in = new BufferedInputStream(socket.getInputStream());
|
||||
out = socket.getOutputStream();
|
||||
}
|
||||
|
||||
/**
|
||||
* TLS to the endpoint on {@code port} after an SSLRequest, naming {@code host} and offering
|
||||
* {@code alpn} (none when empty), and nothing sent on it yet: no startup packet.
|
||||
*/
|
||||
public static DsqlWireClient tls(int port, String host, String... alpn) throws Exception {
|
||||
return new DsqlWireClient(handshake(afterSslRequest(port), host, port, alpn));
|
||||
}
|
||||
|
||||
/** As {@link #tls}, but TLS started at once, with no SSLRequest, as {@code sslnegotiation=direct} does. */
|
||||
public static DsqlWireClient direct(int port, String host, String... alpn) throws Exception {
|
||||
return new DsqlWireClient(handshake(new Socket("127.0.0.1", port), host, port, alpn));
|
||||
}
|
||||
|
||||
/** The ALPN protocol the endpoint picked, empty when none. */
|
||||
public String applicationProtocol() {
|
||||
return socket.getApplicationProtocol();
|
||||
}
|
||||
|
||||
/** Sends the startup packet and the password, and reads up to the first ReadyForQuery. */
|
||||
public DsqlWireClient signIn(String user, String password) throws IOException {
|
||||
Map<String, String> startup = new LinkedHashMap<>();
|
||||
startup.put("user", user);
|
||||
startup.put("database", "postgres");
|
||||
@@ -36,21 +67,26 @@ public final class DsqlWireClient implements AutoCloseable {
|
||||
assertEquals('R', PostgresWire.read(in, Integer.MAX_VALUE, "message").type());
|
||||
send(message('p', PostgresWire.cStringBytes(password)));
|
||||
untilReady();
|
||||
return this;
|
||||
}
|
||||
|
||||
private DsqlWireClient(int port, String host) throws Exception {
|
||||
private static Socket afterSslRequest(int port) throws IOException {
|
||||
Socket plain = new Socket("127.0.0.1", port);
|
||||
plain.getOutputStream().write(PostgresWire.int32(8));
|
||||
plain.getOutputStream().write(PostgresWire.int32(PostgresWire.SSL_REQUEST));
|
||||
assertEquals('S', plain.getInputStream().read());
|
||||
socket = new NonValidatingFactory(null).createSocket(plain, host, port, true);
|
||||
in = new BufferedInputStream(socket.getInputStream());
|
||||
out = socket.getOutputStream();
|
||||
return plain;
|
||||
}
|
||||
|
||||
/** TLS to the endpoint on {@code port}, naming {@code host}, and nothing sent on it yet: no startup packet. */
|
||||
public static DsqlWireClient tls(int port, String host) throws Exception {
|
||||
return new DsqlWireClient(port, host);
|
||||
private static SSLSocket handshake(Socket plain, String host, int port, String... alpn) throws Exception {
|
||||
SSLSocket tls = (SSLSocket) new NonValidatingFactory(null).createSocket(plain, host, port, true);
|
||||
if (alpn.length > 0) {
|
||||
SSLParameters parameters = tls.getSSLParameters();
|
||||
parameters.setApplicationProtocols(alpn);
|
||||
tls.setSSLParameters(parameters);
|
||||
}
|
||||
tls.startHandshake();
|
||||
return tls;
|
||||
}
|
||||
|
||||
/** The next message the endpoint sends; an {@link IOException} once it has closed the connection. */
|
||||
|
||||
Reference in new issue
Block a user