AWSAuthService { public ResponseEntity<String> authenticate(AWSAuthType authType, AWSAuthLogin awsAuthLogin){ if (!ControllerUtil.areAwsLoginInputsValid(authType, awsAuthLogin)) { return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Invalid inputs for the given aws login type"); } if (AWSAuthType.EC2.equals(authType) ) { AWSLogin login = generateAWSEC2Login(awsAuthLogin); return authenticateEC2(login); } else if (AWSAuthType.IAM.equals(authType)) { AWSIAMLogin awsiamLogin = generateIAMLogin(awsAuthLogin); return authenticateIAM(awsiamLogin); } else { return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Invalid Authentication type. Authentication type has to be either ec2 or iam"); } }  ResponseEntity<String> authenticateEC2(AWSLogin login); ResponseEntity<String> createRole(String token, AWSLoginRole awsLoginRole, UserDetails userDetails); ResponseEntity<String> updateRole(String token, AWSLoginRole awsLoginRole); ResponseEntity<String> deleteRole(String token, String role, UserDetails userDetails); ResponseEntity<String> fetchRole(String token, String role); ResponseEntity<String> listRoles(String token); ResponseEntity<String> configureClient(AWSClientConfiguration awsClientConfiguration, String token); ResponseEntity<String> readClientConfiguration(String token); ResponseEntity<String> createSTSRole(AWSStsRole awsStsRole, String token); ResponseEntity<String> authenticateIAM(AWSIAMLogin awsiamLogin); ResponseEntity<String> authenticate(AWSAuthType authType, AWSAuthLogin awsAuthLogin); Response configureAWSRole(String roleName,String policies,String token );  }
@Test public void test_authenticate_AWS_EC2_successfully() { String jsonLoginStr = "{\n" + " \"role\": \"testawsrole\",\n" + " \"pkcs7\": \"MIIBjwYJKoZIhvcNAQcDoIIBgDCCAXwCAQAxggE4MIIBNAIBADCBnDCBlDELMAkGA1UEBhMCWkEx====\"\n" + "}"; AWSAuthLogin awsAuthLogin = new AWSIAMLogin(); awsAuthLogin.setIam_http_request_method("POST"); awsAuthLogin.setIam_request_body("{}"); awsAuthLogin.setIam_request_headers("{\"token\":\"4qJC0tWjMDIKjRDDmtcUAZBt\"}"); awsAuthLogin.setIam_request_url("http: awsAuthLogin.setRole("testawsrole"); awsAuthLogin.setPkcs7("MIIBjwYJKoZIhvcNAQcDoIIBgDCCAXwCAQAxggE4MIIBNAIBADCBnDCBlDELMAkGA1UEBhMCWkEx===="); Response response = getMockResponse(HttpStatus.NO_CONTENT, false, ""); ResponseEntity<String> responseEntityExpected = ResponseEntity.status(HttpStatus.NO_CONTENT).body("{} \t"); AWSLogin login = new AWSLogin(); login.setPkcs7(awsAuthLogin.getPkcs7()); login.setRole(awsAuthLogin.getRole()); when(JSONUtil.getJSON(Mockito.any())).thenReturn(jsonLoginStr); when(ControllerUtil.areAwsLoginInputsValid(AWSAuthType.EC2, awsAuthLogin)).thenReturn(true); when(reqProcessor.process(Mockito.any(),Mockito.any(),Mockito.any())).thenReturn(response); ResponseEntity<String> responseEntity = awsAuthService.authenticate(AWSAuthType.EC2, awsAuthLogin); assertEquals(HttpStatus.NO_CONTENT, responseEntity.getStatusCode()); assertEquals(responseEntityExpected, responseEntity); }
@Test public void test_authenticate_AWS_IAM_successfully() { String jsonLoginStr = "{\n" + " \"role\": \"testawsrole\",\n" + " \"pkcs7\": \"MIIBjwYJKoZIhvcNAQcDoIIBgDCCAXwCAQAxggE4MIIBNAIBADCBnDCBlDELMAkGA1UEBhMCWkEx====\"\n" + "}"; String jsonStr = "{\n" + " \"iam_http_request_method\": \"POST\",\n" + " \"iam_request_body\": \"{}\",\n" + " \"iam_request_headers\": \"{\"token\":\"4qJC0tWjMDIKjRDDmtcUAZBt\"}\",\n" + " \"iam_request_url\": \"http: " \"role\": \"testawsrole\",\n" + " \"pkcs7\": \"MIIBjwYJKoZIhvcNAQcDoIIBgDCCAXwCAQAxggE4MIIBNAIBADCBnDCBlDELMAkGA1UEBhMCWkEx====\"\n" + "}"; AWSAuthLogin awsAuthLogin = new AWSIAMLogin(); awsAuthLogin.setIam_http_request_method("POST"); awsAuthLogin.setIam_request_body("{}"); awsAuthLogin.setIam_request_headers("{\"token\":\"4qJC0tWjMDIKjRDDmtcUAZBt\"}"); awsAuthLogin.setIam_request_url("http: awsAuthLogin.setRole("testawsrole"); awsAuthLogin.setPkcs7("MIIBjwYJKoZIhvcNAQcDoIIBgDCCAXwCAQAxggE4MIIBNAIBADCBnDCBlDELMAkGA1UEBhMCWkEx===="); Response response = getMockResponse(HttpStatus.NO_CONTENT, false, ""); Response responseOk = getMockResponse(HttpStatus.OK, false, ""); ResponseEntity<String> responseEntityExpected = ResponseEntity.status(HttpStatus.OK).body("{} \t"); AWSLogin login = new AWSLogin(); login.setPkcs7(awsAuthLogin.getPkcs7()); login.setRole(awsAuthLogin.getRole()); when(JSONUtil.getJSON(Mockito.any())).thenReturn(jsonStr); when(reqProcessor.process("/auth/aws/iam/login",jsonStr,"")).thenReturn(responseOk); when(ControllerUtil.areAwsLoginInputsValid(AWSAuthType.IAM, awsAuthLogin)).thenReturn(true); ResponseEntity<String> responseEntity = awsAuthService.authenticate(AWSAuthType.IAM, awsAuthLogin); assertEquals(HttpStatus.OK, responseEntity.getStatusCode()); assertEquals(responseEntityExpected, responseEntity); }