From f71903379c5bbf3ef5009f3ee721e5d83a2a7c59 Mon Sep 17 00:00:00 2001 From: lex00 <121451605+lex00@users.noreply.github.com> Date: Wed, 5 Aug 2026 21:16:48 -0600 Subject: [PATCH] fix(cloudformation): AWS::IAM::ManagedPolicy exposes PolicyArn for Fn::GetAtt (#2056) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit CloudFormation documents PolicyArn as the attribute for this type, so a template written against AWS reads it back with Fn::GetAtt [, PolicyArn]. The provisioner only recorded the value under "Arn", so that GetAtt did not resolve and the unresolved literal propagated into whatever consumed it — typically a role's ManagedPolicyArns, which then failed with "Policy .PolicyArn does not exist" and rolled the stack back, several resources away from the cause. Both keys are now recorded; "Arn" stays for anything already reading it. --- .../CloudFormationResourceProvisioner.java | 6 +++++ ...FormationIamAttachmentProvisionerTest.java | 22 +++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationResourceProvisioner.java b/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationResourceProvisioner.java index 5555e2f2ef..c7f9134242 100644 --- a/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationResourceProvisioner.java +++ b/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationResourceProvisioner.java @@ -2201,7 +2201,13 @@ public class CloudFormationResourceProvisioner { var policy = iamService.createPolicy(policyName, "/", null, document, Map.of()); r.getAttributes().put(ROLLBACK_OWNED_ATTR, "true"); r.setPhysicalId(policy.getArn()); + // PolicyArn is the attribute CloudFormation documents for this type, and what a template + // written against AWS asks for. Without it Fn::GetAtt does not resolve and the unresolved + // literal reaches whatever consumed it — a role's ManagedPolicyArns, typically, which then + // fails with "policy does not exist" and rolls the stack back. "Arn" stays for callers + // already using it. r.getAttributes().put("Arn", policy.getArn()); + r.getAttributes().put("PolicyArn", policy.getArn()); r.getAttributes().put("ManagedPolicyRoleTargets", String.join("\n", roleNames)); LinkedHashSet attachedRoleNames = new LinkedHashSet<>(); diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationIamAttachmentProvisionerTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationIamAttachmentProvisionerTest.java index 22051fa071..6f270e6822 100644 --- a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationIamAttachmentProvisionerTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationIamAttachmentProvisionerTest.java @@ -190,6 +190,28 @@ class CloudFormationIamAttachmentProvisionerTest { verify(iamService, never()).deleteRole("existing-role"); } + @Test + void managedPolicyExposesPolicyArnForGetAtt() { + // PolicyArn is the attribute CloudFormation documents for AWS::IAM::ManagedPolicy. Without + // it Fn::GetAtt does not resolve and the unresolved literal reaches the consuming resource. + String policyArn = "arn:aws:iam::" + ACCOUNT_ID + ":policy/test-policy"; + IamPolicy policy = new IamPolicy(); + policy.setArn(policyArn); + when(iamService.createPolicy("test-policy", "/", null, policyDocument(), Map.of())) + .thenReturn(policy); + + StackResource result = provision("ManagedPolicy", "AWS::IAM::ManagedPolicy", """ + { + "ManagedPolicyName": "test-policy", + "PolicyDocument": {"Version": "2012-10-17", "Statement": []} + } + """); + + assertEquals(policyArn, result.getAttributes().get("PolicyArn")); + assertEquals(policyArn, result.getAttributes().get("Arn")); + assertEquals(policyArn, result.getPhysicalId()); + } + @Test void cleanupFailureDoesNotMaskPrimaryFailureOrSkipDeletion() { IamRole role = role("cleanup-role");