| 1 |
#!/usr/bin/python |
| 2 |
# Copyright (c) 2018, David Kainz <[email protected]> <[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: openssh_keypair |
| 10 |
author: "David Kainz (@lolcube)" |
| 11 |
short_description: Generate OpenSSH private and public keys |
| 12 |
description: |
| 13 |
- This module allows one to (re)generate OpenSSH private and public keys. It uses ssh-keygen to generate keys. One can generate |
| 14 |
V(rsa), V(dsa), V(rsa1), V(ed25519) or V(ecdsa) private keys. |
| 15 |
requirements: |
| 16 |
- ssh-keygen (if O(backend=openssh)) |
| 17 |
- cryptography >= 3.3 (if O(backend=cryptography)) |
| 18 |
- bcrypt (if O(backend=cryptography) and O(passphrase) is used) |
| 19 |
extends_documentation_fragment: |
| 20 |
- ansible.builtin.files |
| 21 |
- community.crypto._attributes |
| 22 |
- community.crypto._attributes.files |
| 23 |
attributes: |
| 24 |
check_mode: |
| 25 |
support: full |
| 26 |
diff_mode: |
| 27 |
support: full |
| 28 |
safe_file_operations: |
| 29 |
support: full |
| 30 |
idempotent: |
| 31 |
support: partial |
| 32 |
details: |
| 33 |
- The module is not idempotent if O(force=true) or O(regenerate=always). |
| 34 |
options: |
| 35 |
state: |
| 36 |
description: |
| 37 |
- Whether the private and public keys should exist or not, taking action if the state is different from what is stated. |
| 38 |
type: str |
| 39 |
default: present |
| 40 |
choices: [present, absent] |
| 41 |
size: |
| 42 |
description: |
| 43 |
- 'Specifies the number of bits in the private key to create. For RSA keys, the minimum size is 1024 bits and the default |
| 44 |
is 4096 bits. Generally, 2048 bits is considered sufficient. DSA keys must be exactly 1024 bits as specified by FIPS |
| 45 |
186-2. For ECDSA keys, size determines the key length by selecting from one of three elliptic curve sizes: 256, 384 |
| 46 |
or 521 bits. Attempting to use bit lengths other than these three values for ECDSA keys will cause this module to |
| 47 |
fail. Ed25519 keys have a fixed length and the size will be ignored.' |
| 48 |
type: int |
| 49 |
type: |
| 50 |
description: |
| 51 |
- The algorithm used to generate the SSH private key. V(rsa1) is for protocol version 1. V(rsa1) is deprecated and may |
| 52 |
not be supported by every version of ssh-keygen. |
| 53 |
type: str |
| 54 |
default: rsa |
| 55 |
choices: ['rsa', 'dsa', 'rsa1', 'ecdsa', 'ed25519'] |
| 56 |
force: |
| 57 |
description: |
| 58 |
- Should the key be regenerated even if it already exists. |
| 59 |
type: bool |
| 60 |
default: false |
| 61 |
path: |
| 62 |
description: |
| 63 |
- Name of the files containing the public and private key. The file containing the public key will have the extension |
| 64 |
C(.pub). |
| 65 |
type: path |
| 66 |
required: true |
| 67 |
comment: |
| 68 |
description: |
| 69 |
- Provides a new comment to the public key. |
| 70 |
type: str |
| 71 |
passphrase: |
| 72 |
description: |
| 73 |
- Passphrase used to decrypt an existing private key or encrypt a newly generated private key. |
| 74 |
- Passphrases are not supported for O(type=rsa1). |
| 75 |
- Can only be used when O(backend=cryptography), or when O(backend=auto) and a required C(cryptography) version is installed. |
| 76 |
Note that depending on the cryptography version, the C(bcrypt) package is also needed. |
| 77 |
type: str |
| 78 |
version_added: 1.7.0 |
| 79 |
private_key_format: |
| 80 |
description: |
| 81 |
- Used when O(backend=cryptography) to select a format for the private key at the provided O(path). |
| 82 |
- When set to V(auto) this module will match the key format of the installed OpenSSH version. |
| 83 |
- For OpenSSH < 7.8 private keys will be in PKCS1 format except ed25519 keys which will be in OpenSSH format. |
| 84 |
- For OpenSSH >= 7.8 all private key types will be in the OpenSSH format. |
| 85 |
- Using this option when O(regenerate=partial_idempotence) or O(regenerate=full_idempotence) will cause a new keypair |
| 86 |
to be generated if the private key's format does not match the value of O(private_key_format). This module will not |
| 87 |
however convert existing private keys between formats. |
| 88 |
type: str |
| 89 |
default: auto |
| 90 |
choices: |
| 91 |
- auto |
| 92 |
- pkcs1 |
| 93 |
- pkcs8 |
| 94 |
- ssh |
| 95 |
version_added: 1.7.0 |
| 96 |
backend: |
| 97 |
description: |
| 98 |
- Selects between the V(cryptography) library or the OpenSSH binary V(opensshbin). |
| 99 |
- V(auto) will default to V(opensshbin) unless the OpenSSH binary is not installed or when using O(passphrase). |
| 100 |
type: str |
| 101 |
default: auto |
| 102 |
choices: |
| 103 |
- auto |
| 104 |
- cryptography |
| 105 |
- opensshbin |
| 106 |
version_added: 1.7.0 |
| 107 |
regenerate: |
| 108 |
description: |
| 109 |
- Allows to configure in which situations the module is allowed to regenerate private keys. The module will always generate |
| 110 |
a new key if the destination file does not exist. |
| 111 |
- By default, the key will be regenerated when it does not match the module's options, except when the key cannot be |
| 112 |
read or the passphrase does not match. Please note that this B(changed) for Ansible 2.10. For Ansible 2.9, the behavior |
| 113 |
was as if V(full_idempotence) is specified. |
| 114 |
- If set to V(never), the module will fail if the key cannot be read or the passphrase is not matching, and will never |
| 115 |
regenerate an existing key. |
| 116 |
- If set to V(fail), the module will fail if the key does not correspond to the module's options. |
| 117 |
- If set to V(partial_idempotence), the key will be regenerated if it does not conform to the module's options. The |
| 118 |
key is B(not) regenerated if it cannot be read (broken file), the key is protected by an unknown passphrase, or when |
| 119 |
they key is not protected by a passphrase, but a passphrase is specified. |
| 120 |
- If set to V(full_idempotence), the key will be regenerated if it does not conform to the module's options. This is |
| 121 |
also the case if the key cannot be read (broken file), the key is protected by an unknown passphrase, or when they |
| 122 |
key is not protected by a passphrase, but a passphrase is specified. Make sure you have a B(backup) when using this |
| 123 |
option! |
| 124 |
- If set to V(always), the module will always regenerate the key. This is equivalent to setting O(force) to V(true). |
| 125 |
- Note that adjusting the comment and the permissions can be changed without regeneration. Therefore, even for V(never), |
| 126 |
the task can result in changed. |
| 127 |
type: str |
| 128 |
choices: |
| 129 |
- never |
| 130 |
- fail |
| 131 |
- partial_idempotence |
| 132 |
- full_idempotence |
| 133 |
- always |
| 134 |
default: partial_idempotence |
| 135 |
version_added: '1.0.0' |
| 136 |
notes: |
| 137 |
- In case the ssh key is broken or password protected, the module will fail. Set the O(force) option to V(true) if you want |
| 138 |
to regenerate the keypair. |
| 139 |
- In the case a custom O(mode), O(group), O(owner), or other file attribute is provided it will be applied to both key files. |
| 140 |
""" |
| 141 |
|
| 142 |
EXAMPLES = r""" |
| 143 |
--- |
| 144 |
- name: Generate an OpenSSH keypair with the default values (4096 bits, rsa) |
| 145 |
community.crypto.openssh_keypair: |
| 146 |
path: /tmp/id_ssh_rsa |
| 147 |
|
| 148 |
- name: Generate an OpenSSH keypair with the default values (4096 bits, rsa) and encrypted private key |
| 149 |
community.crypto.openssh_keypair: |
| 150 |
path: /tmp/id_ssh_rsa |
| 151 |
passphrase: super_secret_password |
| 152 |
|
| 153 |
- name: Generate an OpenSSH rsa keypair with a different size (2048 bits) |
| 154 |
community.crypto.openssh_keypair: |
| 155 |
path: /tmp/id_ssh_rsa |
| 156 |
size: 2048 |
| 157 |
|
| 158 |
- name: Force regenerate an OpenSSH keypair if it already exists |
| 159 |
community.crypto.openssh_keypair: |
| 160 |
path: /tmp/id_ssh_rsa |
| 161 |
force: true |
| 162 |
|
| 163 |
- name: Regenerate SSH keypair only if format or options mismatch |
| 164 |
community.crypto.openssh_keypair: |
| 165 |
path: /home/devops/.ssh/id_ed25519 |
| 166 |
type: ed25519 |
| 167 |
regenerate: full_idempotence |
| 168 |
private_key_format: ssh |
| 169 |
|
| 170 |
- name: Generate an OpenSSH keypair with a different algorithm (dsa) |
| 171 |
community.crypto.openssh_keypair: |
| 172 |
path: /tmp/id_ssh_dsa |
| 173 |
type: dsa |
| 174 |
""" |
| 175 |
|
| 176 |
RETURN = r""" |
| 177 |
size: |
| 178 |
description: Size (in bits) of the SSH private key. |
| 179 |
returned: changed or success |
| 180 |
type: int |
| 181 |
sample: 4096 |
| 182 |
type: |
| 183 |
description: Algorithm used to generate the SSH private key. |
| 184 |
returned: changed or success |
| 185 |
type: str |
| 186 |
sample: rsa |
| 187 |
filename: |
| 188 |
description: Path to the generated SSH private key file. |
| 189 |
returned: changed or success |
| 190 |
type: str |
| 191 |
sample: /tmp/id_ssh_rsa |
| 192 |
fingerprint: |
| 193 |
description: The fingerprint of the key. |
| 194 |
returned: changed or success |
| 195 |
type: str |
| 196 |
sample: SHA256:r4YCZxihVjedH2OlfjVGI6Y5xAYtdCwk8VxKyzVyYfM |
| 197 |
public_key: |
| 198 |
description: The public key of the generated SSH private key. |
| 199 |
returned: changed or success |
| 200 |
type: str |
| 201 |
sample: ssh-rsa AAAAB3Nza(...omitted...)veL4E3Xcw== |
| 202 |
comment: |
| 203 |
description: The comment of the generated key. |
| 204 |
returned: changed or success |
| 205 |
type: str |
| 206 |
sample: test@comment |
| 207 |
""" |
| 208 |
|
| 209 |
import typing as t |
| 210 |
|
| 211 |
from ansible.module_utils.basic import AnsibleModule |
| 212 |
|
| 213 |
from ansible_collections.community.crypto.plugins.module_utils._openssh.backends.keypair_backend import ( |
| 214 |
select_backend, |
| 215 |
) |
| 216 |
|
| 217 |
|
| 218 |
def main() -> t.NoReturn: |
| 219 |
module = AnsibleModule( |
| 220 |
argument_spec={ |
| 221 |
"state": { |
| 222 |
"type": "str", |
| 223 |
"default": "present", |
| 224 |
"choices": ["present", "absent"], |
| 225 |
}, |
| 226 |
"size": {"type": "int"}, |
| 227 |
"type": { |
| 228 |
"type": "str", |
| 229 |
"default": "rsa", |
| 230 |
"choices": ["rsa", "dsa", "rsa1", "ecdsa", "ed25519"], |
| 231 |
}, |
| 232 |
"force": {"type": "bool", "default": False}, |
| 233 |
"path": {"type": "path", "required": True}, |
| 234 |
"comment": {"type": "str"}, |
| 235 |
"regenerate": { |
| 236 |
"type": "str", |
| 237 |
"default": "partial_idempotence", |
| 238 |
"choices": [ |
| 239 |
"never", |
| 240 |
"fail", |
| 241 |
"partial_idempotence", |
| 242 |
"full_idempotence", |
| 243 |
"always", |
| 244 |
], |
| 245 |
}, |
| 246 |
"passphrase": {"type": "str", "no_log": True}, |
| 247 |
"private_key_format": { |
| 248 |
"type": "str", |
| 249 |
"default": "auto", |
| 250 |
"no_log": False, |
| 251 |
"choices": ["auto", "pkcs1", "pkcs8", "ssh"], |
| 252 |
}, |
| 253 |
"backend": { |
| 254 |
"type": "str", |
| 255 |
"default": "auto", |
| 256 |
"choices": ["auto", "cryptography", "opensshbin"], |
| 257 |
}, |
| 258 |
}, |
| 259 |
supports_check_mode=True, |
| 260 |
add_file_common_args=True, |
| 261 |
) |
| 262 |
|
| 263 |
keypair = select_backend(module=module, backend=module.params["backend"]) |
| 264 |
|
| 265 |
keypair.execute() |
| 266 |
|
| 267 |
|
| 268 |
if __name__ == "__main__": |
| 269 |
main() |