Add RDS provisioning support (#1253)

* feat(rds): add subnet groups and orderable options

* feat(rds): support subnet group modification

* feat(rds): return managed master user secrets

* fix(rds): preserve instance tags

* fix(rds): honor requested engine versions

* fix(rds): return routable proxy endpoints

* fix(rds): accept PostgreSQL SSL negotiation

* fix(rds): initialize PostgreSQL IAM role

* fix(rds): include PostgreSQL 18 orderable option

* fix(rds): address provisioning support review feedback
This commit is contained in:
Jason van Zyl authored and GitHub committed 2026-06-13 18:46:42 -05:00
1 parent 59a2659e41
commit 22a50b3c80
13 files changed
+1329 -58

No files matched your search

@@ -309,6 +309,9 @@ public class AwsQueryController {
private static final Set<String> RDS_ACTIONS = Set.of(
"CreateDBInstance", "DescribeDBInstances", "DeleteDBInstance",
"ModifyDBInstance", "RebootDBInstance",
"DescribeOrderableDBInstanceOptions",
"CreateDBSubnetGroup", "DescribeDBSubnetGroups", "ModifyDBSubnetGroup", "DeleteDBSubnetGroup",
"AddTagsToResource", "ListTagsForResource", "RemoveTagsFromResource",
"CreateDBCluster", "DescribeDBClusters", "DeleteDBCluster", "ModifyDBCluster",
"CreateDBParameterGroup", "DescribeDBParameterGroups",
"DeleteDBParameterGroup", "ModifyDBParameterGroup", "DescribeDBParameters"
@@ -11,6 +11,7 @@ import io.github.hectorvent.floci.services.rds.model.DbEndpoint;
import io.github.hectorvent.floci.services.rds.model.DbInstance;
import io.github.hectorvent.floci.services.rds.model.DbInstanceStatus;
import io.github.hectorvent.floci.services.rds.model.DbParameterGroup;
import io.github.hectorvent.floci.services.rds.model.DbSubnetGroup;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import jakarta.ws.rs.core.MultivaluedMap;
@@ -19,6 +20,8 @@ import org.jboss.logging.Logger;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
/**
@@ -47,6 +50,11 @@ public class RdsQueryHandler {
case "DeleteDBInstance" -> handleDeleteDbInstance(params);
case "ModifyDBInstance" -> handleModifyDbInstance(params);
case "RebootDBInstance" -> handleRebootDbInstance(params);
case "DescribeOrderableDBInstanceOptions" -> handleDescribeOrderableDbInstanceOptions(params);
case "CreateDBSubnetGroup" -> handleCreateDbSubnetGroup(params);
case "DescribeDBSubnetGroups" -> handleDescribeDbSubnetGroups(params);
case "ModifyDBSubnetGroup" -> handleModifyDbSubnetGroup(params);
case "DeleteDBSubnetGroup" -> handleDeleteDbSubnetGroup(params);
case "CreateDBCluster" -> handleCreateDbCluster(params);
case "DescribeDBClusters" -> handleDescribeDbClusters(params);
case "DeleteDBCluster" -> handleDeleteDbCluster(params);
@@ -61,6 +69,9 @@ public class RdsQueryHandler {
case "DeleteDBClusterParameterGroup" -> handleDeleteDbClusterParameterGroup(params);
case "ModifyDBClusterParameterGroup" -> handleModifyDbClusterParameterGroup(params);
case "DescribeDBClusterParameters" -> handleDescribeDbClusterParameters(params);
case "AddTagsToResource" -> handleAddTagsToResource(params);
case "ListTagsForResource" -> handleListTagsForResource(params);
case "RemoveTagsFromResource" -> handleRemoveTagsFromResource(params);
default -> AwsQueryResponse.error("UnsupportedOperation",
"Operation " + action + " is not supported.", AwsNamespaces.RDS, 400);
};
@@ -91,7 +102,11 @@ public class RdsQueryHandler {
int allocatedStorage = allocatedStorageStr != null ? parseIntSafe(allocatedStorageStr, 20) : 20;
boolean iamEnabled = "true".equalsIgnoreCase(params.getFirst("EnableIAMDatabaseAuthentication"));
String paramGroupName = params.getFirst("DBParameterGroupName");
String dbSubnetGroupName = params.getFirst("DBSubnetGroupName");
String dbClusterIdentifier = params.getFirst("DBClusterIdentifier");
boolean manageMasterUserPassword = "true".equalsIgnoreCase(params.getFirst("ManageMasterUserPassword"));
String masterUserSecretKmsKeyId = params.getFirst("MasterUserSecretKmsKeyId");
Map<String, String> tags = parseTags(params);
if (dbInstanceClass == null) {
dbInstanceClass = "db.t3.micro";
@@ -103,7 +118,8 @@ public class RdsQueryHandler {
try {
DbInstance instance = service.createDbInstance(id, engine, engineVersion, masterUsername,
masterPassword, dbName, dbInstanceClass, allocatedStorage, iamEnabled,
paramGroupName, dbClusterIdentifier);
paramGroupName, dbSubnetGroupName, dbClusterIdentifier,
manageMasterUserPassword, masterUserSecretKmsKeyId, tags);
String result = dbInstanceXml(instance);
return Response.ok(AwsQueryResponse.envelope("CreateDBInstance", AwsNamespaces.RDS, result)).build();
} catch (AwsException e) {
@@ -152,8 +168,9 @@ public class RdsQueryHandler {
String newPassword = params.getFirst("MasterUserPassword");
String iamStr = params.getFirst("EnableIAMDatabaseAuthentication");
Boolean iamEnabled = iamStr != null ? Boolean.parseBoolean(iamStr) : null;
String dbSubnetGroupName = params.getFirst("DBSubnetGroupName");
try {
DbInstance instance = service.modifyDbInstance(id, newPassword, iamEnabled);
DbInstance instance = service.modifyDbInstance(id, newPassword, iamEnabled, dbSubnetGroupName);
String result = dbInstanceXml(instance);
return Response.ok(AwsQueryResponse.envelope("ModifyDBInstance", AwsNamespaces.RDS, result)).build();
} catch (AwsException e) {
@@ -161,6 +178,124 @@ public class RdsQueryHandler {
}
}
private Response handleDescribeOrderableDbInstanceOptions(MultivaluedMap<String, String> params) {
Collection<Map<String, String>> options = service.describeOrderableDbInstanceOptions(
params.getFirst("Engine"),
params.getFirst("EngineVersion"),
params.getFirst("DBInstanceClass"));
XmlBuilder xml = new XmlBuilder().start("OrderableDBInstanceOptions");
for (Map<String, String> option : options) {
xml.start("OrderableDBInstanceOption")
.elem("Engine", option.get("engine"))
.elem("EngineVersion", option.get("engineVersion"))
.elem("DBInstanceClass", option.get("dbInstanceClass"))
.elem("LicenseModel", "postgresql-license")
.start("AvailabilityZones")
.start("AvailabilityZone")
.elem("Name", config.defaultAvailabilityZone())
.end("AvailabilityZone")
.end("AvailabilityZones")
.end("OrderableDBInstanceOption");
}
xml.end("OrderableDBInstanceOptions").start("Marker").end("Marker");
return Response.ok(AwsQueryResponse.envelope("DescribeOrderableDBInstanceOptions",
AwsNamespaces.RDS, xml.build())).build();
}
private Response handleAddTagsToResource(MultivaluedMap<String, String> params) {
String resourceName = params.getFirst("ResourceName");
try {
service.addTagsToResource(resourceName, parseTags(params));
return Response.ok(AwsQueryResponse.envelope("AddTagsToResource", AwsNamespaces.RDS, "")).build();
} catch (AwsException e) {
return AwsQueryResponse.error(e.getErrorCode(), e.getMessage(), AwsNamespaces.RDS, e.getHttpStatus());
}
}
private Response handleListTagsForResource(MultivaluedMap<String, String> params) {
String resourceName = params.getFirst("ResourceName");
try {
XmlBuilder xml = new XmlBuilder().start("TagList");
writeTags(xml, service.listTagsForResource(resourceName));
xml.end("TagList");
return Response.ok(AwsQueryResponse.envelope("ListTagsForResource", AwsNamespaces.RDS, xml.build())).build();
} catch (AwsException e) {
return AwsQueryResponse.error(e.getErrorCode(), e.getMessage(), AwsNamespaces.RDS, e.getHttpStatus());
}
}
private Response handleRemoveTagsFromResource(MultivaluedMap<String, String> params) {
String resourceName = params.getFirst("ResourceName");
try {
service.removeTagsFromResource(resourceName, memberList(params, "TagKeys"));
return Response.ok(AwsQueryResponse.envelope("RemoveTagsFromResource", AwsNamespaces.RDS, "")).build();
} catch (AwsException e) {
return AwsQueryResponse.error(e.getErrorCode(), e.getMessage(), AwsNamespaces.RDS, e.getHttpStatus());
}
}
private Response handleCreateDbSubnetGroup(MultivaluedMap<String, String> params) {
String name = params.getFirst("DBSubnetGroupName");
if (name == null || name.isBlank()) {
return AwsQueryResponse.error("InvalidParameterValue",
"DBSubnetGroupName is required.", AwsNamespaces.RDS, 400);
}
String description = params.getFirst("DBSubnetGroupDescription");
List<String> subnetIds = memberList(params, "SubnetIds");
try {
DbSubnetGroup group = service.createDbSubnetGroup(name, description, subnetIds);
return Response.ok(AwsQueryResponse.envelope("CreateDBSubnetGroup",
AwsNamespaces.RDS, dbSubnetGroupXml(group))).build();
} catch (AwsException e) {
return AwsQueryResponse.error(e.getErrorCode(), e.getMessage(), AwsNamespaces.RDS, e.getHttpStatus());
}
}
private Response handleDescribeDbSubnetGroups(MultivaluedMap<String, String> params) {
String filterName = params.getFirst("DBSubnetGroupName");
try {
Collection<DbSubnetGroup> result = service.listDbSubnetGroups(filterName);
XmlBuilder xml = new XmlBuilder().start("DBSubnetGroups");
for (DbSubnetGroup group : result) {
xml.start("DBSubnetGroup").raw(dbSubnetGroupInnerXml(group)).end("DBSubnetGroup");
}
xml.end("DBSubnetGroups").start("Marker").end("Marker");
return Response.ok(AwsQueryResponse.envelope("DescribeDBSubnetGroups", AwsNamespaces.RDS, xml.build())).build();
} catch (AwsException e) {
return AwsQueryResponse.error(e.getErrorCode(), e.getMessage(), AwsNamespaces.RDS, e.getHttpStatus());
}
}
private Response handleModifyDbSubnetGroup(MultivaluedMap<String, String> params) {
String name = params.getFirst("DBSubnetGroupName");
if (name == null || name.isBlank()) {
return AwsQueryResponse.error("InvalidParameterValue",
"DBSubnetGroupName is required.", AwsNamespaces.RDS, 400);
}
List<String> subnetIds = memberList(params, "SubnetIds");
try {
DbSubnetGroup group = service.modifyDbSubnetGroup(name, subnetIds);
return Response.ok(AwsQueryResponse.envelope("ModifyDBSubnetGroup",
AwsNamespaces.RDS, dbSubnetGroupXml(group))).build();
} catch (AwsException e) {
return AwsQueryResponse.error(e.getErrorCode(), e.getMessage(), AwsNamespaces.RDS, e.getHttpStatus());
}
}
private Response handleDeleteDbSubnetGroup(MultivaluedMap<String, String> params) {
String name = params.getFirst("DBSubnetGroupName");
if (name == null || name.isBlank()) {
return AwsQueryResponse.error("InvalidParameterValue",
"DBSubnetGroupName is required.", AwsNamespaces.RDS, 400);
}
try {
service.deleteDbSubnetGroup(name);
return Response.ok(AwsQueryResponse.envelope("DeleteDBSubnetGroup", AwsNamespaces.RDS, "")).build();
} catch (AwsException e) {
return AwsQueryResponse.error(e.getErrorCode(), e.getMessage(), AwsNamespaces.RDS, e.getHttpStatus());
}
}
private Response handleRebootDbInstance(MultivaluedMap<String, String> params) {
String id = params.getFirst("DBInstanceIdentifier");
if (id == null || id.isBlank()) {
@@ -486,28 +621,73 @@ public class RdsQueryHandler {
.elem("Status", "active")
.end("VpcSecurityGroupMembership")
.end("VpcSecurityGroups")
.start("DBSubnetGroup")
.elem("DBSubnetGroupName", "default")
.elem("VpcId", "vpc-00000000")
.elem("SubnetGroupStatus", "Complete")
.start("Subnets")
.start("member")
.elem("SubnetIdentifier", "subnet-00000000")
.start("SubnetAvailabilityZone")
.elem("Name", config.defaultAvailabilityZone())
.end("SubnetAvailabilityZone")
.elem("SubnetStatus", "Active")
.end("member")
.end("Subnets")
.end("DBSubnetGroup")
.raw(dbSubnetGroupXml(dbSubnetGroupForInstance(i)))
.elem("DbiResourceId", i.getDbiResourceId())
.elem("DBInstanceArn", i.getDbInstanceArn());
if (i.getMasterUserSecretArn() != null && !i.getMasterUserSecretArn().isBlank()) {
xml.start("MasterUserSecret")
.elem("SecretArn", i.getMasterUserSecretArn())
.elem("SecretStatus", i.getMasterUserSecretStatus() == null ? "active" : i.getMasterUserSecretStatus());
if (i.getMasterUserSecretKmsKeyId() != null && !i.getMasterUserSecretKmsKeyId().isBlank()) {
xml.elem("KmsKeyId", i.getMasterUserSecretKmsKeyId());
}
xml.end("MasterUserSecret");
}
if (i.getDbClusterIdentifier() != null && !i.getDbClusterIdentifier().isBlank()) {
xml.elem("DBClusterIdentifier", i.getDbClusterIdentifier());
}
xml.start("TagList");
writeTags(xml, i.getTags());
xml.end("TagList");
return xml.build();
}
private static void writeTags(XmlBuilder xml, Map<String, String> tags) {
if (tags == null) {
return;
}
tags.forEach((key, value) -> xml.start("Tag")
.elem("Key", key)
.elem("Value", value)
.end("Tag"));
}
private String dbSubnetGroupXml(DbSubnetGroup group) {
return new XmlBuilder().start("DBSubnetGroup").raw(dbSubnetGroupInnerXml(group)).end("DBSubnetGroup").build();
}
private String dbSubnetGroupInnerXml(DbSubnetGroup group) {
XmlBuilder xml = new XmlBuilder()
.elem("DBSubnetGroupName", group.getDbSubnetGroupName())
.elem("DBSubnetGroupDescription", group.getDbSubnetGroupDescription())
.elem("VpcId", group.getVpcId() != null ? group.getVpcId() : "vpc-00000000")
.elem("SubnetGroupStatus", "Complete")
.start("Subnets");
for (String subnetId : group.getSubnetIds()) {
xml.start("Subnet")
.elem("SubnetIdentifier", subnetId)
.start("SubnetAvailabilityZone")
.elem("Name", config.defaultAvailabilityZone())
.end("SubnetAvailabilityZone")
.elem("SubnetStatus", "Active")
.end("Subnet");
}
return xml.end("Subnets").build();
}
private DbSubnetGroup dbSubnetGroupForInstance(DbInstance instance) {
String groupName = instance.getDbSubnetGroupName();
if (groupName != null && !groupName.isBlank()) {
try {
return service.getDbSubnetGroup(groupName);
} catch (AwsException ignored) {
return new DbSubnetGroup(groupName, "DB subnet group " + groupName,
"vpc-00000000", List.of("subnet-00000000"));
}
}
return new DbSubnetGroup("default", "default", "vpc-00000000", List.of("subnet-00000000"));
}
private String dbClusterXml(DbCluster c) {
return new XmlBuilder().start("DBCluster").raw(dbClusterInnerXml(c)).end("DBCluster").build();
}
@@ -613,6 +793,46 @@ public class RdsQueryHandler {
return null;
}
private static List<String> memberList(MultivaluedMap<String, String> params, String baseName) {
return params.keySet().stream()
.filter(key -> key.matches(java.util.regex.Pattern.quote(baseName)
+ "(\\.member|\\.SubnetIdentifier)?\\.\\d+"))
.sorted(java.util.Comparator.comparingInt(RdsQueryHandler::numericSuffix))
.map(params::getFirst)
.filter(value -> value != null && !value.isBlank())
.toList();
}
private static int numericSuffix(String key) {
int dot = key.lastIndexOf('.');
if (dot < 0 || dot == key.length() - 1) {
return Integer.MAX_VALUE;
}
try {
return Integer.parseInt(key.substring(dot + 1));
} catch (NumberFormatException e) {
return Integer.MAX_VALUE;
}
}
private static Map<String, String> parseTags(MultivaluedMap<String, String> params) {
Map<String, String> tags = new LinkedHashMap<>();
readTags(params, "Tags.member", tags);
readTags(params, "Tags.Tag", tags);
readTags(params, "Tag", tags);
return tags;
}
private static void readTags(MultivaluedMap<String, String> params, String prefix, Map<String, String> tags) {
for (int i = 1; ; i++) {
String key = params.getFirst(prefix + "." + i + ".Key");
if (key == null) {
break;
}
tags.put(key, params.getFirst(prefix + "." + i + ".Value"));
}
}
private static int parseIntSafe(String value, int defaultValue) {
try {
return Integer.parseInt(value);
@@ -1,10 +1,13 @@
package io.github.hectorvent.floci.services.rds;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.github.hectorvent.floci.config.EmulatorConfig;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.common.RegionResolver;
import io.github.hectorvent.floci.core.common.docker.ContainerStorageHelper;
import io.github.hectorvent.floci.core.common.docker.DockerHostResolver;
import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;
import io.github.hectorvent.floci.core.storage.StorageBackend;
import io.github.hectorvent.floci.core.storage.StorageFactory;
@@ -17,7 +20,10 @@ import io.github.hectorvent.floci.services.rds.model.DbEndpoint;
import io.github.hectorvent.floci.services.rds.model.DbInstance;
import io.github.hectorvent.floci.services.rds.model.DbInstanceStatus;
import io.github.hectorvent.floci.services.rds.model.DbParameterGroup;
import io.github.hectorvent.floci.services.rds.model.DbSubnetGroup;
import io.github.hectorvent.floci.services.rds.proxy.RdsProxyManager;
import io.github.hectorvent.floci.services.secretsmanager.SecretsManagerService;
import io.github.hectorvent.floci.services.secretsmanager.model.Secret;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import org.jboss.logging.Logger;
@@ -30,6 +36,8 @@ import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Core RDS business logic — DB instances, clusters, and parameter groups.
@@ -39,27 +47,37 @@ import java.util.concurrent.ConcurrentHashMap;
public class RdsService {
private static final Logger LOG = Logger.getLogger(RdsService.class);
private static final ObjectMapper JSON = new ObjectMapper();
private final StorageBackend<String, DbInstance> instances;
private final StorageBackend<String, DbCluster> clusters;
private final StorageBackend<String, DbParameterGroup> parameterGroups;
private final StorageBackend<String, DbClusterParameterGroup> clusterParameterGroups;
private final StorageBackend<String, DbSubnetGroup> subnetGroups;
private final RdsContainerManager containerManager;
private final RdsProxyManager proxyManager;
private final RegionResolver regionResolver;
private final EmulatorConfig config;
private final SecretsManagerService secretsManagerService;
private final DockerHostResolver dockerHostResolver;
private final Set<Integer> usedPorts = ConcurrentHashMap.newKeySet();
private static final Pattern IMAGE_TAG_VERSION_PATTERN = Pattern.compile("^(\\d+(?:\\.\\d+)*)(.*)$");
private static final Pattern SAFE_IMAGE_TAG_PATTERN = Pattern.compile("[A-Za-z0-9._-]+");
@Inject
public RdsService(RdsContainerManager containerManager,
RdsProxyManager proxyManager,
RegionResolver regionResolver,
EmulatorConfig config,
StorageFactory storageFactory) {
StorageFactory storageFactory,
SecretsManagerService secretsManagerService,
DockerHostResolver dockerHostResolver) {
this.containerManager = containerManager;
this.proxyManager = proxyManager;
this.regionResolver = regionResolver;
this.config = config;
this.secretsManagerService = secretsManagerService;
this.dockerHostResolver = dockerHostResolver;
this.instances = storageFactory.create("rds", "rds-instances.json",
new TypeReference<Map<String, DbInstance>>() {});
this.clusters = storageFactory.create("rds", "rds-clusters.json",
@@ -68,6 +86,8 @@ public class RdsService {
new TypeReference<Map<String, DbParameterGroup>>() {});
this.clusterParameterGroups = storageFactory.create("rds", "rds-cluster-parameter-groups.json",
new TypeReference<Map<String, DbClusterParameterGroup>>() {});
this.subnetGroups = storageFactory.create("rds", "rds-subnet-groups.json",
new TypeReference<Map<String, DbSubnetGroup>>() {});
}
RdsService(RdsContainerManager containerManager,
@@ -77,15 +97,49 @@ public class RdsService {
StorageBackend<String, DbInstance> instances,
StorageBackend<String, DbCluster> clusters,
StorageBackend<String, DbParameterGroup> parameterGroups,
StorageBackend<String, DbClusterParameterGroup> clusterParameterGroups) {
StorageBackend<String, DbClusterParameterGroup> clusterParameterGroups,
StorageBackend<String, DbSubnetGroup> subnetGroups) {
this(containerManager, proxyManager, regionResolver, config,
instances, clusters, parameterGroups, clusterParameterGroups, subnetGroups, null, null);
}
RdsService(RdsContainerManager containerManager,
RdsProxyManager proxyManager,
RegionResolver regionResolver,
EmulatorConfig config,
StorageBackend<String, DbInstance> instances,
StorageBackend<String, DbCluster> clusters,
StorageBackend<String, DbParameterGroup> parameterGroups,
StorageBackend<String, DbClusterParameterGroup> clusterParameterGroups,
StorageBackend<String, DbSubnetGroup> subnetGroups,
SecretsManagerService secretsManagerService) {
this(containerManager, proxyManager, regionResolver, config,
instances, clusters, parameterGroups, clusterParameterGroups, subnetGroups,
secretsManagerService, null);
}
RdsService(RdsContainerManager containerManager,
RdsProxyManager proxyManager,
RegionResolver regionResolver,
EmulatorConfig config,
StorageBackend<String, DbInstance> instances,
StorageBackend<String, DbCluster> clusters,
StorageBackend<String, DbParameterGroup> parameterGroups,
StorageBackend<String, DbClusterParameterGroup> clusterParameterGroups,
StorageBackend<String, DbSubnetGroup> subnetGroups,
SecretsManagerService secretsManagerService,
DockerHostResolver dockerHostResolver) {
this.containerManager = containerManager;
this.proxyManager = proxyManager;
this.regionResolver = regionResolver;
this.config = config;
this.secretsManagerService = secretsManagerService;
this.dockerHostResolver = dockerHostResolver;
this.instances = instances;
this.clusters = clusters;
this.parameterGroups = parameterGroups;
this.clusterParameterGroups = clusterParameterGroups;
this.subnetGroups = subnetGroups;
}
public void restorePersistedRuntime() {
@@ -99,14 +153,52 @@ public class RdsService {
String masterUsername, String masterPassword,
String dbName, String dbInstanceClass,
int allocatedStorage, boolean iamEnabled,
String paramGroupName, String dbClusterIdentifier) {
String paramGroupName, String dbSubnetGroupName,
String dbClusterIdentifier) {
return createDbInstance(id, engineParam, engineVersion, masterUsername, masterPassword,
dbName, dbInstanceClass, allocatedStorage, iamEnabled, paramGroupName,
dbSubnetGroupName, dbClusterIdentifier, false, null);
}
public DbInstance createDbInstance(String id, String engineParam, String engineVersion,
String masterUsername, String masterPassword,
String dbName, String dbInstanceClass,
int allocatedStorage, boolean iamEnabled,
String paramGroupName, String dbSubnetGroupName,
String dbClusterIdentifier,
boolean manageMasterUserPassword,
String masterUserSecretKmsKeyId) {
return createDbInstance(id, engineParam, engineVersion, masterUsername, masterPassword,
dbName, dbInstanceClass, allocatedStorage, iamEnabled, paramGroupName,
dbSubnetGroupName, dbClusterIdentifier, manageMasterUserPassword,
masterUserSecretKmsKeyId, Map.of());
}
public DbInstance createDbInstance(String id, String engineParam, String engineVersion,
String masterUsername, String masterPassword,
String dbName, String dbInstanceClass,
int allocatedStorage, boolean iamEnabled,
String paramGroupName, String dbSubnetGroupName,
String dbClusterIdentifier,
boolean manageMasterUserPassword,
String masterUserSecretKmsKeyId,
Map<String, String> tags) {
if (instances.get(id).isPresent()) {
throw new AwsException("DBInstanceAlreadyExists",
"DB instance " + id + " already exists.", 400);
}
DatabaseEngine engine = resolveEngine(engineParam);
if (dbSubnetGroupName != null && !dbSubnetGroupName.isBlank()) {
getDbSubnetGroup(dbSubnetGroupName);
}
int proxyPort = allocateProxyPort();
if (masterUsername == null || masterUsername.isBlank()) {
masterUsername = "root";
}
if (manageMasterUserPassword && (masterPassword == null || masterPassword.isBlank())) {
masterPassword = generatedMasterPassword();
}
String backendHost;
int backendPort;
@@ -131,7 +223,7 @@ public class RdsService {
: volumeName(cluster.getVolumeId(), cluster.getDbClusterIdentifier());
} else {
// Standalone instance — start its own container
String image = imageForEngine(engine);
String image = imageForEngine(engine, engineVersion);
instanceVolumeId = String.format("%06x", new SecureRandom().nextInt(0xFFFFFF));
RdsContainerHandle handle = containerManager.start(id, instanceVolumeId, engine, image, masterUsername, masterPassword, dbName);
backendHost = handle.getHost();
@@ -142,23 +234,27 @@ public class RdsService {
instanceDockerVolumeName = volumeName(instanceVolumeId, id);
}
DbEndpoint endpoint = new DbEndpoint("localhost", proxyPort);
DbEndpoint endpoint = new DbEndpoint(proxyEndpointHost(), proxyPort);
DbInstance instance = new DbInstance(id, engine, engineVersion, masterUsername, masterPassword,
dbName, dbInstanceClass, allocatedStorage, DbInstanceStatus.AVAILABLE,
endpoint, iamEnabled, paramGroupName, dbClusterIdentifier, Instant.now(), proxyPort);
instance.setDbSubnetGroupName(dbSubnetGroupName);
instance.setContainerId(containerId);
instance.setContainerHost(containerHost);
instance.setContainerPort(containerPort);
instance.setVolumeId(instanceVolumeId);
instance.setDockerVolumeName(instanceDockerVolumeName);
instance.setTags(tags);
String region = regionResolver.getDefaultRegion();
instance.setDbiResourceId("db-" + java.util.UUID.randomUUID().toString().replace("-", "").substring(0, 24).toUpperCase());
instance.setDbInstanceArn(regionResolver.buildArn("rds", region, "db:" + id));
if (manageMasterUserPassword) {
attachManagedMasterUserSecret(instance, region, masterUserSecretKmsKeyId);
}
String effectiveMasterUser = masterUsername != null ? masterUsername : "root";
proxyManager.startProxy(id, engine, iamEnabled, proxyPort, backendHost, backendPort,
effectiveMasterUser, masterPassword, dbName,
masterUsername, masterPassword, dbName,
(user, pw) -> validateDbPassword(id, user, pw));
if (dbClusterIdentifier != null && !dbClusterIdentifier.isBlank()) {
@@ -174,6 +270,78 @@ public class RdsService {
return instance;
}
public Map<String, String> listTagsForResource(String resourceName) {
DbInstance instance = getDbInstance(dbInstanceIdentifierFromResourceName(resourceName));
return Map.copyOf(instance.getTags());
}
public void addTagsToResource(String resourceName, Map<String, String> tags) {
String id = dbInstanceIdentifierFromResourceName(resourceName);
DbInstance instance = getDbInstance(id);
Map<String, String> updated = new java.util.LinkedHashMap<>(instance.getTags());
updated.putAll(tags);
instance.setTags(updated);
instances.put(id, instance);
}
public void removeTagsFromResource(String resourceName, Collection<String> tagKeys) {
String id = dbInstanceIdentifierFromResourceName(resourceName);
DbInstance instance = getDbInstance(id);
Map<String, String> updated = new java.util.LinkedHashMap<>(instance.getTags());
tagKeys.forEach(updated::remove);
instance.setTags(updated);
instances.put(id, instance);
}
private static String dbInstanceIdentifierFromResourceName(String resourceName) {
if (resourceName == null || resourceName.isBlank()) {
throw new AwsException("InvalidParameterValue", "ResourceName is required.", 400);
}
int marker = resourceName.lastIndexOf(":db:");
if (marker >= 0) {
return resourceName.substring(marker + 4);
}
return resourceName;
}
private void attachManagedMasterUserSecret(DbInstance instance, String region, String kmsKeyId) {
if (secretsManagerService == null) {
throw new AwsException("InvalidParameterCombination",
"ManageMasterUserPassword requires Secrets Manager support.", 400);
}
String secretName = "rds!" + instance.getDbiResourceId();
Secret secret = secretsManagerService.createSecret(
secretName,
managedMasterSecretString(instance),
null,
"Managed RDS master user secret for " + instance.getDbInstanceIdentifier(),
kmsKeyId,
null,
region);
instance.setMasterUserSecretArn(secret.getArn());
instance.setMasterUserSecretStatus("active");
instance.setMasterUserSecretKmsKeyId(kmsKeyId);
}
private static String managedMasterSecretString(DbInstance instance) {
try {
return JSON.writeValueAsString(Map.of(
"username", instance.getMasterUsername(),
"password", instance.getMasterPassword(),
"engine", instance.getEngine().name().toLowerCase(),
"host", instance.getEndpoint().address(),
"port", instance.getEndpoint().port(),
"dbname", instance.getDbName() == null ? "" : instance.getDbName(),
"dbInstanceIdentifier", instance.getDbInstanceIdentifier()));
} catch (JsonProcessingException e) {
throw new IllegalStateException("Unable to serialize RDS master user secret", e);
}
}
private static String generatedMasterPassword() {
return "floci-" + java.util.UUID.randomUUID().toString().replace("-", "");
}
public DbInstance getDbInstance(String id) {
return instances.get(id).orElseThrow(() ->
new AwsException("DBInstanceNotFound",
@@ -187,7 +355,8 @@ public class RdsService {
return instances.scan(k -> true);
}
public DbInstance modifyDbInstance(String id, String newPassword, Boolean iamEnabled) {
public DbInstance modifyDbInstance(String id, String newPassword, Boolean iamEnabled,
String dbSubnetGroupName) {
DbInstance instance = getDbInstance(id);
instance.setStatus(DbInstanceStatus.AVAILABLE);
if (newPassword != null && !newPassword.isBlank()) {
@@ -196,11 +365,37 @@ public class RdsService {
if (iamEnabled != null) {
instance.setIamDatabaseAuthenticationEnabled(iamEnabled);
}
if (dbSubnetGroupName != null && !dbSubnetGroupName.isBlank()) {
getDbSubnetGroup(dbSubnetGroupName);
instance.setDbSubnetGroupName(dbSubnetGroupName);
}
instances.put(id, instance);
LOG.infov("DB instance {0} modified", id);
return instance;
}
public List<Map<String, String>> describeOrderableDbInstanceOptions(String engine,
String engineVersion,
String dbInstanceClass) {
List<Map<String, String>> options = List.of(
Map.of("engine", "postgres", "engineVersion", "16.3", "dbInstanceClass", "db.t3.micro"),
Map.of("engine", "postgres", "engineVersion", "16.14", "dbInstanceClass", "db.t3.micro"),
Map.of("engine", "postgres", "engineVersion", "18.1", "dbInstanceClass", "db.t3.micro"),
Map.of("engine", "postgres", "engineVersion", "16.3", "dbInstanceClass", "db.t4g.micro"),
Map.of("engine", "postgres", "engineVersion", "16.3", "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")
);
return options.stream()
.filter(option -> engine == null || engine.isBlank() || engine.equalsIgnoreCase(option.get("engine")))
.filter(option -> engineVersion == null || engineVersion.isBlank()
|| engineVersion.equalsIgnoreCase(option.get("engineVersion")))
.filter(option -> dbInstanceClass == null || dbInstanceClass.isBlank()
|| dbInstanceClass.equalsIgnoreCase(option.get("dbInstanceClass")))
.toList();
}
public DbInstance rebootDbInstance(String id) {
DbInstance instance = getDbInstance(id);
@@ -217,7 +412,7 @@ public class RdsService {
} catch (Exception e) {
LOG.warnv("Error stopping container during reboot of {0}: {1}", id, e.getMessage());
}
String image = imageForEngine(instance.getEngine());
String image = imageForEngine(instance.getEngine(), instance.getEngineVersion());
RdsContainerHandle handle = containerManager.start(id, instance.getVolumeId(), instance.getEngine(), image,
instance.getMasterUsername(), instance.getMasterPassword(), instance.getDbName());
instance.setContainerId(handle.getContainerId());
@@ -288,12 +483,12 @@ public class RdsService {
DatabaseEngine engine = resolveEngine(engineParam);
int proxyPort = allocateProxyPort();
String image = imageForEngine(engine);
String image = imageForEngine(engine, engineVersion);
String clusterVolumeId = String.format("%06x", new SecureRandom().nextInt(0xFFFFFF));
RdsContainerHandle handle = containerManager.start(id, clusterVolumeId, engine, image, masterUsername, masterPassword, databaseName);
DbEndpoint endpoint = new DbEndpoint("localhost", proxyPort);
DbEndpoint endpoint = new DbEndpoint(proxyEndpointHost(), proxyPort);
DbCluster cluster = new DbCluster(id, engine, engineVersion, masterUsername, masterPassword,
databaseName, DbInstanceStatus.AVAILABLE, endpoint, endpoint,
iamEnabled, new ArrayList<>(), paramGroupName, Instant.now(), proxyPort);
@@ -411,6 +606,52 @@ public class RdsService {
return group;
}
public DbSubnetGroup createDbSubnetGroup(String name, String description, List<String> subnetIds) {
if (subnetGroups.get(name).isPresent()) {
throw new AwsException("DBSubnetGroupAlreadyExists",
"DB subnet group " + name + " already exists.", 400);
}
if (subnetIds == null || subnetIds.isEmpty()) {
throw new AwsException("InvalidParameterValue",
"SubnetIds must contain at least one subnet.", 400);
}
DbSubnetGroup group = new DbSubnetGroup(name, description, "vpc-00000000", subnetIds);
subnetGroups.put(name, group);
return group;
}
public DbSubnetGroup getDbSubnetGroup(String name) {
return subnetGroups.get(name).orElseThrow(() ->
new AwsException("DBSubnetGroupNotFoundFault",
"DB subnet group " + name + " not found.", 404));
}
public Collection<DbSubnetGroup> listDbSubnetGroups(String filterName) {
if (filterName != null && !filterName.isBlank()) {
return subnetGroups.get(filterName).map(List::of).orElse(List.of());
}
return subnetGroups.scan(k -> true);
}
public DbSubnetGroup modifyDbSubnetGroup(String name, List<String> subnetIds) {
DbSubnetGroup group = getDbSubnetGroup(name);
if (subnetIds == null || subnetIds.isEmpty()) {
throw new AwsException("InvalidParameterValue",
"SubnetIds must contain at least one subnet.", 400);
}
group.setSubnetIds(subnetIds);
subnetGroups.put(name, group);
return group;
}
public void deleteDbSubnetGroup(String name) {
if (subnetGroups.get(name).isEmpty()) {
throw new AwsException("DBSubnetGroupNotFoundFault",
"DB subnet group " + name + " not found.", 404);
}
subnetGroups.delete(name);
}
// ── Cluster Parameter Groups ──────────────────────────────────────────────
public DbClusterParameterGroup createDbClusterParameterGroup(String name, String family, String description) {
@@ -493,12 +734,44 @@ public class RdsService {
};
}
private String imageForEngine(DatabaseEngine engine) {
return switch (engine) {
private String imageForEngine(DatabaseEngine engine, String engineVersion) {
String defaultImage = switch (engine) {
case POSTGRES -> config.services().rds().defaultPostgresImage();
case MYSQL -> config.services().rds().defaultMysqlImage();
case MARIADB -> config.services().rds().defaultMariadbImage();
};
return imageForRequestedVersion(defaultImage, engineVersion);
}
static String imageForRequestedVersion(String defaultImage, String engineVersion) {
if (engineVersion == null || engineVersion.isBlank()) {
return defaultImage;
}
String requestedTag = engineVersion.trim();
if (!SAFE_IMAGE_TAG_PATTERN.matcher(requestedTag).matches()) {
throw new AwsException("InvalidParameterValue",
"Unsupported engine version tag: " + engineVersion, 400);
}
int tagSeparator = defaultImage.lastIndexOf(':');
int lastSlash = defaultImage.lastIndexOf('/');
if (tagSeparator <= lastSlash) {
return defaultImage + ":" + requestedTag;
}
String imageName = defaultImage.substring(0, tagSeparator);
String defaultTag = defaultImage.substring(tagSeparator + 1);
Matcher matcher = IMAGE_TAG_VERSION_PATTERN.matcher(defaultTag);
if (!matcher.matches()) {
return imageName + ":" + requestedTag;
}
String suffix = matcher.group(2);
if (!suffix.isEmpty() && !requestedTag.endsWith(suffix)) {
requestedTag += suffix;
}
return imageName + ":" + requestedTag;
}
private int allocateProxyPort() {
@@ -517,6 +790,10 @@ public class RdsService {
usedPorts.remove(port);
}
private String proxyEndpointHost() {
return dockerHostResolver != null ? dockerHostResolver.resolve() : "localhost";
}
private void restoreClusters() {
for (DbCluster cluster : allClusters()) {
if (cluster.getStatus() == DbInstanceStatus.DELETING) {
@@ -524,13 +801,13 @@ public class RdsService {
}
int proxyPort = reserveOrAllocateProxyPort(cluster.getProxyPort());
cluster.setProxyPort(proxyPort);
cluster.setEndpoint(new DbEndpoint("localhost", proxyPort));
cluster.setReaderEndpoint(new DbEndpoint("localhost", proxyPort));
cluster.setEndpoint(new DbEndpoint(proxyEndpointHost(), proxyPort));
cluster.setReaderEndpoint(new DbEndpoint(proxyEndpointHost(), proxyPort));
if (cluster.getDockerVolumeName() == null) {
cluster.setDockerVolumeName(volumeName(cluster.getVolumeId(), cluster.getDbClusterIdentifier()));
}
try {
String image = imageForEngine(cluster.getEngine());
String image = imageForEngine(cluster.getEngine(), cluster.getEngineVersion());
RdsContainerHandle handle = containerManager.start(cluster.getDbClusterIdentifier(),
cluster.getVolumeId(), cluster.getEngine(), image,
cluster.getMasterUsername(), cluster.getMasterPassword(), cluster.getDatabaseName());
@@ -560,7 +837,7 @@ public class RdsService {
}
int proxyPort = reserveOrAllocateProxyPort(instance.getProxyPort());
instance.setProxyPort(proxyPort);
instance.setEndpoint(new DbEndpoint("localhost", proxyPort));
instance.setEndpoint(new DbEndpoint(proxyEndpointHost(), proxyPort));
try {
String backendHost;
int backendPort;
@@ -587,7 +864,7 @@ public class RdsService {
if (instance.getDockerVolumeName() == null) {
instance.setDockerVolumeName(volumeName(instance.getVolumeId(), instance.getDbInstanceIdentifier()));
}
String image = imageForEngine(instance.getEngine());
String image = imageForEngine(instance.getEngine(), instance.getEngineVersion());
RdsContainerHandle handle = containerManager.start(instance.getDbInstanceIdentifier(),
instance.getVolumeId(), instance.getEngine(), image,
instance.getMasterUsername(), instance.getMasterPassword(), instance.getDbName());
@@ -1,5 +1,7 @@
package io.github.hectorvent.floci.services.rds.container;
import com.github.dockerjava.api.async.ResultCallback;
import com.github.dockerjava.api.model.Frame;
import io.github.hectorvent.floci.config.EmulatorConfig;
import io.github.hectorvent.floci.core.common.RegionResolver;
import io.github.hectorvent.floci.core.common.ServiceConfigAccess;
@@ -16,14 +18,17 @@ import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import org.jboss.logging.Logger;
import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
/**
* Manages backend Docker container lifecycle for RDS DB instances and clusters.
@@ -105,6 +110,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);
RdsContainerHandle handle = new RdsContainerHandle(
info.containerId(), instanceId, endpoint.host(), endpoint.port());
@@ -165,6 +171,103 @@ public class RdsContainerManager {
};
}
private void initializeEngine(String containerName, String containerId, DatabaseEngine engine, String masterUsername) {
if (engine == DatabaseEngine.POSTGRES) {
initializePostgresIamRole(containerName, containerId, masterUsername);
}
}
static String postgresIamRoleInitSql() {
return """
DO $$
BEGIN
IF NOT EXISTS (SELECT 1 FROM pg_roles WHERE rolname = 'rds_iam') THEN
CREATE ROLE rds_iam;
END IF;
END
$$;
""";
}
private void initializePostgresIamRole(String containerName, String containerId, String masterUsername) {
String effectiveUser = (masterUsername != null && !masterUsername.isBlank()) ? masterUsername : "postgres";
String[] cmd = {
"psql",
"-v", "ON_ERROR_STOP=1",
"-U", effectiveUser,
"-d", "postgres",
"-c", postgresIamRoleInitSql()
};
String lastOutput = "";
for (int attempt = 1; attempt <= 60; attempt++) {
try {
ContainerExecResult result = execInContainer(containerId, cmd, 5);
lastOutput = result.output();
if (result.exitCode() == 0) {
LOG.infov("Initialized PostgreSQL IAM role in RDS container {0}", containerName);
return;
}
} catch (Exception e) {
lastOutput = e.getMessage();
}
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new IllegalStateException("Interrupted initializing PostgreSQL IAM role in " + containerName, e);
}
}
throw new IllegalStateException("Timed out initializing PostgreSQL IAM role in " + containerName + ": " + lastOutput);
}
private ContainerExecResult execInContainer(String containerId, String[] cmd, int timeoutSeconds) throws Exception {
String execId = lifecycleManager.getDockerClient().execCreateCmd(containerId)
.withCmd(cmd)
.withAttachStdout(true)
.withAttachStderr(true)
.exec()
.getId();
CountDownLatch latch = new CountDownLatch(1);
ByteArrayOutputStream output = new ByteArrayOutputStream();
Closeable callback = lifecycleManager.getDockerClient().execStartCmd(execId).exec(new ResultCallback.Adapter<Frame>() {
@Override
public void onNext(Frame frame) {
if (frame.getPayload() != null) {
try {
output.write(frame.getPayload());
} catch (IOException ignored) {
}
}
}
@Override
public void onComplete() {
latch.countDown();
}
@Override
public void onError(Throwable t) {
LOG.warnv(t, "Container exec {0} failed", execId);
latch.countDown();
}
});
try {
boolean completed = latch.await(timeoutSeconds, TimeUnit.SECONDS);
if (!completed) {
return new ContainerExecResult(-1, "Timed out after " + timeoutSeconds + "s");
}
Long exitCode = lifecycleManager.getDockerClient().inspectExecCmd(execId).exec().getExitCodeLong();
return new ContainerExecResult(
exitCode != null ? exitCode : -1,
output.toString(StandardCharsets.UTF_8));
} finally {
callback.close();
}
}
record ContainerExecResult(long exitCode, String output) {}
public void removeVolume(String instanceId, String volumeId) {
if (ContainerStorageHelper.isNamedVolumeMode(config)) {
ContainerStorageHelper.removeStorage(config, lifecycleManager, "rds", volumeId, instanceId);
@@ -3,6 +3,8 @@ package io.github.hectorvent.floci.services.rds.model;
import io.quarkus.runtime.annotations.RegisterForReflection;
import java.time.Instant;
import java.util.LinkedHashMap;
import java.util.Map;
@RegisterForReflection
public class DbInstance {
@@ -19,9 +21,14 @@ public class DbInstance {
private DbEndpoint endpoint;
private boolean iamDatabaseAuthenticationEnabled;
private String parameterGroupName;
private String dbSubnetGroupName;
private String dbClusterIdentifier;
private String dbiResourceId;
private String dbInstanceArn;
private String masterUserSecretArn;
private String masterUserSecretStatus;
private String masterUserSecretKmsKeyId;
private Map<String, String> tags = new LinkedHashMap<>();
private Instant createdAt;
private int proxyPort;
@@ -96,6 +103,9 @@ public class DbInstance {
public String getParameterGroupName() { return parameterGroupName; }
public void setParameterGroupName(String parameterGroupName) { this.parameterGroupName = parameterGroupName; }
public String getDbSubnetGroupName() { return dbSubnetGroupName; }
public void setDbSubnetGroupName(String dbSubnetGroupName) { this.dbSubnetGroupName = dbSubnetGroupName; }
public String getDbClusterIdentifier() { return dbClusterIdentifier; }
public void setDbClusterIdentifier(String dbClusterIdentifier) { this.dbClusterIdentifier = dbClusterIdentifier; }
@@ -105,6 +115,18 @@ public class DbInstance {
public String getDbInstanceArn() { return dbInstanceArn; }
public void setDbInstanceArn(String dbInstanceArn) { this.dbInstanceArn = dbInstanceArn; }
public String getMasterUserSecretArn() { return masterUserSecretArn; }
public void setMasterUserSecretArn(String masterUserSecretArn) { this.masterUserSecretArn = masterUserSecretArn; }
public String getMasterUserSecretStatus() { return masterUserSecretStatus; }
public void setMasterUserSecretStatus(String masterUserSecretStatus) { this.masterUserSecretStatus = masterUserSecretStatus; }
public String getMasterUserSecretKmsKeyId() { return masterUserSecretKmsKeyId; }
public void setMasterUserSecretKmsKeyId(String masterUserSecretKmsKeyId) { this.masterUserSecretKmsKeyId = masterUserSecretKmsKeyId; }
public Map<String, String> getTags() { return tags; }
public void setTags(Map<String, String> tags) { this.tags = tags != null ? new LinkedHashMap<>(tags) : new LinkedHashMap<>(); }
public Instant getCreatedAt() { return createdAt; }
public void setCreatedAt(Instant createdAt) { this.createdAt = createdAt; }
@@ -0,0 +1,39 @@
package io.github.hectorvent.floci.services.rds.model;
import io.quarkus.runtime.annotations.RegisterForReflection;
import java.util.ArrayList;
import java.util.List;
@RegisterForReflection
public class DbSubnetGroup {
private String dbSubnetGroupName;
private String dbSubnetGroupDescription;
private String vpcId;
private List<String> subnetIds = new ArrayList<>();
public DbSubnetGroup() {}
public DbSubnetGroup(String dbSubnetGroupName, String dbSubnetGroupDescription,
String vpcId, List<String> subnetIds) {
this.dbSubnetGroupName = dbSubnetGroupName;
this.dbSubnetGroupDescription = dbSubnetGroupDescription;
this.vpcId = vpcId;
this.subnetIds = subnetIds != null ? new ArrayList<>(subnetIds) : new ArrayList<>();
}
public String getDbSubnetGroupName() { return dbSubnetGroupName; }
public void setDbSubnetGroupName(String dbSubnetGroupName) { this.dbSubnetGroupName = dbSubnetGroupName; }
public String getDbSubnetGroupDescription() { return dbSubnetGroupDescription; }
public void setDbSubnetGroupDescription(String dbSubnetGroupDescription) { this.dbSubnetGroupDescription = dbSubnetGroupDescription; }
public String getVpcId() { return vpcId; }
public void setVpcId(String vpcId) { this.vpcId = vpcId; }
public List<String> getSubnetIds() { return List.copyOf(subnetIds); }
public void setSubnetIds(List<String> subnetIds) {
this.subnetIds = subnetIds != null ? new ArrayList<>(subnetIds) : new ArrayList<>();
}
}
@@ -1,22 +1,44 @@
package io.github.hectorvent.floci.services.rds.proxy;
import org.jboss.logging.Logger;
import org.bouncycastle.asn1.x500.X500Name;
import org.bouncycastle.asn1.x509.Extension;
import org.bouncycastle.asn1.x509.GeneralName;
import org.bouncycastle.asn1.x509.GeneralNames;
import org.bouncycastle.cert.X509v3CertificateBuilder;
import org.bouncycastle.cert.jcajce.JcaX509CertificateConverter;
import org.bouncycastle.cert.jcajce.JcaX509v3CertificateBuilder;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.operator.ContentSigner;
import org.bouncycastle.operator.jcajce.JcaContentSignerBuilder;
import javax.crypto.Mac;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.SecretKeySpec;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocket;
import java.io.ByteArrayOutputStream;
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.math.BigInteger;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.KeyStore;
import java.security.MessageDigest;
import java.security.SecureRandom;
import java.security.Security;
import java.security.cert.X509Certificate;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Base64;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -26,7 +48,7 @@ import java.util.Map;
*
* <p>Flow:
* <ol>
* <li>Read client StartupMessage (handles SSL rejection)
* <li>Read client StartupMessage (handles PostgreSQL SSL negotiation)
* <li>Challenge client with AuthenticationCleartextPassword
* <li>Read client password
* <li>Validate (IAM SigV4 or plain password)
@@ -41,21 +63,23 @@ public class PostgresProtocolHandler {
private static final int SSL_REQUEST_CODE = 80877103;
private static final int STARTUP_PROTOCOL_VERSION = 196608; // v3.0
private static volatile SSLContext serverSslContext;
public static void handleAuth(Socket client, Socket backend,
String masterUsername, String masterPassword, String dbName,
boolean iamEnabled, RdsSigV4Validator sigV4,
PasswordValidator passwordValidator) throws IOException {
InputStream clientIn = client.getInputStream();
OutputStream clientOut = client.getOutputStream();
// Phase 1: Read client startup message (possibly preceded by SSL request)
String clientUsername = readStartupMessage(clientIn, clientOut);
if (clientUsername == null) {
StartupMessage startup = readStartupMessage(client);
if (startup == null) {
closeQuietly(client);
return;
}
client = startup.socket();
String clientUsername = startup.username();
InputStream clientIn = client.getInputStream();
OutputStream clientOut = client.getOutputStream();
// Phase 2: Challenge client with cleartext password request
sendMessage(clientOut, 'R', intBytes(3)); // AuthenticationCleartextPassword
@@ -133,8 +157,11 @@ public class PostgresProtocolHandler {
// ── Startup ───────────────────────────────────────────────────────────────
private static String readStartupMessage(InputStream in, OutputStream out) throws IOException {
private static StartupMessage readStartupMessage(Socket socket) throws IOException {
Socket currentSocket = socket;
while (true) {
InputStream in = currentSocket.getInputStream();
OutputStream out = currentSocket.getOutputStream();
int length = readInt32(in);
if (length < 8) {
return null;
@@ -142,8 +169,9 @@ public class PostgresProtocolHandler {
int proto = readInt32(in);
if (proto == SSL_REQUEST_CODE) {
out.write('N'); // Reject SSL
out.write('S');
out.flush();
currentSocket = acceptSsl(currentSocket);
continue;
}
@@ -155,10 +183,82 @@ public class PostgresProtocolHandler {
byte[] payload = new byte[length - 8];
readFully(in, payload);
Map<String, String> params = parseStartupParams(payload);
return params.getOrDefault("user", "postgres");
return new StartupMessage(currentSocket, params.getOrDefault("user", "postgres"));
}
}
static Socket acceptSsl(Socket socket) throws IOException {
try {
SSLSocket sslSocket = (SSLSocket) serverSslContext().getSocketFactory()
.createSocket(socket, socket.getInetAddress().getHostAddress(), socket.getPort(), true);
sslSocket.setUseClientMode(false);
sslSocket.startHandshake();
return sslSocket;
} catch (Exception e) {
throw new IOException("Unable to negotiate PostgreSQL SSL", e);
}
}
private static SSLContext serverSslContext() throws Exception {
SSLContext context = serverSslContext;
if (context != null) {
return context;
}
synchronized (PostgresProtocolHandler.class) {
context = serverSslContext;
if (context == null) {
context = createServerSslContext();
serverSslContext = context;
}
return context;
}
}
private static SSLContext createServerSslContext() throws Exception {
if (Security.getProvider(BouncyCastleProvider.PROVIDER_NAME) == null) {
Security.addProvider(new BouncyCastleProvider());
}
KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA");
generator.initialize(2048, new SecureRandom());
KeyPair keyPair = generator.generateKeyPair();
Instant now = Instant.now();
X500Name name = new X500Name("CN=localhost");
X509v3CertificateBuilder certificateBuilder = new JcaX509v3CertificateBuilder(
name,
new BigInteger(128, new SecureRandom()),
Date.from(now.minus(1, ChronoUnit.MINUTES)),
Date.from(now.plus(365, ChronoUnit.DAYS)),
name,
keyPair.getPublic());
certificateBuilder.addExtension(Extension.subjectAlternativeName, false, new GeneralNames(new GeneralName[] {
new GeneralName(GeneralName.dNSName, "localhost"),
new GeneralName(GeneralName.iPAddress, "127.0.0.1")
}));
ContentSigner signer = new JcaContentSignerBuilder("SHA256withRSA")
.setProvider(BouncyCastleProvider.PROVIDER_NAME)
.build(keyPair.getPrivate());
X509Certificate certificate = new JcaX509CertificateConverter()
.setProvider(BouncyCastleProvider.PROVIDER_NAME)
.getCertificate(certificateBuilder.build(signer));
char[] password = new char[0];
KeyStore keyStore = KeyStore.getInstance("PKCS12");
keyStore.load(null, password);
keyStore.setKeyEntry("floci-rds-postgres", keyPair.getPrivate(), password, new java.security.cert.Certificate[] {certificate});
KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
keyManagerFactory.init(keyStore, password);
SSLContext context = SSLContext.getInstance("TLS");
context.init(keyManagerFactory.getKeyManagers(), null, new SecureRandom());
return context;
}
private record StartupMessage(Socket socket, String username) {}
private static Map<String, String> parseStartupParams(byte[] data) {
Map<String, String> params = new HashMap<>();
int i = 0;