diff --git a/.gitea/actions/checks/action.yml b/.gitea/actions/checks/action.yml index f22b828..a01e6c5 100644 --- a/.gitea/actions/checks/action.yml +++ b/.gitea/actions/checks/action.yml @@ -1,9 +1,116 @@ name: Linux checks description: Shared Linux validation for Gitea CI and release packaging. +inputs: + protocol-deploy-key: + description: > + Private half of the read-only deploy key registered on the private + Voelin/VoelinProtocol repository (the repository secret + VOELIN_PROTOCOL_DEPLOY_KEY, passed in by the workflow: secrets are not + visible inside a composite action). The action fails when it is empty. + required: true runs: using: composite steps: + # The workspace depends on the private VoelinProtocol repository over SSH + # (docs/ci.md, "VoelinProtocol access"). First, so that a missing key fails + # in seconds and before anything runs cargo. + - name: Install the VoelinProtocol deploy key + # The key reaches this step through the environment only: never in the + # script text, never echoed, no `set -x`. + env: + PROTOCOL_DEPLOY_KEY: ${{ inputs.protocol-deploy-key }} + run: | + set -euo pipefail + umask 077 + if [[ -z "${PROTOCOL_DEPLOY_KEY//[[:space:]]/}" ]]; then + echo "::error::The VoelinProtocol deploy key is empty. Set the repository secret VOELIN_PROTOCOL_DEPLOY_KEY to the private key of the read-only deploy key on Voelin/VoelinProtocol (docs/ci.md, VoelinProtocol access). Pull requests from forks get no secrets." >&2 + exit 1 + fi + command -v ssh ssh-keygen > /dev/null || { + echo "::error::The job image needs the OpenSSH client (ssh, ssh-keygen)." >&2 + exit 1 + } + ssh_dir="$HOME/.ssh" + key_file="$ssh_dir/voelin_protocol" + mkdir -p "$ssh_dir" + chmod 700 "$ssh_dir" + + # Whatever the secret store did to the line ends and the final newline, + # OpenSSH needs LF lines, the BEGIN line first and a newline at the end. + printf '%s\n' "$PROTOCOL_DEPLOY_KEY" | tr -d '\r' | sed '/^[[:space:]]*$/d' > "$key_file" + unset PROTOCOL_DEPLOY_KEY + chmod 600 "$key_file" + if ! public=$(ssh-keygen -y -f "$key_file" < /dev/null); then + rm -f "$key_file" + echo "::error::VOELIN_PROTOCOL_DEPLOY_KEY is not an unencrypted OpenSSH or PEM private key." >&2 + exit 1 + fi + # The fingerprint of the public half, to compare with the deploy key in + # the VoelinProtocol settings. + printf 'Deploy key installed: %s\n' "$(ssh-keygen -lf - <<< "$public")" + + # The reviewed host key from the repository, never one fetched now. + known_hosts="$ssh_dir/known_hosts" + touch "$known_hosts" + chmod 600 "$known_hosts" + [[ -s "${GITHUB_WORKSPACE:-.}/.gitea/known_hosts" ]] || { + echo "::error::.gitea/known_hosts is missing from the checkout." >&2 + exit 1 + } + while IFS= read -r line; do + case "$line" in '' | '#'*) continue ;; esac + grep -qxF -- "$line" "$known_hosts" || printf '%s\n' "$line" >> "$known_hosts" + done < "${GITHUB_WORKSPACE:-.}/.gitea/known_hosts" + + # Use this key, and only this one, for the Gitea server; fail rather than + # ask when its host key is unknown or changed. The block goes first: ssh + # takes the first value it finds for each option. + config="$ssh_dir/config" + marker="# VoelinProtocol deploy key (.gitea/actions/checks)" + touch "$config" + if ! grep -qxF -- "$marker" "$config"; then + { + cat << EOF + $marker + Host git.faumaray.com + User gitea + IdentityFile $key_file + IdentitiesOnly yes + UserKnownHostsFile $known_hosts + StrictHostKeyChecking yes + BatchMode yes + ConnectTimeout 30 + + EOF + cat "$config" + } > "$config.new" + mv "$config.new" "$config" + fi + chmod 600 "$config" + + # Cargo's built-in git client (libgit2) cannot use IdentityFile; the + # system git runs ssh, which reads the config above. `-F` names it + # outright: ssh looks for ~/.ssh in the passwd entry's home directory, not + # in $HOME, and a job image may set the two apart. + { + echo "CARGO_NET_GIT_FETCH_WITH_CLI=true" + echo "GIT_SSH_COMMAND=ssh -F '$config'" + } >> "$GITHUB_ENV" + shell: bash - uses: ./.github/actions/system-deps + - name: Check read access to VoelinProtocol + # The first thing cargo does, with the same git and ssh configuration, so + # that a wrong key or a changed host key fails here with a clear message + # and not minutes later inside a cargo command. + run: | + set -euo pipefail + if ! GIT_TERMINAL_PROMPT=0 timeout 120 git ls-remote --exit-code \ + ssh://gitea@git.faumaray.com/Voelin/VoelinProtocol.git HEAD > /dev/null; then + echo "::error::git cannot read Voelin/VoelinProtocol over SSH with the deploy key. 'Permission denied (publickey)' above: the key is not registered on that repository (or is not the one in VOELIN_PROTOCOL_DEPLOY_KEY). 'Host key verification failed': .gitea/known_hosts no longer matches the server (docs/ci.md, VoelinProtocol access)." >&2 + exit 1 + fi + echo "Voelin/VoelinProtocol is readable with the deploy key." + shell: bash - name: Install pinned Rust toolchain run: | set -euo pipefail diff --git a/.gitea/workflows/ci.yml b/.gitea/workflows/ci.yml index 46b1534..74c4ab6 100644 --- a/.gitea/workflows/ci.yml +++ b/.gitea/workflows/ci.yml @@ -32,3 +32,8 @@ jobs: steps: - uses: https://github.com/actions/checkout@v4 - uses: ./.gitea/actions/checks + with: + # A repository secret is not visible inside the composite action, so + # it is passed in. Pull requests from forks get no secrets: the + # action then fails at once with a message that says so. + protocol-deploy-key: ${{ secrets.VOELIN_PROTOCOL_DEPLOY_KEY }}