-
Notifications
You must be signed in to change notification settings - Fork 0
feat(release): generate SHA-256 checksums for release artifacts (epic 005 T011) #54
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
Merged
+243
−11
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| #!/usr/bin/env bash | ||
| # Generates a <file>.sha256 checksum file next to each given release | ||
| # artifact (FR-012). Uses sha256sum if present, falling back to | ||
| # `shasum -a 256` (macOS has no sha256sum by default) -- no additional | ||
| # dependency beyond what the runner/OS already provides, per plan.md's | ||
| # Key Design Decision #6. | ||
| # | ||
| # The checksum file references its artifact by basename, not the full | ||
| # invocation path: release assets are downloaded flat, side-by-side, so a | ||
| # checksum file baked with the build machine's absolute path would fail | ||
| # `sha256sum -c`/`shasum -a 256 -c` for every downloader. | ||
| set -euo pipefail | ||
|
|
||
| usage() { | ||
| echo "usage: $0 <file> [file...]" >&2 | ||
| exit 2 | ||
| } | ||
|
|
||
| [ $# -ge 1 ] || usage | ||
|
|
||
| # Detect the available hash tool once, not per-file, and fail fast with a | ||
| # clear message if neither exists (rather than each file silently hitting | ||
| # a "command not found" only when it's its turn to be hashed). | ||
| if command -v sha256sum >/dev/null 2>&1; then | ||
| sha_bin=(sha256sum) | ||
| elif command -v shasum >/dev/null 2>&1; then | ||
| sha_bin=(shasum -a 256) | ||
| else | ||
| echo "error: neither sha256sum nor shasum found" >&2 | ||
| exit 1 | ||
| fi | ||
|
|
||
| for file in "$@"; do | ||
| if [ ! -f "$file" ]; then | ||
| echo "error: $file not found" >&2 | ||
| exit 1 | ||
| fi | ||
| dir="$(dirname "$file")" | ||
| base="$(basename "$file")" | ||
| # `--`: without it, a basename starting with `-` (e.g. `-artifact`) would | ||
| # be parsed as an option by sha256sum/shasum instead of a filename. | ||
| (cd "$dir" && "${sha_bin[@]}" -- "$base") > "${file}.sha256" | ||
| echo "wrote ${file}.sha256" | ||
| done |
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,144 @@ | ||
| #!/usr/bin/env bash | ||
| # Fixture-based tests for generate-checksums.sh (T011, Constitution I). Run | ||
| # manually: | ||
| # bash .github/scripts/tests/test-generate-checksums.sh | ||
| set -euo pipefail | ||
|
|
||
| script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" | ||
| script="$script_dir/../generate-checksums.sh" | ||
|
|
||
| pass=0 | ||
| fail=0 | ||
|
|
||
| check() { | ||
| local desc="$1" ok="$2" | ||
| if [ "$ok" = "0" ]; then | ||
| pass=$((pass + 1)) | ||
| else | ||
| fail=$((fail + 1)) | ||
| echo "FAIL: $desc" | ||
| fi | ||
| } | ||
|
|
||
| verify_checksum() { | ||
| # Portable sha256sum -c across macOS (shasum) and Linux (sha256sum). | ||
| # `--`: a dash-prefixed checksum filename (e.g. "-dash-artifact.sha256") | ||
| # would otherwise be parsed as an option, same footgun the script under | ||
| # test guards against for the artifact filename itself. | ||
| local checksum_file="$1" | ||
| if command -v sha256sum >/dev/null 2>&1; then | ||
| (cd "$(dirname "$checksum_file")" && sha256sum -c -- "$(basename "$checksum_file")") >/dev/null 2>&1 | ||
| else | ||
| (cd "$(dirname "$checksum_file")" && shasum -a 256 -c -- "$(basename "$checksum_file")") >/dev/null 2>&1 | ||
| fi | ||
| } | ||
|
|
||
| checksum_records_basename() { | ||
| # `sha256sum -c` only verifies that the hash matches SOME file with the | ||
| # recorded name -- it doesn't confirm that name is the one we expect. A | ||
| # bug that wrote e.g. second-artifact.sha256 with third-artifact's | ||
| # record (whose content still happens to exist and match) would pass | ||
| # verify_checksum silently. Cross-check the filename field itself. | ||
| local checksum_file="$1" expected_basename="$2" recorded | ||
| recorded="$(awk '{print $NF}' "$checksum_file")" | ||
| [ "$recorded" = "$expected_basename" ] | ||
| } | ||
|
|
||
| check_file_exists() { | ||
| local desc="$1" path="$2" | ||
| if [ -f "$path" ]; then | ||
| check "$desc" 0 | ||
| else | ||
| check "$desc" 1 | ||
| fi | ||
| } | ||
|
|
||
| tmp="$(mktemp -d)" | ||
| trap 'rm -rf "$tmp"' EXIT | ||
|
|
||
| # --- happy path: checksum file is created and verifies against its artifact --- | ||
| printf 'iklo release artifact fixture\n' > "$tmp/iklo-v0.1.0-x86_64-unknown-linux-gnu" | ||
| "$script" "$tmp/iklo-v0.1.0-x86_64-unknown-linux-gnu" | ||
| check_file_exists "checksum file created" "$tmp/iklo-v0.1.0-x86_64-unknown-linux-gnu.sha256" | ||
| if verify_checksum "$tmp/iklo-v0.1.0-x86_64-unknown-linux-gnu.sha256"; then | ||
| check "checksum verifies against its real artifact (sha256sum -c)" 0 | ||
| else | ||
| check "checksum verifies against its real artifact (sha256sum -c)" 1 | ||
| fi | ||
|
|
||
| # --- checksum file references the artifact by basename, not an absolute path --- | ||
| # (release assets are downloaded flat, side-by-side -- a checksum file | ||
| # containing the build machine's absolute path would fail `sha256sum -c` | ||
| # for every downloader.) | ||
| check "checksum file references artifact by basename, not absolute path" \ | ||
| "$(grep -q "$tmp" "$tmp/iklo-v0.1.0-x86_64-unknown-linux-gnu.sha256" && echo 1 || echo 0)" | ||
|
|
||
| # --- negative: tampering the artifact after the fact must fail verification --- | ||
| printf 'tampered content\n' > "$tmp/iklo-v0.1.0-x86_64-unknown-linux-gnu" | ||
| if verify_checksum "$tmp/iklo-v0.1.0-x86_64-unknown-linux-gnu.sha256"; then | ||
| fail=$((fail + 1)) | ||
| echo "FAIL: tampered artifact should NOT verify, but it did" | ||
| else | ||
| pass=$((pass + 1)) | ||
| fi | ||
|
|
||
| # --- multiple artifacts in ONE invocation (exercises the for-loop, not | ||
| # just two separate single-arg calls) -- content-verified, not just | ||
| # existence, so a regression that attributes the wrong checksum to the | ||
| # wrong file (or writes an invalid one) would be caught. --- | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| printf 'second artifact\n' > "$tmp/second-artifact" | ||
| printf 'third artifact\n' > "$tmp/third-artifact" | ||
| "$script" "$tmp/second-artifact" "$tmp/third-artifact" | ||
| check_file_exists "second checksum file created (multi-arg call)" "$tmp/second-artifact.sha256" | ||
| check_file_exists "third checksum file created (multi-arg call)" "$tmp/third-artifact.sha256" | ||
| if verify_checksum "$tmp/second-artifact.sha256"; then | ||
| check "second artifact's checksum verifies (multi-arg call)" 0 | ||
| else | ||
| check "second artifact's checksum verifies (multi-arg call)" 1 | ||
| fi | ||
| if verify_checksum "$tmp/third-artifact.sha256"; then | ||
| check "third artifact's checksum verifies (multi-arg call)" 0 | ||
| else | ||
| check "third artifact's checksum verifies (multi-arg call)" 1 | ||
| fi | ||
| if checksum_records_basename "$tmp/second-artifact.sha256" "second-artifact"; then | ||
| check "second artifact's checksum records the correct basename (not third's)" 0 | ||
| else | ||
| check "second artifact's checksum records the correct basename (not third's)" 1 | ||
| fi | ||
| if checksum_records_basename "$tmp/third-artifact.sha256" "third-artifact"; then | ||
| check "third artifact's checksum records the correct basename (not second's)" 0 | ||
| else | ||
| check "third artifact's checksum records the correct basename (not second's)" 1 | ||
| fi | ||
|
|
||
| # --- dash-prefixed basename: regression test for the `--` fix (without | ||
| # it, sha256sum/shasum would parse "-artifact" as an option and fail) --- | ||
| printf 'dash-prefixed artifact\n' > "$tmp/-dash-artifact" | ||
| "$script" "$tmp/-dash-artifact" | ||
| check_file_exists "dash-prefixed-basename checksum file created" "$tmp/-dash-artifact.sha256" | ||
| if verify_checksum "$tmp/-dash-artifact.sha256"; then | ||
| check "dash-prefixed-basename checksum verifies" 0 | ||
| else | ||
| check "dash-prefixed-basename checksum verifies" 1 | ||
| fi | ||
|
|
||
| # --- missing argument fails --- | ||
| if "$script" >/dev/null 2>&1; then | ||
| fail=$((fail + 1)) | ||
| echo "FAIL (expected failure but succeeded): no arguments" | ||
| else | ||
| pass=$((pass + 1)) | ||
| fi | ||
|
|
||
| # --- nonexistent file fails --- | ||
| if "$script" "$tmp/does-not-exist" >/dev/null 2>&1; then | ||
| fail=$((fail + 1)) | ||
| echo "FAIL (expected failure but succeeded): nonexistent file" | ||
| else | ||
| pass=$((pass + 1)) | ||
| fi | ||
|
|
||
| echo "----" | ||
| echo "pass=$pass fail=$fail" | ||
| [ "$fail" -eq 0 ] | ||
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
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.