|
| 1 | +import warnings |
| 2 | +import logging |
1 | 3 | import click |
| 4 | +import os |
2 | 5 | from core import clone_repo, detect_malicious, scan_insecure, dependency_check, policy_config, report |
3 | 6 |
|
| 7 | +# Suppress deprecation warnings |
| 8 | +warnings.filterwarnings("ignore", category=DeprecationWarning) |
| 9 | + |
| 10 | +# Set up logging |
| 11 | +logging.basicConfig( |
| 12 | + filename="secure_code_scanner.log", |
| 13 | + level=logging.INFO, |
| 14 | + format="%(asctime)s - %(levelname)s - %(message)s", |
| 15 | +) |
| 16 | +logger = logging.getLogger() |
| 17 | + |
4 | 18 | @click.command() |
5 | 19 | @click.option("--repo", prompt="GitHub repository URL", help="URL of the GitHub repository") |
6 | 20 | def main(repo): |
7 | | - """Secure Source Code Scanner CLI.""" |
8 | | - config = policy_config.load_policy("config.yaml") |
9 | | - |
10 | | - # Clone repository |
11 | | - repo_dir = clone_repo.clone_repository(repo) |
12 | | - if not repo_dir: |
13 | | - click.echo("Failed to clone repository.") |
14 | | - return |
15 | | - |
16 | | - # Scan repository for malicious code |
17 | | - malicious_results = [] |
18 | | - insecure_results = [] |
19 | | - |
20 | | - # Perform scans on each .py file |
21 | | - for root, _, files in os.walk(repo_dir): |
22 | | - for file in files: |
23 | | - if file.endswith('.py'): |
24 | | - file_path = os.path.join(root, file) |
25 | | - malicious_results += detect_malicious.detect_malicious_patterns(file_path, config) |
26 | | - insecure_results += scan_insecure.run_bandit_scan(file_path) |
27 | | - |
28 | | - # Check dependencies |
29 | | - dependency_results = dependency_check.check_dependencies(repo_dir, config) |
30 | | - |
31 | | - # Generate report |
32 | | - results = { |
33 | | - "malicious": malicious_results, |
34 | | - "insecure": insecure_results, |
35 | | - "dependencies": dependency_results |
36 | | - } |
37 | | - report.generate_report(results, config) |
38 | | - |
39 | | - # Cleanup cloned repository |
40 | | - clone_repo.cleanup_repository(repo_dir) |
| 21 | + """Secure Code Scanner CLI.""" |
| 22 | + try: |
| 23 | + # Load configuration |
| 24 | + config = policy_config.load_policy("config.yaml") |
| 25 | + logger.info("Configuration loaded successfully.") |
| 26 | + |
| 27 | + # Clone repository |
| 28 | + try: |
| 29 | + repo_dir = clone_repo.clone_repository(repo) |
| 30 | + if not repo_dir: |
| 31 | + click.echo("Failed to clone repository.") |
| 32 | + logger.error("Repository cloning failed.") |
| 33 | + return |
| 34 | + logger.info(f"Repository cloned to {repo_dir}.") |
| 35 | + except Exception as e: |
| 36 | + logger.error(f"Error during repository cloning: {e}") |
| 37 | + click.echo(f"Error during repository cloning: {e}") |
| 38 | + return |
| 39 | + |
| 40 | + # Initialize result containers |
| 41 | + malicious_results = [] |
| 42 | + insecure_results = [] |
| 43 | + |
| 44 | + # Scan each .py file in the repository |
| 45 | + try: |
| 46 | + for root, _, files in os.walk(repo_dir): |
| 47 | + for file in files: |
| 48 | + if file.endswith('.py'): |
| 49 | + file_path = os.path.join(root, file) |
| 50 | + logger.info(f"Scanning file: {file_path}") |
| 51 | + |
| 52 | + # Detect malicious patterns |
| 53 | + try: |
| 54 | + malicious_results += detect_malicious.detect_malicious_patterns(file_path, config) |
| 55 | + except Exception as e: |
| 56 | + logger.error(f"Error in malicious code detection for {file_path}: {e}") |
| 57 | + |
| 58 | + # Check for insecure code practices |
| 59 | + try: |
| 60 | + insecure_results += scan_insecure.run_bandit_scan(file_path) |
| 61 | + except Exception as e: |
| 62 | + logger.error(f"Error in insecure code scanning for {file_path}: {e}") |
| 63 | + except Exception as e: |
| 64 | + logger.error(f"Error during file scanning: {e}") |
| 65 | + click.echo(f"Error during file scanning: {e}") |
| 66 | + return |
| 67 | + |
| 68 | + # Dependency check |
| 69 | + try: |
| 70 | + dependency_results = dependency_check.check_dependencies(repo_dir, config) |
| 71 | + logger.info("Dependency check completed.") |
| 72 | + except Exception as e: |
| 73 | + logger.error(f"Error during dependency checking: {e}") |
| 74 | + click.echo(f"Error during dependency checking: {e}") |
| 75 | + return |
| 76 | + |
| 77 | + # Generate report |
| 78 | + try: |
| 79 | + results = { |
| 80 | + "malicious": malicious_results, |
| 81 | + "insecure": insecure_results, |
| 82 | + "dependencies": dependency_results, |
| 83 | + } |
| 84 | + report.generate_report(results, config) |
| 85 | + logger.info("Report generated successfully.") |
| 86 | + except Exception as e: |
| 87 | + logger.error(f"Error during report generation: {e}") |
| 88 | + click.echo(f"Error during report generation: {e}") |
| 89 | + return |
| 90 | + |
| 91 | + finally: |
| 92 | + # Cleanup cloned repository |
| 93 | + if 'repo_dir' in locals() and repo_dir: |
| 94 | + clone_repo.cleanup_repository(repo_dir) |
| 95 | + logger.info("Temporary repository directory cleaned up.") |
41 | 96 |
|
42 | 97 | if __name__ == "__main__": |
43 | 98 | main() |
0 commit comments