e8fd83e2e4667d3033a1ef90a30b3b27c06c9d24
[clamp.git] / src / main / java / org / onap / clamp / clds / ClampServlet.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2018 AT&T Intellectual Property. All rights
6  *                             reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END============================================
20  * ===================================================================
21  *
22  */
23
24 package org.onap.clamp.clds;
25
26 import com.att.eelf.configuration.EELFLogger;
27 import com.att.eelf.configuration.EELFManager;
28
29 import java.io.IOException;
30 import java.security.Principal;
31 import java.util.ArrayList;
32 import java.util.List;
33
34 import javax.servlet.ServletException;
35 import javax.servlet.http.HttpServletRequest;
36 import javax.servlet.http.HttpServletResponse;
37
38 import org.apache.camel.component.servlet.CamelHttpTransportServlet;
39 import org.onap.clamp.clds.service.SecureServicePermission;
40 import org.springframework.context.ApplicationContext;
41 import org.springframework.http.HttpStatus;
42 import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
43 import org.springframework.security.core.Authentication;
44 import org.springframework.security.core.GrantedAuthority;
45 import org.springframework.security.core.authority.SimpleGrantedAuthority;
46 import org.springframework.security.core.context.SecurityContextHolder;
47 import org.springframework.security.core.userdetails.User;
48 import org.springframework.web.context.support.WebApplicationContextUtils;
49
50 public class ClampServlet extends CamelHttpTransportServlet {
51
52     /**
53      * The serial version ID.
54      */
55     private static final long serialVersionUID = -4198841134910211542L;
56
57     protected static final EELFLogger logger = EELFManager.getInstance().getLogger(ClampServlet.class);
58     public static final String PERM_INSTANCE = "clamp.config.security.permission.instance";
59     public static final String PERM_CL = "clamp.config.security.permission.type.cl";
60     public static final String PERM_TEMPLATE = "clamp.config.security.permission.type.template";
61     public static final String PERM_VF = "clamp.config.security.permission.type.filter.vf";
62     public static final String PERM_MANAGE = "clamp.config.security.permission.type.cl.manage";
63     public static final String PERM_TOSCA = "clamp.config.security.permission.type.tosca";
64     public static final String AUTHENTICATION_CLASS = "clamp.config.security.authentication.class";
65     private static List<SecureServicePermission> permissionList;
66
67     private synchronized Class loadDynamicAuthenticationClass() {
68         try {
69             String authenticationObject = WebApplicationContextUtils.getWebApplicationContext(getServletContext())
70                 .getEnvironment().getProperty(AUTHENTICATION_CLASS);
71             return Class.forName(authenticationObject);
72         } catch (ClassNotFoundException e) {
73             logger.error(
74                 "Exception caught when attempting to create associated class of config:" + AUTHENTICATION_CLASS, e);
75             return Object.class;
76         }
77     }
78
79     private synchronized List<SecureServicePermission> getPermissionList() {
80         if (permissionList == null) {
81             permissionList = new ArrayList<>();
82             ApplicationContext applicationContext = WebApplicationContextUtils
83                 .getWebApplicationContext(getServletContext());
84             String cldsPermissionInstance = applicationContext.getEnvironment().getProperty(PERM_INSTANCE);
85             permissionList.add(SecureServicePermission.create(applicationContext.getEnvironment().getProperty(PERM_CL),
86                 cldsPermissionInstance, "read"));
87             permissionList.add(SecureServicePermission.create(applicationContext.getEnvironment().getProperty(PERM_CL),
88                 cldsPermissionInstance, "update"));
89             permissionList.add(SecureServicePermission.create(
90                 applicationContext.getEnvironment().getProperty(PERM_TEMPLATE), cldsPermissionInstance, "read"));
91             permissionList.add(SecureServicePermission.create(
92                 applicationContext.getEnvironment().getProperty(PERM_TEMPLATE), cldsPermissionInstance, "update"));
93             permissionList.add(SecureServicePermission.create(applicationContext.getEnvironment().getProperty(PERM_VF),
94                 cldsPermissionInstance, "*"));
95             permissionList.add(SecureServicePermission
96                 .create(applicationContext.getEnvironment().getProperty(PERM_MANAGE), cldsPermissionInstance, "*"));
97             permissionList.add(SecureServicePermission
98                 .create(applicationContext.getEnvironment().getProperty(PERM_TOSCA), cldsPermissionInstance, "read"));
99             permissionList.add(SecureServicePermission
100                 .create(applicationContext.getEnvironment().getProperty(PERM_TOSCA), cldsPermissionInstance, "update"));
101         }
102         return permissionList;
103     }
104
105     /**
106      * When AAF is enabled, request object will contain a cadi Wrapper, so queries
107      * to isUserInRole will invoke a http call to AAF server.
108      */
109     @Override
110     protected void doService(HttpServletRequest request, HttpServletResponse response)
111         throws ServletException, IOException {
112         Principal principal = request.getUserPrincipal();
113         if (loadDynamicAuthenticationClass().isInstance(principal)) {
114             // When AAF is enabled, there is a need to provision the permissions to Spring
115             // system
116             List<GrantedAuthority> grantedAuths = new ArrayList<>();
117             for (SecureServicePermission perm : getPermissionList()) {
118                 String permString = perm.toString();
119                 if (request.isUserInRole(permString)) {
120                     grantedAuths.add(new SimpleGrantedAuthority(permString));
121                 }
122             }
123             Authentication auth = new UsernamePasswordAuthenticationToken(new User(principal.getName(), "", grantedAuths), "",
124                 grantedAuths);
125             SecurityContextHolder.getContext().setAuthentication(auth);
126         }
127         try {
128             super.doService(request, response);
129         } catch (ServletException | IOException ioe) {
130             logger.error("Exception caught when executing doService in servlet", ioe);
131             try {
132                 response.sendError(HttpStatus.INTERNAL_SERVER_ERROR.value());
133             } catch (IOException e) {
134                 logger.error("Exception caught when executing HTTP sendError in servlet", e);
135             }
136         }
137
138     }
139 }