Skip to content
Open
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
4 changes: 3 additions & 1 deletion dd-java-agent/instrumentation/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,9 @@ TaskProvider<JavaExec> registerIndexTask(String indexTaskName, String indexer, S
}

instrumentationNaming {
exclusions = ["org-json-20230227"] // org-json does not use semver
exclusions = ["org-json-20230227",
// org-json does not use semver
]
suffixes = ["-common", "-stubs", "-iast"]
}

Expand Down
27 changes: 17 additions & 10 deletions dd-java-agent/instrumentation/commons-httpclient-2.0/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,22 @@ muzzle {
pass {
group = "commons-httpclient"
module = "commons-httpclient"
versions = "[2.0,]"
skipVersions += "20020423" // ancient pre-release version
skipVersions += '2.0-final' // broken metadata on maven central
assertInverse = true
versions = "[2.0,4.0)"
skipVersions += '3.1-jenkins-1'
skipVersions += '2.0-final'
}
pass {
name = 'commons-http-client-x' // for IAST instrumenters valid for 1.x
name = 'commons-http-client-x' // for IAST instrumenters
group = "commons-httpclient"
module = "commons-httpclient"
versions = "[2.0,]"
skipVersions += "20020423" // ancient pre-release version
skipVersions += '2.0-final' // broken metadata on maven central
assertInverse = false
versions = "[2.0,4.0)"
skipVersions += '3.1-jenkins-1'
skipVersions += '2.0-final'
}
fail {
group = "commons-httpclient"
module = "commons-httpclient"
versions = "[,2.0)"
}
}

