Catalog alignment
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / filters / BeCadiServletFilter.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2020 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.openecomp.sdc.be.filters;
22
23 import org.onap.aaf.cadi.Access;
24 import org.onap.aaf.cadi.PropAccess;
25 import org.onap.aaf.cadi.config.Config;
26 import org.onap.aaf.cadi.filter.CadiFilter;
27 import org.openecomp.sdc.be.components.impl.CADIHealthCheck;
28 import org.openecomp.sdc.be.config.CadiFilterParams;
29 import org.openecomp.sdc.be.config.ConfigurationManager;
30 import org.openecomp.sdc.be.impl.WebAppContextWrapper;
31 import org.openecomp.sdc.common.api.Constants;
32 import org.openecomp.sdc.common.api.HealthCheckInfo;
33 import org.openecomp.sdc.common.log.wrappers.Logger;
34 import org.openecomp.sdc.common.util.ThreadLocalsHolder;
35 import org.springframework.web.context.WebApplicationContext;
36
37 import javax.annotation.Priority;
38 import javax.servlet.FilterChain;
39 import javax.servlet.FilterConfig;
40 import javax.servlet.ServletContext;
41 import javax.servlet.ServletException;
42 import javax.servlet.ServletRequest;
43 import javax.servlet.ServletResponse;
44 import javax.servlet.http.HttpServletRequest;
45 import java.io.IOException;
46 import java.util.function.Supplier;
47
48 @Priority(2)
49 public class BeCadiServletFilter extends CadiFilter {
50
51     private static final Logger log = Logger.getLogger(BeCadiServletFilter.class);
52     private ConfigurationManager configurationManager = ConfigurationManager.getConfigurationManager();
53     private static final String BE_CADI_SERVICE_FILTER = "BeCadiServletFilter: ";
54
55
56     public BeCadiServletFilter() {
57         super();
58         log.debug(BE_CADI_SERVICE_FILTER);
59     }
60
61     /**
62      * This constructor to be used when directly constructing and placing in HTTP Engine
63      *
64      * @param access
65      * @param moreTafLurs
66      * @throws ServletException
67      */
68     public BeCadiServletFilter(Access access, Object... moreTafLurs) throws ServletException {
69         super(access, moreTafLurs);
70         log.debug(BE_CADI_SERVICE_FILTER);
71     }
72
73
74     /**
75      * Use this to pass in a PreContructed CADI Filter, but with initializing... let Servlet do it
76      *
77      * @param init
78      * @param access
79      * @param moreTafLurs
80      * @throws ServletException
81      */
82     public BeCadiServletFilter(boolean init, PropAccess access, Object... moreTafLurs) throws ServletException {
83
84         super(init, access, moreTafLurs);
85         log.debug(BE_CADI_SERVICE_FILTER);
86     }
87
88     private void checkIfNullProperty(String key, String value) {
89         /* When value is null, so not defined in application.properties
90            set nothing in System properties */
91         if (value != null) {
92             /* Ensure that any properties already defined in System.prop by JVM params
93                 won't be overwritten by Spring application.properties values */
94             System.setProperty(key, System.getProperty(key, value));
95         }
96     }
97
98     @Override
99     public void init(FilterConfig filterConfig) throws ServletException {
100
101         // set some properties in System so that Cadi filter will find its config
102         // The JVM values set will always overwrite the Spring ones.
103         CadiFilterParams cadiFilterParams = configurationManager.getConfiguration().getCadiFilterParams();
104         checkIfNullProperty(Config.HOSTNAME, cadiFilterParams.getHostname());
105         log.debug("BeCadiServletFilter: HOSTNAME", cadiFilterParams.getHostname());
106
107         checkIfNullProperty(Config.CADI_KEYFILE, cadiFilterParams.getCadi_keyfile());
108         checkIfNullProperty(Config.CADI_LOGLEVEL, cadiFilterParams.getCadi_loglevel());
109
110
111         checkIfNullProperty(Config.CADI_LATITUDE, cadiFilterParams.getAFT_LATITUDE());
112         checkIfNullProperty(Config.CADI_LONGITUDE, cadiFilterParams.getAFT_LONGITUDE());
113
114         checkIfNullProperty(Config.AAF_URL, cadiFilterParams.getAaf_url());
115         //checkIfNullProperty(Config.AAF_LOCATE_URL, cadiFilterParams.getAafLocateUrl());
116         checkIfNullProperty(Config.AAF_APPID, cadiFilterParams.getAaf_id());
117         checkIfNullProperty(Config.AAF_APPPASS, cadiFilterParams.getAaf_password());
118         checkIfNullProperty(Config.AAF_ENV, cadiFilterParams.getAFT_ENVIRONMENT());
119
120         checkIfNullProperty(Config.CADI_X509_ISSUERS, cadiFilterParams.getCadiX509Issuers());
121         checkIfNullProperty(Config.CADI_TRUSTSTORE, cadiFilterParams.getCadi_truststore());
122         checkIfNullProperty(Config.CADI_TRUSTSTORE_PASSWORD, cadiFilterParams.getCadi_truststore_password());
123         super.init(filterConfig);
124         log.debug("BeCadiServletFilter finishing init(), Current status of CADI would be UP");
125         if (!isNeedAuth()) {
126             CADIHealthCheck.getCADIHealthCheckInstance().setIsCADIUp(HealthCheckInfo.HealthCheckStatus.DOWN);
127         } else {
128             CADIHealthCheck.getCADIHealthCheckInstance().setIsCADIUp(HealthCheckInfo.HealthCheckStatus.UP);
129         }
130     }
131
132
133     @Override
134     public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
135
136         if (ThreadLocalsHolder.isExternalRequest() && isNeedAuth()) {
137             log.debug("doFilter: {}", request.getContentType());
138             HttpServletRequest hreq = (HttpServletRequest) request;
139             log.debug("Need aaf authentication : {}", hreq);
140             ThreadLocalUtils threadLocalUtils = getThreadLocalUtils(((HttpServletRequest) request).getSession().getServletContext());
141             threadLocalUtils.setUserContext((HttpServletRequest) request);
142             super.doFilter(request, response, chain);
143         } else {
144             log.debug("No need aaf authentication");
145             chain.doFilter(request, response);
146         }
147     }
148
149     private boolean isNeedAuth() {
150         return configurationManager.getConfiguration().getAafAuthNeeded();
151     }
152
153
154     ThreadLocalUtils getThreadLocalUtils(ServletContext context) {
155         return getClassFromWebAppContext(context, () -> ThreadLocalUtils.class);
156     }
157
158     <T> T getClassFromWebAppContext(ServletContext context, Supplier<Class<T>> businessLogicClassGen) {
159         WebAppContextWrapper webApplicationContextWrapper = (WebAppContextWrapper) context.getAttribute(Constants.WEB_APPLICATION_CONTEXT_WRAPPER_ATTR);
160         WebApplicationContext webApplicationContext = webApplicationContextWrapper.getWebAppContext(context);
161         return webApplicationContext.getBean(businessLogicClassGen.get());
162     }
163
164
165 }
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180