@@ -68,6 +68,7 @@ Jenkins Pipeline Shared library, that contains additional features for Git, Mave
6868- [ Markdown] ( #markdown )
6969 - [ DockerLint (Deprecated)] ( #dockerlint-deprecated )
7070 - [ ShellCheck] ( #shellcheck )
71+ - [ Trivy] ( #trivy )
7172- [ Steps] ( #steps )
7273 - [ mailIfStatusChanged] ( #mailifstatuschanged )
7374 - [ isPullRequest] ( #ispullrequest )
@@ -1240,6 +1241,179 @@ shellCheck(fileList) // fileList="a.sh b.sh" execute shellcheck on a custom list
12401241
12411242See [ shellCheck] ( vars/shellCheck.groovy )
12421243
1244+ # Trivy
1245+
1246+ Scan container images for vulnerabilities with Trivy.
1247+
1248+ ## Create a Trivy object
1249+
1250+ ``` groovy
1251+ Trivy trivy = new Trivy(this)
1252+ // With specific Trivy version
1253+ Trivy trivy = new Trivy(this, "0.57.1")
1254+ // With specific Trivy image
1255+ Trivy trivy = new Trivy(this, "0.57.1", "images.mycompany.test/trivy")
1256+ // With explicit Docker registry
1257+ Docker docker = new Docker(this)
1258+ docker.withRegistry("https://my.registry.invalid", myRegistryCredentialsID)
1259+ Trivy trivy = new Trivy(this, "0.57.1", "aquasec/trivy", docker)
1260+ ```
1261+
1262+ ## Scan image with Trivy
1263+
1264+ Scan an image with Trivy by calling the ` scanImage ` function.
1265+
1266+ ``` groovy
1267+ Trivy trivy = new Trivy(this)
1268+ boolean imageIsSafe = trivy.scanImage("ubuntu:24.04")
1269+ if (!imageIsSafe){
1270+ echo "This image has vulnerabilities!"
1271+ }
1272+ ```
1273+
1274+ ### Set the severity level for the scan
1275+
1276+ You can set the severity levels of the vulnerabilities Trivy should scan for as a parameter of the scan method:
1277+
1278+ ``` groovy
1279+ Trivy trivy = new Trivy(this)
1280+ trivy.scanImage("ubuntu:24.04", TrivySeverityLevel.ALL)
1281+ trivy.scanImage("ubuntu:24.04", "CRITICAL,LOW")
1282+ ```
1283+
1284+ For the available pre-defined severity levels see [ TrivySeverityLevel.groovy] ( src/com/cloudogu/ces/cesbuildlib/TrivySeverityLevel.groovy )
1285+
1286+ ### Set the pipeline strategy
1287+
1288+ To define how the Jenkins pipeline should behave if vulnerabilities are found, you can set certain strategies:
1289+ - TrivyScanStrategy.IGNORE: Ignore the vulnerabilities and continue
1290+ - TrivyScanStrategy.UNSTABLE: Mark the job as "unstable" and continue
1291+ - TrivyScanStrategy.FAIL: Mark the job as failed
1292+
1293+ ``` groovy
1294+ Trivy trivy = new Trivy(this)
1295+ trivy.scanImage("ubuntu:24.04", TrivySeverityLevel.ALL, TrivyScanStrategy.UNSTABLE)
1296+ ```
1297+
1298+ ### Set additional Trivy flags
1299+
1300+ To set additional Trivy command flags, use the ` additionalFlags ` parameter:
1301+
1302+ ``` groovy
1303+ Trivy trivy = new Trivy(this)
1304+ trivy.scanImage("ubuntu:24.04", TrivySeverityLevel.ALL, TrivyScanStrategy.UNSTABLE, "--db-repository public.ecr.aws/aquasecurity/trivy-db")
1305+ ```
1306+
1307+ Note that the flags ` --db-repository public.ecr.aws/aquasecurity/trivy-db --java-db-repository public.ecr.aws/aquasecurity/trivy-java-db `
1308+ are set by default to avoid rate limiting of Trivy database downloads. If you set ` additionalFlags ` by yourself, you are overwriting
1309+ these default flags and have to make sure to include them in your set of additional flags, if needed.
1310+
1311+ ### Set the Trivy report file name
1312+
1313+ If you want to run multiple image scans in one pipeline, you can set distinct file names for the report files:
1314+
1315+ ``` groovy
1316+ Trivy trivy = new Trivy(this)
1317+ trivy.scanImage("ubuntu:20.04", TrivySeverityLevel.ALL, TrivyScanStrategy.UNSTABLE, "", "trivy/ubuntu20.json")
1318+ trivy.scanImage("ubuntu:24.04", TrivySeverityLevel.ALL, TrivyScanStrategy.UNSTABLE, "", "trivy/ubuntu24.json")
1319+ // Save report by using the same file name (last parameter)
1320+ trivy.saveFormattedTrivyReport(TrivyScanFormat.HTML, "UNKNOWN,LOW,MEDIUM,HIGH,CRITICAL", "ubuntu20.04report", "trivy/ubuntu20.json")
1321+ ```
1322+
1323+ ## Save Trivy report in another file format
1324+
1325+ After calling the ` scanImage ` function you can save the scan report as JSON, HTML or table files.
1326+
1327+ ``` groovy
1328+ Trivy trivy = new Trivy(this)
1329+ trivy.scanImage("ubuntu:24.04")
1330+ trivy.saveFormattedTrivyReport(TrivyScanFormat.TABLE)
1331+ trivy.saveFormattedTrivyReport(TrivyScanFormat.JSON)
1332+ trivy.saveFormattedTrivyReport(TrivyScanFormat.HTML)
1333+ ```
1334+
1335+ You may filter the output to show only specific severity levels (default: ` "UNKNOWN,LOW,MEDIUM,HIGH,CRITICAL" ` ):
1336+
1337+ ``` groovy
1338+ Trivy trivy = new Trivy(this)
1339+ trivy.scanImage("ubuntu:24.04")
1340+ trivy.saveFormattedTrivyReport(TrivyScanFormat.TABLE, "CRITICAL")
1341+ trivy.saveFormattedTrivyReport(TrivyScanFormat.JSON, "UNKNOWN,LOW,MEDIUM")
1342+ ```
1343+
1344+ 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.
1345+
1346+ ``` groovy
1347+ Trivy trivy = new Trivy(this)
1348+ trivy.scanImage("ubuntu:24.04")
1349+ trivy.saveFormattedTrivyReport("cosign-vuln", "UNKNOWN,LOW,MEDIUM,HIGH,CRITICAL", "ubuntu24.04cosign.txt")
1350+ trivy.saveFormattedTrivyReport("template --template @myTemplateFile.xyz", "UNKNOWN,LOW,MEDIUM,HIGH,CRITICAL", "ubuntu24.04myTemplate.txt")
1351+ ```
1352+
1353+ ## Scan Dogu image with Trivy
1354+
1355+ This section describes how to get a Dogu image from the testing CES instance and scan it with Trivy.
1356+
1357+ ### Get Dogu image from CES instance
1358+
1359+ Make sure to have a ` build ` stage in your Dogu test pipeline which builds the Dogu image, e.g. via
1360+ the ` ecoSystem.build("/dogu") ` command.
1361+ After the build stage you will be able to copy the Dogu image to your local Jenkins worker via
1362+ the ` ecoSystem.copyDoguImageToJenkinsWorker("/dogu") ` command.
1363+
1364+ ### Scan Dogu image
1365+
1366+ The ` scanDogu() ` function lets you scan a Dogu image without typing its full name. The method reads the image name
1367+ and version from the dogu.json inside the directory you point it to via its first argument.
1368+ The default directory is the current directory.
1369+
1370+ ``` groovy
1371+ // Preparation
1372+ ecoSystem.copyDoguImageToJenkinsWorker("/dogu")
1373+ Trivy trivy = new Trivy(this)
1374+
1375+ // Scan the Dogu image
1376+ trivy.scanDogu()
1377+ // Explicitly set directory that contains the dogu code (dogu.json)
1378+ trivy.scanDogu("subfolder/test1/jenkins")
1379+ // Set scan options just like in the scanImage method
1380+ trivy.scanDogu(".", TrivySeverityLevel.ALL, TrivyScanStrategy.UNSTABLE, "", "trivy/mydogu.json")
1381+ trivy.saveFormattedTrivyReport(TrivyScanFormat.TABLE)
1382+ trivy.saveFormattedTrivyReport(TrivyScanFormat.JSON)
1383+ trivy.saveFormattedTrivyReport(TrivyScanFormat.HTML)
1384+ ```
1385+
1386+ ## Ignore / allowlist
1387+
1388+ If you want to ignore / allow certain vulnerabilities, please use a ` .trivyignore ` file.
1389+
1390+ Provide the file in your repo ` / ` directory where you run your job, e.g.:
1391+
1392+ ``` shell
1393+ .gitignore
1394+ Jenkinsfile
1395+ .trivyignore
1396+ ```
1397+
1398+ [ Offical documentation] ( https://trivy.dev/v0.57/docs/configuration/filtering/#by-finding-ids )
1399+ ``` ignorelang
1400+ # Accept the risk
1401+ CVE-2018-14618
1402+
1403+ # Accept the risk until 2023-01-01
1404+ CVE-2019-14697 exp:2023-01-01
1405+
1406+ # No impact in our settings
1407+ CVE-2019-1543
1408+
1409+ # Ignore misconfigurations
1410+ AVD-DS-0002
1411+
1412+ # Ignore secrets
1413+ generic-unwanted-rule
1414+ aws-account-id
1415+ ```
1416+
12431417# Steps
12441418
12451419## mailIfStatusChanged
@@ -1293,7 +1467,9 @@ For example, if running on `http(s)://server:port/jenkins`, `server` is returned
12931467
12941468Returns true if the build is successful, i.e. not failed or unstable (yet).
12951469
1296- ## findVulnerabilitiesWithTrivy
1470+ ## findVulnerabilitiesWithTrivy (Deprecated)
1471+
1472+ This function is deprecated. Use [ Trivy] ( #trivy ) functionality instead.
12971473
12981474Returns a list of vulnerabilities or an empty list if there are no vulnerabilities for the given severity.
12991475
@@ -1330,36 +1506,7 @@ node {
13301506}
13311507```
13321508
1333- ### Ignore / allowlist
1334-
1335- If you want to ignore / allow certain vulnerabilities please use a .trivyignore file
1336- Provide the file in your repo / directory where you run your job
1337- e.g.:
1338- ``` shell
1339- .gitignore
1340- Jenkinsfile
1341- .trivyignore
1342- ```
1343-
1344- [ Offical documentation] ( https://aquasecurity.github.io/trivy/v0.41/docs/configuration/filtering/#by-finding-ids )
1345- ``` ignorelang
1346- # Accept the risk
1347- CVE-2018-14618
13481509
1349- # Accept the risk until 2023-01-01
1350- CVE-2019-14697 exp:2023-01-01
1351-
1352- # No impact in our settings
1353- CVE-2019-1543
1354-
1355- # Ignore misconfigurations
1356- AVD-DS-0002
1357-
1358- # Ignore secrets
1359- generic-unwanted-rule
1360- aws-account-id
1361-
1362- ```
13631510
13641511If there are vulnerabilities the output looks as follows.
13651512
0 commit comments