Files
claudeandClaude Fable 5.2 ad8cf5756e
CI / shellcheck + selftests (pull_request) Successful in 1m44s
CI / move v1 to main (pull_request) Skipped
fix: tighten the crates lock check, drop the key from child environments
crates-lock-check.sh anchors on `[/:]daniel/gitdan-crates(.git)?/?` and
matches case-insensitively, so a trailing-slash or differently-cased second
commit is counted and a look-alike owner (notdaniel/) is not.
crates-access.sh unsets DEPLOY_KEY once the key file is written, so git, ssh
and timeout no longer inherit it. README says three actions, not two.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01MfBCVNDv3y5gm2djeZSAk4
2026-11-22 11:27:46 -06:00

75 lines
3.9 KiB
Bash
Executable File

#!/usr/bin/env bash
# Gives this job read access to daniel/gitdan-crates over SSH.
#
# DEPLOY_KEY=<private 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"
# The children below (ssh-keygen, git, ssh, timeout) must not inherit the key.
unset DEPLOY_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 </dev/null; then
die "$SECRET is not a usable unencrypted OpenSSH private key (pass the whole private key, including its BEGIN and END lines)"
fi
# Only this key and this known-hosts file: no agent, no user config, no global
# known hosts, and a host key that does not match is a hard failure.
ssh_cmd="ssh -F /dev/null -i '$key' -o IdentitiesOnly=yes -o IdentityAgent=none"
ssh_cmd+=" -o UserKnownHostsFile='$known_hosts' -o GlobalKnownHostsFile=/dev/null"
ssh_cmd+=" -o StrictHostKeyChecking=yes -o BatchMode=yes -o ConnectTimeout=20"
echo "GIT_SSH_COMMAND=$ssh_cmd" >> "$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)"