Skip to content

armgit / floci

Public Imported from GitHub
0

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

feat(dsql): serve each cluster's database on the DSQL endpoint (#6102)

Alessandro Lattaof2b62fbbec0e
diff --git a/README.md b/README.md
index 5303fc307b..f1b314f417 100644
--- a/README.md
+++ b/README.md
@@ -337,7 +337,7 @@ For operation-level compatibility, see the [Services Overview](https://floci.io/
| CloudWatch OAM | In-process | Full 15-operation sink, policy, cross-account link, and tagging API |
| ElastiCache | Real Docker | Redis / Valkey protocol, IAM auth, SigV4 validation |
| MemoryDB | Real Docker | Redis / Valkey protocol via real containers; JSON 1.1 control plane; reuses ElastiCache RESP proxy |
-| Aurora DSQL | In-process | Control plane and `AWS::DSQL::Cluster`: clusters, policies, tags; clusters have no database behind them |
+| Aurora DSQL | Real Docker, mock mode available | PostgreSQL 16 per cluster behind a TLS endpoint with IAM token auth for `admin`; no DSQL dialect yet |
| RDS | Real Docker | PostgreSQL, MySQL, MariaDB, IAM auth, JDBC-compatible engines |
| Timestream for InfluxDB | Real Docker, mock mode available | InfluxDB 2.x DB instances and read replica clusters via `influxdb:2.7`; initial auth secret in Secrets Manager; backups and restore through the influx CLI |
| RDS Data API | REST JSON over real RDS containers | Raw SQL execution and transactions for local MySQL / MariaDB RDS resources |
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 425d065041..a033758782 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
@@ -16,26 +16,56 @@ import software.amazon.awssdk.services.dsql.model.GetClusterResponse;
import software.amazon.awssdk.services.dsql.model.ResourceNotFoundException;
import software.amazon.awssdk.services.dsql.model.ValidationException;
+import javax.net.ssl.SSLContext;
+import javax.net.ssl.SSLParameters;
+import javax.net.ssl.SSLSocket;
+import javax.net.ssl.SSLSocketFactory;
+import javax.net.ssl.TrustManagerFactory;
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.net.InetAddress;
+import java.net.Socket;
+import java.net.http.HttpRequest;
+import java.net.http.HttpResponse;
+import java.nio.charset.StandardCharsets;
+import java.security.KeyStore;
+import java.security.cert.Certificate;
+import java.security.cert.CertificateFactory;
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Statement;
import java.time.Duration;
import java.util.Map;
+import java.util.Properties;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
/**
- * Aurora DSQL's control plane through the real AWS SDK and its waiters: a cluster created,
- * described, listed, tagged, protected from deletion, given a policy and deleted.
+ * Aurora DSQL through the real AWS SDK: the control plane and its waiters, cluster policies, and a JDBC connection
+ * to the cluster with a token from {@code DsqlUtilities}, the SDK's own signer. The connection goes
+ * to Floci's address with the cluster's endpoint as the TLS server name, which is how Floci picks
+ * the cluster, and the certificate is verified for that name against Floci's CA.
+ *
+ * Requires Docker (each cluster's database is a real PostgreSQL container).
*/
@DisplayName("Aurora DSQL")
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
class DsqlCompatTest {
private static final Region REGION = TestFixtures.region();
- private static final Duration WAIT_TIMEOUT =
- TestFixtures.isRealAws() ? Duration.ofMinutes(10) : Duration.ofSeconds(10);
+ /** A cluster is ACTIVE once its database answers: a container start on Floci, minutes on AWS. */
+ private static final Duration ACTIVE_TIMEOUT =
+ TestFixtures.isRealAws() ? Duration.ofMinutes(10) : Duration.ofMinutes(3);
+ private static final Duration DELETE_TIMEOUT =
+ TestFixtures.isRealAws() ? Duration.ofMinutes(10) : Duration.ofMinutes(1);
+ private static final String SERVER_NAME_PROPERTY = "dsqlServerName";
private static DsqlClient dsql;
private static String identifier;
+ private static String endpoint;
@BeforeAll
static void createCluster() {
@@ -46,7 +76,10 @@ class DsqlCompatTest {
identifier = created.identifier();
assertThat(created.status()).isEqualTo(ClusterStatus.CREATING);
- dsql.waiter().waitUntilClusterActive(b -> b.identifier(identifier), w -> w.waitTimeout(WAIT_TIMEOUT));
+ GetClusterResponse active = dsql.waiter()
+ .waitUntilClusterActive(b -> b.identifier(identifier), w -> w.waitTimeout(ACTIVE_TIMEOUT))
+ .matched().response().orElseThrow();
+ endpoint = active.endpoint();
}
@AfterAll
@@ -55,7 +88,7 @@ class DsqlCompatTest {
return;
}
dsql.deleteCluster(b -> b.identifier(identifier));
- dsql.waiter().waitUntilClusterNotExists(b -> b.identifier(identifier), w -> w.waitTimeout(WAIT_TIMEOUT));
+ dsql.waiter().waitUntilClusterNotExists(b -> b.identifier(identifier), w -> w.waitTimeout(DELETE_TIMEOUT));
dsql.close();
}
@@ -107,4 +140,121 @@ class DsqlCompatTest {
assertThatThrownBy(() -> dsql.getClusterPolicy(b -> b.identifier(identifier)))
.isInstanceOf(ResourceNotFoundException.class);
}
+
+ @Test
+ @Order(4)
+ void adminSignsInWithTheSdksToken() throws Exception {
+ String token = dsql.utilities().generateDbConnectAdminAuthToken(b -> b.hostname(endpoint).region(REGION));
+
+ try (Connection connection = connect("admin", token); Statement statement = connection.createStatement()) {
+ try (ResultSet row = statement.executeQuery("SELECT current_user, current_setting('transaction_isolation')")) {
+ assertThat(row.next()).isTrue();
+ assertThat(row.getString(1)).isEqualTo("admin");
+ assertThat(row.getString(2)).isEqualTo("repeatable read");
+ }
+ statement.execute("CREATE TABLE compat_orders (id int PRIMARY KEY, n int)");
+ statement.execute("INSERT INTO compat_orders VALUES (1, 7)");
+ try (ResultSet row = statement.executeQuery("SELECT n FROM compat_orders WHERE id = 1")) {
+ assertThat(row.next()).isTrue();
+ assertThat(row.getInt(1)).isEqualTo(7);
+ }
+ }
+ }
+
+ @Test
+ @Order(5)
+ void adminIsRefusedATokenForAnotherRole() {
+ String token = dsql.utilities().generateDbConnectAuthToken(b -> b.hostname(endpoint).region(REGION));
+
+ assertThatThrownBy(() -> connect("admin", token).close())
+ .isInstanceOf(SQLException.class)
+ .hasMessageContaining("unable to accept connection, access denied")
+ .hasMessageContaining("Wrong user to action mapping. user: admin, action: DbConnect");
+ }
+
+ private static Connection connect(String user, String token) throws SQLException {
+ Properties properties = new Properties();
+ properties.setProperty("user", user);
+ properties.setProperty("password", token);
+ properties.setProperty("sslmode", "require");
+ properties.setProperty("sslfactory", ServerNameSocketFactory.class.getName());
+ properties.setProperty(SERVER_NAME_PROPERTY, endpoint);
+ String host = TestFixtures.endpoint().getHost();
+ return DriverManager.getConnection("jdbc:postgresql://" + host + ":" + dsqlPort() + "/postgres", properties);
+ }
+
+ /** DSQL's port, 5432, unless the Floci under test serves it elsewhere (FLOCI_SERVICES_DSQL_PORT). */
+ private static int dsqlPort() {
+ String port = System.getenv("FLOCI_DSQL_PORT");
+ return port == null || port.isBlank() ? 5432 : Integer.parseInt(port);
+ }
+
+ /**
+ * Opens TLS to whatever pgjdbc dialled, naming the cluster's endpoint and verifying the
+ * certificate for it against Floci's CA. pgjdbc builds it from the connection's properties.
+ */
+ public static final class ServerNameSocketFactory extends SSLSocketFactory {
+
+ private final SSLSocketFactory delegate;
+ private final String serverName;
+
+ public ServerNameSocketFactory(Properties info) throws Exception {
+ this.serverName = info.getProperty(SERVER_NAME_PROPERTY);
+ HttpResponse<String> ca = TestFixtures.emulatorHttpClient().send(
+ HttpRequest.newBuilder(TestFixtures.endpoint().resolve("/_floci/ca.pem")).build(),
+ HttpResponse.BodyHandlers.ofString());
+ KeyStore trusted = KeyStore.getInstance(KeyStore.getDefaultType());
+ trusted.load(null, null);
+ int index = 0;
+ for (Certificate certificate : CertificateFactory.getInstance("X.509").generateCertificates(
+ new ByteArrayInputStream(ca.body().getBytes(StandardCharsets.US_ASCII)))) {
+ trusted.setCertificateEntry("floci-" + index++, certificate);
+ }
+ TrustManagerFactory trust = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
+ trust.init(trusted);
+ SSLContext context = SSLContext.getInstance("TLS");
+ context.init(null, trust.getTrustManagers(), null);
+ this.delegate = context.getSocketFactory();
+ }
+
+ @Override
+ public Socket createSocket(Socket socket, String host, int port, boolean autoClose) throws IOException {
+ SSLSocket tls = (SSLSocket) delegate.createSocket(socket, serverName, port, autoClose);
+ SSLParameters parameters = tls.getSSLParameters();
+ parameters.setEndpointIdentificationAlgorithm("HTTPS");
+ tls.setSSLParameters(parameters);
+ return tls;
+ }
+
+ @Override
+ public String[] getDefaultCipherSuites() {
+ return delegate.getDefaultCipherSuites();
+ }
+
+ @Override
+ public String[] getSupportedCipherSuites() {
+ return delegate.getSupportedCipherSuites();
+ }
+
+ @Override
+ public Socket createSocket(String host, int port) throws IOException {
+ return delegate.createSocket(host, port);
+ }
+
+ @Override
+ public Socket createSocket(String host, int port, InetAddress localHost, int localPort) throws IOException {
+ return delegate.createSocket(host, port, localHost, localPort);
+ }
+
+ @Override
+ public Socket createSocket(InetAddress host, int port) throws IOException {
+ return delegate.createSocket(host, port);
+ }
+
+ @Override
+ public Socket createSocket(InetAddress address, int port, InetAddress localAddress, int localPort)
+ throws IOException {
+ return delegate.createSocket(address, port, localAddress, localPort);
+ }
+ }
}
diff --git a/docs/services/dsql.md b/docs/services/dsql.md
index 3c58ea9e34..32af425c3f 100644
--- a/docs/services/dsql.md
+++ b/docs/services/dsql.md
@@ -1,23 +1,23 @@
# Aurora DSQL
-**Protocol:** REST JSON
+**Protocol:** REST JSON (control plane) + PostgreSQL wire protocol over TLS (clusters)
-**Endpoint:** `http://localhost:4566`
+**Endpoint:** `http://localhost:4566` (control plane), `<cluster-id>.dsql.<region>.localhost.floci.io:5432` (clusters)
-Floci serves Aurora DSQL's control plane and `AWS::DSQL::Cluster`. A cluster has no database
-behind it here: it is `ACTIVE` as soon as it is created, and its endpoint is named as on AWS but
-not served. The answers and messages below were measured on AWS.
+Floci runs each Aurora DSQL cluster as a real PostgreSQL 16 database in a Docker container, behind one
+endpoint that admits clients the way DSQL does: TLS and an IAM token as the password. The answers,
+messages and SQLSTATEs below were measured on AWS.
## Supported Actions
<!-- floci:actions:start -->
| Action | Description |
| --- | --- |
-| `CreateCluster` | Creates a cluster, `CREATING` in the answer and `ACTIVE` right after, or `PENDING_SETUP` for a multi-Region one; deletion protection is on unless turned off |
+| `CreateCluster` | Creates a cluster, `CREATING` until its database answers, then `ACTIVE`, or `PENDING_SETUP` for a multi-Region one; deletion protection is on unless turned off |
| `ListClusters` | Lists the Region's clusters, paginated |
| `GetCluster` | Returns a cluster, its endpoint and encryption details |
| `UpdateCluster` | Changes deletion protection, the KMS key and the multi-Region properties |
-| `DeleteCluster` | Deletes a cluster, `DELETING` in the answer or `PENDING_DELETE` for a multi-Region one; refused while deletion protection is on |
+| `DeleteCluster` | Deletes a cluster and its database, `DELETING` in the answer or `PENDING_DELETE` for a multi-Region one; refused while deletion protection is on |
| `GetVpcEndpointServiceName` | Returns the Region's VPC endpoint service name |
| `PutClusterPolicy` | Sets the cluster's resource policy once it passes DSQL's checks, with optimistic versioning |
| `GetClusterPolicy` | Returns the resource policy in DSQL's form, and its version |
@@ -28,12 +28,72 @@ not served. The answers and messages below were measured on AWS.
that is not a well-formed cluster ARN is refused with `AccessDeniedException`, one of another
Region with `BadRequestException`, and one of no cluster with `NotFoundException`. Cluster
identifiers are 26 characters of lowercase base32, as DSQL's are, and invalid ones are refused with
-DSQL's messages. A cluster's endpoint is `<cluster-id>.dsql.<region>.localhost.floci.io`.
+DSQL's messages.
A cluster created with a witness Region is `PENDING_SETUP`, and lists itself among its clusters.
Once it and each peer it is updated with name one another, all of them are `ACTIVE`, as on AWS,
-where this takes about two minutes. Nothing is replicated between them. A customer-managed KMS key
-is recorded and reported; nothing is encrypted with it.
+where this takes about two minutes; a cluster whose database has not answered yet is `CREATING`
+until it does. Each has a database of its own; nothing is replicated between
+them. A customer-managed KMS key is recorded and reported; nothing is encrypted with it.
+
+## Connecting
+
+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.
+
+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.
+
+The password is an IAM authentication token for the endpoint, generated by the SDKs and the CLI
+exactly as for AWS:
+
+```bash
+curl -s http://localhost:4566/_floci/ca.pem -o floci-root-ca.pem
+export PGPASSWORD=$(AWS_ACCESS_KEY_ID=test AWS_SECRET_ACCESS_KEY=test \
+ aws dsql generate-db-connect-admin-auth-token --region us-east-1 \
+ --hostname <cluster-id>.dsql.us-east-1.localhost.floci.io)
+psql "host=<cluster-id>.dsql.us-east-1.localhost.floci.io port=5432 user=admin dbname=postgres \
+ sslmode=verify-full sslrootcert=floci-root-ca.pem"
+```
+
+When Floci runs in Docker, publish `5432` alongside `4566`.
+
+### Authentication
+
+- `admin` signs in with a `DbConnectAdmin` token; every other role with a `DbConnect` token. The
+ wrong action is refused with the hint `Wrong user to action mapping. user: <user>, action: <action>`.
+- A token is accepted up to 5 seconds past its expiry, as DSQL accepts it; after that the hint is
+ `Signature expired: ...`.
+- Only `admin` signs in. DSQL lets another role in only for an IAM role that `AWS IAM GRANT` mapped
+ to it, which Floci does not support yet, so every other role is refused as an unmapped one is.
+- The signature is verified like any other Floci IAM token: the `test` key, or a key or session
+ Floci issued. With `FLOCI_SERVICES_IAM_ENFORCEMENT_ENABLED`, the principal also needs
+ `dsql:DbConnectAdmin` or `dsql:DbConnect` on the cluster ARN.
+- Refusals are `FATAL` with DSQL's text and a `Session Id` detail: `08P01` without TLS, `08006` for
+ a token refused, `28000` for a role the token cannot sign in as, `55000` for any database other
+ than `postgres`, and `42704` for a custom (dotted) parameter in the startup.
+- `options` is read as DSQL reads it: only `-c name=value` counts, the first one for a name wins,
+ and every other word, `--name=value` included, is ignored.
+
+## SQL
+
+The database is PostgreSQL 16 at `REPEATABLE READ`, as DSQL's is, with the role `admin`, which owns
+the `public` schema and can create roles and databases. A connection closes after an hour, as DSQL
+closes it.
+
+### Differences from AWS
+
+- DSQL's SQL dialect is not applied: what DSQL adds to PostgreSQL (`CREATE INDEX ASYNC`, `AWS IAM
+ GRANT`, the `sys` functions) is refused as PostgreSQL refuses it, what DSQL refuses works, and
+ DSQL's transaction limits (one DDL statement, 3,000 rows, 300 seconds) do not apply.
+- DSQL is optimistic: conflicting transactions both run and the second to commit fails with
+ `40001`. Floci's database is PostgreSQL, so a write that conflicts waits for the other
+ transaction, then fails with PostgreSQL's `40001`.
+- DSQL accepts connections while a cluster is still `CREATING`; Floci's cluster is `CREATING` only
+ while its database starts, and serves once its database answers.
## Cluster policies
@@ -62,7 +122,8 @@ it with DSQL's messages:
The policy is kept and returned in DSQL's form: pretty-printed, its elements in DSQL's order, a
list of one value written as that value, condition values as text, an account id as its root ARN,
and `2008-10-17` when it has no `Version`. A policy's version is the time it was set, in epoch
-milliseconds. Cluster policies are not enforced: Floci's IAM enforcement does not cover DSQL.
+milliseconds. Cluster policies are not enforced: Floci's IAM enforcement checks a connection's
+identity policies (see [Authentication](#authentication)), and no control plane call.
## CloudFormation
@@ -80,7 +141,15 @@ on AWS.
| Variable | Default | Description |
|---|---|---|
| `FLOCI_SERVICES_DSQL_ENABLED` | `true` | Enable or disable Aurora DSQL |
+| `FLOCI_SERVICES_DSQL_MOCK` | `false` | Clusters without a database, `ACTIVE` at once and without Docker: enough for the control plane and CloudFormation |
+| `FLOCI_SERVICES_DSQL_PORT` | `5432` | Port every cluster endpoint is served on |
| `FLOCI_SERVICES_DSQL_ENDPOINT_SUFFIX` | `localhost.floci.io` | Domain the endpoints are named under, `<cluster-id>.dsql.<region>.<suffix>` |
+| `FLOCI_SERVICES_DSQL_IMAGE` | `postgres:16-alpine` | Image each cluster's database runs in |
+| `FLOCI_SERVICES_DSQL_DOCKER_NETWORK` | Floci's network | Docker network for the databases |
+
+A cluster's data lives in a Docker volume of its own, or under the host persistent path, and is
+removed with the cluster under the same policy as RDS's (`prune-volumes-on-delete`). With
+persistent storage a cluster comes back, with its data, when Floci restarts.
See the [Aurora DSQL API Reference](https://docs.aws.amazon.com/aurora-dsql/latest/APIReference/Welcome.html)
and the [Aurora DSQL User Guide](https://docs.aws.amazon.com/aurora-dsql/latest/userguide/what-is-aurora-dsql.html).
diff --git a/docs/services/index.md b/docs/services/index.md
index b518b7efaa..f5460c72b6 100644
--- a/docs/services/index.md
+++ b/docs/services/index.md
@@ -63,7 +63,7 @@ Operation counts are exact. For dispatch-table services (Query and JSON 1.1) eac
| [Amazon Data Lifecycle Manager](dlm.md) | `/policies`, `/policies/{policyId}`, `/tags/{resourceArn}` | REST JSON | 8 |
| [ElastiCache](elasticache.md) | `POST /` with `Action=` param + TCP proxy | Query + RESP | 30 |
| [MemoryDB](memorydb.md) | `POST /` + `X-Amz-Target: AmazonMemoryDB.*` + TCP proxy | JSON 1.1 + RESP | 13 |
-| [Aurora DSQL](dsql.md) | `/cluster`, `/cluster/{identifier}`, `/cluster/{identifier}/policy` | REST JSON | 9 |
+| [Aurora DSQL](dsql.md) | `/cluster`, `/cluster/{identifier}`, `/cluster/{identifier}/policy` + TLS endpoint | REST JSON + wire | 9 |
| [RDS](rds.md) | `POST /` with `Action=` param + TCP proxy | Query + wire | 109 |
| [RDS Data API](rds-data.md) | `/Execute`, `/BeginTransaction`, `/CommitTransaction`, `/RollbackTransaction` | REST JSON | 4 |
| [Timestream for InfluxDB](timestream-influxdb.md) | `POST /` + `X-Amz-Target: AmazonTimestreamInfluxDB.*` + InfluxDB container | JSON 1.0 + InfluxDB HTTP | 24 |
diff --git a/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.java b/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.java
index 9b9d743e50..2e34c2c1ee 100644
--- a/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.java
+++ b/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.java
@@ -1776,13 +1776,32 @@ public interface EmulatorConfig {
@WithDefault("true")
boolean enabled();
+ /** When true, clusters have no database and are ACTIVE at once, without Docker. */
+ @WithDefault("false")
+ boolean mock();
+
+ /**
+ * Port every cluster endpoint is served on, as on AWS, where a DSQL endpoint is always
+ * reached on 5432. Clients connect to {@code <id>.dsql.<region>.<endpoint-suffix>} with TLS,
+ * and the server name they send picks the cluster.
+ */
+ @WithDefault("5432")
+ int port();
+
/**
* Domain under which cluster endpoints are named, {@code <id>.dsql.<region>.<suffix>}. The
* default resolves to the loopback address on the host and to Floci inside its Docker
- * network.
+ * network, so an endpoint works unchanged in both.
*/
@WithDefault("localhost.floci.io")
String endpointSuffix();
+
+ /** PostgreSQL image each cluster's database runs in. DSQL is PostgreSQL 16 compatible. */
+ @WithDefault("postgres:16-alpine")
+ String image();
+
+ /** Docker network for the database containers. Defaults to Floci's own network. */
+ Optional<String> dockerNetwork();
}
interface RdsDataServiceConfig {
diff --git a/src/main/java/io/github/hectorvent/floci/core/common/PostgresWire.java b/src/main/java/io/github/hectorvent/floci/core/common/PostgresWire.java
new file mode 100644
index 0000000000..4ebb67ba73
--- /dev/null
+++ b/src/main/java/io/github/hectorvent/floci/core/common/PostgresWire.java
@@ -0,0 +1,215 @@
+package io.github.hectorvent.floci.core.common;
+
+import java.io.ByteArrayOutputStream;
+import java.io.EOFException;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.nio.charset.StandardCharsets;
+import java.util.LinkedHashMap;
+import java.util.Map;
+
+/**
+ * PostgreSQL's frontend/backend protocol, version 3, as every PostgreSQL endpoint in Floci reads
+ * and writes it (the RDS proxy, Redshift's bridge, Aurora DSQL's endpoint): a message's framing,
+ * the startup packet and its parameters, and the fields of an ErrorResponse or NoticeResponse.
+ */
+public final class PostgresWire {
+
+ public static final int PROTOCOL_3 = 196608;
+ public static final int SSL_REQUEST = 80877103;
+ public static final int GSSENC_REQUEST = 80877104;
+ public static final int CANCEL_REQUEST = 80877102;
+
+ private PostgresWire() {
+ }
+
+ /** A message after the startup: its type byte and its body, without the length. */
+ public record Message(byte type, byte[] body) {
+ }
+
+ /** A startup-phase packet, which has no type byte: its code and the rest of its body. */
+ public record Startup(int code, byte[] body) {
+ }
+
+ /** The NUL-terminated string at an offset, and where the next field starts. */
+ public record CString(String value, int next) {
+ }
+
+ // ── Framing ───────────────────────────────────────────────────────────────
+
+ /** Reads a startup-phase packet: its length, refused outside 8 to {@code maxLength}, code and body. */
+ public static Startup readStartup(InputStream in, int maxLength, String packetName) throws IOException {
+ int length = checkedLength(readInt32(in), 8, maxLength, packetName);
+ int code = readInt32(in);
+ byte[] body = new byte[length - 8];
+ readFully(in, body);
+ return new Startup(code, body);
+ }
+
+ /** Reads one message, or throws {@link EOFException} at the end of the stream. */
+ public static Message read(InputStream in, int maxLength, String packetName) throws IOException {
+ int type = in.read();
+ if (type < 0) {
+ throw new EOFException("Connection closed before a " + packetName);
+ }
+ return new Message((byte) type, readBody(in, 4, maxLength, packetName));
+ }
+
+ /** The body of a message whose type byte was read already: its length, checked, then the rest. */
+ public static byte[] readBody(InputStream in, int minLength, int maxLength, String packetName) throws IOException {
+ byte[] body = new byte[checkedLength(readInt32(in), minLength, maxLength, packetName) - 4];
+ readFully(in, body);
+ return body;
+ }
+
+ /** Writes a message of {@code type} whose body is {@code parts} one after the other. */
+ public static void write(OutputStream out, int type, byte[]... parts) throws IOException {
+ int length = 4;
+ for (byte[] part : parts) {
+ length += part.length;
+ }
+ out.write(type);
+ writeInt32(out, length);
+ for (byte[] part : parts) {
+ out.write(part);
+ }
+ }
+
+ public static void write(OutputStream out, Message message) throws IOException {
+ write(out, message.type(), message.body());
+ }
+
+ /** A whole message, its type, length and body, as bytes. */
+ public static byte[] frame(int type, byte[] body) {
+ ByteArrayOutputStream out = new ByteArrayOutputStream(body.length + 5);
+ out.write(type);
+ out.writeBytes(int32(body.length + 4));
+ out.writeBytes(body);
+ return out.toByteArray();
+ }
+
+ /** {@code length}, refused when below {@code minimum} or above {@code maximum}. */
+ public static int checkedLength(int length, int minimum, int maximum, String packetName) throws IOException {
+ if (length < minimum) {
+ throw new IOException("PostgreSQL " + packetName + " length is below the "
+ + minimum + " byte minimum: " + length);
+ }
+ if (length > maximum) {
+ throw new IOException("PostgreSQL " + packetName + " length exceeds the "
+ + maximum + " byte limit: " + length);
+ }
+ return length;
+ }
+
+ public static int readInt32(InputStream in) throws IOException {
+ int b0 = in.read();
+ int b1 = in.read();
+ int b2 = in.read();
+ int b3 = in.read();
+ if ((b0 | b1 | b2 | b3) < 0) {
+ throw new EOFException("Connection closed while reading Int32");
+ }
+ return (b0 << 24) | (b1 << 16) | (b2 << 8) | b3;
+ }
+
+ public static void readFully(InputStream in, byte[] buffer) throws IOException {
+ int offset = 0;
+ while (offset < buffer.length) {
+ int read = in.read(buffer, offset, buffer.length - offset);
+ if (read < 0) {
+ throw new EOFException("Connection closed while reading " + buffer.length + " bytes");
+ }
+ offset += read;
+ }
+ }
+
+ public static void writeInt32(OutputStream out, int value) throws IOException {
+ out.write(int32(value));
+ }
+
+ public static byte[] int32(int value) {
+ return new byte[]{(byte) (value >> 24), (byte) (value >> 16), (byte) (value >> 8), (byte) value};
+ }
+
+ public static int int32(byte[] bytes, int offset) {
+ return ((bytes[offset] & 0xFF) << 24) | ((bytes[offset + 1] & 0xFF) << 16)
+ | ((bytes[offset + 2] & 0xFF) << 8) | (bytes[offset + 3] & 0xFF);
+ }
+
+ // ── Strings ───────────────────────────────────────────────────────────────
+
+ public static CString cString(byte[] body, int offset) {
+ int end = offset;
+ while (end < body.length && body[end] != 0) {
+ end++;
+ }
+ return new CString(new String(body, offset, end - offset, StandardCharsets.UTF_8), end + 1);
+ }
+
+ public static byte[] cStringBytes(String value) {
+ byte[] text = value.getBytes(StandardCharsets.UTF_8);
+ byte[] bytes = new byte[text.length + 1];
+ System.arraycopy(text, 0, bytes, 0, text.length);
+ return bytes;
+ }
+
+ // ── Startup ───────────────────────────────────────────────────────────────
+
+ /** The {@code name=value} pairs of a StartupMessage body, in the order the client sent them. */
+ public static Map<String, String> startupParameters(byte[] body) {
+ Map<String, String> parameters = new LinkedHashMap<>();
+ int offset = 0;
+ while (offset < body.length) {
+ CString name = cString(body, offset);
+ if (name.value().isEmpty() || name.next() > body.length) {
+ break;
+ }
+ CString value = cString(body, name.next());
+ parameters.put(name.value(), value.value());
+ offset = value.next();
+ }
+ return parameters;
+ }
+
+ /** A whole StartupMessage for protocol 3 carrying {@code parameters} in their order. */
+ public static byte[] startupMessage(Map<String, String> parameters) {
+ ByteArrayOutputStream body = new ByteArrayOutputStream();
+ body.writeBytes(int32(PROTOCOL_3));
+ parameters.forEach((name, value) -> {
+ body.writeBytes(cStringBytes(name));
+ body.writeBytes(cStringBytes(value));
+ });
+ body.write(0);
+ ByteArrayOutputStream packet = new ByteArrayOutputStream(body.size() + 4);
+ packet.writeBytes(int32(body.size() + 4));
+ packet.writeBytes(body.toByteArray());
+ return packet.toByteArray();
+ }
+
+ // ── ErrorResponse and NoticeResponse ──────────────────────────────────────
+
+ /** The body of an ErrorResponse ({@code E}) or NoticeResponse ({@code N}) from its fields, in order. */
+ public static byte[] fields(Map<Character, String> fields) {
+ ByteArrayOutputStream body = new ByteArrayOutputStream();
+ fields.forEach((code, value) -> {
+ body.write(code);
+ body.writeBytes(cStringBytes(value));
+ });
+ body.write(0);
+ return body.toByteArray();
+ }
+
+ /** The fields of an ErrorResponse or NoticeResponse whose first field starts at {@code offset}. */
+ public static Map<Character, String> parseFields(byte[] bytes, int offset) {
+ Map<Character, String> fields = new LinkedHashMap<>();
+ int i = offset;
+ while (i < bytes.length && bytes[i] != 0) {
+ char code = (char) bytes[i];
+ CString value = cString(bytes, i + 1);
+ fields.put(code, value.value());
+ i = value.next();
+ }
+ return fields;
+ }
+}
diff --git a/src/main/java/io/github/hectorvent/floci/core/common/auth/SigV4RequestValidator.java b/src/main/java/io/github/hectorvent/floci/core/common/auth/SigV4RequestValidator.java
index 141e774b57..2aa50c5a62 100644
--- a/src/main/java/io/github/hectorvent/floci/core/common/auth/SigV4RequestValidator.java
+++ b/src/main/java/io/github/hectorvent/floci/core/common/auth/SigV4RequestValidator.java
@@ -9,6 +9,7 @@ import javax.crypto.spec.SecretKeySpec;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
+import java.time.Duration;
import java.time.Instant;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
@@ -88,6 +89,21 @@ public final class SigV4RequestValidator {
public boolean validate(String rawQuery, String canonicalHostHeaderValue, String identityParamName,
boolean identityRequired, String expectedIdentityValue, String logLabel,
String ownerAccountId) {
+ return validate(rawQuery, canonicalHostHeaderValue, "connect", identityParamName, identityRequired,
+ expectedIdentityValue, logLabel, ownerAccountId, Duration.ZERO);
+ }
+
+ /**
+ * As above, for a token whose {@code Action} is not {@code connect} and whose service accepts
+ * it for a while after it expires: Aurora DSQL signs {@code DbConnect} and
+ * {@code DbConnectAdmin}, and takes a token up to 5 seconds past its expiry.
+ *
+ * @param expectedAction the {@code Action} the token must carry
+ * @param expiryGrace how long after its expiry the token is still accepted
+ */
+ public boolean validate(String rawQuery, String canonicalHostHeaderValue, String expectedAction,
+ String identityParamName, boolean identityRequired, String expectedIdentityValue,
+ String logLabel, String ownerAccountId, Duration expiryGrace) {
try {
List<QueryParameter> parameters = decodeQuery(rawQuery);
String action = findParam(parameters, "Action");
@@ -98,7 +114,7 @@ public final class SigV4RequestValidator {
String signedHeaders = findParam(parameters, "X-Amz-SignedHeaders");
String signature = findParam(parameters, "X-Amz-Signature");
- if (!"connect".equals(action) || (identityRequired && identity == null) || dateTime == null
+ if (!expectedAction.equals(action) || (identityRequired && identity == null) || dateTime == null
|| expires == null || credential == null || signedHeaders == null || signature == null) {
LOG.debugv("{0} missing required SigV4 parameters", logLabel);
return false;
@@ -112,7 +128,7 @@ public final class SigV4RequestValidator {
Instant tokenTime = Instant.from(DATETIME_FMT.parse(dateTime));
int expirySeconds = Integer.parseInt(expires);
- if (Instant.now().isAfter(tokenTime.plusSeconds(expirySeconds))) {
+ if (Instant.now().minus(expiryGrace).isAfter(tokenTime.plusSeconds(expirySeconds))) {
LOG.debugv("{0} expired", logLabel);
return false;
}
diff --git a/src/main/java/io/github/hectorvent/floci/services/dsql/DsqlDataPlane.java b/src/main/java/io/github/hectorvent/floci/services/dsql/DsqlDataPlane.java
index a0ad87a860..4fa8e5cce0 100644
--- a/src/main/java/io/github/hectorvent/floci/services/dsql/DsqlDataPlane.java
+++ b/src/main/java/io/github/hectorvent/floci/services/dsql/DsqlDataPlane.java
@@ -1,24 +1,132 @@
package io.github.hectorvent.floci.services.dsql;
+import io.github.hectorvent.floci.config.EmulatorConfig;
+import io.github.hectorvent.floci.services.dsql.dataplane.DsqlDatabases;
+import io.github.hectorvent.floci.services.dsql.dataplane.DsqlEndpoint;
import io.github.hectorvent.floci.services.dsql.model.DsqlCluster;
+import io.quarkus.runtime.ShutdownEvent;
import jakarta.enterprise.context.ApplicationScoped;
+import jakarta.enterprise.event.Observes;
+import jakarta.inject.Inject;
+import org.jboss.logging.Logger;
+import java.io.IOException;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Consumer;
/**
- * The databases behind the clusters. A cluster has none here: it is ready as soon as it is
- * created, and its endpoint is named but not served.
+ * Runs each cluster's database and serves it on the DSQL endpoint. A cluster's database starts in
+ * the background, and the cluster becomes ACTIVE once it answers, as on AWS. With
+ * {@code floci.services.dsql.mock} on, a cluster has none and is ready at once, which is enough
+ * for the control plane and CloudFormation.
*/
@ApplicationScoped
public class DsqlDataPlane {
+ private static final Logger LOG = Logger.getLogger(DsqlDataPlane.class);
+
+ private final boolean mock;
+ private final boolean memoryStorage;
+ private final DsqlDatabases databases;
+ private final DsqlEndpoint endpoint;
+ private final Map<String, DsqlCluster> running = new ConcurrentHashMap<>();
+ private final Set<String> wanted = ConcurrentHashMap.newKeySet();
+ // Whether a cluster is still wanted and its route are changed together, so a deletion during a
+ // start never leaves the route of a database already gone.
+ private final Object routes = new Object();
+
+ @Inject
+ public DsqlDataPlane(EmulatorConfig config, DsqlDatabases databases, DsqlEndpoint endpoint) {
+ this.mock = config.services().dsql().mock();
+ this.memoryStorage = "memory".equals(config.storage().mode());
+ this.databases = databases;
+ this.endpoint = endpoint;
+ }
+
/** Starts the cluster's database, then reports whether it is ready to serve. */
public void provision(DsqlCluster cluster, Consumer<Boolean> onReady) {
- onReady.accept(true);
+ if (mock) {
+ onReady.accept(true);
+ return;
+ }
+ String arn = cluster.getArn();
+ wanted.add(arn);
+ Thread.ofVirtual().name("dsql-start-" + cluster.getIdentifier()).start(() -> {
+ DsqlDatabases.Backend backend;
+ try {
+ backend = databases.start(cluster);
+ } catch (Exception e) {
+ LOG.warnv(e, "The database of DSQL cluster {0} did not start", cluster.getIdentifier());
+ onReady.accept(false);
+ return;
+ }
+ boolean deleted;
+ boolean ready = false;
+ synchronized (routes) {
+ deleted = !wanted.contains(arn);
+ if (!deleted) {
+ try {
+ endpoint.register(cluster, backend);
+ running.put(arn, cluster);
+ ready = true;
+ } catch (IOException e) {
+ LOG.warnv(e, "The DSQL endpoint cannot serve cluster {0}", cluster.getIdentifier());
+ }
+ }
+ }
+ if (!ready) {
+ // Deleted while it started, or unreachable: no database is left running for it.
+ stop(cluster, deleted);
+ }
+ onReady.accept(ready);
+ });
}
- /** Stops the cluster's database and drops its data, then runs {@code onGone}. */
+ /**
+ * Stops the cluster's database and drops its data, then runs {@code onGone}. When Docker cannot
+ * remove them, the cluster stays as it is, and the next start of Floci finishes its deletion.
+ */
public void deprovision(DsqlCluster cluster, Runnable onGone) {
- onGone.run();
+ if (mock) {
+ onGone.run();
+ return;
+ }
+ synchronized (routes) {
+ wanted.remove(cluster.getArn());
+ running.remove(cluster.getArn());
+ endpoint.unregister(cluster);
+ }
+ Thread.ofVirtual().name("dsql-stop-" + cluster.getIdentifier()).start(() -> {
+ if (stop(cluster, true)) {
+ onGone.run();
+ }
+ });
+ }
+
+ private boolean stop(DsqlCluster cluster, boolean dropData) {
+ try {
+ databases.stop(cluster, dropData);
+ return true;
+ } catch (RuntimeException e) {
+ LOG.warnv(e, "The database of DSQL cluster {0} could not be removed", cluster.getIdentifier());
+ return false;
+ }
+ }
+
+ /**
+ * Stops every database when Floci stops. Their data stays for the next start, unless storage is
+ * in memory, where the clusters themselves do not survive.
+ */
+ void onStop(@Observes ShutdownEvent ignored) {
+ if (mock) {
+ return;
+ }
+ endpoint.stop();
+ for (DsqlCluster cluster : running.values()) {
+ stop(cluster, memoryStorage);
+ }
+ running.clear();
}
}
diff --git a/src/main/java/io/github/hectorvent/floci/services/dsql/DsqlService.java b/src/main/java/io/github/hectorvent/floci/services/dsql/DsqlService.java
index be14e610ce..7eaa68cac9 100644
--- a/src/main/java/io/github/hectorvent/floci/services/dsql/DsqlService.java
+++ b/src/main/java/io/github/hectorvent/floci/services/dsql/DsqlService.java
@@ -16,7 +16,9 @@ import io.github.hectorvent.floci.services.iam.IamPolicyEvaluator;
import io.github.hectorvent.floci.services.iam.IamPolicyEvaluator.ResourcePolicyDecision;
import io.github.hectorvent.floci.services.iam.IamService.CallerArns;
import io.github.hectorvent.floci.services.iam.RequestPrincipal;
+import io.quarkus.runtime.StartupEvent;
import jakarta.enterprise.context.ApplicationScoped;
+import jakarta.enterprise.event.Observes;
import jakarta.inject.Inject;
import org.jboss.logging.Logger;
@@ -30,7 +32,6 @@ import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
-import java.util.Locale;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
@@ -72,6 +73,7 @@ public class DsqlService implements Resettable {
private final DsqlPrincipals principals;
private final IamPolicyEvaluator policyEvaluator;
private final ObjectMapper objectMapper;
+ private final boolean enabled;
private final Map<String, Object> locks = new ConcurrentHashMap<>();
@Inject
@@ -79,12 +81,13 @@ public class DsqlService implements Resettable {
DsqlDataPlane dataPlane, DsqlPrincipals principals, IamPolicyEvaluator policyEvaluator,
ObjectMapper objectMapper) {
this(storageFactory.create("dsql", "dsql-clusters.json", new TypeReference<>() {}), regionResolver,
- config.services().dsql().endpointSuffix(), dataPlane, principals, policyEvaluator, objectMapper);
+ config.services().dsql().endpointSuffix(), dataPlane, principals, policyEvaluator, objectMapper,
+ config.services().dsql().enabled());
}
DsqlService(AccountAwareStorageBackend<DsqlCluster> clusters, RegionResolver regionResolver,
String endpointSuffix, DsqlDataPlane dataPlane, DsqlPrincipals principals,
- IamPolicyEvaluator policyEvaluator, ObjectMapper objectMapper) {
+ IamPolicyEvaluator policyEvaluator, ObjectMapper objectMapper, boolean enabled) {
this.clusters = clusters;
this.regionResolver = regionResolver;
this.endpointSuffix = endpointSuffix;
@@ -92,6 +95,7 @@ public class DsqlService implements Resettable {
this.principals = principals;
this.policyEvaluator = policyEvaluator;
this.objectMapper = objectMapper;
+ this.enabled = enabled;
}
// ── Clusters ──────────────────────────────────────────────────────────────
@@ -233,22 +237,6 @@ public class DsqlService implements Resettable {
return cluster;
}
- /** The cluster whose endpoint is {@code host}, in any account: what the data plane serves. */
- public Optional<DsqlCluster> clusterForEndpoint(String host) {
- if (host == null) {
- return Optional.empty();
- }
- String wanted = host.toLowerCase(Locale.ROOT);
- return clusters.scanAllAccounts().stream()
- .filter(cluster -> wanted.equals(cluster.getEndpoint()))
- .findFirst();
- }
-
- /** Every cluster in every account, for the data plane to bring back after a restart. */
- public List<DsqlCluster> allClusters() {
- return clusters.scanAllAccounts();
- }
-
// ── VPC endpoint service ──────────────────────────────────────────────────
public String vpcEndpointServiceName(String region, String identifier) {
@@ -378,6 +366,31 @@ public class DsqlService implements Resettable {
}
}
+ /**
+ * Brings every cluster's database back when Floci starts with clusters in its storage, and
+ * finishes a deletion that a stop interrupted.
+ */
+ void onStart(@Observes StartupEvent ignored) {
+ if (!enabled) {
+ return;
+ }
+ for (DsqlCluster cluster : clusters.scanAllAccounts()) {
+ String accountId = cluster.getAccountId();
+ String region = cluster.getRegion();
+ String identifier = cluster.getIdentifier();
+ if (DELETING.equals(cluster.getStatus())) {
+ dataPlane.deprovision(cluster, () -> {
+ clusters.deleteForAccount(accountId, key(region, identifier));
+ locks.remove(cluster.getArn());
+ });
+ } else if (!FAILED.equals(cluster.getStatus())) {
+ cluster.setDatabaseReady(false);
+ clusters.putForAccount(accountId, key(region, identifier), cluster);
+ dataPlane.provision(cluster, ready -> markReady(accountId, region, identifier, ready));
+ }
+ }
+ }
+
@Override
public void clear() {
for (DsqlCluster cluster : clusters.scanAllAccounts()) {
@@ -389,10 +402,22 @@ public class DsqlService implements Resettable {
// ── Internals ─────────────────────────────────────────────────────────────
+ /**
+ * Records whether the cluster's database came up. A cluster being created becomes ACTIVE or
+ * FAILED; one still waiting for its peers stays PENDING_SETUP until peering, unless its database
+ * failed.
+ */
private void markReady(String accountId, String region, String identifier, boolean ready) {
- clusters.getForAccount(accountId, key(region, identifier)).ifPresent(cluster -> {
- if (CREATING.equals(cluster.getStatus())) {
- cluster.setStatus(ready ? ACTIVE : FAILED);
+ clusters.getForAccount(accountId, key(region, identifier)).ifPresent(found -> {
+ synchronized (lockFor(found.getArn())) {
+ DsqlCluster cluster = clusters.getForAccount(accountId, key(region, identifier)).orElse(null);
+ if (cluster == null) {
+ return;
+ }
+ cluster.setDatabaseReady(ready);
+ if (CREATING.equals(cluster.getStatus()) || (!ready && PENDING_SETUP.equals(cluster.getStatus()))) {
+ cluster.setStatus(ready ? ACTIVE : FAILED);
+ }
clusters.putForAccount(accountId, key(region, identifier), cluster);
}
});
@@ -416,8 +441,9 @@ public class DsqlService implements Resettable {
/**
* Brings out of {@code PENDING_SETUP} every cluster of {@code cluster}'s group once each names
- * every other, as DSQL does when the last peer is updated (measured: both ACTIVE after about
- * 100 s; here at once).
+ * every other, as DSQL does when the last peer is updated (measured: CREATING, then both ACTIVE
+ * after about 100 s; here ACTIVE at once if the cluster's database answers, else CREATING until
+ * it does).
*/
private void completePeering(DsqlCluster cluster) {
if (cluster.getLinkedClusterArns() == null) {
@@ -439,7 +465,7 @@ public class DsqlService implements Resettable {
for (DsqlCluster member : group) {
synchronized (lockFor(member.getArn())) {
if (PENDING_SETUP.equals(member.getStatus())) {
- member.setStatus(ACTIVE);
+ member.setStatus(member.isDatabaseReady() ? ACTIVE : CREATING);
clusters.put(key(member.getRegion(), member.getIdentifier()), member);
}
}
diff --git a/src/main/java/io/github/hectorvent/floci/services/dsql/dataplane/DsqlDatabases.java b/src/main/java/io/github/hectorvent/floci/services/dsql/dataplane/DsqlDatabases.java
new file mode 100644
index 0000000000..091cae4a3e
--- /dev/null
+++ b/src/main/java/io/github/hectorvent/floci/services/dsql/dataplane/DsqlDatabases.java
@@ -0,0 +1,168 @@
+package io.github.hectorvent.floci.services.dsql.dataplane;
+
+import io.github.hectorvent.floci.config.EmulatorConfig;
+import io.github.hectorvent.floci.core.common.docker.ContainerBuilder;
+import io.github.hectorvent.floci.core.common.docker.ContainerDetector;
+import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager;
+import io.github.hectorvent.floci.core.common.docker.ContainerSpec;
+import io.github.hectorvent.floci.core.common.docker.ContainerStorageHelper;
+import io.github.hectorvent.floci.services.dsql.model.DsqlCluster;
+import jakarta.enterprise.context.ApplicationScoped;
+import jakarta.inject.Inject;
+import org.jboss.logging.Logger;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.nio.charset.StandardCharsets;
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.time.Duration;
+import java.time.Instant;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * The PostgreSQL 16 container behind each cluster, set up as DSQL's database is. The data is
+ * kept as RDS keeps an instance's (a Docker volume of its own, or a directory under the host
+ * persistent path), so a cluster survives a restart of Floci when its storage does, and is removed
+ * with the cluster under the same policy. The database trusts its connections, as ElastiCache's
+ * backends do behind their proxy: its port is published on the loopback address only, or, when
+ * Floci runs in Docker, reached on Floci's Docker network without being published. Clients reach
+ * it through Floci's endpoint, which authenticates every one with its IAM token.
+ */
+@ApplicationScoped
+public class DsqlDatabases {
+
+ private static final Logger LOG = Logger.getLogger(DsqlDatabases.class);
+ private static final int PORT = 5432;
+ private static final Duration READY_TIMEOUT = Duration.ofSeconds(90);
+ private static final String DATA_PATH = "/var/lib/postgresql/data";
+
+ private final ContainerBuilder containerBuilder;
+ private final ContainerLifecycleManager lifecycleManager;
+ private final ContainerDetector containerDetector;
+ private final EmulatorConfig config;
+
+ /** Where a cluster's database answers. */
+ public record Backend(String host, int port) {
+ public String jdbcUrl() {
+ return "jdbc:postgresql://" + host + ":" + port + "/postgres";
+ }
+ }
+
+ @Inject
+ public DsqlDatabases(ContainerBuilder containerBuilder, ContainerLifecycleManager lifecycleManager,
+ ContainerDetector containerDetector, EmulatorConfig config) {
+ this.containerBuilder = containerBuilder;
+ this.lifecycleManager = lifecycleManager;
+ this.containerDetector = containerDetector;
+ this.config = config;
+ }
+
+ /** Starts the cluster's database, waits until it answers, and sets it up as DSQL's database is. */
+ public Backend start(DsqlCluster cluster) throws SQLException, IOException {
+ String name = containerName(cluster);
+ Map<String, String> labels = ContainerStorageHelper.resourceIdentityLabels(
+ "dsql", cluster.getIdentifier(), cluster.getAccountId(), cluster.getRegion());
+ lifecycleManager.removeIfExistsStrict(name);
+ ContainerBuilder.Builder spec = containerBuilder.newContainer(config.services().dsql().image())
+ .withName(name)
+ .withEnv(List.of("POSTGRES_USER=dbowner", "POSTGRES_DB=postgres", "POSTGRES_HOST_AUTH_METHOD=trust"))
+ .withDockerNetwork(config.services().dsql().dockerNetwork())
+ .withLogRotation()
+ .withLabels(labels);
+ if (ContainerStorageHelper.isNamedVolumeMode(config)) {
+ lifecycleManager.ensureVolume(name, labels);
+ spec.withNamedVolume(name, DATA_PATH);
+ } else {
+ String hostPath = ContainerStorageHelper.hostResourcePath(config, "dsql", cluster.getIdentifier()).toString();
+ if (!containerDetector.isRunningInContainer()) {
+ ContainerStorageHelper.ensureHostDir(hostPath);
+ }
+ spec.withBind(hostPath, DATA_PATH);
+ }
+ if (containerDetector.isRunningInContainer()) {
+ spec.withExposedPort(PORT);
+ } else {
+ spec.withLoopbackPortBinding(PORT, 0);
+ }
+ ContainerSpec built = spec.build();
+ String containerId = lifecycleManager.create(built);
+ try {
+ ContainerLifecycleManager.EndpointInfo endpoint =
+ lifecycleManager.startCreated(containerId, built).getEndpoint(PORT);
+ Backend backend = new Backend(endpoint.host(), endpoint.port());
+ awaitReady(backend);
+ bootstrap(backend);
+ LOG.infov("DSQL cluster {0} serves from {1}:{2}", cluster.getIdentifier(), backend.host(),
+ String.valueOf(backend.port()));
+ return backend;
+ } catch (SQLException | IOException | RuntimeException e) {
+ // A database that never served does not keep running: its cluster is FAILED, and deleting
+ // the cluster removes its data.
+ try {
+ lifecycleManager.removeIfExistsStrict(name);
+ } catch (RuntimeException removal) {
+ e.addSuppressed(removal);
+ }
+ throw e;
+ }
+ }
+
+ /**
+ * Stops the cluster's database and, with {@code dropData}, removes its volume as Floci removes
+ * a deleted resource's: always with in-memory storage, otherwise when
+ * {@code prune-volumes-on-delete} is on. A host directory is left in place, as RDS leaves it.
+ * Throws when Docker cannot remove either, so a deletion is not taken for done.
+ */
+ public void stop(DsqlCluster cluster, boolean dropData) {
+ String name = containerName(cluster);
+ lifecycleManager.removeIfExistsStrict(name);
+ if (dropData && ContainerStorageHelper.isNamedVolumeMode(config)) {
+ ContainerStorageHelper.removeNamedVolumeStrict(config, lifecycleManager, name);
+ }
+ }
+
+ private static Connection superuser(Backend backend) throws SQLException {
+ return DriverManager.getConnection(backend.jdbcUrl(), "dbowner", "");
+ }
+
+ private String containerName(DsqlCluster cluster) {
+ return ContainerStorageHelper.dockerName(config, "dsql-" + cluster.getIdentifier());
+ }
+
+ private static void awaitReady(Backend backend) throws SQLException {
+ Instant deadline = Instant.now().plus(READY_TIMEOUT);
+ SQLException last = null;
+ while (Instant.now().isBefore(deadline)) {
+ try (Connection connection = superuser(backend); Statement statement = connection.createStatement()) {
+ statement.execute("SELECT 1");
+ return;
+ } catch (SQLException e) {
+ last = e;
+ try {
+ Thread.sleep(250);
+ } catch (InterruptedException interrupted) {
+ Thread.currentThread().interrupt();
+ throw new SQLException("Interrupted while the database started", interrupted);
+ }
+ }
+ }
+ throw new SQLException("The database did not start within " + READY_TIMEOUT.toSeconds() + " s", last);
+ }
+
+ private static void bootstrap(Backend backend) throws SQLException, IOException {
+ String script;
+ try (InputStream in = DsqlDatabases.class.getResourceAsStream("/dsql/bootstrap.sql")) {
+ if (in == null) {
+ throw new IOException("dsql/bootstrap.sql is missing from the classpath");
+ }
+ script = new String(in.readAllBytes(), StandardCharsets.UTF_8);
+ }
+ try (Connection connection = superuser(backend); Statement statement = connection.createStatement()) {
+ statement.execute(script);
+ }
+ }
+}
diff --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.java
new file mode 100644
index 0000000000..f243d38d00
--- /dev/null
+++ b/src/main/java/io/github/hectorvent/floci/services/dsql/dataplane/DsqlEndpoint.java
@@ -0,0 +1,353 @@
+package io.github.hectorvent.floci.services.dsql.dataplane;
+
+import io.github.hectorvent.floci.config.EmulatorConfig;
+import io.github.hectorvent.floci.core.common.PostgresWire;
+import io.github.hectorvent.floci.core.common.RequestScopes;
+import io.github.hectorvent.floci.core.common.auth.SigV4RequestValidator;
+import io.github.hectorvent.floci.services.dsql.model.DsqlCluster;
+import io.github.hectorvent.floci.services.iam.IamPolicyEvaluator;
+import io.github.hectorvent.floci.services.iam.IamService;
+import io.github.hectorvent.floci.services.iam.ScpProvider;
+import io.github.hectorvent.floci.services.iam.model.CallerContext;
+import jakarta.annotation.PreDestroy;
+import jakarta.enterprise.context.ApplicationScoped;
+import jakarta.enterprise.inject.Instance;
+import jakarta.inject.Inject;
+import org.jboss.logging.Logger;
+
+import javax.net.ssl.SSLContext;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.net.InetSocketAddress;
+import java.net.ServerSocket;
+import java.net.Socket;
+import java.net.URI;
+import java.nio.ByteBuffer;
+import java.time.Duration;
+import java.time.Instant;
+import java.time.ZoneOffset;
+import java.time.format.DateTimeFormatter;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+
+/**
+ * The address every DSQL cluster is reached at: one port, as on AWS, where the server name a client
+ * sends picks the cluster. A connection is admitted as Aurora DSQL admits it (measured on AWS,
+ * eu-west-1, 2026-10-16): the password is a SigV4 token for the endpoint's host, {@code admin}
+ * signs {@code DbConnectAdmin} and every other role {@code DbConnect}, a token is taken up to 5
+ * seconds past its expiry, and a role other than {@code admin} logs in only for an IAM role mapped
+ * to it, which Floci does not map yet. Every refusal is the same {@code access denied}, with a hint
+ * only where DSQL gives one. DSQL lets clients in while a cluster is still CREATING; Floci's
+ * clusters are CREATING only while their database starts, and serve once it answers.
+ */
+@ApplicationScoped
+public class DsqlEndpoint {
+
+ private static final Logger LOG = Logger.getLogger(DsqlEndpoint.class);
+ private static final DateTimeFormatter AMZ_DATE =
+ DateTimeFormatter.ofPattern("yyyyMMdd'T'HHmmss'Z'").withZone(ZoneOffset.UTC);
+ static final Duration EXPIRY_GRACE = Duration.ofSeconds(5);
+ private static final int HANDSHAKE_TIMEOUT_MILLIS = 30_000;
+ private static final String ADMIN = "admin";
+
+ private final DsqlTlsCertificates certificates;
+ private final IamService iamService;
+ private final IamPolicyEvaluator policyEvaluator;
+ private final Instance<ScpProvider> scpProvider;
+ private final SigV4RequestValidator validator;
+ private final boolean enforcement;
+ private final int configuredPort;
+ private final Map<String, Route> routes = new ConcurrentHashMap<>();
+ private final Map<Long, DsqlDatabases.Backend> cancelKeys = new ConcurrentHashMap<>();
+ private final Map<String, Set<Socket>> connections = new ConcurrentHashMap<>();
+ private volatile ServerSocket server;
+
+ /** A cluster being served, and where its database answers. */
+ record Route(String host, String arn, String region, String accountId, DsqlDatabases.Backend backend) {
+ }
+
+ /**
+ * Whether a connection may go on, and to which cluster; when it may not, the SQLSTATE DSQL
+ * answers ({@code 08006} for the token, {@code 28000} for the role it would log in as) and its
+ * hint.
+ */
+ record Admission(boolean allowed, String state, String hint, Route route) {
+
+ static Admission refused(String hint) {
+ return new Admission(false, "08006", hint, null);
+ }
+
+ static Admission unauthorized() {
+ return new Admission(false, "28000", null, null);
+ }
+ }
+
+ @Inject
+ public DsqlEndpoint(DsqlTlsCertificates certificates, IamService iamService, IamPolicyEvaluator policyEvaluator,
+ Instance<ScpProvider> scpProvider, EmulatorConfig config) {
+ this.certificates = certificates;
+ this.iamService = iamService;
+ this.policyEvaluator = policyEvaluator;
+ this.scpProvider = scpProvider;
+ this.validator = new SigV4RequestValidator(iamService);
+ this.enforcement = config.services().iam().enforcementEnabled();
+ this.configuredPort = config.services().dsql().port();
+ }
+
+ /** Serves {@code cluster} from {@code backend}, opening the endpoint's port on first use. */
+ public void register(DsqlCluster cluster, DsqlDatabases.Backend backend) throws IOException {
+ listen();
+ String host = cluster.getEndpoint().toLowerCase(Locale.ROOT);
+ routes.put(host, new Route(host, cluster.getArn(), cluster.getRegion(), cluster.getAccountId(), backend));
+ }
+
+ /** Stops serving {@code cluster} and closes its connections, as deleting a cluster does. */
+ public void unregister(DsqlCluster cluster) {
+ String host = cluster.getEndpoint().toLowerCase(Locale.ROOT);
+ routes.remove(host);
+ Set<Socket> open = connections.remove(host);
+ if (open != null) {
+ open.forEach(DsqlEndpoint::closeQuietly);
+ }
+ }
+
+ /** The port the endpoint listens on, once it does: the configured one, or the one picked for 0. */
+ public int port() {
+ ServerSocket listening = server;
+ return listening == null ? configuredPort : listening.getLocalPort();
+ }
+
+ @PreDestroy
+ public synchronized void stop() {
+ ServerSocket listening = server;
+ server = null;
+ if (listening != null) {
+ closeQuietly(listening);
+ }
+ connections.values().forEach(open -> open.forEach(DsqlEndpoint::closeQuietly));
+ connections.clear();
+ routes.clear();
+ }
+
+ private synchronized void listen() throws IOException {
+ if (server != null) {
+ return;
+ }
+ ServerSocket listening = new ServerSocket();
+ listening.setReuseAddress(true);
+ listening.bind(new InetSocketAddress(configuredPort));
+ server = listening;
+ Thread.ofVirtual().name("dsql-endpoint-accept").start(() -> accept(listening));
+ LOG.infov("DSQL endpoint listening on port {0}", String.valueOf(listening.getLocalPort()));
+ }
+
+ private void accept(ServerSocket listening) {
+ while (!listening.isClosed()) {
+ try {
+ Socket client = listening.accept();
+ Thread.ofVirtual().name("dsql-endpoint-connection").start(() -> {
+ try {
+ new DsqlSession(client, this).serve();
+ } catch (IOException | RuntimeException e) {
+ LOG.debugv("DSQL connection ended: {0}", e.getMessage());
+ } finally {
+ closeQuietly(client);
+ }
+ });
+ } catch (IOException e) {
+ if (!listening.isClosed()) {
+ LOG.warnv("DSQL endpoint accept failed: {0}", e.getMessage());
+ }
+ }
+ }
+ }
+
+ SSLContext sslContext() {
+ return certificates.sslContext();
+ }
+
+ int handshakeTimeoutMillis() {
+ return HANDSHAKE_TIMEOUT_MILLIS;
+ }
+
+ /**
+ * 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: DSQL refuses a role no IAM role is mapped to, and
+ * Floci maps none yet, so only {@code admin} signs in.
+ */
+ Admission admit(String token, String user, String serverName) {
+ URI uri;
+ try {
+ uri = URI.create("https://" + token);
+ } catch (IllegalArgumentException ignored) {
+ // A password that is no token is refused without a hint, as DSQL refuses one.
+ return Admission.refused(null);
+ }
+ String host = uri.getHost();
+ String rawQuery = uri.getRawQuery();
+ if (host == null || rawQuery == null || uri.getPort() != -1) {
+ return Admission.refused(null);
+ }
+ host = host.toLowerCase(Locale.ROOT);
+ String target = serverName == null ? host : serverName.toLowerCase(Locale.ROOT);
+ Route route = routes.get(target);
+ if (route == null || !route.host().equals(host)) {
+ return Admission.refused(null);
+ }
+
+ String action = SigV4RequestValidator.queryParameter(rawQuery, "Action");
+ String needed = ADMIN.equals(user) ? "DbConnectAdmin" : "DbConnect";
+ if (!needed.equals(action)) {
+ boolean dsqlAction = "DbConnectAdmin".equals(action) || "DbConnect".equals(action);
+ return Admission.refused(dsqlAction
+ ? "Wrong user to action mapping. user: " + user + ", action: " + action : null);
+ }
+
+ String expiredHint = expiredHint(rawQuery);
+ if (expiredHint != null) {
+ return Admission.refused(expiredHint);
+ }
+
+ String[] scope = credentialScope(rawQuery);
+ if (scope == null || !route.region().equals(scope[2]) || !"dsql".equals(scope[3])) {
+ return Admission.refused(null);
+ }
+ // The endpoint checks tokens outside any request, so the key is looked up in the account
+ // that owns the cluster, as ElastiCache's proxy does: a long-term key of another account
+ // never signs in, and one of the cluster's own account does whichever account is Floci's default.
+ if (!validator.validate(rawQuery, host, needed, "DBUser", false, null, "DSQL IAM token", route.accountId(),
+ EXPIRY_GRACE)) {
+ return Admission.refused(null);
+ }
+
+ String accessKeyId = scope[0];
+ // DSQL takes no cross-account access yet (its resource-based policy guide), so credentials of
+ // another account are refused like any other token, a session's included.
+ if (!route.accountId().equals(iamService.resolveAccountId(accessKeyId).orElse(route.accountId()))) {
+ return Admission.refused(null);
+ }
+ // The endpoint runs outside any request: the caller's policies are read in the cluster's
+ // account, as the enforcement filter reads them in the request's.
+ if (enforcement && !RequestScopes.callAs(route.accountId(), route.region(),
+ () -> allowedByIam(accessKeyId, "dsql:" + needed, route))) {
+ return Admission.refused(null);
+ }
+ if (!ADMIN.equals(user)) {
+ return Admission.unauthorized();
+ }
+ return new Admission(true, null, null, route);
+ }
+
+ /**
+ * DSQL's hint for a token past its expiry and the 5 seconds of grace after it:
+ * {@code Signature expired: <expiry> is now earlier than <now - 5 s> (<now> - 0 min.)}.
+ */
+ private static String expiredHint(String rawQuery) {
+ String date = SigV4RequestValidator.queryParameter(rawQuery, "X-Amz-Date");
+ String expires = SigV4RequestValidator.queryParameter(rawQuery, "X-Amz-Expires");
+ if (date == null || expires == null) {
+ return null;
+ }
+ Instant expiry;
+ try {
+ expiry = Instant.from(AMZ_DATE.parse(date)).plusSeconds(Long.parseLong(expires));
+ } catch (RuntimeException ignored) {
+ // A date or expiry that does not parse gets no hint here: the signature check that
+ // follows refuses the token without one, as DSQL does.
+ return null;
+ }
+ Instant now = Instant.now();
+ Instant earliest = now.minus(EXPIRY_GRACE);
+ if (!expiry.isBefore(earliest)) {
+ return null;
+ }
+ return "Signature expired: " + AMZ_DATE.format(expiry) + " is now earlier than " + AMZ_DATE.format(earliest)
+ + " (" + AMZ_DATE.format(now) + " - 0 min.)";
+ }
+
+ /**
+ * Whether the caller's policies let it {@code action} the cluster, under its organization's
+ * service control policies as the enforcement filter applies them. A known credential with no
+ * policies to evaluate is let through, as the filter lets it.
+ */
+ private boolean allowedByIam(String accessKeyId, String action, Route route) {
+ CallerContext caller = iamService.resolveCallerContext(accessKeyId);
+ if (caller == null) {
+ return true;
+ }
+ List<List<String>> scpLevels = scpProvider.isResolvable()
+ ? scpProvider.get().effectiveScpLevels(route.accountId()) : null;
+ if (scpLevels != null) {
+ caller = caller.withScpLevels(scpLevels);
+ }
+ return policyEvaluator.simulatePrincipalPolicy(caller, action, route.arn(), Map.of())
+ == IamPolicyEvaluator.SimulationDecision.ALLOWED;
+ }
+
+ private static String[] credentialScope(String rawQuery) {
+ String credential = SigV4RequestValidator.queryParameter(rawQuery, "X-Amz-Credential");
+ if (credential == null) {
+ return null;
+ }
+ String[] parts = credential.split("/");
+ return parts.length >= 5 ? parts : null;
+ }
+
+ void opened(Route route, Socket socket) {
+ connections.computeIfAbsent(route.host(), host -> ConcurrentHashMap.newKeySet()).add(socket);
+ if (!routes.containsKey(route.host())) {
+ closeQuietly(socket);
+ }
+ }
+
+ void closed(Route route, Socket socket) {
+ Set<Socket> open = connections.get(route.host());
+ if (open != null) {
+ open.remove(socket);
+ }
+ }
+
+ /** Remembers which database a BackendKeyData came from, so a CancelRequest can reach it. */
+ long registerCancelKey(byte[] keyData, DsqlDatabases.Backend backend) {
+ long key = ByteBuffer.wrap(keyData).getLong(0);
+ cancelKeys.put(key, backend);
+ return key;
+ }
+
+ void forgetCancelKey(long key) {
+ cancelKeys.remove(key);
+ }
+
+ /** Hands a CancelRequest's process id and secret to the database that issued them. */
+ void forwardCancel(byte[] keyData) {
+ if (keyData.length < 8) {
+ return;
+ }
+ DsqlDatabases.Backend backend = cancelKeys.get(ByteBuffer.wrap(keyData).getLong(0));
+ if (backend == null) {
+ return;
+ }
+ try (Socket socket = new Socket()) {
+ socket.connect(new InetSocketAddress(backend.host(), backend.port()), 5_000);
+ OutputStream out = socket.getOutputStream();
+ out.write(ByteBuffer.allocate(16).putInt(16).putInt(PostgresWire.CANCEL_REQUEST)
+ .put(keyData, 0, 8).array());
+ out.flush();
+ } catch (IOException e) {
+ LOG.debugv("Could not forward a DSQL cancel request: {0}", e.getMessage());
+ }
+ }
+
+ private static void closeQuietly(AutoCloseable closing) {
+ try {
+ closing.close();
+ } catch (Exception ignored) {
+ // Already closed.
+ }
+ }
+}
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
new file mode 100644
index 0000000000..6189afbe36
--- /dev/null
+++ b/src/main/java/io/github/hectorvent/floci/services/dsql/dataplane/DsqlSession.java
@@ -0,0 +1,303 @@
+package io.github.hectorvent.floci.services.dsql.dataplane;
+
+import io.github.hectorvent.floci.core.common.PostgresWire;
+import io.github.hectorvent.floci.services.dsql.DsqlIdentifiers;
+
+import javax.net.ssl.SNIHostName;
+import javax.net.ssl.SNIMatcher;
+import javax.net.ssl.SNIServerName;
+import javax.net.ssl.SSLParameters;
+import javax.net.ssl.SSLSocket;
+import javax.net.ssl.StandardConstants;
+import java.io.BufferedInputStream;
+import java.io.BufferedOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.net.InetSocketAddress;
+import java.net.Socket;
+import java.time.Duration;
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+import java.util.Set;
+import java.util.stream.Stream;
+
+/**
+ * One client connection to a DSQL endpoint, as Aurora DSQL serves it (measured on AWS, eu-west-1,
+ * 2026-10-16): TLS is mandatory, the password is an IAM token for the endpoint, the startup is read
+ * as DSQL reads it, and a connection lives an hour.
+ */
+final class DsqlSession {
+
+ static final Duration CONNECTION_LIFETIME = Duration.ofHours(1);
+ // A startup packet is refused above PostgreSQL's own limit, 10,000 bytes, and a message above 64 MB.
+ private static final int MAX_STARTUP = 10_000;
+ private static final int MAX_MESSAGE = 64 * 1024 * 1024;
+ private static final String ACCESS_DENIED = "unable to accept connection, access denied";
+
+ private final Socket socket;
+ private final DsqlEndpoint endpoint;
+ private final String sessionId = DsqlIdentifiers.newId();
+ private volatile long cancelKey;
+
+ DsqlSession(Socket socket, DsqlEndpoint endpoint) {
+ this.socket = socket;
+ this.endpoint = endpoint;
+ }
+
+ void serve() throws IOException {
+ socket.setTcpNoDelay(true);
+ socket.setSoTimeout(endpoint.handshakeTimeoutMillis());
+ InputStream rawIn = socket.getInputStream();
+ OutputStream rawOut = socket.getOutputStream();
+ PostgresWire.Startup first = PostgresWire.readStartup(rawIn, MAX_STARTUP, "startup message");
+ if (first.code() == PostgresWire.CANCEL_REQUEST) {
+ endpoint.forwardCancel(first.body());
+ return;
+ }
+ if (first.code() == PostgresWire.GSSENC_REQUEST) {
+ rawOut.write('N');
+ rawOut.flush();
+ first = PostgresWire.readStartup(rawIn, MAX_STARTUP, "startup message");
+ }
+ 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);
+ SSLParameters parameters = tls.getSSLParameters();
+ parameters.setSNIMatchers(List.of(new SNIMatcher(StandardConstants.SNI_HOST_NAME) {
+ @Override
+ public boolean matches(SNIServerName name) {
+ if (name instanceof SNIHostName host) {
+ serverName[0] = host.getAsciiName().toLowerCase(Locale.ROOT);
+ }
+ return true;
+ }
+ }));
+ tls.setSSLParameters(parameters);
+ tls.setUseClientMode(false);
+ tls.startHandshake();
+
+ InputStream in = new BufferedInputStream(tls.getInputStream());
+ OutputStream out = new BufferedOutputStream(tls.getOutputStream());
+ PostgresWire.Startup startup = PostgresWire.readStartup(in, MAX_STARTUP, "startup message");
+ if (startup.code() == PostgresWire.CANCEL_REQUEST) {
+ endpoint.forwardCancel(startup.body());
+ return;
+ }
+ if (startup.code() != PostgresWire.PROTOCOL_3) {
+ refuse(out, "08P01", "unsupported frontend protocol", null);
+ return;
+ }
+ Map<String, String> startupParameters = PostgresWire.startupParameters(startup.body());
+ String user = startupParameters.getOrDefault("user", "");
+ String database = startupParameters.getOrDefault("database", user);
+
+ PostgresWire.write(out, (byte) 'R', PostgresWire.int32(3));
+ out.flush();
+ PostgresWire.Message password = PostgresWire.read(in, MAX_MESSAGE, "message");
+ if (password.type() != 'p') {
+ refuse(out, "08006", ACCESS_DENIED, null);
+ return;
+ }
+ String token = PostgresWire.cString(password.body(), 0).value();
+ DsqlEndpoint.Admission admission = endpoint.admit(token, user, serverName[0]);
+ if (!admission.allowed()) {
+ refuse(out, admission.state(), ACCESS_DENIED, admission.hint());
+ return;
+ }
+ if (!"postgres".equals(database)) {
+ refuse(out, "55000", "database \"" + database + "\" is not currently accepting connections", null);
+ return;
+ }
+ Map<String, String> optionSettings = optionSettings(startupParameters.get("options"));
+ String custom = customParameter(optionSettings, startupParameters);
+ if (custom != null) {
+ refuse(out, "42704", "unrecognized configuration parameter \"" + custom + "\"", null);
+ return;
+ }
+
+ DsqlDatabases.Backend target = admission.route().backend();
+ endpoint.opened(admission.route(), tls);
+ Socket backend = new Socket();
+ try (backend) {
+ backend.connect(new InetSocketAddress(target.host(), target.port()), 5_000);
+ backend.setTcpNoDelay(true);
+ socket.setSoTimeout(0);
+ InputStream backendIn = new BufferedInputStream(backend.getInputStream());
+ OutputStream backendOut = new BufferedOutputStream(backend.getOutputStream());
+ Map<String, String> backendStartup = new LinkedHashMap<>(startupParameters);
+ backendStartup.remove("options");
+ if (!optionSettings.isEmpty()) {
+ backendStartup.put("options", backendOptions(optionSettings));
+ }
+ backendStartup.put("database", "postgres");
+ backendOut.write(PostgresWire.startupMessage(backendStartup));
+ backendOut.flush();
+
+ Thread toClient = Thread.ofVirtual().name("dsql-backend-" + sessionId)
+ .start(() -> relayToClient(backendIn, out, target, tls));
+ Thread lifetime = Thread.ofVirtual().name("dsql-lifetime-" + sessionId).start(() -> {
+ try {
+ Thread.sleep(CONNECTION_LIFETIME.toMillis());
+ closeQuietly(tls);
+ closeQuietly(backend);
+ } catch (InterruptedException ignored) {
+ // The connection ended first.
+ }
+ });
+ try {
+ relayToBackend(in, backendOut);
+ } finally {
+ lifetime.interrupt();
+ closeQuietly(backend);
+ toClient.interrupt();
+ }
+ } finally {
+ endpoint.closed(admission.route(), tls);
+ if (cancelKey != 0) {
+ endpoint.forgetCancelKey(cancelKey);
+ }
+ }
+ }
+
+ private static void relayToBackend(InputStream in, OutputStream backendOut) throws IOException {
+ while (true) {
+ PostgresWire.Message message = PostgresWire.read(in, MAX_MESSAGE, "message");
+ PostgresWire.write(backendOut, message);
+ if (message.type() == 'X') {
+ backendOut.flush();
+ return;
+ }
+ if (in.available() == 0) {
+ backendOut.flush();
+ }
+ }
+ }
+
+ /**
+ * The settings a client's startup {@code options} make, as DSQL reads them (measured, eu-west-1,
+ * 2026-10-20): words split on white space, with no escapes, of which only {@code -c name=value}
+ * counts, the first for a name winning. DSQL ignores every other word, {@code --name=value}
+ * included, so none of them may reach the database.
+ */
+ static Map<String, String> optionSettings(String options) {
+ Map<String, String> settings = new LinkedHashMap<>();
+ Set<String> seen = new HashSet<>();
+ String[] words = options == null ? new String[0] : options.strip().split("\\s+");
+ for (int i = 0; i + 1 < words.length; i++) {
+ if ("-c".equals(words[i])) {
+ String setting = words[++i];
+ int equals = setting.indexOf('=');
+ if (equals > 0 && seen.add(setting.substring(0, equals).toLowerCase(Locale.ROOT))) {
+ settings.put(setting.substring(0, equals), setting.substring(equals + 1));
+ }
+ }
+ }
+ return settings;
+ }
+
+ /**
+ * The first custom (dotted) parameter a startup sets, in its {@code options} or on its own: DSQL
+ * refuses the connection for one (measured, eu-west-1, 2026-10-20), {@code dsql.} or not.
+ */
+ static String customParameter(Map<String, String> optionSettings, Map<String, String> startupParameters) {
+ return Stream.concat(optionSettings.keySet().stream(), startupParameters.keySet().stream())
+ .filter(name -> name.contains(".")).findFirst().orElse(null);
+ }
+
+ /** {@code settings} as PostgreSQL's {@code options}, whose backslash escapes the next character. */
+ static String backendOptions(Map<String, String> settings) {
+ List<String> words = new ArrayList<>();
+ settings.forEach((name, value) -> words.add("-c " + (name + "=" + value).replace("\\", "\\\\")));
+ return String.join(" ", words);
+ }
+
+ private void relayToClient(InputStream backendIn, OutputStream out, DsqlDatabases.Backend backend, Socket client) {
+ try {
+ PostgresWire.Message authenticated = null;
+ boolean started = false;
+ while (true) {
+ PostgresWire.Message message = PostgresWire.read(backendIn, MAX_MESSAGE, "message");
+ if (!started) {
+ // DSQL refuses a connection instead of authenticating it, so the database's
+ // AuthenticationOk waits for what follows it, and a refusal takes its place.
+ if (message.type() == 'R' && authenticated == null) {
+ authenticated = message;
+ continue;
+ }
+ if (message.type() == 'E') {
+ message = new PostgresWire.Message(message.type(), withSessionId(message.body()));
+ } else if (authenticated != null) {
+ PostgresWire.write(out, authenticated);
+ }
+ authenticated = null;
+ started = message.type() == 'Z';
+ }
+ if (message.type() == 'K') {
+ cancelKey = endpoint.registerCancelKey(message.body(), backend);
+ }
+ PostgresWire.write(out, message);
+ if (backendIn.available() == 0) {
+ out.flush();
+ }
+ }
+ } catch (IOException ignored) {
+ // The database or the client ended the connection: the client's side is closed with it.
+ closeQuietly(client);
+ }
+ }
+
+ /**
+ * A refusal of the database's as DSQL words every one: the session's id as its detail, and no
+ * source location.
+ */
+ private byte[] withSessionId(byte[] body) {
+ Map<Character, String> fields = PostgresWire.parseFields(body, 0);
+ fields.remove('F');
+ fields.remove('L');
+ fields.remove('R');
+ fields.putIfAbsent('D', "Session Id: " + sessionId);
+ return PostgresWire.fields(fields);
+ }
+
+ /** An ErrorResponse body of {@code severity} (FATAL, ERROR), as DSQL orders its fields. */
+ static byte[] error(String severity, String state, String message, String detail, String hint) {
+ Map<Character, String> fields = new LinkedHashMap<>();
+ fields.put('S', severity);
+ fields.put('V', severity);
+ fields.put('C', state);
+ fields.put('M', message);
+ if (detail != null) {
+ fields.put('D', detail);
+ }
+ if (hint != null) {
+ fields.put('H', hint);
+ }
+ return PostgresWire.fields(fields);
+ }
+
+ /** A refusal as DSQL words every one: FATAL, with the session's id as its detail. */
+ private void refuse(OutputStream out, String state, String message, String hint) throws IOException {
+ PostgresWire.write(out, (byte) 'E', error("FATAL", state, message, "Session Id: " + sessionId, hint));
+ out.flush();
+ }
+
+ private static void closeQuietly(Socket closing) {
+ try {
+ closing.close();
+ } catch (IOException ignored) {
+ // Already closed.
+ }
+ }
+}
diff --git a/src/main/java/io/github/hectorvent/floci/services/dsql/dataplane/DsqlTlsCertificates.java b/src/main/java/io/github/hectorvent/floci/services/dsql/dataplane/DsqlTlsCertificates.java
new file mode 100644
index 0000000000..ac4a99013a
--- /dev/null
+++ b/src/main/java/io/github/hectorvent/floci/services/dsql/dataplane/DsqlTlsCertificates.java
@@ -0,0 +1,67 @@
+package io.github.hectorvent.floci.services.dsql.dataplane;
+
+import io.github.hectorvent.floci.config.EmulatorConfig;
+import io.github.hectorvent.floci.config.FlociCertificateAuthority;
+import io.github.hectorvent.floci.core.common.AwsRegions;
+import io.github.hectorvent.floci.core.common.Pem;
+import io.github.hectorvent.floci.services.acm.CertificateGenerator;
+import io.github.hectorvent.floci.services.acm.model.KeyAlgorithm;
+import jakarta.enterprise.context.ApplicationScoped;
+import jakarta.inject.Inject;
+
+import javax.net.ssl.SSLContext;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.TreeSet;
+
+/**
+ * The certificate every DSQL endpoint presents: one leaf, signed by Floci's root CA, naming
+ * {@code *.dsql.<region>.<endpoint-suffix>} for every Region Floci knows, so a client verifying
+ * the endpoint's name (sslmode=verify-full, as DSQL's own connectors do) only has to trust
+ * Floci's CA, the one every launched container already trusts.
+ */
+@ApplicationScoped
+public class DsqlTlsCertificates {
+
+ private final FlociCertificateAuthority certificateAuthority;
+ private final String endpointSuffix;
+ private volatile SSLContext context;
+
+ @Inject
+ public DsqlTlsCertificates(FlociCertificateAuthority certificateAuthority, EmulatorConfig config) {
+ this(certificateAuthority, config.services().dsql().endpointSuffix());
+ }
+
+ DsqlTlsCertificates(FlociCertificateAuthority certificateAuthority, String endpointSuffix) {
+ this.certificateAuthority = certificateAuthority;
+ this.endpointSuffix = endpointSuffix;
+ }
+
+ public SSLContext sslContext() {
+ SSLContext current = context;
+ if (current == null) {
+ synchronized (this) {
+ if (context == null) {
+ context = build();
+ }
+ current = context;
+ }
+ }
+ return current;
+ }
+
+ private SSLContext build() {
+ try {
+ List<String> sans = new ArrayList<>();
+ for (String region : new TreeSet<>(AwsRegions.KNOWN_IDS)) {
+ sans.add("*.dsql." + region + "." + endpointSuffix);
+ }
+ CertificateGenerator.GeneratedCertificate generated = certificateAuthority.issueServerCertificate(
+ "dsql." + endpointSuffix, sans, KeyAlgorithm.RSA_2048, null);
+ return certificateAuthority.serverSslContext(Pem.parseCertificate(generated.certificatePem()),
+ Pem.parsePrivateKey(generated.privateKeyPem()));
+ } catch (Exception e) {
+ throw new IllegalStateException("Failed to issue the DSQL endpoint certificate", e);
+ }
+ }
+}
diff --git a/src/main/java/io/github/hectorvent/floci/services/dsql/model/DsqlCluster.java b/src/main/java/io/github/hectorvent/floci/services/dsql/model/DsqlCluster.java
index 5b18d55816..3d225b1f77 100644
--- a/src/main/java/io/github/hectorvent/floci/services/dsql/model/DsqlCluster.java
+++ b/src/main/java/io/github/hectorvent/floci/services/dsql/model/DsqlCluster.java
@@ -31,6 +31,7 @@ public class DsqlCluster {
private String policyVersion;
private String witnessRegion;
private List<String> linkedClusterArns;
+ private boolean databaseReady;
public String getIdentifier() { return identifier; }
public void setIdentifier(String identifier) { this.identifier = identifier; }
@@ -66,4 +67,7 @@ public class DsqlCluster {
public void setWitnessRegion(String witnessRegion) { this.witnessRegion = witnessRegion; }
public List<String> getLinkedClusterArns() { return linkedClusterArns; }
public void setLinkedClusterArns(List<String> linkedClusterArns) { this.linkedClusterArns = linkedClusterArns; }
+ /** Whether the cluster's database answers, which a multi-Region cluster waits for after peering. */
+ public boolean isDatabaseReady() { return databaseReady; }
+ public void setDatabaseReady(boolean databaseReady) { this.databaseReady = databaseReady; }
}
diff --git a/src/main/java/io/github/hectorvent/floci/services/rds/proxy/PostgresProtocolHandler.java b/src/main/java/io/github/hectorvent/floci/services/rds/proxy/PostgresProtocolHandler.java
index ad39490e77..2a2b261bea 100644
--- a/src/main/java/io/github/hectorvent/floci/services/rds/proxy/PostgresProtocolHandler.java
+++ b/src/main/java/io/github/hectorvent/floci/services/rds/proxy/PostgresProtocolHandler.java
@@ -1,6 +1,7 @@
package io.github.hectorvent.floci.services.rds.proxy;
import io.github.hectorvent.floci.core.common.Digests;
+import io.github.hectorvent.floci.core.common.PostgresWire;
import io.github.hectorvent.floci.core.common.auth.SigV4RequestValidator;
import org.jboss.logging.Logger;
@@ -52,8 +53,6 @@ public class PostgresProtocolHandler {
private static final Logger LOG = Logger.getLogger(PostgresProtocolHandler.class);
- private static final int SSL_REQUEST_CODE = 80877103;
- private static final int STARTUP_PROTOCOL_VERSION = 196608; // v3.0
private static final int MAX_HANDSHAKE_PACKET_LENGTH = 1_048_576;
/**
@@ -154,7 +153,7 @@ public class PostgresProtocolHandler {
OutputStream clientOut = new BufferedOutputStream(client.getOutputStream());
// Phase 2: Challenge client with cleartext password request
- sendMessage(clientOut, 'R', intBytes(3)); // AuthenticationCleartextPassword
+ PostgresWire.write(clientOut, 'R', PostgresWire.int32(3)); // AuthenticationCleartextPassword
clientOut.flush();
// Phase 3: Read client password
@@ -294,7 +293,7 @@ public class PostgresProtocolHandler {
notices.addAll(defaults.warnings());
roleDefaultsAfterHandover = defaults.afterHandover();
if (!defaults.atStartup().isEmpty()) {
- sendMessage(backendOut, 'X');
+ PostgresWire.write(backendOut, 'X');
backendOut.flush();
closeQuietly(backend);
try {
@@ -455,7 +454,7 @@ public class PostgresProtocolHandler {
return null;
}
- sendMessage(clientOut, 'R', intBytes(0)); // AuthenticationOK
+ PostgresWire.write(clientOut, 'R', PostgresWire.int32(0)); // AuthenticationOK
for (byte[] msg : bufferedMessages) {
clientOut.write(msg);
}
@@ -478,10 +477,9 @@ public class PostgresProtocolHandler {
while (true) {
InputStream in = currentSocket.getInputStream();
OutputStream out = currentSocket.getOutputStream();
- int length = checkedPacketLength(readInt32(in), 8, "startup message");
- int proto = readInt32(in);
+ PostgresWire.Startup startup = PostgresWire.readStartup(in, MAX_HANDSHAKE_PACKET_LENGTH, "startup message");
- if (proto == SSL_REQUEST_CODE) {
+ if (startup.code() == PostgresWire.SSL_REQUEST) {
out.write('S');
out.flush();
currentSocket = acceptSsl(currentSocket, tlsCertificates);
@@ -491,14 +489,12 @@ public class PostgresProtocolHandler {
continue;
}
- if (proto != STARTUP_PROTOCOL_VERSION) {
- LOG.warnv("Unexpected PostgreSQL startup protocol version: {0}", proto);
+ if (startup.code() != PostgresWire.PROTOCOL_3) {
+ LOG.warnv("Unexpected PostgreSQL startup protocol version: {0}", startup.code());
return null;
}
- byte[] payload = new byte[length - 8];
- readFully(in, payload);
- Map<String, String> params = parseStartupParams(payload);
+ Map<String, String> params = PostgresWire.startupParameters(startup.body());
return new StartupMessage(currentSocket,
params.getOrDefault("user", "postgres"),
params.get("database"),
@@ -535,33 +531,6 @@ public class PostgresProtocolHandler {
return !messages.isEmpty() && messages.get(messages.size() - 1)[0] == 'E';
}
- static Map<String, String> parseStartupParams(byte[] data) {
- Map<String, String> params = new LinkedHashMap<>();
- int i = 0;
- while (i < data.length) {
- int keyStart = i;
- while (i < data.length && data[i] != 0) {
- i++;
- }
- if (i >= data.length) {
- break;
- }
- String key = new String(data, keyStart, i - keyStart, StandardCharsets.UTF_8);
- i++; // skip null
- if (key.isEmpty()) {
- break; // final null terminator
- }
- int valStart = i;
- while (i < data.length && data[i] != 0) {
- i++;
- }
- String value = new String(data, valStart, i - valStart, StandardCharsets.UTF_8);
- i++; // skip null
- params.put(key, value);
- }
- return params;
- }
-
/**
* Writes the backend StartupMessage: the proxy's own {@code user} and {@code database}, then
* the client's other parameters in the order the client sent them. {@code _pq_.} names request
@@ -569,28 +538,16 @@ public class PostgresProtocolHandler {
*/
private static void sendStartupToBackend(OutputStream out, String username, String dbName,
Map<String, String> clientParameters) throws IOException {
- ByteArrayOutputStream parameters = new ByteArrayOutputStream();
- writeStartupParameter(parameters, "user", username);
- writeStartupParameter(parameters, "database", dbName);
+ Map<String, String> parameters = new LinkedHashMap<>();
+ parameters.put("user", username);
+ parameters.put("database", dbName);
for (Map.Entry<String, String> parameter : clientParameters.entrySet()) {
String name = parameter.getKey();
if (!"user".equals(name) && !"database".equals(name) && !name.startsWith("_pq_.")) {
- writeStartupParameter(parameters, name, parameter.getValue());
+ parameters.put(name, parameter.getValue());
}
}
- parameters.write(0); // final null
-
- writeInt32(out, 4 + 4 + parameters.size());
- writeInt32(out, STARTUP_PROTOCOL_VERSION);
- parameters.writeTo(out);
- }
-
- private static void writeStartupParameter(ByteArrayOutputStream out, String name, String value)
- throws IOException {
- out.write(name.getBytes(StandardCharsets.UTF_8));
- out.write(0);
- out.write(value.getBytes(StandardCharsets.UTF_8));
- out.write(0);
+ out.write(PostgresWire.startupMessage(parameters));
}
// ── Client auth phase ─────────────────────────────────────────────────────
@@ -604,9 +561,9 @@ public class PostgresProtocolHandler {
LOG.warnv("Expected PasswordMessage ('p'), got {0}", (char) type);
return null;
}
- int length = checkedPacketLength(readInt32(in), 5, "password message");
+ int length = PostgresWire.checkedLength(PostgresWire.readInt32(in), 5, MAX_HANDSHAKE_PACKET_LENGTH, "password message");
byte[] data = new byte[length - 4];
- readFully(in, data);
+ PostgresWire.readFully(in, data);
// Strip trailing null terminator
int end = data.length;
while (end > 0 && data[end - 1] == 0) {
@@ -645,8 +602,8 @@ public class PostgresProtocolHandler {
return false;
}
- int length = checkedPacketLength(readInt32(in), 8, "backend authentication message");
- int authType = readInt32(in);
+ int length = PostgresWire.checkedLength(PostgresWire.readInt32(in), 8, MAX_HANDSHAKE_PACKET_LENGTH, "backend authentication message");
+ int authType = PostgresWire.readInt32(in);
if (authType == 0) {
// Trust auth — no password needed
@@ -663,7 +620,7 @@ public class PostgresProtocolHandler {
if (authType == 5) {
// MD5Password — read 4-byte salt
byte[] salt = new byte[4];
- readFully(in, salt);
+ PostgresWire.readFully(in, salt);
String md5pw = computeMd5Password(password, username, salt);
sendPasswordMessage(out, md5pw);
out.flush();
@@ -673,14 +630,14 @@ public class PostgresProtocolHandler {
if (authType == 10) {
// SCRAM-SHA-256 — drain the mechanisms list and perform SCRAM handshake
byte[] mechanismsBytes = new byte[length - 8];
- readFully(in, mechanismsBytes);
+ PostgresWire.readFully(in, mechanismsBytes);
return performScramSha256(in, out, username, password);
}
LOG.warnv("Unsupported backend PostgreSQL auth type: {0}", authType);
if (length > 8) {
byte[] extra = new byte[length - 8];
- readFully(in, extra);
+ PostgresWire.readFully(in, extra);
}
return false;
}
@@ -731,19 +688,19 @@ public class PostgresProtocolHandler {
saslInit.write((firstMsgBytes.length >> 8) & 0xFF);
saslInit.write(firstMsgBytes.length & 0xFF);
saslInit.write(firstMsgBytes);
- sendMessage(out, 'p', saslInit.toByteArray());
+ PostgresWire.write(out, 'p', saslInit.toByteArray());
out.flush();
// Step 2: Read AuthenticationSASLContinue (authType=11)
if (readAuthenticationType(in) != 'R') {
return false;
}
- int len2 = checkedPacketLength(readInt32(in), 8, "SASL continue message");
- if (readInt32(in) != 11) {
+ int len2 = PostgresWire.checkedLength(PostgresWire.readInt32(in), 8, MAX_HANDSHAKE_PACKET_LENGTH, "SASL continue message");
+ if (PostgresWire.readInt32(in) != 11) {
return false;
}
byte[] serverFirstBytes = new byte[len2 - 8];
- readFully(in, serverFirstBytes);
+ PostgresWire.readFully(in, serverFirstBytes);
String serverFirstMessage = new String(serverFirstBytes, StandardCharsets.UTF_8);
// Parse server-first-message: r=<nonce>,s=<base64-salt>,i=<iterations>
@@ -772,19 +729,19 @@ public class PostgresProtocolHandler {
+ ",p=" + Base64.getEncoder().encodeToString(clientProof);
// Send SASLResponse: just the final message bytes
- sendMessage(out, 'p', clientFinalMessage.getBytes(StandardCharsets.UTF_8));
+ PostgresWire.write(out, 'p', clientFinalMessage.getBytes(StandardCharsets.UTF_8));
out.flush();
// Step 4: Read AuthenticationSASLFinal (authType=12) — server signature (ignored)
if (readAuthenticationType(in) != 'R') {
return false;
}
- int len3 = checkedPacketLength(readInt32(in), 8, "SASL final message");
- if (readInt32(in) != 12) {
+ int len3 = PostgresWire.checkedLength(PostgresWire.readInt32(in), 8, MAX_HANDSHAKE_PACKET_LENGTH, "SASL final message");
+ if (PostgresWire.readInt32(in) != 12) {
return false;
}
byte[] serverFinalBytes = new byte[len3 - 8];
- readFully(in, serverFinalBytes);
+ PostgresWire.readFully(in, serverFinalBytes);
// Step 5: Read final AuthenticationOK
return readAuthOk(in);
@@ -847,11 +804,11 @@ public class PostgresProtocolHandler {
LOG.warnv("Expected AuthenticationOK from backend, got type={0}", type);
return false;
}
- int length = checkedPacketLength(readInt32(in), 8, "authentication response");
- int authType = readInt32(in);
+ int length = PostgresWire.checkedLength(PostgresWire.readInt32(in), 8, MAX_HANDSHAKE_PACKET_LENGTH, "authentication response");
+ int authType = PostgresWire.readInt32(in);
if (length > 8) {
byte[] extra = new byte[length - 8];
- readFully(in, extra);
+ PostgresWire.readFully(in, extra);
}
return authType == 0;
}
@@ -859,7 +816,7 @@ public class PostgresProtocolHandler {
private static void sendPasswordMessage(OutputStream out, String password) throws IOException {
byte[] pwBytes = password.getBytes(StandardCharsets.UTF_8);
// 'p' + Int32(4 + pwLen + 1) + password + null
- sendMessage(out, 'p', pwBytes, new byte[]{0});
+ PostgresWire.write(out, 'p', pwBytes, new byte[]{0});
}
private static String computeMd5Password(String password, String username, byte[] salt) {
@@ -902,19 +859,7 @@ public class PostgresProtocolHandler {
/** Reads the rest of a backend message whose type byte was {@code type}, returning it whole. */
private static byte[] readMessage(InputStream in, int type) throws IOException {
- int length = checkedPacketLength(readInt32(in), 4, "backend message");
- byte[] payload = new byte[length - 4];
- readFully(in, payload);
-
- // Reconstruct full message: type + length(4) + payload
- byte[] full = new byte[1 + 4 + payload.length];
- full[0] = (byte) type;
- full[1] = (byte) ((length >> 24) & 0xFF);
- full[2] = (byte) ((length >> 16) & 0xFF);
- full[3] = (byte) ((length >> 8) & 0xFF);
- full[4] = (byte) (length & 0xFF);
- System.arraycopy(payload, 0, full, 5, payload.length);
- return full;
+ return PostgresWire.frame(type, PostgresWire.readBody(in, 4, MAX_HANDSHAKE_PACKET_LENGTH, "backend message"));
}
// ── IAM session role ──────────────────────────────────────────────────────
@@ -929,7 +874,7 @@ public class PostgresProtocolHandler {
private static List<byte[]> assumeSessionRole(InputStream in, OutputStream out, String role)
throws IOException {
String sql = "SET SESSION AUTHORIZATION " + quoteIdentifier(role);
- sendMessage(out, 'Q', sql.getBytes(StandardCharsets.UTF_8), new byte[]{0});
+ PostgresWire.write(out, 'Q', sql.getBytes(StandardCharsets.UTF_8), new byte[]{0});
out.flush();
return readUntilReadyForQuery(in);
}
@@ -1211,7 +1156,7 @@ public class PostgresProtocolHandler {
/** Runs {@code sql} as a simple query and returns every message up to ReadyForQuery. */
private static List<byte[]> runQuery(InputStream in, OutputStream out, String sql) throws IOException {
- sendMessage(out, 'Q', sql.getBytes(StandardCharsets.UTF_8), new byte[]{0});
+ PostgresWire.write(out, 'Q', sql.getBytes(StandardCharsets.UTF_8), new byte[]{0});
out.flush();
List<byte[]> messages = new ArrayList<>();
while (true) {
@@ -1422,7 +1367,7 @@ public class PostgresProtocolHandler {
*/
private static List<byte[]> applySessionSettings(InputStream in, OutputStream out,
List<SessionSetting> settings) throws IOException {
- sendMessage(out, 'Q', setConfigStatement(settings).getBytes(StandardCharsets.UTF_8), new byte[]{0});
+ PostgresWire.write(out, 'Q', setConfigStatement(settings).getBytes(StandardCharsets.UTF_8), new byte[]{0});
out.flush();
return readUntilReadyForQuery(in);
}
@@ -1531,20 +1476,8 @@ public class PostgresProtocolHandler {
/** The {@code field} of an ErrorResponse, such as 'C' for SQLSTATE, or {@code fallback}. */
static String errorField(byte[] errorResponse, char field, String fallback) {
- int i = 5; // skip type byte and Int32 length
- while (i < errorResponse.length && errorResponse[i] != 0) {
- char fieldType = (char) errorResponse[i];
- i++;
- int start = i;
- while (i < errorResponse.length && errorResponse[i] != 0) {
- i++;
- }
- if (fieldType == field) {
- return new String(errorResponse, start, i - start, StandardCharsets.UTF_8);
- }
- i++; // skip the field's null terminator
- }
- return fallback;
+ // The fields start after the type byte and the Int32 length.
+ return PostgresWire.parseFields(errorResponse, 5).getOrDefault(field, fallback);
}
/**
@@ -1632,9 +1565,8 @@ public class PostgresProtocolHandler {
return;
}
byte[] header = new byte[4];
- readFully(from, header);
- int length = ((header[0] & 0xFF) << 24) | ((header[1] & 0xFF) << 16)
- | ((header[2] & 0xFF) << 8) | (header[3] & 0xFF);
+ PostgresWire.readFully(from, header);
+ int length = PostgresWire.int32(header, 0);
if (length < 4) {
return;
}
@@ -1643,7 +1575,7 @@ public class PostgresProtocolHandler {
if (type == 'S') {
// ParameterStatus is always small, so reading it whole to inspect costs nothing.
byte[] payload = new byte[remaining];
- readFully(from, payload);
+ PostgresWire.readFully(from, payload);
String[] status = parameterStatus(payload, 0);
if (status != null && "session_authorization".equals(status[0])
&& !iamRole.equals(status[1])) {
@@ -1700,90 +1632,15 @@ public class PostgresProtocolHandler {
/** An ErrorResponse ('E') or NoticeResponse ('N') carrying severity, SQLSTATE and message. */
static byte[] responseMessage(char type, String severity, String sqlState, String message) {
- byte[] sevBytes = severity.getBytes(StandardCharsets.UTF_8);
- byte[] stateBytes = sqlState.getBytes(StandardCharsets.UTF_8);
- byte[] msgBytes = message.getBytes(StandardCharsets.UTF_8);
-
- // Fields: S=severity, C=sqlstate, M=message, then final null byte
- ByteArrayOutputStream fields = new ByteArrayOutputStream();
- fields.write('S'); fields.writeBytes(sevBytes); fields.write(0);
- fields.write('C'); fields.writeBytes(stateBytes); fields.write(0);
- fields.write('M'); fields.writeBytes(msgBytes); fields.write(0);
- fields.write(0); // final null
-
- ByteArrayOutputStream framed = new ByteArrayOutputStream();
- try {
- sendMessage(framed, type, fields.toByteArray());
- } catch (IOException e) {
- throw new IllegalStateException("writing to memory cannot fail", e);
- }
- return framed.toByteArray();
+ Map<Character, String> fields = new LinkedHashMap<>();
+ fields.put('S', severity);
+ fields.put('C', sqlState);
+ fields.put('M', message);
+ return PostgresWire.frame(type, PostgresWire.fields(fields));
}
// ── Wire helpers ──────────────────────────────────────────────────────────
- private static void sendMessage(OutputStream out, char type, byte[]... parts) throws IOException {
- int totalPayload = 0;
- for (byte[] p : parts) {
- totalPayload += p.length;
- }
- out.write((byte) type);
- writeInt32(out, 4 + totalPayload); // length includes itself
- for (byte[] p : parts) {
- out.write(p);
- }
- }
-
- private static byte[] intBytes(int value) {
- return new byte[]{
- (byte) ((value >> 24) & 0xFF),
- (byte) ((value >> 16) & 0xFF),
- (byte) ((value >> 8) & 0xFF),
- (byte) (value & 0xFF)
- };
- }
-
- private static void writeInt32(OutputStream out, int value) throws IOException {
- out.write((value >> 24) & 0xFF);
- out.write((value >> 16) & 0xFF);
- out.write((value >> 8) & 0xFF);
- out.write(value & 0xFF);
- }
-
- private static int readInt32(InputStream in) throws IOException {
- int b0 = in.read();
- int b1 = in.read();
- int b2 = in.read();
- int b3 = in.read();
- if ((b0 | b1 | b2 | b3) < 0) {
- throw new EOFException("Connection closed while reading Int32");
- }
- return (b0 << 24) | (b1 << 16) | (b2 << 8) | b3;
- }
-
- private static int checkedPacketLength(int length, int minimum, String packetName) throws IOException {
- if (length < minimum) {
- throw new IOException("PostgreSQL " + packetName + " length is below the "
- + minimum + " byte minimum: " + length);
- }
- if (length > MAX_HANDSHAKE_PACKET_LENGTH) {
- throw new IOException("PostgreSQL " + packetName + " length exceeds the "
- + MAX_HANDSHAKE_PACKET_LENGTH + " byte limit: " + length);
- }
- return length;
- }
-
- private static void readFully(InputStream in, byte[] buf) throws IOException {
- int offset = 0;
- while (offset < buf.length) {
- int n = in.read(buf, offset, buf.length - offset);
- if (n < 0) {
- throw new EOFException("Connection closed while reading " + buf.length + " bytes");
- }
- offset += n;
- }
- }
-
private static String bytesToHex(byte[] bytes) {
StringBuilder sb = new StringBuilder(bytes.length * 2);
for (byte b : bytes) {
diff --git a/src/main/java/io/github/hectorvent/floci/services/redshift/proxy/PostgresWireDecoder.java b/src/main/java/io/github/hectorvent/floci/services/redshift/proxy/PostgresWireDecoder.java
index b650f4a2ba..afe1c9aae5 100644
--- a/src/main/java/io/github/hectorvent/floci/services/redshift/proxy/PostgresWireDecoder.java
+++ b/src/main/java/io/github/hectorvent/floci/services/redshift/proxy/PostgresWireDecoder.java
@@ -1,5 +1,7 @@
package io.github.hectorvent.floci.services.redshift.proxy;
+import io.github.hectorvent.floci.core.common.PostgresWire;
+
import java.io.ByteArrayOutputStream;
import java.io.EOFException;
import java.io.IOException;
@@ -95,14 +97,8 @@ public class PostgresWireDecoder {
char type = (char) typeByte;
byte[] lengthBytes = readFully(4);
- int length = ((lengthBytes[0] & 0xFF) << 24)
- | ((lengthBytes[1] & 0xFF) << 16)
- | ((lengthBytes[2] & 0xFF) << 8)
- | (lengthBytes[3] & 0xFF);
-
- if (length < 4) {
- throw new IOException("Invalid message length: " + length);
- }
+ int length = PostgresWire.checkedLength(PostgresWire.int32(lengthBytes, 0), 4, Integer.MAX_VALUE,
+ "frontend message");
if (passthroughOut != null && (!inspectType.test(type) || length > MAX_MESSAGE_BYTES)) {
passthroughOut.write(typeByte);
@@ -126,10 +122,7 @@ public class PostgresWireDecoder {
return new FrontendMessage(type, null);
}
- if (length > MAX_MESSAGE_BYTES) {
- throw new IOException("Refusing PostgreSQL message of " + length
- + " bytes (limit " + MAX_MESSAGE_BYTES + ")");
- }
+ PostgresWire.checkedLength(length, 4, MAX_MESSAGE_BYTES, "frontend message");
byte[] body = (length == 4) ? new byte[0] : readFully(length - 4);
betweenMessages = true;
@@ -190,23 +183,13 @@ public class PostgresWireDecoder {
}
ByteArrayOutputStream body = new ByteArrayOutputStream();
- writeCString(body, statementName);
- writeCString(body, rewrittenSql);
+ body.writeBytes(PostgresWire.cStringBytes(statementName));
+ body.writeBytes(PostgresWire.cStringBytes(rewrittenSql));
writeInt16(body, parameterTypeOids.size());
for (Integer oid : parameterTypeOids) {
- writeInt32(body, Objects.requireNonNull(oid, "parameter type OID must not be null"));
+ body.writeBytes(PostgresWire.int32(Objects.requireNonNull(oid, "parameter type OID must not be null")));
}
-
- byte[] bodyBytes = body.toByteArray();
- int length = bodyBytes.length + 4;
- byte[] packet = new byte[bodyBytes.length + 5];
- packet[0] = 'P';
- packet[1] = (byte) ((length >> 24) & 0xFF);
- packet[2] = (byte) ((length >> 16) & 0xFF);
- packet[3] = (byte) ((length >> 8) & 0xFF);
- packet[4] = (byte) (length & 0xFF);
- System.arraycopy(bodyBytes, 0, packet, 5, bodyBytes.length);
- return packet;
+ return PostgresWire.frame('P', body.toByteArray());
}
private TargetMessage decodeTarget(FrontendMessage message, char expectedType) throws IOException {
@@ -234,35 +217,15 @@ public class PostgresWireDecoder {
return new BodyCursor(message.body());
}
- private static void writeCString(ByteArrayOutputStream out, String value) {
- byte[] bytes = value.getBytes(StandardCharsets.UTF_8);
- out.writeBytes(bytes);
- out.write(0);
- }
-
private static void writeInt16(ByteArrayOutputStream out, int value) {
out.write((value >> 8) & 0xFF);
out.write(value & 0xFF);
}
- private static void writeInt32(ByteArrayOutputStream out, int value) {
- out.write((value >> 24) & 0xFF);
- out.write((value >> 16) & 0xFF);
- out.write((value >> 8) & 0xFF);
- out.write(value & 0xFF);
- }
-
/** Read exactly {@code n} bytes or throw {@link EOFException}. */
private byte[] readFully(int n) throws IOException {
byte[] buf = new byte[n];
- int off = 0;
- while (off < n) {
- int read = in.read(buf, off, n - off);
- if (read == -1) {
- throw new EOFException("Unexpected EOF (expected " + n + " bytes, got " + off + ")");
- }
- off += read;
- }
+ PostgresWire.readFully(in, buf);
return buf;
}
@@ -324,22 +287,7 @@ public class PostgresWireDecoder {
/** Encode an SQL string as a Simple Query ({@code 'Q'}) packet with a NUL-terminated body. */
public static byte[] encodeQuery(String sql) {
- if (sql == null) {
- sql = "";
- }
- byte[] sqlBytes = sql.getBytes(StandardCharsets.UTF_8);
- int bodyLength = sqlBytes.length + 1;
- int totalLength = 4 + bodyLength;
-
- byte[] packet = new byte[1 + 4 + bodyLength];
- packet[0] = 'Q';
- packet[1] = (byte) ((totalLength >> 24) & 0xFF);
- packet[2] = (byte) ((totalLength >> 16) & 0xFF);
- packet[3] = (byte) ((totalLength >> 8) & 0xFF);
- packet[4] = (byte) (totalLength & 0xFF);
- System.arraycopy(sqlBytes, 0, packet, 5, sqlBytes.length);
- packet[packet.length - 1] = 0x00;
- return packet;
+ return PostgresWire.frame('Q', PostgresWire.cStringBytes(sql == null ? "" : sql));
}
/** A single PostgreSQL wire message from the client. */
@@ -366,18 +314,7 @@ public class PostgresWireDecoder {
if (body == null) {
throw new IllegalStateException("Message body was streamed to passthrough output and not retained");
}
- int bodyLen = body.length;
- int lengthField = 4 + bodyLen;
- byte[] packet = new byte[1 + 4 + bodyLen];
- packet[0] = (byte) type;
- packet[1] = (byte) ((lengthField >> 24) & 0xFF);
- packet[2] = (byte) ((lengthField >> 16) & 0xFF);
- packet[3] = (byte) ((lengthField >> 8) & 0xFF);
- packet[4] = (byte) (lengthField & 0xFF);
- if (bodyLen > 0) {
- System.arraycopy(body, 0, packet, 5, bodyLen);
- }
- return packet;
+ return PostgresWire.frame(type, body);
}
}
diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml
index 5d365c8ded..b0116ea081 100644
--- a/src/main/resources/application.yml
+++ b/src/main/resources/application.yml
@@ -541,7 +541,10 @@ floci:
database-resuming-exception-enabled: false # FLOCI_SERVICES_RDS_DATA_DATABASE_RESUMING_EXCEPTION_ENABLED
dsql:
enabled: true
+ mock: false # FLOCI_SERVICES_DSQL_MOCK; true = clusters without a database, no Docker
+ port: 5432 # FLOCI_SERVICES_DSQL_PORT; the port every cluster endpoint is served on
endpoint-suffix: localhost.floci.io # FLOCI_SERVICES_DSQL_ENDPOINT_SUFFIX; endpoints are <id>.dsql.<region>.<suffix>
+ image: "postgres:16-alpine" # FLOCI_SERVICES_DSQL_IMAGE
redshift-data:
enabled: true
result-ttl-hours: 24
diff --git a/src/main/resources/dsql/bootstrap.sql b/src/main/resources/dsql/bootstrap.sql
new file mode 100644
index 0000000000..e94e529891
--- /dev/null
+++ b/src/main/resources/dsql/bootstrap.sql
@@ -0,0 +1,15 @@
+-- What a cluster's PostgreSQL is before any client connects, as Aurora DSQL's database is (measured
+-- on AWS, eu-west-1, 2026-10-16): the role admin, which owns the public schema, and repeatable read.
+-- Installed by Floci as the superuser dbowner every time the database starts; every statement is
+-- idempotent.
+
+DO $$
+BEGIN
+ IF NOT EXISTS (SELECT 1 FROM pg_roles WHERE rolname = 'admin') THEN
+ CREATE ROLE admin WITH LOGIN CREATEROLE CREATEDB;
+ END IF;
+END
+$$;
+GRANT CREATE ON DATABASE postgres TO admin;
+ALTER SCHEMA public OWNER TO admin;
+ALTER DATABASE postgres SET default_transaction_isolation TO 'repeatable read';
diff --git a/src/test/java/io/github/hectorvent/floci/core/common/PostgresWireTest.java b/src/test/java/io/github/hectorvent/floci/core/common/PostgresWireTest.java
new file mode 100644
index 0000000000..d491c13e4c
--- /dev/null
+++ b/src/test/java/io/github/hectorvent/floci/core/common/PostgresWireTest.java
@@ -0,0 +1,109 @@
+package io.github.hectorvent.floci.core.common;
+
+import org.junit.jupiter.api.Test;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.EOFException;
+import java.io.IOException;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+
+import static org.junit.jupiter.api.Assertions.assertArrayEquals;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+class PostgresWireTest {
+
+ @Test
+ void aMessageIsFramedWithALengthThatCountsItself() throws IOException {
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ PostgresWire.write(out, 'Q', PostgresWire.cStringBytes("SELECT 1"));
+
+ byte[] framed = out.toByteArray();
+ assertArrayEquals(PostgresWire.frame('Q', PostgresWire.cStringBytes("SELECT 1")), framed);
+ assertEquals('Q', framed[0]);
+ assertEquals(4 + 9, PostgresWire.int32(framed, 1));
+
+ PostgresWire.Message read = PostgresWire.read(new ByteArrayInputStream(framed), 1024, "message");
+ assertEquals('Q', read.type());
+ assertEquals("SELECT 1", PostgresWire.cString(read.body(), 0).value());
+ }
+
+ @Test
+ void aMessageIsWrittenFromItsPartsInOrder() throws IOException {
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ PostgresWire.write(out, 'p', "secret".getBytes(), new byte[]{0});
+
+ assertArrayEquals(PostgresWire.frame('p', PostgresWire.cStringBytes("secret")), out.toByteArray());
+ }
+
+ @Test
+ void aLengthOutsideItsBoundsIsRefusedNamingThePacket() {
+ IOException low = assertThrows(IOException.class, () -> PostgresWire.checkedLength(3, 4, 10, "backend message"));
+ assertEquals("PostgreSQL backend message length is below the 4 byte minimum: 3", low.getMessage());
+ IOException high = assertThrows(IOException.class, () -> PostgresWire.checkedLength(11, 4, 10, "backend message"));
+ assertEquals("PostgreSQL backend message length exceeds the 10 byte limit: 11", high.getMessage());
+ }
+
+ @Test
+ void aStreamThatEndsMidMessageIsAnEof() {
+ byte[] truncated = {'Q', 0, 0, 0, 9, 'S'};
+
+ assertThrows(EOFException.class, () ->
+ PostgresWire.read(new ByteArrayInputStream(truncated), 1024, "message"));
+ assertThrows(EOFException.class, () ->
+ PostgresWire.read(new ByteArrayInputStream(new byte[0]), 1024, "message"));
+ }
+
+ @Test
+ void aStartupMessageCarriesItsParametersInOrderAndReadsBack() throws IOException {
+ Map<String, String> parameters = new LinkedHashMap<>();
+ parameters.put("user", "admin");
+ parameters.put("database", "postgres");
+ parameters.put("options", "-c search_path=public");
+
+ byte[] packet = PostgresWire.startupMessage(parameters);
+ PostgresWire.Startup startup = PostgresWire.readStartup(new ByteArrayInputStream(packet), 10_000,
+ "startup message");
+
+ assertEquals(PostgresWire.PROTOCOL_3, startup.code());
+ Map<String, String> read = PostgresWire.startupParameters(startup.body());
+ assertEquals(parameters, read);
+ assertEquals(List.of("user", "database", "options"), List.copyOf(read.keySet()));
+ }
+
+ @Test
+ void anSslRequestIsAStartupPacketWithNoBody() throws IOException {
+ byte[] request = {0, 0, 0, 8, 4, (byte) 0xD2, 0x16, 0x2F};
+
+ PostgresWire.Startup startup = PostgresWire.readStartup(new ByteArrayInputStream(request), 10_000,
+ "startup message");
+
+ assertEquals(PostgresWire.SSL_REQUEST, startup.code());
+ assertEquals(0, startup.body().length);
+ }
+
+ @Test
+ void startupParametersStopAtTheEmptyNameOrAnUnterminatedOne() {
+ byte[] body = {'u', 's', 'e', 'r', 0, 'a', 0, 0, 'x', 0, 'y', 0};
+ byte[] unterminated = {'u', 's', 'e', 'r', 0, 'a', 0, 'd', 'b'};
+
+ assertEquals(Map.of("user", "a"), PostgresWire.startupParameters(body));
+ assertEquals(Map.of("user", "a"), PostgresWire.startupParameters(unterminated));
+ }
+
+ @Test
+ void anErrorResponseKeepsItsFieldsInOrder() {
+ Map<Character, String> fields = new LinkedHashMap<>();
+ fields.put('S', "FATAL");
+ fields.put('C', "28000");
+ fields.put('M', "access denied");
+
+ byte[] framed = PostgresWire.frame('E', PostgresWire.fields(fields));
+
+ assertEquals(fields, PostgresWire.parseFields(framed, 5));
+ assertEquals(List.of('S', 'C', 'M'), List.copyOf(PostgresWire.parseFields(framed, 5).keySet()));
+ }
+}
diff --git a/src/test/java/io/github/hectorvent/floci/core/common/auth/SigV4RequestValidatorTest.java b/src/test/java/io/github/hectorvent/floci/core/common/auth/SigV4RequestValidatorTest.java
index 1538744dd8..0d2c191344 100644
--- a/src/test/java/io/github/hectorvent/floci/core/common/auth/SigV4RequestValidatorTest.java
+++ b/src/test/java/io/github/hectorvent/floci/core/common/auth/SigV4RequestValidatorTest.java
@@ -10,6 +10,7 @@ import org.junit.jupiter.params.provider.CsvSource;
import org.junit.jupiter.params.provider.ValueSource;
import java.nio.charset.StandardCharsets;
+import java.time.Duration;
import java.time.Instant;
import static org.junit.jupiter.api.Assertions.assertEquals;
@@ -153,6 +154,22 @@ class SigV4RequestValidatorTest {
assertFalse(validate(iamService, token.replace("DBUser=app%20user", "DBUser=app%2Buser")));
}
+ @Test
+ void validateTakesAnExpiredTokenOnlyWithinTheGraceItIsGiven() throws Exception {
+ IamService iamService = IamServiceTestHelper.iamServiceWithAccessKey("AKIDDSQL", "secret-dsql");
+ String token = SigV4TokenTestHelper.createDsqlToken("abc.dsql.eu-west-1.localhost.floci.io", "eu-west-1",
+ "DbConnectAdmin", "AKIDDSQL", "secret-dsql", Instant.now().minusSeconds(63), 60, null);
+ String rawQuery = token.substring(token.indexOf('?') + 1);
+ SigV4RequestValidator validator = new SigV4RequestValidator(iamService);
+
+ assertTrue(validator.validate(rawQuery, "abc.dsql.eu-west-1.localhost.floci.io", "DbConnectAdmin", "DBUser",
+ false, null, "DSQL IAM token", null, Duration.ofSeconds(5)));
+ assertFalse(validator.validate(rawQuery, "abc.dsql.eu-west-1.localhost.floci.io", "DbConnectAdmin", "DBUser",
+ false, null, "DSQL IAM token", null, Duration.ZERO));
+ assertFalse(validator.validate(rawQuery, "abc.dsql.eu-west-1.localhost.floci.io", "DbConnect", "DBUser",
+ false, null, "DSQL IAM token", null, Duration.ofSeconds(5)));
+ }
+
private static boolean validate(IamService iamService, String token) {
String rawQuery = token.substring(token.indexOf('?') + 1);
return new SigV4RequestValidator(iamService).validate(
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
new file mode 100644
index 0000000000..a2c7a77c5f
--- /dev/null
+++ b/src/test/java/io/github/hectorvent/floci/services/dsql/DsqlDataPlaneIntegrationTest.java
@@ -0,0 +1,250 @@
+package io.github.hectorvent.floci.services.dsql;
+
+import com.github.dockerjava.api.DockerClient;
+import io.github.hectorvent.floci.services.dsql.dataplane.DsqlEndpoint;
+import io.github.hectorvent.floci.testing.EnforcementFixtures;
+import io.github.hectorvent.floci.testing.PartitionCleanup;
+import io.github.hectorvent.floci.testutil.SigV4TokenTestHelper;
+import io.quarkus.test.junit.QuarkusTest;
+import io.quarkus.test.junit.TestProfile;
+import io.restassured.specification.RequestSpecification;
+import jakarta.inject.Inject;
+import org.eclipse.microprofile.config.inject.ConfigProperty;
+import org.junit.jupiter.api.AfterAll;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Tag;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.TestInstance;
+import org.junit.jupiter.api.extension.RegisterExtension;
+import org.postgresql.util.PSQLException;
+
+import java.sql.Connection;
+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.List;
+import java.util.Map;
+import java.util.Properties;
+
+import static io.restassured.RestAssured.given;
+import static org.awaitility.Awaitility.await;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.junit.jupiter.api.Assumptions.assumeTrue;
+
+/**
+ * Aurora DSQL's data plane against a real PostgreSQL container: a client connects to the cluster
+ * endpoint with TLS and an IAM token, with the refusals measured on AWS (eu-west-1, 2026-10-16).
+ */
+@QuarkusTest
+@TestProfile(DsqlDatabasesProfile.class)
+@TestInstance(TestInstance.Lifecycle.PER_CLASS)
+@Tag("docker")
+class DsqlDataPlaneIntegrationTest {
+
+ private static final String ACCOUNT = "111122223333";
+ private static final String REGION = "eu-west-1";
+ private static final String AUTH = "AWS4-HMAC-SHA256 Credential=" + ACCOUNT
+ + "/20261016/" + REGION + "/dsql/aws4_request, SignedHeaders=host, Signature=abc";
+
+ @Inject
+ DockerClient dockerClient;
+
+ @Inject
+ DsqlEndpoint endpoint;
+
+ @ConfigProperty(name = "quarkus.http.test-port", defaultValue = "0")
+ int httpPort;
+
+ @RegisterExtension
+ final PartitionCleanup cleanup = new PartitionCleanup();
+
+ private String identifier;
+ private String host;
+
+ @BeforeAll
+ void createCluster() {
+ try {
+ dockerClient.pingCmd().exec();
+ } catch (Exception e) {
+ assumeTrue(false, "Docker is not available: " + e.getMessage());
+ }
+ identifier = dsql().body(Map.of("deletionProtectionEnabled", false))
+ .post("/cluster").then().statusCode(200).extract().path("identifier");
+ host = identifier + ".dsql." + REGION + ".localhost.floci.io";
+ await().atMost(Duration.ofMinutes(3)).pollInterval(Duration.ofMillis(500)).until(() ->
+ "ACTIVE".equals(dsql().get("/cluster/" + identifier).then().extract().path("status")));
+ }
+
+ @AfterAll
+ void deleteCluster() {
+ if (identifier != null) {
+ dsql().delete("/cluster/" + identifier);
+ await().atMost(Duration.ofMinutes(1)).until(() ->
+ dsql().get("/cluster/" + identifier).then().extract().statusCode() == 404);
+ }
+ }
+
+ @Test
+ void adminConnectsWithAnAdminTokenToDsqlsDatabase() throws Exception {
+ try (Connection connection = connect("admin", adminToken());
+ Statement statement = connection.createStatement();
+ ResultSet row = statement.executeQuery("SELECT current_user, current_setting('transaction_isolation'),"
+ + " (SELECT nspowner::regrole::text FROM pg_namespace WHERE nspname = 'public')")) {
+ assertTrue(row.next());
+ assertEquals("admin", row.getString(1));
+ assertEquals("repeatable read", row.getString(2));
+ assertEquals("admin", row.getString(3));
+ }
+ }
+
+ @Test
+ void aConnectionIsRefusedAsDsqlRefusesIt() throws Exception {
+ PSQLException plain = assertThrows(PSQLException.class, () -> {
+ Properties properties = properties("admin", adminToken());
+ properties.setProperty("sslmode", "disable");
+ DriverManager.getConnection(url(), properties).close();
+ });
+ assertEquals("unable to accept connection, SSL is mandatory. AWS Authentication is required.",
+ plain.getServerErrorMessage().getMessage());
+ assertEquals("08P01", plain.getSQLState());
+ assertTrue(plain.getServerErrorMessage().getDetail().startsWith("Session Id: "));
+
+ PSQLException wrongAction = assertThrows(PSQLException.class, () ->
+ connect("admin", token("DbConnect", "test", "test", Instant.now(), 900, null)).close());
+ assertEquals("unable to accept connection, access denied", wrongAction.getServerErrorMessage().getMessage());
+ assertEquals("08006", wrongAction.getSQLState());
+ assertTrue(wrongAction.getServerErrorMessage().getDetail().startsWith("Session Id: "));
+ assertEquals("Wrong user to action mapping. user: admin, action: DbConnect",
+ wrongAction.getServerErrorMessage().getHint());
+
+ PSQLException expired = assertThrows(PSQLException.class, () -> connect("admin",
+ token("DbConnectAdmin", "test", "test", Instant.now().minusSeconds(120), 60, null)).close());
+ assertTrue(expired.getServerErrorMessage().getHint().startsWith("Signature expired: "),
+ expired.getServerErrorMessage().getHint());
+
+ PSQLException garbage = assertThrows(PSQLException.class, () -> connect("admin", "not-a-token").close());
+ assertEquals("unable to accept connection, access denied", garbage.getServerErrorMessage().getMessage());
+ assertEquals(null, garbage.getServerErrorMessage().getHint());
+
+ PSQLException otherDatabase = assertThrows(PSQLException.class, () -> {
+ Properties properties = properties("admin", adminToken());
+ DriverManager.getConnection("jdbc:postgresql://127.0.0.1:" + endpoint.port() + "/other", properties)
+ .close();
+ });
+ assertEquals("database \"other\" is not currently accepting connections",
+ otherDatabase.getServerErrorMessage().getMessage());
+ assertEquals("55000", otherDatabase.getSQLState());
+ }
+
+ /** A role other than {@code admin} signs in only for an IAM role mapped to it, and none is. */
+ @Test
+ void aRoleOtherThanAdminIsRefusedAsAnUnmappedRole() throws Exception {
+ try (Connection admin = connect("admin", adminToken()); Statement statement = admin.createStatement()) {
+ statement.execute("CREATE ROLE app_role WITH LOGIN");
+ }
+
+ PSQLException unmapped = assertThrows(PSQLException.class, () ->
+ connect("app_role", token("DbConnect", "test", "test", Instant.now(), 900, null)).close());
+ assertEquals("28000", unmapped.getSQLState());
+ assertEquals("unable to accept connection, access denied", unmapped.getServerErrorMessage().getMessage());
+ assertTrue(unmapped.getServerErrorMessage().getDetail().startsWith("Session Id: "));
+
+ PSQLException adminAction = assertThrows(PSQLException.class, () ->
+ connect("app_role", token("DbConnectAdmin", "test", "test", Instant.now(), 900, null)).close());
+ assertEquals("08006", adminAction.getSQLState());
+ assertEquals("Wrong user to action mapping. user: app_role, action: DbConnectAdmin",
+ adminAction.getServerErrorMessage().getHint());
+ }
+
+ @Test
+ void aStartupSettingIsReadAsDsqlReadsIt() throws Exception {
+ for (String name : List.of("dsql.iam_session_user", "myapp.foo")) {
+ Properties custom = properties("admin", adminToken());
+ custom.setProperty("options", "-c " + name + "=x");
+ PSQLException refused = assertThrows(PSQLException.class, () ->
+ DriverManager.getConnection(url(), custom).close());
+ assertEquals("42704", refused.getSQLState());
+ assertEquals("unrecognized configuration parameter \"" + name + "\"",
+ refused.getServerErrorMessage().getMessage());
+ assertTrue(refused.getServerErrorMessage().getDetail().startsWith("Session Id: "));
+ }
+
+ Properties unknown = properties("admin", adminToken());
+ unknown.setProperty("options", "-c nosuch_parameter=1");
+ PSQLException fromTheDatabase = assertThrows(PSQLException.class, () ->
+ DriverManager.getConnection(url(), unknown).close());
+ assertEquals("42704", fromTheDatabase.getSQLState());
+ assertEquals("unrecognized configuration parameter \"nosuch_parameter\"",
+ fromTheDatabase.getServerErrorMessage().getMessage());
+ assertTrue(fromTheDatabase.getServerErrorMessage().getDetail().startsWith("Session Id: "));
+ assertEquals(null, fromTheDatabase.getServerErrorMessage().getFile());
+
+ Properties ignored = properties("admin", adminToken());
+ ignored.setProperty("options", "--search_path=elsewhere -c search_path=first -c search_path=second");
+ try (Connection connection = DriverManager.getConnection(url(), ignored);
+ Statement statement = connection.createStatement();
+ ResultSet row = statement.executeQuery("SELECT current_setting('search_path')")) {
+ assertTrue(row.next());
+ assertEquals("first", row.getString(1));
+ }
+ }
+
+ /**
+ * A long-term key signs in to its own account's clusters and to no other account's, whichever
+ * account is Floci's default: the cluster here is not in the default one.
+ */
+ @Test
+ void aLongTermKeySignsInOnlyToItsOwnAccountsClusters() throws Exception {
+ EnforcementFixtures.UserCredentials own = accessKey(ACCOUNT);
+ EnforcementFixtures.UserCredentials other = accessKey("000000000000");
+
+ connect("admin", token("DbConnectAdmin", own.accessKeyId(), own.secretAccessKey(), Instant.now(), 900,
+ null)).close();
+ PSQLException foreign = assertThrows(PSQLException.class, () -> connect("admin", token("DbConnectAdmin",
+ other.accessKeyId(), other.secretAccessKey(), Instant.now(), 900, null)).close());
+ assertEquals("08006", foreign.getSQLState());
+ assertEquals("unable to accept connection, access denied", foreign.getServerErrorMessage().getMessage());
+ }
+
+ /** A new IAM user's access key in {@code account}, removed with the user after the test. */
+ private EnforcementFixtures.UserCredentials accessKey(String account) {
+ return EnforcementFixtures.createUser(cleanup, httpPort, EnforcementFixtures.auth(account, "iam"),
+ "dsql-" + System.nanoTime());
+ }
+
+ private Connection connect(String user, String password) throws SQLException {
+ return DriverManager.getConnection(url(), properties(user, password));
+ }
+
+ private String url() {
+ return "jdbc:postgresql://127.0.0.1:" + endpoint.port() + "/postgres";
+ }
+
+ private static 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");
+ return properties;
+ }
+
+ private String adminToken() throws Exception {
+ return token("DbConnectAdmin", "test", "test", Instant.now(), 900, null);
+ }
+
+ private String token(String action, String accessKeyId, String secret, Instant at, int expires,
+ String sessionToken) throws Exception {
+ return SigV4TokenTestHelper.createDsqlToken(host, REGION, action, accessKeyId, secret, at, expires,
+ sessionToken);
+ }
+
+ private RequestSpecification dsql() {
+ return given().port(httpPort).header("Authorization", AUTH).contentType("application/json");
+ }
+}
diff --git a/src/test/java/io/github/hectorvent/floci/services/dsql/DsqlDatabasesProfile.java b/src/test/java/io/github/hectorvent/floci/services/dsql/DsqlDatabasesProfile.java
new file mode 100644
index 0000000000..19533eff4a
--- /dev/null
+++ b/src/test/java/io/github/hectorvent/floci/services/dsql/DsqlDatabasesProfile.java
@@ -0,0 +1,13 @@
+package io.github.hectorvent.floci.services.dsql;
+
+import io.quarkus.test.junit.QuarkusTestProfile;
+
+import java.util.Map;
+
+/** Clusters with a real database in Docker, served on an ephemeral port. */
+public final class DsqlDatabasesProfile implements QuarkusTestProfile {
+ @Override
+ public Map<String, String> getConfigOverrides() {
+ return Map.of("floci.services.dsql.mock", "false", "floci.services.dsql.port", "0");
+ }
+}
diff --git a/src/test/java/io/github/hectorvent/floci/services/dsql/DsqlServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/dsql/DsqlServiceTest.java
index 159c2f94d3..fff59e09c4 100644
--- a/src/test/java/io/github/hectorvent/floci/services/dsql/DsqlServiceTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/dsql/DsqlServiceTest.java
@@ -12,15 +12,21 @@ import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.time.Instant;
+import java.util.HashMap;
import java.util.List;
import java.util.Map;
+import java.util.function.Consumer;
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.assertTrue;
+import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.ArgumentMatchers.argThat;
+import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
/**
@@ -35,14 +41,21 @@ class DsqlServiceTest {
private static final String ROOT = "arn:aws:iam::" + ACCOUNT + ":root";
private final DsqlPrincipals principals = mock(DsqlPrincipals.class);
+ private final DsqlDataPlane dataPlane = mock(DsqlDataPlane.class);
private final ObjectMapper mapper = new ObjectMapper();
private final DsqlService service = new DsqlService(
new AccountAwareStorageBackend<>(new InMemoryStorage<>(), null, ACCOUNT),
- new RegionResolver(REGION, ACCOUNT), "localhost.floci.io", new DsqlDataPlane(), principals,
- new IamPolicyEvaluator(mapper), mapper);
+ new RegionResolver(REGION, ACCOUNT), "localhost.floci.io", dataPlane, principals,
+ new IamPolicyEvaluator(mapper), mapper, true);
@BeforeEach
void caller() {
+ // A database that is ready at once and gone at once, as with floci.services.dsql.mock.
+ readyAtOnce(dataPlane);
+ doAnswer(call -> {
+ call.<Runnable>getArgument(1).run();
+ return null;
+ }).when(dataPlane).deprovision(any(), any());
when(principals.caller()).thenReturn(CallerArns.accountRoot(ROOT, ACCOUNT));
when(principals.exists(anyString())).thenAnswer(call -> call.getArgument(0, String.class).endsWith(":root"));
}
@@ -161,6 +174,37 @@ class DsqlServiceTest {
service.getCluster("eu-west-3", second.getIdentifier()).getLinkedClusterArns());
}
+ /**
+ * Peering makes a cluster ACTIVE only once its database answers: until then it is CREATING, and
+ * one whose database failed is FAILED, not left waiting.
+ */
+ @Test
+ void aPeerIsActiveOnlyOnceItsDatabaseAnswers() {
+ DsqlDataPlane slow = mock(DsqlDataPlane.class);
+ Map<String, Consumer<Boolean>> starting = new HashMap<>();
+ doAnswer(call -> {
+ starting.put(call.<DsqlCluster>getArgument(0).getIdentifier(), call.getArgument(1));
+ return null;
+ }).when(slow).provision(any(), any());
+ DsqlService peers = service(new AccountAwareStorageBackend<>(new InMemoryStorage<>(), null, ACCOUNT), slow);
+ DsqlCluster first = peers.createCluster(REGION, false, null, null, null, "eu-west-2", null, null, false);
+ DsqlCluster second = peers.createCluster("eu-west-3", false, null, null, null, "eu-west-2", null, null, false);
+ DsqlCluster failing = peers.createCluster(REGION, false, null, null, null, "eu-west-2", null, null, false);
+
+ starting.get(first.getIdentifier()).accept(true);
+ starting.get(failing.getIdentifier()).accept(false);
+ assertEquals(DsqlService.PENDING_SETUP, peers.getCluster(REGION, first.getIdentifier()).getStatus());
+ assertEquals(DsqlService.FAILED, peers.getCluster(REGION, failing.getIdentifier()).getStatus());
+
+ peers.updateCluster(REGION, first.getIdentifier(), null, null, null, List.of(second.getArn()));
+ peers.updateCluster("eu-west-3", second.getIdentifier(), null, null, null, List.of(first.getArn()));
+ assertEquals(DsqlService.ACTIVE, peers.getCluster(REGION, first.getIdentifier()).getStatus());
+ assertEquals(DsqlService.CREATING, peers.getCluster("eu-west-3", second.getIdentifier()).getStatus());
+
+ starting.get(second.getIdentifier()).accept(true);
+ assertEquals(DsqlService.ACTIVE, peers.getCluster("eu-west-3", second.getIdentifier()).getStatus());
+ }
+
@Test
void anUpdateLeavesWhatItDoesNotNameAsItWas() {
DsqlCluster cluster = service.createCluster(REGION, false, "arn:aws:kms:eu-west-1:" + ACCOUNT + ":key/k",
@@ -198,6 +242,44 @@ class DsqlServiceTest {
assertNull(tooMany.getExtendedData());
}
+ /**
+ * A restart over the same storage starts every cluster's database again, and finishes a
+ * deletion the stop interrupted.
+ */
+ @Test
+ void aRestartBringsEveryClusterBackAndFinishesAnInterruptedDeletion() {
+ AccountAwareStorageBackend<DsqlCluster> storage =
+ new AccountAwareStorageBackend<>(new InMemoryStorage<>(), null, ACCOUNT);
+ DsqlDataPlane stopping = mock(DsqlDataPlane.class);
+ readyAtOnce(stopping);
+ DsqlService before = service(storage, stopping);
+ DsqlCluster kept = before.createCluster(REGION, false, null, null, null, null, null, null, false);
+ DsqlCluster deleting = before.createCluster(REGION, false, null, null, null, null, null, null, false);
+ // The stop comes before the database is gone, so the cluster stays DELETING.
+ before.deleteCluster(REGION, deleting.getIdentifier());
+
+ DsqlService after = service(storage, dataPlane);
+ after.onStart(null);
+
+ verify(dataPlane).provision(argThat(cluster -> kept.getIdentifier().equals(cluster.getIdentifier())), any());
+ verify(dataPlane).deprovision(argThat(cluster -> deleting.getIdentifier().equals(cluster.getIdentifier())), any());
+ assertEquals(DsqlService.ACTIVE, after.getCluster(REGION, kept.getIdentifier()).getStatus());
+ assertThrows(AwsException.class, () -> after.getCluster(REGION, deleting.getIdentifier()));
+ }
+
+ /** Makes {@code plane} start every cluster's database at once. */
+ private static void readyAtOnce(DsqlDataPlane plane) {
+ doAnswer(call -> {
+ call.<Consumer<Boolean>>getArgument(1).accept(true);
+ return null;
+ }).when(plane).provision(any(), any());
+ }
+
+ private DsqlService service(AccountAwareStorageBackend<DsqlCluster> storage, DsqlDataPlane plane) {
+ return new DsqlService(storage, new RegionResolver(REGION, ACCOUNT), "localhost.floci.io", plane, principals,
+ new IamPolicyEvaluator(mapper), mapper, true);
+ }
+
private DsqlCluster cluster() {
return service.createCluster(REGION, false, null, null, null, null, null, null, false);
}
diff --git a/src/test/java/io/github/hectorvent/floci/services/dsql/dataplane/DsqlSessionTest.java b/src/test/java/io/github/hectorvent/floci/services/dsql/dataplane/DsqlSessionTest.java
new file mode 100644
index 0000000000..e189d79ef2
--- /dev/null
+++ b/src/test/java/io/github/hectorvent/floci/services/dsql/dataplane/DsqlSessionTest.java
@@ -0,0 +1,40 @@
+package io.github.hectorvent.floci.services.dsql.dataplane;
+
+import org.junit.jupiter.api.Test;
+
+import java.util.List;
+import java.util.Map;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+class DsqlSessionTest {
+
+ @Test
+ void startupOptionsCountOnlyTheirFirstDashCSettingForEachName() {
+ Map<String, String> settings = DsqlSession.optionSettings(
+ "--dsql.iam_session_user=spoofed -c search_path=first -csearch_path=x -c Search_Path=second"
+ + " -c TimeZone=UTC -B 100 garbage -c application_name -c");
+
+ assertEquals(Map.of("search_path", "first", "TimeZone", "UTC"), settings);
+ assertEquals(List.of("search_path", "TimeZone"), List.copyOf(settings.keySet()));
+ assertEquals(Map.of(), DsqlSession.optionSettings(null));
+ assertEquals(Map.of(), DsqlSession.optionSettings(" "));
+ }
+
+ @Test
+ void startupOptionsTakeNoEscapesAndReachTheDatabaseEscaped() {
+ Map<String, String> settings = DsqlSession.optionSettings("-c search_path=a\\ b");
+
+ assertEquals(Map.of("search_path", "a\\"), settings);
+ assertEquals("-c search_path=a\\\\", DsqlSession.backendOptions(settings));
+ }
+
+ @Test
+ void aCustomParameterInTheStartupIsTheOneRefused() {
+ assertEquals("myapp.foo", DsqlSession.customParameter(Map.of("myapp.foo", "x"), Map.of("user", "admin")));
+ assertEquals("dsql.iam_session_user", DsqlSession.customParameter(Map.of(),
+ Map.of("user", "admin", "dsql.iam_session_user", "spoofed")));
+ assertEquals(null, DsqlSession.customParameter(Map.of("search_path", "public"),
+ Map.of("user", "admin", "options", "-c search_path=public")));
+ }
+}
diff --git a/src/test/java/io/github/hectorvent/floci/services/rds/proxy/PostgresProtocolHandlerTest.java b/src/test/java/io/github/hectorvent/floci/services/rds/proxy/PostgresProtocolHandlerTest.java
index 3bb7e4d645..4f13a7722b 100644
--- a/src/test/java/io/github/hectorvent/floci/services/rds/proxy/PostgresProtocolHandlerTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/rds/proxy/PostgresProtocolHandlerTest.java
@@ -1,6 +1,7 @@
package io.github.hectorvent.floci.services.rds.proxy;
import io.github.hectorvent.floci.config.FlociCertificateAuthority;
+import io.github.hectorvent.floci.core.common.PostgresWire;
import io.github.hectorvent.floci.services.acm.CertificateGenerator;
import io.github.hectorvent.floci.services.acm.model.KeyAlgorithm;
import io.github.hectorvent.floci.testutil.IamServiceTestHelper;
@@ -1771,7 +1772,7 @@ class PostgresProtocolHandlerTest {
int length = in.readInt();
assertEquals(STARTUP_PROTOCOL_VERSION, in.readInt());
byte[] payload = in.readNBytes(length - 8);
- backendStartup.set(PostgresProtocolHandler.parseStartupParams(payload));
+ backendStartup.set(PostgresWire.startupParameters(payload));
out.writeByte('R');
out.writeInt(8);
@@ -2137,7 +2138,7 @@ class PostgresProtocolHandlerTest {
int proto = in.readInt();
assertEquals(STARTUP_PROTOCOL_VERSION, proto);
byte[] payload = in.readNBytes(length - 8);
- backendStartup.set(PostgresProtocolHandler.parseStartupParams(payload));
+ backendStartup.set(PostgresWire.startupParameters(payload));
backendDatabase.set(backendStartup.get().get("database"));
out.writeByte('R');
diff --git a/src/test/java/io/github/hectorvent/floci/services/redshift/proxy/PostgresWireDecoderTest.java b/src/test/java/io/github/hectorvent/floci/services/redshift/proxy/PostgresWireDecoderTest.java
index 15669188d0..f49ea9feb5 100644
--- a/src/test/java/io/github/hectorvent/floci/services/redshift/proxy/PostgresWireDecoderTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/redshift/proxy/PostgresWireDecoderTest.java
@@ -229,7 +229,8 @@ class PostgresWireDecoderTest {
byte[] hostile = new byte[]{'Q', 0x7F, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF};
PostgresWireDecoder decoder = new PostgresWireDecoder(new ByteArrayInputStream(hostile));
IOException ex = assertThrows(IOException.class, decoder::nextMessage);
- assertTrue(ex.getMessage().contains("Refusing"), ex.getMessage());
+ assertTrue(ex.getMessage().contains("exceeds the " + PostgresWireDecoder.MAX_MESSAGE_BYTES + " byte limit"),
+ ex.getMessage());
}
@Test
diff --git a/src/test/java/io/github/hectorvent/floci/testutil/SigV4TokenTestHelper.java b/src/test/java/io/github/hectorvent/floci/testutil/SigV4TokenTestHelper.java
index c7672093c5..a0df30e7f0 100644
--- a/src/test/java/io/github/hectorvent/floci/testutil/SigV4TokenTestHelper.java
+++ b/src/test/java/io/github/hectorvent/floci/testutil/SigV4TokenTestHelper.java
@@ -116,6 +116,29 @@ public final class SigV4TokenTestHelper {
"rds-db", timestamp, expiresSeconds, params, Map.of("host", host + ":" + port));
}
+ /**
+ * An Aurora DSQL token, shaped as the SDKs' DSQL signers shape it:
+ * {@code <endpoint>/?Action=DbConnectAdmin&...}, signing the endpoint's host without a port.
+ */
+ public static String createDsqlToken(
+ String endpoint,
+ String region,
+ String action,
+ String accessKeyId,
+ String secretKey,
+ Instant timestamp,
+ int expiresSeconds,
+ String sessionToken
+ ) throws Exception {
+ Map<String, String> params = new LinkedHashMap<>();
+ params.put("Action", action);
+ if (sessionToken != null) {
+ params.put("X-Amz-Security-Token", sessionToken);
+ }
+ return signToken(endpoint, null, accessKeyId, secretKey, region, "dsql", timestamp, expiresSeconds,
+ params, Map.of("host", endpoint));
+ }
+
/**
* An RDS token that carries the signer's credential under a percent-encoded parameter name
* ({@code X-Amz%2DCredential}) and, after it, a second {@code X-Amz-Credential} naming
diff --git a/src/test/resources/application.yml b/src/test/resources/application.yml
index 5138a49247..34c85887b8 100644
--- a/src/test/resources/application.yml
+++ b/src/test/resources/application.yml
@@ -232,6 +232,8 @@ floci:
database-resuming-exception-enabled: false
dsql:
enabled: true
+ mock: true
+ port: 0
redshift-data:
enabled: true
result-ttl-hours: 24