Skip to content

Commit c285bee

Browse files
riedelndeloof
authored andcommitted
obey Hostname Username Port and ProxyCommand settings from .ssh/config
Signed-off-by: Till Riedel <[email protected]>
1 parent eb8c78c commit c285bee

File tree

1 file changed

+27
-6
lines changed

1 file changed

+27
-6
lines changed

docker/transport/sshconn.py

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import requests.adapters
33
import six
44
import logging
5+
import os
56

67
from docker.transport.basehttpadapter import BaseHTTPAdapter
78
from .. import constants
@@ -73,17 +74,40 @@ def _get_conn(self, timeout):
7374
class SSHHTTPAdapter(BaseHTTPAdapter):
7475

7576
__attrs__ = requests.adapters.HTTPAdapter.__attrs__ + [
76-
'pools', 'timeout', 'ssh_client',
77+
'pools', 'timeout', 'ssh_client', 'ssh_params'
7778
]
7879

7980
def __init__(self, base_url, timeout=60,
8081
pool_connections=constants.DEFAULT_NUM_POOLS):
8182
logging.getLogger("paramiko").setLevel(logging.WARNING)
8283
self.ssh_client = paramiko.SSHClient()
84+
base_url = six.moves.urllib_parse.urlparse(base_url)
85+
self.ssh_params = {
86+
"hostname": base_url.hostname,
87+
"port": base_url.port,
88+
"username": base_url.username
89+
}
90+
ssh_config_file = os.path.expanduser("~/.ssh/config")
91+
if os.path.exists(ssh_config_file):
92+
conf = paramiko.SSHConfig()
93+
with open(ssh_config_file) as f:
94+
conf.parse(f)
95+
host_config = conf.lookup(base_url.hostname)
96+
self.ssh_conf = host_config
97+
if 'proxycommand' in host_config:
98+
self.ssh_params["sock"] = paramiko.ProxyCommand(
99+
self.ssh_conf['proxycommand']
100+
)
101+
if 'hostname' in host_config:
102+
self.ssh_params['hostname'] = host_config['hostname']
103+
if base_url.port is None and 'port' in host_config:
104+
self.ssh_params['port'] = self.ssh_conf['port']
105+
if base_url.username is None and 'user' in host_config:
106+
self.ssh_params['username'] = self.ssh_conf['user']
107+
83108
self.ssh_client.load_system_host_keys()
84109
self.ssh_client.set_missing_host_key_policy(paramiko.WarningPolicy())
85110

86-
self.base_url = base_url
87111
self._connect()
88112
self.timeout = timeout
89113
self.pools = RecentlyUsedContainer(
@@ -92,10 +116,7 @@ def __init__(self, base_url, timeout=60,
92116
super(SSHHTTPAdapter, self).__init__()
93117

94118
def _connect(self):
95-
parsed = six.moves.urllib_parse.urlparse(self.base_url)
96-
self.ssh_client.connect(
97-
parsed.hostname, parsed.port, parsed.username,
98-
)
119+
self.ssh_client.connect(**self.ssh_params)
99120

100121
def get_connection(self, url, proxies=None):
101122
with self.pools.lock:

0 commit comments

Comments
 (0)