Skip to content

Commit

Permalink
fixes #2128 update RouterConfig to support stringified JSON values
Browse files Browse the repository at this point in the history
  • Loading branch information
stevehu committed Feb 14, 2024
1 parent 0edd5ad commit 50f73be
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 49 deletions.
177 changes: 129 additions & 48 deletions egress-router/src/main/java/com/networknt/router/RouterConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -186,10 +187,22 @@ public List<String> 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");
}
}
}

Expand All @@ -203,11 +216,25 @@ public List<UrlRewriteRule> 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<String> 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<String> rules = (List<String>)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<String> rules = (List)mappedConfig.get("urlRewriteRules");
for (String s : rules) {
urlRewriteRules.add(UrlRewriteRule.convertToUrlRewriteRule(s));
}
Expand All @@ -225,11 +252,23 @@ public List<MethodRewriteRule> 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<String> 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<String> rules = (List<String>)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<String> rules = (List)mappedConfig.get("methodRewriteRules");
for (String s : rules) {
methodRewriteRules.add(convertToMethodRewriteRule(s));
}
Expand Down Expand Up @@ -282,26 +321,47 @@ public void setQueryParamRewriteRules(Map<String, List<QueryHeaderRewriteRule>>

public void setQueryParamRewriteRules() {
queryParamRewriteRules = new HashMap<>();
if (mappedConfig.get("queryParamRewriteRules") != null && mappedConfig.get("queryParamRewriteRules") instanceof Map) {
Map<String, Object> map = (Map<String, Object>)mappedConfig.get("queryParamRewriteRules");
for (Map.Entry<String, Object> r: map.entrySet()) {
String key = r.getKey();
Object object = r.getValue();
if(object instanceof List) {
List<QueryHeaderRewriteRule> rules = new ArrayList<>();
List<Map<String, String>> values = (List<Map<String, String>>)object;
for(Map<String, String> 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<String, Object> 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<String, Object> map = (Map<String, Object>)mappedConfig.get("queryParamRewriteRules");
queryParamRewriteRules = populateQueryParameterRules(map);
} else {
logger.error("queryParamRewriteRules is the wrong type. Only JSON map or YAML map is supported.");
}
}
}

private Map<String, List<QueryHeaderRewriteRule>> populateQueryParameterRules(Map<String, Object> map) {
queryParamRewriteRules = new HashMap<>();
for (Map.Entry<String, Object> r: map.entrySet()) {
String key = r.getKey();
Object object = r.getValue();
if(object instanceof List) {
List<QueryHeaderRewriteRule> rules = new ArrayList<>();
List<Map<String, String>> values = (List<Map<String, String>>)object;
for(Map<String, String> 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<String, List<QueryHeaderRewriteRule>> getHeaderRewriteRules() {
Expand All @@ -314,28 +374,49 @@ public void setHeaderRewriteRules(Map<String, List<QueryHeaderRewriteRule>> head

public void setHeaderRewriteRules() {
headerRewriteRules = new HashMap<>();
if (mappedConfig.get("headerRewriteRules") != null && mappedConfig.get("headerRewriteRules") instanceof Map) {
Map<String, Object> map = (Map<String, Object>)mappedConfig.get("headerRewriteRules");
for (Map.Entry<String, Object> r: map.entrySet()) {
String key = r.getKey();
Object object = r.getValue();
if(object instanceof List) {
List<QueryHeaderRewriteRule> rules = new ArrayList<>();
List<Map<String, String>> values = (List<Map<String, String>>)object;
for(Map<String, String> 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<String, Object> 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<String, Object> map = (Map<String, Object>)mappedConfig.get("headerRewriteRules");
headerRewriteRules = populateHeaderRewriteRules(map);
} else {
logger.error("headerRewriteRules is the wrong type. Only JSON map or YAML map is supported.");
}
}
}

private Map<String, List<QueryHeaderRewriteRule>> populateHeaderRewriteRules(Map<String, Object> map) {
headerRewriteRules = new HashMap<>();
for (Map.Entry<String, Object> r: map.entrySet()) {
String key = r.getKey();
Object object = r.getValue();
if(object instanceof List) {
List<QueryHeaderRewriteRule> rules = new ArrayList<>();
List<Map<String, String>> values = (List<Map<String, String>>)object;
for(Map<String, String> 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) {
Expand Down
8 changes: 7 additions & 1 deletion egress-router/src/test/resources/config/values.yml
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 50f73be

Please sign in to comment.