From d7e9d6e8db709ebd67791795f9d08020773fdd48 Mon Sep 17 00:00:00 2001 From: "Smokowski, Kevin (ks6305)" Date: Mon, 7 Oct 2019 19:23:26 +0000 Subject: Update method setMDCPartnerName in MDCSetup class read partner name from authorization header when possible Issue-ID: LOG-1161 Signed-off-by: Smokowski, Kevin (ks6305) Change-Id: I489a0cfe88e845b7e8a4234240601064f8728640 --- .../org/onap/logging/filter/base/MDCSetup.java | 65 +++++++++++++++++----- .../org/onap/logging/filter/base/MDCSetupTest.java | 12 ++++ .../src/test/resources/log4j2.properties | 7 +++ 3 files changed, 70 insertions(+), 14 deletions(-) create mode 100644 reference/logging-filter/logging-filter-base/src/test/resources/log4j2.properties 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; + } } diff --git a/reference/logging-filter/logging-filter-base/src/test/java/org/onap/logging/filter/base/MDCSetupTest.java b/reference/logging-filter/logging-filter-base/src/test/java/org/onap/logging/filter/base/MDCSetupTest.java index 31d8da6..22ab9c3 100644 --- a/reference/logging-filter/logging-filter-base/src/test/java/org/onap/logging/filter/base/MDCSetupTest.java +++ b/reference/logging-filter/logging-filter-base/src/test/java/org/onap/logging/filter/base/MDCSetupTest.java @@ -190,6 +190,18 @@ public class MDCSetupTest extends MDCSetup { assertEquals("Bad Gateway", MDC.get(ONAPLogConstants.MDCs.RESPONSE_DESCRIPTION)); } + @Test + public void setMDCPartnerNameFromBasicAuth() { + MultivaluedMap headerMap = new MultivaluedHashMap<>(); + String value = "Basic dXNlcjpwYXNz"; // decodes to user:pass + headerMap.putSingle(HttpHeaders.AUTHORIZATION, value); + SimpleMap headers = new SimpleJaxrsHeadersMap(headerMap); + + setMDCPartnerName(headers); + + assertEquals("user", MDC.get(ONAPLogConstants.MDCs.PARTNER_NAME)); + } + @Test public void setMDCPartnerNameTest() { MultivaluedMap headerMap = new MultivaluedHashMap<>(); diff --git a/reference/logging-filter/logging-filter-base/src/test/resources/log4j2.properties b/reference/logging-filter/logging-filter-base/src/test/resources/log4j2.properties new file mode 100644 index 0000000..856bb26 --- /dev/null +++ b/reference/logging-filter/logging-filter-base/src/test/resources/log4j2.properties @@ -0,0 +1,7 @@ +appenders = console +appender.console.type = Console +appender.console.name = STDOUT + +rootLogger.level = TRACE +rootLogger.appenderRefs = stdout +rootLogger.appenderRef.stdout.ref = STDOUT \ No newline at end of file -- cgit 1.2.3-korg