Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IT-3421: Add vulnerability scanning to repo #6

Merged
merged 75 commits into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from 74 commits
Commits
Show all changes
75 commits
Select commit Hold shift + click to select a range
f488f21
IT-3421 Add Trivy source code vulnerability scan
brucehoff Jul 7, 2024
5cf5b24
IT-3421 Make Trivy more sensitive
brucehoff Jul 7, 2024
9ad58e0
IT-3421: Add workflow steps to run Trivy on built image, publish resu…
brucehoff Jul 17, 2024
0f3f7f6
IT-3421: fix build for non-tagged push
brucehoff Jul 17, 2024
5d955e3
IT-3421: use format; check for failure in trivy step
brucehoff Jul 17, 2024
216ca1f
IT-3421: fix syntax error
brucehoff Jul 17, 2024
417ad58
IT-3421: fix syntax error
brucehoff Jul 17, 2024
d0ca03c
IT3421: change exit 1 to 0 as a test
brucehoff Jul 17, 2024
5d269e0
IT3421: formatting
brucehoff Jul 17, 2024
585de74
IT3421: set permissions for writing security events
brucehoff Jul 17, 2024
cbd4ab4
IT-3421 trim down permissions
brucehoff Jul 17, 2024
86b0d1d
IT-3421 experiment: don't stop on Trivy failure
brucehoff Jul 17, 2024
376131d
IT-3421 pass tags to both Docker steps
brucehoff Jul 17, 2024
6fe1bc0
IT-3421 Determine default tags earlier in workflow
brucehoff Jul 17, 2024
0ab8f7f
IT-3421: printing mmp, mm values
brucehoff Jul 18, 2024
a12d9c3
IT-3421: printing mmp, mm values
brucehoff Jul 18, 2024
9ff4092
IT-3421 debug: reverse the order for defining mm and mmp
brucehoff Jul 18, 2024
d77878a
IT-3421: Debugging output vars
brucehoff Jul 18, 2024
d4eca18
IT-3421: Debugging output vars
brucehoff Jul 18, 2024
2b2922c
IT-3421 use metadata-action to create Mm and Mmp Docker tags
brucehoff Jul 18, 2024
99ae85a
IT-3421 whitespace
brucehoff Jul 18, 2024
f873eec
IT-3421: added periodic scan and .trivyignore
brucehoff Jul 20, 2024
979d4a4
IT-3421 fix sarif file name
brucehoff Jul 20, 2024
9935fb3
IT-3421 add start and end chars to .yml
brucehoff Jul 20, 2024
8b3bf52
IT-3421 change cron frequency
brucehoff Jul 20, 2024
d6019b9
IT-3421: Suppress private key finding
brucehoff Jul 21, 2024
ae4a328
IT-3421: Add skip-files
brucehoff Jul 21, 2024
2e8b5be
IT_3421: debugging cron
brucehoff Jul 22, 2024
f1065a1
IT-3421: factored trivy into separate GitHub workflow file
brucehoff Jul 22, 2024
7e2dd99
IT-3421: cleaned up workflows
brucehoff Jul 22, 2024
f286e17
IT-3421: adding missing checkout step
brucehoff Jul 22, 2024
26a3b43
IT-3421 fixed up references
brucehoff Jul 22, 2024
c6f987d
IT-3421 fixed up references
brucehoff Jul 22, 2024
515fb64
IT-3421 Don't push docker container while testing
brucehoff Jul 23, 2024
4f2fae1
IT-3421: Call sub-workflow from job, not from step
brucehoff Jul 24, 2024
6c1301e
IT-3421 typo
brucehoff Jul 24, 2024
01b8b04
IT-3421: suppress snakeoil private key finding
brucehoff Jul 24, 2024
a99e569
IT-3421 pass docker image to Trivy as .tat file
brucehoff Jul 24, 2024
77d3488
IT-3421 pass docker image to Trivy as .tar file
brucehoff Jul 24, 2024
1acb1cb
IT-3421 try different way to refer to env
brucehoff Jul 24, 2024
92ff7ef
IT-3421: Change Docker build output type to 'tar'
brucehoff Jul 24, 2024
d95b461
IT-3421: Change push to false
brucehoff Jul 24, 2024
1b2431e
IT-3421: Fix upload and download of artifact
brucehoff Jul 24, 2024
25fac9d
IT-3421: Unique artifact names
brucehoff Jul 24, 2024
2f2e44e
IT-3421: Change ref to artifact
brucehoff Jul 24, 2024
f805b93
IT-3421: Fixed switching logic
brucehoff Jul 24, 2024
b25bf8f
IT-3421: Debug switching logic
brucehoff Jul 24, 2024
430ec74
IT-3421: typo
brucehoff Jul 24, 2024
876943a
IT-3421: Debug switching logic
brucehoff Jul 24, 2024
3f70013
IT-3421: download 'path' is only the 'dir'
brucehoff Jul 24, 2024
683f18f
IT-3421: fixed whitespace
brucehoff Jul 24, 2024
b58e0ed
IT-3421: fixed image-ref
brucehoff Jul 24, 2024
3d7d944
IT-3421: fixed image-ref
brucehoff Jul 24, 2024
040e482
IT-3421: fixed image-ref
brucehoff Jul 24, 2024
9490ce1
IT-3421: try changing 'tar' to 'osi' in Docker output
brucehoff Jul 24, 2024
d4f656f
IT-3421: try changing 'tar' to 'oci' in Docker output
brucehoff Jul 24, 2024
220559c
IT-3421: back to 'tar'
brucehoff Jul 24, 2024
54efa92
IT-3421: load docker image instead of reading .tar
brucehoff Jul 24, 2024
64cb354
IT-3421: use docker import not docker load
brucehoff Jul 24, 2024
a25b9e6
IT-3421: Revert to docker 'load'; download .tar to /tmp; export from …
brucehoff Jul 24, 2024
f837fe5
IT-3421: Trying type=tar & docker load & download to /tmp
brucehoff Jul 24, 2024
181ca83
IT-3421: Switch load to import again
brucehoff Jul 24, 2024
a3644b4
IT-3421: Use 'docker import' in 'push-image' job
brucehoff Jul 24, 2024
8849d44
IT-3421: Fixed error in 'push-image' job
brucehoff Jul 24, 2024
5891670
IT-3421: Fixed line breaks
brucehoff Jul 24, 2024
7fe6e1f
IT-3421: Added build to needs for push-image job
brucehoff Jul 24, 2024
aeb7f64
IT-3421: Adding checkout to push job
brucehoff Jul 24, 2024
2160adf
IT-3421: list images
brucehoff Jul 24, 2024
aa0bee8
IT-3421: push built image
brucehoff Jul 24, 2024
77540cc
IT-3421: testing failure; adding Readme doc'
brucehoff Jul 24, 2024
eee6dc6
IT-3421: fixed passing of EXIT_CODE; added comments
brucehoff Jul 25, 2024
671c540
IT-3421 clean up white space
brucehoff Jul 25, 2024
d47c52a
performed TODOs
brucehoff Jul 25, 2024
1430396
fixed comment
brucehoff Jul 25, 2024
c474a28
IT-3421: set default exit code for failed Trivy scan
brucehoff Jul 30, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 67 additions & 14 deletions .github/workflows/docker_build_push.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
---
#
# This workflow builds a Docker image and passes it to Trivy for
# vulnerability scanning, only publishing it to ghrc.io if
# the scan passes.
#
name: Build and publish a Docker image

