| 1 |
#!/usr/bin/python |
| 2 |
# Copyright (c) 2020, 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_pipe |
| 10 |
short_description: Generate OpenSSL private keys without disk access |
| 11 |
version_added: 1.3.0 |
| 12 |
description: |
| 13 |
- This module allows one to (re)generate OpenSSL private keys without disk access. |
| 14 |
- This allows to read and write keys to vaults without having to write intermediate versions to disk. |
| 15 |
- Make sure to not write the result of this module into logs or to the console, as it contains private key data! Use the |
| 16 |
C(no_log) task option to be sure. |
| 17 |
- Note that this module is implemented as an L(action plugin,https://docs.ansible.com/ansible/latest/plugins/action.html) |
| 18 |
and will always be executed on the controller. |
| 19 |
author: |
| 20 |
- Yanis Guenane (@Spredzy) |
| 21 |
- Felix Fontein (@felixfontein) |
| 22 |
extends_documentation_fragment: |
| 23 |
- community.crypto._attributes |
| 24 |
- community.crypto._attributes.flow |
| 25 |
- community.crypto._module_privatekey |
| 26 |
attributes: |
| 27 |
action: |
| 28 |
support: full |
| 29 |
async: |
| 30 |
support: none |
| 31 |
details: |
| 32 |
- This action runs completely on the controller. |
| 33 |
check_mode: |
| 34 |
support: full |
| 35 |
details: |
| 36 |
- Since community.crypto 3.0.0, the module ignores check mode and always behaves as if check mode is not active. |
| 37 |
options: |
| 38 |
content: |
| 39 |
description: |
| 40 |
- The current private key data. |
| 41 |
- Needed for idempotency. If not provided, the module will always return a change, and all idempotence-related options |
| 42 |
are ignored. |
| 43 |
type: str |
| 44 |
content_base64: |
| 45 |
description: |
| 46 |
- Set to V(true) if the content is base64 encoded. |
| 47 |
type: bool |
| 48 |
default: false |
| 49 |
return_current_key: |
| 50 |
description: |
| 51 |
- Set to V(true) to return the current private key when the module did not generate a new one. |
| 52 |
- Note that in case of check mode, when this option is not set to V(true), the module always returns the current key |
| 53 |
(if it was provided) and Ansible will replace it by C(VALUE_SPECIFIED_IN_NO_LOG_PARAMETER). |
| 54 |
type: bool |
| 55 |
default: false |
| 56 |
regenerate: |
| 57 |
description: |
| 58 |
- Allows to configure in which situations the module is allowed to regenerate private keys. The module will always generate |
| 59 |
a new key if the destination file does not exist. |
| 60 |
- By default, the key will be regenerated when it does not match the module's options, except when the key cannot be |
| 61 |
read or the passphrase does not match. Please note that this B(changed) for Ansible 2.10. For Ansible 2.9, the behavior |
| 62 |
was as if V(full_idempotence) is specified. |
| 63 |
- If set to V(never), the module will fail if the key cannot be read or the passphrase is not matching, and will never |
| 64 |
regenerate an existing key. |
| 65 |
- If set to V(fail), the module will fail if the key does not correspond to the module's options. |
| 66 |
- If set to V(partial_idempotence), the key will be regenerated if it does not conform to the module's options. The |
| 67 |
key is B(not) regenerated if it cannot be read (broken file), the key is protected by an unknown passphrase, or when |
| 68 |
they key is not protected by a passphrase, but a passphrase is specified. |
| 69 |
- If set to V(full_idempotence), the key will be regenerated if it does not conform to the module's options. This is |
| 70 |
also the case if the key cannot be read (broken file), the key is protected by an unknown passphrase, or when they |
| 71 |
key is not protected by a passphrase, but a passphrase is specified. Make sure you have a B(backup) when using this |
| 72 |
option! |
| 73 |
- If set to V(always), the module will always regenerate the key. |
| 74 |
- Note that if O(format_mismatch) is set to V(convert) and everything matches except the format, the key will always |
| 75 |
be converted, except if O(regenerate) is set to V(always). |
| 76 |
seealso: |
| 77 |
- module: community.crypto.openssl_privatekey |
| 78 |
- module: community.crypto.openssl_privatekey_info |
| 79 |
""" |
| 80 |
|
| 81 |
EXAMPLES = r""" |
| 82 |
--- |
| 83 |
- name: Generate an OpenSSL private key with the default values (4096 bits, RSA) |
| 84 |
community.crypto.openssl_privatekey_pipe: |
| 85 |
register: output |
| 86 |
no_log: true # make sure that private key data is not accidentally revealed in logs! |
| 87 |
- name: Show generated key |
| 88 |
ansible.builtin.debug: |
| 89 |
msg: "{{ output.privatekey }}" |
| 90 |
# DO NOT OUTPUT KEY MATERIAL TO CONSOLE OR LOGS IN PRODUCTION! |
| 91 |
|
| 92 |
|
| 93 |
# The following example needs CNCF SOPS (https://github.com/getsops/sops) set up and |
| 94 |
# the community.sops collection installed. See also |
| 95 |
# https://docs.ansible.com/ansible/latest/collections/community/sops/docsite/guide.html |
| 96 |
|
| 97 |
- name: Generate or update a CNCF SOPS encrypted key |
| 98 |
block: |
| 99 |
- name: Update SOPS-encrypted key with the community.sops collection |
| 100 |
community.crypto.openssl_privatekey_pipe: |
| 101 |
content: "{{ lookup('community.sops.sops', 'private_key.pem.sops') }}" |
| 102 |
size: 2048 |
| 103 |
register: output |
| 104 |
no_log: true # make sure that private key data is not accidentally revealed in logs! |
| 105 |
|
| 106 |
- name: Update encrypted key when openssl_privatekey_pipe reported a change |
| 107 |
community.sops.sops_encrypt: |
| 108 |
path: private_key.pem.sops |
| 109 |
content_text: "{{ output.privatekey }}" |
| 110 |
when: output is changed |
| 111 |
always: |
| 112 |
- name: Make sure that output (which contains the private key) is overwritten |
| 113 |
ansible.builtin.set_fact: |
| 114 |
output: '' |
| 115 |
""" |
| 116 |
|
| 117 |
RETURN = r""" |
| 118 |
size: |
| 119 |
description: Size (in bits) of the TLS/SSL private key. |
| 120 |
returned: changed or success |
| 121 |
type: int |
| 122 |
sample: 4096 |
| 123 |
type: |
| 124 |
description: Algorithm used to generate the TLS/SSL private key. |
| 125 |
returned: changed or success |
| 126 |
type: str |
| 127 |
sample: RSA |
| 128 |
curve: |
| 129 |
description: Elliptic curve used to generate the TLS/SSL private key. |
| 130 |
returned: changed or success, and O(type) is V(ECC) |
| 131 |
type: str |
| 132 |
sample: secp256r1 |
| 133 |
fingerprint: |
| 134 |
description: |
| 135 |
- The fingerprint of the public key. Fingerprint will be generated for each C(hashlib.algorithms) available. |
| 136 |
returned: changed or success |
| 137 |
type: dict |
| 138 |
sample: |
| 139 |
md5: "84:75:71:72:8d:04:b5:6c:4d:37:6d:66:83:f5:4c:29" |
| 140 |
sha1: "51:cc:7c:68:5d:eb:41:43:88:7e:1a:ae:c7:f8:24:72:ee:71:f6:10" |
| 141 |
sha224: "b1:19:a6:6c:14:ac:33:1d:ed:18:50:d3:06:5c:b2:32:91:f1:f1:52:8c:cb:d5:75:e9:f5:9b:46" |
| 142 |
sha256: "41:ab:c7:cb:d5:5f:30:60:46:99:ac:d4:00:70:cf:a1:76:4f:24:5d:10:24:57:5d:51:6e:09:97:df:2f:de:c7" |
| 143 |
sha384: "85:39:50:4e:de:d9:19:33:40:70:ae:10:ab:59:24:19:51:c3:a2:e4:0b:1c:b1:6e:dd:b3:0c:d9:9e:6a:46:af:da:18:f8:ef:ae:2e:c0:9a:75:2c:9b:b3:0f:3a:5f:3d" |
| 144 |
sha512: "fd:ed:5e:39:48:5f:9f:fe:7f:25:06:3f:79:08:cd:ee:a5:e7:b3:3d:13:82:87:1f:84:e1:f5:c7:28:77:53:94:86:56:38:69:f0:d9:35:22:01:1e:a6:60:...:0f:9b" |
| 145 |
privatekey: |
| 146 |
description: |
| 147 |
- The generated private key's content. |
| 148 |
- Please note that if the result is not changed, the current private key will only be returned if the O(return_current_key) |
| 149 |
option is set to V(true). |
| 150 |
- Will be Base64-encoded if the key is in raw format. |
| 151 |
returned: changed, or O(return_current_key) is V(true) |
| 152 |
type: str |
| 153 |
""" |