Skip to content

Commit

Permalink
SourceType applied to unify usage 'doc' and 'src'
Browse files Browse the repository at this point in the history
  • Loading branch information
babenek committed Jul 13, 2023
1 parent ffc8fef commit 3bf97ce
Show file tree
Hide file tree
Showing 13 changed files with 37 additions and 27 deletions.
10 changes: 5 additions & 5 deletions credsweeper/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from credsweeper import __version__
from credsweeper.app import APP_PATH, CredSweeper
from credsweeper.common.constants import ThresholdPreset, Severity, RuleType, DiffRowType
from credsweeper.common.constants import ThresholdPreset, Severity, RuleType, DiffRowType, SourceType
from credsweeper.file_handler.files_provider import FilesProvider
from credsweeper.file_handler.patch_provider import PatchProvider
from credsweeper.file_handler.text_provider import TextProvider
Expand Down Expand Up @@ -158,8 +158,8 @@ def get_arguments() -> Namespace:
default=0,
required=False,
metavar="POSITIVE_INT")
parser.add_argument("--src", help="sources-specific scanning", dest="src", action="store_true")
parser.add_argument("--doc", help="document-specific scanning", dest="doc", action="store_true")
parser.add_argument("--src", help="sources-specific scanning", dest=SourceType.SRC.value, action="store_true")
parser.add_argument("--doc", help="document-specific scanning", dest=SourceType.DOC.value, action="store_true")
parser.add_argument("--ml_threshold",
help="setup threshold for the ml model. "
"The lower the threshold - the more credentials will be reported. "
Expand Down Expand Up @@ -270,9 +270,9 @@ def scan(args: Namespace, content_provider: FilesProvider, json_filename: Option

usage_list: List[str] = []
if args.src:
usage_list.append("src")
usage_list.append(SourceType.SRC.value)
if args.doc:
usage_list.append("doc")
usage_list.append(SourceType.DOC.value)

credsweeper = CredSweeper(rule_path=args.rule_path,
config_path=args.config_path,
Expand Down
6 changes: 3 additions & 3 deletions credsweeper/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# Directory of credsweeper sources MUST be placed before imports to avoid circular import error
APP_PATH = Path(__file__).resolve().parent

from credsweeper.common.constants import KeyValidationOption, Severity, ThresholdPreset
from credsweeper.common.constants import KeyValidationOption, Severity, ThresholdPreset, SourceType
from credsweeper.config import Config
from credsweeper.credentials import Candidate, CredentialManager
from credsweeper.deep_scanner.deep_scanner import DeepScanner
Expand Down Expand Up @@ -90,7 +90,7 @@ def __init__(self,
use_filters=use_filters,
find_by_ext=find_by_ext,
depth=depth,
usage_list=usage_list or ["src"],
usage_list=usage_list or [SourceType.SRC.value],
severity=severity,
size_limit=size_limit,
exclude_lines=exclude_lines,
Expand Down Expand Up @@ -303,7 +303,7 @@ def file_scan(self, content_provider: Union[DiffContentProvider, TextContentProv
candidates.append(dummy_candidate)

else:
if self.config.depth or "doc" in self.config.usage_list:
if self.config.depth or SourceType.DOC.value in self.config.usage_list:
# deep scan with possible data representation
candidates = self.deep_scanner.scan(content_provider, self.config.depth, self.config.size_limit)
else:
Expand Down
6 changes: 6 additions & 0 deletions credsweeper/common/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ def get(severity: Union[str, "Severity"]) -> Optional["Severity"]:
return None


class SourceType(Enum):
"""Stores types of sources to configure rules and filters"""
SRC = "src"
DOC = "doc"


class Base(Enum):
"""Stores types of character sets in lower case"""
base36 = "base36"
Expand Down
6 changes: 3 additions & 3 deletions credsweeper/filters/group/group.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from abc import ABC
from typing import List

from credsweeper.common.constants import GroupType
from credsweeper.common.constants import GroupType, SourceType
from credsweeper.config import Config
from credsweeper.filters import (Filter, LineSpecificKeyCheck, SeparatorUnusualCheck, ValueAllowlistCheck,
ValueArrayDictionaryCheck, ValueBlocklistCheck, ValueCamelCaseCheck,
Expand Down Expand Up @@ -51,9 +51,9 @@ def get_keyword_base_filters(config: Config) -> List[Filter]:
ValueTokenCheck(),
VariableNotAllowedPatternCheck(),
]
if "src" in config.usage_list:
if SourceType.SRC.value in config.usage_list:
filters.extend([ValuePatternCheck(config), ValueNotAllowedPatternCheck()])
if "doc" in config.usage_list:
if SourceType.DOC.value in config.usage_list:
filters.append(ValueMaskedCheck())
return filters

Expand Down
4 changes: 2 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import pytest

from credsweeper.app import APP_PATH
from credsweeper.common.constants import Severity, SourceType
from credsweeper.config import Config
from credsweeper.common.constants import Severity
from credsweeper.rules import Rule
from credsweeper.scanner import Scanner
from credsweeper.utils import Util
Expand Down Expand Up @@ -39,7 +39,7 @@ def config() -> Config:
config_dict["use_filters"] = True
config_dict["find_by_ext"] = False
config_dict["depth"] = 0
config_dict["usage_list"] = ["src"]
config_dict["usage_list"] = [SourceType.SRC.value]
config_dict["find_by_ext_list"] = [".txt", ".inf"]
config_dict["size_limit"] = None
config_dict["min_keyword_value_length"] = 4
Expand Down
3 changes: 2 additions & 1 deletion tests/data/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from typing import Dict, Any, List

from credsweeper.common.constants import SourceType
from tests import SAMPLES_POST_CRED_COUNT, SAMPLES_IN_DEEP_3, SAMPLES_CRED_COUNT, SAMPLES_ML_DOC

DATA_TEST_CFG: List[Dict[str, Any]] = [{
Expand All @@ -10,7 +11,7 @@
"__cred_count": SAMPLES_ML_DOC,
"sort_output": True,
"json_filename": "doc.json",
"usage_list": ["doc"]
"usage_list": [SourceType.DOC.value]
}, {
"__cred_count": SAMPLES_IN_DEEP_3,
"sort_output": True,
Expand Down
4 changes: 2 additions & 2 deletions tests/filters/test_value_array_dictionary_check.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pytest

from credsweeper.file_handler.analysis_target import AnalysisTarget
from credsweeper.common.constants import SourceType
from credsweeper.filters import ValueArrayDictionaryCheck, VariableNotAllowedPatternCheck
from credsweeper.rules import Rule
from tests.filters.conftest import LINE_VALUE_PATTERN, DUMMY_ANALYSIS_TARGET
Expand All @@ -19,7 +19,7 @@ def token_rule(self, config) -> Rule:
"filter_type": [VariableNotAllowedPatternCheck.__name__],
"use_ml": True,
"min_line_len": 0,
"usage_list": ["src", "doc"],
"usage_list": [SourceType.SRC.value, SourceType.DOC.value],
"validations": []
}
rule = Rule(config, token_rule_without_filters)
Expand Down
3 changes: 2 additions & 1 deletion tests/filters/test_value_similarity_check.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import pytest

from credsweeper.common.constants import SourceType
from credsweeper.filters import ValueSimilarityCheck, VariableNotAllowedPatternCheck
from credsweeper.rules import Rule
from tests.filters.conftest import DUMMY_ANALYSIS_TARGET
Expand All @@ -17,7 +18,7 @@ def password_rule(self, config) -> Rule:
"values": ["password|passwd|pwd"],
"filter_type": [VariableNotAllowedPatternCheck.__name__],
"use_ml": True,
"usage_list": ["src", "doc"],
"usage_list": [SourceType.SRC.value, SourceType.DOC.value],
"min_line_len": 0,
"validations": []
}
Expand Down
3 changes: 2 additions & 1 deletion tests/ml_model/test_ml_validator.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from credsweeper import ThresholdPreset
from credsweeper.app import APP_PATH
from credsweeper.common.constants import SourceType

from credsweeper.config import Config
from credsweeper.credentials import Candidate
Expand All @@ -17,7 +18,7 @@ def test_ml_validator_simple_p():
config_dict["use_filters"] = True
config_dict["find_by_ext"] = False
config_dict["depth"] = 0
config_dict["usage_list"] = ["src"]
config_dict["usage_list"] = [SourceType.SRC.value]
config_dict["find_by_ext_list"] = []
config_dict["size_limit"] = None
config = Config(config_dict)
Expand Down
6 changes: 3 additions & 3 deletions tests/rules/test_rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import pytest

from credsweeper.common.constants import Severity
from credsweeper.common.constants import Severity, SourceType
from credsweeper.config import Config
from credsweeper.filters.group import GeneralPattern
from credsweeper.rules import Rule
Expand All @@ -22,7 +22,7 @@ class TestRuleConfigParsing:
"min_line_len": 32,
"use_ml": False,
"validations": [],
"usage_list": ["src", "doc"]
"usage_list": [SourceType.SRC.value, SourceType.DOC.value]
},
# Check proper config with no validations
{
Expand All @@ -33,7 +33,7 @@ class TestRuleConfigParsing:
"filter_type": GeneralPattern.__name__,
"min_line_len": 32,
"use_ml": False,
"usage_list": ["src", "doc"]
"usage_list": [SourceType.SRC.value, SourceType.DOC.value]
},
])
def rule_config(self, request: str) -> Any:
Expand Down
6 changes: 3 additions & 3 deletions tests/scanner/scan_type/test_multipattern.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import random
import string
import unittest
from unittest.mock import patch, MagicMock
from unittest.mock import MagicMock

from credsweeper.common.constants import MAX_LINE_LENGTH
from credsweeper.common.constants import MAX_LINE_LENGTH, SourceType
from credsweeper.config import Config
from credsweeper.file_handler.analysis_target import AnalysisTarget
from credsweeper.rules import Rule
Expand All @@ -22,7 +22,7 @@ def setUp(self) -> None:
"name": "MULTI_PATTERN_RULE",
"severity": "info",
"type": "pattern",
"usage_list": ["src"],
"usage_list": [SourceType.SRC.value],
"values": ["a", "b"],
"filter_type": [],
"min_line_len": 0,
Expand Down
4 changes: 2 additions & 2 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from credsweeper.__main__ import EXIT_FAILURE, EXIT_SUCCESS
from credsweeper.app import APP_PATH
from credsweeper.app import CredSweeper
from credsweeper.common.constants import ThresholdPreset, Severity
from credsweeper.common.constants import ThresholdPreset, Severity, SourceType
from credsweeper.credentials import Candidate
from credsweeper.file_handler.files_provider import FilesProvider
from credsweeper.file_handler.text_content_provider import TextContentProvider
Expand Down Expand Up @@ -696,7 +696,7 @@ def test_credit_card_number_n(self) -> None:

def test_doc_p(self) -> None:
content_provider: FilesProvider = TextProvider([SAMPLES_PATH / "test.html"])
cred_sweeper = CredSweeper(usage_list=["doc"])
cred_sweeper = CredSweeper(usage_list=[SourceType.DOC.value])
cred_sweeper.run(content_provider=content_provider)
found_credentials = cred_sweeper.credential_manager.get_credentials()
expected_credential_lines = [
Expand Down
3 changes: 2 additions & 1 deletion tests/test_utils/dummy_line_data.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import re

from credsweeper.app import APP_PATH
from credsweeper.common.constants import SourceType
from credsweeper.config import Config
from credsweeper.credentials import LineData
from credsweeper.utils import Util
Expand All @@ -14,7 +15,7 @@ def config() -> Config:
config_dict["use_filters"] = True
config_dict["find_by_ext"] = False
config_dict["depth"] = 0
config_dict["usage_list"] = ["src"]
config_dict["usage_list"] = [SourceType.SRC.value]
config_dict["size_limit"] = None
return Config(config_dict)

Expand Down

0 comments on commit 3bf97ce

Please sign in to comment.