Skip to content

Commit

Permalink
use fastjson lib and increase template fetch interval
Browse files Browse the repository at this point in the history
  • Loading branch information
ayushaga14 committed Dec 29, 2024
1 parent f640fdd commit d1b2e76
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public class MaliciousTrafficDetectorTask implements Task {

private Map<String, FilterConfig> apiFilters;
private int filterLastUpdatedAt = 0;
private int filterUpdateIntervalSec = 300;

private final Kafka internalKafka;

Expand Down Expand Up @@ -112,7 +113,7 @@ public void run() {

private Map<String, FilterConfig> getFilters() {
int now = (int) (System.currentTimeMillis() / 1000);
if (now - filterLastUpdatedAt < 60) {
if (now - filterLastUpdatedAt < filterUpdateIntervalSec) {
return apiFilters;
}

Expand All @@ -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();
Expand Down
18 changes: 18 additions & 0 deletions libs/dao/src/main/java/com/akto/dto/OriginalHttpRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
9 changes: 9 additions & 0 deletions libs/dao/src/main/java/com/akto/dto/OriginalHttpResponse.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<>();
Expand Down
13 changes: 13 additions & 0 deletions libs/dao/src/main/java/com/akto/dto/RawApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit d1b2e76

Please sign in to comment.