VaultAuthService { private ResponseEntity<String> login(String jsonStr) { Response response = null; if ("ldap".equals(vaultAuthMethod)) { response = reqProcessor.process("/auth/ldap/login",jsonStr,""); } else { response = reqProcessor.process("/auth/userpass/login",jsonStr,""); } if(HttpStatus.OK.equals(response.getHttpstatus())){ Map<String, Object> responseMap = null; try { responseMap = new ObjectMapper().readValue(response.getResponse(), new TypeReference<Map<String, Object>>(){}); } catch (IOException e) { log.error(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, "Login"). put(LogMessage.MESSAGE, String.format("Login check for duplicate safe permission failed")). put(LogMessage.RESPONSE, "Invalid login response"). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); } if(responseMap!=null && responseMap.get("access")!=null) { Map<String,Object> access = (Map<String,Object>)responseMap.get("access"); access = ControllerUtil.filterDuplicateSafePermissions(access); access = ControllerUtil.filterDuplicateSvcaccPermissions(access); access = filterDuplicateCertPermissions(access); responseMap.put("access", access); Map<String,Object> feature = new HashMap<>(); feature.put(TVaultConstants.SELFSERVICE, isSSEnabled); feature.put(TVaultConstants.ADAUTOROTATION, isAdPswdRotationEnabled); responseMap.put("feature", feature); response.setResponse(JSONUtil.getJSON(responseMap)); } return ResponseEntity.status(response.getHttpstatus()).body(response.getResponse()); }else{ if (HttpStatus.BAD_REQUEST.equals(response.getHttpstatus())) { return ResponseEntity.status(response.getHttpstatus()).body("{\"errors\": [\"User Authentication failed\", \"Invalid username or password. Please retry again after correcting username or password.\"]}"); } else if (HttpStatus.INTERNAL_SERVER_ERROR.equals(response.getHttpstatus())) { return ResponseEntity.status(response.getHttpstatus()).body("{\"errors\": [\"User Authentication failed\", \"This may be due to vault services are down or vault services are not reachable\"]}"); } return ResponseEntity.status(response.getHttpstatus()).body("{\"errors\":[\"Username Authentication Failed.\"]}"); } }  Map<String,Object> filterDuplicateSafePermissions(Map<String,Object> access); Map<String,Object> filterDuplicateSvcaccPermissions(Map<String,Object> access); ResponseEntity<String> login(UserLogin user); ResponseEntity<String> renew(String token); ResponseEntity<String> lookup( String token); ResponseEntity<String> revoke(String token);  }
@Test public void test_login_successfully_for_internalcert() { String jsonStr = "{ \"username\": \"safeadmin\", \"password\": \"safeadmin\"}"; UserLogin userLogin = new UserLogin("safeadmin", "safeadmin"); String responseJson = "{ \"client_token\": \"8766fdhjSAtH2a4MdvMyzWid\",\"admin\": \"yes\",\"access\": {\"users\":[{\"safe1\":\"read\"}], \"svcacct\":[{\"svc1\":\"read\"}], \"cert\":[{\"cert1\":\"read\"}]},\"policies\": [\"default\",\"safeadmin\"],\"lease_duration\": 1800000, \"feature\": {\"adpwdrotation\": \"true\", \"serviceaccount\":\"true\"}}"; Response response = getMockResponse(HttpStatus.OK, true, responseJson); Map<String, Object> responseMap = null; try { responseMap = new ObjectMapper().readValue(response.getResponse(), new TypeReference<Map<String, Object>>(){}); } catch (IOException e) { e.printStackTrace(); } ResponseEntity<String> responseEntityExpected = ResponseEntity.status(HttpStatus.OK).body(responseJson); when(JSONUtil.getJSON(Mockito.any())).thenReturn(responseJson); when(JSONUtil.getJSON(userLogin)).thenReturn(jsonStr); when(reqProcessor.process("/auth/userpass/login",jsonStr,"")).thenReturn(response); ResponseEntity<String> responseEntity = vaultAuthService.login(userLogin); assertEquals(HttpStatus.OK, responseEntity.getStatusCode()); assertEquals(responseEntityExpected, responseEntity); }
@Test public void test_login_successfully_for_externalcert() { String jsonStr = "{ \"username\": \"safeadmin\", \"password\": \"safeadmin\"}"; UserLogin userLogin = new UserLogin("safeadmin", "safeadmin"); String responseJson = "{ \"client_token\": \"8766fdhjSAtH2a4MdvMyzWid\",\"admin\": \"yes\",\"access\": {\"users\":[{\"safe1\":\"read\"}], \"svcacct\":[{\"svc1\":\"read\"}], \"externalcerts\":[{\"externalcerts1\":\"read\"}]}, \"policies\": [\"default\",\"safeadmin\"],\"lease_duration\": 1800000, \"feature\": {\"adpwdrotation\": \"true\", \"serviceaccount\":\"true\"}}"; Response response = getMockResponse(HttpStatus.OK, true, responseJson); Map<String, Object> responseMap = null; try { responseMap = new ObjectMapper().readValue(response.getResponse(), new TypeReference<Map<String, Object>>(){}); } catch (IOException e) { e.printStackTrace(); } ResponseEntity<String> responseEntityExpected = ResponseEntity.status(HttpStatus.OK).body(responseJson); when(JSONUtil.getJSON(Mockito.any())).thenReturn(responseJson); when(JSONUtil.getJSON(userLogin)).thenReturn(jsonStr); when(reqProcessor.process("/auth/userpass/login",jsonStr,"")).thenReturn(response); ResponseEntity<String> responseEntity = vaultAuthService.login(userLogin); assertEquals(HttpStatus.OK, responseEntity.getStatusCode()); assertEquals(responseEntityExpected, responseEntity); }
@Test public void test_login_failure_400() { String jsonStr = "{ \"username\": \"safeadmin\", \"password\": \"safeadmin\"}"; UserLogin userLogin = new UserLogin("safeadmin", "safeadmin"); String responseJson = "{\"errors\": [\"User Authentication failed\", \"Invalid username or password. Please retry again after correcting username or password.\"]}"; Response response = getMockResponse(HttpStatus.BAD_REQUEST, true, responseJson); ResponseEntity<String> responseEntityExpected = ResponseEntity.status(HttpStatus.BAD_REQUEST).body(responseJson); when(JSONUtil.getJSON(Mockito.any(UserLogin.class))).thenReturn(jsonStr); when(reqProcessor.process("/auth/userpass/login",jsonStr,"")).thenReturn(response); ResponseEntity<String> responseEntity = vaultAuthService.login(userLogin); assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode()); assertEquals(responseEntityExpected, responseEntity); }
@Test public void test_login_failure_500() { String jsonStr = "{ \"username\": \"safeadmin\", \"password\": \"safeadmin\"}"; UserLogin userLogin = new UserLogin("safeadmin", "safeadmin"); String responseJson = "{\"errors\": [\"User Authentication failed\", \"This may be due to vault services are down or vault services are not reachable\"]}"; Response response = getMockResponse(HttpStatus.INTERNAL_SERVER_ERROR, true, responseJson); ResponseEntity<String> responseEntityExpected = ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(responseJson); when(JSONUtil.getJSON(Mockito.any(UserLogin.class))).thenReturn(jsonStr); when(reqProcessor.process("/auth/userpass/login",jsonStr,"")).thenReturn(response); ResponseEntity<String> responseEntity = vaultAuthService.login(userLogin); assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode()); assertEquals(responseEntityExpected, responseEntity); }