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