Skip to content

Commit 7027e2d

Browse files
authored
CM-31709 - Migrate to the new filter to fetch detection rules (#222)
1 parent 02bdd08 commit 7027e2d

File tree

3 files changed

+10
-34
lines changed

3 files changed

+10
-34
lines changed

cycode/cli/commands/scan/code_scanner.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -116,19 +116,14 @@ def _should_use_sync_flow(scan_type: str, sync_option: bool, scan_parameters: Op
116116

117117

118118
def _enrich_scan_result_with_data_from_detection_rules(
119-
cycode_client: 'ScanClient', scan_type: str, scan_result: ZippedFileScanResult
119+
cycode_client: 'ScanClient', scan_result: ZippedFileScanResult
120120
) -> None:
121-
# TODO(MarshalX): remove scan_type arg after migration to new backend filter
122-
if scan_type not in {consts.SECRET_SCAN_TYPE, consts.INFRA_CONFIGURATION_SCAN_TYPE}:
123-
# not yet
124-
return
125-
126121
detection_rule_ids = set()
127122
for detections_per_file in scan_result.detections_per_file:
128123
for detection in detections_per_file.detections:
129124
detection_rule_ids.add(detection.detection_rule_id)
130125

131-
detection_rules = cycode_client.get_detection_rules(scan_type, detection_rule_ids)
126+
detection_rules = cycode_client.get_detection_rules(detection_rule_ids)
132127
detection_rules_by_id = {detection_rule.detection_rule_id: detection_rule for detection_rule in detection_rules}
133128

134129
for detections_per_file in scan_result.detections_per_file:
@@ -138,9 +133,9 @@ def _enrich_scan_result_with_data_from_detection_rules(
138133
# we want to make sure that BE returned it. better to not map data instead of failed scan
139134
continue
140135

141-
if detection_rule.classification_data:
136+
if not detection.severity and detection_rule.classification_data:
142137
# it's fine to take the first one, because:
143-
# - for "secrets" and "iac" there is only one classification rule per detection rule
138+
# - for "secrets" and "iac" there is only one classification rule per-detection rule
144139
# - for "sca" and "sast" we get severity from detection service
145140
detection.severity = detection_rule.classification_data[0].severity
146141

@@ -187,7 +182,7 @@ def _scan_batch_thread_func(batch: List[Document]) -> Tuple[str, CliError, Local
187182
should_use_sync_flow,
188183
)
189184

190-
_enrich_scan_result_with_data_from_detection_rules(cycode_client, scan_type, scan_result)
185+
_enrich_scan_result_with_data_from_detection_rules(cycode_client, scan_result)
191186

192187
local_scan_result = create_local_scan_result(
193188
scan_result, batch, command_scan_type, scan_type, severity_threshold

cycode/cyclient/models.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,7 @@ class Meta:
3838

3939
message = fields.String()
4040
type = fields.String()
41-
severity = fields.String(missing='High')
42-
# TODO(MarshalX): Remove "missing" arg when IaC and Secrets scans will have classifications
41+
severity = fields.String(missing=None)
4342
detection_type_id = fields.String()
4443
detection_details = fields.Dict()
4544
detection_rule_id = fields.String()

cycode/cyclient/scan_client.py

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ def get_detection_rules_path(self) -> str:
164164
return (
165165
f'{self.scan_config.get_detections_prefix()}/'
166166
f'{self.POLICIES_SERVICE_CONTROLLER_PATH_V3}/'
167-
f'detection_rules'
167+
f'detection_rules/byIds'
168168
)
169169

170170
@staticmethod
@@ -181,36 +181,18 @@ def _get_policy_type_by_scan_type(scan_type: str) -> str:
181181

182182
return scan_type_to_policy_type[scan_type]
183183

184-
@staticmethod
185-
def _filter_detection_rules_by_ids(
186-
detection_rules: List[models.DetectionRule], detection_rules_ids: Union[Set[str], List[str]]
187-
) -> List[models.DetectionRule]:
188-
ids = set(detection_rules_ids) # cast to set to perform faster search
189-
return [rule for rule in detection_rules if rule.detection_rule_id in ids]
190-
191184
@staticmethod
192185
def parse_detection_rules_response(response: Response) -> List[models.DetectionRule]:
193186
return models.DetectionRuleSchema().load(response.json(), many=True)
194187

195-
def get_detection_rules(
196-
self, scan_type: str, detection_rules_ids: Union[Set[str], List[str]]
197-
) -> List[models.DetectionRule]:
198-
# TODO(MarshalX): use filter by list of IDs instead of policy_type when BE will be ready
199-
params = {
200-
'include_hidden': False,
201-
'include_only_enabled_detection_rules': True,
202-
'page_number': 0,
203-
'page_size': 5000,
204-
'policy_types_v2': self._get_policy_type_by_scan_type(scan_type),
205-
}
188+
def get_detection_rules(self, detection_rules_ids: Union[Set[str], List[str]]) -> List[models.DetectionRule]:
206189
response = self.scan_cycode_client.get(
207190
url_path=self.get_detection_rules_path(),
208-
params=params,
191+
params={'ids': detection_rules_ids},
209192
hide_response_content_log=self._hide_response_log,
210193
)
211194

212-
# we are filtering rules by ids in-place for smooth migration when backend will be ready
213-
return self._filter_detection_rules_by_ids(self.parse_detection_rules_response(response), detection_rules_ids)
195+
return self.parse_detection_rules_response(response)
214196

215197
def get_scan_detections_path(self, scan_type: str) -> str:
216198
return f'{self.scan_config.get_detections_prefix()}/{self.get_detections_service_controller_path(scan_type)}'

0 commit comments

Comments
 (0)