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.java index 41facbdfac..307d122df3 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 @@ -1357,6 +1357,16 @@ public class RdsQueryHandler { manageMasterUserPassword, masterUserSecretKmsKeyId, engineMode, storageEncrypted, requestedPort); } + Map tags = parseTags(params); + String kmsKeyId = params.getFirst("KmsKeyId"); + if (!tags.isEmpty()) { + service.addTagsToResource(cluster.getDbClusterArn(), tags, region); + } + if (kmsKeyId != null && !kmsKeyId.isBlank()) { + cluster = service.setNewDbClusterKmsKeyId(region, id, kmsKeyId); + } else if (!tags.isEmpty()) { + cluster = service.getDbCluster(id, region); + } if (!associatedRoles.isEmpty()) { try { cluster = service.addRolesToNewDbCluster(region, id, associatedRoles); @@ -3474,6 +3484,7 @@ public class RdsQueryHandler { .elem("HttpEndpointEnabled", c.isHttpEndpointEnabled()) .elem("MultiAZ", c.isMultiAz()) .elem("StorageEncrypted", c.isStorageEncrypted()) + .elem("KmsKeyId", c.getKmsKeyId()) .elem("AvailabilityZone", c.getAvailabilityZone() != null ? c.getAvailabilityZone() : config.defaultAvailabilityZone()) .elem("PreferredMaintenanceWindow", "mon:00:00-mon:03:00") .elem("PreferredBackupWindow", "04:00-06:00") @@ -3527,6 +3538,9 @@ public class RdsQueryHandler { } xml.end("DBClusterMembers"); writeAssociatedRoles(xml, c.getAssociatedRoles(), "DBClusterRole"); + xml.start("TagList"); + writeTags(xml, c.getTags()); + xml.end("TagList"); return xml.build(); } 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 19415a683a..1dc8d442f9 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 @@ -11212,6 +11212,15 @@ public class RdsService implements Resettable, ResourceProvider { return RESPONSE_COPIER.convertValue(cluster, DbCluster.class); } + /** Stores the KmsKeyId a CreateDBCluster request carries, which {@code createDbCluster} has no argument for. */ + public synchronized DbCluster setNewDbClusterKmsKeyId(String region, String clusterId, String kmsKeyId) { + String effectiveRegion = effectiveRegion(region); + DbCluster cluster = getDbCluster(clusterId, effectiveRegion); + cluster.setKmsKeyId(kmsKeyId); + putClusterForScope(currentAccountId(), effectiveRegion, clusterId, cluster); + return RESPONSE_COPIER.convertValue(cluster, DbCluster.class); + } + /** * Gives a cluster {@code CreateDBCluster} just made the roles the request named, all in one * step: either every role is associated or none is. The handler deletes the new cluster when diff --git a/src/main/java/io/github/hectorvent/floci/services/rds/model/DbCluster.java b/src/main/java/io/github/hectorvent/floci/services/rds/model/DbCluster.java index b8d11b85a1..7700ce52a6 100644 --- a/src/main/java/io/github/hectorvent/floci/services/rds/model/DbCluster.java +++ b/src/main/java/io/github/hectorvent/floci/services/rds/model/DbCluster.java @@ -46,6 +46,7 @@ public class DbCluster { private List associatedRoles = new ArrayList<>(); private String engineMode; private boolean storageEncrypted; + private String kmsKeyId; private String dockerVolumeName; private String volumeId; @@ -199,6 +200,9 @@ public class DbCluster { public boolean isStorageEncrypted() { return storageEncrypted; } public void setStorageEncrypted(boolean storageEncrypted) { this.storageEncrypted = storageEncrypted; } + public String getKmsKeyId() { return kmsKeyId; } + public void setKmsKeyId(String kmsKeyId) { this.kmsKeyId = kmsKeyId; } + public String getDockerVolumeName() { return dockerVolumeName; } public void setDockerVolumeName(String dockerVolumeName) { this.dockerVolumeName = dockerVolumeName; } diff --git a/src/test/java/io/github/hectorvent/floci/services/rds/RdsClusterCreateTagsAndKmsKeyIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/rds/RdsClusterCreateTagsAndKmsKeyIntegrationTest.java new file mode 100644 index 0000000000..2e7888231a --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/rds/RdsClusterCreateTagsAndKmsKeyIntegrationTest.java @@ -0,0 +1,109 @@ +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.AfterEach; +import org.junit.jupiter.api.Test; + +import static io.github.hectorvent.floci.testing.RdsRequests.rds; +import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.not; + +/** + * The Tags and KmsKeyId of a CreateDBCluster request are kept, so ListTagsForResource and DescribeDBClusters return + * them. Without that a caller that reads them back, such as the Terraform AWS provider, sees an empty + * {@code tags_all} and a missing {@code kms_key_id} right after the create, and plans to replace the cluster. + */ +@QuarkusTest +@TestProfile(RdsMockProfile.class) +class RdsClusterCreateTagsAndKmsKeyIntegrationTest { + + private static final String ID = "create-options-cluster"; + private static final String KEY_ARN = "arn:aws:kms:us-east-1:000000000000:key/1234abcd-12ab-34cd-56ef-1234567890ab"; + + @AfterEach + void cleanUp() { + rds("DeleteDBCluster").formParam("DBClusterIdentifier", ID) + .formParam("SkipFinalSnapshot", "true").when().post("/"); + } + + @Test + void tagsGivenAtCreateAreReadableThroughListTagsForResource() { + String arn = createCluster() + .formParam("Tags.Tag.1.Key", "Accounting").formParam("Tags.Tag.1.Value", "gna") + .formParam("Tags.Tag.2.Key", "BusinessSolution").formParam("Tags.Tag.2.Value", "gue") + .when().post("/").then().statusCode(200) + .extract().path("CreateDBClusterResponse.CreateDBClusterResult.DBCluster.DBClusterArn"); + + rds("ListTagsForResource").formParam("ResourceName", arn) + .when().post("/").then().statusCode(200) + .body(containsString("Accounting")) + .body(containsString("gna")) + .body(containsString("BusinessSolution")) + .body(containsString("gue")); + } + + @Test + void tagsAreAlsoReturnedAsTheTagListOfDescribeDbClusters() { + // The Terraform AWS provider reads a cluster's tags from here, not from ListTagsForResource. + createCluster() + .formParam("Tags.Tag.1.Key", "Accounting").formParam("Tags.Tag.1.Value", "gna") + .when().post("/").then().statusCode(200) + .body(containsString("Accountinggna")); + + rds("DescribeDBClusters").formParam("DBClusterIdentifier", ID) + .when().post("/").then().statusCode(200) + .body(containsString("Accountinggna")); + } + + @Test + void tagsAreAlsoReadInTheMemberForm() { + String arn = createCluster() + .formParam("Tags.member.1.Key", "team").formParam("Tags.member.1.Value", "platform") + .when().post("/").then().statusCode(200) + .extract().path("CreateDBClusterResponse.CreateDBClusterResult.DBCluster.DBClusterArn"); + + rds("ListTagsForResource").formParam("ResourceName", arn) + .when().post("/").then().statusCode(200) + .body(containsString("team")) + .body(containsString("platform")); + } + + @Test + void kmsKeyIdRoundTripsFromCreateToDescribe() { + createCluster() + .formParam("StorageEncrypted", "true").formParam("KmsKeyId", KEY_ARN) + .when().post("/").then().statusCode(200) + .body(containsString("" + KEY_ARN + "")); + + rds("DescribeDBClusters").formParam("DBClusterIdentifier", ID) + .when().post("/").then().statusCode(200) + .body(containsString("true")) + .body(containsString("" + KEY_ARN + "")); + } + + @Test + void aClusterCreatedWithoutThemHasNoTagsAndNoKmsKeyId() { + String arn = createCluster().when().post("/").then().statusCode(200) + .body(not(containsString(""))) + .extract().path("CreateDBClusterResponse.CreateDBClusterResult.DBCluster.DBClusterArn"); + + rds("ListTagsForResource").formParam("ResourceName", arn) + .when().post("/").then().statusCode(200) + .body(not(containsString(""))); + rds("DescribeDBClusters").formParam("DBClusterIdentifier", ID) + .when().post("/").then().statusCode(200) + .body(not(containsString(""))); + } + + private static RequestSpecification createCluster() { + return rds("CreateDBCluster") + .formParam("DBClusterIdentifier", ID) + .formParam("Engine", "aurora-postgresql") + .formParam("EngineVersion", "16.3") + .formParam("MasterUsername", "admin") + .formParam("MasterUserPassword", "secret99password"); + } +}