Skip to content

Commit cd6b248

Browse files
Fix failing tests
1 parent 24289d1 commit cd6b248

File tree

2 files changed

+26
-10
lines changed

2 files changed

+26
-10
lines changed

dd-java-agent/appsec/src/main/java/com/datadog/appsec/config/AppSecConfigServiceImpl.java

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -204,18 +204,19 @@ private class AppSecConfigChangesListener implements ProductListener {
204204
@Override
205205
public void accept(ConfigKey configKey, byte[] content, PollingRateHinter pollingRateHinter)
206206
throws IOException {
207-
final String key = configKey.toString();
208207
if (content == null) {
209208
remove(configKey, pollingRateHinter);
210209
return;
211210
}
211+
final String key = configKey.toString();
212212
Map<String, Object> contentMap =
213213
ADAPTER.fromJson(Okio.buffer(Okio.source(new ByteArrayInputStream(content))));
214214
if (contentMap == null || contentMap.isEmpty()) {
215215
ignoredConfigKeys.add(key);
216216
} else {
217217
ignoredConfigKeys.remove(key);
218218
try {
219+
beforeApply(key, contentMap);
219220
maybeInitializeDefaultConfig();
220221
handleWafUpdateResultReport(key, contentMap);
221222
} catch (AppSecModule.AppSecModuleActivationException e) {
@@ -234,6 +235,7 @@ public void remove(ConfigKey configKey, PollingRateHinter pollingRateHinter)
234235
try {
235236
maybeInitializeDefaultConfig();
236237
wafBuilder.removeConfig(key);
238+
afterRemove(key);
237239
} catch (UnclassifiedWafException e) {
238240
throw new RuntimeException(e);
239241
}
@@ -243,12 +245,15 @@ public void remove(ConfigKey configKey, PollingRateHinter pollingRateHinter)
243245
public void commit(PollingRateHinter pollingRateHinter) {
244246
// no action needed
245247
}
248+
249+
protected void beforeApply(final String key, final Map<String, Object> contentMap) {}
250+
251+
protected void afterRemove(final String key) {}
246252
}
247253

248254
private class AppSecConfigChangesDDListener extends AppSecConfigChangesListener {
249255
@Override
250-
public void accept(ConfigKey configKey, byte[] content, PollingRateHinter pollingRateHinter)
251-
throws IOException {
256+
protected void beforeApply(final String key, final Map<String, Object> config) {
252257
if (defaultConfigActivated) { // if we get any config, remove the default one
253258
log.debug("Removing default config");
254259
try {
@@ -258,15 +263,12 @@ public void accept(ConfigKey configKey, byte[] content, PollingRateHinter pollin
258263
}
259264
defaultConfigActivated = false;
260265
}
261-
usedDDWafConfigKeys.add(configKey.toString());
262-
super.accept(configKey, content, pollingRateHinter);
266+
usedDDWafConfigKeys.add(key);
263267
}
264268

265269
@Override
266-
public void remove(ConfigKey configKey, PollingRateHinter pollingRateHinter)
267-
throws IOException {
268-
super.remove(configKey, pollingRateHinter);
269-
usedDDWafConfigKeys.remove(configKey.toString());
270+
protected void afterRemove(final String key) {
271+
usedDDWafConfigKeys.remove(key);
270272
}
271273
}
272274

dd-java-agent/appsec/src/test/groovy/com/datadog/appsec/ddwaf/WAFModuleSpecification.groovy

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

3+
import com.datadog.appsec.AppSecSystem
34
import com.datadog.appsec.config.AppSecConfigService
45
import com.datadog.appsec.config.AppSecConfigServiceImpl
56
import com.datadog.appsec.config.AppSecModuleConfigurer
@@ -30,6 +31,8 @@ import com.squareup.moshi.Types
3031
import datadog.appsec.api.blocking.BlockingContentType
3132
import datadog.communication.monitor.Monitoring
3233
import datadog.remoteconfig.ConfigurationPoller
34+
import datadog.remoteconfig.PollerRequestFactory
35+
import datadog.remoteconfig.PollingRateHinter
3336
import datadog.remoteconfig.Product
3437
import datadog.remoteconfig.state.ConfigKey
3538
import datadog.remoteconfig.state.ParsedConfigKey
@@ -97,6 +100,7 @@ class WAFModuleSpecification extends DDSpecification {
97100
void setup() {
98101
WafMetricCollector.INSTANCE = wafMetricCollector
99102
AgentTracer.forceRegister(tracer)
103+
AppSecSystem.active = true
100104

101105
final configurationPoller = Stub(ConfigurationPoller) {
102106
addListener(Product.ASM_DD, _ as ProductListener) >> {
@@ -972,7 +976,7 @@ class WAFModuleSpecification extends DDSpecification {
972976
973977
void 'configuration can be given later'() {
974978
when:
975-
initialRuleAddWithMap([waf: null])
979+
initialRuleAddWithMap([waf: new BadConfig()]) // empty configs are allowed now
976980
977981
then:
978982
thrown RuntimeException
@@ -1695,4 +1699,14 @@ class WAFModuleSpecification extends DDSpecification {
16951699
throw new IllegalStateException("Unhandled WafErrorCode: $code")
16961700
}
16971701
}
1702+
1703+
private static class BadConfig implements Map<String, Object> {
1704+
@Delegate
1705+
private Map<String, Object> delegate
1706+
1707+
@Override
1708+
Set entrySet() {
1709+
throw new IllegalStateException("You tried to iterate!")
1710+
}
1711+
}
16981712
}

0 commit comments

Comments
 (0)