From 05bc0d5b01245201b733500f62c9b55d56e1f5f4 Mon Sep 17 00:00:00 2001 From: Bryce Lampe Date: Fri, 21 Jul 2023 18:08:30 -0700 Subject: [PATCH] Handle NPE in isRollupIndex (#855) * Handle NPE in isRollupIndex `metadata.index()` can return `null`, so handle that case by returning `false`. Signed-off-by: Bryce Lampe * unit test Signed-off-by: Bryce Lampe --------- Signed-off-by: Bryce Lampe Co-authored-by: bowenlan-amzn --- .../indexmanagement/rollup/util/RollupUtils.kt | 7 +++++-- .../rollup/util/RollupUtilsTests.kt | 17 +++++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/main/kotlin/org/opensearch/indexmanagement/rollup/util/RollupUtils.kt b/src/main/kotlin/org/opensearch/indexmanagement/rollup/util/RollupUtils.kt index 7b310a4f7..502cc181f 100644 --- a/src/main/kotlin/org/opensearch/indexmanagement/rollup/util/RollupUtils.kt +++ b/src/main/kotlin/org/opensearch/indexmanagement/rollup/util/RollupUtils.kt @@ -68,9 +68,12 @@ const val DATE_FIELD_EPOCH_MILLIS_FORMAT = "epoch_millis" @Suppress("ReturnCount") fun isRollupIndex(index: String, clusterState: ClusterState): Boolean { - if (RollupSettings.ROLLUP_INDEX.get(clusterState.metadata.index(index).settings)) { + val idx = clusterState.metadata.index(index) + if (idx == null) { + return false + } else if (RollupSettings.ROLLUP_INDEX.get(idx.settings)) { return true - } else if (LegacyOpenDistroRollupSettings.ROLLUP_INDEX.get(clusterState.metadata.index(index).settings)) { + } else if (LegacyOpenDistroRollupSettings.ROLLUP_INDEX.get(idx.settings)) { return true } return false diff --git a/src/test/kotlin/org/opensearch/indexmanagement/rollup/util/RollupUtilsTests.kt b/src/test/kotlin/org/opensearch/indexmanagement/rollup/util/RollupUtilsTests.kt index c063a3487..d535f1fb7 100644 --- a/src/test/kotlin/org/opensearch/indexmanagement/rollup/util/RollupUtilsTests.kt +++ b/src/test/kotlin/org/opensearch/indexmanagement/rollup/util/RollupUtilsTests.kt @@ -5,6 +5,11 @@ package org.opensearch.indexmanagement.rollup.util +import com.nhaarman.mockitokotlin2.doReturn +import com.nhaarman.mockitokotlin2.mock +import com.nhaarman.mockitokotlin2.whenever +import org.opensearch.cluster.ClusterState +import org.opensearch.cluster.metadata.Metadata import org.opensearch.index.query.BoolQueryBuilder import org.opensearch.index.query.ConstantScoreQueryBuilder import org.opensearch.index.query.DisMaxQueryBuilder @@ -230,4 +235,16 @@ class RollupUtilsTests : OpenSearchTestCase() { assertEquals("Rewritten aggregation builder is not the correct type", aggBuilder.type, rewrittenAgg.type) } } + + fun `test isRollupIndex`() { + val indexName = "missing index" + val clusterState: ClusterState = mock() + val metadata: Metadata = mock() + + whenever(clusterState.metadata).doReturn(metadata) + whenever(metadata.index(indexName)).doReturn(null) + + val actual = isRollupIndex(indexName, clusterState) + assertFalse(actual) + } }