From 1b0e69117a85ada58e5c6642b47962293e3f8af1 Mon Sep 17 00:00:00 2001 From: vasraz Date: Thu, 25 May 2023 12:31:58 +0100 Subject: Remove need for USER_ID header Signed-off-by: Vasyl Razinkov Change-Id: I5dd1e34343bebec8a26786f402dc2b9b818e7f10 Issue-ID: SDC-4508 (cherry picked from commit 01cde8e3cd095919ba74ac1d5e750e4b6842ae64) --- .../openecomp/sdc/be/filters/ThreadLocalUtils.java | 39 +++++++++++++++------- .../scripts/sdcBePy/users/data/users.json | 9 +---- 2 files changed, 28 insertions(+), 20 deletions(-) (limited to 'catalog-be/src/main') diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/filters/ThreadLocalUtils.java b/catalog-be/src/main/java/org/openecomp/sdc/be/filters/ThreadLocalUtils.java index 42b0291c89..13abdd3546 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/filters/ThreadLocalUtils.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/filters/ThreadLocalUtils.java @@ -25,10 +25,14 @@ import java.util.List; import java.util.Optional; import java.util.Set; import javax.servlet.http.HttpServletRequest; + +import org.apache.commons.lang3.StringUtils; import org.onap.sdc.security.AuthenticationCookie; import org.onap.sdc.security.IUsersThreadLocalHolder; import org.onap.sdc.security.PortalClient; import org.onap.sdc.security.RestrictionAccessFilterException; +import org.openecomp.sdc.be.config.Configuration; +import org.openecomp.sdc.be.config.ConfigurationManager; import org.openecomp.sdc.be.model.User; import org.openecomp.sdc.be.user.UserBusinessLogic; import org.openecomp.sdc.common.api.Constants; @@ -41,15 +45,15 @@ public class ThreadLocalUtils implements IUsersThreadLocalHolder { private static final Logger log = Logger.getLogger(ThreadLocalUtils.class); @Autowired - PortalClient portalClient; + private PortalClient portalClient; @Autowired - UserBusinessLogic userBusinessLogic; + private UserBusinessLogic userBusinessLogic; @Override public void setUserContext(AuthenticationCookie authenticationCookie) { UserContext userContext; userContext = new UserContext(authenticationCookie.getUserID(), authenticationCookie.getRoles(), authenticationCookie.getFirstName(), - authenticationCookie.getLastName()); + authenticationCookie.getLastName()); ThreadLocalsHolder.setUserContext(userContext); } @@ -59,7 +63,7 @@ public class ThreadLocalUtils implements IUsersThreadLocalHolder { Set roles = null; try { final Optional userRolesFromPortalOptional = portalClient.fetchUserRolesFromPortal(userId); - if (userRolesFromPortalOptional.isPresent()){ + if (userRolesFromPortalOptional.isPresent()) { roles = new HashSet<>(List.of(userRolesFromPortalOptional.get())); } } catch (RestrictionAccessFilterException e) { @@ -74,17 +78,28 @@ public class ThreadLocalUtils implements IUsersThreadLocalHolder { } protected void setUserContextFromDB(HttpServletRequest httpRequest) { - String user_id = httpRequest.getHeader(Constants.USER_ID_HEADER); - //there are some internal request that have no user_id header e.g. healthcheck - if (user_id != null) { - updateUserContext(user_id); - } else { - log.debug("user_id value in req header is null, userContext will not be initialized"); + String userId = httpRequest.getHeader(Constants.USER_ID_HEADER); + final Configuration.BasicAuthConfig basicAuthConf = ConfigurationManager.getConfigurationManager().getConfiguration().getBasicAuth(); + if (StringUtils.isBlank(userId)) { + final String excludedUrls = basicAuthConf.getExcludedUrls(); + //there are some internal request that have no user_id header e.g. healthcheck + if (StringUtils.isBlank(excludedUrls) || !checkForExclusion(excludedUrls, httpRequest.getPathInfo())) { + log.info("UserId is empty"); + userId = "cs0008"; + } else { + log.debug("user_id value in req header is null, userContext will not be initialized"); + return; + } } + updateUserContext(userId); + } + + private boolean checkForExclusion(final String excludedUrls, final String pathInfo) { + return Arrays.stream(excludedUrls.split(";")).anyMatch(s -> s.endsWith(pathInfo)); } - private void updateUserContext(String user_id) { - User user = userBusinessLogic.getUser(user_id, false); + private void updateUserContext(String userId) { + User user = userBusinessLogic.getUser(userId, false); Set roles = new HashSet<>(Arrays.asList(user.getRole())); UserContext userContext = new UserContext(user.getUserId(), roles, user.getFirstName(), user.getLastName()); ThreadLocalsHolder.setUserContext(userContext); diff --git a/catalog-be/src/main/resources/scripts/sdcBePy/users/data/users.json b/catalog-be/src/main/resources/scripts/sdcBePy/users/data/users.json index ed3adafe66..0c9b556acf 100755 --- a/catalog-be/src/main/resources/scripts/sdcBePy/users/data/users.json +++ b/catalog-be/src/main/resources/scripts/sdcBePy/users/data/users.json @@ -6,13 +6,6 @@ "role": "ADMIN", "email": "demo@openecomp.org" }, - { - "userId": "gv0001", - "firstName": "Giuseppe", - "lastName": "Verdi", - "role": "GOVERNOR", - "email": "gv0001@openecomp.org" - }, { "userId": "jh0003", "firstName": "Jimmy", @@ -34,4 +27,4 @@ "role": "DESIGNER", "email": "cs0008r@openecomp.org" } -] \ No newline at end of file +] -- cgit 1.2.3-korg