diff --git a/alerting/src/main/kotlin/org/opensearch/alerting/resthandler/RestSearchMonitorAction.kt b/alerting/src/main/kotlin/org/opensearch/alerting/resthandler/RestSearchMonitorAction.kt index d19cd6560..a7b167af9 100644 --- a/alerting/src/main/kotlin/org/opensearch/alerting/resthandler/RestSearchMonitorAction.kt +++ b/alerting/src/main/kotlin/org/opensearch/alerting/resthandler/RestSearchMonitorAction.kt @@ -31,6 +31,7 @@ import org.opensearch.action.search.SearchResponse import org.opensearch.alerting.AlertingPlugin import org.opensearch.alerting.action.SearchMonitorAction import org.opensearch.alerting.action.SearchMonitorRequest +import org.opensearch.alerting.alerts.AlertIndices.Companion.ALL_INDEX_PATTERN import org.opensearch.alerting.core.model.ScheduledJob import org.opensearch.alerting.core.model.ScheduledJob.Companion.SCHEDULED_JOBS_INDEX import org.opensearch.alerting.model.Monitor @@ -108,12 +109,18 @@ class RestSearchMonitorAction( log.debug("${request.method()} ${AlertingPlugin.MONITOR_BASE_URI}/_search") val index = request.param("index", SCHEDULED_JOBS_INDEX) + if (index != SCHEDULED_JOBS_INDEX && index != ALL_INDEX_PATTERN) { + throw IllegalArgumentException("Invalid index name.") + } + val searchSourceBuilder = SearchSourceBuilder() searchSourceBuilder.parseXContent(request.contentOrSourceParamParser()) searchSourceBuilder.fetchSource(context(request)) val queryBuilder = QueryBuilders.boolQuery().must(searchSourceBuilder.query()) - queryBuilder.filter(QueryBuilders.existsQuery(Monitor.MONITOR_TYPE)) + if (index == SCHEDULED_JOBS_INDEX) { + queryBuilder.filter(QueryBuilders.existsQuery(Monitor.MONITOR_TYPE)) + } searchSourceBuilder.query(queryBuilder) .seqNoAndPrimaryTerm(true) diff --git a/alerting/src/test/kotlin/org/opensearch/alerting/resthandler/MonitorRestApiIT.kt b/alerting/src/test/kotlin/org/opensearch/alerting/resthandler/MonitorRestApiIT.kt index b1a1cd916..730b3b241 100644 --- a/alerting/src/test/kotlin/org/opensearch/alerting/resthandler/MonitorRestApiIT.kt +++ b/alerting/src/test/kotlin/org/opensearch/alerting/resthandler/MonitorRestApiIT.kt @@ -942,4 +942,22 @@ class MonitorRestApiIT : AlertingRestTestCase() { val monitorHit = hit["_source"] as Map assertEquals("Type is not monitor", monitorHit[Monitor.TYPE_FIELD], "monitor") } + + @Throws(Exception::class) + fun `test search monitor with alerting indices only`() { + // 1. search - must return error as invalid index is passed + val search = SearchSourceBuilder().query(QueryBuilders.matchAllQuery()).toString() + val params: MutableMap = HashMap() + params["index"] = "data-logs" + try { + client().makeRequest( + "GET", + "$ALERTING_BASE_URI/_search", + params, + NStringEntity(search, ContentType.APPLICATION_JSON) + ) + } catch (e: ResponseException) { + assertEquals("Unexpected status", RestStatus.BAD_REQUEST, e.response.restStatus()) + } + } } diff --git a/release-notes/opensearch-alerting.release-notes-1.1.0.0.md b/release-notes/opensearch-alerting.release-notes-1.1.0.0.md index 2ec826afd..ebe3395d9 100644 --- a/release-notes/opensearch-alerting.release-notes-1.1.0.0.md +++ b/release-notes/opensearch-alerting.release-notes-1.1.0.0.md @@ -26,6 +26,7 @@ Compatible with OpenSearch 1.1.0 * Fix bug in paginating multiple bucket paths for Bucket-Level Monitor ([#163](https://github.com/opensearch-project/alerting/pull/163)) * Various bug fixes for Bucket-Level Alerting ([#164](https://github.com/opensearch-project/alerting/pull/164)) * Return only monitors for /monitors/_search ([#162](https://github.com/opensearch-project/alerting/pull/162)) +* Add valid search filters ([#191](https://github.com/opensearch-project/alerting/pull/191)) ### Infrastructure @@ -42,4 +43,4 @@ Compatible with OpenSearch 1.1.0 ### Refactoring -* Refactor MonitorRunner ([#143](https://github.com/opensearch-project/alerting/pull/143)) \ No newline at end of file +* Refactor MonitorRunner ([#143](https://github.com/opensearch-project/alerting/pull/143))