From b28caac44190c405a16c3cd8f39ab53e37692cce Mon Sep 17 00:00:00 2001 From: soreavis <263610811+soreavis@users.noreply.github.com> Date: Thu, 12 Nov 2026 15:44:30 +0100 Subject: [PATCH] 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. --- docs/services/organizations.md | 6 ++--- .../organizations/OrganizationsService.java | 10 +++++--- .../OrganizationsCfnProvisionerTest.java | 3 +-- .../OrganizationsIntegrationTest.java | 21 ++++++++++++++-- .../OrganizationsServiceTest.java | 25 ++++++++++++++++--- 5 files changed, 51 insertions(+), 14 deletions(-) diff --git a/docs/services/organizations.md b/docs/services/organizations.md index 41f4fb2853..3483fb8aba 100644 --- a/docs/services/organizations.md +++ b/docs/services/organizations.md @@ -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. | diff --git a/src/main/java/io/floci/aws/services/organizations/OrganizationsService.java b/src/main/java/io/floci/aws/services/organizations/OrganizationsService.java index 5fddc86ab9..e059a57d5d 100644 --- a/src/main/java/io/floci/aws/services/organizations/OrganizationsService.java +++ b/src/main/java/io/floci/aws/services/organizations/OrganizationsService.java @@ -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); } diff --git a/src/test/java/io/floci/aws/services/cloudformation/provisioners/OrganizationsCfnProvisionerTest.java b/src/test/java/io/floci/aws/services/cloudformation/provisioners/OrganizationsCfnProvisionerTest.java index a5de4525cc..3cb9cc18aa 100644 --- a/src/test/java/io/floci/aws/services/cloudformation/provisioners/OrganizationsCfnProvisionerTest.java +++ b/src/test/java/io/floci/aws/services/cloudformation/provisioners/OrganizationsCfnProvisionerTest.java @@ -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"))); } diff --git a/src/test/java/io/floci/aws/services/organizations/OrganizationsIntegrationTest.java b/src/test/java/io/floci/aws/services/organizations/OrganizationsIntegrationTest.java index 9e6a9ef261..6acdd1aa07 100644 --- a/src/test/java/io/floci/aws/services/organizations/OrganizationsIntegrationTest.java +++ b/src/test/java/io/floci/aws/services/organizations/OrganizationsIntegrationTest.java @@ -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("/") diff --git a/src/test/java/io/floci/aws/services/organizations/OrganizationsServiceTest.java b/src/test/java/io/floci/aws/services/organizations/OrganizationsServiceTest.java index 8d668e10c4..7adb27ae4c 100644 --- a/src/test/java/io/floci/aws/services/organizations/OrganizationsServiceTest.java +++ b/src/test/java/io/floci/aws/services/organizations/OrganizationsServiceTest.java @@ -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,