Skip to content

Commit 11069e2

Browse files
Api Security schema computation moved to post-processing stage in serialization thread
1 parent ee45ab8 commit 11069e2

File tree

4 files changed

+24
-41
lines changed

4 files changed

+24
-41
lines changed

dd-java-agent/appsec/src/main/java/com/datadog/appsec/gateway/AppSecRequestContext.java

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
import datadog.trace.api.UserIdCollectionMode;
1212
import datadog.trace.api.http.StoredBodySupplier;
1313
import datadog.trace.api.internal.TraceSegment;
14-
import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
15-
import datadog.trace.bootstrap.instrumentation.api.AgentTracer;
1614
import datadog.trace.util.stacktrace.StackTraceEvent;
1715
import io.sqreen.powerwaf.Additive;
1816
import io.sqreen.powerwaf.PowerwafContext;
@@ -485,32 +483,15 @@ public String getSessionId() {
485483

486484
@Override
487485
public void close() {
488-
final AgentSpan span = AgentTracer.activeSpan();
489-
close(span != null && span.isRequiresPostProcessing());
490-
}
491-
492-
/* end interface for GatewayBridge */
493-
494-
/* Should be accessible from the modules */
495-
496-
public void close(boolean requiresPostProcessing) {
497-
if (additive != null || derivatives != null) {
498-
log.debug(
499-
SEND_TELEMETRY, "WAF object had not been closed (probably missed request-end event)");
500-
closeAdditive();
501-
derivatives = null;
502-
}
503-
504-
// check if we might need to further post process data related to the span in order to not free
505-
// related data
506-
if (requiresPostProcessing) {
507-
return;
508-
}
509-
486+
closeAdditive();
510487
collectedCookies = null;
511488
requestHeaders.clear();
512489
responseHeaders.clear();
513490
persistentData.clear();
491+
if (derivatives != null) {
492+
derivatives.clear();
493+
derivatives = null;
494+
}
514495
}
515496

516497
/** @return the portion of the body read so far, if any */

dd-java-agent/appsec/src/main/java/com/datadog/appsec/gateway/GatewayBridge.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -766,11 +766,6 @@ private NoopFlow onRequestEnded(RequestContext ctx_, IGSpanInfo spanInfo) {
766766
return NoopFlow.INSTANCE;
767767
}
768768

769-
maybeExtractSchemas(ctx);
770-
771-
// WAF call
772-
ctx.closeAdditive();
773-
774769
TraceSegment traceSeg = ctx_.getTraceSegment();
775770

776771
// AppSec report metric and events for web span only
@@ -833,7 +828,9 @@ private NoopFlow onRequestEnded(RequestContext ctx_, IGSpanInfo spanInfo) {
833828
}
834829
}
835830

836-
ctx.close(spanInfo.isRequiresPostProcessing());
831+
if (!spanInfo.isRequiresPostProcessing()) {
832+
ctx.close();
833+
}
837834
return NoopFlow.INSTANCE;
838835
}
839836

@@ -893,7 +890,13 @@ private void onRequestHeader(RequestContext ctx_, String name, String value) {
893890
}
894891

895892
private void onPostProcessing(RequestContext ctx_) {
896-
// Do AppSec post-processing
893+
AppSecRequestContext ctx = ctx_.getData(RequestContextSlot.APPSEC);
894+
if (ctx == null) {
895+
return;
896+
}
897+
898+
maybeExtractSchemas(ctx);
899+
ctx.close();
897900
}
898901

899902
public void stop() {

dd-java-agent/appsec/src/test/groovy/com/datadog/appsec/gateway/AppSecRequestContextSpecification.groovy

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,6 @@ class AppSecRequestContextSpecification extends DDSpecification {
249249
void 'test that internal data is cleared on close'() {
250250
setup:
251251
final ctx = new AppSecRequestContext()
252-
final fullCleanup = !postProcessing
253252

254253
when:
255254
ctx.requestHeaders.put('Accept', ['*'])
@@ -258,19 +257,17 @@ class AppSecRequestContextSpecification extends DDSpecification {
258257
ctx.persistentData.put(KnownAddresses.REQUEST_METHOD, 'GET')
259258
ctx.derivatives = ['a': 'b']
260259
ctx.additive = createAdditive()
261-
ctx.close(postProcessing)
260+
ctx.close()
262261

263262
then:
264263
ctx.additive == null
265264
ctx.derivatives == null
265+
ctx.additive == null
266266

267-
ctx.requestHeaders.isEmpty() == fullCleanup
268-
ctx.responseHeaders.isEmpty() == fullCleanup
269-
ctx.cookies.isEmpty() == fullCleanup
270-
ctx.persistentData.isEmpty() == fullCleanup
271-
272-
where:
273-
postProcessing << [true, false]
267+
ctx.requestHeaders.isEmpty()
268+
ctx.responseHeaders.isEmpty()
269+
ctx.cookies.isEmpty()
270+
ctx.persistentData.isEmpty()
274271
}
275272

276273
def "test increase and get WafTimeouts"() {

dd-trace-core/src/main/java/datadog/trace/core/CoreTracer.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,9 @@ void onRootSpanPublished(final AgentSpan root) {
276276
RequestContext requestContext = root.getRequestContext();
277277
if (requestContext != null) {
278278
try {
279-
requestContext.close();
279+
if (!root.isRequiresPostProcessing()) {
280+
requestContext.close();
281+
}
280282
} catch (IOException e) {
281283
log.warn("Error closing request context data", e);
282284
}

0 commit comments

Comments
 (0)