1- import os
21import re
2+ from typing import Optional
33
44import click
55
66from cycode .cli import consts
77from cycode .cli .config import config , configuration_manager
88from cycode .cli .sentry import add_breadcrumb
9- from cycode .cli .utils .path_utils import get_absolute_path
9+ from cycode .cli .utils .path_utils import get_absolute_path , is_path_exists
1010from cycode .cli .utils .string_utils import hash_string_to_sha256
1111from cycode .cyclient import logger
1212
1313
14- def _is_path_to_ignore_exists (path : str ) -> bool :
15- return os .path .exists (path )
16-
17-
1814def _is_package_pattern_valid (package : str ) -> bool :
1915 return re .search ('^[^@]+@[^@]+$' , package ) is not None
2016
@@ -47,10 +43,16 @@ def _is_package_pattern_valid(package: str) -> bool:
4743 required = False ,
4844 help = 'Ignore scanning a specific package version while running an SCA scan. Expected pattern: name@version.' ,
4945)
46+ @click .option (
47+ '--by-cve' ,
48+ type = click .STRING ,
49+ required = False ,
50+ help = 'Ignore scanning a specific CVE while running an SCA scan. Expected pattern: CVE-YYYY-NNN.' ,
51+ )
5052@click .option (
5153 '--scan-type' ,
5254 '-t' ,
53- default = 'secret' ,
55+ default = consts . SECRET_SCAN_TYPE ,
5456 help = 'Specify the type of scan you wish to execute (the default is Secrets).' ,
5557 type = click .Choice (config ['scans' ]['supported_scans' ]),
5658 required = False ,
@@ -64,40 +66,68 @@ def _is_package_pattern_valid(package: str) -> bool:
6466 required = False ,
6567 help = 'Add an ignore rule to the global CLI config.' ,
6668)
67- def ignore_command (
68- by_value : str , by_sha : str , by_path : str , by_rule : str , by_package : str , scan_type : str , is_global : bool
69+ def ignore_command ( # noqa: C901
70+ by_value : Optional [str ],
71+ by_sha : Optional [str ],
72+ by_path : Optional [str ],
73+ by_rule : Optional [str ],
74+ by_package : Optional [str ],
75+ by_cve : Optional [str ],
76+ scan_type : str = consts .SECRET_SCAN_TYPE ,
77+ is_global : bool = False ,
6978) -> None :
7079 """Ignores a specific value, path or rule ID."""
7180 add_breadcrumb ('ignore' )
7281
73- if not by_value and not by_sha and not by_path and not by_rule and not by_package :
74- raise click .ClickException ('ignore by type is missing' )
82+ all_by_values = [by_value , by_sha , by_path , by_rule , by_package , by_cve ]
83+ if all (by is None for by in all_by_values ):
84+ raise click .ClickException ('Ignore by type is missing' )
85+ if len ([by for by in all_by_values if by is not None ]) != 1 :
86+ raise click .ClickException ('You must specify only one ignore by type' )
7587
7688 if any (by is not None for by in [by_value , by_sha ]) and scan_type != consts .SECRET_SCAN_TYPE :
77- raise click .ClickException ('this exclude is supported only for secret scan type' )
89+ raise click .ClickException ('This exclude is supported only for Secret scan type' )
90+ if (by_cve or by_package ) and scan_type != consts .SCA_SCAN_TYPE :
91+ raise click .ClickException ('This exclude is supported only for SCA scan type' )
92+
93+ # only one of the by values must be set
94+ # at least one of the by values must be set
95+ exclusion_type = exclusion_value = None
7896
79- if by_value is not None :
97+ if by_value :
8098 exclusion_type = consts .EXCLUSIONS_BY_VALUE_SECTION_NAME
8199 exclusion_value = hash_string_to_sha256 (by_value )
82- elif by_sha is not None :
100+
101+ if by_sha :
83102 exclusion_type = consts .EXCLUSIONS_BY_SHA_SECTION_NAME
84103 exclusion_value = by_sha
85- elif by_path is not None :
104+
105+ if by_path :
86106 absolute_path = get_absolute_path (by_path )
87- if not _is_path_to_ignore_exists (absolute_path ):
88- raise click .ClickException ('the provided path to ignore by is not exist' )
107+ if not is_path_exists (absolute_path ):
108+ raise click .ClickException ('The provided path to ignore by does not exist' )
109+
89110 exclusion_type = consts .EXCLUSIONS_BY_PATH_SECTION_NAME
90111 exclusion_value = get_absolute_path (absolute_path )
91- elif by_package is not None :
92- if scan_type != consts .SCA_SCAN_TYPE :
93- raise click .ClickException ('exclude by package is supported only for sca scan type' )
112+
113+ if by_rule :
114+ exclusion_type = consts .EXCLUSIONS_BY_RULE_SECTION_NAME
115+ exclusion_value = by_rule
116+
117+ if by_package :
94118 if not _is_package_pattern_valid (by_package ):
95119 raise click .ClickException ('wrong package pattern. should be name@version.' )
120+
96121 exclusion_type = consts .EXCLUSIONS_BY_PACKAGE_SECTION_NAME
97122 exclusion_value = by_package
98- else :
99- exclusion_type = consts .EXCLUSIONS_BY_RULE_SECTION_NAME
100- exclusion_value = by_rule
123+
124+ if by_cve :
125+ exclusion_type = consts .EXCLUSIONS_BY_CVE_SECTION_NAME
126+ exclusion_value = by_cve
127+
128+ if not exclusion_type or not exclusion_value :
129+ # should never happen
130+ raise click .ClickException ('Invalid ignore by type' )
101131
102132 configuration_scope = 'global' if is_global else 'local'
103133 logger .debug (
0 commit comments