Skip to content

Commit

Permalink
updates name to cutom regex scanner
Browse files Browse the repository at this point in the history
  • Loading branch information
doug-szeto-slalom committed Jun 11, 2024
1 parent 92efd96 commit 9717c53
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 37 deletions.
10 changes: 6 additions & 4 deletions secureli/actions/scan.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
from secureli.modules.observability.observability_services.logging import LoggingService
from secureli.modules.core.core_services.scanner import HooksScannerService
from secureli.modules.pii_scanner.pii_scanner import PiiScannerService
from secureli.modules.custom_scanner.custom_scanner import CustomScannerService
from secureli.modules.custom_regex_scanner.custom_regex_scanner import (
CustomRegexScannerService,
)
from secureli.modules.shared.models.scan import ScanMode, ScanResult
from secureli.settings import Settings
from secureli.modules.shared import utilities
Expand All @@ -39,13 +41,13 @@ def __init__(
action_deps: action.ActionDependencies,
hooks_scanner: HooksScannerService,
pii_scanner: PiiScannerService,
custom_scanner: CustomScannerService,
custom_regex_scanner: CustomRegexScannerService,
git_repo: GitRepo,
):
super().__init__(action_deps)
self.hooks_scanner = hooks_scanner
self.pii_scanner = pii_scanner
self.custom_scanner = custom_scanner
self.custom_regex_scanner = custom_regex_scanner
self.git_repo = git_repo

