diff options
8 files changed, 86 insertions, 59 deletions
diff --git a/reference/logging-filter/logging-filter-base/src/main/java/org/onap/logging/filter/base/AbstractAuditLogFilter.java b/reference/logging-filter/logging-filter-base/src/main/java/org/onap/logging/filter/base/AbstractAuditLogFilter.java index fd4e95a..ce2f448 100644 --- a/reference/logging-filter/logging-filter-base/src/main/java/org/onap/logging/filter/base/AbstractAuditLogFilter.java +++ b/reference/logging-filter/logging-filter-base/src/main/java/org/onap/logging/filter/base/AbstractAuditLogFilter.java @@ -26,7 +26,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.slf4j.MDC; -public abstract class AbstractAuditLogFilter<GenericRequest, GenericResponse> extends AbstractFilter { +public abstract class AbstractAuditLogFilter<GenericRequest, GenericResponse> extends MDCSetup { protected static final Logger logger = LoggerFactory.getLogger(AbstractAuditLogFilter.class); protected void pre(SimpleMap headers, GenericRequest request, HttpServletRequest httpServletRequest) { diff --git a/reference/logging-filter/logging-filter-base/src/main/java/org/onap/logging/filter/base/AbstractMetricLogFilter.java b/reference/logging-filter/logging-filter-base/src/main/java/org/onap/logging/filter/base/AbstractMetricLogFilter.java index 7857af9..79649a2 100644 --- a/reference/logging-filter/logging-filter-base/src/main/java/org/onap/logging/filter/base/AbstractMetricLogFilter.java +++ b/reference/logging-filter/logging-filter-base/src/main/java/org/onap/logging/filter/base/AbstractMetricLogFilter.java @@ -31,7 +31,7 @@ import org.slf4j.MDC; import org.slf4j.Marker; import org.slf4j.MarkerFactory; -public abstract class AbstractMetricLogFilter<Request, Response, RequestHeaders> extends AbstractFilter { +public abstract class AbstractMetricLogFilter<Request, Response, RequestHeaders> extends MDCSetup { protected static final Logger logger = LoggerFactory.getLogger(AbstractMetricLogFilter.class); private final String partnerName; private static final Marker INVOKE_RETURN = MarkerFactory.getMarker("INVOKE-RETURN"); diff --git a/reference/logging-filter/logging-filter-base/src/main/java/org/onap/logging/filter/base/AbstractServletFilter.java b/reference/logging-filter/logging-filter-base/src/main/java/org/onap/logging/filter/base/AbstractServletFilter.java new file mode 100644 index 0000000..bf165f9 --- /dev/null +++ b/reference/logging-filter/logging-filter-base/src/main/java/org/onap/logging/filter/base/AbstractServletFilter.java @@ -0,0 +1,58 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP - Logging + * ================================================================================ + * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.logging.filter.base; + +import java.util.Enumeration; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import javax.ws.rs.core.HttpHeaders; + +public abstract class AbstractServletFilter { + protected static final String REDACTED = "***REDACTED***"; + + protected String getSecureRequestHeaders(HttpServletRequest httpRequest) { + StringBuilder sb = new StringBuilder(); + String header; + for (Enumeration<String> e = httpRequest.getHeaderNames(); e.hasMoreElements();) { + header = e.nextElement(); + sb.append(header); + sb.append(":"); + if (header.equalsIgnoreCase(HttpHeaders.AUTHORIZATION)) { + sb.append(REDACTED); + } else { + sb.append(httpRequest.getHeader(header)); + } + sb.append(";"); + } + return sb.toString(); + } + + protected String formatResponseHeaders(HttpServletResponse response) { + StringBuilder sb = new StringBuilder(); + for (String headerName : response.getHeaderNames()) { + sb.append(headerName); + sb.append(":"); + sb.append(response.getHeader(headerName)); + sb.append(";"); + } + return sb.toString(); + } +} diff --git a/reference/logging-filter/logging-filter-base/src/main/java/org/onap/logging/filter/base/AbstractFilter.java b/reference/logging-filter/logging-filter-base/src/main/java/org/onap/logging/filter/base/MDCSetup.java index 13c88b0..93c16a8 100644 --- a/reference/logging-filter/logging-filter-base/src/main/java/org/onap/logging/filter/base/AbstractFilter.java +++ b/reference/logging-filter/logging-filter-base/src/main/java/org/onap/logging/filter/base/MDCSetup.java @@ -35,17 +35,17 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.slf4j.MDC; -public abstract class AbstractFilter { +public class MDCSetup { - protected static Logger logger = LoggerFactory.getLogger(AbstractFilter.class); + protected static Logger logger = LoggerFactory.getLogger(MDCSetup.class); private static final String INSTANCE_UUID = UUID.randomUUID().toString(); - protected void setInstanceID() { + public void setInstanceID() { MDC.put(ONAPLogConstants.MDCs.INSTANCE_UUID, INSTANCE_UUID); } - protected void setServerFQDN() { + public void setServerFQDN() { String serverFQDN = ""; InetAddress addr = null; try { @@ -59,7 +59,7 @@ public abstract class AbstractFilter { MDC.put(ONAPLogConstants.MDCs.SERVER_FQDN, serverFQDN); } - protected void setClientIPAddress(HttpServletRequest httpServletRequest) { + public void setClientIPAddress(HttpServletRequest httpServletRequest) { String clientIpAddress = ""; if (httpServletRequest != null) { // This logic is to avoid setting the client ip address to that of the load @@ -74,12 +74,12 @@ public abstract class AbstractFilter { MDC.put(ONAPLogConstants.MDCs.CLIENT_IP_ADDRESS, clientIpAddress); } - protected void setEntryTimeStamp() { + public void setEntryTimeStamp() { MDC.put(ONAPLogConstants.MDCs.ENTRY_TIMESTAMP, ZonedDateTime.now(ZoneOffset.UTC).format(DateTimeFormatter.ISO_INSTANT)); } - protected String getRequestId(SimpleMap headers) { + public String getRequestId(SimpleMap headers) { logger.trace("Checking X-ONAP-RequestID header for requestId."); String requestId = headers.get(ONAPLogConstants.Headers.REQUEST_ID); if (requestId != null && !requestId.isEmpty()) { @@ -108,21 +108,21 @@ public abstract class AbstractFilter { return UUID.randomUUID().toString(); } - protected void setInvocationId(SimpleMap headers) { + public void setInvocationId(SimpleMap headers) { String invocationId = headers.get(ONAPLogConstants.Headers.INVOCATION_ID); if (invocationId == null || invocationId.isEmpty()) invocationId = UUID.randomUUID().toString(); MDC.put(ONAPLogConstants.MDCs.INVOCATION_ID, invocationId); } - protected void setInvocationIdFromMDC() { + public void setInvocationIdFromMDC() { String invocationId = MDC.get(ONAPLogConstants.MDCs.INVOCATION_ID); if (invocationId == null || invocationId.isEmpty()) invocationId = UUID.randomUUID().toString(); MDC.put(ONAPLogConstants.MDCs.INVOCATION_ID, invocationId); } - protected void setMDCPartnerName(SimpleMap headers) { + 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()) { @@ -140,12 +140,12 @@ public abstract class AbstractFilter { MDC.put(ONAPLogConstants.MDCs.PARTNER_NAME, partnerName); } - protected void setLogTimestamp() { + public void setLogTimestamp() { MDC.put(ONAPLogConstants.MDCs.LOG_TIMESTAMP, ZonedDateTime.now(ZoneOffset.UTC).format(DateTimeFormatter.ISO_INSTANT)); } - protected void setElapsedTime() { + public void setElapsedTime() { DateTimeFormatter timeFormatter = DateTimeFormatter.ISO_ZONED_DATE_TIME; ZonedDateTime entryTimestamp = ZonedDateTime.parse(MDC.get(ONAPLogConstants.MDCs.ENTRY_TIMESTAMP), timeFormatter); @@ -155,7 +155,7 @@ public abstract class AbstractFilter { Long.toString(ChronoUnit.MILLIS.between(entryTimestamp, endTimestamp))); } - protected void setElapsedTimeInvokeTimestamp() { + public void setElapsedTimeInvokeTimestamp() { DateTimeFormatter timeFormatter = DateTimeFormatter.ISO_ZONED_DATE_TIME; ZonedDateTime entryTimestamp = ZonedDateTime.parse(MDC.get(ONAPLogConstants.MDCs.INVOKE_TIMESTAMP), timeFormatter); @@ -165,7 +165,7 @@ public abstract class AbstractFilter { Long.toString(ChronoUnit.MILLIS.between(entryTimestamp, endTimestamp))); } - protected void setResponseStatusCode(int code) { + public void setResponseStatusCode(int code) { String statusCode; if (Response.Status.Family.familyOf(code).equals(Response.Status.Family.SUCCESSFUL)) { statusCode = ONAPLogConstants.ResponseStatus.COMPLETE.toString(); @@ -177,11 +177,11 @@ public abstract class AbstractFilter { MDC.put(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE, statusCode); } - protected void setTargetEntity(ONAPComponents targetEntity) { + public void setTargetEntity(ONAPComponentsList targetEntity) { MDC.put(ONAPLogConstants.MDCs.TARGET_ENTITY, targetEntity.toString()); } - protected void clearClientMDCs() { + public void clearClientMDCs() { MDC.remove(ONAPLogConstants.MDCs.INVOCATION_ID); MDC.remove(ONAPLogConstants.MDCs.RESPONSE_DESCRIPTION); MDC.remove(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE); @@ -193,19 +193,19 @@ public abstract class AbstractFilter { MDC.remove(ONAPLogConstants.MDCs.ERROR_DESC); } - protected void setResponseDescription(int statusCode) { + public void setResponseDescription(int statusCode) { MDC.put(ONAPLogConstants.MDCs.RESPONSE_DESCRIPTION, Response.Status.fromStatusCode(statusCode).toString()); } - protected void setErrorCode(int statusCode) { + public void setErrorCode(int statusCode) { MDC.put(ONAPLogConstants.MDCs.ERROR_CODE, String.valueOf(statusCode)); } - protected void setErrorDesc(int statusCode) { + public void setErrorDesc(int statusCode) { MDC.put(ONAPLogConstants.MDCs.ERROR_DESC, Response.Status.fromStatusCode(statusCode).toString()); } - protected String getProperty(String property) { + public String getProperty(String property) { logger.info("Checking for system property [{}]", property); String propertyValue = System.getProperty(property); if (propertyValue == null || propertyValue.isEmpty()) { diff --git a/reference/logging-filter/logging-filter-base/src/main/java/org/onap/logging/filter/base/PayloadLoggingServletFilter.java b/reference/logging-filter/logging-filter-base/src/main/java/org/onap/logging/filter/base/PayloadLoggingServletFilter.java index 4b9cd1f..fa8533a 100644 --- a/reference/logging-filter/logging-filter-base/src/main/java/org/onap/logging/filter/base/PayloadLoggingServletFilter.java +++ b/reference/logging-filter/logging-filter-base/src/main/java/org/onap/logging/filter/base/PayloadLoggingServletFilter.java @@ -29,7 +29,6 @@ import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; -import java.util.Enumeration; import java.util.zip.GZIPInputStream; import javax.servlet.Filter; import javax.servlet.FilterChain; @@ -45,12 +44,10 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequestWrapper; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponseWrapper; -import javax.ws.rs.core.HttpHeaders; -public class PayloadLoggingServletFilter implements Filter { +public class PayloadLoggingServletFilter extends AbstractServletFilter implements Filter { private static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(PayloadLoggingServletFilter.class); - private static final String REDACTED = "***REDACTED***"; private static class ByteArrayServletStream extends ServletOutputStream { ByteArrayOutputStream baos; @@ -330,31 +327,4 @@ public class PayloadLoggingServletFilter implements Filter { return str.toString(); } - protected String getSecureRequestHeaders(HttpServletRequest httpRequest) { - StringBuilder sb = new StringBuilder(); - String header; - for (Enumeration<String> e = httpRequest.getHeaderNames(); e.hasMoreElements();) { - header = e.nextElement(); - sb.append(header); - sb.append(":"); - if (header.equalsIgnoreCase(HttpHeaders.AUTHORIZATION)) { - sb.append(REDACTED); - } else { - sb.append(httpRequest.getHeader(header)); - } - sb.append(";"); - } - return sb.toString(); - } - - protected String formatResponseHeaders(HttpServletResponse response) { - StringBuilder sb = new StringBuilder(); - for (String headerName : response.getHeaderNames()) { - sb.append(headerName); - sb.append(":"); - sb.append(response.getHeader(headerName)); - sb.append(";"); - } - return sb.toString(); - } } diff --git a/reference/logging-filter/logging-filter-base/src/test/java/org/onap/logging/filter/base/AbstractFilterTest.java b/reference/logging-filter/logging-filter-base/src/test/java/org/onap/logging/filter/base/MDCSetupTest.java index cfa74ad..31d8da6 100644 --- a/reference/logging-filter/logging-filter-base/src/test/java/org/onap/logging/filter/base/AbstractFilterTest.java +++ b/reference/logging-filter/logging-filter-base/src/test/java/org/onap/logging/filter/base/MDCSetupTest.java @@ -38,7 +38,7 @@ import org.onap.logging.ref.slf4j.ONAPLogConstants; import org.slf4j.MDC; @RunWith(MockitoJUnitRunner.class) -public class AbstractFilterTest extends AbstractFilter { +public class MDCSetupTest extends MDCSetup { @Mock private HttpServletRequest httpServletRequest; diff --git a/reference/logging-filter/logging-filter-spring/src/main/java/org/onap/logging/filter/spring/StatusLoggingInterceptor.java b/reference/logging-filter/logging-filter-spring/src/main/java/org/onap/logging/filter/spring/StatusLoggingInterceptor.java index 887e621..3401efa 100644 --- a/reference/logging-filter/logging-filter-spring/src/main/java/org/onap/logging/filter/spring/StatusLoggingInterceptor.java +++ b/reference/logging-filter/logging-filter-spring/src/main/java/org/onap/logging/filter/spring/StatusLoggingInterceptor.java @@ -26,16 +26,15 @@ import javax.servlet.http.HttpServletResponse; import javax.ws.rs.core.Context; import javax.ws.rs.core.Response; import javax.ws.rs.ext.Providers; -import org.onap.logging.filter.base.PayloadLoggingServletFilter; +import org.onap.logging.filter.base.AbstractServletFilter; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; -// TODO do we want this class to log message payloads? In the previous implementation it did not @Component -public class StatusLoggingInterceptor extends PayloadLoggingServletFilter implements HandlerInterceptor { +public class StatusLoggingInterceptor extends AbstractServletFilter implements HandlerInterceptor { private static final Logger logger = LoggerFactory.getLogger(StatusLoggingInterceptor.class); diff --git a/reference/logging-filter/logging-filter-spring/src/test/java/org/onap/logging/filter/spring/SpringClientFilterTest.java b/reference/logging-filter/logging-filter-spring/src/test/java/org/onap/logging/filter/spring/SpringClientFilterTest.java index 3836ab7..8a592d2 100644 --- a/reference/logging-filter/logging-filter-spring/src/test/java/org/onap/logging/filter/spring/SpringClientFilterTest.java +++ b/reference/logging-filter/logging-filter-spring/src/test/java/org/onap/logging/filter/spring/SpringClientFilterTest.java @@ -37,7 +37,7 @@ import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.Spy; import org.mockito.junit.MockitoJUnitRunner; -import org.onap.logging.filter.base.AbstractFilter; +import org.onap.logging.filter.base.MDCSetup; import org.onap.logging.filter.base.Constants; import org.onap.logging.ref.slf4j.ONAPLogConstants; import org.slf4j.MDC; @@ -50,7 +50,7 @@ import org.springframework.http.client.ClientHttpResponse; public class SpringClientFilterTest extends SpringClientFilter { @Mock - private AbstractFilter mdcSetup; + private MDCSetup mdcSetup; @Mock private ClientHttpResponse response; |