Skip to content

Commit f1e9c8c

Browse files
authored
Merge pull request #1879 from newrelic/saxon/log-security-env-values
When IAST is disabled log security related env and system properties
2 parents 3362da5 + 6d834a0 commit f1e9c8c

File tree

3 files changed

+60
-11
lines changed

3 files changed

+60
-11
lines changed

newrelic-agent/src/main/java/com/newrelic/agent/Agent.java

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import com.google.common.collect.ImmutableMap;
1111
import com.newrelic.agent.bridge.AgentBridge;
1212
import com.newrelic.agent.config.AgentConfig;
13-
import com.newrelic.agent.config.AgentConfigImpl;
1413
import com.newrelic.agent.config.AgentJarHelper;
1514
import com.newrelic.agent.config.ConfigService;
1615
import com.newrelic.agent.config.ConfigServiceFactory;
@@ -29,8 +28,6 @@
2928
import com.newrelic.agent.util.UnwindableInstrumentation;
3029
import com.newrelic.agent.util.UnwindableInstrumentationImpl;
3130
import com.newrelic.agent.util.asm.ClassStructure;
32-
import com.newrelic.api.agent.Config;
33-
import com.newrelic.api.agent.NewRelic;
3431
import com.newrelic.api.agent.security.NewRelicSecurity;
3532
import com.newrelic.bootstrap.BootstrapAgent;
3633
import com.newrelic.bootstrap.BootstrapLoader;
@@ -275,17 +272,10 @@ public void disconnected(IRPMService rpmService) {
275272
}
276273
} else {
277274
LOG.info("New Relic Security is completely disabled by one of the user provided config `security.enabled`, `security.agent.enabled` or `high_security`. Not loading security capabilities.");
278-
Config config = NewRelic.getAgent().getConfig();
279-
logConfig(config, Level.FINE, AgentConfigImpl.HIGH_SECURITY);
280-
logConfig(config, Level.FINE, SecurityAgentConfig.SECURITY_ENABLED);
281-
logConfig(config, Level.FINE, SecurityAgentConfig.SECURITY_AGENT_ENABLED);
275+
SecurityAgentConfig.logSettings(Level.FINE);
282276
}
283277
}
284278

