Skip to content

Commit

Permalink
secureli-435: address feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
Kathleen Hogan committed Mar 22, 2024
1 parent 1a17a88 commit 9375283
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 26 deletions.
21 changes: 8 additions & 13 deletions secureli/modules/core/core_services/scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,8 @@

import re

import secureli.modules.shared.models.scan as scan
from secureli.modules.shared.abstractions.pre_commit import PreCommitAbstraction
from secureli.modules.shared.models.scan import (
ScanFailure,
ScanMode,
ScanOutput,
ScanResult,
)
from secureli.repositories.repo_settings import PreCommitSettings


Expand All @@ -33,10 +28,10 @@ def __init__(self, pre_commit: PreCommitAbstraction):
def scan_repo(
self,
folder_path: Path,
scan_mode: ScanMode,
scan_mode: scan.ScanMode,
specific_test: Optional[str] = None,
files: Optional[str] = None,
) -> ScanResult:
) -> scan.ScanResult:
"""
Scans the repo according to the repo's seCureLI config
:param scan_mode: Whether to scan the staged files (i.e., the files about to be
Expand All @@ -45,21 +40,21 @@ def scan_repo(
If None, run all hooks.
:return: A ScanResult object containing whether we succeeded and any error
"""
all_files = True if scan_mode == ScanMode.ALL_FILES else False
all_files = True if scan_mode == scan.ScanMode.ALL_FILES else False
execute_result = self.pre_commit.execute_hooks(
folder_path, all_files, hook_id=specific_test, files=files
)
parsed_output = self._parse_scan_ouput(
folder_path, output=execute_result.output
)

return ScanResult(
return scan.ScanResult(
successful=execute_result.successful,
output=execute_result.output,
failures=parsed_output.failures,
)

def _parse_scan_ouput(self, folder_path: Path, output: str = "") -> ScanOutput:
def _parse_scan_ouput(self, folder_path: Path, output: str = "") -> scan.ScanOutput:
"""
Parses the output from a scan and returns a list of Failure objects representing any
hook rule failures during a scan.
Expand Down Expand Up @@ -96,9 +91,9 @@ def _parse_scan_ouput(self, folder_path: Path, output: str = "") -> ScanOutput:
files = self._find_file_names(failure_output_list=failure_output_list)

for file in files:
failures.append(ScanFailure(id=id, file=file, repo=repo))
failures.append(scan.ScanFailure(id=id, file=file, repo=repo))

return ScanOutput(failures=failures)
return scan.ScanOutput(failures=failures)

def _get_single_failure_output(
self, failure_start: int, output_by_line: list[str]
Expand Down
26 changes: 13 additions & 13 deletions secureli/modules/pii_scanner/pii_scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,8 @@
from pathlib import Path
import pydantic

import secureli.modules.shared.models.scan as scan
from secureli.modules.shared.abstractions.echo import EchoAbstraction
from secureli.modules.shared.models.scan import (
ScanFailure,
ScanMode,
ScanResult,
)
from secureli.repositories.repo_files import RepoFilesRepository


Expand Down Expand Up @@ -46,9 +42,9 @@ def __init__(
def scan_repo(
self,
folder_path: Path,
scan_mode: ScanMode,
scan_mode: scan.ScanMode,
files: Optional[list[str]] = None,
) -> ScanResult:
) -> scan.ScanResult:
"""
Scans the repo for potential PII
:param folder_path: The folder path to initialize the repo for
Expand Down Expand Up @@ -92,7 +88,7 @@ def scan_repo(
scan_failures = self._generate_scan_failures(pii_found_files)
output = self._generate_scan_output(pii_found, not pii_found)

return ScanResult(
return scan.ScanResult(
successful=not pii_found,
output=output,
failures=scan_failures,
Expand All @@ -108,7 +104,7 @@ def _file_extension_excluded(self, filename) -> bool:
def _get_files_list(
self,
folder_path: Path,
scan_mode: ScanMode,
scan_mode: scan.ScanMode,
files: Optional[list[str]] = None,
) -> list[Path]:
"""
Expand All @@ -123,19 +119,21 @@ def _get_files_list(
"""
file_paths: list[Path] = []

if scan_mode == ScanMode.STAGED_ONLY:
if scan_mode == scan.ScanMode.STAGED_ONLY:
file_paths = self.repo_files.list_staged_files(folder_path)
if files:
file_paths = list(filter(lambda file: file in file_paths, files))

if scan_mode == ScanMode.ALL_FILES:
if scan_mode == scan.ScanMode.ALL_FILES:
file_paths = self.repo_files.list_repo_files(folder_path)

return list(
filter(lambda file: not self._file_extension_excluded(file), file_paths)
)

def _generate_scan_failures(self, pii_found_files: set[str]) -> list[ScanFailure]:
def _generate_scan_failures(
self, pii_found_files: set[str]
) -> list[scan.ScanFailure]:
"""
Generates a list of ScanFailures for each file in which potential PII was found
:param pii_found_files: The set of files in which potential PII was found
Expand All @@ -145,7 +143,9 @@ def _generate_scan_failures(self, pii_found_files: set[str]) -> list[ScanFailure

for pii_found_file in pii_found_files:
failures.append(
ScanFailure(id="pii_scan", file=pii_found_file, repo=SECURELI_GITHUB)
scan.ScanFailure(
id="pii_scan", file=pii_found_file, repo=SECURELI_GITHUB
)
)
return failures

Expand Down

0 comments on commit 9375283

Please sign in to comment.