use of com.newrelic.weave.weavepackage.WeavePackageConfig in project newrelic-java-agent by newrelic.
the class ClassLoaderClassTransformer method buildCheckAccessPatcherPackage.
private WeavePackage buildCheckAccessPatcherPackage() {
WeavePackageConfig checkAccessPatcher = WeavePackageConfig.builder().name("check-access-patcher").errorHandleClassNode(LogAndReturnOriginal.ERROR_HANDLER_NODE).extensionClassTemplate(extensionTemplate).build();
List<byte[]> checkAccessPackageBytes = new ArrayList<>();
try {
// us to selectively get around security manager checks from instrumentation modules (if config is enabled)
if (System.getSecurityManager() != null) {
ClassTransformerConfig config = ServiceFactory.getConfigService().getDefaultAgentConfig().getClassTransformerConfig();
if (config.isGrantPackageAccess()) {
// Grab the bytes of our Agent Classloader instrumentation class. Note: This call uses "findResource" but
// this is ok because we are not under a classloader lock at this point, we are still in the premain()
byte[] classloaderPackageAccessInstrumentationBytes = WeaveUtils.getClassBytesFromClassLoaderResource(ClassLoaderPackageAccessInstrumentation.class.getName(), ClassLoaderClassTransformer.class.getClassLoader());
checkAccessPackageBytes.add(classloaderPackageAccessInstrumentationBytes);
return new WeavePackage(checkAccessPatcher, checkAccessPackageBytes);
}
}
} catch (IOException e) {
Agent.LOG.log(Level.FINE, e, "Unable to initialize agent classloader instrumentation");
}
return null;
}
use of com.newrelic.weave.weavepackage.WeavePackageConfig in project newrelic-java-agent by newrelic.
the class ClassWeaverService method loadExternalWeavePackages.
/**
* Load new instrumentation packages from disk and put any new matchers in the matchers collection.
*
* @param weaveExtensions
*/
private Collection<ClassMatchVisitorFactory> loadExternalWeavePackages(Collection<File> weaveExtensions) {
Collection<ClassMatchVisitorFactory> matchers = new HashSet<>();
for (File weaveExtension : weaveExtensions) {
try (JarInputStream stream = new JarInputStream(new FileInputStream(weaveExtension))) {
AgentConfig agentConfig = ServiceFactory.getConfigService().getDefaultAgentConfig();
WeavePackageConfig weaveConfig = createWeavePackageConfig(stream, weaveExtension.getAbsolutePath(), instrumentation, WeavePackageType.CUSTOM, agentConfig);
ClassTransformerConfig classTransformerConfig = agentConfig.getClassTransformerConfig();
String instrName = weaveConfig.getName();
if (weavePackageManager.isRegistered(instrName)) {
weavePackageManager.deregister(instrName);
this.externalWeavePackages.remove(weaveExtension.getAbsolutePath());
}
if (!classTransformerConfig.isWeavePackageEnabled(weaveConfig)) {
if (weaveConfig.isEnabled()) {
// Only log this if the module has been explicitly disabled (not if it is disabled by default)
LOG.log(Level.INFO, "Instrumentation {0} is disabled. Skipping.", instrName);
}
continue;
}
WeavePackage externalPackage = WeavePackage.createWeavePackage(stream, weaveConfig);
if (externalPackage.getPackageViolations().size() > 0) {
LOG.log(Level.FINER, "skip loading external weave package: {0}", instrName);
for (WeaveViolation violation : externalPackage.getPackageViolations()) {
LOG.log(Level.FINER, "\t{0}", violation);
}
} else {
weavePackageManager.register(externalPackage);
externalWeavePackages.put(weaveExtension.getAbsolutePath(), weaveConfig.getName());
}
} catch (Exception e) {
LOG.log(Level.FINE, e, "Error reading weave extension {0}", weaveExtension.getAbsolutePath());
}
}
return matchers;
}
use of com.newrelic.weave.weavepackage.WeavePackageConfig in project newrelic-java-agent by newrelic.
the class ClassWeaverService method createWeavePackage.
/**
* Create a weave package from a jar source.
*
* @param inputStream The JarInputStream to read from.
* @param source URL where the jar was read from.
*/
private WeavePackage createWeavePackage(InputStream inputStream, String source) throws Exception {
JarInputStream jarStream = new JarInputStream(inputStream);
AgentConfig agentConfig = ServiceFactory.getConfigService().getDefaultAgentConfig();
WeavePackageConfig weavePackageConfig = createWeavePackageConfig(jarStream, source, instrumentation, WeavePackageType.INTERNAL, agentConfig);
ClassTransformerConfig classTransformerConfig = agentConfig.getClassTransformerConfig();
String weavePackageName = weavePackageConfig.getName();
if (!classTransformerConfig.isWeavePackageEnabled(weavePackageConfig)) {
if (weavePackageConfig.isEnabled()) {
// Only log this if the module has been explicitly disabled (not if it is disabled by default)
LOG.log(Level.INFO, "Instrumentation {0} is disabled. Skipping.", weavePackageName);
}
return null;
}
if ("com.newrelic.instrumentation.jcache-1.0.0".equals(weavePackageName)) {
Boolean jcacheDatastoreEnabled = agentConfig.getValue("class_transformer.com.newrelic.instrumentation.jcache-datastore-1.0.0.enabled", Boolean.FALSE);
if (jcacheDatastoreEnabled) {
LOG.log(Level.INFO, " Instrumentation {0} is disabled since {1} is enabled. Skipping.", weavePackageName, "com.newrelic.instrumentation.jcache-datastore-1.0.0");
return null;
}
}
WeavePackage weavePackage = CachedWeavePackage.createWeavePackage(new URL(source), jarStream, weavePackageConfig);
return weavePackage;
}
use of com.newrelic.weave.weavepackage.WeavePackageConfig in project newrelic-java-agent by newrelic.
the class ClassTransformerConfigImplTest method weavePackageConfigAliasSystemPropertyOverride.
@Test
public void weavePackageConfigAliasSystemPropertyOverride() {
final String moduleName = "com.newrelic.instrumentation.mymodule-1.0";
final String aliasName = "my_alias";
HashMap<String, Object> props = new HashMap<>();
ClassTransformerConfig config = new ClassTransformerConfigImpl(props, true);
System.setProperty("newrelic.config.class_transformer." + aliasName + ".enabled", "false");
WeavePackageConfig weaveConfig = WeavePackageConfig.builder().name(moduleName).alias(aliasName).enabled(true).build();
Assert.assertFalse(config.isWeavePackageEnabled(weaveConfig));
System.clearProperty("newrelic.config.class_transformer." + aliasName + ".enabled");
}
use of com.newrelic.weave.weavepackage.WeavePackageConfig in project newrelic-java-agent by newrelic.
the class ClassTransformerConfigImplTest method weavePackageConfigDefault.
@Test
public void weavePackageConfigDefault() {
HashMap<String, Object> props = new HashMap<>();
ClassTransformerConfig config = new ClassTransformerConfigImpl(props, true);
WeavePackageConfig weaveConfig = WeavePackageConfig.builder().name("notenabled").enabled(false).build();
Assert.assertFalse(config.isWeavePackageEnabled(weaveConfig));
weaveConfig = WeavePackageConfig.builder().name("enabled").enabled(true).build();
assertTrue(config.isWeavePackageEnabled(weaveConfig));
}
Aggregations