|
| 1 | +name: Leaked Secrets Scan |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_call: |
| 5 | + schedule: |
| 6 | + - cron: '0 3 * * *' # Daily at 3 AM UTC (3-4 AM UK time) |
| 7 | + workflow_dispatch: |
| 8 | + push: |
| 9 | + branches: [main] |
| 10 | + pull_request: |
| 11 | + branches: [main] |
| 12 | + |
| 13 | +jobs: |
| 14 | + secrets-scan: |
| 15 | + runs-on: ubuntu-latest |
| 16 | + name: ${{ github.event_name == 'schedule' && 'Scheduled Secrets Scan' || 'Secrets Scan' }} |
| 17 | + |
| 18 | + steps: |
| 19 | + - name: Checkout repository |
| 20 | + uses: actions/checkout@v4 |
| 21 | + with: |
| 22 | + fetch-depth: 0 |
| 23 | + |
| 24 | + - name: Set up Python |
| 25 | + uses: actions/setup-python@v5 |
| 26 | + with: |
| 27 | + python-version: '3.12' |
| 28 | + cache: 'pip' |
| 29 | + |
| 30 | + - name: Install detect-secrets |
| 31 | + run: | |
| 32 | + python -m pip install --upgrade pip |
| 33 | + pip install detect-secrets |
| 34 | +
|
| 35 | + - name: Verify baseline exists |
| 36 | + run: | |
| 37 | + if [ ! -f .secrets.baseline ]; then |
| 38 | + echo "::error::.secrets.baseline not found!" |
| 39 | + exit 1 |
| 40 | + fi |
| 41 | + echo "Found .secrets.baseline" |
| 42 | +
|
| 43 | + - name: Scan for secrets |
| 44 | + run: | |
| 45 | + echo "Scanning for secrets..." |
| 46 | + detect-secrets scan \ |
| 47 | + --baseline .secrets.baseline \ |
| 48 | + --exclude-files '.*\.lock$' \ |
| 49 | + --exclude-files '.*migrations/versions/.*\.py$' \ |
| 50 | + --force-use-all-plugins |
| 51 | +
|
| 52 | + - name: Audit baseline for unaudited secrets |
| 53 | + run: | |
| 54 | + echo "Auditing secrets baseline..." |
| 55 | + if grep -q '"is_secret": null' .secrets.baseline; then |
| 56 | + echo "::error::Found unaudited secrets in baseline! Run: detect-secrets audit .secrets.baseline" |
| 57 | + detect-secrets audit .secrets.baseline --report |
| 58 | + exit 1 |
| 59 | + fi |
| 60 | + echo "All secrets in baseline have been audited" |
| 61 | + detect-secrets audit .secrets.baseline --report |
| 62 | +
|
| 63 | + - name: Check for new secrets in PR |
| 64 | + if: github.event_name == 'pull_request' |
| 65 | + run: | |
| 66 | + echo "Checking for new secrets in PR..." |
| 67 | + mkdir -p /tmp/pr-scan |
| 68 | + git diff origin/main...HEAD --name-only | while read -r file; do |
| 69 | + if [ -f "$file" ]; then |
| 70 | + mkdir -p "/tmp/pr-scan/$(dirname "$file")" 2>/dev/null || true |
| 71 | + cp "$file" "/tmp/pr-scan/$file" 2>/dev/null || true |
| 72 | + fi |
| 73 | + done |
| 74 | +
|
| 75 | + if [ "$(ls -A /tmp/pr-scan 2>/dev/null)" ]; then |
| 76 | + echo "Scanning changed files..." |
| 77 | + detect-secrets scan \ |
| 78 | + --baseline .secrets.baseline \ |
| 79 | + --exclude-files '.*\.lock$' \ |
| 80 | + --exclude-files '.*migrations/versions/.*\.py$' \ |
| 81 | + --force-use-all-plugins \ |
| 82 | + /tmp/pr-scan || echo "No new secrets found" |
| 83 | + else |
| 84 | + echo "No files to scan" |
| 85 | + fi |
| 86 | +
|
| 87 | + - name: Full repository scan (scheduled) |
| 88 | + if: github.event_name == 'schedule' |
| 89 | + run: | |
| 90 | + echo "Performing full repository scan..." |
| 91 | + detect-secrets scan \ |
| 92 | + --exclude-files '.*\.lock$' \ |
| 93 | + --exclude-files '.*migrations/versions/.*\.py$' \ |
| 94 | + --force-use-all-plugins |
| 95 | +
|
| 96 | + - name: Upload baseline on failure |
| 97 | + uses: actions/upload-artifact@v4 |
| 98 | + if: failure() |
| 99 | + with: |
| 100 | + name: secrets-scan-results |
| 101 | + path: .secrets.baseline |
| 102 | + retention-days: 30 |
0 commit comments