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

Add default ini file + enable recursive option in ini #503

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
321 changes: 157 additions & 164 deletions bandit/cli/main.py

Large diffs are not rendered by default.

22 changes: 18 additions & 4 deletions bandit/core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@
except ImportError:
import ConfigParser as configparser

BOOL_PARAMS = [
'recursive',
'verbose',
'debug',
'quiet',
'ignore-nosec',
'exit-zero'
]
LOG = logging.getLogger(__name__)


Expand Down Expand Up @@ -349,12 +357,18 @@ def parse_ini_file(f_loc):
config = configparser.ConfigParser()
try:
config.read(f_loc)
return {k: v for k, v in config.items("bandit")}
options = {k: v for k, v in config['bandit'].items()}
for bool_param in BOOL_PARAMS:
if config['bandit'].getboolean(bool_param) is not None:
options.update(
{bool_param: config['bandit'].getboolean(bool_param)}
)

return options

except (configparser.Error, KeyError, TypeError):
LOG.warning(
"Unable to parse config file %s or missing [bandit] " "section",
f_loc,
LOG.debug("Config file %s not found or missing [bandit] "
"section", f_loc,
)

return None
Expand Down
1 change: 1 addition & 0 deletions examples/ini_file_test/okay.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
print('hopefully no vulnerabilities here')
1 change: 1 addition & 0 deletions examples/ini_file_test/recursive2/okay.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
print('hopefully no vulnerabilities here')
2 changes: 2 additions & 0 deletions examples/ini_file_test/setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[bandit]
recursive=false
1 change: 1 addition & 0 deletions examples/ini_file_test_2/okay.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
print('hopefully no vulnerabilities here')
1 change: 1 addition & 0 deletions examples/ini_file_test_2/recursive2/okay.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
print('hopefully no vulnerabilities here')
2 changes: 2 additions & 0 deletions examples/ini_file_test_2/setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[bandit]
recursive=true
1 change: 1 addition & 0 deletions examples/recursive1/okay.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
print('hopefully no vulnerabilities here')
1 change: 1 addition & 0 deletions examples/recursive1/recursive2/okay.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
print('hopefully no vulnerabilities here')
36 changes: 27 additions & 9 deletions tests/functional/test_runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,6 @@ def _test_example(self, cmdlist, targets):
cmdlist.append(os.path.join(os.getcwd(), "examples", t))
return self._test_runtime(cmdlist)

def test_no_arguments(self):
(retcode, output) = self._test_runtime(
[
"bandit",
]
)
self.assertEqual(2, retcode)
self.assertIn("usage: bandit [-h]", output)

def test_piped_input(self):
with open("examples/imports.py") as infile:
(retcode, output) = self._test_runtime(["bandit", "-"], infile)
Expand Down Expand Up @@ -91,6 +82,33 @@ def test_example_okay(self):
self.assertIn("Files skipped (0):", output)
self.assertIn("No issues identified.", output)

def test_recurse(self):
(retcode, output) = self._test_runtime(['bandit', '-r',
'examples/recursive1'])
self.assertEqual(0, retcode)
self.assertIn("Total lines of code: 2", output)
self.assertIn("Files skipped (0):", output)
self.assertIn("No issues identified.", output)

def test_ini_file_override(self):
"""Tests that ini file properly overrides default values"""
(retcode, output) = self._test_runtime(
['bandit', 'examples/ini_file_test_2', '--ini', 'examples/ini_file_test_2/setup.cfg'])
self.assertEqual(0, retcode)
self.assertIn("Total lines of code: 2", output)
self.assertIn("Files skipped (0):", output)
self.assertIn("No issues identified.", output)

def test_cli_before_ini_file(self):
"""Tests that ini file doesn't override cli args"""
(retcode, output) = self._test_runtime(
['bandit', '-r', 'examples/ini_file_test', '--ini', 'examples/ini_file_test/setup.cfg']
)
self.assertEqual(0, retcode)
self.assertIn("Total lines of code: 2", output)
self.assertIn("Files skipped (0):", output)
self.assertIn("No issues identified.", output)

def test_example_nonsense(self):
(retcode, output) = self._test_example(
[
Expand Down
9 changes: 5 additions & 4 deletions tests/unit/cli/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def tearDown(self):
def test_get_options_from_ini_no_ini_path_no_target(self):
# Test that no config options are loaded when no ini path or target
# directory are provided
self.assertIsNone(bandit._get_options_from_ini(None, []))
self.assertEqual(bandit._get_options_from_ini(None, []), {})

def test_get_options_from_ini_empty_directory_no_target(self):
# Test that no config options are loaded when an empty directory is
Expand All @@ -96,10 +96,11 @@ def test_get_options_from_ini_empty_directory_no_target(self):

def test_get_options_from_ini_no_ini_path_no_bandit_files(self):
# Test that no config options are loaded when no ini path is provided
# and the target directory contains no bandit config files (.bandit)
# and the target directory contains no bandit config files
target_directory = self.useFixture(fixtures.TempDir()).path
self.assertIsNone(
bandit._get_options_from_ini(None, [target_directory])
self.assertEqual(
bandit._get_options_from_ini(None, [target_directory]),
{}
)

def test_get_options_from_ini_no_ini_path_multi_bandit_files(self):
Expand Down