Skip to content

Commit

Permalink
Add Trivy implementation structure; #136
Browse files Browse the repository at this point in the history
  • Loading branch information
robertauer committed Nov 18, 2024
1 parent c482734 commit c2897fb
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 0 deletions.
44 changes: 44 additions & 0 deletions src/com/cloudogu/ces/cesbuildlib/Trivy.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.cloudogu.ces.cesbuildlib

class Trivy implements Serializable {
def script
String trivyReportFilename

Trivy(script, String trivyReportFilename = "${env.WORKSPACE}/.trivy/trivyReport.json") {
this.script = script
this.trivyReportFilename = trivyReportFilename
}

/**
* Scans an image for vulnerabilities.
* Notes:
* - Use a .trivyignore file for allowed CVEs
* - This function will generate a JSON formatted report file which can be converted to other formats via saveFormattedTrivyReport()
* - Evaluate via exit codes: 0 = no vulnerability; 1 = vulnerabilities found; other = function call failed
*
* @param imageName The image name; may include version tag
* @param trivyVersion The version of Trivy used for scanning
* @param additionalFlags Additional Trivy command flags
* @param scanLevel The vulnerability level to scan. Can be a member of TrivyScanLevel or a custom String (e.g. 'CRITICAL,LOW')
* @param strategy The strategy to follow after the scan. Should the build become unstable or failed? Or Should any vulnerability be ignored? (@see TrivyScanStrategy)
* // TODO: A strategy could be implemented by the user via the exit codes of this function. Should we remove the strategy parameter?
* @return Returns 0 if the scan was ok (no vulnerability found); returns 1 if any vulnerability was found
*/
int scanImage(String imageName, String trivyVersion = "0.57.0", String additionalFlags, String scanLevel = TrivyScanLevel.CRITICAL, String strategy = TrivyScanStrategy.FAIL) {
// TODO: Run trivy scan inside Docker container, e.g. via Jenkins' Docker.image() function
// See runTrivyInDocker function: https://github.com/cloudogu/ces-build-lib/blob/c48273409f8f506e31872fe2857650bbfc76a222/vars/findVulnerabilitiesWithTrivy.groovy#L48
// TODO: Write result to trivyReportFile in json format (--format json), which can be converted in the saveFormattedTrivyReport function
// TODO: Include .trivyignore file, if existent. Do not fail if .trivyignore file does not exist.
}

/**
* Save the Trivy scan results as a file with a specific format
*
* @param format The format of the output file (@see TrivyScanFormat)
*/
void saveFormattedTrivyReport(String format = TrivyScanFormat.HTML) {
// TODO: DO NOT scan again! Take the trivyReportFile and convert its content
// See https://aquasecurity.github.io/trivy/v0.52/docs/references/configuration/cli/trivy_convert/
}

}
21 changes: 21 additions & 0 deletions src/com/cloudogu/ces/cesbuildlib/TrivyScanFormat.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.cloudogu.ces.cesbuildlib

/**
* Defines the output format for the trivy report.
*/
class TrivyScanFormat {
/**
* Output as HTML file.
*/
static String HTML = "html"

/**
* Output as JSON file.
*/
static String JSON = "json"

/**
* Output as plain text file.
*/
static String PLAIN = "plain"
}
26 changes: 26 additions & 0 deletions src/com/cloudogu/ces/cesbuildlib/TrivyScanLevel.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.cloudogu.ces.cesbuildlib

/**
* Defines aggregated vulnerability levels
*/
class TrivyScanLevel {
/**
* Only critical vulnerabilities.
*/
static String CRITICAL = "CRITICAL"

/**
* High or critical vulnerabilities.
*/
static String HIGH = "CRITICAL,HIGH"

/**
* Medium or higher vulnerabilities.
*/
static String MEDIUM = "CRITICAL,HIGH,MEDIUM"

/**
* All vunlerabilities.
*/
static String ALL = "UNKNOWN,LOW,MEDIUM,HIGH,CRITICAL"
}
18 changes: 18 additions & 0 deletions src/com/cloudogu/ces/cesbuildlib/TrivyScanStrategy.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.cloudogu.ces.cesbuildlib

class TrivyScanStrategy {
/**
* Strategy: Fail if any vulnerability was found.
*/
static String FAIL = "fail"

/**
* Strategy: Make build unstable if any vulnerability was found.
*/
static String UNSTABLE = "unstable"

/**
* Strategy: Ignore any found vulnerability.
*/
static String IGNORE = "ignore"
}

0 comments on commit c2897fb

Please sign in to comment.