Light, fluffy, and always free - The AWS Local Emulator alternative
fix(dsql): refuse a connection that sends no TLS server name (#6282)
Alessandro Lattaod20084ade341
diff --git a/docs/services/dsql.md b/docs/services/dsql.mdindex beb8cf13c3..30c79d7cc4 100644--- a/docs/services/dsql.md+++ b/docs/services/dsql.md@@ -41,7 +41,9 @@ them. A customer-managed KMS key is recorded and reported; nothing is encryptedA cluster's endpoint is `<cluster-id>.dsql.<region>.localhost.floci.io`, port `5432`.`localhost.floci.io` resolves to `127.0.0.1` on the host, and to Floci inside the containers Flocistarts, so the same endpoint works from both. All clusters share the port: the TLS server name a-client sends picks the cluster, as DNS does on AWS.+client sends picks the cluster, as DNS does on AWS. A connection that sends no server name is+refused once its password is in, with `FATAL 08P01 unable to accept connection, sni was not+received`, as DSQL refuses it.TLS is mandatory. The certificate covers `*.dsql.<region>.localhost.floci.io` for every Region andis signed by Floci's local CA (`GET /_floci/ca.pem`, see [TLS](../configuration/tls.md)), which thediff --git a/src/main/java/io/github/hectorvent/floci/services/dsql/dataplane/DsqlEndpoint.java b/src/main/java/io/github/hectorvent/floci/services/dsql/dataplane/DsqlEndpoint.javaindex a004ba5a9c..e7008fdc74 100644--- a/src/main/java/io/github/hectorvent/floci/services/dsql/dataplane/DsqlEndpoint.java+++ b/src/main/java/io/github/hectorvent/floci/services/dsql/dataplane/DsqlEndpoint.java@@ -276,9 +276,8 @@ public class DsqlEndpoint {/*** Whether {@code token} lets {@code user} in, checked in the order DSQL reports: the cluster- * the client asked for by name (or, without a name, the one the token is for), the action- * the user needs, the token's expiry, then its signature and, for a role other than- * {@code admin}, the IAM role mapped to it.+ * the client named as its TLS server, the action the user needs, the token's expiry, then its+ * signature and, for a role other than {@code admin}, the IAM role mapped to it.*/Admission admit(String token, String user, String serverName) {URI uri;@@ -294,8 +293,7 @@ public class DsqlEndpoint {return Admission.refused(null);}host = host.toLowerCase(Locale.ROOT);- String target = serverName == null ? host : serverName.toLowerCase(Locale.ROOT);- Route route = routes.get(target);+ Route route = routes.get(serverName.toLowerCase(Locale.ROOT));if (route == null || !route.host().equals(host)) {return Admission.refused(null);}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.javaindex 40a0e11265..85f7bddf5f 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@@ -176,6 +176,12 @@ final class DsqlSession {refuse(out, "08006", ACCESS_DENIED, null);return;}+ if (serverName[0] == null) {+ // DSQL knows the cluster only by the TLS server name, and refuses a connection that sends+ // none once its password is in, whatever the password (measured).+ refuse(out, "08P01", "unable to accept connection, sni was not received", null);+ return;+ }String token = PostgresWire.cString(password.body(), 0).value();DsqlEndpoint.Admission admission = endpoint.admit(token, user, serverName[0]);if (!admission.allowed()) {diff --git a/src/test/java/io/github/hectorvent/floci/services/dsql/DsqlClusterFixture.java b/src/test/java/io/github/hectorvent/floci/services/dsql/DsqlClusterFixture.javaindex 0e8076ab96..c4f55cadd8 100644--- a/src/test/java/io/github/hectorvent/floci/services/dsql/DsqlClusterFixture.java+++ b/src/test/java/io/github/hectorvent/floci/services/dsql/DsqlClusterFixture.java@@ -69,13 +69,17 @@ public final class DsqlClusterFixture {return "jdbc:postgresql://127.0.0.1:" + endpoint.port() + "/postgres";}- /** What JDBC needs to sign in as {@code user}: TLS, without checking Floci's certificate. */+ /**+ * What JDBC needs to sign in as {@code user}: TLS naming the cluster as its server, as DSQL+ * requires, without checking Floci's certificate.+ */public Properties properties(String user, String password) {Properties properties = new Properties();properties.setProperty("user", user);properties.setProperty("password", password);properties.setProperty("sslmode", "require");- properties.setProperty("sslfactory", "org.postgresql.ssl.NonValidatingFactory");+ properties.setProperty("sslfactory", DsqlServerNameSocketFactory.class.getName());+ properties.setProperty(DsqlServerNameSocketFactory.SERVER_NAME, host);return properties;}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.javaindex be2ada1500..8b89499558 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@@ -116,6 +116,19 @@ class DsqlDataPlaneIntegrationTest {assertEquals("08P01", plain.getSQLState());assertTrue(plain.getServerErrorMessage().getDetail().startsWith("Session Id: "));+ // Measured: a connection that names no server is refused once its password is in, whatever it is.+ for (String password : List.of(cluster.adminToken(), "not-a-token")) {+ PSQLException unnamed = assertThrows(PSQLException.class, () -> {+ Properties properties = cluster.properties("admin", password);+ properties.setProperty("sslfactory", "org.postgresql.ssl.NonValidatingFactory");+ DriverManager.getConnection(cluster.url(), properties).close();+ });+ assertEquals("unable to accept connection, sni was not received",+ unnamed.getServerErrorMessage().getMessage());+ assertEquals("08P01", unnamed.getSQLState());+ assertTrue(unnamed.getServerErrorMessage().getDetail().startsWith("Session Id: "));+ }+PSQLException wrongAction = assertThrows(PSQLException.class, () ->cluster.connect("admin", cluster.token("DbConnect", "test", "test", Instant.now(), 900, null)).close());assertEquals("unable to accept connection, access denied", wrongAction.getServerErrorMessage().getMessage());diff --git a/src/test/java/io/github/hectorvent/floci/services/dsql/DsqlServerNameSocketFactory.java b/src/test/java/io/github/hectorvent/floci/services/dsql/DsqlServerNameSocketFactory.javanew file mode 100644index 0000000000..8fc76d0a52--- /dev/null+++ b/src/test/java/io/github/hectorvent/floci/services/dsql/DsqlServerNameSocketFactory.java@@ -0,0 +1,30 @@+package io.github.hectorvent.floci.services.dsql;++import org.postgresql.ssl.NonValidatingFactory;++import java.io.IOException;+import java.net.Socket;+import java.security.GeneralSecurityException;+import java.util.Properties;++/**+ * pgjdbc's {@link NonValidatingFactory}, naming a DSQL cluster as the TLS server whatever address+ * pgjdbc dialled, as a client that resolves the cluster's endpoint does. pgjdbc builds it from the+ * connection's properties, which carry the name in {@link #SERVER_NAME}.+ */+public final class DsqlServerNameSocketFactory extends NonValidatingFactory {++ public static final String SERVER_NAME = "dsqlServerName";++ private final String serverName;++ public DsqlServerNameSocketFactory(Properties info) throws GeneralSecurityException {+ super(null);+ this.serverName = info.getProperty(SERVER_NAME);+ }++ @Override+ public Socket createSocket(Socket socket, String host, int port, boolean autoClose) throws IOException {+ return super.createSocket(socket, serverName, port, autoClose);+ }+}