Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent double init #12

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
119 changes: 77 additions & 42 deletions src/main/java/com/dotcms/saml/osgi/Activator.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,26 @@
import java.util.HashMap;
import java.util.Hashtable;
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;
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
Expand All @@ -38,89 +37,125 @@ public class Activator extends GenericBundleActivator {
private ServiceRegistration samlServiceBuilder;

private String interceptorName;
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;

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 {

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) {
Logger.warn(this.getClass().getName(), "dotSAML failed to activate:" + e.getMessage(), e);
System.setProperty(DOT_SAML_STATE, STOPPED);
}
}

private void activate(final BundleContext context) {

System.out.println("SAML OSGI STARTING INIT.....");
System.out.println("SAML version: " + VERSION + ", build number: " + buildNumber);


final Map<String, Object> contextMap = new HashMap<>();
final Initializer initializer = new SamlInitializer();
if (STARTED.equals(System.getProperty(DOT_SAML_STATE))) {
Logger.warn(this.getClass().getName(), "dotSAML already activated, returning");
return;
}

initializer.init(contextMap);
synchronized (Config.class) {

final SamlServiceBuilderImpl samlServiceBuilderImpl = new SamlServiceBuilderImpl();
samlServiceBuilderImpl.setInitializer(initializer);
if (STARTED.equals(System.getProperty(DOT_SAML_STATE))) {
Logger.warn(this.getClass().getName(), "dotSAML already activated, returning");
return;
}

// Register the TikaServiceBuilder as a OSGI service
this.samlServiceBuilder = context.registerService(SamlServiceBuilder.class.getName(), samlServiceBuilderImpl,
new Hashtable<>());
System.out.println("SAML OSGI STARTING INIT.....");
System.out.println("SAML version: " + VERSION + ", build number: " + buildNumber);

Logger.info(this.getClass().getName(), "Adding the SAML Web Filter");
final Map<String, Object> contextMap = new HashMap<>();
final Initializer initializer = new SamlInitializer();

addSamlWebInterceptor();
initializer.init(contextMap);

final SamlServiceBuilderImpl samlServiceBuilderImpl = new SamlServiceBuilderImpl();
samlServiceBuilderImpl.setInitializer(initializer);

Logger.info(this.getClass().getName(), "Adding the SAML Web Service: " + clazz.getName());
RestServiceUtil.addResource(clazz);
// Register the TikaServiceBuilder as a OSGI service
this.samlServiceBuilder = context.registerService(SamlServiceBuilder.class.getName(),
samlServiceBuilderImpl, new Hashtable<>());

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()));
Logger.info(this.getClass().getName(), "Adding the SAML Web Filter");

addSamlWebInterceptor();

System.out.println("SAML OSGI STARTED.....");
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_STATE, STARTED);
}
}

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_STATE, STOPPED);

}

}