2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018 AT&T Intellectual Property. All rights
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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 * ===================================================================
24 package org.onap.clamp.clds;
26 import com.att.eelf.configuration.EELFLogger;
27 import com.att.eelf.configuration.EELFManager;
29 import java.io.IOException;
30 import java.security.Principal;
31 import java.util.ArrayList;
32 import java.util.List;
34 import javax.servlet.ServletException;
35 import javax.servlet.http.HttpServletRequest;
36 import javax.servlet.http.HttpServletResponse;
38 import org.apache.camel.component.servlet.CamelHttpTransportServlet;
39 import org.onap.aaf.cadi.principal.X509Principal;
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 {
56 private static final long serialVersionUID = -4198841134910211542L;
58 protected static final EELFLogger logger = EELFManager.getInstance().getLogger(ClampServlet.class);
59 public static final String PERM_INSTANCE = "clamp.config.security.permission.instance";
60 public static final String PERM_CL = "clamp.config.security.permission.type.cl";
61 public static final String PERM_TEMPLATE = "clamp.config.security.permission.type.template";
62 public static final String PERM_VF = "clamp.config.security.permission.type.filter.vf";
63 public static final String PERM_MANAGE = "clamp.config.security.permission.type.cl.manage";
64 public static final String PERM_TOSCA = "clamp.config.security.permission.type.tosca";
65 private static List<SecureServicePermission> permissionList;
67 private synchronized List<SecureServicePermission> getPermissionList() {
68 if (permissionList == null) {
69 permissionList=new ArrayList<>();
70 ApplicationContext applicationContext = WebApplicationContextUtils
71 .getWebApplicationContext(getServletContext());
72 String cldsPermissionInstance = applicationContext.getEnvironment().getProperty(PERM_INSTANCE);
73 permissionList.add(SecureServicePermission.create(applicationContext.getEnvironment().getProperty(PERM_CL),
74 cldsPermissionInstance, "read"));
75 permissionList.add(SecureServicePermission.create(applicationContext.getEnvironment().getProperty(PERM_CL),
76 cldsPermissionInstance, "update"));
77 permissionList.add(SecureServicePermission.create(
78 applicationContext.getEnvironment().getProperty(PERM_TEMPLATE), cldsPermissionInstance, "read"));
79 permissionList.add(SecureServicePermission.create(
80 applicationContext.getEnvironment().getProperty(PERM_TEMPLATE), cldsPermissionInstance, "update"));
81 permissionList.add(SecureServicePermission.create(applicationContext.getEnvironment().getProperty(PERM_VF),
82 cldsPermissionInstance, "*"));
83 permissionList.add(SecureServicePermission
84 .create(applicationContext.getEnvironment().getProperty(PERM_MANAGE), cldsPermissionInstance, "*"));
85 permissionList.add(SecureServicePermission
86 .create(applicationContext.getEnvironment().getProperty(PERM_TOSCA), cldsPermissionInstance, "read"));
87 permissionList.add(SecureServicePermission
88 .create(applicationContext.getEnvironment().getProperty(PERM_TOSCA), cldsPermissionInstance, "update"));
90 return permissionList;
94 * When AAF is enabled, request object will contain a cadi Wrapper, so queries
95 * to isUserInRole will invoke a http call to AAF server.
98 protected void doService(HttpServletRequest request, HttpServletResponse response)
99 throws ServletException, IOException {
101 Principal p = request.getUserPrincipal();
102 if (p instanceof X509Principal) {
103 // When AAF is enabled, there is a need to provision the permissions to Spring
105 List<GrantedAuthority> grantedAuths = new ArrayList<>();
106 for (SecureServicePermission perm : getPermissionList()) {
107 String permString = perm.toString();
108 if (request.isUserInRole(permString)) {
109 grantedAuths.add(new SimpleGrantedAuthority(permString));
112 Authentication auth = new UsernamePasswordAuthenticationToken(new User(p.getName(), "", grantedAuths), "",
114 SecurityContextHolder.getContext().setAuthentication(auth);
117 super.doService(request, response);
118 } catch (ServletException | IOException ioe) {
119 logger.error("Exception caught when executing doService in servlet", ioe);
121 response.sendError(HttpStatus.INTERNAL_SERVER_ERROR.value());
122 } catch (IOException e) {
123 logger.error("Exception caught when executing HTTP sendError in servlet", e);