2ef57803e3ece6bb239a0c1f68a7255fc47f672d
[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 java.io.IOException;
27 import java.security.Principal;
28 import java.util.ArrayList;
29 import java.util.List;
30 import javax.servlet.http.HttpServletRequest;
31 import javax.servlet.http.HttpServletResponse;
32 import javax.servlet.ServletException;
33
34 import com.att.eelf.configuration.EELFLogger;
35 import com.att.eelf.configuration.EELFManager;
36
37 import org.apache.camel.component.servlet.CamelHttpTransportServlet;
38
39 import org.springframework.context.ApplicationContext;
40 import org.springframework.security.authentication.AuthenticationManager;
41 import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
42 import org.springframework.security.core.authority.SimpleGrantedAuthority;
43 import org.springframework.security.core.Authentication;
44 import org.springframework.security.core.context.SecurityContext;
45 import org.springframework.security.core.context.SecurityContextHolder;
46 import org.springframework.security.core.GrantedAuthority;
47 import org.springframework.security.core.userdetails.User;
48 import org.springframework.web.context.support.WebApplicationContextUtils;
49
50 import org.onap.clamp.clds.config.ClampProperties;
51 import org.onap.clamp.clds.service.SecureServicePermission;
52 import org.onap.clamp.clds.util.ClampTimer;
53
54
55 public class ClampServlet extends CamelHttpTransportServlet {
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_TEMPLACE = "clamp.config.security.permission.type.template";
61
62     protected void doService(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
63         Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
64         List<SecureServicePermission> permissionList = new ArrayList<>();
65
66         // Get Principal info and translate it into Spring Authentication
67         // If authenticataion is null: a) the authentication info was set manually in the previous thread 
68         //                             b) handled by Spring automatically
69         // for the 2 cases above, no need for the translation, just skip the following step
70         if (null == authentication) {
71            logger.debug ("Populate Spring Authenticataion info manually.");
72             ApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
73             // Start a timer to clear the authentication after 5 mins, so that the authentication will be reinitialized with AAF DB
74             new ClampTimer(300);
75             String cldsPersmissionTypeCl = applicationContext.getEnvironment().getProperty(PERM_INSTANCE);
76             String cldsPermissionTypeTemplate = applicationContext.getEnvironment().getProperty(PERM_CL);
77             String cldsPermissionInstance = applicationContext.getEnvironment().getProperty(PERM_TEMPLACE);
78
79             // set the stragety to Mode_Global, so that all thread is able to see the authentication
80             SecurityContextHolder.setStrategyName(SecurityContextHolder.MODE_GLOBAL);
81             Principal p = request.getUserPrincipal(); 
82
83             permissionList.add(SecureServicePermission.create(cldsPersmissionTypeCl, cldsPermissionInstance, "read"));
84             permissionList.add(SecureServicePermission.create(cldsPersmissionTypeCl, cldsPermissionInstance, "update"));
85             permissionList.add(SecureServicePermission.create(cldsPermissionTypeTemplate, cldsPermissionInstance, "read"));
86             permissionList.add(SecureServicePermission.create(cldsPermissionTypeTemplate, cldsPermissionInstance, "update"));
87
88             List<GrantedAuthority> grantedAuths = new ArrayList<>();
89             for (SecureServicePermission perm:permissionList) {
90                 String permString = perm.toString();
91                 if (request.isUserInRole(permString)) {
92                     grantedAuths.add(new SimpleGrantedAuthority(permString));
93                 }
94             }
95             Authentication auth =  new UsernamePasswordAuthenticationToken(new User(p.getName(), "", grantedAuths), "", grantedAuths);
96             SecurityContextHolder.getContext().setAuthentication(auth);
97         }
98         super.doService(request, response);
99     }
100 }