From 186a5380d55d658e3fc472f29f2af58df91bc812 Mon Sep 17 00:00:00 2001 From: Hector Ventura Date: Sun, 20 Sep 2026 23:02:03 -0500 Subject: [PATCH] fix(cloudformation): validate required fields on AWS::IAM::Policy (#4050) IamPolicyCfnProvisioner accepted a Policy that AWS rejects: it generated a PolicyName when the property was absent, and it silently created nothing when no principal was named. Now a genuine create missing PolicyName is rejected with ValidationError (the registry required set lists PolicyName, and PutRolePolicy demands it), and a Policy naming none of Roles/Users/Groups is rejected too (AWS requires at least one, mirrored by Terraform's AtLeastOneOf on the attachment). Updates keep their behavior: a normal update reuses the recorded name, and the legacy managed-policy migration still generates a fresh inline name because its prior id is an ARN, not a usable policy name. Refs #3971 --- .../provisioners/IamPolicyCfnProvisioner.java | 27 ++++++++++++++++--- .../IamPolicyCfnProvisionerTest.java | 18 +++++++++++++ 2 files changed, 42 insertions(+), 3 deletions(-) diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/IamPolicyCfnProvisioner.java b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/IamPolicyCfnProvisioner.java index f373ce28cd..8a5821e5a3 100644 --- a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/IamPolicyCfnProvisioner.java +++ b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/IamPolicyCfnProvisioner.java @@ -65,9 +65,25 @@ public class IamPolicyCfnProvisioner implements CfnResourceProvisioner { boolean legacyManagedPolicy = isIamManagedPolicyArn(previousPolicyName); String policyName = ctx.resolveOptional(props, "PolicyName"); if (policyName == null || policyName.isBlank()) { - policyName = previousPolicyName != null && !previousPolicyName.isBlank() && !legacyManagedPolicy - ? previousPolicyName - : ctx.generatePhysicalName(r.getLogicalId(), 128, false); + // PolicyName is required for AWS::IAM::Policy (registry required set, and PutRolePolicy + // demands it). A genuine first create has nothing to fall back to, so reject rather than + // invent a name. An update keeps its prior: a normal one reuses the recorded name, and a + // legacy managed-policy migration generates a fresh inline name because the prior id is + // an ARN, not a usable policy name. + if (previousPolicyName == null || previousPolicyName.isBlank()) { + throw new AwsException("ValidationError", + "AWS::IAM::Policy " + r.getLogicalId() + " is missing the required PolicyName property", 400); + } + policyName = legacyManagedPolicy + ? ctx.generatePhysicalName(r.getLogicalId(), 128, false) + : previousPolicyName; + } + // An inline policy must attach to at least one principal; AWS rejects a Policy that names + // none of Roles, Users or Groups. + if (!hasPrincipals(props, "Roles") && !hasPrincipals(props, "Users") && !hasPrincipals(props, "Groups")) { + throw new AwsException("ValidationError", + "AWS::IAM::Policy " + r.getLogicalId() + + " must specify at least one of Roles, Users or Groups", 400); } String document = ctx.resolvePolicyDocument(props); @@ -133,6 +149,11 @@ public class IamPolicyCfnProvisioner implements CfnResourceProvisioner { name -> iamService.deleteGroupPolicy(name, policyName)); } + private static boolean hasPrincipals(JsonNode props, String propName) { + JsonNode node = props == null ? null : props.get(propName); + return node != null && node.isArray() && !node.isEmpty(); + } + private void putInlinePolicy(JsonNode props, String propName, CloudFormationTemplateEngine engine, List successfulTargets, Consumer op) { if (props == null || !props.has(propName)) { diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/IamPolicyCfnProvisionerTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/IamPolicyCfnProvisionerTest.java index 56ea31195e..b32cf213c4 100644 --- a/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/IamPolicyCfnProvisionerTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/IamPolicyCfnProvisionerTest.java @@ -52,6 +52,24 @@ class IamPolicyCfnProvisionerTest { assertEquals(null, r.getAttributes().get("Arn")); } + @Test + void missingPolicyNameOnCreateIsRejected() throws Exception { + AwsException e = assertThrows(AwsException.class, () -> provisioner.provision(resource(), props(""" + {"PolicyDocument": {"Version": "2012-10-17", "Statement": []}, + "Roles": ["web-role"]} + """), ctx(null))); + assertEquals("ValidationError", e.getErrorCode()); + } + + @Test + void aPolicyThatNamesNoPrincipalIsRejected() throws Exception { + AwsException e = assertThrows(AwsException.class, () -> provisioner.provision(resource(), props(""" + {"PolicyName": "app-policy", + "PolicyDocument": {"Version": "2012-10-17", "Statement": []}} + """), ctx(null))); + assertEquals("ValidationError", e.getErrorCode()); + } + @Test void updateReusesTheNameAndDetachesAPrincipalNoLongerListed() throws Exception { StackResource r = resource();