Skip to content

Commit

Permalink
Better error if plugin file in baseline not found
Browse files Browse the repository at this point in the history
Fixes: #718
  • Loading branch information
msabramo committed Oct 26, 2023
1 parent 44095a0 commit 0a76035
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
12 changes: 11 additions & 1 deletion detect_secrets/core/plugins/initialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,17 @@ def from_plugin_classname(classname: str) -> Plugin:
"""
:raises: TypeError
"""
for plugin_type in get_mapping_from_secret_type_to_class().values():
try:
plugin_types = get_mapping_from_secret_type_to_class().values()
except FileNotFoundError as e:
log.error(f'Error: Failed to load `{classname}` plugin: {e}')
log.error(
'This error can occur when using a baseline that references a '
'custom plugin with a path that does not exist.',
)
raise

for plugin_type in plugin_types:
if plugin_type.__name__ == classname:
break
else:
Expand Down
8 changes: 6 additions & 2 deletions detect_secrets/core/scan.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,12 @@ def scan_line(line: str) -> Generator[PotentialSecret, None, None]:


def scan_file(filename: str) -> Generator[PotentialSecret, None, None]:
if not get_plugins(): # pragma: no cover
log.error('No plugins to scan with!')
try:
if not get_plugins(): # pragma: no cover
log.error('No plugins to scan with!')
return
except FileNotFoundError as e:
log.error('Unable to load plugins!')
return

if _is_filtered_out(required_filter_parameters=['filename'], filename=filename):
Expand Down
4 changes: 3 additions & 1 deletion detect_secrets/util/importlib.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import errno
import importlib.util
import os
import pkgutil
Expand Down Expand Up @@ -85,7 +86,8 @@ def import_file_as_module(filename: str, name: Optional[str] = None) -> ModuleTy
for you.
"""
if not os.path.exists(filename):
raise FileNotFoundError
# Source: https://stackoverflow.com/a/36077407
raise FileNotFoundError(errno.ENOENT, os.strerror(errno.ENOENT), filename)

if not name:
# NOTE: After several trial and error attempts, I could not discern the importance
Expand Down

0 comments on commit 0a76035

Please sign in to comment.