fix(rds,docdb): list DocumentDB clusters and instances in the RDS-family list form (#2623)

* fix(rds,docdb): list DocumentDB clusters and instances in the RDS-family list form

The list form of DescribeDBClusters and DescribeDBInstances answered from
one store: on the rds scope from RDS, on the docdb scope from DocumentDB,
so a DocumentDB cluster was invisible to every list while its identifier
form worked. A live account lists the whole family from either endpoint.
RdsQueryHandler now assembles the list from both stores, honouring the
db-cluster-id / db-instance-id filters in each and an engine filter across
them (case-insensitive, OR over values, refused for a name outside the
family's engine list as on AWS), and the controller sends the docdb-scope
list form there too. The identifier form keeps routing to the store that
owns the identifier. Both halves list the region the request is signed
for.

Closes #2613

* fix(rds): report and filter an Aurora member instance by its Aurora engine name

DbInstance collapsed aurora-postgresql and aurora-mysql onto the community
engine enum, so a cluster member reported Engine postgres and the engine
filter matched it as postgres, where a live account says and matches
aurora-postgresql. Keep the engine name the request gave (or the
cluster's, for a member created without one) on the instance, and read it
for both the Engine element and the filter; a record persisted before the
field existed falls back to the enum.

* fix(rds): give a persisted instance its engine name on restore

An instance written before engineIdentifier existed deserializes with it
null, and a runtime fallback to the enum would keep reporting an Aurora
member as postgres. Backfill it in restorePersistedRuntime from the state
that knows: the member's cluster for a member, the enum for a standalone
instance.

* fix(rds): never persist a guessed engine name for a member of a nameless cluster

A cluster persisted before its own engineIdentifier existed (#1829) has
none to hand down, and writing the member's enum in that case would store
postgres for what may be an Aurora member — a guess that outlives the
code able to tell. The backfill writes only what is certain: the
cluster's stored name for a member, the enum for a standalone instance;
a member of a nameless cluster is left unset.

* fix(rds,neptune): list Neptune clusters and instances in the RDS-family list form too

A live account lists a Neptune cluster from the rds, docdb and neptune
endpoints alike and selects it with engine=neptune. The list form now
merges Neptune rows the way it merges DocumentDB ones, and the neptune
scope's list form goes to the same assembly. The Neptune store is not
keyed by region, so its rows are scoped to the request's region by ARN.

* fix(rds): close the subnet-group tags test in rds.bats

---------

Co-authored-by: Priamo Germosen <6233424+pgermosen@users.noreply.github.com>
This commit is contained in:
Michal SchreiberandPriamo Germosen authored and GitHub committed 2026-08-28 09:37:29 -04:00
1 parent 7188b3ec9d
commit 3b0e31b888
14 files changed
+784 -10

No files matched your search

@@ -317,12 +317,22 @@ public class AwsQueryController {
}
yield rdsQueryHandler.handle(action, formParams, region);
}
case "neptune" -> neptuneQueryHandler.handle(action, formParams);
case "neptune" -> isFamilyListing(action, formParams)
? rdsQueryHandler.handle(action, formParams, region)
: neptuneQueryHandler.handle(action, formParams);
case "docdb" -> {
// The same check as on the rds scope: DocumentDB is reachable under either, and a
// create that skipped it here would put a second record under an ARN RDS owns.
Response clash = rdsAlreadyHoldsIdentifier(action, formParams, region);
yield clash != null ? clash : docDbQueryHandler.handle(action, formParams);
if (clash != null) {
yield clash;
}
// The list form answers for the whole RDS family on either endpoint; the RDS
// handler assembles it from both stores.
if (isFamilyListing(action, formParams)) {
yield rdsQueryHandler.handle(action, formParams, region);
}
yield docDbQueryHandler.handle(action, formParams);
}
case "email" -> sesQueryHandler.handle(action, formParams, region);
case "monitoring" -> cloudWatchMetricsQueryHandler.handle(action, formParams, region);
@@ -378,6 +388,18 @@ public class AwsQueryController {
* included — so one identifier keeps one answer. The collision is logged once, because the
* RDS record behind it is unreachable on this scope and only deleting one of the two fixes it.
*/
private static boolean isFamilyListing(String action, MultivaluedMap<String, String> formParams) {
return switch (action) {
case "DescribeDBClusters" -> isBlank(formParams.getFirst("DBClusterIdentifier"));
case "DescribeDBInstances" -> isBlank(formParams.getFirst("DBInstanceIdentifier"));
default -> false;
};
}
private static boolean isBlank(String value) {
return value == null || value.isBlank();
}
private boolean namesDocDbResource(String resourceName, String region) {
if (!docDbService.hasResourceWithArn(resourceName)) {
return false;
@@ -111,6 +111,19 @@ public class DocDbQueryHandler {
return Response.ok(AwsQueryResponse.envelope("DescribeDBClusters", AwsNamespaces.RDS, xml.build())).build();
}
/**
* The rows the list form of DescribeDBClusters would return, for the RDS-family listing that
* {@code RdsQueryHandler} assembles: a live account lists DocumentDB clusters from the RDS
* endpoint and the DocumentDB endpoint alike.
*/
public List<String> clusterRowsXml(String filterId) {
return service.listDbClusters(filterId).stream().map(this::clusterInnerXml).toList();
}
public List<String> instanceRowsXml(String filterId) {
return service.listDbInstances(filterId).stream().map(this::instanceInnerXml).toList();
}
private Response handleDescribeGlobalClusters(MultivaluedMap<String, String> params) {
// Global clusters are not modeled; an empty list is what completes the provider's read.
// Real SDKs sign DocumentDB with the "rds" scope and land on RdsQueryHandler instead;
@@ -14,6 +14,7 @@ import jakarta.ws.rs.core.Response;
import org.jboss.logging.Logger;
import java.util.Collection;
import java.util.List;
@ApplicationScoped
public class NeptuneQueryHandler {
@@ -92,6 +93,34 @@ public class NeptuneQueryHandler {
return Response.ok(AwsQueryResponse.envelope("DescribeDBClusters", AwsNamespaces.RDS, xml.build())).build();
}
/**
* The rows the list form of DescribeDBClusters would return for the region a request is signed
* for, for the RDS-family listing {@code RdsQueryHandler} assembles: a live account lists
* Neptune clusters from the RDS endpoint too. The Neptune store is not keyed by region, so the
* region is read off each record's ARN.
*/
public List<String> clusterRowsXml(String filterId, String region) {
return service.listDbClusters(filterId).stream()
.filter(c -> inRegion(c.getDbClusterArn(), region))
.map(this::clusterInnerXml)
.toList();
}
public List<String> instanceRowsXml(String filterId, String region) {
return service.listDbInstances(filterId).stream()
.filter(i -> inRegion(i.getDbInstanceArn(), region))
.map(this::instanceInnerXml)
.toList();
}
private static boolean inRegion(String arn, String region) {
if (region == null || region.isBlank()) {
return true;
}
String[] parts = arn == null ? new String[0] : arn.split(":", -1);
return parts.length >= 4 && region.equals(parts[3]);
}
private Response handleDeleteDbCluster(MultivaluedMap<String, String> params) {
String id = params.getFirst("DBClusterIdentifier");
if (id == null || id.isBlank()) {
@@ -9,6 +9,8 @@ import io.github.hectorvent.floci.services.rds.model.DatabaseEngine;
import io.github.hectorvent.floci.services.rds.model.DbCluster;
import io.github.hectorvent.floci.services.rds.model.DbClusterParameterGroup;
import io.github.hectorvent.floci.services.rds.model.DbEndpoint;
import io.github.hectorvent.floci.services.docdb.DocDbQueryHandler;
import io.github.hectorvent.floci.services.neptune.NeptuneQueryHandler;
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;
@@ -40,10 +42,15 @@ public class RdsQueryHandler {
private static final Logger LOG = Logger.getLogger(RdsQueryHandler.class);
private final RdsService service;
private final DocDbQueryHandler docDbQueryHandler;
private final NeptuneQueryHandler neptuneQueryHandler;
private final EmulatorConfig config;
@Inject
public RdsQueryHandler(RdsService service, EmulatorConfig config) {
public RdsQueryHandler(RdsService service, EmulatorConfig config, DocDbQueryHandler docDbQueryHandler,
NeptuneQueryHandler neptuneQueryHandler) {
this.docDbQueryHandler = docDbQueryHandler;
this.neptuneQueryHandler = neptuneQueryHandler;
this.service = service;
this.config = config;
}
@@ -186,9 +193,24 @@ public class RdsQueryHandler {
throw new AwsException("DBInstanceNotFound",
"DBInstance " + identifier + " not found.", 404);
}
List<String> engines = engineFilter(params);
XmlBuilder xml = new XmlBuilder().start("DBInstances");
for (DbInstance i : result) {
xml.start("DBInstance").raw(dbInstanceInnerXml(i)).end("DBInstance");
if (engines.isEmpty() || engines.contains(instanceEngine(i))) {
xml.start("DBInstance").raw(dbInstanceInnerXml(i)).end("DBInstance");
}
}
boolean listForm = (identifier == null || identifier.isBlank())
&& extractRdsFilterValues(params, "dbi-resource-id").isEmpty();
if (listForm && (engines.isEmpty() || engines.contains(DOCDB_ENGINE))) {
for (String row : docDbQueryHandler.instanceRowsXml(filterId)) {
xml.start("DBInstance").raw(row).end("DBInstance");
}
}
if (listForm && (engines.isEmpty() || engines.contains(NEPTUNE_ENGINE))) {
for (String row : neptuneQueryHandler.instanceRowsXml(filterId, region)) {
xml.start("DBInstance").raw(row).end("DBInstance");
}
}
xml.end("DBInstances").start("Marker").end("Marker");
return Response.ok(AwsQueryResponse.envelope("DescribeDBInstances", AwsNamespaces.RDS, xml.build())).build();
@@ -453,9 +475,27 @@ public class RdsQueryHandler {
throw new AwsException("DBClusterNotFoundFault",
"DBCluster " + identifier + " not found.", 404);
}
List<String> engines = engineFilter(params);
XmlBuilder xml = new XmlBuilder().start("DBClusters");
for (DbCluster c : result) {
xml.start("DBCluster").raw(dbClusterInnerXml(c)).end("DBCluster");
if (engines.isEmpty() || engines.contains(clusterEngine(c))) {
xml.start("DBCluster").raw(dbClusterInnerXml(c)).end("DBCluster");
}
}
// The list form covers the whole RDS family: a live account lists DocumentDB and
// Neptune clusters here too. The identifier form is routed to the store that owns the
// identifier.
if (identifier == null || identifier.isBlank()) {
if (engines.isEmpty() || engines.contains(DOCDB_ENGINE)) {
for (String row : docDbQueryHandler.clusterRowsXml(filterId)) {
xml.start("DBCluster").raw(row).end("DBCluster");
}
}
if (engines.isEmpty() || engines.contains(NEPTUNE_ENGINE)) {
for (String row : neptuneQueryHandler.clusterRowsXml(filterId, region)) {
xml.start("DBCluster").raw(row).end("DBCluster");
}
}
}
xml.end("DBClusters").start("Marker").end("Marker");
return Response.ok(AwsQueryResponse.envelope("DescribeDBClusters", AwsNamespaces.RDS, xml.build())).build();
@@ -1198,7 +1238,7 @@ public class RdsQueryHandler {
private String dbInstanceInnerXml(DbInstance i) {
DbEndpoint ep = i.getEndpoint();
String engineStr = i.getEngine() != null ? i.getEngine().name() : "";
String engineStr = instanceEngine(i);
String statusStr = i.getStatus() != null ? statusLabel(i.getStatus()) : "available";
XmlBuilder xml = new XmlBuilder()
@@ -1596,6 +1636,53 @@ public class RdsQueryHandler {
* {@code Filters.Filter.N.Name=filterName} / {@code Filters.Filter.N.Values.Value.1=value}.
* Returns null if no matching filter is present.
*/
private static final String DOCDB_ENGINE = "docdb";
private static final String NEPTUNE_ENGINE = "neptune";
/**
* Every engine name the RDS family knows (the CreateDBInstance / CreateDBCluster lists in the
* API reference, plus DocumentDB and Neptune, which share the API and whose records the list
* form merges in). A live account refuses an {@code engine} filter naming anything else, and
* answers an empty list for a known engine it holds nothing of — including ones Floci cannot
* create.
*/
private static final java.util.Set<String> KNOWN_ENGINES = java.util.Set.of(
"aurora", "aurora-mysql", "aurora-postgresql", "mysql", "mariadb", "postgres",
"custom-oracle-ee", "custom-oracle-ee-cdb", "custom-oracle-se2", "custom-oracle-se2-cdb",
"custom-sqlserver-dev", "custom-sqlserver-ee", "custom-sqlserver-se", "custom-sqlserver-web",
"db2-ae", "db2-se", "oracle-ee", "oracle-ee-cdb", "oracle-se2", "oracle-se2-cdb",
"sqlserver-ee", "sqlserver-ex", "sqlserver-se", "sqlserver-web",
DOCDB_ENGINE, NEPTUNE_ENGINE);
/** The {@code engine} filter, lower-cased: a live account matches engine names case-insensitively. */
private static List<String> engineFilter(MultivaluedMap<String, String> params) {
List<String> engines = extractRdsFilterValues(params, "engine").stream().map(String::toLowerCase).toList();
for (String engine : engines) {
if (!KNOWN_ENGINES.contains(engine)) {
throw new AwsException("InvalidParameterValue", "Unrecognized engine name: " + engine, 400);
}
}
return engines;
}
private static String clusterEngine(DbCluster c) {
String engine = c.getEngineIdentifier() != null
? c.getEngineIdentifier()
: c.getEngine() != null ? c.getEngine().name() : "";
return engine.toLowerCase();
}
/**
* The engine name AWS reports for the instance: the one the request gave (an Aurora member
* says aurora-postgresql, not postgres), or the enum for a record persisted before it was kept.
*/
private static String instanceEngine(DbInstance i) {
if (i.getEngineIdentifier() != null && !i.getEngineIdentifier().isBlank()) {
return i.getEngineIdentifier().toLowerCase();
}
return i.getEngine() != null ? i.getEngine().name().toLowerCase() : "";
}
private static String extractRdsFilterValue(MultivaluedMap<String, String> params, String filterName) {
List<String> values = extractRdsFilterValues(params, filterName);
return values.isEmpty() ? null : values.getFirst();
@@ -338,6 +338,7 @@ public class RdsService implements Resettable, ResourceProvider {
restoreInstances();
restoreProxies();
backfillManagedSecretOwnership();
backfillInstanceEngineIdentifiers();
}
public void clear() {
@@ -545,6 +546,8 @@ public class RdsService implements Resettable, ResourceProvider {
String instanceStorageResourceId = dbiResourceId;
PlacementResolution placement;
String engineIdentifier = engineParam != null && !engineParam.isBlank()
? engineParam.toLowerCase() : null;
if (dbClusterIdentifier != null && !dbClusterIdentifier.isBlank()) {
// Cluster member — share the cluster's container (none exists in mock mode)
DbCluster cluster = Optional.ofNullable(
@@ -553,6 +556,9 @@ public class RdsService implements Resettable, ResourceProvider {
.orElseThrow(() ->
new AwsException("DBClusterNotFoundFault",
"DB cluster " + dbClusterIdentifier + " not found.", 404));
if (engineIdentifier == null && cluster.getEngineIdentifier() != null) {
engineIdentifier = cluster.getEngineIdentifier().toLowerCase();
}
backendHost = cluster.getContainerHost();
backendPort = cluster.getContainerPort();
containerId = cluster.getContainerId();
@@ -608,6 +614,7 @@ public class RdsService implements Resettable, ResourceProvider {
instance.setMultiAz(placement.multiAz());
instance.setSubnetAvailabilityZones(placement.subnetAvailabilityZones());
instance.setAutoMinorVersionUpgrade(autoMinorVersionUpgrade);
instance.setEngineIdentifier(engineIdentifier);
instance.setDbiResourceId(dbiResourceId);
instance.setDbInstanceArn(dbInstanceArn);
@@ -838,6 +845,47 @@ public class RdsService implements Resettable, ResourceProvider {
}
}
/**
* Gives an instance persisted before {@code engineIdentifier} existed the engine name AWS would
* report, and only when that name is known for certain: a cluster member takes its cluster's
* stored name (an Aurora member is aurora-postgresql, which the enum alone cannot say); a
* standalone instance takes the enum's, since a standalone RDS instance is never Aurora. A
* member whose cluster predates the field too is left unset rather than written down as the
* enum — a persisted guess would outlive the code that could later tell.
*/
void backfillInstanceEngineIdentifiers() {
try {
for (DbInstance instance : allInstances()) {
if (instance.getEngineIdentifier() != null && !instance.getEngineIdentifier().isBlank()) {
continue;
}
String accountId = accountIdFromArn(instance.getDbInstanceArn());
String region = regionFromArn(instance.getDbInstanceArn());
String engineIdentifier;
String clusterId = instance.getDbClusterIdentifier();
if (clusterId != null && !clusterId.isBlank()) {
DbCluster cluster = findClusterForScope(accountId, region, clusterId);
if (cluster == null || cluster.getEngineIdentifier() == null
|| cluster.getEngineIdentifier().isBlank()) {
LOG.debugv("Instance {0} keeps no engine name: its cluster {1} has none stored",
instance.getDbInstanceIdentifier(), clusterId);
continue;
}
engineIdentifier = cluster.getEngineIdentifier().toLowerCase();
} else if (instance.getEngine() != null) {
engineIdentifier = instance.getEngine().name().toLowerCase();
} else {
continue;
}
instance.setEngineIdentifier(engineIdentifier);
putInstanceForScope(accountId, region, instance.getDbInstanceIdentifier(), instance);
}
} catch (RuntimeException e) {
// Reading persisted state must not be able to stop the emulator from starting.
LOG.warnv(e, "Skipped the instance engine name backfill");
}
}
private void attachManagedMasterUserSecret(DbInstance instance, String region, String kmsKeyId) {
if (secretsManagerService == null) {
throw new AwsException("InvalidParameterCombination",
@@ -13,6 +13,9 @@ public class DbInstance {
private String dbInstanceIdentifier;
private DatabaseEngine engine;
// the engine name the request gave (aurora-postgresql, postgres, ...): the enum collapses the
// Aurora names onto the community engine that backs them, which is not what AWS reports
private String engineIdentifier;
private String engineVersion;
private String masterUsername;
private String masterPassword;
@@ -120,6 +123,9 @@ public class DbInstance {
public String getDbSubnetGroupName() { return dbSubnetGroupName; }
public void setDbSubnetGroupName(String dbSubnetGroupName) { this.dbSubnetGroupName = dbSubnetGroupName; }
public String getEngineIdentifier() { return engineIdentifier; }
public void setEngineIdentifier(String engineIdentifier) { this.engineIdentifier = engineIdentifier; }
public String getDbClusterIdentifier() { return dbClusterIdentifier; }
public void setDbClusterIdentifier(String dbClusterIdentifier) { this.dbClusterIdentifier = dbClusterIdentifier; }