diff --git a/rest-api-spec/src/main/resources/rest-api-spec/test/cat.indices/10_basic.yml b/rest-api-spec/src/main/resources/rest-api-spec/test/cat.indices/10_basic.yml index 74a7e7d734592..be8cc0ba8190f 100644 --- a/rest-api-spec/src/main/resources/rest-api-spec/test/cat.indices/10_basic.yml +++ b/rest-api-spec/src/main/resources/rest-api-spec/test/cat.indices/10_basic.yml @@ -25,7 +25,7 @@ /^(green \s+ open \s+ index1 \s+ - ([01]{10})?([a-zA-Z0-9=/_+]|[\\\-]){22} \s+ + ([01]{0,10})([a-zA-Z0-9=/_+]|[\\\-]){22} \s+ 1 \s+ 0 \s+ 0 \s+ @@ -81,7 +81,7 @@ /^( \s+ close \s+ index-2 \s+ - ([01]{10})?([a-zA-Z0-9=/_+]|[\\\-]){22} \s+ + ([01]{0,10})([a-zA-Z0-9=/_+]|[\\\-]){22} \s+ \s+ \s+ \s+ @@ -122,7 +122,7 @@ /^(green \s+ close \s+ index-2 \s+ - ([01]{10})?([a-zA-Z0-9=/_+]|[\\\-]){22} \s+ + ([01]{0,10})([a-zA-Z0-9=/_+]|[\\\-]){22} \s+ 3 \s+ 0 \s+ \s+ diff --git a/rest-api-spec/src/main/resources/rest-api-spec/test/cat.indices/20_hidden.yml b/rest-api-spec/src/main/resources/rest-api-spec/test/cat.indices/20_hidden.yml index a78e1efee73d2..4476a78222de1 100644 --- a/rest-api-spec/src/main/resources/rest-api-spec/test/cat.indices/20_hidden.yml +++ b/rest-api-spec/src/main/resources/rest-api-spec/test/cat.indices/20_hidden.yml @@ -27,7 +27,7 @@ /^(green \s+ open \s+ index1 \s+ - ([01]{10})?([a-zA-Z0-9=/_+]|[\\\-]){22} \s+ + ([01]{0,10})([a-zA-Z0-9=/_+]|[\\\-]){22} \s+ 1 \s+ 0 \s+ 0 \s+ @@ -66,7 +66,7 @@ /^(green \s+ open \s+ \.index1 \s+ - ([01]{10})?([a-zA-Z0-9=/_+]|[\\\-]){22} \s+ + ([01]{0,10})([a-zA-Z0-9=/_+]|[\\\-]){22} \s+ 1 \s+ 0 \s+ 0 \s+ @@ -111,7 +111,7 @@ /^(green \s+ open \s+ index1 \s+ - ([01]{10})?([a-zA-Z0-9=/_+]|[\\\-]){22} \s+ + ([01]{0,10})([a-zA-Z0-9=/_+]|[\\\-]){22} \s+ 1 \s+ 0 \s+ 0 \s+ @@ -129,7 +129,7 @@ /^(green \s+ open \s+ index1 \s+ - ([01]{10})?([a-zA-Z0-9=/_+]|[\\\-]){22} \s+ + ([01]{0,10})([a-zA-Z0-9=/_+]|[\\\-]){22} \s+ 1 \s+ 0 \s+ 0 \s+ @@ -171,7 +171,7 @@ /^(green \s+ open \s+ index1 \s+ - ([01]{10})?([a-zA-Z0-9=/_+]|[\\\-]){22} \s+ + ([01]{0,10})([a-zA-Z0-9=/_+]|[\\\-]){22} \s+ 1 \s+ 0 \s+ 0 \s+ @@ -189,7 +189,7 @@ /^(green \s+ open \s+ index1 \s+ - ([01]{10})?([a-zA-Z0-9=/_+]|[\\\-]){22} \s+ + ([01]{0,10})([a-zA-Z0-9=/_+]|[\\\-]){22} \s+ 1 \s+ 0 \s+ 0 \s+ @@ -229,7 +229,7 @@ /^(green \s+ open \s+ index1 \s+ - ([01]{10})?([a-zA-Z0-9=/_+]|[\\\-]){22} \s+ + ([01]{0,10})([a-zA-Z0-9=/_+]|[\\\-]){22} \s+ 1 \s+ 0 \s+ 0 \s+ diff --git a/server/src/test/java/org/opensearch/cluster/metadata/MetadataCreateIndexServiceTests.java b/server/src/test/java/org/opensearch/cluster/metadata/MetadataCreateIndexServiceTests.java index e40826915c848..27e5308b52e3e 100644 --- a/server/src/test/java/org/opensearch/cluster/metadata/MetadataCreateIndexServiceTests.java +++ b/server/src/test/java/org/opensearch/cluster/metadata/MetadataCreateIndexServiceTests.java @@ -54,6 +54,7 @@ import org.opensearch.cluster.routing.allocation.decider.AwarenessAllocationDecider; import org.opensearch.cluster.routing.allocation.decider.MaxRetryAllocationDecider; import org.opensearch.cluster.service.ClusterService; +import org.opensearch.common.UUIDs; import org.opensearch.common.compress.CompressedXContent; import org.opensearch.common.settings.ClusterSettings; import org.opensearch.common.settings.IndexScopedSettings; @@ -107,6 +108,8 @@ import java.util.function.BiConsumer; import java.util.function.BiFunction; import java.util.function.Consumer; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -1810,6 +1813,64 @@ public void testRequestDurabilityWhenRestrictSettingTrue() { assertEquals(Translog.Durability.REQUEST, INDEX_TRANSLOG_DURABILITY_SETTING.get(indexSettings)); } + public void testGetIndexUuidWithBinaryPrefix() { + + String standardIndexUUIDRegex = "^([a-zA-Z0-9=/_+]|[\\\\\\-]){22}$"; + String binaryPrefixIndexUUIDRegex = "^([01]{0,10})([a-zA-Z0-9=/_+]|[\\\\\\-]){22}$"; + + Pattern standardIndexUUIDPattern = Pattern.compile(standardIndexUUIDRegex); + Pattern binaryPrefixIndexUUIDPattern = Pattern.compile(binaryPrefixIndexUUIDRegex); + + Settings binaryPrefixEnabledSettings = Settings.builder() + .put(IndicesService.CLUSTER_INDICES_BINARY_PREFIX_INDEX_UUID_SETTING.getKey(), true) + .build(); + Settings standardIndexUUIDSetting = Settings.builder() + .put(IndicesService.CLUSTER_INDICES_BINARY_PREFIX_INDEX_UUID_SETTING.getKey(), false) + .build(); + + // Binary prefix setting disabled, no binary prefix length + ClusterSettings clusterSettings = new ClusterSettings(standardIndexUUIDSetting, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS); + String indexUUID = MetadataCreateIndexService.getIndexUuidWithBinaryPrefix(clusterSettings); + Matcher matcher = standardIndexUUIDPattern.matcher(indexUUID); + assertTrue(matcher.matches()); + + // Binary prefix setting disabled, variable binary prefix length + Settings settings = Settings.builder() + .put(standardIndexUUIDSetting) + .put(IndicesService.CLUSTER_INDICES_BINARY_PREFIX_INDEX_UUID_LENGTH_SETTING.getKey(), randomIntBetween(0, 10)) + .build(); + clusterSettings = new ClusterSettings(settings, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS); + indexUUID = MetadataCreateIndexService.getIndexUuidWithBinaryPrefix(clusterSettings); + matcher = standardIndexUUIDPattern.matcher(indexUUID); + assertTrue(matcher.matches()); + + // Binary prefix setting enabled, default binary prefix length + clusterSettings = new ClusterSettings(binaryPrefixEnabledSettings, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS); + indexUUID = MetadataCreateIndexService.getIndexUuidWithBinaryPrefix(clusterSettings); + matcher = binaryPrefixIndexUUIDPattern.matcher(indexUUID); + assertTrue(matcher.matches()); + + // Binary prefix setting enabled, binary prefix random length between 2 and 10 + settings = Settings.builder() + .put(binaryPrefixEnabledSettings) + .put(IndicesService.CLUSTER_INDICES_BINARY_PREFIX_INDEX_UUID_LENGTH_SETTING.getKey(), randomIntBetween(0, 10)) + .build(); + clusterSettings = new ClusterSettings(settings, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS); + indexUUID = MetadataCreateIndexService.getIndexUuidWithBinaryPrefix(clusterSettings); + matcher = binaryPrefixIndexUUIDPattern.matcher(indexUUID); + assertTrue(matcher.matches()); + + // Length 0 + indexUUID = UUIDs.randomBase64UUID(); + matcher = binaryPrefixIndexUUIDPattern.matcher(indexUUID); + assertTrue(matcher.matches()); + + // Length 11 + indexUUID = randomFrom("0", "1") + "1".repeat(10) + UUIDs.randomBase64UUID(); + matcher = binaryPrefixIndexUUIDPattern.matcher(indexUUID); + assertFalse(matcher.matches()); + } + private IndexTemplateMetadata addMatchingTemplate(Consumer configurator) { IndexTemplateMetadata.Builder builder = templateMetadataBuilder("template1", "te*"); configurator.accept(builder);