AWSIAMAuthService { public ResponseEntity<String> createIAMRole(AWSIAMRole awsiamRole, String token, UserDetails userDetails) throws TVaultValidationException{ if (!ControllerUtil.areAWSIAMRoleInputsValid(awsiamRole)) { throw new TVaultValidationException("Invalid inputs for the given aws login type"); } String jsonStr = JSONUtil.getJSON(awsiamRole); Response response = reqProcessor.process("/auth/aws/iam/role/create",jsonStr, token); if(response.getHttpstatus().equals(HttpStatus.NO_CONTENT)){ String metadataJson = ControllerUtil.populateAWSMetaJson(awsiamRole.getRole(), userDetails.getUsername()); if(ControllerUtil.createMetadata(metadataJson, token)) { return ResponseEntity.status(HttpStatus.OK).body("{\"messages\":[\"AWS IAM Role created successfully \"]}"); } Response deleteResponse = reqProcessor.process("/auth/aws/iam/roles/delete","{\"role\":\""+awsiamRole.getRole()+"\"}",token); if (deleteResponse.getHttpstatus().equals(HttpStatus.NO_CONTENT)) { return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"errors\":[\"AWS IAM role creation failed.\"]}"); } else { return ResponseEntity.status(HttpStatus.OK).body("{\"messages\":[\"AWS IAM role created however metadata update failed. Please try with AWS role/update \"]}"); } }else{ return ResponseEntity.status(response.getHttpstatus()).body(response.getResponse()); } }  ResponseEntity<String> createIAMRole(AWSIAMRole awsiamRole, String token, UserDetails userDetails); ResponseEntity<String> updateIAMRole(String token, AWSIAMRole awsiamRole); ResponseEntity<String> fetchIAMRole(String token, String role); ResponseEntity<String> listIAMRoles(String token); ResponseEntity<String> deleteIAMRole(String token, String role); Response configureAWSIAMRole(String roleName,String policies,String token );  }
@Test public void test_createIAMRole_successfully() { String token = "5PDrOhsy4ig8L3EpsJZSLAMg"; AWSIAMRole awsiamRole = new AWSIAMRole(); awsiamRole.setAuth_type("iam"); String[] arns = {"arn:aws:iam::123456789012:user/tst"}; awsiamRole.setBound_iam_principal_arn(arns); String[] policies = {"default"}; awsiamRole.setPolicies(policies); awsiamRole.setResolve_aws_unique_ids(true); awsiamRole.setRole("string"); String jsonStr = "{\"auth_type\": \"iam\",\"bound_iam_principal_arn\":" + " [\"arn:aws:iam::123456789012:user/tst\"],\"policies\": " + "[\"string\"],\"resolve_aws_unique_ids\": true,\"role\": \"string\"}"; Response response = getMockResponse(HttpStatus.NO_CONTENT, true, ""); ResponseEntity<String> responseEntityExpected = ResponseEntity.status(HttpStatus.OK).body("{\"messages\":[\"AWS IAM Role created successfully \"]}"); when(reqProcessor.process("/auth/aws/iam/role/create",jsonStr, token)).thenReturn(response); when(JSONUtil.getJSON(awsiamRole)).thenReturn(jsonStr); UserDetails userDetails = getMockUser(true); when(ControllerUtil.createMetadata(Mockito.any(), eq(token))).thenReturn(true); try { when(ControllerUtil.areAWSIAMRoleInputsValid(awsiamRole)).thenReturn(true); ResponseEntity<String> responseEntity = awsIamAuthService.createIAMRole(awsiamRole, token, userDetails); assertEquals(HttpStatus.OK, responseEntity.getStatusCode()); assertEquals(responseEntityExpected, responseEntity); } catch (TVaultValidationException e) { e.printStackTrace(); } }
@Test public void test_createIAMRole_failure_revert() { String token = "5PDrOhsy4ig8L3EpsJZSLAMg"; AWSIAMRole awsiamRole = new AWSIAMRole(); awsiamRole.setAuth_type("iam"); String[] arns = {"arn:aws:iam::123456789012:user/tst"}; awsiamRole.setBound_iam_principal_arn(arns); String[] policies = {"default"}; awsiamRole.setPolicies(policies); awsiamRole.setResolve_aws_unique_ids(true); awsiamRole.setRole("string"); String jsonStr = "{\"auth_type\": \"iam\",\"bound_iam_principal_arn\":" + " [\"arn:aws:iam::123456789012:user/tst\"],\"policies\": " + "[\"string\"],\"resolve_aws_unique_ids\": true,\"role\": \"string\"}"; Response response = getMockResponse(HttpStatus.NO_CONTENT, true, ""); ResponseEntity<String> responseEntityExpected = ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"errors\":[\"AWS IAM role creation failed.\"]}"); when(reqProcessor.process("/auth/aws/iam/role/create",jsonStr, token)).thenReturn(response); when(JSONUtil.getJSON(awsiamRole)).thenReturn(jsonStr); UserDetails userDetails = getMockUser(true); when(ControllerUtil.createMetadata(Mockito.any(), eq(token))).thenReturn(false); when(reqProcessor.process("/auth/aws/iam/roles/delete","{\"role\":\""+awsiamRole.getRole()+"\"}",token)).thenReturn(response); try { when(ControllerUtil.areAWSIAMRoleInputsValid(awsiamRole)).thenReturn(true); ResponseEntity<String> responseEntity = awsIamAuthService.createIAMRole(awsiamRole, token, userDetails); assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode()); assertEquals(responseEntityExpected, responseEntity); } catch (TVaultValidationException e) { e.printStackTrace(); } }
@Test public void test_createIAMRole_revert_failure() { String token = "5PDrOhsy4ig8L3EpsJZSLAMg"; AWSIAMRole awsiamRole = new AWSIAMRole(); awsiamRole.setAuth_type("iam"); String[] arns = {"arn:aws:iam::123456789012:user/tst"}; awsiamRole.setBound_iam_principal_arn(arns); String[] policies = {"default"}; awsiamRole.setPolicies(policies); awsiamRole.setResolve_aws_unique_ids(true); awsiamRole.setRole("string"); String jsonStr = "{\"auth_type\": \"iam\",\"bound_iam_principal_arn\":" + " [\"arn:aws:iam::123456789012:user/tst\"],\"policies\": " + "[\"string\"],\"resolve_aws_unique_ids\": true,\"role\": \"string\"}"; Response response = getMockResponse(HttpStatus.NO_CONTENT, true, ""); ResponseEntity<String> responseEntityExpected = ResponseEntity.status(HttpStatus.OK).body("{\"messages\":[\"AWS IAM role created however metadata update failed. Please try with AWS role/update \"]}"); when(reqProcessor.process("/auth/aws/iam/role/create",jsonStr, token)).thenReturn(response); when(JSONUtil.getJSON(awsiamRole)).thenReturn(jsonStr); UserDetails userDetails = getMockUser(true); when(ControllerUtil.createMetadata(Mockito.any(), eq(token))).thenReturn(false); Response response404 = getMockResponse(HttpStatus.NOT_FOUND, true, ""); when(reqProcessor.process("/auth/aws/iam/roles/delete","{\"role\":\""+awsiamRole.getRole()+"\"}",token)).thenReturn(response404); try { when(ControllerUtil.areAWSIAMRoleInputsValid(awsiamRole)).thenReturn(true); ResponseEntity<String> responseEntity = awsIamAuthService.createIAMRole(awsiamRole, token, userDetails); assertEquals(HttpStatus.OK, responseEntity.getStatusCode()); assertEquals(responseEntityExpected, responseEntity); } catch (TVaultValidationException e) { e.printStackTrace(); } }
@Test(expected = TVaultValidationException.class) public void test_createIAMRole_failure_400() throws TVaultValidationException{ String token = "5PDrOhsy4ig8L3EpsJZSLAMg"; AWSIAMRole awsiamRole = new AWSIAMRole(); awsiamRole.setAuth_type("iam"); String[] arns = {"arn:aws:iam::123456789012:user/tst"}; awsiamRole.setBound_iam_principal_arn(arns); String[] policies = {"default"}; awsiamRole.setPolicies(policies); awsiamRole.setResolve_aws_unique_ids(true); awsiamRole.setRole("string"); when(ControllerUtil.areAWSIAMRoleInputsValid(awsiamRole)).thenReturn(false); UserDetails userDetails = getMockUser(true); ResponseEntity<String> responseEntity = awsIamAuthService.createIAMRole(awsiamRole, token, userDetails); }