Skip to content

Commit

Permalink
feat: add trivy scan
Browse files Browse the repository at this point in the history
  • Loading branch information
ovsds committed Nov 29, 2024
1 parent a6eef13 commit ecdd0d4
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 5 deletions.
9 changes: 9 additions & 0 deletions .github/actions/setup_environment/action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ inputs:
description: "Task version"
required: true
default: "3.33.1"
trivy-version:
description: "Trivy version"
required: true
default: "v0.57.1"
python-version:
description: "Python version"
required: true
Expand All @@ -23,6 +27,11 @@ runs:
with:
version: ${{ inputs.task-version }}

- name: Install Trivy
uses: aquasecurity/[email protected]
with:
version: ${{ inputs.trivy-version }}

- name: Set up Python
uses: actions/setup-python@v4
with:
Expand Down
25 changes: 25 additions & 0 deletions .github/workflows/check-pr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ jobs:

permissions:
contents: read
pull-requests: write

steps:
- name: Checkout repository
Expand All @@ -33,3 +34,27 @@ jobs:
working-directory: backend
run: |
task test-container
- name: Build backend image
working-directory: backend
run: |
task image-build
- name: Scan backend image
id: scan
uses: ovsds/run-with-output-action@v1
continue-on-error: true
with:
run: task backend:image-scan

- name: Report vulnerabilities
uses: ovsds/create-or-update-unique-comment-action@v1
with:
issue-number: ${{ github.event.number }}
body: |
## Vulnerabilities found
```
${{ steps.scan.outputs.stdout }}
```
unique-body-includes: "## Vulnerabilities found"
delete: ${{ steps.scan.outputs.exit_code == 0 }}
47 changes: 47 additions & 0 deletions .github/workflows/release-scan.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
name: 🔒 Security Scan

on:
schedule:
# Every Sunday at 00:00 UTC
- cron: 0 0 * * 0
workflow_dispatch:

jobs:
scan:
runs-on: ubuntu-latest

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

- name: Setup Environment
uses: ./.github/actions/setup_environment

- name: Get latest image tag
id: get_tag
uses: pozetroninc/[email protected]
with:
owner: ${{ github.repository_owner }}
repo: ${{ github.event.repository.name }}
excludes: prerelease, draft

- name: Scan image for vulnerabilities
id: scan
run: |
task backend:ci-image-scan
env:
IMAGE_REGISTRY: ghcr.io/${{ github.repository_owner }}
IMAGE_NAME: ${{ github.event.repository.name }}
IMAGE_TAG: ${{ steps.get_tag.outputs.release }}

- name: Report vulnerabilities
uses: ovsds/create-or-update-unique-issue-action@main
with:
title: "Security scan issues for ${{ steps.get_tag.outputs.release }}"
body: |
## Vulnerabilities found
```
${{ steps.scan.outputs.stdout }}
```
unique-title-includes: "Security scan issues for "
close: ${{ steps.scan.outputs.exit_code == 0 }}
4 changes: 2 additions & 2 deletions backend/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ COPY lib /opt/app/lib
WORKDIR /opt/app
CMD [".venv/bin/python", "-m", "bin.main"]

FROM builder as builder_dev
FROM builder AS builder_dev

RUN poetry install --with dev

FROM ${BASE_RUNTIME_IMAGE} as runtime_dev
FROM ${BASE_RUNTIME_IMAGE} AS runtime_dev

RUN mkdir --parents /opt/app
COPY --from=builder_dev /opt/app/.venv /opt/app/.venv
Expand Down
28 changes: 25 additions & 3 deletions backend/Taskfile.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -116,16 +116,26 @@ tasks:

image-build:
desc: Build image target for local usage
requires:
vars:
- TARGET
cmds:
- echo 'Building image {{.TARGET}} target...'
- docker build
--tag {{.IMAGE_NAME}}:{{.TARGET}}
{{ if .TAG }}--tag {{.TAG}}{{ end }}
--target {{.TARGET}}
--load
.
vars:
TARGET: '{{.TARGET | default "runtime"}}'

image-scan:
desc: Scan image for vulnerabilities
cmds:
- echo 'Scanning image for vulnerabilities...'
- trivy image
--config trivy.yaml
{{.IMAGE_NAME}}:{{.TARGET}}
vars:
TARGET: '{{.TARGET | default "runtime"}}'

test:
desc: Run tests
Expand Down Expand Up @@ -245,3 +255,15 @@ tasks:
cmds:
- echo 'Uploading backend image...'
- docker push {{.IMAGE_REGISTRY}}/{{.IMAGE_NAME}}:{{.IMAGE_TAG}}

ci-image-scan:
desc: Scan image for vulnerabilities
requires:
vars:
- IMAGE_TAG
- IMAGE_REGISTRY
cmds:
- echo 'Scanning image for vulnerabilities...'
- trivy image
--config trivy.yaml
{{.IMAGE_REGISTRY}}/{{.IMAGE_NAME}}:{{.IMAGE_TAG}}
11 changes: 11 additions & 0 deletions backend/trivy.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
scan:
scanners:
- vuln
- misconfig
- secret

severity:
- HIGH
- CRITICAL

exit-code: 1

0 comments on commit ecdd0d4

Please sign in to comment.