From 3007534104bbe9a45cfb2c56f67120221fa51f8e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joaqu=C3=ADn=20Gamboa?= Date: Mon, 16 Nov 2026 20:22:41 -0300 Subject: [PATCH] feat(dsql): translate role names and record each cluster's roles (#8631) --- .../dsql/dataplane/DockerDsqlBackend.java | 23 +- .../dsql/dataplane/DsqlBootstrap.java | 26 +- .../dsql/dataplane/DsqlStatements.java | 633 +++++++++++++++++- .../dsql/dataplane/PrefixedRoleNames.java | 60 ++ .../services/dsql/dataplane/RoleNames.java | 9 + src/main/resources/dsql/bootstrap.sql | 169 ++++- .../services/dsql/ExternalPostgresServer.java | 134 ++++ .../DsqlBootstrapExternalIntegrationTest.java | 352 ++++++++++ .../dsql/dataplane/DsqlBootstrapTest.java | 64 +- .../dsql/dataplane/DsqlRoleNamesTest.java | 355 ++++++++++ .../dsql/dataplane/DsqlStatementsTest.java | 38 ++ 11 files changed, 1800 insertions(+), 63 deletions(-) create mode 100644 src/main/java/io/floci/aws/services/dsql/dataplane/PrefixedRoleNames.java create mode 100644 src/test/java/io/floci/aws/services/dsql/ExternalPostgresServer.java create mode 100644 src/test/java/io/floci/aws/services/dsql/dataplane/DsqlBootstrapExternalIntegrationTest.java create mode 100644 src/test/java/io/floci/aws/services/dsql/dataplane/DsqlRoleNamesTest.java diff --git a/src/main/java/io/floci/aws/services/dsql/dataplane/DockerDsqlBackend.java b/src/main/java/io/floci/aws/services/dsql/dataplane/DockerDsqlBackend.java index 6e194163be..33badc9b1f 100644 --- a/src/main/java/io/floci/aws/services/dsql/dataplane/DockerDsqlBackend.java +++ b/src/main/java/io/floci/aws/services/dsql/dataplane/DockerDsqlBackend.java @@ -123,10 +123,25 @@ public class DockerDsqlBackend implements DsqlBackend { } private static void awaitReady(DsqlDatabases.Backend backend) throws SQLException { - Instant deadline = Instant.now().plus(READY_TIMEOUT); + awaitReady(() -> DsqlDatabases.superuser(backend), READY_TIMEOUT); + } + + /** Opens a connection to the database {@link #awaitReady(Connector, Duration)} waits for. */ + @FunctionalInterface + public interface Connector { + Connection open() throws SQLException; + } + + /** + * Runs {@code SELECT 1} through a connection from {@code connector} every 250 ms until it + * succeeds or {@code timeout} has passed, as a PostgreSQL that is still starting refuses + * connections. Public for the tests' throwaway PostgreSQL servers, which wait the same way. + */ + public static void awaitReady(Connector connector, Duration timeout) throws SQLException { + Instant deadline = Instant.now().plus(timeout); SQLException last = null; while (Instant.now().isBefore(deadline)) { - try (Connection connection = DsqlDatabases.superuser(backend); Statement statement = connection.createStatement()) { + try (Connection connection = connector.open(); Statement statement = connection.createStatement()) { statement.execute("SELECT 1"); return; } catch (SQLException e) { @@ -139,11 +154,11 @@ public class DockerDsqlBackend implements DsqlBackend { } } } - throw new SQLException("The database did not start within " + READY_TIMEOUT.toSeconds() + " s", last); + throw new SQLException("The database did not start within " + timeout.toSeconds() + " s", last); } private static void bootstrap(DsqlDatabases.Backend backend) throws SQLException { - String script = DsqlBootstrap.render(DATABASE, SUPERUSER, DsqlEndpoint.ADMIN); + String script = DsqlBootstrap.render(DATABASE, SUPERUSER, DsqlEndpoint.ADMIN, "", "", false); try (Connection connection = DsqlDatabases.superuser(backend); Statement statement = connection.createStatement()) { statement.execute(script); } diff --git a/src/main/java/io/floci/aws/services/dsql/dataplane/DsqlBootstrap.java b/src/main/java/io/floci/aws/services/dsql/dataplane/DsqlBootstrap.java index 5ecfb4c3f9..86a7bdb264 100644 --- a/src/main/java/io/floci/aws/services/dsql/dataplane/DsqlBootstrap.java +++ b/src/main/java/io/floci/aws/services/dsql/dataplane/DsqlBootstrap.java @@ -8,8 +8,10 @@ import java.nio.charset.StandardCharsets; /** * Renders the DSQL bootstrap script for the database, owner role and admin role of one cluster. * The script's placeholders: {@code {{database}}}, {@code {{owner}}} and {@code {{admin}}} as - * identifiers, always quoted; {@code {{owner_literal}}} and {@code {{admin_literal}}} - * as SQL string literals. + * identifiers, always quoted; {@code {{owner_literal}}}, {@code {{admin_literal}}}, + * {@code {{connect_role_literal}}} and {@code {{role_prefix}}} as SQL string literals + * ({@code 'dabc_'}, or {@code ''} for no prefix); {@code {{external}}} as the SQL boolean + * {@code true} or {@code false}. */ final class DsqlBootstrap { @@ -18,14 +20,28 @@ final class DsqlBootstrap { private DsqlBootstrap() { } - /** The script for one cluster's database. */ - static String render(String database, String owner, String admin) { + /** + * The script for one cluster's database. {@code rolePrefix} is what the cluster's backend role + * names start with (empty in Docker mode), and {@code external} says whether the database is one + * of many on a shared server, whose roles are not all the cluster's. There, {@code connectRole} + * names the NOLOGIN role through which the cluster's roles may connect to its database, which + * PUBLIC may not; it must not start with {@code rolePrefix}. In Docker mode it is unused and empty. + */ + static String render(String database, String owner, String admin, String connectRole, String rolePrefix, + boolean external) { + if (external && (connectRole.isEmpty() || connectRole.startsWith(rolePrefix))) { + throw new IllegalArgumentException("The external bootstrap needs a connect role outside the prefix " + + rolePrefix + ", not \"" + connectRole + "\""); + } return template() .replace("{{database}}", identifier(database)) .replace("{{owner_literal}}", literal(owner)) .replace("{{owner}}", identifier(owner)) .replace("{{admin_literal}}", literal(admin)) - .replace("{{admin}}", identifier(admin)); + .replace("{{admin}}", identifier(admin)) + .replace("{{connect_role_literal}}", literal(connectRole)) + .replace("{{role_prefix}}", literal(rolePrefix)) + .replace("{{external}}", Boolean.toString(external)); } private static String template() { diff --git a/src/main/java/io/floci/aws/services/dsql/dataplane/DsqlStatements.java b/src/main/java/io/floci/aws/services/dsql/dataplane/DsqlStatements.java index e967891a75..ff165a0d04 100644 --- a/src/main/java/io/floci/aws/services/dsql/dataplane/DsqlStatements.java +++ b/src/main/java/io/floci/aws/services/dsql/dataplane/DsqlStatements.java @@ -1,9 +1,13 @@ package io.floci.aws.services.dsql.dataplane; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.List; import java.util.Locale; +import java.util.Map; import java.util.Set; +import java.util.TreeSet; +import java.util.regex.Pattern; /** * The statements of a query as Aurora DSQL reads them. DSQL's own syntax ({@code CREATE INDEX @@ -28,8 +32,19 @@ public final class DsqlStatements { private static final String RESERVED_PREFIX = "dsql."; private static final Set ISOLATION_PARAMETERS = Set.of("default_transaction_isolation", "transaction_isolation"); private static final Set ROLE_KINDS = Set.of("ROLE", "USER", "GROUP"); - private static final Set ROLE_SPECIFIERS = Set.of("CURRENT_ROLE", "CURRENT_USER", "SESSION_USER", "PUBLIC", - "NONE"); + /** What PostgreSQL 16 lets {@code REVOKE