diff --git a/bootstrap.sh b/bootstrap.sh index 42c5f63..897aa42 100755 --- a/bootstrap.sh +++ b/bootstrap.sh @@ -57,7 +57,7 @@ readonly RAW_BASE="https://gitea.nevim42.cz/nevim42/engram-boot/raw/branch/main" readonly ENGRAM_REPO="git@gitea.nevim42.cz:nevim42/engram.git" readonly ENGRAM_DIR="$HOME/Repositories/engram" readonly KNOWN_HOST="gitea.nevim42.cz" -readonly DEPS=(age git gnupg) +readonly DEPS=(age git gnupg openssh) WORK="" CHOSEN_KEYS=() @@ -135,31 +135,49 @@ install_keys() { local available=() mapfile -t available < <(find "$WORK/plain/ssh" -maxdepth 1 -type f \ - ! -name '*.pub' ! -name 'known_hosts*' -printf '%f\n' | sort) + ! -name '.*' ! -name '*.pub' ! -name 'known_hosts*' -printf '%f\n' | sort) if (( ${#available[@]} == 0 )); then die "No SSH keys found in the bundle." fi - info "Available keys: ${available[*]}" - local answer - read -rp " Install which keys? (space separated, empty = none): " answer - read -ra CHOSEN_KEYS <<< "$answer" + info "Available keys:" + local i + for i in "${!available[@]}"; do + printf ' %d) %s\n' "$((i + 1))" "${available[$i]}" + done - local name - for name in "${CHOSEN_KEYS[@]}"; do - if [[ ! -f "$WORK/plain/ssh/$name" ]]; then - die "Unknown key: $name" + local answer + read -rp " Which keys? (numbers, first one becomes id_ed25519, empty = none): " answer + + local num n name target + n=0 + for num in $answer; do + if [[ ! $num =~ ^[0-9]+$ ]] || (( num < 1 || num > ${#available[@]} )); then + die "Not a valid key number: $num" fi - if [[ -e "$HOME/.ssh/$name" ]]; then - warn "$name already exists in ~/.ssh - skipping" + + name="${available[$((num - 1))]}" + n=$((n + 1)) + + # The first chosen key becomes the standard default identity, + # anything after that keeps its own name. + if (( n == 1 )); then + target="id_ed25519" + else + target="$name" + fi + + if [[ -e "$HOME/.ssh/$target" ]]; then + warn "$target already exists in ~/.ssh - skipping" continue fi - install -m 600 "$WORK/plain/ssh/$name" "$HOME/.ssh/$name" + install -m 600 "$WORK/plain/ssh/$name" "$HOME/.ssh/$target" if [[ -f "$WORK/plain/ssh/$name.pub" ]]; then - install -m 644 "$WORK/plain/ssh/$name.pub" "$HOME/.ssh/$name.pub" + install -m 644 "$WORK/plain/ssh/$name.pub" "$HOME/.ssh/$target.pub" fi - info "Installed $HOME/.ssh/$name" + CHOSEN_KEYS+=("$name") + info "Installed $name as ~/.ssh/$target" done } @@ -216,25 +234,17 @@ clone_engram() { return fi - if (( ${#CHOSEN_KEYS[@]} == 0 )); then - warn "No SSH key was installed - cannot clone." + if [[ ! -f "$HOME/.ssh/id_ed25519" ]]; then + warn "No default key (~/.ssh/id_ed25519) - cannot clone." return fi mkdir -p "$(dirname "$ENGRAM_DIR")" - - local key - for key in "${CHOSEN_KEYS[@]}"; do - info "Trying key: $key" - if GIT_SSH_COMMAND="ssh -o IdentitiesOnly=yes -i $HOME/.ssh/$key" \ - git clone "$ENGRAM_REPO" "$ENGRAM_DIR"; then - info "Cloned into $ENGRAM_DIR (key: $key)" - return - fi + if ! git clone "$ENGRAM_REPO" "$ENGRAM_DIR"; then rm -rf "$ENGRAM_DIR" - done - - die "Clone failed with every key - check the deploy key setup on gitea." + die "Clone failed - is the deploy key set up on gitea?" + fi + info "Cloned into $ENGRAM_DIR" } # --------------------------------------------------------------------------- diff --git a/secrets/README.md b/secrets/README.md index a982510..a61654d 100644 --- a/secrets/README.md +++ b/secrets/README.md @@ -9,7 +9,8 @@ master key to all of it, so keep it outside this repo. - `ssh/nevim42-desktop`, `ssh/nevim42-mikoshi`, ... - SSH private keys named after machines (+ their `.pub`; `nevim42-mikoshi.pub` is registered as a - read-only deploy key on the engram repo) + read-only deploy key on the engram repo). The bootstrap installs your first + chosen key as the standard `~/.ssh/id_ed25519`. - `ssh/known_hosts.gitea` - pinned gitea host key, no TOFU on first connect - `gpg/nevim42.asc` - armored export of the GPG secret key