pants icon indicating copy to clipboard operation
pants copied to clipboard

Bandit plugin used by flake8 can't read the config from the bandit's config

Open ShantanuKumar opened this issue 2 years ago • 16 comments

Describe the bug flake8-bandit isn't able to read the config used by bandit itself. I suppose this is just not about this particular plugin but any such plugin which flake8 uses.

Pants version 2.10.0

OS macOS

Additional info https://pantsbuild.slack.com/archives/C046T6T9U/p1650639627749519

ShantanuKumar avatar Apr 22 '22 15:04 ShantanuKumar

So the issue is that flake8 has plugins, and those plugins could be expecting config files Pants doesn't know to grab. There's really two solutions here (and maybe we should split into two issues):

  1. (Generic) Allow people to specify more than one config for [flake8] (in this case, the flake8 and the bandit configs)
  2. (This issue) Add bandti as a first-class tool in Python

thejcannon avatar Apr 22 '22 15:04 thejcannon

How do you normally specify the config file for flake8-bandit? Is it auto-discovered? The issue with making [flake8].config a list is that we not only make sure to include the file in the sandbox, but also explicitly said argv for you.

The most specific yet awkward way to solve this would be something like:

[flake8]
plugin_config = [["bandit.yaml", "--bandit-config=bandit.yaml"]]

or

[flake8]
argv.add = ["--bandit-config=bandit.yaml"]
plugin_config = ["bandit.yaml"]

Eric-Arellano avatar Apr 22 '22 17:04 Eric-Arellano

They say that using .bandit should work. This is how my .bandit looks like

skips: ["B101"]

try_except_continue:
  check_typed_exception: false
try_except_pass:
  check_typed_exception: false

ShantanuKumar avatar Apr 22 '22 19:04 ShantanuKumar

Cool. So then in your case, this would look like:

[flake8]
plugin_config = [[".bandit", ""]]  # i.e. no args

or

[flake8]
plugin_config = [".bandit"]

That second one is much clearer in my opinion. And maybe we make this even more generic? That option tells pants to find that file on your local file system and include it in the sandbox.

[flake8]
extra_files = [".bandit"]

Wdyt?

Eric-Arellano avatar Apr 22 '22 19:04 Eric-Arellano

yeah either of the two i.e. extra_files or plugin_config would work.

ShantanuKumar avatar Apr 23 '22 15:04 ShantanuKumar

Cool, I like the extra_files approach. Given that pants already supports bandit as a dedicated integration, is this a blocker for you or only a nice to have?

Eric-Arellano avatar Apr 25 '22 22:04 Eric-Arellano

It's not a blocker. Just nice to have.

ShantanuKumar avatar Apr 26 '22 07:04 ShantanuKumar

Sounds good. Any interest in contributing this change? I don't think it would be super hard to pull off, and I'm happy to give some thoughts on how to do it

Eric-Arellano avatar Apr 26 '22 14:04 Eric-Arellano

I will take a look and let you know.

ShantanuKumar avatar Apr 27 '22 09:04 ShantanuKumar

Hey @Eric-Arellano I would like to tackle this now. Could you give me some pointers on how should I go about it?

ShantanuKumar avatar Jul 22 '22 15:07 ShantanuKumar

Hey @ShantanuKumar sorry for the delay! Was moving apartments and OOO.

  1. Add a new option extra_files to bandit/subsystem.py, which will be advanced=True and type FileListOption
  2. Add Get(Digest, PathGlobs()) here for the extra_files. Error if the globs are not matched, similar to second snippet. Include the digest in the MergeDigests

https://github.com/pantsbuild/pants/blob/40e17902092a9b7414473f99bf32e3f3c84e5921/src/python/pants/backend/python/lint/bandit/rules.py#L52-L68

https://github.com/pantsbuild/pants/blob/40e17902092a9b7414473f99bf32e3f3c84e5921/src/python/pants/core/util_rules/config_files.py#L64-L71

I think we likely want to add a test to bandit/rules_integration_test.py, although tests do have a downside with slower CI and more code, so it's a tradeoff. I wonder if you can modify a current test like test_3rdparty_plugin to test both things in the same test. Otherwise, a dedicated test is probably worth it.

Eric-Arellano avatar Aug 03 '22 20:08 Eric-Arellano

If I understand it correctly shouldn't these changes be made to flake8? The plugin flake8-bandit is for flake8 to use bandit.

ShantanuKumar avatar Aug 05 '22 18:08 ShantanuKumar

Ah, yep. You're right

Eric-Arellano avatar Aug 05 '22 18:08 Eric-Arellano

Hey @Eric-Arellano For the 2nd step

Add Get(Digest, PathGlobs()) here for the extra_files

What value should I pass for the argument globs of PathGlobs ?

ShantanuKumar avatar Aug 10 '22 16:08 ShantanuKumar

flake8_subsystem.extra_files, assuming you used FileListOption in step 1. That will be a list[str], which we want.

Eric-Arellano avatar Aug 10 '22 17:08 Eric-Arellano

~~Yeah but I feel like based on different plugins, this can be quite varied. E.g. if it's flake8-bandit, the glob could be [".bandit"]. If it's flake8-pydocstyle, it could be [".pydocstyle"]. I don't see a clear glob pattern here.~~

EDIT: Sorry, I understand now you mean flake8_subsystem.extra_files

ShantanuKumar avatar Aug 10 '22 17:08 ShantanuKumar