From d1b2e76eea770d5c264cff60ea416ab913e2b227 Mon Sep 17 00:00:00 2001 From: ayushaga14 Date: Mon, 30 Dec 2024 00:14:38 +0530 Subject: [PATCH] use fastjson lib and increase template fetch interval --- .../tasks/MaliciousTrafficDetectorTask.java | 5 +++-- .../java/com/akto/dto/OriginalHttpRequest.java | 18 ++++++++++++++++++ .../com/akto/dto/OriginalHttpResponse.java | 9 +++++++++ .../dao/src/main/java/com/akto/dto/RawApi.java | 13 +++++++++++++ 4 files changed, 43 insertions(+), 2 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 07dd61d82c..d33401ec1e 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 @@ -52,6 +52,7 @@ public class MaliciousTrafficDetectorTask implements Task { private Map apiFilters; private int filterLastUpdatedAt = 0; + private int filterUpdateIntervalSec = 300; private final Kafka internalKafka; @@ -112,7 +113,7 @@ public void run() { private Map getFilters() { int now = (int) (System.currentTimeMillis() / 1000); - if (now - filterLastUpdatedAt < 60) { + if (now - filterLastUpdatedAt < filterUpdateIntervalSec) { return apiFilters; } @@ -126,7 +127,7 @@ private boolean validateFilterForRequest( HttpResponseParams responseParam, FilterConfig apiFilter) { try { String message = responseParam.getOrig(); - RawApi rawApi = RawApi.buildFromMessage(message); + RawApi rawApi = RawApi.buildFromMessageNew(message); int apiCollectionId = httpCallParser.createApiCollectionId(responseParam); responseParam.requestParams.setApiCollectionId(apiCollectionId); String url = responseParam.getRequestParams().getURL(); diff --git a/libs/dao/src/main/java/com/akto/dto/OriginalHttpRequest.java b/libs/dao/src/main/java/com/akto/dto/OriginalHttpRequest.java index f6c85ebc52..abdb32e1af 100644 --- a/libs/dao/src/main/java/com/akto/dto/OriginalHttpRequest.java +++ b/libs/dao/src/main/java/com/akto/dto/OriginalHttpRequest.java @@ -83,6 +83,24 @@ public void buildFromSampleMessage(String message) { this.headers = buildHeadersMap(json, "requestHeaders"); } + public void buildFromSampleMessageNew(JSONObject json) { + String rawUrl = (String) json.get("path"); + String[] rawUrlArr = rawUrl.split("\\?"); + this.url = rawUrlArr[0]; + if (rawUrlArr.length > 1) { + this.queryParams = rawUrlArr[1]; + } + + this.type = (String) json.get("type"); + + this.method = (String) json.get("method"); + + String requestPayload = (String) json.get("requestPayload"); + this.body = requestPayload.trim(); + + this.headers = buildHeadersMap(json, "requestHeaders"); + } + public String getJsonRequestBody() { return HttpRequestResponseUtils.rawToJsonString(this.body, this.headers); } diff --git a/libs/dao/src/main/java/com/akto/dto/OriginalHttpResponse.java b/libs/dao/src/main/java/com/akto/dto/OriginalHttpResponse.java index 3964e35eb0..6e464605fd 100644 --- a/libs/dao/src/main/java/com/akto/dto/OriginalHttpResponse.java +++ b/libs/dao/src/main/java/com/akto/dto/OriginalHttpResponse.java @@ -1,6 +1,8 @@ package com.akto.dto; import com.akto.util.HttpRequestResponseUtils; +import com.alibaba.fastjson2.JSON; +import com.alibaba.fastjson2.JSONObject; import com.google.gson.Gson; import org.apache.commons.lang3.math.NumberUtils; @@ -39,6 +41,13 @@ public void buildFromSampleMessage(String message) { this.statusCode = Integer.parseInt(json.get("statusCode").toString()); } + public void buildFromSampleMessageNew(JSONObject json) { + String responsePayload = (String) json.get("responsePayload"); + this.body = responsePayload != null ? responsePayload.trim() : null; + this.headers = OriginalHttpRequest.buildHeadersMap(json, "responseHeaders"); + this.statusCode = Integer.parseInt(json.get("statusCode").toString()); + } + public void addHeaderFromLine(String line) { if (this.headers == null || this.headers.isEmpty()) { this.headers = new HashMap<>(); diff --git a/libs/dao/src/main/java/com/akto/dto/RawApi.java b/libs/dao/src/main/java/com/akto/dto/RawApi.java index bbc8b9488c..748bbd2a9e 100644 --- a/libs/dao/src/main/java/com/akto/dto/RawApi.java +++ b/libs/dao/src/main/java/com/akto/dto/RawApi.java @@ -7,6 +7,8 @@ import java.util.Map; import com.akto.dto.type.RequestTemplate; +import com.alibaba.fastjson2.JSON; +import com.alibaba.fastjson2.JSONObject; import com.mongodb.BasicDBList; import com.fasterxml.jackson.databind.ObjectMapper; import com.mongodb.BasicDBObject; @@ -55,6 +57,17 @@ public static RawApi buildFromMessage(String message) { return new RawApi(request, response, message); } + public static RawApi buildFromMessageNew(String message) { + JSONObject json = JSON.parseObject(message); + OriginalHttpRequest request = new OriginalHttpRequest(); + request.buildFromSampleMessageNew(json); + + OriginalHttpResponse response = new OriginalHttpResponse(); + response.buildFromSampleMessageNew(json); + + return new RawApi(request, response, message); + } + public BasicDBObject fetchReqPayload() { OriginalHttpRequest req = this.getRequest(); String reqBody = req.getBody();