diff --git a/spoke/setup-spoke.sh b/spoke/setup-spoke.sh index dabb2d8..48b1319 100755 --- a/spoke/setup-spoke.sh +++ b/spoke/setup-spoke.sh @@ -184,9 +184,10 @@ info "Hostname set to: $SPOKE_NAME" header "SSH Key Setup" echo "How would you like to handle the SSH key for the tunnel to $HUB_HOST?" echo " 1) Generate a new key automatically" -echo " 2) Use an existing key (paste the private key)" +echo " 2) Choose an existing key from $SSH_DIR" +echo " 3) Paste a private key manually" echo "" -read -rp "Choose [1/2]: " KEY_CHOICE +read -rp "Choose [1/2/3]: " KEY_CHOICE case "$KEY_CHOICE" in 1) @@ -217,6 +218,34 @@ case "$KEY_CHOICE" in read -rp "Press ENTER once the key has been added to ${HUB_HOST}..." ;; 2) + mkdir -p "$SSH_DIR" + chown "$SPOKE_USER":"$SPOKE_USER" "$SSH_DIR" + chmod 700 "$SSH_DIR" + + AVAILABLE_KEYS=() + while IFS= read -r keyfile; do + AVAILABLE_KEYS+=("$keyfile") + done < <(find "$SSH_DIR" -maxdepth 1 -type f ! -name "*.pub" ! -name "known_hosts" ! -name "authorized_keys" ! -name "config" | sort) + + if [ ${#AVAILABLE_KEYS[@]} -eq 0 ]; then + die "No private keys found in $SSH_DIR." + fi + + echo "Available keys:" + for i in "${!AVAILABLE_KEYS[@]}"; do + echo " $i) ${AVAILABLE_KEYS[$i]}" + done + echo "" + read -rp "Choose key [0]: " KEY_IDX + KEY_IDX="${KEY_IDX:-0}" + [[ "$KEY_IDX" =~ ^[0-9]+$ ]] && [ "$KEY_IDX" -lt "${#AVAILABLE_KEYS[@]}" ] || die "Invalid choice." + KEY_PATH="${AVAILABLE_KEYS[$KEY_IDX]}" + KEY_NAME="$(basename "$KEY_PATH")" + info "Using existing key: $KEY_PATH" + echo "" + read -rp "Press ENTER once the public key has been added to ${HUB_HOST} authorized_keys..." + ;; +3) read -rp "Enter a name for the key file [hubkey]: " KEY_NAME KEY_NAME="${KEY_NAME:-hubkey}" KEY_PATH="$SSH_DIR/$KEY_NAME"