Skip to content

Commit bb94fe7

Browse files
committed
Support unspecified protocol in base_url when using TLS
(assume HTTPS) Signed-off-by: Joffrey F <[email protected]>
1 parent 446e6d0 commit bb94fe7

File tree

3 files changed

+11
-6
lines changed

3 files changed

+11
-6
lines changed

docker/client.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,17 @@ def __init__(self, base_url=None, version=None,
4545
timeout=constants.DEFAULT_TIMEOUT_SECONDS, tls=False):
4646
super(Client, self).__init__()
4747

48-
if tls and (not base_url or not base_url.startswith('https://')):
48+
if tls and not base_url:
4949
raise errors.TLSParameterError(
50-
'If using TLS, the base_url argument must begin with '
51-
'"https://".')
50+
'If using TLS, the base_url argument must be provided.'
51+
)
5252

5353
self.base_url = base_url
5454
self.timeout = timeout
5555

5656
self._auth_configs = auth.load_config()
5757

58-
base_url = utils.parse_host(base_url, sys.platform)
58+
base_url = utils.parse_host(base_url, sys.platform, tls=bool(tls))
5959
if base_url.startswith('http+unix://'):
6060
self._custom_adapter = unixconn.UnixAdapter(base_url, timeout)
6161
self.mount('http+docker://', self._custom_adapter)

docker/utils/utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ def parse_repository_tag(repo_name):
345345
# fd:// protocol unsupported (for obvious reasons)
346346
# Added support for http and https
347347
# Protocol translation: tcp -> http, unix -> http+unix
348-
def parse_host(addr, platform=None):
348+
def parse_host(addr, platform=None, tls=False):
349349
proto = "http+unix"
350350
host = DEFAULT_HTTP_HOST
351351
port = None
@@ -381,7 +381,7 @@ def parse_host(addr, platform=None):
381381
raise errors.DockerException(
382382
"Invalid bind address protocol: {0}".format(addr)
383383
)
384-
proto = "http"
384+
proto = "https" if tls else "http"
385385

386386
if proto != "http+unix" and ":" in addr:
387387
host_parts = addr.split(':')

tests/unit/utils_test.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,11 @@ def test_parse_host_empty_value(self):
360360

361361
assert parse_host(val, 'win32') == tcp_port
362362

363+
def test_parse_host_tls(self):
364+
host_value = 'myhost.docker.net:3348'
365+
expected_result = 'https://myhost.docker.net:3348'
366+
self.assertEqual(parse_host(host_value, None, True), expected_result)
367+
363368

364369
class ParseRepositoryTagTest(base.BaseTestCase):
365370
sha = 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855'

0 commit comments

Comments
 (0)