Skip to content

Commit 338dfb0

Browse files
committed
Add support for SSH protocol in base_url
Signed-off-by: Joffrey F <[email protected]>
1 parent 479f13e commit 338dfb0

File tree

4 files changed

+145
-5
lines changed

4 files changed

+145
-5
lines changed

docker/api/client.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@
3939
except ImportError:
4040
pass
4141

42+
try:
43+
from ..transport import SSHAdapter
44+
except ImportError:
45+
pass
46+
4247

4348
class APIClient(
4449
requests.Session,
@@ -141,6 +146,18 @@ def __init__(self, base_url=None, version=None,
141146
)
142147
self.mount('http+docker://', self._custom_adapter)
143148
self.base_url = 'http+docker://localnpipe'
149+
elif base_url.startswith('ssh://'):
150+
try:
151+
self._custom_adapter = SSHAdapter(
152+
base_url, timeout, pool_connections=num_pools
153+
)
154+
except NameError:
155+
raise DockerException(
156+
'Install paramiko package to enable ssh:// support'
157+
)
158+
self.mount('http+docker://ssh', self._custom_adapter)
159+
self._unmount('http://', 'https://')
160+
self.base_url = 'http+docker://ssh'
144161
else:
145162
# Use SSLAdapter for the ability to specify SSL version
146163
if isinstance(tls, TLSConfig):
@@ -279,6 +296,8 @@ def _get_raw_response_socket(self, response):
279296
self._raise_for_status(response)
280297
if self.base_url == "http+docker://localnpipe":
281298
sock = response.raw._fp.fp.raw.sock
299+
elif self.base_url.startswith('http+docker://ssh'):
300+
sock = response.raw._fp.fp.channel
282301
elif six.PY3:
283302
sock = response.raw._fp.fp.raw
284303
if self.base_url.startswith("https://"):

docker/transport/__init__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,8 @@
66
from .npipesocket import NpipeSocket
77
except ImportError:
88
pass
9+
10+
try:
11+
from .sshconn import SSHAdapter
12+
except ImportError:
13+
pass

docker/transport/sshconn.py

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
import urllib.parse
2+
3+
import paramiko
4+
import requests.adapters
5+
import six
6+
7+
8+
from .. import constants
9+
10+
if six.PY3:
11+
import http.client as httplib
12+
else:
13+
import httplib
14+
15+
try:
16+
import requests.packages.urllib3 as urllib3
17+
except ImportError:
18+
import urllib3
19+
20+
RecentlyUsedContainer = urllib3._collections.RecentlyUsedContainer
21+
22+
23+
class SSHConnection(httplib.HTTPConnection, object):
24+
def __init__(self, ssh_transport, timeout=60):
25+
super(SSHConnection, self).__init__(
26+
'localhost', timeout=timeout
27+
)
28+
self.ssh_transport = ssh_transport
29+
self.timeout = timeout
30+
31+
def connect(self):
32+
sock = self.ssh_transport.open_session()
33+
sock.settimeout(self.timeout)
34+
sock.exec_command('docker system dial-stdio')
35+
self.sock = sock
36+
37+
38+
class SSHConnectionPool(urllib3.connectionpool.HTTPConnectionPool):
39+
scheme = 'ssh'
40+
41+
def __init__(self, ssh_client, timeout=60, maxsize=10):
42+
super(SSHConnectionPool, self).__init__(
43+
'localhost', timeout=timeout, maxsize=maxsize
44+
)
45+
self.ssh_transport = ssh_client.get_transport()
46+
self.timeout = timeout
47+
48+
def _new_conn(self):
49+
return SSHConnection(self.ssh_transport, self.timeout)
50+
51+
# When re-using connections, urllib3 calls fileno() on our
52+
# SSH channel instance, quickly overloading our fd limit. To avoid this,
53+
# we override _get_conn
54+
def _get_conn(self, timeout):
55+
conn = None
56+
try:
57+
conn = self.pool.get(block=self.block, timeout=timeout)
58+
59+
except AttributeError: # self.pool is None
60+
raise urllib3.exceptions.ClosedPoolError(self, "Pool is closed.")
61+
62+
except six.moves.queue.Empty:
63+
if self.block:
64+
raise urllib3.exceptions.EmptyPoolError(
65+
self,
66+
"Pool reached maximum size and no more "
67+
"connections are allowed."
68+
)
69+
pass # Oh well, we'll create a new connection then
70+
71+
return conn or self._new_conn()
72+
73+
74+
class SSHAdapter(requests.adapters.HTTPAdapter):
75+
76+
__attrs__ = requests.adapters.HTTPAdapter.__attrs__ + [
77+
'pools', 'timeout', 'ssh_client',
78+
]
79+
80+
def __init__(self, base_url, timeout=60,
81+
pool_connections=constants.DEFAULT_NUM_POOLS):
82+
self.ssh_client = paramiko.SSHClient()
83+
self.ssh_client.load_system_host_keys()
84+
85+
parsed = urllib.parse.urlparse(base_url)
86+
self.ssh_client.connect(
87+
parsed.hostname, parsed.port, parsed.username,
88+
)
89+
self.timeout = timeout
90+
self.pools = RecentlyUsedContainer(
91+
pool_connections, dispose_func=lambda p: p.close()
92+
)
93+
super(SSHAdapter, self).__init__()
94+
95+
def get_connection(self, url, proxies=None):
96+
with self.pools.lock:
97+
pool = self.pools.get(url)
98+
if pool:
99+
return pool
100+
101+
pool = SSHConnectionPool(
102+
self.ssh_client, self.timeout
103+
)
104+
self.pools[url] = pool
105+
106+
return pool
107+
108+
def close(self):
109+
self.pools.clear()
110+
self.ssh_client.close()

docker/utils/utils.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -250,24 +250,30 @@ def parse_host(addr, is_win32=False, tls=False):
250250
addr = addr[8:]
251251
elif addr.startswith('fd://'):
252252
raise errors.DockerException("fd protocol is not implemented")
253+
elif addr.startswith('ssh://'):
254+
proto = 'ssh'
255+
addr = addr[6:]
253256
else:
254257
if "://" in addr:
255258
raise errors.DockerException(
256259
"Invalid bind address protocol: {0}".format(addr)
257260
)
258261
proto = "https" if tls else "http"
259262

260-
if proto in ("http", "https"):
263+
if proto in ("http", "https", "ssh"):
261264
address_parts = addr.split('/', 1)
262265
host = address_parts[0]
263266
if len(address_parts) == 2:
264267
path = '/' + address_parts[1]
265268
host, port = splitnport(host)
266269

267-
if port is None:
268-
raise errors.DockerException(
269-
"Invalid port: {0}".format(addr)
270-
)
270+
if port is None or port < 0:
271+
if proto == 'ssh':
272+
port = 22
273+
else:
274+
raise errors.DockerException(
275+
"Invalid port: {0}".format(addr)
276+
)
271277

272278
if not host:
273279
host = DEFAULT_HTTP_HOST

0 commit comments

Comments
 (0)