Skip to content

Commit fa8eef4

Browse files
authored
IDS-9532: LDAP server validation (#165)
* init * add ldap server checker * add warnings to ldap checker * remove line
1 parent edf5658 commit fa8eef4

File tree

1 file changed

+99
-2
lines changed

1 file changed

+99
-2
lines changed

cws-installer/src/main/java/jpl/cws/task/CwsInstaller.java

Lines changed: 99 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -588,7 +588,27 @@ private static void setIdentityPluginType() throws UnsupportedOperationException
588588

589589
// PROMPT USER FOR LDAP SERVER URL
590590
if (cws_installer_mode.equals("interactive")) {
591-
cws_ldap_url = readLine("Enter the LDAP URL, default is " + cws_ldap_url + ": ", cws_ldap_url);
591+
boolean valid_ldap_server = false;
592+
while (!valid_ldap_server) {
593+
String read_cws_ldap_url = readLine("Enter the LDAP URL. " + "Default is " + cws_ldap_url + ": ", cws_ldap_url);
594+
595+
try {
596+
boolean checkLdapServer = checkLdapServerStatus(read_cws_ldap_url);
597+
if (checkLdapServer == true) {
598+
valid_ldap_server = true;
599+
cws_ldap_url = read_cws_ldap_url;
600+
} else {
601+
valid_ldap_server = false;
602+
print(" WARNING: LDAP (" + read_cws_ldap_url + ") cannot be reached." );
603+
print(" Possible Issues: ");
604+
print(" - Incorrect configuration of 'config/templates/tomcat_conf/ldap_plugin_bean.xml'.");
605+
print(" - Can't contact LDAP server because of bad certificate in host machine.");
606+
print(" - LDAP server is inactive.");
607+
}
608+
} catch(IOException e) {
609+
// exception
610+
}
611+
}
592612
}
593613

594614
cws_identity_plugin_class = ldap_identity_plugin_class;
@@ -1731,6 +1751,7 @@ private static void validateConfig() {
17311751
warningCount += validateDbConfig();
17321752
if (cws_auth_scheme.equals("LDAP")) {
17331753
try {
1754+
warningCount += validateLdapServer();
17341755
warningCount += validateLdapUserConfig();
17351756
} catch(IOException e) {
17361757
// exception
@@ -1939,9 +1960,85 @@ private static int validateDbConfig() {
19391960
return warningCount;
19401961
}
19411962

1963+
/**
1964+
* Validates the LDAP URL configuration.
1965+
*
1966+
*/
1967+
private static int validateLdapServer() throws IOException {
1968+
int warningCount = 0;
1969+
// VALIDATE LDAP SERVER
1970+
print("");
1971+
if (cws_auth_scheme.equals("LDAP")) {
1972+
print("checking that user provided LDAP Server (" + cws_ldap_url + ") is accessible...");
1973+
}
1974+
1975+
boolean checkLdapServer = checkLdapServerStatus(cws_ldap_url);
1976+
if (checkLdapServer == false) {
1977+
print(" [WARNING]");
1978+
print(" Possible Issues: ");
1979+
print(" - Incorrect configuration of 'config/templates/tomcat_conf/ldap_plugin_bean.xml'.");
1980+
print(" - Can't contact LDAP server because of bad certificate in host machine.");
1981+
print(" - LDAP server is inactive.");
1982+
return 1;
1983+
} else {
1984+
print(" [OK]");
1985+
}
1986+
return warningCount;
1987+
}
1988+
1989+
private static boolean checkLdapServerStatus(String ldapUrl) throws IOException {
1990+
//
1991+
// Check for LDAP Server accessibility
1992+
//
1993+
Path pluginBeanFilePath = Paths.get(config_templates_dir + SEP + "tomcat_conf" + SEP + "ldap_plugin_bean.xml");
1994+
String ldapBaseDn = getLdapBaseDnValue(pluginBeanFilePath);
1995+
String[] baseDnArray = ldapBaseDn.split(",");
1996+
String searchBase = baseDnArray[0];
1997+
1998+
Hashtable env = new Hashtable();
1999+
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
2000+
env.put(Context.PROVIDER_URL, ldapUrl);
2001+
try {
2002+
DirContext ctx = new InitialDirContext(env);
2003+
SearchControls ctrl = new SearchControls();
2004+
ctrl.setSearchScope(SearchControls.SUBTREE_SCOPE);
2005+
2006+
String filter = "(&(" + searchBase + "))";
2007+
NamingEnumeration ldapQuery = ctx.search(ldapBaseDn, filter, ctrl);
2008+
2009+
if (!ldapQuery.hasMore()) {
2010+
print(" ERROR: LDAP server (" + ldapUrl + ")" + " Search Query return is empty. Server may be inactive.") ;
2011+
return false;
2012+
}
2013+
2014+
while (ldapQuery.hasMore()) {
2015+
SearchResult r = (SearchResult) ldapQuery.next();
2016+
if (r.getNameInNamespace().toString() != null || r.getNameInNamespace().length() == 0) {
2017+
break;
2018+
}
2019+
}
2020+
// Close the context
2021+
ctx.close();
2022+
} catch (AuthenticationNotSupportedException e) {
2023+
print(" ERROR: LDAP authentication failed with server " + ldapUrl + " (" + e.toString() + ")");
2024+
return false;
2025+
} catch (AuthenticationException e) {
2026+
print(" ERROR AuthenticationException: " + e.toString());
2027+
return false;
2028+
} catch (NamingException e) {
2029+
print(" ERROR NamingException: " + e.toString());
2030+
return false;
2031+
}
2032+
return true;
2033+
}
2034+
2035+
/**
2036+
* Validates the LDAP User Admin configuration.
2037+
*
2038+
*/
19422039
private static int validateLdapUserConfig() throws IOException {
19432040
int warningCount = 0;
1944-
// VALIDATE LDAP or CAM CONFIGURATION AND LDAP USER INFO RETREIVEL
2041+
// VALIDATE LDAP CONFIGURATION AND LDAP USER INFO RETREIVEL
19452042
print("");
19462043
if (cws_auth_scheme.equals("LDAP")) {
19472044
print("checking that user provided LDAP authentication profile (UID: " + cws_user + ") is valid...");

0 commit comments

Comments
 (0)