diff --git a/qa/evil-tests/src/test/java/org/elasticsearch/index/store/LuceneFilesExtensionsTests.java b/qa/evil-tests/src/test/java/org/elasticsearch/index/store/LuceneFilesExtensionsTests.java new file mode 100644 index 0000000000000..9248a2ca8e9fb --- /dev/null +++ b/qa/evil-tests/src/test/java/org/elasticsearch/index/store/LuceneFilesExtensionsTests.java @@ -0,0 +1,43 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0 and the Server Side Public License, v 1; you may not use this file except + * in compliance with, at your election, the Elastic License 2.0 or the Server + * Side Public License, v 1. + */ + +package org.elasticsearch.index.store; + +import org.elasticsearch.Assertions; +import org.elasticsearch.core.SuppressForbidden; +import org.elasticsearch.test.ESTestCase; + +import static org.hamcrest.Matchers.containsString; + +public class LuceneFilesExtensionsTests extends ESTestCase { + + public void testUnknownFileExtension() { + if (Assertions.ENABLED) { + AssertionError e = expectThrows(AssertionError.class, () -> LuceneFilesExtensions.fromExtension("abc")); + assertThat(e.getMessage(), containsString("unknown Lucene file extension [abc]")); + + setEsAllowUnknownLuceneFileExtensions("true"); + try { + assertNull(LuceneFilesExtensions.fromExtension("abc")); + } finally { + setEsAllowUnknownLuceneFileExtensions(null); + } + } else { + assertNull(LuceneFilesExtensions.fromExtension("abc")); + } + } + + @SuppressForbidden(reason = "set or clear system property es.allow_unknown_lucene_file_extensions") + public void setEsAllowUnknownLuceneFileExtensions(final String value) { + if (value == null) { + System.clearProperty("es.allow_unknown_lucene_file_extensions"); + } else { + System.setProperty("es.allow_unknown_lucene_file_extensions", value); + } + } +} diff --git a/server/src/main/java/org/elasticsearch/index/store/LuceneFilesExtensions.java b/server/src/main/java/org/elasticsearch/index/store/LuceneFilesExtensions.java index e173534795e6a..9d43fd4d7aa41 100644 --- a/server/src/main/java/org/elasticsearch/index/store/LuceneFilesExtensions.java +++ b/server/src/main/java/org/elasticsearch/index/store/LuceneFilesExtensions.java @@ -71,6 +71,16 @@ public enum LuceneFilesExtensions { // Lucene 9.0 indexed vectors metadata VEM("vem","Vector Metadata", true, false); + /** + * Allow plugin developers of custom codecs to opt out of the assertion in {@link #fromExtension} + * that checks that all encountered file extensions are known to this class. + * In the future, we would like to add a proper plugin extension point for this. + */ + private static boolean allowUnknownLuceneFileExtensions() { + return Boolean.parseBoolean( + System.getProperty("es.allow_unknown_lucene_file_extensions", "false")); + } + /** * Lucene file's extension. */ @@ -128,7 +138,7 @@ public boolean shouldMmap() { public static LuceneFilesExtensions fromExtension(String ext) { if (ext != null && ext.isEmpty() == false) { final LuceneFilesExtensions extension = extensions.get(ext); - assert extension != null: "unknown Lucene file extension [" + ext + ']'; + assert allowUnknownLuceneFileExtensions() || extension != null: "unknown Lucene file extension [" + ext + ']'; return extension; } return null; diff --git a/x-pack/plugin/searchable-snapshots/src/main/java/org/elasticsearch/xpack/searchablesnapshots/cache/blob/BlobStoreCacheService.java b/x-pack/plugin/searchable-snapshots/src/main/java/org/elasticsearch/xpack/searchablesnapshots/cache/blob/BlobStoreCacheService.java index 2f090d3ccf594..adeffe253efcf 100644 --- a/x-pack/plugin/searchable-snapshots/src/main/java/org/elasticsearch/xpack/searchablesnapshots/cache/blob/BlobStoreCacheService.java +++ b/x-pack/plugin/searchable-snapshots/src/main/java/org/elasticsearch/xpack/searchablesnapshots/cache/blob/BlobStoreCacheService.java @@ -260,7 +260,6 @@ public void onFailure(Exception e) { */ public ByteRange computeBlobCacheByteRange(String fileName, long fileLength, ByteSizeValue maxMetadataLength) { final LuceneFilesExtensions fileExtension = LuceneFilesExtensions.fromExtension(IndexFileNames.getExtension(fileName)); - assert fileExtension != null : "unknown Lucene file extension [" + fileName + "] - should it be considered a metadata file?"; if (useLegacyCachedBlobSizes()) { if (fileLength <= ByteSizeUnit.KB.toBytes(8L)) {