| 1 |
# This code is part of the Ansible collection community.docker, but is an independent component. |
| 2 |
# This particular file, and this file only, is based on the Docker SDK for Python (https://github.com/docker/docker-py/) |
| 3 |
# |
| 4 |
# Copyright (c) 2016-2022 Docker, Inc. |
| 5 |
# |
| 6 |
# It is licensed under the Apache 2.0 license (see LICENSES/Apache-2.0.txt in this collection) |
| 7 |
# SPDX-License-Identifier: Apache-2.0 |
| 8 |
|
| 9 |
# Note that this module util is **PRIVATE** to the collection. It can have breaking changes at any time. |
| 10 |
# Do not use this from other collections or standalone plugins/modules! |
| 11 |
|
| 12 |
from __future__ import annotations |
| 13 |
|
| 14 |
import os |
| 15 |
import typing as t |
| 16 |
|
| 17 |
from . import errors |
| 18 |
from .transport.ssladapter import SSLHTTPAdapter |
| 19 |
|
| 20 |
if t.TYPE_CHECKING: |
| 21 |
from ansible_collections.community.docker.plugins.module_utils._api.api.client import ( |
| 22 |
APIClient, |
| 23 |
) |
| 24 |
|
| 25 |
|
| 26 |
class TLSConfig: |
| 27 |
""" |
| 28 |
TLS configuration. |
| 29 |
|
| 30 |
Args: |
| 31 |
client_cert (tuple of str): Path to client cert, path to client key. |
| 32 |
ca_cert (str): Path to CA cert file. |
| 33 |
verify (bool or str): This can be ``False`` or a path to a CA cert |
| 34 |
file. |
| 35 |
assert_hostname (bool): Verify the hostname of the server. |
| 36 |
|
| 37 |
.. _`SSL version`: |
| 38 |
https://docs.python.org/3.5/library/ssl.html#ssl.PROTOCOL_TLSv1 |
| 39 |
""" |
| 40 |
|
| 41 |
cert: tuple[str, str] | None = None |
| 42 |
ca_cert: str | None = None |
| 43 |
verify: bool | None = None |
| 44 |
|
| 45 |
def __init__( |
| 46 |
self, |
| 47 |
client_cert: tuple[str, str] | None = None, |
| 48 |
ca_cert: str | None = None, |
| 49 |
verify: bool | None = None, |
| 50 |
assert_hostname: bool | None = None, |
| 51 |
): |
| 52 |
# Argument compatibility/mapping with |
| 53 |
# https://docs.docker.com/engine/articles/https/ |
| 54 |
# This diverges from the Docker CLI in that users can specify 'tls' |
| 55 |
# here, but also disable any public/default CA pool verification by |
| 56 |
# leaving verify=False |
| 57 |
|
| 58 |
self.assert_hostname = assert_hostname |
| 59 |
|
| 60 |
# "client_cert" must have both or neither cert/key files. In |
| 61 |
# either case, Alert the user when both are expected, but any are |
| 62 |
# missing. |
| 63 |
|
| 64 |
if client_cert: |
| 65 |
try: |
| 66 |
tls_cert, tls_key = client_cert |
| 67 |
except ValueError: |
| 68 |
raise errors.TLSParameterError( |
| 69 |
"client_cert must be a tuple of (client certificate, key file)" |
| 70 |
) from None |
| 71 |
|
| 72 |
if not (tls_cert and tls_key) or ( |
| 73 |
not os.path.isfile(tls_cert) or not os.path.isfile(tls_key) |
| 74 |
): |
| 75 |
raise errors.TLSParameterError( |
| 76 |
"Path to a certificate and key files must be provided" |
| 77 |
" through the client_cert param" |
| 78 |
) |
| 79 |
self.cert = (tls_cert, tls_key) |
| 80 |
|
| 81 |
# If verify is set, make sure the cert exists |
| 82 |
self.verify = verify |
| 83 |
self.ca_cert = ca_cert |
| 84 |
if self.verify and self.ca_cert and not os.path.isfile(self.ca_cert): |
| 85 |
raise errors.TLSParameterError( |
| 86 |
"Invalid CA certificate provided for `ca_cert`." |
| 87 |
) |
| 88 |
|
| 89 |
def configure_client(self, client: APIClient) -> None: |
| 90 |
""" |
| 91 |
Configure a client with these TLS options. |
| 92 |
""" |
| 93 |
|
| 94 |
if self.verify and self.ca_cert: |
| 95 |
client.verify = self.ca_cert |
| 96 |
else: |
| 97 |
client.verify = self.verify |
| 98 |
|
| 99 |
if self.cert: |
| 100 |
client.cert = self.cert |
| 101 |
|
| 102 |
client.mount( |
| 103 |
"https://", |
| 104 |
SSLHTTPAdapter( |
| 105 |
assert_hostname=self.assert_hostname, |
| 106 |
), |
| 107 |
) |