2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 package org.onap.dmaap.dbcapi.resources;
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;
41 public class AAFAuthenticationFilter implements Filter {
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";
47 private boolean isCadiEnabled;
48 private CadiFilter cadiFilter;
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);
60 public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
61 throws IOException, ServletException {
64 cadiFilter.doFilter(servletRequest, servletResponse, filterChain);
65 updateResponseBody((HttpServletResponse)servletResponse);
67 filterChain.doFilter(servletRequest, servletResponse);
71 private void updateResponseBody(HttpServletResponse httpResponse)
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();
83 private String buildErrorResponse(String msg) {
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());
94 public void destroy() {
98 private void initCadi(DmaapConfig dmaapConfig) throws ServletException {
101 String cadiPropertiesFile = dmaapConfig.getProperty(CADI_PROPERTIES);
102 if(cadiPropertiesFile != null && !cadiPropertiesFile.isEmpty()) {
103 cadiFilter = new CadiFilter(loadCadiProperties(cadiPropertiesFile));
105 throw new ServletException("Cannot initialize CADI filter.CADI properties not available.");
107 } catch (ServletException e) {
108 LOGGER.error("CADI init error :" + e.getMessage());
114 private PropAccess loadCadiProperties(String propertiesFilePath) throws ServletException {
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);
126 DmaapConfig getConfig() {
127 return (DmaapConfig) DmaapConfig.getConfig();
131 CadiFilter getCadiFilter() {
135 void setCadiFilter(CadiFilter cadiFilter) {
136 this.cadiFilter = cadiFilter;
139 boolean isCadiEnabled() {
140 return isCadiEnabled;