Make testing more generic.
[lgpl/argeo-commons.git] / org.argeo.enterprise / src / org / argeo / osgi / useradmin / UserAdminConf.java
index b3ead140c7b5aa641418d5ec54ca204ed853f585..bb7345f0b1ca51854434db0aad51348915763fab 100644 (file)
@@ -13,9 +13,8 @@ import java.util.Map;
 
 import javax.naming.Context;
 import javax.naming.NamingException;
+import javax.naming.ldap.LdapName;
 
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
 import org.argeo.naming.DnsBrowser;
 import org.argeo.naming.NamingUtils;
 import org.osgi.framework.Constants;
@@ -43,11 +42,13 @@ public enum UserAdminConf {
        /** Read-only source */
        readOnly(null),
 
+       /** Disabled source */
+       disabled(null),
+
        /** Authentication realm */
        realm(null);
 
        public final static String FACTORY_PID = "org.argeo.osgi.useradmin.config";
-       private final static Log log = LogFactory.getLog(UserAdminConf.class);
 
        public final static String SCHEME_LDAP = "ldap";
        public final static String SCHEME_FILE = "file";
@@ -144,6 +145,9 @@ public enum UserAdminConf {
                        if (bDn.endsWith(".ldif"))
                                bDn = bDn.substring(0, bDn.length() - ".ldif".length());
 
+                       // Normalize base DN as LDAP name
+                       bDn = new LdapName(bDn).toString();
+
                        String principal = null;
                        String credentials = null;
                        if (scheme != null)
@@ -214,8 +218,6 @@ public enum UserAdminConf {
                        }
                        URI convertedUri = new URI(
                                        SCHEME_LDAP + "://" + ldapHostsStr + "/" + IpaUtils.domainToUserDirectoryConfigPath(kerberosRealm));
-                       if (log.isDebugEnabled())
-                               log.debug("Converted " + uri + " to " + convertedUri);
                        return convertedUri;
                } catch (NamingException | IOException | URISyntaxException e) {
                        throw new UserDirectoryException("cannot convert IPA uri " + uri, e);
@@ -241,7 +243,6 @@ public enum UserAdminConf {
                try {
                        hostname = InetAddress.getLocalHost().getHostName();
                } catch (UnknownHostException e) {
-                       log.warn("Using localhost as hostname", e);
                        hostname = "localhost.localdomain";
                }
                int dotIdx = hostname.indexOf('.');
@@ -254,4 +255,15 @@ public enum UserAdminConf {
                        return "dc=" + hostname;
                }
        }
+
+       /**
+        * Hash the base DN in order to have a deterministic string to be used as a cn
+        * for the underlying user directory.
+        */
+       public static String baseDnHash(Dictionary<String, Object> properties) {
+               String bDn = (String) properties.get(baseDn.name());
+               if (bDn == null)
+                       throw new UserDirectoryException("No baseDn in " + properties);
+               return DigestUtils.sha1str(bDn);
+       }
 }