From 4fa38ec2564f6a52b7eaee34de4260c4d6c03895 Mon Sep 17 00:00:00 2001 From: Cees Timmerman Date: Sat, 20 Aug 2022 16:19:43 +0200 Subject: [PATCH 1/2] Make -c support ini as well. --- bandit/core/config.py | 17 +++++++++++------ bandit/core/utils.py | 5 ++++- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/bandit/core/config.py b/bandit/core/config.py index 236f357c5..66ad26d22 100644 --- a/bandit/core/config.py +++ b/bandit/core/config.py @@ -58,12 +58,17 @@ def __init__(self, config_file=None): LOG.error(err) raise utils.ConfigError("Error parsing file.", config_file) else: - try: - with f: - self._config = yaml.safe_load(f) - except yaml.YAMLError as err: - LOG.error(err) - raise utils.ConfigError("Error parsing file.", config_file) + self._config = utils.parse_ini_file(config_file) + if not self._config: + try: + with f: + self._config = yaml.safe_load(f) + except yaml.YAMLError as err: + LOG.error(err) + raise utils.ConfigError( + "Error parsing YAML file.", + config_file + ) self.validate(config_file) diff --git a/bandit/core/utils.py b/bandit/core/utils.py index 3ac78f54f..4b63eed44 100644 --- a/bandit/core/utils.py +++ b/bandit/core/utils.py @@ -349,7 +349,10 @@ def parse_ini_file(f_loc): config = configparser.ConfigParser() try: config.read(f_loc) - return {k: v for k, v in config.items("bandit")} + d = {k: v for k, v in config.items("bandit")} + for k in ("skips", "tests"): + d[k] = d[k].split(",") if k in d else [] + return d except (configparser.Error, KeyError, TypeError): LOG.warning( From 6509d765bf415643ef63972bf1bdc3937c8cc0ca Mon Sep 17 00:00:00 2001 From: Cees Timmerman Date: Sat, 20 Aug 2022 19:08:21 +0200 Subject: [PATCH 2/2] Fix --ini --- bandit/cli/config_generator.py | 4 ++-- bandit/cli/main.py | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/bandit/cli/config_generator.py b/bandit/cli/config_generator.py index e46867f5e..b0569b1e2 100644 --- a/bandit/cli/config_generator.py +++ b/bandit/cli/config_generator.py @@ -153,8 +153,8 @@ def main(): try: with open(args.output_file, "w") as f: - skips = args.skips.split(",") if args.skips else [] - tests = args.tests.split(",") if args.tests else [] + skips = args.skips if args.skips else [] + tests = args.tests if args.tests else [] for skip in skips: if not extension_loader.MANAGER.check_id(skip): diff --git a/bandit/cli/main.py b/bandit/cli/main.py index 47588859d..bb03f4cda 100644 --- a/bandit/cli/main.py +++ b/bandit/cli/main.py @@ -609,8 +609,8 @@ def main(): profile = _get_profile(b_conf, args.profile, args.config_file) _log_info(args, profile) - profile["include"].update(args.tests.split(",") if args.tests else []) - profile["exclude"].update(args.skips.split(",") if args.skips else []) + profile["include"].update(args.tests if args.tests else []) + profile["exclude"].update(args.skips if args.skips else []) extension_mgr.validate_profile(profile) except (utils.ProfileNotFound, ValueError) as e: