From e044e1d18e37c2783c4e3ba6a06837aee79bedcc Mon Sep 17 00:00:00 2001 From: Steve Hu Date: Fri, 24 Nov 2023 23:32:24 -0500 Subject: [PATCH] fixes #2019 do not overwrite the values.yml if config server is not available --- .../com/networknt/server/DefaultConfigLoader.java | 13 +++++++++++-- .../src/main/java/com/networknt/status/Status.java | 6 +++--- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/server/src/main/java/com/networknt/server/DefaultConfigLoader.java b/server/src/main/java/com/networknt/server/DefaultConfigLoader.java index 2a3eb1bfb0..a02087a795 100644 --- a/server/src/main/java/com/networknt/server/DefaultConfigLoader.java +++ b/server/src/main/java/com/networknt/server/DefaultConfigLoader.java @@ -198,6 +198,10 @@ private synchronized void loadConfigs(String queryParameters) { String configServerConfigsPath = CONFIG_SERVER_CONFIGS_CONTEXT_ROOT + queryParameters; //get service configs and put them in config cache Map serviceConfigs = getServiceConfigs(configServerConfigsPath); + if(serviceConfigs == null) { + logger.error("Failed to load configs from config server. Please check the logs for more details."); + return; + } 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 @@ -235,6 +239,10 @@ private void loadFiles(String configPath, String contextRoot) { String configServerFilesPath = contextRoot + configPath; //get service files and put them in config dir Map serviceFiles = getServiceConfigs(configServerFilesPath); + if(serviceFiles == null) { + logger.error("Failed to load files from config server. Please check the logs for more details."); + return; + } if(logger.isDebugEnabled()) logger.debug("loadFiles: " + JsonMapper.toJson(serviceFiles)); try { Path filePath = Paths.get(targetConfigsDirectory); @@ -249,7 +257,7 @@ private void loadFiles(String configPath, String contextRoot) { Files.write(filePath, ba); } } catch (IOException e) { - logger.error("Exception while creating {} dir or creating files there:{}",targetConfigsDirectory, e); + logger.error("Exception while creating {} dir or creating files there", targetConfigsDirectory, e); } } @@ -293,7 +301,8 @@ private Map getServiceConfigs(String configServerPath) { String body = response.body(); if(statusCode >= 300) { logger.error("Failed to load configs from config server" + statusCode + ":" + body); - throw new Exception("Failed to load configs from config server: " + statusCode); + // return null, so that the values.yml won't be overwritten and the server can still be started with it. + return null; } else { // validate the headers against the product id and version. If they are not matched, throw an exception. // this validation call is commented out for now as it is not ready on the config server side. diff --git a/status/src/main/java/com/networknt/status/Status.java b/status/src/main/java/com/networknt/status/Status.java index caa7ed5a8e..cd0c9643c3 100644 --- a/status/src/main/java/com/networknt/status/Status.java +++ b/status/src/main/java/com/networknt/status/Status.java @@ -232,15 +232,15 @@ public void setMetadata(Map metadata) { } public static boolean shouldShowMetadata() { - return config.get(SHOW_METADATA) == null ? false : (boolean)config.get(SHOW_METADATA); + return config.get(SHOW_METADATA) == null ? false : Config.loadBooleanValue(SHOW_METADATA, config.get(SHOW_METADATA)); } public static boolean shouldShowMessage() { - return config.get(SHOW_MESSAGE) == null ? true : (boolean)config.get(SHOW_MESSAGE); + return config.get(SHOW_MESSAGE) == null ? true : Config.loadBooleanValue(SHOW_MESSAGE, config.get(SHOW_MESSAGE)); } public static boolean shouldShowDescription() { - return config.get(SHOW_DESCRIPTION) == null ? true : (boolean)config.get(SHOW_DESCRIPTION); + return config.get(SHOW_DESCRIPTION) == null ? true : Config.loadBooleanValue(SHOW_DESCRIPTION, config.get(SHOW_DESCRIPTION)); } /**