@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); }
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 { 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 { 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(); }
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); }
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);  }