aboutsummaryrefslogtreecommitdiffstats
path: root/catalog-be/src/main/java/org/openecomp/sdc/be/filters/BeCadiServletFilter.java
blob: 7edd1621b101f576e5b4deef61125edbbc3893ac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*-
 * ============LICENSE_START=======================================================
 * SDC
 * ================================================================================
 * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */
package org.openecomp.sdc.be.filters;

import java.io.IOException;
import java.util.function.Supplier;
import javax.annotation.Priority;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import org.onap.aaf.cadi.Access;
import org.onap.aaf.cadi.PropAccess;
import org.onap.aaf.cadi.config.Config;
import org.onap.aaf.cadi.filter.CadiFilter;
import org.openecomp.sdc.be.components.impl.CADIHealthCheck;
import org.openecomp.sdc.be.config.CadiFilterParams;
import org.openecomp.sdc.be.config.ConfigurationManager;
import org.openecomp.sdc.be.impl.WebAppContextWrapper;
import org.openecomp.sdc.common.api.Constants;
import org.openecomp.sdc.common.api.HealthCheckInfo;
import org.openecomp.sdc.common.log.wrappers.Logger;
import org.openecomp.sdc.common.util.ThreadLocalsHolder;
import org.springframework.web.context.WebApplicationContext;

@Priority(2)
public class BeCadiServletFilter extends CadiFilter {

    private static final Logger log = Logger.getLogger(BeCadiServletFilter.class);
    private static final String BE_CADI_SERVICE_FILTER = "BeCadiServletFilter: ";
    private ConfigurationManager configurationManager = ConfigurationManager.getConfigurationManager();

    public BeCadiServletFilter() {
        super();
        log.debug(BE_CADI_SERVICE_FILTER);
    }

    /**
     * This constructor to be used when directly constructing and placing in HTTP Engine
     *
     * @param access
     * @param moreTafLurs
     * @throws ServletException
     */
    public BeCadiServletFilter(Access access, Object... moreTafLurs) throws ServletException {
        super(access, moreTafLurs);
        log.debug(BE_CADI_SERVICE_FILTER);
    }

    /**
     * Use this to pass in a PreContructed CADI Filter, but with initializing... let Servlet do it
     *
     * @param init
     * @param access
     * @param moreTafLurs
     * @throws ServletException
     */
    public BeCadiServletFilter(boolean init, PropAccess access, Object... moreTafLurs) throws ServletException {
        super(init, access, moreTafLurs);
        log.debug(BE_CADI_SERVICE_FILTER);
    }

    private void checkIfNullProperty(String key, String value) {
        /* When value is null, so not defined in application.properties
           set nothing in System properties */
        if (value != null) { 
            /* Ensure that any properties already defined in System.prop by JVM params
                won't be overwritten by Spring application.properties values */
            System.setProperty(key, System.getProperty(key, value));
        }
    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        // set some properties in System so that Cadi filter will find its config

        // The JVM values set will always overwrite the Spring ones.
        CadiFilterParams cadiFilterParams = configurationManager.getConfiguration().getCadiFilterParams();
        checkIfNullProperty(Config.HOSTNAME, cadiFilterParams.getHostname());
        log.debug("BeCadiServletFilter: HOSTNAME", cadiFilterParams.getHostname());
        checkIfNullProperty(Config.CADI_KEYFILE, cadiFilterParams.getCadi_keyfile());
        checkIfNullProperty(Config.CADI_LOGLEVEL, cadiFilterParams.getCadi_loglevel());
        checkIfNullProperty(Config.CADI_LATITUDE, cadiFilterParams.getAFT_LATITUDE());
        checkIfNullProperty(Config.CADI_LONGITUDE, cadiFilterParams.getAFT_LONGITUDE());
        checkIfNullProperty(Config.AAF_URL, cadiFilterParams.getAaf_url());
        //checkIfNullProperty(Config.AAF_LOCATE_URL, cadiFilterParams.getAafLocateUrl());
        checkIfNullProperty(Config.AAF_APPID, cadiFilterParams.getAaf_id());
        checkIfNullProperty(Config.AAF_APPPASS, cadiFilterParams.getAaf_password());
        checkIfNullProperty(Config.AAF_ENV, cadiFilterParams.getAFT_ENVIRONMENT());
        checkIfNullProperty(Config.CADI_X509_ISSUERS, cadiFilterParams.getCadiX509Issuers());
        checkIfNullProperty(Config.CADI_TRUSTSTORE, cadiFilterParams.getCadi_truststore());
        checkIfNullProperty(Config.CADI_TRUSTSTORE_PASSWORD, cadiFilterParams.getCadi_truststore_password());
        super.init(filterConfig);
        log.debug("BeCadiServletFilter finishing init(), Current status of CADI would be UP");
        if (!isNeedAuth()) {
            CADIHealthCheck.getCADIHealthCheckInstance().setIsCADIUp(HealthCheckInfo.HealthCheckStatus.DOWN);
        } else {
            CADIHealthCheck.getCADIHealthCheckInstance().setIsCADIUp(HealthCheckInfo.HealthCheckStatus.UP);
        }
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        if (ThreadLocalsHolder.isExternalRequest() && isNeedAuth()) {
            log.debug("doFilter: {}", request.getContentType());
            HttpServletRequest hreq = (HttpServletRequest) request;
            log.debug("Need aaf authentication : {}", hreq);
            ThreadLocalUtils threadLocalUtils = getThreadLocalUtils(((HttpServletRequest) request).getSession().getServletContext());
            threadLocalUtils.setUserContext((HttpServletRequest) request);
            super.doFilter(request, response, chain);
        } else {
            log.debug("No need aaf authentication");
            chain.doFilter(request, response);
        }
    }

    private boolean isNeedAuth() {
        return configurationManager.getConfiguration().getAafAuthNeeded();
    }

    ThreadLocalUtils getThreadLocalUtils(ServletContext context) {
        return getClassFromWebAppContext(context, () -> ThreadLocalUtils.class);
    }

    <T> T getClassFromWebAppContext(ServletContext context, Supplier<Class<T>> businessLogicClassGen) {
        WebAppContextWrapper webApplicationContextWrapper = (WebAppContextWrapper) context
            .getAttribute(Constants.WEB_APPLICATION_CONTEXT_WRAPPER_ATTR);
        WebApplicationContext webApplicationContext = webApplicationContextWrapper.getWebAppContext(context);
        return webApplicationContext.getBean(businessLogicClassGen.get());
    }
}