on:
Expand All @@ -9,39 +14,87 @@ on:
type: string

env:
REGISTRY: ghcr.io
IMAGE_PATH: ghcr.io/${{ github.repository }}-${{ inputs.NOTEBOOK_TYPE }}
TARFILE_NAME: ${{ inputs.NOTEBOOK_TYPE }}-image.tar

jobs:
build-and-push-image:
build:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write

steps:
- name: Checkout repository
uses: actions/checkout@v2

- name: Log in to the Container registry
uses: docker/[email protected]
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
uses: actions/checkout@v4

- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/[email protected]
with:
images: ${{ env.IMAGE_PATH }}
tags: |
type=ref,event=branch
type=ref,event=pr
type=semver,pattern={{version}} # major.minor.patch
type=semver,pattern={{major}}.{{minor}}

- name: Build and push Docker image
uses: docker/build-push-action@v3.2.0
- name: Build Docker image for scanning, but don't push to ghcr.io yet
uses: docker/build-push-action@v6.4.0
with:
context: .
build-args: notebook_type=${{ inputs.NOTEBOOK_TYPE }}
push: true
push: false
outputs: type=tar,dest=${{ env.TARFILE_NAME }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}

- name: Upload tarball for use by Trivy job
uses: actions/upload-artifact@v4
with:
name: ${{ env.TARFILE_NAME }}
path: ${{ env.TARFILE_NAME }}

outputs:
tags: ${{ steps.meta.outputs.tags }}
tarfile_artifact: ${{ env.TARFILE_NAME }}

trivy-scan:
needs: build
uses: "./.github/workflows/trivy.yml"
with:
NOTEBOOK_TYPE: ${{ inputs.NOTEBOOK_TYPE }}
SOURCE_TYPE: tar
zaro0508 marked this conversation as resolved.
Show resolved Hide resolved
IMAGE_NAME: ${{ needs.build.outputs.tags }}
TARFILE_NAME: ${{ needs.build.outputs.tarfile_artifact }}
EXIT_CODE: 1

push-image:
needs: [build, trivy-scan]
runs-on: ubuntu-latest
permissions:
contents: read
packages: write

steps:
- name: Download tar file
id: tar-download
uses: actions/download-artifact@v4
with:
name: ${{ env.TARFILE_NAME }}
path: /tmp

- name: Load Docker image from tar
run: cat
${{ steps.tar-download.outputs.download-path}}/${{ env.TARFILE_NAME}}
| docker import - ${{ needs.build.outputs.tags }}

- name: Login to GitHub Container Registry
uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Push Docker image
run: docker push ${{ needs.build.outputs.tags }}
...
3 changes: 2 additions & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ jobs:
uses: pre-commit/[email protected]

