|
16 | 16 | // under the License. |
17 | 17 | package org.apache.cloudstack.ldap; |
18 | 18 |
|
| 19 | +import java.io.FileInputStream; |
19 | 20 | import java.io.IOException; |
20 | 21 | import java.util.Hashtable; |
21 | 22 |
|
|
24 | 25 | import javax.naming.NamingException; |
25 | 26 | import javax.naming.ldap.InitialLdapContext; |
26 | 27 | import javax.naming.ldap.LdapContext; |
| 28 | +import java.security.KeyStore; |
27 | 29 |
|
28 | 30 | import org.apache.commons.lang3.StringUtils; |
29 | 31 | import org.apache.log4j.Logger; |
@@ -72,8 +74,36 @@ private void enableSSL(final Hashtable<String, String> environment, Long domainI |
72 | 74 | if (sslStatus) { |
73 | 75 | s_logger.info("LDAP SSL enabled."); |
74 | 76 | 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; |
77 | 107 | } |
78 | 108 | } |
79 | 109 |
|
|
0 commit comments