Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_raise_fwd_ext(self):
""" Test that we can silence the exceptions on sshtunnel creation """
server = open_tunnel(
'10.10.10.10',
ssh_username=SSH_USERNAME,
ssh_password=SSH_PASSWORD,
remote_bind_address=('10.0.0.1', 8080),
mute_exceptions=True,
)
# This should not raise an exception
server._raise(sshtunnel.BaseSSHTunnelForwarderError, 'test')
server._raise_fwd_exc = True # now exceptions are not silenced
with self.assertRaises(sshtunnel.BaseSSHTunnelForwarderError):
server._raise(sshtunnel.BaseSSHTunnelForwarderError, 'test')
if ports:
local_addresses = [('0.0.0.0', int(p.split(':')[0])) for p in ports]
remote_addresses = [(p.split(':')[1], int(p.split(':')[2])) for p in ports]
if cmdline:
func = partial(_ssh_exec, ip, port, cmdline, username, password, ssh_private_key)
else:
def _sleep():
while True:
time.sleep(1)
func = _sleep
_create_tunnel(ip, port, username, password, ssh_private_key,
local_addresses, remote_addresses, func)
else:
_ssh_exec(ip, port, cmdline, username, password, ssh_private_key)
except (BaseSSHTunnelForwarderError, paramiko.ssh_exception.AuthenticationException) as e:
raise CLIError('Connection to remote host failed. Please check provided credentials. Error: {0}'.format(e))
def _raise(self, exception=BaseSSHTunnelForwarderError, reason=None):
if self._raise_fwd_exc:
raise exception(reason)
else:
self.logger.error(repr(exception(reason)))
try:
self._connect_to_gateway()
except socket.gaierror: # raised by paramiko.Transport
msg = 'Could not resolve IP address for {0}, aborting!' \
.format(self.ssh_host)
self.logger.error(msg)
return
except (paramiko.SSHException, socket.error) as e:
template = 'Could not connect to gateway {0}:{1} : {2}'
msg = template.format(self.ssh_host, self.ssh_port, e.args[0])
self.logger.error(msg)
return
for (rem, loc) in zip(self._remote_binds, self._local_binds):
try:
self._make_ssh_forward_server(rem, loc)
except BaseSSHTunnelForwarderError as e:
msg = 'Problem setting SSH Forwarder up: {0}'.format(e.value)
self.logger.error(msg)
else:
forward_maker_class = self._make_ssh_forward_server_class
_Server = forward_maker_class(remote_address)
ssh_forward_server = _Server(
local_bind_address,
_Handler,
logger=self.logger,
)
if ssh_forward_server:
ssh_forward_server.daemon_threads = self.daemon_forward_servers
self._server_list.append(ssh_forward_server)
self.tunnel_is_up[ssh_forward_server.server_address] = False
else:
self._raise(
BaseSSHTunnelForwarderError,
'Problem setting up ssh {0} <> {1} forwarder. You can '
'suppress this exception by using the `mute_exceptions`'
'argument'.format(address_to_str(local_bind_address),
address_to_str(remote_address))
)
except IOError:
self._raise(
BaseSSHTunnelForwarderError,
"Couldn't open tunnel {0} <> {1} might be in use or "
"destination not reachable".format(
address_to_str(local_bind_address),
address_to_str(remote_address)
)
def local_bind_address(self):
# BACKWARDS COMPATIBILITY
self._check_is_started()
if len(self._server_list) != 1:
raise BaseSSHTunnelForwarderError(
'Use .local_bind_addresses property for more than one tunnel'
)
return self.local_bind_addresses[0]
def local_bind_port(self):
# BACKWARDS COMPATIBILITY
self._check_is_started()
if len(self._server_list) != 1:
raise BaseSSHTunnelForwarderError(
'Use .local_bind_ports property for more than one tunnel'
)
return self.local_bind_ports[0]
# Errors #
# #
########################
class BaseSSHTunnelForwarderError(Exception):
""" Exception raised by :class:`SSHTunnelForwarder` errors """
def __init__(self, *args, **kwargs):
self.value = kwargs.pop('value', args[0] if args else '')
def __str__(self):
return self.value
class HandlerSSHTunnelForwarderError(BaseSSHTunnelForwarderError):
""" Exception for Tunnel forwarder errors """
pass
########################
# #
# Handlers #
# #
########################
class _ForwardHandler(socketserver.BaseRequestHandler):
""" Base handler for tunnel connections """
remote_address = None
ssh_transport = None
logger = None
def local_bind_host(self):
# BACKWARDS COMPATIBILITY
self._check_is_started()
if len(self._server_list) != 1:
raise BaseSSHTunnelForwarderError(
'Use .local_bind_hosts property for more than one tunnel'
)
return self.local_bind_hosts[0]
def _check_is_started(self):
if not self.is_active: # underlying transport not alive
msg = 'Server is not started. Please .start() first!'
raise BaseSSHTunnelForwarderError(msg)
if not self.is_alive:
msg = 'Tunnels are not started. Please .start() first!'
raise HandlerSSHTunnelForwarderError(msg)