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

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

Closed
1 of 2 tasks
msabramo opened this issue Oct 26, 2023 · 1 comment · Fixed by #719
Closed
1 of 2 tasks

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

msabramo opened this issue Oct 26, 2023 · 1 comment · Fixed by #719
Labels
pending The issue still needs to be reviewed by one of the maintainers.

Comments

@msabramo
Copy link
Contributor

msabramo commented Oct 26, 2023

  • I'm submitting a ...

    • bug report
    • feature request
  • What is the current behavior?
    If you run something like detect-secrets-hook --baseline .secrets.baseline setup.py and secrets.baseline references a custom plugin that doesn't exist, you get a FileNotFoundError with a long traceback, but unfortunately it doesn't tell you which file is missing.

  • If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal demo of the problem

$ cat .secrets.baseline
{
  "version": "1.4.0",
  "plugins_used": [
    {
      "name": "FakeCustomPlugin",
      "path": "file://./path/to/plugin/that/does/not/exist/plugin.py"
    }
  ],
  "filters_used": [],
  "results": {}
}

$ 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

$ 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 <module>
    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 <listcomp>
    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 added a commit to msabramo/detect-secrets that referenced this issue Oct 26, 2023
msabramo added a commit to msabramo/detect-secrets that referenced this issue Oct 26, 2023
msabramo added a commit to msabramo/detect-secrets that referenced this issue Oct 26, 2023
@msabramo msabramo changed the title FileNotFoundError with traceback displayed if baseline references plugin files that don't exist Should include filename in error displayed if baseline references plugin file that doesn't exist Oct 26, 2023
@msabramo msabramo changed the title Should include filename in error displayed if baseline references plugin file that doesn't exist Should include filename in error displayed if plugin file in baseline not found Oct 26, 2023
@msabramo
Copy link
Contributor Author

msabramo commented Oct 26, 2023

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!

@lorenzodb1 lorenzodb1 added the pending The issue still needs to be reviewed by one of the maintainers. label Nov 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pending The issue still needs to be reviewed by one of the maintainers.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants