Skip to content

Commit

Permalink
add tests to validate system tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jandro996 committed Jun 17, 2024
1 parent 5684500 commit d71fbfd
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@
import io.opentracing.Span;
import io.opentracing.util.GlobalTracer;
import java.io.IOException;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

@RestController
Expand All @@ -30,6 +29,12 @@ public String greetings(
return "Hello I'm service " + System.getProperty("dd.service.name");
}

@GetMapping(value = "/returnheaders", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Map<String, String>> returnheaders(
@RequestHeader Map<String, String> headers) {
return ResponseEntity.ok(headers);
}

@GetMapping("/appsec/{id}")
public String pathParam(
@PathVariable("id") String id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@ abstract class AbstractAsmStandaloneBillingSmokeTest extends AbstractServerSmoke
}
}

protected DecodedTrace getServiceTraceFromUrl(String url) {
return traces.find { trace ->
trace.spans.find { span ->
span.meta["http.url"] == url
}
}
}

protected checkRootSpanPrioritySampling(DecodedTrace trace, byte priority) {
return trace.spans[0].metrics['_sampling_priority_v1'] == priority
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package datadog.smoketest.asmstandalonebilling

import datadog.trace.api.sampling.PrioritySampling
import groovy.json.JsonSlurper
import okhttp3.Request

class AsmStandaloneBillingSamplingSmokeTest extends AbstractAsmStandaloneBillingSmokeTest {
Expand All @@ -10,6 +11,7 @@ class AsmStandaloneBillingSamplingSmokeTest extends AbstractAsmStandaloneBilling
final String[] processProperties = [
"-Ddd.experimental.appsec.standalone.enabled=true",
"-Ddd.iast.enabled=true",
"-Ddd.appsec.enabled=true",
"-Ddd.iast.detection.mode=FULL",
"-Ddd.iast.debug.enabled=true",
"-Ddd.service.name=asm-standalone-billing-sampling-spring-smoketest-app",
Expand Down Expand Up @@ -66,4 +68,68 @@ class AsmStandaloneBillingSamplingSmokeTest extends AbstractAsmStandaloneBilling
checkRootSpanPrioritySampling(traces[3], PrioritySampling.SAMPLER_DROP)
!hasAppsecPropagationTag(traces[3])
}

void 'test propagation in single process'(){
setup:
final downstreamUrl = "http://localhost:${httpPorts[0]}/rest-api/greetings"
final standAloneBillingUrl = "http://localhost:${httpPorts[0]}/rest-api/iast?injection=vulnerable%26url=${downstreamUrl}"
final upstreamUrl = "http://localhost:${httpPorts[0]}/rest-api/greetings?&url=${standAloneBillingUrl}"
final request = new Request.Builder().url(upstreamUrl).get().build()

when:
final response = client.newCall(request).execute()

then:
response.successful
waitForTraceCount(3)

and: 'No upstream ASM events'
def upstreamTrace = getServiceTraceFromUrl(upstreamUrl)
isSampledBySampler (upstreamTrace)
!hasAppsecPropagationTag (upstreamTrace)
hasApmDisabledTag (upstreamTrace)
def upstreamTraceId = upstreamTrace.spans[0].traceId

and: 'ASM events, resulting in force keep and appsec propagation'
def standAloneBillingTrace = getServiceTraceFromUrl("http://localhost:${httpPorts[0]}/rest-api/iast?injection=vulnerable&url=http://localhost:${httpPorts[0]}/rest-api/greetings")
checkRootSpanPrioritySampling(standAloneBillingTrace, PrioritySampling.USER_KEEP)
hasAppsecPropagationTag (standAloneBillingTrace)
hasApmDisabledTag (standAloneBillingTrace)
def standAloneBillingTraceId = standAloneBillingTrace.spans[0].traceId
upstreamTraceId != standAloneBillingTraceId //There is no propagation!!!!

and: 'Default APM distributed tracing behavior with'
def downstreamTrace = getServiceTraceFromUrl(downstreamUrl)
checkRootSpanPrioritySampling(downstreamTrace, PrioritySampling.USER_KEEP)
hasAppsecPropagationTag (downstreamTrace)
hasApmDisabledTag (downstreamTrace)
def downstreamTraceId = downstreamTrace.spans[0].traceId
standAloneBillingTraceId == downstreamTraceId //There is propagation
}

void 'test propagation simulating 3 process'(){
setup:
def jsonSlurper = new JsonSlurper()
final trace_id = "1212121212121212121"
final parent_id = "34343434"
final url = "http://localhost:${httpPorts[0]}/rest-api/appsec/appscan_fingerprint?url=http://localhost:${httpPorts[0]}/rest-api/returnheaders"
final request = new Request.Builder()
.url(url)
.header("x-datadog-trace-id", trace_id)
.header("x-datadog-parent-id", parent_id)
.header("x-datadog-origin", "rum")
.header("x-datadog-sampling-priority", "1")
.get().build()

when:
final response = client.newCall(request).execute()

then:
response.successful
waitForTraceCount(2)
def downstreamTrace = getServiceTraceFromUrl("http://localhost:${httpPorts[0]}/rest-api/returnheaders")
checkRootSpanPrioritySampling(downstreamTrace, PrioritySampling.USER_KEEP)
def downstreamHeaders = jsonSlurper.parseText(response.body().string())
downstreamHeaders["x-datadog-sampling-priority"] == "2"
}
}

0 comments on commit d71fbfd

Please sign in to comment.