Bandit is a security linter for Python source code, utilizing the ast module from the Python standard library.
The ast module is used to convert source code into a parsed tree of Python syntax nodes. Bandit allows users to define custom tests that are performed against those nodes. At the completion of testing, a report is generated that lists security issues identified within the target source code.
Bandit is currently a stand-alone tool which can be downloaded by end-users and run against arbitrary source code. As it matures and is proven to be useful, we see it being a possible addition to OpenStack CI gate tests with non-voting and eventually voting capabilities.
Bandit can be obtained by cloning the repository at https://git.openstack.org/openstack/bandit.git. The README.rst file contains documentation regarding installation, usage, and configuration.
Bandit makes use of the OpenStack CI infrastructure provided through OpenStack:
- Read-only OpenStack git repository: https://git.openstack.org/openstack/bandit
- Browsable OpenStack git repository: https://git.openstack.org/cgit/openstack/bandit/
- Launchpad bug reporting and tracking: https://launchpad.net/bandit
- Gerrit endpoint for git repository: ssh://[username]@review.openstack.org:29418/openstack/bandit.git
- Gerrit reviews: https://review.openstack.org/#/q/project:openstack/bandit,n,z
An easy way to contribute is to write a plugin/test that will allow Bandit to identify more security issues. Extensions and improvements to the underlying framework are also welcomed, although we'll be attempting to maintain stability in the interface that is presented to plugins.
See Development Workflow for information on the general contribution/review workflow.
Gate Testing with Bandit
Bandit can help maintain the security of OpenStack projects when it's used as a gate test. Projects such as Keystone have created a gate test which runs Bandit to ensure that common security code mistakes are not introduced when code is modified.
To set up a Bandit gate test for an OpenStack project, follow these steps:
- Add a Bandit config for your project. This determines what bandit checks will run on the project. Some checks might not make sense for the project, or the test doesn't passing when you first enable it. Here's the Keystone bandit.yaml.
- Add "bandit" (the package name on pypi) to the test-requirements.txt file. While running Bandit only actually requires the bandit package, it's easiest for now to just leave it in with the rest of the test requirements. This file lists the requirements for creating the virtual environment Bandit runs in and gets updated automatically in most projects by the OpenStack proposal bot.
- Add a tox environment to run Bandit. Keystone's is here: tox.ini in the [testenv:bandit] section
- Add the Bandit job to the list of jobs for your project in OpenStack Infra. Keystone implemented this change here: jenkins/jobs/projects.yaml
- Add the Bandit gate to your project section in OpenStack Infra. For an example, here is Keystone's job: zuul/layout.yaml. Note: for Keystone Bandit is currently set up as non-voting. This means that failures in Bandit won't cause Jenkins to reject the change. This has the advantage that it will never cause your change to fail, but it also doesn't allow Bandit to prevent anything. To make the Bandit check voting set "voting: true".
- Modify the "check" section of your project in OpenStack infra to run Bandit as a check. For an example see this change: Keystone add Bandit check.
- Run this way for a while and then make it voting!
Steps 1-3 should be done in one commit for your project, and steps 4-6 will be a separate commit for OpenStack Infra. If you have any questions or comments please contact tmcpeak in #openstack-security on Freenode IRC.
By default Bandit runs all plugins that it finds in the plugins directory. While this may be useful for doing a thorough manual review, for use cases such as automation and gate testing, this is probably overkill. Luckily, Bandit allows a user to create profiles which run or exclude specific tests. For example, here is the profile that the Keystone gate uses: Keystone's Bandit Gate Profile .
Once a profile has been created, you can run it by using the "-c" command line option to point to the config file which contains your profile and "-p" to point to the specific profile you'd like to use.
TODO is now tracked using blueprints in Launchpad: https://blueprints.launchpad.net/bandit
Bandit is a tool from the OpenStack Security project.
Core project team:
- Jamie Finnigan (chair6)
- Travis McPeak (tmcpeak)
- Tim Kelsey (tkelsey)
- Eric Brown (browne)
- Ian Cordasco (sigmavirus24)