Skip to content

Commit 1ed8916

Browse files
Fix more tests
1 parent b474849 commit 1ed8916

File tree

11 files changed

+38
-34
lines changed

11 files changed

+38
-34
lines changed

dd-java-agent/appsec/src/main/java/com/datadog/appsec/AppSecSystem.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.datadog.appsec;
22

33
import com.datadog.appsec.api.security.ApiSecurityDownstreamSampler;
4+
import com.datadog.appsec.api.security.ApiSecurityDownstreamSamplerImpl;
45
import com.datadog.appsec.api.security.ApiSecuritySampler;
56
import com.datadog.appsec.api.security.ApiSecuritySamplerImpl;
67
import com.datadog.appsec.api.security.AppSecSpanPostProcessor;
@@ -218,7 +219,7 @@ private static void maybeInitializeApiSecurity() {
218219
API_SECURITY_SAMPLER = requestSampler;
219220

220221
final double rate = Config.get().getApiSecurityDownstreamRequestAnalysisSampleRate();
221-
API_SECURITY_DOWNSTREAM_SAMPLER = ApiSecurityDownstreamSampler.build(rate);
222+
API_SECURITY_DOWNSTREAM_SAMPLER = new ApiSecurityDownstreamSamplerImpl(rate);
222223
}
223224
}
224225
}

dd-java-agent/appsec/src/main/java/com/datadog/appsec/api/security/ApiSecurityDownstreamSampler.java

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,4 @@ public boolean isSampled(AppSecRequestContext ctx, long requestId) {
2020
return false;
2121
}
2222
}
23-
24-
static ApiSecurityDownstreamSampler build(double rate) {
25-
if (rate < 0.0D) {
26-
rate = 0.D;
27-
} else if (rate > 1.0D) {
28-
rate = 1.0D;
29-
}
30-
return new ApiSecurityDownstreamSamplerImpl(rate);
31-
}
3223
}

dd-java-agent/appsec/src/main/java/com/datadog/appsec/api/security/ApiSecurityDownstreamSamplerImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public class ApiSecurityDownstreamSamplerImpl implements ApiSecurityDownstreamSa
1212
private final double threshold;
1313

1414
public ApiSecurityDownstreamSamplerImpl(double rate) {
15-
threshold = samplingCutoff(rate);
15+
threshold = samplingCutoff(rate < 0.0 ? 0 : (rate > 1.0 ? 1 : rate));
1616
}
1717

