---
name: Trivy vulnerability scanner
on:
  push:
    branches:
      - master
  schedule:
    - cron: '0 0 * * *'  # Run daily at midnight UTC
jobs:
  build:
    name: Build
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6

      - name: Parse go version from release-tools/prow.sh
        run: |
          GO_VERSION=$(cat release-tools/prow.sh  | grep "configvar CSI_PROW_GO_VERSION_BUILD" | awk '{print $3}' | sed 's/"//g')
          echo "Using go from release-tools: $GO_VERSION"
          echo "$GO_VERSION" >> '.go-version'

      - name: Install go
        uses: actions/setup-go@4a3601121dd01d1626a1e23e37211e3254c1c06c # v6
        with:
          go-version-file: .go-version

      - name: Build images from Dockerfile
        run: |
          make
          docker build -t test/csi-snapshotter:latest -f ./cmd/csi-snapshotter/Dockerfile --output=type=docker --label revision=latest .
          docker build -t test/snapshot-controller:latest -f ./cmd/snapshot-controller/Dockerfile --output=type=docker --label revision=latest .
          docker build -t test/snapshot-conversion-webhook:latest -f ./cmd/snapshot-conversion-webhook/Dockerfile --output=type=docker --label revision=latest .

      - name: Run Trivy vulnerability scanner on csi-snapshotter image
        uses: aquasecurity/trivy-action@57a97c7e7821a5776cebc9bb87c984fa69cba8f1  # v0.35.0
        env:
          TRIVY_DB_REPOSITORY: "public.ecr.aws/aquasecurity/trivy-db:2"
        with:
          image-ref: 'test/csi-snapshotter:latest'
          format: 'table'
          exit-code: '1'
          ignore-unfixed: true
          vuln-type: 'os,library'
          severity: 'CRITICAL,HIGH,MEDIUM,LOW,UNKNOWN'

      - name: Run Trivy vulnerability scanner on snapshotter-controller image
        uses: aquasecurity/trivy-action@57a97c7e7821a5776cebc9bb87c984fa69cba8f1  # v0.35.0
        env:
          TRIVY_DB_REPOSITORY: "public.ecr.aws/aquasecurity/trivy-db:2"
        with:
          image-ref: 'test/snapshot-controller:latest'
          format: 'table'
          exit-code: '1'
          ignore-unfixed: true
          vuln-type: 'os,library'
          severity: 'CRITICAL,HIGH,MEDIUM,LOW,UNKNOWN'

      - name: Run Trivy vulnerability scanner on snapshot-conversion-webhook image
        uses: aquasecurity/trivy-action@57a97c7e7821a5776cebc9bb87c984fa69cba8f1  # v0.35.0
        env:
          TRIVY_DB_REPOSITORY: "public.ecr.aws/aquasecurity/trivy-db:2"
        with:
          image-ref: 'test/snapshot-conversion-webhook:latest'
          format: 'table'
          exit-code: '1'
          ignore-unfixed: true
          vuln-type: 'os,library'
          severity: 'CRITICAL,HIGH,MEDIUM,LOW,UNKNOWN'
