Skip to content

Commit 48dec10

Browse files
committed
Add more meaningful keystore version mismatch errors (#46291)
This commit changes the version bounds of keystore reading to give better error messages when a user has a too new or too old format. relates #44624
1 parent a0b5819 commit 48dec10

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

server/src/main/java/org/elasticsearch/common/settings/KeyStoreWrapper.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
package org.elasticsearch.common.settings;
2121

2222
import org.apache.lucene.codecs.CodecUtil;
23+
import org.apache.lucene.index.IndexFormatTooNewException;
24+
import org.apache.lucene.index.IndexFormatTooOldException;
2325
import org.apache.lucene.store.BufferedChecksumIndexInput;
2426
import org.apache.lucene.store.ChecksumIndexInput;
2527
import org.apache.lucene.store.IOContext;
@@ -39,7 +41,6 @@
3941
import javax.crypto.spec.GCMParameterSpec;
4042
import javax.crypto.spec.PBEKeySpec;
4143
import javax.crypto.spec.SecretKeySpec;
42-
4344
import java.io.ByteArrayInputStream;
4445
import java.io.ByteArrayOutputStream;
4546
import java.io.DataInputStream;
@@ -205,7 +206,16 @@ public static KeyStoreWrapper load(Path configDir) throws IOException {
205206
SimpleFSDirectory directory = new SimpleFSDirectory(configDir);
206207
try (IndexInput indexInput = directory.openInput(KEYSTORE_FILENAME, IOContext.READONCE)) {
207208
ChecksumIndexInput input = new BufferedChecksumIndexInput(indexInput);
208-
int formatVersion = CodecUtil.checkHeader(input, KEYSTORE_FILENAME, MIN_FORMAT_VERSION, FORMAT_VERSION);
209+
final int formatVersion;
210+
try {
211+
formatVersion = CodecUtil.checkHeader(input, KEYSTORE_FILENAME, MIN_FORMAT_VERSION, FORMAT_VERSION);
212+
} catch (IndexFormatTooOldException e) {
213+
throw new IllegalStateException("The Elasticsearch keystore [" + keystoreFile + "] format is too old. " +
214+
"You should delete and recreate it in order to upgrade.", e);
215+
} catch (IndexFormatTooNewException e) {
216+
throw new IllegalStateException("The Elasticsearch keystore [" + keystoreFile + "] format is too new. " +
217+
"Are you trying to downgrade? You should delete and recreate it in order to downgrade.", e);
218+
}
209219
byte hasPasswordByte = input.readByte();
210220
boolean hasPassword = hasPasswordByte == 1;
211221
if (hasPassword == false && hasPasswordByte != 0) {

0 commit comments

Comments
 (0)