@Test public void test_getSelfServiceTokenWithAppRole_sscred_success() throws Exception { String jsonStr = "{\"role_id\":\"testroleid\",\"secret_id\":\"testsecretid\"}"; ReflectionTestUtils.setField(tokenUtils, "selfserviceUsername", "dGVzdGFkbWlu"); ReflectionTestUtils.setField(tokenUtils, "selfservicePassword", "dGVzdGFkbWlu"); ReflectionTestUtils.setField(tokenUtils, "vaultAuthMethod", "userpass"); SSCred sscred = new SSCred(); sscred.setUsername("dGVzdGFkbWlu"); sscred.setPassword("dGVzdGFkbWlu"); ReflectionTestUtils.setField(ControllerUtil.class,"sscred", sscred); ReflectionTestUtils.setField(ControllerUtil.class,"ssUsername", sscred.getUsername()); ReflectionTestUtils.setField(ControllerUtil.class,"ssPassword", sscred.getPassword()); when(JSONUtil.getJSON(Mockito.any())).thenReturn(jsonStr); Response response = getMockResponse(HttpStatus.OK, true, "{\n" + " \"auth\": {\n" + " \"client_token\": \"s.6tdd9QSp50YwJwjXViRXGnMQT4\",\n" + " \"accessor\": \"7M3bp7PTaOHeB4u2J9zVfE9g\",\n" + " \"policies\": [\n" + " \"default\",\n" + " \"string\"\n" + " ],\n" + " \"token_policies\": [\n" + " \"default\",\n" + " \"string\"\n" + " ],\n" + " \"metadata\": {\n" + " \"role_name\": \"approle\"\n" + " },\n" + " \"lease_duration\": 2764800,\n" + " \"renewable\": true,\n" + " \"entity_id\": \"5e191de5-7b96-b9cc-71d5-d254b636308b\",\n" + " \"token_type\": \"service\"\n" + " },\n" + " \"data\": null,\n" + " \"lease_duration\": 0,\n" + " \"lease_id\": \"\"\n" + "}"); when(reqProcessor.process("/auth/approle/login",jsonStr,"")).thenReturn(response); String token = tokenUtils.getSelfServiceTokenWithAppRole(); assertEquals("s.6tdd9QSp50YwJwjXViRXGnMQT4", token); }
public String getSelfServiceTokenWithAppRole() { log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, "getSelfServiceTokenWithAppRole"). put(LogMessage.MESSAGE, String.format ("Trying to generate SelfServiceTokenWithAppRole")). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); String selfServiceToken = null; String role_id = new String(Base64.getDecoder().decode(selfserviceUsername)); String secret_id = new String(Base64.getDecoder().decode(selfservicePassword)); if (ControllerUtil.getSscred() != null) { role_id = new String(Base64.getDecoder().decode(ControllerUtil.getSsUsername())); secret_id = new String(Base64.getDecoder().decode(ControllerUtil.getSsPassword())); } AppRoleIdSecretId approleLogin = new AppRoleIdSecretId(); approleLogin.setRole_id(role_id); approleLogin.setSecret_id(secret_id); String jsonStr = JSONUtil.getJSON(approleLogin); Response response = reqProcessor.process("/auth/approle/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, "getSelfServiceTokenWithAppRole"). put(LogMessage.MESSAGE, String.format ("SelfService token successfully created using AppRole")). 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()); if (responseMap.get("auth") != null) { selfServiceToken = (String)((LinkedHashMap<String, Object>) responseMap.get("auth")).get("client_token"); } } }else{ log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, "getSelfServiceTokenWithAppRole"). put(LogMessage.MESSAGE, String.format ("SelfService token failed")). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); } return selfServiceToken; }
TokenUtils { public String getSelfServiceTokenWithAppRole() { log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, "getSelfServiceTokenWithAppRole"). put(LogMessage.MESSAGE, String.format ("Trying to generate SelfServiceTokenWithAppRole")). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); String selfServiceToken = null; String role_id = new String(Base64.getDecoder().decode(selfserviceUsername)); String secret_id = new String(Base64.getDecoder().decode(selfservicePassword)); if (ControllerUtil.getSscred() != null) { role_id = new String(Base64.getDecoder().decode(ControllerUtil.getSsUsername())); secret_id = new String(Base64.getDecoder().decode(ControllerUtil.getSsPassword())); } AppRoleIdSecretId approleLogin = new AppRoleIdSecretId(); approleLogin.setRole_id(role_id); approleLogin.setSecret_id(secret_id); String jsonStr = JSONUtil.getJSON(approleLogin); Response response = reqProcessor.process("/auth/approle/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, "getSelfServiceTokenWithAppRole"). put(LogMessage.MESSAGE, String.format ("SelfService token successfully created using AppRole")). 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()); if (responseMap.get("auth") != null) { selfServiceToken = (String)((LinkedHashMap<String, Object>) responseMap.get("auth")).get("client_token"); } } }else{ log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, "getSelfServiceTokenWithAppRole"). put(LogMessage.MESSAGE, String.format ("SelfService token failed")). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); } return selfServiceToken; } }
TokenUtils { public String getSelfServiceTokenWithAppRole() { log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, "getSelfServiceTokenWithAppRole"). put(LogMessage.MESSAGE, String.format ("Trying to generate SelfServiceTokenWithAppRole")). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); String selfServiceToken = null; String role_id = new String(Base64.getDecoder().decode(selfserviceUsername)); String secret_id = new String(Base64.getDecoder().decode(selfservicePassword)); if (ControllerUtil.getSscred() != null) { role_id = new String(Base64.getDecoder().decode(ControllerUtil.getSsUsername())); secret_id = new String(Base64.getDecoder().decode(ControllerUtil.getSsPassword())); } AppRoleIdSecretId approleLogin = new AppRoleIdSecretId(); approleLogin.setRole_id(role_id); approleLogin.setSecret_id(secret_id); String jsonStr = JSONUtil.getJSON(approleLogin); Response response = reqProcessor.process("/auth/approle/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, "getSelfServiceTokenWithAppRole"). put(LogMessage.MESSAGE, String.format ("SelfService token successfully created using AppRole")). 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()); if (responseMap.get("auth") != null) { selfServiceToken = (String)((LinkedHashMap<String, Object>) responseMap.get("auth")).get("client_token"); } } }else{ log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, "getSelfServiceTokenWithAppRole"). put(LogMessage.MESSAGE, String.format ("SelfService token failed")). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); } return selfServiceToken; } TokenUtils(); }
TokenUtils { public String getSelfServiceTokenWithAppRole() { log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, "getSelfServiceTokenWithAppRole"). put(LogMessage.MESSAGE, String.format ("Trying to generate SelfServiceTokenWithAppRole")). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); String selfServiceToken = null; String role_id = new String(Base64.getDecoder().decode(selfserviceUsername)); String secret_id = new String(Base64.getDecoder().decode(selfservicePassword)); if (ControllerUtil.getSscred() != null) { role_id = new String(Base64.getDecoder().decode(ControllerUtil.getSsUsername())); secret_id = new String(Base64.getDecoder().decode(ControllerUtil.getSsPassword())); } AppRoleIdSecretId approleLogin = new AppRoleIdSecretId(); approleLogin.setRole_id(role_id); approleLogin.setSecret_id(secret_id); String jsonStr = JSONUtil.getJSON(approleLogin); Response response = reqProcessor.process("/auth/approle/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, "getSelfServiceTokenWithAppRole"). put(LogMessage.MESSAGE, String.format ("SelfService token successfully created using AppRole")). 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()); if (responseMap.get("auth") != null) { selfServiceToken = (String)((LinkedHashMap<String, Object>) responseMap.get("auth")).get("client_token"); } } }else{ log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, "getSelfServiceTokenWithAppRole"). 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 getSelfServiceTokenWithAppRole() { log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, "getSelfServiceTokenWithAppRole"). put(LogMessage.MESSAGE, String.format ("Trying to generate SelfServiceTokenWithAppRole")). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); String selfServiceToken = null; String role_id = new String(Base64.getDecoder().decode(selfserviceUsername)); String secret_id = new String(Base64.getDecoder().decode(selfservicePassword)); if (ControllerUtil.getSscred() != null) { role_id = new String(Base64.getDecoder().decode(ControllerUtil.getSsUsername())); secret_id = new String(Base64.getDecoder().decode(ControllerUtil.getSsPassword())); } AppRoleIdSecretId approleLogin = new AppRoleIdSecretId(); approleLogin.setRole_id(role_id); approleLogin.setSecret_id(secret_id); String jsonStr = JSONUtil.getJSON(approleLogin); Response response = reqProcessor.process("/auth/approle/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, "getSelfServiceTokenWithAppRole"). put(LogMessage.MESSAGE, String.format ("SelfService token successfully created using AppRole")). 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()); if (responseMap.get("auth") != null) { selfServiceToken = (String)((LinkedHashMap<String, Object>) responseMap.get("auth")).get("client_token"); } } }else{ log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, "getSelfServiceTokenWithAppRole"). 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);  }