2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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 * ===================================================================
24 package org.onap.clamp.clds.service;
26 import com.att.eelf.configuration.EELFLogger;
27 import com.att.eelf.configuration.EELFManager;
29 import java.security.Principal;
30 import java.util.Date;
32 import javax.ws.rs.NotAuthorizedException;
33 import javax.ws.rs.core.Context;
34 import javax.ws.rs.core.SecurityContext;
36 import org.onap.clamp.clds.util.LoggingUtils;
39 * Base/abstract Service class. Implements shared security methods.
41 public abstract class SecureServiceBase {
42 protected static final EELFLogger logger = EELFManager.getInstance().getLogger(SecureServiceBase.class);
43 protected static final EELFLogger auditLogger = EELFManager.getInstance().getAuditLogger();
44 protected static final EELFLogger securityLogger = EELFManager.getInstance().getSecurityLogger();
46 // By default we'll set it to a default handler
47 private static UserNameHandler userNameHandler = new DefaultUserNameHandler();
50 private SecurityContext securityContext;
53 * Get the userId from AAF/CSP.
57 public String getUserId() {
66 public String getUserName() {
67 String name = userNameHandler.retrieveUserName(securityContext);
68 Date startTime = new Date();
69 LoggingUtils.setTargetContext("CLDS", "getUserName");
70 LoggingUtils.setTimeContext(startTime, new Date());
71 securityLogger.debug("User logged into the CLDS system={}", name);
76 * Get the principal name.
80 public String getPrincipalName() {
81 Principal principal = securityContext.getUserPrincipal();
82 String name = "Not found";
83 if (principal != null) {
84 name = principal.getName();
86 logger.debug("userPrincipal.getName()={}", name);
91 * Check if user is authorized for the given the permission. Allow matches
92 * if user has a permission with an "*" in permission instance or permission
93 * action even if the permission to check has a specific value in those
94 * fields. For example: if the user has this permission: app-perm-type|*|*
95 * it will be authorized if the inPermission to check is:
96 * app-perm-type|dev|read
99 * The permission to validate
100 * @return A boolean to indicate if the user has the permission to do
101 * execute the inPermission
102 * @throws NotAuthorizedException
103 * In case of issues with the permission test, error is returned
106 public boolean isAuthorized(SecureServicePermission inPermission) throws NotAuthorizedException {
107 Date startTime = new Date();
108 LoggingUtils.setTargetContext("CLDS", "isAuthorized");
109 LoggingUtils.setTimeContext(startTime, new Date());
110 securityLogger.debug("checking if {} has permission: {}", getPrincipalName(), inPermission);
112 return isUserPermitted(inPermission);
113 } catch (NotAuthorizedException nae) {
114 String msg = getPrincipalName() + " does not have permission: " + inPermission;
115 LoggingUtils.setErrorContext("100", "Authorization Error");
116 securityLogger.warn(msg);
117 throw new NotAuthorizedException(msg);
122 * Check if user is authorized for the given aaf permission. Allow matches
123 * if user has a permission with an "*" in permission instance or permission
124 * action even if the permission to check has a specific value in those
125 * fields. For example: if the user has this permission: app-perm-type|*|*
126 * it will be authorized if the inPermission to check is:
127 * app-perm-type|dev|read
129 * @param inPermission
130 * The permission to validate
131 * @return A boolean to indicate if the user has the permission to do
132 * execute the inPermission
134 public boolean isAuthorizedNoException(SecureServicePermission inPermission) {
135 securityLogger.debug("checking if {} has permission: {}", getPrincipalName(), inPermission);
136 Date startTime = new Date();
137 LoggingUtils.setTargetContext("CLDS", "isAuthorizedNoException");
138 LoggingUtils.setTimeContext(startTime, new Date());
140 return isUserPermitted(inPermission);
141 } catch (NotAuthorizedException nae) {
142 String msg = getPrincipalName() + " does not have permission: " + inPermission;
143 LoggingUtils.setErrorContext("100", "Authorization Error");
144 securityLogger.warn(msg);
150 * This method can be used by the Application.class to set the
151 * UserNameHandler that must be used in this class. The UserNameHandler
152 * where to get the User name
155 * The Handler impl to use
157 public static final void setUserNameHandler(UserNameHandler handler) {
158 if (handler != null) {
159 userNameHandler = handler;
163 public void setSecurityContext(SecurityContext securityContext) {
164 this.securityContext = securityContext;
167 private boolean isUserPermitted(SecureServicePermission inPermission) throws NotAuthorizedException {
168 boolean authorized = false;
169 // check if the user has the permission key or the permission key with a
170 // combination of all instance and/or all action.
171 if (securityContext.isUserInRole(inPermission.getKey())) {
172 securityLogger.info("{} authorized for permission: {}", getPrincipalName(), inPermission.getKey());
174 // the rest of these don't seem to be required - isUserInRole method
175 // appears to take * as a wildcard
176 } else if (securityContext.isUserInRole(inPermission.getKeyAllInstance())) {
177 securityLogger.info("{} authorized because user has permission with * for instance: {}", getPrincipalName(), inPermission.getKey());
179 } else if (securityContext.isUserInRole(inPermission.getKeyAllInstanceAction())) {
180 securityLogger.info("{} authorized because user has permission with * for instance and * for action: {}", getPrincipalName(), inPermission.getKey());
182 } else if (securityContext.isUserInRole(inPermission.getKeyAllAction())) {
183 securityLogger.info("{} authorized because user has permission with * for action: {}", getPrincipalName(), inPermission.getKey());
186 throw new NotAuthorizedException("");