Light, fluffy, and always free - The AWS Local Emulator alternative
fix(rds): resolve AWS-managed default DB cluster parameter groups on reference (#1805)
Alex Bannab31cbb92315f
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 eb9f046069..a2ed77be7b 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@@ -36,6 +36,7 @@ import java.security.SecureRandom;import java.time.Instant;import java.util.ArrayList;import java.util.Collection;+import java.util.Comparator;import java.util.LinkedHashMap;import java.util.List;import java.util.Map;@@ -54,6 +55,26 @@ public class RdsService implements Resettable {private static final Logger LOG = Logger.getLogger(RdsService.class);private static final ObjectMapper JSON = new ObjectMapper();+ private static final List<ManagedClusterParameterGroup> MANAGED_CLUSTER_PARAMETER_GROUPS = List.of(+ managedDefault("aurora-mysql5.7"),+ managedDefault("aurora-mysql8.0"),+ managedDefault("aurora-mysql8.4"),+ managedDefault("aurora-postgresql11"),+ managedDefault("aurora-postgresql12"),+ managedDefault("aurora-postgresql13"),+ managedDefault("aurora-postgresql14"),+ managedDefault("aurora-postgresql15"),+ managedDefault("aurora-postgresql16"),+ managedDefault("aurora-postgresql17"),+ managedDefault("aurora-postgresql18"),+ managedDefault("mysql8.0"),+ managedDefault("mysql8.4"),+ managedDefault("postgres13"),+ managedDefault("postgres14"),+ managedDefault("postgres15"),+ managedDefault("postgres16"),+ managedDefault("postgres17"),+ managedDefault("postgres18"));private final StorageBackend<String, DbInstance> instances;private final StorageBackend<String, DbCluster> clusters;@@ -910,7 +931,7 @@ public class RdsService implements Resettable {// ── Cluster Parameter Groups ──────────────────────────────────────────────public DbClusterParameterGroup createDbClusterParameterGroup(String name, String family, String description) {- if (clusterParameterGroups.get(name).isPresent()) {+ if (managedClusterParameterGroup(name) != null || clusterParameterGroups.get(name).isPresent()) {throw new AwsException("DBParameterGroupAlreadyExists","DB cluster parameter group " + name + " already exists.", 400);}@@ -920,19 +941,71 @@ public class RdsService implements Resettable {}public DbClusterParameterGroup getDbClusterParameterGroup(String name) {- return clusterParameterGroups.get(name).orElseThrow(() ->- new AwsException("DBClusterParameterGroupNotFound",- "DBClusterParameterGroupName doesn't refer to an existing DB cluster parameter group.", 404));+ if (name != null) {+ DbClusterParameterGroup persisted = clusterParameterGroups.get(name).orElse(null);+ if (persisted != null) {+ return persisted;+ }+ ManagedClusterParameterGroup managed = managedClusterParameterGroup(name);+ if (managed != null) {+ return managed.toModel();+ }+ }+ throw new AwsException("DBClusterParameterGroupNotFound",+ "DBClusterParameterGroupName doesn't refer to an existing DB cluster parameter group.", 404);}public Collection<DbClusterParameterGroup> listDbClusterParameterGroups(String filterName) {if (filterName != null && !filterName.isBlank()) {- return clusterParameterGroups.get(filterName).map(List::of).orElse(List.of());+ try {+ return List.of(getDbClusterParameterGroup(filterName));+ } catch (AwsException e) {+ if ("DBClusterParameterGroupNotFound".equals(e.getErrorCode())) {+ throw new AwsException("DBParameterGroupNotFound",+ "DBParameterGroupName doesn't refer to an existing DB parameter group.", 404);+ }+ throw e;+ }+ }+ Map<String, DbClusterParameterGroup> groups = new LinkedHashMap<>();+ for (ManagedClusterParameterGroup managed : MANAGED_CLUSTER_PARAMETER_GROUPS) {+ groups.put(managed.name(), managed.toModel());+ }+ clusterParameterGroups.scan(k -> true).stream()+ .sorted(Comparator.comparing(+ DbClusterParameterGroup::getDbClusterParameterGroupName,+ Comparator.nullsLast(Comparator.naturalOrder())))+ .forEach(group -> groups.put(group.getDbClusterParameterGroupName(), group));+ return List.copyOf(groups.values());+ }++ private static ManagedClusterParameterGroup managedClusterParameterGroup(String name) {+ for (ManagedClusterParameterGroup group : MANAGED_CLUSTER_PARAMETER_GROUPS) {+ if (group.name().equals(name)) {+ return group;+ }+ }+ return null;+ }++ private static ManagedClusterParameterGroup managedDefault(String family) {+ return new ManagedClusterParameterGroup(+ "default." + family,+ family,+ "Default cluster parameter group");+ }++ private record ManagedClusterParameterGroup(String name, String family, String description) {+ private DbClusterParameterGroup toModel() {+ return new DbClusterParameterGroup(name, family, description);}- return clusterParameterGroups.scan(k -> true);}public void deleteDbClusterParameterGroup(String name) {+ if (managedClusterParameterGroup(name) != null) {+ throw new AwsException("InvalidDBParameterGroupState",+ "The default DB cluster parameter group cannot be deleted.", 400);+ }if (clusterParameterGroups.get(name).isEmpty()) {throw new AwsException("DBClusterParameterGroupNotFound","DBClusterParameterGroupName doesn't refer to an existing DB cluster parameter group.", 404);@@ -942,6 +1015,10 @@ public class RdsService implements Resettable {public DbClusterParameterGroup modifyDbClusterParameterGroup(String name,java.util.Map<String, String> parameters) {+ if (managedClusterParameterGroup(name) != null) {+ throw new AwsException("InvalidDBParameterGroupState",+ "The default DB cluster parameter group cannot be modified.", 400);+ }DbClusterParameterGroup group = getDbClusterParameterGroup(name);if (parameters != null) {group.getParameters().putAll(parameters);@@ -1010,7 +1087,41 @@ public class RdsService implements Resettable {return;}DbClusterParameterGroup group = getDbClusterParameterGroup(paramGroupName);- validateParameterGroupFamily(paramGroupName, group.getDbParameterGroupFamily(), engineParam, engineVersion);+ String family = group.getDbParameterGroupFamily();+ String expectedFamily = expectedClusterParameterGroupFamily(engineParam, engineVersion);+ if (family == null || !family.equalsIgnoreCase(expectedFamily)) {+ throw new AwsException("InvalidParameterCombination", invalidParameterCombinationMessage(), 400);+ }+ }++ private String expectedClusterParameterGroupFamily(String engineParam, String engineVersion) {+ String normalizedEngine = effectiveEngineName(engineParam).toLowerCase();+ String effectiveVersion = engineVersion;+ if (effectiveVersion == null || effectiveVersion.isBlank()) {+ effectiveVersion = switch (normalizedEngine) {+ case "postgres", "aurora-postgresql" -> "16.3";+ case "mysql", "aurora", "aurora-mysql" -> "8.0.36";+ case "mariadb" -> "11.2";+ default -> throw new AwsException("InvalidParameterValue", invalidParameterValueMessage(), 400);+ };+ }++ Matcher matcher = IMAGE_TAG_VERSION_PATTERN.matcher(effectiveVersion.trim());+ if (!matcher.matches()) {+ throw new AwsException("InvalidParameterValue", invalidParameterValueMessage(), 400);+ }+ String[] versionParts = matcher.group(1).split("\\.");+ String familyVersion = switch (normalizedEngine) {+ case "postgres", "aurora-postgresql" -> versionParts[0];+ case "mysql", "aurora", "aurora-mysql", "mariadb" -> {+ if (versionParts.length < 2) {+ throw new AwsException("InvalidParameterValue", invalidParameterValueMessage(), 400);+ }+ yield versionParts[0] + "." + versionParts[1];+ }+ default -> throw new AwsException("InvalidParameterValue", invalidParameterValueMessage(), 400);+ };+ return expectedFamilyPrefix(normalizedEngine) + familyVersion;}private void validateParameterGroupFamily(String groupName, String family, String engineParam, String engineVersion) {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 02df455e56..7e2cc3ca2c 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@@ -22,6 +22,8 @@ import io.github.hectorvent.floci.services.secretsmanager.SecretsManagerService;import io.github.hectorvent.floci.services.secretsmanager.model.Secret;import org.junit.jupiter.api.BeforeEach;import org.junit.jupiter.api.Test;+import org.junit.jupiter.params.ParameterizedTest;+import org.junit.jupiter.params.provider.CsvSource;import org.mockito.ArgumentCaptor;import java.util.Collection;@@ -42,6 +44,27 @@ import static org.mockito.Mockito.when;class RdsServiceTest {+ private static final List<String> CURRENT_MANAGED_CLUSTER_PARAMETER_GROUP_FAMILIES = List.of(+ "aurora-mysql5.7",+ "aurora-mysql8.0",+ "aurora-mysql8.4",+ "aurora-postgresql11",+ "aurora-postgresql12",+ "aurora-postgresql13",+ "aurora-postgresql14",+ "aurora-postgresql15",+ "aurora-postgresql16",+ "aurora-postgresql17",+ "aurora-postgresql18",+ "mysql8.0",+ "mysql8.4",+ "postgres13",+ "postgres14",+ "postgres15",+ "postgres16",+ "postgres17",+ "postgres18");+private RdsService rdsService;private RdsContainerManager containerManager;private RdsProxyManager proxyManager;@@ -574,6 +597,103 @@ class RdsServiceTest {assertEquals("DBClusterParameterGroupName doesn't refer to an existing DB cluster parameter group.", exception.getMessage());}+ @ParameterizedTest+ @CsvSource({+ "aurora-mysql, 5.7.mysql_aurora.2.12.4, aurora-mysql5.7",+ "aurora-mysql, 8.0.mysql_aurora.3.10.0, aurora-mysql8.0",+ "aurora-mysql, 8.4.mysql_aurora.8.4.7, aurora-mysql8.4",+ "aurora-postgresql, 11.21, aurora-postgresql11",+ "aurora-postgresql, 12.22, aurora-postgresql12",+ "aurora-postgresql, 13.18, aurora-postgresql13",+ "aurora-postgresql, 14.15, aurora-postgresql14",+ "aurora-postgresql, 15.10, aurora-postgresql15",+ "aurora-postgresql, 16.4, aurora-postgresql16",+ "aurora-postgresql, 17.4, aurora-postgresql17",+ "aurora-postgresql, 18.3, aurora-postgresql18",+ "mysql, 8.0.36, mysql8.0",+ "mysql, 8.4.7, mysql8.4",+ "postgres, 13.20, postgres13",+ "postgres, 14.17, postgres14",+ "postgres, 15.12, postgres15",+ "postgres, 16.8, postgres16",+ "postgres, 17.4, postgres17",+ "postgres, 18.1, postgres18"+ })+ void createDbClusterAcceptsCurrentAwsDefaultClusterParameterGroups(+ String engine, String engineVersion, String family) {+ String groupName = "default." + family;++ DbCluster cluster = rdsService.createDbCluster("cluster", engine, engineVersion,+ "admin", "password", "coredb", false, groupName);++ assertEquals("cluster", cluster.getDbClusterIdentifier());+ assertEquals(family, rdsService.getDbClusterParameterGroup(groupName).getDbParameterGroupFamily());+ }++ @Test+ void createDbClusterRejectsUnsupportedAwsDefaultClusterParameterGroup() {+ AwsException exception = assertThrows(AwsException.class, () ->+ rdsService.createDbCluster("aurora-cluster", "aurora-postgresql", "16.4",+ "admin", "password", "coredb", false,+ "default.aurora-postgresql999"));++ assertEquals("DBClusterParameterGroupNotFound", exception.getErrorCode());+ }++ @Test+ void listDbClusterParameterGroupsAlwaysIncludesStableManagedDefaults() {+ List<String> before = rdsService.listDbClusterParameterGroups(null).stream()+ .map(group -> group.getDbClusterParameterGroupName()+ + ":" + group.getDbParameterGroupFamily())+ .toList();++ Collection<DbClusterParameterGroup> groups =+ rdsService.listDbClusterParameterGroups("default.aurora-postgresql16");++ assertEquals(1, groups.size());+ DbClusterParameterGroup group = groups.iterator().next();+ assertEquals("default.aurora-postgresql16", group.getDbClusterParameterGroupName());+ assertEquals("aurora-postgresql16", group.getDbParameterGroupFamily());+ assertEquals("Default cluster parameter group", group.getDescription());+ List<String> after = rdsService.listDbClusterParameterGroups(null).stream()+ .map(item -> item.getDbClusterParameterGroupName()+ + ":" + item.getDbParameterGroupFamily())+ .toList();+ assertEquals(CURRENT_MANAGED_CLUSTER_PARAMETER_GROUP_FAMILIES.stream()+ .map(family -> "default." + family + ":" + family)+ .toList(), before);+ assertEquals(before, after);+ }++ @Test+ void unsupportedManagedDefaultNamesAreNotFabricatedByReads() {+ List<String> before = rdsService.listDbClusterParameterGroups(null).stream()+ .map(DbClusterParameterGroup::getDbClusterParameterGroupName)+ .toList();++ for (String name : List.of("default.", "default.not-a-real-family",+ "default.aurora-postgresql999", "default.mariadb11.2")) {+ AwsException exception = assertThrows(+ AwsException.class, () -> rdsService.getDbClusterParameterGroup(name));+ assertEquals("DBClusterParameterGroupNotFound", exception.getErrorCode());+ }++ assertEquals(before, rdsService.listDbClusterParameterGroups(null).stream()+ .map(DbClusterParameterGroup::getDbClusterParameterGroupName)+ .toList());+ }++ @Test+ void namedClusterParameterGroupListingUsesAwsNotFoundCode() {+ AwsException exception = assertThrows(AwsException.class, () ->+ rdsService.listDbClusterParameterGroups("default.not-a-real-family"));++ assertEquals("DBParameterGroupNotFound", exception.getErrorCode());+ assertEquals("DBParameterGroupName doesn't refer to an existing DB parameter group.",+ exception.getMessage());+ assertEquals(404, exception.getHttpStatus());+ }+@Testvoid createDbClusterRejectsIncompatibleClusterParameterGroupFamily() {rdsService.createDbClusterParameterGroup("cpg1", "aurora-mysql8.0", "test group");@@ -586,6 +706,22 @@ class RdsServiceTest {exception.getMessage());}+ @ParameterizedTest+ @CsvSource({+ "aurora-postgresql, 16.4, default.aurora-postgresql15",+ "aurora-mysql, 8.0.mysql_aurora.3.10.0, default.aurora-mysql5.7",+ "postgres, 16.8, default.postgres15",+ "mysql, 8.4.7, default.mysql8.0"+ })+ void createDbClusterRejectsManagedDefaultFromAnotherMajorVersion(+ String engine, String engineVersion, String groupName) {+ AwsException exception = assertThrows(AwsException.class, () ->+ rdsService.createDbCluster("cluster", engine, engineVersion,+ "admin", "password", "coredb", false, groupName));++ assertEquals("InvalidParameterCombination", exception.getErrorCode());+ }+@Testvoid createDbClusterParameterGroupRoundTrip() {DbClusterParameterGroup created = rdsService.createDbClusterParameterGroup(@@ -598,7 +734,42 @@ class RdsServiceTest {assertEquals("cpg1", fetched.getDbClusterParameterGroupName());Collection<DbClusterParameterGroup> listed = rdsService.listDbClusterParameterGroups(null);- assertEquals(1, listed.size());+ List<String> names = listed.stream()+ .map(DbClusterParameterGroup::getDbClusterParameterGroupName)+ .toList();+ assertEquals(CURRENT_MANAGED_CLUSTER_PARAMETER_GROUP_FAMILIES.size() + 1, names.size());+ assertTrue(names.containsAll(CURRENT_MANAGED_CLUSTER_PARAMETER_GROUP_FAMILIES.stream()+ .map(family -> "default." + family)+ .toList()));+ assertTrue(names.contains("cpg1"));+ }++ @Test+ void createDbClusterParameterGroupRejectsManagedDefaultName() {+ AwsException exception = assertThrows(AwsException.class, () ->+ rdsService.createDbClusterParameterGroup(+ "default.aurora-postgresql16", "aurora-postgresql16", "shadow"));++ assertEquals("DBParameterGroupAlreadyExists", exception.getErrorCode());+ }++ @Test+ void persistedManagedDefaultOverridesCatalogWithoutDuplication() {+ StorageBackend<String, DbClusterParameterGroup> clusterGroups = new InMemoryStorage<>();+ clusterGroups.put("default.aurora-postgresql16", new DbClusterParameterGroup(+ "default.aurora-postgresql16", "aurora-postgresql16", "persisted default"));+ RdsService service = newService(containerManager, proxyManager,+ new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>(),+ clusterGroups, new InMemoryStorage<>());++ List<DbClusterParameterGroup> listed = List.copyOf(service.listDbClusterParameterGroups(null));+ assertEquals(CURRENT_MANAGED_CLUSTER_PARAMETER_GROUP_FAMILIES.size(), listed.size());+ List<DbClusterParameterGroup> matching = listed.stream()+ .filter(group -> "default.aurora-postgresql16".equals(+ group.getDbClusterParameterGroupName()))+ .toList();+ assertEquals(1, matching.size());+ assertEquals("persisted default", matching.getFirst().getDescription());}@Test@@ -744,6 +915,31 @@ class RdsServiceTest {assertEquals("pg_stat_statements", modified.getParameters().get("shared_preload_libraries"));}+ @Test+ void modifyManagedDefaultClusterParameterGroupIsRejectedWithoutPersistingChanges() {+ String name = "default.aurora-postgresql16";++ AwsException exception = assertThrows(AwsException.class, () ->+ rdsService.modifyDbClusterParameterGroup(+ name, Map.of("log_statement", "all")));++ assertEquals("InvalidDBParameterGroupState", exception.getErrorCode());+ assertEquals(400, exception.getHttpStatus());+ assertTrue(rdsService.getDbClusterParameterGroup(name).getParameters().isEmpty());+ }++ @Test+ void deleteManagedDefaultClusterParameterGroupIsRejectedAndGroupRemainsResolvable() {+ String name = "default.aurora-postgresql16";++ AwsException exception = assertThrows(AwsException.class, () ->+ rdsService.deleteDbClusterParameterGroup(name));++ assertEquals("InvalidDBParameterGroupState", exception.getErrorCode());+ assertEquals(400, exception.getHttpStatus());+ assertEquals(name, rdsService.getDbClusterParameterGroup(name).getDbClusterParameterGroupName());+ }+@Testvoid deleteDbClusterParameterGroupMissingThrows() {AwsException exception = assertThrows(AwsException.class, () ->