1
- import os
2
1
import re
2
+ from typing import Optional
3
3
4
4
import click
5
5
6
6
from cycode .cli import consts
7
7
from cycode .cli .config import config , configuration_manager
8
8
from 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
10
10
from cycode .cli .utils .string_utils import hash_string_to_sha256
11
11
from cycode .cyclient import logger
12
12
13
13
14
- def _is_path_to_ignore_exists (path : str ) -> bool :
15
- return os .path .exists (path )
16
-
17
-
18
14
def _is_package_pattern_valid (package : str ) -> bool :
19
15
return re .search ('^[^@]+@[^@]+$' , package ) is not None
20
16
@@ -47,10 +43,16 @@ def _is_package_pattern_valid(package: str) -> bool:
47
43
required = False ,
48
44
help = 'Ignore scanning a specific package version while running an SCA scan. Expected pattern: name@version.' ,
49
45
)
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
+ )
50
52
@click .option (
51
53
'--scan-type' ,
52
54
'-t' ,
53
- default = 'secret' ,
55
+ default = consts . SECRET_SCAN_TYPE ,
54
56
help = 'Specify the type of scan you wish to execute (the default is Secrets).' ,
55
57
type = click .Choice (config ['scans' ]['supported_scans' ]),
56
58
required = False ,
@@ -64,40 +66,68 @@ def _is_package_pattern_valid(package: str) -> bool:
64
66
required = False ,
65
67
help = 'Add an ignore rule to the global CLI config.' ,
66
68
)
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 ,
69
78
) -> None :
70
79
"""Ignores a specific value, path or rule ID."""
71
80
add_breadcrumb ('ignore' )
72
81
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' )
75
87
76
88
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
78
96
79
- if by_value is not None :
97
+ if by_value :
80
98
exclusion_type = consts .EXCLUSIONS_BY_VALUE_SECTION_NAME
81
99
exclusion_value = hash_string_to_sha256 (by_value )
82
- elif by_sha is not None :
100
+
101
+ if by_sha :
83
102
exclusion_type = consts .EXCLUSIONS_BY_SHA_SECTION_NAME
84
103
exclusion_value = by_sha
85
- elif by_path is not None :
104
+
105
+ if by_path :
86
106
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
+
89
110
exclusion_type = consts .EXCLUSIONS_BY_PATH_SECTION_NAME
90
111
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 :
94
118
if not _is_package_pattern_valid (by_package ):
95
119
raise click .ClickException ('wrong package pattern. should be name@version.' )
120
+
96
121
exclusion_type = consts .EXCLUSIONS_BY_PACKAGE_SECTION_NAME
97
122
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' )
101
131
102
132
configuration_scope = 'global' if is_global else 'local'
103
133
logger .debug (
0 commit comments