Skip to content
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

Add bandit to pre-commit #9

Merged
merged 1 commit into from
Mar 9, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
8 changes: 8 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,11 @@ repos:
"-sn", # Don't display the score
"--disable=fixme"
]
- repo: https://github.com/PyCQA/bandit
rev: 1.7.4
hooks:
- id: bandit
name: bandit
types: [python]
files: "^express/"
args: ["-f", "custom", "-q"]
23 changes: 6 additions & 17 deletions express/gcp_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import subprocess # nosec
import os
import logging
import tempfile
from typing import List
from urllib.parse import urlparse

Expand Down Expand Up @@ -103,23 +102,13 @@ def copy_files(source_files: List[str], destination: str):
copy
destination (str): the destination blob/folder to copy the files to
"""
with subprocess.Popen( # nosec
['gsutil', '-o', '"GSUtil:use_gcloud_storage=True"', '-q', '-m', 'cp', '-I',
destination], stdin=subprocess.PIPE
) as gsutil_copy:
gsutil_copy.communicate(b"\n".join([f.encode() for f in source_files]))

# Write file paths to a text file before piping
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code threw bandit warnings, so I rewrote it as a shorter piece of code that still throws bandit warnings and suppressed them 😅

with tempfile.TemporaryDirectory() as temp_folder:
upload_text_file = os.path.join(temp_folder, "files_to_upload.txt")
with open(upload_text_file, "w", encoding="utf-8") as out_file:
for file in source_files:
out_file.write(file)
out_file.write("\n")

# Write files to tmp director
with subprocess.Popen(["cat", upload_text_file], stdout=subprocess.PIPE) \
as pipe_file_list:
subprocess.call( # nosec
['gsutil', '-o', '"GSUtil:use_gcloud_storage=True"', '-q', '-m', 'cp', '-I',
destination], stdin=pipe_file_list.stdout)

logger.info("A total of %s files were copied to %s", len(source_files), destination)
logger.info("A total of %s files were copied to %s", len(source_files), destination)

def copy_file(self, source_file: str, destination: str) -> str:
"""
Expand Down
4 changes: 0 additions & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,10 @@ dataclasses-json = "^0.5.7"
pandas = "^1.3.5"

[tool.poetry.group.test.dependencies]
bandit = { version = "^1.7.4", extras = ["toml"] }
liccheck = "^0.7.3"
pylint = "2.16.4"
pre-commit = "^3.1.1"

[tool.bandit]
exclude_dirs = ["*/mlpipelines/components/sd_finetuning_component/src/train_text_to_image.py"]
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should add bandit for the components once they're migrated


[build-system]
requires = ["poetry-core>=1.2.0"]
build-backend = "poetry.core.masonry.api"