Skip to content

Commit

Permalink
feat(sbom): Support SBOM generation
Browse files Browse the repository at this point in the history
Signed-off-by: Simar <simar@linux.com>
  • Loading branch information
simar7 committed Jun 17, 2022
1 parent 49e970d commit 7290abc
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 4 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/build.yaml
Expand Up @@ -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

Expand Down
36 changes: 36 additions & 0 deletions README.md
Expand Up @@ -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.

Expand Down
7 changes: 5 additions & 2 deletions action.yaml
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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"
Expand All @@ -105,3 +107,4 @@ runs:
- '-r ${{ inputs.list-all-pkgs }}'
- '-s ${{ inputs.security-checks }}'
- '-t ${{ inputs.trivyignores }}'
- '-u ${{ inputs.artifact-type }}'
10 changes: 8 additions & 2 deletions 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}
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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}"
Expand Down
8 changes: 8 additions & 0 deletions test/test.bats
@@ -1,4 +1,6 @@
#!/usr/bin/env bats
load '/usr/lib/bats-support/load.bash'
load '/usr/lib/bats-assert/load.bash'

@test "trivy image" {
# trivy image --severity CRITICAL --format json --output image.test knqyf263/vuln-image:1.2.3
Expand Down Expand Up @@ -55,3 +57,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
}

0 comments on commit 7290abc

Please sign in to comment.