update getBasicAuthUserName
[logging-analytics.git] / reference / logging-filter / logging-filter-base / src / main / java / org / onap / logging / filter / base / MDCSetup.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - Logging
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.logging.filter.base;
22
23 import java.net.InetAddress;
24 import java.net.UnknownHostException;
25 import java.time.ZoneOffset;
26 import java.time.ZonedDateTime;
27 import java.time.format.DateTimeFormatter;
28 import java.time.temporal.ChronoUnit;
29 import java.util.Base64;
30 import java.util.UUID;
31 import javax.servlet.http.HttpServletRequest;
32 import javax.ws.rs.core.HttpHeaders;
33 import javax.ws.rs.core.Response;
34 import org.onap.logging.ref.slf4j.ONAPLogConstants;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37 import org.slf4j.MDC;
38
39 public class MDCSetup {
40
41     protected static Logger logger = LoggerFactory.getLogger(MDCSetup.class);
42
43     private static final String INSTANCE_UUID = UUID.randomUUID().toString();
44
45     public void setInstanceID() {
46         MDC.put(ONAPLogConstants.MDCs.INSTANCE_UUID, INSTANCE_UUID);
47     }
48
49     public void setServerFQDN() {
50         String serverFQDN = "";
51         InetAddress addr = null;
52         try {
53             addr = InetAddress.getLocalHost();
54             serverFQDN = addr.getCanonicalHostName();
55             MDC.put(ONAPLogConstants.MDCs.SERVER_IP_ADDRESS, addr.getHostAddress());
56         } catch (UnknownHostException e) {
57             logger.warn("Cannot Resolve Host Name");
58             serverFQDN = "";
59         }
60         MDC.put(ONAPLogConstants.MDCs.SERVER_FQDN, serverFQDN);
61     }
62
63     public void setClientIPAddress(HttpServletRequest httpServletRequest) {
64         String clientIpAddress = "";
65         if (httpServletRequest != null) {
66             // This logic is to avoid setting the client ip address to that of the load
67             // balancer in front of the application
68             String getForwadedFor = httpServletRequest.getHeader("X-Forwarded-For");
69             if (getForwadedFor != null) {
70                 clientIpAddress = getForwadedFor;
71             } else {
72                 clientIpAddress = httpServletRequest.getRemoteAddr();
73             }
74         }
75         MDC.put(ONAPLogConstants.MDCs.CLIENT_IP_ADDRESS, clientIpAddress);
76     }
77
78     public void setEntryTimeStamp() {
79         MDC.put(ONAPLogConstants.MDCs.ENTRY_TIMESTAMP,
80                 ZonedDateTime.now(ZoneOffset.UTC).format(DateTimeFormatter.ISO_INSTANT));
81     }
82
83     public String getRequestId(SimpleMap headers) {
84         logger.trace("Checking X-ONAP-RequestID header for requestId.");
85         String requestId = headers.get(ONAPLogConstants.Headers.REQUEST_ID);
86         if (requestId != null && !requestId.isEmpty()) {
87             return requestId;
88         }
89
90         logger.trace("No valid X-ONAP-RequestID header value. Checking X-RequestID header for requestId.");
91         requestId = headers.get(Constants.HttpHeaders.HEADER_REQUEST_ID);
92         if (requestId != null && !requestId.isEmpty()) {
93             return requestId;
94         }
95
96         logger.trace("No valid X-RequestID header value. Checking X-TransactionID header for requestId.");
97         requestId = headers.get(Constants.HttpHeaders.TRANSACTION_ID);
98         if (requestId != null && !requestId.isEmpty()) {
99             return requestId;
100         }
101
102         logger.trace("No valid X-TransactionID header value. Checking X-ECOMP-RequestID header for requestId.");
103         requestId = headers.get(Constants.HttpHeaders.ECOMP_REQUEST_ID);
104         if (requestId != null && !requestId.isEmpty()) {
105             return requestId;
106         }
107
108         logger.trace("No valid requestId headers. Generating requestId: {}", requestId);
109         return UUID.randomUUID().toString();
110     }
111
112     public void setInvocationId(SimpleMap headers) {
113         String invocationId = headers.get(ONAPLogConstants.Headers.INVOCATION_ID);
114         if (invocationId == null || invocationId.isEmpty())
115             invocationId = UUID.randomUUID().toString();
116         MDC.put(ONAPLogConstants.MDCs.INVOCATION_ID, invocationId);
117     }
118
119     public void setInvocationIdFromMDC() {
120         String invocationId = MDC.get(ONAPLogConstants.MDCs.INVOCATION_ID);
121         if (invocationId == null || invocationId.isEmpty())
122             invocationId = UUID.randomUUID().toString();
123         MDC.put(ONAPLogConstants.MDCs.INVOCATION_ID, invocationId);
124     }
125
126     public void setMDCPartnerName(SimpleMap headers) {
127         String partnerName = getMDCPartnerName(headers);
128         MDC.put(ONAPLogConstants.MDCs.PARTNER_NAME, partnerName);
129     }
130
131     protected String getMDCPartnerName(SimpleMap headers) {
132         String checkHeaderLogPattern = "Checking {} header to determine the value of {}";
133
134         logger.trace(checkHeaderLogPattern, HttpHeaders.AUTHORIZATION, ONAPLogConstants.MDCs.PARTNER_NAME);
135         String partnerName = getBasicAuthUserName(headers);
136         if (partnerName != null && !partnerName.isEmpty()) {
137             return partnerName;
138         }
139
140         logger.trace(checkHeaderLogPattern, ONAPLogConstants.Headers.PARTNER_NAME, ONAPLogConstants.MDCs.PARTNER_NAME);
141         partnerName = headers.get(ONAPLogConstants.Headers.PARTNER_NAME);
142         if (partnerName != null && !partnerName.isEmpty()) {
143             return partnerName;
144         }
145
146         logger.trace(checkHeaderLogPattern, HttpHeaders.USER_AGENT, ONAPLogConstants.MDCs.PARTNER_NAME);
147         partnerName = headers.get(HttpHeaders.USER_AGENT);
148         if (partnerName != null && !partnerName.isEmpty()) {
149             return partnerName;
150         }
151
152         logger.trace(checkHeaderLogPattern, Constants.HttpHeaders.CLIENT_ID, ONAPLogConstants.MDCs.PARTNER_NAME);
153         partnerName = headers.get(Constants.HttpHeaders.CLIENT_ID);
154         if (partnerName != null && !partnerName.isEmpty()) {
155             return partnerName;
156         }
157
158         logger.trace("{} value could not be determined, defaulting partnerName to {}.",
159                 ONAPLogConstants.MDCs.PARTNER_NAME, Constants.DefaultValues.UNKNOWN);
160         return Constants.DefaultValues.UNKNOWN;
161     }
162
163     public void setLogTimestamp() {
164         MDC.put(ONAPLogConstants.MDCs.LOG_TIMESTAMP,
165                 ZonedDateTime.now(ZoneOffset.UTC).format(DateTimeFormatter.ISO_INSTANT));
166     }
167
168     public void setElapsedTime() {
169         try {
170             DateTimeFormatter timeFormatter = DateTimeFormatter.ISO_ZONED_DATE_TIME;
171             ZonedDateTime entryTimestamp =
172                     ZonedDateTime.parse(MDC.get(ONAPLogConstants.MDCs.ENTRY_TIMESTAMP), timeFormatter);
173             ZonedDateTime endTimestamp =
174                     ZonedDateTime.parse(MDC.get(ONAPLogConstants.MDCs.LOG_TIMESTAMP), timeFormatter);
175
176             MDC.put(ONAPLogConstants.MDCs.ELAPSED_TIME,
177                     Long.toString(ChronoUnit.MILLIS.between(entryTimestamp, endTimestamp)));
178         } catch (Exception e) {
179             logger.warn("Unable to calculate elapsed time due to error: {}", e.getMessage());
180         }
181     }
182
183     public void setElapsedTimeInvokeTimestamp() {
184         try {
185             DateTimeFormatter timeFormatter = DateTimeFormatter.ISO_ZONED_DATE_TIME;
186             ZonedDateTime entryTimestamp =
187                     ZonedDateTime.parse(MDC.get(ONAPLogConstants.MDCs.INVOKE_TIMESTAMP), timeFormatter);
188             ZonedDateTime endTimestamp =
189                     ZonedDateTime.parse(MDC.get(ONAPLogConstants.MDCs.LOG_TIMESTAMP), timeFormatter);
190
191             MDC.put(ONAPLogConstants.MDCs.ELAPSED_TIME,
192                     Long.toString(ChronoUnit.MILLIS.between(entryTimestamp, endTimestamp)));
193         } catch (Exception e) {
194             logger.warn("Unable to calculate elapsed time due to error: {}", e.getMessage());
195         }
196     }
197
198     public void setResponseStatusCode(int code) {
199         String statusCode;
200         if (Response.Status.Family.familyOf(code).equals(Response.Status.Family.SUCCESSFUL)) {
201             statusCode = ONAPLogConstants.ResponseStatus.COMPLETE.toString();
202         } else {
203             statusCode = ONAPLogConstants.ResponseStatus.ERROR.toString();
204             setErrorCode(code);
205             setErrorDesc(code);
206         }
207         MDC.put(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE, statusCode);
208     }
209
210     public void setTargetEntity(ONAPComponentsList targetEntity) {
211         MDC.put(ONAPLogConstants.MDCs.TARGET_ENTITY, targetEntity.toString());
212     }
213
214     public void clearClientMDCs() {
215         MDC.remove(ONAPLogConstants.MDCs.INVOCATION_ID);
216         MDC.remove(ONAPLogConstants.MDCs.RESPONSE_DESCRIPTION);
217         MDC.remove(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE);
218         MDC.remove(ONAPLogConstants.MDCs.RESPONSE_CODE);
219         MDC.remove(ONAPLogConstants.MDCs.TARGET_ENTITY);
220         MDC.remove(ONAPLogConstants.MDCs.TARGET_SERVICE_NAME);
221         MDC.remove(ONAPLogConstants.MDCs.INVOKE_TIMESTAMP);
222         MDC.remove(ONAPLogConstants.MDCs.ERROR_CODE);
223         MDC.remove(ONAPLogConstants.MDCs.ERROR_DESC);
224     }
225
226     public void setResponseDescription(int statusCode) {
227         MDC.put(ONAPLogConstants.MDCs.RESPONSE_DESCRIPTION, Response.Status.fromStatusCode(statusCode).toString());
228     }
229
230     public void setErrorCode(int statusCode) {
231         MDC.put(ONAPLogConstants.MDCs.ERROR_CODE, String.valueOf(statusCode));
232     }
233
234     public void setErrorDesc(int statusCode) {
235         MDC.put(ONAPLogConstants.MDCs.ERROR_DESC, Response.Status.fromStatusCode(statusCode).toString());
236     }
237
238     public String getProperty(String property) {
239         String propertyValue = System.getProperty(property);
240         if (propertyValue == null || propertyValue.isEmpty()) {
241             propertyValue = System.getenv(property);
242             if (propertyValue == null || propertyValue.isEmpty()) {
243                 propertyValue = Constants.DefaultValues.UNKNOWN;
244             }
245         }
246         return propertyValue;
247     }
248
249     protected String getBasicAuthUserName(SimpleMap headers) {
250         String encodedAuthorizationValue = headers.get(HttpHeaders.AUTHORIZATION);
251         if (encodedAuthorizationValue != null && encodedAuthorizationValue.startsWith("Basic")) {
252             try {
253                 // This will strip the word Basic and single space
254                 encodedAuthorizationValue = encodedAuthorizationValue.substring(6);
255                 byte[] decodedBytes = Base64.getDecoder().decode(encodedAuthorizationValue);
256                 String decodedString = new String(decodedBytes);
257                 int idx = decodedString.indexOf(':');
258                 return decodedString.substring(0, idx);
259             } catch (IllegalArgumentException e) {
260                 logger.error("could not decode basic auth value " + encodedAuthorizationValue, e);
261             }
262         }
263         return null;
264     }
265 }