generate.sh 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #!/usr/bin/env bash
  2. # Generate the per-release SBOM (CycloneDX + SPDX) for OliveTin.
  3. #
  4. # Called by GoReleaser (sboms, artifacts: any) with the working directory set to dist/.
  5. # Usage: generate.sh <version> [dist-dir]
  6. #
  7. # One SBOM covers every archive/package/MSI because they all ship the same two things:
  8. # - the Go service binary: modules are read from its buildinfo, NOT from service/go.mod
  9. # (go.mod also carries tool deps like golangci-lint that are never linked);
  10. # - the webui bundle: npm runtime deps from frontend/package-lock.json (dev deps excluded).
  11. # Container images get their own per-platform SBOM attestations from BuildKit (dockers_v2 sbom: true).
  12. set -euo pipefail
  13. VERSION="${1:?version is required}"
  14. DIST_DIR="$(cd "${2:-.}" && pwd)"
  15. SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
  16. REPO_ROOT="$(cd "${SCRIPT_DIR}/../.." && pwd)"
  17. # linux/amd64 is the reference: its module set is a superset of the other targets
  18. # (darwin/windows only drop the Linux-only github.com/prometheus/procfs).
  19. BINARY="${DIST_DIR}/OliveTin_linux_amd64_v1/OliveTin"
  20. OUT="${DIST_DIR}/OliveTin-${VERSION}-sbom"
  21. command -v syft >/dev/null || { echo "syft is required to generate SBOMs" >&2; exit 1; }
  22. command -v jq >/dev/null || { echo "jq is required to check SBOMs" >&2; exit 1; }
  23. [[ -f "${BINARY}" ]] || { echo "Reference binary not found: ${BINARY}" >&2; exit 1; }
  24. STAGE="$(mktemp -d)"
  25. trap 'rm -rf "${STAGE}"' EXIT
  26. mkdir -p "${STAGE}/bin" "${STAGE}/webui"
  27. cp "${BINARY}" "${STAGE}/bin/OliveTin"
  28. cp "${REPO_ROOT}/frontend/package.json" "${REPO_ROOT}/frontend/package-lock.json" "${STAGE}/webui/"
  29. SYFT_JAVASCRIPT_INCLUDE_DEV_DEPENDENCIES=false syft scan "dir:${STAGE}" \
  30. --source-name OliveTin \
  31. --source-version "${VERSION}" \
  32. -o "cyclonedx-json=${OUT}.cdx.json" \
  33. -o "spdx-json=${OUT}.spdx.json"
  34. # Sanity checks: catch a scan that picked up go.mod/dev deps or missed a whole ecosystem.
  35. names="$(jq -r '.components[].name' "${OUT}.cdx.json")"
  36. for required in github.com/expr-lang/expr @connectrpc/connect; do
  37. grep -qxF "${required}" <<<"${names}" || { echo "SBOM is missing expected component: ${required}" >&2; exit 1; }
  38. done
  39. for forbidden in github.com/golangci/golangci-lint/v2 vite eslint; do
  40. if grep -qxF "${forbidden}" <<<"${names}"; then
  41. echo "SBOM contains a build/dev-only component: ${forbidden}" >&2
  42. exit 1
  43. fi
  44. done
  45. echo "SBOM written: ${OUT}.cdx.json ($(wc -l <<<"${names}") components), ${OUT}.spdx.json"