fix(cloudformation): apply AWS::SQS::Queue tags from the template (#3532)

The registry schema declares Tags on AWS::SQS::Queue, but SqsCfnProvisioner never
read the property, so a template that tagged a queue produced an untagged queue
and nothing reported it. This is the first of the three follow-ups #2985 named
for itself.

Tags go through one reconcile used by both the create and the update path, rather
than only being applied at create. CloudFormation drives a resource's tags to the
template's desired state, so a key the template drops has to be untagged, and SQS
has no replace-tags call to do that in one step. The removal is computed with
ProvisionContext.staleTagKeys, the same shape AcmCfnProvisioner uses. On create
listQueueTags is empty, so the shared path degenerates to a plain tag and a new
queue and an updated one end in the same state for the same template.

SqsService already had everything needed: tagQueue, untagQueue and listQueueTags.
Create still calls the three-argument createQueue rather than the tags overload,
which keeps one tag path instead of two and leaves the existing tests untouched.

Three tests, each verified to fail before this change rather than assumed to. The
second is the one that matters: it drops a key and asserts only that key is
untagged, which is what a fix that just called tagQueue would get wrong. The third
covers a template that drops Tags altogether, which the reconcile docstring
promises and nothing pinned: it holds only because resolveTags answers an empty
map rather than null.
This commit is contained in:
Hector Ventura authored and GitHub committed 2026-09-13 14:23:22 -05:00
1 parent d90cb23c23
commit a5045d3aef
2 files changed
+83

No files matched your search

@@ -10,6 +10,7 @@ import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
@@ -128,12 +129,33 @@ public class SqsCfnProvisioner implements CfnResourceProvisioner {
// Queue object, so build it here from region + accountId + queueName. Without this,
// Fn::GetAtt [Queue, Arn] references resolve to an empty string.
String queueArn = AwsArnUtils.Arn.of("sqs", ctx.region(), ctx.accountId(), queueName).toString();
reconcileTags(queueUrl, ctx.resolveTags(props, "Tags"), ctx.region());
r.setPhysicalId(queueUrl);
r.getAttributes().put("Arn", queueArn);
r.getAttributes().put("QueueName", queueName);
r.getAttributes().put("QueueUrl", queueUrl);
}
/**
* Drives the queue's tags to the template's desired set, which is what CloudFormation does on
* update: a key the template drops is untagged, and a template with no {@code Tags} leaves the
* queue untagged. SQS has no replace-tags call, so the removal has to be computed, the same
* shape {@code AcmCfnProvisioner} uses.
*
* <p>Serves the create path too, where {@code listQueueTags} is empty and this is a plain tag.
* One path for both keeps a newly created queue and an updated one in the same state for the
* same template.
*/
private void reconcileTags(String queueUrl, Map<String, String> desired, String region) {
List<String> stale = ProvisionContext.staleTagKeys(sqsService.listQueueTags(queueUrl, region), desired);
if (!stale.isEmpty()) {
sqsService.untagQueue(queueUrl, stale, region);
}
if (!desired.isEmpty()) {
sqsService.tagQueue(queueUrl, desired, region);
}
}
private void provisionQueuePolicy(StackResource r, ProvisionContext ctx) {
// A policy has no backing entity, so its id only has to stay put across updates.
r.setPhysicalId(ctx.isUpdate()
@@ -12,6 +12,7 @@ import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.assertEquals;
@@ -21,6 +22,7 @@ import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.argThat;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
@@ -318,4 +320,63 @@ class SqsCfnProvisionerTest {
provisioner.delete("AWS::SQS::QueuePolicy", "queue-policy-abc", "us-east-1");
verifyNoInteractions(sqs);
}
@Test
void queueTagsFromTheTemplateReachTheQueue() {
// The registry schema declares Tags on AWS::SQS::Queue, but the provisioner never read the
// property, so a tagged template produced an untagged queue and nothing reported it.
when(sqs.createQueue(eq("the-queue"), any(), eq("us-east-1")))
.thenReturn(new Queue("the-queue", "http://q/the-queue"));
when(sqs.listQueueTags("http://q/the-queue", "us-east-1")).thenReturn(Map.of());
StackResource r = resource("AWS::SQS::Queue", "MyQueue");
ObjectNode props = mapper.createObjectNode().put("QueueName", "the-queue");
props.putArray("Tags")
.add(mapper.createObjectNode().put("Key", "env").put("Value", "prod"))
.add(mapper.createObjectNode().put("Key", "team").put("Value", "platform"));
provisioner.provision(r, props, ctx());
verify(sqs).tagQueue("http://q/the-queue", Map.of("env", "prod", "team", "platform"),
"us-east-1");
verify(sqs, never()).untagQueue(anyString(), any(), anyString());
}
@Test
void updateUntagsOnlyTheKeyTheTemplateDropped() {
// CloudFormation drives tags to the template's desired state, so a dropped key is untagged.
// Calling tagQueue alone would leave "owner" on the queue forever: SQS has no replace-tags
// call, which is why this goes through staleTagKeys the way AcmCfnProvisioner does.
when(sqs.listQueueTags("http://q/orders", "us-east-1"))
.thenReturn(new HashMap<>(Map.of("env", "prod", "owner", "alice")));
StackResource r = resource("AWS::SQS::Queue", "Orders");
r.setAttributes(new HashMap<>(Map.of("QueueName", "orders")));
ObjectNode props = mapper.createObjectNode().put("QueueName", "orders");
props.putArray("Tags")
.add(mapper.createObjectNode().put("Key", "env").put("Value", "staging"));
provisioner.provision(r, props, updateCtx("http://q/orders"));
verify(sqs).untagQueue("http://q/orders", List.of("owner"), "us-east-1");
verify(sqs).tagQueue("http://q/orders", Map.of("env", "staging"), "us-east-1");
}
@Test
void updateWithTheTagsPropertyGoneUntagsTheQueueCompletely() {
// Dropping Tags altogether is the edge the reconcile docstring promises but nothing pinned.
// It holds only because resolveTags answers an empty map rather than null: were that to
// change, every key would silently survive an update that asked for none.
when(sqs.listQueueTags("http://q/orders", "us-east-1"))
.thenReturn(new HashMap<>(Map.of("env", "prod", "owner", "alice")));
StackResource r = resource("AWS::SQS::Queue", "Orders");
r.setAttributes(new HashMap<>(Map.of("QueueName", "orders")));
ObjectNode props = mapper.createObjectNode().put("QueueName", "orders");
provisioner.provision(r, props, updateCtx("http://q/orders"));
// Order is not part of the contract: staleTagKeys walks a HashMap's key set.
verify(sqs).untagQueue(eq("http://q/orders"),
argThat(keys -> keys.size() == 2 && keys.containsAll(List.of("env", "owner"))),
eq("us-east-1"));
verify(sqs, never()).tagQueue(anyString(), any(), anyString());
}
}