From 985b6b666630ba189c4665c2359dadaaa4c40e58 Mon Sep 17 00:00:00 2001 From: Harshit Singh Lodha Date: Wed, 26 Jul 2023 12:39:35 +0530 Subject: [PATCH] Introduced random reconnection delay in WS connection --- .../security/intcodeagent/utils/CommonUtils.java | 14 ++++++++++++++ .../security/intcodeagent/websocket/WSClient.java | 4 +++- .../intcodeagent/websocket/WSReconnectionST.java | 6 +++++- .../security/intcodeagent/websocket/WSUtils.java | 1 + 4 files changed, 23 insertions(+), 2 deletions(-) diff --git a/newrelic-security-agent/src/main/java/com/newrelic/agent/security/intcodeagent/utils/CommonUtils.java b/newrelic-security-agent/src/main/java/com/newrelic/agent/security/intcodeagent/utils/CommonUtils.java index f65e74937..d518684bb 100644 --- a/newrelic-security-agent/src/main/java/com/newrelic/agent/security/intcodeagent/utils/CommonUtils.java +++ b/newrelic-security-agent/src/main/java/com/newrelic/agent/security/intcodeagent/utils/CommonUtils.java @@ -24,6 +24,7 @@ import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.attribute.PosixFilePermissions; +import java.security.SecureRandom; import java.util.Arrays; import java.util.Collection; import java.util.Stack; @@ -34,6 +35,8 @@ public class CommonUtils { public static final String POLICY_WRITE_FAILED = "policy write failed : "; public static final String POLICY_WRITTEN_TO_FILE = "policy written to file : "; + public static SecureRandom secureRandom = new SecureRandom(); + public static boolean validateCollectorPolicyParameterSchema(AgentPolicyParameters policyParameters) { try { @@ -149,4 +152,15 @@ public static void deleteRolloverLogFiles(String fileName, int max) { } } } + + + /** + * Generate random int between range start to end. Both inclusive. + * @param start lower bound + * @param end upper bound + * @return random int + */ + public static int generateSecureRandomBetween(int start, int end) { + return secureRandom.nextInt(end-start) + start; + } } 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 0ee15dabb..f84ba0e66 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 @@ -212,7 +212,9 @@ public void onClose(int code, String reason, boolean remote) { } if (code != CloseFrame.POLICY_VALIDATION && code != CloseFrame.NORMAL) { - WSReconnectionST.getInstance().submitNewTaskSchedule(15); + int delay = CommonUtils.generateSecureRandomBetween(5, 15); + logger.log(LogLevel.INFO, String.format(WSUtils.NEXT_WS_CONNECTION_ATTEMPT_WILL_BE_IN_S_SECONDS, delay), WSReconnectionST.class.getName()); + WSReconnectionST.getInstance().submitNewTaskSchedule(delay); } } diff --git a/newrelic-security-agent/src/main/java/com/newrelic/agent/security/intcodeagent/websocket/WSReconnectionST.java b/newrelic-security-agent/src/main/java/com/newrelic/agent/security/intcodeagent/websocket/WSReconnectionST.java index c20ba1f3f..d42a23091 100644 --- a/newrelic-security-agent/src/main/java/com/newrelic/agent/security/intcodeagent/websocket/WSReconnectionST.java +++ b/newrelic-security-agent/src/main/java/com/newrelic/agent/security/intcodeagent/websocket/WSReconnectionST.java @@ -3,7 +3,9 @@ import com.newrelic.agent.security.intcodeagent.filelogging.FileLoggerThreadPool; import com.newrelic.agent.security.intcodeagent.filelogging.LogLevel; import com.newrelic.agent.security.intcodeagent.logging.IAgentConstants; +import com.newrelic.agent.security.intcodeagent.utils.CommonUtils; +import java.security.SecureRandom; import java.util.concurrent.*; import java.util.concurrent.atomic.AtomicInteger; @@ -31,7 +33,9 @@ public void run() { logger.postLogMessageIfNecessary(LogLevel.SEVERE, ERROR_WHILE_WS_RECONNECTION + e.getMessage() + COLON_SEPARATOR + e.getCause(), e, WSClient.class.getName()); } finally { if (!WSUtils.isConnected()) { - futureTask = scheduledService.schedule(runnable, 15, TimeUnit.SECONDS); + int delay = CommonUtils.generateSecureRandomBetween(5, 15); + logger.log(LogLevel.INFO, String.format(WSUtils.NEXT_WS_CONNECTION_ATTEMPT_WILL_BE_IN_S_SECONDS, delay), WSReconnectionST.class.getName()); + futureTask = scheduledService.schedule(runnable, delay, TimeUnit.SECONDS); } } } diff --git a/newrelic-security-agent/src/main/java/com/newrelic/agent/security/intcodeagent/websocket/WSUtils.java b/newrelic-security-agent/src/main/java/com/newrelic/agent/security/intcodeagent/websocket/WSUtils.java index 05fc88741..66ef0e301 100644 --- a/newrelic-security-agent/src/main/java/com/newrelic/agent/security/intcodeagent/websocket/WSUtils.java +++ b/newrelic-security-agent/src/main/java/com/newrelic/agent/security/intcodeagent/websocket/WSUtils.java @@ -5,6 +5,7 @@ import java.util.concurrent.atomic.AtomicBoolean; public class WSUtils { + public static final String NEXT_WS_CONNECTION_ATTEMPT_WILL_BE_IN_S_SECONDS = "Next WS connection attempt will be in %s seconds"; private static WSUtils instance; private static final Object lock = new Object();