|
7 | 7 |
|
8 | 8 | package org.elasticsearch.xpack.ccr; |
9 | 9 |
|
| 10 | +import org.elasticsearch.ElasticsearchStatusException; |
| 11 | +import org.elasticsearch.action.admin.cluster.state.ClusterStateRequest; |
| 12 | +import org.elasticsearch.action.admin.cluster.state.ClusterStateResponse; |
| 13 | +import org.elasticsearch.client.internal.Client; |
10 | 14 | import org.elasticsearch.client.internal.RemoteClusterClient; |
| 15 | +import org.elasticsearch.cluster.ClusterName; |
| 16 | +import org.elasticsearch.cluster.ClusterState; |
| 17 | +import org.elasticsearch.cluster.metadata.AliasMetadata; |
| 18 | +import org.elasticsearch.cluster.metadata.DataStream; |
| 19 | +import org.elasticsearch.cluster.metadata.DataStreamAlias; |
| 20 | +import org.elasticsearch.cluster.metadata.DataStreamTestHelper; |
| 21 | +import org.elasticsearch.cluster.metadata.IndexMetadata; |
| 22 | +import org.elasticsearch.cluster.metadata.Metadata; |
11 | 23 | import org.elasticsearch.common.settings.Settings; |
12 | 24 | import org.elasticsearch.common.util.concurrent.ThreadContext; |
| 25 | +import org.elasticsearch.core.Tuple; |
| 26 | +import org.elasticsearch.index.IndexNotFoundException; |
| 27 | +import org.elasticsearch.index.IndexVersion; |
| 28 | +import org.elasticsearch.indices.IndexClosedException; |
| 29 | +import org.elasticsearch.license.RemoteClusterLicenseChecker; |
13 | 30 | import org.elasticsearch.test.ESTestCase; |
| 31 | +import org.elasticsearch.threadpool.ThreadPool; |
14 | 32 | import org.elasticsearch.xpack.core.security.user.User; |
| 33 | +import org.mockito.ArgumentCaptor; |
15 | 34 |
|
| 35 | +import java.util.List; |
| 36 | +import java.util.Map; |
| 37 | +import java.util.concurrent.ExecutorService; |
16 | 38 | import java.util.concurrent.atomic.AtomicBoolean; |
| 39 | +import java.util.function.BiConsumer; |
| 40 | +import java.util.function.Consumer; |
| 41 | +import java.util.function.Function; |
17 | 42 |
|
18 | 43 | import static org.hamcrest.Matchers.containsString; |
| 44 | +import static org.hamcrest.Matchers.equalTo; |
19 | 45 | import static org.hamcrest.Matchers.hasToString; |
20 | 46 | import static org.hamcrest.Matchers.instanceOf; |
| 47 | +import static org.mockito.ArgumentMatchers.any; |
| 48 | +import static org.mockito.ArgumentMatchers.eq; |
21 | 49 | import static org.mockito.Mockito.mock; |
| 50 | +import static org.mockito.Mockito.times; |
| 51 | +import static org.mockito.Mockito.verify; |
| 52 | +import static org.mockito.Mockito.verifyNoInteractions; |
| 53 | +import static org.mockito.Mockito.when; |
22 | 54 |
|
23 | 55 | public class CcrLicenseCheckerTests extends ESTestCase { |
24 | 56 |
|
@@ -46,4 +78,164 @@ User getUser(final ThreadContext threadContext) { |
46 | 78 | assertTrue(invoked.get()); |
47 | 79 | } |
48 | 80 |
|
| 81 | + /** |
| 82 | + * Tests all validation logic after obtaining the remote cluster state and before executing the check for follower privileges. |
| 83 | + */ |
| 84 | + public void testRemoteIndexValidation() { |
| 85 | + // A cluster state with |
| 86 | + // - a data stream, containing a backing index and a failure index |
| 87 | + // - an alias that points to said data stream |
| 88 | + // - a standalone index |
| 89 | + // - an alias that points to said standalone index |
| 90 | + // - a closed index |
| 91 | + String indexName = "random-index"; |
| 92 | + String closedIndexName = "closed-index"; |
| 93 | + String dataStreamName = "logs-test-data"; |
| 94 | + String aliasName = "foo-alias"; |
| 95 | + String dsAliasName = "ds-alias"; |
| 96 | + IndexMetadata indexMetadata = IndexMetadata.builder(indexName) |
| 97 | + .putAlias(AliasMetadata.builder(aliasName)) |
| 98 | + .settings(settings(IndexVersion.current())) |
| 99 | + .numberOfShards(5) |
| 100 | + .numberOfReplicas(1) |
| 101 | + .build(); |
| 102 | + IndexMetadata closedIndexMetadata = IndexMetadata.builder(closedIndexName) |
| 103 | + .settings(settings(IndexVersion.current())) |
| 104 | + .numberOfShards(5) |
| 105 | + .numberOfReplicas(1) |
| 106 | + .state(IndexMetadata.State.CLOSE) |
| 107 | + .build(); |
| 108 | + IndexMetadata firstBackingIndex = DataStreamTestHelper.createFirstBackingIndex(dataStreamName).build(); |
| 109 | + IndexMetadata firstFailureStore = DataStreamTestHelper.createFirstFailureStore(dataStreamName).build(); |
| 110 | + DataStream dataStream = DataStreamTestHelper.newInstance( |
| 111 | + dataStreamName, |
| 112 | + List.of(firstBackingIndex.getIndex()), |
| 113 | + List.of(firstFailureStore.getIndex()) |
| 114 | + ); |
| 115 | + ClusterState remoteClusterState = ClusterState.builder(new ClusterName(randomIdentifier())) |
| 116 | + .metadata( |
| 117 | + Metadata.builder() |
| 118 | + .put(indexMetadata, false) |
| 119 | + .put(closedIndexMetadata, false) |
| 120 | + .put(firstBackingIndex, false) |
| 121 | + .put(firstFailureStore, false) |
| 122 | + .dataStreams( |
| 123 | + Map.of(dataStreamName, dataStream), |
| 124 | + Map.of(dsAliasName, new DataStreamAlias(dsAliasName, List.of(dataStreamName), dataStreamName, Map.of())) |
| 125 | + ) |
| 126 | + ) |
| 127 | + .build(); |
| 128 | + |
| 129 | + final boolean isCcrAllowed = randomBoolean(); |
| 130 | + final CcrLicenseChecker checker = new CcrLicenseChecker(() -> isCcrAllowed, () -> true) { |
| 131 | + @Override |
| 132 | + User getUser(ThreadContext threadContext) { |
| 133 | + return null; |
| 134 | + } |
| 135 | + |
| 136 | + @Override |
| 137 | + protected void doCheckRemoteClusterLicenseAndFetchClusterState( |
| 138 | + Client client, |
| 139 | + String clusterAlias, |
| 140 | + RemoteClusterClient remoteClient, |
| 141 | + ClusterStateRequest request, |
| 142 | + Consumer<Exception> onFailure, |
| 143 | + Consumer<ClusterStateResponse> leaderClusterStateConsumer, |
| 144 | + Function<RemoteClusterLicenseChecker.LicenseCheck, ElasticsearchStatusException> nonCompliantLicense, |
| 145 | + Function<Exception, ElasticsearchStatusException> unknownLicense |
| 146 | + ) { |
| 147 | + leaderClusterStateConsumer.accept(new ClusterStateResponse(remoteClusterState.getClusterName(), remoteClusterState, false)); |
| 148 | + } |
| 149 | + |
| 150 | + @Override |
| 151 | + public void hasPrivilegesToFollowIndices( |
| 152 | + ThreadContext threadContext, |
| 153 | + RemoteClusterClient remoteClient, |
| 154 | + String[] indices, |
| 155 | + Consumer<Exception> handler |
| 156 | + ) { |
| 157 | + fail("Test case should fail before this code is called"); |
| 158 | + } |
| 159 | + }; |
| 160 | + |
| 161 | + String clusterAlias = randomIdentifier(); |
| 162 | + |
| 163 | + ExecutorService mockExecutor = mock(ExecutorService.class); |
| 164 | + ThreadPool mockThreadPool = mock(ThreadPool.class); |
| 165 | + when(mockThreadPool.executor(eq(Ccr.CCR_THREAD_POOL_NAME))).thenReturn(mockExecutor); |
| 166 | + RemoteClusterClient mockRemoteClient = mock(RemoteClusterClient.class); |
| 167 | + |
| 168 | + Client mockClient = mock(Client.class); |
| 169 | + when(mockClient.threadPool()).thenReturn(mockThreadPool); |
| 170 | + when(mockClient.getRemoteClusterClient(eq(clusterAlias), eq(mockExecutor), any())).thenReturn(mockRemoteClient); |
| 171 | + |
| 172 | + // When following an index that does not exist, throw IndexNotFoundException |
| 173 | + { |
| 174 | + Exception exception = executeExpectingException(checker, mockClient, clusterAlias, "non-existent-index"); |
| 175 | + assertThat(exception, instanceOf(IndexNotFoundException.class)); |
| 176 | + assertThat(exception.getMessage(), equalTo("no such index [non-existent-index]")); |
| 177 | + } |
| 178 | + |
| 179 | + // When following an alias, throw IllegalArgumentException |
| 180 | + { |
| 181 | + Exception exception = executeExpectingException(checker, mockClient, clusterAlias, aliasName); |
| 182 | + assertThat(exception, instanceOf(IllegalArgumentException.class)); |
| 183 | + assertThat(exception.getMessage(), equalTo("cannot follow [" + aliasName + "], because it is a ALIAS")); |
| 184 | + } |
| 185 | + |
| 186 | + // When following a data stream, throw IllegalArgumentException |
| 187 | + { |
| 188 | + Exception exception = executeExpectingException(checker, mockClient, clusterAlias, dataStreamName); |
| 189 | + assertThat(exception, instanceOf(IllegalArgumentException.class)); |
| 190 | + assertThat(exception.getMessage(), equalTo("cannot follow [" + dataStreamName + "], because it is a DATA_STREAM")); |
| 191 | + } |
| 192 | + |
| 193 | + // When following a data stream alias, throw IllegalArgumentException |
| 194 | + { |
| 195 | + Exception exception = executeExpectingException(checker, mockClient, clusterAlias, dsAliasName); |
| 196 | + assertThat(exception, instanceOf(IllegalArgumentException.class)); |
| 197 | + assertThat(exception.getMessage(), equalTo("cannot follow [" + dsAliasName + "], because it is a ALIAS")); |
| 198 | + } |
| 199 | + |
| 200 | + // When following a closed index, throw IndexClosedException |
| 201 | + { |
| 202 | + Exception exception = executeExpectingException(checker, mockClient, clusterAlias, closedIndexName); |
| 203 | + assertThat(exception, instanceOf(IndexClosedException.class)); |
| 204 | + assertThat(exception.getMessage(), equalTo("closed")); |
| 205 | + } |
| 206 | + |
| 207 | + // When following a failure store index, throw IllegalArgumentException |
| 208 | + { |
| 209 | + Exception exception = executeExpectingException(checker, mockClient, clusterAlias, firstFailureStore.getIndex().getName()); |
| 210 | + assertThat(exception, instanceOf(IllegalArgumentException.class)); |
| 211 | + assertThat( |
| 212 | + exception.getMessage(), |
| 213 | + equalTo("cannot follow [" + firstFailureStore.getIndex().getName() + "], because it is a failure store index") |
| 214 | + ); |
| 215 | + } |
| 216 | + } |
| 217 | + |
| 218 | + private static Exception executeExpectingException( |
| 219 | + CcrLicenseChecker checker, |
| 220 | + Client mockClient, |
| 221 | + String clusterAlias, |
| 222 | + String leaderIndex |
| 223 | + ) { |
| 224 | + @SuppressWarnings("unchecked") |
| 225 | + Consumer<Exception> onFailure = mock(Consumer.class); |
| 226 | + @SuppressWarnings("unchecked") |
| 227 | + BiConsumer<String[], Tuple<IndexMetadata, DataStream>> consumer = mock(BiConsumer.class); |
| 228 | + checker.checkRemoteClusterLicenseAndFetchLeaderIndexMetadataAndHistoryUUIDs( |
| 229 | + mockClient, |
| 230 | + clusterAlias, |
| 231 | + leaderIndex, |
| 232 | + onFailure, |
| 233 | + consumer |
| 234 | + ); |
| 235 | + ArgumentCaptor<Exception> captor = ArgumentCaptor.forClass(Exception.class); |
| 236 | + verify(onFailure, times(1)).accept(captor.capture()); |
| 237 | + verifyNoInteractions(consumer); |
| 238 | + return captor.getValue(); |
| 239 | + } |
| 240 | + |
49 | 241 | } |
0 commit comments