action.yml 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. name: 'Provenance / SBOM / Sign'
  2. description: 'Creates SBOM & provenance files and signs the image'
  3. inputs:
  4. image-name:
  5. description: "name of the image"
  6. required: true
  7. default: ''
  8. image-tag:
  9. description: "image tag"
  10. required: true
  11. default: ""
  12. runs:
  13. using: "composite"
  14. steps:
  15. - name: Install cosign
  16. # https://github.com/sigstore/cosign-installer/releases/tag/v4.0.0
  17. uses: sigstore/cosign-installer@faadad0cce49287aee09b3a48701e75088a2c6ad # v4.0.0
  18. with:
  19. cosign-release: 'v3.0.2'
  20. - name: Install Syft
  21. # https://github.com/anchore/sbom-action/releases/tag/v0.22.2
  22. uses: anchore/sbom-action/download-syft@28d71544de8eaf1b958d335707167c5f783590ad # v0.22.2
  23. with:
  24. syft-version: v1.41.2
  25. - name: Check Cosign install
  26. shell: bash
  27. run: cosign version
  28. - name: Login to ghcr.io
  29. uses: docker/login-action@5e57cd118135c172c3672efd75eb46360885c0ef # v3.6.0
  30. with:
  31. registry: ghcr.io
  32. username: ${{ github.actor }}
  33. password: ${{ github.token }}
  34. - name: Setup Go
  35. uses: actions/setup-go@44694675825211faa026b3c33043df3e48a5fa00 # v6.0.0
  36. with:
  37. go-version-file: go.mod
  38. - name: Set up crane
  39. shell: bash
  40. run: go install github.com/google/go-containerregistry/cmd/crane@v0.11.0
  41. - name: Get docker image tag
  42. id: container_info
  43. shell: bash
  44. env:
  45. IMAGE_NAME: ${{ inputs.image-name }}
  46. IMAGE_TAG: ${{ inputs.image-tag }}
  47. run: |
  48. echo "::group::Crane digest lookup"
  49. echo "Looking up digest for ${IMAGE_NAME}:${IMAGE_TAG}"
  50. DIGEST=$(crane digest ${IMAGE_NAME}:${IMAGE_TAG})
  51. echo "Found digest: ${DIGEST}"
  52. echo "digest=${DIGEST}" >> $GITHUB_OUTPUT
  53. echo "::endgroup::"
  54. - name: Sign image
  55. shell: bash
  56. env:
  57. IMAGE_NAME: ${{ inputs.image-name }}
  58. CONTAINER_DIGEST: ${{ steps.container_info.outputs.digest }}
  59. GITHUB_TRIGGERING_ACTOR: ${{ github.triggering_actor }}
  60. run: |
  61. echo "::group::Cosign sign"
  62. echo "Signing ${IMAGE_NAME}@${CONTAINER_DIGEST}"
  63. cosign sign --yes --new-bundle-format=false --use-signing-config=false -a GITHUB_ACTOR=${GITHUB_TRIGGERING_ACTOR} "${IMAGE_NAME}@${CONTAINER_DIGEST}"
  64. echo "::endgroup::"
  65. - name: Attach SBOM to image
  66. shell: bash
  67. id: sbom
  68. env:
  69. IMAGE_NAME: ${{ inputs.image-name }}
  70. IMAGE_TAG: ${{ inputs.image-tag }}
  71. CONTAINER_DIGEST: ${{ steps.container_info.outputs.digest }}
  72. run: |
  73. echo "::group::Image SBOM generation"
  74. # Image SBOM (OS + application libs contained in the image)
  75. echo "Generating image SBOM for ${IMAGE_NAME}@${CONTAINER_DIGEST}"
  76. syft "${IMAGE_NAME}@${CONTAINER_DIGEST}" -o spdx-json=sbom.${IMAGE_TAG}.spdx.json
  77. ORIGINAL_IMAGE_SBOM_SIZE="$(wc -c < sbom.${IMAGE_TAG}.spdx.json)"
  78. echo "Original image SBOM size: ${ORIGINAL_IMAGE_SBOM_SIZE} bytes"
  79. MAX_SBOM_SIZE_BYTES=10000000
  80. echo "Deduplicating image SPDX package nodes and relationships"
  81. bash ./hack/dedupe-spdx-gomod.sh \
  82. --input sbom.${IMAGE_TAG}.spdx.json \
  83. --output sbom.${IMAGE_TAG}.dedup.spdx.json
  84. DEDUP_IMAGE_SBOM_SIZE="$(wc -c < sbom.${IMAGE_TAG}.dedup.spdx.json)"
  85. echo "Deduplicated image SBOM size: ${DEDUP_IMAGE_SBOM_SIZE} bytes"
  86. if [[ "${DEDUP_IMAGE_SBOM_SIZE}" -gt "${MAX_SBOM_SIZE_BYTES}" ]]; then
  87. echo "Deduped image SBOM still above ${MAX_SBOM_SIZE_BYTES} bytes, dropping file ownership data"
  88. bash ./hack/dedupe-spdx-gomod.sh \
  89. --input sbom.${IMAGE_TAG}.spdx.json \
  90. --output sbom.${IMAGE_TAG}.dedup.spdx.json \
  91. --drop-file-ownership
  92. DEDUP_IMAGE_SBOM_SIZE="$(wc -c < sbom.${IMAGE_TAG}.dedup.spdx.json)"
  93. echo "Ownership-pruned deduplicated image SBOM size: ${DEDUP_IMAGE_SBOM_SIZE} bytes"
  94. fi
  95. if [[ "${DEDUP_IMAGE_SBOM_SIZE}" -gt "${MAX_SBOM_SIZE_BYTES}" ]]; then
  96. echo "Image SBOM predicate is still too large (${DEDUP_IMAGE_SBOM_SIZE} bytes)."
  97. echo "Refusing attestation to avoid Rekor submission retries/failure."
  98. exit 1
  99. fi
  100. echo "::endgroup::"
  101. echo "::group::Attest image SBOM"
  102. cosign attest --yes --new-bundle-format=false --use-signing-config=false --predicate sbom.${IMAGE_TAG}.dedup.spdx.json --type spdx "${IMAGE_NAME}@${CONTAINER_DIGEST}"
  103. echo "::endgroup::"
  104. echo "::group::Verify image SBOM attestation"
  105. echo "Using certificate-identity-regexp: https://github.com/$GITHUB_REPOSITORY/.*"
  106. cosign verify-attestation --type spdx ${IMAGE_NAME}@${CONTAINER_DIGEST} \
  107. --certificate-identity-regexp "https://github.com/$GITHUB_REPOSITORY/.*" \
  108. --certificate-oidc-issuer https://token.actions.githubusercontent.com | jq '.payload |= @base64d | .payload | fromjson'
  109. echo "::endgroup::"
  110. echo "::group::Go modules SBOM generation"
  111. # Go modules SBOM (dependencies from the source tree)
  112. # Requires repository to be checked out before this composite action runs.
  113. syft dir:. -o spdx-json=sbom.gomod.${IMAGE_TAG}.spdx.json
  114. ORIGINAL_GOMOD_SBOM_SIZE="$(wc -c < sbom.gomod.${IMAGE_TAG}.spdx.json)"
  115. echo "Original Go modules SBOM size: ${ORIGINAL_GOMOD_SBOM_SIZE} bytes"
  116. echo "Deduplicating Go modules SPDX package nodes and relationships"
  117. bash ./hack/dedupe-spdx-gomod.sh \
  118. --input sbom.gomod.${IMAGE_TAG}.spdx.json \
  119. --output sbom.gomod.${IMAGE_TAG}.dedup.spdx.json
  120. DEDUP_GOMOD_SBOM_SIZE="$(wc -c < sbom.gomod.${IMAGE_TAG}.dedup.spdx.json)"
  121. echo "Deduplicated Go modules SBOM size: ${DEDUP_GOMOD_SBOM_SIZE} bytes"
  122. # Rekor requests can fail when predicates are too large. If the deduped
  123. # SBOM is still big, drop file ownership-heavy data and re-check size.
  124. if [[ "${DEDUP_GOMOD_SBOM_SIZE}" -gt "${MAX_SBOM_SIZE_BYTES}" ]]; then
  125. echo "Deduped SBOM still above ${MAX_SBOM_SIZE_BYTES} bytes, dropping file ownership data"
  126. bash ./hack/dedupe-spdx-gomod.sh \
  127. --input sbom.gomod.${IMAGE_TAG}.spdx.json \
  128. --output sbom.gomod.${IMAGE_TAG}.dedup.spdx.json \
  129. --drop-file-ownership
  130. DEDUP_GOMOD_SBOM_SIZE="$(wc -c < sbom.gomod.${IMAGE_TAG}.dedup.spdx.json)"
  131. echo "Ownership-pruned deduplicated Go modules SBOM size: ${DEDUP_GOMOD_SBOM_SIZE} bytes"
  132. fi
  133. if [[ "${DEDUP_GOMOD_SBOM_SIZE}" -gt "${MAX_SBOM_SIZE_BYTES}" ]]; then
  134. echo "Go modules SBOM predicate is still too large (${DEDUP_GOMOD_SBOM_SIZE} bytes)."
  135. echo "Refusing attestation to avoid Rekor submission retries/failure."
  136. exit 1
  137. fi
  138. echo "::endgroup::"
  139. echo "::group::Attest Go modules SBOM"
  140. cosign attest --yes --new-bundle-format=false --use-signing-config=false --predicate sbom.gomod.${IMAGE_TAG}.dedup.spdx.json --type spdx "${IMAGE_NAME}@${CONTAINER_DIGEST}"
  141. echo "::endgroup::"
  142. echo "::group::Verify Go modules SBOM attestation"
  143. cosign verify-attestation --type spdx ${IMAGE_NAME}@${CONTAINER_DIGEST} \
  144. --certificate-identity-regexp "https://github.com/$GITHUB_REPOSITORY/.*" \
  145. --certificate-oidc-issuer https://token.actions.githubusercontent.com | jq ' .payload |= @base64d | .payload | fromjson | .subject'
  146. echo "::endgroup::"
  147. - name: Generate provenance
  148. shell: bash
  149. env:
  150. IMAGE_NAME: ${{ inputs.image-name }}
  151. IMAGE_TAG: ${{ inputs.image-tag }}
  152. CONTAINER_DIGEST: ${{ steps.container_info.outputs.digest }}
  153. run: |
  154. echo "::group::Generate provenance"
  155. ./hack/generate-provenance.sh \
  156. --repository "${IMAGE_NAME}" \
  157. --digest "${CONTAINER_DIGEST}" \
  158. --tags "${IMAGE_TAG}" \
  159. --output-path "provenance.${IMAGE_TAG}.intoto.jsonl"
  160. echo "::endgroup::"
  161. - name: Attach provenance
  162. shell: bash
  163. id: provenance
  164. env:
  165. IMAGE_NAME: ${{ inputs.image-name }}
  166. IMAGE_TAG: ${{ inputs.image-tag }}
  167. CONTAINER_DIGEST: ${{ steps.container_info.outputs.digest }}
  168. run: |
  169. echo "::group::Prepare provenance predicate"
  170. jq '.predicate' provenance.${IMAGE_TAG}.intoto.jsonl > provenance-predicate.att
  171. echo "::endgroup::"
  172. echo "::group::Attest provenance"
  173. cosign attest --yes --new-bundle-format=false --use-signing-config=false --predicate provenance-predicate.att --type slsaprovenance "${IMAGE_NAME}@${CONTAINER_DIGEST}"
  174. echo "::endgroup::"
  175. echo "::group::Verify provenance attestation"
  176. cosign verify-attestation --type slsaprovenance ${IMAGE_NAME}@${CONTAINER_DIGEST} \
  177. --certificate-identity-regexp "https://github.com/$GITHUB_REPOSITORY/.*" \
  178. --certificate-oidc-issuer https://token.actions.githubusercontent.com
  179. echo "::endgroup::"