config_generator.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. # Copyright 2015 Red Hat Inc.
  2. #
  3. # SPDX-License-Identifier: Apache-2.0
  4. """Bandit is a tool designed to find common security issues in Python code."""
  5. import argparse
  6. import importlib
  7. import logging
  8. import os
  9. import sys
  10. import yaml
  11. from bandit.core import extension_loader
  12. PROG_NAME = "bandit_conf_generator"
  13. LOG = logging.getLogger(__name__)
  14. template = """
  15. ### Bandit config file generated from:
  16. # '{cli}'
  17. ### This config may optionally select a subset of tests to run or skip by
  18. ### filling out the 'tests' and 'skips' lists given below. If no tests are
  19. ### specified for inclusion then it is assumed all tests are desired. The skips
  20. ### set will remove specific tests from the include set. This can be controlled
  21. ### using the -t/-s CLI options. Note that the same test ID should not appear
  22. ### in both 'tests' and 'skips', this would be nonsensical and is detected by
  23. ### Bandit at runtime.
  24. # Available tests:
  25. {test_list}
  26. # (optional) list included test IDs here, eg '[B101, B406]':
  27. {test}
  28. # (optional) list skipped test IDs here, eg '[B101, B406]':
  29. {skip}
  30. ### (optional) plugin settings - some test plugins require configuration data
  31. ### that may be given here, per-plugin. All bandit test plugins have a built in
  32. ### set of sensible defaults and these will be used if no configuration is
  33. ### provided. It is not necessary to provide settings for every (or any) plugin
  34. ### if the defaults are acceptable.
  35. {settings}
  36. """
  37. def init_logger():
  38. """Init logger."""
  39. LOG.handlers = []
  40. log_level = logging.INFO
  41. log_format_string = "[%(levelname)5s]: %(message)s"
  42. logging.captureWarnings(True)
  43. LOG.setLevel(log_level)
  44. handler = logging.StreamHandler(sys.stdout)
  45. handler.setFormatter(logging.Formatter(log_format_string))
  46. LOG.addHandler(handler)
  47. def parse_args():
  48. """Parse arguments."""
  49. help_description = """Bandit Config Generator
  50. This tool is used to generate an optional profile. The profile may be used
  51. to include or skip tests and override values for plugins.
  52. When used to store an output profile, this tool will output a template that
  53. includes all plugins and their default settings. Any settings which aren't
  54. being overridden can be safely removed from the profile and default values
  55. will be used. Bandit will prefer settings from the profile over the built
  56. in values."""
  57. parser = argparse.ArgumentParser(
  58. description=help_description,
  59. formatter_class=argparse.RawTextHelpFormatter,
  60. )
  61. parser.add_argument(
  62. "--show-defaults",
  63. dest="show_defaults",
  64. action="store_true",
  65. help="show the default settings values for each "
  66. "plugin but do not output a profile",
  67. )
  68. parser.add_argument(
  69. "-o",
  70. "--out",
  71. dest="output_file",
  72. action="store",
  73. help="output file to save profile",
  74. )
  75. parser.add_argument(
  76. "-t",
  77. "--tests",
  78. dest="tests",
  79. action="store",
  80. default=None,
  81. type=str,
  82. help="list of test names to run",
  83. )
  84. parser.add_argument(
  85. "-s",
  86. "--skip",
  87. dest="skips",
  88. action="store",
  89. default=None,
  90. type=str,
  91. help="list of test names to skip",
  92. )
  93. args = parser.parse_args()
  94. if not args.output_file and not args.show_defaults:
  95. parser.print_help()
  96. parser.exit(1)
  97. return args
  98. def get_config_settings():
  99. """Get configuration settings."""
  100. config = {}
  101. for plugin in extension_loader.MANAGER.plugins:
  102. fn_name = plugin.name
  103. function = plugin.plugin
  104. # if a function takes config...
  105. if hasattr(function, "_takes_config"):
  106. fn_module = importlib.import_module(function.__module__)
  107. # call the config generator if it exists
  108. if hasattr(fn_module, "gen_config"):
  109. config[fn_name] = fn_module.gen_config(function._takes_config)
  110. return yaml.safe_dump(config, default_flow_style=False)
  111. def main():
  112. """Config generator to write configuration file."""
  113. init_logger()
  114. args = parse_args()
  115. yaml_settings = get_config_settings()
  116. if args.show_defaults:
  117. print(yaml_settings)
  118. if args.output_file:
  119. if os.path.exists(os.path.abspath(args.output_file)):
  120. LOG.error("File %s already exists, exiting", args.output_file)
  121. sys.exit(2)
  122. try:
  123. with open(args.output_file, "w") as f:
  124. skips = args.skips.split(",") if args.skips else []
  125. tests = args.tests.split(",") if args.tests else []
  126. for skip in skips:
  127. if not extension_loader.MANAGER.check_id(skip):
  128. raise RuntimeError("unknown ID in skips: %s" % skip)
  129. for test in tests:
  130. if not extension_loader.MANAGER.check_id(test):
  131. raise RuntimeError("unknown ID in tests: %s" % test)
  132. tpl = "# {0} : {1}"
  133. test_list = [
  134. tpl.format(t.plugin._test_id, t.name)
  135. for t in extension_loader.MANAGER.plugins
  136. ]
  137. others = [
  138. tpl.format(k, v["name"])
  139. for k, v in (
  140. extension_loader.MANAGER.blacklist_by_id.items()
  141. )
  142. ]
  143. test_list.extend(others)
  144. test_list.sort()
  145. contents = template.format(
  146. cli=" ".join(sys.argv),
  147. settings=yaml_settings,
  148. test_list="\n".join(test_list),
  149. skip="skips: " + str(skips) if skips else "skips:",
  150. test="tests: " + str(tests) if tests else "tests:",
  151. )
  152. f.write(contents)
  153. except OSError:
  154. LOG.error("Unable to open %s for writing", args.output_file)
  155. except Exception as e:
  156. LOG.error("Error: %s", e)
  157. else:
  158. LOG.info("Successfully wrote profile: %s", args.output_file)
  159. return 0
  160. if __name__ == "__main__":
  161. sys.exit(main())