| 1 | #!/usr/bin/env bash |
| 2 | # Write ~/.ssh/id_ed25519 for AUR pushes from CI or local scripts. |
| 3 | # |
| 4 | # Supported GitLab CI variables (pick one): |
| 5 | # AUR_SSH_PRIVATE_KEY_B64 — base64 of the private key (Variable type, one line; Masked OK) |
| 6 | # or File type whose contents are that base64 string |
| 7 | # AUR_SSH_PRIVATE_KEY — raw OpenSSH private key (File type preferred), or raw key text |
| 8 | # File contents may also be base64 (auto-detected) |
| 9 | # |
| 10 | # Always normalizes CRLF → LF (Windows pastes into GitLab often break OpenSSH otherwise). |
| 11 | set -euo pipefail |
| 12 | |
| 13 | KEY_PATH="${1:-$HOME/.ssh/id_ed25519}" |
| 14 | |
| 15 | install -d -m 700 "$(dirname "$KEY_PATH")" |
| 16 | |
| 17 | is_raw_ssh_key() { |
| 18 | grep -qE 'BEGIN (OPENSSH |RSA |EC )?PRIVATE KEY' "$1" 2>/dev/null |
| 19 | } |
| 20 | |
| 21 | normalize_key_file() { |
| 22 | # Strip CR; ensure exactly one trailing newline (OpenSSH is picky). |
| 23 | local tmp |
| 24 | tmp="$(mktemp)" |
| 25 | tr -d '\r' < "$KEY_PATH" > "$tmp" |
| 26 | # Ensure file ends with a newline |
| 27 | if [[ -s "$tmp" ]] && [[ "$(tail -c1 "$tmp" | wc -l)" -eq 0 ]]; then |
| 28 | printf '\n' >> "$tmp" |
| 29 | fi |
| 30 | mv "$tmp" "$KEY_PATH" |
| 31 | chmod 600 "$KEY_PATH" |
| 32 | } |
| 33 | |
| 34 | decode_b64_to_key() { |
| 35 | local src="$1" |
| 36 | if [[ -f "$src" ]]; then |
| 37 | # GitLab File variable: env value is a path to a temp file |
| 38 | tr -d '\r\n[:space:]' < "$src" | base64 -d > "$KEY_PATH" |
| 39 | else |
| 40 | # Variable type: env value is the base64 string itself |
| 41 | printf '%s' "$src" | tr -d '\r\n[:space:]' | base64 -d > "$KEY_PATH" |
| 42 | fi |
| 43 | } |
| 44 | |
| 45 | if [[ -n "${AUR_SSH_PRIVATE_KEY_B64:-}" ]]; then |
| 46 | decode_b64_to_key "$AUR_SSH_PRIVATE_KEY_B64" |
| 47 | elif [[ -n "${AUR_SSH_PRIVATE_KEY:-}" && -f "${AUR_SSH_PRIVATE_KEY}" ]]; then |
| 48 | if is_raw_ssh_key "$AUR_SSH_PRIVATE_KEY"; then |
| 49 | cp "$AUR_SSH_PRIVATE_KEY" "$KEY_PATH" |
| 50 | else |
| 51 | # File variable filled with base64 instead of raw PEM |
| 52 | decode_b64_to_key "$AUR_SSH_PRIVATE_KEY" |
| 53 | fi |
| 54 | elif [[ -n "${AUR_SSH_PRIVATE_KEY:-}" ]]; then |
| 55 | if printf '%s' "$AUR_SSH_PRIVATE_KEY" | grep -qE 'BEGIN (OPENSSH |RSA |EC )?PRIVATE KEY'; then |
| 56 | printf '%s\n' "$AUR_SSH_PRIVATE_KEY" > "$KEY_PATH" |
| 57 | else |
| 58 | decode_b64_to_key "$AUR_SSH_PRIVATE_KEY" |
| 59 | fi |
| 60 | elif [[ -f "$HOME/.ssh/id_ed25519" ]]; then |
| 61 | cp "$HOME/.ssh/id_ed25519" "$KEY_PATH" |
| 62 | else |
| 63 | echo "No AUR SSH key found. Set AUR_SSH_PRIVATE_KEY_B64 or AUR_SSH_PRIVATE_KEY." >&2 |
| 64 | exit 1 |
| 65 | fi |
| 66 | |
| 67 | normalize_key_file |
| 68 | |
| 69 | if ! is_raw_ssh_key "$KEY_PATH"; then |
| 70 | echo "AUR SSH key is not a valid OpenSSH private key after setup." >&2 |
| 71 | echo "Use either:" >&2 |
| 72 | echo " - AUR_SSH_PRIVATE_KEY as File with raw key (BEGIN ... PRIVATE KEY)" >&2 |
| 73 | echo " - AUR_SSH_PRIVATE_KEY_B64 as Variable with one-line base64 of that key" >&2 |
| 74 | echo "Tip: generate base64 from a LF-normalized key (see docs/AUR.md)." >&2 |
| 75 | exit 1 |
| 76 | fi |
| 77 | |
| 78 | # Cheap structural check without printing key material |
| 79 | if ! ssh-keygen -y -f "$KEY_PATH" >/dev/null 2>&1; then |
| 80 | echo "AUR SSH key failed ssh-keygen validation (often CRLF or truncated paste)." >&2 |
| 81 | exit 1 |
| 82 | fi |
| 83 | |
| 84 | ssh-keyscan aur.archlinux.org >> "$HOME/.ssh/known_hosts" 2>/dev/null || true |