From b53ae0e09ce5917ec82ddd1328eb3fb3b617e56c Mon Sep 17 00:00:00 2001 From: meleksabit <32045473+meleksabit@users.noreply.github.com> Date: Thu, 29 Aug 2024 02:49:58 +0300 Subject: [PATCH] add devsecops_pipeline.py (#7) --- devsecops_pipeline.py | 64 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 devsecops_pipeline.py diff --git a/devsecops_pipeline.py b/devsecops_pipeline.py new file mode 100644 index 0000000..045a782 --- /dev/null +++ b/devsecops_pipeline.py @@ -0,0 +1,64 @@ +import os +import subprocess +import sys + +# Helper function to run shell commands +def run_command(command, cwd=None): + result = subprocess.run(command, shell=True, cwd=cwd, text=True, capture_output=True) + if result.returncode != 0: + print(f"Error: Command '{command}' failed with exit code {result.returncode}") + print(result.stdout) + print(result.stderr) + sys.exit(result.returncode) + return result.stdout + +# Static Code Analysis and Code Coverage (using SonarQube) +def run_sonarqube_analysis(path): + print("Running SonarQube for static code analysis and code coverage...") + run_command(f"sonar-scanner -Dsonar.projectBaseDir={path}") + +# Dependency Checking (using Safety) +def run_safety(): + print("Running Safety for dependency checking...") + run_command("safety check --full-report") + +# Secret Scanning (using TruffleHog) +def run_trufflehog(path): + print("Running TruffleHog for secret scanning...") + run_command(f"trufflehog {path}") + +# Infrastructure as Code Scanning (using Terraform and Snyk) +def run_terraform_scan(path): + print("Running Snyk for Terraform IaC scanning...") + run_command(f"snyk iac test {path}") + +# Code Coverage and Linting (using Pylint) +def run_pylint(path): + print("Running Pylint for code linting...") + run_command(f"pylint {path}") + +# Main function to orchestrate the DevSecOps pipeline +def main(): + project_path = os.getcwd() + + # Static Analysis and Code Coverage with SonarQube + run_sonarqube_analysis(project_path) + + # Dependency Checking + run_safety() + + # Secret Scanning + run_trufflehog(project_path) + + # Terraform IaC Scanning + terraform_path = os.path.join(project_path, 'terraform') + if os.path.exists(terraform_path): + run_terraform_scan(terraform_path) + + # Linting + run_pylint(project_path) + + print("DevSecOps pipeline completed successfully!") + +if __name__ == "__main__": + main()