diff --git a/README.md b/README.md index 0e03d65..0a03245 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ Shared Gitea Actions composite actions for the gitdan forge. -Currently one thing, done properly: **`cargo-cache`** — a persistent, +Two actions. The first, done properly: **`cargo-cache`** — a persistent, per-branch Cargo build cache for self-hosted Gitea runners, where a pull request's cache is a near-free hardlink clone of an immutable snapshot its base branch published. @@ -522,6 +522,66 @@ of a merge-preview build, which is not what `dev` is. --- +## `gitdan-crates-access` + +Gives a job read access to the private `daniel/gitdan-crates` repo. The Actions +job token cannot read it, so the job authenticates with a read-only deploy key +held in the user-level Actions secret `GITDAN_CRATES_DEPLOY_KEY` (created and +rotated by gitdan#123). Call it once, before any cargo step: + +```yaml +- uses: https://gitdan.com/daniel/gitdan-actions/gitdan-crates-access@v1 + with: + deploy-key: ${{ secrets.GITDAN_CRATES_DEPLOY_KEY }} +``` + +Consumers declare the dependency as +`ssh://git@gitdan.com:22022/daniel/gitdan-crates.git` and set +`[net] git-fetch-with-cli = true`, so Cargo fetches through `git` and honours +`GIT_SSH_COMMAND`. The action: + +1. Writes the key to a `0600` file in a private directory under `$RUNNER_TEMP` + (else `mktemp -d`), never in the workspace, and copies in the pinned host + key for `[gitdan.com]:22022`. The key travels by environment variable only: + not in argv, not echoed. +2. Exports `GIT_SSH_COMMAND` through `$GITHUB_ENV`: that key only, that + known-hosts file only, `StrictHostKeyChecking=yes`, no agent, no user ssh + config. Every later `ssh://` git remote in the job uses it, so a job that + fetches other hosts over SSH must not use this action. No `CARGO_*` + variable is set; those split sccache keys. +3. Runs `git ls-remote` on gitdan-crates, so a missing or revoked key fails + here with an error naming `GITDAN_CRATES_DEPLOY_KEY`. +4. Fails when `Cargo.lock` lists more than one commit of gitdan-crates (two + commits mean two copies of every shared type). No entry passes. + +| input | default | meaning | +|---|---|---| +| `deploy-key` | *(required)* | the private key; an empty value fails, naming the secret (a fork PR gets no secrets, so it fails there too) | +| `lockfile` | `Cargo.lock` | lockfile to check, relative to the workspace; a missing file fails, an empty string skips the check | + +The image needs an `ssh` client and `git`; the action fails with a message if +either is missing and installs nothing. + +### When the host key changes + +The pin is `scripts/gitdan-known-hosts` (the server offers RSA only; SHA256 +`XAMzCdVbcQhcH/cnynQO7gPLFqHMh9wuoUysK9EhBP0`). After a forge rebuild or host +key rotation every job fails at the `ls-remote` with `Host key verification +failed`. Re-pin from a machine that already trusts the new key, never from a +bare `ssh-keyscan`: + +```bash +ssh-keygen -F '[gitdan.com]:22022' # the trusted record; compare it with +ssh-keyscan -p 22022 gitdan.com # what the server offers now +``` + +Replace the file with the confirmed line(s), update `PINNED_FPR` in +`scripts/crates-access-selftest.sh`, and merge; `v1` moves with `main`. If the +two disagree and you did not rotate the key, stop: that is the symptom of a +man in the middle. + +--- + ## Multiple jobs in one workflow Two jobs building the same ref — a `ci` job and a `wasm` job, say — are two diff --git a/gitdan-crates-access/action.yml b/gitdan-crates-access/action.yml new file mode 100644 index 0000000..61d8072 --- /dev/null +++ b/gitdan-crates-access/action.yml @@ -0,0 +1,43 @@ +name: 'gitdan-crates access' +description: >- + Gives the job read access to the private daniel/gitdan-crates repo over SSH + with a read-only deploy key: pins the forge's host key, exports + GIT_SSH_COMMAND so every later git (and Cargo, with + net.git-fetch-with-cli) authenticates, proves access with git ls-remote, and + checks that Cargo.lock holds one commit of the crates. +author: 'gitdan' + +inputs: + deploy-key: + description: >- + Private half of the read-only deploy key: pass + ${{ secrets.GITDAN_CRATES_DEPLOY_KEY }}. Never echoed, never in argv. + required: true + lockfile: + description: >- + Path to the Cargo.lock to check, relative to the workspace. Fails when it + lists more than one commit of the gitdan-crates source; no entry passes. + A missing file fails; set to an empty string to skip the check. + required: false + default: 'Cargo.lock' + +runs: + using: 'composite' + steps: + # The key reaches the script through the environment, never as an argument + # or interpolated into the script text, so it cannot show up in argv or in + # the step's echoed command. + - shell: bash + env: + DEPLOY_KEY: ${{ inputs.deploy-key }} + run: | + set -euo pipefail + bash "$(cd "${{ github.action_path }}/.." && pwd)/scripts/crates-access.sh" + + - if: ${{ inputs.lockfile != '' }} + shell: bash + env: + LOCKFILE: ${{ inputs.lockfile }} + run: | + set -euo pipefail + bash "$(cd "${{ github.action_path }}/.." && pwd)/scripts/crates-lock-check.sh" "$LOCKFILE" diff --git a/scripts/crates-access-selftest.sh b/scripts/crates-access-selftest.sh new file mode 100755 index 0000000..594f338 --- /dev/null +++ b/scripts/crates-access-selftest.sh @@ -0,0 +1,179 @@ +#!/usr/bin/env bash +# Regression test for the gitdan-crates-access action's two scripts. +# +# crates-lock-check.sh +# 1. ONE COMMIT PASSES — however many packages share it, and whatever the +# query string or URL spelling. +# 2. TWO COMMITS FAIL — and the message names both. +# 3. NONE PASSES — including a lockfile whose only git source is another +# repo, with commits of its own. +# 4. A MISSING LOCKFILE FAILS — a mistyped path must not read as "clean". +# +# crates-access.sh (git is stubbed: there is no key and no network here) +# 5. EMPTY KEY FAILS, naming the secret. +# 6. THE KEY STAYS PRIVATE — 0600 in a 0700 directory outside the +# workspace, absent from argv, output and $GITHUB_ENV, and a key with no +# trailing newline (or CRLF line ends) still loads. +# 7. GIT_SSH_COMMAND USES ONLY THAT KEY AND THE PINNED HOST KEY, strictly, +# and no CARGO_* variable is exported (those split sccache keys). +# 8. A FAILED ls-remote FAILS THE STEP, naming the secret. +# 9. NO ssh CLIENT FAILS CLEARLY; A SCRATCH DIR IN THE WORKSPACE IS REFUSED. +# 10. THE PINNED HOST KEY is the one recorded in README.md (SHA256 below). +# +# What this cannot prove: that the live forge accepts the key, and that its +# host key is still the pinned one. Both are first-consumer-run facts. +set -euo pipefail +script_dir=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) +lock_check="$script_dir/crates-lock-check.sh" +access="$script_dir/crates-access.sh" + +PINNED_FPR='SHA256:XAMzCdVbcQhcH/cnynQO7gPLFqHMh9wuoUysK9EhBP0' + +scratch=$(mktemp -d) +trap 'rm -rf "$scratch"' EXIT +pass_count=0 +fail() { echo "ASSERTION FAILED: $*" >&2; exit 1; } +ok() { pass_count=$((pass_count + 1)); echo "PASS: $*"; } + +SRC='git+ssh://git@gitdan.com:22022/daniel/gitdan-crates.git' +sha_a=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +sha_b=bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +entry() { printf '[[package]]\nname = "%s"\nversion = "0.1.0"\nsource = "%s"\n\n' "$1" "$2"; } +check() { bash "$lock_check" "$@"; } + +echo "=== 1-4. crates-lock-check.sh ===" +{ entry core "$SRC?branch=main#$sha_a"; entry shared "$SRC?branch=main#$sha_a" + entry serde "registry+https://github.com/rust-lang/crates.io-index"; } > "$scratch/one.lock" +check "$scratch/one.lock" >/dev/null || fail "one commit shared by two packages was refused" +ok "one commit passes, however many packages share it" + +{ entry core "$SRC?branch=main#$sha_a"; entry shared "$SRC?rev=${sha_a}#$sha_a" + entry odd "git+https://gitdan.com/daniel/gitdan-crates#$sha_a"; } > "$scratch/spellings.lock" +check "$scratch/spellings.lock" >/dev/null || fail "one commit under three spellings was refused" +ok "query string and URL spelling do not make a second commit" + +{ entry core "$SRC?branch=main#$sha_a"; entry shared "$SRC?branch=main#$sha_b"; } > "$scratch/two.lock" +if out=$(check "$scratch/two.lock" 2>&1); then fail "two commits passed"; fi +[[ "$out" == *"$sha_a"* && "$out" == *"$sha_b"* ]] || fail "failure did not name both commits: $out" +ok "two commits fail, naming both" + +{ entry core "$SRC?branch=main#$sha_a"; entry shared "git+https://gitdan.com/daniel/gitdan-crates.git#$sha_b"; } > "$scratch/two2.lock" +check "$scratch/two2.lock" >/dev/null 2>&1 && fail "two commits under different spellings passed" +ok "two commits fail across URL spellings too" + +{ entry other "git+ssh://git@gitdan.com:22022/daniel/gitdan-crates-extra.git#$sha_a" + entry a "git+https://example.com/x/y#$sha_a"; entry b "git+https://example.com/x/y#$sha_b" + entry serde "registry+https://github.com/rust-lang/crates.io-index"; } > "$scratch/none.lock" +check "$scratch/none.lock" >/dev/null || fail "a lockfile with no gitdan-crates entry was refused" +printf '' > "$scratch/empty.lock" +check "$scratch/empty.lock" >/dev/null || fail "an empty lockfile was refused" +ok "none passes, and other git sources (or look-alike repos) are not counted" + +if out=$(check "$scratch/absent.lock" 2>&1); then fail "a missing lockfile passed"; fi +grep -q "lockfile" <<<"$out" || fail "missing-lockfile message does not mention the input: $out" +ok "a missing lockfile fails" + +echo "=== 5-9. crates-access.sh ===" +stub="$scratch/stub"; mkdir -p "$stub" +cat > "$stub/git" <<'STUB' +#!/usr/bin/env bash +printf '%s\n' "$@" > "$STUB_DIR/git.argv" +printf '%s\n' "${GIT_SSH_COMMAND:-}" > "$STUB_DIR/git.ssh" +[ -z "${STUB_GIT_ERR:-}" ] || echo "$STUB_GIT_ERR" >&2 +exit "${STUB_GIT_RC:-0}" +STUB +chmod +x "$stub/git" + +# A real key, so the parse check inside the script is exercised when +# ssh-keygen exists; otherwise a marker the leak assertions can still grep for. +have_keygen=0; command -v ssh-keygen >/dev/null && have_keygen=1 +if [ "$have_keygen" = 1 ]; then + ssh-keygen -q -t ed25519 -N '' -C selftest -f "$scratch/id" [VAR=value ...]; leaves $scratch/.{out,env} + local name="$1"; shift + : > "$scratch/$name.env" + env PATH="$stub:$PATH" STUB_DIR="$scratch" GITHUB_ENV="$scratch/$name.env" \ + RUNNER_TEMP="$scratch/runner-temp" GITHUB_WORKSPACE="$scratch/workspace" \ + "$@" bash "$access" > "$scratch/$name.out" 2>&1 +} +mkdir -p "$scratch/runner-temp" "$scratch/workspace" + +if run_access empty DEPLOY_KEY=; then fail "an empty key passed"; fi +grep -q GITDAN_CRATES_DEPLOY_KEY "$scratch/empty.out" || fail "empty-key message does not name the secret" +ok "an empty deploy-key fails, naming GITDAN_CRATES_DEPLOY_KEY" + +run_access good DEPLOY_KEY="$KEY" || { cat "$scratch/good.out"; fail "a good key was refused"; } +cp "$scratch/git.argv" "$scratch/good.argv"; cp "$scratch/git.ssh" "$scratch/good.ssh" +cmd=$(sed -n 's/^GIT_SSH_COMMAND=//p' "$scratch/good.env") +keyfile=$(sed -n "s/.* -i '\\([^']*\\)'.*/\\1/p" <<<"$cmd") +[ -f "$keyfile" ] || fail "key file not found from GIT_SSH_COMMAND: $cmd" +[ "$(stat -c %a "$keyfile")" = 600 ] || fail "key file mode is $(stat -c %a "$keyfile"), not 600" +[ "$(stat -c %a "$(dirname "$keyfile")")" = 700 ] || fail "key directory is not 0700" +case "$(dirname "$keyfile")/" in "$scratch/workspace"/*) fail "key landed in the workspace" ;; esac +grep -rqF "$secret_line" "$scratch/workspace" && fail "key material found in the workspace" +for f in good.out good.env good.argv good.ssh; do + grep -qF "$secret_line" "$scratch/$f" && fail "key material leaked into $f" +done +ok "key is 0600 in a 0700 directory outside the workspace and in no output, env file or argv" + +[ "$(tail -c1 "$keyfile" | od -An -c | tr -d ' ')" = '\n' ] || fail "key file does not end in a newline" +if [ "$have_keygen" = 1 ]; then + ssh-keygen -y -P '' -f "$keyfile" >/dev/null 2>&1 /dev/null 2>&1 "$scratch/nossh.env" +if env PATH="$nossh" STUB_DIR="$scratch" GITHUB_ENV="$scratch/nossh.env" DEPLOY_KEY="$KEY" \ + "$nossh/bash" "$access" > "$scratch/nossh.out" 2>&1; then fail "no ssh client passed"; fi +grep -q 'no ssh client' "$scratch/nossh.out" || fail "missing-ssh message unclear: $(cat "$scratch/nossh.out")" +ok "a missing ssh client fails with a clear message" + +mkdir -p "$scratch/workspace/tmp" +if run_access inws DEPLOY_KEY="$KEY" RUNNER_TEMP="$scratch/workspace/tmp"; then fail "scratch dir inside the workspace passed"; fi +grep -q 'inside the workspace' "$scratch/inws.out" || fail "workspace refusal unclear" +ok "a scratch directory inside the workspace is refused" + +echo "=== 10. pinned host key ===" +[ "$(wc -l < "$script_dir/gitdan-known-hosts")" -ge 1 ] || fail "known-hosts file is empty" +grep -q '^\[gitdan.com\]:22022 ' "$script_dir/gitdan-known-hosts" || fail "pinned entry is not for [gitdan.com]:22022" +if [ "$have_keygen" = 1 ]; then + fprs=$(ssh-keygen -lf "$script_dir/gitdan-known-hosts" | awk '{print $2}') + grep -qx "$PINNED_FPR" <<<"$fprs" || fail "pinned key fingerprint is not $PINNED_FPR (got: $fprs)" +fi +ok "pinned host key is for [gitdan.com]:22022 and has the recorded fingerprint" + +echo "crates-access-selftest: $pass_count checks passed" diff --git a/scripts/crates-access.sh b/scripts/crates-access.sh new file mode 100755 index 0000000..1fe09a8 --- /dev/null +++ b/scripts/crates-access.sh @@ -0,0 +1,72 @@ +#!/usr/bin/env bash +# Gives this job read access to daniel/gitdan-crates over SSH. +# +# DEPLOY_KEY= crates-access.sh +# +# Writes the key (0600) and the pinned host key into a private directory +# outside the workspace, exports GIT_SSH_COMMAND through $GITHUB_ENV, and +# proves access with `git ls-remote`. +# +# The key arrives in the environment only: it is never an argument, never +# echoed, and the one place it is written is the 0600 file. Cargo reaches the +# repo through the git CLI (net.git-fetch-with-cli), so GIT_SSH_COMMAND covers +# it; a CARGO_* variable would split sccache keys, which is why none is used. +# +# Test seams: GITDAN_CRATES_URL, and the PATH the script resolves git on. +set -euo pipefail + +SECRET=GITDAN_CRATES_DEPLOY_KEY +URL="${GITDAN_CRATES_URL:-ssh://git@gitdan.com:22022/daniel/gitdan-crates.git}" +script_dir=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) +known_hosts_src="$script_dir/gitdan-known-hosts" + +die() { echo "::error::gitdan-crates-access: $*" >&2; exit 1; } + +[ -n "${DEPLOY_KEY:-}" ] || die "deploy-key is empty. Pass \${{ secrets.${SECRET} }}; the secret is missing, or this run cannot read user-level secrets (fork PRs get none)" +[ -n "${GITHUB_ENV:-}" ] || die "GITHUB_ENV is not set; this runs inside a job" +command -v ssh >/dev/null || die "no ssh client in this image; install openssh-client before this step" +command -v git >/dev/null || die "no git in this image" +[ -f "$known_hosts_src" ] || die "pinned host key file missing: $known_hosts_src" + +# mktemp -d makes the directory 0700; the key lives only inside it. +dir=$(mktemp -d "${RUNNER_TEMP:-${TMPDIR:-/tmp}}/gitdan-crates-access.XXXXXX") +dir=$(cd "$dir" && pwd -P) +case "$dir" in + *[[:space:]\'\"\\]*) die "scratch directory '$dir' has a space or quote; GIT_SSH_COMMAND cannot carry it" ;; +esac +if [ -n "${GITHUB_WORKSPACE:-}" ]; then + ws=$(cd "$GITHUB_WORKSPACE" && pwd -P) + case "$dir/" in "$ws"/*) die "scratch directory '$dir' is inside the workspace '$ws'; set RUNNER_TEMP outside it" ;; esac +fi + +key="$dir/deploy-key" +known_hosts="$dir/known_hosts" + +# Strip CR (a key pasted from Windows) and end with exactly one newline: an +# OpenSSH private key without its final newline fails to load. printf is a +# builtin, so the key is not in any process's argv. +( umask 077; printf '%s\n' "${DEPLOY_KEY%$'\n'}" | tr -d '\r' > "$key" ) +chmod 600 "$key" +cp "$known_hosts_src" "$known_hosts" +chmod 600 "$known_hosts" + +# Parse-check before using it, for a message that names the secret rather than +# a bare "Permission denied". -P '' makes a passphrase-protected key fail fast. +if command -v ssh-keygen >/dev/null && ! ssh-keygen -y -P '' -f "$key" >/dev/null 2>&1 > "$GITHUB_ENV" + +# Prove access now, so a missing or revoked key fails here with its name on it +# and not inside a cargo fetch several steps later. +if ! err=$(GIT_SSH_COMMAND="$ssh_cmd" GIT_TERMINAL_PROMPT=0 timeout 90 git ls-remote --exit-code "$URL" refs/heads/main 2>&1 >/dev/null); then + echo "$err" >&2 + die "cannot read gitdan-crates with $SECRET. If the output above says 'Permission denied', the secret is wrong or the deploy key was revoked (gitdan#123 rotates it); if it says 'Host key verification failed', the forge's SSH host key changed and scripts/gitdan-known-hosts needs re-pinning (README, gitdan-crates-access)" +fi +echo "gitdan-crates access: ok ($URL readable)" diff --git a/scripts/crates-lock-check.sh b/scripts/crates-lock-check.sh new file mode 100755 index 0000000..e430b1e --- /dev/null +++ b/scripts/crates-lock-check.sh @@ -0,0 +1,29 @@ +#!/usr/bin/env bash +# Fails when a Cargo.lock lists more than one commit of the gitdan-crates +# git source: two commits mean two copies of every shared type. +# +# crates-lock-check.sh +# +# Several packages from the one source share a commit, so what counts is +# distinct commits, not entries. The query string (`?branch=main`, `?rev=...`) +# is ignored, and so is the URL's spelling (scheme, port, `.git`): two copies +# of the repo are two copies whichever way they were named. No entry passes. +set -euo pipefail + +[ $# -eq 1 ] || { echo "::error::crates-lock-check: expected " >&2; exit 1; } +lock="$1" +[ -f "$lock" ] || { + echo "::error::gitdan-crates-access: lockfile '$lock' not found; set the action's 'lockfile' input to its path, or to an empty string to skip the check" >&2 + exit 1 +} + +# source = "git+[?query]#" +shas=$(sed -nE 's#^source = "git\+[^"?\#]*daniel/gitdan-crates(\.git)?(\?[^"\#]*)?\#([^"]*)"$#\3#p' "$lock" | sort -u) +count=$(grep -c . <<<"$shas" || true) + +if [ "$count" -gt 1 ]; then + echo "::error::$lock locks $count different commits of gitdan-crates; every consumer must resolve one, or shared types are duplicated. Run: cargo update -p --precise , or cargo update, to unify them:" >&2 + while read -r s; do echo " $s" >&2; done <<<"$shas" + exit 1 +fi +echo "gitdan-crates lock check: ${count} commit(s) of gitdan-crates in $lock" diff --git a/scripts/gitdan-known-hosts b/scripts/gitdan-known-hosts new file mode 100644 index 0000000..346aec5 --- /dev/null +++ b/scripts/gitdan-known-hosts @@ -0,0 +1 @@ +[gitdan.com]:22022 ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQDH9sQaH4m5PQ+oNVrRUrsT0muOm0nb3wHSpg3Ps72apDYyJqF6ljdxq1EbUQPfzk3GHSqGKPCNrNfWkOSTphmXyq7+IqlknAPU7cuPxAUi7u0QY6nUM19GrgE79k7rhCHHdPIHpAropgrg9ezmhjV/57rUsTLuxv/thLaxza0zjbYIIyKf6FDMI+PZiQ/vJAAQOCIPvK0XJ2gmNuG3w+pbFsi6AlrcfT72Vpde0CppYBH1fyGGNrnH7muBD1t1BkX3gtfK9+gPAr4IC1Liyi/rT1CLiJNTRW1C9R/aYXFwfi9djd7Fe1o5ez6bQ5f/avyQmXNrtLcYR2Wz2jhA3P2aRtSHO+UgHH5hSLetVVzSRToiVvhAqdT/AWUJGOkQoDpd9MMyyAxgIiht5mL2+Tv3jtgkv+9KwV8DjZwBQIL0bHhUnL7HJTNwWZxqp/rgXC7rnQ1dCuq47W+LH1fTDkYbsiB3NZhLvJ1tBgzBdiKtZ+FYiIXCAbpetwTi+hnyBuanQ3DIkGPRCGGU3ZIvRs4Z8Qf+rQY6POjAFl8zUDCdxbNUE4M0phHHErUDzcgd/AifAC17Nr4jwdU0u2p2NUHrvqrHLRWvw0o+Oo+tOdWRkBbe//KIHf4UQ4vLXdpjv9K2thKgUEIMdILAyBtavDD2j3MDAXYUDQx5KmntRe29JQ== diff --git a/scripts/selftest.sh b/scripts/selftest.sh index a215799..4aeb07d 100755 --- a/scripts/selftest.sh +++ b/scripts/selftest.sh @@ -13,7 +13,7 @@ script_dir=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) FAST=0 [ "${1:-}" = "--fast" ] && FAST=1 -FIXTURE_TESTS=(cache-root-selftest.sh seed-target-dir-selftest.sh publish-snapshot-selftest.sh prune-cache-selftest.sh release-v1-selftest.sh) +FIXTURE_TESTS=(cache-root-selftest.sh seed-target-dir-selftest.sh publish-snapshot-selftest.sh prune-cache-selftest.sh release-v1-selftest.sh crates-access-selftest.sh) CARGO_TESTS=(hardlink-clone-selftest.sh restore-mtimes-selftest.sh) TESTS=("${FIXTURE_TESTS[@]}")