From 494801458558fc02d3c280d3fcd6c781aa3e0999 Mon Sep 17 00:00:00 2001 From: lovesh-ap Date: Wed, 22 Jan 2025 10:54:19 +0530 Subject: [PATCH] Implement Request Rate-Based Processing Algorithm --- .../iast/monitoring/IastMonitoring.java | 13 ++++++------- .../intcodeagent/schedulers/SchedulerHelper.java | 2 ++ .../security/intcodeagent/websocket/WSClient.java | 2 -- .../java/com/newrelic/api/agent/security/Agent.java | 2 +- 4 files changed, 9 insertions(+), 10 deletions(-) diff --git a/newrelic-security-agent/src/main/java/com/newrelic/agent/security/intcodeagent/iast/monitoring/IastMonitoring.java b/newrelic-security-agent/src/main/java/com/newrelic/agent/security/intcodeagent/iast/monitoring/IastMonitoring.java index 737e46882..879c126b8 100644 --- a/newrelic-security-agent/src/main/java/com/newrelic/agent/security/intcodeagent/iast/monitoring/IastMonitoring.java +++ b/newrelic-security-agent/src/main/java/com/newrelic/agent/security/intcodeagent/iast/monitoring/IastMonitoring.java @@ -121,7 +121,6 @@ public static void resetEventSampler() { } IastMonitoring.getInstance().incrementSamplerCycle(); - IastMonitoring.getInstance().setRemainingHarvestRequests(0); IastMonitoring.getInstance().getHarvestedTraceId().clear(); logger.log( LogLevel.FINEST, String.format("IAST Monitoring: Sampling of Data started for sampling cycle %s", IastMonitoring.getInstance().getSamplerCycle()), IastMonitoring.class.getName()); } @@ -133,11 +132,11 @@ public static void collectSampleIfHarvested() { AgentConfig.getInstance().getAgentMode().getIastScan().getMonitoringMode().getHarvesting().set(false); NewRelicSecurity.getAgent().getSecurityMetaData().removeCustomAttribute("HARVEST"); IastMonitoring.getInstance().incrementRequestHarvested(); - int remaining = IastMonitoring.getInstance().decrementRemainingHarvestRequests(); - if(remaining <= 0){ - IastMonitoring.getInstance().setHarvestActive(false); - logger.log(LogLevel.FINEST, "IAST Monitoring: Harvesting Completed", IastMonitoring.class.getName()); - } +// int remaining = IastMonitoring.getInstance().decrementRemainingHarvestRequests(); +// if(remaining <= 0){ +// IastMonitoring.getInstance().setHarvestActive(false); +// logger.log(LogLevel.FINEST, "IAST Monitoring: Harvesting Completed", IastMonitoring.class.getName()); +// } logger.log( LogLevel.FINEST, String.format("IAST Monitoring: %s:%s Sample collected", IastMonitoring.getInstance().getHarvestCycleCount(), IastMonitoring.getInstance().getRequestHarvested()), IastMonitoring.class.getName()); } } @@ -153,7 +152,7 @@ public static boolean eventQuotaReached(String traceId) { public static boolean shouldProcessInterception() { if(AgentConfig.getInstance().getAgentMode().getIastScan().getMonitoring()) { - return IastMonitoring.getInstance().getHarvestActive() && NewRelicSecurity.getAgent().getSecurityMetaData().customAttributeContainsKey("HARVEST") && NewRelicSecurity.getAgent().getSecurityMetaData().getCustomAttribute("HARVEST", Boolean.class); + return NewRelicSecurity.getAgent().getSecurityMetaData().customAttributeContainsKey("HARVEST") && NewRelicSecurity.getAgent().getSecurityMetaData().getCustomAttribute("HARVEST", Boolean.class); } else { return true; } diff --git a/newrelic-security-agent/src/main/java/com/newrelic/agent/security/intcodeagent/schedulers/SchedulerHelper.java b/newrelic-security-agent/src/main/java/com/newrelic/agent/security/intcodeagent/schedulers/SchedulerHelper.java index 9ceda1d64..d70dffef8 100644 --- a/newrelic-security-agent/src/main/java/com/newrelic/agent/security/intcodeagent/schedulers/SchedulerHelper.java +++ b/newrelic-security-agent/src/main/java/com/newrelic/agent/security/intcodeagent/schedulers/SchedulerHelper.java @@ -2,6 +2,7 @@ import com.newrelic.agent.security.AgentConfig; import com.newrelic.agent.security.intcodeagent.filelogging.LogFileHelper; +import com.newrelic.agent.security.intcodeagent.iast.monitoring.IastMonitoring; import com.newrelic.agent.security.intcodeagent.logging.IAgentConstants; import com.newrelic.agent.security.util.IUtilConstants; import com.newrelic.api.agent.NewRelic; @@ -113,6 +114,7 @@ public void scheduleSampling(Runnable runnable, long initialDelay, long delay, T } public void shutdownSampling() { + IastMonitoring.getInstance().setHarvestActive(false); if(scheduledFutureMap.containsKey("sampling")){ ScheduledFuture future = scheduledFutureMap.get("sampling"); future.cancel(false); diff --git a/newrelic-security-agent/src/main/java/com/newrelic/agent/security/intcodeagent/websocket/WSClient.java b/newrelic-security-agent/src/main/java/com/newrelic/agent/security/intcodeagent/websocket/WSClient.java index 53ecedc11..889b9ad06 100644 --- a/newrelic-security-agent/src/main/java/com/newrelic/agent/security/intcodeagent/websocket/WSClient.java +++ b/newrelic-security-agent/src/main/java/com/newrelic/agent/security/intcodeagent/websocket/WSClient.java @@ -4,7 +4,6 @@ import com.newrelic.agent.security.AgentInfo; import com.newrelic.agent.security.instrumentator.dispatcher.DispatcherPool; import com.newrelic.agent.security.instrumentator.httpclient.RestRequestThreadPool; -import com.newrelic.agent.security.instrumentator.utils.AgentUtils; import com.newrelic.agent.security.instrumentator.utils.INRSettingsKey; import com.newrelic.agent.security.intcodeagent.controlcommand.ControlCommandProcessor; import com.newrelic.agent.security.intcodeagent.controlcommand.ControlCommandProcessorThreadPool; @@ -16,7 +15,6 @@ import com.newrelic.api.agent.security.utils.SecurityConnection; import com.newrelic.api.agent.security.utils.logging.LogLevel; import com.newrelic.agent.security.intcodeagent.logging.IAgentConstants; -import com.newrelic.agent.security.intcodeagent.utils.CommonUtils; import com.newrelic.agent.security.util.IUtilConstants; import com.newrelic.api.agent.NewRelic; import com.newrelic.api.agent.security.instrumentation.helpers.GrpcClientRequestReplayHelper; diff --git a/newrelic-security-agent/src/main/java/com/newrelic/api/agent/security/Agent.java b/newrelic-security-agent/src/main/java/com/newrelic/api/agent/security/Agent.java index c23b9ca7b..b06cdb1c0 100644 --- a/newrelic-security-agent/src/main/java/com/newrelic/api/agent/security/Agent.java +++ b/newrelic-security-agent/src/main/java/com/newrelic/api/agent/security/Agent.java @@ -258,7 +258,7 @@ private void startSecurityServices() { SchedulerHelper.getInstance().scheduleApplicationRuntimeErrorPosting(RuntimeErrorReporter.getInstance()::reportApplicationRuntimeError, 30 , 30, TimeUnit.SECONDS); SchedulerHelper.getInstance().scheduleDailyLogRollover(LogFileHelper::performDailyRollover); - SchedulerHelper.getInstance().scheduleSampling(IastMonitoring::sampleData, 0, 5, TimeUnit.SECONDS); +// SchedulerHelper.getInstance().scheduleSampling(IastMonitoring::sampleData, 0, 5, TimeUnit.SECONDS); SchedulerHelper.getInstance().scheduleResetEventSampler(IastMonitoring::resetEventSampler, 0, AgentConfig.getInstance().getAgentMode().getIastScan().getMonitoringMode().getEventQuotaTimeDuration(), TimeUnit.MINUTES); logger.logInit(