Skip to content

Commit 2c00ffd

Browse files
committed
change to a single check at the end of the config init
1 parent fcfeac0 commit 2c00ffd

File tree

3 files changed

+33
-16
lines changed

3 files changed

+33
-16
lines changed

dd-java-agent/agent-bootstrap/src/main/java/datadog/trace/bootstrap/Agent.java

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
import datadog.trace.bootstrap.instrumentation.jfr.InstrumentationBasedProfiling;
4242
import datadog.trace.util.AgentTaskScheduler;
4343
import datadog.trace.util.AgentThreadFactory.AgentThread;
44-
import datadog.trace.util.SystemUtils;
4544
import datadog.trace.util.throwable.FatalAgentMisconfigurationError;
4645
import java.lang.instrument.Instrumentation;
4746
import java.lang.reflect.InvocationTargetException;
@@ -371,18 +370,6 @@ public void run() {
371370
StaticEventLogger.end("Profiling");
372371
}
373372

374-
// log eventual errors that would have happened before we could log them
375-
if (SystemUtils.hasEnvError) {
376-
log.warn(
377-
"The Java Security Manager prevented the Datadog Tracer from accessing at least one environment variable. "
378-
+ "Consider granting AllPermission to the dd-java-agent jar.");
379-
}
380-
if (SystemUtils.hasPropertyError) {
381-
log.warn(
382-
"The Java Security Manager prevented the Datadog Tracer from accessing at least one system property. "
383-
+ "Consider granting AllPermission to the dd-java-agent jar.");
384-
}
385-
386373
StaticEventLogger.end("Agent.start");
387374
}
388375

internal-api/src/main/java/datadog/trace/api/Config.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1794,6 +1794,19 @@ PROFILING_DATADOG_PROFILER_ENABLED, isDatadogProfilerSafeInCurrentEnvironment())
17941794
"AppSec SCA is enabled but telemetry is disabled. AppSec SCA will not work.");
17951795
}
17961796

1797+
// check if the security manager is causing us trouble
1798+
if (!SystemUtils.canAccessEnvironmentVariables()) {
1799+
log.warn(
1800+
"The Java Security Manager is preventing the Datadog Tracer from accessing some environment variables. "
1801+
+ "Consider granting AllPermission to the dd-java-agent jar.");
1802+
}
1803+
1804+
if (!SystemUtils.canAccessSystemProperties()) {
1805+
log.warn(
1806+
"The Java Security Manager is preventing the Datadog Tracer from accessing some system properties. "
1807+
+ "Consider granting AllPermission to the dd-java-agent jar.");
1808+
}
1809+
17971810
log.debug("New instance: {}", this);
17981811
}
17991812

internal-api/src/main/java/datadog/trace/util/SystemUtils.java

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ public static String getEnvOrDefault(String envVar, String defaultValue) {
1414
try {
1515
return System.getenv(envVar);
1616
} catch (SecurityException e) {
17-
hasEnvError = true;
1817
return defaultValue;
1918
}
2019
}
@@ -23,7 +22,6 @@ public static String tryGetProperty(String property) {
2322
try {
2423
return System.getProperty(property);
2524
} catch (SecurityException e) {
26-
hasPropertyError = true;
2725
return null;
2826
}
2927
}
@@ -32,8 +30,27 @@ public static String getPropertyOrDefault(String property, String defaultValue)
3230
try {
3331
return System.getProperty(property, defaultValue);
3432
} catch (SecurityException e) {
35-
hasPropertyError = true;
3633
return defaultValue;
3734
}
3835
}
36+
37+
public static boolean canAccessSystemProperties() {
38+
try {
39+
// try to access a common system property and see what happens
40+
System.getProperty("os.name");
41+
} catch (SecurityException e) {
42+
return false;
43+
}
44+
return true;
45+
}
46+
47+
public static boolean canAccessEnvironmentVariables() {
48+
try {
49+
// try to access a common env var and see what happens
50+
System.getenv("DD_ENV");
51+
} catch (SecurityException e) {
52+
return false;
53+
}
54+
return true;
55+
}
3956
}

0 commit comments

Comments
 (0)