2121import static datadog .remoteconfig .Capabilities .CAPABILITY_ASM_TRUSTED_IPS ;
2222import static datadog .remoteconfig .Capabilities .CAPABILITY_ASM_USER_BLOCKING ;
2323import static datadog .remoteconfig .Capabilities .CAPABILITY_ENDPOINT_FINGERPRINT ;
24- import static datadog .trace .logging .LogLevel .ERROR ;
2524
2625import com .datadog .appsec .AppSecModule ;
2726import com .datadog .appsec .AppSecSystem ;
4847import datadog .trace .api .Config ;
4948import datadog .trace .api .ProductActivation ;
5049import datadog .trace .api .UserIdCollectionMode ;
51- import datadog .trace .api .telemetry .LogCollector ;
5250import java .io .ByteArrayInputStream ;
5351import java .io .FileInputStream ;
5452import java .io .FileNotFoundException ;
6260import java .util .Map ;
6361import java .util .Set ;
6462import java .util .concurrent .ConcurrentHashMap ;
63+
64+ import datadog .trace .api .telemetry .LogCollector ;
6565import okio .Okio ;
6666import org .slf4j .Logger ;
6767import org .slf4j .LoggerFactory ;
@@ -101,10 +101,6 @@ public class AppSecConfigServiceImpl implements AppSecConfigService {
101101 private final String DEFAULT_WAF_CONFIG_RULE = "DEFAULT_WAF_CONFIG" ;
102102 private String currentRuleVersion ;
103103 private List <AppSecModule > modulesToUpdateVersionIn ;
104- private final LogCollector telemetryLogger = LogCollector .get ();
105-
106- Moshi moshi = new Moshi .Builder ().build ();
107- JsonAdapter <Map > mapToJsonAdapter ;
108104
109105 public AppSecConfigServiceImpl (
110106 Config tracerConfig ,
@@ -117,7 +113,6 @@ public AppSecConfigServiceImpl(
117113 if (tracerConfig .isAppSecWafMetrics ()) {
118114 traceSegmentPostProcessors .add (statsReporter );
119115 }
120- mapToJsonAdapter = moshi .adapter (Map .class );
121116 }
122117
123118 private void subscribeConfigurationPoller () {
@@ -161,11 +156,11 @@ private void subscribeConfigurationPoller() {
161156 }
162157
163158 private void subscribeRulesAndData () {
164- this .configurationPoller .addListener (Product .ASM_DD , new AsmDDTypedListener (Product . ASM_DD ));
159+ this .configurationPoller .addListener (Product .ASM_DD , new AsmDDTypedListener ());
165160 this .configurationPoller .addListener (
166- Product .ASM_DATA , new AppSecConfigConfigurationChangesTypedListener (Product . ASM_DATA ));
161+ Product .ASM_DATA , new AppSecConfigConfigurationChangesTypedListener ());
167162 this .configurationPoller .addListener (
168- Product .ASM , new AppSecConfigConfigurationChangesTypedListener (Product . ASM ));
163+ Product .ASM , new AppSecConfigConfigurationChangesTypedListener ());
169164 }
170165
171166 public void modulesToUpdateVersionIn (List <AppSecModule > modules ) {
@@ -177,12 +172,6 @@ public String getCurrentRuleVersion() {
177172 }
178173
179174 private class AppSecConfigConfigurationChangesTypedListener implements ProductListener {
180- private Product productType ;
181-
182- public AppSecConfigConfigurationChangesTypedListener (Product product ) {
183- this .productType = product ;
184- }
185-
186175 @ Override
187176 public void accept (ConfigKey configKey , byte [] content , PollingRateHinter pollingRateHinter )
188177 throws IOException {
@@ -200,7 +189,7 @@ public void accept(ConfigKey configKey, byte[] content, PollingRateHinter pollin
200189 Map <String , Object > contentMap =
201190 ADAPTER .fromJson (Okio .buffer (Okio .source (new ByteArrayInputStream (content ))));
202191 try {
203- handleWafUpdateResultReport (configKey .toString (), contentMap , productType );
192+ handleWafUpdateResultReport (configKey .toString (), contentMap );
204193 } catch (AppSecModule .AppSecModuleActivationException e ) {
205194 throw new RuntimeException (e );
206195 }
@@ -220,10 +209,6 @@ public void commit(PollingRateHinter pollingRateHinter) {
220209 }
221210
222211 private class AsmDDTypedListener extends AppSecConfigConfigurationChangesTypedListener {
223- public AsmDDTypedListener (Product product ) {
224- super (product );
225- }
226-
227212 @ Override
228213 public void accept (ConfigKey configKey , byte [] content , PollingRateHinter pollingRateHinter )
229214 throws IOException {
@@ -237,11 +222,7 @@ public void accept(ConfigKey configKey, byte[] content, PollingRateHinter pollin
237222 defaultConfigActivated = false ;
238223 }
239224 super .accept (configKey , content , pollingRateHinter );
240- if (content == null ) {
241- usedDDWafConfigKeys .remove (configKey .toString ());
242- } else {
243- usedDDWafConfigKeys .add (configKey .toString ());
244- }
225+ usedDDWafConfigKeys .add (configKey .toString ());
245226 }
246227
247228 @ Override
@@ -252,8 +233,7 @@ public void remove(ConfigKey configKey, PollingRateHinter pollingRateHinter)
252233 }
253234 }
254235
255- private void handleWafUpdateResultReport (
256- String configKey , Map <String , Object > rawConfig , Product productType )
236+ private void handleWafUpdateResultReport (String configKey , Map <String , Object > rawConfig )
257237 throws AppSecModule .AppSecModuleActivationException {
258238 wafBuilder = getWafBuilder ();
259239 if (modulesToUpdateVersionIn != null
@@ -267,6 +247,9 @@ private void handleWafUpdateResultReport(
267247 StandardizedLogging .numLoadedRules (log , configKey , countRules (rawConfig ));
268248 }
269249
250+ // TODO: Send diagnostics via telemetry
251+ final LogCollector telemetryLogger = LogCollector .get ();
252+
270253 initReporter .setReportForPublication (wafDiagnostics );
271254 if (wafDiagnostics .rulesetVersion != null
272255 && !wafDiagnostics .rulesetVersion .isEmpty ()
@@ -278,17 +261,13 @@ private void handleWafUpdateResultReport(
278261 modulesToUpdateVersionIn .forEach (module -> module .setRuleVersion (currentRuleVersion ));
279262 }
280263 }
281- if (wafDiagnostics .getNumConfigError () > 0 ) {
282- addTelemetryErrorLog (wafDiagnostics );
283- }
284264 } catch (InvalidRuleSetException e ) {
285265 log .debug (
286266 "Invalid rule during waf config update for config key {}: {}" ,
287267 configKey ,
288268 e .wafDiagnostics );
289269
290- addTelemetryErrorLog (e .wafDiagnostics );
291- sendErrorToRemoteConfig (e .wafDiagnostics , productType );
270+ // TODO: Propagate diagostics back to remote config apply_error
292271
293272 initReporter .setReportForPublication (e .wafDiagnostics );
294273 throw new RuntimeException (e );
@@ -298,117 +277,6 @@ private void handleWafUpdateResultReport(
298277 }
299278 }
300279
301- private void sendErrorToRemoteConfig (WafDiagnostics wafDiagnostics , Product productType ) {
302- if (wafDiagnostics .rules != null ) {
303- getRemoteConfigErrorLogFor ("rules" , wafDiagnostics .rules .getErrors (), productType );
304- }
305- if (wafDiagnostics .customRules != null ) {
306- getRemoteConfigErrorLogFor (
307- "customRules" , wafDiagnostics .customRules .getErrors (), productType );
308- }
309- if (wafDiagnostics .rulesData != null ) {
310- getRemoteConfigErrorLogFor ("rulesData" , wafDiagnostics .rulesData .getErrors (), productType );
311- }
312- if (wafDiagnostics .rulesOverride != null ) {
313- getRemoteConfigErrorLogFor (
314- "rulesOverride" , wafDiagnostics .rulesOverride .getErrors (), productType );
315- }
316- if (wafDiagnostics .exclusions != null ) {
317- getRemoteConfigErrorLogFor ("exclusions" , wafDiagnostics .exclusions .getErrors (), productType );
318- }
319- if (wafDiagnostics .exclusionData != null ) {
320- getRemoteConfigErrorLogFor (
321- "exclusionData" , wafDiagnostics .exclusionData .getErrors (), productType );
322- }
323- if (wafDiagnostics .actions != null ) {
324- getRemoteConfigErrorLogFor ("actions" , wafDiagnostics .actions .getErrors (), productType );
325- }
326- if (wafDiagnostics .processors != null ) {
327- getRemoteConfigErrorLogFor ("processors" , wafDiagnostics .processors .getErrors (), productType );
328- }
329- if (wafDiagnostics .scanners != null ) {
330- getRemoteConfigErrorLogFor ("scanners" , wafDiagnostics .scanners .getErrors (), productType );
331- }
332- }
333-
334- private void getRemoteConfigErrorLogFor (
335- String configType , Map <String , List <String >> errors , Product productType ) {
336- if (productType == null ) {
337- return ; // no need for RC error upon initialization
338- }
339-
340- String error = "{" ;
341- Set <String > messageKeySet = errors .keySet ();
342- for (String key : messageKeySet ) {
343- error = error + "\" message\" : \" " + key + "\" : [" ;
344- List <String > errorsPerKey = errors .get (key );
345- for (int i = 0 ; i < errorsPerKey .size (); i ++) {
346- error = error + "\" " + errorsPerKey .get (i ) + "\" " ;
347- if (i < errorsPerKey .size () - 1 ) {
348- error = error + "," ;
349- }
350- }
351- error = error + "]," ;
352-
353- error = error + "\" level\" : \" ERROR\" ," ;
354-
355- error =
356- error
357- + "\" tags\" : {"
358- + "\" log_type\" : \" rc::"
359- + productType .name ().toLowerCase ()
360- + "::diagnostic\" "
361- + "\" appsec_config_key\" : \" "
362- + configType
363- + "\" "
364- + "\" rc_config_id\" : \" \" }" ;
365- error = error + "}," ;
366- }
367-
368- error = error .substring (0 , error .length () - 1 ); // remove last comma
369-
370- throw new RuntimeException (error );
371- }
372-
373- private void addTelemetryErrorLog (WafDiagnostics wafDiagnostics ) {
374- if (wafDiagnostics .rules != null ) {
375- addTelemetryErrorLogFor ("rules" , wafDiagnostics .rules .getErrors ());
376- }
377- if (wafDiagnostics .customRules != null ) {
378- addTelemetryErrorLogFor ("customRules" , wafDiagnostics .customRules .getErrors ());
379- }
380- if (wafDiagnostics .rulesData != null ) {
381- addTelemetryErrorLogFor ("rulesData" , wafDiagnostics .rulesData .getErrors ());
382- }
383- if (wafDiagnostics .rulesOverride != null ) {
384- addTelemetryErrorLogFor ("rulesOverride" , wafDiagnostics .rulesOverride .getErrors ());
385- }
386- if (wafDiagnostics .exclusions != null ) {
387- addTelemetryErrorLogFor ("exclusions" , wafDiagnostics .exclusions .getErrors ());
388- }
389- if (wafDiagnostics .exclusionData != null ) {
390- addTelemetryErrorLogFor ("exclusionData" , wafDiagnostics .exclusionData .getErrors ());
391- }
392- if (wafDiagnostics .actions != null ) {
393- addTelemetryErrorLogFor ("actions" , wafDiagnostics .actions .getErrors ());
394- }
395- if (wafDiagnostics .processors != null ) {
396- addTelemetryErrorLogFor ("processors" , wafDiagnostics .processors .getErrors ());
397- }
398- if (wafDiagnostics .scanners != null ) {
399- addTelemetryErrorLogFor ("scanners" , wafDiagnostics .scanners .getErrors ());
400- }
401- }
402-
403- private void addTelemetryErrorLogFor (String section , Map <String , List <String >> errors ) {
404- if (!errors .isEmpty ()) {
405- String error =
406- "{\" " + section + "\" : {\" errors\" :" + mapToJsonAdapter .toJson (errors ) + "}}" ;
407- telemetryLogger .addLogMessage (
408- ERROR .name (), error , new AppSecModule .AppSecModuleActivationException (error ));
409- }
410- }
411-
412280 private void subscribeAsmFeatures () {
413281 this .configurationPoller .addListener (
414282 Product .ASM_FEATURES ,
@@ -480,7 +348,10 @@ public void init() {
480348 throw new IllegalStateException ("Expected default waf config to be available" );
481349 }
482350 try {
483- handleWafUpdateResultReport (DEFAULT_WAF_CONFIG_RULE , wafConfig , null );
351+ handleWafUpdateResultReport (
352+ DEFAULT_WAF_CONFIG_RULE ,
353+ wafConfig ,
354+ defaultConfigActivated ? DEFAULT_CONFIG_LOCATION : tracerConfig .getAppSecRulesFile ());
484355 } catch (AppSecModule .AppSecModuleActivationException e ) {
485356 throw new RuntimeException (e );
486357 }
0 commit comments