PolicyUtils { public String[] getCurrentPolicies(String token, String username, UserDetails userDetails) { Response userResponse = new Response(); String[] policies = {}; if (TVaultConstants.USERPASS.equals(vaultAuthMethod)) { userResponse = ControllerUtil.getReqProcessor().process("/auth/userpass/read","{\"username\":\""+username+"\"}",token); } else if (TVaultConstants.LDAP.equals(vaultAuthMethod)) { userResponse = ControllerUtil.getReqProcessor().process("/auth/ldap/users","{\"username\":\""+username+"\"}",token); } else if(TVaultConstants.OIDC.equals(vaultAuthMethod)) { ResponseEntity<OIDCEntityResponse> responseEntity = oidcUtil.oidcFetchEntityDetails(token, username, userDetails); userResponse.setHttpstatus(responseEntity.getStatusCode()); if (HttpStatus.OK.equals(responseEntity.getStatusCode())) { policies = responseEntity.getBody().getPolicies().stream().toArray(String[] :: new); } } if(HttpStatus.OK.equals(userResponse.getHttpstatus())){ String responseJson = userResponse.getResponse(); try { if(!TVaultConstants.OIDC.equals(vaultAuthMethod)) { ObjectMapper objMapper = new ObjectMapper(); String policiesStr = ControllerUtil.getPoliciesAsStringFromJson(objMapper, responseJson); policies = policiesStr.split(","); } } catch (IOException e) { log.error(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, "getPolicies"). put(LogMessage.MESSAGE, "Error while trying to list of policies for the user"). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); } } return policies; } PolicyUtils(); ArrayList<String> getPoliciesTobeCheked(String safeType, String safeName); ArrayList<String> getAdminPolicies(); String[] getCurrentPolicies(String token, String username, UserDetails userDetails); ArrayList<String> getCertPoliciesTobeCheked(String certName);  }
@Test public void test_getCurrentPolicies_successfully() throws IOException { String token = "5PDrOhsy4ig8L3EpsJZSLAMg"; ReflectionTestUtils.setField(policyUtils, "vaultAuthMethod", "userpass"); String[] expectedPolicies = {"s_shared_mysafe01", "s_shared_mysafe02"}; Response response = getMockResponse(HttpStatus.OK, true, "{\"data\":{\"policies\":\"s_shared_mysafe01,s_shared_mysafe02\"}}"); when(ControllerUtil.getReqProcessor().process("/auth/userpass/read","{\"username\":\"normaluser\"}",token)).thenReturn(response); when(ControllerUtil.getPoliciesAsStringFromJson(Mockito.any(), eq("{\"data\":{\"policies\":\"s_shared_mysafe01,s_shared_mysafe02\"}}"))).thenReturn("s_shared_mysafe01,s_shared_mysafe02"); String[] policies = policyUtils.getCurrentPolicies(token, "normaluser", getMockUser(true)); assertEquals(expectedPolicies, policies); }
@Test public void test_getCurrentPolicies_successfully_ldap() throws IOException { String token = "5PDrOhsy4ig8L3EpsJZSLAMg"; ReflectionTestUtils.setField(policyUtils, "vaultAuthMethod", "ldap"); String[] expectedPolicies = {"s_shared_mysafe01", "s_shared_mysafe02"}; Response response = getMockResponse(HttpStatus.OK, true, "{\"data\":{\"policies\":\"s_shared_mysafe01,s_shared_mysafe02\"}}"); when(ControllerUtil.getReqProcessor().process("/auth/ldap/users","{\"username\":\"normaluser\"}",token)).thenReturn(response); when(ControllerUtil.getPoliciesAsStringFromJson(Mockito.any(), eq("{\"data\":{\"policies\":\"s_shared_mysafe01,s_shared_mysafe02\"}}"))).thenReturn("s_shared_mysafe01,s_shared_mysafe02"); String[] policies = policyUtils.getCurrentPolicies(token, "normaluser", getMockUser(true)); assertEquals(expectedPolicies, policies); }
@Test public void test_getCurrentPolicies_failure() throws IOException { String token = "5PDrOhsy4ig8L3EpsJZSLAMg"; ReflectionTestUtils.setField(policyUtils, "vaultAuthMethod", "userpass"); Response response = getMockResponse(HttpStatus.OK, true, "{\"data\":{\"policies\":\"s_shared_mysafe01,s_shared_mysafe02\"}}"); when(ControllerUtil.getReqProcessor().process("/auth/userpass/read","{\"username\":\"normaluser\"}",token)).thenReturn(response); when(ControllerUtil.getPoliciesAsStringFromJson(Mockito.any(), eq("{\"data\":{\"policies\":\"s_shared_mysafe01,s_shared_mysafe02\"}}"))).thenThrow(IOException.class); String[] policies = policyUtils.getCurrentPolicies(token, "normaluser", getMockUser(true)); String[] expectedPolicies = {}; assertEquals(expectedPolicies, policies); }