def ServerHostKeyPolicy(*args, **kwarks): paramiko = import_paramiko() class ServerHostKeyPolicy(paramiko.MissingHostKeyPolicy): def __init__(self, fingerprints_func): self.fingerprints_func = fingerprints_func @lazy def fingerprints(self): return self.fingerprints_func() def missing_host_key(self, client, hostname, key): ssh_key_info = SSHKeyInfo(key) for fingerprint in self.fingerprints: if fingerprint in ssh_key_info: if not fingerprint.store: return client.get_host_keys().add(hostname, key.get_name(), key) if client._host_keys_filename is not None: client.save_host_keys(client._host_keys_filename) return raise paramiko.SSHException( "Fingerprint doesn't match for %s (got %r, expected: %s)" % ( hostname, ssh_key_info.get_fingerprint_objects(), self.fingerprints)) return ServerHostKeyPolicy(*args, **kwarks)
def setUp(self): import ploy_openvz paramiko = import_paramiko() self.directory = tempfile.mkdtemp() self.ctrl = Controller(self.directory) self.ctrl.__dict__['plugins'] = {'vz': ploy_openvz.plugin} self._ssh_client_mock = patch("%s.SSHClient" % paramiko.__name__) self.ssh_client_mock = self._ssh_client_mock.start() self.ssh_client_exec_results = [] def exec_command(cmd): if len(self.ssh_client_exec_results) == 0: # pragma: no cover - only if test is wrong self.fail("No results for exec_command, expected on for '%s'" % cmd) result = self.ssh_client_exec_results.pop(0) if len(result) != 2 or len(result[1]) != 2: # pragma: no cover - only if test is wrong self.fail("ssh_client_exec_results needs to contain tuples in the form of (expected_cmd, (stdout, stderr)).") self.assertEquals(cmd, result[0], 'expected command mismatch') return None, StringIO(result[1][0]), StringIO(result[1][1]) self.ssh_client_mock().exec_command.side_effect = exec_command self._ssh_config_mock = patch("%s.SSHConfig" % paramiko.__name__) self.ssh_config_mock = self._ssh_config_mock.start() self.ssh_config_mock().lookup.return_value = {} self._os_execvp_mock = patch("subprocess.call") self.os_execvp_mock = self._os_execvp_mock.start()
def ServerHostKeyPolicy(*args, **kwarks): paramiko = import_paramiko() class ServerHostKeyPolicy(paramiko.MissingHostKeyPolicy): def __init__(self, fingerprints_func): self.fingerprints_func = fingerprints_func @lazy def fingerprints(self): return self.fingerprints_func() def missing_host_key(self, client, hostname, key): ssh_key_info = SSHKeyInfo(key) for fingerprint in self.fingerprints: if fingerprint in ssh_key_info: if not fingerprint.store: return client.get_host_keys().add(hostname, key.get_name(), key) if client._host_keys_filename is not None: client.save_host_keys(client._host_keys_filename) return raise paramiko.SSHException( "Fingerprint doesn't match for %s (got %r, expected: %s)" % (hostname, ssh_key_info.get_fingerprint_objects(), self.fingerprints)) return ServerHostKeyPolicy(*args, **kwarks)
def setUp(self): import ploy_openvz paramiko = import_paramiko() self.directory = tempfile.mkdtemp() self.ctrl = Controller(self.directory) self.ctrl.__dict__['plugins'] = {'vz': ploy_openvz.plugin} self._ssh_client_mock = patch("%s.SSHClient" % paramiko.__name__) self.ssh_client_mock = self._ssh_client_mock.start() self._ssh_config_mock = patch("%s.SSHConfig" % paramiko.__name__) self.ssh_config_mock = self._ssh_config_mock.start() self.ssh_config_mock().lookup.return_value = {} self._os_execvp_mock = patch("os.execvp") self.os_execvp_mock = self._os_execvp_mock.start()
def paramiko(): from ploy.common import import_paramiko return import_paramiko()