Skip to content

Commit cbd2b5a

Browse files
authored
Add check for ldap truststore password (#11055)
1 parent 0d5a0ea commit cbd2b5a

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

plugins/user-authenticators/ldap/src/main/java/org/apache/cloudstack/ldap/LdapContextFactory.java

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
// under the License.
1717
package org.apache.cloudstack.ldap;
1818

19+
import java.io.FileInputStream;
1920
import java.io.IOException;
2021
import java.util.Hashtable;
2122

@@ -24,6 +25,7 @@
2425
import javax.naming.NamingException;
2526
import javax.naming.ldap.InitialLdapContext;
2627
import javax.naming.ldap.LdapContext;
28+
import java.security.KeyStore;
2729

2830
import org.apache.commons.lang3.StringUtils;
2931
import org.apache.log4j.Logger;
@@ -72,8 +74,36 @@ private void enableSSL(final Hashtable<String, String> environment, Long domainI
7274
if (sslStatus) {
7375
s_logger.info("LDAP SSL enabled.");
7476
environment.put(Context.SECURITY_PROTOCOL, "ssl");
75-
System.setProperty("javax.net.ssl.trustStore", _ldapConfiguration.getTrustStore(domainId));
76-
System.setProperty("javax.net.ssl.trustStorePassword", _ldapConfiguration.getTrustStorePassword(domainId));
77+
String trustStore = _ldapConfiguration.getTrustStore(domainId);
78+
String trustStorePassword = _ldapConfiguration.getTrustStorePassword(domainId);
79+
80+
if (!validateTrustStore(trustStore, trustStorePassword)) {
81+
throw new RuntimeException("Invalid truststore or truststore password");
82+
}
83+
84+
System.setProperty("javax.net.ssl.trustStore", trustStore);
85+
System.setProperty("javax.net.ssl.trustStorePassword", trustStorePassword);
86+
}
87+
}
88+
89+
private boolean validateTrustStore(String trustStore, String trustStorePassword) {
90+
if (trustStore == null) {
91+
return true;
92+
}
93+
94+
if (trustStorePassword == null) {
95+
return false;
96+
}
97+
98+
try {
99+
KeyStore.getInstance("JKS").load(
100+
new FileInputStream(trustStore),
101+
trustStorePassword.toCharArray()
102+
);
103+
return true;
104+
} catch (Exception e) {
105+
s_logger.warn("Failed to validate truststore: " + e.getMessage());
106+
return false;
77107
}
78108
}
79109

plugins/user-authenticators/ldap/src/main/java/org/apache/cloudstack/ldap/LdapManagerImpl.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,11 @@ private LdapConfigurationResponse addConfigurationInternal(final String hostname
186186
} catch (NamingException | IOException e) {
187187
LOGGER.debug("NamingException while doing an LDAP bind", e);
188188
throw new InvalidParameterValueException("Unable to bind to the given LDAP server");
189+
} catch (RuntimeException e) {
190+
if (e.getMessage().contains("Invalid truststore")) {
191+
throw new InvalidParameterValueException("Invalid truststore or truststore password");
192+
}
193+
throw e;
189194
} finally {
190195
closeContext(context);
191196
}

0 commit comments

Comments
 (0)