@@ -3,10 +3,12 @@ use cli::config_file::get_config;
33use cli:: constants:: {
44 DEFAULT_MAX_CPUS , DEFAULT_MAX_FILE_SIZE_KB , EXIT_CODE_GITHOOK_FAILED ,
55 EXIT_CODE_INVALID_CONFIGURATION , EXIT_CODE_INVALID_DIRECTORY , EXIT_CODE_NO_DIRECTORY ,
6- EXIT_CODE_NO_SECRET_OR_STATIC_ANALYSIS , EXIT_CODE_RULE_CHECKSUM_INVALID ,
7- EXIT_CODE_SHA_OR_DEFAULT_BRANCH ,
6+ EXIT_CODE_NO_SECRET_OR_STATIC_ANALYSIS , EXIT_CODE_RULESET_NOT_FOUND ,
7+ EXIT_CODE_RULE_CHECKSUM_INVALID , EXIT_CODE_SHA_OR_DEFAULT_BRANCH ,
8+ } ;
9+ use cli:: datadog_utils:: {
10+ get_all_default_rulesets, get_rules_from_rulesets, get_secrets_rules, DatadogApiError ,
811} ;
9- use cli:: datadog_utils:: { get_all_default_rulesets, get_rules_from_rulesets, get_secrets_rules} ;
1012use cli:: file_utils:: { filter_files_by_size, get_files, read_files_from_gitignore} ;
1113use cli:: git_utils:: {
1214 get_changed_files_between_shas, get_changed_files_with_branch, get_default_branch,
@@ -24,7 +26,7 @@ use itertools::Itertools;
2426use kernel:: analysis:: ddsa_lib:: v8_platform:: { initialize_v8, Initialized , V8Platform } ;
2527use kernel:: classifiers:: ArtifactClassification ;
2628use kernel:: config:: common:: { ConfigMethod , PathConfig } ;
27- use kernel:: config:: file_v1 ;
29+ use kernel:: config:: file_v2 ;
2830use kernel:: constants:: { CARGO_VERSION , VERSION } ;
2931use kernel:: model:: common:: OutputFormat :: Json ;
3032use kernel:: model:: rule:: { Rule , RuleResult } ;
@@ -255,7 +257,7 @@ fn main() -> Result<()> {
255257 let configuration_file_and_method = get_config ( directory_to_analyze. as_str ( ) , use_debug) ;
256258
257259 let ( configuration_file, configuration_method) : (
258- Option < file_v1 :: ConfigFile > ,
260+ Option < file_v2 :: ConfigFile > ,
259261 Option < ConfigMethod > ,
260262 ) = match configuration_file_and_method {
261263 Ok ( cfg) => match cfg {
@@ -276,29 +278,57 @@ fn main() -> Result<()> {
276278 . map ( RuleConfigProvider :: from_config)
277279 . unwrap_or_default ( ) ;
278280
281+ // A list of rulesets that were fetched due to being specifically listed in a ConfigFile::use_rulesets list.
282+ let mut fetched_rulesets = Vec :: < & str > :: new ( ) ;
279283 // if there is a configuration file, we load the rules from it. But it means
280284 // we cannot have the rule parameter given.
281- if let Some ( conf) = configuration_file {
282- ignore_gitignore = conf. ignore_gitignore . unwrap_or ( false ) ;
285+ if let Some ( conf) = & configuration_file {
286+ ignore_gitignore = conf
287+ . global_config
288+ . as_ref ( )
289+ . and_then ( |g| g. use_gitignore . map ( |b| !b) )
290+ . unwrap_or ( false ) ;
283291
284292 if static_analysis_enabled {
285- let rulesets = conf. rulesets . keys ( ) . cloned ( ) . collect_vec ( ) ;
286- let rules_from_api = get_rules_from_rulesets ( & rulesets, use_staging, use_debug)
287- . context ( "error when reading rules from API" ) ?;
288- rules. extend ( rules_from_api) ;
293+ if let Some ( rulesets) = & conf. use_rulesets {
294+ let rules_from_api = get_rules_from_rulesets ( rulesets, use_staging, use_debug)
295+ . inspect_err ( |e| {
296+ if let DatadogApiError :: RulesetNotFound ( rs) = e {
297+ eprintln ! ( "Error: ruleset {rs} not found" ) ;
298+ exit ( EXIT_CODE_RULESET_NOT_FOUND ) ;
299+ }
300+ } )
301+ . context ( "error when reading rules from API" ) ?;
302+ rules. extend ( rules_from_api) ;
303+ for r in rulesets {
304+ fetched_rulesets. push ( r. as_str ( ) ) ;
305+ }
306+ }
289307 }
290308
291309 // copy the only and ignore paths from the configuration file
292- path_config. ignore . extend ( conf. paths . ignore ) ;
293- path_config. only = conf. paths . only ;
310+ if let Some ( pc) = conf. global_config . as_ref ( ) . and_then ( |g| g. paths . as_ref ( ) ) {
311+ path_config. ignore . extend_from_slice ( & pc. ignore ) ;
312+ path_config. only = pc. only . clone ( ) ;
313+ }
294314
295315 // Get the max file size from the configuration or default to the default constant.
296- max_file_size_kb = conf. max_file_size_kb . unwrap_or ( DEFAULT_MAX_FILE_SIZE_KB ) ;
297- ignore_generated_files = conf. ignore_generated_files . unwrap_or ( true ) ;
298- } else {
316+ max_file_size_kb = conf
317+ . global_config
318+ . as_ref ( )
319+ . and_then ( |g| g. max_file_size_kb )
320+ . unwrap_or ( DEFAULT_MAX_FILE_SIZE_KB ) ;
321+ ignore_generated_files = conf
322+ . global_config
323+ . as_ref ( )
324+ . and_then ( |g| g. ignore_generated_files )
325+ . unwrap_or ( true ) ;
326+ }
327+
328+ if static_analysis_enabled {
299329 // if there is no config file, we take the default rules from our APIs.
300330
301- if use_debug {
331+ if configuration_file . is_none ( ) && use_debug {
302332 println ! ( "WARNING: no configuration file detected, getting the default rules from the Datadog API" ) ;
303333 println ! ( "Check the following resources to configure your rules:" ) ;
304334 println ! (
@@ -307,10 +337,11 @@ fn main() -> Result<()> {
307337 println ! ( " - Static analyzer repository on GitHub: https://github.com/DataDog/datadog-static-analyzer" ) ;
308338 }
309339
310- if static_analysis_enabled {
340+ let should_fetch = !matches ! ( & configuration_file, Some ( config) if config. use_default_rulesets == Some ( false ) ) ;
341+ if should_fetch {
311342 let rulesets_from_api =
312- get_all_default_rulesets ( use_staging, use_debug) . expect ( "cannot get default rules" ) ;
313-
343+ get_all_default_rulesets ( use_staging, use_debug, & fetched_rulesets )
344+ . context ( "cannot get default rules" ) ? ;
314345 rules. extend ( rulesets_from_api. into_iter ( ) . flat_map ( |rs| rs. into_rules ( ) ) ) ;
315346 }
316347 }
0 commit comments