Skip to content
This repository was archived by the owner on Aug 2, 2022. It is now read-only.

Commit b0e54ca

Browse files
authored
fix failed IT cases for AD (#320)
1 parent eb60989 commit b0e54ca

File tree

4 files changed

+114
-86
lines changed

4 files changed

+114
-86
lines changed

alerting/src/test/kotlin/com/amazon/opendistroforelasticsearch/alerting/MonitorRunnerIT.kt

Lines changed: 85 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -654,92 +654,104 @@ class MonitorRunnerIT : AlertingRestTestCase() {
654654
}
655655

656656
fun `test execute AD monitor doesn't return search result without user`() {
657-
val user = randomUser()
658-
val detectorId = randomAlphaOfLength(5)
659-
prepareTestAnomalyResult(detectorId, user)
660-
// for old monitor before enable FGAC, the user field is empty
661-
val monitor = randomADMonitor(inputs = listOf(adSearchInput(detectorId)), triggers = listOf(adMonitorTrigger()), user = null)
662-
val response = executeMonitor(monitor, params = DRYRUN_MONITOR)
663-
val output = entityAsMap(response)
664-
@Suppress("UNCHECKED_CAST")
665-
(output["trigger_results"] as HashMap<String, Any>).forEach {
666-
_, v -> assertTrue((v as HashMap<String, Boolean>)["triggered"] as Boolean)
657+
// TODO: change to REST API call to test security enabled case
658+
if (!securityEnabled()) {
659+
val user = randomUser()
660+
val detectorId = randomAlphaOfLength(5)
661+
prepareTestAnomalyResult(detectorId, user)
662+
// for old monitor before enable FGAC, the user field is empty
663+
val monitor = randomADMonitor(inputs = listOf(adSearchInput(detectorId)), triggers = listOf(adMonitorTrigger()), user = null)
664+
val response = executeMonitor(monitor, params = DRYRUN_MONITOR)
665+
val output = entityAsMap(response)
666+
@Suppress("UNCHECKED_CAST")
667+
(output["trigger_results"] as HashMap<String, Any>).forEach {
668+
_, v -> assertTrue((v as HashMap<String, Boolean>)["triggered"] as Boolean)
669+
}
670+
assertEquals(monitor.name, output["monitor_name"])
671+
@Suppress("UNCHECKED_CAST")
672+
val searchResult = (output.objectMap("input_results")["results"] as List<Map<String, Any>>).first()
673+
@Suppress("UNCHECKED_CAST")
674+
val total = searchResult.stringMap("hits")?.get("total") as Map<String, String>
675+
assertEquals("Incorrect search result", 1, total["value"])
676+
@Suppress("UNCHECKED_CAST")
677+
val maxAnomalyGrade = searchResult.stringMap("aggregations")?.get("max_anomaly_grade") as Map<String, String>
678+
assertEquals("Incorrect search result", 0.75, maxAnomalyGrade["value"])
667679
}
668-
assertEquals(monitor.name, output["monitor_name"])
669-
@Suppress("UNCHECKED_CAST")
670-
val searchResult = (output.objectMap("input_results")["results"] as List<Map<String, Any>>).first()
671-
@Suppress("UNCHECKED_CAST")
672-
val total = searchResult.stringMap("hits")?.get("total") as Map<String, String>
673-
assertEquals("Incorrect search result", 1, total["value"])
674-
@Suppress("UNCHECKED_CAST")
675-
val maxAnomalyGrade = searchResult.stringMap("aggregations")?.get("max_anomaly_grade") as Map<String, String>
676-
assertEquals("Incorrect search result", 0.75, maxAnomalyGrade["value"])
677680
}
678681

679682
fun `test execute AD monitor doesn't return search result with empty backend role`() {
680-
val user = randomUser()
681-
val detectorId = randomAlphaOfLength(5)
682-
prepareTestAnomalyResult(detectorId, user)
683-
// for old monitor before enable FGAC, the user field is empty
684-
val monitor = randomADMonitor(inputs = listOf(adSearchInput(detectorId)), triggers = listOf(adMonitorTrigger()),
685-
user = User(user.name, listOf(), user.roles, user.customAttNames))
686-
val response = executeMonitor(monitor, params = DRYRUN_MONITOR)
687-
val output = entityAsMap(response)
688-
@Suppress("UNCHECKED_CAST")
689-
(output["trigger_results"] as HashMap<String, Any>).forEach {
690-
_, v -> assertTrue((v as HashMap<String, Boolean>)["triggered"] as Boolean)
683+
// TODO: change to REST API call to test security enabled case
684+
if (!securityEnabled()) {
685+
val user = randomUser()
686+
val detectorId = randomAlphaOfLength(5)
687+
prepareTestAnomalyResult(detectorId, user)
688+
// for old monitor before enable FGAC, the user field is empty
689+
val monitor = randomADMonitor(inputs = listOf(adSearchInput(detectorId)), triggers = listOf(adMonitorTrigger()),
690+
user = User(user.name, listOf(), user.roles, user.customAttNames))
691+
val response = executeMonitor(monitor, params = DRYRUN_MONITOR)
692+
val output = entityAsMap(response)
693+
@Suppress("UNCHECKED_CAST")
694+
(output["trigger_results"] as HashMap<String, Any>).forEach {
695+
_, v -> assertTrue((v as HashMap<String, Boolean>)["triggered"] as Boolean)
696+
}
697+
assertEquals(monitor.name, output["monitor_name"])
698+
@Suppress("UNCHECKED_CAST")
699+
val searchResult = (output.objectMap("input_results")["results"] as List<Map<String, Any>>).first()
700+
@Suppress("UNCHECKED_CAST")
701+
val total = searchResult.stringMap("hits")?.get("total") as Map<String, String>
702+
assertEquals("Incorrect search result", 1, total["value"])
703+
@Suppress("UNCHECKED_CAST")
704+
val maxAnomalyGrade = searchResult.stringMap("aggregations")?.get("max_anomaly_grade") as Map<String, String>
705+
assertEquals("Incorrect search result", 0.9, maxAnomalyGrade["value"])
691706
}
692-
assertEquals(monitor.name, output["monitor_name"])
693-
@Suppress("UNCHECKED_CAST")
694-
val searchResult = (output.objectMap("input_results")["results"] as List<Map<String, Any>>).first()
695-
@Suppress("UNCHECKED_CAST")
696-
val total = searchResult.stringMap("hits")?.get("total") as Map<String, String>
697-
assertEquals("Incorrect search result", 1, total["value"])
698-
@Suppress("UNCHECKED_CAST")
699-
val maxAnomalyGrade = searchResult.stringMap("aggregations")?.get("max_anomaly_grade") as Map<String, String>
700-
assertEquals("Incorrect search result", 0.9, maxAnomalyGrade["value"])
701707
}
702708

703709
fun `test execute AD monitor returns search result with same backend role`() {
704-
val detectorId = randomAlphaOfLength(5)
705-
val user = randomUser()
706-
prepareTestAnomalyResult(detectorId, user)
707-
// Test monitor with same user
708-
val monitor = randomADMonitor(inputs = listOf(adSearchInput(detectorId)), triggers = listOf(adMonitorTrigger()), user = user)
709-
val response = executeMonitor(monitor, params = DRYRUN_MONITOR)
710-
val output = entityAsMap(response)
711-
@Suppress("UNCHECKED_CAST")
712-
(output["trigger_results"] as HashMap<String, Any>).forEach {
713-
_, v -> assertTrue((v as HashMap<String, Boolean>)["triggered"] as Boolean)
710+
// TODO: change to REST API call to test security enabled case
711+
if (!securityEnabled()) {
712+
val detectorId = randomAlphaOfLength(5)
713+
val user = randomUser()
714+
prepareTestAnomalyResult(detectorId, user)
715+
// Test monitor with same user
716+
val monitor = randomADMonitor(inputs = listOf(adSearchInput(detectorId)), triggers = listOf(adMonitorTrigger()), user = user)
717+
val response = executeMonitor(monitor, params = DRYRUN_MONITOR)
718+
val output = entityAsMap(response)
719+
@Suppress("UNCHECKED_CAST")
720+
(output["trigger_results"] as HashMap<String, Any>).forEach {
721+
_, v -> assertTrue((v as HashMap<String, Boolean>)["triggered"] as Boolean)
722+
}
723+
@Suppress("UNCHECKED_CAST")
724+
val searchResult = (output.objectMap("input_results")["results"] as List<Map<String, Any>>).first()
725+
@Suppress("UNCHECKED_CAST")
726+
val total = searchResult.stringMap("hits")?.get("total") as Map<String, String>
727+
assertEquals("Incorrect search result", 3, total["value"])
728+
@Suppress("UNCHECKED_CAST")
729+
val maxAnomalyGrade = searchResult.stringMap("aggregations")?.get("max_anomaly_grade") as Map<String, String>
730+
assertEquals("Incorrect search result", 0.8, maxAnomalyGrade["value"])
714731
}
715-
@Suppress("UNCHECKED_CAST")
716-
val searchResult = (output.objectMap("input_results")["results"] as List<Map<String, Any>>).first()
717-
@Suppress("UNCHECKED_CAST")
718-
val total = searchResult.stringMap("hits")?.get("total") as Map<String, String>
719-
assertEquals("Incorrect search result", 3, total["value"])
720-
@Suppress("UNCHECKED_CAST")
721-
val maxAnomalyGrade = searchResult.stringMap("aggregations")?.get("max_anomaly_grade") as Map<String, String>
722-
assertEquals("Incorrect search result", 0.8, maxAnomalyGrade["value"])
723732
}
724733

725734
fun `test execute AD monitor returns no search result with different backend role`() {
726-
val detectorId = randomAlphaOfLength(5)
727-
val user = randomUser()
728-
prepareTestAnomalyResult(detectorId, user)
729-
// Test monitor with different user
730-
val monitor = randomADMonitor(inputs = listOf(adSearchInput(detectorId)),
731-
triggers = listOf(adMonitorTrigger()), user = randomUser())
732-
val response = executeMonitor(monitor, params = DRYRUN_MONITOR)
733-
val output = entityAsMap(response)
734-
@Suppress("UNCHECKED_CAST")
735-
(output["trigger_results"] as HashMap<String, Any>).forEach {
736-
_, v -> assertFalse((v as HashMap<String, Boolean>)["triggered"] as Boolean)
735+
// TODO: change to REST API call to test security enabled case
736+
if (!securityEnabled()) {
737+
val detectorId = randomAlphaOfLength(5)
738+
val user = randomUser()
739+
prepareTestAnomalyResult(detectorId, user)
740+
// Test monitor with different user
741+
val monitor = randomADMonitor(inputs = listOf(adSearchInput(detectorId)),
742+
triggers = listOf(adMonitorTrigger()), user = randomUser())
743+
val response = executeMonitor(monitor, params = DRYRUN_MONITOR)
744+
val output = entityAsMap(response)
745+
@Suppress("UNCHECKED_CAST")
746+
(output["trigger_results"] as HashMap<String, Any>).forEach {
747+
_, v -> assertFalse((v as HashMap<String, Boolean>)["triggered"] as Boolean)
748+
}
749+
@Suppress("UNCHECKED_CAST")
750+
val searchResult = (output.objectMap("input_results")["results"] as List<Map<String, Any>>).first()
751+
@Suppress("UNCHECKED_CAST")
752+
val total = searchResult.stringMap("hits")?.get("total") as Map<String, String>
753+
assertEquals("Incorrect search result", 0, total["value"])
737754
}
738-
@Suppress("UNCHECKED_CAST")
739-
val searchResult = (output.objectMap("input_results")["results"] as List<Map<String, Any>>).first()
740-
@Suppress("UNCHECKED_CAST")
741-
val total = searchResult.stringMap("hits")?.get("total") as Map<String, String>
742-
assertEquals("Incorrect search result", 0, total["value"])
743755
}
744756

745757
private fun prepareTestAnomalyResult(detectorId: String, user: User) {

alerting/src/test/kotlin/com/amazon/opendistroforelasticsearch/alerting/ODFERestTestCase.kt

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ import com.amazon.opendistroforelasticsearch.commons.ConfigConstants.OPENDISTRO_
2323
import com.amazon.opendistroforelasticsearch.commons.rest.SecureRestClientBuilder
2424
import org.apache.http.HttpHost
2525
import org.elasticsearch.client.Request
26+
import org.elasticsearch.client.RequestOptions
2627
import org.elasticsearch.client.RestClient
28+
import org.elasticsearch.client.WarningsHandler
2729
import org.elasticsearch.common.io.PathUtils
2830
import org.elasticsearch.common.settings.Settings
2931
import org.elasticsearch.common.xcontent.DeprecationHandler
@@ -79,9 +81,15 @@ abstract class ODFERestTestCase : ESRestTestCase() {
7981
val indexName: String = jsonObject["index"] as String
8082
// .opendistro_security isn't allowed to delete from cluster
8183
if (".opendistro_security" != indexName) {
82-
adminClient().performRequest(Request("DELETE", "/$indexName"))
84+
var request = Request("DELETE", "/$indexName")
85+
// TODO: remove PERMISSIVE option after moving system index access to REST API call
86+
val options = RequestOptions.DEFAULT.toBuilder()
87+
options.setWarningsHandler(WarningsHandler.PERMISSIVE)
88+
request.options = options.build()
89+
adminClient().performRequest(request)
8390
}
84-
} }
91+
}
92+
}
8593
}
8694

8795
/**

alerting/src/test/kotlin/com/amazon/opendistroforelasticsearch/alerting/TestHelpers.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import org.elasticsearch.client.Request
3939
import org.elasticsearch.client.RequestOptions
4040
import org.elasticsearch.client.Response
4141
import org.elasticsearch.client.RestClient
42+
import org.elasticsearch.client.WarningsHandler
4243
import org.elasticsearch.common.UUIDs
4344
import org.elasticsearch.common.settings.SecureString
4445
import org.elasticsearch.common.settings.Settings
@@ -252,7 +253,9 @@ fun RestClient.makeRequest(
252253
vararg headers: Header
253254
): Response {
254255
val request = Request(method, endpoint)
256+
// TODO: remove PERMISSIVE option after moving system index access to REST API call
255257
val options = RequestOptions.DEFAULT.toBuilder()
258+
options.setWarningsHandler(WarningsHandler.PERMISSIVE)
256259
headers.forEach { options.addHeader(it.name, it.value) }
257260
request.options = options.build()
258261
params.forEach { request.addParameter(it.key, it.value) }
@@ -276,6 +279,8 @@ fun RestClient.makeRequest(
276279
): Response {
277280
val request = Request(method, endpoint)
278281
val options = RequestOptions.DEFAULT.toBuilder()
282+
// TODO: remove PERMISSIVE option after moving system index access to REST API call
283+
options.setWarningsHandler(WarningsHandler.PERMISSIVE)
279284
headers.forEach { options.addHeader(it.name, it.value) }
280285
request.options = options.build()
281286
if (entity != null) {

alerting/src/test/kotlin/com/amazon/opendistroforelasticsearch/alerting/resthandler/MonitorRestApiIT.kt

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -187,17 +187,20 @@ class MonitorRestApiIT : AlertingRestTestCase() {
187187
}
188188

189189
fun `test creating an AD monitor with no detector has monitor backend role`() {
190-
createAnomalyDetectorIndex()
191-
indexDoc(ANOMALY_DETECTOR_INDEX, "1", randomAnomalyDetector())
192-
indexDoc(ANOMALY_DETECTOR_INDEX, "2", randomAnomalyDetectorWithUser(randomAlphaOfLength(5)))
193-
try {
194-
val monitor = randomADMonitor()
195-
client().makeRequest("POST", ALERTING_BASE_URI, emptyMap(), monitor.toHttpEntity())
196-
} catch (e: ResponseException) {
197-
// When user create AD monitor with no detector has backend role, will throw exception
198-
assertTrue("Unexpected error", e.message!!.contains("User has no available detectors"))
199-
assertTrue("Unexpected status",
200-
listOf<RestStatus>(RestStatus.NOT_FOUND).contains(e.response.restStatus()))
190+
if (!securityEnabled()) {
191+
createAnomalyDetectorIndex()
192+
// TODO: change to REST API call to test security enabled case
193+
indexDoc(ANOMALY_DETECTOR_INDEX, "1", randomAnomalyDetector())
194+
indexDoc(ANOMALY_DETECTOR_INDEX, "2", randomAnomalyDetectorWithUser(randomAlphaOfLength(5)))
195+
try {
196+
val monitor = randomADMonitor()
197+
client().makeRequest("POST", ALERTING_BASE_URI, emptyMap(), monitor.toHttpEntity())
198+
} catch (e: ResponseException) {
199+
// When user create AD monitor with no detector has backend role, will throw exception
200+
assertTrue("Unexpected error", e.message!!.contains("User has no available detectors"))
201+
assertTrue("Unexpected status",
202+
listOf<RestStatus>(RestStatus.NOT_FOUND).contains(e.response.restStatus()))
203+
}
201204
}
202205
}
203206

0 commit comments

Comments
 (0)