Upgrade spring/camel versions
[clamp.git] / src / main / java / org / onap / clamp / clds / service / SecureServiceBase.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2017 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.service;
25
26 import com.att.eelf.configuration.EELFLogger;
27 import com.att.eelf.configuration.EELFManager;
28
29 import java.util.Date;
30 import javax.ws.rs.NotAuthorizedException;
31
32 import org.onap.clamp.clds.util.LoggingUtils;
33 import org.springframework.security.core.Authentication;
34 import org.springframework.security.core.GrantedAuthority;
35 import org.springframework.security.core.context.SecurityContext;
36 import org.springframework.security.core.context.SecurityContextHolder;
37 import org.springframework.security.core.userdetails.UserDetails;
38
39 /**
40  * Base/abstract Service class. Implements shared security methods.
41  */
42 public abstract class SecureServiceBase {
43     protected static final EELFLogger logger          = EELFManager.getInstance().getLogger(SecureServiceBase.class);
44     protected static final EELFLogger auditLogger     = EELFManager.getInstance().getAuditLogger();
45     protected static final EELFLogger securityLogger  = EELFManager.getInstance().getSecurityLogger();
46
47     // By default we'll set it to a default handler
48     private static UserNameHandler    userNameHandler = new DefaultUserNameHandler();
49
50
51     private SecurityContext           securityContext = SecurityContextHolder.getContext();
52
53     /**
54      * Get the userId from AAF/CSP.
55      *
56      * @return
57      */
58     public String getUserId() {
59         return getUserName();
60     }
61
62     /**
63      * Get the Full name.
64      *
65      * @return
66      */
67     public String getUserName() {
68         String name = userNameHandler.retrieveUserName(securityContext);
69         Date startTime = new Date();
70         LoggingUtils.setTargetContext("CLDS", "getUserName");
71         LoggingUtils.setTimeContext(startTime, new Date());
72         securityLogger.debug("User logged into the CLDS system={}", name);
73         return name;
74     }
75
76     /**
77      * Get the principal name.
78      *
79      * @return
80      */
81     public String getPrincipalName() {
82         String principal = ((UserDetails)securityContext.getAuthentication().getPrincipal()).getUsername();
83         String name = "Not found";
84         if (principal != null) {
85             name = principal;
86         }
87         logger.debug("userPrincipal.getName()={}", name);
88         return name;
89     }
90
91     /**
92      * Check if user is authorized for the given the permission. Allow matches
93      * if user has a permission with an "*" in permission instance or permission
94      * action even if the permission to check has a specific value in those
95      * fields. For example: if the user has this permission: app-perm-type|*|*
96      * it will be authorized if the inPermission to check is:
97      * app-perm-type|dev|read
98      *
99      * @param inPermission
100      *            The permission to validate
101      * @return A boolean to indicate if the user has the permission to do
102      *         execute the inPermission
103      * @throws NotAuthorizedException
104      *             In case of issues with the permission test, error is returned
105      *             in this exception
106      */
107     public boolean isAuthorized(SecureServicePermission inPermission) throws NotAuthorizedException {
108         Date startTime = new Date();
109         LoggingUtils.setTargetContext("CLDS", "isAuthorized");
110         LoggingUtils.setTimeContext(startTime, new Date());
111         securityLogger.debug("checking if {} has permission: {}", getPrincipalName(), inPermission);
112         try {
113             return isUserPermitted(inPermission);
114         } catch (NotAuthorizedException nae) {
115             String msg = getPrincipalName() + " does not have permission: " + inPermission;
116             LoggingUtils.setErrorContext("100", "Authorization Error");
117             securityLogger.warn(msg);
118             throw new NotAuthorizedException(msg);
119         }
120     }
121
122     /**
123      * Check if user is authorized for the given aaf permission. Allow matches
124      * if user has a permission with an "*" in permission instance or permission
125      * action even if the permission to check has a specific value in those
126      * fields. For example: if the user has this permission: app-perm-type|*|*
127      * it will be authorized if the inPermission to check is:
128      * app-perm-type|dev|read
129      *
130      * @param inPermission
131      *            The permission to validate
132      * @return A boolean to indicate if the user has the permission to do
133      *         execute the inPermission
134      */
135     public boolean isAuthorizedNoException(SecureServicePermission inPermission) {
136         securityLogger.debug("checking if {} has permission: {}", getPrincipalName(), inPermission);
137         Date startTime = new Date();
138         LoggingUtils.setTargetContext("CLDS", "isAuthorizedNoException");
139         LoggingUtils.setTimeContext(startTime, new Date());
140         try {
141             return isUserPermitted(inPermission);
142         } catch (NotAuthorizedException nae) {
143             String msg = getPrincipalName() + " does not have permission: " + inPermission;
144             LoggingUtils.setErrorContext("100", "Authorization Error");
145             securityLogger.warn(msg);
146         }
147         return false;
148     }
149
150     /**
151      * This method can be used by the Application.class to set the
152      * UserNameHandler that must be used in this class. The UserNameHandler
153      * where to get the User name
154      *
155      * @param handler
156      *            The Handler impl to use
157      */
158     public static final void setUserNameHandler(UserNameHandler handler) {
159         if (handler != null) {
160             userNameHandler = handler;
161         }
162     }
163
164     public void setSecurityContext(SecurityContext securityContext) {
165         this.securityContext = securityContext;
166     }
167
168     private boolean isUserPermitted(SecureServicePermission inPermission) {
169         boolean authorized = false;
170         // check if the user has the permission key or the permission key with a
171         // combination of  all instance and/or all action.
172         if (hasRole(inPermission.getKey())) {
173             securityLogger.info("{} authorized for permission: {}", getPrincipalName(), inPermission.getKey());
174             authorized = true;
175             // the rest of these don't seem to be required - isUserInRole method
176             // appears to take * as a wildcard
177         } else if (hasRole(inPermission.getKeyAllInstance())) {
178             securityLogger.info("{} authorized because user has permission with * for instance: {}", getPrincipalName(), inPermission.getKey());
179             authorized = true;
180         } else if (hasRole(inPermission.getKeyAllInstanceAction())) {
181             securityLogger.info("{} authorized because user has permission with * for instance and * for action: {}", getPrincipalName(), inPermission.getKey());
182             authorized = true;
183         } else if (hasRole(inPermission.getKeyAllAction())) {
184             securityLogger.info("{} authorized because user has permission with * for action: {}", getPrincipalName(), inPermission.getKey());
185             authorized = true;
186         } else {
187             throw new NotAuthorizedException("");
188         }
189         return authorized;
190     }
191
192     protected boolean hasRole(String role) {
193         Authentication authentication = securityContext.getAuthentication();
194         if (authentication == null) {
195             return false;
196         }
197
198         for (GrantedAuthority auth : authentication.getAuthorities()) {
199             if (role.equals(auth.getAuthority()))
200                 return true;
201         }
202
203         return false;
204     }
205 }