Yelp / detect-secrets

An enterprise friendly way of detecting and preventing secrets in code.
Apache License 2.0
3.59k stars 448 forks source link

Should include filename in error displayed if plugin file in baseline not found #718

Closed msabramo closed 2 months ago

msabramo commented 8 months ago

$ detect-secrets-hook --baseline .secrets.baseline setup.py Traceback (most recent call last): ... File "/Users/abramowi/Code/OpenSource/detect-secrets/detect_secrets/util/importlib.py", line 88, in import_file_as_module raise FileNotFoundError FileNotFoundError


Note that a `FileNotFoundError` is raised, but it doesn't say **which** file was not found.

* **What is the expected behavior?**

A user-friendly error message that tells the user which file was not found. For example, if an invalid plugin is passed in on the command-line:

$ detect-secrets-hook --plugin ./app/detect-secrets/plugins.py --baseline .secrets.baseline ... detect-secrets-hook: error: argument -p/--plugin: ./app/detect-secrets/plugins.py is not a valid file.


So a better behavior for this case would be something like this:

$ detect-secrets-hook --baseline .secrets.baseline setup.py detect-secrets-hook: error: plugin referenced in .secrets.baseline: ./path/to/plugin/that/does/not/exist/plugin.py is not a valid file.


But I'd be happy if it still displayed a traceback, but `FileNotFoundError` was raised with the filename that was not found as a parameter.

* **What is the motivation / use case for changing the behavior?**
To make debugging easier. I spent a bunch of time yesterday troubleshooting an issue like this and this change would've saved me a lot of time.

* **Please tell us about your environment:**
  - detect-secrets Version: 1.4.0
  - Python Version: 3.10
  - OS Version: OS X 12.6.8
  - File type (if applicable):

* **Other information**
<!-- (e.g. detailed explanation, stacktraces, related issues, suggestions how to fix, links for us to have context, eg. Stack Overflow, gitter etc.). If you have or can produce a baseline file, please provide it as part of a separate comment. -->

$ detect-secrets-hook --baseline .secrets.baseline setup.py Traceback (most recent call last): File "/Users/abramowi/Code/OpenSource/detect-secrets/.venv/bin/detect-secrets-hook", line 8, in sys.exit(main()) ^^^^^^ File "/Users/abramowi/Code/OpenSource/detect-secrets/detect_secrets/pre_commit_hook.py", line 30, in main secrets.scan_file(filename) File "/Users/abramowi/Code/OpenSource/detect-secrets/detect_secrets/core/secrets_collection.py", line 75, in scan_file for secret in scan.scan_file(os.path.join(self.root, filename)): File "/Users/abramowi/Code/OpenSource/detect-secrets/detect_secrets/core/scan.py", line 141, in scan_file if not get_plugins(): # pragma: no cover ^^^^^^^^^^^^^ File "/Users/abramowi/Code/OpenSource/detect-secrets/detect_secrets/settings.py", line 266, in get_plugins return [ ^ File "/Users/abramowi/Code/OpenSource/detect-secrets/detect_secrets/settings.py", line 267, in plugins.initialize.from_plugin_classname(classname) File "/Users/abramowi/Code/OpenSource/detect-secrets/detect_secrets/core/plugins/initialize.py", line 34, in from_plugin_classname for plugin_type in get_mapping_from_secret_type_to_class().values(): ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/abramowi/Code/OpenSource/detect-secrets/detect_secrets/core/plugins/util.py", line 43, in get_mapping_from_secret_type_to_class for plugin_class in get_plugins_from_file(filename): File "/Users/abramowi/Code/OpenSource/detect-secrets/detect_secrets/core/plugins/util.py", line 51, in get_plugins_from_file for plugin_class in get_plugins_from_module(import_file_as_module(filename)): ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/abramowi/Code/OpenSource/detect-secrets/detect_secrets/util/importlib.py", line 88, in import_file_as_module raise FileNotFoundError FileNotFoundError

msabramo commented 8 months ago

With #719, the output is:

$ detect-secrets-hook --baseline .secrets.baseline setup.py
[initialize]    ERROR   Error: Failed to load `FakeCustomPlugin` plugin:
    [Errno 2] No such file or directory: './path/to/plugin/that/does/not/exist/plugin.py'
[initialize]    ERROR   This error can occur when using a baseline that references a custom plugin
    with a path that does not exist.
[scan]  ERROR   Unable to load plugins!

which is much more useful for troubleshooting!