Skip to content

Commit c55c2c7

Browse files
committed
Add check for ldap truststore password
1 parent 0d5a0ea commit c55c2c7

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

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

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,34 @@ private void enableSSL(final Hashtable<String, String> environment, Long domainI
7272
if (sslStatus) {
7373
s_logger.info("LDAP SSL enabled.");
7474
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));
75+
String trustStore = _ldapConfiguration.getTrustStore(domainId);
76+
String trustStorePassword = _ldapConfiguration.getTrustStorePassword(domainId);
77+
78+
// Validate truststore and password before setting system properties
79+
if (!validateTrustStore(trustStore, trustStorePassword)) {
80+
throw new RuntimeException("Invalid truststore or truststore password");
81+
}
82+
83+
System.setProperty("javax.net.ssl.trustStore", trustStore);
84+
System.setProperty("javax.net.ssl.trustStorePassword", trustStorePassword);
85+
}
86+
}
87+
88+
private boolean validateTrustStore(String trustStore, String trustStorePassword) {
89+
if (trustStore == null || trustStorePassword == null) {
90+
return false;
91+
}
92+
93+
try {
94+
// Try to load the truststore with the provided password
95+
java.security.KeyStore.getInstance("JKS").load(
96+
new java.io.FileInputStream(trustStore),
97+
trustStorePassword.toCharArray()
98+
);
99+
return true;
100+
} catch (Exception e) {
101+
s_logger.warn("Failed to validate truststore: " + e.getMessage());
102+
return false;
77103
}
78104
}
79105

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)