Skip to content

Commit 2292d86

Browse files
integrate security-analytics & alerting for correlation engine (#878) (#881)
Signed-off-by: Subhobrata Dey <[email protected]>
1 parent 7998830 commit 2292d86

File tree

3 files changed

+42
-3
lines changed

3 files changed

+42
-3
lines changed

alerting/src/main/kotlin/org/opensearch/alerting/DocumentLevelMonitorRunner.kt

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ package org.opensearch.alerting
88
import org.apache.logging.log4j.LogManager
99
import org.opensearch.ExceptionsHelper
1010
import org.opensearch.OpenSearchStatusException
11+
import org.opensearch.action.ActionListener
1112
import org.opensearch.action.index.IndexRequest
1213
import org.opensearch.action.index.IndexResponse
1314
import org.opensearch.action.search.SearchAction
@@ -26,12 +27,16 @@ import org.opensearch.alerting.util.IndexUtils
2627
import org.opensearch.alerting.util.defaultToPerExecutionAction
2728
import org.opensearch.alerting.util.getActionExecutionPolicy
2829
import org.opensearch.client.Client
30+
import org.opensearch.client.node.NodeClient
2931
import org.opensearch.cluster.metadata.IndexMetadata
3032
import org.opensearch.cluster.routing.ShardRouting
3133
import org.opensearch.cluster.service.ClusterService
3234
import org.opensearch.common.bytes.BytesReference
3335
import org.opensearch.common.xcontent.XContentFactory
3436
import org.opensearch.common.xcontent.XContentType
37+
import org.opensearch.commons.alerting.AlertingPluginInterface
38+
import org.opensearch.commons.alerting.action.PublishFindingsRequest
39+
import org.opensearch.commons.alerting.action.SubscribeFindingsResponse
3540
import org.opensearch.commons.alerting.model.ActionExecutionResult
3641
import org.opensearch.commons.alerting.model.Alert
3742
import org.opensearch.commons.alerting.model.DocLevelMonitorInput
@@ -342,6 +347,7 @@ object DocumentLevelMonitorRunner : MonitorRunner() {
342347
val finding = Finding(
343348
id = UUID.randomUUID().toString(),
344349
relatedDocIds = listOf(docIndex[0]),
350+
correlatedDocIds = listOf(docIndex[0]),
345351
monitorId = monitor.id,
346352
monitorName = monitor.name,
347353
index = docIndex[1],
@@ -363,9 +369,33 @@ object DocumentLevelMonitorRunner : MonitorRunner() {
363369
monitorCtx.client!!.index(indexRequest, it)
364370
}
365371
}
372+
373+
try {
374+
publishFinding(monitor, monitorCtx, finding)
375+
} catch (e: Exception) {
376+
// suppress exception
377+
logger.error("Optional finding callback failed", e)
378+
}
366379
return finding.id
367380
}
368381

382+
private fun publishFinding(
383+
monitor: Monitor,
384+
monitorCtx: MonitorRunnerExecutionContext,
385+
finding: Finding
386+
) {
387+
val publishFindingsRequest = PublishFindingsRequest(monitor.id, finding)
388+
AlertingPluginInterface.publishFinding(
389+
monitorCtx.client!! as NodeClient,
390+
publishFindingsRequest,
391+
object : ActionListener<SubscribeFindingsResponse> {
392+
override fun onResponse(response: SubscribeFindingsResponse) {}
393+
394+
override fun onFailure(e: Exception) {}
395+
}
396+
)
397+
}
398+
369399
private suspend fun updateLastRunContext(
370400
lastRunContext: Map<String, Any>,
371401
monitorCtx: MonitorRunnerExecutionContext,

alerting/src/main/resources/org/opensearch/alerting/alerts/finding_mapping.json

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"dynamic": "strict",
33
"_meta" : {
4-
"schema_version": 1
4+
"schema_version": 2
55
},
66
"properties": {
77
"schema_version": {
@@ -51,6 +51,15 @@
5151
},
5252
"timestamp": {
5353
"type": "long"
54+
},
55+
"correlated_doc_ids": {
56+
"type" : "text",
57+
"analyzer": "whitespace",
58+
"fields" : {
59+
"keyword" : {
60+
"type" : "keyword"
61+
}
62+
}
5463
}
5564
}
5665
}

alerting/src/test/kotlin/org/opensearch/alerting/alerts/AlertIndicesIT.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ class AlertIndicesIT : AlertingRestTestCase() {
7575

7676
putFindingMappings(
7777
AlertIndices.findingMapping().trimStart('{').trimEnd('}')
78-
.replace("\"schema_version\": 1", "\"schema_version\": 0")
78+
.replace("\"schema_version\": 2", "\"schema_version\": 0")
7979
)
8080
assertIndexExists(AlertIndices.FINDING_HISTORY_WRITE_INDEX)
8181
verifyIndexSchemaVersion(AlertIndices.FINDING_HISTORY_WRITE_INDEX, 0)
@@ -89,7 +89,7 @@ class AlertIndicesIT : AlertingRestTestCase() {
8989
executeMonitor(trueMonitor.id)
9090
assertIndexExists(AlertIndices.FINDING_HISTORY_WRITE_INDEX)
9191
verifyIndexSchemaVersion(ScheduledJob.SCHEDULED_JOBS_INDEX, 6)
92-
verifyIndexSchemaVersion(AlertIndices.FINDING_HISTORY_WRITE_INDEX, 1)
92+
verifyIndexSchemaVersion(AlertIndices.FINDING_HISTORY_WRITE_INDEX, 2)
9393
}
9494

9595
fun `test alert index gets recreated automatically if deleted`() {

0 commit comments

Comments
 (0)