From 3eaf88c0eb12530c00448e24738c35da6db8a20d Mon Sep 17 00:00:00 2001 From: Will Ezell Date: Thu, 17 Jun 2021 16:21:40 -0400 Subject: [PATCH 1/6] sync the activator and prevent saml from initing twice --- .../java/com/dotcms/saml/osgi/Activator.java | 109 +++++++++++------- 1 file changed, 67 insertions(+), 42 deletions(-) diff --git a/src/main/java/com/dotcms/saml/osgi/Activator.java b/src/main/java/com/dotcms/saml/osgi/Activator.java index cd61285..09f1aa6 100644 --- a/src/main/java/com/dotcms/saml/osgi/Activator.java +++ b/src/main/java/com/dotcms/saml/osgi/Activator.java @@ -3,27 +3,25 @@ import java.util.HashMap; import java.util.Hashtable; import java.util.Map; - +import org.osgi.framework.BundleContext; +import org.osgi.framework.ServiceRegistration; import com.dotcms.auth.providers.saml.v1.DotSamlResource; import com.dotcms.filters.interceptor.FilterWebInterceptorProvider; import com.dotcms.filters.interceptor.WebInterceptorDelegate; import com.dotcms.filters.interceptor.saml.SamlWebInterceptor; import com.dotcms.rest.config.RestServiceUtil; import com.dotcms.saml.DotSamlProxyFactory; +import com.dotcms.saml.SamlServiceBuilder; +import com.dotcms.saml.service.impl.SamlServiceBuilderImpl; +import com.dotcms.saml.service.init.Initializer; +import com.dotcms.saml.service.init.SamlInitializer; import com.dotcms.security.apps.AppSecretSavedEvent; import com.dotmarketing.business.APILocator; import com.dotmarketing.filters.AutoLoginFilter; +import com.dotmarketing.osgi.GenericBundleActivator; import com.dotmarketing.util.Config; import com.dotmarketing.util.Logger; import io.vavr.control.Try; -import org.opensaml.core.config.InitializationService; -import org.osgi.framework.BundleContext; -import org.osgi.framework.ServiceRegistration; -import com.dotcms.saml.SamlServiceBuilder; -import com.dotcms.saml.service.impl.SamlServiceBuilderImpl; -import com.dotcms.saml.service.init.Initializer; -import com.dotcms.saml.service.init.SamlInitializer; -import com.dotmarketing.osgi.GenericBundleActivator; /** * This activator will register the {@link SamlServiceBuilder} this class will provide the main @@ -38,6 +36,10 @@ public class Activator extends GenericBundleActivator { private ServiceRegistration samlServiceBuilder; private String interceptorName; + + + private final String DOT_SAML_ACTIVATED="DOT_SAML_ACTIVATED"; + private final Class clazz = DotSamlResource.class; @@ -47,40 +49,60 @@ public class Activator extends GenericBundleActivator { @SuppressWarnings("unchecked") public void start(final BundleContext context) throws Exception { - - System.out.println("SAML OSGI STARTING INIT....."); - System.out.println("SAML version: " + VERSION + ", build number: " + buildNumber); - - - final Map contextMap = new HashMap<>(); - final Initializer initializer = new SamlInitializer(); - - initializer.init(contextMap); - - final SamlServiceBuilderImpl samlServiceBuilderImpl = new SamlServiceBuilderImpl(); - samlServiceBuilderImpl.setInitializer(initializer); - - // Register the TikaServiceBuilder as a OSGI service - this.samlServiceBuilder = context.registerService(SamlServiceBuilder.class.getName(), samlServiceBuilderImpl, - new Hashtable<>()); - - Logger.info(this.getClass().getName(), "Adding the SAML Web Filter"); - - addSamlWebInterceptor(); - - - Logger.info(this.getClass().getName(), "Adding the SAML Web Service: " + clazz.getName()); - RestServiceUtil.addResource(clazz); - - Logger.info(this.getClass().getName(), "Subscribing to AppSecretSavedEvent events"); - Try.run (()-> APILocator.getLocalSystemEventsAPI(). - subscribe(AppSecretSavedEvent.class, DotSamlProxyFactory.getInstance())) - .onFailure(e -> Logger.error(this.getClass().getName(), e.getMessage())); - - - System.out.println("SAML OSGI STARTED....."); - + + if(System.getProperty(DOT_SAML_ACTIVATED)!=null) { + Logger.warn(this.getClass(), "dotSAML already activated, returning"); + return; + } + + synchronized (Config.class) { + + if (null != System.getProperty(DOT_SAML_ACTIVATED)) { + Logger.warn(this.getClass().getName(), "dotSAML already activated, returning"); + return; + } + + + + System.out.println("SAML OSGI STARTING INIT....."); + System.out.println("SAML version: " + VERSION + ", build number: " + buildNumber); + + + final Map contextMap = new HashMap<>(); + final Initializer initializer = new SamlInitializer(); + + initializer.init(contextMap); + + final SamlServiceBuilderImpl samlServiceBuilderImpl = new SamlServiceBuilderImpl(); + samlServiceBuilderImpl.setInitializer(initializer); + + // Register the TikaServiceBuilder as a OSGI service + this.samlServiceBuilder = context.registerService(SamlServiceBuilder.class.getName(), samlServiceBuilderImpl, + new Hashtable<>()); + + Logger.info(this.getClass().getName(), "Adding the SAML Web Filter"); + + addSamlWebInterceptor(); + + + Logger.info(this.getClass().getName(), "Adding the SAML Web Service: " + clazz.getName()); + RestServiceUtil.addResource(clazz); + + Logger.info(this.getClass().getName(), "Subscribing to AppSecretSavedEvent events"); + Try.run (()-> APILocator.getLocalSystemEventsAPI(). + subscribe(AppSecretSavedEvent.class, DotSamlProxyFactory.getInstance())) + .onFailure(e -> Logger.error(this.getClass().getName(), e.getMessage())); + + + System.out.println("SAML OSGI STARTED....."); + + System.setProperty(DOT_SAML_ACTIVATED, "true"); + } } + + + + private void addSamlWebInterceptor() { final FilterWebInterceptorProvider filterWebInterceptorProvider = @@ -121,6 +143,9 @@ public void stop(final BundleContext context) throws Exception { // Unregister the registered services this.samlServiceBuilder.unregister(); + + System.setProperty(DOT_SAML_ACTIVATED, null); + } } From 798e34d5c709d8219879df089a2faa576df53293 Mon Sep 17 00:00:00 2001 From: Will Ezell Date: Thu, 17 Jun 2021 16:23:16 -0400 Subject: [PATCH 2/6] sync the activator and prevent saml from initing twice --- .../java/com/dotcms/saml/osgi/Activator.java | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/dotcms/saml/osgi/Activator.java b/src/main/java/com/dotcms/saml/osgi/Activator.java index 09f1aa6..cd92900 100644 --- a/src/main/java/com/dotcms/saml/osgi/Activator.java +++ b/src/main/java/com/dotcms/saml/osgi/Activator.java @@ -49,6 +49,18 @@ public class Activator extends GenericBundleActivator { @SuppressWarnings("unchecked") public void start(final BundleContext context) throws Exception { + + try { + activate(context); + } + catch(Exception e) { + System.setProperty(DOT_SAML_ACTIVATED, null); + } + } + + + private void activate(final BundleContext context) { + if(System.getProperty(DOT_SAML_ACTIVATED)!=null) { Logger.warn(this.getClass(), "dotSAML already activated, returning"); @@ -57,8 +69,8 @@ public void start(final BundleContext context) throws Exception { synchronized (Config.class) { - if (null != System.getProperty(DOT_SAML_ACTIVATED)) { - Logger.warn(this.getClass().getName(), "dotSAML already activated, returning"); + if(System.getProperty(DOT_SAML_ACTIVATED)!=null) { + Logger.warn(this.getClass(), "dotSAML already activated, returning"); return; } From ddfaa909e9b27a11205afbb4e2e90ec07dc8567b Mon Sep 17 00:00:00 2001 From: Will Ezell Date: Thu, 17 Jun 2021 16:23:52 -0400 Subject: [PATCH 3/6] sync the activator and prevent saml from initing twice --- src/main/java/com/dotcms/saml/osgi/Activator.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/com/dotcms/saml/osgi/Activator.java b/src/main/java/com/dotcms/saml/osgi/Activator.java index cd92900..9cfb943 100644 --- a/src/main/java/com/dotcms/saml/osgi/Activator.java +++ b/src/main/java/com/dotcms/saml/osgi/Activator.java @@ -54,6 +54,7 @@ public void start(final BundleContext context) throws Exception { activate(context); } catch(Exception e) { + Logger.warn(this.getClass(), "dotSAML failed to activate:" + e.getMessage(), e); System.setProperty(DOT_SAML_ACTIVATED, null); } } From b7d1334bcf4dc621fb2b57285086839d392cd16e Mon Sep 17 00:00:00 2001 From: Will Ezell Date: Thu, 17 Jun 2021 16:35:47 -0400 Subject: [PATCH 4/6] print out runlevel for testing --- .../java/com/dotcms/saml/osgi/Activator.java | 108 ++++++++---------- 1 file changed, 50 insertions(+), 58 deletions(-) diff --git a/src/main/java/com/dotcms/saml/osgi/Activator.java b/src/main/java/com/dotcms/saml/osgi/Activator.java index 9cfb943..9086155 100644 --- a/src/main/java/com/dotcms/saml/osgi/Activator.java +++ b/src/main/java/com/dotcms/saml/osgi/Activator.java @@ -5,6 +5,7 @@ import java.util.Map; import org.osgi.framework.BundleContext; import org.osgi.framework.ServiceRegistration; +import org.osgi.framework.startlevel.BundleStartLevel; import com.dotcms.auth.providers.saml.v1.DotSamlResource; import com.dotcms.filters.interceptor.FilterWebInterceptorProvider; import com.dotcms.filters.interceptor.WebInterceptorDelegate; @@ -36,129 +37,120 @@ public class Activator extends GenericBundleActivator { private ServiceRegistration samlServiceBuilder; private String interceptorName; - - - private final String DOT_SAML_ACTIVATED="DOT_SAML_ACTIVATED"; - + + private final String DOT_SAML_STARTED = "DOT_SAML_STARTED"; + private final String DOT_SAML_STOPPED = "DOT_SAML_STOPPED"; private final Class clazz = DotSamlResource.class; private final String VERSION = "21.04"; - private final long buildNumber = 1; + private final long buildNumber = 1; @SuppressWarnings("unchecked") public void start(final BundleContext context) throws Exception { try { activate(context); - } - catch(Exception e) { - Logger.warn(this.getClass(), "dotSAML failed to activate:" + e.getMessage(), e); - System.setProperty(DOT_SAML_ACTIVATED, null); + } catch (Exception e) { + Logger.warn(this.getClass().getName(), "dotSAML failed to activate:" + e.getMessage(), e); + System.setProperty(DOT_SAML_STARTED, DOT_SAML_STOPPED); } } - - + private void activate(final BundleContext context) { + + BundleStartLevel bundleStartLevel = context.getBundle().adapt(BundleStartLevel.class); + Logger.warn(this.getClass().getName(), " "); + Logger.warn(this.getClass().getName(), "dotSAML startlevel=" + bundleStartLevel.getStartLevel()); + Logger.warn(this.getClass().getName(), " "); - if(System.getProperty(DOT_SAML_ACTIVATED)!=null) { - Logger.warn(this.getClass(), "dotSAML already activated, returning"); + if (DOT_SAML_STARTED.equals(System.getProperty(DOT_SAML_STARTED))) { + Logger.warn(this.getClass().getName(), "dotSAML already activated, returning"); return; } - + synchronized (Config.class) { - if(System.getProperty(DOT_SAML_ACTIVATED)!=null) { - Logger.warn(this.getClass(), "dotSAML already activated, returning"); + if (DOT_SAML_STARTED.equals(System.getProperty(DOT_SAML_STARTED))) { + Logger.warn(this.getClass().getName(), "dotSAML already activated, returning"); return; } - - - + System.out.println("SAML OSGI STARTING INIT....."); System.out.println("SAML version: " + VERSION + ", build number: " + buildNumber); - - + final Map contextMap = new HashMap<>(); final Initializer initializer = new SamlInitializer(); - + initializer.init(contextMap); - + final SamlServiceBuilderImpl samlServiceBuilderImpl = new SamlServiceBuilderImpl(); samlServiceBuilderImpl.setInitializer(initializer); - + // Register the TikaServiceBuilder as a OSGI service - this.samlServiceBuilder = context.registerService(SamlServiceBuilder.class.getName(), samlServiceBuilderImpl, - new Hashtable<>()); - + this.samlServiceBuilder = context.registerService(SamlServiceBuilder.class.getName(), + samlServiceBuilderImpl, new Hashtable<>()); + Logger.info(this.getClass().getName(), "Adding the SAML Web Filter"); - + addSamlWebInterceptor(); - - + Logger.info(this.getClass().getName(), "Adding the SAML Web Service: " + clazz.getName()); RestServiceUtil.addResource(clazz); - + Logger.info(this.getClass().getName(), "Subscribing to AppSecretSavedEvent events"); - Try.run (()-> APILocator.getLocalSystemEventsAPI(). - subscribe(AppSecretSavedEvent.class, DotSamlProxyFactory.getInstance())) - .onFailure(e -> Logger.error(this.getClass().getName(), e.getMessage())); - - + Try.run(() -> APILocator.getLocalSystemEventsAPI().subscribe(AppSecretSavedEvent.class, + DotSamlProxyFactory.getInstance())) + .onFailure(e -> Logger.error(this.getClass().getName(), e.getMessage())); + System.out.println("SAML OSGI STARTED....."); - - System.setProperty(DOT_SAML_ACTIVATED, "true"); + + System.setProperty(DOT_SAML_STARTED, "true"); } } - - - - private void addSamlWebInterceptor() { final FilterWebInterceptorProvider filterWebInterceptorProvider = - FilterWebInterceptorProvider.getInstance(Config.CONTEXT); + FilterWebInterceptorProvider.getInstance(Config.CONTEXT); - final WebInterceptorDelegate delegate = - filterWebInterceptorProvider.getDelegate(AutoLoginFilter.class); + final WebInterceptorDelegate delegate = filterWebInterceptorProvider.getDelegate(AutoLoginFilter.class); final SamlWebInterceptor samlWebInterceptor = new SamlWebInterceptor(); this.interceptorName = samlWebInterceptor.getName(); - // in old versions of dotcms may still have the interceptor on core, so lets remove it before add a new one - Try.run(()->delegate.remove(this.interceptorName, true)) - .onFailure(e -> Logger.error(this.getClass().getName(), e.getMessage())); + // in old versions of dotcms may still have the interceptor on core, so lets remove it before add a + // new one + Try.run(() -> delegate.remove(this.interceptorName, true)) + .onFailure(e -> Logger.error(this.getClass().getName(), e.getMessage())); delegate.add(samlWebInterceptor); } - public void stop(final BundleContext context) throws Exception { Logger.info(this.getClass().getName(), "UnSubscribing to AppSecretSavedEvent events."); - Try.run (()-> APILocator.getLocalSystemEventsAPI(). - unsubscribe(AppSecretSavedEvent.class, DotSamlProxyFactory.getInstance().getClass().getName())) - .onFailure(e -> Logger.error(this.getClass().getName(), e.getMessage())); + Try.run(() -> APILocator.getLocalSystemEventsAPI().unsubscribe(AppSecretSavedEvent.class, + DotSamlProxyFactory.getInstance().getClass().getName())) + .onFailure(e -> Logger.error(this.getClass().getName(), e.getMessage())); Logger.info(this.getClass().getName(), "Removing the SAML Web Service"); RestServiceUtil.removeResource(clazz); Logger.info(this.getClass().getName(), "Removing the SAML Web Filter"); final FilterWebInterceptorProvider filterWebInterceptorProvider = - FilterWebInterceptorProvider.getInstance(Config.CONTEXT); + FilterWebInterceptorProvider.getInstance(Config.CONTEXT); - final WebInterceptorDelegate delegate = - filterWebInterceptorProvider.getDelegate(AutoLoginFilter.class); + final WebInterceptorDelegate delegate = filterWebInterceptorProvider.getDelegate(AutoLoginFilter.class); delegate.remove(this.interceptorName, true); // Unregister the registered services this.samlServiceBuilder.unregister(); - - System.setProperty(DOT_SAML_ACTIVATED, null); - + + System.setProperty(DOT_SAML_STARTED, DOT_SAML_STOPPED); + } } From 43cf0b7b24228ba0e09818daf644c6fed87d99b8 Mon Sep 17 00:00:00 2001 From: Will Ezell Date: Thu, 17 Jun 2021 16:40:12 -0400 Subject: [PATCH 5/6] change state var --- .../java/com/dotcms/saml/osgi/Activator.java | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/dotcms/saml/osgi/Activator.java b/src/main/java/com/dotcms/saml/osgi/Activator.java index 9086155..5ab559a 100644 --- a/src/main/java/com/dotcms/saml/osgi/Activator.java +++ b/src/main/java/com/dotcms/saml/osgi/Activator.java @@ -37,9 +37,9 @@ public class Activator extends GenericBundleActivator { private ServiceRegistration samlServiceBuilder; private String interceptorName; - - private final String DOT_SAML_STARTED = "DOT_SAML_STARTED"; - private final String DOT_SAML_STOPPED = "DOT_SAML_STOPPED"; + private final String DOT_SAML_STATE = "DOT_SAML_STATE"; + private final String STARTED = "STARTED"; + private final String STOPPED = "STOPPED"; private final Class clazz = DotSamlResource.class; @@ -54,7 +54,7 @@ public void start(final BundleContext context) throws Exception { activate(context); } catch (Exception e) { Logger.warn(this.getClass().getName(), "dotSAML failed to activate:" + e.getMessage(), e); - System.setProperty(DOT_SAML_STARTED, DOT_SAML_STOPPED); + System.setProperty(DOT_SAML_STATE, STOPPED); } } @@ -64,16 +64,15 @@ private void activate(final BundleContext context) { Logger.warn(this.getClass().getName(), " "); Logger.warn(this.getClass().getName(), "dotSAML startlevel=" + bundleStartLevel.getStartLevel()); Logger.warn(this.getClass().getName(), " "); - - - if (DOT_SAML_STARTED.equals(System.getProperty(DOT_SAML_STARTED))) { + + if (STARTED.equals(System.getProperty(DOT_SAML_STATE))) { Logger.warn(this.getClass().getName(), "dotSAML already activated, returning"); return; } synchronized (Config.class) { - if (DOT_SAML_STARTED.equals(System.getProperty(DOT_SAML_STARTED))) { + if (STARTED.equals(System.getProperty(DOT_SAML_STATE))) { Logger.warn(this.getClass().getName(), "dotSAML already activated, returning"); return; } @@ -107,7 +106,7 @@ private void activate(final BundleContext context) { System.out.println("SAML OSGI STARTED....."); - System.setProperty(DOT_SAML_STARTED, "true"); + System.setProperty(DOT_SAML_STATE, STARTED); } } @@ -149,7 +148,7 @@ public void stop(final BundleContext context) throws Exception { // Unregister the registered services this.samlServiceBuilder.unregister(); - System.setProperty(DOT_SAML_STARTED, DOT_SAML_STOPPED); + System.setProperty(DOT_SAML_STATE, STOPPED); } From 44afe1c4f8d672879aef76848213807c9d1e9af3 Mon Sep 17 00:00:00 2001 From: Will Ezell Date: Thu, 17 Jun 2021 17:50:40 -0400 Subject: [PATCH 6/6] change state var --- src/main/java/com/dotcms/saml/osgi/Activator.java | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/dotcms/saml/osgi/Activator.java b/src/main/java/com/dotcms/saml/osgi/Activator.java index 5ab559a..14b4639 100644 --- a/src/main/java/com/dotcms/saml/osgi/Activator.java +++ b/src/main/java/com/dotcms/saml/osgi/Activator.java @@ -49,7 +49,16 @@ public class Activator extends GenericBundleActivator { @SuppressWarnings("unchecked") public void start(final BundleContext context) throws Exception { + + BundleStartLevel bundleStartLevel = context.getBundle().adapt(BundleStartLevel.class); + + Logger.warn(this.getClass().getName(), " "); + Logger.warn(this.getClass().getName(), "dotSAML startlevel = " + bundleStartLevel.getStartLevel()); + Logger.warn(this.getClass().getName(), "dotSAML persistentlyStarted = " + bundleStartLevel.isPersistentlyStarted()); + Logger.warn(this.getClass().getName(), "dotSAML activationPolicyUsed = " + bundleStartLevel.isActivationPolicyUsed()); + Logger.warn(this.getClass().getName(), " "); + try { activate(context); } catch (Exception e) { @@ -60,10 +69,7 @@ public void start(final BundleContext context) throws Exception { private void activate(final BundleContext context) { - BundleStartLevel bundleStartLevel = context.getBundle().adapt(BundleStartLevel.class); - Logger.warn(this.getClass().getName(), " "); - Logger.warn(this.getClass().getName(), "dotSAML startlevel=" + bundleStartLevel.getStartLevel()); - Logger.warn(this.getClass().getName(), " "); + if (STARTED.equals(System.getProperty(DOT_SAML_STATE))) { Logger.warn(this.getClass().getName(), "dotSAML already activated, returning");