diff options
author | 2019-09-09 20:55:15 +0000 | |
---|---|---|
committer | 2019-09-19 12:57:18 +0000 | |
commit | d815d848cd83cfb9180286af5bc8a7516f4eafe9 (patch) | |
tree | 5479796e4553cea9ea98d15450d9bffbcc41774a /filters/provider/src/main/java | |
parent | 5439ed46b5392671137d4f73445f075e47938191 (diff) |
update filters
replace legacy filters with common filters
Issue-ID: CCSDK-1698
Signed-off-by: Smokowski, Kevin (ks6305) <kevin.smokowski@att.com>
Change-Id: Id6b1f003f19105956d221d5bfd31390ac096c9bc
Diffstat (limited to 'filters/provider/src/main/java')
6 files changed, 47 insertions, 623 deletions
diff --git a/filters/provider/src/main/java/org/onap/ccsdk/sli/core/filters/ControllerAuditLogFilter.java b/filters/provider/src/main/java/org/onap/ccsdk/sli/core/filters/ControllerAuditLogFilter.java new file mode 100644 index 000000000..7805e365d --- /dev/null +++ b/filters/provider/src/main/java/org/onap/ccsdk/sli/core/filters/ControllerAuditLogFilter.java @@ -0,0 +1,40 @@ +package org.onap.ccsdk.sli.core.filters; + +import javax.servlet.http.HttpServletRequest; +import org.onap.logging.filter.base.AuditLogServletFilter; +import org.onap.logging.ref.slf4j.ONAPLogConstants; +import org.slf4j.MDC; + +public class ControllerAuditLogFilter extends AuditLogServletFilter { + private static final String MDC_HTTP_METHOD_KEY = "HttpMethod"; + + @Override + protected void additionalPreHandling(HttpServletRequest httpServletRequest) { + // Don't overwrite service instance id if it was set outside of this automated method + if (MDC.get(ONAPLogConstants.MDCs.SERVICE_INSTANCE_ID) == null) { + String serviceInstanceId = getServiceInstanceId(httpServletRequest.getPathInfo()); + if (serviceInstanceId != null) { + MDC.put(ONAPLogConstants.MDCs.SERVICE_INSTANCE_ID, serviceInstanceId); + } + } + MDC.put(MDC_HTTP_METHOD_KEY, httpServletRequest.getMethod()); + } + + // restconf URLs follow a pattern, this method attempts to extract the service instance id according to that pattern + protected String getServiceInstanceId(String path) { + int idx = path.indexOf("service-list"); + if (idx != -1) { + // chomp off service-list/ + String str = path.substring(idx + 13); + idx = str.indexOf("/"); + //if there is another forward slash with more information chomp it off + if (idx != -1) { + return str.substring(0, idx); + } else { + return str; + } + } + return null; + } + +} diff --git a/filters/provider/src/main/java/org/onap/ccsdk/sli/core/filters/ControllerPayloadLoggingFilter.java b/filters/provider/src/main/java/org/onap/ccsdk/sli/core/filters/ControllerPayloadLoggingFilter.java new file mode 100644 index 000000000..3f9f93fc5 --- /dev/null +++ b/filters/provider/src/main/java/org/onap/ccsdk/sli/core/filters/ControllerPayloadLoggingFilter.java @@ -0,0 +1,7 @@ +package org.onap.ccsdk.sli.core.filters; + +import org.onap.logging.filter.base.PayloadLoggingServletFilter; + +public class ControllerPayloadLoggingFilter extends PayloadLoggingServletFilter { + +} diff --git a/filters/provider/src/main/java/org/onap/ccsdk/sli/core/filters/FiltersService.java b/filters/provider/src/main/java/org/onap/ccsdk/sli/core/filters/FiltersService.java deleted file mode 100644 index 9587e8d96..000000000 --- a/filters/provider/src/main/java/org/onap/ccsdk/sli/core/filters/FiltersService.java +++ /dev/null @@ -1,4 +0,0 @@ -package org.onap.ccsdk.sli.core.filters; - -public interface FiltersService { -} diff --git a/filters/provider/src/main/java/org/onap/ccsdk/sli/core/filters/FiltersServiceImpl.java b/filters/provider/src/main/java/org/onap/ccsdk/sli/core/filters/FiltersServiceImpl.java deleted file mode 100644 index 12370ca9a..000000000 --- a/filters/provider/src/main/java/org/onap/ccsdk/sli/core/filters/FiltersServiceImpl.java +++ /dev/null @@ -1,33 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP : CCSDK - * ================================================================================ - * Copyright (C) 2017 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.ccsdk.sli.core.filters; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class FiltersServiceImpl implements FiltersService { - private static final Logger LOG = LoggerFactory.getLogger(FiltersServiceImpl.class); - - public FiltersServiceImpl() { - LOG.debug("Registering {}", FiltersServiceImpl.class.getName()); - } -} diff --git a/filters/provider/src/main/java/org/onap/ccsdk/sli/core/filters/LogFilter.java b/filters/provider/src/main/java/org/onap/ccsdk/sli/core/filters/LogFilter.java deleted file mode 100644 index 4481ab904..000000000 --- a/filters/provider/src/main/java/org/onap/ccsdk/sli/core/filters/LogFilter.java +++ /dev/null @@ -1,223 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP : CCSDK - * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights - * reserved. - * ================================================================================ - * Modifications Copyright (C) 2018 IBM. - * ================================================================================ - * 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.ccsdk.sli.core.filters; - -import java.io.IOException; -import java.util.UUID; - -import javax.servlet.Filter; -import javax.servlet.FilterChain; -import javax.servlet.FilterConfig; -import javax.servlet.ServletException; -import javax.servlet.ServletRequest; -import javax.servlet.ServletResponse; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; - -import org.apache.commons.codec.binary.Base64; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.slf4j.MDC; - -import java.text.DateFormat; -import java.text.SimpleDateFormat; -import java.util.Date; -import java.util.TimeZone; - - - -/** - * Logs IN request according ECOMP Logging Guidelines at https://tspace.web.att.com/viewer/app/lcfiles/ae5f7751-39da-4c6b-8a83-5836c8c815e1/content - */ -public class LogFilter implements Filter { - - //X-ECOMP is shared between audit and metric - public static final String BEGIN_TIMESTAMP = "AUDIT-BeginTimestamp"; - public static final String END_TIMESTAMP = "AUDIT-EndTimestamp"; - public static final String REQUEST_ID = "X-ECOMP-RequestID"; - public static final String SERVICE_INSTANCE = "X-ECOMP-ServiceInstanceID"; - public static final String THREAD_ID ="X-ECOMP-ThreadId"; //optional - public static final String PHYSICAL_SERVER_NAME="X-ECOMP-PhysicalServerName"; //optional - public static final String SERVICE_NAME="X-ECOMP-ServiceName"; - public static final String PARTNER_NAME="X-ECOMP-PartnerName"; - public static final String STATUS_CODE="AUDIT-StatusCode"; - public static final String RESP_CODE="AUDIT-ResponseCode"; - public static final String RESP_DESC="AUDIT-ResponseDescription"; - public static final String INSTANCE_UUID="AUDIT-InstanceUUID"; - public static final String CATEGORY="AUDIT-INFO"; - public static final String SEVERITY ="AUDIT-Severity"; //optional - public static final String SERVER_IP="AUDIT-ServerIP"; //by chef node['ip'] - public static final String ELAPSED_TIME="AUDIT-ElapsedTime"; - public static final String SERVER_HOST="AUDIT-Server";//by chef node['fqdn'] - public static final String CLIENT_IP="AUDIT-ClientIPaddress"; - public static final String CLASS="AUDIT-Classname"; //optional - public static final String UNUSED="AUDIT-Unused"; //empty - public static final String PROCESS_KEY="AUDIT-ProcessKey"; //optional - public static final String CUST_1="AUDIT-CustomField1";//optional - public static final String CUST_2="AUDIT-CustomField2"; //optional - public static final String CUST_3="AUDIT-CustomField3"; //optional - public static final String CUST_4="AUDIT-CustomField4"; //optional - public static final String DETAIL_MESSAGE="AUDIT-DetailMessage";//optional - - - private static final Logger log = LoggerFactory.getLogger(LogFilter.class); - private static final Logger AUDIT = LoggerFactory.getLogger("org.onap.ccsdk.sli.core.filters.audit"); - @Override - public void destroy() { - // this method does nothing - } - - @Override - public void doFilter(final ServletRequest request, final ServletResponse response, - final FilterChain filterChain) throws IOException, ServletException { - - long startTime = System.currentTimeMillis(); - try { - - if ( request != null && request instanceof HttpServletRequest ) { - pre((HttpServletRequest)request); - } - filterChain.doFilter(request, response); - - - } finally { - - if (request != null && request instanceof HttpServletRequest ) { - post((HttpServletRequest)request,(HttpServletResponse)response,startTime); - } - MDC.clear(); - } - - } - - @Override - public void init(FilterConfig filterConfig) throws ServletException { - // this method does nothing - } - - - - private void pre(HttpServletRequest request) { - - UUID uuid = UUID.randomUUID(); - // check if uuid is in header X-ECOMP-RequestID - - String ecompUUID = request.getHeader(REQUEST_ID); - - if (ecompUUID != null && ecompUUID.length() > 0) { - try { - uuid = UUID.fromString(ecompUUID); - log.info("UUID is ECOMP UUID " + uuid.toString()); - } catch (Exception ex){ - log.warn("Failed to convert ECOMP UUID to java.util.UUID format:" + ecompUUID,ex); - } - } - MDC.put(REQUEST_ID, uuid.toString()); - - String userName="unknown"; - - - // going directly after Authorization header - if (request.getHeader("Authorization") != null) { - String authzHeader = request.getHeader("Authorization"); - String usernameAndPassword = new String(Base64.decodeBase64(authzHeader.substring(6).getBytes())); - - int userNameIndex = usernameAndPassword.indexOf(':'); - String username = usernameAndPassword.substring(0, userNameIndex); - userName = username; - - } - - - MDC.put(PARTNER_NAME, userName); - //just to initilaze for metric logger (outbound calls) - MDC.put("X-ECOMP-TargetEntity",""); - MDC.put("X-ECOMP-TargetServiceName",""); - - MDC.put(SERVICE_NAME,request.getRequestURL().toString()); - MDC.put(SERVICE_INSTANCE,""); - - } - - - private void post(HttpServletRequest request,HttpServletResponse response,long startTime) { - - MDC.put(BEGIN_TIMESTAMP,asIso8601(startTime)); - MDC.put(END_TIMESTAMP,asIso8601(System.currentTimeMillis())); - MDC.put(SERVICE_NAME,request.getRequestURL().toString()); - int idx = request.getPathInfo().lastIndexOf(':'); - String instance = ""; - if ( idx != -1 ) { - instance = request.getPathInfo().substring(idx+1); - } - MDC.put(SERVICE_INSTANCE,instance); - MDC.put(THREAD_ID,""); - MDC.put(PHYSICAL_SERVER_NAME,""); - if ( response.getStatus() >= 400 ) { - MDC.put(STATUS_CODE,"ERROR"); - } else { - MDC.put(STATUS_CODE,"COMPLETE"); - } - - MDC.put(RESP_CODE, Integer.toString(response.getStatus())); - MDC.put(RESP_DESC,""); - MDC.put(INSTANCE_UUID,""); - MDC.put(CATEGORY,""); - MDC.put(SEVERITY,""); - //MDC.put(SERVER_IP,""); //by chef - MDC.put(ELAPSED_TIME,Long.toString(System.currentTimeMillis() - startTime)); - //MDC.put(SERVER_HOST,""); //by chef - String forwardedHost = request.getHeader("X-Forwarded-For"); - if (forwardedHost != null) { - MDC.put(CLIENT_IP, forwardedHost); - } - else{ - MDC.put(CLIENT_IP,request.getRemoteHost()); - } - MDC.put(CLASS,""); - MDC.put(UNUSED,""); - MDC.put(PROCESS_KEY,""); - MDC.put(CUST_1,""); - MDC.put(CUST_2,""); - MDC.put(CUST_3,""); - MDC.put(CUST_4,""); - MDC.put(DETAIL_MESSAGE,request.getMethod()); - - AUDIT.info(""); - } - - private String asIso8601(Date date) { - TimeZone tz = TimeZone.getTimeZone("UTC"); - DateFormat df = new SimpleDateFormat("yyy-MM-dd'T'HH:mm:ss.SS'+00:00'"); - df.setTimeZone(tz); - return df.format(date); - } - - private String asIso8601(long tsInMillis) { - return asIso8601(new Date(tsInMillis)); - } - - -} diff --git a/filters/provider/src/main/java/org/onap/ccsdk/sli/core/filters/RequestResponseLoggingFilter.java b/filters/provider/src/main/java/org/onap/ccsdk/sli/core/filters/RequestResponseLoggingFilter.java deleted file mode 100644 index e24a75274..000000000 --- a/filters/provider/src/main/java/org/onap/ccsdk/sli/core/filters/RequestResponseLoggingFilter.java +++ /dev/null @@ -1,363 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP : CCSDK - * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights - * reserved. - * ================================================================================ - * Modifications Copyright (C) 2018 IBM. - * ================================================================================ - * 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.ccsdk.sli.core.filters; - -import java.io.BufferedReader; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -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; -import javax.servlet.FilterConfig; -import javax.servlet.ReadListener; -import javax.servlet.ServletException; -import javax.servlet.ServletInputStream; -import javax.servlet.ServletOutputStream; -import javax.servlet.ServletRequest; -import javax.servlet.ServletResponse; -import javax.servlet.WriteListener; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletRequestWrapper; -import javax.servlet.http.HttpServletResponse; -import javax.servlet.http.HttpServletResponseWrapper; - -public class RequestResponseLoggingFilter implements Filter { - - private static org.slf4j.Logger log = - org.slf4j.LoggerFactory.getLogger("org.onap.ccsdk.sli.core.filters.request.response"); - - private static class ByteArrayServletStream extends ServletOutputStream { - - ByteArrayOutputStream baos; - - ByteArrayServletStream(ByteArrayOutputStream baos) { - this.baos = baos; - } - - @Override - public void write(int param) throws IOException { - baos.write(param); - } - - @Override - public boolean isReady() { - return true; - } - - @Override - public void setWriteListener(WriteListener arg0) { - // this method does nothing - - } - } - - private static class ByteArrayPrintWriter extends PrintWriter { - private ByteArrayOutputStream baos; - private int errorCode = -1; - private String errorMsg = ""; - private boolean errored = false; - - public ByteArrayPrintWriter(ByteArrayOutputStream out) { - super(out); - this.baos = out; - } - - public ServletOutputStream getStream() { - return new ByteArrayServletStream(baos); - } - - public Boolean hasErrored() { - return errored; - } - public int getErrorCode() { - return errorCode; - } - public String getErrorMsg() { - return errorMsg; - } - - public void setError(int code) { - errorCode = code; - errored = true; - } - - public void setError(int code, String msg) { - errorMsg = msg; - errorCode = code; - errored = true; - } - - } - - private class BufferedServletInputStream extends ServletInputStream { - - ByteArrayInputStream bais; - - public BufferedServletInputStream(ByteArrayInputStream bais) { - this.bais = bais; - } - - @Override - public int available() { - return bais.available(); - } - - @Override - public int read() { - return bais.read(); - } - - @Override - public int read(byte[] buf, int off, int len) { - return bais.read(buf, off, len); - } - - @Override - public boolean isFinished() { - return available() < 1; - } - - @Override - public boolean isReady() { - return true; - } - - @Override - public void setReadListener(ReadListener arg0) { - // this method does nothing - } - - } - - private class BufferedRequestWrapper extends HttpServletRequestWrapper { - - ByteArrayInputStream bais; - - ByteArrayOutputStream baos; - - BufferedServletInputStream bsis; - - byte[] buffer; - - public BufferedRequestWrapper(HttpServletRequest req) throws IOException { - super(req); - - InputStream is = req.getInputStream(); - baos = new ByteArrayOutputStream(); - byte[] buf = new byte[1024]; - int letti; - while ((letti = is.read(buf)) > 0) { - baos.write(buf, 0, letti); - } - buffer = baos.toByteArray(); - - } - - @Override - public ServletInputStream getInputStream() { - try { - bais = new ByteArrayInputStream(buffer); - bsis = new BufferedServletInputStream(bais); - } catch (Exception ex) { - log.error("Exception in getInputStream", ex); - } - - return bsis; - } - - public byte[] getBuffer() { - return buffer; - } - - } - - @Override - public void init(FilterConfig filterConfig) throws ServletException { - // this method does nothing - } - - @Override - public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) - throws IOException, ServletException { - - final HttpServletRequest httpRequest = (HttpServletRequest) servletRequest; - BufferedRequestWrapper bufferedRequest = new BufferedRequestWrapper(httpRequest); - - StringBuilder requestHeaders = new StringBuilder("REQUEST|"); - requestHeaders.append(httpRequest.getMethod()); - requestHeaders.append(":"); - requestHeaders.append(httpRequest.getRequestURL().toString()); - requestHeaders.append("|"); - String header; - for (Enumeration<String> e = httpRequest.getHeaderNames(); e.hasMoreElements();) { - header = e.nextElement(); - requestHeaders.append(header); - requestHeaders.append(":"); - requestHeaders.append(httpRequest.getHeader(header)); - requestHeaders.append(";"); - - } - log.info(requestHeaders.toString()); - - log.info("REQUEST BODY|" + new String(bufferedRequest.getBuffer())); - - final HttpServletResponse response = (HttpServletResponse) servletResponse; - final ByteArrayOutputStream baos = new ByteArrayOutputStream(); - final ByteArrayPrintWriter pw = new ByteArrayPrintWriter(baos); - - HttpServletResponse wrappedResp = new HttpServletResponseWrapper(response) { - @Override - public PrintWriter getWriter() { - return pw; - } - - @Override - public ServletOutputStream getOutputStream() { - return pw.getStream(); - } - - @Override - public void sendError(int sc) throws IOException { - super.sendError(sc); - pw.setError(sc); - - } - @Override - public void sendError(int sc, String msg) throws IOException { - super.sendError(sc, msg); - pw.setError(sc, msg); - } - }; - - try { - filterChain.doFilter(bufferedRequest, wrappedResp); - } catch (Exception e) { - log.error("Chain Exception", e); - throw e; - } finally { - try { - byte[] bytes = baos.toByteArray(); - StringBuilder responseHeaders = new StringBuilder("RESPONSE HEADERS|"); - - for (String headerName : response.getHeaderNames()) { - responseHeaders.append(headerName); - responseHeaders.append(":"); - responseHeaders.append(response.getHeader(headerName)); - responseHeaders.append(";"); - } - responseHeaders.append("Status:"); - responseHeaders.append(response.getStatus()); - responseHeaders.append(";IsCommited:" + wrappedResp.isCommitted()); - - log.info(responseHeaders.toString()); - - if ("gzip".equals(response.getHeader("Content-Encoding"))) { - - log.info("UNGZIPED RESPONSE BODY|" + decompressGZIPByteArray(bytes)); - - } else { - - log.info("RESPONSE BODY|" + new String(bytes)); - } - - if (pw.hasErrored()) { - log.info("ERROR RESPONSE|" + pw.getErrorCode() + ":" + pw.getErrorMsg()); - } else { - if (!wrappedResp.isCommitted()){ - response.getOutputStream().write(bytes); - response.getOutputStream().flush(); - } - } - - } catch (Exception e) { - log.error("Exception in response filter", e); - } - - } - } - - @Override - public void destroy() { - // this method does nothing - } - - private String decompressGZIPByteArray(byte[] bytes) { - - BufferedReader in = null; - InputStreamReader inR = null; - ByteArrayInputStream byteS = null; - GZIPInputStream gzS = null; - StringBuilder str = new StringBuilder(); - try { - byteS = new ByteArrayInputStream(bytes); - gzS = new GZIPInputStream(byteS); - inR = new InputStreamReader(gzS); - in = new BufferedReader(inR); - - if (in != null) { - - String content; - - while ((content = in.readLine()) != null) { - str.append(content); - } - } - - } catch (Exception e) { - log.error("Failed get read GZIPInputStream", e); - } finally { - - if (byteS != null) - try { - byteS.close(); - } catch (IOException e1) { - log.error("Failed to close ByteStream", e1); - } - if (gzS != null) - try { - gzS.close(); - } catch (IOException e2) { - log.error("Failed to close GZStream", e2); - } - if (inR != null) - try { - inR.close(); - } catch (IOException e3) { - log.error("Failed to close InputReader", e3); - } - if (in != null) - try { - in.close(); - } catch (IOException e) { - log.error("Failed to close BufferedReader", e); - } - } - return str.toString(); - } -} |