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