aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/org/onap/clamp/clds/ClampServlet.java
blob: 6adb76cb3187712f76f7d94c46e5f70e225fe24b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*-
 * ============LICENSE_START=======================================================
 * ONAP CLAMP
 * ================================================================================
 * Copyright (C) 2018 AT&T Intellectual Property. All rights
 *                             reserved.
 * ================================================================================
 * Modifications Copyright (c) 2019 Samsung
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END============================================
 * ===================================================================
 *
 */

package org.onap.clamp.clds;

import com.att.eelf.configuration.EELFLogger;
import com.att.eelf.configuration.EELFManager;

import fj.data.Array;
import java.io.IOException;
import java.security.Principal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.camel.component.servlet.CamelHttpTransportServlet;
import org.onap.clamp.authorization.SecureServicePermission;
import org.springframework.context.ApplicationContext;
import org.springframework.http.HttpStatus;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.context.support.WebApplicationContextUtils;

public class ClampServlet extends CamelHttpTransportServlet {

    /**
     * The serial version ID.
     */
    private static final long serialVersionUID = -4198841134910211542L;

    private static final EELFLogger logger = EELFManager.getInstance().getLogger(ClampServlet.class);
    private static final String PERM_INSTANCE = "clamp.config.security.permission.instance";
    private static final String PERM_CL = "clamp.config.security.permission.type.cl";
    private static final String PERM_TEMPLATE = "clamp.config.security.permission.type.template";
    private static final String PERM_VF = "clamp.config.security.permission.type.filter.vf";
    private static final String PERM_MANAGE = "clamp.config.security.permission.type.cl.manage";
    private static final String PERM_TOSCA = "clamp.config.security.permission.type.tosca";
    private static final String AUTHENTICATION_CLASS = "clamp.config.security.authentication.class";
    private static final String READ = "read";
    private static final String UPDATE = "update";

    private static List<SecureServicePermission> permissionList;

    private synchronized String[] loadDynamicAuthenticationClasses() {
        String[] authenticationObjects = WebApplicationContextUtils.getWebApplicationContext(getServletContext())
                .getEnvironment().getProperty(AUTHENTICATION_CLASS).split(",");
        Arrays.stream(authenticationObjects).forEach(className -> className.trim());
        return authenticationObjects;
    }

    private synchronized List<SecureServicePermission> getPermissionList() {
        if (permissionList == null) {
            permissionList = new ArrayList<>();
            ApplicationContext applicationContext = WebApplicationContextUtils
                    .getWebApplicationContext(getServletContext());
            String cldsPermissionInstance = applicationContext.getEnvironment().getProperty(PERM_INSTANCE);
            permissionList.add(SecureServicePermission.create(applicationContext.getEnvironment().getProperty(PERM_CL),
                    cldsPermissionInstance, READ));
            permissionList.add(SecureServicePermission.create(applicationContext.getEnvironment().getProperty(PERM_CL),
                    cldsPermissionInstance, UPDATE));
            permissionList.add(SecureServicePermission.create(
                    applicationContext.getEnvironment().getProperty(PERM_TEMPLATE), cldsPermissionInstance, READ));
            permissionList.add(SecureServicePermission.create(
                    applicationContext.getEnvironment().getProperty(PERM_TEMPLATE), cldsPermissionInstance, UPDATE));
            permissionList.add(SecureServicePermission.create(applicationContext.getEnvironment().getProperty(PERM_VF),
                    cldsPermissionInstance, "*"));
            permissionList.add(SecureServicePermission
                    .create(applicationContext.getEnvironment().getProperty(PERM_MANAGE), cldsPermissionInstance, "*"));
            permissionList.add(SecureServicePermission
                    .create(applicationContext.getEnvironment().getProperty(PERM_TOSCA), cldsPermissionInstance, READ));
            permissionList.add(SecureServicePermission
                    .create(applicationContext.getEnvironment().getProperty(PERM_TOSCA),
                            cldsPermissionInstance, UPDATE));
        }
        return permissionList;
    }

    /**
     * When AAF is enabled, request object will contain a cadi Wrapper, so queries
     * to isUserInRole will invoke a http call to AAF server.
     */
    @Override
    protected void doService(HttpServletRequest request, HttpServletResponse response) {
        Principal principal = request.getUserPrincipal();
        if (principal != null && Arrays.stream(loadDynamicAuthenticationClasses())
                .anyMatch(className -> className.equals(principal.getClass().getName()))) {
            // When AAF is enabled, there is a need to provision the permissions to Spring
            // system
            List<GrantedAuthority> grantedAuths = new ArrayList<>();
            for (SecureServicePermission perm : getPermissionList()) {
                String permString = perm.toString();
                if (request.isUserInRole(permString)) {
                    grantedAuths.add(new SimpleGrantedAuthority(permString));
                }
            }
            Authentication auth = new UsernamePasswordAuthenticationToken(new User(principal.getName(), "",
                    grantedAuths), "", grantedAuths);
            SecurityContextHolder.getContext().setAuthentication(auth);
        }
        try {
            super.doService(request, response);
        } catch (ServletException | IOException ioe) {
            logger.error("Exception caught when executing doService in servlet", ioe);
            try {
                response.sendError(HttpStatus.INTERNAL_SERVER_ERROR.value());
            } catch (IOException e) {
                logger.error("Exception caught when executing HTTP sendError in servlet", e);
            }
        }
    }
}