2e43495b7fd6852d4425ba5d62854541ca98e46d
[clamp.git] / src / main / java / org / onap / clamp / authorization / AuthorizationController.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2019 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.authorization;
27
28 import com.att.eelf.configuration.EELFLogger;
29 import com.att.eelf.configuration.EELFManager;
30
31 import java.util.Date;
32
33 import org.onap.clamp.clds.exception.NotAuthorizedException;
34
35 import org.apache.camel.Exchange;
36 import org.onap.clamp.clds.config.ClampProperties;
37 import org.onap.clamp.clds.service.SecureServiceBase;
38 import org.onap.clamp.clds.service.SecureServicePermission;
39 import org.onap.clamp.clds.util.LoggingUtils;
40 import org.onap.clamp.util.PrincipalUtils;
41 import org.springframework.beans.factory.annotation.Autowired;
42 import org.springframework.security.core.Authentication;
43 import org.springframework.security.core.GrantedAuthority;
44 import org.springframework.stereotype.Component;
45
46 /**
47  * Create CLDS Event.
48  */
49 @Component
50 public class AuthorizationController {
51
52     protected static final EELFLogger logger          = EELFManager.getInstance().getLogger(SecureServiceBase.class);
53     protected static final EELFLogger auditLogger     = EELFManager.getInstance().getMetricsLogger();
54     protected static final EELFLogger securityLogger  = EELFManager.getInstance().getSecurityLogger();
55
56     // By default we'll set it to a default handler
57     @Autowired
58     private ClampProperties refProp;
59
60     public static final String PERM_PREFIX = "security.permission.type.";
61     private static final String PERM_INSTANCE = "security.permission.instance";
62
63     /**
64      * Insert authorize the api based on the permission
65      *
66      * @param camelExchange
67      *        The Camel Exchange object containing the properties
68      * @param typeVar
69      *        The type of the permissions
70      * @param instanceVar
71      *        The instance of the permissions. e.g. dev
72      * @param action
73      *        The action of the permissions. e.g. read
74      */
75     public void authorize(Exchange camelExchange, String typeVar, String instanceVar, String action) {
76         String type = refProp.getStringValue(PERM_PREFIX + typeVar);
77         String instance = refProp.getStringValue(PERM_INSTANCE);
78
79         if (null == type || type.isEmpty()) {
80             //authorization is turned off, since the permission is not defined
81             return;
82         }
83         if (null != instanceVar && !instanceVar.isEmpty()) {
84             instance = instanceVar;
85         }
86         String principalName = PrincipalUtils.getPrincipalName();
87         SecureServicePermission perm = SecureServicePermission.create(type, instance, action);
88         Date startTime = new Date();
89         LoggingUtils.setTargetContext("Clamp", "authorize");
90         LoggingUtils.setTimeContext(startTime, new Date());
91         securityLogger.debug("checking if {} has permission: {}", principalName, perm);
92
93         if (!isUserPermitted(perm)){
94             String msg = principalName + " does not have permission: " + perm;
95             LoggingUtils.setErrorContext("100", "Authorization Error");
96             securityLogger.warn(msg);
97             throw new NotAuthorizedException(msg);
98         }
99     }
100
101     public boolean isUserPermitted(SecureServicePermission inPermission) {
102
103         String principalName = PrincipalUtils.getPrincipalName();
104         // check if the user has the permission key or the permission key with a
105         // combination of  all instance and/or all action.
106         if (hasRole(inPermission.getKey()) || hasRole(inPermission.getKeyAllInstance())) {
107             auditLogger.info("{} authorized because user has permission with * for instance: {}",
108                     principalName, inPermission.getKey());
109             return true;
110             // the rest of these don't seem to be required - isUserInRole method
111             // appears to take * as a wildcard
112         } else if (hasRole(inPermission.getKeyAllInstanceAction())) {
113             auditLogger.info("{} authorized because user has permission with * for instance and * for action: {}",
114                     principalName, inPermission.getKey());
115             return true;
116         } else if (hasRole(inPermission.getKeyAllAction())) {
117             auditLogger.info("{} authorized because user has permission with * for action: {}",
118                     principalName, inPermission.getKey());
119             return true;
120         } else {
121             return false;
122         }
123     }
124
125     protected boolean hasRole(String role) {
126         Authentication authentication = PrincipalUtils.getSecurityContext().getAuthentication();
127         if (authentication == null) {
128             return false;
129         }
130         for (GrantedAuthority auth : authentication.getAuthorities()) {
131             if (role.equals(auth.getAuthority())) {
132                 return true;
133             }
134         }
135         return false;
136     }
137
138 }