|
| 1 | +#!/bin/bash |
| 2 | +# MiOS v0.2.0 90-generate-sbom: Generate Software Bill of Materials (SBOM) |
| 3 | +# Uses Syft to generate CycloneDX and SPDX manifests for the final image. |
| 4 | +set -euo pipefail |
| 5 | + |
| 6 | +# shellcheck source=lib/common.sh |
| 7 | +source "$(dirname "$0")/lib/common.sh" |
| 8 | + |
| 9 | +echo "[90-generate-sbom] Starting SBOM generation..." |
| 10 | + |
| 11 | +ARTIFACT_DIR="/usr/lib/mios/artifacts/sbom" |
| 12 | +mkdir -p "$ARTIFACT_DIR" |
| 13 | + |
| 14 | +# Check if syft is available |
| 15 | +if ! command -v syft &> /dev/null; then |
| 16 | + echo "[90-generate-sbom] WARN: Syft not found. Attempting to install..." |
| 17 | + # Syft is in packages-utils, but if it's missing for some reason, try to install it. |
| 18 | + dnf install -y syft || { |
| 19 | + echo "[90-generate-sbom] ERROR: Failed to install Syft. Skipping SBOM generation." |
| 20 | + exit 0 # Non-fatal |
| 21 | + } |
| 22 | +fi |
| 23 | + |
| 24 | +VERSION=$(cat /ctx/VERSION 2>/dev/null || echo "v0.2.0") |
| 25 | +TIMESTAMP=$(date -u +%Y%m%dT%H%M%SZ) |
| 26 | + |
| 27 | +echo "[90-generate-sbom] Scanning root filesystem..." |
| 28 | + |
| 29 | +# Generate CycloneDX (JSON) - Primary for AI and automation |
| 30 | +syft scan dir:/ \ |
| 31 | + --output cyclonedx-json \ |
| 32 | + --file "${ARTIFACT_DIR}/mios-sbom-${VERSION}-${TIMESTAMP}.cyclonedx.json" \ |
| 33 | + --exclude "/ctx" \ |
| 34 | + --exclude "/var/cache" |
| 35 | + |
| 36 | +# Generate SPDX (Tag-Value) - Standard compliance |
| 37 | +syft scan dir:/ \ |
| 38 | + --output spdx-tag-value \ |
| 39 | + --file "${ARTIFACT_DIR}/mios-sbom-${VERSION}-${TIMESTAMP}.spdx.txt" \ |
| 40 | + --exclude "/ctx" \ |
| 41 | + --exclude "/var/cache" |
| 42 | + |
| 43 | +# Create latest symlinks |
| 44 | +ln -sf "mios-sbom-${VERSION}-${TIMESTAMP}.cyclonedx.json" "${ARTIFACT_DIR}/latest.cyclonedx.json" |
| 45 | +ln -sf "mios-sbom-${VERSION}-${TIMESTAMP}.spdx.txt" "${ARTIFACT_DIR}/latest.spdx.txt" |
| 46 | + |
| 47 | +echo "[90-generate-sbom] SBOMs generated in ${ARTIFACT_DIR}:" |
| 48 | +ls -lh "$ARTIFACT_DIR" |
| 49 | + |
| 50 | +echo "[90-generate-sbom] Done." |
0 commit comments