/
RemoteClusterLicenseCheckerTests.java
450 lines (372 loc) · 21.7 KB
/
RemoteClusterLicenseCheckerTests.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
package org.elasticsearch.license;
import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.ResourceNotFoundException;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.client.Client;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.util.concurrent.ThreadContext;
import org.elasticsearch.protocol.xpack.XPackInfoResponse;
import org.elasticsearch.protocol.xpack.license.LicenseStatus;
import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.threadpool.TestThreadPool;
import org.elasticsearch.threadpool.ThreadPool;
import org.elasticsearch.xpack.core.action.XPackInfoAction;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Consumer;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasToString;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.argThat;
import static org.mockito.Matchers.same;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
public final class RemoteClusterLicenseCheckerTests extends ESTestCase {
public void testIsNotRemoteIndex() {
assertFalse(RemoteClusterLicenseChecker.isRemoteIndex("local-index"));
}
public void testIsRemoteIndex() {
assertTrue(RemoteClusterLicenseChecker.isRemoteIndex("remote-cluster:remote-index"));
}
public void testNoRemoteIndex() {
final List<String> indices = Arrays.asList("local-index1", "local-index2");
assertFalse(RemoteClusterLicenseChecker.containsRemoteIndex(indices));
}
public void testRemoteIndex() {
final List<String> indices = Arrays.asList("local-index", "remote-cluster:remote-index");
assertTrue(RemoteClusterLicenseChecker.containsRemoteIndex(indices));
}
public void testNoRemoteIndices() {
final List<String> indices = Collections.singletonList("local-index");
assertThat(RemoteClusterLicenseChecker.remoteIndices(indices), is(empty()));
}
public void testRemoteIndices() {
final List<String> indices = Arrays.asList("local-index1", "remote-cluster1:index1", "local-index2", "remote-cluster2:index1");
assertThat(
RemoteClusterLicenseChecker.remoteIndices(indices),
containsInAnyOrder("remote-cluster1:index1", "remote-cluster2:index1"));
}
public void testNoRemoteClusterAliases() {
final List<String> indices = Arrays.asList("local-index1", "local-index2");
assertThat(RemoteClusterLicenseChecker.remoteClusterAliases(indices), empty());
}
public void testOneRemoteClusterAlias() {
final List<String> indices = Arrays.asList("local-index1", "remote-cluster1:remote-index1");
assertThat(RemoteClusterLicenseChecker.remoteClusterAliases(indices), contains("remote-cluster1"));
}
public void testMoreThanOneRemoteClusterAlias() {
final List<String> indices = Arrays.asList("remote-cluster1:remote-index1", "local-index1", "remote-cluster2:remote-index1");
assertThat(RemoteClusterLicenseChecker.remoteClusterAliases(indices), contains("remote-cluster1", "remote-cluster2"));
}
public void testDuplicateRemoteClusterAlias() {
final List<String> indices = Arrays.asList(
"remote-cluster1:remote-index1", "local-index1", "remote-cluster2:index1", "remote-cluster2:remote-index2");
assertThat(RemoteClusterLicenseChecker.remoteClusterAliases(indices), contains("remote-cluster1", "remote-cluster2"));
}
public void testCheckRemoteClusterLicensesGivenCompatibleLicenses() {
final AtomicInteger index = new AtomicInteger();
final List<XPackInfoResponse> responses = new ArrayList<>();
final ThreadPool threadPool = createMockThreadPool();
final Client client = createMockClient(threadPool);
doAnswer(invocationMock -> {
@SuppressWarnings("unchecked") ActionListener<XPackInfoResponse> listener =
(ActionListener<XPackInfoResponse>) invocationMock.getArguments()[2];
listener.onResponse(responses.get(index.getAndIncrement()));
return null;
}).when(client).execute(same(XPackInfoAction.INSTANCE), any(), any());
final List<String> remoteClusterAliases = Arrays.asList("valid1", "valid2", "valid3");
responses.add(new XPackInfoResponse(null, createPlatinumLicenseResponse(), null));
responses.add(new XPackInfoResponse(null, createPlatinumLicenseResponse(), null));
responses.add(new XPackInfoResponse(null, createPlatinumLicenseResponse(), null));
final RemoteClusterLicenseChecker licenseChecker =
new RemoteClusterLicenseChecker(client, XPackLicenseState::isPlatinumOrTrialOperationMode);
final AtomicReference<RemoteClusterLicenseChecker.LicenseCheck> licenseCheck = new AtomicReference<>();
licenseChecker.checkRemoteClusterLicenses(
remoteClusterAliases,
doubleInvocationProtectingListener(new ActionListener<RemoteClusterLicenseChecker.LicenseCheck>() {
@Override
public void onResponse(final RemoteClusterLicenseChecker.LicenseCheck response) {
licenseCheck.set(response);
}
@Override
public void onFailure(final Exception e) {
fail(e.getMessage());
}
}));
verify(client, times(3)).execute(same(XPackInfoAction.INSTANCE), any(), any());
assertNotNull(licenseCheck.get());
assertTrue(licenseCheck.get().isSuccess());
}
public void testCheckRemoteClusterLicensesGivenIncompatibleLicense() {
final AtomicInteger index = new AtomicInteger();
final List<String> remoteClusterAliases = Arrays.asList("good", "cluster-with-basic-license", "good2");
final List<XPackInfoResponse> responses = new ArrayList<>();
responses.add(new XPackInfoResponse(null, createPlatinumLicenseResponse(), null));
responses.add(new XPackInfoResponse(null, createBasicLicenseResponse(), null));
responses.add(new XPackInfoResponse(null, createPlatinumLicenseResponse(), null));
final ThreadPool threadPool = createMockThreadPool();
final Client client = createMockClient(threadPool);
doAnswer(invocationMock -> {
@SuppressWarnings("unchecked") ActionListener<XPackInfoResponse> listener =
(ActionListener<XPackInfoResponse>) invocationMock.getArguments()[2];
listener.onResponse(responses.get(index.getAndIncrement()));
return null;
}).when(client).execute(same(XPackInfoAction.INSTANCE), any(), any());
final RemoteClusterLicenseChecker licenseChecker =
new RemoteClusterLicenseChecker(client, XPackLicenseState::isPlatinumOrTrialOperationMode);
final AtomicReference<RemoteClusterLicenseChecker.LicenseCheck> licenseCheck = new AtomicReference<>();
licenseChecker.checkRemoteClusterLicenses(
remoteClusterAliases,
doubleInvocationProtectingListener(new ActionListener<RemoteClusterLicenseChecker.LicenseCheck>() {
@Override
public void onResponse(final RemoteClusterLicenseChecker.LicenseCheck response) {
licenseCheck.set(response);
}
@Override
public void onFailure(final Exception e) {
fail(e.getMessage());
}
}));
verify(client, times(2)).execute(same(XPackInfoAction.INSTANCE), any(), any());
assertNotNull(licenseCheck.get());
assertFalse(licenseCheck.get().isSuccess());
assertThat(licenseCheck.get().remoteClusterLicenseInfo().clusterAlias(), equalTo("cluster-with-basic-license"));
assertThat(licenseCheck.get().remoteClusterLicenseInfo().licenseInfo().getType(), equalTo("BASIC"));
}
public void testCheckRemoteClusterLicencesGivenNonExistentCluster() {
final AtomicInteger index = new AtomicInteger();
final List<XPackInfoResponse> responses = new ArrayList<>();
final List<String> remoteClusterAliases = Arrays.asList("valid1", "valid2", "valid3");
final String failingClusterAlias = randomFrom(remoteClusterAliases);
final ThreadPool threadPool = createMockThreadPool();
final Client client = createMockClientThatThrowsOnGetRemoteClusterClient(threadPool, failingClusterAlias);
doAnswer(invocationMock -> {
@SuppressWarnings("unchecked") ActionListener<XPackInfoResponse> listener =
(ActionListener<XPackInfoResponse>) invocationMock.getArguments()[2];
listener.onResponse(responses.get(index.getAndIncrement()));
return null;
}).when(client).execute(same(XPackInfoAction.INSTANCE), any(), any());
responses.add(new XPackInfoResponse(null, createPlatinumLicenseResponse(), null));
responses.add(new XPackInfoResponse(null, createPlatinumLicenseResponse(), null));
responses.add(new XPackInfoResponse(null, createPlatinumLicenseResponse(), null));
final RemoteClusterLicenseChecker licenseChecker =
new RemoteClusterLicenseChecker(client, XPackLicenseState::isPlatinumOrTrialOperationMode);
final AtomicReference<Exception> exception = new AtomicReference<>();
licenseChecker.checkRemoteClusterLicenses(
remoteClusterAliases,
doubleInvocationProtectingListener(new ActionListener<RemoteClusterLicenseChecker.LicenseCheck>() {
@Override
public void onResponse(final RemoteClusterLicenseChecker.LicenseCheck response) {
fail();
}
@Override
public void onFailure(final Exception e) {
exception.set(e);
}
}));
assertNotNull(exception.get());
assertThat(exception.get(), instanceOf(ElasticsearchException.class));
assertThat(exception.get().getMessage(), equalTo("could not determine the license type for cluster [" + failingClusterAlias + "]"));
assertNotNull(exception.get().getCause());
assertThat(exception.get().getCause(), instanceOf(IllegalArgumentException.class));
}
public void testRemoteClusterLicenseCallUsesSystemContext() throws InterruptedException {
final ThreadPool threadPool = new TestThreadPool(getTestName());
try {
final Client client = createMockClient(threadPool);
doAnswer(invocationMock -> {
assertTrue(threadPool.getThreadContext().isSystemContext());
@SuppressWarnings("unchecked") ActionListener<XPackInfoResponse> listener =
(ActionListener<XPackInfoResponse>) invocationMock.getArguments()[2];
listener.onResponse(new XPackInfoResponse(null, createPlatinumLicenseResponse(), null));
return null;
}).when(client).execute(same(XPackInfoAction.INSTANCE), any(), any());
final RemoteClusterLicenseChecker licenseChecker =
new RemoteClusterLicenseChecker(client, XPackLicenseState::isPlatinumOrTrialOperationMode);
final List<String> remoteClusterAliases = Collections.singletonList("valid");
licenseChecker.checkRemoteClusterLicenses(
remoteClusterAliases, doubleInvocationProtectingListener(ActionListener.wrap(() -> {})));
verify(client, times(1)).execute(same(XPackInfoAction.INSTANCE), any(), any());
} finally {
terminate(threadPool);
}
}
public void testListenerIsExecutedWithCallingContext() throws InterruptedException {
final AtomicInteger index = new AtomicInteger();
final List<XPackInfoResponse> responses = new ArrayList<>();
final ThreadPool threadPool = new TestThreadPool(getTestName());
try {
final List<String> remoteClusterAliases = Arrays.asList("valid1", "valid2", "valid3");
final Client client;
final boolean failure = randomBoolean();
if (failure) {
client = createMockClientThatThrowsOnGetRemoteClusterClient(threadPool, randomFrom(remoteClusterAliases));
} else {
client = createMockClient(threadPool);
}
doAnswer(invocationMock -> {
@SuppressWarnings("unchecked") ActionListener<XPackInfoResponse> listener =
(ActionListener<XPackInfoResponse>) invocationMock.getArguments()[2];
listener.onResponse(responses.get(index.getAndIncrement()));
return null;
}).when(client).execute(same(XPackInfoAction.INSTANCE), any(), any());
responses.add(new XPackInfoResponse(null, createPlatinumLicenseResponse(), null));
responses.add(new XPackInfoResponse(null, createPlatinumLicenseResponse(), null));
responses.add(new XPackInfoResponse(null, createPlatinumLicenseResponse(), null));
final RemoteClusterLicenseChecker licenseChecker =
new RemoteClusterLicenseChecker(client, XPackLicenseState::isPlatinumOrTrialOperationMode);
final AtomicBoolean listenerInvoked = new AtomicBoolean();
threadPool.getThreadContext().putHeader("key", "value");
licenseChecker.checkRemoteClusterLicenses(
remoteClusterAliases,
doubleInvocationProtectingListener(new ActionListener<RemoteClusterLicenseChecker.LicenseCheck>() {
@Override
public void onResponse(final RemoteClusterLicenseChecker.LicenseCheck response) {
if (failure) {
fail();
}
assertThat(threadPool.getThreadContext().getHeader("key"), equalTo("value"));
assertFalse(threadPool.getThreadContext().isSystemContext());
listenerInvoked.set(true);
}
@Override
public void onFailure(final Exception e) {
if (failure == false) {
fail();
}
assertThat(threadPool.getThreadContext().getHeader("key"), equalTo("value"));
assertFalse(threadPool.getThreadContext().isSystemContext());
listenerInvoked.set(true);
}
}));
assertTrue(listenerInvoked.get());
} finally {
terminate(threadPool);
}
}
public void testBuildErrorMessageForActiveCompatibleLicense() {
final XPackInfoResponse.LicenseInfo platinumLicence = createPlatinumLicenseResponse();
final RemoteClusterLicenseChecker.RemoteClusterLicenseInfo info =
new RemoteClusterLicenseChecker.RemoteClusterLicenseInfo("platinum-cluster", platinumLicence);
final AssertionError e = expectThrows(
AssertionError.class,
() -> RemoteClusterLicenseChecker.buildErrorMessage("", info, RemoteClusterLicenseChecker::isLicensePlatinumOrTrial));
assertThat(e, hasToString(containsString("license must be incompatible to build error message")));
}
public void testBuildErrorMessageForIncompatibleLicense() {
final XPackInfoResponse.LicenseInfo basicLicense = createBasicLicenseResponse();
final RemoteClusterLicenseChecker.RemoteClusterLicenseInfo info =
new RemoteClusterLicenseChecker.RemoteClusterLicenseInfo("basic-cluster", basicLicense);
assertThat(
RemoteClusterLicenseChecker.buildErrorMessage("Feature", info, RemoteClusterLicenseChecker::isLicensePlatinumOrTrial),
equalTo("the license mode [BASIC] on cluster [basic-cluster] does not enable [Feature]"));
}
public void testBuildErrorMessageForInactiveLicense() {
final XPackInfoResponse.LicenseInfo expiredLicense = createExpiredLicenseResponse();
final RemoteClusterLicenseChecker.RemoteClusterLicenseInfo info =
new RemoteClusterLicenseChecker.RemoteClusterLicenseInfo("expired-cluster", expiredLicense);
assertThat(
RemoteClusterLicenseChecker.buildErrorMessage("Feature", info, RemoteClusterLicenseChecker::isLicensePlatinumOrTrial),
equalTo("the license on cluster [expired-cluster] is not active"));
}
public void testCheckRemoteClusterLicencesNoLicenseMetadata() {
final ThreadPool threadPool = createMockThreadPool();
final Client client = createMockClient(threadPool);
doAnswer(invocationMock -> {
@SuppressWarnings("unchecked") ActionListener<XPackInfoResponse> listener =
(ActionListener<XPackInfoResponse>) invocationMock.getArguments()[2];
listener.onResponse(new XPackInfoResponse(null, null, null));
return null;
}).when(client).execute(same(XPackInfoAction.INSTANCE), any(), any());
final RemoteClusterLicenseChecker licenseChecker =
new RemoteClusterLicenseChecker(client, XPackLicenseState::isPlatinumOrTrialOperationMode);
final AtomicReference<Exception> exception = new AtomicReference<>();
licenseChecker.checkRemoteClusterLicenses(
Collections.singletonList("remote"),
doubleInvocationProtectingListener(new ActionListener<RemoteClusterLicenseChecker.LicenseCheck>() {
@Override
public void onResponse(final RemoteClusterLicenseChecker.LicenseCheck response) {
fail();
}
@Override
public void onFailure(final Exception e) {
exception.set(e);
}
}));
assertNotNull(exception.get());
assertThat(exception.get(), instanceOf(ResourceNotFoundException.class));
assertThat(exception.get().getMessage(), equalTo("license info is missing for cluster [remote]"));
}
private ActionListener<RemoteClusterLicenseChecker.LicenseCheck> doubleInvocationProtectingListener(
final ActionListener<RemoteClusterLicenseChecker.LicenseCheck> listener) {
final AtomicBoolean listenerInvoked = new AtomicBoolean();
return new ActionListener<RemoteClusterLicenseChecker.LicenseCheck>() {
@Override
public void onResponse(final RemoteClusterLicenseChecker.LicenseCheck response) {
if (listenerInvoked.compareAndSet(false, true) == false) {
fail("listener invoked twice");
}
listener.onResponse(response);
}
@Override
public void onFailure(final Exception e) {
if (listenerInvoked.compareAndSet(false, true) == false) {
fail("listener invoked twice");
}
listener.onFailure(e);
}
};
}
private ThreadPool createMockThreadPool() {
final ThreadPool threadPool = mock(ThreadPool.class);
when(threadPool.getThreadContext()).thenReturn(new ThreadContext(Settings.EMPTY));
return threadPool;
}
private Client createMockClient(final ThreadPool threadPool) {
return createMockClient(threadPool, client -> when(client.getRemoteClusterClient(anyString())).thenReturn(client));
}
private Client createMockClientThatThrowsOnGetRemoteClusterClient(final ThreadPool threadPool, final String clusterAlias) {
return createMockClient(
threadPool,
client -> {
when(client.getRemoteClusterClient(clusterAlias)).thenThrow(new IllegalArgumentException());
when(client.getRemoteClusterClient(argThat(not(clusterAlias)))).thenReturn(client);
});
}
private Client createMockClient(final ThreadPool threadPool, final Consumer<Client> finish) {
final Client client = mock(Client.class);
when(client.threadPool()).thenReturn(threadPool);
finish.accept(client);
return client;
}
private XPackInfoResponse.LicenseInfo createPlatinumLicenseResponse() {
return new XPackInfoResponse.LicenseInfo("uid", "PLATINUM", "PLATINUM", LicenseStatus.ACTIVE, randomNonNegativeLong());
}
private XPackInfoResponse.LicenseInfo createBasicLicenseResponse() {
return new XPackInfoResponse.LicenseInfo("uid", "BASIC", "BASIC", LicenseStatus.ACTIVE, randomNonNegativeLong());
}
private XPackInfoResponse.LicenseInfo createExpiredLicenseResponse() {
return new XPackInfoResponse.LicenseInfo("uid", "PLATINUM", "PLATINUM", LicenseStatus.EXPIRED, randomNonNegativeLong());
}
}