Skip to content

Commit

Permalink
Added verbose logging
Browse files Browse the repository at this point in the history
  • Loading branch information
zbalkan committed Sep 17, 2023
1 parent 6703f96 commit 37f7006
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 15 deletions.
7 changes: 3 additions & 4 deletions src/PANFile.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,12 @@ def set_error(self, error_msg: str) -> None:
self.errors.append(error_msg)
logging.error(f'{error_msg} ({self.path})')

# def scan_with(self, dispatcher: Dispatcher) -> list[PAN]:
def scan_with(self, dispatcher: Dispatcher) -> list:
def scan_with(self, dispatcher: Dispatcher, verbose: bool) -> list:
"""Checks the file for matching regular expressions: if a ZIP then each file in the ZIP (recursively) or the text in a document"""
if verbose:
logging.info(f'Scanning file: {self.path} ({self.mime_type})')

try:
# match_list: list[PAN] = dispatcher.dispatch(
# mime_type=self.mime_type, extension=self.extension, path=self.path, encoding=self.encoding)
match_list: list = dispatcher.dispatch(
mime_type=self.mime_type, extension=self.extension, path=self.path, encoding=self.encoding)
if len(match_list) > 0:
Expand Down
28 changes: 19 additions & 9 deletions src/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class PANHuntConfiguration:
excluded_directories: list
# excluded_pans: list[str]
excluded_pans: list
verbose: bool

def __init__(self) -> None:
if os.name == 'nt':
Expand All @@ -43,7 +44,8 @@ def with_args(self,
mask_pans: bool = False,
excluded_directories_string: Optional[str] = None,
excluded_pans_string: Optional[str] = None,
json_dir: Optional[str] = None) -> None:
json_dir: Optional[str] = None,
verbose: bool = False) -> None:
"""If any parameter is provided, it overwrites the previous value
"""

Expand All @@ -53,7 +55,8 @@ def with_args(self,
json_dir=json_dir,
mask_pans=mask_pans,
excluded_directories_string=excluded_directories_string,
excluded_pans_string=excluded_pans_string)
excluded_pans_string=excluded_pans_string,
verbose=verbose)

def with_file(self, config_file: str) -> None:
"""If a config file provided and it has specific values, they overwrite the previous values
Expand Down Expand Up @@ -81,14 +84,17 @@ def with_file(self, config_file: str) -> None:
config_from_file)
excluded_pans_string: Optional[str] = PANHuntConfiguration.__try_parse(
config_from_file=config_from_file, property='excludepans')
verbose: bool = PANHuntConfiguration.__check_verbose(
config_from_file=config_from_file)

self.__update(search_dir=search_dir,
file_path=file_path,
report_dir=report_dir,
json_dir=json_dir,
mask_pans=mask_pans,
excluded_directories_string=excluded_directories_string,
excluded_pans_string=excluded_pans_string)
excluded_pans_string=excluded_pans_string,
verbose=verbose)

def get_json_path(self) -> Optional[str]:
if self.json_dir:
Expand Down Expand Up @@ -117,11 +123,11 @@ def __check_masked(config_from_file) -> Optional[bool]:
return mask_pans

@staticmethod
def __get_search_pdf(config_from_file) -> Optional[bool]:
mask_pans: Optional[bool] = None
if 'pdf' in config_from_file:
mask_pans = (config_from_file['pdf'].upper() == 'TRUE')
return mask_pans
def __check_verbose(config_from_file) -> bool:
is_verbose: bool = False
if 'verbose' in config_from_file:
is_verbose = (config_from_file['verbose'].upper() == 'TRUE')
return is_verbose

@staticmethod
def __try_parse(config_from_file: dict, property: str) -> Optional[str]:
Expand All @@ -136,7 +142,8 @@ def __update(self,
json_dir: Optional[str],
mask_pans: Optional[bool],
excluded_directories_string: Optional[str],
excluded_pans_string: Optional[str]) -> None:
excluded_pans_string: Optional[str],
verbose: bool) -> None:

if search_dir and search_dir != 'None':
self.search_dir = os.path.abspath(path=search_dir)
Expand Down Expand Up @@ -165,3 +172,6 @@ def __update(self,
self.json_dir = panutils.get_root_dir()
else:
self.json_dir = os.path.abspath(json_dir)

if verbose:
self.verbose = verbose
3 changes: 2 additions & 1 deletion src/hunter.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ def scan_files(self) -> Generator[tuple, None, None]:

for pan_file in self.__all_files:
# matches: list[PAN] = pan_file.scan_with(dispatcher=dispatcher)
matches: list = pan_file.scan_with(dispatcher=dispatcher)
matches: list = pan_file.scan_with(
dispatcher=dispatcher, verbose=self.__conf.verbose)
matches_found += len(matches)
files_completed += 1
yield matches_found, files_completed
Expand Down
6 changes: 5 additions & 1 deletion src/panhunt.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@ def main() -> None:
'-X', dest='exclude_pan', help='PAN to exclude from search')
arg_parser.add_argument('-q', dest='quiet', action='store_true',
default=False, help='No terminal output')
arg_parser.add_argument('-v', dest='verbose', action='store_true',
default=False, help='Verbose logging')
arg_parser.add_argument('-c', dest='check_file_hash',
help=argparse.SUPPRESS) # hidden argument

Expand All @@ -173,6 +175,7 @@ def main() -> None:
json_dir: Optional[str] = args.json_dir
config_file: Optional[str] = args.config
quiet: bool = args.quiet
verbose: bool = args.verbose

# Initiated with default values
config: PANHuntConfiguration = PANHuntConfiguration()
Expand All @@ -189,7 +192,8 @@ def main() -> None:
json_dir=json_dir,
mask_pans=mask_pans,
excluded_directories_string=excluded_directories_string,
excluded_pans_string=excluded_pans_string)
excluded_pans_string=excluded_pans_string,
verbose=verbose)

report: Report = hunt_pans(quiet=quiet, configuration=config)

Expand Down

0 comments on commit 37f7006

Please sign in to comment.