| 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 base64 |
| 8 |
import typing as t |
| 9 |
|
| 10 |
from ansible.module_utils.common.text.converters import to_bytes |
| 11 |
|
| 12 |
from ansible_collections.community.crypto.plugins.module_utils._crypto.basic import ( |
| 13 |
OpenSSLObjectError, |
| 14 |
) |
| 15 |
from ansible_collections.community.crypto.plugins.module_utils._crypto.module_backends.privatekey import ( |
| 16 |
get_privatekey_argument_spec, |
| 17 |
select_backend, |
| 18 |
) |
| 19 |
from ansible_collections.community.crypto.plugins.plugin_utils._action_module import ( |
| 20 |
ActionModuleBase, |
| 21 |
) |
| 22 |
|
| 23 |
if t.TYPE_CHECKING: |
| 24 |
from ansible_collections.community.crypto.plugins.module_utils._argspec import ( # pragma: no cover |
| 25 |
ArgumentSpec, |
| 26 |
) |
| 27 |
from ansible_collections.community.crypto.plugins.module_utils._crypto.module_backends.privatekey import ( # pragma: no cover |
| 28 |
PrivateKeyBackend, |
| 29 |
) |
| 30 |
from ansible_collections.community.crypto.plugins.plugin_utils._action_module import ( # pragma: no cover |
| 31 |
AnsibleActionModule, |
| 32 |
) |
| 33 |
|
| 34 |
|
| 35 |
class PrivateKeyModule: |
| 36 |
def __init__( |
| 37 |
self, module: AnsibleActionModule, module_backend: PrivateKeyBackend |
| 38 |
) -> None: |
| 39 |
self.module = module |
| 40 |
self.module_backend = module_backend |
| 41 |
self.check_mode = module.check_mode |
| 42 |
self.changed = False |
| 43 |
self.return_current_key: bool = module.params["return_current_key"] |
| 44 |
|
| 45 |
content: str | None = module.params["content"] |
| 46 |
content_base64: bool = module.params["content_base64"] |
| 47 |
if content is not None: |
| 48 |
if content_base64: |
| 49 |
try: |
| 50 |
data = base64.b64decode(content) |
| 51 |
except Exception as e: |
| 52 |
module.fail_json(msg=f"Cannot decode Base64 encoded data: {e}") |
| 53 |
else: |
| 54 |
data = to_bytes(content) |
| 55 |
module_backend.set_existing(privatekey_bytes=data) |
| 56 |
|
| 57 |
def generate(self, module: AnsibleActionModule) -> None: |
| 58 |
"""Generate a keypair.""" |
| 59 |
|
| 60 |
if self.module_backend.needs_regeneration(): |
| 61 |
# Regenerate |
| 62 |
self.module_backend.generate_private_key() |
| 63 |
# Call get_private_key_data() to make sure that exceptions are raised now: |
| 64 |
self.module_backend.get_private_key_data() |
| 65 |
self.changed = True |
| 66 |
elif self.module_backend.needs_conversion(): |
| 67 |
# Convert |
| 68 |
self.module_backend.convert_private_key() |
| 69 |
# Call get_private_key_data() to make sure that exceptions are raised now: |
| 70 |
self.module_backend.get_private_key_data() |
| 71 |
self.changed = True |
| 72 |
|
| 73 |
def dump(self) -> dict[str, t.Any]: |
| 74 |
"""Serialize the object into a dictionary.""" |
| 75 |
result = self.module_backend.dump( |
| 76 |
include_key=self.changed or self.return_current_key |
| 77 |
) |
| 78 |
result["changed"] = self.changed |
| 79 |
return result |
| 80 |
|
| 81 |
|
| 82 |
class ActionModule(ActionModuleBase): |
| 83 |
def setup_module(self) -> tuple[ArgumentSpec, dict[str, t.Any]]: |
| 84 |
argument_spec = get_privatekey_argument_spec() |
| 85 |
argument_spec.argument_spec.update( |
| 86 |
{ |
| 87 |
"content": {"type": "str", "no_log": True}, |
| 88 |
"content_base64": {"type": "bool", "default": False}, |
| 89 |
"return_current_key": {"type": "bool", "default": False}, |
| 90 |
} |
| 91 |
) |
| 92 |
return argument_spec, { |
| 93 |
"supports_check_mode": True, |
| 94 |
} |
| 95 |
|
| 96 |
def run_module(self, module: AnsibleActionModule) -> None: |
| 97 |
module_backend = select_backend(module=module) |
| 98 |
|
| 99 |
try: |
| 100 |
private_key = PrivateKeyModule(module, module_backend) |
| 101 |
private_key.generate(module) |
| 102 |
result = private_key.dump() |
| 103 |
if private_key.return_current_key: |
| 104 |
# In case the module's input (`content`) is returned as `privatekey`: |
| 105 |
# Since `content` is no_log=True, `privatekey`'s value will get replaced by |
| 106 |
# VALUE_SPECIFIED_IN_NO_LOG_PARAMETER. To avoid this, we remove the value of |
| 107 |
# `content` from module.no_log_values. Since we explicitly set |
| 108 |
# `module.no_log = True`, this should be safe. |
| 109 |
module.no_log = True |
| 110 |
try: |
| 111 |
module.no_log_values.remove(module.params["content"]) |
| 112 |
except KeyError: |
| 113 |
pass |
| 114 |
module.params["content"] = "ANSIBLE_NO_LOG_VALUE" |
| 115 |
module.exit_json(**result) |
| 116 |
except OpenSSLObjectError as exc: |
| 117 |
module.fail_json(msg=str(exc)) |