| 1 |
#!/usr/bin/python |
| 2 |
# Copyright (c) 2016-2017, Yanis Guenane <[email protected]> |
| 3 |
# Copyright (c) 2017, Markus Teufelberger <[email protected]> |
| 4 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 6 |
|
| 7 |
from __future__ import annotations |
| 8 |
|
| 9 |
DOCUMENTATION = r""" |
| 10 |
module: x509_certificate_info |
| 11 |
short_description: Provide information of OpenSSL X.509 certificates |
| 12 |
description: |
| 13 |
- This module allows one to query information on OpenSSL certificates. |
| 14 |
- It uses the cryptography Python library to interact with OpenSSL. |
| 15 |
- Note that this module was called C(openssl_certificate_info) when included directly in Ansible up to version 2.9. When |
| 16 |
moved to the collection C(community.crypto), it was renamed to M(community.crypto.x509_certificate_info). From Ansible |
| 17 |
2.10 on, it can still be used by the old short name (or by C(ansible.builtin.openssl_certificate_info)), which redirects |
| 18 |
to M(community.crypto.x509_certificate_info). When using FQCNs or when using the |
| 19 |
L(collections,https://docs.ansible.com/ansible/latest/user_guide/collections_using.html#using-collections-in-a-playbook) |
| 20 |
keyword, the new name M(community.crypto.x509_certificate_info) should be used to avoid a deprecation warning. |
| 21 |
author: |
| 22 |
- Felix Fontein (@felixfontein) |
| 23 |
- Yanis Guenane (@Spredzy) |
| 24 |
- Markus Teufelberger (@MarkusTeufelberger) |
| 25 |
extends_documentation_fragment: |
| 26 |
- community.crypto._attributes |
| 27 |
- community.crypto._attributes.info_module |
| 28 |
- community.crypto._attributes.idempotent_not_modify_state |
| 29 |
- community.crypto._cryptography_dep.minimum |
| 30 |
- community.crypto._name_encoding |
| 31 |
options: |
| 32 |
path: |
| 33 |
description: |
| 34 |
- Remote absolute path where the certificate file is loaded from. |
| 35 |
- Either O(path) or O(content) must be specified, but not both. |
| 36 |
- PEM and DER formats are supported. |
| 37 |
type: path |
| 38 |
content: |
| 39 |
description: |
| 40 |
- Content of the X.509 certificate in PEM format. |
| 41 |
- Either O(path) or O(content) must be specified, but not both. |
| 42 |
type: str |
| 43 |
version_added: '1.0.0' |
| 44 |
valid_at: |
| 45 |
description: |
| 46 |
- A dict of names mapping to time specifications. Every time specified here will be checked whether the certificate |
| 47 |
is valid at this point. See the RV(valid_at) return value for information on the result. |
| 48 |
- Time can be specified either as relative time or as absolute timestamp. |
| 49 |
- Time will always be interpreted as UTC. |
| 50 |
- Valid format is C([+-]timespec | ASN.1 TIME) where timespec can be an integer + C([w | d | h | m | s]) (for example |
| 51 |
V(+32w1d2h)), and ASN.1 TIME (in other words, pattern C(YYYYMMDDHHMMSSZ)). Note that all timestamps will be treated |
| 52 |
as being in UTC. |
| 53 |
type: dict |
| 54 |
select_crypto_backend: |
| 55 |
description: |
| 56 |
- Determines which crypto backend to use. |
| 57 |
- The default choice is V(auto), which tries to use C(cryptography) if available. |
| 58 |
- If set to V(cryptography), will try to use the L(cryptography,https://cryptography.io/) library. |
| 59 |
- Note that with community.crypto 3.0.0, all values behave the same. |
| 60 |
This option will be deprecated in a later version. |
| 61 |
We recommend to not set it explicitly. |
| 62 |
type: str |
| 63 |
default: auto |
| 64 |
choices: [auto, cryptography] |
| 65 |
|
| 66 |
notes: |
| 67 |
- All timestamp values are provided in ASN.1 TIME format, in other words, following the C(YYYYMMDDHHMMSSZ) pattern. They |
| 68 |
are all in UTC. |
| 69 |
seealso: |
| 70 |
- module: community.crypto.x509_certificate |
| 71 |
- module: community.crypto.x509_certificate_pipe |
| 72 |
- plugin: community.crypto.x509_certificate_info |
| 73 |
plugin_type: filter |
| 74 |
description: A filter variant of this module. |
| 75 |
- plugin: community.crypto.to_serial |
| 76 |
plugin_type: filter |
| 77 |
""" |
| 78 |
|
| 79 |
EXAMPLES = r""" |
| 80 |
--- |
| 81 |
- name: Generate a Self Signed OpenSSL certificate |
| 82 |
community.crypto.x509_certificate: |
| 83 |
path: /etc/ssl/crt/ansible.com.crt |
| 84 |
privatekey_path: /etc/ssl/private/ansible.com.pem |
| 85 |
csr_path: /etc/ssl/csr/ansible.com.csr |
| 86 |
provider: selfsigned |
| 87 |
|
| 88 |
|
| 89 |
# Get information on the certificate |
| 90 |
|
| 91 |
- name: Get information on generated certificate |
| 92 |
community.crypto.x509_certificate_info: |
| 93 |
path: /etc/ssl/crt/ansible.com.crt |
| 94 |
register: result |
| 95 |
|
| 96 |
- name: Dump information |
| 97 |
ansible.builtin.debug: |
| 98 |
var: result |
| 99 |
|
| 100 |
|
| 101 |
# Check whether the certificate is valid or not valid at certain times, fail |
| 102 |
# if this is not the case. The first task (x509_certificate_info) collects |
| 103 |
# the information, and the second task (assert) validates the result and |
| 104 |
# makes the playbook fail in case something is not as expected. |
| 105 |
|
| 106 |
- name: Test whether that certificate is valid tomorrow and/or in three weeks |
| 107 |
community.crypto.x509_certificate_info: |
| 108 |
path: /etc/ssl/crt/ansible.com.crt |
| 109 |
valid_at: |
| 110 |
point_1: "+1d" |
| 111 |
point_2: "+3w" |
| 112 |
register: result |
| 113 |
|
| 114 |
- name: Validate that certificate is valid tomorrow, but not in three weeks |
| 115 |
ansible.builtin.assert: |
| 116 |
that: |
| 117 |
- result.valid_at.point_1 # valid in one day |
| 118 |
- not result.valid_at.point_2 # not valid in three weeks |
| 119 |
""" |
| 120 |
|
| 121 |
RETURN = r""" |
| 122 |
expired: |
| 123 |
description: Whether the certificate is expired (in other words, C(notAfter) is in the past). |
| 124 |
returned: success |
| 125 |
type: bool |
| 126 |
basic_constraints: |
| 127 |
description: Entries in the C(basic_constraints) extension, or V(none) if extension is not present. |
| 128 |
returned: success |
| 129 |
type: list |
| 130 |
elements: str |
| 131 |
sample: ["CA:TRUE", "pathlen:1"] |
| 132 |
basic_constraints_critical: |
| 133 |
description: Whether the C(basic_constraints) extension is critical. |
| 134 |
returned: success |
| 135 |
type: bool |
| 136 |
extended_key_usage: |
| 137 |
description: Entries in the C(extended_key_usage) extension, or V(none) if extension is not present. |
| 138 |
returned: success |
| 139 |
type: list |
| 140 |
elements: str |
| 141 |
sample: [Biometric Info, DVCS, Time Stamping] |
| 142 |
extended_key_usage_critical: |
| 143 |
description: Whether the C(extended_key_usage) extension is critical. |
| 144 |
returned: success |
| 145 |
type: bool |
| 146 |
extensions_by_oid: |
| 147 |
description: Returns a dictionary for every extension OID. |
| 148 |
returned: success |
| 149 |
type: dict |
| 150 |
contains: |
| 151 |
critical: |
| 152 |
description: Whether the extension is critical. |
| 153 |
returned: success |
| 154 |
type: bool |
| 155 |
value: |
| 156 |
description: |
| 157 |
- The Base64 encoded value (in DER format) of the extension. |
| 158 |
- B(Note) that depending on the C(cryptography) version used, it is not possible to extract the ASN.1 content of the |
| 159 |
extension, but only to provide the re-encoded content of the extension in case it was parsed by C(cryptography). |
| 160 |
This should usually result in exactly the same value, except if the original extension value was malformed. |
| 161 |
returned: success |
| 162 |
type: str |
| 163 |
sample: "MAMCAQU=" |
| 164 |
sample: {"1.3.6.1.5.5.7.1.24": {"critical": false, "value": "MAMCAQU="}} |
| 165 |
key_usage: |
| 166 |
description: Entries in the C(key_usage) extension, or V(none) if extension is not present. |
| 167 |
returned: success |
| 168 |
type: str |
| 169 |
sample: [Key Agreement, Data Encipherment] |
| 170 |
key_usage_critical: |
| 171 |
description: Whether the C(key_usage) extension is critical. |
| 172 |
returned: success |
| 173 |
type: bool |
| 174 |
subject_alt_name: |
| 175 |
description: |
| 176 |
- Entries in the C(subject_alt_name) extension, or V(none) if extension is not present. |
| 177 |
- See O(name_encoding) for how IDNs are handled. |
| 178 |
returned: success |
| 179 |
type: list |
| 180 |
elements: str |
| 181 |
sample: ["DNS:www.ansible.com", "IP:1.2.3.4"] |
| 182 |
subject_alt_name_critical: |
| 183 |
description: Whether the C(subject_alt_name) extension is critical. |
| 184 |
returned: success |
| 185 |
type: bool |
| 186 |
ocsp_must_staple: |
| 187 |
description: V(true) if the OCSP Must Staple extension is present, V(none) otherwise. |
| 188 |
returned: success |
| 189 |
type: bool |
| 190 |
ocsp_must_staple_critical: |
| 191 |
description: Whether the C(ocsp_must_staple) extension is critical. |
| 192 |
returned: success |
| 193 |
type: bool |
| 194 |
issuer: |
| 195 |
description: |
| 196 |
- The certificate's issuer. |
| 197 |
- Note that for repeated values, only the last one will be returned. |
| 198 |
returned: success |
| 199 |
type: dict |
| 200 |
sample: {"organizationName": "Ansible", "commonName": "ca.example.com"} |
| 201 |
issuer_ordered: |
| 202 |
description: The certificate's issuer as an ordered list of tuples. |
| 203 |
returned: success |
| 204 |
type: list |
| 205 |
elements: list |
| 206 |
sample: [["organizationName", "Ansible"], ["commonName": "ca.example.com"]] |
| 207 |
subject: |
| 208 |
description: |
| 209 |
- The certificate's subject as a dictionary. |
| 210 |
- Note that for repeated values, only the last one will be returned. |
| 211 |
returned: success |
| 212 |
type: dict |
| 213 |
sample: {"commonName": "www.example.com", "emailAddress": "[email protected]"} |
| 214 |
subject_ordered: |
| 215 |
description: The certificate's subject as an ordered list of tuples. |
| 216 |
returned: success |
| 217 |
type: list |
| 218 |
elements: list |
| 219 |
sample: [["commonName", "www.example.com"], ["emailAddress": "[email protected]"]] |
| 220 |
not_after: |
| 221 |
description: C(notAfter) date as ASN.1 TIME. |
| 222 |
returned: success |
| 223 |
type: str |
| 224 |
sample: '20190413202428Z' |
| 225 |
not_before: |
| 226 |
description: C(notBefore) date as ASN.1 TIME. |
| 227 |
returned: success |
| 228 |
type: str |
| 229 |
sample: '20190331202428Z' |
| 230 |
public_key: |
| 231 |
description: Certificate's public key in PEM format. |
| 232 |
returned: success |
| 233 |
type: str |
| 234 |
sample: "-----BEGIN PUBLIC KEY-----\nMIICIjANBgkqhkiG9w0BAQEFAAOCAg8A..." |
| 235 |
public_key_type: |
| 236 |
description: |
| 237 |
- The certificate's public key's type. |
| 238 |
- One of V(RSA), V(DSA), V(ECC), V(Ed25519), V(X25519), V(Ed448), or V(X448). |
| 239 |
- Will start with V(unknown) if the key type cannot be determined. |
| 240 |
returned: success |
| 241 |
type: str |
| 242 |
version_added: 1.7.0 |
| 243 |
sample: RSA |
| 244 |
public_key_data: |
| 245 |
description: |
| 246 |
- Public key data. Depends on the public key's type. |
| 247 |
returned: success |
| 248 |
type: dict |
| 249 |
version_added: 1.7.0 |
| 250 |
contains: |
| 251 |
size: |
| 252 |
description: |
| 253 |
- Bit size of modulus (RSA) or prime number (DSA). |
| 254 |
type: int |
| 255 |
returned: When RV(public_key_type=RSA) or RV(public_key_type=DSA) |
| 256 |
modulus: |
| 257 |
description: |
| 258 |
- The RSA key's modulus. |
| 259 |
type: int |
| 260 |
returned: When RV(public_key_type=RSA) |
| 261 |
exponent: |
| 262 |
description: |
| 263 |
- The RSA key's public exponent. |
| 264 |
type: int |
| 265 |
returned: When RV(public_key_type=RSA) |
| 266 |
p: |
| 267 |
description: |
| 268 |
- The C(p) value for DSA. |
| 269 |
- This is the prime modulus upon which arithmetic takes place. |
| 270 |
type: int |
| 271 |
returned: When RV(public_key_type=DSA) |
| 272 |
q: |
| 273 |
description: |
| 274 |
- The C(q) value for DSA. |
| 275 |
- This is a prime that divides C(p - 1), and at the same time the order of the subgroup of the multiplicative group |
| 276 |
of the prime field used. |
| 277 |
type: int |
| 278 |
returned: When RV(public_key_type=DSA) |
| 279 |
g: |
| 280 |
description: |
| 281 |
- The C(g) value for DSA. |
| 282 |
- This is the element spanning the subgroup of the multiplicative group of the prime field used. |
| 283 |
type: int |
| 284 |
returned: When RV(public_key_type=DSA) |
| 285 |
curve: |
| 286 |
description: |
| 287 |
- The curve's name for ECC. |
| 288 |
type: str |
| 289 |
returned: When RV(public_key_type=ECC) |
| 290 |
exponent_size: |
| 291 |
description: |
| 292 |
- The maximum number of bits of a private key. This is basically the bit size of the subgroup used. |
| 293 |
type: int |
| 294 |
returned: When RV(public_key_type=ECC) |
| 295 |
x: |
| 296 |
description: |
| 297 |
- The C(x) coordinate for the public point on the elliptic curve. |
| 298 |
type: int |
| 299 |
returned: When RV(public_key_type=ECC) |
| 300 |
y: |
| 301 |
description: |
| 302 |
- For RV(public_key_type=ECC), this is the C(y) coordinate for the public point on the elliptic curve. |
| 303 |
- For RV(public_key_type=DSA), this is the publicly known group element whose discrete logarithm w.r.t. C(g) is the |
| 304 |
private key. |
| 305 |
type: int |
| 306 |
returned: When RV(public_key_type=DSA) or RV(public_key_type=ECC) |
| 307 |
public_key_fingerprints: |
| 308 |
description: |
| 309 |
- Fingerprints of certificate's public key. |
| 310 |
- For every hash algorithm available, the fingerprint is computed. |
| 311 |
returned: success |
| 312 |
type: dict |
| 313 |
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': |
| 314 |
'f7:07:4a:f0:b0:f0:e6:8b:95:5f:f9:e6:61:0a:32:68:f1..." |
| 315 |
fingerprints: |
| 316 |
description: |
| 317 |
- Fingerprints of the DER-encoded form of the whole certificate. |
| 318 |
- For every hash algorithm available, the fingerprint is computed. |
| 319 |
returned: success |
| 320 |
type: dict |
| 321 |
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': |
| 322 |
'f7:07:4a:f0:b0:f0:e6:8b:95:5f:f9:e6:61:0a:32:68:f1..." |
| 323 |
version_added: 1.2.0 |
| 324 |
signature_algorithm: |
| 325 |
description: The signature algorithm used to sign the certificate. |
| 326 |
returned: success |
| 327 |
type: str |
| 328 |
sample: sha256WithRSAEncryption |
| 329 |
serial_number: |
| 330 |
description: |
| 331 |
- The certificate's serial number. |
| 332 |
- This return value is an B(integer). If you need the serial numbers as a colon-separated hex string, such as C(11:22:33), |
| 333 |
you need to convert it to that form with P(community.crypto.to_serial#filter). |
| 334 |
returned: success |
| 335 |
type: int |
| 336 |
sample: 1234 |
| 337 |
version: |
| 338 |
description: The certificate version. |
| 339 |
returned: success |
| 340 |
type: int |
| 341 |
sample: 3 |
| 342 |
valid_at: |
| 343 |
description: For every time stamp provided in the O(valid_at) option, a boolean whether the certificate is valid at that |
| 344 |
point in time or not. |
| 345 |
returned: success |
| 346 |
type: dict |
| 347 |
subject_key_identifier: |
| 348 |
description: |
| 349 |
- The certificate's subject key identifier. |
| 350 |
- The identifier is returned in hexadecimal, with V(:) used to separate bytes. |
| 351 |
- Is V(none) if the C(SubjectKeyIdentifier) extension is not present. |
| 352 |
returned: success |
| 353 |
type: str |
| 354 |
sample: '00:11:22:33:44:55:66:77:88:99:aa:bb:cc:dd:ee:ff:00:11:22:33' |
| 355 |
authority_key_identifier: |
| 356 |
description: |
| 357 |
- The certificate's authority key identifier. |
| 358 |
- The identifier is returned in hexadecimal, with V(:) used to separate bytes. |
| 359 |
- Is V(none) if the C(AuthorityKeyIdentifier) extension is not present. |
| 360 |
returned: success |
| 361 |
type: str |
| 362 |
sample: '00:11:22:33:44:55:66:77:88:99:aa:bb:cc:dd:ee:ff:00:11:22:33' |
| 363 |
authority_cert_issuer: |
| 364 |
description: |
| 365 |
- The certificate's authority cert issuer as a list of general names. |
| 366 |
- Is V(none) if the C(AuthorityKeyIdentifier) extension is not present. |
| 367 |
- See O(name_encoding) for how IDNs are handled. |
| 368 |
returned: success |
| 369 |
type: list |
| 370 |
elements: str |
| 371 |
sample: ["DNS:www.ansible.com", "IP:1.2.3.4"] |
| 372 |
authority_cert_serial_number: |
| 373 |
description: |
| 374 |
- The certificate's authority cert serial number. |
| 375 |
- Is V(none) if the C(AuthorityKeyIdentifier) extension is not present. |
| 376 |
- This return value is an B(integer). If you need the serial numbers as a colon-separated hex string, such as C(11:22:33), |
| 377 |
you need to convert it to that form with P(community.crypto.to_serial#filter). |
| 378 |
returned: success |
| 379 |
type: int |
| 380 |
sample: 12345 |
| 381 |
ocsp_uri: |
| 382 |
description: The OCSP responder URI, if included in the certificate. Will be V(none) if no OCSP responder URI is included. |
| 383 |
returned: success |
| 384 |
type: str |
| 385 |
issuer_uri: |
| 386 |
description: The Issuer URI, if included in the certificate. Will be V(none) if no issuer URI is included. |
| 387 |
returned: success |
| 388 |
type: str |
| 389 |
version_added: 2.9.0 |
| 390 |
""" |
| 391 |
|
| 392 |
import typing as t |
| 393 |
|
| 394 |
from ansible.module_utils.basic import AnsibleModule |
| 395 |
from ansible.module_utils.common.text.converters import to_text |
| 396 |
|
| 397 |
from ansible_collections.community.crypto.plugins.module_utils._crypto.basic import ( |
| 398 |
OpenSSLObjectError, |
| 399 |
) |
| 400 |
from ansible_collections.community.crypto.plugins.module_utils._crypto.cryptography_support import ( |
| 401 |
CRYPTOGRAPHY_TIMEZONE, |
| 402 |
) |
| 403 |
from ansible_collections.community.crypto.plugins.module_utils._crypto.module_backends.certificate_info import ( |
| 404 |
select_backend, |
| 405 |
) |
| 406 |
from ansible_collections.community.crypto.plugins.module_utils._time import ( |
| 407 |
get_relative_time_option, |
| 408 |
) |
| 409 |
|
| 410 |
|
| 411 |
def main() -> t.NoReturn: |
| 412 |
module = AnsibleModule( |
| 413 |
argument_spec={ |
| 414 |
"path": {"type": "path"}, |
| 415 |
"content": {"type": "str"}, |
| 416 |
"valid_at": {"type": "dict"}, |
| 417 |
"name_encoding": { |
| 418 |
"type": "str", |
| 419 |
"default": "ignore", |
| 420 |
"choices": ["ignore", "idna", "unicode"], |
| 421 |
}, |
| 422 |
"select_crypto_backend": { |
| 423 |
"type": "str", |
| 424 |
"default": "auto", |
| 425 |
"choices": ["auto", "cryptography"], |
| 426 |
}, |
| 427 |
}, |
| 428 |
required_one_of=(["path", "content"],), |
| 429 |
mutually_exclusive=(["path", "content"],), |
| 430 |
supports_check_mode=True, |
| 431 |
) |
| 432 |
|
| 433 |
content: str | None = module.params["content"] |
| 434 |
path: str | None = module.params["path"] |
| 435 |
if content is not None: |
| 436 |
data = content.encode("utf-8") |
| 437 |
else: |
| 438 |
if path is None: |
| 439 |
module.fail_json(msg="One of path and content must be provided") |
| 440 |
try: |
| 441 |
with open(path, "rb") as f: |
| 442 |
data = f.read() |
| 443 |
except (IOError, OSError) as e: |
| 444 |
module.fail_json(msg=f"Error while reading certificate file from disk: {e}") |
| 445 |
|
| 446 |
module_backend = select_backend(module=module, content=data) |
| 447 |
|
| 448 |
valid_at: dict[str, t.Any] = module.params["valid_at"] |
| 449 |
if valid_at: |
| 450 |
for k, v in valid_at.items(): |
| 451 |
if not isinstance(v, (str, bytes)): |
| 452 |
module.fail_json( |
| 453 |
msg=f"The value for valid_at.{k} must be of type string (got {type(v)})" |
| 454 |
) |
| 455 |
valid_at[k] = get_relative_time_option( |
| 456 |
to_text(v), |
| 457 |
input_name=f"valid_at.{k}", |
| 458 |
with_timezone=CRYPTOGRAPHY_TIMEZONE, |
| 459 |
) |
| 460 |
|
| 461 |
try: |
| 462 |
result = module_backend.get_info(der_support_enabled=content is None) |
| 463 |
|
| 464 |
not_before = module_backend.get_not_before() |
| 465 |
not_after = module_backend.get_not_after() |
| 466 |
|
| 467 |
result["valid_at"] = {} |
| 468 |
if valid_at: |
| 469 |
for k, v in valid_at.items(): |
| 470 |
result["valid_at"][k] = not_before <= v <= not_after |
| 471 |
|
| 472 |
module.exit_json(**result) |
| 473 |
except OpenSSLObjectError as exc: |
| 474 |
module.fail_json(msg=str(exc)) |
| 475 |
|
| 476 |
|
| 477 |
if __name__ == "__main__": |
| 478 |
main() |