Repository navigation
feat(actions): add attest-chart and setup-cyclonedx #40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
tuxerrante
wants to merge
5
commits into
main
Choose a base branch
from
feat/attest-chart-action
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
3d1940b
chore: add a yamllint config for workflow and action files
tuxerrante 6b005c8
feat(actions): add attest-chart and setup-cyclonedx
tuxerrante d02d693
fix(attest-chart): accept only GHCR chart refs
tuxerrante 097e1fa
fix(attest-chart): match the default signing identity literally
tuxerrante 5d4105f
fix(attest-chart): merge only the image SBOMs bom.json references
tuxerrante File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| # Permissive on layout, strict on syntax: GitHub workflow and action files | ||
| # use long run: lines, `on:` keys and SHA-pin comments the defaults reject. | ||
| extends: default | ||
|
|
||
| rules: | ||
| document-start: disable | ||
| truthy: | ||
| check-keys: false | ||
| line-length: | ||
| max: 200 | ||
| comments: | ||
| min-spaces-from-content: 1 | ||
| comments-indentation: disable |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,184 @@ | ||
| name: bom attest chart | ||
| description: > | ||
| Merge the per-image CycloneDX SBOMs that a chart's bom.json references, written by generate | ||
| with sbom: true, into one chart-level SBOM and attest it on the pushed chart's GHCR digest | ||
| with keyless Cosign. | ||
| inputs: | ||
| chart-path: | ||
| description: Chart directory whose bom.json lists the per-image SBOMs to merge. Not used when sbom-path is set. | ||
| required: false | ||
| default: "" | ||
| chart-name: | ||
| description: Name of the software the merged SBOM describes, usually the chart name. | ||
| required: false | ||
| default: "" | ||
| chart-version: | ||
| description: Version of the software the merged SBOM describes, usually the chart version. | ||
| required: false | ||
| default: "" | ||
| chart-group: | ||
| description: Group of the software the merged SBOM describes. | ||
| required: false | ||
| default: codesphere | ||
| chart-ref: | ||
| description: Pushed chart to attest, as ghcr.io/<repository>@sha256:<digest>. Required when attest is true. | ||
| required: false | ||
| default: "" | ||
| sbom-path: | ||
| description: An already merged chart SBOM to attest. Skips the merge. | ||
| required: false | ||
| default: "" | ||
| output-dir: | ||
| description: Directory the merged SBOM is written to, as <chart-name>-<chart-version>.cdx.json. | ||
| required: false | ||
| default: sbom-out | ||
| spec-version: | ||
| description: CycloneDX specification version of the merged SBOM, as cyclonedx-cli's --output-version takes it. | ||
| required: false | ||
| default: v1_7 | ||
| attest: | ||
| description: Attest the SBOM on chart-ref and verify the attestation. Requires id-token write permission. | ||
| required: false | ||
| default: "true" | ||
| certificate-identity-regexp: | ||
| description: Signing identity the attestation is verified against. Defaults to any workflow of the calling repository. | ||
| required: false | ||
| default: "" | ||
| registry-token: | ||
| description: GHCR token cosign uses for the chart registry; the default needs the workflow permission packages write. | ||
| required: false | ||
| default: ${{ github.token }} | ||
| outputs: | ||
| sbom-path: | ||
| description: The merged or given chart SBOM. Empty when the chart has no image SBOMs. | ||
| value: ${{ steps.merge.outputs.sbom-path }} | ||
| runs: | ||
| using: composite | ||
| steps: | ||
| # GitHub only warns on a missing required input of a composite action, so | ||
| # these checks are the real enforcement, and they run before anything is | ||
| # downloaded or signed. | ||
| - name: Validate inputs | ||
| shell: bash | ||
| env: | ||
| CHART_PATH: ${{ inputs.chart-path }} | ||
| CHART_NAME: ${{ inputs.chart-name }} | ||
| CHART_VERSION: ${{ inputs.chart-version }} | ||
| CHART_REF: ${{ inputs.chart-ref }} | ||
| SBOM_PATH: ${{ inputs.sbom-path }} | ||
| SPEC_VERSION: ${{ inputs.spec-version }} | ||
| ATTEST: ${{ inputs.attest }} | ||
| run: | | ||
| if [ -n "$SBOM_PATH" ]; then | ||
| if [ ! -f "$SBOM_PATH" ]; then | ||
| echo "::error title=No SBOM::sbom-path '$SBOM_PATH' does not exist" | ||
| exit 1 | ||
| fi | ||
| elif [ -z "$CHART_PATH" ] || [ -z "$CHART_NAME" ] || [ -z "$CHART_VERSION" ]; then | ||
| echo "::error title=Missing inputs::chart-path, chart-name and chart-version are required unless sbom-path is set" | ||
| exit 1 | ||
| fi | ||
| if ! [[ "$SPEC_VERSION" =~ ^v1_[0-9]+$ ]]; then | ||
| echo "::error title=Invalid spec-version::expected v1_<minor>, got '$SPEC_VERSION'" | ||
| exit 1 | ||
| fi | ||
| # Registry credentials reach cosign only through the GHCR keychain (see | ||
| # the attest step), so another registry would fail to authorize. | ||
| if [ "$ATTEST" = "true" ] && ! [[ "$CHART_REF" =~ ^ghcr\.io/[^@[:space:]]+@sha256:[0-9a-f]{64}$ ]]; then | ||
| echo "::error title=Invalid chart-ref::attesting needs ghcr.io/<repository>@sha256:<digest>, got '$CHART_REF'" | ||
| exit 1 | ||
| fi | ||
|
|
||
| - id: merge | ||
| name: Merge image SBOMs into the chart SBOM | ||
| shell: bash | ||
| env: | ||
| CHART_PATH: ${{ inputs.chart-path }} | ||
| CHART_NAME: ${{ inputs.chart-name }} | ||
| CHART_VERSION: ${{ inputs.chart-version }} | ||
| CHART_GROUP: ${{ inputs.chart-group }} | ||
| SBOM_PATH: ${{ inputs.sbom-path }} | ||
| OUTPUT_DIR: ${{ inputs.output-dir }} | ||
| SPEC_VERSION: ${{ inputs.spec-version }} | ||
| INSTALL_DIR: ${{ runner.temp }}/cyclonedx-cli | ||
| run: | | ||
| if [ -n "$SBOM_PATH" ]; then | ||
| echo "sbom-path=$SBOM_PATH" >> "$GITHUB_OUTPUT" | ||
| exit 0 | ||
| fi | ||
|
|
||
| # generate keeps SBOMs of digests the chart no longer uses, so only | ||
| # the ones the current bom.json references describe the chart. | ||
| bom_json="$CHART_PATH/bom.json" | ||
| image_sboms=() | ||
| if [ -f "$bom_json" ]; then | ||
| # Outside the loop, so a bom.json jq cannot read fails the step. | ||
| sbom_paths="$(jq -r '[.containerImages // {} | .[] | .sboms.cyclonedx.path // empty] | unique | .[]' "$bom_json")" | ||
| while IFS= read -r path; do | ||
| [ -n "$path" ] || continue | ||
| if ! [[ "$path" =~ ^sboms/sha256-[0-9a-f]{64}\.cdx\.json$ ]]; then | ||
| echo "::error title=Invalid SBOM path::$bom_json references '$path', expected sboms/sha256-<digest>.cdx.json" | ||
| exit 1 | ||
| fi | ||
| if [ ! -f "$CHART_PATH/$path" ]; then | ||
| echo "::error title=Missing SBOM::$bom_json references $path, which does not exist" | ||
| exit 1 | ||
| fi | ||
| image_sboms+=("$CHART_PATH/$path") | ||
| done <<< "$sbom_paths" | ||
| fi | ||
| if [ "${#image_sboms[@]}" -eq 0 ]; then | ||
| echo "::notice::$CHART_PATH has no image SBOMs in bom.json, so it gets no chart SBOM." | ||
| echo "sbom-path=" >> "$GITHUB_OUTPUT" | ||
| exit 0 | ||
| fi | ||
|
|
||
| cyclonedx="$("${{ github.action_path }}/../setup-cyclonedx/install.sh" "$INSTALL_DIR")" | ||
| out="$OUTPUT_DIR/${CHART_NAME}-${CHART_VERSION}.cdx.json" | ||
| mkdir -p "$OUTPUT_DIR" | ||
| "$cyclonedx" merge --hierarchical --output-version "$SPEC_VERSION" \ | ||
| --group "$CHART_GROUP" --name "$CHART_NAME" --version "$CHART_VERSION" \ | ||
| --input-files "${image_sboms[@]}" \ | ||
| --output-file "$out" | ||
| echo "sbom-path=$out" >> "$GITHUB_OUTPUT" | ||
|
|
||
| - name: Set up Cosign | ||
| if: ${{ inputs.attest == 'true' && steps.merge.outputs.sbom-path != '' }} | ||
| uses: sigstore/cosign-installer@6f9f17788090df1f26f669e9d70d6ae9567deba6 # v4.1.2 | ||
| with: | ||
| # The default is a shared path under $HOME, which concurrent jobs on a | ||
| # persistent runner would download the same binary into at once. | ||
| install-dir: ${{ runner.temp }}/cosign | ||
|
|
||
| - name: Attest and verify the chart SBOM | ||
| if: ${{ inputs.attest == 'true' && steps.merge.outputs.sbom-path != '' }} | ||
| shell: bash | ||
| env: | ||
| CHART_REF: ${{ inputs.chart-ref }} | ||
| PREDICATE: ${{ steps.merge.outputs.sbom-path }} | ||
| IDENTITY_REGEXP: ${{ inputs.certificate-identity-regexp }} | ||
| # cosign has no env var for registry credentials. --k8s-keychain adds | ||
| # go-containerregistry's GitHub keychain, which reads GITHUB_TOKEN (and | ||
| # GITHUB_ACTOR) for ghcr.io, so the token is in neither argv nor a file. | ||
| GITHUB_TOKEN: ${{ inputs.registry-token }} | ||
| run: | | ||
| # An empty docker config of this step's own, so cosign cannot pick up | ||
| # a credential another step or job left in $HOME. Without any | ||
| # config.json, cosign's keychain falls back to $REGISTRY_AUTH_FILE and | ||
| # $XDG_RUNTIME_DIR/containers/auth.json, which may be shared. | ||
| DOCKER_CONFIG="$(mktemp -d "$RUNNER_TEMP/attest-chart-auth.XXXXXX")" | ||
| export DOCKER_CONFIG | ||
| echo '{}' > "$DOCKER_CONFIG/config.json" | ||
|
|
||
| # A repository name may contain '.', which must match only itself. | ||
| repository_pattern="${GITHUB_REPOSITORY//./\\.}" | ||
| identity="${IDENTITY_REGEXP:-^https://github\\.com/${repository_pattern}/}" | ||
|
|
||
| cosign attest --yes --k8s-keychain --type cyclonedx \ | ||
| --predicate "$PREDICATE" "$CHART_REF" | ||
| # Read it back, so an attestation that never landed fails the run | ||
| # instead of shipping a chart we only believe is attested. | ||
| cosign verify-attestation --k8s-keychain --type cyclonedx \ | ||
| --certificate-identity-regexp "$identity" \ | ||
| --certificate-oidc-issuer https://token.actions.githubusercontent.com \ | ||
| "$CHART_REF" > /dev/null | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.