Skip to content

Commit

Permalink
Change ruleId if it exists
Browse files Browse the repository at this point in the history
Signed-off-by: Ashish Agrawal <[email protected]>
  • Loading branch information
lezzago committed Oct 3, 2023
1 parent 3c9b23a commit abdb6a6
Showing 1 changed file with 10 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/
package org.opensearch.securityanalytics.util;

import java.util.HashSet;
import java.util.Set;

import com.google.common.collect.ImmutableMap;
Expand Down Expand Up @@ -70,6 +71,7 @@
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;
import java.util.stream.Collectors;
import java.util.stream.Stream;

Expand Down Expand Up @@ -272,10 +274,13 @@ private String getRuleCategory(Path folderPath) {
private void ingestQueries(Map<String, List<String>> logIndexToRules, WriteRequest.RefreshPolicy refreshPolicy, TimeValue indexTimeout, ActionListener<BulkResponse> listener) throws SigmaError, IOException {
List<Rule> queries = new ArrayList<>();

Set<String> ruleIds = new HashSet<>();

Check warning on line 277 in src/main/java/org/opensearch/securityanalytics/util/RuleIndices.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/securityanalytics/util/RuleIndices.java#L277

Added line #L277 was not covered by tests
for (Map.Entry<String, List<String>> logIndexToRule: logIndexToRules.entrySet()) {
Map<String, String> fieldMappings = logTypeService.getRuleFieldMappingsForBuiltinLogType(logIndexToRule.getKey());
final QueryBackend backend = new OSQueryBackend(fieldMappings, true, true);
queries.addAll(getQueries(backend, logIndexToRule.getKey(), logIndexToRule.getValue()));
List<Rule> rules = getQueries(backend, logIndexToRule.getKey(), logIndexToRule.getValue(), ruleIds);
rules.forEach(rule -> ruleIds.add(rule.getId()));
queries.addAll(rules);

Check warning on line 283 in src/main/java/org/opensearch/securityanalytics/util/RuleIndices.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/securityanalytics/util/RuleIndices.java#L281-L283

Added lines #L281 - L283 were not covered by tests
}
loadRules(queries, refreshPolicy, indexTimeout, listener, true);
}
Expand All @@ -285,16 +290,18 @@ private void loadQueries(String[] paths, WriteRequest.RefreshPolicy refreshPolic
loadQueries(path, refreshPolicy, indexTimeout, listener);
}

private List<Rule> getQueries(QueryBackend backend, String category, List<String> rules) throws SigmaError {
private List<Rule> getQueries(QueryBackend backend, String category, List<String> rules, Set<String> ruleIds) throws SigmaError {
List<Rule> queries = new ArrayList<>();
for (String ruleStr: rules) {
SigmaRule rule = SigmaRule.fromYaml(ruleStr, true);
backend.resetQueryFields();
List<Object> ruleQueries = backend.convertRule(rule);
Set<String> queryFieldNames = backend.getQueryFields().keySet();
String ruleId = ruleIds.contains(rule.getId().toString()) ?
UUID.randomUUID().toString() : rule.getId().toString();

Check warning on line 301 in src/main/java/org/opensearch/securityanalytics/util/RuleIndices.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/securityanalytics/util/RuleIndices.java#L301

Added line #L301 was not covered by tests

Rule ruleModel = new Rule(
rule.getId().toString(), NO_VERSION, rule, category,
ruleId, NO_VERSION, rule, category,
ruleQueries,
new ArrayList<>(queryFieldNames),
ruleStr
Expand Down

0 comments on commit abdb6a6

Please sign in to comment.