From eb6ab9ef3552758626a3182b40fbe3a1c8fd0acf Mon Sep 17 00:00:00 2001 From: Maduranga Siriwardena Date: Thu, 15 Feb 2024 21:18:05 +0530 Subject: [PATCH] Delay the config parser data read --- .../ContextRewriteValveServiceComponent.java | 160 ++++++++++++++++++ ...extRewriteValveServiceComponentHolder.java | 61 +++++++ .../OrganizationContextRewriteValve.java | 53 +----- .../valve/TenantContextRewriteValve.java | 94 +--------- 4 files changed, 230 insertions(+), 138 deletions(-) diff --git a/components/org.wso2.carbon.identity.context.rewrite.valve/src/main/java/org/wso2/carbon/identity/context/rewrite/internal/ContextRewriteValveServiceComponent.java b/components/org.wso2.carbon.identity.context.rewrite.valve/src/main/java/org/wso2/carbon/identity/context/rewrite/internal/ContextRewriteValveServiceComponent.java index 66933d9a..f4c2efd7 100644 --- a/components/org.wso2.carbon.identity.context.rewrite.valve/src/main/java/org/wso2/carbon/identity/context/rewrite/internal/ContextRewriteValveServiceComponent.java +++ b/components/org.wso2.carbon.identity.context.rewrite.valve/src/main/java/org/wso2/carbon/identity/context/rewrite/internal/ContextRewriteValveServiceComponent.java @@ -21,6 +21,10 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.osgi.service.component.ComponentContext; +import org.wso2.carbon.identity.context.rewrite.bean.OrganizationRewriteContext; +import org.wso2.carbon.identity.context.rewrite.bean.RewriteContext; +import org.wso2.carbon.identity.core.util.IdentityConfigParser; +import org.wso2.carbon.identity.core.util.IdentityCoreInitializedEvent; import org.wso2.carbon.identity.organization.management.service.OrganizationManagementInitialize; import org.wso2.carbon.user.core.service.RealmService; import org.osgi.service.component.annotations.Activate; @@ -37,6 +41,16 @@ import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.regex.Pattern; + +import static org.wso2.carbon.identity.context.rewrite.constant.RewriteConstants.ORGANIZATION_PATH_PARAM; +import static org.wso2.carbon.identity.core.util.IdentityCoreConstants.ENABLE_TENANT_QUALIFIED_URLS; @Component( name = "identity.context.rewrite.valve.component", @@ -51,6 +65,16 @@ protected void activate(ComponentContext context) { log.debug("ContextRewriteValveServiceComponent is activated."); } loadPageNotFoundErrorPage(); + ContextRewriteValveServiceComponentHolder.getInstance() + .setOrganizationRewriteContexts(getOrgContextsToRewrite()); + // Initialize the tenant context rewrite valve. + ContextRewriteValveServiceComponentHolder.getInstance().setContextsToRewrite(getContextsToRewrite()); + ContextRewriteValveServiceComponentHolder.getInstance() + .setContextListToOverwriteDispatch(getContextListToOverwriteDispatchLocation()); + ContextRewriteValveServiceComponentHolder.getInstance() + .setIgnorePathListForOverwriteDispatch(getIgnorePathListForOverwriteDispatch()); + ContextRewriteValveServiceComponentHolder.getInstance() + .setTenantQualifiedUrlsEnabled(isTenantQualifiedUrlsEnabled()); } @Deactivate @@ -100,6 +124,23 @@ protected void unsetOrganizationManagementEnablingService( ContextRewriteValveServiceComponentHolder.getInstance().setOrganizationManagementEnable(null); } + @Reference( + name = "identityCoreInitializedEventService", + service = IdentityCoreInitializedEvent.class, + cardinality = ReferenceCardinality.MANDATORY, + policy = ReferencePolicy.DYNAMIC, + unbind = "unsetIdentityCoreInitializedEventService" + ) + protected void setIdentityCoreInitializedEventService(IdentityCoreInitializedEvent identityCoreInitializedEvent) { + /* reference IdentityCoreInitializedEvent service to guarantee that this component will wait until identity core + is started */ + } + + protected void unsetIdentityCoreInitializedEventService(IdentityCoreInitializedEvent identityCoreInitializedEvent) { + /* reference IdentityCoreInitializedEvent service to guarantee that this component will wait until identity core + is started */ + } + private void loadPageNotFoundErrorPage() { String errorPage = "Page Not Found"; @@ -123,5 +164,124 @@ private void loadPageNotFoundErrorPage() { } ContextRewriteValveServiceComponentHolder.getInstance().setPageNotFoundErrorPage(errorPage); } + + private List getOrgContextsToRewrite() { + + List organizationRewriteContexts = new ArrayList<>(); + Map configuration = IdentityConfigParser.getInstance().getConfiguration(); + Object webAppBasePathContexts = configuration.get("OrgContextsToRewrite.WebApp.Context.BasePath"); + setOrganizationRewriteContexts(organizationRewriteContexts, webAppBasePathContexts, true); + + Object webAppSubPathContexts = configuration.get("OrgContextsToRewrite.WebApp.Context.SubPaths.Path"); + setSubPathContexts(organizationRewriteContexts, webAppSubPathContexts); + + Object servletBasePathContexts = configuration.get("OrgContextsToRewrite.Servlet.Context"); + setOrganizationRewriteContexts(organizationRewriteContexts, servletBasePathContexts, false); + + return organizationRewriteContexts; + } + + private void setOrganizationRewriteContexts(List organizationRewriteContexts, + Object basePathContexts, boolean isWebApp) { + + if (basePathContexts != null) { + if (basePathContexts instanceof ArrayList) { + for (String context : (ArrayList) basePathContexts) { + organizationRewriteContexts.add(new OrganizationRewriteContext(isWebApp, context)); + } + } else { + organizationRewriteContexts.add(new OrganizationRewriteContext(isWebApp, + basePathContexts.toString())); + } + } + } + + private void setSubPathContexts(List organizationRewriteContexts, + Object subPathContexts) { + + if (subPathContexts != null) { + if (subPathContexts instanceof ArrayList) { + for (String subPath : (ArrayList) subPathContexts) { + Optional maybeOrgRewriteContext = organizationRewriteContexts.stream() + .filter(rewriteContext -> subPath.startsWith(rewriteContext.getContext())) + .max(Comparator.comparingInt(rewriteContext -> rewriteContext.getContext().length())); + maybeOrgRewriteContext.ifPresent( + organizationRewriteContext -> organizationRewriteContext.addSubPath( + Pattern.compile("^" + ORGANIZATION_PATH_PARAM + "([^/]+)" + subPath))); + } + } + } + } + + private List getContextsToRewrite() { + + List rewriteContexts = new ArrayList<>(); + Map configuration = IdentityConfigParser.getInstance().getConfiguration(); + Object webAppContexts = configuration.get("TenantContextsToRewrite.WebApp.Context"); + if (webAppContexts != null) { + if (webAppContexts instanceof ArrayList) { + for (String context : (ArrayList) webAppContexts) { + rewriteContexts.add(new RewriteContext(true, context)); + } + } else { + rewriteContexts.add(new RewriteContext(true, webAppContexts.toString())); + } + } + + Object servletContexts = configuration.get("TenantContextsToRewrite.Servlet.Context"); + if (servletContexts != null) { + if (servletContexts instanceof ArrayList) { + for (String context : (ArrayList) servletContexts) { + rewriteContexts.add(new RewriteContext(false, context)); + } + } else { + rewriteContexts.add(new RewriteContext(false, servletContexts.toString())); + } + } + return rewriteContexts; + } + + private boolean isTenantQualifiedUrlsEnabled() { + + Map configuration = IdentityConfigParser.getInstance().getConfiguration(); + String enableTenantQualifiedUrls = (String) configuration.get(ENABLE_TENANT_QUALIFIED_URLS); + return Boolean.parseBoolean(enableTenantQualifiedUrls); + } + + /** + * Get context list to overwrite dispatch location. + * + * @return list of contexts. + */ + private List getContextListToOverwriteDispatchLocation() { + + return getConfigValues("TenantContextsToRewrite.OverwriteDispatch.Context"); + } + + /** + * Get path list to ignore for overwrite dispatch. + * + * @return list of paths. + */ + private List getIgnorePathListForOverwriteDispatch() { + + return getConfigValues("TenantContextsToRewrite.OverwriteDispatch.IgnorePath"); + } + + private List getConfigValues(String elementPath) { + + Map configuration = IdentityConfigParser.getInstance().getConfiguration(); + Object elements = configuration.get(elementPath); + if (elements != null) { + List configValues = new ArrayList<>(); + if (elements instanceof List) { + configValues.addAll((List) elements); + } else { + configValues.add(elements.toString()); + } + return configValues; + } + return Collections.emptyList(); + } } diff --git a/components/org.wso2.carbon.identity.context.rewrite.valve/src/main/java/org/wso2/carbon/identity/context/rewrite/internal/ContextRewriteValveServiceComponentHolder.java b/components/org.wso2.carbon.identity.context.rewrite.valve/src/main/java/org/wso2/carbon/identity/context/rewrite/internal/ContextRewriteValveServiceComponentHolder.java index 2b3f4716..f15731d5 100644 --- a/components/org.wso2.carbon.identity.context.rewrite.valve/src/main/java/org/wso2/carbon/identity/context/rewrite/internal/ContextRewriteValveServiceComponentHolder.java +++ b/components/org.wso2.carbon.identity.context.rewrite.valve/src/main/java/org/wso2/carbon/identity/context/rewrite/internal/ContextRewriteValveServiceComponentHolder.java @@ -20,9 +20,13 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.identity.context.rewrite.bean.OrganizationRewriteContext; +import org.wso2.carbon.identity.context.rewrite.bean.RewriteContext; import org.wso2.carbon.identity.organization.management.service.OrganizationManagementInitialize; import org.wso2.carbon.user.core.service.RealmService; +import java.util.List; + public class ContextRewriteValveServiceComponentHolder { private static ContextRewriteValveServiceComponentHolder instance = new ContextRewriteValveServiceComponentHolder(); @@ -31,6 +35,12 @@ public class ContextRewriteValveServiceComponentHolder { private static final Log log = LogFactory.getLog(ContextRewriteValveServiceComponentHolder.class); private boolean isOrganizationManagementEnable; + private List organizationRewriteContexts; + private List contextsToRewrite; + private List contextListToOverwriteDispatch; + private List ignorePathListForOverwriteDispatch; + private boolean isTenantQualifiedUrlsEnabled; + private ContextRewriteValveServiceComponentHolder() { } @@ -91,4 +101,55 @@ public void setOrganizationManagementEnable( isOrganizationManagementEnable = organizationManagementInitializeService.isOrganizationManagementEnabled(); } } + + public List getOrganizationRewriteContexts() { + + return organizationRewriteContexts; + } + + public void setOrganizationRewriteContexts( + List organizationRewriteContexts) { + + this.organizationRewriteContexts = organizationRewriteContexts; + } + + public List getContextsToRewrite() { + + return contextsToRewrite; + } + + public void setContextsToRewrite(List contextsToRewrite) { + + this.contextsToRewrite = contextsToRewrite; + } + + public List getContextListToOverwriteDispatch() { + + return contextListToOverwriteDispatch; + } + + public void setContextListToOverwriteDispatch(List contextListToOverwriteDispatch) { + + this.contextListToOverwriteDispatch = contextListToOverwriteDispatch; + } + + public List getIgnorePathListForOverwriteDispatch() { + + return ignorePathListForOverwriteDispatch; + } + + public void setIgnorePathListForOverwriteDispatch(List ignorePathListForOverwriteDispatch) { + + this.ignorePathListForOverwriteDispatch = ignorePathListForOverwriteDispatch; + } + + public boolean isTenantQualifiedUrlsEnabled() { + + return isTenantQualifiedUrlsEnabled; + } + + public void setTenantQualifiedUrlsEnabled(boolean tenantQualifiedUrlsEnabled) { + + this.isTenantQualifiedUrlsEnabled = tenantQualifiedUrlsEnabled; + } } diff --git a/components/org.wso2.carbon.identity.context.rewrite.valve/src/main/java/org/wso2/carbon/identity/context/rewrite/valve/OrganizationContextRewriteValve.java b/components/org.wso2.carbon.identity.context.rewrite.valve/src/main/java/org/wso2/carbon/identity/context/rewrite/valve/OrganizationContextRewriteValve.java index c70086ab..fb504ead 100644 --- a/components/org.wso2.carbon.identity.context.rewrite.valve/src/main/java/org/wso2/carbon/identity/context/rewrite/valve/OrganizationContextRewriteValve.java +++ b/components/org.wso2.carbon.identity.context.rewrite.valve/src/main/java/org/wso2/carbon/identity/context/rewrite/valve/OrganizationContextRewriteValve.java @@ -57,13 +57,11 @@ */ public class OrganizationContextRewriteValve extends ValveBase { - private static List orgContextsToRewrite; @Override protected synchronized void startInternal() throws LifecycleException { super.startInternal(); - orgContextsToRewrite = getOrgContextsToRewrite(); } @Override @@ -85,7 +83,8 @@ public void invoke(Request request, Response response) throws IOException, Servl if (ContextRewriteValveServiceComponentHolder.getInstance().isOrganizationManagementEnabled() && StringUtils.startsWith(requestURI, ORGANIZATION_PATH_PARAM)) { - for (OrganizationRewriteContext organizationRewriteContext : orgContextsToRewrite) { + for (OrganizationRewriteContext organizationRewriteContext + : ContextRewriteValveServiceComponentHolder.getInstance().getOrganizationRewriteContexts()) { Pattern orgPattern = organizationRewriteContext.getOrgContextPattern(); if (orgPattern.matcher(requestURI).find() || orgPattern.matcher(requestURI + "/").find()) { subPathsConfigured = false; @@ -158,52 +157,4 @@ private void unsetMDCThreadLocals() { MDC.remove(TENANT_DOMAIN); MDC.remove(TENANT_ID); } - - private List getOrgContextsToRewrite() { - - List organizationRewriteContexts = new ArrayList<>(); - Map configuration = IdentityConfigParser.getInstance().getConfiguration(); - Object webAppBasePathContexts = configuration.get("OrgContextsToRewrite.WebApp.Context.BasePath"); - setOrganizationRewriteContexts(organizationRewriteContexts, webAppBasePathContexts, true); - - Object webAppSubPathContexts = configuration.get("OrgContextsToRewrite.WebApp.Context.SubPaths.Path"); - setSubPathContexts(organizationRewriteContexts, webAppSubPathContexts); - - Object servletBasePathContexts = configuration.get("OrgContextsToRewrite.Servlet.Context"); - setOrganizationRewriteContexts(organizationRewriteContexts, servletBasePathContexts, false); - - return organizationRewriteContexts; - } - - private void setOrganizationRewriteContexts(List organizationRewriteContexts, - Object basePathContexts, boolean isWebApp) { - - if (basePathContexts != null) { - if (basePathContexts instanceof ArrayList) { - for (String context : (ArrayList) basePathContexts) { - organizationRewriteContexts.add(new OrganizationRewriteContext(isWebApp, context)); - } - } else { - organizationRewriteContexts.add(new OrganizationRewriteContext(isWebApp, - basePathContexts.toString())); - } - } - } - - private void setSubPathContexts(List organizationRewriteContexts, - Object subPathContexts) { - - if (subPathContexts != null) { - if (subPathContexts instanceof ArrayList) { - for (String subPath : (ArrayList) subPathContexts) { - Optional maybeOrgRewriteContext = organizationRewriteContexts.stream() - .filter(rewriteContext -> subPath.startsWith(rewriteContext.getContext())) - .max(Comparator.comparingInt(rewriteContext -> rewriteContext.getContext().length())); - maybeOrgRewriteContext.ifPresent( - organizationRewriteContext -> organizationRewriteContext.addSubPath( - Pattern.compile("^" + ORGANIZATION_PATH_PARAM + "([^/]+)" + subPath))); - } - } - } - } } diff --git a/components/org.wso2.carbon.identity.context.rewrite.valve/src/main/java/org/wso2/carbon/identity/context/rewrite/valve/TenantContextRewriteValve.java b/components/org.wso2.carbon.identity.context.rewrite.valve/src/main/java/org/wso2/carbon/identity/context/rewrite/valve/TenantContextRewriteValve.java index e956cdc8..43e34de7 100644 --- a/components/org.wso2.carbon.identity.context.rewrite.valve/src/main/java/org/wso2/carbon/identity/context/rewrite/valve/TenantContextRewriteValve.java +++ b/components/org.wso2.carbon.identity.context.rewrite.valve/src/main/java/org/wso2/carbon/identity/context/rewrite/valve/TenantContextRewriteValve.java @@ -58,11 +58,6 @@ public class TenantContextRewriteValve extends ValveBase { - private static List contextsToRewrite; - private static List contextListToOverwriteDispatch; - private static List ignorePathListForOverwriteDispatch; - private static List organizationRoutingOnlySupportedAPIPaths; - private boolean isTenantQualifiedUrlsEnabled; private TenantManager tenantManager; private static final Log log = LogFactory.getLog(TenantContextRewriteValve.class); @@ -71,12 +66,6 @@ public class TenantContextRewriteValve extends ValveBase { protected synchronized void startInternal() throws LifecycleException { super.startInternal(); - // Initialize the tenant context rewrite valve. - contextsToRewrite = getContextsToRewrite(); - contextListToOverwriteDispatch = getContextListToOverwriteDispatchLocation(); - ignorePathListForOverwriteDispatch = getIgnorePathListForOverwriteDispatch(); - isTenantQualifiedUrlsEnabled = isTenantQualifiedUrlsEnabled(); - } @Override @@ -95,7 +84,7 @@ public void invoke(Request request, Response response) throws IOException, Servl String contextTenantDomain = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain(); //Get the rewrite contexts and check whether request URI contains any of rewrite contains. - for (RewriteContext context : contextsToRewrite) { + for (RewriteContext context : ContextRewriteValveServiceComponentHolder.getInstance().getContextsToRewrite()) { Pattern patternTenant = context.getTenantContextPattern(); Pattern patternSuperTenant = context.getBaseContextPattern(); if (patternTenant.matcher(requestURI).find() || patternTenant.matcher(requestURI + "/").find()) { @@ -103,8 +92,9 @@ public void invoke(Request request, Response response) throws IOException, Servl isWebApp = context.isWebApp(); contextToForward = context.getContext(); break; - } else if (isTenantQualifiedUrlsEnabled && (patternSuperTenant.matcher(requestURI).find() || - patternSuperTenant.matcher(requestURI + "/").find())) { + } else if (ContextRewriteValveServiceComponentHolder.getInstance().isTenantQualifiedUrlsEnabled() + && (patternSuperTenant.matcher(requestURI).find() + || patternSuperTenant.matcher(requestURI + "/").find())) { IdentityUtil.threadLocalProperties.get().put(TENANT_NAME_FROM_CONTEXT, contextTenantDomain); break; } @@ -138,7 +128,7 @@ public void invoke(Request request, Response response) throws IOException, Servl requestURI.contains(ORGANIZATION_PATH_PARAM)) { dispatchLocation = "/o" + dispatchLocation; } - if (contextListToOverwriteDispatch.contains(contextToForward) && !isIgnorePath(dispatchLocation)) { + if (ContextRewriteValveServiceComponentHolder.getInstance().getContextListToOverwriteDispatch().contains(contextToForward) && !isIgnorePath(dispatchLocation)) { dispatchLocation = "/"; } @@ -182,80 +172,10 @@ private void unsetMDCThreadLocals() { MDC.remove(TENANT_ID); } - private boolean isTenantQualifiedUrlsEnabled() { - - Map configuration = IdentityConfigParser.getInstance().getConfiguration(); - String enableTenantQualifiedUrls = (String) configuration.get(ENABLE_TENANT_QUALIFIED_URLS); - return Boolean.parseBoolean(enableTenantQualifiedUrls); - } - - private List getContextsToRewrite() { - - List rewriteContexts = new ArrayList<>(); - Map configuration = IdentityConfigParser.getInstance().getConfiguration(); - Object webAppContexts = configuration.get("TenantContextsToRewrite.WebApp.Context"); - if (webAppContexts != null) { - if (webAppContexts instanceof ArrayList) { - for (String context : (ArrayList) webAppContexts) { - rewriteContexts.add(new RewriteContext(true, context)); - } - } else { - rewriteContexts.add(new RewriteContext(true, webAppContexts.toString())); - } - } - - Object servletContexts = configuration.get("TenantContextsToRewrite.Servlet.Context"); - if (servletContexts != null) { - if (servletContexts instanceof ArrayList) { - for (String context : (ArrayList) servletContexts) { - rewriteContexts.add(new RewriteContext(false, context)); - } - } else { - rewriteContexts.add(new RewriteContext(false, servletContexts.toString())); - } - } - return rewriteContexts; - } - - /** - * Get context list to overwrite dispatch location. - * - * @return list of contexts. - */ - private List getContextListToOverwriteDispatchLocation() { - - return getConfigValues("TenantContextsToRewrite.OverwriteDispatch.Context"); - } - - /** - * Get path list to ignore for overwrite dispatch. - * - * @return list of paths. - */ - private List getIgnorePathListForOverwriteDispatch() { - - return getConfigValues("TenantContextsToRewrite.OverwriteDispatch.IgnorePath"); - } - - private List getConfigValues(String elementPath) { - - Map configuration = IdentityConfigParser.getInstance().getConfiguration(); - Object elements = configuration.get(elementPath); - if (elements != null) { - List configValues = new ArrayList<>(); - if (elements instanceof List) { - configValues.addAll((List) elements); - } else { - configValues.add(elements.toString()); - } - return configValues; - } - return Collections.emptyList(); - } - private boolean isIgnorePath(String dispatchLocation) { - for (String path : ignorePathListForOverwriteDispatch) { + for (String path : ContextRewriteValveServiceComponentHolder.getInstance() + .getIgnorePathListForOverwriteDispatch()) { if (dispatchLocation.startsWith(path)) { return true; }