Skip to content

Commit c2897fb

Browse files
committedNov 18, 2024··
Add Trivy implementation structure; #136
1 parent c482734 commit c2897fb

File tree

4 files changed

+109
-0
lines changed

4 files changed

+109
-0
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.cloudogu.ces.cesbuildlib
2+
3+
class Trivy implements Serializable {
4+
def script
5+
String trivyReportFilename
6+
7+
Trivy(script, String trivyReportFilename = "${env.WORKSPACE}/.trivy/trivyReport.json") {
8+
this.script = script
9+
this.trivyReportFilename = trivyReportFilename
10+
}
11+
12+
/**
13+
* Scans an image for vulnerabilities.
14+
* Notes:
15+
* - Use a .trivyignore file for allowed CVEs
16+
* - This function will generate a JSON formatted report file which can be converted to other formats via saveFormattedTrivyReport()
17+
* - Evaluate via exit codes: 0 = no vulnerability; 1 = vulnerabilities found; other = function call failed
18+
*
19+
* @param imageName The image name; may include version tag
20+
* @param trivyVersion The version of Trivy used for scanning
21+
* @param additionalFlags Additional Trivy command flags
22+
* @param scanLevel The vulnerability level to scan. Can be a member of TrivyScanLevel or a custom String (e.g. 'CRITICAL,LOW')
23+
* @param strategy The strategy to follow after the scan. Should the build become unstable or failed? Or Should any vulnerability be ignored? (@see TrivyScanStrategy)
24+
* // TODO: A strategy could be implemented by the user via the exit codes of this function. Should we remove the strategy parameter?
25+
* @return Returns 0 if the scan was ok (no vulnerability found); returns 1 if any vulnerability was found
26+
*/
27+
int scanImage(String imageName, String trivyVersion = "0.57.0", String additionalFlags, String scanLevel = TrivyScanLevel.CRITICAL, String strategy = TrivyScanStrategy.FAIL) {
28+
// TODO: Run trivy scan inside Docker container, e.g. via Jenkins' Docker.image() function
29+
// See runTrivyInDocker function: https://github.com/cloudogu/ces-build-lib/blob/c48273409f8f506e31872fe2857650bbfc76a222/vars/findVulnerabilitiesWithTrivy.groovy#L48
30+
// TODO: Write result to trivyReportFile in json format (--format json), which can be converted in the saveFormattedTrivyReport function
31+
// TODO: Include .trivyignore file, if existent. Do not fail if .trivyignore file does not exist.
32+
}
33+
34+
/**
35+
* Save the Trivy scan results as a file with a specific format
36+
*
37+
* @param format The format of the output file (@see TrivyScanFormat)
38+
*/
39+
void saveFormattedTrivyReport(String format = TrivyScanFormat.HTML) {
40+
// TODO: DO NOT scan again! Take the trivyReportFile and convert its content
41+
// See https://aquasecurity.github.io/trivy/v0.52/docs/references/configuration/cli/trivy_convert/
42+
}
43+
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.cloudogu.ces.cesbuildlib
2+
3+
/**
4+
* Defines the output format for the trivy report.
5+
*/
6+
class TrivyScanFormat {
7+
/**
8+
* Output as HTML file.
9+
*/
10+
static String HTML = "html"
11+
12+
/**
13+
* Output as JSON file.
14+
*/
15+
static String JSON = "json"
16+
17+
/**
18+
* Output as plain text file.
19+
*/
20+
static String PLAIN = "plain"
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.cloudogu.ces.cesbuildlib
2+
3+
/**
4+
* Defines aggregated vulnerability levels
5+
*/
6+
class TrivyScanLevel {
7+
/**
8+
* Only critical vulnerabilities.
9+
*/
10+
static String CRITICAL = "CRITICAL"
11+
12+
/**
13+
* High or critical vulnerabilities.
14+
*/
15+
static String HIGH = "CRITICAL,HIGH"
16+
17+
/**
18+
* Medium or higher vulnerabilities.
19+
*/
20+
static String MEDIUM = "CRITICAL,HIGH,MEDIUM"
21+
22+
/**
23+
* All vunlerabilities.
24+
*/
25+
static String ALL = "UNKNOWN,LOW,MEDIUM,HIGH,CRITICAL"
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.cloudogu.ces.cesbuildlib
2+
3+
class TrivyScanStrategy {
4+
/**
5+
* Strategy: Fail if any vulnerability was found.
6+
*/
7+
static String FAIL = "fail"
8+
9+
/**
10+
* Strategy: Make build unstable if any vulnerability was found.
11+
*/
12+
static String UNSTABLE = "unstable"
13+
14+
/**
15+
* Strategy: Ignore any found vulnerability.
16+
*/
17+
static String IGNORE = "ignore"
18+
}

0 commit comments

Comments
 (0)
Please sign in to comment.