AAI-common sonar fixes
[aai/aai-common.git] / aai-core / src / main / java / org / onap / aai / logging / CNName.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 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.aai.logging;
22
23 import static java.util.Base64.getDecoder;
24
25 import ch.qos.logback.access.pattern.AccessConverter;
26 import ch.qos.logback.access.spi.IAccessEvent;
27
28 import com.att.eelf.configuration.EELFLogger;
29 import com.att.eelf.configuration.EELFManager;
30
31 import java.security.cert.X509Certificate;
32
33 import javax.security.auth.x500.X500Principal;
34 import javax.servlet.http.HttpServletRequest;
35
36 public class CNName extends AccessConverter {
37     private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(CNName.class);
38
39     /**
40      * Converts access events to String response codes
41      * 
42      * @param accessEvent the IAccessEvent
43      */
44     public String convert(IAccessEvent accessEvent) {
45         if (!isStarted()) {
46             return "INACTIVE_HEADER_CONV";
47         }
48
49         String cipherSuite = (String) accessEvent.getRequest().getAttribute("javax.servlet.request.cipher_suite");
50         String authUser = null;
51         if (cipherSuite != null) {
52             try {
53                 X509Certificate certChain[] = (X509Certificate[]) accessEvent.getRequest()
54                         .getAttribute("javax.servlet.request.X509Certificate");
55                 if (certChain == null || certChain.length == 0) {
56
57                     HttpServletRequest request = accessEvent.getRequest();
58
59                     String authorization = request.getHeader("Authorization");
60
61                     // Set the auth user to "-" so if the authorization header is not found
62                     // Or if the decoded basic auth credentials are not found in the format required
63                     // it should return "-"
64                     // If the decoded string is in the right format, find the index of ":"
65                     // Then get the substring of the starting point to the colon not including the colon
66
67                     authUser = "-";
68
69                     if (authorization != null && authorization.startsWith("Basic ")) {
70                         String credentials = authorization.replace("Basic ", "");
71                         byte[] userCredentials = getDecoder().decode(credentials.getBytes("utf-8"));
72                         credentials = new String(userCredentials);
73
74                         int codePoint = credentials.indexOf(':');
75
76                         if (codePoint != -1) {
77                             authUser = credentials.substring(0, codePoint);
78                         }
79
80                     }
81
82                     return authUser;
83
84                 } else {
85                     X509Certificate clientCert = certChain[0];
86                     X500Principal subjectDN = clientCert.getSubjectX500Principal();
87                     authUser = subjectDN.toString();
88                     return authUser;
89                 }
90             } catch (Exception e) {
91                 LOGGER.error(e.getMessage(), e);
92                 return "-";
93             }
94         } else {
95             return "-";
96         }
97     }
98
99 }