Skip to content

Commit

Permalink
Set parallel client host to actual destination when using proxy for n…
Browse files Browse the repository at this point in the history
…ative client - resolves #120
  • Loading branch information
pkittenis committed May 24, 2018
1 parent a255b25 commit ecb01d2
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 23 deletions.
8 changes: 8 additions & 0 deletions Changelog.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
Change Log
============

1.6.1
+++++++

Fixes
-------

* Host would always be `127.0.0.1` when using ``proxy_host`` on native client - #120.

1.6.0
++++++

Expand Down
7 changes: 4 additions & 3 deletions pssh/clients/native/parallel.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,12 +316,13 @@ def _make_ssh_client(self, host):
if self.proxy_host is not None:
tunnel = self._start_tunnel(host)
_user, _port, _password, _pkey = self._get_host_config_values(host)
_host = host if self.proxy_host is None else '127.0.0.1'
proxy_host = None if self.proxy_host is None else '127.0.0.1'
_port = _port if self.proxy_host is None else tunnel.listen_port
self.host_clients[host] = SSHClient(
_host, user=_user, password=_password, port=_port, pkey=_pkey,
host, user=_user, password=_password, port=_port, pkey=_pkey,
num_retries=self.num_retries, timeout=self.timeout,
allow_agent=self.allow_agent, retry_delay=self.retry_delay)
allow_agent=self.allow_agent, retry_delay=self.retry_delay,
proxy_host=proxy_host)

def copy_file(self, local_file, remote_file, recurse=False, copy_args=None):
"""Copy local file to remote file in parallel
Expand Down
11 changes: 8 additions & 3 deletions pssh/clients/native/single.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ def __init__(self, host,
num_retries=DEFAULT_RETRIES,
retry_delay=RETRY_DELAY,
allow_agent=True, timeout=None,
forward_ssh_agent=True):
forward_ssh_agent=True,
proxy_host=None):
""":param host: Host name or IP to connect to.
:type host: str
:param user: User to connect as. Defaults to logged in user.
Expand Down Expand Up @@ -93,6 +94,9 @@ def __init__(self, host,
equivalent to `ssh -A` from the `ssh` command line utility.
Defaults to True if not set.
:type forward_ssh_agent: bool
:param proxy_host: Connection to host is via provided proxy host
and client should use self.proxy_host for connection attempts.
:type proxy_host: str
"""
self.host = host
self.user = user if user else None
Expand All @@ -111,7 +115,8 @@ def __init__(self, host,
self.forward_ssh_agent = forward_ssh_agent
self._forward_requested = False
self.session = None
self._connect(self.host, self.port)
self._host = proxy_host if proxy_host else host
self._connect(self._host, self.port)
THREAD_POOL.apply(self._init)

def disconnect(self):
Expand Down Expand Up @@ -139,7 +144,7 @@ def _connect_init_retry(self, retries):
if not self.sock.closed:
self.sock.close()
sleep(self.retry_delay)
self._connect(self.host, self.port, retries=retries)
self._connect(self._host, self.port, retries=retries)
return self._init(retries=retries)

def _init(self, retries=1):
Expand Down
3 changes: 1 addition & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
'optimize.use_switch': True,
'wraparound': False,
}
_embedded_lib = bool(os.environ.get('EMBEDDED_LIB', 1))
_embedded_lib = bool(int(os.environ.get('EMBEDDED_LIB', 1)))

cython_args = {'cython_directives': cython_directives,
'cython_compile_time_env': {'EMBEDDED_LIB': _embedded_lib},
Expand Down Expand Up @@ -66,7 +66,6 @@
**cython_args
)]


package_data = {}

if ON_WINDOWS:
Expand Down
30 changes: 15 additions & 15 deletions tests/test_native_parallel_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1389,8 +1389,8 @@ def test_scp_send_dir(self):
gevent.joinall(cmds, raise_error=True)
self.assertTrue(os.path.isdir(remote_test_dir_abspath))
self.assertTrue(os.path.isfile(remote_file_abspath))
remote_file_data = open(remote_file_abspath, 'r').readlines()
self.assertEqual(remote_file_data[0].strip(), test_file_data)
remote_file_data = open(remote_file_abspath, 'r').read()
self.assertEqual(remote_file_data.strip(), test_file_data)
except Exception:
raise
finally:
Expand Down Expand Up @@ -1496,19 +1496,19 @@ def test_scp_recv(self):
shutil.rmtree(remote_test_path_abs)
shutil.rmtree(local_copied_dir)

## OpenSSHServer needs to run in its own thread for this test to work
## Race conditions otherwise.
#
# def test_tunnel(self):
# proxy_host = '127.0.0.9'
# server = OpenSSHServer(listen_ip=proxy_host, port=self.port)
# server.start_server()
# client = ParallelSSHClient(
# [self.host], port=self.port, pkey=self.user_key,
# proxy_host=proxy_host, proxy_port=self.port, num_retries=1,
# proxy_pkey=self.user_key,
# timeout=2)
# client.join(client.run_command('echo me'))
# This is a unit test, no output is checked, due to race conditions
# with running server in same thread.
def test_tunnel(self):
proxy_host = '127.0.0.9'
server = OpenSSHServer(listen_ip=proxy_host, port=self.port)
server.start_server()
client = ParallelSSHClient(
[self.host], port=self.port, pkey=self.user_key,
proxy_host=proxy_host, proxy_port=self.port, num_retries=1,
proxy_pkey=self.user_key,
timeout=2)
output = client.run_command('echo me', stop_on_errors=False)
self.assertEqual(self.host, list(output.keys())[0])

# def test_proxy_remote_host_failure_timeout(self):
# """Test that timeout setting is passed on to proxy to be used for the
Expand Down

0 comments on commit ecb01d2

Please sign in to comment.