Skip to content

Commit 7cad65d

Browse files
author
Daan Hoogland
committed
Merge release branch 4.19 to 4.20
* 4.19: Add check for ldap truststore password (apache#11055)
2 parents c24e4ee + cbd2b5a commit 7cad65d

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

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
@@ -184,6 +184,11 @@ private LdapConfigurationResponse addConfigurationInternal(final String hostname
184184
} catch (NamingException | IOException e) {
185185
logger.debug("NamingException while doing an LDAP bind", e);
186186
throw new InvalidParameterValueException("Unable to bind to the given LDAP server");
187+
} catch (RuntimeException e) {
188+
if (e.getMessage().contains("Invalid truststore")) {
189+
throw new InvalidParameterValueException("Invalid truststore or truststore password");
190+
}
191+
throw e;
187192
} finally {
188193
closeContext(context);
189194
}

0 commit comments

Comments
 (0)