Fix NPE on service import
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / impl / aaf / RoleAuthorizationHandler.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20 package org.openecomp.sdc.be.components.impl.aaf;
21
22 import javax.servlet.http.HttpServletRequest;
23 import org.aspectj.lang.JoinPoint;
24 import org.aspectj.lang.annotation.Aspect;
25 import org.aspectj.lang.annotation.Before;
26 import org.openecomp.sdc.be.components.impl.exceptions.ByActionStatusComponentException;
27 import org.openecomp.sdc.be.config.ConfigurationManager;
28 import org.openecomp.sdc.be.dao.api.ActionStatus;
29 import org.openecomp.sdc.be.servlets.BeGenericServlet;
30 import org.openecomp.sdc.common.log.wrappers.Logger;
31 import org.openecomp.sdc.common.util.ThreadLocalsHolder;
32
33 // aop id defined via application-context.xml. the annotations are only for test purposes
34 @Aspect
35 public class RoleAuthorizationHandler {
36
37     private static final Logger log = Logger.getLogger(RoleAuthorizationHandler.class);
38     private final ConfigurationManager configurationManager = ConfigurationManager.getConfigurationManager();
39
40     @Before("@annotation(permissions)")
41     public void authorizeRole(JoinPoint joinPoint, PermissionAllowed permissions) {
42         if (isPermissionAuthenticationNeeded()) {
43             String methodName = joinPoint.getSignature().toShortString();
44             HttpServletRequest request = ((BeGenericServlet) joinPoint.getThis()).getServletRequest();
45             String[] perms = permissions.value();
46             logAuth(methodName, perms, true, null);
47             for (String perm : perms) {
48                 if (request.isUserInRole(getFullPermission(perm))) {
49                     logAuth(methodName, perms, false, true);
50                     return;
51                 }
52             }
53             logAuth(methodName, perms, false, false);
54             throw new ByActionStatusComponentException(ActionStatus.AUTH_FAILED);
55         }
56     }
57
58     private void logAuth(String methodName, String[] perms, boolean beforeAuth, Boolean success) {
59         if (beforeAuth) {
60             log.trace("#{} - authorizing before invoking endpoint {}", methodName);
61         } else {
62             String status = success ? "SUCCESS" : "FAILED";
63             log.trace("#{} - authorizing before invoking endpoint {}, Status: {}", methodName, status);
64         }
65     }
66
67     private String getFullPermission(String role) {
68         return AafPermission.getEnumByString(role).getFullPermission();
69     }
70
71     private boolean isPermissionAuthenticationNeeded() {
72         if (configurationManager.getConfiguration().getAafAuthNeeded() && ThreadLocalsHolder.isExternalRequest()) {
73             return true;
74         } else {
75             return false;
76         }
77     }
78 }