Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include filename in error displayed if plugin file in baseline not found #719

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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:
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
19 changes: 19 additions & 0 deletions tests/core/baseline_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,25 @@ def test_load_and_output():
break


def test_plugin_not_found_in_baseline():
# Test fix for the issue in #718
data = {
'version': '1.4.0',
'plugins_used': [{
'name': 'FakeCustomPlugin',
'path': 'file://./path/to/plugin/that/does/not/exist/plugin.py',
}],
'results': {},
}
secrets = baseline.load(data)
with pytest.raises(FileNotFoundError) as exc_info:
baseline.format_for_output(secrets)

# Check that filename of file that was not found is in the error message
# (#718)
exc_info.match(r'\./path/to/plugin/that/does/not/exist/plugin\.py')


def test_upgrade_does_nothing_if_newer_version():
current_baseline = {'version': '3.0.0'}
assert baseline.upgrade(current_baseline) == current_baseline
Expand Down
Loading