-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Added functionality to specify a custom scan when using the sca…
…n command (#565) secureli-XXX <!-- Include general description here --> ## Changes This PR improves the `scan` action by adding new functionality allowing users to specify a custom scan id instead of only being able to specify pre-commit hook ids. For example, you can now do `secureli scan -t check-pii` to run the pii scan. A new service was introduced, CustomScannersService to help orchestrate which custom scans should be run. Either a specific scan if an Id is specified, all custom scans if no id is specified, or a None result is returned if the specified id doesn't match a value in the new CustomScanId enum. There was also some refactoring done. modules/core/core_services/scanner.py is now modules/core/core_services/**hook**_scanner.py to more accurately describe its function. The pii scanner and custom_regex_scanner directories have been moved into a new directory; secureli/modules/custom_scanners/ ## Testing Added unit tests and performed manual testing to confirm that pre-commit hooks can be specified, custom scans can be specified, and when no id is specified, then all scans are done ## Clean Code Checklist <!-- This is here to support you. Some/most checkboxes may not apply to your change --> - [x] Meets acceptance criteria for issue - [x] New logic is covered with automated tests - [ ] Appropriate exception handling added - [ ] Thoughtful logging included - [ ] Documentation is updated - [ ] Follow-up work is documented in TODOs - [ ] TODOs have a ticket associated with them - [x] No commented-out code included <!-- Github-flavored markdown reference: https://docs.github.com/en/get-started/writing-on-github --> --------- Co-authored-by: Ian Bowden <ian.bowden@slalom>
- Loading branch information
1 parent
730fe09
commit 385803d
Showing
12 changed files
with
308 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
from enum import Enum | ||
import logging | ||
from typing import Optional | ||
from pathlib import Path | ||
|
||
from secureli.modules.custom_scanners.custom_regex_scanner.custom_regex_scanner import ( | ||
CustomRegexScannerService, | ||
) | ||
from secureli.modules.custom_scanners.pii_scanner.pii_scanner import PiiScannerService | ||
from secureli.modules.shared import utilities | ||
import secureli.modules.shared.models.scan as scan | ||
|
||
|
||
class CustomScanId(str, Enum): | ||
""" | ||
Scan ids of custom scans | ||
""" | ||
|
||
PII = "check-pii" | ||
CUSTOM_REGEX = "check-regex" | ||
|
||
|
||
class CustomScannersService: | ||
""" | ||
This service orchestrates running custom scans. A custom scan is a | ||
scan that is not a precommit hook scan, i.e. PII and custom regex scans. | ||
""" | ||
|
||
def __init__( | ||
self, | ||
pii_scanner: PiiScannerService, | ||
custom_regex_scanner: CustomRegexScannerService, | ||
): | ||
self.pii_scanner = pii_scanner | ||
self.custom_regex_scanner = custom_regex_scanner | ||
|
||
def scan_repo( | ||
self, | ||
folder_path: Path, | ||
scan_mode: scan.ScanMode, | ||
custom_scan_id: Optional[str] = None, | ||
files: Optional[str] = None, | ||
) -> scan.ScanResult: | ||
# If no custom scan is specified, run all custom scans | ||
if custom_scan_id is None: | ||
pii_scan_result = self.pii_scanner.scan_repo( | ||
folder_path, scan_mode, files=files | ||
) | ||
regex_scan_result = self.custom_regex_scanner.scan_repo( | ||
folder_path=folder_path, scan_mode=scan_mode, files=files | ||
) | ||
custom_scan_results = utilities.merge_scan_results( | ||
[pii_scan_result, regex_scan_result] | ||
) | ||
return custom_scan_results | ||
|
||
# If the specified scan isn't known, do nothing | ||
if custom_scan_id not in CustomScanId.__members__.values(): | ||
return None | ||
|
||
# Run the specified custom scan only | ||
scan_results = None | ||
if custom_scan_id == CustomScanId.PII: | ||
scan_results = self.pii_scanner.scan_repo( | ||
folder_path, scan_mode, files=files | ||
) | ||
|
||
if custom_scan_id == CustomScanId.CUSTOM_REGEX: | ||
scan_results = self.custom_regex_scanner.scan_repo( | ||
folder_path=folder_path, scan_mode=scan_mode, files=files | ||
) | ||
|
||
return scan_results |
File renamed without changes.
Oops, something went wrong.