285-
private static void logConfig(Config config, Level logLevel, String key) {
286-
LOG.log(logLevel, "{0} = {1}", key, config.getValue(key));
287-
}
288-
289279
private static Instrumentation maybeWrapInstrumentation(Instrumentation inst) {
290280
if (System.getProperty(BootstrapAgent.NR_AGENT_ARGS_SYSTEM_PROPERTY) != null) {
291281
return UnwindableInstrumentationImpl.wrapInstrumentation(inst);

newrelic-agent/src/main/java/com/newrelic/agent/config/SecurityAgentConfig.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,14 @@
99

1010
import com.google.common.collect.Sets;
1111
import com.newrelic.api.agent.Config;
12+
import com.newrelic.api.agent.Logger;
1213
import com.newrelic.api.agent.NewRelic;
1314

15+
import java.util.Arrays;
1416
import java.util.Collections;
17+
import java.util.Map;
1518
import java.util.Set;
19+
import java.util.logging.Level;
1620

1721
/* Default config should look like:
1822
*
@@ -150,4 +154,32 @@ public static boolean isSecurityLowPriorityInstrumentationEnabled() {
150154
return config.getValue(SECURITY_LOW_PRIORITY_INSTRUMENTATION_ENABLED, SECURITY_LOW_PRIORITY_INSTRUMENTATION_ENABLED_DEFAULT);
151155
}
152156

157+
/**
158+
* Log security settings to help debug when the IAST agent is not enabled.
159+
*/
160+
public static void logSettings(Level logLevel) {
161+
logSettings(NewRelic.getAgent().getConfig(), NewRelic.getAgent().getLogger(), logLevel,
162+
System.getenv(), System.getProperties());
163+
}
164+
165+
static void logSettings(final Config config, final Logger logger, Level logLevel,
166+
Map<String, String> environment,
167+
Map<Object, Object> systemProperties) {
168+
169+
if (logger.isLoggable(logLevel)) {
170+
Arrays.asList(AgentConfigImpl.HIGH_SECURITY, SECURITY_ENABLED, SECURITY_AGENT_ENABLED).forEach(key ->
171+
logger.log(logLevel, "{0} = {1}", key, config.getValue(key)));
172+
173+
environment.forEach((key, value) -> {
174+
if (key.contains("NEW_RELIC") && key.contains("SECURITY")) {
175+
logger.log(logLevel, "Environment {0} = {1}", key, value);
176+
}
177+
});
178+
systemProperties.forEach((key, value) -> {
179+
if (key.toString().contains("newrelic.config.") && key.toString().contains("security")) {
180+
logger.log(logLevel, "System property {0} = {1}", key, value);
181+
}
182+
});
183+
}
184+
}
153185
}

newrelic-agent/src/test/java/com/newrelic/agent/config/SecurityAgentConfigTest.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
package com.newrelic.agent.config;
22

3+
import com.google.common.collect.ImmutableMap;
34
import com.newrelic.api.agent.Agent;
5+
import com.newrelic.api.agent.Config;
6+
import com.newrelic.api.agent.Logger;
47
import com.newrelic.api.agent.NewRelic;
58
import org.junit.AfterClass;
69
import org.junit.BeforeClass;
710
import org.junit.Test;
811
import org.mockito.MockedStatic;
912

13+
import java.util.Map;
14+
import java.util.logging.Level;
15+
1016
import static com.newrelic.agent.config.SecurityAgentConfig.SECURITY_AGENT_ENABLED;
1117
import static com.newrelic.agent.config.SecurityAgentConfig.SECURITY_AGENT_ENABLED_DEFAULT;
1218
import static com.newrelic.agent.config.SecurityAgentConfig.SECURITY_DETECTION_DESERIALIZATION_ENABLED;
@@ -28,6 +34,8 @@
2834
import static org.junit.Assert.assertTrue;
2935
import static org.mockito.Mockito.mock;
3036
import static org.mockito.Mockito.mockStatic;
37+
import static org.mockito.Mockito.times;
38+
import static org.mockito.Mockito.verify;
3139
import static org.mockito.Mockito.when;
3240

3341
public class SecurityAgentConfigTest {
@@ -163,4 +171,23 @@ public void isSecurityLowPriorityInstrumentationEnabled_returnsCorrectEnabledFla
163171
assertFalse(SecurityAgentConfig.isSecurityLowPriorityInstrumentationEnabled());
164172
}
165173

174+
@Test
175+
public void testLogSettings() {
176+
Config config = mock(Config.class);
177+
Logger logger = mock(Logger.class);
178+
when(logger.isLoggable(Level.FINE)).thenReturn(true);
179+
final String test = "test";
180+
final String environmentValueToSkip = "SECURITY_THING";
181+
Map<String, String> env = ImmutableMap.of("NEW_RELIC_SECURITY_AGENT_ENABLED", "true",
182+
environmentValueToSkip, test);
183+
final String systemPropertyToSkip = "security.test";
184+
Map<Object, Object> systemProperties = ImmutableMap.of("newrelic.config.security.enabled", "true",
185+
systemPropertyToSkip, test);
186+
SecurityAgentConfig.logSettings(config, logger, Level.FINE, env, systemProperties);
187+
188+
verify(logger, times(1)).log(Level.FINE, "Environment {0} = {1}", "NEW_RELIC_SECURITY_AGENT_ENABLED", "true");
189+
verify(logger, times(0)).log(Level.FINE, "Environment {0} = {1}", environmentValueToSkip, test);
190+
verify(logger, times(1)).log(Level.FINE, "System property {0} = {1}", "newrelic.config.security.enabled", "true");
191+
verify(logger, times(0)).log(Level.FINE, "System property {0} = {1}", systemPropertyToSkip, test);
192+
}
166193
}

0 commit comments

Comments
 (0)