From c36cfd0b059fc2b3c8ab84db5179cc8c8e7143c6 Mon Sep 17 00:00:00 2001 From: Chris Opland Date: Fri, 12 Apr 2019 10:48:39 -0500 Subject: Add support for automatically adding known_hosts Currently, if a server is not in the known_hosts file then the client will fail with an error like: paramiko.SSHException: Server '[]:' not found in known_hosts This change allows users to configure paramiko with the AutoAddPolicy(). It should be noted that using this policy opens users to man-in-the-middle attacks, which is why it is an optional setting and defaults to False. --- pygerrit/client.py | 16 ++++++++++------ pygerrit/ssh.py | 7 +++++-- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/pygerrit/client.py b/pygerrit/client.py index 733aa9a..9161bdf 100644 --- a/pygerrit/client.py +++ b/pygerrit/client.py @@ -37,14 +37,17 @@ class GerritClient(object): """ Gerrit client interface. - :arg str host: The hostname. - :arg str username: (optional) The username to use when connecting. - :arg str port: (optional) The port number to connect to. - :arg int keepalive: (optional) Keepalive interval in seconds. + :arg str host: The hostname. + :arg str username: (optional) The username to use when connecting. + :arg str port: (optional) The port number to connect to. + :arg int keepalive: (optional) Keepalive interval in seconds. + :arg bool auto_add_hosts: (optional) If True, the ssh client will + automatically add hosts to known_hosts. """ - def __init__(self, host, username=None, port=None, keepalive=None): + def __init__(self, host, username=None, port=None, + keepalive=None, auto_add_hosts=False): self._factory = GerritEventFactory() self._events = Queue() self._stream = None @@ -52,7 +55,8 @@ class GerritClient(object): self._ssh_client = GerritSSHClient(host, username=username, port=port, - keepalive=keepalive) + keepalive=keepalive, + auto_add_hosts=auto_add_hosts) def gerrit_version(self): """ Get the Gerrit version. diff --git a/pygerrit/ssh.py b/pygerrit/ssh.py index fbc5a29..03952e3 100644 --- a/pygerrit/ssh.py +++ b/pygerrit/ssh.py @@ -29,7 +29,7 @@ from threading import Event, Lock from .error import GerritError -from paramiko import SSHClient, SSHConfig, ProxyCommand +from paramiko import AutoAddPolicy, SSHClient, SSHConfig, ProxyCommand from paramiko.ssh_exception import SSHException @@ -65,7 +65,8 @@ class GerritSSHClient(SSHClient): """ Gerrit SSH Client, wrapping the paramiko SSH Client. """ - def __init__(self, hostname, username=None, port=None, keepalive=None): + def __init__(self, hostname, username=None, port=None, + keepalive=None, auto_add_hosts=False): """ Initialise and connect to SSH. """ super(GerritSSHClient, self).__init__() self.remote_version = None @@ -77,6 +78,8 @@ class GerritSSHClient(SSHClient): self.lock = Lock() self.proxy = None self.keepalive = keepalive + if auto_add_hosts: + self.set_missing_host_key_policy(AutoAddPolicy()) def _configure(self): """ Configure the ssh parameters from the config file. """ -- cgit v1.2.1