Skip to content

Commit a3ba11c

Browse files
authored
Improve CryptoService error message on missing secure file (#43623) (#44364)
This improves the error message when encrypting of sensitive watcher data is configured, but no system file was specified in the keystore. This error message is displayed on startup. This also closes the input stream of the secure file properly. Closes #43619
1 parent 67ec0a4 commit a3ba11c

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/crypto/CryptoService.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,20 @@
88
import org.apache.logging.log4j.LogManager;
99
import org.apache.logging.log4j.Logger;
1010
import org.elasticsearch.ElasticsearchException;
11+
import org.elasticsearch.common.CharArrays;
1112
import org.elasticsearch.common.io.Streams;
1213
import org.elasticsearch.common.settings.Setting;
1314
import org.elasticsearch.common.settings.Setting.Property;
1415
import org.elasticsearch.common.settings.Settings;
15-
import org.elasticsearch.xpack.core.watcher.WatcherField;
1616
import org.elasticsearch.xpack.core.security.SecurityField;
17-
import org.elasticsearch.common.CharArrays;
17+
import org.elasticsearch.xpack.core.watcher.WatcherField;
1818

1919
import javax.crypto.BadPaddingException;
2020
import javax.crypto.Cipher;
2121
import javax.crypto.IllegalBlockSizeException;
2222
import javax.crypto.SecretKey;
2323
import javax.crypto.spec.IvParameterSpec;
2424
import javax.crypto.spec.SecretKeySpec;
25-
2625
import java.io.IOException;
2726
import java.io.InputStream;
2827
import java.security.MessageDigest;
@@ -79,11 +78,16 @@ public CryptoService(Settings settings) throws IOException {
7978
throw new IllegalArgumentException("invalid key length [" + keyLength + "]. value must be a multiple of 8");
8079
}
8180

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+
}
8791
}
8892
assert encryptionKey != null : "the encryption key should never be null";
8993
}

x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/watcher/crypto/CryptoServiceTests.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ public void testEncryptedChar() throws Exception {
5454
assertThat(service.isEncrypted(service.encrypt(randomAlphaOfLength(10).toCharArray())), is(true));
5555
}
5656

57+
public void testErrorMessageWhenSecureEncryptionKeySettingDoesNotExist() throws Exception {
58+
final ElasticsearchException e = expectThrows(ElasticsearchException.class, () -> new CryptoService(Settings.EMPTY));
59+
assertThat(e.getMessage(), is("setting [" + WatcherField.ENCRYPTION_KEY_SETTING.getKey() + "] must be set in keystore"));
60+
}
61+
5762
public static byte[] generateKey() {
5863
try {
5964
KeyGenerator generator = KeyGenerator.getInstance(CryptoService.KEY_ALGO);

0 commit comments

Comments
 (0)