Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

fix(dsql): take TLS started with no SSLRequest, as DSQL does (#6296)

Alessandro Lattao1f25fd88cf21
diff --git a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/DsqlCompatTest.java b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/DsqlCompatTest.java
index 63160f5afe..6485f60f37 100644
--- a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/DsqlCompatTest.java
+++ b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/DsqlCompatTest.java
@@ -179,11 +179,33 @@ class DsqlCompatTest {
.hasMessageContaining("Wrong user to action mapping. user: admin, action: DbConnect");
}
+ @Test
+ @Order(6)
+ void adminSignsInWithTlsStartedAtOnce() throws Exception {
+ String token = dsql.utilities().generateDbConnectAdminAuthToken(b -> b.hostname(endpoint).region(REGION));
+
+ try (Connection connection = connect("admin", token, "direct");
+ Statement statement = connection.createStatement();
+ ResultSet row = statement.executeQuery("SELECT current_user")) {
+ assertThat(row.next()).isTrue();
+ assertThat(row.getString(1)).isEqualTo("admin");
+ }
+ }
+
private static Connection connect(String user, String token) throws SQLException {
+ return connect(user, token, "postgres");
+ }
+
+ /**
+ * {@code sslNegotiation} is pgjdbc's: {@code postgres} sends an SSLRequest first, {@code direct}
+ * starts TLS at once, offering the ALPN protocol postgresql.
+ */
+ private static Connection connect(String user, String token, String sslNegotiation) throws SQLException {
Properties properties = new Properties();
properties.setProperty("user", user);
properties.setProperty("password", token);
properties.setProperty("sslmode", "require");
+ properties.setProperty("sslNegotiation", sslNegotiation);
properties.setProperty("sslfactory", ServerNameSocketFactory.class.getName());
properties.setProperty(SERVER_NAME_PROPERTY, endpoint);
String host = TestFixtures.endpoint().getHost();
diff --git a/docs/services/dsql.md b/docs/services/dsql.md
index 30c79d7cc4..853c1b5ebb 100644
--- a/docs/services/dsql.md
+++ b/docs/services/dsql.md
@@ -47,7 +47,10 @@ received`, as DSQL refuses it.
TLS is mandatory. The certificate covers `*.dsql.<region>.localhost.floci.io` for every Region and
is signed by Floci's local CA (`GET /_floci/ca.pem`, see [TLS](../configuration/tls.md)), which the
-containers Floci starts already trust, so `sslmode=verify-full` works.
+containers Floci starts already trust, so `sslmode=verify-full` works. A client may also start TLS
+at once, with no SSLRequest first (`sslnegotiation=direct` in libpq, `sslNegotiation=direct` in
+pgjdbc), offering the ALPN protocol `postgresql` as those clients do; a direct handshake that does
+not offer it ends the connection without a message.
The limits are DSQL's: a startup packet of 10,000 bytes or more, sent over TLS, ends the connection
with `FATAL 08P01 invalid length of startup packet`, and a client message over 10 MiB with
diff --git a/src/main/java/io/github/hectorvent/floci/config/TlsProxyServer.java b/src/main/java/io/github/hectorvent/floci/config/TlsProxyServer.java
index 39efac2ecb..b487a83d16 100644
--- a/src/main/java/io/github/hectorvent/floci/config/TlsProxyServer.java
+++ b/src/main/java/io/github/hectorvent/floci/config/TlsProxyServer.java
@@ -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;
diff --git a/src/main/java/io/github/hectorvent/floci/services/dsql/dataplane/DsqlSession.java b/src/main/java/io/github/hectorvent/floci/services/dsql/dataplane/DsqlSession.java
index 85f7bddf5f..6c2238a8ef 100644
--- a/src/main/java/io/github/hectorvent/floci/services/dsql/dataplane/DsqlSession.java
+++ b/src/main/java/io/github/hectorvent/floci/services/dsql/dataplane/DsqlSession.java
@@ -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());
diff --git a/src/test/java/io/github/hectorvent/floci/services/dsql/DsqlDataPlaneIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/dsql/DsqlDataPlaneIntegrationTest.java
index 8b89499558..0d32d68960 100644
--- a/src/test/java/io/github/hectorvent/floci/services/dsql/DsqlDataPlaneIntegrationTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/dsql/DsqlDataPlaneIntegrationTest.java
@@ -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())) {
diff --git a/src/test/java/io/github/hectorvent/floci/services/dsql/DsqlWireClient.java b/src/test/java/io/github/hectorvent/floci/services/dsql/DsqlWireClient.java
index 13fd989a75..09acf815c2 100644
--- a/src/test/java/io/github/hectorvent/floci/services/dsql/DsqlWireClient.java
+++ b/src/test/java/io/github/hectorvent/floci/services/dsql/DsqlWireClient.java
@@ -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. */