Skip to content

Commit

Permalink
Enable custom formats for Trivy report conversion; #136
Browse files Browse the repository at this point in the history
  • Loading branch information
robertauer committed Dec 11, 2024
1 parent 174645f commit 15fe1a9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1332,6 +1332,16 @@ trivy.saveFormattedTrivyReport(TrivyScanFormat.JSON)
trivy.saveFormattedTrivyReport(TrivyScanFormat.HTML)
```

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.
The output file of this converted Trivy report will have the extension "custom".

```groovy
Trivy trivy = new Trivy(this)
trivy.scanImage("ubuntu:24.04")
trivy.saveFormattedTrivyReport("cosign-vuln")
trivy.saveFormattedTrivyReport("template --template @myTemplateFile.xyz")
```

## Scan Dogu image with Trivy

The `scanDogu()` function lets you scan a Dogu image without typing its full name. The method reads the image name
Expand Down
18 changes: 15 additions & 3 deletions src/com/cloudogu/ces/cesbuildlib/Trivy.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class Trivy implements Serializable {
.mountDockerSocket()
.inside("-v ${script.env.WORKSPACE}/.trivy/.cache:/root/.cache/") {
// Write result to $trivyReportFile in json format (--format json), which can be converted in the saveFormattedTrivyReport function
// Exit with exit code 10 if vulnerabilities are found or OS is so old that trivy has no records for it anymore
// Exit with exit code 10 if vulnerabilities are found or OS is so old that Trivy has no records for it anymore
script.sh("mkdir -p " + trivyDirectory)
script.sh(script: "trivy image --exit-code 10 --exit-on-eol 10 --format ${TrivyScanFormat.JSON} -o ${trivyReportFile} --severity ${severityLevel} ${additionalFlags} ${imageName}", returnStatus: true)
}
Expand Down Expand Up @@ -121,8 +121,20 @@ class Trivy implements Serializable {
fileExtension = "txt"
break
default:
script.error("This format did not match the supported formats: " + format)
return
// You may enter supported formats (sarif, cyclonedx, spdx, spdx-json, github, cosign-vuln, table or json)
// or your own template ("template --template @FILENAME")
List<String> trivyFormats = ['sarif', 'cyclonedx', 'spdx', 'spdx-json', 'github', 'cosign-vuln', 'table', 'json']
// Check if "format" is a custom template from a file
boolean isTemplateFormat = format ==~ /^template --template @\S+$/
// Check if "format" is one of the trivyFormats or a template
if (trivyFormats.any { format.contains(it) } || isTemplateFormat) {
formatString = format
fileExtension = "custom"
break
} else {
script.error("This format did not match the supported formats: " + format)
return
}
}
docker.image("${trivyImage}:${trivyVersion}")
.inside("-v ${script.env.WORKSPACE}/.trivy/.cache:/root/.cache/") {
Expand Down

0 comments on commit 15fe1a9

Please sign in to comment.