move authN and authZ filter decission to enableCADI flag
[dmaap/dbcapi.git] / src / main / java / org / onap / dmaap / dbcapi / resources / AAFAuthenticationFilter.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * org.onap.dmaap
4  * ================================================================================
5  * Copyright (C) 2019 Nokia 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 package org.onap.dmaap.dbcapi.resources;
21
22 import com.fasterxml.jackson.core.JsonProcessingException;
23 import com.fasterxml.jackson.databind.ObjectMapper;
24 import java.io.FileInputStream;
25 import java.io.IOException;
26 import java.util.Properties;
27 import javax.servlet.Filter;
28 import javax.servlet.FilterChain;
29 import javax.servlet.FilterConfig;
30 import javax.servlet.ServletException;
31 import javax.servlet.ServletRequest;
32 import javax.servlet.ServletResponse;
33 import javax.servlet.http.HttpServletResponse;
34 import org.apache.log4j.Logger;
35 import org.eclipse.jetty.http.HttpStatus;
36 import org.onap.aaf.cadi.PropAccess;
37 import org.onap.aaf.cadi.filter.CadiFilter;
38 import org.onap.dmaap.dbcapi.model.ApiError;
39 import org.onap.dmaap.dbcapi.util.DmaapConfig;
40
41 public class AAFAuthenticationFilter implements Filter {
42
43     private static final Logger LOGGER = Logger.getLogger(AAFAuthenticationFilter.class.getName());
44     static final String CADI_PROPERTIES = "cadi.properties";
45     static final String CADI_AUTHN_FLAG = "enableCADI";
46
47     private boolean isCadiEnabled;
48     private CadiFilter cadiFilter;
49
50     @Override
51     public void init(FilterConfig filterConfig) throws ServletException {
52         DmaapConfig dmaapConfig = getConfig();
53         String flag = dmaapConfig.getProperty(CADI_AUTHN_FLAG, "false");
54         isCadiEnabled = "true".equalsIgnoreCase(flag);
55         initCadi(dmaapConfig);
56     }
57
58
59     @Override
60     public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
61         throws IOException, ServletException {
62
63         if(isCadiEnabled) {
64             cadiFilter.doFilter(servletRequest, servletResponse, filterChain);
65             updateResponseBody((HttpServletResponse)servletResponse);
66         } else {
67             filterChain.doFilter(servletRequest, servletResponse);
68         }
69     }
70
71     private void updateResponseBody(HttpServletResponse httpResponse)
72         throws IOException {
73         if(httpResponse.getStatus() == 401) {
74             String errorMsg = "invalid or no credentials provided";
75             LOGGER.error(errorMsg);
76             httpResponse.setContentType("application/json");
77             httpResponse.setCharacterEncoding("UTF-8");
78             httpResponse.getWriter().print(buildErrorResponse(errorMsg));
79             httpResponse.getWriter().flush();
80         }
81     }
82
83     private String buildErrorResponse(String msg) {
84         try {
85             return new ObjectMapper().writeValueAsString(new ApiError(HttpStatus.UNAUTHORIZED_401, msg, "Authentication"));
86         } catch (JsonProcessingException e) {
87             LOGGER.warn("Could not serialize response entity: " + e.getMessage());
88             return "";
89         }
90     }
91
92
93     @Override
94     public void destroy() {
95         //nothing to cleanup
96     }
97
98     private void initCadi(DmaapConfig dmaapConfig) throws ServletException {
99         if(isCadiEnabled) {
100             try {
101                 String cadiPropertiesFile = dmaapConfig.getProperty(CADI_PROPERTIES);
102                 if(cadiPropertiesFile != null && !cadiPropertiesFile.isEmpty()) {
103                     cadiFilter = new CadiFilter(loadCadiProperties(cadiPropertiesFile));
104                 } else {
105                     throw new ServletException("Cannot initialize CADI filter.CADI properties not available.");
106                 }
107             } catch (ServletException e) {
108                 LOGGER.error("CADI init error :" + e.getMessage());
109                 throw e;
110             }
111         }
112     }
113
114     private PropAccess loadCadiProperties(String propertiesFilePath) throws ServletException {
115         try {
116             Properties props = new Properties();
117             props.load(new FileInputStream(propertiesFilePath));
118             return new PropAccess(props);
119         } catch (IOException e) {
120             String msg = "Could not load CADI properties file: " + propertiesFilePath;
121             LOGGER.error(msg, e);
122             throw new ServletException(msg);
123         }
124     }
125
126     DmaapConfig getConfig() {
127         return (DmaapConfig) DmaapConfig.getConfig();
128     }
129
130     //tests only
131     CadiFilter getCadiFilter() {
132         return cadiFilter;
133     }
134
135     void setCadiFilter(CadiFilter cadiFilter) {
136         this.cadiFilter = cadiFilter;
137     }
138
139     boolean isCadiEnabled() {
140         return isCadiEnabled;
141     }
142 }