|
8 | 8 | import org.apache.logging.log4j.LogManager; |
9 | 9 | import org.apache.logging.log4j.Logger; |
10 | 10 | import org.elasticsearch.ElasticsearchException; |
| 11 | +import org.elasticsearch.common.CharArrays; |
11 | 12 | import org.elasticsearch.common.io.Streams; |
12 | 13 | import org.elasticsearch.common.settings.Setting; |
13 | 14 | import org.elasticsearch.common.settings.Setting.Property; |
14 | 15 | import org.elasticsearch.common.settings.Settings; |
15 | | -import org.elasticsearch.xpack.core.watcher.WatcherField; |
16 | 16 | import org.elasticsearch.xpack.core.security.SecurityField; |
17 | | -import org.elasticsearch.common.CharArrays; |
| 17 | +import org.elasticsearch.xpack.core.watcher.WatcherField; |
18 | 18 |
|
19 | 19 | import javax.crypto.BadPaddingException; |
20 | 20 | import javax.crypto.Cipher; |
21 | 21 | import javax.crypto.IllegalBlockSizeException; |
22 | 22 | import javax.crypto.SecretKey; |
23 | 23 | import javax.crypto.spec.IvParameterSpec; |
24 | 24 | import javax.crypto.spec.SecretKeySpec; |
25 | | - |
26 | 25 | import java.io.IOException; |
27 | 26 | import java.io.InputStream; |
28 | 27 | import java.security.MessageDigest; |
@@ -79,11 +78,16 @@ public CryptoService(Settings settings) throws IOException { |
79 | 78 | throw new IllegalArgumentException("invalid key length [" + keyLength + "]. value must be a multiple of 8"); |
80 | 79 | } |
81 | 80 |
|
82 | | - SecretKey systemKey = readSystemKey(WatcherField.ENCRYPTION_KEY_SETTING.get(settings)); |
83 | | - try { |
84 | | - encryptionKey = encryptionKey(systemKey, keyLength, keyAlgorithm); |
85 | | - } catch (NoSuchAlgorithmException nsae) { |
86 | | - throw new ElasticsearchException("failed to start crypto service. could not load encryption key", nsae); |
| 81 | + try (InputStream in = WatcherField.ENCRYPTION_KEY_SETTING.get(settings)) { |
| 82 | + if (in == null) { |
| 83 | + throw new ElasticsearchException("setting [" + WatcherField.ENCRYPTION_KEY_SETTING.getKey() + "] must be set in keystore"); |
| 84 | + } |
| 85 | + SecretKey systemKey = readSystemKey(in); |
| 86 | + try { |
| 87 | + encryptionKey = encryptionKey(systemKey, keyLength, keyAlgorithm); |
| 88 | + } catch (NoSuchAlgorithmException nsae) { |
| 89 | + throw new ElasticsearchException("failed to start crypto service. could not load encryption key", nsae); |
| 90 | + } |
87 | 91 | } |
88 | 92 | assert encryptionKey != null : "the encryption key should never be null"; |
89 | 93 | } |
|
0 commit comments