|
| 1 | +"""CLI/Commands - Vulnerabilities.""" |
| 2 | + |
| 3 | +import click |
| 4 | + |
| 5 | +from ...core.api.vulnerabilities import ( |
| 6 | + _print_vulnerabilities_assessment_table, |
| 7 | + _print_vulnerabilities_summary_table, |
| 8 | + get_package_scan_result, |
| 9 | +) |
| 10 | +from .. import decorators, utils, validators |
| 11 | +from ..exceptions import handle_api_exceptions |
| 12 | +from .main import main |
| 13 | + |
| 14 | + |
| 15 | +@main.command() |
| 16 | +@decorators.common_cli_config_options |
| 17 | +@decorators.common_cli_output_options |
| 18 | +@decorators.common_api_auth_options |
| 19 | +@decorators.initialise_api |
| 20 | +@click.argument( |
| 21 | + "owner_repo_package", |
| 22 | + metavar="OWNER/REPO/PACKAGE", |
| 23 | + callback=validators.validate_owner_repo_package, |
| 24 | +) |
| 25 | +@click.option( |
| 26 | + "-A", |
| 27 | + "--show-assessment", |
| 28 | + is_flag=True, |
| 29 | + help="Show assessment with vulnerability details.", |
| 30 | +) |
| 31 | +@click.option( |
| 32 | + "--fixable/--non-fixable", |
| 33 | + is_flag=True, |
| 34 | + default=None, # Changed to allow None (Show All) vs True/False |
| 35 | + help="Filter by fixable status (only fixable vs only non-fixable).", |
| 36 | +) |
| 37 | +@click.option( |
| 38 | + "--severity", |
| 39 | + "severity_filter", |
| 40 | + help="Filter by severities (e.g., 'CRITICAL', 'HIGH', 'MEDIUM', 'LOW').", |
| 41 | +) |
| 42 | +@click.pass_context |
| 43 | +def vulnerabilities( |
| 44 | + ctx, opts, owner_repo_package, show_assessment, fixable, severity_filter |
| 45 | +): |
| 46 | + """ |
| 47 | + Retrieve vulnerability scan results for a package. |
| 48 | +
|
| 49 | + \b |
| 50 | + Usage: |
| 51 | + cloudsmith vulnerabilities myorg/repo/pkg_identifier [flags] |
| 52 | +
|
| 53 | + \b |
| 54 | + Aliases: |
| 55 | + vulnerabilities, vuln |
| 56 | +
|
| 57 | + Examples: |
| 58 | +
|
| 59 | + \b |
| 60 | + # Display the vulnerability summary |
| 61 | + cloudsmith vulnerabilities myorg/repo/pkg_identifier |
| 62 | +
|
| 63 | + \b |
| 64 | + # Display detailed vulnerability assessment |
| 65 | + cloudsmith vulnerabilities myorg/repo/pkg_identifier -A / --show-assessment |
| 66 | +
|
| 67 | + \b |
| 68 | + # Filter the result by severity |
| 69 | + cloudsmith vulnerabilities myorg/repo/pkg_identifier --severity critical,high |
| 70 | +
|
| 71 | + \b |
| 72 | + # Filter by fixable or non-fixable vulnerabilities |
| 73 | + cloudsmith vulnerabilities myorg/repo/pkg_identifier --fixable / --non-fixable |
| 74 | +
|
| 75 | +
|
| 76 | + """ |
| 77 | + use_stderr = utils.should_use_stderr(opts) |
| 78 | + |
| 79 | + owner, repo, slug = owner_repo_package |
| 80 | + |
| 81 | + total_filtered_vulns = 0 |
| 82 | + |
| 83 | + context_msg = "Failed to retrieve vulnerability report!" |
| 84 | + with handle_api_exceptions(ctx, opts=opts, context_msg=context_msg): |
| 85 | + with utils.maybe_spinner(opts): |
| 86 | + data = get_package_scan_result( |
| 87 | + opts=opts, |
| 88 | + owner=owner, |
| 89 | + repo=repo, |
| 90 | + package=slug, |
| 91 | + show_assessment=show_assessment, |
| 92 | + severity_filter=severity_filter, |
| 93 | + fixable=fixable, |
| 94 | + ) |
| 95 | + |
| 96 | + click.secho("OK", fg="green", err=use_stderr) |
| 97 | + |
| 98 | + # Filter results if severity or fixable flags are active |
| 99 | + if severity_filter or fixable is not None: |
| 100 | + scans = getattr(data, "scans", []) |
| 101 | + |
| 102 | + allowed_severities = ( |
| 103 | + [s.strip().lower() for s in severity_filter.split(",")] |
| 104 | + if severity_filter |
| 105 | + else None |
| 106 | + ) |
| 107 | + |
| 108 | + for scan in scans: |
| 109 | + results = getattr(scan, "results", []) |
| 110 | + |
| 111 | + # 1. Filter by Severity |
| 112 | + if allowed_severities: |
| 113 | + results = [ |
| 114 | + res |
| 115 | + for res in results |
| 116 | + if getattr(res, "severity", "unknown").lower() in allowed_severities |
| 117 | + ] |
| 118 | + |
| 119 | + # 2. Filter by Fixable Status |
| 120 | + # fixable=True: Keep only if has fix_version |
| 121 | + # fixable=False: Keep only if NO fix_version |
| 122 | + if fixable is not None: |
| 123 | + results = [ |
| 124 | + res |
| 125 | + for res in results |
| 126 | + if bool( |
| 127 | + getattr(res, "fix_version", getattr(res, "fixed_version", None)) |
| 128 | + ) |
| 129 | + is fixable |
| 130 | + ] |
| 131 | + |
| 132 | + scan.results = results |
| 133 | + total_filtered_vulns += len(results) |
| 134 | + |
| 135 | + if utils.maybe_print_as_json(opts, data): |
| 136 | + return |
| 137 | + |
| 138 | + _print_vulnerabilities_summary_table(data, severity_filter, total_filtered_vulns) |
| 139 | + |
| 140 | + if show_assessment: |
| 141 | + _print_vulnerabilities_assessment_table(data, severity_filter) |
0 commit comments