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.java index f9e9281854..d5771448b0 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 @@ -192,6 +192,7 @@ public class RdsService { if (dbSubnetGroupName != null && !dbSubnetGroupName.isBlank()) { getDbSubnetGroup(dbSubnetGroupName); } + validateInstanceParameterGroup(paramGroupName, engineParam, engineVersion); int proxyPort = allocateProxyPort(); if (masterUsername == null || masterUsername.isBlank()) { masterUsername = "root"; @@ -485,6 +486,7 @@ public class RdsService { } DatabaseEngine engine = resolveEngine(engineParam); + validateClusterParameterGroup(paramGroupName, engineParam, engineVersion); int proxyPort = allocateProxyPort(); String image = imageForEngine(engine, engineVersion); String clusterVolumeId = String.format("%06x", new SecureRandom().nextInt(0xFFFFFF)); @@ -581,7 +583,7 @@ public class RdsService { public DbParameterGroup getDbParameterGroup(String name) { return parameterGroups.get(name).orElseThrow(() -> new AwsException("DBParameterGroupNotFound", - "DB parameter group " + name + " not found.", 404)); + "DBParameterGroupName doesn't refer to an existing DB parameter group.", 404)); } public Collection listDbParameterGroups(String filterName) { @@ -594,7 +596,7 @@ public class RdsService { public void deleteDbParameterGroup(String name) { if (parameterGroups.get(name).isEmpty()) { throw new AwsException("DBParameterGroupNotFound", - "DB parameter group " + name + " not found.", 404); + "DBParameterGroupName doesn't refer to an existing DB parameter group.", 404); } parameterGroups.delete(name); } @@ -669,8 +671,8 @@ public class RdsService { public DbClusterParameterGroup getDbClusterParameterGroup(String name) { return clusterParameterGroups.get(name).orElseThrow(() -> - new AwsException("DBParameterGroupNotFound", - "DB cluster parameter group " + name + " not found.", 404)); + new AwsException("DBClusterParameterGroupNotFound", + "DBClusterParameterGroupName doesn't refer to an existing DB cluster parameter group.", 404)); } public Collection listDbClusterParameterGroups(String filterName) { @@ -682,8 +684,8 @@ public class RdsService { public void deleteDbClusterParameterGroup(String name) { if (clusterParameterGroups.get(name).isEmpty()) { - throw new AwsException("DBParameterGroupNotFound", - "DB cluster parameter group " + name + " not found.", 404); + throw new AwsException("DBClusterParameterGroupNotFound", + "DBClusterParameterGroupName doesn't refer to an existing DB cluster parameter group.", 404); } clusterParameterGroups.delete(name); } @@ -732,8 +734,7 @@ public class RdsService { case "postgres", "aurora-postgresql" -> DatabaseEngine.POSTGRES; case "mysql", "aurora-mysql", "aurora" -> DatabaseEngine.MYSQL; case "mariadb" -> DatabaseEngine.MARIADB; - default -> throw new AwsException("InvalidParameterValue", - "Unsupported engine: " + engineParam + ". Supported: postgres, mysql, mariadb.", 400); + default -> throw new AwsException("InvalidParameterValue", invalidParameterValueMessage(), 400); }; } @@ -746,6 +747,54 @@ public class RdsService { return imageForRequestedVersion(defaultImage, engineVersion); } + private void validateInstanceParameterGroup(String paramGroupName, String engineParam, String engineVersion) { + if (paramGroupName == null || paramGroupName.isBlank()) { + return; + } + DbParameterGroup group = getDbParameterGroup(paramGroupName); + validateParameterGroupFamily(paramGroupName, group.getDbParameterGroupFamily(), engineParam, engineVersion); + } + + private void validateClusterParameterGroup(String paramGroupName, String engineParam, String engineVersion) { + if (paramGroupName == null || paramGroupName.isBlank()) { + return; + } + DbClusterParameterGroup group = getDbClusterParameterGroup(paramGroupName); + validateParameterGroupFamily(paramGroupName, group.getDbParameterGroupFamily(), engineParam, engineVersion); + } + + private void validateParameterGroupFamily(String groupName, String family, String engineParam, String engineVersion) { + String normalizedFamily = family == null ? "" : family.toLowerCase(); + String expectedPrefix = expectedFamilyPrefix(engineParam); + if (!normalizedFamily.startsWith(expectedPrefix)) { + throw new AwsException("InvalidParameterCombination", invalidParameterCombinationMessage(), 400); + } + } + + private String expectedFamilyPrefix(String engineParam) { + String normalizedEngine = effectiveEngineName(engineParam).toLowerCase(); + return switch (normalizedEngine) { + case "postgres" -> "postgres"; + case "aurora-postgresql" -> "aurora-postgresql"; + case "mysql" -> "mysql"; + case "aurora", "aurora-mysql" -> "aurora-mysql"; + case "mariadb" -> "mariadb"; + default -> throw new AwsException("InvalidParameterValue", invalidParameterValueMessage(), 400); + }; + } + + private String effectiveEngineName(String engineParam) { + return engineParam == null || engineParam.isBlank() ? "postgres" : engineParam; + } + + private String invalidParameterValueMessage() { + return "A value that you provided for a parameter isn't valid. Check the parameter constraints and try again."; + } + + private String invalidParameterCombinationMessage() { + return "Parameters that must not be used together were used together. Remove one of the conflicting parameters and try again."; + } + static String imageForRequestedVersion(String defaultImage, String engineVersion) { if (engineVersion == null || engineVersion.isBlank()) { return defaultImage; diff --git a/src/test/java/io/github/hectorvent/floci/services/rds/RdsQueryHandlerTest.java b/src/test/java/io/github/hectorvent/floci/services/rds/RdsQueryHandlerTest.java index 960dffd687..28a5c28976 100644 --- a/src/test/java/io/github/hectorvent/floci/services/rds/RdsQueryHandlerTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/rds/RdsQueryHandlerTest.java @@ -340,7 +340,7 @@ class RdsQueryHandlerTest { eq(null), eq(null), eq(null), eq("db.t3.micro"), eq(20), eq(false), eq(null), eq(null), eq(null), eq(false), eq(null), eq(java.util.Map.of()))) .thenThrow(new AwsException("InvalidParameterValue", - "Unsupported engine: oracle. Supported: postgres, mysql, mariadb.", 400)); + "A value that you provided for a parameter isn't valid. Check the parameter constraints and try again.", 400)); MultivaluedMap p = params(); p.add("DBInstanceIdentifier", "mydb"); @@ -349,6 +349,7 @@ class RdsQueryHandlerTest { assertEquals(400, response.getStatus()); assertTrue(((String) response.getEntity()).contains("InvalidParameterValue")); + assertTrue(((String) response.getEntity()).contains("Check the parameter constraints and try again.")); } @Test 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.java index a0e7914ce9..92d4a029b3 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 @@ -165,6 +165,29 @@ class RdsServiceTest { assertTrue(secretString.getValue().contains("\"dbInstanceIdentifier\":\"mydb\"")); } + @Test + void createDbInstanceRejectsUnknownParameterGroup() { + AwsException exception = assertThrows(AwsException.class, () -> rdsService.createDbInstance("mydb", "postgres", "13", + "admin", "password", "dbname", "db.t3.micro", + 20, false, "does-not-exist", null, null)); + + assertEquals("DBParameterGroupNotFound", exception.getErrorCode()); + assertEquals("DBParameterGroupName doesn't refer to an existing DB parameter group.", exception.getMessage()); + } + + @Test + void createDbInstanceRejectsIncompatibleParameterGroupFamily() { + rdsService.createDbParameterGroup("pg1", "mysql8.0", "test group"); + + AwsException exception = assertThrows(AwsException.class, () -> rdsService.createDbInstance("mydb", "postgres", "13", + "admin", "password", "dbname", "db.t3.micro", + 20, false, "pg1", null, null)); + + assertEquals("InvalidParameterCombination", exception.getErrorCode()); + assertEquals("Parameters that must not be used together were used together. Remove one of the conflicting parameters and try again.", + exception.getMessage()); + } + @Test void listDbInstancesIsCaseInsensitive() { rdsService.createDbInstance("mydb", "postgres", "13", @@ -296,6 +319,27 @@ class RdsServiceTest { assertTrue(exception.getMessage().contains("still has DB instances")); } + @Test + void createDbClusterRejectsUnknownClusterParameterGroup() { + AwsException exception = assertThrows(AwsException.class, () -> rdsService.createDbCluster("cluster1", "aurora-postgresql", "16.3", + "admin", "password", "dbname", false, "does-not-exist")); + + assertEquals("DBClusterParameterGroupNotFound", exception.getErrorCode()); + assertEquals("DBClusterParameterGroupName doesn't refer to an existing DB cluster parameter group.", exception.getMessage()); + } + + @Test + void createDbClusterRejectsIncompatibleClusterParameterGroupFamily() { + rdsService.createDbClusterParameterGroup("cpg1", "aurora-mysql8.0", "test group"); + + AwsException exception = assertThrows(AwsException.class, () -> rdsService.createDbCluster("cluster1", "aurora-postgresql", "16.3", + "admin", "password", "dbname", false, "cpg1")); + + assertEquals("InvalidParameterCombination", exception.getErrorCode()); + assertEquals("Parameters that must not be used together were used together. Remove one of the conflicting parameters and try again.", + exception.getMessage()); + } + @Test void createDbClusterParameterGroupRoundTrip() { DbClusterParameterGroup created = rdsService.createDbClusterParameterGroup( @@ -337,7 +381,8 @@ class RdsServiceTest { AwsException exception = assertThrows(AwsException.class, () -> rdsService.deleteDbClusterParameterGroup("nonexistent")); - assertEquals("DBParameterGroupNotFound", exception.getErrorCode()); + assertEquals("DBClusterParameterGroupNotFound", exception.getErrorCode()); + assertEquals("DBClusterParameterGroupName doesn't refer to an existing DB cluster parameter group.", exception.getMessage()); } @Test @@ -345,7 +390,8 @@ class RdsServiceTest { AwsException exception = assertThrows(AwsException.class, () -> rdsService.getDbClusterParameterGroup("nonexistent")); - assertEquals("DBParameterGroupNotFound", exception.getErrorCode()); + assertEquals("DBClusterParameterGroupNotFound", exception.getErrorCode()); + assertEquals("DBClusterParameterGroupName doesn't refer to an existing DB cluster parameter group.", exception.getMessage()); } @Test