From c8230949aa0b212748ba6a7c0a9957eb6fb02663 Mon Sep 17 00:00:00 2001 From: Steve Hu Date: Fri, 21 Jan 2022 13:00:48 -0500 Subject: [PATCH] fixes #1084 update the DefaultConfigLoader to get the values.yml from the config server --- .../com/networknt/security/JwtIssuerTest.java | 12 ++++++++++++ .../networknt/server/DefaultConfigLoader.java | 18 +++++++----------- 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/security/src/test/java/com/networknt/security/JwtIssuerTest.java b/security/src/test/java/com/networknt/security/JwtIssuerTest.java index 3ef4fab74c..7046121b16 100644 --- a/security/src/test/java/com/networknt/security/JwtIssuerTest.java +++ b/security/src/test/java/com/networknt/security/JwtIssuerTest.java @@ -229,6 +229,18 @@ public void sidecarReferenceBootstrap() throws Exception { System.out.println("***Reference Long lived Bootstrap token for config server and controller: " + jwt); } + /** + * This token is used to connect to the light-config-server with serviceId 0100 for testing with a service specific for a client. + * @throws Exception + */ + @Test + public void sidecarReferenceBootstrapWithServiceId() throws Exception { + JwtClaims claims = ClaimsUtil.getTestCcClaimsScopeService("f7d42348-c647-4efb-a52d-4c5787421e72", "A8E73740C0041C03D67C3A951AA1D7533C8F9F2FB57D7BA107210B9BC9E06DA2", "com.networknt.petstore-1.0.0"); + claims.setExpirationTimeMinutesInTheFuture(5256000); + String jwt = JwtIssuer.getJwt(claims); + System.out.println("***Reference Long lived Bootstrap token for config server and controller: " + jwt); + } + /** * This token is used to connect to the light-config-server with serviceId example-service for unit test populated configs. * @throws Exception diff --git a/server/src/main/java/com/networknt/server/DefaultConfigLoader.java b/server/src/main/java/com/networknt/server/DefaultConfigLoader.java index ae07279815..e7ce07dd12 100644 --- a/server/src/main/java/com/networknt/server/DefaultConfigLoader.java +++ b/server/src/main/java/com/networknt/server/DefaultConfigLoader.java @@ -24,6 +24,7 @@ import com.networknt.client.ClientConfig; import com.networknt.client.Http2Client; import com.networknt.config.Config; +import com.networknt.config.JsonMapper; import com.networknt.monad.Failure; import com.networknt.status.Status; import com.networknt.utility.StringUtils; @@ -147,8 +148,8 @@ public void init() { try { String configPath = getConfigServerPath(); - // This is the method in the future to load values.yml from the config server - // loadConfigs(configPath); + // This is the method to load values.yml from the config server + loadConfigs(configPath); loadFiles(configPath, CONFIG_SERVER_CERTS_CONTEXT_ROOT); @@ -186,10 +187,7 @@ private void loadConfigs(String configPath) { String configServerConfigsPath = CONFIG_SERVER_CONFIGS_CONTEXT_ROOT + configPath; //get service configs and put them in config cache Map serviceConfigs = getServiceConfigs(configServerConfigsPath); - - //set the environment value (the one used to fetch configs) in the serviceConfigs going into configCache - serviceConfigs.put(ENV_PROPERTY_KEY, lightEnv); - logger.debug("serviceConfigs received from Config Server: {}", serviceConfigs); + if(logger.isDebugEnabled()) logger.debug("serviceConfigs received from Config Server: ", JsonMapper.toJson(serviceConfigs)); // pass serviceConfigs through Config.yaml's load method so that it can decrypt any encrypted values DumperOptions options = new DumperOptions(); @@ -212,8 +210,7 @@ private void loadFiles(String configPath, String contextRoot) { String configServerFilesPath = contextRoot + configPath; //get service files and put them in config dir Map serviceFiles = getServiceConfigs(configServerFilesPath); - logger.debug("{} files loaded from config sever.", serviceFiles.size()); - logger.debug("loadFiles: {}", serviceFiles); + if(logger.isDebugEnabled()) logger.debug("loadFiles:", JsonMapper.toJson(serviceFiles)); try { Path filePath = Paths.get(targetConfigsDirectory); if (!Files.exists(filePath)) { @@ -260,7 +257,7 @@ private Map getServiceConfigs(String configServerPath) { Map configs = new HashMap<>(); - logger.debug("Calling Config Server endpoint:host{}:path{}", configServerUri, configServerPath); + if(logger.isDebugEnabled()) logger.debug("Calling Config Server endpoint:host{}:path{}", configServerUri, configServerPath); HttpRequest request = HttpRequest.newBuilder() .uri(URI.create(configServerUri.trim() + configServerPath.trim())) .header(Headers.AUTHORIZATION_STRING, authorization) @@ -274,8 +271,7 @@ private Map getServiceConfigs(String configServerPath) { logger.error("Failed to load configs from config server" + statusCode + ":" + body); throw new Exception("Failed to load configs from config server: " + statusCode); } else { - Map responseMap = (Map) mapper.readValue(body, new TypeReference>() {}); - configs = (Map) responseMap.get("configProperties"); + configs = mapper.readValue(body, new TypeReference>() {}); } } catch (Exception e) { logger.error("Exception while calling config server:", e);