Skip to content

Commit 9d6eab2

Browse files
authored
Fix NPE in cluster state collector for monitoring. (#52371)
Take into account a null license may be returned by the license service. Closes #52317
1 parent 284c978 commit 9d6eab2

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

0 commit comments

Comments
 (0)