|
| 1 | +name: Scan Container Image Grype SARIF |
| 2 | +description: 'Scan a container image for vulnerabilities and optionally upload the results for GitHub code scanning' |
| 3 | +inputs: |
| 4 | + image-ref: |
| 5 | + description: 'The image to scan' |
| 6 | + required: true |
| 7 | + upload-sarif: |
| 8 | + description: 'Whether to upload the scan results as a SARIF file' |
| 9 | + required: false |
| 10 | + default: 'true' |
| 11 | + severity-cutoff: |
| 12 | + description: 'Minimum severity to report (critical, high, medium, low, negligible)' |
| 13 | + required: false |
| 14 | + default: 'medium' |
| 15 | + fail-build: |
| 16 | + description: 'Fail the workflow if vulnerabilities are found' |
| 17 | + required: false |
| 18 | + default: 'true' |
| 19 | + output-file: |
| 20 | + description: 'Output file name for SARIF results' |
| 21 | + required: false |
| 22 | + default: 'results.sarif' |
| 23 | + timeout-minutes: |
| 24 | + description: 'Maximum time in minutes to wait for the scan to complete' |
| 25 | + required: false |
| 26 | + default: '30' |
| 27 | + retention-days: |
| 28 | + description: 'Number of days to retain the scan results artifact' |
| 29 | + required: false |
| 30 | + default: '90' |
| 31 | + category-prefix: |
| 32 | + description: 'Prefix to use for the SARIF category name' |
| 33 | + required: false |
| 34 | + default: 'image-scan-' |
| 35 | + only-fixed: |
| 36 | + description: 'Only report vulnerabilities that have a fix available' |
| 37 | + required: false |
| 38 | + default: 'true' |
| 39 | + |
| 40 | +runs: |
| 41 | + using: 'composite' |
| 42 | + steps: |
| 43 | + - name: Extract image details |
| 44 | + id: image_details |
| 45 | + shell: bash |
| 46 | + run: | |
| 47 | + IMAGE_NAME=$(echo "${{ inputs.image-ref }}" | cut -d':' -f1) |
| 48 | + IMAGE_TAG=$(echo "${{ inputs.image-ref }}" | cut -d':' -f2) |
| 49 | + [[ "$IMAGE_TAG" == "$IMAGE_NAME" ]] && IMAGE_TAG="latest" |
| 50 | + SAFE_NAME=$(echo "${IMAGE_NAME}-${IMAGE_TAG}" | sed 's/[\/:]/-/g') |
| 51 | + { |
| 52 | + echo "image_name=${IMAGE_NAME}" |
| 53 | + echo "image_tag=${IMAGE_TAG}" |
| 54 | + echo "safe_name=${SAFE_NAME}" |
| 55 | + } >> "$GITHUB_OUTPUT" |
| 56 | +
|
| 57 | + - name: Scan image with Grype |
| 58 | + uses: anchore/scan-action@v6 |
| 59 | + id: scan |
| 60 | + continue-on-error: ${{ inputs.fail-build != 'true' }} |
| 61 | + with: |
| 62 | + image: "${{ inputs.image-ref }}" |
| 63 | + fail-build: "${{ inputs.fail-build }}" |
| 64 | + severity-cutoff: "${{ inputs.severity-cutoff }}" |
| 65 | + output-format: sarif |
| 66 | + output-file: "${{ inputs.output-file }}" |
| 67 | + by-cve: true |
| 68 | + only-fixed: "${{ inputs.only-fixed }}" |
| 69 | + |
| 70 | + - name: Check scan status |
| 71 | + if: steps.scan.outcome == 'failure' && inputs.fail-build == 'true' |
| 72 | + shell: bash |
| 73 | + run: | |
| 74 | + echo "::error::Scan failed for image ${{ inputs.image-ref }}" |
| 75 | + echo "Please check the scan logs above for details" |
| 76 | + exit 1 |
| 77 | +
|
| 78 | + - name: Enrich or generate SARIF |
| 79 | + if: ${{ !cancelled() && inputs.upload-sarif == 'true' }} |
| 80 | + shell: bash |
| 81 | + run: | |
| 82 | + if [ ! -f ${{ inputs.output-file }} ]; then |
| 83 | + echo "No SARIF file found — creating minimal empty SARIF" |
| 84 | + echo '{"version":"2.1.0","runs":[{"tool":{"driver":{"name":"Anchore Grype","informationUri":"https://github.com/anchore/grype","rules":[]}},"results":[],"properties":{"isFallbackSarif":true}}]}' > ${{ inputs.output-file }} |
| 85 | + fi |
| 86 | +
|
| 87 | + # Validate SARIF file before enrichment |
| 88 | + if ! jq empty ${{ inputs.output-file }}; then |
| 89 | + echo "::error::Invalid SARIF file detected" |
| 90 | + exit 1 |
| 91 | + fi |
| 92 | +
|
| 93 | + # Create a backup of the original file |
| 94 | + cp ${{ inputs.output-file }} ${{ inputs.output-file }}.bak |
| 95 | +
|
| 96 | + # Attempt to enrich the SARIF file |
| 97 | + if ! jq --arg imageRef "${{ inputs.image-ref }}" \ |
| 98 | + --arg repo "replicatedhq/ekco" \ |
| 99 | + --arg name "${{ steps.image_details.outputs.image_name }}" \ |
| 100 | + --arg tag "${{ steps.image_details.outputs.image_tag }}" \ |
| 101 | + --arg scanTime "$(date -u +"%Y-%m-%dT%H:%M:%SZ")" \ |
| 102 | + --arg fullName "replicatedhq/${{ steps.image_details.outputs.image_name }}" \ |
| 103 | + ' |
| 104 | + def strip_registry: |
| 105 | + if startswith("docker.io/") then |
| 106 | + sub("^docker\\.io/"; "") |
| 107 | + else |
| 108 | + . |
| 109 | + end; |
| 110 | + def make_asset_id: |
| 111 | + "image:" + (. | strip_registry); |
| 112 | + def make_full_ref: |
| 113 | + if contains("/") then . else "replicatedhq/" + . end; |
| 114 | + .runs[0].properties = { |
| 115 | + "imageRef": ($fullName + ":" + $tag), |
| 116 | + "repository": $repo, |
| 117 | + "scanTime": $scanTime, |
| 118 | + "DisplayName": ($fullName + ":" + $tag | make_asset_id), |
| 119 | + "UniqueID": ($fullName + ":" + $tag | make_asset_id), |
| 120 | + "imageMetadata": { |
| 121 | + "name": $fullName, |
| 122 | + "tag": $tag, |
| 123 | + "annotations": { |
| 124 | + "scanTime": $scanTime, |
| 125 | + "tool": "grype", |
| 126 | + "toolVersion": "latest" |
| 127 | + } |
| 128 | + } |
| 129 | + } | |
| 130 | + .runs[0].results[] |= ( |
| 131 | + if .locations then |
| 132 | + .locations[].logicalLocations[].fullyQualifiedName |= |
| 133 | + if contains("docker.io/") then |
| 134 | + . |
| 135 | + else |
| 136 | + "docker.io/" + ($fullName + ":" + $tag) + |
| 137 | + "@" + (. | split("@")[1]) |
| 138 | + end |
| 139 | + else |
| 140 | + . |
| 141 | + end |
| 142 | + )' ${{ inputs.output-file }} > enriched-results.sarif; then |
| 143 | + echo "::error::Failed to enrich SARIF file" |
| 144 | + # Restore the backup |
| 145 | + mv ${{ inputs.output-file }}.bak ${{ inputs.output-file }} |
| 146 | + exit 1 |
| 147 | + fi |
| 148 | +
|
| 149 | + # Validate the enriched file |
| 150 | + if ! jq empty enriched-results.sarif; then |
| 151 | + echo "::error::Invalid enriched SARIF file" |
| 152 | + # Restore the backup |
| 153 | + mv ${{ inputs.output-file }}.bak ${{ inputs.output-file }} |
| 154 | + exit 1 |
| 155 | + fi |
| 156 | +
|
| 157 | + mv enriched-results.sarif ${{ inputs.output-file }} |
| 158 | + rm -f ${{ inputs.output-file }}.bak |
| 159 | +
|
| 160 | + - name: Upload SARIF file |
| 161 | + if: ${{ !cancelled() && inputs.upload-sarif == 'true' }} |
| 162 | + uses: github/codeql-action/upload-sarif@v3 |
| 163 | + with: |
| 164 | + sarif_file: ${{ inputs.output-file }} |
| 165 | + category: '${{ inputs.category-prefix }}${{ steps.image_details.outputs.safe_name }}' |
| 166 | + |
| 167 | + - name: Archive scan results |
| 168 | + if: ${{ !cancelled() && inputs.upload-sarif == 'true' }} |
| 169 | + uses: actions/upload-artifact@v4 |
| 170 | + with: |
| 171 | + name: "sarif-${{ steps.image_details.outputs.safe_name }}" |
| 172 | + path: ${{ inputs.output-file }} |
| 173 | + retention-days: ${{ inputs.retention-days }} |
0 commit comments