-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Rewrite Bandit Without Tox #43241
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
Open
JennyPng
wants to merge
7
commits into
Azure:main
Choose a base branch
from
JennyPng:jennypng-bandit
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Rewrite Bandit Without Tox #43241
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
3df08ff
initial script
JennyPng f97d856
add pip freeze
JennyPng 332bfed
black
JennyPng 5dbc2b0
black again
JennyPng 1a90c12
Merge branch 'main' into jennypng-bandit
JennyPng 3aa04e7
warn instead of error
JennyPng bfe4763
Merge branch 'main' into jennypng-bandit
JennyPng File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import argparse | ||
import os | ||
import sys | ||
from typing import Optional, List | ||
import subprocess | ||
from subprocess import check_call, CalledProcessError | ||
|
||
from .Check import Check | ||
from ci_tools.environment_exclusions import is_check_enabled | ||
from ci_tools.variables import in_ci, set_envvar_defaults | ||
from ci_tools.logging import logger | ||
from ci_tools.functions import install_into_venv, get_pip_command | ||
|
||
|
||
class bandit(Check): | ||
def __init__(self) -> None: | ||
super().__init__() | ||
|
||
def register( | ||
self, subparsers: "argparse._SubParsersAction", parent_parsers: Optional[List[argparse.ArgumentParser]] = None | ||
) -> None: | ||
"""Register the bandit check. The bandit check installs bandit and runs bandit against the target package to find common security issues.""" | ||
parents = parent_parsers or [] | ||
p = subparsers.add_parser( | ||
"bandit", parents=parents, help="Run the bandit check to find common security issues for a package" | ||
) | ||
p.set_defaults(func=self.run) | ||
|
||
def run(self, args: argparse.Namespace) -> int: | ||
"""Run the bandit check command.""" | ||
logger.info("Running bandit check...") | ||
|
||
set_envvar_defaults() | ||
targeted = self.get_targeted_directories(args) | ||
|
||
results: List[int] = [] | ||
|
||
for parsed in targeted: | ||
package_dir = parsed.folder | ||
package_name = parsed.name | ||
executable, staging_directory = self.get_executable(args.isolate, args.command, sys.executable, package_dir) | ||
logger.info(f"Processing {package_name} for bandit check") | ||
|
||
self.install_dev_reqs(executable, args, package_dir) | ||
|
||
try: | ||
install_into_venv(executable, ["bandit"], package_dir) | ||
except CalledProcessError as e: | ||
logger.error(f"Failed to install bandit: {e}") | ||
return e.returncode | ||
|
||
# debug a pip freeze result | ||
cmd = get_pip_command(executable) + ["freeze"] | ||
freeze_result = subprocess.run( | ||
cmd, cwd=package_dir, check=False, text=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT | ||
) | ||
logger.debug(f"Running pip freeze with {cmd}") | ||
logger.debug(freeze_result.stdout) | ||
|
||
if in_ci(): | ||
if not is_check_enabled(package_dir, "bandit"): | ||
logger.warning(f"Bandit is disabled for {package_name}. Skipping...") | ||
continue | ||
|
||
try: | ||
check_call( | ||
[ | ||
executable, | ||
"-m", | ||
"bandit", | ||
"-r", | ||
os.path.join(package_dir, "azure"), | ||
"-ll", | ||
] | ||
) | ||
except CalledProcessError as e: | ||
logger.error(f"{package_name} exited with error {e.returncode}") | ||
results.append(e.returncode) | ||
|
||
return max(results) if results else 0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.