Skip to content

Commit ca96dd3

Browse files
committed
extract trivy_scan: add --all and --severity option
1 parent 47869f8 commit ca96dd3

File tree

4 files changed

+566
-23
lines changed

4 files changed

+566
-23
lines changed

CHANGELOG.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,18 @@ All notable changes to this project will be documented in this file.
44

55
The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](https://semver.org/)
66

7+
## [0.7.0] 2025-03-17
8+
9+
### Fixed
10+
11+
- even extraction of zero keys will result in a metadata file
12+
13+
### Added
14+
15+
- extract trivy-scan:
16+
- --all option to extract all vulnerability groups
17+
- --severity option to extract an explicit Vulnerability group
18+
719
## [0.6.2] 2025-02-05
820

921
### Added

cimd/commands/extract/trivy_scans.py

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,22 +61,46 @@ def image_for_severity_count(severity: str, count: int) -> str:
6161
"JSON_FILE",
6262
type=click.Path(exists=True, dir_okay=False, file_okay=True, readable=True, resolve_path=True),
6363
)
64+
@click.option(
65+
"--severity",
66+
type=click.Choice(list(COLORS.keys()), case_sensitive=False),
67+
help="Request a single severity group only. This results in explicit zero counts.",
68+
)
69+
@click.option(
70+
"--all",
71+
"all_",
72+
is_flag=True,
73+
help="Will explicitly extract all known severity groups, even zero counts.",
74+
)
6475
@click.option(
6576
"--replace",
6677
is_flag=True,
67-
show_default=True,
6878
help="Replace items in case they already exists.",
6979
)
7080
@click.pass_obj
71-
def trivy_scan_command(app: ApplicationContext, json_file: str, replace: bool) -> None:
72-
"""Extract metadata from a trivy scan JSON output file."""
81+
def trivy_scan_command(
82+
app: ApplicationContext, json_file: str, severity: str, all_: bool, replace: bool
83+
) -> None:
84+
"""Extract metadata from a trivy scan JSON output file.
85+
86+
This command will extract counts of vulnerabilities, grouped by
87+
severity. Per default, only severity groups with at least one
88+
vulnerability will be extracted. If you need explicit zero counts,
89+
use `--severity` or `--all`.
90+
"""
7391
counter = count_json_file(json_file=json_file)
74-
for severity, count in counter.items():
75-
key = f"trivy-scan-{severity.lower()}"
92+
severities = [severity] if severity else counter.keys()
93+
if all_:
94+
severities = list(COLORS.keys())
95+
severities.extend(counter.keys())
96+
severities = list(set(severities))
97+
for _ in severities:
98+
count = counter.get(_, 0)
99+
key = f"trivy-scan-{_.lower()}"
76100
new_item = Item(
77101
value=str(count),
78-
label=severity,
79-
description=f"Count of found vulnerabilities with severity '{severity}'",
80-
image=image_for_severity_count(severity=severity, count=count),
102+
label=_,
103+
description=f"Count of found vulnerabilities with severity '{_}'",
104+
image=image_for_severity_count(severity=_, count=count),
81105
)
82106
app.add_item(key=key, item=new_item, replace=replace)

0 commit comments

Comments
 (0)