fix(cloudformation): list a service-managed stack set's targets and tags in DescribeStackSet (#7242)

AWS returns a stack set's tags and the recorded root and OU deployment targets of a service-managed stack set. Read the same saved targets that ListStackSetAutoDeploymentTargets reports.

Replace the tag map on UpdateStackSet instead of clearing and refilling a map that a concurrent DescribeStackSet may be reading. Copy the request's tags so the published map is independent of later request changes.

Cover both response members, an update interleaved with tag iteration, and request-map isolation with regression tests.

Use the shared account-scoped InMemoryStorageFactory for the added regression tests instead of a private factory copy.
This commit is contained in:
galsuchetzky-act authored and GitHub committed 2026-11-05 13:38:13 -04:00
1 parent 5bcb83b932
commit 918ce95b53
5 files changed
+136 -3

No files matched your search

+1 -1
View File
@@ -64,7 +64,7 @@ changing credentials already issued.
## StackSets compatibility
StackSets support both `SELF_MANAGED` and the Cloud Launchpad `SERVICE_MANAGED` workflow. `ActivateOrganizationsAccess` requires an Organizations management account with all features enabled and enables trusted access for `stacksets.cloudformation.amazonaws.com`. Service-managed organizational-unit deployment targets are resolved from the caller's actual Organizations state rather than converted into synthetic account IDs. Targeting an OU includes eligible accounts directly in that OU and in all descendant OUs, while excluding the organization management account, matching AWS StackSets targeting semantics.
StackSets support both `SELF_MANAGED` and the Cloud Launchpad `SERVICE_MANAGED` workflow. `ActivateOrganizationsAccess` requires an Organizations management account with all features enabled and enables trusted access for `stacksets.cloudformation.amazonaws.com`. Service-managed organizational-unit deployment targets are resolved from the caller's actual Organizations state rather than converted into synthetic account IDs. Targeting an OU includes eligible accounts directly in that OU and in all descendant OUs, while excluding the organization management account, matching AWS StackSets targeting semantics. `DescribeStackSet` returns the stack set's `Tags` and, for a service-managed stack set, the root and OU ids its instances were created for as `OrganizationalUnitIds`.
`CreateStackInstances` and `UpdateStackSet` execute the backing CloudFormation stacks in each target account. A failed resource can therefore drive the backing stack through rollback and leave the stack instance `INOPERABLE` with detailed status `FAILED`; the StackSet operation is reported as `FAILED` instead of being forced to success.
@@ -920,6 +920,15 @@ public class CloudFormationQueryHandler {
.end("ManagedExecution");
appendCapabilities(xml, ss.getCapabilities());
appendParameters(xml, ss.getParameters());
appendStackSetTags(xml, ss.getTags());
if ("SERVICE_MANAGED".equals(ss.getPermissionModel())) {
List<String> targets = stackSetService.organizationalUnitIds(ss.getStackSetName());
if (!targets.isEmpty()) {
xml.start("OrganizationalUnitIds");
targets.forEach(target -> xml.elem("member", target));
xml.end("OrganizationalUnitIds");
}
}
xml.end("StackSet").end("DescribeStackSetResult")
.raw(AwsQueryResponse.responseMetadata())
.end("DescribeStackSetResponse");
@@ -1168,6 +1177,18 @@ public class CloudFormationQueryHandler {
xml.end("Parameters");
}
private void appendStackSetTags(XmlBuilder xml, Map<String, String> tags) {
if (tags == null || tags.isEmpty()) {
return;
}
xml.start("Tags");
tags.forEach((key, value) -> xml.start("member")
.elem("Key", key)
.elem("Value", value)
.end("member"));
xml.end("Tags");
}
private Response xmlError(String code, String message, int status) {
String xml = new XmlBuilder()
.start("ErrorResponse", CF_NS)
@@ -228,8 +228,7 @@ public class StackSetService implements PassedRoleLookup {
ss.getCapabilities().addAll(capabilities);
}
if (tags != null && !tags.isEmpty()) {
ss.getTags().clear();
ss.getTags().putAll(tags);
ss.setTags(new LinkedHashMap<>(tags));
}
if (description != null) {
ss.setDescription(description);
@@ -379,6 +378,17 @@ public class StackSetService implements PassedRoleLookup {
.toList();
}
/**
* The organization root and OU ids a service-managed stack set targets, as DescribeStackSet's
* {@code OrganizationalUnitIds} lists them: every target {@code CreateStackInstances} named that still
* has a Region.
*/
public List<String> organizationalUnitIds(String name) {
return listStackSetAutoDeploymentTargets(name).stream()
.map(StackSetAutoDeploymentTarget::getOrganizationalUnitId)
.toList();
}
public List<StackInstance> listStackInstances(String name, String accountFilter, String regionFilter) {
getStackSetOrThrow(name);
String prefix = name + ":";
@@ -433,6 +433,52 @@ class CloudFormationServiceManagedStackSetsIntegrationTest {
.body(containsString("all features"));
}
@Test
void describeServiceManagedStackSetListsTargetedOrganizationalUnitsAndTags() {
String management = "616161616161";
organizations(management, "CreateOrganization", "{\"FeatureSet\":\"ALL\"}")
.post("/").then().statusCode(200);
String rootId = organizations(management, "ListRoots", "{}")
.post("/").then().statusCode(200)
.extract().jsonPath().getString("Roots[0].Id");
String targetOu = organizations(management, "CreateOrganizationalUnit",
"{\"ParentId\":\"" + rootId + "\",\"Name\":\"DescribeTarget\"}")
.post("/").then().statusCode(200)
.extract().jsonPath().getString("OrganizationalUnit.Id");
cloudFormation(management, "ActivateOrganizationsAccess")
.post("/").then().statusCode(200);
String stackSet = "describe-targets-" + UUID.randomUUID().toString().substring(0, 8);
cloudFormation(management, "CreateStackSet")
.formParam("StackSetName", stackSet)
.formParam("TemplateBody", queueTemplate("unused"))
.formParam("PermissionModel", "SERVICE_MANAGED")
.formParam("AutoDeployment.Enabled", "true")
.formParam("Tags.member.1.Key", "DeploymentVersion")
.formParam("Tags.member.1.Value", "20261101-abcdef12")
.post("/").then().statusCode(200);
cloudFormation(management, "DescribeStackSet")
.formParam("StackSetName", stackSet)
.post("/").then().statusCode(200)
.body(containsString("<Tags>"))
.body(containsString("<Key>DeploymentVersion</Key>"))
.body(containsString("<Value>20261101-abcdef12</Value>"))
.body(not(containsString("<OrganizationalUnitIds>")));
cloudFormation(management, "CreateStackInstances")
.formParam("StackSetName", stackSet)
.formParam("DeploymentTargets.OrganizationalUnitIds.member.1", rootId)
.formParam("DeploymentTargets.OrganizationalUnitIds.member.2", targetOu)
.formParam("Regions.member.1", REGION)
.post("/").then().statusCode(200);
cloudFormation(management, "DescribeStackSet")
.formParam("StackSetName", stackSet)
.post("/").then().statusCode(200)
.body(containsString("<OrganizationalUnitIds>"))
.body(containsString("<member>" + rootId + "</member>"))
.body(containsString("<member>" + targetOu + "</member>"));
}
private String createAccount(String email, String name) {
JsonPath response = organizations("CreateAccount",
"{\"Email\":\"" + email + "\",\"AccountName\":\"" + name + "\"}")
@@ -0,0 +1,56 @@
package io.github.hectorvent.floci.services.cloudformation;
import io.github.hectorvent.floci.testing.InMemoryStorageFactory;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.mock;
class StackSetTagReadTest {
private static final String SET = "tag-snapshot";
private StackSetService service;
private Map<String, String> initialTags;
@BeforeEach
void setUp() {
service = new StackSetService(mock(CloudFormationService.class), new InMemoryStorageFactory("000000000000"));
initialTags = new LinkedHashMap<>();
initialTags.put("team", "red");
initialTags.put("owner", "original");
service.createStackSet(SET, "{\"Resources\":{}}", null, null, initialTags, null);
}
@Test
void updatingTagsLeavesAnActiveReadOnItsOriginalMap() {
Map<String, String> tagsBeingRead = service.describeStackSet(SET).getTags();
Iterator<Map.Entry<String, String>> iterator = tagsBeingRead.entrySet().iterator();
assertEquals("team", iterator.next().getKey());
Map<String, String> updated = Map.of("team", "blue", "owner", "updated");
service.updateStackSet(SET, null, null, null, updated, null);
Map.Entry<String, String> remaining = assertDoesNotThrow(iterator::next);
assertEquals("owner", remaining.getKey());
assertEquals("original", remaining.getValue());
assertEquals(initialTags, tagsBeingRead);
assertEquals(updated, service.describeStackSet(SET).getTags());
}
@Test
void updatingTagsCopiesTheRequestMap() {
Map<String, String> updated = new LinkedHashMap<>();
updated.put("team", "blue");
service.updateStackSet(SET, null, null, null, updated, null);
updated.put("team", "green");
assertEquals(Map.of("team", "blue"), service.describeStackSet(SET).getTags());
}
}