| 1 |
# Copyright (c) 2026 Felix Fontein <[email protected]> |
| 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 |
from __future__ import annotations |
| 9 |
|
| 10 |
import os |
| 11 |
import typing as t |
| 12 |
|
| 13 |
from ansible.module_utils.common.text.converters import to_bytes as _to_bytes |
| 14 |
from ansible.module_utils.common.validation import check_type_bool as _check_type_bool |
| 15 |
from ansible.module_utils.common.validation import check_type_dict as _check_type_dict |
| 16 |
from ansible.module_utils.common.validation import check_type_int as _check_type_int |
| 17 |
from ansible.module_utils.common.validation import check_type_float as _check_type_float |
| 18 |
from ansible.module_utils.common.validation import check_type_list as _check_type_list |
| 19 |
from ansible.module_utils.common.validation import check_type_path as _check_type_path |
| 20 |
from ansible.module_utils.common.validation import check_type_str as _check_type_str |
| 21 |
from ansible.utils.path import unfrackpath as _unfrackpath |
| 22 |
|
| 23 |
from ansible_collections.community.sops.plugins.module_utils.sops import get_sops_argument_spec as _get_sops_argument_spec |
| 24 |
|
| 25 |
if t.TYPE_CHECKING: |
| 26 |
from collections.abc import Callable, Mapping # pragma: no cover |
| 27 |
|
| 28 |
|
| 29 |
def wrap_get_option_value( |
| 30 |
get_option_value: Callable[[str], t.Any], |
| 31 |
*, |
| 32 |
overrides: Mapping[str, t.Any], |
| 33 |
) -> Callable[[str], t.Any]: |
| 34 |
def new_get_option_value(option: str) -> t.Any: |
| 35 |
if option in overrides: |
| 36 |
return overrides[option] |
| 37 |
return get_option_value(option) |
| 38 |
|
| 39 |
return new_get_option_value |
| 40 |
|
| 41 |
|
| 42 |
def wrap_get_option_value_plugin_path( |
| 43 |
get_option_value: Callable[[str], t.Any], |
| 44 |
*, |
| 45 |
sops_binary: t.Any, |
| 46 |
sops_binary_origin: str | None = "Direct", |
| 47 |
) -> Callable[[str], t.Any]: |
| 48 |
if isinstance(sops_binary, str): |
| 49 |
candidate = sops_binary |
| 50 |
# ansible.config.manager.resolve_path() handles {{CWD}}: |
| 51 |
if "{{CWD}}" in candidate: |
| 52 |
candidate = candidate.replace("{{CWD}}", os.getcwd()) |
| 53 |
basedir = sops_binary_origin if sops_binary_origin and os.path.isabs(sops_binary_origin) and os.path.exists(_to_bytes(sops_binary_origin)) else None |
| 54 |
candidate = _unfrackpath(candidate, follow=False, basedir=basedir) |
| 55 |
# Check whether the candidate is a file: |
| 56 |
if os.path.isfile(candidate): |
| 57 |
sops_binary = candidate |
| 58 |
else: |
| 59 |
# If not, fall back to what a module would do with the path |
| 60 |
sops_binary = os.path.expanduser(os.path.expandvars(sops_binary)) |
| 61 |
overrides = {"sops_binary": sops_binary} |
| 62 |
return wrap_get_option_value(get_option_value, overrides=overrides) |
| 63 |
|
| 64 |
|
| 65 |
_TYPE_CHECKS = { |
| 66 |
"dict": _check_type_dict, |
| 67 |
"bool": _check_type_bool, |
| 68 |
"int": _check_type_int, |
| 69 |
"float": _check_type_float, |
| 70 |
"str": lambda value: _check_type_str(value, allow_conversion=False), |
| 71 |
"path": lambda value: _check_type_path(_check_type_str(value, allow_conversion=False)), |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
def _ensure_type_impl(value: t.Any, *, option_type: str) -> t.Any: |
| 76 |
return _TYPE_CHECKS[option_type](value) |
| 77 |
|
| 78 |
|
| 79 |
def _ensure_type(value: t.Any, *, option_type: str, elements_type: str | None, sensitive: bool) -> t.Any: |
| 80 |
if value is None: |
| 81 |
return None |
| 82 |
if option_type != "list": |
| 83 |
return _ensure_type_impl(value, option_type=option_type) |
| 84 |
value = _check_type_list(value) |
| 85 |
if elements_type is None: |
| 86 |
# Our options do not have a list without 'elements', so this is unreachable. |
| 87 |
# We still want to keep this code though... |
| 88 |
return value # pragma: no cover |
| 89 |
return [_ensure_type_impl(v, option_type=elements_type) for v in value] |
| 90 |
|
| 91 |
|
| 92 |
def wrap_get_option_value_check_types( |
| 93 |
get_option_value: Callable[[str], t.Any], |
| 94 |
*, |
| 95 |
add_encrypt_specific: bool, |
| 96 |
) -> Callable[[str], t.Any]: |
| 97 |
overrides: dict[str, t.Any] = {} |
| 98 |
for option, data in _get_sops_argument_spec(add_encrypt_specific=add_encrypt_specific).items(): |
| 99 |
value = get_option_value(option) |
| 100 |
try: |
| 101 |
value = _ensure_type( |
| 102 |
value, |
| 103 |
option_type=data.get("type", "str"), |
| 104 |
elements_type=data.get("elements"), |
| 105 |
sensitive=data.get("no_log", False), |
| 106 |
) |
| 107 |
except TypeError as exc: |
| 108 |
raise ValueError(f"option {option} has invalid value: {exc}") |
| 109 |
overrides[option] = value |
| 110 |
return wrap_get_option_value(get_option_value, overrides=overrides) |