fix(organizations): resolve the resource policy ID in tag operations (#8111)

TagResource, UntagResource and ListTagsForResource answered
TargetNotFoundException for the organization's resource policy, because
resolveTagOwner matched account, root, OU and policy IDs only. The
model's TaggableResourceId pattern accepts rp- IDs, and AWS's own
CloudFormation handler for AWS::Organizations::ResourcePolicy tags the
policy and lists its tags through that ID.

resolveTagOwner now matches the organization's resource policy ID and
saves the organization, as the root branch does. Any other rp- ID still
answers TargetNotFoundException. The code comments that said a resource
policy is not a TagResource target are corrected, and the docs table
lists the resource policy among the tag targets.
This commit is contained in:
soreavis authored and GitHub committed 2026-11-12 10:44:30 -04:00
1 parent 3f1dcc9173
commit b28caac441
5 files changed
+51 -14

No files matched your search

@@ -478,8 +478,7 @@ class OrganizationsCfnProvisionerTest {
provisioner.provision(r, props, ctx());
// putResourcePolicy replaces the tag map, so handing it exactly the template's set is what
// removes the dropped key. The other three types reach the same end via replaceTags, which
// cannot be used here because a resource policy is not a TagResource target.
// removes the dropped key. The other three types reach the same end via replaceTags.
verify(organizations).putResourcePolicy(eq(ACCOUNT), anyString(), eq(Map.of("env", "prod")));
}
@@ -844,7 +844,7 @@ class OrganizationsIntegrationTest {
String content = "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\","
+ "\"Principal\":{\"AWS\":\"*\"},\"Action\":\"organizations:Describe*\",\"Resource\":\"*\"}]}";
organizations("PutResourcePolicy", "{\"Content\":" + quote(content) + "}")
String resourcePolicyId = organizations("PutResourcePolicy", "{\"Content\":" + quote(content) + "}")
.when()
.post("/")
.then()
@@ -852,7 +852,8 @@ class OrganizationsIntegrationTest {
.body("ResourcePolicy.ResourcePolicySummary.Id", startsWith("rp-"))
.body("ResourcePolicy.ResourcePolicySummary.Arn",
startsWith("arn:aws:organizations::000000000000:resourcepolicy/" + organizationId))
.body("ResourcePolicy.Content", equalTo(content));
.body("ResourcePolicy.Content", equalTo(content))
.extract().jsonPath().getString("ResourcePolicy.ResourcePolicySummary.Id");
organizations("DescribeResourcePolicy", "{}")
.when()
@@ -861,6 +862,22 @@ class OrganizationsIntegrationTest {
.statusCode(200)
.body("ResourcePolicy.Content", equalTo(content));
organizations("TagResource",
"{\"ResourceId\":\"" + resourcePolicyId + "\",\"Tags\":[{\"Key\":\"env\",\"Value\":\"test\"}]}")
.when()
.post("/")
.then()
.statusCode(200);
organizations("ListTagsForResource", "{\"ResourceId\":\"" + resourcePolicyId + "\"}")
.when()
.post("/")
.then()
.statusCode(200)
.body("Tags", hasSize(1))
.body("Tags[0].Key", equalTo("env"))
.body("Tags[0].Value", equalTo("test"));
organizations("DeleteResourcePolicy", "{}")
.when()
.post("/")
@@ -397,8 +397,9 @@ class OrganizationsServiceTest {
service.enablePolicyType(MANAGEMENT_ACCOUNT, rootId, "SERVICE_CONTROL_POLICY");
OrganizationPolicy policy = service.createPolicy(MANAGEMENT_ACCOUNT, "{}", null, "Policy",
"SERVICE_CONTROL_POLICY", null);
String resourcePolicyId = service.putResourcePolicy(MANAGEMENT_ACCOUNT, "{}", null).id();
for (String resourceId : List.of(rootId, unit.getId(), policy.getId(), MANAGEMENT_ACCOUNT)) {
for (String resourceId : List.of(rootId, unit.getId(), policy.getId(), MANAGEMENT_ACCOUNT, resourcePolicyId)) {
service.tagResource(MANAGEMENT_ACCOUNT, resourceId, Map.of("env", "test"));
assertEquals(Map.of("env", "test"), service.listTagsForResource(MANAGEMENT_ACCOUNT, resourceId));
service.untagResource(MANAGEMENT_ACCOUNT, resourceId, List.of("env"));
@@ -415,8 +416,7 @@ class OrganizationsServiceTest {
assertEquals(Map.of("env", "test", "owner", "platform"),
service.describeResourcePolicy(MANAGEMENT_ACCOUNT).tags());
// The resource policy is not addressable by TagResource/UntagResource, so Put is the only
// way a dropped key can be removed — CloudFormation relies on this to converge on update.
// Put drops a key the new set leaves out; CloudFormation relies on that to converge on update.
service.putResourcePolicy(MANAGEMENT_ACCOUNT, content, Map.of("env", "prod"));
assertEquals(Map.of("env", "prod"), service.describeResourcePolicy(MANAGEMENT_ACCOUNT).tags());
@@ -426,6 +426,25 @@ class OrganizationsServiceTest {
assertEquals(Map.of("env", "prod"), service.describeResourcePolicy(MANAGEMENT_ACCOUNT).tags());
}
@Test
void putResourcePolicyTagsAreListedThroughItsId() {
service.createOrganization(MANAGEMENT_ACCOUNT, "ALL");
String policyId = service.putResourcePolicy(MANAGEMENT_ACCOUNT, "{\"Version\":\"2012-10-17\"}",
Map.of("env", "test")).id();
assertEquals(Map.of("env", "test"), service.listTagsForResource(MANAGEMENT_ACCOUNT, policyId));
}
@Test
void anUnknownResourcePolicyIdIsNotATagTarget() {
service.createOrganization(MANAGEMENT_ACCOUNT, "ALL");
service.putResourcePolicy(MANAGEMENT_ACCOUNT, "{\"Version\":\"2012-10-17\"}", null);
AwsException error = assertThrows(AwsException.class, () -> service.tagResource(
MANAGEMENT_ACCOUNT, "rp-doesnotexist", Map.of("env", "test")));
assertEquals("TargetNotFoundException", error.getErrorCode());
}
@Test
void anAccountOutsideAnyOrganizationGetsNotInUse() {
AwsException error = assertThrows(AwsException.class,