Fix use of encrypted user in backend
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / filters / ThreadLocalUtils.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.filters;
21
22 import java.util.Arrays;
23 import java.util.HashSet;
24 import java.util.Set;
25 import javax.servlet.http.HttpServletRequest;
26 import org.onap.sdc.security.AuthenticationCookie;
27 import org.onap.sdc.security.IUsersThreadLocalHolder;
28 import org.onap.sdc.security.PortalClient;
29 import org.onap.sdc.security.RestrictionAccessFilterException;
30 import org.openecomp.sdc.be.model.User;
31 import org.openecomp.sdc.be.user.UserBusinessLogic;
32 import org.openecomp.sdc.common.api.Constants;
33 import org.openecomp.sdc.common.datastructure.UserContext;
34 import org.openecomp.sdc.common.log.wrappers.Logger;
35 import org.openecomp.sdc.common.util.ThreadLocalsHolder;
36 import org.springframework.beans.factory.annotation.Autowired;
37
38 public class ThreadLocalUtils implements IUsersThreadLocalHolder {
39
40     private static final Logger log = Logger.getLogger(ThreadLocalUtils.class);
41     @Autowired
42     PortalClient portalClient;
43     @Autowired
44     UserBusinessLogic userBusinessLogic;
45
46     @Override
47     public void setUserContext(AuthenticationCookie authenticationCookie) {
48         UserContext userContext;
49         userContext = new UserContext(authenticationCookie.getUserID(), authenticationCookie.getRoles(), authenticationCookie.getFirstName(),
50             authenticationCookie.getLastName());
51         ThreadLocalsHolder.setUserContext(userContext);
52     }
53
54     protected void setUserContext(HttpServletRequest httpRequest) {
55         String user_id = httpRequest.getHeader(Constants.USER_ID_HEADER);
56         if (user_id != null) {
57             String userRolesFromPortal = null;
58             Set<String> roles = null;
59             try {
60                 userRolesFromPortal = portalClient.fetchUserRolesFromPortal(user_id);
61                 roles = new HashSet<>(Arrays.asList(userRolesFromPortal));
62             } catch (RestrictionAccessFilterException e) {
63                 log.debug("Failed to fetch user ID - {} from portal", user_id);
64                 log.debug(e.getMessage());
65             }
66             UserContext userContext = new UserContext(user_id, roles, null, null);
67             ThreadLocalsHolder.setUserContext(userContext);
68         } else {
69             log.debug("user_id value in req header is null, userContext will not be initialized");
70         }
71     }
72
73     protected void setUserContextFromDB(HttpServletRequest httpRequest) {
74         String user_id = httpRequest.getHeader(Constants.USER_ID_HEADER);
75         //there are some internal request that have no user_id header e.g. healthcheck
76         if (user_id != null) {
77             updateUserContext(user_id);
78         } else {
79             log.debug("user_id value in req header is null, userContext will not be initialized");
80         }
81     }
82
83     private void updateUserContext(String user_id) {
84         User user = userBusinessLogic.getUser(user_id, false);
85         Set<String> roles = new HashSet<>(Arrays.asList(user.getRole()));
86         UserContext userContext = new UserContext(user.getUserId(), roles, user.getFirstName(), user.getLastName());
87         ThreadLocalsHolder.setUserContext(userContext);
88     }
89 }