Fix the AAF login
[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  * 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
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
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  * ===================================================================
23  *
24  */
25
26 package org.onap.clamp.clds;
27
28 import com.att.eelf.configuration.EELFLogger;
29 import com.att.eelf.configuration.EELFManager;
30
31 import fj.data.Array;
32 import java.io.IOException;
33 import java.security.Principal;
34 import java.util.ArrayList;
35 import java.util.Arrays;
36 import java.util.List;
37
38 import java.util.stream.Collector;
39 import java.util.stream.Collectors;
40 import javax.servlet.ServletException;
41 import javax.servlet.http.HttpServletRequest;
42 import javax.servlet.http.HttpServletResponse;
43
44 import org.apache.camel.component.servlet.CamelHttpTransportServlet;
45 import org.onap.clamp.authorization.SecureServicePermission;
46 import org.springframework.context.ApplicationContext;
47 import org.springframework.http.HttpStatus;
48 import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
49 import org.springframework.security.core.Authentication;
50 import org.springframework.security.core.GrantedAuthority;
51 import org.springframework.security.core.authority.SimpleGrantedAuthority;
52 import org.springframework.security.core.context.SecurityContextHolder;
53 import org.springframework.security.core.userdetails.User;
54 import org.springframework.web.context.support.WebApplicationContextUtils;
55
56 public class ClampServlet extends CamelHttpTransportServlet {
57
58     /**
59      * The serial version ID.
60      */
61     private static final long serialVersionUID = -4198841134910211542L;
62
63     private static final EELFLogger logger = EELFManager.getInstance().getLogger(ClampServlet.class);
64     private static final String PERM_INSTANCE = "clamp.config.security.permission.instance";
65     private static final String PERM_CL = "clamp.config.security.permission.type.cl";
66     private static final String PERM_TEMPLATE = "clamp.config.security.permission.type.template";
67     private static final String PERM_VF = "clamp.config.security.permission.type.filter.vf";
68     private static final String PERM_MANAGE = "clamp.config.security.permission.type.cl.manage";
69     private static final String PERM_TOSCA = "clamp.config.security.permission.type.tosca";
70     private static final String AUTHENTICATION_CLASS = "clamp.config.security.authentication.class";
71     private static final String READ = "read";
72     private static final String UPDATE = "update";
73
74     private static List<SecureServicePermission> permissionList;
75
76     private synchronized List<String> loadDynamicAuthenticationClasses() {
77         return Arrays.stream(WebApplicationContextUtils.getWebApplicationContext(getServletContext())
78                 .getEnvironment().getProperty(AUTHENTICATION_CLASS).split(",")).map(className -> className.trim())
79                 .collect(Collectors.toList());
80     }
81
82     private synchronized List<SecureServicePermission> getPermissionList() {
83         if (permissionList == null) {
84             permissionList = new ArrayList<>();
85             ApplicationContext applicationContext = WebApplicationContextUtils
86                     .getWebApplicationContext(getServletContext());
87             String cldsPermissionInstance = applicationContext.getEnvironment().getProperty(PERM_INSTANCE);
88             permissionList.add(SecureServicePermission.create(applicationContext.getEnvironment().getProperty(PERM_CL),
89                     cldsPermissionInstance, READ));
90             permissionList.add(SecureServicePermission.create(applicationContext.getEnvironment().getProperty(PERM_CL),
91                     cldsPermissionInstance, UPDATE));
92             permissionList.add(SecureServicePermission.create(
93                     applicationContext.getEnvironment().getProperty(PERM_TEMPLATE), cldsPermissionInstance, READ));
94             permissionList.add(SecureServicePermission.create(
95                     applicationContext.getEnvironment().getProperty(PERM_TEMPLATE), cldsPermissionInstance, UPDATE));
96             permissionList.add(SecureServicePermission.create(applicationContext.getEnvironment().getProperty(PERM_VF),
97                     cldsPermissionInstance, "*"));
98             permissionList.add(SecureServicePermission
99                     .create(applicationContext.getEnvironment().getProperty(PERM_MANAGE), cldsPermissionInstance, "*"));
100             permissionList.add(SecureServicePermission
101                     .create(applicationContext.getEnvironment().getProperty(PERM_TOSCA), cldsPermissionInstance, READ));
102             permissionList.add(SecureServicePermission
103                     .create(applicationContext.getEnvironment().getProperty(PERM_TOSCA),
104                             cldsPermissionInstance, UPDATE));
105         }
106         return permissionList;
107     }
108
109     /**
110      * When AAF is enabled, request object will contain a cadi Wrapper, so queries
111      * to isUserInRole will invoke a http call to AAF server.
112      */
113     @Override
114     protected void doService(HttpServletRequest request, HttpServletResponse response) {
115         Principal principal = request.getUserPrincipal();
116         if (principal != null && loadDynamicAuthenticationClasses().stream()
117                 .anyMatch(className -> className.equals(principal.getClass().getName()))) {
118             // When AAF is enabled, there is a need to provision the permissions to Spring
119             // system
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));
125                 }
126             }
127             Authentication auth = new UsernamePasswordAuthenticationToken(new User(principal.getName(), "",
128                     grantedAuths), "", grantedAuths);
129             SecurityContextHolder.getContext().setAuthentication(auth);
130         }
131         try {
132             super.doService(request, response);
133         } catch (ServletException | IOException ioe) {
134             logger.error("Exception caught when executing doService in servlet", ioe);
135             try {
136                 response.sendError(HttpStatus.INTERNAL_SERVER_ERROR.value());
137             } catch (IOException e) {
138                 logger.error("Exception caught when executing HTTP sendError in servlet", e);
139             }
140         }
141     }
142 }