| 1 |
#!/usr/bin/python |
| 2 |
# Copyright (c) 2022, Felix Fontein <[email protected]> |
| 3 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 4 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 5 |
|
| 6 |
from __future__ import annotations |
| 7 |
|
| 8 |
DOCUMENTATION = r""" |
| 9 |
module: openssl_privatekey_convert |
| 10 |
short_description: Convert OpenSSL private keys |
| 11 |
version_added: 2.1.0 |
| 12 |
description: |
| 13 |
- This module allows one to convert OpenSSL private keys. |
| 14 |
- The default mode for the private key file will be V(0600) if O(mode) is not explicitly set. |
| 15 |
author: |
| 16 |
- Felix Fontein (@felixfontein) |
| 17 |
extends_documentation_fragment: |
| 18 |
- ansible.builtin.files |
| 19 |
- community.crypto._attributes |
| 20 |
- community.crypto._attributes.files |
| 21 |
- community.crypto._module_privatekey_convert |
| 22 |
attributes: |
| 23 |
check_mode: |
| 24 |
support: full |
| 25 |
safe_file_operations: |
| 26 |
support: full |
| 27 |
options: |
| 28 |
dest_path: |
| 29 |
description: |
| 30 |
- Name of the file in which the generated TLS/SSL private key will be written. It will have V(0600) mode if O(mode) |
| 31 |
is not explicitly set. |
| 32 |
type: path |
| 33 |
required: true |
| 34 |
backup: |
| 35 |
description: |
| 36 |
- Create a backup file including a timestamp so you can get the original private key back if you overwrote it with a |
| 37 |
new one by accident. |
| 38 |
type: bool |
| 39 |
default: false |
| 40 |
seealso: [] |
| 41 |
""" |
| 42 |
|
| 43 |
EXAMPLES = r""" |
| 44 |
--- |
| 45 |
- name: Convert private key to PKCS8 format with passphrase |
| 46 |
community.crypto.openssl_privatekey_convert: |
| 47 |
src_path: /etc/ssl/private/ansible.com.pem |
| 48 |
dest_path: /etc/ssl/private/ansible.com.key |
| 49 |
dest_passphrase: '{{ private_key_passphrase }}' |
| 50 |
format: pkcs8 |
| 51 |
""" |
| 52 |
|
| 53 |
RETURN = r""" |
| 54 |
backup_file: |
| 55 |
description: Name of backup file created. |
| 56 |
returned: changed and if O(backup) is V(true) |
| 57 |
type: str |
| 58 |
sample: /path/to/privatekey.pem.2019-03-09@11:22~ |
| 59 |
""" |
| 60 |
|
| 61 |
import os |
| 62 |
import typing as t |
| 63 |
|
| 64 |
from ansible_collections.community.crypto.plugins.module_utils._crypto.basic import ( |
| 65 |
OpenSSLObjectError, |
| 66 |
) |
| 67 |
from ansible_collections.community.crypto.plugins.module_utils._crypto.module_backends.privatekey_convert import ( |
| 68 |
get_privatekey_argument_spec, |
| 69 |
select_backend, |
| 70 |
) |
| 71 |
from ansible_collections.community.crypto.plugins.module_utils._crypto.support import ( |
| 72 |
OpenSSLObject, |
| 73 |
) |
| 74 |
from ansible_collections.community.crypto.plugins.module_utils._io import ( |
| 75 |
load_file_if_exists, |
| 76 |
write_file, |
| 77 |
) |
| 78 |
|
| 79 |
if t.TYPE_CHECKING: |
| 80 |
from ansible.module_utils.basic import AnsibleModule # pragma: no cover |
| 81 |
|
| 82 |
from ansible_collections.community.crypto.plugins.module_utils._crypto.module_backends.privatekey_convert import ( # pragma: no cover |
| 83 |
PrivateKeyConvertBackend, |
| 84 |
) |
| 85 |
|
| 86 |
|
| 87 |
class PrivateKeyConvertModule(OpenSSLObject): |
| 88 |
def __init__( |
| 89 |
self, module: AnsibleModule, module_backend: PrivateKeyConvertBackend |
| 90 |
) -> None: |
| 91 |
super().__init__( |
| 92 |
path=module.params["dest_path"], |
| 93 |
state="present", |
| 94 |
force=False, |
| 95 |
check_mode=module.check_mode, |
| 96 |
) |
| 97 |
self.module_backend = module_backend |
| 98 |
|
| 99 |
self.backup: bool = module.params["backup"] |
| 100 |
self.backup_file: str | None = None |
| 101 |
|
| 102 |
module.params["path"] = module.params["dest_path"] |
| 103 |
if module.params["mode"] is None: |
| 104 |
module.params["mode"] = "0600" |
| 105 |
|
| 106 |
module_backend.set_existing_destination( |
| 107 |
privatekey_bytes=load_file_if_exists(path=self.path, module=module) |
| 108 |
) |
| 109 |
|
| 110 |
def generate(self, module: AnsibleModule) -> None: |
| 111 |
"""Do conversion.""" |
| 112 |
|
| 113 |
if self.module_backend.needs_conversion(): |
| 114 |
# Convert |
| 115 |
privatekey_data = self.module_backend.get_private_key_data() |
| 116 |
if privatekey_data is None: |
| 117 |
raise AssertionError( |
| 118 |
"Contract violation: privatekey_data is None" |
| 119 |
) # pragma: no cover |
| 120 |
if not self.check_mode: |
| 121 |
if self.backup: |
| 122 |
self.backup_file = module.backup_local(self.path) |
| 123 |
write_file(module=module, content=privatekey_data, default_mode=0o600) |
| 124 |
self.changed = True |
| 125 |
|
| 126 |
file_args = module.load_file_common_arguments(module.params) |
| 127 |
if module.check_file_absent_if_check_mode(file_args["path"]): |
| 128 |
self.changed = True |
| 129 |
else: |
| 130 |
self.changed = module.set_fs_attributes_if_different( |
| 131 |
file_args, self.changed |
| 132 |
) |
| 133 |
|
| 134 |
def dump(self) -> dict[str, t.Any]: |
| 135 |
"""Serialize the object into a dictionary.""" |
| 136 |
|
| 137 |
result = self.module_backend.dump() |
| 138 |
result["changed"] = self.changed |
| 139 |
if self.backup_file: |
| 140 |
result["backup_file"] = self.backup_file |
| 141 |
|
| 142 |
return result |
| 143 |
|
| 144 |
|
| 145 |
def main() -> t.NoReturn: |
| 146 |
argument_spec = get_privatekey_argument_spec() |
| 147 |
argument_spec.argument_spec.update( |
| 148 |
{ |
| 149 |
"dest_path": {"type": "path", "required": True}, |
| 150 |
"backup": {"type": "bool", "default": False}, |
| 151 |
} |
| 152 |
) |
| 153 |
module = argument_spec.create_ansible_module( |
| 154 |
supports_check_mode=True, |
| 155 |
add_file_common_args=True, |
| 156 |
) |
| 157 |
|
| 158 |
base_dir = os.path.dirname(module.params["dest_path"]) or "." |
| 159 |
if not os.path.isdir(base_dir): |
| 160 |
module.fail_json( |
| 161 |
name=base_dir, |
| 162 |
msg=f"The directory {base_dir} does not exist or the file is not a directory", |
| 163 |
) |
| 164 |
|
| 165 |
module_backend = select_backend(module=module) |
| 166 |
|
| 167 |
try: |
| 168 |
private_key = PrivateKeyConvertModule(module, module_backend) |
| 169 |
|
| 170 |
private_key.generate(module) |
| 171 |
|
| 172 |
result = private_key.dump() |
| 173 |
module.exit_json(**result) |
| 174 |
except OpenSSLObjectError as exc: |
| 175 |
module.fail_json(msg=str(exc)) |
| 176 |
|
| 177 |
|
| 178 |
if __name__ == "__main__": |
| 179 |
main() |