From 6ef50cbefcf38aa2a7685ccd21d508712a091e82 Mon Sep 17 00:00:00 2001 From: Mark Date: Sun, 1 Nov 2026 22:11:49 +0100 Subject: [PATCH] fix(rds): store the Tags and KmsKeyId of CreateDBCluster (#7225) * fix(rds): store the Tags and KmsKeyId of CreateDBCluster CreateDBCluster ignored both. ListTagsForResource on a cluster created with Tags returned none, DescribeDBClusters had no TagList, and it never returned a KmsKeyId, so a caller that reads them back sees none of them. The Terraform AWS provider then has an empty tags and tags_all in the state and, because kms_key_id differs from the configuration, plans to replace the cluster on every run. handleCreateDbCluster now reads Tags (the Tag and member forms, as the other RDS create calls do) and KmsKeyId and stores them on the new cluster. DBCluster gets a KmsKeyId of its own, separate from the master user secret's, and CreateDBCluster and DescribeDBClusters return it, and the cluster's tags as its TagList. * docs(rds): keep the addRolesToNewDbCluster javadoc above its method applyCreateDbClusterOptions was inserted between the javadoc of addRolesToNewDbCluster and the method, so that javadoc described the new method. Each method now has its own. * refactor(rds): tag a new DB cluster through addTagsToResource --- .../floci/services/rds/RdsQueryHandler.java | 14 +++ .../floci/services/rds/RdsService.java | 9 ++ .../floci/services/rds/model/DbCluster.java | 4 + ...terCreateTagsAndKmsKeyIntegrationTest.java | 109 ++++++++++++++++++ 4 files changed, 136 insertions(+) create mode 100644 src/test/java/io/github/hectorvent/floci/services/rds/RdsClusterCreateTagsAndKmsKeyIntegrationTest.java 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"); + } +}