Catalog alignment
[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
21 package org.openecomp.sdc.be.components.impl.aaf;
22
23
24 import org.aspectj.lang.JoinPoint;
25 import org.aspectj.lang.annotation.Aspect;
26 import org.aspectj.lang.annotation.Before;
27 import org.openecomp.sdc.be.components.impl.exceptions.ByActionStatusComponentException;
28 import org.openecomp.sdc.be.config.ConfigurationManager;
29 import org.openecomp.sdc.be.dao.api.ActionStatus;
30 import org.openecomp.sdc.be.servlets.BeGenericServlet;
31 import org.openecomp.sdc.common.log.wrappers.Logger;
32 import org.openecomp.sdc.common.util.ThreadLocalsHolder;
33
34 import javax.servlet.http.HttpServletRequest;
35
36 // aop id defined via application-context.xml. the annotations are only for test purposes
37 @Aspect
38 public class RoleAuthorizationHandler {
39
40     private static final Logger log = Logger.getLogger(RoleAuthorizationHandler.class);
41
42     private ConfigurationManager configurationManager = ConfigurationManager.getConfigurationManager();
43
44     @Before("@annotation(permissions)")
45     public void authorizeRole(JoinPoint joinPoint, PermissionAllowed permissions) {
46
47         if (isPermissionAuthenticationNeeded()) {
48             String methodName = joinPoint.getSignature().toShortString();
49             HttpServletRequest request = ((BeGenericServlet) joinPoint.getThis()).getServletRequest();
50             String[] perms = permissions.value();
51             logAuth(methodName, perms, true, null);
52             for (String perm : perms) {
53                 if (request.isUserInRole(getFullPermission(perm))) {
54                     logAuth(methodName, perms, false, true);
55                     return;
56                 }
57             }
58             logAuth(methodName, perms, false, false);
59             throw new ByActionStatusComponentException(ActionStatus.AUTH_FAILED);
60         }
61
62     }
63
64     private void logAuth(String methodName, String[] perms, boolean beforeAuth, Boolean success) {
65         if (beforeAuth)
66             log.trace("#{} - authorizing before invoking endpoint {}", methodName);
67         else {
68             String status = success ? "SUCCESS" : "FAILED";
69             log.trace("#{} - authorizing before invoking endpoint {}, Status: {}", methodName, status);
70         }
71     }
72
73     private String getFullPermission(String role) {
74         return AafPermission.getEnumByString(role).getFullPermission();
75     }
76
77     private boolean isPermissionAuthenticationNeeded() {
78         if (configurationManager.getConfiguration().getAafAuthNeeded() && ThreadLocalsHolder.isExternalRequest()) {
79             return true;
80         } else return false;
81     }
82 }