-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
fix(cloudformation): resolve IAM assume role policy intrinsics #2630
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,6 +15,7 @@ | |
| import java.util.Map; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertFalse; | ||
| import static org.junit.jupiter.api.Assertions.assertNull; | ||
| import static org.junit.jupiter.api.Assertions.assertThrows; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
@@ -44,17 +45,21 @@ class IamRoleCfnProvisionerTest { | |
|
|
||
| private ProvisionContext ctx() { | ||
| CloudFormationTemplateEngine engine = mock(CloudFormationTemplateEngine.class); | ||
|
|
||
| when(engine.resolve(any())).thenAnswer(inv -> { | ||
| JsonNode node = inv.getArgument(0); | ||
| return node == null ? null : node.asText(); | ||
| JsonNode node = inv.getArgument(0); | ||
| return node == null ? null : node.asText(); | ||
| }); | ||
|
|
||
| when(engine.resolveNode(any())).thenAnswer(inv -> inv.getArgument(0)); | ||
|
|
||
| when(engine.resolveJsonAttribute(any())).thenAnswer(inv -> { | ||
| JsonNode node = inv.getArgument(0); | ||
| return node != null && node.isTextual() ? node.asText() : node.toString(); | ||
| JsonNode node = inv.getArgument(0); | ||
| return node != null && node.isTextual() ? node.asText() : node.toString(); | ||
| }); | ||
|
|
||
| return new ProvisionContext(engine, "us-east-1", ACCOUNT_ID, "test-stack"); | ||
| } | ||
| } | ||
|
|
||
| private StackResource resource() { | ||
| StackResource r = new StackResource(); | ||
|
|
@@ -364,4 +369,84 @@ void deletePropagatesUnexpectedLookupFailures() { | |
| assertEquals("AccessDenied", failure.getErrorCode()); | ||
| verify(iam, never()).deleteRole("denied-role"); | ||
| } | ||
| @Test | ||
| void assumeRolePolicyIntrinsicsAreResolved() { | ||
|
Comment on lines
+372
to
+373
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The new test annotation and method declaration are placed at column one rather than using the class-member indentation established throughout this file, while the adjacent Context Used: AGENTS.md (source) Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time! |
||
| stubCreate("app-role"); | ||
| StackResource r = resource(); | ||
|
|
||
| CloudFormationTemplateEngine engine = mock(CloudFormationTemplateEngine.class); | ||
|
|
||
| when(engine.resolve(any())).thenAnswer(inv -> { | ||
| JsonNode node = inv.getArgument(0); | ||
| return node == null ? null : node.asText(); | ||
| }); | ||
|
|
||
| JsonNode assumeRolePolicy = props(""" | ||
| { | ||
| "Version": "2012-10-17", | ||
| "Statement": [{ | ||
| "Effect": "Allow", | ||
| "Principal": { | ||
| "AWS": { | ||
| "Fn::Sub": "arn:aws:iam::${AWS::AccountId}:root" | ||
| } | ||
| }, | ||
| "Action": "sts:AssumeRole" | ||
| }] | ||
| } | ||
| """); | ||
|
|
||
| when(engine.resolveJsonAttribute(any())).thenReturn( | ||
| "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\"," | ||
| + "\"Principal\":{\"AWS\":\"arn:aws:iam::" + ACCOUNT_ID + ":root\"}," | ||
| + "\"Action\":\"sts:AssumeRole\"}]}" | ||
| ); | ||
|
|
||
| ProvisionContext ctx = new ProvisionContext( | ||
| engine, | ||
| "us-east-1", | ||
| ACCOUNT_ID, | ||
| "test-stack" | ||
| ); | ||
|
|
||
| JsonNode roleProps = props(""" | ||
| { | ||
| "RoleName": "app-role", | ||
| "AssumeRolePolicyDocument": { | ||
| "Version": "2012-10-17", | ||
| "Statement": [{ | ||
| "Effect": "Allow", | ||
| "Principal": { | ||
| "AWS": { | ||
| "Fn::Sub": "arn:aws:iam::${AWS::AccountId}:root" | ||
| } | ||
| }, | ||
| "Action": "sts:AssumeRole" | ||
| }] | ||
| } | ||
| } | ||
| """); | ||
|
|
||
| provisioner.provision(r, roleProps, ctx); | ||
|
|
||
| ArgumentCaptor<String> docCaptor = ArgumentCaptor.forClass(String.class); | ||
|
|
||
| verify(iam).createRole( | ||
| eq("app-role"), | ||
| eq("/"), | ||
| docCaptor.capture(), | ||
| any(), | ||
| eq(3600), | ||
| eq(Map.of()) | ||
| ); | ||
|
|
||
| String storedDoc = docCaptor.getValue(); | ||
|
|
||
| assertFalse(storedDoc.contains("Fn::Sub")); | ||
| assertFalse(storedDoc.contains("\"Ref\"")); | ||
| assertTrue(storedDoc.contains( | ||
| "arn:aws:iam::" + ACCOUNT_ID + ":root" | ||
| )); | ||
| } | ||
|
|
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When an
AssumeRolePolicyDocumentcontains anFn::Ifwhose selected branch is an object or array,resolveJsonAttributeroutes that branch through scalar resolution and replaces it with an empty string, causing the role to store a malformed trust policy.