|
@@ -15,8 +15,6 @@ import javax.net.ssl.X509ExtendedKeyManager;
|
|
|
import javax.net.ssl.X509ExtendedTrustManager;
|
|
|
|
|
|
import java.io.IOException;
|
|
|
-import java.io.InputStream;
|
|
|
-import java.nio.file.Files;
|
|
|
import java.nio.file.Path;
|
|
|
import java.security.GeneralSecurityException;
|
|
|
import java.security.Key;
|
|
@@ -49,7 +47,7 @@ class StoreKeyConfig extends KeyConfig {
|
|
|
|
|
|
/**
|
|
|
* Creates a new configuration that can be used to load key and trust material from a {@link KeyStore}
|
|
|
- * @param keyStorePath the path to the keystore file
|
|
|
+ * @param keyStorePath the path to the keystore file or null when keyStoreType is pkcs11
|
|
|
* @param keyStoreType the type of the keystore file
|
|
|
* @param keyStorePassword the password for the keystore
|
|
|
* @param keyPassword the password for the private key in the keystore
|
|
@@ -58,7 +56,7 @@ class StoreKeyConfig extends KeyConfig {
|
|
|
*/
|
|
|
StoreKeyConfig(String keyStorePath, String keyStoreType, SecureString keyStorePassword, SecureString keyPassword,
|
|
|
String keyStoreAlgorithm, String trustStoreAlgorithm) {
|
|
|
- this.keyStorePath = Objects.requireNonNull(keyStorePath, "keystore path must be specified");
|
|
|
+ this.keyStorePath = keyStorePath;
|
|
|
this.keyStoreType = Objects.requireNonNull(keyStoreType, "keystore type must be specified");
|
|
|
// since we support reloading the keystore, we must store the passphrase in memory for the life of the node, so we
|
|
|
// clone the password and never close it during our uses below
|
|
@@ -71,7 +69,7 @@ class StoreKeyConfig extends KeyConfig {
|
|
|
@Override
|
|
|
X509ExtendedKeyManager createKeyManager(@Nullable Environment environment) {
|
|
|
try {
|
|
|
- KeyStore ks = getKeyStore(environment);
|
|
|
+ KeyStore ks = getStore(environment, keyStorePath, keyStoreType, keyStorePassword);
|
|
|
checkKeyStore(ks);
|
|
|
return CertParsingUtils.keyManager(ks, keyPassword.getChars(), keyStoreAlgorithm);
|
|
|
} catch (IOException | CertificateException | NoSuchAlgorithmException | UnrecoverableKeyException | KeyStoreException e) {
|
|
@@ -82,16 +80,16 @@ class StoreKeyConfig extends KeyConfig {
|
|
|
@Override
|
|
|
X509ExtendedTrustManager createTrustManager(@Nullable Environment environment) {
|
|
|
try {
|
|
|
- return CertParsingUtils.trustManager(keyStorePath, keyStoreType, keyStorePassword.getChars(), trustStoreAlgorithm, environment);
|
|
|
- } catch (Exception e) {
|
|
|
+ KeyStore ks = getStore(environment, keyStorePath, keyStoreType, keyStorePassword);
|
|
|
+ return CertParsingUtils.trustManager(ks, trustStoreAlgorithm);
|
|
|
+ } catch (IOException | CertificateException | NoSuchAlgorithmException | KeyStoreException e) {
|
|
|
throw new ElasticsearchException("failed to initialize a TrustManagerFactory", e);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
Collection<CertificateInfo> certificates(Environment environment) throws GeneralSecurityException, IOException {
|
|
|
- final Path path = CertParsingUtils.resolvePath(keyStorePath, environment);
|
|
|
- final KeyStore trustStore = CertParsingUtils.readKeyStore(path, keyStoreType, keyStorePassword.getChars());
|
|
|
+ final KeyStore trustStore = getStore(environment, keyStorePath, keyStoreType, keyStorePassword);
|
|
|
final List<CertificateInfo> certificates = new ArrayList<>();
|
|
|
final Enumeration<String> aliases = trustStore.aliases();
|
|
|
while (aliases.hasMoreElements()) {
|
|
@@ -112,13 +110,16 @@ class StoreKeyConfig extends KeyConfig {
|
|
|
|
|
|
@Override
|
|
|
List<Path> filesToMonitor(@Nullable Environment environment) {
|
|
|
+ if (keyStorePath == null) {
|
|
|
+ return Collections.emptyList();
|
|
|
+ }
|
|
|
return Collections.singletonList(CertParsingUtils.resolvePath(keyStorePath, environment));
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
List<PrivateKey> privateKeys(@Nullable Environment environment) {
|
|
|
try {
|
|
|
- KeyStore keyStore = getKeyStore(environment);
|
|
|
+ KeyStore keyStore = getStore(environment, keyStorePath, keyStoreType, keyStorePassword);
|
|
|
List<PrivateKey> privateKeys = new ArrayList<>();
|
|
|
for (Enumeration<String> e = keyStore.aliases(); e.hasMoreElements(); ) {
|
|
|
final String alias = e.nextElement();
|
|
@@ -135,15 +136,6 @@ class StoreKeyConfig extends KeyConfig {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private KeyStore getKeyStore(@Nullable Environment environment)
|
|
|
- throws KeyStoreException, CertificateException, NoSuchAlgorithmException, IOException {
|
|
|
- try (InputStream in = Files.newInputStream(CertParsingUtils.resolvePath(keyStorePath, environment))) {
|
|
|
- KeyStore ks = KeyStore.getInstance(keyStoreType);
|
|
|
- ks.load(in, keyStorePassword.getChars());
|
|
|
- return ks;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
private void checkKeyStore(KeyStore keyStore) throws KeyStoreException {
|
|
|
Enumeration<String> aliases = keyStore.aliases();
|
|
|
while (aliases.hasMoreElements()) {
|
|
@@ -152,9 +144,11 @@ class StoreKeyConfig extends KeyConfig {
|
|
|
return;
|
|
|
}
|
|
|
}
|
|
|
- throw new IllegalArgumentException("the keystore [" + keyStorePath + "] does not contain a private key entry");
|
|
|
+ final String message = null != keyStorePath ?
|
|
|
+ "the keystore [" + keyStorePath + "] does not contain a private key entry" :
|
|
|
+ "the configured PKCS#11 token does not contain a private key entry";
|
|
|
+ throw new IllegalArgumentException(message);
|
|
|
}
|
|
|
-
|
|
|
@Override
|
|
|
public boolean equals(Object o) {
|
|
|
if (this == o) return true;
|