Scan Container for Vulnerabilities #35
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Scan Container for Vulnerabilities | |
permissions: | |
contents: read | |
on: | |
workflow_dispatch: | |
schedule: | |
- cron: "0 6 * * 1" # At 06:00 on Monday. | |
jobs: | |
CVE-scan: | |
runs-on: ubuntu-latest | |
environment: dev | |
permissions: | |
issues: write | |
security-events: write # for github/codeql-action/upload-sarif to upload SARIF results | |
steps: | |
- name: Checkout the code | |
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 #v4.1.1 | |
- name: Build the Docker image | |
run: "docker build . --tag localbuild/testimage:latest" | |
- name: Trivy scan | |
id: scan | |
uses: aquasecurity/trivy-action@d43c1f16c00cfd3978dde6c07f4bbcf9eb6993ca # v0.16.1 | |
with: | |
image-ref: "localbuild/testimage:latest" | |
format: "sarif" | |
output: "results.sarif" | |
- name: Upload Trivy scan results to GitHub Security tab | |
uses: github/codeql-action/upload-sarif@v3 | |
with: | |
sarif_file: "results.sarif" | |
- name: CVE Description escaped extraction and print | |
run: | | |
SCAN_RESULTS=$(jq -r '.runs[0].tool.driver.rules | map(.help.text) | join("\\n")' results.sarif) | |
echo "CVE_CRITICAL=$(echo $SCAN_RESULTS | grep -o CRITICAL | wc -l)" >> $GITHUB_ENV | |
echo "CVE_HIGH=$(echo $SCAN_RESULTS | grep -o HIGH | wc -l)" >> $GITHUB_ENV | |
echo "CVE_MEDIUM=$(echo $SCAN_RESULTS | grep -o MEDIUM | wc -l)" >> $GITHUB_ENV | |
echo $SCAN_RESULTS | |
- name: Fails if CVE HIGH or CRITICAL are detected | |
id: cve-threshold | |
if: env.CVE_HIGH > 0 || env.CVE_CRITICAL > 0 | |
run: exit 1 | |
- name: Send notification to Slack | |
id: slack | |
if: always() && github.event_name == 'schedule' && steps.cve-threshold.outcome == 'failure' | |
uses: slackapi/slack-github-action@e28cf165c92ffef168d23c5c9000cffc8a25e117 #v1.24.0 | |
with: | |
payload: | | |
{ | |
"blocks": [ | |
{ | |
"type": "header", | |
"text": { | |
"type": "plain_text", | |
"text": "[ ${{ github.event.repository.name }} ]" | |
} | |
}, | |
{ | |
"type": "section", | |
"text": { | |
"type": "mrkdwn", | |
"text": " `CRITICAL` : *${{ env.CVE_CRITICAL }}*\n\n`HIGH` : *${{ env.CVE_HIGH }}*\n\n`MEDIUM` : *${{ env.CVE_MEDIUM }}*\n\n<https://github.com/${{ github.repository }}/security/code-scanning |See details on GitHub>" | |
} | |
} | |
] | |
} | |
env: | |
SLACK_WEBHOOK_URL: ${{ secrets.CVE_SCAN_SLACK_WEBHOOK }} | |
SLACK_WEBHOOK_TYPE: INCOMING_WEBHOOK |