29
29
import com .datadog .appsec .util .StandardizedLogging ;
30
30
import com .datadog .ddwaf .WafBuilder ;
31
31
import com .datadog .ddwaf .exception .InvalidRuleSetException ;
32
+ import datadog .remoteconfig .ConfigurationChangesTypedListener ;
32
33
import datadog .remoteconfig .ConfigurationEndListener ;
33
34
import datadog .remoteconfig .ConfigurationPoller ;
34
35
import datadog .remoteconfig .Product ;
@@ -131,75 +132,85 @@ private void subscribeConfigurationPoller(WafBuilder wafBuilder) {
131
132
132
133
private void subscribeRulesAndData (WafBuilder wafBuilder ) {
133
134
this .configurationPoller .addListener (
134
- Product .ASM_DD ,
135
- AppSecConfigDeserializer .INSTANCE ,
136
- (configKey , newConfig , hinter ) -> {
137
- // read initialized so that the state is currentAppSecConfig is visible
138
- if (!initialized ) {
139
- throw new IllegalStateException ();
140
- }
141
- if (newConfig == null || newConfig .getRawConfig () == null ) {
142
- log .debug ("AppSec config given by remote config was pulled. Removing WAF config" );
143
- wafBuilder .removeConfig (configKey );
144
- } else {
145
- if (defaultConfigActivated ) {
146
- log .debug ("Removing default config" );
147
- wafBuilder .removeConfig (DEFAULT_WAF_CONFIG_RULE );
148
- defaultConfigActivated = false ;
149
- }
150
- try {
151
- wafBuilder .addOrUpdateConfig (configKey , newConfig .getRawConfig ());
152
- } catch (InvalidRuleSetException e ) {
153
- throw new RuntimeException (e );
154
- }
155
- }
156
- this .currentAppSecConfig .setDdConfig (newConfig );
157
- // base rules can contain all rules/data/exclusions/etc
158
- this .currentAppSecConfig .dirtyStatus .markAllDirty ();
159
- });
135
+ Product .ASM_DD , AppSecConfigDeserializer .INSTANCE , asmDDTypedListener (wafBuilder ));
160
136
this .configurationPoller .addListener (
161
- Product .ASM_DATA ,
162
- AppSecDataDeserializer .INSTANCE ,
163
- (configKey , newConfig , hinter ) -> {
164
- if (!initialized ) {
165
- throw new IllegalStateException ();
166
- }
167
- if (newConfig == null ) {
168
- currentAppSecConfig .mergedAsmData .removeConfig (configKey );
169
- wafBuilder .removeConfig (configKey );
170
- } else {
171
- currentAppSecConfig .mergedAsmData .addConfig (configKey , newConfig );
172
- try {
173
- wafBuilder .addOrUpdateConfig (configKey , newConfig .getRawConfig ());
174
- } catch (InvalidRuleSetException e ) {
175
- throw new RuntimeException (e );
176
- }
177
- }
178
- this .currentAppSecConfig .dirtyStatus .data = true ;
179
- });
137
+ Product .ASM_DATA , AppSecDataDeserializer .INSTANCE , asmDataTypedListener (wafBuilder ));
180
138
this .configurationPoller .addListener (
181
- Product .ASM ,
182
- AppSecUserConfigDeserializer .INSTANCE ,
183
- (configKey , newConfig , hinter ) -> {
184
- if (!initialized ) {
185
- throw new IllegalStateException ();
186
- }
187
- DirtyStatus dirtyStatus ;
188
- if (newConfig == null ) {
189
- dirtyStatus = currentAppSecConfig .userConfigs .removeConfig (configKey );
190
- wafBuilder .removeConfig (configKey );
191
- } else {
192
- AppSecUserConfig userCfg = newConfig .build (configKey );
193
- dirtyStatus = currentAppSecConfig .userConfigs .addConfig (userCfg );
194
- try {
195
- wafBuilder .addOrUpdateConfig (configKey , newConfig .getRawConfig ());
196
- } catch (InvalidRuleSetException e ) {
197
- throw new RuntimeException (e );
198
- }
199
- }
139
+ Product .ASM , AppSecUserConfigDeserializer .INSTANCE , asmTypedListener (wafBuilder ));
140
+ }
200
141
201
- this .currentAppSecConfig .dirtyStatus .mergeFrom (dirtyStatus );
202
- });
142
+ private ConfigurationChangesTypedListener <AppSecUserConfig .Builder > asmTypedListener (
143
+ WafBuilder wafBuilder ) {
144
+ return (configKey , newConfig , hinter ) -> {
145
+ if (!initialized ) {
146
+ throw new IllegalStateException ();
147
+ }
148
+ DirtyStatus dirtyStatus ;
149
+ if (newConfig == null ) {
150
+ dirtyStatus = currentAppSecConfig .getUserConfigs ().removeConfig (configKey );
151
+ wafBuilder .removeConfig (configKey );
152
+ } else {
153
+ AppSecUserConfig userCfg = newConfig .build (configKey );
154
+ dirtyStatus = currentAppSecConfig .getUserConfigs ().addConfig (userCfg );
155
+ try {
156
+ wafBuilder .addOrUpdateConfig (configKey , newConfig .getRawConfig ());
157
+ } catch (InvalidRuleSetException e ) {
158
+ throw new RuntimeException (e );
159
+ }
160
+ }
161
+
162
+ this .currentAppSecConfig .dirtyStatus .mergeFrom (dirtyStatus );
163
+ };
164
+ }
165
+
166
+ private ConfigurationChangesTypedListener <AppSecData > asmDataTypedListener (
167
+ WafBuilder wafBuilder ) {
168
+ return (configKey , newConfig , hinter ) -> {
169
+ if (!initialized ) {
170
+ throw new IllegalStateException ();
171
+ }
172
+ if (newConfig == null ) {
173
+ currentAppSecConfig .mergedAsmData .removeConfig (configKey );
174
+ wafBuilder .removeConfig (configKey );
175
+ } else {
176
+ currentAppSecConfig .mergedAsmData .addConfig (configKey , newConfig );
177
+ try {
178
+ wafBuilder .addOrUpdateConfig (configKey , newConfig .getRawConfig ());
179
+ } catch (InvalidRuleSetException e ) {
180
+ log .debug ("Could not add or update config {}, {}" , configKey , e .ruleSetInfo );
181
+ throw new RuntimeException (e );
182
+ }
183
+ }
184
+ this .currentAppSecConfig .dirtyStatus .data = true ;
185
+ };
186
+ }
187
+
188
+ private ConfigurationChangesTypedListener <AppSecConfig > asmDDTypedListener (
189
+ WafBuilder wafBuilder ) {
190
+ return (configKey , newConfig , hinter ) -> {
191
+ // read initialized so that the state is currentAppSecConfig is visible
192
+ if (!initialized ) {
193
+ throw new IllegalStateException ();
194
+ }
195
+ if (newConfig == null || newConfig .getRawConfig () == null ) {
196
+ log .debug ("AppSec config given by remote config was pulled. Removing WAF config" );
197
+ wafBuilder .removeConfig (configKey );
198
+ } else {
199
+ if (defaultConfigActivated ) {
200
+ log .debug ("Removing default config" );
201
+ wafBuilder .removeConfig (DEFAULT_WAF_CONFIG_RULE );
202
+ defaultConfigActivated = false ;
203
+ }
204
+ try {
205
+ wafBuilder .addOrUpdateConfig (configKey , newConfig .getRawConfig ());
206
+ } catch (InvalidRuleSetException e ) {
207
+ throw new RuntimeException (e );
208
+ }
209
+ }
210
+ this .currentAppSecConfig .setDdConfig (newConfig );
211
+ // base rules can contain all rules/data/exclusions/etc
212
+ this .currentAppSecConfig .dirtyStatus .markAllDirty ();
213
+ };
203
214
}
204
215
205
216
private void subscribeAsmFeatures () {
@@ -213,6 +224,9 @@ private void subscribeAsmFeatures() {
213
224
if (newConfig == null ) {
214
225
mergedAsmFeatures .removeConfig (configKey );
215
226
} else {
227
+ if (!configKey .equals (CurrentAppSecConfig .DEFAULT_KEY )) {
228
+ mergedAsmFeatures .removeConfig (CurrentAppSecConfig .DEFAULT_KEY );
229
+ }
216
230
mergedAsmFeatures .addConfig (configKey , newConfig );
217
231
}
218
232
});
0 commit comments