Skip to content

Commit 9c2980a

Browse files
authored
Remote cluster license checker and no license info. (#36837)
Fail with a descriptive error when the xpack info returns no license info. Relates to #36815
1 parent cfc0a47 commit 9c2980a

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

x-pack/plugin/core/src/main/java/org/elasticsearch/license/RemoteClusterLicenseChecker.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
package org.elasticsearch.license;
88

99
import org.elasticsearch.ElasticsearchException;
10+
import org.elasticsearch.ResourceNotFoundException;
1011
import org.elasticsearch.action.ActionListener;
1112
import org.elasticsearch.action.support.ContextPreservingActionListener;
1213
import org.elasticsearch.client.Client;
@@ -159,6 +160,10 @@ public void checkRemoteClusterLicenses(final List<String> clusterAliases, final
159160
@Override
160161
public void onResponse(final XPackInfoResponse xPackInfoResponse) {
161162
final XPackInfoResponse.LicenseInfo licenseInfo = xPackInfoResponse.getLicenseInfo();
163+
if (licenseInfo == null) {
164+
listener.onFailure(new ResourceNotFoundException("license info is missing for cluster [" + clusterAlias.get() + "]"));
165+
return;
166+
}
162167
if ((licenseInfo.getStatus() == LicenseStatus.ACTIVE) == false
163168
|| predicate.test(License.OperationMode.resolve(licenseInfo.getMode())) == false) {
164169
listener.onResponse(LicenseCheck.failure(new RemoteClusterLicenseInfo(clusterAlias.get(), licenseInfo)));

x-pack/plugin/core/src/test/java/org/elasticsearch/license/RemoteClusterLicenseCheckerTests.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
package org.elasticsearch.license;
88

99
import org.elasticsearch.ElasticsearchException;
10+
import org.elasticsearch.ResourceNotFoundException;
1011
import org.elasticsearch.action.ActionListener;
1112
import org.elasticsearch.client.Client;
1213
import org.elasticsearch.common.settings.Settings;
@@ -349,6 +350,41 @@ public void testBuildErrorMessageForInactiveLicense() {
349350
equalTo("the license on cluster [expired-cluster] is not active"));
350351
}
351352

353+
public void testCheckRemoteClusterLicencesNoLicenseMetadata() {
354+
final ThreadPool threadPool = createMockThreadPool();
355+
final Client client = createMockClient(threadPool);
356+
doAnswer(invocationMock -> {
357+
@SuppressWarnings("unchecked") ActionListener<XPackInfoResponse> listener =
358+
(ActionListener<XPackInfoResponse>) invocationMock.getArguments()[2];
359+
listener.onResponse(new XPackInfoResponse(null, null, null));
360+
return null;
361+
}).when(client).execute(same(XPackInfoAction.INSTANCE), any(), any());
362+
363+
final RemoteClusterLicenseChecker licenseChecker =
364+
new RemoteClusterLicenseChecker(client, XPackLicenseState::isPlatinumOrTrialOperationMode);
365+
final AtomicReference<Exception> exception = new AtomicReference<>();
366+
367+
licenseChecker.checkRemoteClusterLicenses(
368+
Collections.singletonList("remote"),
369+
doubleInvocationProtectingListener(new ActionListener<RemoteClusterLicenseChecker.LicenseCheck>() {
370+
371+
@Override
372+
public void onResponse(final RemoteClusterLicenseChecker.LicenseCheck response) {
373+
fail();
374+
}
375+
376+
@Override
377+
public void onFailure(final Exception e) {
378+
exception.set(e);
379+
}
380+
381+
}));
382+
383+
assertNotNull(exception.get());
384+
assertThat(exception.get(), instanceOf(ResourceNotFoundException.class));
385+
assertThat(exception.get().getMessage(), equalTo("license info is missing for cluster [remote]"));
386+
}
387+
352388
private ActionListener<RemoteClusterLicenseChecker.LicenseCheck> doubleInvocationProtectingListener(
353389
final ActionListener<RemoteClusterLicenseChecker.LicenseCheck> listener) {
354390
final AtomicBoolean listenerInvoked = new AtomicBoolean();

0 commit comments

Comments
 (0)