fix(s3): preserve bucket tags in TagResource (#8441)

This commit is contained in:
Rashen Anupama authored and GitHub committed 2026-11-15 08:31:25 -04:00
1 parent 908b81fcf5
commit adfe173764
4 files changed
+50 -27

No files matched your search

@@ -148,14 +148,16 @@ class S3ControlTest {
@Test
@Order(4)
@DisplayName("tagResource: replaces all existing tags (not a merge)")
void tagResourceReplacesAllTags() {
// tagResource replaces — only the new tags should remain
@DisplayName("tagResource: adds and updates tags while preserving unrelated keys")
void tagResourceMergesTags() {
s3control.tagResource(TagResourceRequest.builder()
.accountId(ACCOUNT_ID)
.resourceArn(bucketArn())
.tags(software.amazon.awssdk.services.s3control.model.Tag.builder()
.key("NewOnly").value("yes").build())
.tags(
software.amazon.awssdk.services.s3control.model.Tag.builder()
.key("Team").value("storage").build(),
software.amazon.awssdk.services.s3control.model.Tag.builder()
.key("NewOnly").value("yes").build())
.build());
ListTagsForResourceResponse response = s3control.listTagsForResource(
@@ -169,7 +171,8 @@ class S3ControlTest {
software.amazon.awssdk.services.s3control.model.Tag::key,
software.amazon.awssdk.services.s3control.model.Tag::value));
assertThat(tags).containsOnlyKeys("NewOnly");
assertThat(tags).containsExactlyInAnyOrderEntriesOf(
Map.of("Team", "storage", "CostCenter", "engineering", "NewOnly", "yes"));
}
@Test
@@ -26,7 +26,6 @@ import org.jboss.logging.Logger;
import org.jboss.resteasy.reactive.server.ServerExceptionMapper;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
@@ -265,7 +264,7 @@ public class S3ControlController {
}
/**
* TagResource — replaces all tags on the specified S3 bucket.
* TagResource adds or updates tags on the specified S3 bucket.
*
* POST /v20180820/tags/{resourceArn+}
* Header: x-amz-account-id
@@ -283,7 +282,7 @@ public class S3ControlController {
AwsArnUtils.Arn arn = taggedBucketArn(resourceArn);
String xml = new String(body, StandardCharsets.UTF_8);
Map<String, String> tags = XmlParser.extractPairs(xml, "Tag", "Key", "Value");
s3Service.putBucketTaggingForAccount(arn.accountId(), S3ControlArnUtils.bucketName(arn), tags);
s3Service.mergeBucketTaggingForAccount(arn.accountId(), S3ControlArnUtils.bucketName(arn), tags);
return Response.noContent().build();
} catch (AwsException e) {
return xmlErrorResponse(e);
@@ -305,11 +304,7 @@ public class S3ControlController {
try {
AwsArnUtils.Arn arn = taggedBucketArn(resourceArn);
String bucketName = S3ControlArnUtils.bucketName(arn);
Map<String, String> existing =
new HashMap<>(s3Service.getBucketTaggingForAccount(arn.accountId(), bucketName));
tagKeys.forEach(existing::remove);
s3Service.putBucketTaggingForAccount(arn.accountId(), bucketName, existing);
s3Service.removeBucketTagsForAccount(arn.accountId(), S3ControlArnUtils.bucketName(arn), tagKeys);
return Response.noContent().build();
} catch (AwsException e) {
return xmlErrorResponse(e);
@@ -101,6 +101,7 @@ import java.util.function.Function;
import java.util.function.LongConsumer;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.function.UnaryOperator;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
@@ -3937,10 +3938,34 @@ public class S3Service implements Resettable, ResourceProvider {
/** Replaces the tags of the bucket a control-plane tagging ARN names, in the partition it names. */
public void putBucketTaggingForAccount(String accountId, String bucketName, Map<String, String> tags) {
updateBucketTaggingForAccount(accountId, bucketName, existing -> tags != null ? tags : new HashMap<>());
}
public void mergeBucketTaggingForAccount(String accountId, String bucketName, Map<String, String> tags) {
updateBucketTaggingForAccount(accountId, bucketName, existing -> {
Map<String, String> updated = new HashMap<>(existing);
updated.putAll(tags);
return updated;
});
}
public void removeBucketTagsForAccount(String accountId, String bucketName, List<String> tagKeys) {
updateBucketTaggingForAccount(accountId, bucketName, existing -> {
Map<String, String> updated = new HashMap<>(existing);
tagKeys.forEach(updated::remove);
return updated;
});
}
private void updateBucketTaggingForAccount(String accountId, String bucketName,
UnaryOperator<Map<String, String>> update) {
Bucket bucket = bucketForAccount(accountId, bucketName);
bucket.setTags(tags != null ? tags : new HashMap<>());
writeBucketForAccount(namedAccount(accountId), bucketName, bucket);
LOG.debugv("Put tags on bucket {0} of account {1}", bucketName, namedAccount(accountId));
synchronized (bucket) {
Map<String, String> existing = bucket.getTags() != null ? bucket.getTags() : Map.of();
bucket.setTags(update.apply(existing));
writeBucketForAccount(namedAccount(accountId), bucketName, bucket);
}
LOG.debugv("Updated tags on bucket {0} of account {1}", bucketName, namedAccount(accountId));
}
/**
@@ -4142,12 +4167,7 @@ public class S3Service implements Resettable, ResourceProvider {
}
public void deleteBucketTagging(String bucketName) {
Bucket bucket = bucketStore.get(bucketName)
.orElseThrow(() -> new AwsException("NoSuchBucket",
"The specified bucket does not exist.", 404));
bucket.setTags(new java.util.HashMap<>());
bucketStore.put(bucketName, bucket);
LOG.debugv("Deleted tags from bucket: {0}", bucketName);
putBucketTagging(bucketName, Map.of());
}
// --- Metrics Configurations ---
@@ -10,6 +10,7 @@ import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
import java.util.List;
import java.util.Map;
import static io.restassured.RestAssured.given;
import static org.hamcrest.MatcherAssert.assertThat;
@@ -68,6 +69,7 @@ class S3ControlUrlEncodedArnIntegrationTest {
String tagBody = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
"<Tagging><TagSet>" +
"<Tag><Key>Env</Key><Value>dev</Value></Tag>" +
"<Tag><Key>Project</Key><Value>infra</Value></Tag>" +
"</TagSet></Tagging>";
given().contentType("application/xml").body(tagBody)
@@ -106,11 +108,12 @@ class S3ControlUrlEncodedArnIntegrationTest {
@Test
@Order(4)
@DisplayName("TagResource accepts URL-encoded ARN from Go AWS SDK v2 (#435)")
@DisplayName("TagResource merges tags through a URL-encoded ARN")
void tagResourceWithUrlEncodedArn() {
String body = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
"<Tagging xmlns=\"http://awss3control.amazonaws.com/doc/2018-08-20/\">" +
"<Tags>" +
"<Tag><Key>Env</Key><Value>prod</Value></Tag>" +
"<Tag><Key>Owner</Key><Value>team-a</Value></Tag>" +
"<Tag><Key>CostCenter</Key><Value>cc-42</Value></Tag>" +
"</Tags></Tagging>";
@@ -124,14 +127,16 @@ class S3ControlUrlEncodedArnIntegrationTest {
.then()
.statusCode(204);
given()
String response = given()
.header("x-amz-account-id", ACCOUNT)
.when()
.get("/v20180820/tags/" + ENCODED_ARN)
.then()
.statusCode(200)
.body(containsString("<Key>Owner</Key>"))
.body(containsString("<Key>CostCenter</Key>"));
.extract().asString();
assertEquals(Map.of("Env", "prod", "Project", "infra", "Owner", "team-a", "CostCenter", "cc-42"),
XmlParser.extractPairs(response, "Tag", "Key", "Value"));
}
@Test