Skip to content

Commit e57462a

Browse files
committed
Merge pull request #59 from hokadiri/master
Added `allow_agent` class parameter to pass on to paramiko to disabling use of SSH agent (on by default).
2 parents 4356eb1 + b36f468 commit e57462a

File tree

3 files changed

+46
-6
lines changed

3 files changed

+46
-6
lines changed

pssh/pssh_client.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def __init__(self, hosts,
5050
user=None, password=None, port=None, pkey=None,
5151
forward_ssh_agent=True, num_retries=DEFAULT_RETRIES, timeout=120,
5252
pool_size=10, proxy_host=None, proxy_port=22,
53-
agent=None, host_config=None, channel_timeout=None):
53+
agent=None, allow_agent=True, host_config=None, channel_timeout=None):
5454
"""
5555
:param hosts: Hosts to connect to
5656
:type hosts: list(str)
@@ -96,7 +96,10 @@ def __init__(self, hosts,
9696
:param channel_timeout: (Optional) Time in seconds before an SSH operation \
9797
times out.
9898
:type channel_timeout: int
99-
99+
:type channel_timeout: int
100+
:param allow_agent: (Optional) set to False to disable connecting to \
101+
the SSH agent
102+
:type allow_agent: bool
100103
**Example Usage**
101104
102105
>>> from pssh.pssh_client import ParallelSSHClient
@@ -255,6 +258,7 @@ def __init__(self, hosts,
255258
# To hold host clients
256259
self.host_clients = {}
257260
self.agent = agent
261+
self.allow_agent = allow_agent
258262
self.host_config = host_config if host_config else {}
259263
self.channel_timeout = channel_timeout
260264

@@ -474,7 +478,7 @@ def _exec_command(self, host, *args, **kwargs):
474478
timeout=self.timeout,
475479
proxy_host=self.proxy_host,
476480
proxy_port=self.proxy_port,
477-
agent=self.agent,
481+
allow_agent=self.allow_agent, agent=self.agent,
478482
channel_timeout=self.channel_timeout)
479483
return self.host_clients[host].exec_command(*args, **kwargs)
480484

pssh/ssh_client.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,9 @@ class SSHClient(object):
4141
def __init__(self, host,
4242
user=None, password=None, port=None,
4343
pkey=None, forward_ssh_agent=True,
44-
num_retries=DEFAULT_RETRIES, agent=None, timeout=10,
45-
proxy_host=None, proxy_port=22, channel_timeout=None):
44+
num_retries=DEFAULT_RETRIES, agent=None,
45+
allow_agent=True, timeout=10, proxy_host=None,
46+
proxy_port=22, channel_timeout=None):
4647
"""Connect to host honouring any user set configuration in ~/.ssh/config \
4748
or /etc/ssh/ssh_config
4849
@@ -74,6 +75,10 @@ def __init__(self, host,
7475
connecting to local SSH agent to lookup keys with our own SSH agent \
7576
object.
7677
:type agent: :mod:`paramiko.agent.Agent`
78+
:param forward_ssh_agent: (Optional) Turn on SSH agent forwarding - \
79+
equivalent to `ssh -A` from the `ssh` command line utility. \
80+
Defaults to True if not set.
81+
:type forward_ssh_agent: bool
7782
:param proxy_host: (Optional) SSH host to tunnel connection through \
7883
so that SSH clients connects to self.host via client -> proxy_host -> host
7984
:type proxy_host: str
@@ -83,6 +88,9 @@ def __init__(self, host,
8388
:param channel_timeout: (Optional) Time in seconds before an SSH operation \
8489
times out.
8590
:type channel_timeout: int
91+
:param allow_agent: (Optional) set to False to disable connecting to \
92+
the SSH agent
93+
:type allow_agent: bool
8694
"""
8795
ssh_config = paramiko.SSHConfig()
8896
_ssh_config_file = os.path.sep.join([os.path.expanduser('~'),
@@ -107,6 +115,7 @@ def __init__(self, host,
107115
self.pkey = pkey
108116
self.port = port if port else 22
109117
self.host = resolved_address
118+
self.allow_agent = allow_agent
110119
if agent:
111120
self.client._agent = agent
112121
self.num_retries = num_retries
@@ -158,7 +167,8 @@ def _connect(self, client, host, port, sock=None, retries=1):
158167
client.connect(host, username=self.user,
159168
password=self.password, port=port,
160169
pkey=self.pkey,
161-
sock=sock, timeout=self.timeout)
170+
sock=sock, timeout=self.timeout,
171+
allow_agent=self.allow_agent)
162172
except sock_gaierror, ex:
163173
logger.error("Could not resolve host '%s' - retry %s/%s",
164174
self.host, retries, self.num_retries)

tests/test_pssh_client.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -690,3 +690,29 @@ def test_host_config(self):
690690
msg="Host config pkey override failed")
691691
for (server, _) in servers:
692692
server.kill()
693+
694+
def test_pssh_client_override_allow_agent_authentication(self):
695+
"""Test running command with allow_agent set to False"""
696+
client = ParallelSSHClient([self.host], port=self.listen_port,
697+
pkey=self.user_key, allow_agent=False)
698+
output = client.run_command(self.fake_cmd)
699+
expected_exit_code = 0
700+
expected_stdout = [self.fake_resp]
701+
expected_stderr = []
702+
703+
stdout = list(output[self.host]['stdout'])
704+
stderr = list(output[self.host]['stderr'])
705+
exit_code = output[self.host]['exit_code']
706+
self.assertEqual(expected_exit_code, exit_code,
707+
msg="Got unexpected exit code - %s, expected %s" %
708+
(exit_code,
709+
expected_exit_code,))
710+
self.assertEqual(expected_stdout, stdout,
711+
msg="Got unexpected stdout - %s, expected %s" %
712+
(stdout,
713+
expected_stdout,))
714+
self.assertEqual(expected_stderr, stderr,
715+
msg="Got unexpected stderr - %s, expected %s" %
716+
(stderr,
717+
expected_stderr,))
718+
del client

0 commit comments

Comments
 (0)