@@ -4,9 +4,11 @@ import com.datadog.appsec.event.EventProducerService
44import com.datadog.appsec.event.ExpiredSubscriberInfoException
55import com.datadog.appsec.event.data.KnownAddresses
66import com.datadog.appsec.gateway.AppSecRequestContext
7- import datadog.trace.api.gateway.RequestContext
7+ import datadog.trace.api.ProductTraceSource
8+ import datadog.trace.api.config.AppSecConfig
9+ import datadog.trace.api.config.GeneralConfig
810import datadog.trace.api.internal.TraceSegment
9- import datadog.trace.bootstrap.instrumentation.api.AgentSpan
11+ import datadog.trace.bootstrap.instrumentation.api.Tags
1012import datadog.trace.test.util.DDSpecification
1113
1214class ApiSecurityProcessorTest extends DDSpecification {
@@ -29,18 +31,14 @@ class ApiSecurityProcessorTest extends DDSpecification {
2931 1 * producer. getDataSubscribers(KnownAddresses . WAF_CONTEXT_PROCESSOR ) >> subInfo
3032 1 * subInfo. isEmpty() >> false
3133 1 * producer. publishDataEvent(_, ctx, _, _)
32- 1 * ctx. commitDerivatives(traceSegment)
33- 1 * ctx. closeWafContext()
34- 1 * ctx. close()
34+ 1 * traceSegment. setTagTop(' asm.keep' , true )
3535 0 * _
3636 }
3737
3838 void ' no schema extracted if sampling is false' () {
3939 given :
4040 def sampler = Mock (ApiSecuritySampler )
4141 def producer = Mock (EventProducerService )
42- def span = Mock (AgentSpan )
43- def reqCtx = Mock (RequestContext )
4442 def ctx = Mock (AppSecRequestContext )
4543 def traceSegment = Mock (TraceSegment )
4644 def processor = new ApiSecurityProcessor (sampler, producer)
@@ -50,45 +48,30 @@ class ApiSecurityProcessorTest extends DDSpecification {
5048
5149 then :
5250 noExceptionThrown()
53- 1 * span. getRequestContext() >> reqCtx
54- 1 * reqCtx. getData(_) >> ctx
5551 1 * sampler. sample(ctx) >> false
56- 1 * ctx. closeWafContext()
57- 1 * ctx. close()
5852 0 * _
5953 }
6054
61- void ' permit is released even if request context close throws ' () {
55+ void ' process null appsec request context does nothing ' () {
6256 given :
6357 def sampler = Mock (ApiSecuritySampler )
6458 def producer = Mock (EventProducerService )
65- def span = Mock (AgentSpan )
66- def reqCtx = Mock (RequestContext )
6759 def traceSegment = Mock (TraceSegment )
68- def ctx = Mock (AppSecRequestContext )
6960 def processor = new ApiSecurityProcessor (sampler, producer)
7061
7162 when :
72- processor. processTraceSegment(traceSegment, ctx , null )
63+ processor. processTraceSegment(traceSegment, null , null )
7364
7465 then :
7566 noExceptionThrown()
76- 1 * span. getRequestContext() >> reqCtx
77- 1 * reqCtx. getData(_) >> ctx
78- 1 * sampler. sample(ctx) >> true
79- 1 * reqCtx. getTraceSegment() >> traceSegment
80- 1 * producer. getDataSubscribers(_) >> null
81- 1 * ctx. closeWafContext()
82- 1 * ctx. close() >> { throw new RuntimeException () }
8367 0 * _
8468 }
8569
86- void ' context is cleaned up on timeout ' () {
70+ void ' empty event subscription does not break the process ' () {
8771 given :
8872 def sampler = Mock (ApiSecuritySampler )
8973 def producer = Mock (EventProducerService )
90- def span = Mock (AgentSpan )
91- def reqCtx = Mock (RequestContext )
74+ def subInfo = Mock (EventProducerService.DataSubscriberInfo )
9275 def traceSegment = Mock (TraceSegment )
9376 def ctx = Mock (AppSecRequestContext )
9477 def processor = new ApiSecurityProcessor (sampler, producer)
@@ -98,39 +81,17 @@ class ApiSecurityProcessorTest extends DDSpecification {
9881
9982 then :
10083 noExceptionThrown()
101- 1 * span. getRequestContext() >> reqCtx
102- 1 * reqCtx. getData(_) >> ctx
103- 1 * ctx. closeWafContext()
104- 1 * ctx. close()
105- 0 * _
106- }
107-
108- void ' process null appsec request context does nothing' () {
109- given :
110- def sampler = Mock (ApiSecuritySampler )
111- def producer = Mock (EventProducerService )
112- def span = Mock (AgentSpan )
113- def traceSegment = Mock (TraceSegment )
114- def reqCtx = Mock (RequestContext )
115- def processor = new ApiSecurityProcessor (sampler, producer)
116-
117- when :
118- processor. processTraceSegment(traceSegment, null , null )
119-
120- then :
121- noExceptionThrown()
122- 1 * span. getRequestContext() >> reqCtx
123- 1 * reqCtx. getData(_) >> null
84+ 1 * sampler. sample(ctx) >> true
85+ 1 * producer. getDataSubscribers(_) >> subInfo
86+ 1 * subInfo. isEmpty() >> true
12487 0 * _
12588 }
12689
127- void ' empty event subscription does not break the process' () {
90+ void ' expired event subscription does not break the process' () {
12891 given :
12992 def sampler = Mock (ApiSecuritySampler )
13093 def producer = Mock (EventProducerService )
13194 def subInfo = Mock (EventProducerService.DataSubscriberInfo )
132- def span = Mock (AgentSpan )
133- def reqCtx = Mock (RequestContext )
13495 def traceSegment = Mock (TraceSegment )
13596 def ctx = Mock (AppSecRequestContext )
13697 def processor = new ApiSecurityProcessor (sampler, producer)
@@ -140,42 +101,34 @@ class ApiSecurityProcessorTest extends DDSpecification {
140101
141102 then :
142103 noExceptionThrown()
143- 1 * span. getRequestContext() >> reqCtx
144- 1 * reqCtx. getData(_) >> ctx
145104 1 * sampler. sample(ctx) >> true
146- 1 * reqCtx. getTraceSegment() >> traceSegment
147105 1 * producer. getDataSubscribers(_) >> subInfo
148- 1 * subInfo. isEmpty() >> true
149- 1 * ctx. closeWafContext()
150- 1 * ctx. close()
106+ 1 * subInfo. isEmpty() >> false
107+ 1 * producer. publishDataEvent(_, ctx, _, _) >> { throw new ExpiredSubscriberInfoException () }
151108 0 * _
152109 }
153110
154- void ' expired event subscription does not break the process ' () {
111+ void ' test api security sampling with tracing disabled ' () {
155112 given :
113+ injectSysConfig(GeneralConfig . APM_TRACING_ENABLED , " false" )
114+ injectSysConfig(AppSecConfig . API_SECURITY_ENABLED , " true" )
156115 def sampler = Mock (ApiSecuritySampler )
157- def producer = Mock (EventProducerService )
158116 def subInfo = Mock (EventProducerService.DataSubscriberInfo )
159- def span = Mock (AgentSpan )
160- def reqCtx = Mock (RequestContext )
117+ def producer = Mock (EventProducerService )
161118 def traceSegment = Mock (TraceSegment )
162- def ctx = Mock (AppSecRequestContext )
163119 def processor = new ApiSecurityProcessor (sampler, producer)
120+ def ctx = Mock (AppSecRequestContext )
164121
165122 when :
166123 processor. processTraceSegment(traceSegment, ctx, null )
167124
168125 then :
169- noExceptionThrown()
170- 1 * span. getRequestContext() >> reqCtx
171- 1 * reqCtx. getData(_) >> ctx
172126 1 * sampler. sample(ctx) >> true
173- 1 * reqCtx. getTraceSegment() >> traceSegment
174- 1 * producer. getDataSubscribers(_) >> subInfo
127+ 1 * producer. getDataSubscribers(KnownAddresses . WAF_CONTEXT_PROCESSOR ) >> subInfo
175128 1 * subInfo. isEmpty() >> false
176- 1 * producer. publishDataEvent(_, ctx, _, _) >> { throw new ExpiredSubscriberInfoException () }
177- 1 * ctx . closeWafContext( )
178- 1 * ctx . close( )
129+ 1 * producer. publishDataEvent(_, ctx, _, _)
130+ 1 * traceSegment . setTagTop( ' asm.keep ' , true )
131+ 1 * traceSegment . setTagTop( Tags . PROPAGATED_TRACE_SOURCE , ProductTraceSource . ASM )
179132 0 * _
180133 }
181134}
0 commit comments