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
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,15 @@ public static Response onResponse(
}
}

publish(ctx, clientResponse, responseCb);
try {
publish(ctx, clientResponse, responseCb);
} catch (final BlockingException e) {
throw e;
} catch (final Exception e) {
// the response body has already been captured into `result` above; don't lose it by
// letting the caller fall back to the original (possibly already-consumed) response
LOGGER.debug("Failed to run AppSec response hooks", e);
}
return result;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package datadog.trace.instrumentation.okhttp2;

import static datadog.trace.api.gateway.Events.EVENTS;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
Expand All @@ -9,8 +11,13 @@
import static org.mockito.Mockito.when;

import com.squareup.okhttp.Interceptor;
import com.squareup.okhttp.MediaType;
import com.squareup.okhttp.Protocol;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import com.squareup.okhttp.ResponseBody;
import datadog.trace.api.gateway.CallbackProvider;
import datadog.trace.api.gateway.Flow;
import datadog.trace.api.gateway.RequestContext;
import datadog.trace.api.gateway.RequestContextSlot;
import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
Expand Down Expand Up @@ -65,4 +72,32 @@ void ioExceptionFromProceedPropagatesWithoutRetry() throws IOException {
assertSame(failure, thrown);
verify(chain, times(1)).proceed(request);
}

@Test
void responseHookFailureAfterBodyCapturePreservesCapturedBody() throws IOException {
final String json = "{\"hello\":\"world\"}";
final Response response =
new Response.Builder()
.request(request)
.protocol(Protocol.HTTP_1_1)
.code(200)
.message("OK")
.body(ResponseBody.create(MediaType.parse("application/json"), json))
.build();
when(chain.proceed(request)).thenReturn(response);

final CallbackProvider cbp = mock(CallbackProvider.class);
when(cbp.getCallback(EVENTS.httpClientSampling()))
.thenReturn((ctx, requestId) -> new Flow.ResultFlow<>(Boolean.TRUE));
when(cbp.getCallback(EVENTS.httpClientResponse()))
.thenReturn(
(ctx, clientResponse) -> {
throw new RuntimeException("boom");
});
when(AgentTracer.get().getCallbackProvider(any(RequestContextSlot.class))).thenReturn(cbp);

final Response result = interceptor.intercept(chain);

assertEquals(json, result.body().string());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,15 @@ public static Response onResponse(
}
}

publish(ctx, clientResponse, responseCb);
try {
publish(ctx, clientResponse, responseCb);
} catch (final BlockingException e) {
throw e;
} catch (final Exception e) {
// the response body has already been captured into `result` above; don't lose it by
// letting the caller fall back to the original (possibly already-consumed) response
LOGGER.debug("Failed to run AppSec response hooks", e);
}
return result;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package datadog.trace.instrumentation.okhttp3;

import static datadog.trace.api.gateway.Events.EVENTS;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
Expand All @@ -9,14 +11,19 @@
import static org.mockito.Mockito.when;

import datadog.trace.api.gateway.CallbackProvider;
import datadog.trace.api.gateway.Flow;
import datadog.trace.api.gateway.RequestContext;
import datadog.trace.api.gateway.RequestContextSlot;
import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
import datadog.trace.bootstrap.instrumentation.api.AgentTracer;
import datadog.trace.bootstrap.instrumentation.api.Tags;
import java.io.IOException;
import okhttp3.Interceptor;
import okhttp3.MediaType;
import okhttp3.Protocol;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -65,4 +72,32 @@ void ioExceptionFromProceedPropagatesWithoutRetry() throws IOException {
assertSame(failure, thrown);
verify(chain, times(1)).proceed(request);
}

@Test
void responseHookFailureAfterBodyCapturePreservesCapturedBody() throws IOException {
final String json = "{\"hello\":\"world\"}";
final Response response =
new Response.Builder()
.request(request)
.protocol(Protocol.HTTP_1_1)
.code(200)
.message("OK")
.body(ResponseBody.create(MediaType.parse("application/json"), json))
.build();
when(chain.proceed(request)).thenReturn(response);

final CallbackProvider cbp = mock(CallbackProvider.class);
when(cbp.getCallback(EVENTS.httpClientSampling()))
.thenReturn((ctx, requestId) -> new Flow.ResultFlow<>(Boolean.TRUE));
when(cbp.getCallback(EVENTS.httpClientResponse()))
.thenReturn(
(ctx, clientResponse) -> {
throw new RuntimeException("boom");
});
when(AgentTracer.get().getCallbackProvider(any(RequestContextSlot.class))).thenReturn(cbp);

final Response result = interceptor.intercept(chain);

assertEquals(json, result.body().string());
}
}
Loading