-
Notifications
You must be signed in to change notification settings - Fork 75
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
Alerts in correlations [Experminental] #1040
Merged
riysaxen-amzn
merged 14 commits into
opensearch-project:main
from
riysaxen-amzn:alertsInCorrelations
Jun 11, 2024
Merged
Changes from 11 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
da73e7e
notification for alerting in correlation
riysaxen-amzn 439e0c0
correlation alerts mapping change
riysaxen-amzn e2530f5
Merge branch 'main' into alertsInCorrelations
riysaxen-amzn a890e73
Merge branch 'main' into alertsInCorrelations
riysaxen-amzn 425831d
working code
riysaxen-amzn 7e0b257
alertsInCorrelation without notifciations
riysaxen-amzn c244a99
alertsInCorrelation without notifciations
riysaxen-amzn 8603d5a
alertsInCorrelation without notifciations
riysaxen-amzn 29ba163
alerts in correlations notification service added
riysaxen-amzn 2a4cf26
addressing the comments
riysaxen-amzn e23e969
addressing the comments
riysaxen-amzn 1547393
address the design changes discussed
riysaxen-amzn abade3f
address the design changes discussed
riysaxen-amzn 5ca1899
fixed tests
riysaxen-amzn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,7 @@ | |
import org.opensearch.common.xcontent.XContentType; | ||
import org.opensearch.commons.alerting.action.PublishFindingsRequest; | ||
import org.opensearch.commons.alerting.model.Finding; | ||
import org.opensearch.commons.authuser.User; | ||
import org.opensearch.core.xcontent.NamedXContentRegistry; | ||
import org.opensearch.core.xcontent.XContentParser; | ||
import org.opensearch.index.query.BoolQueryBuilder; | ||
|
@@ -32,9 +33,13 @@ | |
import org.opensearch.search.SearchHit; | ||
import org.opensearch.search.builder.SearchSourceBuilder; | ||
import org.opensearch.securityanalytics.config.monitors.DetectorMonitorConfig; | ||
import org.opensearch.securityanalytics.correlation.alert.CorrelationAlertService; | ||
import org.opensearch.securityanalytics.correlation.alert.CorrelationRuleScheduler; | ||
import org.opensearch.securityanalytics.correlation.alert.notifications.NotificationService; | ||
import org.opensearch.securityanalytics.logtype.LogTypeService; | ||
import org.opensearch.securityanalytics.model.CorrelationQuery; | ||
import org.opensearch.securityanalytics.model.CorrelationRule; | ||
import org.opensearch.securityanalytics.model.CorrelationRuleTrigger; | ||
import org.opensearch.securityanalytics.model.Detector; | ||
import org.opensearch.securityanalytics.transport.TransportCorrelateFindingAction; | ||
import org.opensearch.securityanalytics.util.AutoCorrelationsRepo; | ||
|
@@ -68,18 +73,30 @@ public class JoinEngine { | |
|
||
private final LogTypeService logTypeService; | ||
|
||
private final CorrelationAlertService correlationAlertService; | ||
|
||
private final NotificationService notificationService; | ||
|
||
private volatile TimeValue indexTimeout; | ||
|
||
private static final Logger log = LogManager.getLogger(JoinEngine.class); | ||
|
||
private final User user; | ||
|
||
public JoinEngine(Client client, PublishFindingsRequest request, NamedXContentRegistry xContentRegistry, | ||
long corrTimeWindow, TransportCorrelateFindingAction.AsyncCorrelateFindingAction correlateFindingAction, | ||
LogTypeService logTypeService, boolean enableAutoCorrelations) { | ||
long corrTimeWindow, TimeValue indexTimeout, TransportCorrelateFindingAction.AsyncCorrelateFindingAction correlateFindingAction, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. where is index timeout being set? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In JoinEngine
|
||
LogTypeService logTypeService, boolean enableAutoCorrelations, CorrelationAlertService correlationAlertService, NotificationService notificationService, User user) { | ||
this.client = client; | ||
this.request = request; | ||
this.xContentRegistry = xContentRegistry; | ||
this.corrTimeWindow = corrTimeWindow; | ||
this.indexTimeout = indexTimeout; | ||
this.correlateFindingAction = correlateFindingAction; | ||
this.logTypeService = logTypeService; | ||
this.enableAutoCorrelations = enableAutoCorrelations; | ||
this.correlationAlertService = correlationAlertService; | ||
this.notificationService = notificationService; | ||
this.user = user; | ||
} | ||
|
||
public void onSearchDetectorResponse(Detector detector, Finding finding) { | ||
|
@@ -349,7 +366,7 @@ private void getValidDocuments(String detectorType, List<String> indices, List<C | |
} | ||
} | ||
searchFindingsByTimestamp(detectorType, categoryToQueriesMap, categoryToTimeWindowMap, | ||
filteredCorrelationRules.stream().map(it -> it.correlationRule).map(CorrelationRule::getId).collect(Collectors.toList()), | ||
filteredCorrelationRules.stream().map(it -> it.correlationRule).collect(Collectors.toList()), | ||
autoCorrelations | ||
); | ||
}, this::onFailure)); | ||
|
@@ -362,7 +379,7 @@ private void getValidDocuments(String detectorType, List<String> indices, List<C | |
* this method searches for parent findings given the log category & correlation time window & collects all related docs | ||
* for them. | ||
*/ | ||
private void searchFindingsByTimestamp(String detectorType, Map<String, List<CorrelationQuery>> categoryToQueriesMap, Map<String, Long> categoryToTimeWindowMap, List<String> correlationRules, Map<String, List<String>> autoCorrelations) { | ||
private void searchFindingsByTimestamp(String detectorType, Map<String, List<CorrelationQuery>> categoryToQueriesMap, Map<String, Long> categoryToTimeWindowMap, List<CorrelationRule> correlationRules, Map<String, List<String>> autoCorrelations) { | ||
long findingTimestamp = request.getFinding().getTimestamp().toEpochMilli(); | ||
MultiSearchRequest mSearchRequest = new MultiSearchRequest(); | ||
List<Pair<String, List<CorrelationQuery>>> categoryToQueriesPairs = new ArrayList<>(); | ||
|
@@ -418,14 +435,14 @@ private void searchFindingsByTimestamp(String detectorType, Map<String, List<Cor | |
searchDocsWithFilterKeys(detectorType, relatedDocsMap, categoryToTimeWindowMap, correlationRules, autoCorrelations); | ||
}, this::onFailure)); | ||
} else { | ||
getTimestampFeature(detectorType, correlationRules, autoCorrelations); | ||
getTimestampFeature(detectorType, correlationRules.stream().map(CorrelationRule::getId).collect(Collectors.toList()) , autoCorrelations); | ||
} | ||
} | ||
|
||
/** | ||
* Given the related docs from parent findings, this method filters only those related docs which match parent join criteria. | ||
*/ | ||
private void searchDocsWithFilterKeys(String detectorType, Map<String, DocSearchCriteria> relatedDocsMap, Map<String, Long> categoryToTimeWindowMap, List<String> correlationRules, Map<String, List<String>> autoCorrelations) { | ||
private void searchDocsWithFilterKeys(String detectorType, Map<String, DocSearchCriteria> relatedDocsMap, Map<String, Long> categoryToTimeWindowMap, List<CorrelationRule> correlationRules, Map<String, List<String>> autoCorrelations) { | ||
MultiSearchRequest mSearchRequest = new MultiSearchRequest(); | ||
List<String> categories = new ArrayList<>(); | ||
|
||
|
@@ -476,15 +493,15 @@ private void searchDocsWithFilterKeys(String detectorType, Map<String, DocSearch | |
getCorrelatedFindings(detectorType, filteredRelatedDocIds, categoryToTimeWindowMap, correlationRules, autoCorrelations); | ||
}, this::onFailure)); | ||
} else { | ||
getTimestampFeature(detectorType, correlationRules, autoCorrelations); | ||
getTimestampFeature(detectorType, correlationRules.stream().map(CorrelationRule::getId).collect(Collectors.toList()), autoCorrelations); | ||
} | ||
} | ||
|
||
/** | ||
* Given the filtered related docs of the parent findings, this method gets the actual filtered parent findings for | ||
* the finding to be correlated. | ||
*/ | ||
private void getCorrelatedFindings(String detectorType, Map<String, List<String>> filteredRelatedDocIds, Map<String, Long> categoryToTimeWindowMap, List<String> correlationRules, Map<String, List<String>> autoCorrelations) { | ||
private void getCorrelatedFindings(String detectorType, Map<String, List<String>> filteredRelatedDocIds, Map<String, Long> categoryToTimeWindowMap, List<CorrelationRule> correlationRules, Map<String, List<String>> autoCorrelations) { | ||
long findingTimestamp = request.getFinding().getTimestamp().toEpochMilli(); | ||
MultiSearchRequest mSearchRequest = new MultiSearchRequest(); | ||
List<String> categories = new ArrayList<>(); | ||
|
@@ -540,6 +557,12 @@ private void getCorrelatedFindings(String detectorType, Map<String, List<String> | |
++idx; | ||
} | ||
|
||
if (!correlatedFindings.isEmpty()) { | ||
CorrelationRuleScheduler correlationRuleScheduler = new CorrelationRuleScheduler(client, correlationAlertService, notificationService); | ||
correlationRuleScheduler.schedule(correlationRules, correlatedFindings, request.getFinding().getId(), indexTimeout, user); | ||
correlationRuleScheduler.shutdown(); | ||
} | ||
|
||
for (Map.Entry<String, List<String>> autoCorrelation: autoCorrelations.entrySet()) { | ||
if (correlatedFindings.containsKey(autoCorrelation.getKey())) { | ||
Set<String> alreadyCorrelatedFindings = new HashSet<>(correlatedFindings.get(autoCorrelation.getKey())); | ||
|
@@ -549,10 +572,10 @@ private void getCorrelatedFindings(String detectorType, Map<String, List<String> | |
correlatedFindings.put(autoCorrelation.getKey(), autoCorrelation.getValue()); | ||
} | ||
} | ||
correlateFindingAction.initCorrelationIndex(detectorType, correlatedFindings, correlationRules); | ||
correlateFindingAction.initCorrelationIndex(detectorType, correlatedFindings, correlationRules.stream().map(CorrelationRule::getId).collect(Collectors.toList())); | ||
}, this::onFailure)); | ||
} else { | ||
getTimestampFeature(detectorType, correlationRules, autoCorrelations); | ||
getTimestampFeature(detectorType, correlationRules.stream().map(CorrelationRule::getId).collect(Collectors.toList()), autoCorrelations); | ||
} | ||
} | ||
|
||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove * imports.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updated