mirror of
https://github.com/floci-io/floci.git
synced 2026-11-23 22:11:36 -05:00
feat(rds): add SQL Server support to RDS (#3752)
* feat(rds): add SQL Server support * fix(rds): align SQL Server master handling * fix(rds): reject SQL Server DBName * fix(test): import Mockito matchers
This commit is contained in:
1 parent
7e539155e1
commit
a3da2a3767
20 files changed
+417
-15
No files matched your search
@@ -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();
|
||||
|
||||
|
||||
@@ -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";
|
||||
};
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
};
|
||||
}
|
||||
|
||||
+61
-7
@@ -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,12 +470,49 @@ 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() {
|
||||
return """
|
||||
DO $$
|
||||
@@ -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();
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -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());
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
}
|
||||
}
|
||||
+3
@@ -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";
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user