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
This commit is contained in:
Mark authored and GitHub committed 2026-11-01 17:11:49 -04:00
1 parent c2a572dc5f
commit 6ef50cbefc
4 files changed
+136

No files matched your search

@@ -1357,6 +1357,16 @@ public class RdsQueryHandler {
manageMasterUserPassword, masterUserSecretKmsKeyId, engineMode, storageEncrypted,
requestedPort);
}
Map<String, String> 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();
}
@@ -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
@@ -46,6 +46,7 @@ public class DbCluster {
private List<DbRoleAssociation> 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; }
@@ -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("<Key>Accounting</Key>"))
.body(containsString("<Value>gna</Value>"))
.body(containsString("<Key>BusinessSolution</Key>"))
.body(containsString("<Value>gue</Value>"));
}
@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("<TagList><Tag><Key>Accounting</Key><Value>gna</Value></Tag></TagList>"));
rds("DescribeDBClusters").formParam("DBClusterIdentifier", ID)
.when().post("/").then().statusCode(200)
.body(containsString("<TagList><Tag><Key>Accounting</Key><Value>gna</Value></Tag></TagList>"));
}
@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("<Key>team</Key>"))
.body(containsString("<Value>platform</Value>"));
}
@Test
void kmsKeyIdRoundTripsFromCreateToDescribe() {
createCluster()
.formParam("StorageEncrypted", "true").formParam("KmsKeyId", KEY_ARN)
.when().post("/").then().statusCode(200)
.body(containsString("<KmsKeyId>" + KEY_ARN + "</KmsKeyId>"));
rds("DescribeDBClusters").formParam("DBClusterIdentifier", ID)
.when().post("/").then().statusCode(200)
.body(containsString("<StorageEncrypted>true</StorageEncrypted>"))
.body(containsString("<KmsKeyId>" + KEY_ARN + "</KmsKeyId>"));
}
@Test
void aClusterCreatedWithoutThemHasNoTagsAndNoKmsKeyId() {
String arn = createCluster().when().post("/").then().statusCode(200)
.body(not(containsString("<KmsKeyId>")))
.extract().path("CreateDBClusterResponse.CreateDBClusterResult.DBCluster.DBClusterArn");
rds("ListTagsForResource").formParam("ResourceName", arn)
.when().post("/").then().statusCode(200)
.body(not(containsString("<Key>")));
rds("DescribeDBClusters").formParam("DBClusterIdentifier", ID)
.when().post("/").then().statusCode(200)
.body(not(containsString("<KmsKeyId>")));
}
private static RequestSpecification createCluster() {
return rds("CreateDBCluster")
.formParam("DBClusterIdentifier", ID)
.formParam("Engine", "aurora-postgresql")
.formParam("EngineVersion", "16.3")
.formParam("MasterUsername", "admin")
.formParam("MasterUserPassword", "secret99password");
}
}