| 1 |
#!/usr/bin/python |
| 2 |
# Copyright (c) 2021, 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_publickey_info |
| 10 |
short_description: Provide information for OpenSSL public keys |
| 11 |
description: |
| 12 |
- This module allows one to query information on OpenSSL public keys. |
| 13 |
- It uses the cryptography python library to interact with OpenSSL. |
| 14 |
version_added: 1.7.0 |
| 15 |
author: |
| 16 |
- Felix Fontein (@felixfontein) |
| 17 |
extends_documentation_fragment: |
| 18 |
- community.crypto._attributes |
| 19 |
- community.crypto._attributes.info_module |
| 20 |
- community.crypto._attributes.idempotent_not_modify_state |
| 21 |
- community.crypto._cryptography_dep.minimum |
| 22 |
options: |
| 23 |
path: |
| 24 |
description: |
| 25 |
- Remote absolute path where the public key file is loaded from. |
| 26 |
type: path |
| 27 |
content: |
| 28 |
description: |
| 29 |
- Content of the public key file. |
| 30 |
- Either O(path) or O(content) must be specified, but not both. |
| 31 |
type: str |
| 32 |
|
| 33 |
select_crypto_backend: |
| 34 |
description: |
| 35 |
- Determines which crypto backend to use. |
| 36 |
- The default choice is V(auto), which tries to use C(cryptography) if available. |
| 37 |
- If set to V(cryptography), will try to use the L(cryptography,https://cryptography.io/) library. |
| 38 |
- Note that with community.crypto 3.0.0, all values behave the same. |
| 39 |
This option will be deprecated in a later version. |
| 40 |
We recommend to not set it explicitly. |
| 41 |
type: str |
| 42 |
default: auto |
| 43 |
choices: [auto, cryptography] |
| 44 |
|
| 45 |
seealso: |
| 46 |
- module: community.crypto.openssl_publickey |
| 47 |
- module: community.crypto.openssl_privatekey_info |
| 48 |
- plugin: community.crypto.openssl_publickey_info |
| 49 |
plugin_type: filter |
| 50 |
description: A filter variant of this module. |
| 51 |
""" |
| 52 |
|
| 53 |
EXAMPLES = r""" |
| 54 |
--- |
| 55 |
- name: Generate an OpenSSL private key with the default values (4096 bits, RSA) |
| 56 |
community.crypto.openssl_privatekey: |
| 57 |
path: /etc/ssl/private/ansible.com.pem |
| 58 |
|
| 59 |
- name: Create public key from private key |
| 60 |
community.crypto.openssl_publickey: |
| 61 |
privatekey_path: /etc/ssl/private/ansible.com.pem |
| 62 |
path: /etc/ssl/ansible.com.pub |
| 63 |
|
| 64 |
- name: Get information on public key |
| 65 |
community.crypto.openssl_publickey_info: |
| 66 |
path: /etc/ssl/ansible.com.pub |
| 67 |
register: result |
| 68 |
|
| 69 |
- name: Dump information |
| 70 |
ansible.builtin.debug: |
| 71 |
var: result |
| 72 |
""" |
| 73 |
|
| 74 |
RETURN = r""" |
| 75 |
fingerprints: |
| 76 |
description: |
| 77 |
- Fingerprints of public key. |
| 78 |
- For every hash algorithm available, the fingerprint is computed. |
| 79 |
returned: success |
| 80 |
type: dict |
| 81 |
sample: "{'sha256': 'd4:b3:aa:6d:c8:04:ce:4e:ba:f6:29:4d:92:a3:94:b0:c2:ff:bd:bf:33:63:11:43:34:0f:51:b0:95:09:2f:63', 'sha512': |
| 82 |
'f7:07:4a:f0:b0:f0:e6:8b:95:5f:f9:e6:61:0a:32:68:f1..." |
| 83 |
type: |
| 84 |
description: |
| 85 |
- The key's type. |
| 86 |
- One of V(RSA), V(DSA), V(ECC), V(Ed25519), V(X25519), V(Ed448), or V(X448). |
| 87 |
- Will start with V(unknown) if the key type cannot be determined. |
| 88 |
returned: success |
| 89 |
type: str |
| 90 |
sample: RSA |
| 91 |
public_data: |
| 92 |
description: |
| 93 |
- Public key data. Depends on key type. |
| 94 |
returned: success |
| 95 |
type: dict |
| 96 |
contains: |
| 97 |
size: |
| 98 |
description: |
| 99 |
- Bit size of modulus (RSA) or prime number (DSA). |
| 100 |
type: int |
| 101 |
returned: When RV(type=RSA) or RV(type=DSA) |
| 102 |
modulus: |
| 103 |
description: |
| 104 |
- The RSA key's modulus. |
| 105 |
type: int |
| 106 |
returned: When RV(type=RSA) |
| 107 |
exponent: |
| 108 |
description: |
| 109 |
- The RSA key's public exponent. |
| 110 |
type: int |
| 111 |
returned: When RV(type=RSA) |
| 112 |
p: |
| 113 |
description: |
| 114 |
- The C(p) value for DSA. |
| 115 |
- This is the prime modulus upon which arithmetic takes place. |
| 116 |
type: int |
| 117 |
returned: When RV(type=DSA) |
| 118 |
q: |
| 119 |
description: |
| 120 |
- The C(q) value for DSA. |
| 121 |
- This is a prime that divides C(p - 1), and at the same time the order of the subgroup of the multiplicative group |
| 122 |
of the prime field used. |
| 123 |
type: int |
| 124 |
returned: When RV(type=DSA) |
| 125 |
g: |
| 126 |
description: |
| 127 |
- The C(g) value for DSA. |
| 128 |
- This is the element spanning the subgroup of the multiplicative group of the prime field used. |
| 129 |
type: int |
| 130 |
returned: When RV(type=DSA) |
| 131 |
curve: |
| 132 |
description: |
| 133 |
- The curve's name for ECC. |
| 134 |
type: str |
| 135 |
returned: When RV(type=ECC) |
| 136 |
exponent_size: |
| 137 |
description: |
| 138 |
- The maximum number of bits of a private key. This is basically the bit size of the subgroup used. |
| 139 |
type: int |
| 140 |
returned: When RV(type=ECC) |
| 141 |
x: |
| 142 |
description: |
| 143 |
- The C(x) coordinate for the public point on the elliptic curve. |
| 144 |
type: int |
| 145 |
returned: When RV(type=ECC) |
| 146 |
y: |
| 147 |
description: |
| 148 |
- For RV(type=ECC), this is the C(y) coordinate for the public point on the elliptic curve. |
| 149 |
- For RV(type=DSA), this is the publicly known group element whose discrete logarithm w.r.t. C(g) is the private key. |
| 150 |
type: int |
| 151 |
returned: When RV(type=DSA) or RV(type=ECC) |
| 152 |
""" |
| 153 |
|
| 154 |
import typing as t |
| 155 |
|
| 156 |
from ansible.module_utils.basic import AnsibleModule |
| 157 |
|
| 158 |
from ansible_collections.community.crypto.plugins.module_utils._crypto.basic import ( |
| 159 |
OpenSSLObjectError, |
| 160 |
) |
| 161 |
from ansible_collections.community.crypto.plugins.module_utils._crypto.module_backends.publickey_info import ( |
| 162 |
PublicKeyParseError, |
| 163 |
select_backend, |
| 164 |
) |
| 165 |
|
| 166 |
|
| 167 |
def main() -> t.NoReturn: |
| 168 |
module = AnsibleModule( |
| 169 |
argument_spec={ |
| 170 |
"path": {"type": "path"}, |
| 171 |
"content": {"type": "str", "no_log": True}, |
| 172 |
"select_crypto_backend": { |
| 173 |
"type": "str", |
| 174 |
"default": "auto", |
| 175 |
"choices": ["auto", "cryptography"], |
| 176 |
}, |
| 177 |
}, |
| 178 |
required_one_of=(["path", "content"],), |
| 179 |
mutually_exclusive=(["path", "content"],), |
| 180 |
supports_check_mode=True, |
| 181 |
) |
| 182 |
|
| 183 |
result = { |
| 184 |
"can_load_key": False, |
| 185 |
"can_parse_key": False, |
| 186 |
"key_is_consistent": None, |
| 187 |
} |
| 188 |
|
| 189 |
if module.params["content"] is not None: |
| 190 |
data = module.params["content"].encode("utf-8") |
| 191 |
else: |
| 192 |
try: |
| 193 |
with open(module.params["path"], "rb") as f: |
| 194 |
data = f.read() |
| 195 |
except (IOError, OSError) as e: |
| 196 |
module.fail_json( |
| 197 |
msg=f"Error while reading public key file from disk: {e}", |
| 198 |
**result, # type: ignore |
| 199 |
) |
| 200 |
|
| 201 |
module_backend = select_backend(module=module, content=data) |
| 202 |
|
| 203 |
try: |
| 204 |
result.update(module_backend.get_info()) |
| 205 |
module.exit_json(**result) |
| 206 |
except PublicKeyParseError as exc: |
| 207 |
result.update(exc.result) |
| 208 |
module.fail_json(msg=exc.error_message, **result) # type: ignore |
| 209 |
except OpenSSLObjectError as exc: |
| 210 |
module.fail_json(msg=str(exc)) |
| 211 |
|
| 212 |
|
| 213 |
if __name__ == "__main__": |
| 214 |
main() |