|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the "Elastic License |
| 4 | + * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side |
| 5 | + * Public License v 1"; you may not use this file except in compliance with, at |
| 6 | + * your election, the "Elastic License 2.0", the "GNU Affero General Public |
| 7 | + * License v3.0 only", or the "Server Side Public License, v 1". |
| 8 | + */ |
| 9 | + |
| 10 | +package org.elasticsearch.validation; |
| 11 | + |
| 12 | +import org.elasticsearch.cluster.service.ClusterService; |
| 13 | +import org.elasticsearch.common.settings.ClusterSettings; |
| 14 | +import org.elasticsearch.common.settings.Setting; |
| 15 | +import org.elasticsearch.common.settings.Settings; |
| 16 | +import org.elasticsearch.common.util.concurrent.ThreadContext; |
| 17 | +import org.elasticsearch.common.util.set.Sets; |
| 18 | +import org.elasticsearch.test.ESTestCase; |
| 19 | +import org.elasticsearch.threadpool.ThreadPool; |
| 20 | +import org.junit.BeforeClass; |
| 21 | + |
| 22 | +import java.util.HashSet; |
| 23 | +import java.util.Set; |
| 24 | + |
| 25 | +import static org.mockito.Mockito.mock; |
| 26 | +import static org.mockito.Mockito.when; |
| 27 | + |
| 28 | +public class DotPrefixValidatorTests extends ESTestCase { |
| 29 | + private final OperatorValidator<?> opV = new OperatorValidator<>(); |
| 30 | + private final NonOperatorValidator<?> nonOpV = new NonOperatorValidator<>(); |
| 31 | + private static final Set<Setting<?>> settings; |
| 32 | + |
| 33 | + private static ClusterService clusterService; |
| 34 | + private static ClusterSettings clusterSettings; |
| 35 | + |
| 36 | + static { |
| 37 | + Set<Setting<?>> cSettings = new HashSet<>(ClusterSettings.BUILT_IN_CLUSTER_SETTINGS); |
| 38 | + cSettings.add(DotPrefixValidator.VALIDATE_DOT_PREFIXES); |
| 39 | + settings = cSettings; |
| 40 | + } |
| 41 | + |
| 42 | + @BeforeClass |
| 43 | + public static void beforeClass() { |
| 44 | + clusterService = mock(ClusterService.class); |
| 45 | + clusterSettings = new ClusterSettings(Settings.EMPTY, Sets.newHashSet(DotPrefixValidator.VALIDATE_DOT_PREFIXES)); |
| 46 | + when(clusterService.getClusterSettings()).thenReturn(clusterSettings); |
| 47 | + when(clusterService.getSettings()).thenReturn(Settings.EMPTY); |
| 48 | + when(clusterService.threadPool()).thenReturn(mock(ThreadPool.class)); |
| 49 | + } |
| 50 | + |
| 51 | + public void testValidation() { |
| 52 | + |
| 53 | + nonOpV.validateIndices(Set.of("regular")); |
| 54 | + opV.validateIndices(Set.of("regular")); |
| 55 | + assertFails(Set.of(".regular")); |
| 56 | + opV.validateIndices(Set.of(".regular")); |
| 57 | + assertFails(Set.of("first", ".second")); |
| 58 | + assertFails(Set.of("<.regular-{MM-yy-dd}>")); |
| 59 | + |
| 60 | + // Test ignored names |
| 61 | + nonOpV.validateIndices(Set.of(".elastic-connectors-v1")); |
| 62 | + nonOpV.validateIndices(Set.of(".elastic-connectors-sync-jobs-v1")); |
| 63 | + nonOpV.validateIndices(Set.of(".ml-state")); |
| 64 | + nonOpV.validateIndices(Set.of(".ml-anomalies-unrelated")); |
| 65 | + |
| 66 | + // Test ignored patterns |
| 67 | + nonOpV.validateIndices(Set.of(".ml-state-21309")); |
| 68 | + nonOpV.validateIndices(Set.of(">.ml-state-21309>")); |
| 69 | + nonOpV.validateIndices(Set.of(".slo-observability.sli-v2")); |
| 70 | + nonOpV.validateIndices(Set.of(".slo-observability.sli-v2.3")); |
| 71 | + nonOpV.validateIndices(Set.of(".slo-observability.sli-v2.3-2024-01-01")); |
| 72 | + nonOpV.validateIndices(Set.of("<.slo-observability.sli-v3.3.{2024-10-16||/M{yyyy-MM-dd|UTC}}>")); |
| 73 | + nonOpV.validateIndices(Set.of(".slo-observability.summary-v2")); |
| 74 | + nonOpV.validateIndices(Set.of(".slo-observability.summary-v2.3")); |
| 75 | + nonOpV.validateIndices(Set.of(".slo-observability.summary-v2.3-2024-01-01")); |
| 76 | + nonOpV.validateIndices(Set.of("<.slo-observability.summary-v3.3.{2024-10-16||/M{yyyy-MM-dd|UTC}}>")); |
| 77 | + } |
| 78 | + |
| 79 | + private void assertFails(Set<String> indices) { |
| 80 | + nonOpV.validateIndices(indices); |
| 81 | + assertWarnings( |
| 82 | + "Index [" |
| 83 | + + indices.stream().filter(i -> i.startsWith(".") || i.startsWith("<.")).toList().getFirst() |
| 84 | + + "] name begins with a dot (.), which is deprecated, and will not be allowed in a future Elasticsearch version." |
| 85 | + ); |
| 86 | + } |
| 87 | + |
| 88 | + private class NonOperatorValidator<R> extends DotPrefixValidator<R> { |
| 89 | + |
| 90 | + private NonOperatorValidator() { |
| 91 | + super(new ThreadContext(Settings.EMPTY), clusterService); |
| 92 | + } |
| 93 | + |
| 94 | + @Override |
| 95 | + protected Set<String> getIndicesFromRequest(Object request) { |
| 96 | + return Set.of(); |
| 97 | + } |
| 98 | + |
| 99 | + @Override |
| 100 | + public String actionName() { |
| 101 | + return ""; |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + boolean isInternalRequest() { |
| 106 | + return false; |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + private class OperatorValidator<R> extends NonOperatorValidator<R> { |
| 111 | + @Override |
| 112 | + boolean isInternalRequest() { |
| 113 | + return true; |
| 114 | + } |
| 115 | + } |
| 116 | +} |
0 commit comments