Merge "[AAI] Fix doc config files"
[aai/aai-common.git] / aai-aaf-auth / src / main / java / org / onap / aai / aaf / filters / AafCertFilter.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  * <p>
11  * http://www.apache.org/licenses/LICENSE-2.0
12  * <p>
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.aaf.filters;
22
23 import java.io.IOException;
24
25 import javax.servlet.FilterChain;
26 import javax.servlet.ServletException;
27 import javax.servlet.http.HttpServletRequest;
28 import javax.servlet.http.HttpServletResponse;
29
30 import org.onap.aaf.cadi.PropAccess;
31 import org.onap.aaf.cadi.filter.CadiFilter;
32 import org.onap.aai.aaf.auth.AafRequestFilter;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35 import org.springframework.beans.factory.annotation.Autowired;
36 import org.springframework.beans.factory.annotation.Value;
37 import org.springframework.boot.web.servlet.filter.OrderedRequestContextFilter;
38 import org.springframework.context.annotation.Profile;
39 import org.springframework.context.annotation.PropertySource;
40 import org.springframework.stereotype.Component;
41
42 /**
43  * AAF with client cert authentication filter
44  */
45
46 @Component
47 @Profile(AafProfiles.AAF_CERT_AUTHENTICATION)
48 @PropertySource(value = "file:${CONFIG_HOME}/aaf/permissions.properties", ignoreResourceNotFound = true)
49 @PropertySource(value = "file:${server.local.startpath}/aaf/permissions.properties", ignoreResourceNotFound = true)
50 public class AafCertFilter extends OrderedRequestContextFilter {
51
52     private static final Logger LOGGER = LoggerFactory.getLogger(AafCertFilter.class);
53
54     String aafUserChainPattern;
55
56     private final CadiFilter cadiFilter;
57
58     private final CadiProps cadiProps;
59
60     @Autowired
61     public AafCertFilter(@Value("${aaf.userchain.pattern}") String aafUserChainPattern, CadiProps cadiProps)
62             throws IOException, ServletException {
63
64         this.aafUserChainPattern = aafUserChainPattern;
65         this.cadiProps = cadiProps;
66         cadiFilter = new CadiFilter(new PropAccess((level, element) -> {
67             switch (level) {
68                 case DEBUG:
69                     LOGGER.debug(buildMsg(element));
70                     break;
71                 case INFO:
72                 case AUDIT:
73                     LOGGER.info(buildMsg(element));
74                     break;
75                 case WARN:
76                     LOGGER.warn(buildMsg(element));
77                     break;
78                 case ERROR:
79                     LOGGER.error(buildMsg(element));
80                     break;
81                 case INIT:
82                     LOGGER.info(buildMsg(element));
83                     break;
84                 case TRACE:
85                     LOGGER.trace(buildMsg(element));
86                     break;
87                 case NONE:
88                     break;
89             }
90         }, new String[] {"cadi_prop_files=" + cadiProps.getCadiFileName()}));
91         this.setOrder(FilterPriority.AAF_CERT_AUTHENTICATION.getPriority());
92     }
93
94     @Override
95     protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
96             throws IOException, ServletException {
97         AafRequestFilter.authenticationFilter(request, response, filterChain, cadiFilter, cadiProps.getCadiProperties(),
98                 aafUserChainPattern);
99     }
100
101     private String buildMsg(Object[] objects) {
102         StringBuilder sb = new StringBuilder();
103         boolean first = true;
104         for (Object o : objects) {
105             if (first) {
106                 first = false;
107             } else {
108                 sb.append(' ');
109             }
110             sb.append(o.toString());
111         }
112         return (sb.toString());
113     }
114 }