From a7f53db8a8fea087d58871d01d56dd9fb09071c0 Mon Sep 17 00:00:00 2001 From: Bryce Lampe Date: Mon, 17 Jul 2023 14:37:18 -0700 Subject: [PATCH 1/2] Handle NPE in isRollupIndex `metadata.index()` can return `null`, so handle that case by returning `false`. Signed-off-by: Bryce Lampe --- .../opensearch/indexmanagement/rollup/util/RollupUtils.kt | 7 +++++-- 1 file changed, 5 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 f6d749fbb..f563f99d1 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 From 5122498e8cd95a99c4ed9cfdc2b9ea1c8881e3e9 Mon Sep 17 00:00:00 2001 From: Bryce Lampe Date: Wed, 19 Jul 2023 14:25:02 -0700 Subject: [PATCH 2/2] unit test Signed-off-by: Bryce Lampe --- .../rollup/util/RollupUtilsTests.kt | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) 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) + } }