Repository navigation
Add block-outcome telemetry to Grizzly, Jetty, Liberty, Spring-webmvc, Undertow and Akka-http #12527
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
Merged
Merged
Add block-outcome telemetry to Grizzly, Jetty, Liberty, Spring-webmvc, Undertow and Akka-http #12527
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
57fe813
Add block-outcome telemetry to blocking response helpers
jandro996 fa682b9
Centralize block_failure reporting via BlockResponseFunction overload
jandro996 6aaa2e4
Report block failure on unguarded exception paths in Grizzly, Jetty a…
jandro996 41c75ff
Guard remaining Undertow form-parse commit call and scope Grizzly fai…
jandro996 3f3db6e
Fix block_failure reporting gaps in Undertow and Liberty exception paths
jandro996 6497f3e
Fix block_failure reporting gaps in Spring-webmvc blocking response p…
jandro996 12f21e3
Report block_failure on Liberty direct response-commit path in syncBu…
jandro996 811af3f
Merge branch 'master' into block-telemetry-3
jandro996 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
167 changes: 167 additions & 0 deletions
167
...va/datadog/trace/instrumentation/akkahttp/appsec/UnmarshallerHelpersBlockFailureTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,167 @@ | ||
| package datadog.trace.instrumentation.akkahttp.appsec; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
| import static org.junit.jupiter.api.Assertions.assertNull; | ||
| import static org.junit.jupiter.api.Assertions.assertSame; | ||
|
|
||
| import datadog.appsec.api.blocking.BlockingContentType; | ||
| import datadog.appsec.api.blocking.BlockingException; | ||
| import datadog.trace.api.appsec.AppSecContext; | ||
| import datadog.trace.api.gateway.BlockResponseFunction; | ||
| import datadog.trace.api.gateway.Flow; | ||
| import datadog.trace.api.gateway.RequestContext; | ||
| import datadog.trace.api.gateway.RequestContextSlot; | ||
| import datadog.trace.api.internal.TraceSegment; | ||
| import datadog.trace.bootstrap.instrumentation.api.ClientIpAddressData; | ||
| import java.util.Map; | ||
| import java.util.function.Function; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| /** | ||
| * Covers the {@code UnmarshallerHelpers.tryBlock() -> AppSecContext.reportBlockFailure()} path. | ||
| * Hand-written test doubles are used because Mockito is only on this module's test runtime | ||
| * classpath, not its test compile classpath (see {@code gradle/java_deps.gradle}: {@code | ||
| * testRuntimeOnly libs.mokito.core}). | ||
| */ | ||
| class UnmarshallerHelpersBlockFailureTest { | ||
|
|
||
| private static final Flow.Action.RequestBlockingAction RBA = | ||
| new Flow.Action.RequestBlockingAction(403, BlockingContentType.AUTO); | ||
|
|
||
| @Test | ||
| void reportsBlockFailureWhenBlockingResponseCannotBeCommitted() { | ||
| CountingAppSecContext appSecCtx = new CountingAppSecContext(); | ||
| TestRequestContext ctx = | ||
| new TestRequestContext(new TestBlockResponseFunction(false), appSecCtx); | ||
|
|
||
| BlockingException exception = UnmarshallerHelpers.tryBlock(ctx, RBA, "for test"); | ||
|
|
||
| assertNull(exception); | ||
| assertEquals(1, appSecCtx.blockFailures); | ||
| assertSame(RBA, ctx.brf.lastAction); | ||
| assertSame(ctx.traceSegment, ctx.brf.lastSegment); | ||
| } | ||
|
|
||
| @Test | ||
| void doesNotReportBlockFailureWhenBlockingResponseIsCommitted() { | ||
| CountingAppSecContext appSecCtx = new CountingAppSecContext(); | ||
| TestRequestContext ctx = new TestRequestContext(new TestBlockResponseFunction(true), appSecCtx); | ||
|
|
||
| BlockingException exception = UnmarshallerHelpers.tryBlock(ctx, RBA, "for test"); | ||
|
|
||
| assertNotNull(exception); | ||
| assertEquals("Blocked request (for test)", exception.getMessage()); | ||
| assertEquals(0, appSecCtx.blockFailures); | ||
| } | ||
|
|
||
| @Test | ||
| void doesNotReportOrThrowWhenNoBlockResponseFunctionIsRegistered() { | ||
| CountingAppSecContext appSecCtx = new CountingAppSecContext(); | ||
| TestRequestContext ctx = new TestRequestContext(null, appSecCtx); | ||
|
|
||
| BlockingException exception = UnmarshallerHelpers.tryBlock(ctx, RBA, "for test"); | ||
|
|
||
| assertNull(exception); | ||
| assertEquals(0, appSecCtx.blockFailures); | ||
| } | ||
|
|
||
| @Test | ||
| void doesNotThrowWhenAppSecSlotDoesNotHoldAnAppSecContext() { | ||
| TestRequestContext nullSlot = | ||
| new TestRequestContext(new TestBlockResponseFunction(false), null); | ||
| assertNull(UnmarshallerHelpers.tryBlock(nullSlot, RBA, "for test")); | ||
|
|
||
| TestRequestContext foreignSlot = | ||
| new TestRequestContext(new TestBlockResponseFunction(false), "not an AppSecContext"); | ||
| assertNull(UnmarshallerHelpers.tryBlock(foreignSlot, RBA, "for test")); | ||
| } | ||
|
|
||
| private static final class CountingAppSecContext implements AppSecContext { | ||
| private int blockFailures; | ||
|
|
||
| @Override | ||
| public boolean isManuallyKept() { | ||
| return false; | ||
| } | ||
|
|
||
| @Override | ||
| public void reportBlockFailure() { | ||
| blockFailures++; | ||
| } | ||
| } | ||
|
|
||
| private static final class TestBlockResponseFunction implements BlockResponseFunction { | ||
| private final boolean committed; | ||
| private TraceSegment lastSegment; | ||
| private Flow.Action.RequestBlockingAction lastAction; | ||
|
|
||
| private TestBlockResponseFunction(boolean committed) { | ||
| this.committed = committed; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean tryCommitBlockingResponse( | ||
| TraceSegment segment, Flow.Action.RequestBlockingAction rba) { | ||
| this.lastAction = rba; | ||
| return BlockResponseFunction.super.tryCommitBlockingResponse(segment, rba); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean tryCommitBlockingResponse( | ||
| TraceSegment segment, | ||
| int statusCode, | ||
| BlockingContentType templateType, | ||
| Map<String, String> extraHeaders, | ||
| String securityResponseId) { | ||
| this.lastSegment = segment; | ||
| return committed; | ||
| } | ||
| } | ||
|
|
||
| private static final class TestRequestContext implements RequestContext { | ||
| private final TestBlockResponseFunction brf; | ||
| private final Object appSecData; | ||
| private final TraceSegment traceSegment = TraceSegment.NoOp.INSTANCE; | ||
|
|
||
| private TestRequestContext(TestBlockResponseFunction brf, Object appSecData) { | ||
| this.brf = brf; | ||
| this.appSecData = appSecData; | ||
| } | ||
|
|
||
| @SuppressWarnings("unchecked") | ||
| @Override | ||
| public <T> T getData(RequestContextSlot slot) { | ||
| return slot == RequestContextSlot.APPSEC ? (T) appSecData : null; | ||
| } | ||
|
|
||
| @Override | ||
| public TraceSegment getTraceSegment() { | ||
| return traceSegment; | ||
| } | ||
|
|
||
| @Override | ||
| public void setBlockResponseFunction(BlockResponseFunction blockResponseFunction) {} | ||
|
|
||
| @Override | ||
| public BlockResponseFunction getBlockResponseFunction() { | ||
| return brf; | ||
| } | ||
|
|
||
| @Override | ||
| public <T> T getOrCreateMetaStructTop(String key, Function<String, T> defaultValue) { | ||
| return null; | ||
| } | ||
|
|
||
| @Override | ||
| public void setClientIpAddressData(ClientIpAddressData clientIpAddressData) {} | ||
|
|
||
| @Override | ||
| public ClientIpAddressData getClientIpAddressData() { | ||
| return null; | ||
| } | ||
|
|
||
| @Override | ||
| public void close() {} | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.