def publish_results(
Expand Down Expand Up @@ -124,7 +126,7 @@ def scan_repo(
folder_path, scan_mode, files=files
)

custom_scan_result = self.custom_scanner.scan_repo(
custom_scan_result = self.custom_regex_scanner.scan_repo(
folder_path=folder_path,
scan_mode=scan_mode,
files=files,
Expand Down
10 changes: 6 additions & 4 deletions secureli/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
from secureli.modules.core.core_services.scanner import HooksScannerService
from secureli.modules.core.core_services.updater import UpdaterService
from secureli.modules.pii_scanner.pii_scanner import PiiScannerService
from secureli.modules.custom_scanner.custom_scanner import CustomScannerService
from secureli.modules.custom_regex_scanner.custom_regex_scanner import (
CustomRegexScannerService,
)
from secureli.modules.secureli_ignore import SecureliIgnoreService
from secureli.settings import Settings

Expand Down Expand Up @@ -144,8 +146,8 @@ class Container(containers.DeclarativeContainer):
echo=echo,
)

custom_scanner_service = providers.Factory(
CustomScannerService, repo_files=repo_files_repository, echo=echo
custom_regex_scanner_service = providers.Factory(
CustomRegexScannerService, repo_files=repo_files_repository, echo=echo
)

updater_service = providers.Factory(
Expand Down Expand Up @@ -188,7 +190,7 @@ class Container(containers.DeclarativeContainer):
action_deps=action_deps,
hooks_scanner=hooks_scanner_service,
pii_scanner=pii_scanner_service,
custom_scanner=custom_scanner_service,
custom_regex_scanner=custom_regex_scanner_service,
git_repo=git_repo,
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from secureli.repositories.repo_files import RepoFilesRepository


class CustomScanResult(pydantic.BaseModel):
class CustomRegexScanResult(pydantic.BaseModel):
"""
An individual result of potential custom RegEx found
"""
Expand All @@ -23,7 +23,7 @@ class CustomScanResult(pydantic.BaseModel):
regex_pattern: str


class CustomScannerService:
class CustomRegexScannerService:
"""
Scans the repo for potential custom RegEx
"""
Expand Down Expand Up @@ -56,7 +56,7 @@ def scan_repo(
folder_path=folder_path, scan_mode=scan_mode, files=files
)
current_line_num = 0
custom_regex_found: dict[str, list[CustomScanResult]] = {}
custom_regex_found: dict[str, list[CustomRegexScanResult]] = {}
custom_regex_found_files = set()

for file_path in file_paths:
Expand Down Expand Up @@ -169,7 +169,7 @@ def _generate_initial_output(self, success: bool) -> str:
return output

def _generate_scan_output(
self, custom_regex_found: dict[str, list[CustomScanResult]], success: bool
self, custom_regex_found: dict[str, list[CustomRegexScanResult]], success: bool
) -> str:
"""
Generates the scan output of the PII scan, listing all the areas where potential PII was found
Expand Down
16 changes: 8 additions & 8 deletions tests/actions/test_scan_action.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,12 @@ def mock_pii_scanner() -> MagicMock:


@pytest.fixture()
def mock_custom_scanner() -> MagicMock:
mock_custom_scanner = MagicMock()
mock_custom_scanner.scan_repo.return_value = ScanResult(
def mock_custom_regex_scanner() -> MagicMock:
mock_custom_regex_scanner = MagicMock()
mock_custom_regex_scanner.scan_repo.return_value = ScanResult(
successful=True, failures=[]
)
return mock_custom_scanner
return mock_custom_regex_scanner


@pytest.fixture()
Expand Down Expand Up @@ -152,14 +152,14 @@ def action_deps(
def scan_action(
action_deps: ActionDependencies,
mock_pii_scanner: MagicMock,
mock_custom_scanner: MagicMock,
mock_custom_regex_scanner: MagicMock,
mock_git_repo: MagicMock,
) -> ScanAction:
return ScanAction(
action_deps=action_deps,
hooks_scanner=action_deps.hooks_scanner,
pii_scanner=mock_pii_scanner,
custom_scanner=mock_custom_scanner,
custom_regex_scanner=mock_custom_regex_scanner,
git_repo=mock_git_repo,
)

Expand All @@ -174,7 +174,7 @@ def test_that_scan_repo_errors_if_not_successful(
scan_action: ScanAction,
mock_hooks_scanner: MagicMock,
mock_pii_scanner: MagicMock,
mock_custom_scanner: MagicMock,
mock_custom_regex_scanner: MagicMock,
mock_secureli_config: MagicMock,
mock_language_analyzer: MagicMock,
):
Expand All @@ -187,7 +187,7 @@ def test_that_scan_repo_errors_if_not_successful(
successful=False, output="So much PII", failures=[]
)

mock_custom_scanner.scan_repo.return_vale = ScanResult(
mock_custom_regex_scanner.scan_repo.return_vale = ScanResult(
successful=False, output="Horrible regex pattern matched", failures=[]
)

Expand Down
36 changes: 19 additions & 17 deletions tests/modules/custom_scanner/test_custom_scanner_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
from pytest_mock import MockerFixture
from unittest.mock import MagicMock, Mock
from pathlib import Path
from secureli.modules.custom_scanner.custom_scanner import CustomScannerService
from secureli.modules.custom_regex_scanner.custom_regex_scanner import (
CustomRegexScannerService,
)
from secureli.modules.shared.models.scan import ScanMode


Expand Down Expand Up @@ -53,20 +55,20 @@ def mock_re(mocker: MockerFixture) -> MagicMock:


@pytest.fixture()
def custom_scanner_service(
def custom_regex_scanner_service(
mock_repo_files_repository: MagicMock, mock_echo: MagicMock
) -> CustomScannerService:
return CustomScannerService(mock_repo_files_repository, mock_echo)
) -> CustomRegexScannerService:
return CustomRegexScannerService(mock_repo_files_repository, mock_echo)


def test_that_custom_scanner_service_finds_regex(
custom_scanner_service: CustomScannerService,
def test_that_custom_regex_scanner_service_finds_regex(
custom_regex_scanner_service: CustomRegexScannerService,
mock_repo_files_repository: MagicMock,
custom_regex_patterns: list[str],
mock_open_fn: MagicMock,
mock_re: MagicMock,
):
scan_result = custom_scanner_service.scan_repo(
scan_result = custom_regex_scanner_service.scan_repo(
folder_path=test_folder_path,
scan_mode=ScanMode.STAGED_ONLY,
custom_regex_patterns=custom_regex_patterns,
Expand All @@ -79,13 +81,13 @@ def test_that_custom_scanner_service_finds_regex(
assert "Pattern Matched:" in scan_result.output


def test_that_custom_scanner_service_scans_all_files_when_specified(
custom_scanner_service: CustomScannerService,
def test_that_custom_regex_scanner_service_scans_all_files_when_specified(
custom_regex_scanner_service: CustomRegexScannerService,
mock_repo_files_repository: MagicMock,
custom_regex_patterns: list[str],
mock_open_fn: MagicMock,
):
custom_scanner_service.scan_repo(
custom_regex_scanner_service.scan_repo(
folder_path=test_folder_path,
scan_mode=ScanMode.ALL_FILES,
custom_regex_patterns=custom_regex_patterns,
Expand All @@ -94,16 +96,16 @@ def test_that_custom_scanner_service_scans_all_files_when_specified(
mock_repo_files_repository.list_repo_files.assert_called_once()


def test_that_custom_scanner_service_ignores_secureli_yaml(
custom_scanner_service: CustomScannerService,
def test_that_custom_regex_scanner_service_ignores_secureli_yaml(
custom_regex_scanner_service: CustomRegexScannerService,
mock_repo_files_repository: MagicMock,
custom_regex_patterns: list[str],
mock_open_fn: MagicMock,
mock_re: MagicMock,
):
mock_repo_files_repository.list_staged_files.return_value = [".secureli.yaml"]

scan_result = custom_scanner_service.scan_repo(
scan_result = custom_regex_scanner_service.scan_repo(
folder_path=test_folder_path,
scan_mode=ScanMode.STAGED_ONLY,
custom_regex_patterns=custom_regex_patterns,
Expand All @@ -113,7 +115,7 @@ def test_that_custom_scanner_service_ignores_secureli_yaml(


def test_that_pii_scanner_service_only_scans_specific_files_if_provided(
custom_scanner_service: CustomScannerService,
custom_regex_scanner_service: CustomRegexScannerService,
mock_repo_files_repository: MagicMock,
custom_regex_patterns: list[str],
mock_open_fn: MagicMock,
Expand All @@ -125,7 +127,7 @@ def test_that_pii_scanner_service_only_scans_specific_files_if_provided(
specified_file,
ignored_file,
]
scan_result = custom_scanner_service.scan_repo(
scan_result = custom_regex_scanner_service.scan_repo(
folder_path=test_folder_path,
scan_mode=ScanMode.STAGED_ONLY,
files=[specified_file],
Expand All @@ -138,13 +140,13 @@ def test_that_pii_scanner_service_only_scans_specific_files_if_provided(


def test_that_pii_scanner_prints_when_exceptions_encountered(
custom_scanner_service: CustomScannerService,
custom_regex_scanner_service: CustomRegexScannerService,
custom_regex_patterns: list[str],
mock_open_fn: MagicMock,
mock_echo: MagicMock,
):
mock_open_fn.side_effect = Exception("Oh no")
custom_scanner_service.scan_repo(
custom_regex_scanner_service.scan_repo(
folder_path=test_folder_path,
scan_mode=ScanMode.STAGED_ONLY,
custom_regex_patterns=custom_regex_patterns,
Expand Down

0 comments on commit 9717c53

Please sign in to comment.