|
18 | 18 | import software.amazon.awssdk.services.iam.model.EntityAlreadyExistsException; |
19 | 19 | import software.amazon.awssdk.services.iam.model.GetRoleRequest; |
20 | 20 | import software.amazon.awssdk.services.iam.model.GetRoleResponse; |
| 21 | +import software.amazon.awssdk.services.iam.model.GetRolePolicyRequest; |
| 22 | +import software.amazon.awssdk.services.iam.model.GetRolePolicyResponse; |
| 23 | +import software.amazon.awssdk.services.iam.model.NoSuchEntityException; |
21 | 24 | import software.amazon.awssdk.services.iam.model.Role; |
22 | 25 | import software.amazon.awssdk.services.iam.model.UpdateAssumeRolePolicyRequest; |
23 | 26 | import software.amazon.awssdk.services.iam.model.UpdateAssumeRolePolicyResponse; |
@@ -49,6 +52,8 @@ public class AwsIamTest { |
49 | 52 | private static final String TEST_TENANT_ID = "123456789012"; |
50 | 53 | private static final String TEST_REGION = "us-west-2"; |
51 | 54 | private static final String TEST_ROLE_ARN = "arn:aws:iam::123456789012:role/TestRole"; |
| 55 | + private static final String TEST_POLICY_NAME = "TestPolicy"; |
| 56 | + private static final String TEST_POLICY_DOCUMENT = "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Action\":\"s3:GetObject\",\"Resource\":\"*\"}]}"; |
52 | 57 | public static final ObjectMapper OBJECT_MAPPER = new ObjectMapper(); |
53 | 58 |
|
54 | 59 | @Mock |
@@ -157,16 +162,10 @@ void testCreateIdentityWithoutTrustConfigDefaultsToSameAccountRoot() throws Exce |
157 | 162 |
|
158 | 163 | JsonNode stmt = doc.at("/Statement/0"); |
159 | 164 | assertFalse(stmt.isMissingNode(), "Statement should not be missing"); |
160 | | - JsonNode effect = stmt.at("/Effect"); |
161 | | - assertFalse(effect.isMissingNode(), "Effect should not be missing"); |
162 | | - assertEquals("Allow", effect.asText()); |
163 | | - JsonNode action = stmt.at("/Action"); |
164 | | - assertFalse(action.isMissingNode(), "Action should not be missing"); |
165 | | - assertEquals("sts:AssumeRole", action.asText()); |
166 | | - JsonNode principal = stmt.at("/Principal/AWS"); |
167 | | - assertFalse(principal.isMissingNode(), "Principal should not be missing"); |
| 165 | + assertEquals("Allow", stmt.at("/Effect").asText()); |
| 166 | + assertEquals("sts:AssumeRole", stmt.at("/Action").asText()); |
168 | 167 | assertEquals("arn:aws:iam::" + TEST_TENANT_ID + ":root", |
169 | | - principal.asText()); |
| 168 | + stmt.at("/Principal/AWS").asText()); |
170 | 169 | } |
171 | 170 |
|
172 | 171 | @Test |
@@ -443,7 +442,7 @@ void testCreateIdentityAlreadyExistsUpdatesTrustPolicyWhenDifferent() throws Exc |
443 | 442 |
|
444 | 443 | assertEquals(TEST_ROLE_ARN, result); |
445 | 444 |
|
446 | | - ArgumentCaptor<UpdateAssumeRolePolicyRequest> updatePolicyCaptor = |
| 445 | + ArgumentCaptor<UpdateAssumeRolePolicyRequest> updatePolicyCaptor = |
447 | 446 | ArgumentCaptor.forClass(UpdateAssumeRolePolicyRequest.class); |
448 | 447 | verify(mockIamClient, times(1)).updateAssumeRolePolicy(updatePolicyCaptor.capture()); |
449 | 448 | assertEquals(TEST_ROLE_NAME, updatePolicyCaptor.getValue().roleName()); |
@@ -535,15 +534,100 @@ void testCreateIdentityAlreadyExistsUpdatesMultipleAttributesWhenDifferent() thr |
535 | 534 | assertEquals(newDescription, updateCaptor.getValue().description()); |
536 | 535 | assertEquals(7200, updateCaptor.getValue().maxSessionDuration()); |
537 | 536 |
|
538 | | - ArgumentCaptor<UpdateAssumeRolePolicyRequest> updatePolicyCaptor = |
| 537 | + ArgumentCaptor<UpdateAssumeRolePolicyRequest> updatePolicyCaptor = |
539 | 538 | ArgumentCaptor.forClass(UpdateAssumeRolePolicyRequest.class); |
540 | 539 | verify(mockIamClient, times(1)).updateAssumeRolePolicy(updatePolicyCaptor.capture()); |
541 | 540 | assertEquals(TEST_ROLE_NAME, updatePolicyCaptor.getValue().roleName()); |
542 | 541 | } |
543 | 542 |
|
544 | 543 | private String buildDefaultAssumeRolePolicy() { |
545 | 544 | return "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\"," + |
546 | | - "\"Action\":\"sts:AssumeRole\",\"Principal\":{\"AWS\":\"arn:aws:iam::" + |
| 545 | + "\"Action\":\"sts:AssumeRole\",\"Principal\":{\"AWS\":\"arn:aws:iam::" + |
547 | 546 | TEST_TENANT_ID + ":root\"}}]}"; |
548 | 547 | } |
| 548 | + |
| 549 | + @Test |
| 550 | + void testGetInlinePolicyDetailsReturnsDocument() { |
| 551 | + when(mockIamClient.getRolePolicy(any(GetRolePolicyRequest.class))) |
| 552 | + .thenReturn(GetRolePolicyResponse.builder() |
| 553 | + .policyDocument(TEST_POLICY_DOCUMENT) |
| 554 | + .build()); |
| 555 | + |
| 556 | + String result = awsIam.getInlinePolicyDetails( |
| 557 | + TEST_ROLE_NAME, |
| 558 | + TEST_POLICY_NAME, |
| 559 | + TEST_ROLE_NAME, |
| 560 | + TEST_TENANT_ID, |
| 561 | + TEST_REGION); |
| 562 | + |
| 563 | + assertEquals(TEST_POLICY_DOCUMENT, result); |
| 564 | + |
| 565 | + ArgumentCaptor<GetRolePolicyRequest> captor = ArgumentCaptor.forClass(GetRolePolicyRequest.class); |
| 566 | + verify(mockIamClient, times(1)).getRolePolicy(captor.capture()); |
| 567 | + assertEquals(TEST_ROLE_NAME, captor.getValue().roleName()); |
| 568 | + assertEquals(TEST_POLICY_NAME, captor.getValue().policyName()); |
| 569 | + } |
| 570 | + |
| 571 | + @Test |
| 572 | + void testGetInlinePolicyDetailsWithNoSuchEntityException() { |
| 573 | + when(mockIamClient.getRolePolicy(any(GetRolePolicyRequest.class))) |
| 574 | + .thenThrow(NoSuchEntityException.builder() |
| 575 | + .message("Policy not found") |
| 576 | + .build()); |
| 577 | + |
| 578 | + assertThrows(NoSuchEntityException.class, () -> |
| 579 | + awsIam.getInlinePolicyDetails( |
| 580 | + TEST_ROLE_NAME, |
| 581 | + TEST_POLICY_NAME, |
| 582 | + TEST_ROLE_NAME, |
| 583 | + TEST_TENANT_ID, |
| 584 | + TEST_REGION) |
| 585 | + ); |
| 586 | + |
| 587 | + ArgumentCaptor<GetRolePolicyRequest> captor = ArgumentCaptor.forClass(GetRolePolicyRequest.class); |
| 588 | + verify(mockIamClient, times(1)).getRolePolicy(captor.capture()); |
| 589 | + assertEquals(TEST_ROLE_NAME, captor.getValue().roleName()); |
| 590 | + assertEquals(TEST_POLICY_NAME, captor.getValue().policyName()); |
| 591 | + } |
| 592 | + |
| 593 | + @Test |
| 594 | + void testGetInlinePolicyDetailsVerifiesParameters() { |
| 595 | + when(mockIamClient.getRolePolicy(any(GetRolePolicyRequest.class))) |
| 596 | + .thenReturn(GetRolePolicyResponse.builder() |
| 597 | + .policyDocument(TEST_POLICY_DOCUMENT) |
| 598 | + .build()); |
| 599 | + |
| 600 | + awsIam.getInlinePolicyDetails( |
| 601 | + TEST_ROLE_NAME, |
| 602 | + TEST_POLICY_NAME, |
| 603 | + TEST_ROLE_NAME, |
| 604 | + TEST_TENANT_ID, |
| 605 | + TEST_REGION); |
| 606 | + |
| 607 | + ArgumentCaptor<GetRolePolicyRequest> captor = ArgumentCaptor.forClass(GetRolePolicyRequest.class); |
| 608 | + verify(mockIamClient).getRolePolicy(captor.capture()); |
| 609 | + |
| 610 | + GetRolePolicyRequest capturedRequest = captor.getValue(); |
| 611 | + assertEquals(TEST_ROLE_NAME, capturedRequest.roleName()); |
| 612 | + assertEquals(TEST_POLICY_NAME, capturedRequest.policyName()); |
| 613 | + } |
| 614 | + |
| 615 | + @Test |
| 616 | + void testGetInlinePolicyDetailsThrowsGenericException() { |
| 617 | + RuntimeException genericException = new RuntimeException("Service error"); |
| 618 | + |
| 619 | + when(mockIamClient.getRolePolicy(any(GetRolePolicyRequest.class))) |
| 620 | + .thenThrow(genericException); |
| 621 | + |
| 622 | + RuntimeException exception = assertThrows(RuntimeException.class, () -> |
| 623 | + awsIam.getInlinePolicyDetails( |
| 624 | + TEST_ROLE_NAME, |
| 625 | + TEST_POLICY_NAME, |
| 626 | + TEST_ROLE_NAME, |
| 627 | + TEST_TENANT_ID, |
| 628 | + TEST_REGION) |
| 629 | + ); |
| 630 | + |
| 631 | + assertEquals(genericException, exception); |
| 632 | + } |
549 | 633 | } |
0 commit comments