Skip to content

Commit

Permalink
Merge pull request #1265 from akto-api-security/hotfix/grpc_logging
Browse files Browse the repository at this point in the history
grpc logging and allowing replace body
  • Loading branch information
shivam-rawat-akto authored Jul 11, 2024
2 parents 6b4df80 + 1b830fd commit 7809d35
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import java.util.regex.Pattern;

import static com.akto.runtime.RuntimeUtil.matchesDefaultPayload;
import static com.akto.util.HttpRequestResponseUtils.GRPC_CONTENT_TYPE;

public class HttpCallParser {
private final int sync_threshold_count;
Expand Down Expand Up @@ -88,7 +89,8 @@ public HttpCallParser(String userIdentifier, int thresh, int sync_threshold_coun
apiCatalogSync.buildFromDB(false, fetchAllSTI);
this.dependencyAnalyser = new DependencyAnalyser(apiCatalogSync.dbState, !Main.isOnprem);
}

private static int GRPC_DEBUG_COUNTER = 50;

public static HttpResponseParams parseKafkaMessage(String message) throws Exception {

//convert java object to JSON format
Expand All @@ -102,7 +104,16 @@ public static HttpResponseParams parseKafkaMessage(String message) throws Except
String rawRequestPayload = (String) json.get("requestPayload");
String requestPayload = HttpRequestResponseUtils.rawToJsonString(rawRequestPayload,requestHeaders);


if (GRPC_DEBUG_COUNTER > 0) {
String acceptableContentType = HttpRequestResponseUtils.getAcceptableContentType(requestHeaders);
if (acceptableContentType != null && rawRequestPayload.length() > 0) {
// only if request payload is of FORM_URL_ENCODED_CONTENT_TYPE we convert it to json
if (acceptableContentType.equals(GRPC_CONTENT_TYPE)) {
loggerMaker.infoAndAddToDb("grpc kafka payload:" + message,LogDb.RUNTIME);
GRPC_DEBUG_COUNTER--;
}
}
}

String apiCollectionIdStr = json.getOrDefault("akto_vxlan_id", "0").toString();
int apiCollectionId = 0;
Expand Down
5 changes: 3 additions & 2 deletions libs/dao/src/main/java/com/akto/util/grpc/ProtoBufUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InvalidObjectException;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
Expand Down Expand Up @@ -103,12 +104,12 @@ private static Map<Object, Object> decodeProtoInput(CodedInputStream input, int
return map;
}

public static String base64EncodedJsonToProtobuf(String payload) throws IOException{
public static String base64EncodedJsonToProtobuf(String payload) throws Exception{
Map<Object, Object> map = null;
try {
map = ProtoBufUtils.getInstance().mapper.readValue(payload, Map.class);
} catch (Exception e) {
map = new HashMap<>();
throw new InvalidObjectException("Unable to parse payload");
}
return base64EncodedJsonToProtobuf(map);
}
Expand Down
1 change: 1 addition & 0 deletions libs/utils/src/main/java/com/akto/testing/ApiExecutor.java
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,7 @@ private static OriginalHttpResponse sendWithRequestBody(OriginalHttpRequest requ
try {
payload = ProtoBufUtils.base64EncodedJsonToProtobuf(payload);
} catch (Exception e) {
loggerMaker.errorAndAddToDb("Unable to encode payload:" + payload, LogDb.RUNTIME);
payload = request.getBody();
}
}
Expand Down

0 comments on commit 7809d35

Please sign in to comment.