1818
private static double samplingCutoff(double rate) {

dd-java-agent/appsec/src/main/java/com/datadog/appsec/event/data/KnownAddresses.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ public interface KnownAddresses {
129129
Address<Object> IO_NET_REQUEST_BODY = new Address<>("server.io.net.request.body");
130130

131131
/** The status of a network resource being requested (outgoing request) */
132-
Address<Integer> IO_NET_RESPONSE_STATUS = new Address<>("server.io.net.response.status");
132+
Address<String> IO_NET_RESPONSE_STATUS = new Address<>("server.io.net.response.status");
133133

134134
/** The response headers of a network resource being requested (outgoing request) */
135135
Address<Map<String, List<String>>> IO_NET_RESPONSE_HEADERS =

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,8 @@ public class AppSecRequestContext implements DataBundle, Closeable {
149149
private volatile Long apiSecurityEndpointHash;
150150
private volatile byte keepType = PrioritySampling.SAMPLER_KEEP;
151151

152-
private static final AtomicInteger httpClientRequestCount = new AtomicInteger(0);
153-
private static final Set<Long> sampledHttpClientRequests = new HashSet<>();
152+
private final AtomicInteger httpClientRequestCount = new AtomicInteger(0);
153+
private final Set<Long> sampledHttpClientRequests = new HashSet<>();
154154

155155
private static final AtomicIntegerFieldUpdater<AppSecRequestContext> WAF_TIMEOUTS_UPDATER =
156156
AtomicIntegerFieldUpdater.newUpdater(AppSecRequestContext.class, "wafTimeouts");

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ private Flow<Void> onHttpClientResponse(RequestContext ctx_, HttpClientResponse
386386

387387
final MapDataBundle.Builder bundleBuilder =
388388
new MapDataBundle.Builder(CAPACITY_3_4)
389-
.add(KnownAddresses.IO_NET_RESPONSE_STATUS, response.getStatus())
389+
.add(KnownAddresses.IO_NET_RESPONSE_STATUS, Integer.toString(response.getStatus()))
390390
.add(KnownAddresses.IO_NET_RESPONSE_HEADERS, response.getHeaders());
391391
// ignore the response if not sampled
392392
final ApiSecurityDownstreamSampler sampler = downstreamSamplerSupplier.get();

dd-java-agent/appsec/src/main/java/com/datadog/appsec/util/BodyParser.java

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ static BodyParser forMediaType(final MediaType type) {
3333
return null;
3434
}
3535

36+
static BodyParser forMediaType(final String type) {
37+
return forMediaType(MediaType.parse(type));
38+
}
39+
3640
class State {
3741
private int elemsLeft = MAX_ELEMENTS;
3842
public boolean objectTooDeep = false;
@@ -62,6 +66,7 @@ public BoundedObjectAdapter(final State state) {
6266
this.state = state;
6367
}
6468

69+
@Generated
6570
@Override
6671
public void toJson(final JsonWriter writer, @Nullable final Object value) throws IOException {
6772
throw new UnsupportedOperationException("Parsing-only adapter");
@@ -79,18 +84,16 @@ private Object readValue(final JsonReader r, final int depth) throws IOException
7984
return null;
8085
}
8186

82-
if (state.elemsLeft == 0) {
87+
if (state.elemsLeft-- == 0) {
8388
state.listMapTooLarge = true;
8489
r.skipValue();
8590
return null;
8691
}
87-
state.elemsLeft--;
8892

8993
switch (r.peek()) {
9094
case BEGIN_OBJECT:
9195
return readObject(r, depth);
9296
case BEGIN_ARRAY:
93-
state.elemsLeft--;
9497
return readArray(r, depth);
9598
case STRING:
9699
String value = r.nextString();
@@ -116,12 +119,9 @@ private Map<String, Object> readObject(final JsonReader r, final int depth)
116119
r.beginObject();
117120
while (r.hasNext()) {
118121
String name = r.nextName();
119-
if (state.elemsLeft > 0) {
120-
Object val = readValue(r, depth + 1);
122+
Object val = readValue(r, depth + 1);
123+
if (!state.listMapTooLarge) {
121124
map.put(name, val);
122-
} else {
123-
state.listMapTooLarge = true;
124-
r.skipValue();
125125
}
126126
}
127127
r.endObject();
@@ -132,11 +132,9 @@ private List<Object> readArray(final JsonReader r, final int depth) throws IOExc
132132
List<Object> list = new ArrayList<>();
133133
r.beginArray();
134134
while (r.hasNext()) {
135-
if (state.elemsLeft > 0) {
136-
list.add(readValue(r, depth + 1));
137-
} else {
138-
state.listMapTooLarge = true;
139-
r.skipValue();
135+
Object value = readValue(r, depth + 1);
136+
if (!state.listMapTooLarge) {
137+
list.add(value);
140138
}
141139
}
142140
r.endArray();

dd-java-agent/appsec/src/test/groovy/com/datadog/appsec/api/security/ApiSecurityDownstreamSamplerTest.groovy

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,21 @@ class ApiSecurityDownstreamSamplerTest extends DDSpecification {
2626
void 'test sampling algorithm'() {
2727
given:
2828
final epsilon = 0.05
29+
final expectedRate = rate < 0 ? 0.0 : rate > 1 ? 1.0 : rate
2930
final ctx = Mock(AppSecRequestContext) {
3031
sampleHttpClientRequest(_ as long) >> true
3132
isHttpClientRequestSampled(_ as long) >> true
3233
}
33-
final sampler = ApiSecurityDownstreamSampler.build(expectedRate)
34+
final sampler = new ApiSecurityDownstreamSamplerImpl(rate)
3435

3536
when:
3637
final samples = (1..100).collect { sampler.sampleHttpClientRequest(ctx, it)}
3738

3839
then:
39-
final rate = samples.count { it } / samples.size()
40-
rate.subtract(expectedRate).abs() <= epsilon
40+
final receivedRate = samples.count { it } / samples.size()
41+
receivedRate.subtract(expectedRate).abs() <= epsilon
4142

4243
where:
43-
expectedRate << [0.0, 0.1, 0.25, 0.5, 0.75, 0.9, 1.0]
44+
rate << [-1.0, 0.0, 0.1, 0.25, 0.5, 0.75, 0.9, 1.0, 2.0]
4445
}
4546
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -924,7 +924,7 @@ class GatewayBridgeSpecification extends DDSpecification {
924924
1 * eventDispatcher.publishDataEvent(nonEmptyDsInfo, ctx.data, _ as DataBundle, _ as GatewayContext) >>
925925
{ a, b, db, gw -> bundle = db; gatewayContext = gw; NoopFlow.INSTANCE }
926926
bundle.size() == (sampled ? 3 : 2)
927-
bundle.get(KnownAddresses.IO_NET_RESPONSE_STATUS) == status
927+
bundle.get(KnownAddresses.IO_NET_RESPONSE_STATUS) == Integer.toString(status)
928928
bundle.get(KnownAddresses.IO_NET_RESPONSE_HEADERS) == headers
929929
if (sampled) {
930930
bundle.get(KnownAddresses.IO_NET_RESPONSE_BODY) == ['Hello': 'World!']

dd-java-agent/appsec/src/test/groovy/com/datadog/appsec/util/BodyParserSpecification.groovy

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,19 @@ import java.nio.charset.StandardCharsets
77

88
class BodyParserSpecification extends Specification {
99

10+
void 'test body parser per media type'() {
11+
when:
12+
final result = BodyParser.forMediaType(media)
13+
14+
then:
15+
(result != null) == nonNull
16+
17+
where:
18+
media | nonNull
19+
'application/json' | true
20+
'application/xml' | false
21+
}
22+
1023
void 'test parse simple JSON object'() {
1124
given:
1225
def parser = BodyParser.forJson()

0 commit comments

Comments
 (0)