Skip to content

Commit f19ea79

Browse files
authored
Merge pull request #110 from networknt/issue109
fixes #109 All header access by key should be case insensitive
2 parents c475424 + 379610a commit f19ea79

File tree

14 files changed

+66
-30
lines changed

14 files changed

+66
-30
lines changed

pom.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,16 @@
112112
<artifactId>status</artifactId>
113113
<version>${version.light-4j}</version>
114114
</dependency>
115+
<dependency>
116+
<groupId>com.networknt</groupId>
117+
<artifactId>cluster</artifactId>
118+
<version>${version.light-4j}</version>
119+
</dependency>
120+
<dependency>
121+
<groupId>com.networknt</groupId>
122+
<artifactId>monad-result</artifactId>
123+
<version>${version.light-4j}</version>
124+
</dependency>
115125
<dependency>
116126
<groupId>com.networknt</groupId>
117127
<artifactId>env-config</artifactId>

src/main/java/com/networknt/aws/lambda/handler/middleware/audit/AuditMiddleware.java

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import com.networknt.config.JsonMapper;
1010
import com.networknt.mask.Mask;
1111
import com.networknt.status.Status;
12+
import com.networknt.utility.MapUtil;
1213
import com.networknt.utility.ModuleRegistry;
1314
import com.networknt.utility.StringUtils;
1415
import org.slf4j.Logger;
@@ -159,9 +160,13 @@ private void auditRequest(LightLambdaExchange exchange, Map<String, Object> audi
159160

160161
private void auditHeader(LightLambdaExchange exchange, Map<String, Object> auditMap) {
161162
for (String name : CONFIG.getHeaderList()) {
162-
String value = exchange.getRequest().getHeaders().get(name);
163-
if(LOG.isTraceEnabled()) LOG.trace("header name = " + name + " header value = " + value);
164-
auditMap.put(name, CONFIG.isMask() ? Mask.maskRegex(value, "requestHeader", name) : value);
163+
Optional<String> optionalValue = MapUtil.getValueIgnoreCase(exchange.getRequest().getHeaders(), name);
164+
if(optionalValue.isEmpty()) {
165+
if(LOG.isTraceEnabled()) LOG.trace("header name = {} header value is null", name);
166+
continue;
167+
}
168+
if(LOG.isTraceEnabled()) LOG.trace("header name = {} header value = {}", name, optionalValue.get());
169+
auditMap.put(name, CONFIG.isMask() ? Mask.maskRegex(optionalValue.get(), "requestHeader", name) : optionalValue.get());
165170
}
166171
}
167172

@@ -170,8 +175,9 @@ private void auditRequestBody(LightLambdaExchange exchange, Map<String, Object>
170175
String requestBodyString = exchange.getRequest().getBody();
171176
// Mask requestBody json string if mask enabled
172177
if (requestBodyString != null && !requestBodyString.isEmpty()) {
173-
String contentType = exchange.getRequest().getHeaders().get(HeaderKey.CONTENT_TYPE);
174-
if(contentType != null) {
178+
Optional<String> optionalContentType = MapUtil.getValueIgnoreCase(exchange.getRequest().getHeaders(), HeaderKey.CONTENT_TYPE);
179+
if(optionalContentType.isPresent()) {
180+
String contentType = optionalContentType.get();
175181
if(contentType.startsWith("application/json")) {
176182
if(CONFIG.isMask()) requestBodyString = Mask.maskJson(requestBodyString, REQUEST_BODY_KEY);
177183
} else if(contentType.startsWith("text") || contentType.startsWith("application/xml")) {
@@ -192,8 +198,9 @@ private void auditResponseBody(LightLambdaExchange exchange, Map<String, Object>
192198
String responseBodyString = exchange.getResponse().getBody();
193199
// mask the response body json string if mask is enabled.
194200
if(responseBodyString != null && !responseBodyString.isEmpty()) {
195-
String contentType = exchange.getResponse().getHeaders().get(HeaderKey.CONTENT_TYPE);
196-
if(contentType != null) {
201+
Optional<String> optionalContentType = MapUtil.getValueIgnoreCase(exchange.getResponse().getHeaders(), HeaderKey.CONTENT_TYPE);
202+
if(optionalContentType.isPresent()) {
203+
String contentType = optionalContentType.orElse(null);
197204
if(contentType.startsWith("application/json")) {
198205
if(CONFIG.isMask()) responseBodyString =Mask.maskJson(responseBodyString, RESPONSE_BODY_KEY);
199206
} else if(contentType.startsWith("text") || contentType.startsWith("application/xml")) {

src/main/java/com/networknt/aws/lambda/handler/middleware/correlation/CorrelationMiddleware.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.networknt.config.Config;
77
import com.networknt.correlation.CorrelationConfig;
88
import com.networknt.status.Status;
9+
import com.networknt.utility.MapUtil;
910
import com.networknt.utility.ModuleRegistry;
1011
import org.apache.commons.codec.binary.Base64;
1112
import org.slf4j.Logger;
@@ -14,6 +15,7 @@
1415

1516
import java.nio.ByteBuffer;
1617
import java.util.HashMap;
18+
import java.util.Optional;
1719
import java.util.UUID;
1820

1921

@@ -34,15 +36,17 @@ public Status execute(final LightLambdaExchange exchange) {
3436
// check if the cid is in the request header
3537
String cid = null;
3638
if(exchange.getRequest().getHeaders() != null) {
37-
cid = exchange.getRequest().getHeaders().get(HeaderKey.CORRELATION);
39+
Optional<String> optionalCid = MapUtil.getValueIgnoreCase(exchange.getRequest().getHeaders(), HeaderKey.CORRELATION);
40+
cid = optionalCid.orElse(null);
3841
} else {
3942
exchange.getRequest().setHeaders(new HashMap<>());
4043
}
4144
if (cid == null && CONFIG.isAutogenCorrelationID()) {
4245
cid = this.getUUID();
4346
exchange.getRequest().getHeaders().put(HeaderKey.CORRELATION, cid);
4447
exchange.addAttachment(CORRELATION_ATTACHMENT_KEY, cid);
45-
String tid = exchange.getRequest().getHeaders().get(HeaderKey.TRACEABILITY);
48+
Optional<String> optionalTid = MapUtil.getValueIgnoreCase(exchange.getRequest().getHeaders(), HeaderKey.TRACEABILITY);
49+
String tid = optionalTid.orElse(null);
4650
if (tid != null && LOG.isInfoEnabled())
4751
LOG.info("Associate traceability Id {} with correlation Id {}", tid, cid);
4852
}

src/main/java/com/networknt/aws/lambda/handler/middleware/limit/key/AkamaiAddressKeyResolver.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package com.networknt.aws.lambda.handler.middleware.limit.key;
22

33
import com.networknt.aws.lambda.LightLambdaExchange;
4+
import com.networknt.utility.MapUtil;
45

56
import java.util.Map;
7+
import java.util.Optional;
68

79
/**
810
* When native-lambda is used for external clients and all external requests go through the
@@ -16,8 +18,8 @@ public class AkamaiAddressKeyResolver implements KeyResolver {
1618
public String resolve(LightLambdaExchange exchange) {
1719
String key = "127.0.0.1";
1820
Map<String, String> headerMap = exchange.getResponse().getHeaders();
19-
String value = headerMap.get("True-Client-IP");
20-
if(value != null) key = value;
21+
Optional<String> valueString = MapUtil.getValueIgnoreCase(headerMap, "True-Client-IP");
22+
if(valueString.isPresent()) key = valueString.get();
2123
return key;
2224
}
2325
}

src/main/java/com/networknt/aws/lambda/handler/middleware/limit/key/JwtHeaderClientIdKeyResolver.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
import com.networknt.aws.lambda.LightLambdaExchange;
44
import com.networknt.aws.lambda.handler.middleware.audit.AuditMiddleware;
55
import com.networknt.utility.Constants;
6+
import com.networknt.utility.MapUtil;
67

78
import java.util.Map;
9+
import java.util.Optional;
810

911
/**
1012
* This is a customized KeyResolver for one of our customers on the external gateway in the DMZ.
@@ -27,8 +29,8 @@ public String resolve(LightLambdaExchange exchange) {
2729
if(key == null) {
2830
// try to get the key from the header
2931
Map<String, String> headerMap = exchange.getRequest().getHeaders();
30-
String value = headerMap.get("Client-Id");
31-
if(value != null) key = value;
32+
Optional<String> optionalValue = MapUtil.getValueIgnoreCase(headerMap, "Client-Id");
33+
if(optionalValue.isPresent()) key = optionalValue.get();
3234
}
3335
return key;
3436
}

src/main/java/com/networknt/aws/lambda/handler/middleware/sanitizer/SanitizerMiddleware.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,18 +49,21 @@ public Status execute(LightLambdaExchange exchange) {
4949
for (Map.Entry<String, String> entry: headerMap.entrySet()) {
5050
// if ignore list exists, it will take the precedence.
5151
if(CONFIG.getHeaderAttributesToIgnore() != null && CONFIG.getHeaderAttributesToIgnore().stream().anyMatch(entry.getKey()::equalsIgnoreCase)) {
52-
if(LOG.isTraceEnabled()) LOG.trace("Ignore header " + entry.getKey() + " as it is in the ignore list.");
52+
if(LOG.isTraceEnabled())
53+
LOG.trace("Ignore header {} as it is in the ignore list.", entry.getKey());
5354
continue;
5455
}
5556

5657
if(CONFIG.getHeaderAttributesToEncode() != null) {
5758
if(CONFIG.getHeaderAttributesToEncode().stream().anyMatch(entry.getKey()::equalsIgnoreCase)) {
58-
if(LOG.isTraceEnabled()) LOG.trace("Encode header " + entry.getKey() + " as it is not in the ignore list and it is in the encode list.");
59+
if(LOG.isTraceEnabled())
60+
LOG.trace("Encode header {} as it is not in the ignore list and it is in the encode list.", entry.getKey());
5961
entry.setValue(headerEncoder.applyEncoding(entry.getValue()));
6062
}
6163
} else {
6264
// no attributes to encode, encode everything except the ignore list.
63-
if(LOG.isTraceEnabled()) LOG.trace("Encode header " + entry.getKey() + " as it is not in the ignore list and the encode list is null.");
65+
if(LOG.isTraceEnabled())
66+
LOG.trace("Encode header {} as it is not in the ignore list and the encode list is null.", entry.getKey());
6467
entry.setValue(headerEncoder.applyEncoding(entry.getValue()));
6568
}
6669
}

src/main/java/com/networknt/aws/lambda/handler/middleware/security/JwtVerifyMiddleware.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,9 @@ public Status handleJwt(LightLambdaExchange exchange, String pathPrefix, String
120120
auditInfo.put(Constants.CLIENT_ID_STRING, clientId);
121121
auditInfo.put(Constants.ISSUER_CLAIMS, issuer);
122122
Map<String, String> headers = exchange.getRequest().getHeaders();
123-
String callerId = headers.get(Constants.CALLER_ID_STRING);
124-
125-
if (callerId != null)
126-
auditInfo.put(Constants.CALLER_ID_STRING, callerId);
123+
Optional<String> optionalCallerId = MapUtil.getValueIgnoreCase(headers, Constants.CALLER_ID_STRING);
124+
if (optionalCallerId.isPresent())
125+
auditInfo.put(Constants.CALLER_ID_STRING, optionalCallerId.get());
127126

128127
exchange.addAttachment(AUDIT_ATTACHMENT_KEY, auditInfo);
129128

src/main/java/com/networknt/aws/lambda/handler/middleware/security/SwtVerifyMiddleware.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,9 @@ public Status handleSwt(LightLambdaExchange exchange, String reqPath, List<Strin
106106
String issuer = tokenInfo.getIss();
107107
auditInfo.put(Constants.ISSUER_CLAIMS, issuer);
108108
Map<String, String> headers = exchange.getRequest().getHeaders();
109-
String callerId = headers.get(Constants.CALLER_ID_STRING);
110-
if (callerId != null)
111-
auditInfo.put(Constants.CALLER_ID_STRING, callerId);
109+
Optional<String> optionalCallerId = MapUtil.getValueIgnoreCase(headers, Constants.CALLER_ID_STRING);
110+
if (optionalCallerId.isPresent())
111+
auditInfo.put(Constants.CALLER_ID_STRING, optionalCallerId.get());
112112
exchange.addAttachment(AUDIT_ATTACHMENT_KEY, auditInfo);
113113

114114
if (CONFIG != null && CONFIG.isEnableVerifyScope()) {

src/main/java/com/networknt/aws/lambda/handler/middleware/traceability/TraceabilityMiddleware.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@
77
import com.networknt.config.Config;
88
import com.networknt.status.Status;
99
import com.networknt.traceability.TraceabilityConfig;
10+
import com.networknt.utility.MapUtil;
1011
import com.networknt.utility.ModuleRegistry;
1112
import org.slf4j.Logger;
1213
import org.slf4j.LoggerFactory;
1314
import org.slf4j.MDC;
1415

1516
import java.util.HashMap;
17+
import java.util.Optional;
1618

1719
public class TraceabilityMiddleware implements MiddlewareHandler {
1820
private static final Logger LOG = LoggerFactory.getLogger(TraceabilityMiddleware.class);
@@ -34,7 +36,8 @@ public Status execute(final LightLambdaExchange exchange) {
3436

3537
String tid = null;
3638
if(exchange.getRequest().getHeaders() != null) {
37-
tid = exchange.getRequest().getHeaders().get(HeaderKey.TRACEABILITY);
39+
Optional<String> tidOptional = MapUtil.getValueIgnoreCase(exchange.getRequest().getHeaders(), HeaderKey.TRACEABILITY);
40+
tid = tidOptional.orElse(null);
3841
}
3942

4043
if (tid != null) {

src/main/java/com/networknt/aws/lambda/handler/middleware/transformer/RequestTransformerMiddleware.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import com.networknt.status.Status;
1212
import com.networknt.utility.ConfigUtils;
1313
import com.networknt.utility.Constants;
14+
import com.networknt.utility.MapUtil;
1415
import com.networknt.utility.ModuleRegistry;
1516
import org.slf4j.Logger;
1617
import org.slf4j.LoggerFactory;
@@ -140,7 +141,7 @@ public Status execute(LightLambdaExchange exchange) {
140141
if(removeList != null) {
141142
removeList.forEach(s -> {
142143
if(LOG.isTraceEnabled()) LOG.trace("removing request header: " + s);
143-
exchange.getRequest().getHeaders().remove(s);
144+
MapUtil.delValueIgnoreCase(exchange.getRequest().getHeaders(), s);
144145
});
145146
}
146147
Map<String, Object> updateMap = (Map)requestHeaders.get("update");

0 commit comments

Comments
 (0)