#!/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" # 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 > "$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)"