fix(organizations): apply PutResourcePolicy tags only on creation (#8174)

PutResourcePolicy replaced the tag set of an existing resource policy
whenever the request carried Tags. The model says tags apply only to
the initial creation of the resource policy and that the call throws
otherwise. It now applies Tags only when it creates the policy and
answers InvalidInputException for Tags on an existing one, before it
changes anything. The model names no error for this case;
InvalidInputException is the one it lists for invalid request values.
An empty Tags list has no tags to apply and is accepted.

The CloudFormation provisioner relied on the old replace to converge
the Tags of AWS::Organizations::ResourcePolicy on update. An update now
leaves Tags out of PutResourcePolicy and converges them through
TagResource and UntagResource by the rp- ID, as the provisioner does
for OUs, accounts and policies and as AWS's own handler does.

Co-authored-by: Priamo Germosen <6233424+pgermosen@users.noreply.github.com>
This commit is contained in:
soreavisandPriamo Germosen authored and GitHub committed 2026-11-12 22:18:32 -04:00
1 parent 405a3cad12
commit b2fa3e0cfb
7 files changed
+141 -38

No files matched your search

@@ -260,9 +260,17 @@ public class OrganizationsCfnProvisioner implements CfnResourceProvisioner {
// ──────────────────────────── Resource policy ────────────────────────────
private void provisionResourcePolicy(StackResource r, JsonNode props, ProvisionContext ctx) {
// PutResourcePolicy is already create-or-update, so the same call serves both paths.
OrganizationsService.ResourcePolicyView policy = organizationsService.putResourcePolicy(
ctx.accountId(), resolveContent(props, ctx), resolveTags(props, ctx));
String content = resolveContent(props, ctx);
Map<String, String> tags = resolveTags(props, ctx);
OrganizationsService.ResourcePolicyView policy;
if (ctx.isUpdate()) {
// PutResourcePolicy applies Tags only when it creates the policy.
policy = organizationsService.putResourcePolicy(ctx.accountId(), content, null);
replaceTags(ctx.accountId(), policy.id(), tags);
} else {
policy = organizationsService.putResourcePolicy(ctx.accountId(), content, tags);
}
r.setPhysicalId(policy.id());
r.getAttributes().put("Id", policy.id());
@@ -1249,15 +1249,16 @@ public class OrganizationsService implements ScpProvider, RootAccessOrganization
organization.setResourcePolicyId(policyId);
organization.setResourcePolicyArn(
arn(organization.getMasterAccountId(), "resourcepolicy/" + organization.getId() + "/" + policyId));
if (tags != null) {
organization.getResourcePolicyTags().putAll(tags);
}
} else if (tags != null && !tags.isEmpty()) {
// The model says Tags apply only when this call creates the policy and that it throws
// otherwise, without naming the error. An empty list has no tags to apply.
throw invalidInput("Tags apply only to the initial creation of the resource policy."
+ " Use TagResource and UntagResource to change its tags.");
}
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. 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);
}
organizations.putForAccount(organization.getMasterAccountId(), organization.getId(), organization);
return resourcePolicyView(organization);
}