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