Light, fluffy, and always free - The AWS Local Emulator alternative
feat(rds): refuse role feature names the engine does not support (#5637)
Davidff32bae98c17
diff --git a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/RdsControlPlaneTest.java b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/RdsControlPlaneTest.javaindex 7d4dc055bd..b989b1b07f 100644--- a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/RdsControlPlaneTest.java+++ b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/RdsControlPlaneTest.java@@ -790,6 +790,11 @@ class RdsControlPlaneTest {.containsExactly(s3Role, lambdaRole);assertThatThrownBy(() -> rds.addRoleToDBCluster(b -> b.dbClusterIdentifier(clusterName).roleArn(s3Role))).isInstanceOf(DbClusterRoleAlreadyExistsException.class);+ // A feature the engine does not list in SupportedFeatureNames is refused.+ assertThatThrownBy(() -> rds.addRoleToDBCluster(b -> b.dbClusterIdentifier(clusterName)+ .roleArn("arn:aws:iam::000000000000:role/rds-sqlserver-s3").featureName("S3_INTEGRATION")))+ .isInstanceOfSatisfying(RdsException.class,+ e -> assertThat(e.awsErrorDetails().errorCode()).isEqualTo("InvalidParameterValue"));rds.removeRoleFromDBCluster(b -> b.dbClusterIdentifier(clusterName).roleArn(s3Role));assertThatThrownBy(() -> rds.removeRoleFromDBCluster(b -> bdiff --git a/docs/services/rds.md b/docs/services/rds.mdindex 2a85e7b46c..c485fe9dc6 100644--- a/docs/services/rds.md+++ b/docs/services/rds.md@@ -634,9 +634,12 @@ the feature named; an association that does not exist is `DBClusterRoleNotFound``DBInstanceRoleNotFound`. A stopped cluster or instance refuses both (`InvalidDBClusterStateFault` /`InvalidDBInstanceState`).-Associations are recorded, not exercised: Floci does not check the role in IAM, does not check the-feature name against the engine, and does not hand the role's credentials to the database, so an-`aws_s3` import still needs credentials of its own. Deleting a cluster or instance drops its+A `FeatureName` must be one the engine lists in `SupportedFeatureNames` (`DescribeDBEngineVersions`),+matched without regard to case; any other is `InvalidParameterValue`, and a `CreateDBCluster` naming+one creates nothing. MySQL, MariaDB and Aurora MySQL list none, so their roles take no feature name.++Associations are recorded, not exercised: Floci does not check the role in IAM and does not hand the+role's credentials to the database, so an `aws_s3` import still needs credentials of its own. Deleting a cluster or instance drops itsassociations, and snapshots, restores and read replicas start without any.## Aurora managed password opt-outdiff --git a/src/main/java/io/github/hectorvent/floci/services/rds/RdsEngineCatalog.java b/src/main/java/io/github/hectorvent/floci/services/rds/RdsEngineCatalog.javaindex 45c838b80e..0d90ae89d8 100644--- a/src/main/java/io/github/hectorvent/floci/services/rds/RdsEngineCatalog.java+++ b/src/main/java/io/github/hectorvent/floci/services/rds/RdsEngineCatalog.java@@ -85,7 +85,7 @@ final class RdsEngineCatalog {private static final Map<String, List<String>> FEATURE_NAMES = Map.of("postgres", List.of("s3Import", "s3Export", "Lambda"),"sqlserver-se", List.of("S3_INTEGRATION"),- "aurora-postgresql", List.of("s3Import", "s3Export", "Lambda", "Comprehend", "SageMaker"));+ "aurora-postgresql", List.of("s3Import", "s3Export", "Lambda", "Comprehend", "SageMaker", "Bedrock"));static List<String> exportableLogTypes(EngineVersion version) {return EXPORTABLE_LOG_TYPES.getOrDefault(version.engine(), List.of());@@ -95,6 +95,26 @@ final class RdsEngineCatalog {return FEATURE_NAMES.getOrDefault(version.engine(), List.of());}+ /**+ * The role feature names an engine supports, or null when the catalogue does not list the+ * engine. {@code aurora} is Aurora MySQL's older name, and the SQL Server editions (or the bare+ * {@code sqlserver} older state records) share one entry.+ */+ static List<String> featureNamesForEngine(String engine) {+ if (engine == null) {+ return null;+ }+ String catalogued = switch (engine.toLowerCase(Locale.ROOT)) {+ case "aurora" -> "aurora-mysql";+ case "sqlserver", "sqlserver-ee", "sqlserver-ex", "sqlserver-web" -> "sqlserver-se";+ default -> engine.toLowerCase(Locale.ROOT);+ };+ if (VERSIONS.stream().noneMatch(v -> v.engine().equals(catalogued))) {+ return null;+ }+ return FEATURE_NAMES.getOrDefault(catalogued, List.of());+ }+/** Floci creates read replicas of PostgreSQL instances only. */static boolean supportsReadReplica(EngineVersion version) {return version.engine().equals("postgres");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.javaindex 8ff5ec2404..ab8a345c59 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@@ -1179,7 +1179,7 @@ public class RdsQueryHandler {try {List<DbRoleAssociation> associatedRoles = parseAssociatedRoles(params);- service.validateClusterRoleAssociations(id, associatedRoles);+ service.validateClusterRoleAssociations(id, engine, associatedRoles);Double serverlessV2Min = parseDoubleParam(params, "ServerlessV2ScalingConfiguration.MinCapacity");Double serverlessV2Max = parseDoubleParam(params, "ServerlessV2ScalingConfiguration.MaxCapacity");Integer serverlessV2SecondsUntilAutoPause = parseIntegerParam(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.javaindex 2002ab14f4..6bbeac5011 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@@ -9743,6 +9743,7 @@ public class RdsService implements Resettable, ResourceProvider {String effectiveRegion = effectiveRegion(region);DbCluster cluster = getDbCluster(clusterId, effectiveRegion);requireAvailableCluster(cluster, clusterId);+ requireSupportedFeature(engineName(cluster), featureName);requireRoleNotAssociated(cluster, clusterId, roleArn);cluster.getAssociatedRoles().add(new DbRoleAssociation(roleArn, blankToNull(featureName)));putClusterForScope(currentAccountId(), effectiveRegion, clusterId, cluster);@@ -9761,6 +9762,7 @@ public class RdsService implements Resettable, ResourceProvider {DbCluster cluster = getDbCluster(clusterId, effectiveRegion);requireAvailableCluster(cluster, clusterId);for (DbRoleAssociation role : roles) {+ requireSupportedFeature(engineName(cluster), role.getFeatureName());requireRoleNotAssociated(cluster, clusterId, role.getRoleArn());}for (DbRoleAssociation role : roles) {@@ -9793,7 +9795,7 @@ public class RdsService implements Resettable, ResourceProvider {requireAvailableCluster(cluster, clusterId);String feature = blankToNull(featureName);boolean removed = cluster.getAssociatedRoles().removeIf(existing -> existing.getRoleArn().equals(roleArn)- && (feature == null || feature.equals(existing.getFeatureName())));+ && (feature == null || feature.equalsIgnoreCase(existing.getFeatureName())));if (!removed) {throw new AwsException("DBClusterRoleNotFound", "Role ARN " + roleArn+ (feature == null ? "" : " for feature " + feature)@@ -9816,8 +9818,9 @@ public class RdsService implements Resettable, ResourceProvider {String effectiveRegion = effectiveRegion(region);DbInstance instance = getDbInstance(instanceId, effectiveRegion);requireAvailableInstance(instance, instanceId);+ requireSupportedFeature(engineName(instance), featureName);for (DbRoleAssociation existing : instance.getAssociatedRoles()) {- if (existing.getRoleArn().equals(roleArn) || featureName.equals(existing.getFeatureName())) {+ if (existing.getRoleArn().equals(roleArn) || featureName.equalsIgnoreCase(existing.getFeatureName())) {throw new AwsException("DBInstanceRoleAlreadyExists", "Role ARN " + existing.getRoleArn()+ " is already associated with DB instance " + instanceId + " for feature "+ existing.getFeatureName() + ".", 400);@@ -9839,7 +9842,7 @@ public class RdsService implements Resettable, ResourceProvider {DbInstance instance = getDbInstance(instanceId, effectiveRegion);requireAvailableInstance(instance, instanceId);boolean removed = instance.getAssociatedRoles().removeIf(existing -> existing.getRoleArn().equals(roleArn)- && featureName.equals(existing.getFeatureName()));+ && featureName.equalsIgnoreCase(existing.getFeatureName()));if (!removed) {throw new AwsException("DBInstanceRoleNotFound", "Role ARN " + roleArn+ " is not associated with DB instance " + instanceId + " for feature " + featureName + ".", 404);@@ -9852,12 +9855,17 @@ public class RdsService implements Resettable, ResourceProvider {/*** Checks the roles a {@code CreateDBCluster} request names before the cluster exists, by the* rules {@link #addRoleToDbCluster} applies one at a time, so a list that would be refused part- * way through refuses the create instead of leaving a cluster with half its roles.+ * way through refuses the create instead of leaving a cluster with half its roles. The feature+ * names are checked against {@code engine} when the request names one; a create that takes its+ * engine from a global cluster has them checked once the cluster exists.*/- public void validateClusterRoleAssociations(String clusterId, List<DbRoleAssociation> roles) {+ public void validateClusterRoleAssociations(String clusterId, String engine, List<DbRoleAssociation> roles) {Set<String> roleArns = new HashSet<>();for (DbRoleAssociation role : roles) {requireParameter(role.getRoleArn(), "AssociatedRoles.RoleArn");+ if (!isBlank(engine)) {+ requireSupportedFeature(engine, role.getFeatureName());+ }if (!roleArns.add(role.getRoleArn())) {throw new AwsException("DBClusterRoleAlreadyExists", "Role ARN " + role.getRoleArn()+ " is named more than once for DB cluster " + clusterId + ".", 400);@@ -9869,6 +9877,35 @@ public class RdsService implements Resettable, ResourceProvider {return isBlank(value) ? null : value;}+ /**+ * Refuses a feature name the engine's {@code SupportedFeatureNames} does not list, matched+ * without regard to case. No feature name (a cluster role may have none) and an engine the+ * catalogue does not list pass, so Floci never refuses a feature it cannot judge.+ */+ private static void requireSupportedFeature(String engine, String featureName) {+ if (isBlank(featureName)) {+ return;+ }+ List<String> supported = RdsEngineCatalog.featureNamesForEngine(engine);+ if (supported == null || supported.stream().anyMatch(name -> name.equalsIgnoreCase(featureName))) {+ return;+ }+ throw new AwsException("InvalidParameterValue", "The feature " + featureName+ + " is not supported for engine " + engine.toLowerCase(Locale.ROOT) + ". Supported features: "+ + (supported.isEmpty() ? "none" : String.join(", ", supported)) + ".", 400);+ }++ /** The engine name the request gave, or the engine's own name for state recorded without one. */+ private static String engineName(DbCluster cluster) {+ return !isBlank(cluster.getEngineIdentifier()) ? cluster.getEngineIdentifier()+ : cluster.getEngine() != null ? cluster.getEngine().name() : null;+ }++ private static String engineName(DbInstance instance) {+ return !isBlank(instance.getEngineIdentifier()) ? instance.getEngineIdentifier()+ : instance.getEngine() != null ? instance.getEngine().name() : null;+ }+private static void requireParameter(String value, String name) {if (isBlank(value)) {throw new AwsException("MissingParameter", "The request must contain the parameter " + name + ".", 400);diff --git a/src/test/java/io/github/hectorvent/floci/services/rds/RdsCatalogueReadIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/rds/RdsCatalogueReadIntegrationTest.javaindex d9853c4933..1910641a97 100644--- a/src/test/java/io/github/hectorvent/floci/services/rds/RdsCatalogueReadIntegrationTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/rds/RdsCatalogueReadIntegrationTest.java@@ -102,6 +102,11 @@ class RdsCatalogueReadIntegrationTest {.body(VERSIONS + ".SupportsReadReplica", equalTo("false")).body(containsString("<SupportedFeatureNames></SupportedFeatureNames>")).body(VERSIONS + ".SupportsGlobalDatabases", equalTo("true"));+ query("DescribeDBEngineVersions").formParam("Engine", "aurora-postgresql").formParam("DefaultOnly", "true")+ .when().post("/").then()+ .statusCode(200)+ .body(VERSIONS + ".SupportedFeatureNames.member",+ contains("s3Import", "s3Export", "Lambda", "Comprehend", "SageMaker", "Bedrock"));}@Testdiff --git a/src/test/java/io/github/hectorvent/floci/services/rds/RdsRoleFeatureNameIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/rds/RdsRoleFeatureNameIntegrationTest.javanew file mode 100644index 0000000000..b1877334c5--- /dev/null+++ b/src/test/java/io/github/hectorvent/floci/services/rds/RdsRoleFeatureNameIntegrationTest.java@@ -0,0 +1,159 @@+package io.github.hectorvent.floci.services.rds;++import io.github.hectorvent.floci.testing.RdsMockProfile;+import io.quarkus.test.junit.QuarkusTest;+import io.quarkus.test.junit.TestProfile;+import io.restassured.specification.RequestSpecification;+import org.junit.jupiter.api.MethodOrderer;+import org.junit.jupiter.api.Order;+import org.junit.jupiter.api.Test;+import org.junit.jupiter.api.TestMethodOrder;++import static io.restassured.RestAssured.given;+import static io.restassured.http.ContentType.URLENC;+import static org.hamcrest.Matchers.containsString;+import static org.hamcrest.Matchers.equalTo;++/**+ * A role's {@code FeatureName} has to be one the engine lists in {@code SupportedFeatureNames}, on+ * {@code AddRoleToDBInstance}, {@code AddRoleToDBCluster} and {@code CreateDBCluster}.+ */+@QuarkusTest+@TestProfile(RdsMockProfile.class)+@TestMethodOrder(MethodOrderer.OrderAnnotation.class)+class RdsRoleFeatureNameIntegrationTest {++ private static final String ROLE = "arn:aws:iam::000000000000:role/rds-feature";+ private static final String OTHER_ROLE = "arn:aws:iam::000000000000:role/rds-feature-other";++ private static final String AUTH =+ "AWS4-HMAC-SHA256 Credential=test/20261014/us-east-1/rds/aws4_request, "+ + "SignedHeaders=content-type;host, Signature=test";++ private static RequestSpecification query(String action) {+ return given().header("Authorization", AUTH)+ .contentType(URLENC)+ .formParam("Action", action)+ .formParam("Version", "2014-10-31");+ }++ private static void createInstance(String id, String engine) {+ query("CreateDBInstance")+ .formParam("DBInstanceIdentifier", id)+ .formParam("Engine", engine)+ .formParam("DBInstanceClass", "db.t3.micro")+ .formParam("MasterUsername", "admin")+ .formParam("MasterUserPassword", "password123")+ .formParam("AllocatedStorage", "20")+ .when().post("/").then().statusCode(200);+ }++ private static void createCluster(String id, String engine) {+ query("CreateDBCluster")+ .formParam("DBClusterIdentifier", id)+ .formParam("Engine", engine)+ .formParam("MasterUsername", "admin")+ .formParam("MasterUserPassword", "password123")+ .when().post("/").then().statusCode(200);+ }++ @Test+ @Order(1)+ void instanceFeatureMustBeOneTheEngineSupports() {+ createInstance("feature-mysql", "mysql");+ query("AddRoleToDBInstance").formParam("DBInstanceIdentifier", "feature-mysql")+ .formParam("RoleArn", ROLE).formParam("FeatureName", "Lambda")+ .when().post("/").then().statusCode(400)+ .body("ErrorResponse.Error.Code", equalTo("InvalidParameterValue"))+ .body("ErrorResponse.Error.Message", containsString("Supported features: none"));++ createInstance("feature-postgres", "postgres");+ query("AddRoleToDBInstance").formParam("DBInstanceIdentifier", "feature-postgres")+ .formParam("RoleArn", ROLE).formParam("FeatureName", "SageMaker")+ .when().post("/").then().statusCode(400)+ .body("ErrorResponse.Error.Code", equalTo("InvalidParameterValue"))+ .body("ErrorResponse.Error.Message", containsString("s3Import, s3Export, Lambda"));+ // Matched without regard to case, and kept as the request spelled it.+ query("AddRoleToDBInstance").formParam("DBInstanceIdentifier", "feature-postgres")+ .formParam("RoleArn", ROLE).formParam("FeatureName", "s3import")+ .when().post("/").then().statusCode(200);+ query("DescribeDBInstances").formParam("DBInstanceIdentifier", "feature-postgres")+ .when().post("/").then().statusCode(200)+ .body(containsString("<FeatureName>s3import</FeatureName>"));+ }++ @Test+ @Order(5)+ void theOtherFeatureChecksMatchWithoutRegardToCaseToo() {+ // feature-postgres holds ROLE for "s3import" (order 1). The same feature in another case is+ // the same feature: a second role for it is refused, and it finds the association to remove.+ query("AddRoleToDBInstance").formParam("DBInstanceIdentifier", "feature-postgres")+ .formParam("RoleArn", OTHER_ROLE).formParam("FeatureName", "s3Import")+ .when().post("/").then().statusCode(400)+ .body("ErrorResponse.Error.Code", equalTo("DBInstanceRoleAlreadyExists"));+ query("RemoveRoleFromDBInstance").formParam("DBInstanceIdentifier", "feature-postgres")+ .formParam("RoleArn", ROLE).formParam("FeatureName", "S3IMPORT")+ .when().post("/").then().statusCode(200);++ // feature-apg holds ROLE for "Bedrock" (order 2); a cluster removal matches it the same way.+ query("RemoveRoleFromDBCluster").formParam("DBClusterIdentifier", "feature-apg")+ .formParam("RoleArn", ROLE).formParam("FeatureName", "bedrock")+ .when().post("/").then().statusCode(200);+ }++ @Test+ @Order(2)+ void clusterFeatureMustBeOneTheEngineSupports() {+ createCluster("feature-apg", "aurora-postgresql");+ query("AddRoleToDBCluster").formParam("DBClusterIdentifier", "feature-apg")+ .formParam("RoleArn", ROLE).formParam("FeatureName", "Bedrock")+ .when().post("/").then().statusCode(200);+ query("AddRoleToDBCluster").formParam("DBClusterIdentifier", "feature-apg")+ .formParam("RoleArn", OTHER_ROLE).formParam("FeatureName", "S3_INTEGRATION")+ .when().post("/").then().statusCode(400)+ .body("ErrorResponse.Error.Code", equalTo("InvalidParameterValue"));++ // Aurora MySQL lists no features: its roles take none, and a named one is refused.+ createCluster("feature-ams", "aurora-mysql");+ query("AddRoleToDBCluster").formParam("DBClusterIdentifier", "feature-ams")+ .formParam("RoleArn", ROLE).formParam("FeatureName", "s3Import")+ .when().post("/").then().statusCode(400)+ .body("ErrorResponse.Error.Code", equalTo("InvalidParameterValue"));+ query("AddRoleToDBCluster").formParam("DBClusterIdentifier", "feature-ams")+ .formParam("RoleArn", ROLE)+ .when().post("/").then().statusCode(200);+ }++ @Test+ @Order(3)+ void createDbClusterWithAnUnsupportedFeatureCreatesNothing() {+ query("CreateDBCluster")+ .formParam("DBClusterIdentifier", "feature-create")+ .formParam("Engine", "aurora-mysql")+ .formParam("MasterUsername", "admin")+ .formParam("MasterUserPassword", "password123")+ .formParam("AssociatedRoles.DBClusterAssociatedRole.1.RoleArn", ROLE)+ .formParam("AssociatedRoles.DBClusterAssociatedRole.1.FeatureName", "Lambda")+ .when().post("/").then().statusCode(400)+ .body("ErrorResponse.Error.Code", equalTo("InvalidParameterValue"));+ query("DescribeDBClusters").formParam("DBClusterIdentifier", "feature-create")+ .when().post("/").then().statusCode(404);+ }++ @Test+ @Order(4)+ void createDbClusterWithoutAnEngineChecksTheFeatureOnceTheClusterExists() {+ // No Engine means PostgreSQL, which lists no Comprehend: the roles are refused after the+ // cluster is made, and the cluster is deleted again.+ query("CreateDBCluster")+ .formParam("DBClusterIdentifier", "feature-no-engine")+ .formParam("MasterUsername", "admin")+ .formParam("MasterUserPassword", "password123")+ .formParam("AssociatedRoles.DBClusterAssociatedRole.1.RoleArn", ROLE)+ .formParam("AssociatedRoles.DBClusterAssociatedRole.1.FeatureName", "Comprehend")+ .when().post("/").then().statusCode(400)+ .body("ErrorResponse.Error.Code", equalTo("InvalidParameterValue"));+ query("DescribeDBClusters").formParam("DBClusterIdentifier", "feature-no-engine")+ .when().post("/").then().statusCode(404);+ }+}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.javaindex e2b2f1dbe2..83445a61ca 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@@ -2948,6 +2948,17 @@ class RdsServiceTest {RdsEngineCatalog.VERSIONS.forEach(v -> assertEquals(1L, defaults.get(v.engine()), v.engine()));}+ @Test+ void supportedFeatureNamesResolveEngineAliases() {+ assertEquals(List.of("S3_INTEGRATION"), RdsEngineCatalog.featureNamesForEngine("sqlserver-ee"));+ assertEquals(List.of("S3_INTEGRATION"), RdsEngineCatalog.featureNamesForEngine("SQLSERVER"));+ assertEquals(List.of(), RdsEngineCatalog.featureNamesForEngine("aurora"));+ assertEquals(List.of("s3Import", "s3Export", "Lambda"), RdsEngineCatalog.featureNamesForEngine("POSTGRES"));+ // An engine the catalogue does not list is not judged.+ assertNull(RdsEngineCatalog.featureNamesForEngine("oracle-ee"));+ assertNull(RdsEngineCatalog.featureNamesForEngine(null));+ }+@Testvoid engineVersionsSortNumericallyForUpgradeTargets() {RdsEngineCatalog.EngineVersion pg163 = RdsEngineCatalog.VERSIONS.stream()