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
This commit is contained in:
Hector Ventura authored and GitHub committed 2026-09-21 00:02:03 -04:00
1 parent f9d1b8101c
commit 186a5380d5
2 files changed
+42 -3

No files matched your search

@@ -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();