mirror of
https://github.com/floci-io/floci.git
synced 2026-11-23 06:00:46 -05:00
fix(dsql): refuse a connection that sends no TLS server name (#6282)
DSQL knows the cluster only by the TLS server name, and refuses a connection that sends none with FATAL 08P01 "unable to accept connection, sni was not received" once its password is in, whatever the password (measured). The endpoint fell back to the host in the token and let such a connection in. It now refuses it as DSQL does, and admit takes the cluster from the server name alone. The integration tests connected to 127.0.0.1 through pgjdbc's NonValidatingFactory and sent no server name; DsqlClusterFixture now gives pgjdbc a NonValidatingFactory that names the cluster, as the compatibility test's factory does.
This commit is contained in:
1 parent
a9ceee1de6
commit
d20084ade3
6 files changed
+61
-8
No files matched your search
@@ -41,7 +41,9 @@ them. A customer-managed KMS key is recorded and reported; nothing is encrypted
|
||||
A 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 Floci
|
||||
starts, 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 and
|
||||
is signed by Floci's local CA (`GET /_floci/ca.pem`, see [TLS](../configuration/tls.md)), which the
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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()) {
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
+13
@@ -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());
|
||||
|
||||
+30
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user