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