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 field based rules support in correlation engine #737

Merged
merged 2 commits into from
Dec 6, 2023

Conversation

sbcd90
Copy link
Collaborator

@sbcd90 sbcd90 commented Nov 28, 2023

Description

add field based correlation rules support in correlation engine.

e.g., sample correlation rule with group by field

POST /_plugins/_security_analytics/correlation/rules
{
  "correlate": [
    {
      "index": "windows5",
      "field": "SourceIp",
      "category": "windows"
    },
    {
      "index": "vpc_flow1",
      "category": "vpcflow",
      "field": "netflow.srcaddr"
    }
  ],
  "name": "vpcflow to windows",
  "time_window": 300000
}

sample correlation rule with group by field & filter query

{
  "correlate": [
    {
      "index": "cloudtrail-5",
      "query": "eventName:CreateUser",
      "field": "requestParameters.userName",
      "category": "cloudtrail"
    },
    {
      "index": "cloudtrail-5",
      "category": "cloudtrail",
      "query": "eventName:DeleteUser",
      "field": "requestParameters.userName"
    }
  ],
  "name": "cloudtrail create to delete account",
  "time_window": 600000
}

Issues Resolved

[List any issues this PR will resolve]

Check List

  • New functionality includes testing.
    • All tests pass
  • New functionality has been documented.
    • New functionality has javadoc added
  • Commits are signed per the DCO using --signoff

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
For more information on following Developer Certificate of Origin and signing off your commits, please check here.

Copy link

codecov bot commented Nov 28, 2023

Codecov Report

Attention: 80 lines in your changes are missing coverage. Please review.

Comparison is base (c0f7bd9) 24.73% compared to head (dec7296) 24.62%.

Files Patch % Lines
...arch/securityanalytics/correlation/JoinEngine.java 0.00% 52 Missing ⚠️
...arch/securityanalytics/model/CorrelationQuery.java 0.00% 15 Missing ⚠️
...earch/securityanalytics/model/CorrelationRule.java 0.00% 10 Missing ⚠️
...ics/transport/TransportCorrelateFindingAction.java 0.00% 3 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##               main     #737      +/-   ##
============================================
- Coverage     24.73%   24.62%   -0.12%     
+ Complexity     1021     1018       -3     
============================================
  Files           275      275              
  Lines         12662    12711      +49     
  Branches       1389     1399      +10     
