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

+3 -3
View File
@@ -116,9 +116,9 @@ call `LeaveOrganization`. An account in no organization gets
| `EnablePolicyType` | Enables a policy type on the specified root. |
| `DisablePolicyType` | Disables a policy type on the specified root. |
| `DescribeEffectivePolicy` | Returns the merged policy of the given type that applies to a target. |
| `TagResource` | Adds or overwrites tags on a root, OU, account or policy. |
| `UntagResource` | Removes the specified tags from a root, OU, account or policy. |
| `ListTagsForResource` | Lists the tags on a root, OU, account or policy. |
| `TagResource` | Adds or overwrites tags on a root, OU, account, policy or resource policy. |
| `UntagResource` | Removes the specified tags from a root, OU, account, policy or resource policy. |
| `ListTagsForResource` | Lists the tags on a root, OU, account, policy or resource policy. |
| `EnableAWSServiceAccess` | Enables trusted access for an AWS service principal. |
| `DisableAWSServiceAccess` | Disables trusted access for an AWS service principal. |
| `ListAWSServiceAccessForOrganization` | Lists the service principals with trusted access enabled. |
@@ -1253,10 +1253,8 @@ public class OrganizationsService implements ScpProvider, RootAccessOrganization
organization.setResourcePolicyContent(content);
if (tags != null) {
// Put replaces rather than merges: a caller that supplies Tags is stating the full set,
// so a key it no longer lists is dropped. The resource policy is not addressable by
// TagResource/UntagResource the way accounts, OUs, roots and policies are, so this call
// is the only way CloudFormation can converge Tags on an update. Omitting Tags
// entirely (null) leaves the existing ones untouched.
// so a key it no longer lists is dropped. CloudFormation relies on this to converge
// Tags on an update. Omitting Tags entirely (null) leaves the existing ones untouched.
organization.getResourcePolicyTags().clear();
organization.getResourcePolicyTags().putAll(tags);
}
@@ -1806,6 +1804,10 @@ public class OrganizationsService implements ScpProvider, RootAccessOrganization
return new TagOwner(policy.getTags(),
() -> policies.putForAccount(master, resourceId, policy));
}
if (resourceId.equals(organization.getResourcePolicyId())) {
return new TagOwner(organization.getResourcePolicyTags(),
() -> organizations.putForAccount(master, organization.getId(), organization));
}
throw new AwsException("TargetNotFoundException",
"We can't find a resource with the ResourceId " + resourceId + ".", 400);
}
@@ -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,