|
14 | 14 | import javax.net.ssl.X509ExtendedKeyManager;
|
15 | 15 | import javax.net.ssl.X509ExtendedTrustManager;
|
16 | 16 |
|
| 17 | +import java.io.FileNotFoundException; |
17 | 18 | import java.io.IOException;
|
| 19 | +import java.nio.file.AccessDeniedException; |
| 20 | +import java.nio.file.NoSuchFileException; |
18 | 21 | import java.nio.file.Path;
|
| 22 | +import java.security.AccessControlException; |
19 | 23 | import java.security.GeneralSecurityException;
|
20 | 24 | import java.security.Key;
|
21 | 25 | import java.security.KeyStore;
|
22 | 26 | import java.security.KeyStoreException;
|
23 |
| -import java.security.NoSuchAlgorithmException; |
24 | 27 | import java.security.PrivateKey;
|
25 |
| -import java.security.UnrecoverableKeyException; |
26 | 28 | import java.security.cert.Certificate;
|
27 |
| -import java.security.cert.CertificateException; |
28 | 29 | import java.security.cert.X509Certificate;
|
29 | 30 | import java.util.ArrayList;
|
30 | 31 | import java.util.Collection;
|
|
38 | 39 | */
|
39 | 40 | class StoreKeyConfig extends KeyConfig {
|
40 | 41 |
|
| 42 | + private static final String KEYSTORE_FILE = "keystore"; |
| 43 | + |
41 | 44 | final String keyStorePath;
|
42 | 45 | final String keyStoreType;
|
43 | 46 | final SecureString keyStorePassword;
|
@@ -68,28 +71,42 @@ class StoreKeyConfig extends KeyConfig {
|
68 | 71 |
|
69 | 72 | @Override
|
70 | 73 | X509ExtendedKeyManager createKeyManager(@Nullable Environment environment) {
|
| 74 | + Path ksPath = keyStorePath == null ? null : CertParsingUtils.resolvePath(keyStorePath, environment); |
71 | 75 | try {
|
72 |
| - KeyStore ks = getStore(environment, keyStorePath, keyStoreType, keyStorePassword); |
| 76 | + KeyStore ks = getStore(ksPath, keyStoreType, keyStorePassword); |
73 | 77 | checkKeyStore(ks);
|
74 | 78 | return CertParsingUtils.keyManager(ks, keyPassword.getChars(), keyStoreAlgorithm);
|
75 |
| - } catch (IOException | CertificateException | NoSuchAlgorithmException | UnrecoverableKeyException | KeyStoreException e) { |
76 |
| - throw new ElasticsearchException("failed to initialize a KeyManagerFactory", e); |
| 79 | + } catch (FileNotFoundException | NoSuchFileException e) { |
| 80 | + throw missingKeyConfigFile(e, KEYSTORE_FILE, ksPath); |
| 81 | + } catch (AccessDeniedException e) { |
| 82 | + throw unreadableKeyConfigFile(e, KEYSTORE_FILE, ksPath); |
| 83 | + } catch (AccessControlException e) { |
| 84 | + throw blockedKeyConfigFile(e, environment, KEYSTORE_FILE, ksPath); |
| 85 | + } catch (IOException | GeneralSecurityException e) { |
| 86 | + throw new ElasticsearchException("failed to initialize SSL KeyManager", e); |
77 | 87 | }
|
78 | 88 | }
|
79 | 89 |
|
80 | 90 | @Override
|
81 | 91 | X509ExtendedTrustManager createTrustManager(@Nullable Environment environment) {
|
| 92 | + final Path ksPath = CertParsingUtils.resolvePath(keyStorePath, environment); |
82 | 93 | try {
|
83 |
| - KeyStore ks = getStore(environment, keyStorePath, keyStoreType, keyStorePassword); |
| 94 | + KeyStore ks = getStore(ksPath, keyStoreType, keyStorePassword); |
84 | 95 | return CertParsingUtils.trustManager(ks, trustStoreAlgorithm);
|
85 |
| - } catch (IOException | CertificateException | NoSuchAlgorithmException | KeyStoreException e) { |
86 |
| - throw new ElasticsearchException("failed to initialize a TrustManagerFactory", e); |
| 96 | + } catch (FileNotFoundException | NoSuchFileException e) { |
| 97 | + throw missingTrustConfigFile(e, KEYSTORE_FILE, ksPath); |
| 98 | + } catch (AccessDeniedException e) { |
| 99 | + throw missingTrustConfigFile(e, KEYSTORE_FILE, ksPath); |
| 100 | + } catch (AccessControlException e) { |
| 101 | + throw blockedTrustConfigFile(e, environment, KEYSTORE_FILE, List.of(ksPath)); |
| 102 | + } catch (IOException | GeneralSecurityException e) { |
| 103 | + throw new ElasticsearchException("failed to initialize SSL TrustManager", e); |
87 | 104 | }
|
88 | 105 | }
|
89 | 106 |
|
90 | 107 | @Override
|
91 | 108 | Collection<CertificateInfo> certificates(Environment environment) throws GeneralSecurityException, IOException {
|
92 |
| - final KeyStore trustStore = getStore(environment, keyStorePath, keyStoreType, keyStorePassword); |
| 109 | + final KeyStore trustStore = getStore(CertParsingUtils.resolvePath(keyStorePath, environment), keyStoreType, keyStorePassword); |
93 | 110 | final List<CertificateInfo> certificates = new ArrayList<>();
|
94 | 111 | final Enumeration<String> aliases = trustStore.aliases();
|
95 | 112 | while (aliases.hasMoreElements()) {
|
|
0 commit comments