Skip to content
Snippets Groups Projects
Commit e3f1ab09 authored by Adrian Damian's avatar Adrian Damian
Browse files

Changed LdapConfig in a properties file to be found in ~/config/

parent 82b45a08
No related branches found
No related tags found
No related merge requests found
#server proxyuser proxyUserDN password driver serverURL
<serverType in LdapConfig.properties: LDAP or DEVLDAP> <proxyUser in LdapConfig.properties> <proxyUserLdapDN> <password> N/A N/A
# This are the configuration fields required by the Ldap ldap-dao unit tests
server = mach275.cadc.dao.nrc.ca
port = 389
admin = uid=webproxy,ou=administrators,ou=topologymanagement,o=netscaperoot
passwd = go4it
usersDn = ou=Users,ou=ds,dc=canfar,dc=net
groupsDn = ou=Groups,ou=ds,dc=canfar,dc=net
deletedGroupsDN = ou=DeletedGroups,ou=ds,dc=canfar,dc=net
testGroupsDN = ou=TestGroups,ou=ds,dc=canfar,dc=net
\ No newline at end of file
# This are the configuration fields required by the Ldap
server = <name of server>
port = <389 or 636>
serverType = <DEVLDAP or LDAP or...>
proxyUser = <name of proxy user>
usersDn = <DN of users branch>
groupsDn = <DN of groups branch>
adminGroupsDn = <DN of admin groups>
......@@ -68,14 +68,24 @@
*/
package ca.nrc.cadc.ac.server.ldap;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URL;
import java.util.Properties;
import java.util.List;
import org.apache.log4j.Logger;
import ca.nrc.cadc.db.ConnectionConfig;
import ca.nrc.cadc.db.DBConfig;
import ca.nrc.cadc.util.MultiValuedProperties;
import ca.nrc.cadc.util.PropertiesReader;
import ca.nrc.cadc.util.StringUtil;
/**
* Reads and stores the LDAP configuration information. The information
*
* @author adriand
*
*/
public class LdapConfig
{
private static final Logger logger = Logger.getLogger(LdapConfig.class);
......@@ -84,125 +94,130 @@ public class LdapConfig
".properties";
public static final String LDAP_SERVER = "server";
public static final String LDAP_PORT = "port";
public static final String LDAP_ADMIN = "admin";
public static final String LDAP_PASSWD = "passwd";
public static final String LDAP_SERVER_TYPE = "serverType";
public static final String LDAP_SERVER_PROXY_USER = "proxyUser";
public static final String LDAP_USERS_DN = "usersDn";
public static final String LDAP_GROUPS_DN = "groupsDn";
public static final String LDAP_ADMIN_GROUPS_DN = "adminGroupsDn";
public static final String LDAP_AVAIL_TEST_GROUP = "availabilityTestGroup";
public static final String LDAP_AVAIL_TEST_CALLING_USER_DN = "availabilityTestCallingUserDN";
private String usersDN;
private String groupsDN;
private String adminGroupsDN;
private String server;
private int port;
private String adminUserDN;
private String adminPasswd;
private String availabilityTestGroup;
private String availabilityTestCallingUserDN;
private String proxyUserDN;
private String proxyPasswd;
public static LdapConfig getLdapConfig()
{
Properties config = new Properties();
URL url = null;
try
public String getProxyUserDN()
{
url = LdapConfig.class.getClassLoader().getResource(CONFIG);
logger.debug("Using config from: " + url);
if (url != null)
{
config.load(url.openStream());
return proxyUserDN;
}
else
public String getProxyPasswd()
{
throw new IOException("File not found");
return proxyPasswd;
}
}
catch (Exception ex)
public static LdapConfig getLdapConfig()
{
throw new RuntimeException("failed to read " + CONFIG +
" from " + url, ex);
PropertiesReader pr = new PropertiesReader(CONFIG);
MultiValuedProperties config = pr.getAllProperties();
if (config.keySet() == null)
{
throw new RuntimeException("failed to read any LDAP property ");
}
String server = config.getProperty(LDAP_SERVER);
if (!StringUtil.hasText(server))
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);
String port = config.getProperty(LDAP_PORT);
if (!StringUtil.hasText(port))
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));
String ldapAdmin = config.getProperty(LDAP_ADMIN);
if (!StringUtil.hasText(ldapAdmin))
prop = config.getProperty(LDAP_SERVER_TYPE);
if ((prop == null) || (prop.size() != 1))
{
throw new RuntimeException("failed to read property " + LDAP_ADMIN);
throw new RuntimeException("failed to read property " +
LDAP_SERVER_TYPE);
}
String serverType = prop.get(0);
if (!"LDAP".equalsIgnoreCase(serverType) &&
!"DEVLDAP".equalsIgnoreCase(serverType))
{
throw new RuntimeException("Unknow server type: " + serverType +
" (valid: LDAP and DEVLDAP)");
}
String ldapPasswd = config.getProperty(LDAP_PASSWD);
if (!StringUtil.hasText(ldapPasswd))
prop = config.getProperty(LDAP_SERVER_PROXY_USER);
if ((prop == null) || (prop.size() != 1))
{
throw new RuntimeException("failed to read property " +
LDAP_PASSWD);
LDAP_SERVER_PROXY_USER);
}
String ldapProxy = prop.get(0);
String ldapUsersDn = config.getProperty(LDAP_USERS_DN);
if (!StringUtil.hasText(ldapUsersDn))
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);
String ldapGroupsDn = config.getProperty(LDAP_GROUPS_DN);
if (!StringUtil.hasText(ldapGroupsDn))
prop = config.getProperty(LDAP_GROUPS_DN);
if ((prop == null) || (prop.size() != 1))
{
throw new RuntimeException("failed to read property " +
LDAP_GROUPS_DN);
}
String ldapGroupsDn = prop.get(0);
String ldapAdminGroupsDn = config.getProperty(LDAP_ADMIN_GROUPS_DN);
if (!StringUtil.hasText(ldapAdminGroupsDn))
prop = config.getProperty(LDAP_ADMIN_GROUPS_DN);
if ((prop == null) || (prop.size() != 1))
{
throw new RuntimeException("failed to read property " +
LDAP_ADMIN_GROUPS_DN);
}
String ldapAdminGroupsDn = prop.get(0);
String availGroup = config.getProperty(LDAP_AVAIL_TEST_GROUP);
if (!StringUtil.hasText(availGroup))
DBConfig dbConfig;
try
{
throw new RuntimeException("failed to read property " +
LDAP_AVAIL_TEST_GROUP);
dbConfig = new DBConfig();
}
String availUser = config.getProperty(LDAP_AVAIL_TEST_CALLING_USER_DN);
if (!StringUtil.hasText(availUser))
catch (FileNotFoundException e)
{
throw new RuntimeException("failed to read property " +
LDAP_AVAIL_TEST_CALLING_USER_DN);
throw new RuntimeException("failed to find .dbrc file ");
}
return new LdapConfig(server, Integer.valueOf(port), ldapAdmin,
ldapPasswd, ldapUsersDn, ldapGroupsDn,
ldapAdminGroupsDn, availGroup, availUser);
catch (IOException e)
{
throw new RuntimeException("failed to read .dbrc file ");
}
public LdapConfig(String server, int port, String adminUserDN,
String adminPasswd, String usersDN, String groupsDN,
String adminGroupsDN)
ConnectionConfig cc = dbConfig.getConnectionConfig(serverType, ldapProxy);
if ( (cc == null) || (cc.getUsername() == null) || (cc.getPassword() == null))
{
this(server, port, adminUserDN, adminPasswd, usersDN, groupsDN, adminGroupsDN, null, null);
throw new RuntimeException("failed to find connection info in ~/.dbrc");
}
public LdapConfig(String server, int port, String adminUserDN,
String adminPasswd, String usersDN, String groupsDN,
String adminGroupsDN, String availGroup, String availUser)
return new LdapConfig(server, Integer.valueOf(port), cc.getUsername(),
cc.getPassword(), ldapUsersDn, ldapGroupsDn,
ldapAdminGroupsDn);
}
public LdapConfig(String server, int port, String proxyUserDN,
String proxyPasswd, String usersDN, String groupsDN,
String adminGroupsDN)
{
if (!StringUtil.hasText(server))
{
......@@ -213,11 +228,11 @@ public class LdapConfig
throw new IllegalArgumentException("Illegal LDAP server port: " +
port);
}
if (!StringUtil.hasText(adminUserDN))
if (!StringUtil.hasText(proxyUserDN))
{
throw new IllegalArgumentException("Illegal Admin DN");
}
if (!StringUtil.hasText(adminPasswd))
if (!StringUtil.hasText(proxyPasswd))
{
throw new IllegalArgumentException("Illegal Admin password");
}
......@@ -234,16 +249,16 @@ public class LdapConfig
throw new IllegalArgumentException("Illegal admin groups LDAP DN");
}
this.server = server;
this.port = port;
this.adminUserDN = adminUserDN;
this.adminPasswd = adminPasswd;
this.proxyUserDN = proxyUserDN;
this.proxyPasswd = proxyPasswd;
this.usersDN = usersDN;
this.groupsDN = groupsDN;
this.adminGroupsDN = adminGroupsDN;
this.availabilityTestGroup = availGroup;
this.availabilityTestCallingUserDN = availUser;
logger.debug(proxyPasswd);
logger.debug(proxyUserDN);
logger.debug(toString());
}
public String getUsersDN()
......@@ -273,22 +288,25 @@ public class LdapConfig
public String getAdminUserDN()
{
return this.adminUserDN;
return this.proxyUserDN;
}
public String getAdminPasswd()
{
return this.adminPasswd;
return this.proxyPasswd;
}
public String getAvailabilityTestGroup()
public String toString()
{
return this.availabilityTestGroup;
StringBuilder sb = new StringBuilder();
sb.append("server = ");
sb.append(server);
sb.append(" port = ");
sb.append(port);
sb.append(" proxyUserDN = ");
sb.append(proxyUserDN);
sb.append(" proxyPasswd = ");
sb.append(proxyPasswd);
return sb.toString();
}
public String getAvailabilityTestCallingUserDN()
{
return this.availabilityTestCallingUserDN;
}
}
......@@ -119,7 +119,7 @@ public abstract class LdapDAO
if (conn == null)
{
conn = new LDAPConnection(config.getServer(), config.getPort());
conn.bind(config.getAdminUserDN(), config.getAdminPasswd());
conn.bind(config.getProxyUserDN(), config.getProxyPasswd());
}
return conn;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment