fix(cloudformation): give resource-tag conditions the tags of the stack, stack set or change set a request names (#8527)

The generated request resources give CloudFormation's stack, stackset and
changeset types aws:ResourceTag/ keys, but CloudFormationStoredResourceArns
did not answer storedTags, so a condition on their tags saw none. It now
answers from CloudFormationService.storedTags, which reads the live stack, or
the change set, under the ARN in the account and Region it names, and
StackSetService.storedTags, which reads the stack set under the ARN in the
account it names, a pre-multi-account one of the default account in place;
each matched on the whole ARN, reading only.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-11-15 20:12:12 -04:00
1 parent bcca1bb8d3
commit b46507dbec
8 files changed
+148 -8

No files matched your search

@@ -88,6 +88,31 @@ class CloudFormationIamIntegrationTest {
"111122223333", "StackInstanceRegion", REGION)), "cloudformation:DescribeStackInstance");
}
/** A resource-tag condition reads the tags CloudFormation holds on the stack or the stack set a request names. */
@Test
void aResourceTagConditionReadsTheTagsOfTheStackAndTheStackSet() {
String tagged = name();
String untagged = name();
call(ACCOUNT, "CreateStack", Map.of("StackName", tagged, "TemplateBody", TEMPLATE,
"Tags.member.1.Key", "team", "Tags.member.1.Value", "data")).statusCode(200);
cleanup.register(() -> call(ACCOUNT, "DeleteStack", Map.of("StackName", tagged)));
stack(untagged);
String taggedSet = name();
String untaggedSet = name();
call(ACCOUNT, "CreateStackSet", Map.of("StackSetName", taggedSet, "TemplateBody", TEMPLATE,
"Tags.member.1.Key", "team", "Tags.member.1.Value", "data")).statusCode(200);
cleanup.register(() -> call(ACCOUNT, "DeleteStackSet", Map.of("StackSetName", taggedSet)));
stackSet(untaggedSet);
String user = user("{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Action\":"
+ "[\"cloudformation:DescribeStacks\",\"cloudformation:DescribeStackSet\"],\"Resource\":\"*\","
+ "\"Condition\":{\"StringEquals\":{\"aws:ResourceTag/team\":\"data\"}}}]}");
call(user, "DescribeStacks", Map.of("StackName", tagged)).statusCode(200);
denied(call(user, "DescribeStacks", Map.of("StackName", untagged)), "cloudformation:DescribeStacks");
call(user, "DescribeStackSet", Map.of("StackSetName", taggedSet)).statusCode(200);
denied(call(user, "DescribeStackSet", Map.of("StackSetName", untaggedSet)), "cloudformation:DescribeStackSet");
}
private String stack(String name) {
String id = call(ACCOUNT, "CreateStack", Map.of("StackName", name, "TemplateBody", TEMPLATE)).statusCode(200)
.extract().xmlPath().getString("CreateStackResponse.CreateStackResult.StackId");
@@ -0,0 +1,39 @@
package io.floci.aws.services.cloudformation;
import io.floci.aws.services.cloudformation.model.ChangeSet;
import io.floci.aws.services.cloudformation.model.Stack;
import io.floci.aws.services.cloudformation.provisioners.CfnResourceDispatcher;
import org.junit.jupiter.api.Test;
import java.util.List;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.mock;
class CloudFormationStoredTagsTest {
private static final String ACCOUNT = "000000000000";
private static final String REGION = "us-east-1";
private static final String TEMPLATE =
"{\"Resources\":{\"Handle\":{\"Type\":\"AWS::CloudFormation::WaitConditionHandle\"}}}";
/**
* The tags IAM gives a resource-tag condition: the stack's or the change set's, read on its whole ARN in the
* account and Region it names; none for anything else.
*/
@Test
void storedTagsReadsTheStackOrTheChangeSetItsArnNames() {
CloudFormationService cfn = CloudFormationServiceFixture.create(mock(CfnResourceDispatcher.class));
ChangeSet changeSet = cfn.createChangeSet("tagged", "first", "CREATE", TEMPLATE, null, Map.of(), List.of(),
Map.of("team", "data"), REGION, ACCOUNT);
Stack stack = cfn.describeStacks("tagged", REGION, ACCOUNT).getFirst();
assertEquals(Map.of("team", "data"), cfn.storedTags(changeSet.getChangeSetId()));
assertEquals(stack.tagsSnapshot(), cfn.storedTags(stack.getStackId()));
assertEquals(Map.of(), cfn.storedTags(changeSet.getChangeSetId().replace("/first/", "/other/")));
assertEquals(Map.of(), cfn.storedTags(stack.getStackId().replace(":" + ACCOUNT + ":", ":111122223333:")));
assertEquals(Map.of(), cfn.storedTags(changeSet.getChangeSetId().replace(REGION, "eu-west-1")));
assertEquals(Map.of(), cfn.storedTags("arn:aws:s3:::tagged"));
}
}
@@ -26,6 +26,17 @@ class StackSetTagReadTest {
service.createStackSet(SET, "{\"Resources\":{}}", null, null, initialTags, null);
}
/** The tags IAM gives a resource-tag condition: the stack set's, read on its ARN in the account it names. */
@Test
void storedTagsReadsTheStackSetItsArnNames() {
String arn = service.stackSetArn(SET, "us-east-1").orElseThrow();
assertEquals(initialTags, service.storedTags(arn));
assertEquals(Map.of(), service.storedTags(arn.replace(":000000000000:", ":111122223333:")));
assertEquals(Map.of(), service.storedTags(arn.substring(0, arn.lastIndexOf(':')) + ":not-its-id"));
assertEquals(Map.of(), service.storedTags(arn.replace("stackset/", "stack/")));
}
@Test
void updatingTagsLeavesAnActiveReadOnItsOriginalMap() {
Map<String, String> tagsBeingRead = service.describeStackSet(SET).getTags();