Skip to content

armgit / floci

Public Imported from GitHub
0

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

feat(rds): add SQL Server support to RDS (#3752)

Vũ Thế Huya3da2a3767b7
diff --git a/docs/configuration/advanced/application-yml.md b/docs/configuration/advanced/application-yml.md
index 22bef0220f..ac6cf27628 100644
--- a/docs/configuration/advanced/application-yml.md
+++ b/docs/configuration/advanced/application-yml.md
@@ -187,6 +187,7 @@ floci:
# default-postgres-image: "registry.example.com/postgres:16-alpine"
# default-mysql-image: "registry.example.com/mysql:8.0"
# default-mariadb-image: "registry.example.com/mariadb:11"
+ default-sql-server-image: "mcr.microsoft.com/mssql/server:2022-latest"
rds-data:
enabled: true
diff --git a/docs/configuration/environment-variables.md b/docs/configuration/environment-variables.md
index e8fcca283c..c9bd11e0fd 100644
--- a/docs/configuration/environment-variables.md
+++ b/docs/configuration/environment-variables.md
@@ -388,6 +388,7 @@ These services spawn Docker containers. They require access to the Docker socket
| `FLOCI_SERVICES_RDS_DEFAULT_POSTGRES_IMAGE` | `postgres:16-alpine` | Default PostgreSQL Docker image |
| `FLOCI_SERVICES_RDS_DEFAULT_MYSQL_IMAGE` | `mysql:8.0` | Default MySQL Docker image |
| `FLOCI_SERVICES_RDS_DEFAULT_MARIADB_IMAGE` | `mariadb:11` | Default MariaDB Docker image |
+| `FLOCI_SERVICES_RDS_DEFAULT_SQL_SERVER_IMAGE` | `mcr.microsoft.com/mssql/server:2022-latest` | Default SQL Server Docker image |
| `FLOCI_SERVICES_RDS_DOCKER_NETWORK` | _(none)_ | Docker network for RDS containers (overrides `FLOCI_SERVICES_DOCKER_NETWORK`) |
| `FLOCI_SERVICES_RDS_DATA_ENABLED` | `true` | Enable the RDS Data API service. Requires `FLOCI_SERVICES_RDS_ENABLED=true` |
| `FLOCI_SERVICES_RDS_DATA_TRANSACTION_TTL_SECONDS` | `180` | Idle timeout, in seconds, before leaked RDS Data API transactions expire |
diff --git a/docs/services/rds.md b/docs/services/rds.md
index 651142212f..cc2be4d766 100644
--- a/docs/services/rds.md
+++ b/docs/services/rds.md
@@ -1,10 +1,10 @@
# RDS
-**Protocol:** Query (XML) for management API + PostgreSQL / MySQL wire protocol for data plane
+**Protocol:** Query (XML) for management API + PostgreSQL / MySQL / SQL Server wire protocol for data plane
**Management Endpoint:** `POST http://localhost:4566/`
**Data Endpoint:** `localhost:<proxy-port>` (TCP)
-Floci manages real PostgreSQL, MySQL, and MariaDB Docker containers and proxies TCP connections to them, including IAM authentication support.
+Floci manages real PostgreSQL, MySQL, MariaDB, and SQL Server Docker containers and proxies TCP connections to them, including IAM authentication support where the protocol supports it. SQL Server uses a transparent TCP relay for its native TDS protocol.
RDS Data API (`rds-data`) is documented separately because it uses REST JSON routes instead of the RDS Query protocol. See [RDS Data API](rds-data.md).
@@ -110,6 +110,7 @@ checked against the instance's other window. Modifications apply immediately —
| `FLOCI_SERVICES_RDS_DEFAULT_POSTGRES_IMAGE` | `postgres:16-alpine` | Docker image for PostgreSQL instances |
| `FLOCI_SERVICES_RDS_DEFAULT_MYSQL_IMAGE` | `mysql:8.0` | Docker image for MySQL instances |
| `FLOCI_SERVICES_RDS_DEFAULT_MARIADB_IMAGE` | `mariadb:11` | Docker image for MariaDB instances |
+| `FLOCI_SERVICES_RDS_DEFAULT_SQL_SERVER_IMAGE` | `mcr.microsoft.com/mssql/server:2022-latest` | Docker image for SQL Server instances |
| `FLOCI_SERVICES_RDS_PROXY_HANDSHAKE_TIMEOUT_MILLIS` | `10000` | Max time a client has to complete the startup/auth handshake before the proxy drops it |
| `FLOCI_SERVICES_RDS_PROXY_BACKEND_CONNECT_TIMEOUT_MILLIS` | `5000` | Max time the proxy waits for the backend TCP connect |
| `FLOCI_SERVICES_RDS_PROXY_MAX_CONNECTIONS` | `100` | Max concurrent connections per proxy before new ones are refused |
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 407855c2ae..0bb1233d12 100644
--- a/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.java
+++ b/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.java
@@ -1444,6 +1444,10 @@ public interface EmulatorConfig {
/** Empty when Floci should adapt its built-in image to the requested engine version. */
Optional<String> defaultMariadbImage();
+ /** Docker image used for SQL Server instances when no override is configured. */
+ @WithDefault("mcr.microsoft.com/mssql/server:2022-latest")
+ String defaultSqlServerImage();
+
/** Hostname advertised for RDS endpoints. Uses published Docker ports when configured. */
Optional<String> endpointHost();
diff --git a/src/main/java/io/github/hectorvent/floci/services/rds/RdsQueryHandler.java b/src/main/java/io/github/hectorvent/floci/services/rds/RdsQueryHandler.java
index 39a3ad0a6f..e4ae7e8fc7 100644
--- a/src/main/java/io/github/hectorvent/floci/services/rds/RdsQueryHandler.java
+++ b/src/main/java/io/github/hectorvent/floci/services/rds/RdsQueryHandler.java
@@ -441,7 +441,7 @@ public class RdsQueryHandler {
.elem("Engine", option.get("engine"))
.elem("EngineVersion", option.get("engineVersion"))
.elem("DBInstanceClass", option.get("dbInstanceClass"))
- .elem("LicenseModel", "postgresql-license")
+ .elem("LicenseModel", licenseModel(option.get("engine")))
.start("AvailabilityZones")
.start("AvailabilityZone")
.elem("Name", config.defaultAvailabilityZone())
@@ -454,6 +454,13 @@ public class RdsQueryHandler {
AwsNamespaces.RDS, xml.build())).build();
}
+ private static String licenseModel(String engine) {
+ if (engine != null && engine.regionMatches(true, 0, "sqlserver", 0, 9)) {
+ return "license-included";
+ }
+ return "postgresql-license";
+ }
+
private Response handleAddTagsToResource(MultivaluedMap<String, String> params, String region) {
String resourceName = params.getFirst("ResourceName");
try {
@@ -2381,6 +2388,7 @@ public class RdsQueryHandler {
case "postgres", "aurora-postgresql" -> "16.3";
case "mysql", "aurora-mysql", "aurora" -> "8.0.36";
case "mariadb" -> "11.2";
+ case "sqlserver-ee", "sqlserver-se", "sqlserver-ex", "sqlserver-web" -> "15.00";
default -> "1.0";
};
}
diff --git a/src/main/java/io/github/hectorvent/floci/services/rds/RdsService.java b/src/main/java/io/github/hectorvent/floci/services/rds/RdsService.java
index eb2c4d846c..2dbcdf716a 100644
--- a/src/main/java/io/github/hectorvent/floci/services/rds/RdsService.java
+++ b/src/main/java/io/github/hectorvent/floci/services/rds/RdsService.java
@@ -598,6 +598,10 @@ public class RdsService implements Resettable, ResourceProvider {
}
DatabaseEngine engine = resolveEngine(engineParam);
+ if (engine == DatabaseEngine.SQLSERVER && dbName != null && !dbName.isBlank()) {
+ throw new AwsException("InvalidParameterCombination",
+ "DBName must be null for SQL Server.", 400);
+ }
if (dbSubnetGroupName != null && !dbSubnetGroupName.isBlank() && !"default".equalsIgnoreCase(dbSubnetGroupName)) {
getDbSubnetGroup(dbSubnetGroupName, effectiveRegion);
}
@@ -2004,7 +2008,8 @@ public class RdsService implements Resettable, ResourceProvider {
Map.of("engine", "postgres", "engineVersion", "16.14", "dbInstanceClass", "db.t4g.small"),
Map.of("engine", "postgres", "engineVersion", "16.3", "dbInstanceClass", "db.t4g.medium"),
Map.of("engine", "mysql", "engineVersion", "8.0", "dbInstanceClass", "db.t3.micro"),
- Map.of("engine", "mariadb", "engineVersion", "11", "dbInstanceClass", "db.t3.micro")
+ Map.of("engine", "mariadb", "engineVersion", "11", "dbInstanceClass", "db.t3.micro"),
+ Map.of("engine", "sqlserver-se", "engineVersion", "15.00", "dbInstanceClass", "db.t3.micro")
);
return options.stream()
.filter(option -> engine == null || engine.isBlank() || engine.equalsIgnoreCase(option.get("engine")))
@@ -4425,6 +4430,7 @@ public class RdsService implements Resettable, ResourceProvider {
// to the default case below instead of silently becoming aurora-mysql.
case "mysql", "aurora-mysql" -> DatabaseEngine.MYSQL;
case "mariadb" -> DatabaseEngine.MARIADB;
+ case "sqlserver-ee", "sqlserver-se", "sqlserver-ex", "sqlserver-web" -> DatabaseEngine.SQLSERVER;
default -> throw new AwsException("InvalidParameterValue", invalidParameterValueMessage(), 400);
};
}
@@ -4440,6 +4446,7 @@ public class RdsService implements Resettable, ResourceProvider {
case MARIADB -> config.services().rds().defaultMariadbImage()
.orElseGet(() -> imageForRequestedVersion(
EmulatorConfig.RdsServiceConfig.DEFAULT_MARIADB_IMAGE, engineVersion));
+ case SQLSERVER -> config.services().rds().defaultSqlServerImage();
};
}
@@ -4473,6 +4480,7 @@ public class RdsService implements Resettable, ResourceProvider {
case "postgres", "aurora-postgresql" -> "16.3";
case "mysql", "aurora", "aurora-mysql" -> "8.0.36";
case "mariadb" -> "11.2";
+ case "sqlserver-ee", "sqlserver-se", "sqlserver-ex", "sqlserver-web" -> "15.00";
default -> throw new AwsException("InvalidParameterValue", invalidParameterValueMessage(), 400);
};
}
@@ -4490,6 +4498,7 @@ public class RdsService implements Resettable, ResourceProvider {
}
yield versionParts[0] + "." + versionParts[1];
}
+ case "sqlserver-ee", "sqlserver-se", "sqlserver-ex", "sqlserver-web" -> versionParts[0];
default -> throw new AwsException("InvalidParameterValue", invalidParameterValueMessage(), 400);
};
return expectedFamilyPrefix(normalizedEngine) + familyVersion;
@@ -4511,6 +4520,7 @@ public class RdsService implements Resettable, ResourceProvider {
case "mysql" -> "mysql";
case "aurora", "aurora-mysql" -> "aurora-mysql";
case "mariadb" -> "mariadb";
+ case "sqlserver-ee", "sqlserver-se", "sqlserver-ex", "sqlserver-web" -> "sqlserver";
default -> throw new AwsException("InvalidParameterValue", invalidParameterValueMessage(), 400);
};
}
diff --git a/src/main/java/io/github/hectorvent/floci/services/rds/container/RdsContainerManager.java b/src/main/java/io/github/hectorvent/floci/services/rds/container/RdsContainerManager.java
index 88215f63f2..967fd80fc0 100644
--- a/src/main/java/io/github/hectorvent/floci/services/rds/container/RdsContainerManager.java
+++ b/src/main/java/io/github/hectorvent/floci/services/rds/container/RdsContainerManager.java
@@ -260,7 +260,7 @@ public class RdsContainerManager {
EndpointInfo endpoint = info.getEndpoint(enginePort);
LOG.infov("RDS backend for instance {0}: {1}", instanceId, endpoint);
- initializeEngine(containerName, info.containerId(), engine, masterUsername);
+ initializeEngine(containerName, info.containerId(), engine, masterUsername, masterPassword, dbName);
handle = new RdsContainerHandle(
info.containerId(), effectiveRuntimeId, instanceId,
@@ -434,6 +434,7 @@ public class RdsContainerManager {
return switch (engine) {
case POSTGRES -> postgresDataPath(image);
case MYSQL, MARIADB -> "/var/lib/mysql";
+ case SQLSERVER -> "/var/opt/mssql";
};
}
@@ -469,10 +470,47 @@ public class RdsContainerManager {
return Integer.parseInt(tag.substring(0, end));
}
- private void initializeEngine(String containerName, String containerId, DatabaseEngine engine, String masterUsername) {
- if (engine == DatabaseEngine.POSTGRES) {
- initializePostgresIamRole(containerName, containerId, masterUsername);
+ private void initializeEngine(String containerName, String containerId, DatabaseEngine engine,
+ String masterUsername, String masterPassword, String dbName) {
+ switch (engine) {
+ case POSTGRES -> initializePostgresIamRole(containerName, containerId, masterUsername);
+ case SQLSERVER -> initializeSqlServerMaster(containerName, containerId,
+ masterUsername, masterPassword);
+ case MYSQL, MARIADB -> {
+ }
+ }
+ }
+
+ private void initializeSqlServerMaster(String containerName, String containerId,
+ String masterUsername, String masterPassword) {
+ String effectiveUser = (masterUsername == null || masterUsername.isBlank()) ? "sa" : masterUsername;
+ if ("sa".equalsIgnoreCase(effectiveUser)) {
+ return;
}
+ String[] cmd = {
+ "/opt/mssql-tools18/bin/sqlcmd", "-S", "127.0.0.1", "-C",
+ "-U", "sa", "-P", masterPassword, "-Q",
+ sqlServerMasterLoginSql(effectiveUser, masterPassword)
+ };
+ execUntilSuccess(containerName, containerId, cmd, "SQL Server master login");
+ }
+
+ static String sqlServerMasterLoginSql(String masterUsername, String masterPassword) {
+ if ("sa".equalsIgnoreCase(masterUsername)) {
+ return "";
+ }
+ return "IF SUSER_ID(N'" + sqlServerLiteral(masterUsername) + "') IS NULL BEGIN "
+ + "CREATE LOGIN " + sqlServerIdentifier(masterUsername)
+ + " WITH PASSWORD = N'" + sqlServerLiteral(masterPassword) + "'; END;";
+ }
+
+ private static String sqlServerIdentifier(String value) {
+ String escaped = value.replace("]", "]]");
+ return "[" + escaped + "]";
+ }
+
+ private static String sqlServerLiteral(String value) {
+ return value == null ? "" : value.replace("'", "''");
}
static String postgresIamRoleInitSql() {
@@ -510,8 +548,10 @@ public class RdsContainerManager {
* container, so the backend's stored credential matches what clients (and the auth proxy) use
* from now on. For MySQL/MariaDB this runs as the master user itself with the old password —
* changing your own password needs no extra privileges, and the container's root password is
- * the creation-time one, not reliably known after earlier rotations. PostgreSQL's official
- * image trusts local socket connections, so psql needs no password at all.
+ * the creation-time one, not reliably known after earlier rotations. SQL Server uses the
+ * effective master login and its old password, so repeated rotations do not depend on the
+ * unrelated {@code sa} credential. PostgreSQL's official image trusts local socket
+ * connections, so psql needs no password at all.
*/
public void rotateMasterPassword(String containerName, String containerId, DatabaseEngine engine,
String masterUsername, String oldPassword, String newPassword) {
@@ -528,6 +568,16 @@ public class RdsContainerManager {
return new String[]{"psql", "-v", "ON_ERROR_STOP=1", "-U", effectiveUser, "-d", "postgres",
"-c", postgresPasswordRotationSql(effectiveUser, newPassword)};
}
+ if (engine == DatabaseEngine.SQLSERVER) {
+ String effectiveUser = (masterUsername != null && !masterUsername.isBlank())
+ ? masterUsername : "sa";
+ return new String[]{
+ "/opt/mssql-tools18/bin/sqlcmd", "-S", "127.0.0.1", "-C",
+ "-U", effectiveUser, "-P", oldPassword, "-Q",
+ "ALTER LOGIN " + sqlServerIdentifier(effectiveUser)
+ + " WITH PASSWORD = N'" + sqlServerLiteral(newPassword)
+ + "' OLD_PASSWORD = N'" + sqlServerLiteral(oldPassword) + "';"};
+ }
String effectiveUser = (masterUsername != null && !masterUsername.isBlank())
? masterUsername : "root";
// MariaDB images ≥10.4 (Floci's floor is 10.11) ship only the `mariadb` client binary.
@@ -883,6 +933,10 @@ public class RdsContainerManager {
}
envs.add("MARIADB_DATABASE=" + effectiveDb);
}
+ case SQLSERVER -> {
+ envs.add("ACCEPT_EULA=Y");
+ envs.add("MSSQL_SA_PASSWORD=" + masterPassword);
+ }
}
return envs;
}
@@ -904,7 +958,7 @@ public class RdsContainerManager {
"--authentication-policy=mysql_native_password")
: List.of("--default-authentication-plugin=mysql_native_password");
}
- case POSTGRES, MARIADB -> List.of();
+ case POSTGRES, MARIADB, SQLSERVER -> List.of();
};
}
diff --git a/src/main/java/io/github/hectorvent/floci/services/rds/model/DatabaseEngine.java b/src/main/java/io/github/hectorvent/floci/services/rds/model/DatabaseEngine.java
index 29b2343802..0c23c44375 100644
--- a/src/main/java/io/github/hectorvent/floci/services/rds/model/DatabaseEngine.java
+++ b/src/main/java/io/github/hectorvent/floci/services/rds/model/DatabaseEngine.java
@@ -4,12 +4,13 @@ import io.quarkus.runtime.annotations.RegisterForReflection;
@RegisterForReflection
public enum DatabaseEngine {
- POSTGRES, MYSQL, MARIADB;
+ POSTGRES, MYSQL, MARIADB, SQLSERVER;
public int defaultPort() {
return switch (this) {
case POSTGRES -> 5432;
case MYSQL, MARIADB -> 3306;
+ case SQLSERVER -> 1433;
};
}
@@ -19,7 +20,7 @@ public enum DatabaseEngine {
return switch (this) {
case POSTGRES -> 63;
case MYSQL -> 32;
- case MARIADB -> 16;
+ case MARIADB, SQLSERVER -> 16;
};
}
}
diff --git a/src/main/java/io/github/hectorvent/floci/services/rds/proxy/RdsAuthProxy.java b/src/main/java/io/github/hectorvent/floci/services/rds/proxy/RdsAuthProxy.java
index 3221da7980..641ca2f045 100644
--- a/src/main/java/io/github/hectorvent/floci/services/rds/proxy/RdsAuthProxy.java
+++ b/src/main/java/io/github/hectorvent/floci/services/rds/proxy/RdsAuthProxy.java
@@ -191,6 +191,10 @@ public class RdsAuthProxy {
handshakeTimeoutMillis,
iamUserChecker, mysqlBinding);
}
+ case SQLSERVER -> {
+ backend = connector.connect();
+ TcpStreamBridge.relay(client, backend);
+ }
}
} catch (Exception e) {
LOG.debugv("RDS connection error for instance {0}: {1}", instanceId, e.getMessage());
diff --git a/src/main/java/io/github/hectorvent/floci/services/rds/proxy/TcpStreamBridge.java b/src/main/java/io/github/hectorvent/floci/services/rds/proxy/TcpStreamBridge.java
new file mode 100644
index 0000000000..1303188058
--- /dev/null
+++ b/src/main/java/io/github/hectorvent/floci/services/rds/proxy/TcpStreamBridge.java
@@ -0,0 +1,52 @@
+package io.github.hectorvent.floci.services.rds.proxy;
+
+import org.jboss.logging.Logger;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.net.Socket;
+import java.util.concurrent.CountDownLatch;
+
+/** Relays an opaque database protocol between an RDS client and its backend. */
+final class TcpStreamBridge {
+
+ private static final Logger LOG = Logger.getLogger(TcpStreamBridge.class);
+
+ private TcpStreamBridge() {
+ }
+
+ static void relay(Socket client, Socket backend) {
+ CountDownLatch directions = new CountDownLatch(2);
+ Thread.ofVirtual().start(() -> copy(client, backend, directions));
+ Thread.ofVirtual().start(() -> copy(backend, client, directions));
+ try {
+ directions.await();
+ } catch (InterruptedException e) {
+ Thread.currentThread().interrupt();
+ LOG.debug("Opaque database relay interrupted", e);
+ }
+ }
+
+ private static void copy(Socket source, Socket target, CountDownLatch directions) {
+ try {
+ InputStream input = source.getInputStream();
+ OutputStream output = target.getOutputStream();
+ input.transferTo(output);
+ output.flush();
+ } catch (IOException e) {
+ LOG.debugv("Opaque database relay closed: {0}", e.getMessage());
+ } finally {
+ directions.countDown();
+ closeOutput(target);
+ }
+ }
+
+ private static void closeOutput(Socket socket) {
+ try {
+ socket.shutdownOutput();
+ } catch (IOException e) {
+ LOG.debugv(e, "Failed to half-close opaque database relay socket: {0}", e.getMessage());
+ }
+ }
+}
diff --git a/src/main/java/io/github/hectorvent/floci/services/rdsdata/RdsDataConnectionFactory.java b/src/main/java/io/github/hectorvent/floci/services/rdsdata/RdsDataConnectionFactory.java
index 0f8eec31a6..8d85bb53c2 100644
--- a/src/main/java/io/github/hectorvent/floci/services/rdsdata/RdsDataConnectionFactory.java
+++ b/src/main/java/io/github/hectorvent/floci/services/rdsdata/RdsDataConnectionFactory.java
@@ -35,6 +35,8 @@ class RdsDataConnectionFactory {
+ "?useSSL=false&allowPublicKeyRetrieval=true";
case POSTGRES -> "jdbc:postgresql://" + host + ":" + port + "/" + database
+ "?sslmode=disable";
+ case SQLSERVER -> throw new IllegalArgumentException(
+ "RDS Data API is not supported for SQL Server");
};
}
@@ -43,6 +45,7 @@ class RdsDataConnectionFactory {
return switch (engine) {
case MYSQL, MARIADB -> "5000";
case POSTGRES -> "5";
+ case SQLSERVER -> "5000";
};
}
}
diff --git a/src/main/java/io/github/hectorvent/floci/services/rdsdata/RdsDataService.java b/src/main/java/io/github/hectorvent/floci/services/rdsdata/RdsDataService.java
index dabbcf201f..c671b75d94 100644
--- a/src/main/java/io/github/hectorvent/floci/services/rdsdata/RdsDataService.java
+++ b/src/main/java/io/github/hectorvent/floci/services/rdsdata/RdsDataService.java
@@ -395,7 +395,7 @@ public class RdsDataService implements Resettable {
private static boolean returnsGeneratedKeys(DatabaseEngine engine) {
return switch (engine) {
case MYSQL, MARIADB -> true;
- case POSTGRES -> false;
+ case POSTGRES, SQLSERVER -> false;
};
}
@@ -407,7 +407,7 @@ public class RdsDataService implements Resettable {
private static boolean usesBackslashEscapes(DatabaseEngine engine) {
return switch (engine) {
case MYSQL, MARIADB -> true;
- case POSTGRES -> false;
+ case POSTGRES, SQLSERVER -> false;
};
}
diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml
index c6e06a84e6..d31c6b03ba 100644
--- a/src/main/resources/application.yml
+++ b/src/main/resources/application.yml
@@ -403,6 +403,7 @@ floci:
# default-postgres-image: "registry.example.com/postgres:16-alpine"
# default-mysql-image: "registry.example.com/mysql:8.0"
# default-mariadb-image: "registry.example.com/mariadb:11"
+ default-sql-server-image: "mcr.microsoft.com/mssql/server:2022-latest"
proxy-handshake-timeout-millis: 10000 # Max time a client has to complete the startup/auth handshake
proxy-backend-connect-timeout-millis: 5000 # Max time to wait for the backend TCP connect
proxy-max-connections: 100 # Max concurrent proxy connections before new ones are refused
diff --git a/src/test/java/io/github/hectorvent/floci/services/rds/RdsAwsIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/rds/RdsAwsIntegrationTest.java
index 5a26ff7af4..5bad22e150 100644
--- a/src/test/java/io/github/hectorvent/floci/services/rds/RdsAwsIntegrationTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/rds/RdsAwsIntegrationTest.java
@@ -1,14 +1,27 @@
package io.github.hectorvent.floci.services.rds;
import com.github.dockerjava.api.DockerClient;
+import com.github.dockerjava.api.model.Frame;
+import com.github.dockerjava.api.model.StreamType;
+import com.github.dockerjava.core.command.ExecStartResultCallback;
+import io.github.hectorvent.floci.services.rds.model.DbInstance;
import io.quarkus.test.junit.QuarkusTest;
import jakarta.inject.Inject;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
+
+import java.io.ByteArrayOutputStream;
+import java.io.Closeable;
+import java.io.IOException;
+import java.nio.charset.StandardCharsets;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.containsString;
import static org.junit.jupiter.api.Assumptions.assumeTrue;
+import static org.junit.jupiter.api.Assertions.assertTrue;
@QuarkusTest
@Tag("docker")
@@ -19,6 +32,9 @@ class RdsAwsIntegrationTest {
@Inject
DockerClient dockerClient;
+ @Inject
+ RdsService rdsService;
+
@BeforeEach
void checkDocker() {
try {
@@ -93,6 +109,66 @@ class RdsAwsIntegrationTest {
.body(containsString(restoreId));
}
+ @Test
+ void sqlServerCustomMasterRotatesPasswordTwice() throws Exception {
+ String suffix = Long.toString(System.nanoTime(), 36);
+ String dbId = "test-sqlserver-" + suffix;
+ String username = "flociadmin";
+ String firstPassword = "Password123!";
+ String secondPassword = "Password456!";
+ String finalPassword = "Password789!";
+
+ try {
+ given()
+ .header("Authorization", RDS_AUTH)
+ .formParam("Action", "CreateDBInstance")
+ .formParam("DBInstanceIdentifier", dbId)
+ .formParam("Engine", "sqlserver-se")
+ .formParam("MasterUsername", username)
+ .formParam("MasterUserPassword", firstPassword)
+ .formParam("DBInstanceClass", "db.t3.micro")
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200)
+ .body(containsString(dbId));
+
+ waitForDb(dbId);
+ DbInstance instance = rdsService.getDbInstance(dbId);
+ assertTrue(sqlServerQuery(instance.getContainerId(), username, firstPassword).contains("1"));
+
+ given()
+ .header("Authorization", RDS_AUTH)
+ .formParam("Action", "ModifyDBInstance")
+ .formParam("DBInstanceIdentifier", dbId)
+ .formParam("MasterUserPassword", secondPassword)
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200);
+
+ given()
+ .header("Authorization", RDS_AUTH)
+ .formParam("Action", "ModifyDBInstance")
+ .formParam("DBInstanceIdentifier", dbId)
+ .formParam("MasterUserPassword", finalPassword)
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200);
+
+ assertTrue(sqlServerQuery(instance.getContainerId(), username, finalPassword).contains("1"));
+ } finally {
+ given()
+ .header("Authorization", RDS_AUTH)
+ .formParam("Action", "DeleteDBInstance")
+ .formParam("DBInstanceIdentifier", dbId)
+ .formParam("SkipFinalSnapshot", "true")
+ .when()
+ .post("/");
+ }
+ }
+
private void waitForDb(String dbId) throws Exception {
for (int i = 0; i < 60; i++) {
String status = given()
@@ -111,4 +187,54 @@ class RdsAwsIntegrationTest {
}
throw new RuntimeException("DB instance " + dbId + " did not become available.");
}
+
+ private String sqlServerQuery(String containerId, String username, String password) throws Exception {
+ String[] command = {
+ "/opt/mssql-tools18/bin/sqlcmd", "-S", "127.0.0.1", "-C",
+ "-U", username, "-P", password, "-Q", "SET NOCOUNT ON; SELECT 1;", "-h", "-1", "-W"
+ };
+ String execId = dockerClient.execCreateCmd(containerId)
+ .withCmd(command)
+ .withAttachStdout(true)
+ .withAttachStderr(true)
+ .exec()
+ .getId();
+ ByteArrayOutputStream output = new ByteArrayOutputStream();
+ ByteArrayOutputStream errors = new ByteArrayOutputStream();
+ CountDownLatch latch = new CountDownLatch(1);
+ Closeable callback = dockerClient.execStartCmd(execId).exec(new ExecStartResultCallback() {
+ @Override
+ public void onNext(Frame frame) {
+ try {
+ if (frame.getStreamType() == StreamType.STDOUT) {
+ output.write(frame.getPayload());
+ } else if (frame.getStreamType() == StreamType.STDERR) {
+ errors.write(frame.getPayload());
+ }
+ } catch (IOException e) {
+ throw new IllegalStateException("Failed to capture SQL Server output", e);
+ }
+ }
+
+ @Override
+ public void onComplete() {
+ latch.countDown();
+ }
+
+ @Override
+ public void onError(Throwable throwable) {
+ latch.countDown();
+ }
+ });
+ try {
+ assumeTrue(latch.await(30, TimeUnit.SECONDS), "Timed out running sqlcmd");
+ long exitCode = dockerClient.inspectExecCmd(execId).exec().getExitCodeLong();
+ assertTrue(exitCode == 0,
+ "sqlcmd failed with exit code " + exitCode + ": "
+ + errors.toString(StandardCharsets.UTF_8));
+ return output.toString(StandardCharsets.UTF_8);
+ } finally {
+ callback.close();
+ }
+ }
}
diff --git a/src/test/java/io/github/hectorvent/floci/services/rds/RdsServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/rds/RdsServiceTest.java
index 0af21f544c..21256ff917 100644
--- a/src/test/java/io/github/hectorvent/floci/services/rds/RdsServiceTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/rds/RdsServiceTest.java
@@ -66,6 +66,7 @@ import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyList;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.ArgumentMatchers.isNull;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
@@ -75,6 +76,7 @@ import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.Mockito.when;
class RdsServiceTest {
@@ -140,6 +142,7 @@ class RdsServiceTest {
when(rdsConfig.defaultPostgresImage()).thenReturn(Optional.empty());
when(rdsConfig.defaultMysqlImage()).thenReturn(Optional.empty());
when(rdsConfig.defaultMariadbImage()).thenReturn(Optional.empty());
+ when(rdsConfig.defaultSqlServerImage()).thenReturn("mcr.microsoft.com/mssql/server:2022-latest");
rdsService = newService(containerManager, proxyManager,
new InMemoryStorage<>(), new InMemoryStorage<>(),
@@ -177,6 +180,30 @@ class RdsServiceTest {
assertEquals("arn:aws:rds:us-east-1:123456789012:db:mydb", instance.getDbInstanceArn());
}
+ @Test
+ void createDbInstanceSupportsSqlServerEngineIdentifiers() {
+ DbInstance instance = rdsService.createDbInstance("sqlserver-db", "sqlserver-se", "15.00",
+ "sa", "Password123!", null, "db.t3.micro",
+ 20, false, null, null, null, null, false);
+
+ assertEquals(DatabaseEngine.SQLSERVER, instance.getEngine());
+ assertEquals("sqlserver-se", instance.getEngineIdentifier());
+ verify(containerManager).tryStart(any(), any(), any(), any(), eq(DatabaseEngine.SQLSERVER),
+ eq("mcr.microsoft.com/mssql/server:2022-latest"), eq("sa"), eq("Password123!"), isNull());
+ }
+
+ @Test
+ void createDbInstanceRejectsDbNameForSqlServer() {
+ AwsException exception = assertThrows(AwsException.class, () ->
+ rdsService.createDbInstance("sqlserver-db", "sqlserver-se", "15.00",
+ "sa", "Password123!", "app", "db.t3.micro",
+ 20, false, null, null, null, null, false));
+
+ assertEquals("InvalidParameterCombination", exception.getErrorCode());
+ assertEquals("DBName must be null for SQL Server.", exception.getMessage());
+ verifyNoInteractions(containerManager);
+ }
+
@Test
void createDbInstanceMakesRequestedPasswordValidBeforeProxyStartsAcceptingConnections() {
doAnswer(invocation -> {
diff --git a/src/test/java/io/github/hectorvent/floci/services/rds/container/RdsContainerManagerTest.java b/src/test/java/io/github/hectorvent/floci/services/rds/container/RdsContainerManagerTest.java
index b49838e6f7..65bcca44a0 100644
--- a/src/test/java/io/github/hectorvent/floci/services/rds/container/RdsContainerManagerTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/rds/container/RdsContainerManagerTest.java
@@ -73,6 +73,53 @@ class RdsContainerManagerTest {
DatabaseEngine.POSTGRES, "postgres:18").isEmpty());
assertTrue(RdsContainerManager.buildContainerCmd(
DatabaseEngine.MARIADB, "mariadb:11").isEmpty());
+ assertTrue(RdsContainerManager.buildContainerCmd(
+ DatabaseEngine.SQLSERVER, "mcr.microsoft.com/mssql/server:2022-latest").isEmpty());
+ }
+
+ @Test
+ void sqlServerUsesPersistentDataDirectory() {
+ assertEquals("/var/opt/mssql",
+ RdsContainerManager.engineDefaultDataPath(DatabaseEngine.SQLSERVER,
+ "mcr.microsoft.com/mssql/server:2022-latest"));
+ }
+
+ @Test
+ void sqlServerContainerUsesOfficialImageEnvironmentAndPort() {
+ EmulatorConfig config = config(tempDir.resolve("sqlserver-root"));
+ ContainerLifecycleManager lifecycleManager = mock(ContainerLifecycleManager.class);
+ stubStarts(lifecycleManager, new ContainerLifecycleManager.ContainerInfo(
+ "sqlserver-container", Map.of(1433,
+ new ContainerLifecycleManager.EndpointInfo("db1", 1433))));
+ ContainerDetector detector = mock(ContainerDetector.class);
+ when(detector.isRunningInContainer()).thenReturn(false);
+ ContainerLogStreamer logStreamer = mock(ContainerLogStreamer.class);
+ lenient().when(logStreamer.generateLogStreamName(any())).thenReturn("log-stream");
+
+ RdsContainerManager manager = new RdsContainerManager(
+ new ContainerBuilder(config, mock(DockerHostResolver.class), mock(EmbeddedDnsServer.class)),
+ lifecycleManager, logStreamer, detector, config,
+ new RegionResolver("us-east-1", "000000000000"), mock(ServiceConfigAccess.class));
+
+ manager.start("db1", "vol1", DatabaseEngine.SQLSERVER,
+ "mcr.microsoft.com/mssql/server:2022-latest", "sa", "Password123!", null);
+
+ org.mockito.ArgumentCaptor<ContainerSpec> spec = org.mockito.ArgumentCaptor.forClass(ContainerSpec.class);
+ verify(lifecycleManager).create(spec.capture());
+ assertEquals("mcr.microsoft.com/mssql/server:2022-latest", spec.getValue().image());
+ assertTrue(spec.getValue().env().contains("ACCEPT_EULA=Y"));
+ assertTrue(spec.getValue().env().contains("MSSQL_SA_PASSWORD=Password123!"));
+ assertEquals(1433, spec.getValue().portBindings().keySet().iterator().next());
+ assertEquals("/var/opt/mssql", spec.getValue().binds().getFirst().getVolume().getPath());
+ }
+
+ @Test
+ void sqlServerMasterLoginDoesNotCreateDatabaseOrGrantSysadmin() {
+ String sql = RdsContainerManager.sqlServerMasterLoginSql("admin", "Password123!");
+
+ assertTrue(sql.contains("CREATE LOGIN [admin]"));
+ assertFalse(sql.contains("CREATE DATABASE"));
+ assertFalse(sql.contains("sysadmin"));
}
@Test
@@ -128,6 +175,12 @@ class RdsContainerManagerTest {
DatabaseEngine.POSTGRES, "admin", "old-pass", "new-pass");
assertEquals("psql", postgres[0]);
assertEquals("ALTER ROLE \"admin\" WITH PASSWORD 'new-pass';", postgres[postgres.length - 1]);
+
+ String[] sqlServer = RdsContainerManager.passwordRotationCommand(
+ DatabaseEngine.SQLSERVER, "admin", "old-pass", "new-pass");
+ assertEquals("admin", sqlServer[5]);
+ assertEquals("old-pass", sqlServer[7]);
+ assertTrue(sqlServer[9].contains("ALTER LOGIN [admin]"));
}
@Test
diff --git a/src/test/java/io/github/hectorvent/floci/services/rds/model/DatabaseEngineTest.java b/src/test/java/io/github/hectorvent/floci/services/rds/model/DatabaseEngineTest.java
new file mode 100644
index 0000000000..89f4eccafd
--- /dev/null
+++ b/src/test/java/io/github/hectorvent/floci/services/rds/model/DatabaseEngineTest.java
@@ -0,0 +1,14 @@
+package io.github.hectorvent.floci.services.rds.model;
+
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+class DatabaseEngineTest {
+
+ @Test
+ void sqlServerUsesRdsPortAndMasterUsernameLimit() {
+ assertEquals(1433, DatabaseEngine.SQLSERVER.defaultPort());
+ assertEquals(16, DatabaseEngine.SQLSERVER.maxMasterUsernameLength());
+ }
+}
diff --git a/src/test/java/io/github/hectorvent/floci/services/rds/proxy/TcpStreamBridgeTest.java b/src/test/java/io/github/hectorvent/floci/services/rds/proxy/TcpStreamBridgeTest.java
new file mode 100644
index 0000000000..6b5f107620
--- /dev/null
+++ b/src/test/java/io/github/hectorvent/floci/services/rds/proxy/TcpStreamBridgeTest.java
@@ -0,0 +1,40 @@
+package io.github.hectorvent.floci.services.rds.proxy;
+
+import org.junit.jupiter.api.Test;
+
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.net.ServerSocket;
+import java.net.Socket;
+import java.nio.charset.StandardCharsets;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+class TcpStreamBridgeTest {
+
+ @Test
+ void relaysBytesInBothDirections() throws Exception {
+ try (ServerSocket listener = new ServerSocket(0);
+ Socket client = new Socket("127.0.0.1", listener.getLocalPort());
+ Socket backend = listener.accept()) {
+ Thread relay = Thread.ofVirtual().start(() -> TcpStreamBridge.relay(client, backend));
+ OutputStream clientOutput = client.getOutputStream();
+ InputStream clientInput = client.getInputStream();
+ OutputStream backendOutput = backend.getOutputStream();
+ InputStream backendInput = backend.getInputStream();
+
+ clientOutput.write("client-to-server".getBytes(StandardCharsets.UTF_8));
+ clientOutput.flush();
+ byte[] fromClient = backendInput.readNBytes(16);
+ backendOutput.write("server-to-client".getBytes(StandardCharsets.UTF_8));
+ backendOutput.flush();
+ byte[] fromBackend = clientInput.readNBytes(16);
+
+ assertEquals("client-to-server", new String(fromClient, StandardCharsets.UTF_8));
+ assertEquals("server-to-client", new String(fromBackend, StandardCharsets.UTF_8));
+ client.shutdownOutput();
+ backend.shutdownOutput();
+ relay.join();
+ }
+ }
+}
diff --git a/src/test/java/io/github/hectorvent/floci/services/rdsdata/RdsDataServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/rdsdata/RdsDataServiceTest.java
index e12efd16bb..13f5da9d42 100644
--- a/src/test/java/io/github/hectorvent/floci/services/rdsdata/RdsDataServiceTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/rdsdata/RdsDataServiceTest.java
@@ -832,6 +832,7 @@ class RdsDataServiceTest {
return switch (engine) {
case MYSQL, MARIADB -> "MySQL";
case POSTGRES -> "PostgreSQL";
+ case SQLSERVER -> "MSSQLServer";
};
}
diff --git a/src/test/resources/application.yml b/src/test/resources/application.yml
index 303102d083..3adc12dc76 100644
--- a/src/test/resources/application.yml
+++ b/src/test/resources/application.yml
@@ -199,6 +199,7 @@ floci:
mock: false
proxy-base-port: 7001
proxy-max-port: 7099
+ default-sql-server-image: "mcr.microsoft.com/mssql/server:2022-latest"
proxy-handshake-timeout-millis: 10000
proxy-backend-connect-timeout-millis: 5000
proxy-max-connections: 100