Skip to content

Commit

Permalink
Let the user decide which file extension to use; #136
Browse files Browse the repository at this point in the history
  • Loading branch information
robertauer committed Dec 19, 2024
1 parent d2cb3a7 commit e4ea857
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1333,13 +1333,12 @@ 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", "ubuntu24.04cosign")
trivy.saveFormattedTrivyReport("template --template @myTemplateFile.xyz", "ubuntu24.04myTemplate")
trivy.saveFormattedTrivyReport("cosign-vuln", "ubuntu24.04cosign.txt")
trivy.saveFormattedTrivyReport("template --template @myTemplateFile.xyz", "ubuntu24.04myTemplate.txt")
```

## Scan Dogu image with Trivy
Expand Down
18 changes: 12 additions & 6 deletions src/com/cloudogu/ces/cesbuildlib/Trivy.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -104,21 +104,27 @@ class Trivy implements Serializable {
* @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", String trivyReportFile = "trivy/trivyReport.json") {
String fileExtension
void saveFormattedTrivyReport(String format = TrivyScanFormat.HTML, String formattedTrivyReportFilename = "formattedTrivyReport.txt", String trivyReportFile = "trivy/trivyReport.json") {
String formatString
String defaultFilename = "formattedTrivyReport.txt"
switch (format) {
case TrivyScanFormat.HTML:
formatString = "template --template \"@/contrib/html.tpl\""
fileExtension = "html"
if (formattedTrivyReportFilename == defaultFilename) {
formattedTrivyReportFilename == "formattedTrivyReport.html"
}
break
case TrivyScanFormat.JSON:
formatString = "json"
fileExtension = "json"
if (formattedTrivyReportFilename == defaultFilename) {
formattedTrivyReportFilename == "formattedTrivyReport.json"
}
break
case TrivyScanFormat.TABLE:
formatString = "table"
fileExtension = "txt"
if (formattedTrivyReportFilename == defaultFilename) {
formattedTrivyReportFilename == "formattedTrivyReport.table"
}
break
default:
// You may enter supported formats (sarif, cyclonedx, spdx, spdx-json, github, cosign-vuln, table or json)
Expand All @@ -138,7 +144,7 @@ class Trivy implements Serializable {
}
docker.image("${trivyImage}:${trivyVersion}")
.inside("-v ${script.env.WORKSPACE}/.trivy/.cache:/root/.cache/") {
script.sh(script: "trivy convert --format ${formatString} --output ${trivyDirectory}/${formattedTrivyReportFilename}.${fileExtension} ${trivyReportFile}")
script.sh(script: "trivy convert --format ${formatString} --output ${trivyDirectory}/${formattedTrivyReportFilename} ${trivyReportFile}")
}
script.archiveArtifacts artifacts: "${trivyDirectory}/${formattedTrivyReportFilename}.*", allowEmptyArchive: true
}
Expand Down

0 comments on commit e4ea857

Please sign in to comment.