Rework the CldsService (rest apis)
[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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  */
23
24 package org.onap.clamp.clds.service;
25
26 import java.security.Principal;
27
28 import javax.ws.rs.NotAuthorizedException;
29 import javax.ws.rs.core.Context;
30 import javax.ws.rs.core.SecurityContext;
31
32 import org.onap.clamp.clds.util.LoggingUtils;
33
34 import com.att.eelf.configuration.EELFLogger;
35 import com.att.eelf.configuration.EELFManager;
36
37 /**
38  * Base/abstract Service class. Implements shared security methods.
39  */
40 public abstract class SecureServiceBase {
41     protected static final EELFLogger       logger          = EELFManager.getInstance().getLogger(SecureServiceBase.class);
42     protected static final EELFLogger auditLogger     = EELFManager.getInstance().getAuditLogger();
43
44     // By default we'll set it to a default handler
45     private static UserNameHandler  userNameHandler = new DefaultUserNameHandler();
46
47     @Context
48     private SecurityContext         securityContext;
49
50     /**
51      * Get the userId from AAF/CSP.
52      *
53      * @return
54      */
55     public String getUserId() {
56         return getUserName();
57     }
58
59     /**
60      * Get the Full name.
61      *
62      * @return
63      */
64     public String getUserName() {
65         String name = userNameHandler.retrieveUserName(securityContext);
66         logger.debug("userName={}", name);
67         return name;
68     }
69
70     /**
71      * Get the principal name.
72      *
73      * @return
74      */
75     public String getPrincipalName() {
76         Principal principal = securityContext.getUserPrincipal();
77         String name = "Not found";
78         if (principal != null) {
79             name = principal.getName();
80         }
81         logger.debug("userPrincipal.getName()={}", name);
82         return name;
83     }
84
85     /**
86      * Check if user is authorized for the given the permission. Allow matches
87      * if user has a permission with an "*" in permission instance or permission
88      * action even if the permission to check has a specific value in those
89      * fields. For example: if the user has this permission: app-perm-type|*|*
90      * it will be authorized if the inPermission to check is:
91      * app-perm-type|dev|read
92      *
93      * @param inPermission
94      * @return
95      * @throws NotAuthorizedException
96      */
97     public boolean isAuthorized(SecureServicePermission inPermission) throws NotAuthorizedException {
98         boolean authorized = false;
99         logger.debug("checking if {} has permission: {}", getPrincipalName(), inPermission);
100         // check if the user has the permission key or the permission key with a
101         // combination of all instance and/or all action.
102         if (securityContext.isUserInRole(inPermission.getKey())) {
103             logger.info("{} authorized for permission: {}", getPrincipalName(), inPermission.getKey());
104             authorized = true;
105             // the rest of these don't seem to be required - isUserInRole method
106             // appears to take * as a wildcard
107         } else if (securityContext.isUserInRole(inPermission.getKeyAllInstance())) {
108             logger.info("{} authorized because user has permission with * for instance: {}", getPrincipalName(),
109                     inPermission.getKey());
110             authorized = true;
111         } else if (securityContext.isUserInRole(inPermission.getKeyAllInstanceAction())) {
112             logger.info("{} authorized because user has permission with * for instance and * for action: {}",
113                     getPrincipalName(), inPermission.getKey());
114             authorized = true;
115         } else if (securityContext.isUserInRole(inPermission.getKeyAllAction())) {
116             logger.info("{} authorized because user has permission with * for action: {}", getPrincipalName(),
117                     inPermission.getKey());
118             authorized = true;
119         } else {
120             String msg = getPrincipalName() + " does not have permission: " + inPermission;
121             LoggingUtils.setErrorContext("100", "Authorization Error");
122             logger.warn(msg);
123             throw new NotAuthorizedException(msg);
124         }
125         return authorized;
126     }
127
128     /**
129      * Check if user is authorized for the given aaf permission. Allow matches
130      * if user has a permission with an "*" in permission instance or permission
131      * action even if the permission to check has a specific value in those
132      * fields. For example: if the user has this permission: app-perm-type|*|*
133      * it will be authorized if the inPermission to check is:
134      * app-perm-type|dev|read
135      *
136      * @param aafPermission
137      * @return
138      * @throws NotAuthorizedException
139      */
140     public boolean isAuthorizedNoException(SecureServicePermission inPermission) throws NotAuthorizedException {
141         boolean authorized = false;
142         logger.debug("checking if {} has permission: {}", getPrincipalName(), inPermission);
143         // check if the user has the permission key or the permission key with a
144         // combination of all instance and/or all action.
145         if (securityContext.isUserInRole(inPermission.getKey())) {
146             logger.info("{} authorized for permission: {}", getPrincipalName(), inPermission.getKey());
147             authorized = true;
148             // the rest of these don't seem to be required - isUserInRole method
149             // appears to take * as a wildcard
150         } else if (securityContext.isUserInRole(inPermission.getKeyAllInstance())) {
151             logger.info("{} authorized because user has permission with * for instance: {}", getPrincipalName(),
152                     inPermission.getKey());
153             authorized = true;
154         } else if (securityContext.isUserInRole(inPermission.getKeyAllInstanceAction())) {
155             logger.info("{} authorized because user has permission with * for instance and * for action: {}",
156                     getPrincipalName(), inPermission.getKey());
157             authorized = true;
158         } else if (securityContext.isUserInRole(inPermission.getKeyAllAction())) {
159             logger.info("{} authorized because user has permission with * for action: {}", getPrincipalName(),
160                     inPermission.getKey());
161             authorized = true;
162         } else {
163             String msg = getPrincipalName() + " does not have permission: " + inPermission;
164             LoggingUtils.setErrorContext("100", "Authorization Error");
165             logger.warn(msg);
166         }
167         return authorized;
168     }
169
170     public static final void setUserNameHandler(UserNameHandler handler) {
171         if (handler != null) {
172             userNameHandler = handler;
173         }
174     }
175 }