Skip to content

Commit d3db6cb

Browse files
committed
Fix NPE in cluster state collector for monitoring. (elastic#52371)
Take into account a null license may be returned by the license service. Closes elastic#52317
1 parent c9f72a0 commit d3db6cb

File tree

2 files changed

+62
-1
lines changed

2 files changed

+62
-1
lines changed

x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/collector/cluster/ClusterStatsCollector.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,8 @@ protected Collection<MonitoringDoc> doCollect(final MonitoringDoc.Node node,
105105
final List<XPackFeatureSet.Usage> xpackUsage = collect(usageSupplier);
106106
final boolean apmIndicesExist = doAPMIndicesExist(clusterState);
107107
// if they have any other type of license, then they are either okay or already know
108-
final boolean clusterNeedsTLSEnabled = license.operationMode() == License.OperationMode.TRIAL &&
108+
final boolean clusterNeedsTLSEnabled = license != null &&
109+
license.operationMode() == License.OperationMode.TRIAL &&
109110
settings.hasValue(SECURITY_ENABLED.getKey()) &&
110111
SECURITY_ENABLED.get(settings) &&
111112
TRANSPORT_SSL_ENABLED.get(settings) == false;

x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/collector/cluster/ClusterStatsCollectorTests.java

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,4 +260,64 @@ public void testDoCollect() throws Exception {
260260
verify(clusterAdminClient).prepareClusterStats();
261261
verify(client).execute(same(XPackUsageAction.INSTANCE), any(XPackUsageRequest.class));
262262
}
263+
264+
public void testDoCollectNoLicense() throws Exception {
265+
final TimeValue timeout;
266+
{
267+
final String clusterName = randomAlphaOfLength(10);
268+
whenClusterStateWithName(clusterName);
269+
final String clusterUUID = UUID.randomUUID().toString();
270+
whenClusterStateWithUUID(clusterUUID);
271+
timeout = TimeValue.timeValueSeconds(randomIntBetween(1, 120));
272+
withCollectionTimeout(ClusterStatsCollector.CLUSTER_STATS_TIMEOUT, timeout);
273+
}
274+
final IndexNameExpressionResolver indexNameExpressionResolver;
275+
{
276+
indexNameExpressionResolver = mock(IndexNameExpressionResolver.class);
277+
when(indexNameExpressionResolver.concreteIndices(clusterState, IndicesOptions.lenientExpandOpen(), "apm-*"))
278+
.thenReturn(new Index[0]);
279+
}
280+
281+
final Client client = mock(Client.class);
282+
{
283+
final ClusterStatsResponse mockClusterStatsResponse = mock(ClusterStatsResponse.class);
284+
final ClusterHealthStatus clusterStatus = randomFrom(ClusterHealthStatus.values());
285+
when(mockClusterStatsResponse.getStatus()).thenReturn(clusterStatus);
286+
when(mockClusterStatsResponse.getNodesStats()).thenReturn(mock(ClusterStatsNodes.class));
287+
288+
final ClusterStatsIndices mockClusterStatsIndices = mock(ClusterStatsIndices.class);
289+
290+
when(mockClusterStatsIndices.getIndexCount()).thenReturn(0);
291+
when(mockClusterStatsResponse.getIndicesStats()).thenReturn(mockClusterStatsIndices);
292+
293+
final ClusterStatsRequestBuilder clusterStatsRequestBuilder = mock(ClusterStatsRequestBuilder.class);
294+
when(clusterStatsRequestBuilder.get(eq(timeout))).thenReturn(mockClusterStatsResponse);
295+
296+
final ClusterAdminClient clusterAdminClient = mock(ClusterAdminClient.class);
297+
when(clusterAdminClient.prepareClusterStats()).thenReturn(clusterStatsRequestBuilder);
298+
299+
final AdminClient adminClient = mock(AdminClient.class);
300+
when(adminClient.cluster()).thenReturn(clusterAdminClient);
301+
when(client.admin()).thenReturn(adminClient);
302+
303+
final XPackUsageResponse xPackUsageResponse = new XPackUsageResponse(
304+
singletonList(new MonitoringFeatureSetUsage(true, true, false, null)));
305+
@SuppressWarnings("unchecked")
306+
final ActionFuture<XPackUsageResponse> xPackUsageFuture = (ActionFuture<XPackUsageResponse>) mock(ActionFuture.class);
307+
when(client.execute(same(XPackUsageAction.INSTANCE), any(XPackUsageRequest.class))).thenReturn(xPackUsageFuture);
308+
when(xPackUsageFuture.actionGet()).thenReturn(xPackUsageResponse);
309+
}
310+
311+
final long interval = randomNonNegativeLong();
312+
final Settings.Builder settings = Settings.builder();
313+
final MonitoringDoc.Node node = MonitoringTestUtils.randomMonitoringNode(random());
314+
315+
final ClusterStatsCollector collector =
316+
new ClusterStatsCollector(settings.build(), clusterService, licenseState,
317+
client, licenseService, indexNameExpressionResolver);
318+
final Collection<MonitoringDoc> results = collector.doCollect(node, interval, clusterState);
319+
assertEquals(1, results.size());
320+
final ClusterStatsMonitoringDoc doc = (ClusterStatsMonitoringDoc) results.iterator().next();
321+
assertThat(doc.getLicense(), nullValue());
322+
}
263323
}

0 commit comments

Comments
 (0)