Skip to content

Commit

Permalink
Select specific severity levels in formatted report; #136
Browse files Browse the repository at this point in the history
  • Loading branch information
robertauer committed Dec 19, 2024
1 parent e4ea857 commit 790c557
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
15 changes: 12 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1317,7 +1317,7 @@ Trivy trivy = new Trivy(this)
trivy.scanImage("ubuntu:20.04", TrivySeverityLevel.ALL, TrivyScanStrategy.UNSTABLE, "", "trivy/ubuntu20.json")
trivy.scanImage("ubuntu:24.04", TrivySeverityLevel.ALL, TrivyScanStrategy.UNSTABLE, "", "trivy/ubuntu24.json")
// Save report by using the same file name (last parameter)
trivy.saveFormattedTrivyReport(TrivyScanFormat.HTML, "ubuntu20.04report", "trivy/ubuntu20.json")
trivy.saveFormattedTrivyReport(TrivyScanFormat.HTML, "UNKNOWN,LOW,MEDIUM,HIGH,CRITICAL", "ubuntu20.04report", "trivy/ubuntu20.json")
```

## Save Trivy report in another file format
Expand All @@ -1332,13 +1332,22 @@ trivy.saveFormattedTrivyReport(TrivyScanFormat.JSON)
trivy.saveFormattedTrivyReport(TrivyScanFormat.HTML)
```

You may filter the output to show only specific severity levels (default: "UNKNOWN,LOW,MEDIUM,HIGH,CRITICAL"):

```groovy
Trivy trivy = new Trivy(this)
trivy.scanImage("ubuntu:24.04")
trivy.saveFormattedTrivyReport(TrivyScanFormat.TABLE, "CRITICAL")
trivy.saveFormattedTrivyReport(TrivyScanFormat.JSON, "UNKNOWN,LOW,MEDIUM")
```

You may also use any other supported [Trivy format](https://trivy.dev/v0.57/docs/references/configuration/cli/trivy_convert/) or a custom template from a file in your workspace.

```groovy
Trivy trivy = new Trivy(this)
trivy.scanImage("ubuntu:24.04")
trivy.saveFormattedTrivyReport("cosign-vuln", "ubuntu24.04cosign.txt")
trivy.saveFormattedTrivyReport("template --template @myTemplateFile.xyz", "ubuntu24.04myTemplate.txt")
trivy.saveFormattedTrivyReport("cosign-vuln", "UNKNOWN,LOW,MEDIUM,HIGH,CRITICAL", "ubuntu24.04cosign.txt")
trivy.saveFormattedTrivyReport("template --template @myTemplateFile.xyz", "UNKNOWN,LOW,MEDIUM,HIGH,CRITICAL", "ubuntu24.04myTemplate.txt")
```

## Scan Dogu image with Trivy
Expand Down
12 changes: 10 additions & 2 deletions src/com/cloudogu/ces/cesbuildlib/Trivy.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,13 @@ class Trivy implements Serializable {
* Save the Trivy scan results as a file with a specific format
*
* @param format The format of the output file (@see TrivyScanFormat)
* @param severity Severities of security issues to be added (taken from UNKNOWN,LOW,MEDIUM,HIGH,CRITICAL)
* @param formattedTrivyReportFilename The file name your report files should get, without file extension. E.g. "ubuntu24report"
* @param trivyReportFile The "trivyReportFile" parameter you used in the "scanImage" function, if it was set
*/
void saveFormattedTrivyReport(String format = TrivyScanFormat.HTML, String formattedTrivyReportFilename = "formattedTrivyReport.txt", String trivyReportFile = "trivy/trivyReport.json") {
void saveFormattedTrivyReport(String format = TrivyScanFormat.HTML, String severity = "UNKNOWN,LOW,MEDIUM,HIGH,CRITICAL", String formattedTrivyReportFilename = "formattedTrivyReport.txt", String trivyReportFile = "trivy/trivyReport.json") {
String formatString
String defaultSeverityLevels = "UNKNOWN,LOW,MEDIUM,HIGH,CRITICAL"
String defaultFilename = "formattedTrivyReport.txt"
switch (format) {
case TrivyScanFormat.HTML:
Expand Down Expand Up @@ -142,9 +144,15 @@ class Trivy implements Serializable {
return
}
}
// Validate severity input parameter to prevent injection of additional parameters
if (severity != defaultSeverityLevels) {
if (!severity.split(',').every { it.trim() in ["UNKNOWN", "LOW", "MEDIUM", "HIGH", "CRITICAL"] }) {
script.error("The severity levels provided ($severity) do not match the applicable levels (UNKNOWN,LOW,MEDIUM,HIGH,CRITICAL).")
}
}
docker.image("${trivyImage}:${trivyVersion}")
.inside("-v ${script.env.WORKSPACE}/.trivy/.cache:/root/.cache/") {
script.sh(script: "trivy convert --format ${formatString} --output ${trivyDirectory}/${formattedTrivyReportFilename} ${trivyReportFile}")
script.sh(script: "trivy convert --format ${formatString} --severity ${severity} --output ${trivyDirectory}/${formattedTrivyReportFilename} ${trivyReportFile}")
}
script.archiveArtifacts artifacts: "${trivyDirectory}/${formattedTrivyReportFilename}.*", allowEmptyArchive: true
}
Expand Down

0 comments on commit 790c557

Please sign in to comment.