Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for Jersey 2 #149

Merged
merged 5 commits into from
Dec 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions instrumentation-security/jersey-2.16/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
dependencies {
implementation(project(":newrelic-security-api"))
implementation("com.newrelic.agent.java:newrelic-weaver-api:${nrAPIVersion}")
implementation("com.newrelic.agent.java:newrelic-api:${nrAPIVersion}")
implementation("org.glassfish.jersey.core:jersey-server:2.16")

}

jar {
manifest { attributes 'Implementation-Title': 'com.newrelic.instrumentation.security.jersey-2-16' }
}

// org.glassfish.jersey.core 2.28 version starts pulling in jakarata jar named dependencies.
// Version 3.0.0-M1 starts pulling in jakarata with renamed jar and packages
verifyInstrumentation {
passesOnly 'org.glassfish.jersey.core:jersey-server:[2.16,3.0)'
exclude 'org.glassfish.jersey.core:jersey-server:[2.0-m05-2,2.0)'
excludeRegex '.*-(M|RC)[0-9]*'
}

site {
title 'Jersey'
type 'Framework'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
*
* * Copyright 2020 New Relic Corporation. All rights reserved.
* * SPDX-License-Identifier: Apache-2.0
*
*/

package com.newrelic.agent.security.instrumentation.jersey2;

import com.newrelic.api.agent.Trace;
import com.newrelic.api.agent.weaver.MatchType;
import com.newrelic.api.agent.weaver.Weave;
import com.newrelic.api.agent.weaver.Weaver;
import org.glassfish.jersey.server.ContainerRequest;

import java.io.OutputStream;

@Weave(type = MatchType.ExactClass, originalName = "org.glassfish.jersey.server.ApplicationHandler")
public abstract class ApplicationHandler_Handler {

public void handle(ContainerRequest requestContext) {
boolean isRequestLockAcquired = false;
try {
if (requestContext != null) {
isRequestLockAcquired = HttpRequestHelper.acquireRequestLockIfPossible();
if (isRequestLockAcquired) {
HttpRequestHelper.preprocessSecurityHook(requestContext);
HttpRequestHelper.registerUserLevelCode("JERSEY");
}
}
Weaver.callOriginal();
} finally {
if(isRequestLockAcquired){
HttpRequestHelper.releaseRequestLock();
}
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
*
* * Copyright 2020 New Relic Corporation. All rights reserved.
* * SPDX-License-Identifier: Apache-2.0
*
*/

package com.newrelic.agent.security.instrumentation.jersey2;

import com.newrelic.api.agent.security.NewRelicSecurity;
import com.newrelic.api.agent.security.instrumentation.helpers.GenericHelper;
import com.newrelic.api.agent.security.schema.StringUtils;
import com.newrelic.api.agent.weaver.MatchType;
import com.newrelic.api.agent.weaver.Weaver;
import org.glassfish.jersey.message.internal.OutboundJaxrsResponse;

import com.newrelic.api.agent.weaver.Weave;
import org.glassfish.jersey.message.internal.OutboundMessageContext;
import org.glassfish.jersey.server.ContainerRequest;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.newrelic.api.agent.security.instrumentation.helpers.ServletHelper.SERVLET_GET_IS_OPERATION_LOCK;

@Weave(type = MatchType.ExactClass, originalName = "org.glassfish.jersey.server.ContainerResponse")
public abstract class ContainerResponse_Instrumentation {

ContainerResponse_Instrumentation(final ContainerRequest requestContext, final OutboundJaxrsResponse response) {
if(response != null && response.getContext() != null && response.getContext().hasEntity()){
Object responseObject = response.getContext().getEntity();
NewRelicSecurity.getAgent().getSecurityMetaData().getResponse().setResponseBody(new StringBuilder(String.valueOf(responseObject)));
}
}

public abstract OutboundMessageContext getWrappedMessageContext();

public void close() {
boolean isLockAcquired = false;
try {
isLockAcquired = GenericHelper.acquireLockIfPossible(SERVLET_GET_IS_OPERATION_LOCK);
if(isLockAcquired) {
HttpRequestHelper.postProcessSecurityHook(this.getClass().getName(), getWrappedMessageContext());
}
Weaver.callOriginal();
} finally {
if(isLockAcquired) {
GenericHelper.releaseLock(SERVLET_GET_IS_OPERATION_LOCK);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.newrelic.agent.security.instrumentation.jersey2;

import com.newrelic.api.agent.security.NewRelicSecurity;
import com.newrelic.api.agent.security.instrumentation.helpers.GenericHelper;
import com.newrelic.api.agent.weaver.MatchType;
import com.newrelic.api.agent.weaver.Weave;
import com.newrelic.api.agent.weaver.Weaver;

import java.io.InputStream;

import static com.newrelic.api.agent.security.instrumentation.helpers.ServletHelper.SERVLET_GET_IS_OPERATION_LOCK;

@Weave(type = MatchType.ExactClass, originalName = "org.glassfish.jersey.message.internal.EntityInputStream")
public class EntityInputStream_Instrumentation {

public final InputStream getWrappedStream() {
InputStream retunObject;
boolean isLockAcquired = false;
try {
isLockAcquired = GenericHelper.acquireLockIfPossible(SERVLET_GET_IS_OPERATION_LOCK);
retunObject = Weaver.callOriginal();
if (isLockAcquired && NewRelicSecurity.isHookProcessingActive() && retunObject != null) {
HttpRequestHelper.registerInputStreamHashIfNeeded(retunObject.hashCode());
}
} finally {
if(isLockAcquired) {
GenericHelper.releaseLock(SERVLET_GET_IS_OPERATION_LOCK);
}
}
return retunObject;
}

}
Loading
Loading