Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added debug logs for hosts #1601

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,13 @@ public static boolean aggregate(List<HttpResponseParams> responses, Map<Integer,
boolean ret = false;
Set<String> urlSet= new HashSet<>();
for (HttpResponseParams responseParams: responses) {

String debugHost = Utils.printDebugHostLog(responseParams);
if (debugHost != null) {
HttpRequestParams requestParams = responseParams.getRequestParams();
loggerMaker.infoAndAddToDb("Found host: " + debugHost + " for url: " + requestParams.getMethod() + " " + requestParams.getURL());
}

if (responseParams.getSource() == HttpResponseParams.Source.HAR || responseParams.getSource() == HttpResponseParams.Source.PCAP) {
ret = true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import com.akto.dto.usage.MetricTypes;
import com.akto.log.LoggerMaker;
import com.akto.log.LoggerMaker.LogDb;
import com.akto.runtime.utils.Utils;
import com.akto.util.filter.DictionaryFilter;
import com.akto.runtime.merge.MergeOnHostOnly;
import com.akto.runtime.policies.AktoPolicyNew;
Expand Down Expand Up @@ -1258,6 +1259,11 @@ public DbUpdateReturn getDBUpdatesForParams(APICatalog currentDelta, APICatalog
for(String key: deltaInfoMap.keySet()) {
SingleTypeInfo dbInfo = dbInfoMap.get(key);
SingleTypeInfo deltaInfo = deltaInfoMap.get(key);

if (Utils.printDebugUrlLog(deltaInfo.getUrl())) {
loggerMaker.infoAndAddToDb("Found debug url in getDBUpdatesForParams " + deltaInfo.getUrl());
}

Bson update;

int inc = deltaInfo.getCount() - (dbInfo == null ? 0 : dbInfo.getCount());
Expand Down
14 changes: 12 additions & 2 deletions apps/api-runtime/src/main/java/com/akto/runtime/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import org.apache.commons.lang3.StringUtils;
import org.apache.kafka.clients.consumer.*;
import org.apache.kafka.common.errors.WakeupException;
import org.apache.kafka.common.serialization.StringDeserializer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.concurrent.atomic.AtomicBoolean;
Expand All @@ -41,7 +40,7 @@ public class Main {
public static final String VPC_CIDR = "vpc_cidr";
public static final String ACCOUNT_ID = "account_id";
private static final Logger logger = LoggerFactory.getLogger(Main.class);
private static final LoggerMaker loggerMaker = new LoggerMaker(Main.class);
private static final LoggerMaker loggerMaker = new LoggerMaker(Main.class, LogDb.RUNTIME);

// this sync threshold time is used for deleting sample data
public static final int sync_threshold_time = 120;
Expand Down Expand Up @@ -228,6 +227,17 @@ public void run() {
}

httpResponseParams = HttpCallParser.parseKafkaMessage(r.value());
HttpRequestParams requestParams = httpResponseParams.getRequestParams();

String debugHost = Utils.printDebugHostLog(httpResponseParams);
if (debugHost != null) {
loggerMaker.infoAndAddToDb("Found debug host: " + debugHost + " in url: " + requestParams.getMethod() + " " + requestParams.getURL());
}

if (Utils.printDebugUrlLog(requestParams.getURL())) {
loggerMaker.infoAndAddToDb("Found debug url: " + requestParams.getURL());
}

} catch (Exception e) {
loggerMaker.errorAndAddToDb(e, "Error while parsing kafka message " + e, LogDb.RUNTIME);
continue;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.akto.hybrid_parsers;

import com.akto.dao.ApiCollectionsDao;
import com.akto.dao.billing.OrganizationsDao;
import com.akto.dao.context.Context;
import com.akto.dao.traffic_metrics.TrafficMetricsDao;
import com.akto.hybrid_dependency.DependencyAnalyser;
Expand All @@ -20,6 +18,7 @@
import com.akto.hybrid_runtime.Main;
import com.akto.hybrid_runtime.URLAggregator;
import com.akto.runtime.RuntimeUtil;
import com.akto.runtime.utils.Utils;
import com.akto.util.JSONUtils;
import com.akto.util.Constants;
import com.akto.util.HttpRequestResponseUtils;
Expand All @@ -29,7 +28,6 @@
import com.mongodb.client.model.*;
import okhttp3.*;
import org.apache.commons.lang3.math.NumberUtils;
import org.bson.conversions.Bson;
import com.alibaba.fastjson2.*;

import java.io.IOException;
Expand Down Expand Up @@ -546,6 +544,12 @@ public static boolean aggregate(List<HttpResponseParams> responses, Map<Integer,
boolean ret = false;
Set<String> urlSet= new HashSet<>();
for (HttpResponseParams responseParams: responses) {
String debugHost = Utils.printDebugHostLog(responseParams);
if (debugHost != null) {
HttpRequestParams requestParams = responseParams.getRequestParams();
loggerMaker.infoAndAddToDb("Found host: " + debugHost + " for url: " + requestParams.getMethod() + " " + requestParams.getURL());
}

if (responseParams.getSource() == HttpResponseParams.Source.HAR || responseParams.getSource() == HttpResponseParams.Source.PCAP) {
ret = true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.akto.data_actor.DataActor;
import com.akto.data_actor.DataActorFactory;
import com.akto.hybrid_runtime.policies.AktoPolicyNew;
import com.akto.runtime.utils.Utils;
import com.akto.util.filter.DictionaryFilter;
import com.akto.types.CappedSet;
import com.akto.util.JSONUtils;
Expand Down Expand Up @@ -1412,6 +1413,10 @@ public DbUpdateReturnHybrid getDBUpdatesForParamsHybrid(APICatalog currentDelta,
SingleTypeInfo dbInfo = dbInfoMap.get(key);
SingleTypeInfo deltaInfo = deltaInfoMap.get(key);

if (Utils.printDebugUrlLog(deltaInfo.getUrl())) {
loggerMaker.infoAndAddToDb("Found debug url in getDBUpdatesForParamsHybrid " + deltaInfo.getUrl());
}

int inc = deltaInfo.getCount() - (dbInfo == null ? 0 : dbInfo.getCount());
long lastSeenDiff = deltaInfo.getLastSeen() - (dbInfo == null ? 0 : dbInfo.getLastSeen());
boolean minMaxChanged = (dbInfo == null) || (dbInfo.getMinValue() != deltaInfo.getMinValue()) || (dbInfo.getMaxValue() != deltaInfo.getMaxValue());
Expand Down
19 changes: 12 additions & 7 deletions apps/mini-runtime/src/main/java/com/akto/hybrid_runtime/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@
import java.util.*;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern;

import com.akto.DaoInit;
import com.akto.RuntimeMode;
import com.akto.billing.UsageMetricUtils;
import com.akto.dao.*;
Expand All @@ -21,17 +19,13 @@
import com.akto.hybrid_parsers.HttpCallParser;
import com.akto.data_actor.DataActor;
import com.akto.data_actor.DataActorFactory;
import com.akto.database_abstractor_authenticator.JwtAuthenticator;
import com.akto.util.DashboardMode;
import com.akto.util.filter.DictionaryFilter;
import com.google.gson.Gson;
import com.mongodb.ConnectionString;
import com.mongodb.client.model.Filters;

import org.apache.commons.lang3.StringUtils;
import org.apache.kafka.clients.consumer.*;
import org.apache.kafka.common.errors.WakeupException;
import org.apache.kafka.common.serialization.StringDeserializer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.concurrent.atomic.AtomicBoolean;
Expand All @@ -43,7 +37,7 @@ public class Main {
public static final String VPC_CIDR = "vpc_cidr";
public static final String ACCOUNT_ID = "account_id";
private static final Logger logger = LoggerFactory.getLogger(Main.class);
private static final LoggerMaker loggerMaker = new LoggerMaker(Main.class);
private static final LoggerMaker loggerMaker = new LoggerMaker(Main.class, LogDb.RUNTIME);

private static final DataActor dataActor = DataActorFactory.fetchInstance();

Expand Down Expand Up @@ -234,6 +228,17 @@ public void run() {
}

httpResponseParams = HttpCallParser.parseKafkaMessage(r.value());
HttpRequestParams requestParams = httpResponseParams.getRequestParams();

String debugHost = Utils.printDebugHostLog(httpResponseParams);
if (debugHost != null) {
loggerMaker.infoAndAddToDb("Found debug host: " + debugHost + " in url: " + requestParams.getMethod() + " " + requestParams.getURL());
}

if (Utils.printDebugUrlLog(requestParams.getURL())) {
loggerMaker.infoAndAddToDb("Found debug url: " + requestParams.getURL());
}

} catch (Exception e) {
loggerMaker.errorAndAddToDb(e, "Error while parsing kafka message " + e, LogDb.RUNTIME);
continue;
Expand Down
44 changes: 39 additions & 5 deletions libs/utils/src/main/java/com/akto/runtime/utils/Utils.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
package com.akto.runtime.utils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.StringJoiner;
import java.util.*;
import java.util.regex.Pattern;

import org.apache.commons.lang3.math.NumberUtils;
Expand Down Expand Up @@ -98,6 +94,44 @@ public static Map<String,String> parseCookie(List<String> cookieList){

private static int GRPC_DEBUG_COUNTER = 50;

private static final Set<String> DEBUG_HOSTS_SET = initializeDebugHostsSet();
private static final Set<String> DEBUG_URLS_SET = initializeDebugUrlsSet();

private static Set<String> initializeDebugHostsSet() {
String debugHosts = System.getenv("DEBUG_HOSTS");
if (debugHosts == null || debugHosts.isEmpty()) {
return new HashSet<>();
}
return new HashSet<>(Arrays.asList(debugHosts.split(",")));
}

public static String printDebugHostLog(HttpResponseParams httpResponseParams) {
if (DEBUG_HOSTS_SET.isEmpty()) return null;
if (httpResponseParams == null || httpResponseParams.requestParams == null || httpResponseParams.requestParams.getHeaders() == null) {
return null;
}
Map<String, List<String>> headers = httpResponseParams.getRequestParams().getHeaders();
List<String> hosts = headers.get("host");
if (hosts == null || hosts.isEmpty()) return null;
String host = hosts.get(0);

return DEBUG_HOSTS_SET.contains(host) ? host : null;
}

private static Set<String> initializeDebugUrlsSet() {
String debugUrls = System.getenv("DEBUG_URLS");
if (debugUrls == null || debugUrls.isEmpty()) {
return new HashSet<>();
}
return new HashSet<>(Arrays.asList(debugUrls.split(",")));
}

public static boolean printDebugUrlLog(String url) {
if (DEBUG_URLS_SET.isEmpty()) return false;
return DEBUG_HOSTS_SET.contains(url);
}


public static HttpResponseParams parseKafkaMessage(String message) throws Exception {

//convert java object to JSON format
Expand Down
Loading