Working ident client authentication
[lgpl/argeo-commons.git] / org.argeo.cms / src / org / argeo / cms / internal / kernel / Activator.java
index 5ba7c01e8bc18864b9aeba4f40de56ea7c263985..bba8f2bbb519e08fc3843a3620f5e0c1fe32c80e 100644 (file)
@@ -13,6 +13,7 @@ import javax.security.auth.login.Configuration;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.argeo.cms.CmsException;
+import org.argeo.ident.IdentClient;
 import org.argeo.node.ArgeoLogger;
 import org.argeo.node.NodeConstants;
 import org.argeo.node.NodeDeployment;
@@ -26,6 +27,7 @@ import org.osgi.framework.Constants;
 import org.osgi.framework.ServiceReference;
 import org.osgi.service.log.LogReaderService;
 import org.osgi.service.useradmin.UserAdmin;
+import org.osgi.util.tracker.ServiceTracker;
 
 /**
  * Activates the kernel. Gives access to kernel information for the rest of the
@@ -37,6 +39,7 @@ public class Activator implements BundleActivator {
        private static Activator instance;
 
        private BundleContext bc;
+
        private LogReaderService logReaderService;
 
        private NodeLogger logger;
@@ -44,6 +47,8 @@ public class Activator implements BundleActivator {
        private CmsDeployment nodeDeployment;
        private CmsInstance nodeInstance;
 
+       private ServiceTracker<UserAdmin, NodeUserAdmin> userAdminSt;
+
        @Override
        public void start(BundleContext bundleContext) throws Exception {
                Runtime.getRuntime().addShutdownHook(new CmsShutdown());
@@ -55,7 +60,11 @@ public class Activator implements BundleActivator {
                        initSecurity();
                        initArgeoLogger();
                        initNode();
-                       log.debug("Kernel bundle started");
+
+                       userAdminSt = new ServiceTracker<>(instance.bc, UserAdmin.class, null);
+                       userAdminSt.open();
+                       if (log.isTraceEnabled())
+                               log.trace("Kernel bundle started");
                } catch (Throwable e) {
                        log.error("## FATAL: CMS activator failed", e);
                }
@@ -127,6 +136,9 @@ public class Activator implements BundleActivator {
                        if (nodeState != null)
                                nodeState.shutdown();
 
+                       if (userAdminSt != null)
+                               userAdminSt.close();
+
                        instance = null;
                        this.bc = null;
                        this.logReaderService = null;
@@ -155,10 +167,36 @@ public class Activator implements BundleActivator {
                return getNodeUserAdmin().isSingleUser();
        }
 
+       public static UserAdmin getUserAdmin() {
+               return (UserAdmin) getNodeUserAdmin();
+       }
+
+       public static String getHttpProxySslHeader() {
+               return KernelUtils.getFrameworkProp(NodeConstants.HTTP_PROXY_SSL_DN);
+       }
+
+       public static IdentClient getIdentClient(String remoteAddr) {
+               if (!IdentClient.isDefaultAuthdPassphraseFileAvailable())
+                       return null;
+               // TODO make passphrase more configurable
+               return new IdentClient(remoteAddr);
+       }
+
        private static NodeUserAdmin getNodeUserAdmin() {
-               ServiceReference<UserAdmin> sr = instance.bc.getServiceReference(UserAdmin.class);
-               NodeUserAdmin userAdmin = (NodeUserAdmin) instance.bc.getService(sr);
-               return userAdmin;
+               NodeUserAdmin res;
+               try {
+                       res = instance.userAdminSt.waitForService(60000);
+               } catch (InterruptedException e) {
+                       throw new CmsException("Cannot retrieve Node user admin", e);
+               }
+               if (res == null)
+                       throw new CmsException("No Node user admin found");
+
+               return res;
+               // ServiceReference<UserAdmin> sr =
+               // instance.bc.getServiceReference(UserAdmin.class);
+               // NodeUserAdmin userAdmin = (NodeUserAdmin) instance.bc.getService(sr);
+               // return userAdmin;
 
        }