JMXFetch loading threw permission errors

This commit is contained in:
Laplie Anderson 2019-07-30 16:56:49 -04:00
parent c41536cbe2
commit cf98110991
2 changed files with 32 additions and 13 deletions

View File

@ -10,6 +10,7 @@ import java.net.URLClassLoader;
import java.net.URLConnection; import java.net.URLConnection;
import java.net.URLStreamHandler; import java.net.URLStreamHandler;
import java.nio.file.NoSuchFileException; import java.nio.file.NoSuchFileException;
import java.security.Permission;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.jar.JarEntry; import java.util.jar.JarEntry;
@ -148,10 +149,16 @@ public class DatadogClassLoader extends URLClassLoader {
@Override @Override
protected URLConnection openConnection(final URL url) throws IOException { protected URLConnection openConnection(final URL url) throws IOException {
if (url.getFile().equals("/")) {
// "/" is used as the default url of the jar
// This is called by the SecureClassLoader trying to obtain permissions
return new InternalJarURLConnection(url, new byte[] {});
}
final byte[] bytes = filenameToBytes.get(url.getFile()); final byte[] bytes = filenameToBytes.get(url.getFile());
if (bytes == null) { if (bytes == null) {
throw new NoSuchFileException(url.getFile()); throw new NoSuchFileException(url.getFile(), null, url.getFile() + " not in internal jar");
} }
return new InternalJarURLConnection(url, bytes); return new InternalJarURLConnection(url, bytes);
@ -175,6 +182,12 @@ public class DatadogClassLoader extends URLClassLoader {
public InputStream getInputStream() throws IOException { public InputStream getInputStream() throws IOException {
return new ByteArrayInputStream(bytes); return new ByteArrayInputStream(bytes);
} }
@Override
public Permission getPermission() {
// No permissions needed because all classes are in memory
return null;
}
} }
/** /**

View File

@ -53,9 +53,19 @@ public class TracingAgent {
final Method registerCallbackMethod = final Method registerCallbackMethod =
agentInstallerClass.getMethod("registerClassLoadCallback", String.class, Runnable.class); agentInstallerClass.getMethod("registerClassLoadCallback", String.class, Runnable.class);
registerCallbackMethod.invoke( registerCallbackMethod.invoke(
null, null, "java.util.logging.LogManager", new LoggingCallback(inst));
"java.util.logging.LogManager", } else {
new Runnable() { startJmxFetch(inst);
}
}
protected static class LoggingCallback implements Runnable {
private final Instrumentation inst;
public LoggingCallback(final Instrumentation inst) {
this.inst = inst;
}
@Override @Override
public void run() { public void run() {
try { try {
@ -64,10 +74,6 @@ public class TracingAgent {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
} }
});
} else {
startJmxFetch(inst);
}
} }
public static synchronized void startDatadogAgent(final Instrumentation inst) throws Exception { public static synchronized void startDatadogAgent(final Instrumentation inst) throws Exception {