e92ac8932cf0b5ab5d7f4a662bb57c9039c47920

Author
Marc Cornellà <marc.cornella@live.com>
Committer
GitHub <noreply@github.com>
Date

Message

Merge pull request #5328 from mcornella/ssh-agent-refactor

Refactor of ssh-agent plugin

Diff

This diff is truncated to protect this page.

  1diff --git a/plugins/ssh-agent/README.md b/plugins/ssh-agent/README.md
  2new file mode 100644
  3index 0000000000000000000000000000000000000000..00af42f01a2da775e02adba1a4db209cb3c52605
  4--- /dev/null
  5+++ b/plugins/ssh-agent/README.md
  6@@ -0,0 +1,38 @@
  7+# ssh-agent plugin
  8+
  9+This plugin starts automatically `ssh-agent` to set up and load whichever
 10+credentials you want for ssh connections.
 11+
 12+To enable it, add `ssh-agent` to your plugins:
 13+
 14+```zsh
 15+plugins=(... ssh-agent)
 16+```
 17+
 18+## Instructions
 19+
 20+To enable **agent forwarding support** add the following to your zshrc file:
 21+
 22+```zsh
 23+zstyle :omz:plugins:ssh-agent agent-forwarding on
 24+```
 25+
 26+To **load multiple identities** use the `identities` style, For example:
 27+
 28+```zsh
 29+zstyle :omz:plugins:ssh-agent identities id_rsa id_rsa2 id_github
 30+```
 31+
 32+To **set the maximum lifetime of the identities**, use the `lifetime` style.
 33+The lifetime may be specified in seconds or as described in sshd_config(5)
 34+(see _TIME FORMATS_). If left unspecified, the default lifetime is forever.
 35+
 36+```zsh
 37+zstyle :omz:plugins:ssh-agent lifetime 4h
 38+```
 39+
 40+## Credits
 41+
 42+Based on code from Joseph M. Reagle: http://www.cygwin.com/ml/cygwin/2001-06/msg00537.html
 43+
 44+Agent-forwarding support based on ideas from Florent Thoumie and Jonas Pfenniger
 45diff --git a/plugins/ssh-agent/ssh-agent.plugin.zsh b/plugins/ssh-agent/ssh-agent.plugin.zsh
 46index b77b9ee78dd4f2f86b54fd3a5a18378f1da68f7b..a84aac58ca337d4ecae06928d1b31cb5195ce01c 100644
 47--- a/plugins/ssh-agent/ssh-agent.plugin.zsh
 48+++ b/plugins/ssh-agent/ssh-agent.plugin.zsh
 49@@ -1,79 +1,42 @@
 50-#
 51-# INSTRUCTIONS
 52-#
 53-#   To enable agent forwarding support add the following to
 54-#   your .zshrc file:
 55-#
 56-#     zstyle :omz:plugins:ssh-agent agent-forwarding on
 57-#
 58-#   To load multiple identities use the identities style, For
 59-#   example:
 60-#
 61-#     zstyle :omz:plugins:ssh-agent identities id_rsa id_rsa2 id_github
 62-#
 63-#   To set the maximum lifetime of the identities, use the
 64-#   lifetime style. The lifetime may be specified in seconds
 65-#   or as described in sshd_config(5) (see TIME FORMATS)
 66-#   If left unspecified, the default lifetime is forever.
 67-#
 68-#     zstyle :omz:plugins:ssh-agent lifetime 4h
 69-#
 70-# CREDITS
 71-#
 72-#   Based on code from Joseph M. Reagle
 73-#   http://www.cygwin.com/ml/cygwin/2001-06/msg00537.html
 74-#
 75-#   Agent forwarding support based on ideas from
 76-#   Florent Thoumie and Jonas Pfenniger
 77-#
 78+typeset _agent_forwarding _ssh_env_cache
 79 
 80-local _plugin__ssh_env
 81-local _plugin__forwarding
 82+function _start_agent() {
 83+	local lifetime
 84+	local -a identities
 85 
 86-function _plugin__start_agent()
 87-{
 88-  local -a identities
 89-  local lifetime
 90-  zstyle -s :omz:plugins:ssh-agent lifetime lifetime
 91+	# start ssh-agent and setup environment
 92+	zstyle -s :omz:plugins:ssh-agent lifetime lifetime
 93 
 94-  # start ssh-agent and setup environment
 95-  /usr/bin/env ssh-agent ${lifetime:+-t} ${lifetime} | sed 's/^echo/#echo/' >! ${_plugin__ssh_env}
 96-  chmod 600 ${_plugin__ssh_env}
 97-  . ${_plugin__ssh_env} > /dev/null
 98+	ssh-agent -s ${lifetime:+-t} ${lifetime} | sed 's/^echo/#echo/' >! $_ssh_env_cache
 99+	chmod 600 $_ssh_env_cache
100+	. $_ssh_env_cache > /dev/null
101 
102-  # load identies
103-  zstyle -a :omz:plugins:ssh-agent identities identities
104-  echo starting ssh-agent...
105+	# load identies
106+	zstyle -a :omz:plugins:ssh-agent identities identities
107 
108-  /usr/bin/ssh-add $HOME/.ssh/${^identities}
109+	echo starting ssh-agent...
110+	ssh-add $HOME/.ssh/${^identities}
111 }
112 
113 # Get the filename to store/lookup the environment from
114-if (( $+commands[scutil] )); then
115-  # It's OS X!
116-  _plugin__ssh_env="$HOME/.ssh/environment-$(scutil --get ComputerName)"
117-else
118-  _plugin__ssh_env="$HOME/.ssh/environment-$HOST"
119-fi
120+_ssh_env_cache="$HOME/.ssh/environment-$SHORT_HOST"
121 
122 # test if agent-forwarding is enabled
123-zstyle -b :omz:plugins:ssh-agent agent-forwarding _plugin__forwarding
124-if [[ ${_plugin__forwarding} == "yes" && -n "$SSH_AUTH_SOCK" ]]; then
125-  # Add a nifty symlink for screen/tmux if agent forwarding
126-  [[ -L $SSH_AUTH_SOCK ]] || ln -sf "$SSH_AUTH_SOCK" /tmp/ssh-agent-$USER-screen
127-
128-elif [ -f "${_plugin__ssh_env}" ]; then
129-  # Source SSH settings, if applicable
130-  . ${_plugin__ssh_env} > /dev/null
131-  ps x | grep ${SSH_AGENT_PID} | grep ssh-agent > /dev/null || {
132-    _plugin__start_agent;
133-  }
134+zstyle -b :omz:plugins:ssh-agent agent-forwarding _agent_forwarding
135+
136+if [[ $_agent_forwarding == "yes" && -n "$SSH_AUTH_SOCK" ]]; then
137+	# Add a nifty symlink for screen/tmux if agent forwarding
138+	[[ -L $SSH_AUTH_SOCK ]] || ln -sf "$SSH_AUTH_SOCK" /tmp/ssh-agent-$USER-screen
139+elif [[ -f "$_ssh_env_cache" ]]; then
140+	# Source SSH settings, if applicable
141+	. $_ssh_env_cache > /dev/null
142+	ps -o cmd -p $SSH_AGENT_PID | grep -q ssh-agent || {
143+		_start_agent
144+	}
145 else
146-  _plugin__start_agent;
147+	_start_agent
148 fi