Skip to content
Open
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions doc/tool_usage_guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ This repo is currently migrating all checks from a slower `tox`-based framework,
|`black`| Runs `black` checks. | `azpysdk black .` |
|`verifytypes`| Runs `verifytypes` checks. | `azpysdk verifytypes .` |
|`ruff`| Runs `ruff` checks. | `azpysdk ruff .` |
|`bandit`| Runs `bandit` checks, which detect common security issues. | `azpysdk bandit .` |
|`import_all`| Installs the package w/ default dependencies, then attempts to `import *` from the base namespace. Ensures that all imports will resolve after a base install and import. | `azpysdk import_all .` |

## Common arguments
Expand Down
81 changes: 81 additions & 0 deletions eng/tools/azure-sdk-tools/azpysdk/bandit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
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.error(f"Bandit is disabled for {package_name}. Skipping...")
results.append(1)
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
2 changes: 2 additions & 0 deletions eng/tools/azure-sdk-tools/azpysdk/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from .next_pyright import next_pyright
from .ruff import ruff
from .verifytypes import verifytypes
from .bandit import bandit

from ci_tools.logging import configure_logging, logger

Expand Down Expand Up @@ -81,6 +82,7 @@ def build_parser() -> argparse.ArgumentParser:
next_pyright().register(subparsers, [common])
ruff().register(subparsers, [common])
verifytypes().register(subparsers, [common])
bandit().register(subparsers, [common])

return parser

Expand Down
Loading