fix(cloudhsmv2): preserve tags when copying backups (#7981)

* fix(cloudhsmv2): preserve tags when copying backups

* fix(cloudhsmv2): keep backup tag copy focused
This commit is contained in:
Jimmy Liu Jia authored and GitHub committed 2026-11-11 09:21:12 -04:00
1 parent 55bb5c0f6e
commit 5e0f318a40
3 files changed
+126 -2

No files matched your search

@@ -429,7 +429,8 @@ public class CloudHsmV2JsonHandler {
"DestinationRegion is required when copying a backup.", 400);
}
String backupId = text(request, "BackupId");
Backup backup = service.copyBackupToRegion(destRegion, backupId, region);
Map<String, String> tags = parseTagList(request.path("TagList"));
Backup backup = service.copyBackupToRegion(destRegion, backupId, tags, region);
ObjectNode response = objectMapper.createObjectNode();
ObjectNode destBackup = objectMapper.createObjectNode();
destBackup.put("CreateTimestamp", EpochSeconds.fromInstant(backup.getCreateTimestamp()));
@@ -701,7 +701,7 @@ public class CloudHsmV2Service {
return backup;
}
public Backup copyBackupToRegion(String destinationRegion, String backupId, String sourceRegion) {
public synchronized Backup copyBackupToRegion(String destinationRegion, String backupId, Map<String, String> tags, String sourceRegion) {
// Source region emulation: we'll just clone the backup locally.
Backup source = getBackup(backupId, sourceRegion);
Backup copy = new Backup();
@@ -717,6 +717,7 @@ public class CloudHsmV2Service {
copy.setHsmType(source.getHsmType());
copy.setClusterCertificate(source.getClusterCertificate());
copy.setNeverExpires(source.getNeverExpires());
copy.setTagList(new LinkedHashMap<>(tags != null ? tags : source.getTagList()));
backups.put(regionKey(destinationRegion, copy.getBackupId()), copy);
return copy;
}
@@ -0,0 +1,122 @@
package io.floci.aws.services.cloudhsmv2;
import io.floci.aws.testing.RestAssuredJsonUtils;
import io.quarkus.test.junit.QuarkusTest;
import io.restassured.response.ValidatableResponse;
import io.restassured.specification.RequestSpecification;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasItems;
import static org.hamcrest.Matchers.hasSize;
@QuarkusTest
class CloudHsmV2CopyBackupTagsIntegrationTest {
private static final String SOURCE_REGION = "us-east-1";
private static final String DESTINATION_REGION = "us-west-2";
@BeforeAll
static void configureRestAssured() {
RestAssuredJsonUtils.configureAwsContentTypes();
}
@Test
void copyWithTagsUsesOnlyTheRequestedTags() {
String sourceBackupId = createTaggedSourceBackup();
request("CopyBackupToRegion", SOURCE_REGION)
.body("""
{"BackupId":"%s","DestinationRegion":"%s",
"TagList":[{"Key":"team","Value":"destination"}]}
""".formatted(sourceBackupId, DESTINATION_REGION))
.post("/")
.then().statusCode(200);
ValidatableResponse copiedBackup = describeCopiedBackup(sourceBackupId)
.body("Backups[0].TagList", hasSize(1))
.body("Backups[0].TagList[0].Key", equalTo("team"))
.body("Backups[0].TagList[0].Value", equalTo("destination"));
String destinationBackupId = copiedBackup.extract().jsonPath().getString("Backups[0].BackupId");
request("ListTags", DESTINATION_REGION)
.body("{\"ResourceId\":\"" + destinationBackupId + "\"}")
.post("/")
.then().statusCode(200)
.body("TagList", hasSize(1))
.body("TagList[0].Key", equalTo("team"))
.body("TagList[0].Value", equalTo("destination"));
}
@Test
void copyWithoutTagsInheritsSourceBackupTags() {
String sourceBackupId = createTaggedSourceBackup();
request("CopyBackupToRegion", SOURCE_REGION)
.body("{\"BackupId\":\"" + sourceBackupId + "\",\"DestinationRegion\":\"" + DESTINATION_REGION + "\"}")
.post("/")
.then().statusCode(200);
ValidatableResponse copiedBackup = describeCopiedBackup(sourceBackupId)
.body("Backups[0].TagList", hasSize(2))
.body("Backups[0].TagList[0].Key", equalTo("team"))
.body("Backups[0].TagList[0].Value", equalTo("source"))
.body("Backups[0].TagList[1].Key", equalTo("obsolete"))
.body("Backups[0].TagList[1].Value", equalTo("old"));
String destinationBackupId = copiedBackup.extract().jsonPath().getString("Backups[0].BackupId");
request("UntagResource", DESTINATION_REGION)
.body("{\"ResourceId\":\"" + destinationBackupId + "\",\"TagKeyList\":[\"team\"]}")
.post("/")
.then().statusCode(200);
request("ListTags", SOURCE_REGION)
.body("{\"ResourceId\":\"" + sourceBackupId + "\"}")
.post("/")
.then().statusCode(200)
.body("TagList", hasSize(2))
.body("TagList.Key", hasItems("team", "obsolete"));
}
private ValidatableResponse describeCopiedBackup(String sourceBackupId) {
return request("DescribeBackups", DESTINATION_REGION)
.body("{\"Filters\":{\"sourceBackupIds\":[\"" + sourceBackupId + "\"]}}")
.post("/")
.then().statusCode(200)
.body("Backups.size()", equalTo(1));
}
private String createTaggedSourceBackup() {
String clusterId = request("CreateCluster", SOURCE_REGION)
.body("{\"HsmType\":\"hsm1.medium\",\"SubnetIds\":[\"subnet-abcdef01\"]}")
.post("/")
.then().statusCode(200)
.extract().jsonPath().getString("Cluster.ClusterId");
String backupId = request("DescribeBackups", SOURCE_REGION)
.body("{\"Filters\":{\"clusterIds\":[\"" + clusterId + "\"]}}")
.post("/")
.then().statusCode(200)
.body("Backups.size()", equalTo(1))
.extract().jsonPath().getString("Backups[0].BackupId");
request("TagResource", SOURCE_REGION)
.body("""
{"ResourceId":"%s","TagList":[
{"Key":"team","Value":"source"},
{"Key":"obsolete","Value":"old"}]}
""".formatted(backupId))
.post("/")
.then().statusCode(200);
return backupId;
}
private RequestSpecification request(String operation, String region) {
return given()
.header("X-Amz-Target", "BaldrApiService." + operation)
.header("Authorization", "AWS4-HMAC-SHA256 Credential=test/20261111/" + region + "/cloudhsm/aws4_request")
.contentType("application/x-amz-json-1.1; charset=UTF-8");
}
}