@Test public void validRole() { String roleArn = "account/validRole"; Role expectedRole = new Role().withArn(roleArn); GetRoleResult getRoleResult = mock(GetRoleResult.class); when(getRoleResult.getRole()).thenReturn(expectedRole); when(iam.getRole(any(GetRoleRequest.class))).thenReturn(getRoleResult); ValidationResultBuilder validationRequestBuilder = new ValidationResultBuilder(); Role role = awsIamService.getRole(iam, roleArn, validationRequestBuilder); assertThat(role.getArn()).isEqualTo(roleArn); assertThat(validationRequestBuilder.build().hasError()).isFalse(); }
public Role getRole(AmazonIdentityManagement iam, String roleArn, ValidationResultBuilder validationResultBuilder) { Role role = null; if (roleArn != null && roleArn.contains("/")) { String roleName = roleArn.split("/", 2)[1]; GetRoleRequest roleRequest = new GetRoleRequest().withRoleName(roleName); try { role = iam.getRole(roleRequest).getRole(); } catch (NoSuchEntityException | ServiceFailureException e) { String msg = String.format("Role (%s) doesn't exist.", roleArn); LOGGER.debug(msg, e); validationResultBuilder.error(msg); } } return role; }
AwsIamService { public Role getRole(AmazonIdentityManagement iam, String roleArn, ValidationResultBuilder validationResultBuilder) { Role role = null; if (roleArn != null && roleArn.contains("/")) { String roleName = roleArn.split("/", 2)[1]; GetRoleRequest roleRequest = new GetRoleRequest().withRoleName(roleName); try { role = iam.getRole(roleRequest).getRole(); } catch (NoSuchEntityException | ServiceFailureException e) { String msg = String.format("Role (%s) doesn't exist.", roleArn); LOGGER.debug(msg, e); validationResultBuilder.error(msg); } } return role; } }
AwsIamService { public Role getRole(AmazonIdentityManagement iam, String roleArn, ValidationResultBuilder validationResultBuilder) { Role role = null; if (roleArn != null && roleArn.contains("/")) { String roleName = roleArn.split("/", 2)[1]; GetRoleRequest roleRequest = new GetRoleRequest().withRoleName(roleName); try { role = iam.getRole(roleRequest).getRole(); } catch (NoSuchEntityException | ServiceFailureException e) { String msg = String.format("Role (%s) doesn't exist.", roleArn); LOGGER.debug(msg, e); validationResultBuilder.error(msg); } } return role; }  }
AwsIamService { public Role getRole(AmazonIdentityManagement iam, String roleArn, ValidationResultBuilder validationResultBuilder) { Role role = null; if (roleArn != null && roleArn.contains("/")) { String roleName = roleArn.split("/", 2)[1]; GetRoleRequest roleRequest = new GetRoleRequest().withRoleName(roleName); try { role = iam.getRole(roleRequest).getRole(); } catch (NoSuchEntityException | ServiceFailureException e) { String msg = String.format("Role (%s) doesn't exist.", roleArn); LOGGER.debug(msg, e); validationResultBuilder.error(msg); } } return role; }  InstanceProfile getInstanceProfile(AmazonIdentityManagement iam, String instanceProfileArn,
            ValidationResultBuilder validationResultBuilder); Set<Role> getValidRoles(AmazonIdentityManagement iam, Set<String> roleArns,
            ValidationResultBuilder validationResultBuilder); Role getRole(AmazonIdentityManagement iam, String roleArn,
            ValidationResultBuilder validationResultBuilder); Policy getAssumeRolePolicy(Role role); Policy getPolicy(String policyFileName, Map<String, String> replacements); SortedSet<String> getStatementActions(Statement statement); SortedSet<String> getStatementResources(Statement statement); List<EvaluationResult> simulatePrincipalPolicy(AmazonIdentityManagement iam,
            String policySourceArn, Collection<String> actionNames, Collection<String> resourceArns); List<EvaluationResult> validateRolePolicies(AmazonIdentityManagement iam, Role role,
            Collection<Policy> policies); }
AwsIamService { public Role getRole(AmazonIdentityManagement iam, String roleArn, ValidationResultBuilder validationResultBuilder) { Role role = null; if (roleArn != null && roleArn.contains("/")) { String roleName = roleArn.split("/", 2)[1]; GetRoleRequest roleRequest = new GetRoleRequest().withRoleName(roleName); try { role = iam.getRole(roleRequest).getRole(); } catch (NoSuchEntityException | ServiceFailureException e) { String msg = String.format("Role (%s) doesn't exist.", roleArn); LOGGER.debug(msg, e); validationResultBuilder.error(msg); } } return role; }  InstanceProfile getInstanceProfile(AmazonIdentityManagement iam, String instanceProfileArn,
            ValidationResultBuilder validationResultBuilder); Set<Role> getValidRoles(AmazonIdentityManagement iam, Set<String> roleArns,
            ValidationResultBuilder validationResultBuilder); Role getRole(AmazonIdentityManagement iam, String roleArn,
            ValidationResultBuilder validationResultBuilder); Policy getAssumeRolePolicy(Role role); Policy getPolicy(String policyFileName, Map<String, String> replacements); SortedSet<String> getStatementActions(Statement statement); SortedSet<String> getStatementResources(Statement statement); List<EvaluationResult> simulatePrincipalPolicy(AmazonIdentityManagement iam,
            String policySourceArn, Collection<String> actionNames, Collection<String> resourceArns); List<EvaluationResult> validateRolePolicies(AmazonIdentityManagement iam, Role role,
            Collection<Policy> policies);  }