def test__get_connection_exception(self, ssh_connect_mock): ssh_connect_mock.side_effect = iter( [exception.SSHConnectFailed(host='fake')]) self.assertRaises(exception.SSHConnectFailed, ssh._get_connection, self.node) driver_info = ssh._parse_driver_info(self.node) ssh_connect_mock.assert_called_once_with(driver_info)
def ssh_connect(connection): """Method to connect to a remote system using ssh protocol. :param connection: a dict of connection parameters. :returns: paramiko.SSHClient -- an active ssh connection. :raises: SSHConnectFailed """ try: ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect(connection.get('host'), username=connection.get('username'), password=connection.get('password'), port=connection.get('port', 22), key_filename=connection.get('key_filename'), timeout=connection.get('timeout', 10)) # send TCP keepalive packets every 20 seconds ssh.get_transport().set_keepalive(20) except Exception as e: LOG.debug(_("SSH connect failed: %s") % e) raise exception.SSHConnectFailed(host=connection.get('host')) return ssh
def test__get_connection_exception(self): with mock.patch.object(utils, 'ssh_connect') as ssh_connect_mock: ssh_connect_mock.side_effect = exception.SSHConnectFailed( host='fake') self.assertRaises(exception.SSHConnectFailed, ssh._get_connection, self.node) driver_info = ssh._parse_driver_info(self.node) ssh_connect_mock.assert_called_once_with(driver_info)
def test__validate_info_ssh_connect_failed(self, ssh_connect_mock): info = ssh._parse_driver_info(self.node) ssh_connect_mock.side_effect = exception.SSHConnectFailed(host='fake') with task_manager.acquire(self.context, info['uuid'], shared=False) as task: self.assertRaises(exception.InvalidParameterValue, task.driver.power.validate, task) driver_info = ssh._parse_driver_info(task.node) ssh_connect_mock.assert_called_once_with(driver_info)
def test__validate_info_ssh_connect_failed(self): info = ssh._parse_driver_info(self.node) self.get_conn_patcher.stop() self.get_conn_mock = None with mock.patch.object(utils, 'ssh_connect') \ as ssh_connect_mock: ssh_connect_mock.side_effect = exception.SSHConnectFailed( host='fake') with task_manager.acquire(self.context, [info['uuid']], shared=False) as task: self.assertRaises(exception.InvalidParameterValue, task.resources[0].driver.power.validate, task, self.node) driver_info = ssh._parse_driver_info(self.node) ssh_connect_mock.assert_called_once_with(driver_info)
def ssh_connect(connection): """Method to connect to a remote system using ssh protocol. :param connection: a dict of connection parameters. :returns: paramiko.SSHClient -- an active ssh connection. :raises: SSHConnectFailed """ try: ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) key_contents = connection.get('key_contents') if key_contents: data = six.moves.StringIO(key_contents) if "BEGIN RSA PRIVATE" in key_contents: pkey = paramiko.RSAKey.from_private_key(data) elif "BEGIN DSA PRIVATE" in key_contents: pkey = paramiko.DSSKey.from_private_key(data) else: # Can't include the key contents - secure material. raise ValueError(_("Invalid private key")) else: pkey = None ssh.connect(connection.get('host'), username=connection.get('username'), password=connection.get('password'), port=connection.get('port', 22), pkey=pkey, key_filename=connection.get('key_filename'), timeout=connection.get('timeout', 10)) # send TCP keepalive packets every 20 seconds ssh.get_transport().set_keepalive(20) except Exception as e: LOG.debug("SSH connect failed: %s" % e) raise exception.SSHConnectFailed(host=connection.get('host')) return ssh
def _get_ssh_connection(connection): """Returns an SSH client connected to a node. :param node: the Node. :returns: paramiko.SSHClient, an active ssh connection. """ try: ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) key_contents = connection.get('key_contents') if key_contents: data = six.StringIO(key_contents) if "BEGIN RSA PRIVATE" in key_contents: pkey = paramiko.RSAKey.from_private_key(data) elif "BEGIN DSA PRIVATE" in key_contents: pkey = paramiko.DSSKey.from_private_key(data) else: # Can't include the key contents - secure material. raise ValueError(_("Invalid private key")) else: pkey = None ssh.connect(connection.get('host'), username=connection.get('username'), password=None, port=connection.get('port', 22), pkey=pkey, key_filename=connection.get('key_filename'), timeout=connection.get('timeout', 10)) # send TCP keepalive packets every 20 seconds ssh.get_transport().set_keepalive(20) except Exception as e: LOG.debug("SSH connect failed: %s", e) raise exception.SSHConnectFailed(host=connection.get('host')) return ssh