- name: Build Docker Image
uses: docker/build-push-action@v3.2.0
uses: docker/build-push-action@v6.4.0
with:
context: .
build-args: notebook_type=jupyter
push: false

- name: Install dependencies
run: pip install -r requirements.txt -r requirements-dev.txt
Expand Down
87 changes: 87 additions & 0 deletions .github/workflows/trivy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
---
#
# This workflow runs Trivy on a Docker image
# It can pull the image from a container registry
# or download a tar file. The latter is used
# to check a container image prior to publishing
# to the registry.

name: Run Trivy on a Docker image and push results to GitHub

on:
workflow_call:
inputs:
NOTEBOOK_TYPE:
required: true
type: string
SOURCE_TYPE: # 'tar' or 'image'
required: true
type: string
TARFILE_NAME: # only used if SOURCE_TYPE=='tar'
required: false
type: string
IMAGE_NAME:
required: true
type: string
EXIT_CODE:
required: false
brucehoff marked this conversation as resolved.
Show resolved Hide resolved
type: number

env:
sarif_file_name: trivy-results-${{ inputs.NOTEBOOK_TYPE }}.sarif

jobs:
trivy:
name: Trivy-${{ inputs.NOTEBOOK_TYPE }}
runs-on: ubuntu-latest
permissions:
contents: read
security-events: write

steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Download tar file
id: tar-download
uses: actions/download-artifact@v4
if: ${{ inputs.SOURCE_TYPE == 'tar' }}
with:
name: ${{ inputs.TARFILE_NAME }}
path: /tmp

- name: load docker image from tar file
if: ${{ inputs.SOURCE_TYPE == 'tar' }}
run: cat ${{ steps.tar-download.outputs.download-path
}}/${{ inputs.TARFILE_NAME
}} | docker import - ${{ inputs.IMAGE_NAME }}

- name: Run Trivy vulnerability scanner for any major issues
uses: aquasecurity/[email protected]
id: trivy
with:
image-ref: ${{ inputs.IMAGE_NAME }}
ignore-unfixed: true # skip vul'ns for which there is no fix
# list files to skip, each with a justification
skip-files: |
/etc/ssl/private/ssl-cert-snakeoil.key
# ssl-cert-snakeoil.key is req'd by the ssl package.
severity: 'CRITICAL,HIGH'
brucehoff marked this conversation as resolved.
Show resolved Hide resolved
format: 'sarif'
# only output findings for configured severities
limit-severities-for-sarif: true
output: ${{ env.sarif_file_name }}
exit-code: ${{ inputs.EXIT_CODE }}

- name: Upload Trivy scan results to GitHub Security tab
uses: github/codeql-action/[email protected]
# This is the recommended way to upload scan results
# after Trivy exits with HIGH/CRITICAL findings
# See https://github.com/aquasecurity/trivy-action?\
# tab=readme-ov-file#using-trivy-with-github-code-scanning
if: ${{ success() || steps.trivy.conclusion=='failure' }}
with:
sarif_file: ${{ env.sarif_file_name }}
category: ${{ inputs.NOTEBOOK_TYPE }}
wait-for-processing: true
...
29 changes: 29 additions & 0 deletions .github/workflows/trivy_periodic_image_scan.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
---
#
# This workflow scans the published container images
# for new vulnerabilities daily, publishing findings.
# Findings will be associated with the 'main' branch
# of the repo' in the GitHub Security tab.
#
name: Trivy Periodic Image Scan

on:
schedule:
# run daily
- cron: "0 0 * * *"

jobs:
trivy-matrix:
zaro0508 marked this conversation as resolved.
Show resolved Hide resolved
name: ${{ matrix.notebook_type }}
strategy:
matrix:
notebook_type:
- jupyter
- rstudio
uses: "./.github/workflows/trivy.yml"
with:
NOTEBOOK_TYPE: ${{ matrix.notebook_type }}
SOURCE_TYPE: image
IMAGE_NAME: ghcr.io/${{ github.repository
}}-${{ matrix.notebook_type }}:main
...
12 changes: 12 additions & 0 deletions .trivyignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#
# List vulnerabilities flagged by Trivy but for which
# the affected code is not used or the risk is acceptable.
# Enter the ID of the vulnerability along with the
# justification as comment, for example:
#
# # Accept the risk
# CVE-2018-14618
#
# More here:
# https://aquasecurity.github.io/trivy/v0.22.0/vulnerability/examples/filter/
#
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,20 @@ docker run -d --name ${NOTEBOOK_CONTAINER_NAME} \
${DOCKER_IMAGE}

```


## Security

Trivy is run on each built container and they will not be published
to `ghcr.io` if any CRITICAL or HIGH
vulnerabilites are found. Trivy is also run daily to check for new
vulnerabilities in existing images. So periodic review of new findings
is needed: Go to the Security tab in GitHub, select Code Scanning at left,
and then select Branch > Main to check for new findings. To suppress
false positives, either:

- Enter the CVE in `.trivyignore`, or

- Enter the file to skip while scanning in the `trivy.yml` workflow.

In either case, add a comment justifying why the finding is suppressd.
Loading