From 6fce807726567d0a9951a3b8b1206555d24aae5c Mon Sep 17 00:00:00 2001 From: ayushaga14 Date: Tue, 14 Jan 2025 15:16:54 +0530 Subject: [PATCH] save and parse agg rules --- .../tasks/MaliciousTrafficDetectorTask.java | 8 +++---- libs/dao/src/main/java/com/akto/DaoInit.java | 2 +- .../AggregationLayerParser.java | 3 +-- .../monitoring/FilterConfigYamlParser.java | 22 +++++++++++++++---- .../dto/api_protection_parse_layer/Rule.java | 5 +---- .../com/akto/dto/monitoring/FilterConfig.java | 13 ++++++++++- 6 files changed, 37 insertions(+), 16 deletions(-) diff --git a/apps/threat-detection/src/main/java/com/akto/threat/detection/tasks/MaliciousTrafficDetectorTask.java b/apps/threat-detection/src/main/java/com/akto/threat/detection/tasks/MaliciousTrafficDetectorTask.java index 3ba3603d50..37672bd831 100644 --- a/apps/threat-detection/src/main/java/com/akto/threat/detection/tasks/MaliciousTrafficDetectorTask.java +++ b/apps/threat-detection/src/main/java/com/akto/threat/detection/tasks/MaliciousTrafficDetectorTask.java @@ -186,10 +186,10 @@ private void processRecord(HttpResponseParam record) throws Exception { // we still push malicious requests to kafka // todo: modify fetch yaml and read aggregate rules from it - List rules = new ArrayList<>(); - rules.add(new Rule("Lfi Rule 1", new Condition(10, 10))); - AggregationRules aggRules = new AggregationRules(); - aggRules.setRule(rules); + // List rules = new ArrayList<>(); + // rules.add(new Rule("Lfi Rule 1", new Condition(10, 10))); + AggregationRules aggRules = apiFilter.getAggregationRules(); + //aggRules.setRule(rules); boolean isAggFilter = aggRules != null && !aggRules.getRule().isEmpty(); diff --git a/libs/dao/src/main/java/com/akto/DaoInit.java b/libs/dao/src/main/java/com/akto/DaoInit.java index 8e7037391d..86413486e2 100644 --- a/libs/dao/src/main/java/com/akto/DaoInit.java +++ b/libs/dao/src/main/java/com/akto/DaoInit.java @@ -414,4 +414,4 @@ public static void createIndices() { VulnerableTestingRunResultDao.instance.createIndicesIfAbsent(); } -} \ No newline at end of file +} diff --git a/libs/dao/src/main/java/com/akto/dao/api_protection_parse_layer/AggregationLayerParser.java b/libs/dao/src/main/java/com/akto/dao/api_protection_parse_layer/AggregationLayerParser.java index 90af26affe..958b441849 100644 --- a/libs/dao/src/main/java/com/akto/dao/api_protection_parse_layer/AggregationLayerParser.java +++ b/libs/dao/src/main/java/com/akto/dao/api_protection_parse_layer/AggregationLayerParser.java @@ -9,7 +9,7 @@ import com.fasterxml.jackson.databind.ObjectMapper; public class AggregationLayerParser { - + ObjectMapper objectMapper = new ObjectMapper(); public AggregationLayerParser() { @@ -30,7 +30,6 @@ public AggregationRules parse(Map aggregationRules) throws Excep } catch (Exception e) { throw e; } - return aggRules; } diff --git a/libs/dao/src/main/java/com/akto/dao/monitoring/FilterConfigYamlParser.java b/libs/dao/src/main/java/com/akto/dao/monitoring/FilterConfigYamlParser.java index e1593bf32c..e4a4aea2ce 100644 --- a/libs/dao/src/main/java/com/akto/dao/monitoring/FilterConfigYamlParser.java +++ b/libs/dao/src/main/java/com/akto/dao/monitoring/FilterConfigYamlParser.java @@ -4,7 +4,9 @@ import java.util.List; import java.util.Map; +import com.akto.dao.api_protection_parse_layer.AggregationLayerParser; import com.akto.dao.test_editor.filter.ConfigParser; +import com.akto.dto.api_protection_parse_layer.AggregationRules; import com.akto.dto.monitoring.FilterConfig; import com.akto.dto.test_editor.ConfigParserResult; import com.akto.dto.test_editor.ExecutorConfigParserResult; @@ -36,7 +38,7 @@ public static FilterConfig parseConfig(Map config,boolean should Object filterMap = config.get(FilterConfig.FILTER); if (filterMap == null) { isFilterError = true; - filterConfig = new FilterConfig(id, null, null); + filterConfig = new FilterConfig(id, null, null, null); } ConfigParser configParser = new ConfigParser(); @@ -44,7 +46,7 @@ public static FilterConfig parseConfig(Map config,boolean should if (filters == null) { // todo: throw error isFilterError = true; - filterConfig = new FilterConfig(id, null, null); + filterConfig = new FilterConfig(id, null, null, null); } Map> wordListMap = new HashMap<>(); @@ -54,10 +56,10 @@ public static FilterConfig parseConfig(Map config,boolean should } } catch (Exception e) { isFilterError = true; - filterConfig = new FilterConfig(id, filters, null); + filterConfig = new FilterConfig(id, filters, null, null); } if(!isFilterError){ - filterConfig = new FilterConfig(id, filters, wordListMap); + filterConfig = new FilterConfig(id, filters, wordListMap, null); } if(shouldParseExecutor){ @@ -70,6 +72,18 @@ public static FilterConfig parseConfig(Map config,boolean should filterConfig.setExecutor(executorConfigParserResult); } + AggregationLayerParser parser = new AggregationLayerParser(); + AggregationRules aggRules = null; + try { + aggRules = parser.parse(config); + } catch (Exception e) { + // TODO: handle exception + } + + if (filterConfig != null) { + filterConfig.setAggregationRules(aggRules); + } + return filterConfig; } diff --git a/libs/dao/src/main/java/com/akto/dto/api_protection_parse_layer/Rule.java b/libs/dao/src/main/java/com/akto/dto/api_protection_parse_layer/Rule.java index 50fc2422a8..3530bfa11a 100644 --- a/libs/dao/src/main/java/com/akto/dto/api_protection_parse_layer/Rule.java +++ b/libs/dao/src/main/java/com/akto/dto/api_protection_parse_layer/Rule.java @@ -7,7 +7,7 @@ public class Rule { public Rule() { } - + public Rule(String name, Condition condition) { this.name = name; this.condition = condition; @@ -25,7 +25,4 @@ public Condition getCondition() { public void setCondition(Condition condition) { this.condition = condition; } - - - } diff --git a/libs/dao/src/main/java/com/akto/dto/monitoring/FilterConfig.java b/libs/dao/src/main/java/com/akto/dto/monitoring/FilterConfig.java index 04d3c69619..e8dfd07991 100644 --- a/libs/dao/src/main/java/com/akto/dto/monitoring/FilterConfig.java +++ b/libs/dao/src/main/java/com/akto/dto/monitoring/FilterConfig.java @@ -4,6 +4,7 @@ import java.util.List; import java.util.Map; +import com.akto.dto.api_protection_parse_layer.AggregationRules; import com.akto.dto.test_editor.ConfigParserResult; import com.akto.dto.test_editor.ExecutorConfigParserResult; @@ -22,6 +23,7 @@ public class FilterConfig { private String author; public static final String _CONTENT = "content"; private String content; + private AggregationRules aggregationRules; public static final String DEFAULT_ALLOW_FILTER = "DEFAULT_ALLOW_FILTER"; public static final String DEFAULT_BLOCK_FILTER = "DEFAULT_BLOCK_FILTER"; @@ -32,10 +34,11 @@ public enum FILTER_TYPE{ private ExecutorConfigParserResult executor; - public FilterConfig(String id, ConfigParserResult filter, Map> wordLists) { + public FilterConfig(String id, ConfigParserResult filter, Map> wordLists, AggregationRules aggregationRules) { this.id = id; this.filter = filter; this.wordLists = wordLists; + this.aggregationRules = aggregationRules; } public FilterConfig() { @@ -114,4 +117,12 @@ public ExecutorConfigParserResult getExecutor() { public void setExecutor(ExecutorConfigParserResult executor) { this.executor = executor; } + + public AggregationRules getAggregationRules() { + return aggregationRules; + } + + public void setAggregationRules(AggregationRules aggregationRules) { + this.aggregationRules = aggregationRules; + } } \ No newline at end of file