Skip to content
Prev Previous commit
Next Next commit
obey Hostname Username Port and ProxyCommand settings from .ssh/config
Signed-off-by: Till Riedel <[email protected]>
  • Loading branch information
riedel authored and ulyssessouza committed Feb 6, 2020
commit ed9b208e156a2685b032d934c02621f54caf7608
33 changes: 27 additions & 6 deletions docker/transport/sshconn.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import requests.adapters
import six
import logging
import os

from docker.transport.basehttpadapter import BaseHTTPAdapter
from .. import constants
Expand Down Expand Up @@ -73,17 +74,40 @@ def _get_conn(self, timeout):
class SSHHTTPAdapter(BaseHTTPAdapter):

__attrs__ = requests.adapters.HTTPAdapter.__attrs__ + [
'pools', 'timeout', 'ssh_client',
'pools', 'timeout', 'ssh_client', 'ssh_params'
]

def __init__(self, base_url, timeout=60,
pool_connections=constants.DEFAULT_NUM_POOLS):
logging.getLogger("paramiko").setLevel(logging.WARNING)
self.ssh_client = paramiko.SSHClient()
base_url = six.moves.urllib_parse.urlparse(base_url)
self.ssh_params = {
"hostname": base_url.hostname,
"port": base_url.port,
"username": base_url.username
}
ssh_config_file = os.path.expanduser("~/.ssh/config")
if os.path.exists(ssh_config_file):
conf = paramiko.SSHConfig()
with open(ssh_config_file) as f:
conf.parse(f)
host_config = conf.lookup(base_url.hostname)
self.ssh_conf = host_config
if 'proxycommand' in host_config:
self.ssh_params["sock"] = paramiko.ProxyCommand(
self.ssh_conf['proxycommand']
)
if 'hostname' in host_config:
self.ssh_params['hostname'] = host_config['hostname']
if base_url.port is None and 'port' in host_config:
self.ssh_params['port'] = self.ssh_conf['port']
if base_url.username is None and 'user' in host_config:
self.ssh_params['username'] = self.ssh_conf['user']

self.ssh_client.load_system_host_keys()
self.ssh_client.set_missing_host_key_policy(paramiko.WarningPolicy())

self.base_url = base_url
self._connect()
self.timeout = timeout
self.pools = RecentlyUsedContainer(
Expand All @@ -92,10 +116,7 @@ def __init__(self, base_url, timeout=60,
super(SSHHTTPAdapter, self).__init__()

def _connect(self):
parsed = six.moves.urllib_parse.urlparse(self.base_url)
self.ssh_client.connect(
parsed.hostname, parsed.port, parsed.username,
)
self.ssh_client.connect(**self.ssh_params)

def get_connection(self, url, proxies=None):
with self.pools.lock:
Expand Down