TokenUtils { public String getSelfServiceToken() { log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, "getSelfServiceToken"). put(LogMessage.MESSAGE, String.format ("Trying to generate SelfServiceToken")). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); if (TVaultConstants.APPROLE.equals(selfServiceTokenGenerator)) { return getSelfServiceTokenWithAppRole(); } String selfServiceToken = null; String tvaultSelfServiceUsername = new String(Base64.getDecoder().decode(selfserviceUsername)); String tvaultSelfServicePassword = new String(Base64.getDecoder().decode(selfservicePassword)); UserLogin userLogin = new UserLogin(tvaultSelfServiceUsername, tvaultSelfServicePassword); String jsonStr = JSONUtil.getJSON(userLogin); Response response = null; if (TVaultConstants.LDAP.equals(vaultAuthMethod)) { response = reqProcessor.process("/auth/ldap/login",jsonStr,""); } else { response = reqProcessor.process("/auth/userpass/login",jsonStr,""); } if(HttpStatus.OK.equals(response.getHttpstatus())){ log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, "getSelfServiceToken"). put(LogMessage.MESSAGE, String.format ("SelfService token successfully created")). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); String res = response.getResponse(); if (!StringUtils.isEmpty(res)) { LinkedHashMap<String, Object> responseMap = (LinkedHashMap<String, Object>)ControllerUtil.parseJson(response.getResponse()); selfServiceToken = (String) responseMap.get("client_token"); } }else{ log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, "getSelfServiceToken"). put(LogMessage.MESSAGE, String.format ("SelfService token failed")). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); } return selfServiceToken; } TokenUtils(); String getSelfServiceToken(); String getSelfServiceTokenWithAppRole(); void revokePowerToken(String token);  }
@Test public void test_getSelfServiceToken_success() throws Exception { String jsonStr = "{\"username\":\"testadmin\",\"password\":\"testadmin\"}"; ReflectionTestUtils.setField(tokenUtils, "selfserviceUsername", "dGVzdGFkbWlu"); ReflectionTestUtils.setField(tokenUtils, "selfservicePassword", "dGVzdGFkbWlu"); ReflectionTestUtils.setField(tokenUtils, "vaultAuthMethod", "userpass"); when(JSONUtil.getJSON(Mockito.any())).thenReturn(jsonStr); Response response = getMockResponse(HttpStatus.OK, true, "{\"client_token\":\"7QPMPIGiyDFlJkrK3jFykUqa\",\"admin\":\"yes\",\"access\":{},\"policies\":[\"default\",\"testadmin\"],\"lease_duration\":1800000}"); when(reqProcessor.process("/auth/userpass/login",jsonStr,"")).thenReturn(response); String token = tokenUtils.getSelfServiceToken(); assertEquals("7QPMPIGiyDFlJkrK3jFykUqa", token); }
@Test public void test_getSelfServiceToken_success_ldap() throws Exception { String jsonStr = "{\"username\":\"testadmin\",\"password\":\"testadmin\"}"; ReflectionTestUtils.setField(tokenUtils, "selfserviceUsername", "dGVzdGFkbWlu"); ReflectionTestUtils.setField(tokenUtils, "selfservicePassword", "dGVzdGFkbWlu"); ReflectionTestUtils.setField(tokenUtils, "vaultAuthMethod", "ldap"); when(JSONUtil.getJSON(Mockito.any())).thenReturn(jsonStr); Response response = getMockResponse(HttpStatus.OK, true, "{\"client_token\":\"7QPMPIGiyDFlJkrK3jFykUqa\",\"admin\":\"yes\",\"access\":{},\"policies\":[\"default\",\"testadmin\"],\"lease_duration\":1800000}"); when(reqProcessor.process("/auth/ldap/login",jsonStr,"")).thenReturn(response); String token = tokenUtils.getSelfServiceToken(); assertEquals("7QPMPIGiyDFlJkrK3jFykUqa", token); }
@Test public void test_getSelfServiceToken_failure() throws Exception { String jsonStr = "{\"username\":\"testadmin\",\"password\":\"testadmin\"}"; ReflectionTestUtils.setField(tokenUtils, "selfserviceUsername", "dGVzdGFkbWlu"); ReflectionTestUtils.setField(tokenUtils, "selfservicePassword", "dGVzdGFkbWlu"); ReflectionTestUtils.setField(tokenUtils, "vaultAuthMethod", "userpass"); when(JSONUtil.getJSON(Mockito.any())).thenReturn(jsonStr); Response response = getMockResponse(HttpStatus.NOT_FOUND, true, ""); when(reqProcessor.process("/auth/userpass/login",jsonStr,"")).thenReturn(response); String token = tokenUtils.getSelfServiceToken(); assertNull(token); }
@Test public void test_getSelfServiceToken_success_approle() throws Exception { String jsonStr = "{\"roleId\":\"a736d57a-ac97-a08d-53ab-eb5ab16d03b5\",\"secretId\":\"10884921-79d6-fde1-3f28-56af75d83616\"}"; ReflectionTestUtils.setField(tokenUtils, "selfserviceUsername", "YTczNmQ1N2EtYWM5Ny1hMDhkLTUzYWItZWI1YWIxNmQwM2I1"); ReflectionTestUtils.setField(tokenUtils, "selfservicePassword", "MTA4ODQ5MjEtNzlkNi1mZGUxLTNmMjgtNTZhZjc1ZDgzNjE2"); ReflectionTestUtils.setField(tokenUtils, "vaultAuthMethod", "userpass"); ReflectionTestUtils.setField(tokenUtils, "selfServiceTokenGenerator", "approle"); when(JSONUtil.getJSON(Mockito.any(AppRoleIdSecretId.class))).thenReturn(jsonStr); Response response = getMockResponse(HttpStatus.OK, true, "{\"auth\":{\"client_token\":\"6WcC0r5Nw9z0RILHEJnn0OCB\",\"accessor\":\"2xmlSRCSxGb0AlopA1JfQyuY\",\"policies\":[\"default\",\"selfservicesupport\"],\"token_policies\":[\"default\",\"selfservicesupport\"],\"metadata\":{\"role_name\":\"selfservicesupportrole\"},\"lease_duration\":2764800,\"renewable\":true,\"entity_id\":\"eb74b6b1-b9a0-fd59-ff33-5813be3fdbbf\"},\"data\":null,\"lease_duration\":0,\"lease_id\":\"\"}"); when(reqProcessor.process(eq("/auth/approle/login"),Mockito.any(),eq(""))).thenReturn(response); String token = tokenUtils.getSelfServiceToken(); assertEquals("6WcC0r5Nw9z0RILHEJnn0OCB", token); }
@Test public void test_getSelfServiceToken_failure_approle() throws Exception { String jsonStr = "{\"roleId\":\"a736d57a-ac97-a08d-53ab-eb5ab16d03b5\",\"secretId\":\"10884921-79d6-fde1-3f28-56af75d83616\"}"; ReflectionTestUtils.setField(tokenUtils, "selfserviceUsername", "YTczNmQ1N2EtYWM5Ny1hMDhkLTUzYWItZWI1YWIxNmQwM2I1"); ReflectionTestUtils.setField(tokenUtils, "selfservicePassword", "MTA4ODQ5MjEtNzlkNi1mZGUxLTNmMjgtNTZhZjc1ZDgzNjE2"); ReflectionTestUtils.setField(tokenUtils, "vaultAuthMethod", "userpass"); ReflectionTestUtils.setField(tokenUtils, "selfServiceTokenGenerator", "approle"); when(JSONUtil.getJSON(Mockito.any())).thenReturn(jsonStr); Response response = getMockResponse(HttpStatus.NOT_FOUND, true, ""); when(reqProcessor.process("/auth/approle/login",jsonStr,"")).thenReturn(response); String token = tokenUtils.getSelfServiceToken(); assertNull(token); }