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

[WIP] use bandit.yml as default config file (resolves #318) #458

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
22 changes: 12 additions & 10 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -295,26 +295,28 @@ An optional config file may be supplied and may include:

Per Project Command Line Args
-----------------------------
Projects may include a `.bandit` file that specifies command line arguments
Projects may include a `.bandit.yml` file that specifies command line arguments
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You have .bandit.yml now

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

my bad, I will make sure that every file mentions the same config filename

that should be supplied for that project. The currently supported arguments
are:

- targets: comma separated list of target dirs/files to run bandit on
- exclude: comma separated list of excluded paths
- skips: comma separated list of tests to skip
- tests: comma separated list of tests to run
- targets: list of target dirs/files to run bandit on
- exclude: list of excluded paths
- skips: list of tests to skip
- tests: list of tests to run

To use this, put a .bandit file in your project's directory. For example:
To use this, put a .bandit.yml file in your project's directory. For example:

::

[bandit]
exclude: /test
exclude:
- /test

::

[bandit]
tests: B101,B102,B301
tests:
- B101
- B102
- B301


Exclusions
Expand Down
3 changes: 1 addition & 2 deletions bandit/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
from bandit.core import utils


BASE_CONFIG = 'bandit.yaml'
LOG = logging.getLogger()


Expand Down Expand Up @@ -291,7 +290,7 @@ def main():
"{relpath:20.20s}: {line:03}: {test_id:^8}: DEFECT: {msg:>20}"

See python documentation for more information about formatting style:
https://docs.python.org/3.4/library/string.html
https://docs.python.org/3/library/string.html
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an unrelated change. Please put in a separate PR.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just wanted to make my PR green :P


The following tests were discovered and loaded:
-----------------------------------------------
Expand Down
4 changes: 4 additions & 0 deletions bandit/core/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
# under the License.

import logging
import os.path

import yaml

Expand All @@ -23,6 +24,7 @@
from bandit.core import utils


BASE_CONFIG = 'bandit.yml'
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is in disagreement with README file, where name .bandit.yml is used.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, there are two configuration files. One, .bandit is to put command line options. The other, the bandit.yml via -c is for more extensive options.

However this patch is making it even more confusing.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LOG = logging.getLogger(__name__)


Expand All @@ -39,6 +41,8 @@ def __init__(self, config_file=None):
self.config_file = config_file
self._config = {}

if config_file is None and os.path.exists(BASE_CONFIG):
config_file = BASE_CONFIG
if config_file:
try:
f = open(config_file, 'r')
Expand Down
5 changes: 2 additions & 3 deletions tests/unit/cli/test_baseline.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def test_bandit_baseline(self):
git_repo.index.commit('Initial commit')
os.chdir(repo_directory)

with open('bandit.yaml', 'wt') as fd:
with open('bandit.yml', 'wt') as fd:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this being changed?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

because thats the new default filename and it seems fitting that we should test "default" YAML filename as part of baseline tests
other tests already test of custom provided filename

I will likely change the default filename to .bandit.yml or bandit.yaml (in that add a test case with custom filename) because neither PyCQA or openstack is using bandit.yml - I just took that name from one of the comments from #318 discussion.

fd.write(config)

# create three branches, first has only benign, second adds malicious,
Expand All @@ -102,8 +102,7 @@ def test_bandit_baseline(self):
'benign_two.py'],
'expected_return': 0}]

baseline_command = ['bandit-baseline', '-c', 'bandit.yaml', '-r', '.',
'-p', 'test']
baseline_command = ['bandit-baseline', '-r', '.', '-p', 'test']
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this changed?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

baseline tests should test the basic features - or at least that was my guess, so for for YAML that would be default filename
there are other tests for custom named YAML config files


for branch in branches:
branch['branch'] = git_repo.create_head(branch['name'])
Expand Down