@@ -9,7 +9,7 @@ class Trivy implements Serializable {
9
9
private String trivyImage
10
10
private String trivyDirectory = " trivy"
11
11
12
- Trivy (script , String trivyVersion = " 0.57.1 " , String trivyImage = " aquasec/trivy " , Docker docker = new Docker (script)) {
12
+ Trivy (script , String trivyVersion = DEFAULT_TRIVY_VERSION , String trivyImage = DEFAULT_TRIVY_IMAGE , Docker docker = new Docker (script)) {
13
13
this . script = script
14
14
this . trivyVersion = trivyVersion
15
15
this . trivyImage = trivyImage
@@ -94,39 +94,37 @@ class Trivy implements Serializable {
94
94
) {
95
95
String image = script. sh(script : " jq .Image ${ doguDir} /dogu.json" , returnStdout : true ). trim()
96
96
String version = script. sh(script : " jq .Version ${ doguDir} /dogu.json" , returnStdout : true ). trim()
97
- return scanImage(image+ " :" + version, severityLevel, strategy, additionalFlags, trivyReportFile)
97
+ return scanImage(image + " :" + version, severityLevel, strategy, additionalFlags, trivyReportFile)
98
98
}
99
99
100
100
/**
101
101
* Save the Trivy scan results as a file with a specific format
102
102
*
103
- * @param format The format of the output file (@see TrivyScanFormat)
103
+ * @param format The format of the output file {@link TrivyScanFormat}.
104
+ * You may enter supported formats (sarif, cyclonedx, spdx, spdx-json, github, cosign-vuln, table or json)
105
+ * or your own template ("template --template @FILENAME").
106
+ * If you want to convert to a format that requires a list of packages, such as SBOM, you need to add
107
+ * the `--list-all-pkgs` flag to the {@link Trivy#scanImage} call, when outputting in JSON
108
+ * (See <a href =" https://trivy.dev/latest/docs/configuration/reporting/?ref=anaisurl.com#converting" >trivy docs</a>).
104
109
* @param severity Severities of security issues to be added (taken from UNKNOWN,LOW,MEDIUM,HIGH,CRITICAL)
105
- * @param formattedTrivyReportFilename The file name your report files should get, without file extension. E.g. "ubuntu24report"
110
+ * @param formattedTrivyReportFilename The file name your report files should get, with file extension. E.g. "ubuntu24report.html "
106
111
* @param trivyReportFile The "trivyReportFile" parameter you used in the "scanImage" function, if it was set
107
112
*/
108
- void saveFormattedTrivyReport (String format = TrivyScanFormat . HTML , String severity = " UNKNOWN,LOW,MEDIUM,HIGH,CRITICAL" , String formattedTrivyReportFilename = " formattedTrivyReport.txt" , String trivyReportFile = " trivy/trivyReport.json" ) {
113
+ void saveFormattedTrivyReport (String format = TrivyScanFormat . HTML ,
114
+ String severity = " UNKNOWN,LOW,MEDIUM,HIGH,CRITICAL" ,
115
+ String formattedTrivyReportFilename = null ,
116
+ String trivyReportFile = " trivy/trivyReport.json" ) {
117
+
118
+ // set default report filename depending on the chosen format
119
+ if (formattedTrivyReportFilename == null ) {
120
+ formattedTrivyReportFilename = " formattedTrivyReport" + getFileExtension(format)
121
+ }
122
+
109
123
String formatString
110
- String defaultSeverityLevels = " UNKNOWN,LOW,MEDIUM,HIGH,CRITICAL"
111
- String defaultFilename = " formattedTrivyReport.txt"
112
124
switch (format) {
125
+ // TrivyScanFormat.JSON and TrivyScanFormat.TABLE are handled by the default case, too
113
126
case TrivyScanFormat . HTML :
114
127
formatString = " template --template \" @/contrib/html.tpl\" "
115
- if (formattedTrivyReportFilename == defaultFilename) {
116
- formattedTrivyReportFilename == " formattedTrivyReport.html"
117
- }
118
- break
119
- case TrivyScanFormat . JSON :
120
- formatString = " json"
121
- if (formattedTrivyReportFilename == defaultFilename) {
122
- formattedTrivyReportFilename == " formattedTrivyReport.json"
123
- }
124
- break
125
- case TrivyScanFormat . TABLE :
126
- formatString = " table"
127
- if (formattedTrivyReportFilename == defaultFilename) {
128
- formattedTrivyReportFilename == " formattedTrivyReport.table"
129
- }
130
128
break
131
129
default :
132
130
// You may enter supported formats (sarif, cyclonedx, spdx, spdx-json, github, cosign-vuln, table or json)
@@ -135,7 +133,7 @@ class Trivy implements Serializable {
135
133
// Check if "format" is a custom template from a file
136
134
boolean isTemplateFormat = format ==~ / ^template --template @\S +$/
137
135
// Check if "format" is one of the trivyFormats or a template
138
- if (trivyFormats. any { format . contains( it) } || isTemplateFormat) {
136
+ if (trivyFormats. any { (format == it) } || isTemplateFormat) {
139
137
formatString = format
140
138
break
141
139
} else {
@@ -144,15 +142,18 @@ class Trivy implements Serializable {
144
142
}
145
143
}
146
144
// Validate severity input parameter to prevent injection of additional parameters
147
- if (severity != defaultSeverityLevels) {
148
- if (! severity. split(' ,' ). every { it. trim() in [" UNKNOWN" , " LOW" , " MEDIUM" , " HIGH" , " CRITICAL" ] }) {
149
- script. error(" The severity levels provided ($severity ) do not match the applicable levels (UNKNOWN,LOW,MEDIUM,HIGH,CRITICAL)." )
150
- }
145
+ if (! severity. split(' ,' ). every { it. trim() in [" UNKNOWN" , " LOW" , " MEDIUM" , " HIGH" , " CRITICAL" ] }) {
146
+ script. error(" The severity levels provided ($severity ) do not match the applicable levels (UNKNOWN,LOW,MEDIUM,HIGH,CRITICAL)." )
151
147
}
148
+
152
149
docker. image(" ${ trivyImage} :${ trivyVersion} " )
153
150
.inside(" -v ${ script.env.WORKSPACE} /.trivy/.cache:/root/.cache/" ) {
154
151
script. sh(script : " trivy convert --format ${ formatString} --severity ${ severity} --output ${ trivyDirectory} /${ formattedTrivyReportFilename} ${ trivyReportFile} " )
155
152
}
156
153
script. archiveArtifacts artifacts : " ${ trivyDirectory} /${ formattedTrivyReportFilename} .*" , allowEmptyArchive : true
157
154
}
155
+
156
+ private static String getFileExtension (String format ) {
157
+ return TrivyScanFormat . isStandardScanFormat(format) ? " ." + format : " .txt"
158
+ }
158
159
}
0 commit comments