AWSIAMAuthService { public ResponseEntity<String> updateIAMRole(String token, AWSIAMRole awsiamRole) throws TVaultValidationException{ if (!ControllerUtil.areAWSIAMRoleInputsValid(awsiamRole)) { throw new TVaultValidationException("Invalid inputs for the given aws login type"); } String jsonStr = JSONUtil.getJSON(awsiamRole); ObjectMapper objMapper = new ObjectMapper(); String currentPolicies = ""; String latestPolicies = ""; String roleName = "" ; try { JsonNode root = objMapper.readTree(jsonStr); roleName = root.get("role").asText(); if(root.get("policies") != null) latestPolicies = root.get("policies").asText(); } catch (IOException e) { logger.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, "Update IAM role"). put(LogMessage.MESSAGE, String.format("Failed to extract role/policies from json string [%s]", jsonStr)). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); } Response awsResponse = reqProcessor.process("/auth/aws/iam/roles","{\"role\":\""+roleName+"\"}",token); String responseJson=""; if(HttpStatus.OK.equals(awsResponse.getHttpstatus())){ responseJson = awsResponse.getResponse(); try { Map<String,Object> responseMap; responseMap = objMapper.readValue(responseJson, new TypeReference<Map<String, Object>>(){}); @SuppressWarnings("unchecked") List<String> policies = (List<String>) responseMap.get("policies"); currentPolicies = policies.stream().collect(Collectors.joining(",")).toString(); } catch (IOException e) { logger.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, "Update IAM role"). put(LogMessage.MESSAGE, "Failed to extract from IAM read response"). put(LogMessage.RESPONSE, awsResponse.getResponse()). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); } }else{ return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("{\"messages\":[\"Update failed . AWS Role does not exist \"]}"); } Response response = reqProcessor.process("/auth/aws/roles/delete",jsonStr,token); if(response.getHttpstatus().equals(HttpStatus.NO_CONTENT)){ response = reqProcessor.process("/auth/aws/iam/roles/update",jsonStr,token); if(response.getHttpstatus().equals(HttpStatus.NO_CONTENT)){ response = ControllerUtil.updateMetaDataOnConfigChanges(roleName, "aws-roles", currentPolicies, latestPolicies, token); if(!HttpStatus.OK.equals(response.getHttpstatus())) return ResponseEntity.status(response.getHttpstatus()).body("{\"messages\":[\"AWS Role configured\",\""+response.getResponse()+"\"]}"); return ResponseEntity.status(HttpStatus.OK).body("{\"messages\":[\"AWS Role updated \"]}"); }else{ return ResponseEntity.status(response.getHttpstatus()).body(response.getResponse()); } }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_updateIAMRole_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\": \"mytestawsrole\"}"; Response response = getMockResponse(HttpStatus.NO_CONTENT, true, ""); Response updateResponse = getMockResponse(HttpStatus.OK, true, "{\"messages\":[\"AWS Role updated \"]}"); String jsonGetStr = "{\"bound_ami_id\": [\"ami-fce3c696\"],\"role_tag\": \"\",\"policies\": " + "[\"\\\"[prod\", \"dev\\\"]\" ],\"bound_iam_principal_arn\": [],\"bound_iam_role_arn\":" + "[ \"arn:aws:iam::8987887:role/test-role\"],\"max_ttl\": 0,\"disallow_reauthentication\": " + "false,\"allow_instance_migration\": false}"; Response getResponse = getMockResponse(HttpStatus.OK, true, jsonGetStr); ResponseEntity<String> responseEntityExpected = ResponseEntity.status(HttpStatus.OK).body("{\"messages\":[\"AWS Role updated \"]}"); when(JSONUtil.getJSON(awsiamRole)).thenReturn(jsonStr); when(reqProcessor.process("/auth/aws/iam/roles","{\"role\":\"mytestawsrole\"}",token)).thenReturn(getResponse); when(reqProcessor.process("/auth/aws/roles/delete",jsonStr,token)).thenReturn(response); when(reqProcessor.process("/auth/aws/iam/roles/update",jsonStr,token)).thenReturn(response); when(ControllerUtil.updateMetaDataOnConfigChanges(Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any())).thenReturn(updateResponse); try { when(ControllerUtil.areAWSIAMRoleInputsValid(awsiamRole)).thenReturn(true); ResponseEntity<String> responseEntity = awsIamAuthService.updateIAMRole(token, awsiamRole); assertEquals(HttpStatus.OK, responseEntity.getStatusCode()); assertEquals(responseEntityExpected, responseEntity); } catch (TVaultValidationException e) { e.printStackTrace(); } }
@Test public void test_updateIAMRole_failure_400() { 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\": \"mytestawsrole\"}"; Response getResponse = getMockResponse(HttpStatus.NOT_FOUND, false, ""); ResponseEntity<String> responseEntityExpected = ResponseEntity.status(HttpStatus.BAD_REQUEST).body("{\"messages\":[\"Update failed . AWS Role does not exist \"]}"); when(JSONUtil.getJSON(awsiamRole)).thenReturn(jsonStr); when(reqProcessor.process("/auth/aws/iam/roles","{\"role\":\"mytestawsrole\"}",token)).thenReturn(getResponse); try { when(ControllerUtil.areAWSIAMRoleInputsValid(awsiamRole)).thenReturn(true); ResponseEntity<String> responseEntity = awsIamAuthService.updateIAMRole(token, awsiamRole); assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode()); assertEquals(responseEntityExpected, responseEntity); } catch (TVaultValidationException e) { e.printStackTrace(); } }