Remove the manual timer
[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      *
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
65     /**
66      * When AAF is enabled, request object will contain a cadi Wrapper, so queries
67      * to isUserInRole will invoke a http call to AAF server.
68      */
69     @Override
70     protected void doService(HttpServletRequest request, HttpServletResponse response)
71         throws ServletException, IOException {
72         List<SecureServicePermission> permissionList = new ArrayList<>();
73
74         ApplicationContext applicationContext = WebApplicationContextUtils
75             .getWebApplicationContext(this.getServletContext());
76
77         String cldsPersmissionTypeCl = applicationContext.getEnvironment().getProperty(PERM_CL);
78         String cldsPermissionTypeTemplate = applicationContext.getEnvironment().getProperty(PERM_TEMPLATE);
79         String cldsPermissionInstance = applicationContext.getEnvironment().getProperty(PERM_INSTANCE);
80         String cldsPermissionTypeFilterVf = applicationContext.getEnvironment().getProperty(PERM_VF);
81         String cldsPermissionTypeClManage = applicationContext.getEnvironment().getProperty(PERM_MANAGE);
82         String cldsPermissionTypeTosca = applicationContext.getEnvironment().getProperty(PERM_TOSCA);
83
84         // set the stragety to Mode_Global, so that all thread is able to
85         // see the authentication
86         SecurityContextHolder.setStrategyName(SecurityContextHolder.MODE_GLOBAL);
87         Principal p = request.getUserPrincipal();
88         if (null != p) {
89             permissionList.add(SecureServicePermission.create(cldsPersmissionTypeCl, cldsPermissionInstance, "read"));
90             permissionList.add(SecureServicePermission.create(cldsPersmissionTypeCl, cldsPermissionInstance, "update"));
91             permissionList
92             .add(SecureServicePermission.create(cldsPermissionTypeTemplate, cldsPermissionInstance, "read"));
93             permissionList
94             .add(SecureServicePermission.create(cldsPermissionTypeTemplate, cldsPermissionInstance, "update"));
95             permissionList.add(SecureServicePermission.create(cldsPermissionTypeFilterVf, cldsPermissionInstance, "*"));
96             permissionList.add(SecureServicePermission.create(cldsPermissionTypeClManage, cldsPermissionInstance, "*"));
97             permissionList.add(SecureServicePermission.create(cldsPermissionTypeTosca, cldsPermissionInstance, "read"));
98             permissionList
99             .add(SecureServicePermission.create(cldsPermissionTypeTosca, cldsPermissionInstance, "update"));
100
101             List<GrantedAuthority> grantedAuths = new ArrayList<>();
102             for (SecureServicePermission perm : permissionList) {
103                 String permString = perm.toString();
104                 if (request.isUserInRole(permString)) {
105                     grantedAuths.add(new SimpleGrantedAuthority(permString));
106                 }
107             }
108
109             Authentication auth = new UsernamePasswordAuthenticationToken(new User(p.getName(), "", grantedAuths), "",
110                 grantedAuths);
111             SecurityContextHolder.getContext().setAuthentication(auth);
112         }
113         try {
114             super.doService(request, response);
115         } catch (ServletException | IOException ioe) {
116             logger.error("Exception caught when executing doService in servlet", ioe);
117             try {
118                 response.sendError(HttpStatus.INTERNAL_SERVER_ERROR.value());
119             } catch (IOException e) {
120                 logger.error("Exception caught when executing HTTP sendError in servlet", e);
121             }
122         }
123
124     }
125 }