Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add valid search filters. #191

Merged
merged 2 commits into from
Oct 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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))