77import com .datadog .appsec .config .AppSecConfigService ;
88import com .datadog .appsec .config .AppSecConfigServiceImpl ;
99import com .datadog .appsec .ddwaf .WAFModule ;
10+ import com .datadog .appsec .ddwaf .WafInitialization ;
1011import com .datadog .appsec .event .EventDispatcher ;
1112import com .datadog .appsec .event .ReplaceableEventProducerService ;
1213import com .datadog .appsec .gateway .GatewayBridge ;
1314import com .datadog .appsec .util .AbortStartupException ;
1415import com .datadog .appsec .util .StandardizedLogging ;
16+ import com .datadog .ddwaf .WafBuilder ;
17+ import com .datadog .ddwaf .WafConfig ;
1518import datadog .appsec .api .blocking .Blocking ;
1619import datadog .appsec .api .blocking .BlockingService ;
1720import datadog .communication .ddagent .SharedCommunicationObjects ;
@@ -43,6 +46,7 @@ public class AppSecSystem {
4346 private static ReplaceableEventProducerService REPLACEABLE_EVENT_PRODUCER ; // testing
4447 private static Runnable STOP_SUBSCRIPTION_SERVICE ;
4548 private static Runnable RESET_SUBSCRIPTION_SERVICE ;
49+ private static WafBuilder wafBuilder ;
4650
4751 public static void start (SubscriptionService gw , SharedCommunicationObjects sco ) {
4852 try {
@@ -64,7 +68,10 @@ private static void doStart(SubscriptionService gw, SharedCommunicationObjects s
6468 return ;
6569 }
6670 log .debug ("AppSec is starting ({})" , appSecEnabledConfig );
67-
71+ if (!WafInitialization .ONLINE ) {
72+ log .debug ("In-app WAF initialization failed. See previous log entries" );
73+ return ;
74+ }
6875 REPLACEABLE_EVENT_PRODUCER = new ReplaceableEventProducerService ();
6976 EventDispatcher eventDispatcher = new EventDispatcher ();
7077 REPLACEABLE_EVENT_PRODUCER .replaceEventProducerService (eventDispatcher );
@@ -86,7 +93,8 @@ private static void doStart(SubscriptionService gw, SharedCommunicationObjects s
8693 APP_SEC_CONFIG_SERVICE =
8794 new AppSecConfigServiceImpl (
8895 config , configurationPoller , () -> reloadSubscriptions (REPLACEABLE_EVENT_PRODUCER ));
89- APP_SEC_CONFIG_SERVICE .init ();
96+ wafBuilder = new WafBuilder (createWafConfig (config ));
97+ APP_SEC_CONFIG_SERVICE .init (wafBuilder );
9098
9199 sco .createRemaining (config );
92100
@@ -105,7 +113,7 @@ private static void doStart(SubscriptionService gw, SharedCommunicationObjects s
105113
106114 setActive (appSecEnabledConfig == ProductActivation .FULLY_ENABLED );
107115
108- APP_SEC_CONFIG_SERVICE .maybeSubscribeConfigPolling ();
116+ APP_SEC_CONFIG_SERVICE .maybeSubscribeConfigPolling (wafBuilder );
109117
110118 Blocking .setBlockingService (new BlockingServiceImpl (REPLACEABLE_EVENT_PRODUCER ));
111119
@@ -143,8 +151,8 @@ public static void stop() {
143151 RESET_SUBSCRIPTION_SERVICE = null ;
144152 }
145153 Blocking .setBlockingService (BlockingService .NOOP );
146-
147154 APP_SEC_CONFIG_SERVICE .close ();
155+ wafBuilder .destroy ();
148156 }
149157
150158 private static void loadModules (EventDispatcher eventDispatcher , Monitoring monitoring ) {
@@ -155,9 +163,9 @@ private static void loadModules(EventDispatcher eventDispatcher, Monitoring moni
155163 for (AppSecModule module : modules ) {
156164 log .debug ("Starting appsec module {}" , module .getName ());
157165 try {
158- AppSecConfigService .TransactionalAppSecModuleConfigurer cfgObject ;
159- cfgObject = APP_SEC_CONFIG_SERVICE .createAppSecModuleConfigurer ();
160- module .config (cfgObject );
166+ AppSecConfigService .TransactionalAppSecModuleConfigurer cfgObject =
167+ APP_SEC_CONFIG_SERVICE .createAppSecModuleConfigurer ();
168+ module .config (cfgObject , wafBuilder );
161169 cfgObject .commit ();
162170 } catch (RuntimeException | AppSecModule .AppSecModuleActivationException t ) {
163171 log .error ("Startup of appsec module {} failed" , module .getName (), t );
@@ -209,4 +217,21 @@ public static Set<String> getStartedModulesInfo() {
209217 return Collections .emptySet ();
210218 }
211219 }
220+
221+ private static WafConfig createWafConfig (Config config ) {
222+ WafConfig wafConfig = new WafConfig ();
223+ String keyRegexp = config .getAppSecObfuscationParameterKeyRegexp ();
224+ if (keyRegexp != null ) {
225+ wafConfig .obfuscatorKeyRegex = keyRegexp ;
226+ } else { // reset
227+ wafConfig .obfuscatorKeyRegex = WafConfig .DEFAULT_KEY_REGEX ;
228+ }
229+ String valueRegexp = config .getAppSecObfuscationParameterValueRegexp ();
230+ if (valueRegexp != null ) {
231+ wafConfig .obfuscatorValueRegex = valueRegexp ;
232+ } else { // reset
233+ wafConfig .obfuscatorValueRegex = WafConfig .DEFAULT_VALUE_REGEX ;
234+ }
235+ return wafConfig ;
236+ }
212237}
0 commit comments