Commit edf9d408 authored by Brian Major's avatar Brian Major
Browse files

Merge branch 'ac2' of mach277:/home/majorb/git/opencadc into ac2

parents 96bbf0c1 20133e41
Loading
Loading
Loading
Loading
+22 −3
Original line number Diff line number Diff line
# This are the configuration fields required by the Ldap
server = <name of server> 
###############################################################
#
# LDAP Connection and Pool Configuration
#
#
###############################################################

# Read-only connection pool
readOnly.servers = <list of ldap servers for readonly access>
readOnly.poolInitSize = <number of initial connections in the readonly pool>
readOnly.poolMaxSize = <maximum number of connections in the readonly pool>
readOnly.poolPolicy = <roundRobin || fewestConnections>

# Read-write connection pool
readWrite.servers = <list of ldap servers for readwrite access>
readWrite.poolInitSize = <number of initial connections in the readwrite pool>
readWrite.poolMaxSize = <maximum number of connections in the readwrite pool>
readWrite.poolPolicy = <roundRobin || fewestConnections>

# server configuration -- applies to all servers
dbrcHost = <prodLdap || devLdap>
port = <389 or 636>
proxyUser = <name of proxy user>
usersDn = <DN of users branch>
+14 −9
Original line number Diff line number Diff line
@@ -81,6 +81,11 @@ import ca.nrc.cadc.net.TransientException;

public interface GroupPersistence<T extends Principal>
{
    /**
     * Call if this object is to be shut down.
     */
    void destroy();

    /**
     * Get all group names.
     *
+11 −10
Original line number Diff line number Diff line
@@ -70,12 +70,17 @@ package ca.nrc.cadc.ac.server;

import ca.nrc.cadc.ac.server.ldap.LdapGroupPersistence;
import ca.nrc.cadc.ac.server.ldap.LdapUserPersistence;

import java.lang.reflect.Constructor;
import java.net.URL;
import java.security.AccessControlException;
import java.security.Principal;
import java.util.Properties;
import java.util.Set;
import org.apache.log4j.Logger;

import com.unboundid.ldap.sdk.LDAPException;

public class PluginFactory
{
    private static final Logger log = Logger.getLogger(PluginFactory.class);
@@ -113,54 +118,50 @@ public class PluginFactory
    }

    @SuppressWarnings("unchecked")
    public <T extends Principal> GroupPersistence<T> getGroupPersistence()
    public <T extends Principal> GroupPersistence<T> createGroupPersistence()
    {
        GroupPersistence<T> ret = null;
        String name = GroupPersistence.class.getName();
        String cname = config.getProperty(name);
        if (cname == null)
        {
            ret = new LdapGroupPersistence<T>();
            return new LdapGroupPersistence<T>();
        }
        else
        {
            try
            {
                Class<?> c = Class.forName(cname);
                ret = (GroupPersistence<T>) c.newInstance();
                return (GroupPersistence<T>) c.newInstance();
            }
            catch (Exception ex)
            {
                throw new RuntimeException("config error: failed to create GroupPersistence " + cname, ex);
            }
        }
        return ret;
    }

    @SuppressWarnings("unchecked")
    public <T extends Principal> UserPersistence<T> getUserPersistence()
    public <T extends Principal> UserPersistence<T> createUserPersistence()
    {
        UserPersistence ret = null;
        String name = UserPersistence.class.getName();
        String cname = config.getProperty(name);

        if (cname == null)
        {
            ret = new LdapUserPersistence<T>();
            return new LdapUserPersistence<T>();
        }
        else
        {
            try
            {
                Class<?> c = Class.forName(cname);
                ret = (UserPersistence) c.newInstance();
                return (UserPersistence) c.newInstance();
            }
            catch (Exception ex)
            {
                throw new RuntimeException("config error: failed to create UserPersistence " + cname, ex);
            }
        }
        return ret;
    }

}
+6 −0
Original line number Diff line number Diff line
@@ -81,6 +81,12 @@ import java.util.Collection;

public interface UserPersistence<T extends Principal>
{

    /**
     * Call if this object is to be shut down.
     */
    void destroy();

