From c82ec744666bbd43b1b041781ee2676face1ccd8 Mon Sep 17 00:00:00 2001 From: Simar Date: Fri, 17 Jun 2022 15:19:49 -0700 Subject: [PATCH] feat(sbom): Support SBOM generation Signed-off-by: Simar --- .github/workflows/build.yaml | 3 +++ README.md | 36 ++++++++++++++++++++++++++++++++++++ action.yaml | 7 +++++-- entrypoint.sh | 10 ++++++++-- test/test.bats | 6 ++++++ 5 files changed, 58 insertions(+), 4 deletions(-) diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index 66a1f6a..09729f8 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -13,6 +13,9 @@ jobs: with: bats-version: 1.2.1 + - name: Setup Bats libs + uses: brokenpip3/setup-bats-libs@0.1.0 + - name: Check out code uses: actions/checkout@v1 diff --git a/README.md b/README.md index eb3328d..d59a319 100644 --- a/README.md +++ b/README.md @@ -227,6 +227,42 @@ jobs: sarif_file: 'trivy-results.sarif' ``` +### Using Trivy to generate SBOM +It's possible for Trivy to generate an SBOM of your dependencies and submit them to a consumer like GitHub Dependency Snapshot. + +The sending of SBOM to GitHub feature is only available if you currently have [GitHub Dependency Snapshot](https://docs.github.com/en/code-security/supply-chain-security/understanding-your-software-supply-chain/about-the-dependency-graph) available to you in your repo. + +In addition to send results to the GitHub Dependency Snapshot, you will need to create a [GitHub PAT](https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/creating-a-personal-access-token) +```yaml +--- +name: Pull Request +on: + push: + branches: + - master + pull_request: +jobs: + build: + name: Checks + runs-on: ubuntu-20.04 + steps: + - name: Checkout code + uses: actions/checkout@v3 + + - name: Run Trivy in GitHub SBOM mode + uses: aquasecurity/trivy-action@master + with: + scan-type: 'sbom' + format: 'github' + output: 'dependency-results.sbom.json' + artifact-type: 'fs' + image-ref: '.' + + - name: Upload Trivy SBOM results to GitHub Dependency tab + run: | + curl -u "${{ secrets.PAT_TOKEN }}" -H 'Content-Type: application/json' 'https://api.github.com/repos/'$GITHUB_REPOSITORY'/dependency-graph/snapshots' -d @./dependency-results.sbom.json +``` + ### Using Trivy to scan your private registry It's also possible to scan your private registry with Trivy's built-in image scan. All you have to do is set ENV vars. diff --git a/action.yaml b/action.yaml index 18c9495..ac51653 100644 --- a/action.yaml +++ b/action.yaml @@ -20,7 +20,6 @@ inputs: exit-code: description: 'exit code when vulnerabilities were found' required: false - default: '0' ignore-unfixed: description: 'ignore unfixed vulnerabilities' required: false @@ -68,7 +67,6 @@ inputs: hide-progress: description: 'hide progress output' required: false - default: 'true' list-all-pkgs: description: 'output all packages regardless of vulnerability' required: false @@ -81,6 +79,10 @@ inputs: description: 'comma-separated list of relative paths in repository to one or more .trivyignore files' required: false default: '' + artifact-type: + description: 'input artifact type (image, fs, repo, archive) for SBOM generation' + required: false + runs: using: 'docker' image: "Dockerfile" @@ -105,3 +107,4 @@ runs: - '-r ${{ inputs.list-all-pkgs }}' - '-s ${{ inputs.security-checks }}' - '-t ${{ inputs.trivyignores }}' + - '-u ${{ inputs.artifact-type }}' diff --git a/entrypoint.sh b/entrypoint.sh index 7529aba..6ad58ad 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -1,6 +1,6 @@ #!/bin/bash set -e -while getopts "a:b:c:d:e:f:g:h:i:j:k:l:m:n:o:p:q:r:s:t:" o; do +while getopts "a:b:c:d:e:f:g:h:i:j:k:l:m:n:o:p:q:r:s:t:u:" o; do case "${o}" in a) export scanType=${OPTARG} @@ -62,6 +62,9 @@ while getopts "a:b:c:d:e:f:g:h:i:j:k:l:m:n:o:p:q:r:s:t:" o; do t) export trivyIgnores=${OPTARG} ;; + u) + export artifactType=${OPTARG} + ;; esac done @@ -97,7 +100,7 @@ if [ "$ignoreUnfixed" == "true" ] && [ "$scanType" != "config" ];then ARGS="$ARGS --ignore-unfixed" SARIF_ARGS="$SARIF_ARGS --ignore-unfixed" fi -if [ $vulnType ] && [ "$scanType" != "config" ];then +if [ $vulnType ] && [ "$scanType" != "config" ] && [ "$scanType" != "sbom" ];then ARGS="$ARGS --vuln-type $vulnType" SARIF_ARGS="$SARIF_ARGS --vuln-type $vulnType" fi @@ -152,6 +155,9 @@ if [ "$skipFiles" ];then ARGS="$ARGS --skip-files $i" done fi +if [ $artifactType ]; then + ARGS="$ARGS --artifact-type $artifactType" +fi echo "Running trivy with options: ${ARGS}" "${artifactRef}" echo "Global options: " "${GLOBAL_ARGS}" diff --git a/test/test.bats b/test/test.bats index 5c4c11c..d2b3132 100644 --- a/test/test.bats +++ b/test/test.bats @@ -55,3 +55,9 @@ result="$(diff ./test/data/image-trivyignores.test image-trivyignores.test)" [ "$result" == '' ] } + +@test "trivy image with sbom option" { + # trivy sbom --format github --artifact-type image knqyf263/vuln-image:1.2.3 + run ./entrypoint.sh "-a sbom" "-b github" "-i knqyf263/vuln-image:1.2.3" "-j ." "-u image" + assert_output --partial '"package_url": "pkg:apk/ca-certificates@20171114-r0",' # TODO: Output contains time, need to mock +}