Debian Codegraph @

ansible_collections/community/crypto/plugins/module_utils/_cryptography_dep.py

raw
1 # Copyright (c) 2025 Ansible project
2 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
3 # SPDX-License-Identifier: GPL-3.0-or-later
4
5 # Note that this module util is **PRIVATE** to the collection. It can have breaking changes at any time.
6 # Do not use this from other collections or standalone plugins/modules!
7
8 """
9 Module utils for cryptography requirements.
10
11 Must be kept in sync with plugins/doc_fragments/cryptography_dep.py.
12 """
13
14 from __future__ import annotations
15
16 import traceback
17 import typing as t
18
19 from ansible.module_utils.basic import missing_required_lib
20
21 from ansible_collections.community.crypto.plugins.module_utils._version import (
22 LooseVersion,
23 )
24
25 if t.TYPE_CHECKING:
26 from ansible.module_utils.basic import AnsibleModule # pragma: no cover
27
28 from ansible_collections.community.crypto.plugins.plugin_utils._action_module import ( # pragma: no cover
29 AnsibleActionModule,
30 )
31 from ansible_collections.community.crypto.plugins.plugin_utils._filter_module import ( # pragma: no cover
32 FilterModuleMock,
33 )
34
35 GeneralAnsibleModule = t.Union[ # noqa: UP007
36 AnsibleModule, AnsibleActionModule, FilterModuleMock
37 ] # pragma: no cover
38
39
40 _CRYPTOGRAPHY_IMP_ERR: str | None = None # pylint: disable=invalid-name
41 _CRYPTOGRAPHY_FILE: str | None = None # pylint: disable=invalid-name
42 try:
43 import cryptography
44 from cryptography import x509 # noqa: F401, pylint: disable=unused-import
45
46 except ImportError:
47 _CRYPTOGRAPHY_IMP_ERR = traceback.format_exc() # pylint: disable=invalid-name
48 CRYPTOGRAPHY_FOUND = False
49 CRYPTOGRAPHY_VERSION = LooseVersion("0.0") # pylint: disable=invalid-name
50 else:
51 CRYPTOGRAPHY_FOUND = True
52 # pylint: disable-next=invalid-name
53 CRYPTOGRAPHY_VERSION = LooseVersion(cryptography.__version__)
54 _CRYPTOGRAPHY_FILE = cryptography.__file__ # pylint: disable=invalid-name
55
56
57 # Corresponds to the community.crypto.cryptography_dep.minimum doc fragment
58 COLLECTION_MINIMUM_CRYPTOGRAPHY_VERSION = "3.3"
59
60
61 def assert_required_cryptography_version(
62 module: GeneralAnsibleModule,
63 *,
64 minimum_cryptography_version: str = COLLECTION_MINIMUM_CRYPTOGRAPHY_VERSION,
65 ) -> None:
66 if not CRYPTOGRAPHY_FOUND:
67 module.fail_json(
68 msg=missing_required_lib(f"cryptography >= {minimum_cryptography_version}"),
69 exception=_CRYPTOGRAPHY_IMP_ERR,
70 )
71 if LooseVersion(minimum_cryptography_version) > CRYPTOGRAPHY_VERSION:
72 module.fail_json(
73 msg=(
74 f"Cannot detect the required Python library cryptography (>= {minimum_cryptography_version})."
75 f" Only found a too old version ({CRYPTOGRAPHY_VERSION}) at {_CRYPTOGRAPHY_FILE}."
76 ),
77 )
78
79
80 __all__ = (
81 "COLLECTION_MINIMUM_CRYPTOGRAPHY_VERSION",
82 "CRYPTOGRAPHY_FOUND",
83 "CRYPTOGRAPHY_VERSION",
84 "assert_required_cryptography_version",
85 )