Expand All @@ -27,5 +30,9 @@ dependencies {

testImplementation group: 'commons-httpclient', name: 'commons-httpclient', version: '2.0'

latestDepTestImplementation group: 'commons-httpclient', name: 'commons-httpclient', version: '(2.0,20000000]'
// latestDepTest tests against the highest 2.x release (module instruments 2.x only)
latestDepTestImplementation group: 'commons-httpclient', name: 'commons-httpclient', version: '2+'

testRuntimeOnly(project(':dd-java-agent:instrumentation:datadog:asm:iast-instrumenter'))
testRuntimeOnly(project(':dd-java-agent:instrumentation:java:java-net:java-net-1.8'))
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.StatusLine;
import org.apache.commons.httpclient.URIException;

public class CommonsHttpClientDecorator extends HttpClientDecorator<HttpMethod, HttpMethod> {

public static final CharSequence COMMONS_HTTP_CLIENT =
UTF8BytesString.create("commons-http-client");
public static final CommonsHttpClientDecorator DECORATE = new CommonsHttpClientDecorator();
Expand All @@ -34,11 +34,14 @@ protected String method(final HttpMethod httpMethod) {
@Override
protected URI url(final HttpMethod httpMethod) throws URISyntaxException {
try {
// org.apache.commons.httpclient.URI -> java.net.URI
return new URI(httpMethod.getURI().toString());
} catch (final URIException e) {
throw new URISyntaxException("", e.getMessage());
org.apache.commons.httpclient.URI uri = httpMethod.getURI();
if (uri != null) {
return new URI(uri.toString());
}
} catch (Exception e) {
// getURI() can throw URIException; fall through to return null
}
return null;
}

@Override
Expand All @@ -53,18 +56,18 @@ protected int status(final HttpMethod httpMethod) {
}

@Override
protected String getRequestHeader(HttpMethod request, String headerName) {
Header header = request.getRequestHeader(headerName);
if (null != header) {
protected String getRequestHeader(HttpMethod httpMethod, String headerName) {
Header header = httpMethod.getRequestHeader(headerName);
if (header != null) {
return header.getValue();
}
return null;
}

@Override
protected String getResponseHeader(HttpMethod response, String headerName) {
Header header = response.getResponseHeader(headerName);
if (null != header) {
protected String getResponseHeader(HttpMethod httpMethod, String headerName) {
Header header = httpMethod.getResponseHeader(headerName);
if (header != null) {
return header.getValue();
}
return null;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,15 @@
package datadog.trace.instrumentation.commonshttpclient;

import static datadog.trace.agent.tooling.InstrumenterModule.TargetSystem.CONTEXT_TRACKING;
import static datadog.trace.agent.tooling.bytebuddy.matcher.NameMatchers.named;
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.activateSpan;
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.startSpan;
import static datadog.trace.bootstrap.instrumentation.api.Java8BytecodeBridge.getCurrentContext;
import static datadog.trace.instrumentation.commonshttpclient.CommonsHttpClientDecorator.DECORATE;
import static datadog.trace.instrumentation.commonshttpclient.CommonsHttpClientDecorator.HTTP_REQUEST;
import static datadog.trace.instrumentation.commonshttpclient.HttpHeadersInjectAdapter.SETTER;
import static net.bytebuddy.matcher.ElementMatchers.isMethod;
import static net.bytebuddy.matcher.ElementMatchers.takesArgument;
import static net.bytebuddy.matcher.ElementMatchers.takesArguments;

import com.google.auto.service.AutoService;
import datadog.appsec.api.blocking.BlockingException;
import datadog.trace.agent.tooling.Instrumenter;
import datadog.trace.agent.tooling.InstrumenterModule;
import datadog.trace.agent.tooling.annotation.AppliesOn;
import datadog.trace.bootstrap.CallDepthThreadLocalMap;
import datadog.trace.bootstrap.instrumentation.api.AgentScope;
import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
import net.bytebuddy.asm.Advice;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;

@AutoService(InstrumenterModule.class)
Expand All @@ -40,72 +28,40 @@ public String instrumentedType() {
@Override
public String[] helperClassNames() {
return new String[] {
packageName + ".CommonsHttpClientDecorator", packageName + ".HttpHeadersInjectAdapter",
packageName + ".CommonsHttpClientDecorator",
packageName + ".HttpHeadersInjectAdapter",
packageName + ".HelperMethods",
};
}

@Override
public void methodAdvice(MethodTransformer transformer) {
transformer.applyAdvices(
// All executeMethod overloads delegate to the 3-arg version:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This must be verified for each version to make sure it alway such three arguments because of it's much stricter and Java allows method overloading. Restricting arguments other than the one that's used seems to be too limiting. That can cause inflexibility if in some versions of the library this method signature changes.
The comment about avoiding duplicate spans is misleading. It's avoided anyways with the call-depth check, that is kept as-is anyways, but moved to a helper class.

// executeMethod(HostConfiguration, HttpMethod, HttpState)
// Instrumenting only the delegate avoids duplicate spans.
transformer.applyAdvice(
isMethod()
.and(named("executeMethod"))
.and(takesArguments(3))
.and(takesArgument(1, named("org.apache.commons.httpclient.HttpMethod"))),
CommonsHttpClientInstrumentation.class.getName() + "$ExecAdvice",
CommonsHttpClientInstrumentation.class.getName() + "$ContextPropagationAdvice");
.and(takesArgument(0, named("org.apache.commons.httpclient.HostConfiguration")))
.and(takesArgument(1, named("org.apache.commons.httpclient.HttpMethod")))
.and(takesArgument(2, named("org.apache.commons.httpclient.HttpState"))),
CommonsHttpClientInstrumentation.class.getName() + "$ExecuteMethodAdvice");
}

public static class ExecAdvice {
@Advice.OnMethodEnter(suppress = Throwable.class)
public static AgentScope methodEnter(@Advice.Argument(1) final HttpMethod httpMethod) {

try {
final int callDepth = CallDepthThreadLocalMap.incrementCallDepth(HttpClient.class);
if (callDepth > 0) {
return null;
}

final AgentSpan span = startSpan("commons-http-client", HTTP_REQUEST);
final AgentScope scope = activateSpan(span);
public static class ExecuteMethodAdvice {

DECORATE.afterStart(span);
DECORATE.onRequest(span, httpMethod);

return scope;
} catch (BlockingException e) {
CallDepthThreadLocalMap.reset(HttpClient.class);
// re-throw blocking exceptions
throw e;
}
@Advice.OnMethodEnter(suppress = Throwable.class)
public static AgentScope methodEnter(@Advice.Argument(1) final HttpMethod method) {
return HelperMethods.doMethodEnter(method);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no value in introducing the HelperMethods class

}

@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void methodExit(
@Advice.Enter final AgentScope scope,
@Advice.Argument(1) final HttpMethod httpMethod,
@Advice.Argument(1) final HttpMethod method,
@Advice.Thrown final Throwable throwable) {

if (scope == null) {
return;
}
final AgentSpan span = scope.span();
try {
DECORATE.onResponse(span, httpMethod);
DECORATE.onError(span, throwable);
DECORATE.beforeFinish(span);
} finally {
scope.close();
span.finish();
CallDepthThreadLocalMap.reset(HttpClient.class);
}
}
}

@AppliesOn(CONTEXT_TRACKING)
public static class ContextPropagationAdvice {
@Advice.OnMethodEnter(suppress = Throwable.class)
public static void methodEnter(@Advice.Argument(1) final HttpMethod httpMethod) {
DECORATE.injectContext(getCurrentContext(), httpMethod, SETTER);
HelperMethods.doMethodExit(scope, method, throwable);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package datadog.trace.instrumentation.commonshttpclient;

import static datadog.context.Context.current;
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.activateSpan;
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.startSpan;
import static datadog.trace.instrumentation.commonshttpclient.CommonsHttpClientDecorator.DECORATE;
import static datadog.trace.instrumentation.commonshttpclient.CommonsHttpClientDecorator.HTTP_REQUEST;
import static datadog.trace.instrumentation.commonshttpclient.HttpHeadersInjectAdapter.SETTER;

import datadog.trace.bootstrap.CallDepthThreadLocalMap;
import datadog.trace.bootstrap.instrumentation.api.AgentScope;
import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;

public class HelperMethods {

public static AgentScope doMethodEnter(final HttpMethod method) {
final int callDepth = CallDepthThreadLocalMap.incrementCallDepth(HttpClient.class);
if (callDepth > 0) {
return null;
}

final AgentSpan span =
startSpan(CommonsHttpClientDecorator.COMMONS_HTTP_CLIENT.toString(), HTTP_REQUEST);
DECORATE.afterStart(span);
DECORATE.onRequest(span, method);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Restore BlockingException handling around onRequest

When AppSec/RASP blocks an outbound URL, DECORATE.onRequest can throw BlockingException; this advice is still declared with suppress = Throwable, so without the previous catch/reset path the exception is swallowed by the advice wrapper and CallDepthThreadLocalMap remains incremented. In that AppSec blocking scenario the commons-httpclient request is allowed to continue, and later calls on the same thread return null from doMethodEnter, disabling tracing for this client until the thread is reused/reset.

Useful? React with 👍 / 👎.


final AgentScope scope = activateSpan(span);

DECORATE.injectContext(current(), method, SETTER);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Restore the context-tracking propagation advice

This injects propagation only from the tracing helper, whereas the previous ContextPropagationAdvice was a separate @AppliesOn(CONTEXT_TRACKING) advice. Since AgentInstaller enables CONTEXT_TRACKING independently of tracing, deployments with tracing disabled still used to forward existing trace context through commons-httpclient; after this collapse there is no context-tracking-only advice for this matcher, so those calls stop carrying propagation headers.

Useful? React with 👍 / 👎.


return scope;
}

public static void doMethodExit(
final AgentScope scope, final HttpMethod method, final Throwable throwable) {
if (scope == null) {
return;
}
final AgentSpan span = scope.span();
try {
if (throwable != null) {
DECORATE.onError(span, throwable);
} else {
DECORATE.onResponse(span, method);
}
DECORATE.beforeFinish(span);
} finally {
scope.close();
span.finish();
CallDepthThreadLocalMap.reset(HttpClient.class);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import datadog.context.propagation.CarrierSetter;
import javax.annotation.ParametersAreNonnullByDefault;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpMethod;

@ParametersAreNonnullByDefault
Expand All @@ -12,6 +11,6 @@ public class HttpHeadersInjectAdapter implements CarrierSetter<HttpMethod> {

@Override
public void set(final HttpMethod carrier, final String key, final String value) {
carrier.setRequestHeader(new Header(key, value));
carrier.setRequestHeader(key, value);
}
}

This file was deleted.

Loading
Loading