-
Notifications
You must be signed in to change notification settings - Fork 77
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 interfaces and models for rule engine #990
Open
engechas
wants to merge
5
commits into
opensearch-project:main
Choose a base branch
from
engechas:rule-engine
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
59b6a4e
Add models and interfaces for rule engine
engechas 9dbc8ca
Add javadoc for interfaces
engechas 149c7e8
Add license headers
engechas 9303562
Move rule engine to its own gradle project
engechas 730d563
Fix directory structure
engechas 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
apply plugin: 'java' | ||
apply plugin: 'jacoco' | ||
|
||
repositories { | ||
mavenLocal() | ||
mavenCentral() | ||
maven { url "https://aws.oss.sonatype.org/content/repositories/snapshots" } | ||
} | ||
|
||
dependencies { | ||
implementation rootProject | ||
implementation "com.github.seancfoley:ipaddress:5.4.1" | ||
} |
8 changes: 8 additions & 0 deletions
8
rule-engine/src/main/java/org/opensearch/securityanalytics/ruleengine/RuleEngine.java
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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
package org.opensearch.securityanalytics.ruleengine; | ||
|
||
public class RuleEngine { | ||
} |
19 changes: 19 additions & 0 deletions
19
...ne/src/main/java/org/opensearch/securityanalytics/ruleengine/evaluator/RuleEvaluator.java
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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
package org.opensearch.securityanalytics.ruleengine.evaluator; | ||
|
||
import org.opensearch.securityanalytics.ruleengine.model.Match; | ||
|
||
import java.util.List; | ||
|
||
public interface RuleEvaluator<T> { | ||
/** | ||
* A method to evaluate the rules against a set of incoming data. | ||
* | ||
* @param data - the data to be evaluated against the rules | ||
* @return - A list of Matches for positive rule evaluations against the incoming data | ||
*/ | ||
List<Match> evaluate(List<T> data); | ||
} |
17 changes: 17 additions & 0 deletions
17
...in/java/org/opensearch/securityanalytics/ruleengine/evaluator/StatelessRuleEvaluator.java
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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
package org.opensearch.securityanalytics.ruleengine.evaluator; | ||
|
||
import org.opensearch.securityanalytics.ruleengine.model.DataType; | ||
import org.opensearch.securityanalytics.ruleengine.model.Match; | ||
|
||
import java.util.List; | ||
|
||
public class StatelessRuleEvaluator implements RuleEvaluator<DataType> { | ||
@Override | ||
public List<Match> evaluate(final List<DataType> data) { | ||
return null; | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
rule-engine/src/main/java/org/opensearch/securityanalytics/ruleengine/model/DataType.java
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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
package org.opensearch.securityanalytics.ruleengine.model; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public abstract class DataType { | ||
private final Map<String, String> dataTypeMetadata; | ||
|
||
public DataType() { | ||
this.dataTypeMetadata = new HashMap<>(); | ||
} | ||
|
||
public abstract Object getValue(String fieldName); | ||
public abstract String getTimeFieldName(); | ||
|
||
public void putDataTypeMetadata(final String key, final String value) { | ||
dataTypeMetadata.put(key, value); | ||
} | ||
|
||
public Map<String, String> getDataTypeMetadata() { | ||
return dataTypeMetadata; | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
rule-engine/src/main/java/org/opensearch/securityanalytics/ruleengine/model/Match.java
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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
package org.opensearch.securityanalytics.ruleengine.model; | ||
|
||
import org.opensearch.securityanalytics.ruleengine.rules.Rule; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class Match { | ||
private final DataType datum; | ||
private final List<Rule> rules; | ||
|
||
public Match(final DataType datum) { | ||
this.datum = datum; | ||
this.rules = new ArrayList<>(); | ||
} | ||
|
||
public void addRule(final Rule rule) { | ||
rules.add(rule); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
rule-engine/src/main/java/org/opensearch/securityanalytics/ruleengine/parser/RuleParser.java
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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
package org.opensearch.securityanalytics.ruleengine.parser; | ||
|
||
import org.opensearch.securityanalytics.ruleengine.provider.RuleData; | ||
import org.opensearch.securityanalytics.ruleengine.rules.ParsedRules; | ||
|
||
public interface RuleParser { | ||
/** | ||
* A method to parse the information of a RuleData object into the internal representation of a rule used for evaluation. | ||
* | ||
* @param ruleData - the information representing one or more rules to be parsed | ||
* @return - A ParsedRules object containing the internal representation of the rules that were parsed | ||
*/ | ||
ParsedRules parseRules(RuleData ruleData); | ||
} |
34 changes: 34 additions & 0 deletions
34
rule-engine/src/main/java/org/opensearch/securityanalytics/ruleengine/provider/RuleData.java
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 |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
package org.opensearch.securityanalytics.ruleengine.provider; | ||
|
||
import org.opensearch.securityanalytics.ruleengine.model.DataType; | ||
|
||
import java.util.Map; | ||
import java.util.function.Predicate; | ||
|
||
public class RuleData { | ||
private final String ruleAsString; | ||
private final Predicate<DataType> evaluationCondition; | ||
private final Map<String, String> metadata; | ||
|
||
public RuleData(final String ruleAsString, final Predicate<DataType> evaluationCondition, final Map<String, String> metadata) { | ||
this.ruleAsString = ruleAsString; | ||
this.evaluationCondition = evaluationCondition; | ||
this.metadata = metadata; | ||
} | ||
|
||
public String getRuleAsString() { | ||
return ruleAsString; | ||
} | ||
|
||
public Predicate<DataType> getEvaluationCondition() { | ||
return evaluationCondition; | ||
} | ||
|
||
public Map<String, String> getMetadata() { | ||
return metadata; | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
...gine/src/main/java/org/opensearch/securityanalytics/ruleengine/provider/RuleProvider.java
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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
package org.opensearch.securityanalytics.ruleengine.provider; | ||
|
||
import java.util.List; | ||
|
||
public interface RuleProvider { | ||
/** | ||
* A method to fetch RuleData from an external source | ||
* | ||
* @return - A list of RuleData used to parse the rules into the internal representation used for evaluation | ||
*/ | ||
List<RuleData> getRuleData(); | ||
} |
25 changes: 25 additions & 0 deletions
25
rule-engine/src/main/java/org/opensearch/securityanalytics/ruleengine/rules/ParsedRules.java
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 |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
package org.opensearch.securityanalytics.ruleengine.rules; | ||
|
||
import java.util.List; | ||
|
||
public class ParsedRules { | ||
private final List<StatelessRule> statelessRules; | ||
private final List<StatefulRule> statefulRules; | ||
|
||
public ParsedRules(final List<StatelessRule> statelessRules, final List<StatefulRule> statefulRules) { | ||
this.statelessRules = statelessRules; | ||
this.statefulRules = statefulRules; | ||
} | ||
|
||
public List<StatelessRule> getStatelessRules() { | ||
return statelessRules; | ||
} | ||
|
||
public List<StatefulRule> getStatefulRules() { | ||
return statefulRules; | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
rule-engine/src/main/java/org/opensearch/securityanalytics/ruleengine/rules/Rule.java
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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
package org.opensearch.securityanalytics.ruleengine.rules; | ||
|
||
import java.util.function.Predicate; | ||
|
||
public abstract class Rule<T, U> { | ||
private final String id; | ||
private final Predicate<T> evaluationCondition; | ||
private final Predicate<U> ruleCondition; | ||
|
||
public Rule(final String id, final Predicate<T> evaluationCondition, final Predicate<U> ruleCondition) { | ||
this.id = id; | ||
this.evaluationCondition = evaluationCondition; | ||
this.ruleCondition = ruleCondition; | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
...-engine/src/main/java/org/opensearch/securityanalytics/ruleengine/rules/StatefulRule.java
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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
package org.opensearch.securityanalytics.ruleengine.rules; | ||
|
||
import org.opensearch.securityanalytics.ruleengine.model.Match; | ||
|
||
import java.time.Duration; | ||
import java.util.List; | ||
import java.util.function.Predicate; | ||
|
||
public class StatefulRule extends Rule<Match, List<Match>> { | ||
private final Duration timeframe; | ||
private final List<String> filterFields; | ||
|
||
public StatefulRule(final String id, final Predicate<Match> evaluationCondition, | ||
final Predicate<List<Match>> ruleCondition, final Duration timeframe, | ||
final List<String> filterFields) { | ||
super(id, evaluationCondition, ruleCondition); | ||
this.timeframe = timeframe; | ||
this.filterFields = filterFields; | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
...engine/src/main/java/org/opensearch/securityanalytics/ruleengine/rules/StatelessRule.java
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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
package org.opensearch.securityanalytics.ruleengine.rules; | ||
|
||
import org.opensearch.securityanalytics.ruleengine.model.DataType; | ||
|
||
import java.util.function.Predicate; | ||
|
||
public class StatelessRule extends Rule<DataType, DataType> { | ||
private final boolean isStatefulCondition; | ||
|
||
public StatelessRule(final String id, final Predicate<DataType> evaluationCondition, | ||
final Predicate<DataType> ruleCondition, final boolean isStatefulCondition) { | ||
super(id, evaluationCondition, ruleCondition); | ||
this.isStatefulCondition = isStatefulCondition; | ||
} | ||
} |
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.
Is this a veritable dependency or can we use a dependency already used in core.
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.
This is needed for CIDR range matching which will be introduced in the next PR. Alerting takes a dependency on the same module (but Security Analytics doesn't): https://github.com/opensearch-project/alerting/blob/main/alerting/build.gradle#L116