Skip to content

Commit

Permalink
Add valid search filters. (#191)
Browse files Browse the repository at this point in the history
* Add valid search filters.

* Added this fix to release notes
  • Loading branch information
skkosuri-amzn authored Oct 1, 2021
1 parent 05eb22e commit 3a8b874
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -942,4 +942,22 @@ class MonitorRestApiIT : AlertingRestTestCase() {
val monitorHit = hit["_source"] as Map<String, Any>
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<String, String> = 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())
}
}
}
3 changes: 2 additions & 1 deletion release-notes/opensearch-alerting.release-notes-1.1.0.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -42,4 +43,4 @@ Compatible with OpenSearch 1.1.0

### Refactoring

* Refactor MonitorRunner ([#143](https://github.com/opensearch-project/alerting/pull/143))
* Refactor MonitorRunner ([#143](https://github.com/opensearch-project/alerting/pull/143))

0 comments on commit 3a8b874

Please sign in to comment.