|
| 1 | +import datadog.smoketest.appsec.AbstractAppSecServerSmokeTest |
| 2 | +import datadog.trace.agent.test.utils.OkHttpUtils |
| 3 | +import groovy.json.JsonOutput |
| 4 | +import groovy.json.JsonSlurper |
| 5 | +import okhttp3.MediaType |
| 6 | +import okhttp3.Request |
| 7 | +import okhttp3.RequestBody |
| 8 | +import okhttp3.Response |
| 9 | + |
| 10 | +import java.util.zip.GZIPInputStream |
| 11 | + |
| 12 | +class AppSecRatpackSmokeTest extends AbstractAppSecServerSmokeTest { |
| 13 | + |
| 14 | + @Override |
| 15 | + ProcessBuilder createProcessBuilder() { |
| 16 | + String ratpackUberJar = System.getProperty("datadog.smoketest.ratpack.shadowJar.path") |
| 17 | + |
| 18 | + List<String> command = new ArrayList<>() |
| 19 | + command.add(javaPath()) |
| 20 | + command.addAll(defaultJavaProperties) |
| 21 | + command.addAll(defaultAppSecProperties) |
| 22 | + command.addAll((String[]) [ |
| 23 | + "-Ddd.writer.type=MultiWriter:TraceStructureWriter:${output.getAbsolutePath()},DDAgentWriter", |
| 24 | + "-Dratpack.http.port=${httpPort}", |
| 25 | + "-jar", |
| 26 | + ratpackUberJar |
| 27 | + ]) |
| 28 | + ProcessBuilder processBuilder = new ProcessBuilder(command) |
| 29 | + processBuilder.directory(new File(buildDirectory)) |
| 30 | + } |
| 31 | + |
| 32 | + @Override |
| 33 | + File createTemporaryFile() { |
| 34 | + return new File("${buildDirectory}/tmp/trace-structure-ratpack.out") |
| 35 | + } |
| 36 | + |
| 37 | + void 'API Security samples only one request per endpoint'() { |
| 38 | + given: |
| 39 | + def url = "http://localhost:${httpPort}/api_security/sampling/200?test=value" |
| 40 | + def client = OkHttpUtils.clientBuilder().build() |
| 41 | + def request = new Request.Builder() |
| 42 | + .url(url) |
| 43 | + .addHeader('X-My-Header', "value") |
| 44 | + .get() |
| 45 | + .build() |
| 46 | + |
| 47 | + when: |
| 48 | + List<Response> responses = (1..3).collect { |
| 49 | + client.newCall(request).execute() |
| 50 | + } |
| 51 | + |
| 52 | + then: |
| 53 | + responses.each { |
| 54 | + assert it.code() == 200 |
| 55 | + } |
| 56 | + waitForTraceCount(3) |
| 57 | + def spans = rootSpans.toList().toSorted { it.span.duration } |
| 58 | + spans.size() == 3 |
| 59 | + def sampledSpans = spans.findAll { |
| 60 | + it.meta.keySet().any { |
| 61 | + it.startsWith('_dd.appsec.s.req.') |
| 62 | + } |
| 63 | + } |
| 64 | + sampledSpans.size() == 1 |
| 65 | + def span = sampledSpans[0] |
| 66 | + span.meta.containsKey('_dd.appsec.s.req.query') |
| 67 | + span.meta.containsKey('_dd.appsec.s.req.params') |
| 68 | + span.meta.containsKey('_dd.appsec.s.req.headers') |
| 69 | + } |
| 70 | + |
| 71 | + void 'test response schema extraction'() { |
| 72 | + given: |
| 73 | + def url = "http://localhost:${httpPort}/api_security/response" |
| 74 | + def client = OkHttpUtils.clientBuilder().build() |
| 75 | + def body = [ |
| 76 | + "main" : [["key": "id001", "value": 1345.67], ["value": 1567.89, "key": "id002"]], |
| 77 | + "nullable": null, |
| 78 | + ] |
| 79 | + def request = new Request.Builder() |
| 80 | + .url(url) |
| 81 | + .post(RequestBody.create(MediaType.get('application/json'), JsonOutput.toJson(body))) |
| 82 | + .build() |
| 83 | + |
| 84 | + when: |
| 85 | + final response = client.newCall(request).execute() |
| 86 | + waitForTraceCount(1) |
| 87 | + |
| 88 | + then: |
| 89 | + response.code() == 200 |
| 90 | + def span = rootSpans.first() |
| 91 | + span.meta.containsKey('_dd.appsec.s.res.headers') |
| 92 | + span.meta.containsKey('_dd.appsec.s.res.body') |
| 93 | + final schema = new JsonSlurper().parse(unzip(span.meta.get('_dd.appsec.s.res.body'))) |
| 94 | + assert schema == [["main": [[[["key": [8], "value": [16]]]], ["len": 2]], "nullable": [1]]] |
| 95 | + } |
| 96 | + |
| 97 | + private static byte[] unzip(final String text) { |
| 98 | + final inflaterStream = new GZIPInputStream(new ByteArrayInputStream(text.decodeBase64())) |
| 99 | + return inflaterStream.getBytes() |
| 100 | + } |
| 101 | +} |
0 commit comments