|
| 1 | +# Daily security scan of published Docker images |
| 2 | +name: 🔒 Daily Security Scan |
| 3 | + |
| 4 | +on: |
| 5 | + schedule: |
| 6 | + # Run daily at 6 AM UTC |
| 7 | + - cron: '0 6 * * *' |
| 8 | + workflow_dispatch: # Allow manual trigger |
| 9 | + |
| 10 | +permissions: |
| 11 | + contents: read |
| 12 | + security-events: write |
| 13 | + issues: write # Required for creating issues on vulnerabilities |
| 14 | + |
| 15 | +jobs: |
| 16 | + scan-published-image: |
| 17 | + name: 🔍 Scan published image |
| 18 | + runs-on: ubuntu-latest |
| 19 | + timeout-minutes: 15 |
| 20 | + steps: |
| 21 | + - name: 📂 Checkout |
| 22 | + uses: actions/checkout@v4 |
| 23 | + |
| 24 | + - name: 🔍 Run Trivy scan on latest image |
| 25 | + uses: aquasecurity/trivy-action@0.29.0 |
| 26 | + id: trivy-scan |
| 27 | + with: |
| 28 | + image-ref: lotest/locust-k8s-operator:latest |
| 29 | + format: sarif |
| 30 | + output: trivy-results.sarif |
| 31 | + severity: CRITICAL,HIGH |
| 32 | + exit-code: 0 # Don't fail, we'll create an issue instead |
| 33 | + |
| 34 | + - name: 📊 Upload Trivy results to GitHub Security tab |
| 35 | + if: always() |
| 36 | + uses: github/codeql-action/upload-sarif@v3 |
| 37 | + with: |
| 38 | + sarif_file: trivy-results.sarif |
| 39 | + category: trivy-daily-scan |
| 40 | + |
| 41 | + - name: 🔍 Check for vulnerabilities |
| 42 | + id: check-vulns |
| 43 | + run: | |
| 44 | + # Check if SARIF file contains vulnerabilities |
| 45 | + if grep -q '"level": "error"' trivy-results.sarif || grep -q '"level": "warning"' trivy-results.sarif; then |
| 46 | + echo "vulnerabilities_found=true" >> $GITHUB_OUTPUT |
| 47 | + else |
| 48 | + echo "vulnerabilities_found=false" >> $GITHUB_OUTPUT |
| 49 | + fi |
| 50 | + |
| 51 | + - name: 🚨 Create issue for vulnerabilities |
| 52 | + if: steps.check-vulns.outputs.vulnerabilities_found == 'true' |
| 53 | + uses: actions/github-script@v7 |
| 54 | + with: |
| 55 | + script: | |
| 56 | + const fs = require('fs'); |
| 57 | + const sarif = JSON.parse(fs.readFileSync('trivy-results.sarif', 'utf8')); |
| 58 | + |
| 59 | + let vulnerabilities = []; |
| 60 | + for (const run of sarif.runs) { |
| 61 | + for (const result of run.results || []) { |
| 62 | + const severity = result.level === 'error' ? 'CRITICAL/HIGH' : 'MEDIUM'; |
| 63 | + const ruleId = result.ruleId || 'Unknown'; |
| 64 | + const message = result.message?.text || 'No description'; |
| 65 | + vulnerabilities.push(`- **${ruleId}** (${severity}): ${message}`); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + if (vulnerabilities.length === 0) { |
| 70 | + console.log('No vulnerabilities found in SARIF'); |
| 71 | + return; |
| 72 | + } |
| 73 | + |
| 74 | + const issueTitle = `🚨 Security vulnerabilities found in latest Docker image`; |
| 75 | + const issueBody = `## Security Scan Alert |
| 76 | + |
| 77 | + Daily security scan detected vulnerabilities in \`lotest/locust-k8s-operator:latest\`. |
| 78 | + |
| 79 | + ### Vulnerabilities Found: |
| 80 | + ${vulnerabilities.slice(0, 20).join('\n')} |
| 81 | + ${vulnerabilities.length > 20 ? `\n\n... and ${vulnerabilities.length - 20} more` : ''} |
| 82 | + |
| 83 | + ### Next Steps: |
| 84 | + 1. Review the full scan results in the [Security tab](${context.payload.repository.html_url}/security/code-scanning) |
| 85 | + 2. Update dependencies or Go version as needed |
| 86 | + 3. Rebuild and push a new image |
| 87 | + |
| 88 | + **Scan Date:** ${new Date().toISOString().split('T')[0]} |
| 89 | + **Image:** lotest/locust-k8s-operator:latest |
| 90 | + `; |
| 91 | + |
| 92 | + // Check if an open issue already exists |
| 93 | + const issues = await github.rest.issues.listForRepo({ |
| 94 | + owner: context.repo.owner, |
| 95 | + repo: context.repo.repo, |
| 96 | + state: 'open', |
| 97 | + labels: 'security,automated' |
| 98 | + }); |
| 99 | + |
| 100 | + const existingIssue = issues.data.find(issue => |
| 101 | + issue.title.includes('Security vulnerabilities found') |
| 102 | + ); |
| 103 | + |
| 104 | + if (existingIssue) { |
| 105 | + // Update existing issue |
| 106 | + await github.rest.issues.createComment({ |
| 107 | + owner: context.repo.owner, |
| 108 | + repo: context.repo.repo, |
| 109 | + issue_number: existingIssue.number, |
| 110 | + body: `## New Scan Results\n\n${issueBody}` |
| 111 | + }); |
| 112 | + console.log(`Updated existing issue #${existingIssue.number}`); |
| 113 | + } else { |
| 114 | + // Create new issue |
| 115 | + await github.rest.issues.create({ |
| 116 | + owner: context.repo.owner, |
| 117 | + repo: context.repo.repo, |
| 118 | + title: issueTitle, |
| 119 | + body: issueBody, |
| 120 | + labels: ['security', 'automated'] |
| 121 | + }); |
| 122 | + console.log('Created new security issue'); |
| 123 | + } |
| 124 | + |
| 125 | + - name: 📦 Upload scan results |
| 126 | + if: always() |
| 127 | + uses: actions/upload-artifact@v4 |
| 128 | + with: |
| 129 | + name: trivy-daily-scan-${{ github.run_number }} |
| 130 | + path: trivy-results.sarif |
| 131 | + retention-days: 30 |
0 commit comments