|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# SBOM directory path |
| 4 | +SBOM_DIR="/opt/kayobe/stackhpc/sboms" |
| 5 | + |
| 6 | +# Ensure the SBOM directory exists |
| 7 | +mkdir -p "$SBOM_DIR" |
| 8 | + |
| 9 | +# Ensure the custom output template exists |
| 10 | +cat <<EOL > "$SBOM_DIR/trivy-custom.tmpl" |
| 11 | +"Package","Version Installed","Vulnerability ID","Severity","Title" |
| 12 | +{{- range \$ri, \$r := . }} |
| 13 | +{{- range \$vi, \$v := .Vulnerabilities }} |
| 14 | +"{{ $v.PkgName }}","{{$v.InstalledVersion }}","{{ $v.VulnerabilityID }}","{{$v.Severity }}","{{$v.Title }}" |
| 15 | +{{- end}} |
| 16 | +{{- end }} |
| 17 | +EOL |
| 18 | + |
| 19 | +# Loop through each container image and process its SBOM |
| 20 | +docker image ls --format "{{.Repository}}:{{.Tag}}" | sort | uniq | while read -r image; do |
| 21 | + # Generate SBOM filename |
| 22 | + sbom_file="$SBOM_DIR/$(echo "$image" | tr '/:' '_').sbom" |
| 23 | + |
| 24 | + # Generate SBOM if missing |
| 25 | + if [[ ! -f "$sbom_file" ]]; then |
| 26 | + echo "Generating SBOM for $image..." |
| 27 | + if ! trivy image --quiet --format spdx-json --output "$sbom_file" "$image"; then |
| 28 | + echo "Failed to generate SBOM for $image. Skipping." |
| 29 | + continue |
| 30 | + fi |
| 31 | + fi |
| 32 | + |
| 33 | + # Scan SBOM and prepend image info to each output line |
| 34 | + trivy sbom \ |
| 35 | + --scanners vuln \ |
| 36 | + --severity CRITICAL,HIGH \ |
| 37 | + --ignore-unfixed \ |
| 38 | + --quiet \ |
| 39 | + --format template \ |
| 40 | + --template "@$SBOM_DIR/trivy-custom.tmpl" \ |
| 41 | + "$sbom_file" | \ |
| 42 | + awk -v img="$image" '{print "Trivy:\"" img "\"," $0}' |
| 43 | +done |
0 commit comments