@@ -279,8 +279,9 @@ fn main() -> Result<()> {
279279 exit ( EXIT_CODE_INVALID_CONFIGURATION )
280280 }
281281 } ;
282+ let sast_config = configuration_file. as_ref ( ) . and_then ( |cfg| cfg. sast ( ) ) ;
282283
283- if configuration_file . is_none ( ) && use_debug {
284+ if sast_config . is_none ( ) && use_debug {
284285 eprintln ! ( "INFO: no configuration detected locally or remotely" )
285286 }
286287
@@ -290,11 +291,11 @@ fn main() -> Result<()> {
290291 . unwrap_or_default ( ) ;
291292 let mut rules: Vec < Rule > = Vec :: new ( ) ;
292293
293- // A list of rulesets that were fetched due to being specifically listed in a ConfigFile::use_rulesets list.
294- let mut fetched_rulesets = Vec :: < & str > :: new ( ) ;
294+ // Rulesets to exclude when fetching default rulesets
295+ let mut excluded_rulesets = Vec :: < & str > :: new ( ) ;
295296 // if there is a configuration file, we load the rules from it. But it means
296297 // we cannot have the rule parameter given.
297- if let Some ( conf) = & configuration_file {
298+ if let Some ( conf) = sast_config {
298299 ignore_gitignore = conf
299300 . global_config
300301 . as_ref ( )
@@ -306,20 +307,18 @@ fn main() -> Result<()> {
306307 }
307308
308309 if static_analysis_enabled {
309- if let Some ( rulesets) = & conf. use_rulesets {
310- let rules_from_api = get_rules_from_rulesets ( rulesets, use_staging, use_debug)
311- . inspect_err ( |e| {
312- if let DatadogApiError :: RulesetNotFound ( rs) = e {
313- eprintln ! ( "Error: ruleset {rs} not found" ) ;
314- exit ( EXIT_CODE_RULESET_NOT_FOUND ) ;
315- }
316- } )
317- . context ( "error when reading rules from API" ) ?;
318- rules. extend ( rules_from_api) ;
319- for r in rulesets {
320- fetched_rulesets. push ( r. as_str ( ) ) ;
321- }
322- }
310+ let explicit_rs = conf. explicit_rulesets ( ) . collect :: < Vec < _ > > ( ) ;
311+ let rules_from_api = get_rules_from_rulesets ( & explicit_rs, use_staging, use_debug)
312+ . inspect_err ( |e| {
313+ if let DatadogApiError :: RulesetNotFound ( rs) = e {
314+ eprintln ! ( "Error: ruleset {rs} not found" ) ;
315+ exit ( EXIT_CODE_RULESET_NOT_FOUND ) ;
316+ }
317+ } )
318+ . context ( "error when reading rules from API" ) ?;
319+ rules. extend ( rules_from_api) ;
320+ excluded_rulesets. extend ( explicit_rs) ;
321+ excluded_rulesets. extend ( conf. ignore_rulesets . iter ( ) . map ( String :: as_str) ) ;
323322 }
324323 // copy the only and ignore paths from the configuration file
325324 if let Some ( pc) = conf. global_config . as_ref ( ) . and_then ( |g| g. paths . as_ref ( ) ) {
@@ -343,18 +342,19 @@ fn main() -> Result<()> {
343342 if static_analysis_enabled {
344343 // if there is no config file, we take the default rules from our APIs.
345344 if rules_file. is_none ( ) {
346- if configuration_file . is_none ( ) {
347- println ! ( "WARNING: no configuration file detected, getting the default rules from the Datadog API" ) ;
345+ if sast_config . is_none ( ) {
346+ println ! ( "WARNING: no SAST configuration detected, getting the default rules from the Datadog API" ) ;
348347 println ! ( "Check the following resources to configure your rules:" ) ;
349348 println ! (
350349 " - Datadog documentation: https://docs.datadoghq.com/code_analysis/static_analysis"
351350 ) ;
352351 println ! ( " - Static analyzer repository on GitHub: https://github.com/DataDog/datadog-static-analyzer" ) ;
353352 }
354- let should_fetch = !matches ! ( & configuration_file, Some ( config) if config. use_default_rulesets == Some ( false ) ) ;
353+
354+ let should_fetch = sast_config. is_none_or ( |c| c. use_default_rulesets != Some ( false ) ) ;
355355 if should_fetch {
356356 let rulesets_from_api =
357- get_all_default_rulesets ( use_staging, use_debug, & fetched_rulesets )
357+ get_all_default_rulesets ( use_staging, use_debug, & excluded_rulesets )
358358 . context ( "cannot get default rules" ) ?;
359359 rules. extend ( rulesets_from_api. into_iter ( ) . flat_map ( |rs| rs. into_rules ( ) ) ) ;
360360 }
0 commit comments