From 918ce95b53eaa2758a92a2b6f18ce868c8d11d17 Mon Sep 17 00:00:00 2001 From: galsuchetzky-act Date: Thu, 5 Nov 2026 19:38:13 +0200 Subject: [PATCH] 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. --- docs/services/cloudformation.md | 2 +- .../CloudFormationQueryHandler.java | 21 +++++++ .../cloudformation/StackSetService.java | 14 ++++- ...erviceManagedStackSetsIntegrationTest.java | 46 +++++++++++++++ .../cloudformation/StackSetTagReadTest.java | 56 +++++++++++++++++++ 5 files changed, 136 insertions(+), 3 deletions(-) create mode 100644 src/test/java/io/github/hectorvent/floci/services/cloudformation/StackSetTagReadTest.java diff --git a/docs/services/cloudformation.md b/docs/services/cloudformation.md index 5e757008c3..8e5a14db9f 100644 --- a/docs/services/cloudformation.md +++ b/docs/services/cloudformation.md @@ -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. diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationQueryHandler.java b/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationQueryHandler.java index d10b910e42..9f48e8e218 100644 --- a/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationQueryHandler.java +++ b/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationQueryHandler.java @@ -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 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 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) diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudformation/StackSetService.java b/src/main/java/io/github/hectorvent/floci/services/cloudformation/StackSetService.java index 98c874dd81..1213901f73 100644 --- a/src/main/java/io/github/hectorvent/floci/services/cloudformation/StackSetService.java +++ b/src/main/java/io/github/hectorvent/floci/services/cloudformation/StackSetService.java @@ -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 organizationalUnitIds(String name) { + return listStackSetAutoDeploymentTargets(name).stream() + .map(StackSetAutoDeploymentTarget::getOrganizationalUnitId) + .toList(); + } + public List listStackInstances(String name, String accountFilter, String regionFilter) { getStackSetOrThrow(name); String prefix = name + ":"; diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationServiceManagedStackSetsIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationServiceManagedStackSetsIntegrationTest.java index 39e01962a5..05ce2f28eb 100644 --- a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationServiceManagedStackSetsIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationServiceManagedStackSetsIntegrationTest.java @@ -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("")) + .body(containsString("DeploymentVersion")) + .body(containsString("20261101-abcdef12")) + .body(not(containsString(""))); + + 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("")) + .body(containsString("" + rootId + "")) + .body(containsString("" + targetOu + "")); + } + private String createAccount(String email, String name) { JsonPath response = organizations("CreateAccount", "{\"Email\":\"" + email + "\",\"AccountName\":\"" + name + "\"}") diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/StackSetTagReadTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/StackSetTagReadTest.java new file mode 100644 index 0000000000..3e617f7389 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/StackSetTagReadTest.java @@ -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 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 tagsBeingRead = service.describeStackSet(SET).getTags(); + Iterator> iterator = tagsBeingRead.entrySet().iterator(); + assertEquals("team", iterator.next().getKey()); + Map updated = Map.of("team", "blue", "owner", "updated"); + + service.updateStackSet(SET, null, null, null, updated, null); + + Map.Entry 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 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()); + } + +}