diff --git a/egress-router/src/main/java/com/networknt/router/RouterConfig.java b/egress-router/src/main/java/com/networknt/router/RouterConfig.java index 77a97f563d..05354a8060 100644 --- a/egress-router/src/main/java/com/networknt/router/RouterConfig.java +++ b/egress-router/src/main/java/com/networknt/router/RouterConfig.java @@ -17,6 +17,7 @@ import com.networknt.config.Config; import com.networknt.config.ConfigException; +import com.networknt.config.JsonMapper; import com.networknt.handler.config.MethodRewriteRule; import com.networknt.handler.config.QueryHeaderRewriteRule; import com.networknt.handler.config.UrlRewriteRule; @@ -186,10 +187,22 @@ public List getHostWhitelist() { public void setHostWhitelist() { this.hostWhitelist = new ArrayList<>(); - if (mappedConfig.get("hostWhitelist") !=null && mappedConfig.get("hostWhitelist") instanceof String) { - hostWhitelist.add((String)mappedConfig.get("hostWhitelist")); - } else { - hostWhitelist = (List)mappedConfig.get("hostWhitelist"); + if (mappedConfig.get("hostWhitelist") != null) { + if (mappedConfig.get("hostWhitelist") instanceof String) { + // multiple host as an JSON list. + String s = (String)mappedConfig.get("hostWhitelist"); + s = s.trim(); + if(logger.isTraceEnabled()) logger.trace("s = " + s); + if(s.startsWith("[")) { + // json list + hostWhitelist = (List)JsonMapper.fromJson(s, List.class); + } else { + // single host as a string + hostWhitelist.add((String) mappedConfig.get("hostWhitelist")); + } + } else { + hostWhitelist = (List)mappedConfig.get("hostWhitelist"); + } } } @@ -203,11 +216,25 @@ public List getUrlRewriteRules() { public void setUrlRewriteRules() { this.urlRewriteRules = new ArrayList<>(); - if (mappedConfig.get("urlRewriteRules") !=null && mappedConfig.get("urlRewriteRules") instanceof String) { - urlRewriteRules.add(UrlRewriteRule.convertToUrlRewriteRule((String)mappedConfig.get("urlRewriteRules"))); - } else { - List rules = (List)mappedConfig.get("urlRewriteRules"); - if(rules != null) { + if (mappedConfig.get("urlRewriteRules") != null) { + if (mappedConfig.get("urlRewriteRules") instanceof String) { + String s = (String)mappedConfig.get("urlRewriteRules"); + s = s.trim(); + if(logger.isTraceEnabled()) logger.trace("s = " + s); + // There are two formats for the urlRewriteRules. One is a string separated by a space + // and the other is a list of strings separated by a space in JSON list format. + if(s.startsWith("[")) { + // multiple rules + List rules = (List)JsonMapper.fromJson(s, List.class); + for (String rule : rules) { + urlRewriteRules.add(UrlRewriteRule.convertToUrlRewriteRule(rule)); + } + } else { + // single rule + urlRewriteRules.add(UrlRewriteRule.convertToUrlRewriteRule(s)); + } + } else if (mappedConfig.get("urlRewriteRules") instanceof List) { + List rules = (List)mappedConfig.get("urlRewriteRules"); for (String s : rules) { urlRewriteRules.add(UrlRewriteRule.convertToUrlRewriteRule(s)); } @@ -225,11 +252,23 @@ public List getMethodRewriteRules() { public void setMethodRewriteRules() { this.methodRewriteRules = new ArrayList<>(); - if (mappedConfig.get("methodRewriteRules") !=null && mappedConfig.get("methodRewriteRules") instanceof String) { - methodRewriteRules.add(convertToMethodRewriteRule((String)mappedConfig.get("methodRewriteRules"))); - } else { - List rules = (List)mappedConfig.get("methodRewriteRules"); - if(rules != null) { + if(mappedConfig.get("methodRewriteRules") != null) { + if (mappedConfig.get("methodRewriteRules") instanceof String) { + String s = (String)mappedConfig.get("methodRewriteRules"); + s = s.trim(); + if(logger.isTraceEnabled()) logger.trace("s = " + s); + if(s.startsWith("[")) { + // multiple rules + List rules = (List)JsonMapper.fromJson(s, List.class); + for (String rule : rules) { + methodRewriteRules.add(convertToMethodRewriteRule(rule)); + } + } else { + // single rule + methodRewriteRules.add(convertToMethodRewriteRule(s)); + } + } else if (mappedConfig.get("methodRewriteRules") instanceof List) { + List rules = (List)mappedConfig.get("methodRewriteRules"); for (String s : rules) { methodRewriteRules.add(convertToMethodRewriteRule(s)); } @@ -282,26 +321,47 @@ public void setQueryParamRewriteRules(Map> public void setQueryParamRewriteRules() { queryParamRewriteRules = new HashMap<>(); - if (mappedConfig.get("queryParamRewriteRules") != null && mappedConfig.get("queryParamRewriteRules") instanceof Map) { - Map map = (Map)mappedConfig.get("queryParamRewriteRules"); - for (Map.Entry r: map.entrySet()) { - String key = r.getKey(); - Object object = r.getValue(); - if(object instanceof List) { - List rules = new ArrayList<>(); - List> values = (List>)object; - for(Map value: values) { - QueryHeaderRewriteRule rule = new QueryHeaderRewriteRule(); - rule.setOldK(value.get("oldK")); - rule.setNewK(value.get("newK")); - rule.setOldV(value.get("oldV")); - rule.setNewV(value.get("newV")); - rules.add(rule); - } - queryParamRewriteRules.put(key, rules); + if(mappedConfig.get("queryParamRewriteRules") != null) { + if(mappedConfig.get("queryParamRewriteRules") instanceof String) { + String s = (String)mappedConfig.get("queryParamRewriteRules"); + s = s.trim(); + if(logger.isTraceEnabled()) logger.trace("s = " + s); + if(s.startsWith("{")) { + // json map + Map map = JsonMapper.fromJson(s, Map.class); + queryParamRewriteRules = populateQueryParameterRules(map); + } else { + logger.error("queryParamRewriteRules is the wrong type. Only JSON map or YAML map is supported."); + } + } else if(mappedConfig.get("queryParamRewriteRules") instanceof Map) { + Map map = (Map)mappedConfig.get("queryParamRewriteRules"); + queryParamRewriteRules = populateQueryParameterRules(map); + } else { + logger.error("queryParamRewriteRules is the wrong type. Only JSON map or YAML map is supported."); + } + } + } + + private Map> populateQueryParameterRules(Map map) { + queryParamRewriteRules = new HashMap<>(); + for (Map.Entry r: map.entrySet()) { + String key = r.getKey(); + Object object = r.getValue(); + if(object instanceof List) { + List rules = new ArrayList<>(); + List> values = (List>)object; + for(Map value: values) { + QueryHeaderRewriteRule rule = new QueryHeaderRewriteRule(); + rule.setOldK(value.get("oldK")); + rule.setNewK(value.get("newK")); + rule.setOldV(value.get("oldV")); + rule.setNewV(value.get("newV")); + rules.add(rule); } + queryParamRewriteRules.put(key, rules); } } + return queryParamRewriteRules; } public Map> getHeaderRewriteRules() { @@ -314,28 +374,49 @@ public void setHeaderRewriteRules(Map> head public void setHeaderRewriteRules() { headerRewriteRules = new HashMap<>(); - if (mappedConfig.get("headerRewriteRules") != null && mappedConfig.get("headerRewriteRules") instanceof Map) { - Map map = (Map)mappedConfig.get("headerRewriteRules"); - for (Map.Entry r: map.entrySet()) { - String key = r.getKey(); - Object object = r.getValue(); - if(object instanceof List) { - List rules = new ArrayList<>(); - List> values = (List>)object; - for(Map value: values) { - QueryHeaderRewriteRule rule = new QueryHeaderRewriteRule(); - rule.setOldK(value.get("oldK")); - rule.setNewK(value.get("newK")); - rule.setOldV(value.get("oldV")); - rule.setNewV(value.get("newV")); - rules.add(rule); - } - headerRewriteRules.put(key, rules); + if(mappedConfig.get("headerRewriteRules") != null) { + if(mappedConfig.get("headerRewriteRules") instanceof String) { + String s = (String)mappedConfig.get("headerRewriteRules"); + s = s.trim(); + if(logger.isTraceEnabled()) logger.trace("s = " + s); + if(s.startsWith("{")) { + // json map + Map map = JsonMapper.fromJson(s, Map.class); + headerRewriteRules = populateHeaderRewriteRules(map); + } else { + logger.error("headerRewriteRules is the wrong type. Only JSON map or YAML map is supported."); } + } else if(mappedConfig.get("headerRewriteRules") instanceof Map) { + Map map = (Map)mappedConfig.get("headerRewriteRules"); + headerRewriteRules = populateHeaderRewriteRules(map); + } else { + logger.error("headerRewriteRules is the wrong type. Only JSON map or YAML map is supported."); } } } + private Map> populateHeaderRewriteRules(Map map) { + headerRewriteRules = new HashMap<>(); + for (Map.Entry r: map.entrySet()) { + String key = r.getKey(); + Object object = r.getValue(); + if(object instanceof List) { + List rules = new ArrayList<>(); + List> values = (List>)object; + for(Map value: values) { + QueryHeaderRewriteRule rule = new QueryHeaderRewriteRule(); + rule.setOldK(value.get("oldK")); + rule.setNewK(value.get("newK")); + rule.setOldV(value.get("oldV")); + rule.setNewV(value.get("newV")); + rules.add(rule); + } + headerRewriteRules.put(key, rules); + } + } + return headerRewriteRules; + } + public void setPathPrefixMaxRequestTime() { pathPrefixMaxRequestTime = new HashMap<>(); if (mappedConfig.get(PATH_PREFIX_MAX_REQUEST_TIME) != null) { diff --git a/egress-router/src/test/resources/config/values.yml b/egress-router/src/test/resources/config/values.yml index b2c206bea3..c7e630e4de 100644 --- a/egress-router/src/test/resources/config/values.yml +++ b/egress-router/src/test/resources/config/values.yml @@ -1,7 +1,9 @@ +# router.hostWhitelist: "[\"192.168.0.*\",\"10.1.2.*\"]" router.hostWhitelist: - 192.168.0.* - 10.1.2.* +# router.urlRewriteRules: "[\"/listings/(.*)$ /listing.html?listing=$1\",\"/ph/uat/de-asia-ekyc-service/v1 /uat-de-asia-ekyc-service/v1\",\"(/tutorial/.*)/wordpress/(\\\\w+)\\\\.?.*$ $1/cms/$2.php\"]" router.urlRewriteRules: # test your regex rule at https://www.freeformatter.com/java-regex-tester.html#ad-output # /listings/123 to /listing.html?listing=123 @@ -11,6 +13,7 @@ router.urlRewriteRules: # /tutorial/linux/wordpress/file1 to /tutorial/linux/cms/file1.php - (/tutorial/.*)/wordpress/(\w+)\.?.*$ $1/cms/$2.php +# router.methodRewriteRules: "[\"/v2/address POST PUT\",\"/v1/address POST PATCH\",\"/v1/address GET DELETE\",\"/v1/pets/{petId} GET DELETE\"]" router.methodRewriteRules: # rewrite POST to PUT for path /v2/address - /v2/address POST PUT @@ -21,6 +24,7 @@ router.methodRewriteRules: # rewrite GET to DELETE for path /v1/pets/{petId} with path parameters - /v1/pets/{petId} GET DELETE +# router.queryParamRewriteRules: "{\"/v1/address\":[{\"oldK\":\"business-query\",\"newK\":\"request-query\",\"oldV\":\"value1\",\"newV\":\"value2\"},{\"oldK\":\"module\",\"newK\":\"mod\"},{\"oldK\":\"app-id\",\"oldV\":\"esb\",\"newV\":\"emb\"}],\"/v2/address\":[{\"oldK\":\"key1\",\"oldV\":\"old\",\"newV\":\"new\"}],\"/v3/address\":[{\"oldK\":\"path\",\"newK\":\"route\"}],\"/v1/pets/{petId}\":[{\"oldK\":\"path\",\"newK\":\"route\"}]}" router.queryParamRewriteRules: # rewrite query parameter key business-query to request-query and value value1 to value2 for /v1/address /v1/address: @@ -51,6 +55,7 @@ router.queryParamRewriteRules: - oldK: path newK: route +# router.headerRewriteRules: "{\"/v1/address\":[{\"oldK\":\"business-query\",\"newK\":\"request-query\",\"oldV\":\"value1\",\"newV\":\"value2\"},{\"oldK\":\"module\",\"newK\":\"mod\"},{\"oldK\":\"app-id\",\"oldV\":\"esb\",\"newV\":\"emb\"}],\"/v2/address\":[{\"oldK\":\"key1\",\"oldV\":\"old\",\"newV\":\"new\"}],\"/v3/address\":[{\"oldK\":\"path\",\"newK\":\"route\"}],\"/v1/pets/{petId}\":[{\"oldK\":\"path\",\"newK\":\"route\"}]}" router.headerRewriteRules: # rewrite header key business-query to request-query and value value1 to value2 for /v1/address /v1/address: @@ -86,7 +91,8 @@ router.headerRewriteRules: # /v1/address: 5000 # /v2/address: 10000 # /v3/address: 30000 -router.pathPrefixMaxRequestTime: {"/v1/address":5000,"/v2/address":10000,"/v3/address":30000,"/v1/pets/{petId}": 5000} +router.pathPrefixMaxRequestTime: "{\"/v1/address\":5000,\"/v2/address\":10000,\"/v3/address\":30000,\"/v1/pets/{petId}\": 5000}" +# router.pathPrefixMaxRequestTime: {"/v1/address":5000,"/v2/address":10000,"/v3/address":30000,"/v1/pets/{petId}": 5000} # client.yml # test configuration for the TokenHandler with multiple OAuth 2.0 providers.