Skip to content

Commit

Permalink
Remote cluster license checker and no license info. (#36837)
Browse files Browse the repository at this point in the history
Fail with a descriptive error when the xpack info returns no license info.

Relates to #36815
  • Loading branch information
martijnvg committed Dec 20, 2018
1 parent 0d2528c commit 3e32a60
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
Expand Up @@ -7,6 +7,7 @@
package org.elasticsearch.license;

import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.ResourceNotFoundException;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.support.ContextPreservingActionListener;
import org.elasticsearch.client.Client;
Expand Down Expand Up @@ -159,6 +160,10 @@ public void checkRemoteClusterLicenses(final List<String> clusterAliases, final
@Override
public void onResponse(final XPackInfoResponse xPackInfoResponse) {
final XPackInfoResponse.LicenseInfo licenseInfo = xPackInfoResponse.getLicenseInfo();
if (licenseInfo == null) {
listener.onFailure(new ResourceNotFoundException("license info is missing for cluster [" + clusterAlias.get() + "]"));
return;
}
if ((licenseInfo.getStatus() == LicenseStatus.ACTIVE) == false
|| predicate.test(License.OperationMode.resolve(licenseInfo.getMode())) == false) {
listener.onResponse(LicenseCheck.failure(new RemoteClusterLicenseInfo(clusterAlias.get(), licenseInfo)));
Expand Down
Expand Up @@ -7,6 +7,7 @@
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;
Expand Down Expand Up @@ -349,6 +350,41 @@ public void testBuildErrorMessageForInactiveLicense() {
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();
Expand Down

0 comments on commit 3e32a60

Please sign in to comment.