5a9d9553cd6861d80cc958132ab5af40fe661ac4

Author
Simone Gaiarin <simgunz@gmail.com>
Committer
GitHub <noreply@github.com>
Date

Message

feat(ssh-agent): allow using external helper to ask for passwords (#7631)

Diff

This diff is truncated to protect this page.

  1diff --git a/plugins/ssh-agent/README.md b/plugins/ssh-agent/README.md
  2index 8765a9c7ea9aaaa7229996570aef96317bf77930..f46e8bf6af3319077887c52fcb8fc79f48dedb3c 100644
  3--- a/plugins/ssh-agent/README.md
  4+++ b/plugins/ssh-agent/README.md
  5@@ -55,6 +55,15 @@ ssh-add -K -c -a /run/user/1000/ssh-auth <identities>
  6 
  7 For valid `ssh-add` arguments run `ssh-add --help` or `man ssh-add`.
  8 
  9+----
 10+
 11+To set an **external helper** to ask for the passwords and possibly store
 12+them in the system keychain use the `helper` style. For example:
 13+
 14+```zsh
 15+zstyle :omz:plugins:ssh-agent helper ksshaskpass
 16+```
 17+
 18 ## Credits
 19 
 20 Based on code from Joseph M. Reagle: https://www.cygwin.com/ml/cygwin/2001-06/msg00537.html
 21diff --git a/plugins/ssh-agent/ssh-agent.plugin.zsh b/plugins/ssh-agent/ssh-agent.plugin.zsh
 22index d45406f63837dfc7adc770e73855df4aa436e3da..9bc2e8a21a84db59cf756495ae0a9ec1922ec9e9 100644
 23--- a/plugins/ssh-agent/ssh-agent.plugin.zsh
 24+++ b/plugins/ssh-agent/ssh-agent.plugin.zsh
 25@@ -1,56 +1,76 @@
 26 typeset _agent_forwarding _ssh_env_cache
 27 
 28 function _start_agent() {
 29-	local lifetime
 30-	zstyle -s :omz:plugins:ssh-agent lifetime lifetime
 31-
 32-	# start ssh-agent and setup environment
 33-	echo Starting ssh-agent...
 34-	ssh-agent -s ${lifetime:+-t} ${lifetime} | sed 's/^echo/#echo/' >! $_ssh_env_cache
 35-	chmod 600 $_ssh_env_cache
 36-	. $_ssh_env_cache > /dev/null
 37+  local lifetime
 38+  zstyle -s :omz:plugins:ssh-agent lifetime lifetime
 39+
 40+  # start ssh-agent and setup environment
 41+  echo Starting ssh-agent...
 42+  ssh-agent -s ${lifetime:+-t} ${lifetime} | sed 's/^echo/#echo/' >! $_ssh_env_cache
 43+  chmod 600 $_ssh_env_cache
 44+  . $_ssh_env_cache > /dev/null
 45 }
 46 
 47 function _add_identities() {
 48-	local id line sig lines
 49-	local -a identities loaded_sigs loaded_ids not_loaded
 50-	zstyle -a :omz:plugins:ssh-agent identities identities
 51-
 52-	# check for .ssh folder presence
 53-	if [[ ! -d $HOME/.ssh ]]; then
 54-		return
 55-	fi
 56-
 57-	# add default keys if no identities were set up via zstyle
 58-	# this is to mimic the call to ssh-add with no identities
 59-	if [[ ${#identities} -eq 0 ]]; then
 60-		# key list found on `ssh-add` man page's DESCRIPTION section
 61-		for id in id_rsa id_dsa id_ecdsa id_ed25519 identity; do
 62-			# check if file exists
 63-			[[ -f "$HOME/.ssh/$id" ]] && identities+=$id
 64-		done
 65-	fi
 66-
 67-	# get list of loaded identities' signatures and filenames
 68-	if lines=$(ssh-add -l); then
 69-		for line in ${(f)lines}; do
 70-			loaded_sigs+=${${(z)line}[2]}
 71-			loaded_ids+=${${(z)line}[3]}
 72-		done
 73-	fi
 74-
 75-	# add identities if not already loaded
 76-	for id in $identities; do
 77-		# check for filename match, otherwise try for signature match
 78-		if [[ ${loaded_ids[(I)$HOME/.ssh/$id]} -le 0 ]]; then
 79-			sig="$(ssh-keygen -lf "$HOME/.ssh/$id" | awk '{print $2}')"
 80-			[[ ${loaded_sigs[(I)$sig]} -le 0 ]] && not_loaded+="$HOME/.ssh/$id"
 81-		fi
 82-	done
 83-
 84-	local args
 85-	zstyle -a :omz:plugins:ssh-agent ssh-add-args args
 86-	[[ -n "$not_loaded" ]] && ssh-add "${args[@]}" ${^not_loaded}
 87+  local id line sig lines
 88+  local -a identities loaded_sigs loaded_ids not_loaded
 89+  zstyle -a :omz:plugins:ssh-agent identities identities
 90+
 91+  # check for .ssh folder presence
 92+  if [[ ! -d $HOME/.ssh ]]; then
 93+    return
 94+  fi
 95+
 96+  # add default keys if no identities were set up via zstyle
 97+  # this is to mimic the call to ssh-add with no identities
 98+  if [[ ${#identities} -eq 0 ]]; then
 99+    # key list found on `ssh-add` man page's DESCRIPTION section
100+    for id in id_rsa id_dsa id_ecdsa id_ed25519 identity; do
101+      # check if file exists
102+      [[ -f "$HOME/.ssh/$id" ]] && identities+=($id)
103+    done
104+  fi
105+
106+  # get list of loaded identities' signatures and filenames
107+  if lines=$(ssh-add -l); then
108+    for line in ${(f)lines}; do
109+      loaded_sigs+=${${(z)line}[2]}
110+      loaded_ids+=${${(z)line}[3]}
111+    done
112+  fi
113+
114+  # add identities if not already loaded
115+  for id in $identities; do
116+    # check for filename match, otherwise try for signature match
117+    if [[ ${loaded_ids[(I)$HOME/.ssh/$id]} -le 0 ]]; then
118+      sig="$(ssh-keygen -lf "$HOME/.ssh/$id" | awk '{print $2}')"
119+      [[ ${loaded_sigs[(I)$sig]} -le 0 ]] && not_loaded+=("$HOME/.ssh/$id")
120+    fi
121+  done
122+
123+  # abort if no identities need to be loaded
124+  if [[ ${#not_loaded} -eq 0 ]]; then