Skip to content

Commit fa9327c

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 44c4412 commit fa9327c

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;
@@ -40,7 +42,6 @@
4042
import javax.crypto.spec.GCMParameterSpec;
4143
import javax.crypto.spec.PBEKeySpec;
4244
import javax.crypto.spec.SecretKeySpec;
43-
4445
import java.io.ByteArrayInputStream;
4546
import java.io.ByteArrayOutputStream;
4647
import java.io.DataInputStream;
@@ -217,7 +218,16 @@ public static KeyStoreWrapper load(Path configDir) throws IOException {
217218
SimpleFSDirectory directory = new SimpleFSDirectory(configDir);
218219
try (IndexInput indexInput = directory.openInput(KEYSTORE_FILENAME, IOContext.READONCE)) {
219220
ChecksumIndexInput input = new BufferedChecksumIndexInput(indexInput);
220-
int formatVersion = CodecUtil.checkHeader(input, KEYSTORE_FILENAME, MIN_FORMAT_VERSION, FORMAT_VERSION);
221+
final int formatVersion;
222+
try {
223+
formatVersion = CodecUtil.checkHeader(input, KEYSTORE_FILENAME, MIN_FORMAT_VERSION, FORMAT_VERSION);
224+
} catch (IndexFormatTooOldException e) {
225+
throw new IllegalStateException("The Elasticsearch keystore [" + keystoreFile + "] format is too old. " +
226+
"You should delete and recreate it in order to upgrade.", e);
227+
} catch (IndexFormatTooNewException e) {
228+
throw new IllegalStateException("The Elasticsearch keystore [" + keystoreFile + "] format is too new. " +
229+
"Are you trying to downgrade? You should delete and recreate it in order to downgrade.", e);
230+
}
221231
byte hasPasswordByte = input.readByte();
222232
boolean hasPassword = hasPasswordByte == 1;
223233
if (hasPassword == false && hasPasswordByte != 0) {

0 commit comments

Comments
 (0)