| 1 |
# Copyright (c) 2020, 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 |
from __future__ import annotations |
| 6 |
|
| 7 |
import typing as t |
| 8 |
from collections.abc import Sequence, Mapping |
| 9 |
|
| 10 |
from ansible.module_utils.common.text.converters import to_native |
| 11 |
from ansible.utils.display import Display |
| 12 |
|
| 13 |
from ansible_collections.community.sops.plugins.module_utils.sops import Sops, get_sops_argument_spec |
| 14 |
|
| 15 |
from ansible_collections.community.sops.plugins.plugin_utils.action_module import ActionModuleBase, ArgumentSpec |
| 16 |
|
| 17 |
try: |
| 18 |
from ansible.template import trust_as_template as _trust_as_template |
| 19 |
HAS_DATATAGGING = True |
| 20 |
except ImportError: |
| 21 |
HAS_DATATAGGING = False |
| 22 |
|
| 23 |
try: |
| 24 |
from ansible.plugins.action import VariableLayer # type: ignore[attr-defined] |
| 25 |
HAS_REGISTER_HOST_VARIABLES = True |
| 26 |
except ImportError: |
| 27 |
HAS_REGISTER_HOST_VARIABLES = False |
| 28 |
|
| 29 |
if t.TYPE_CHECKING: |
| 30 |
from ansible_collections.community.sops.plugins.plugin_utils.action_module import AnsibleActionModule |
| 31 |
|
| 32 |
display = Display() |
| 33 |
|
| 34 |
|
| 35 |
def _make_safe(value: t.Any) -> t.Any: |
| 36 |
if HAS_DATATAGGING and isinstance(value, str): |
| 37 |
return _trust_as_template(value) |
| 38 |
return value |
| 39 |
|
| 40 |
|
| 41 |
class ActionModule(ActionModuleBase): |
| 42 |
|
| 43 |
def _load(self, filename: str, module: AnsibleActionModule) -> dict: |
| 44 |
def get_option_value(argument_name): |
| 45 |
return module.params.get(argument_name) |
| 46 |
|
| 47 |
output = Sops.decrypt(filename, display=display, get_option_value=get_option_value) |
| 48 |
|
| 49 |
data = self._loader.load(output, file_name=filename, show_content=False) |
| 50 |
if not data: |
| 51 |
data = dict() |
| 52 |
if not isinstance(data, dict): |
| 53 |
# Should not happen with sops-encrypted files |
| 54 |
raise Exception('{0} must be stored as a dictionary/hash'.format(to_native(filename))) |
| 55 |
return data |
| 56 |
|
| 57 |
def _evaluate(self, value): |
| 58 |
if isinstance(value, str): |
| 59 |
# must come *before* Sequence, as strings are also instances of Sequence |
| 60 |
return self._templar.template(_make_safe(value)) |
| 61 |
if isinstance(value, Sequence): |
| 62 |
return [self._evaluate(v) for v in value] |
| 63 |
if isinstance(value, Mapping): |
| 64 |
return dict((k, self._evaluate(v)) for k, v in value.items()) |
| 65 |
return value |
| 66 |
|
| 67 |
def _make_safe(self, value): |
| 68 |
if isinstance(value, str): |
| 69 |
# must come *before* Sequence, as strings are also instances of Sequence |
| 70 |
return _make_safe(value) |
| 71 |
if isinstance(value, Sequence): |
| 72 |
return [self._make_safe(v) for v in value] |
| 73 |
if isinstance(value, Mapping): |
| 74 |
return dict((k, self._make_safe(v)) for k, v in value.items()) |
| 75 |
return value |
| 76 |
|
| 77 |
@staticmethod |
| 78 |
def setup_module(): |
| 79 |
argument_spec = ArgumentSpec( |
| 80 |
argument_spec=dict( |
| 81 |
file=dict(type='path', required=True), |
| 82 |
name=dict(type='str'), |
| 83 |
expressions=dict(type='str', default='ignore', choices=['ignore', 'evaluate-on-load', 'lazy-evaluation']), |
| 84 |
return_method=dict(type='str', default='auto', choices=['auto', 'facts-only', 'vars-only']), |
| 85 |
), |
| 86 |
) |
| 87 |
argument_spec.argument_spec.update(get_sops_argument_spec()) |
| 88 |
return argument_spec, {} |
| 89 |
|
| 90 |
def run_module(self, module): |
| 91 |
expressions = module.params['expressions'] |
| 92 |
if expressions == 'lazy-evaluation' and not HAS_DATATAGGING: |
| 93 |
module.fail_json(msg='expressions=lazy-evaluation requires ansible-core 2.19+ with Data Tagging support.') |
| 94 |
|
| 95 |
return_method_str = module.params['return_method'] |
| 96 |
if return_method_str == 'auto': |
| 97 |
return_as_facts = not HAS_REGISTER_HOST_VARIABLES |
| 98 |
else: |
| 99 |
return_as_facts = return_method_str == 'facts-only' |
| 100 |
if not HAS_REGISTER_HOST_VARIABLES and not return_as_facts: |
| 101 |
module.fail_json(msg='return_method=vars-only requires ansible-core 2.21+') |
| 102 |
|
| 103 |
data = {} |
| 104 |
files = [] |
| 105 |
try: |
| 106 |
filename = self._find_needle('vars', module.params['file']) |
| 107 |
data.update(self._load(filename, module)) |
| 108 |
files.append(filename) |
| 109 |
except Exception as e: |
| 110 |
module.fail_json(msg=to_native(e)) |
| 111 |
|
| 112 |
name = module.params['name'] |
| 113 |
if name is None: |
| 114 |
value = data |
| 115 |
else: |
| 116 |
value = {name: data} |
| 117 |
|
| 118 |
if expressions == 'evaluate-on-load': |
| 119 |
value = self._evaluate(value) |
| 120 |
|
| 121 |
if expressions == 'lazy-evaluation': |
| 122 |
value = self._make_safe(value) |
| 123 |
|
| 124 |
result = { |
| 125 |
'ansible_included_var_files': files, |
| 126 |
'_ansible_no_log': True, |
| 127 |
} |
| 128 |
|
| 129 |
if return_as_facts: |
| 130 |
result['ansible_facts'] = value |
| 131 |
else: |
| 132 |
self.register_host_variables(variables=value, layer=VariableLayer.INCLUDE_VARS) |
| 133 |
|
| 134 |
module.exit_json(**result) |