diff options
author | 2019-10-07 19:23:26 +0000 | |
---|---|---|
committer | 2019-10-08 16:43:15 +0000 | |
commit | d7e9d6e8db709ebd67791795f9d08020773fdd48 (patch) | |
tree | 2512680c9617182d8c8564718e01407d608074d7 /reference/logging-filter/logging-filter-base/src/main/java/org | |
parent | 2ef607c851f540a5a6172f73dcab9c26dd7b4751 (diff) |
Update method setMDCPartnerName in MDCSetup class
read partner name from authorization header when possible
Issue-ID: LOG-1161
Signed-off-by: Smokowski, Kevin (ks6305) <kevin.smokowski@att.com>
Change-Id: I489a0cfe88e845b7e8a4234240601064f8728640
Diffstat (limited to 'reference/logging-filter/logging-filter-base/src/main/java/org')
-rw-r--r-- | reference/logging-filter/logging-filter-base/src/main/java/org/onap/logging/filter/base/MDCSetup.java | 65 |
1 files changed, 51 insertions, 14 deletions
diff --git a/reference/logging-filter/logging-filter-base/src/main/java/org/onap/logging/filter/base/MDCSetup.java b/reference/logging-filter/logging-filter-base/src/main/java/org/onap/logging/filter/base/MDCSetup.java index 93c16a8..75fde49 100644 --- a/reference/logging-filter/logging-filter-base/src/main/java/org/onap/logging/filter/base/MDCSetup.java +++ b/reference/logging-filter/logging-filter-base/src/main/java/org/onap/logging/filter/base/MDCSetup.java @@ -26,6 +26,7 @@ import java.time.ZoneOffset; import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; import java.time.temporal.ChronoUnit; +import java.util.Base64; import java.util.UUID; import javax.servlet.http.HttpServletRequest; import javax.ws.rs.core.HttpHeaders; @@ -123,23 +124,42 @@ public class MDCSetup { } public void setMDCPartnerName(SimpleMap headers) { - logger.trace("Checking X-ONAP-PartnerName header for partnerName."); - String partnerName = headers.get(ONAPLogConstants.Headers.PARTNER_NAME); - if (partnerName == null || partnerName.isEmpty()) { - logger.trace("No valid X-ONAP-PartnerName header value. Checking User-Agent header for partnerName."); - partnerName = headers.get(HttpHeaders.USER_AGENT); - if (partnerName == null || partnerName.isEmpty()) { - logger.trace("No valid User-Agent header value. Checking X-ClientID header for partnerName."); - partnerName = headers.get(Constants.HttpHeaders.CLIENT_ID); - if (partnerName == null || partnerName.isEmpty()) { - logger.trace("No valid partnerName headers. Defaulting partnerName to UNKNOWN."); - partnerName = Constants.DefaultValues.UNKNOWN; - } - } - } + String partnerName = getMDCPartnerName(headers); MDC.put(ONAPLogConstants.MDCs.PARTNER_NAME, partnerName); } + protected String getMDCPartnerName(SimpleMap headers) { + String checkHeaderLogPattern = "Checking {} header to determine the value of {}"; + + logger.trace(checkHeaderLogPattern, HttpHeaders.AUTHORIZATION, ONAPLogConstants.MDCs.PARTNER_NAME); + String partnerName = getBasicAuthUserName(headers); + if (partnerName != null && !partnerName.isEmpty()) { + return partnerName; + } + + logger.trace(checkHeaderLogPattern, ONAPLogConstants.Headers.PARTNER_NAME, ONAPLogConstants.MDCs.PARTNER_NAME); + partnerName = headers.get(ONAPLogConstants.Headers.PARTNER_NAME); + if (partnerName != null && !partnerName.isEmpty()) { + return partnerName; + } + + logger.trace(checkHeaderLogPattern, HttpHeaders.USER_AGENT, ONAPLogConstants.MDCs.PARTNER_NAME); + partnerName = headers.get(HttpHeaders.USER_AGENT); + if (partnerName != null && !partnerName.isEmpty()) { + return partnerName; + } + + logger.trace(checkHeaderLogPattern, Constants.HttpHeaders.CLIENT_ID, ONAPLogConstants.MDCs.PARTNER_NAME); + partnerName = headers.get(Constants.HttpHeaders.CLIENT_ID); + if (partnerName != null && !partnerName.isEmpty()) { + return partnerName; + } + + logger.trace("{} value could not be determined, defaulting partnerName to {}.", + ONAPLogConstants.MDCs.PARTNER_NAME, Constants.DefaultValues.UNKNOWN); + return Constants.DefaultValues.UNKNOWN; + } + public void setLogTimestamp() { MDC.put(ONAPLogConstants.MDCs.LOG_TIMESTAMP, ZonedDateTime.now(ZoneOffset.UTC).format(DateTimeFormatter.ISO_INSTANT)); @@ -219,4 +239,21 @@ public class MDCSetup { } return propertyValue; } + + protected String getBasicAuthUserName(SimpleMap headers) { + String encodedAuthorizationValue = headers.get(HttpHeaders.AUTHORIZATION); + if (encodedAuthorizationValue != null) { + try { + // This will strip the word Basic and single space + encodedAuthorizationValue = encodedAuthorizationValue.substring(6); + byte[] decodedBytes = Base64.getDecoder().decode(encodedAuthorizationValue); + String decodedString = new String(decodedBytes); + int idx = decodedString.indexOf(':'); + return decodedString.substring(0, idx); + } catch (IllegalArgumentException e) { + logger.error("could not decode basic auth value " + encodedAuthorizationValue, e); + } + } + return null; + } } |