Skip to content
105 changes: 94 additions & 11 deletions .github/workflows/security.yaml
Original file line number Diff line number Diff line change
@@ -1,31 +1,114 @@
name: Security
name: Security Scan

on:
push:
branches:
- main
branches: [main]
pull_request:
branches: [main]
workflow_dispatch:

jobs:
build:
name: Build
runs-on: ubuntu-20.04
trivy-scan:
name: Trivy
runs-on: ubuntu-latest
permissions:
contents: read
security-events: write
actions: read

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

- name: Trivy scan
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.11'

- name: Install dependencies
run: |
python -m pip install --upgrade pip
if [ -f pyproject.toml ]; then
pip install -e ".[dev]"
fi

- name: Run Trivy vulnerability scan
uses: aquasecurity/[email protected]
with:
scan-type: 'fs'
scan-ref: '.'
format: 'sarif'
output: 'trivy-results.sarif'
severity: 'MEDIUM,HIGH,CRITICAL'
severity: 'CRITICAL,HIGH,MEDIUM,LOW'
exit-code: '0'
ignore-unfixed: false
- name: Check for critical and high vulnerabilities
uses: aquasecurity/[email protected]
with:
scan-type: 'fs'
scan-ref: '.'
format: 'table'
severity: 'CRITICAL,HIGH'
exit-code: '1'
continue-on-error: true

- name: Update Security tab
- name: Upload Trivy scan results to Security tab
uses: github/codeql-action/upload-sarif@v3
if: always()
with:
sarif_file: 'trivy-results.sarif'
category: 'trivy-security-scan'

bandit-scan:
name: Bandit
runs-on: ubuntu-latest
permissions:
security-events: write
actions: read
contents: read
checks: write

steps:
- uses: actions/checkout@v4

- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: "3.11"
cache: "pip"

- name: Create virtual environment
run: |
python -m pip install --upgrade pip
python -m venv .venv

- name: Install dependencies
run: |
source .venv/bin/activate
pip install -e ".[dev]"

- name: Install Bandit
run: |
source .venv/bin/activate
pip install bandit[sarif]

- name: Run Bandit Security Scan
uses: PyCQA/bandit-action@v1
with:
targets: "."
exclude: "tests"

Comment on lines +89 to +98
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Bandit is installed separately despite being used via a GitHub Action.

Check if installing Bandit separately is required, as the GitHub Action may already handle this. Remove the step if it's unnecessary.

Suggested change
- name: Install Bandit
run: |
source .venv/bin/activate
pip install bandit[sarif]
- name: Run Bandit Security Scan
uses: PyCQA/bandit-action@v1
with:
targets: "."
exclude: "tests"
- name: Run Bandit Security Scan
uses: PyCQA/bandit-action@v1
with:
targets: "."
exclude: "tests"

- name: Upload SARIF results to Security tab
if: github.ref == 'refs/heads/main'
uses: github/codeql-action/upload-sarif@v3
with:
sarif_file: results.sarif
category: bandit-security-scan
continue-on-error: true

- name: Upload SARIF as artifact
uses: actions/upload-artifact@v4
with:
sarif_file: 'trivy-results.sarif'
name: bandit-sarif-results
path: results.sarif
retention-days: 30
continue-on-error: true
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,4 @@ markers = [
[tool.setuptools.packages.find]
where = ["src"]

[tool.setuptools_scm]
[tool.setuptools_scm]
Loading