import logging
from typing import List
from credsweeper.common.constants import RuleType
from credsweeper.config.config import Config
from credsweeper.credentials.candidate import Candidate
from credsweeper.file_handler.analysis_target import AnalysisTarget
from credsweeper.rules.rule import Rule
from credsweeper.scanner.scan_type.scan_type import ScanType
from credsweeper.utils.pem_key_detector import PemKeyDetector
logger = logging.getLogger(__name__)
[docs]
class PemKeyPattern(ScanType):
"""Scanner detects single PEM private key in target from current line"""
[docs]
@classmethod
def run(cls, config: Config, rule: Rule, target: AnalysisTarget) -> List[Candidate]:
"""Check if target is a PEM key
Args:
config: user configs
rule: Rule object to check current line. Should be a pem-pattern rule
target: Analysis target
Return:
List of Candidate objects if pattern defined in a rule is present in a line
and filters defined in rule do not remove current line. Empty list - otherwise
"""
if RuleType.PEM_KEY != rule.rule_type:
raise ValueError(f"Rule `{rule}` provided to `{cls.__name__}`.run "
f"should have pattern_type equal to `{RuleType.PEM_KEY.value}`")
for candidate in cls._get_candidates(config, rule, target):
if pem_lines := PemKeyDetector(config).detect_pem_key(candidate.line_data_list[0], target):
candidate.line_data_list = pem_lines
return [candidate]
return []