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

@@ -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 ---