apiVersion: argoproj.io/v1alpha1
kind: WorkflowTemplate
metadata:
  name: trivy-scan
  labels:
    dagweave.io/catalog-item: trivy-scan
  annotations:
    # The catalog semver this manifest was authored at. The dagweave app records
    # (catalog-item-id, semver) -> content-hash -> base-vN when it mints the tenant
    # cluster name via ResolveOrMint. Keep in sync with metadata.yaml `version`.
    dagweave.io/catalog-version: "1.0.3"
    dagweave.io/catalog-category: "CI/CD"
spec:
  entrypoint: scan
  templates:
    - name: scan
      inputs:
        parameters:
          # What to scan: an image reference (e.g. alpine:3.21) or a filesystem path.
          # If the target exists as a path in the container it is scanned as a
          # filesystem, otherwise it is treated as an image reference.
          - name: target
          # Comma-separated severities to report.
          - name: severity
            default: "CRITICAL,HIGH"
          # When "true", the step fails (exit 1) if any matching vulnerability is
          # found; when "false", it always reports and succeeds.
          - name: exit-on-findings
            default: "true"
      container:
        # Pinned by digest per the catalog convention: an image rebuild can never
        # silently change the behaviour of an already-released item version.
        # aquasec/trivy:0.76.1
        image: aquasec/trivy@sha256:790fd66d1697395d8693d1acaa1ea702406a20f109b6fc7bac8db623eca6c06c
        command: [sh, -c]
        # The target, severity and flag are passed as env vars (below), never
        # interpolated from Argo into the shell, so values with quotes or shell
        # metacharacters are safe. The scan mode (image vs fs) is inferred from whether
        # the target resolves to a path in the container.
        args:
          - |
            set -eu
            EXIT_CODE=0
            if [ "$EXIT_ON_FINDINGS" = "true" ]; then EXIT_CODE=1; fi
            if [ -e "$TARGET" ]; then MODE=fs; else MODE=image; fi
            trivy "$MODE" --severity "$SEVERITY" --exit-code "$EXIT_CODE" "$TARGET"
        env:
          - name: TARGET
            value: "{{inputs.parameters.target}}"
          - name: SEVERITY
            value: "{{inputs.parameters.severity}}"
          - name: EXIT_ON_FINDINGS
            value: "{{inputs.parameters.exit-on-findings}}"
