mirror of
https://github.com/ansible-collections/community.docker.git
synced 2026-04-12 04:31:28 +00:00
Fix TLS handling for contexts.
This commit is contained in:
parent
e02ef61f11
commit
5e225c7a5f
@ -34,7 +34,7 @@ class Context(object):
|
|||||||
"""A context."""
|
"""A context."""
|
||||||
|
|
||||||
def __init__(self, name, orchestrator=None, host=None, endpoints=None,
|
def __init__(self, name, orchestrator=None, host=None, endpoints=None,
|
||||||
tls=False, description=None):
|
skip_tls_verify=False, description=None):
|
||||||
if not name:
|
if not name:
|
||||||
raise Exception("Name not provided")
|
raise Exception("Name not provided")
|
||||||
self.name = name
|
self.name = name
|
||||||
@ -54,8 +54,8 @@ class Context(object):
|
|||||||
|
|
||||||
self.endpoints = {
|
self.endpoints = {
|
||||||
default_endpoint: {
|
default_endpoint: {
|
||||||
"Host": get_context_host(host, tls),
|
"Host": get_context_host(host, skip_tls_verify),
|
||||||
"SkipTLSVerify": not tls
|
"SkipTLSVerify": skip_tls_verify,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
@ -73,9 +73,9 @@ class Context(object):
|
|||||||
continue
|
continue
|
||||||
|
|
||||||
self.endpoints[k]["Host"] = v.get("Host", get_context_host(
|
self.endpoints[k]["Host"] = v.get("Host", get_context_host(
|
||||||
host, tls))
|
host, skip_tls_verify))
|
||||||
self.endpoints[k]["SkipTLSVerify"] = bool(v.get(
|
self.endpoints[k]["SkipTLSVerify"] = bool(v.get(
|
||||||
"SkipTLSVerify", not tls))
|
"SkipTLSVerify", skip_tls_verify))
|
||||||
|
|
||||||
def set_endpoint(
|
def set_endpoint(
|
||||||
self, name="docker", host=None, tls_cfg=None,
|
self, name="docker", host=None, tls_cfg=None,
|
||||||
@ -152,13 +152,13 @@ class Context(object):
|
|||||||
cert = os.path.join(tls_dir, endpoint, filename)
|
cert = os.path.join(tls_dir, endpoint, filename)
|
||||||
elif filename.startswith("key"):
|
elif filename.startswith("key"):
|
||||||
key = os.path.join(tls_dir, endpoint, filename)
|
key = os.path.join(tls_dir, endpoint, filename)
|
||||||
if all([ca_cert, cert, key]):
|
if all([cert, key]) or ca_cert:
|
||||||
verify = None
|
verify = None
|
||||||
if endpoint == "docker" and not self.endpoints["docker"].get(
|
if endpoint == "docker" and not self.endpoints["docker"].get(
|
||||||
"SkipTLSVerify", False):
|
"SkipTLSVerify", False):
|
||||||
verify = True
|
verify = True
|
||||||
certs[endpoint] = TLSConfig(
|
certs[endpoint] = TLSConfig(
|
||||||
client_cert=(cert, key), ca_cert=ca_cert, verify=verify)
|
client_cert=(cert, key) if cert and key else None, ca_cert=ca_cert, verify=verify)
|
||||||
self.tls_cfg = certs
|
self.tls_cfg = certs
|
||||||
self.tls_path = tls_dir
|
self.tls_path = tls_dir
|
||||||
|
|
||||||
|
|||||||
@ -229,7 +229,6 @@ def context_to_json(context, current):
|
|||||||
|
|
||||||
# Create config for the modules
|
# Create config for the modules
|
||||||
module_config['docker_host'] = host_str
|
module_config['docker_host'] = host_str
|
||||||
module_config['tls'] = not to_bool(endpoint.get('SkipTLSVerify'))
|
|
||||||
if context.tls_cfg.get('docker'):
|
if context.tls_cfg.get('docker'):
|
||||||
tls_cfg = context.tls_cfg['docker']
|
tls_cfg = context.tls_cfg['docker']
|
||||||
if tls_cfg.ca_cert:
|
if tls_cfg.ca_cert:
|
||||||
@ -238,7 +237,9 @@ def context_to_json(context, current):
|
|||||||
module_config['client_cert'] = tls_cfg.cert[0]
|
module_config['client_cert'] = tls_cfg.cert[0]
|
||||||
module_config['client_key'] = tls_cfg.cert[1]
|
module_config['client_key'] = tls_cfg.cert[1]
|
||||||
module_config['validate_certs'] = tls_cfg.verify
|
module_config['validate_certs'] = tls_cfg.verify
|
||||||
module_config['tls'] = to_bool(tls_cfg.verify)
|
module_config['tls'] = True
|
||||||
|
else:
|
||||||
|
module_config['tls'] = to_bool(endpoint.get('SkipTLSVerify'))
|
||||||
return {
|
return {
|
||||||
'current': current,
|
'current': current,
|
||||||
'name': context.name,
|
'name': context.name,
|
||||||
@ -285,10 +286,10 @@ def main():
|
|||||||
else:
|
else:
|
||||||
contexts = ContextAPI.contexts()
|
contexts = ContextAPI.contexts()
|
||||||
|
|
||||||
json_contexts = [
|
json_contexts = sorted([
|
||||||
context_to_json(context, context.name == current_context_name)
|
context_to_json(context, context.name == current_context_name)
|
||||||
for context in contexts
|
for context in contexts
|
||||||
]
|
], key=lambda entry: entry['name'])
|
||||||
|
|
||||||
module.exit_json(
|
module.exit_json(
|
||||||
changed=False,
|
changed=False,
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user