Skip to content

Commit 105efa0

Browse files
committed
Specify when to use tls on Context constructor
Signed-off-by: Ulysses Souza <[email protected]>
1 parent 9b59e49 commit 105efa0

File tree

3 files changed

+11
-6
lines changed

3 files changed

+11
-6
lines changed

docker/context/config.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ def get_tls_dir(name=None, endpoint=""):
7373
return os.path.join(context_dir, "tls")
7474

7575

76-
def get_context_host(path=None):
77-
host = utils.parse_host(path, IS_WINDOWS_PLATFORM)
76+
def get_context_host(path=None, tls=False):
77+
host = utils.parse_host(path, IS_WINDOWS_PLATFORM, tls)
7878
if host == DEFAULT_UNIX_SOCKET:
7979
# remove http+ from default docker socket url
8080
return host.strip("http+")

docker/context/context.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111

1212
class Context:
1313
"""A context."""
14-
def __init__(self, name, orchestrator="swarm", host=None, endpoints=None):
14+
def __init__(self, name, orchestrator="swarm", host=None, endpoints=None,
15+
tls=False):
1516
if not name:
1617
raise Exception("Name not provided")
1718
self.name = name
@@ -22,8 +23,8 @@ def __init__(self, name, orchestrator="swarm", host=None, endpoints=None):
2223
) else orchestrator
2324
self.endpoints = {
2425
default_endpoint: {
25-
"Host": get_context_host(host),
26-
"SkipTLSVerify": False
26+
"Host": get_context_host(host, tls),
27+
"SkipTLSVerify": not tls
2728
}
2829
}
2930
else:
@@ -44,7 +45,7 @@ def set_endpoint(
4445
self, name="docker", host=None, tls_cfg=None,
4546
skip_tls_verify=False, def_namespace=None):
4647
self.endpoints[name] = {
47-
"Host": get_context_host(host),
48+
"Host": get_context_host(host, not skip_tls_verify),
4849
"SkipTLSVerify": skip_tls_verify
4950
}
5051
if def_namespace:

tests/unit/context_test.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ def test_default_in_context_list(self):
3737
def test_get_current_context(self):
3838
assert ContextAPI.get_current_context().Name == "default"
3939

40+
def test_https_host(self):
41+
c = Context("test", host="tcp://testdomain:8080", tls=True)
42+
assert c.Host == "https://testdomain:8080"
43+
4044
def test_context_inspect_without_params(self):
4145
ctx = ContextAPI.inspect_context()
4246
assert ctx["Name"] == "default"

0 commit comments

Comments
 (0)