============================================
- Hits           3132     3130       -2     
- Misses         9264     9314      +50     
- Partials        266      267       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@sbcd90 sbcd90 marked this pull request as draft November 29, 2023 16:34
if (enableAutoCorrelations) {
generateAutoCorrelations(detector, finding);
} else {
onAutoCorrelations(detector, finding, Map.of());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this method seems to be search on correlation index

if auto-correlation is disabled why are we invoking this method?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is the callback when auto correlations complete. so, if auto-correlations are disabled, we directly call the callback without generating auto correlations.

@sbcd90 sbcd90 force-pushed the test456 branch 2 times, most recently from 15d461a to 5d5d6dd Compare November 29, 2023 20:33
@sbcd90 sbcd90 marked this pull request as ready for review November 29, 2023 20:58
@sbcd90 sbcd90 requested a review from jowg-amazon as a code owner November 29, 2023 20:58
for (CorrelationRule rule: filteredCorrelationRules) {
List<CorrelationQuery> queries = rule.getCorrelationQueries();
Map<String, Long> categoryToTimeWindowMap = new HashMap<>();
for (Triple<CorrelationRule, SearchHit[], String> rule: filteredCorrelationRules) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: why are we not creating pojos. we have increased from list to triple?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed it.

@@ -138,6 +138,12 @@ public class SecurityAnalyticsSettings {
Setting.Property.NodeScope, Setting.Property.Dynamic
);

public static final Setting<Boolean> ENABLE_AUTO_CORRELATIONS = Setting.boolSetting(
"plugins.security_analytics.enable_auto_correlations",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rename this to plugins.security_analytics.auto_correlations_enabled

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed it.

@@ -138,6 +138,12 @@ public class SecurityAnalyticsSettings {
Setting.Property.NodeScope, Setting.Property.Dynamic
);

public static final Setting<Boolean> ENABLE_AUTO_CORRELATIONS = Setting.boolSetting(
"plugins.security_analytics.enable_auto_correlations",
false,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is auto correlations disabled by default?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

auto correlations are disabled because they may generate too many irrelevant correlations. we're trying to further filter down auto correlations so that meaningful results are shown.

@@ -259,6 +259,29 @@ public static String randomRule() {
"level: high";
}

public static String randomRuleForCorrelations(String value) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: rename to randomCloudtrailRuleForCorrelations

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed it.

}
}

public void testBasicCorrelationEngineWorkflowWithFieldBasedRulesOnMultipleLogTypes() throws IOException, InterruptedException {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we support correlation for custom log types?

if yes, can we add a tests for correlation on (custom log types + log types)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added an integ test for it.

Map<String, Object> responseMap = entityAsMap(response);
List<Object> results = (List<Object>) responseMap.get("findings");
if (results.size() == 1) {
Assert.assertTrue(true);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why are we not validating field values like finding id or field name etc.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we do not need to in this test as only 1 result is expected. 0 correlations mean error.

@eirsep
Copy link
Member

eirsep commented Nov 29, 2023

Plz elaborate PR description for better understanding of the feature.. typically a github issue with the elaborate description tagged with PR is best practice and good for understanding and tracking

Describe settings introduced and description of setting

Add javadocs for settings and critical sections of code
plz add details in PR description with example of correlation finding document and if there is any change after field based correlation.

@@ -264,7 +264,7 @@ public void testGetFindings_byDetectorType_success() throws IOException {
getFindingsBody = entityAsMap(getFindingsResponse);
Assert.assertEquals(1, getFindingsBody.get("total_findings"));
}
@Ignore
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why did we uncomment
is this test flakiness fixed?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@@ -49,7 +50,6 @@
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.function.Predicate;
import java.util.stream.Collectors;


Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is JoinEngine not a "component" injected from createComponents() in Plugin definition? We shouldn't have to parse a setting in transport action and pass it to correlation engine

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JoinEngine needs an instance of TransportCorrelateFindingAction. hence, it cannot be part of createComponents().

eirsep
eirsep previously approved these changes Nov 30, 2023
@@ -4,7 +4,8 @@
*/
package org.opensearch.securityanalytics.correlation;

import kotlin.Pair;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.commons.lang3.tuple.Triple;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this import used at all?

@@ -46,6 +46,9 @@
"type" : "keyword"
}
}
},
"fields": {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we support multiple fields?

@eirsep eirsep merged commit 01facfc into opensearch-project:main Dec 6, 2023
11 of 16 checks passed
@opensearch-trigger-bot
Copy link
Contributor

The backport to 2.11-with-features failed:

The process '/usr/bin/git' failed with exit code 1

To backport manually, run these commands in your terminal:

# Fetch latest updates from GitHub
git fetch
# Create a new working tree
git worktree add .worktrees/backport-2.11-with-features 2.11-with-features
# Navigate to the new working tree
cd .worktrees/backport-2.11-with-features
# Create a new branch
git switch --create backport/backport-737-to-2.11-with-features
# Cherry-pick the merged commit of this pull request and resolve the conflicts
git cherry-pick -x --mainline 1 01facfc90ad56f9c7d0a63025c980297b5d352dd
# Push it to GitHub
git push --set-upstream origin backport/backport-737-to-2.11-with-features
# Go back to the original working tree
cd ../..
# Delete the working tree
git worktree remove .worktrees/backport-2.11-with-features

Then, create a pull request where the base branch is 2.11-with-features and the compare/head branch is backport/backport-737-to-2.11-with-features.

@opensearch-trigger-bot
Copy link
Contributor

The backport to 2.x failed:

The process '/usr/bin/git' failed with exit code 1

To backport manually, run these commands in your terminal:

# Fetch latest updates from GitHub
git fetch
# Create a new working tree
git worktree add .worktrees/backport-2.x 2.x
# Navigate to the new working tree
cd .worktrees/backport-2.x
# Create a new branch
git switch --create backport/backport-737-to-2.x
# Cherry-pick the merged commit of this pull request and resolve the conflicts
git cherry-pick -x --mainline 1 01facfc90ad56f9c7d0a63025c980297b5d352dd
# Push it to GitHub
git push --set-upstream origin backport/backport-737-to-2.x
# Go back to the original working tree
cd ../..
# Delete the working tree
git worktree remove .worktrees/backport-2.x

Then, create a pull request where the base branch is 2.x and the compare/head branch is backport/backport-737-to-2.x.

jowg-amazon pushed a commit to jowg-amazon/security-analytics that referenced this pull request Dec 6, 2023
…ct#737)

* add field based rules support in correlation engine

Signed-off-by: Subhobrata Dey <[email protected]>

* add field based rules support in correlation engine

Signed-off-by: Subhobrata Dey <[email protected]>

---------

Signed-off-by: Subhobrata Dey <[email protected]>
jowg-amazon added a commit that referenced this pull request Dec 6, 2023
* add field based rules support in correlation engine

Signed-off-by: Subhobrata Dey <[email protected]>
Co-authored-by: Subhobrata Dey <[email protected]>
jowg-amazon pushed a commit to jowg-amazon/security-analytics that referenced this pull request Jan 18, 2024
…ct#737)

* add field based rules support in correlation engine

Signed-off-by: Subhobrata Dey <[email protected]>

* add field based rules support in correlation engine

Signed-off-by: Subhobrata Dey <[email protected]>

---------

Signed-off-by: Subhobrata Dey <[email protected]>
sbcd90 pushed a commit that referenced this pull request Feb 6, 2024
riysaxen-amzn pushed a commit to riysaxen-amzn/security-analytics that referenced this pull request Mar 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants