Search in sources :

Example 11 with WeavePackageConfig

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;
}
Also used : WeavePackageConfig(com.newrelic.weave.weavepackage.WeavePackageConfig) ClassTransformerConfig(com.newrelic.agent.config.ClassTransformerConfig) ArrayList(java.util.ArrayList) ClassLoaderPackageAccessInstrumentation(com.newrelic.agent.instrumentation.builtin.ClassLoaderPackageAccessInstrumentation) WeavePackage(com.newrelic.weave.weavepackage.WeavePackage) IOException(java.io.IOException)

Example 12 with WeavePackageConfig

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;
}
Also used : WeavePackageConfig(com.newrelic.weave.weavepackage.WeavePackageConfig) ClassMatchVisitorFactory(com.newrelic.agent.instrumentation.context.ClassMatchVisitorFactory) JarInputStream(java.util.jar.JarInputStream) WeavePackage(com.newrelic.weave.weavepackage.WeavePackage) CachedWeavePackage(com.newrelic.weave.weavepackage.CachedWeavePackage) WeaveViolation(com.newrelic.weave.violation.WeaveViolation) FileInputStream(java.io.FileInputStream) IllegalClassFormatException(java.lang.instrument.IllegalClassFormatException) IOException(java.io.IOException) AgentConfig(com.newrelic.agent.config.AgentConfig) ClassTransformerConfig(com.newrelic.agent.config.ClassTransformerConfig) File(java.io.File) HashSet(java.util.HashSet)

Example 13 with WeavePackageConfig

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;
}
Also used : AgentConfig(com.newrelic.agent.config.AgentConfig) WeavePackageConfig(com.newrelic.weave.weavepackage.WeavePackageConfig) JarInputStream(java.util.jar.JarInputStream) ClassTransformerConfig(com.newrelic.agent.config.ClassTransformerConfig) WeavePackage(com.newrelic.weave.weavepackage.WeavePackage) CachedWeavePackage(com.newrelic.weave.weavepackage.CachedWeavePackage) URL(java.net.URL)

Example 14 with WeavePackageConfig

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");
}
Also used : WeavePackageConfig(com.newrelic.weave.weavepackage.WeavePackageConfig) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 15 with WeavePackageConfig

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));
}
Also used : WeavePackageConfig(com.newrelic.weave.weavepackage.WeavePackageConfig) HashMap(java.util.HashMap) Test(org.junit.Test)

Aggregations

WeavePackageConfig (com.newrelic.weave.weavepackage.WeavePackageConfig)22 Test (org.junit.Test)10 WeavePackage (com.newrelic.weave.weavepackage.WeavePackage)9 HashMap (java.util.HashMap)9 ArrayList (java.util.ArrayList)7 IOException (java.io.IOException)4 ClassTransformerConfig (com.newrelic.agent.config.ClassTransformerConfig)3 URL (java.net.URL)3 JarInputStream (java.util.jar.JarInputStream)3 AgentConfig (com.newrelic.agent.config.AgentConfig)2 WeaveViolation (com.newrelic.weave.violation.WeaveViolation)2 CachedWeavePackage (com.newrelic.weave.weavepackage.CachedWeavePackage)2 File (java.io.File)2 HashSet (java.util.HashSet)2 AgentClassLoaderBaseInstrumentation (com.newrelic.agent.instrumentation.builtin.AgentClassLoaderBaseInstrumentation)1 AgentClassLoaderInstrumentation (com.newrelic.agent.instrumentation.builtin.AgentClassLoaderInstrumentation)1 ClassLoaderPackageAccessInstrumentation (com.newrelic.agent.instrumentation.builtin.ClassLoaderPackageAccessInstrumentation)1 ClassMatchVisitorFactory (com.newrelic.agent.instrumentation.context.ClassMatchVisitorFactory)1 AgentPostprocessors (com.newrelic.agent.instrumentation.weaver.preprocessors.AgentPostprocessors)1 AgentPreprocessors (com.newrelic.agent.instrumentation.weaver.preprocessors.AgentPreprocessors)1