|
| 1 | +name: Security scan |
| 2 | + |
| 3 | +on: |
| 4 | + # Manual run on a PR branch before merging (required check scan-result |
| 5 | + # stays "expected" until this is run on the latest head SHA). |
| 6 | + workflow_dispatch: |
| 7 | + push: |
| 8 | + branches: |
| 9 | + - "main" |
| 10 | + schedule: |
| 11 | + - cron: '0 0 * * 1' |
| 12 | + |
| 13 | +concurrency: |
| 14 | + group: ${{ github.workflow }}-${{ github.ref }} |
| 15 | + cancel-in-progress: true |
| 16 | + |
| 17 | +# Least privilege for the workflow GITHUB_TOKEN: checkout needs read |
| 18 | +# access only; scan-image and scan-result narrow it further to none. |
| 19 | +permissions: |
| 20 | + contents: read |
| 21 | + |
| 22 | +jobs: |
| 23 | + build-image: |
| 24 | + name: build-image |
| 25 | + runs-on: self-hosted |
| 26 | + timeout-minutes: 30 |
| 27 | + steps: |
| 28 | + - name: Checkout |
| 29 | + uses: actions/checkout@v4 |
| 30 | + |
| 31 | + - name: Set up Docker Buildx |
| 32 | + uses: docker/setup-buildx-action@v3 |
| 33 | + with: |
| 34 | + driver-opts: | |
| 35 | + image=moby/buildkit:v0.19.0 |
| 36 | +
|
| 37 | + - name: Build image without publishing |
| 38 | + uses: docker/build-push-action@v5 |
| 39 | + with: |
| 40 | + context: . |
| 41 | + push: false |
| 42 | + load: true |
| 43 | + tags: eupf:security-scan |
| 44 | + |
| 45 | + - name: Export image |
| 46 | + run: docker save eupf:security-scan | gzip > eupf-security-scan.tar.gz |
| 47 | + |
| 48 | + - name: Upload image artifact |
| 49 | + uses: actions/upload-artifact@v4 |
| 50 | + with: |
| 51 | + name: eupf-security-scan-image |
| 52 | + path: eupf-security-scan.tar.gz |
| 53 | + retention-days: 3 |
| 54 | + |
| 55 | + scan-fs: |
| 56 | + name: scan-fs |
| 57 | + runs-on: self-hosted |
| 58 | + timeout-minutes: 30 |
| 59 | + steps: |
| 60 | + - name: Checkout code |
| 61 | + uses: actions/checkout@v4 |
| 62 | + |
| 63 | + - name: Run Trivy vulnerability scanner in fs mode |
| 64 | + uses: aquasecurity/[email protected] |
| 65 | + with: |
| 66 | + scan-type: 'fs' |
| 67 | + scan-ref: '.' |
| 68 | + trivyignores: .trivyignore |
| 69 | + trivy-config: trivy.yaml |
| 70 | + |
| 71 | + scan-config: |
| 72 | + name: scan-config |
| 73 | + runs-on: self-hosted |
| 74 | + timeout-minutes: 30 |
| 75 | + steps: |
| 76 | + - name: Checkout code |
| 77 | + uses: actions/checkout@v4 |
| 78 | + |
| 79 | + - name: Run Trivy vulnerability scanner in config mode |
| 80 | + uses: aquasecurity/[email protected] |
| 81 | + with: |
| 82 | + scan-type: 'config' |
| 83 | + scan-ref: '.' |
| 84 | + trivyignores: .trivyignore |
| 85 | + trivy-config: trivy.yaml |
| 86 | + |
| 87 | + scan-image: |
| 88 | + name: scan-image |
| 89 | + runs-on: self-hosted |
| 90 | + timeout-minutes: 30 |
| 91 | + needs: [build-image] |
| 92 | + # No GITHUB_TOKEN usage: same-run artifact download uses the |
| 93 | + # ACTIONS_RUNTIME_TOKEN, docker load and trivy are local operations. |
| 94 | + permissions: {} |
| 95 | + steps: |
| 96 | + - name: Download image artifact |
| 97 | + uses: actions/download-artifact@v4 |
| 98 | + with: |
| 99 | + name: eupf-security-scan-image |
| 100 | + |
| 101 | + - name: Load image |
| 102 | + run: docker load < eupf-security-scan.tar.gz |
| 103 | + |
| 104 | + - name: Run Trivy vulnerability scanner in image mode |
| 105 | + uses: aquasecurity/[email protected] |
| 106 | + with: |
| 107 | + scan-type: 'image' |
| 108 | + image-ref: 'eupf:security-scan' |
| 109 | + trivyignores: .trivyignore |
| 110 | + trivy-config: trivy.yaml |
| 111 | + |
| 112 | + # Aggregated result to be marked as the required check in branch |
| 113 | + # protection: fails if any of the scans above failed or were skipped. |
| 114 | + scan-result: |
| 115 | + name: scan-result |
| 116 | + runs-on: ubuntu-latest |
| 117 | + timeout-minutes: 5 |
| 118 | + if: always() |
| 119 | + needs: [build-image, scan-fs, scan-config, scan-image] |
| 120 | + # Pure shell job, no GITHUB_TOKEN usage at all. |
| 121 | + permissions: {} |
| 122 | + steps: |
| 123 | + - name: Report scan result |
| 124 | + run: | |
| 125 | + failed=0 |
| 126 | + if [ "${{ needs.result.build-image }}" != "success" ]; then echo "build-image: ${{ needs.result.build-image }}"; failed=1; fi |
| 127 | + if [ "${{ needs.result.scan-fs }}" != "success" ]; then echo "scan-fs: ${{ needs.result.scan-fs }}"; failed=1; fi |
| 128 | + if [ "${{ needs.result.scan-config }}" != "success" ]; then echo "scan-config: ${{ needs.result.scan-config }}"; failed=1; fi |
| 129 | + if [ "${{ needs.result.scan-image }}" != "success" ]; then echo "scan-image: ${{ needs.result.scan-image }}"; failed=1; fi |
| 130 | + exit $failed |
0 commit comments