    /**
     * Add the user to the active users tree.
     *
+183 −135
Original line number Diff line number Diff line
@@ -70,6 +70,7 @@ package ca.nrc.cadc.ac.server.ldap;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

import org.apache.log4j.Logger;
@@ -90,26 +91,89 @@ public class LdapConfig
{
    private static final Logger logger = Logger.getLogger(LdapConfig.class);

    public static final String CONFIG = LdapConfig.class.getSimpleName() + 
                                        ".properties";
    public static final String LDAP_SERVER = "server";
    public static final String CONFIG = LdapConfig.class.getSimpleName() + ".properties";

    public static final String READONLY_PREFIX = "readOnly.";
    public static final String READWRITE_PREFIX = "readWrite.";
    public static final String POOL_SERVERS = "servers";
    public static final String POOL_INIT_SIZE = "poolInitSize";
    public static final String POOL_MAX_SIZE = "poolMaxSize";
    public static final String POOL_POLICY = "poolPolicy";

    public static final String LDAP_DBRC_ENTRY = "dbrcHost";
    public static final String LDAP_PORT = "port";
    public static final String LDAP_SERVER_PROXY_USER = "proxyUser";
    public static final String LDAP_USERS_DN = "usersDn";
    public static final String LDAP_USERS_DN = "usersDN";
    public static final String LDAP_USER_REQUESTS_DN = "userRequestsDN";
    public static final String LDAP_GROUPS_DN = "groupsDn";
    public static final String LDAP_ADMIN_GROUPS_DN  = "adminGroupsDn";
    public static final String LDAP_GROUPS_DN = "groupsDN";
    public static final String LDAP_ADMIN_GROUPS_DN  = "adminGroupsDN";

    private final static int SECURE_PORT = 636;

    public enum PoolPolicy
    {
        roundRobin,
        fewestConnections;
    };

    public class LdapPool
    {
        private List<String> servers;
        private int initSize;
        private int maxSize;
        private PoolPolicy policy;

        public List<String> getServers()
        {
            return servers;
        }
        public int getInitSize()
        {
            return initSize;
        }
        public int getMaxSize()
        {
            return maxSize;
        }
        public PoolPolicy getPolicy()
        {
            return policy;
        }

        @Override
        public boolean equals(Object other)
        {
            if (other == null || !(other instanceof LdapPool))
                return false;

            LdapPool l = (LdapPool) other;

            if (! l.servers.equals(servers))
                return false;

            if (l.initSize != initSize)
                return false;

            if (l.maxSize != maxSize)
                return false;

            if ( !(l.policy.equals(policy)))
                return false;

            return true;
        }
    };

    private LdapPool readOnlyPool = new LdapPool();
    private LdapPool readWritePool = new LdapPool();
    private int port;
    private String usersDN;
    private String userRequestsDN;
    private String groupsDN;
    private String adminGroupsDN;
    private String server;
    private int port;
    private String proxyUserDN;
    private String proxyPasswd;
    private String dbrcHost;

    public String getProxyUserDN()
    {
@@ -126,7 +190,7 @@ public class LdapConfig
        return getLdapConfig(CONFIG);
    }

    public static LdapConfig getLdapConfig(final String ldapProperties)
    public static LdapConfig getLdapConfig(String ldapProperties)
    {
        logger.debug("Reading LDAP properties from: " + ldapProperties);
        PropertiesReader pr = new PropertiesReader(ldapProperties);
@@ -138,65 +202,35 @@ public class LdapConfig
            throw new RuntimeException("failed to read any LDAP property ");
        }

        List<String> prop = config.getProperty(LDAP_SERVER);
        if ((prop == null) || (prop.size() != 1))
        {
            throw new RuntimeException("failed to read property " + 
                                       LDAP_SERVER);
        }
        String server = prop.get(0);

        prop = config.getProperty(LDAP_PORT);
        if ((prop == null) || (prop.size() != 1))
        {
            throw new RuntimeException("failed to read property " + LDAP_PORT);
        }
        int port = Integer.valueOf(prop.get(0));
        LdapConfig ldapConfig = new LdapConfig();

        prop = config.getProperty(LDAP_SERVER_PROXY_USER);
        if ((prop == null) || (prop.size() != 1))
        {
            throw new RuntimeException("failed to read property " + 
                    LDAP_SERVER_PROXY_USER);
        }
        String ldapProxy = prop.get(0);
        ldapConfig.readOnlyPool.servers = getMultiProperty(pr, READONLY_PREFIX + POOL_SERVERS);
        ldapConfig.readOnlyPool.initSize = Integer.valueOf(getProperty(pr, READONLY_PREFIX + POOL_INIT_SIZE));
        ldapConfig.readOnlyPool.maxSize = Integer.valueOf(getProperty(pr, READONLY_PREFIX + POOL_MAX_SIZE));
        ldapConfig.readOnlyPool.policy = PoolPolicy.valueOf(getProperty(pr, READONLY_PREFIX + POOL_POLICY));

        prop = config.getProperty(LDAP_USERS_DN);
        if ((prop == null) || (prop.size() != 1))
        {
            throw new RuntimeException("failed to read property " + 
                                       LDAP_USERS_DN);
        }
        String ldapUsersDn = prop.get(0);
        ldapConfig.readWritePool.servers = getMultiProperty(pr, READWRITE_PREFIX + POOL_SERVERS);
        ldapConfig.readWritePool.initSize = Integer.valueOf(getProperty(pr, READWRITE_PREFIX + POOL_INIT_SIZE));
        ldapConfig.readWritePool.maxSize = Integer.valueOf(getProperty(pr, READWRITE_PREFIX + POOL_MAX_SIZE));
        ldapConfig.readWritePool.policy = PoolPolicy.valueOf(getProperty(pr, READWRITE_PREFIX + POOL_POLICY));

        prop = config.getProperty(LDAP_USER_REQUESTS_DN);
        if ((prop == null) || (prop.size() != 1))
        {
            throw new RuntimeException("failed to read property " +
                LDAP_USER_REQUESTS_DN);
        }
        String ldapUserRequestsDn = prop.get(0);
        ldapConfig.dbrcHost = getProperty(pr, LDAP_DBRC_ENTRY);
        ldapConfig.port = Integer.valueOf(getProperty(pr, LDAP_PORT));
        ldapConfig.proxyUserDN = getProperty(pr, LDAP_SERVER_PROXY_USER);
        ldapConfig.usersDN = getProperty(pr, LDAP_USERS_DN);
        ldapConfig.userRequestsDN = getProperty(pr, LDAP_USER_REQUESTS_DN);
        ldapConfig.groupsDN = getProperty(pr, LDAP_GROUPS_DN);
        ldapConfig.adminGroupsDN = getProperty(pr, LDAP_ADMIN_GROUPS_DN);

        prop = config.getProperty(LDAP_GROUPS_DN);
        if ((prop == null) || (prop.size() != 1))
        try
        {
            throw new RuntimeException("failed to read property " + 
                                       LDAP_GROUPS_DN);
        }
        String ldapGroupsDn = prop.get(0);
        
        prop = config.getProperty(LDAP_ADMIN_GROUPS_DN);
        if ((prop == null) || (prop.size() != 1))
            DBConfig dbConfig = new DBConfig();
            ConnectionConfig cc = dbConfig.getConnectionConfig(ldapConfig.dbrcHost, ldapConfig.proxyUserDN);
            if ( (cc == null) || (cc.getUsername() == null) || (cc.getPassword() == null))
            {
            throw new RuntimeException("failed to read property " + 
                                       LDAP_ADMIN_GROUPS_DN);
                throw new RuntimeException("failed to find connection info in ~/.dbrc");
            }
        String ldapAdminGroupsDn = prop.get(0);
        
        DBConfig dbConfig;
        try
        {
            dbConfig = new DBConfig();
            ldapConfig.proxyPasswd = cc.getPassword();
        }
        catch (FileNotFoundException e)
        {
@@ -206,65 +240,81 @@ public class LdapConfig
        {
            throw new RuntimeException("failed to read .dbrc file ");
        }
        ConnectionConfig cc = dbConfig.getConnectionConfig(server, ldapProxy);
        if ( (cc == null) || (cc.getUsername() == null) || (cc.getPassword() == null))
        {
            throw new RuntimeException("failed to find connection info in ~/.dbrc");
        }

        return new LdapConfig(server, Integer.valueOf(port), cc.getUsername(), 
                              cc.getPassword(), ldapUsersDn, ldapUserRequestsDn,
                              ldapGroupsDn, ldapAdminGroupsDn);
        return ldapConfig;
    }


    public LdapConfig(String server, int port, String proxyUserDN, 
                      String proxyPasswd, String usersDN, String userRequestsDN,
                      String groupsDN, String adminGroupsDN)
    private static String getProperty(PropertiesReader properties, String key)
    {
        if (!StringUtil.hasText(server))
        String prop = properties.getFirstPropertyValue(key);
        if (prop == null)
        {
            throw new IllegalArgumentException("Illegal LDAP server name");
            throw new RuntimeException("failed to read property " + key);
        }
        if (port < 0)
        {
            throw new IllegalArgumentException("Illegal LDAP server port: " + 
                                               port);
        }
        if (!StringUtil.hasText(proxyUserDN))
        {
            throw new IllegalArgumentException("Illegal Admin DN");
        }
        if (!StringUtil.hasText(proxyPasswd))
        {
            throw new IllegalArgumentException("Illegal Admin password");
        return prop;
    }
        if (!StringUtil.hasText(usersDN))

    private static List<String> getMultiProperty(PropertiesReader properties, String key)
    {
            throw new IllegalArgumentException("Illegal users LDAP DN");
        String prop = getProperty(properties, key);

        if (prop == null)
            throw new RuntimeException("failed to read property " + key);

        String[] props = prop.split(" ");
        return Arrays.asList(props);
    }
        if (!StringUtil.hasText(userRequestsDN))

    @Override
    public boolean equals(Object other)
    {
            throw new IllegalArgumentException("Illegal userRequests LDAP DN");
        if (other == null || !(other instanceof LdapConfig))
            return false;

        LdapConfig l = (LdapConfig) other;

        if (l.port != port)
            return false;

        if ( !(l.usersDN.equals(usersDN)))
            return false;

        if ( !(l.userRequestsDN.equals(userRequestsDN)))
            return false;

        if ( !(l.groupsDN.equals(groupsDN)))
            return false;

        if ( !(l.adminGroupsDN.equals(adminGroupsDN)))
            return false;

        if ( !(l.proxyUserDN.equals(proxyUserDN)))
            return false;

        if ( !(l.dbrcHost.equals(dbrcHost)))
            return false;

        if ( !(l.readOnlyPool.equals(readOnlyPool)))
            return false;

        if ( !(l.readWritePool.equals(readWritePool)))
            return false;

        return true;
    }
        if (!StringUtil.hasText(groupsDN))

    private LdapConfig()
    {
            throw new IllegalArgumentException("Illegal groups LDAP DN");
    }
        if (!StringUtil.hasText(adminGroupsDN))

    public LdapPool getReadOnlyPool()
    {
            throw new IllegalArgumentException("Illegal admin groups LDAP DN");
        return readOnlyPool;
    }

        this.server = server;
        this.port = port;
        this.proxyUserDN = proxyUserDN;
        this.proxyPasswd = proxyPasswd;
        this.usersDN = usersDN;
        this.userRequestsDN = userRequestsDN;
        this.groupsDN = groupsDN;
        this.adminGroupsDN = adminGroupsDN;
        logger.debug(toString());
    public LdapPool getReadWritePool()
    {
        return readWritePool;
    }

    public String getUsersDN()
@@ -287,9 +337,9 @@ public class LdapConfig
        return this.adminGroupsDN;
    }

    public String getServer()
    public String getDbrcHost()
    {
        return this.server;
        return this.dbrcHost;
    }

    public int getPort()
@@ -315,14 +365,12 @@ public class LdapConfig
    public String toString()
    {
        StringBuilder sb = new StringBuilder();
        sb.append("server = ");
        sb.append(server);
        sb.append("ldap dbrc host = ");
        sb.append(dbrcHost);
        sb.append(" port = ");
        sb.append(port);
        sb.append(" proxyUserDN = ");
        sb.append(proxyUserDN);
        sb.append(" proxyPasswd = ");
        sb.append(proxyPasswd);
        return sb.toString();
    }
}
Loading