Search in sources :

Example 6 with WeavePackage

use of com.newrelic.weave.weavepackage.WeavePackage 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 7 with WeavePackage

use of com.newrelic.weave.weavepackage.WeavePackage 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 8 with WeavePackage

use of com.newrelic.weave.weavepackage.WeavePackage 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 9 with WeavePackage

use of com.newrelic.weave.weavepackage.WeavePackage in project newrelic-java-agent by newrelic.

the class ClassWeaverService method registerInstrumentationCloseable.

/**
 * Register a closable which will run if/when a {@link WeavePackage} is deregistered.
 */
public void registerInstrumentationCloseable(String instrumentationName, Closeable closeable) {
    WeavePackage weavePackage = weavePackageManager.getWeavePackage(instrumentationName);
    listener.registerInstrumentationCloseable(instrumentationName, weavePackage, closeable);
}
Also used : WeavePackage(com.newrelic.weave.weavepackage.WeavePackage) CachedWeavePackage(com.newrelic.weave.weavepackage.CachedWeavePackage)

Example 10 with WeavePackage

use of com.newrelic.weave.weavepackage.WeavePackage in project newrelic-java-agent by newrelic.

the class TestCachedWeavePackage method testCachedWeavePackage.

/**
 * Test to make sure internal WeavePackage caching is working.
 */
@Test
public void testCachedWeavePackage() {
    WeavePackage internalPackage = manager.getWeavePackage("com.newrelic.instrumentation.servlet-2.4");
    Assert.assertNotNull(internalPackage);
    Assert.assertTrue("Class type is " + internalPackage.getClass().getName(), internalPackage instanceof CachedWeavePackage);
    WeavePackage externalPackage = manager.getWeavePackage("Weave Test");
    Assert.assertNotNull(externalPackage);
    Assert.assertFalse(externalPackage instanceof CachedWeavePackage);
}
Also used : CachedWeavePackage(com.newrelic.weave.weavepackage.CachedWeavePackage) WeavePackage(com.newrelic.weave.weavepackage.WeavePackage) CachedWeavePackage(com.newrelic.weave.weavepackage.CachedWeavePackage) Test(org.junit.Test)

Aggregations

WeavePackage (com.newrelic.weave.weavepackage.WeavePackage)18 WeavePackageConfig (com.newrelic.weave.weavepackage.WeavePackageConfig)9 CachedWeavePackage (com.newrelic.weave.weavepackage.CachedWeavePackage)8 ArrayList (java.util.ArrayList)7 WeaveViolation (com.newrelic.weave.violation.WeaveViolation)4 IOException (java.io.IOException)4 ClassTransformerConfig (com.newrelic.agent.config.ClassTransformerConfig)3 ClassMatchVisitorFactory (com.newrelic.agent.instrumentation.context.ClassMatchVisitorFactory)3 ClassCache (com.newrelic.weave.utils.ClassCache)3 ClassLoaderFinder (com.newrelic.weave.utils.ClassLoaderFinder)3 PackageValidationResult (com.newrelic.weave.weavepackage.PackageValidationResult)3 URL (java.net.URL)3 HashSet (java.util.HashSet)3 JarInputStream (java.util.jar.JarInputStream)3 AgentConfig (com.newrelic.agent.config.AgentConfig)2 File (java.io.File)2 FileInputStream (java.io.FileInputStream)2 Map (java.util.Map)2 Test (org.junit.Test)2 AgentClassLoaderBaseInstrumentation (com.newrelic.agent.instrumentation.builtin.AgentClassLoaderBaseInstrumentation)1