| 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: x509_crl_info |
| 10 |
version_added: '1.0.0' |
| 11 |
short_description: Retrieve information on Certificate Revocation Lists (CRLs) |
| 12 |
description: |
| 13 |
- This module allows one to retrieve information on Certificate Revocation Lists (CRLs). |
| 14 |
author: |
| 15 |
- Felix Fontein (@felixfontein) |
| 16 |
extends_documentation_fragment: |
| 17 |
- community.crypto._attributes |
| 18 |
- community.crypto._attributes.info_module |
| 19 |
- community.crypto._attributes.idempotent_not_modify_state |
| 20 |
- community.crypto._cryptography_dep.minimum |
| 21 |
- community.crypto._name_encoding |
| 22 |
options: |
| 23 |
path: |
| 24 |
description: |
| 25 |
- Remote absolute path where the generated CRL file should be created or is already located. |
| 26 |
- Either O(path) or O(content) must be specified, but not both. |
| 27 |
type: path |
| 28 |
content: |
| 29 |
description: |
| 30 |
- Content of the X.509 CRL in PEM format, or Base64-encoded X.509 CRL. |
| 31 |
- Either O(path) or O(content) must be specified, but not both. |
| 32 |
type: str |
| 33 |
list_revoked_certificates: |
| 34 |
description: |
| 35 |
- If set to V(false), the list of revoked certificates is not included in the result. |
| 36 |
- This is useful when retrieving information on large CRL files. Enumerating all revoked certificates can take some |
| 37 |
time, including serializing the result as JSON, sending it to the Ansible controller, and decoding it again. |
| 38 |
type: bool |
| 39 |
default: true |
| 40 |
version_added: 1.7.0 |
| 41 |
|
| 42 |
notes: |
| 43 |
- All timestamp values are provided in ASN.1 TIME format, in other words, following the C(YYYYMMDDHHMMSSZ) pattern. They |
| 44 |
are all in UTC. |
| 45 |
seealso: |
| 46 |
- module: community.crypto.x509_crl |
| 47 |
- plugin: community.crypto.x509_crl_info |
| 48 |
plugin_type: filter |
| 49 |
description: A filter variant of this module. |
| 50 |
- plugin: community.crypto.to_serial |
| 51 |
plugin_type: filter |
| 52 |
""" |
| 53 |
|
| 54 |
EXAMPLES = r""" |
| 55 |
--- |
| 56 |
- name: Get information on CRL |
| 57 |
community.crypto.x509_crl_info: |
| 58 |
path: /etc/ssl/my-ca.crl |
| 59 |
register: result |
| 60 |
|
| 61 |
- name: Print the information |
| 62 |
ansible.builtin.debug: |
| 63 |
msg: "{{ result }}" |
| 64 |
|
| 65 |
- name: Get information on CRL without list of revoked certificates |
| 66 |
community.crypto.x509_crl_info: |
| 67 |
path: /etc/ssl/very-large.crl |
| 68 |
list_revoked_certificates: false |
| 69 |
register: result |
| 70 |
""" |
| 71 |
|
| 72 |
RETURN = r""" |
| 73 |
format: |
| 74 |
description: |
| 75 |
- Whether the CRL is in PEM format (V(pem)) or in DER format (V(der)). |
| 76 |
returned: success |
| 77 |
type: str |
| 78 |
sample: pem |
| 79 |
choices: |
| 80 |
- pem |
| 81 |
- der |
| 82 |
issuer: |
| 83 |
description: |
| 84 |
- The CRL's issuer. |
| 85 |
- Note that for repeated values, only the last one will be returned. |
| 86 |
- See O(name_encoding) for how IDNs are handled. |
| 87 |
returned: success |
| 88 |
type: dict |
| 89 |
sample: {"organizationName": "Ansible", "commonName": "ca.example.com"} |
| 90 |
issuer_ordered: |
| 91 |
description: The CRL's issuer as an ordered list of tuples. |
| 92 |
returned: success |
| 93 |
type: list |
| 94 |
elements: list |
| 95 |
sample: [["organizationName", "Ansible"], ["commonName": "ca.example.com"]] |
| 96 |
last_update: |
| 97 |
description: The point in time from which this CRL can be trusted as ASN.1 TIME. |
| 98 |
returned: success |
| 99 |
type: str |
| 100 |
sample: '20190413202428Z' |
| 101 |
next_update: |
| 102 |
description: |
| 103 |
- The point in time from which a new CRL will be issued and the client has to check for it as ASN.1 TIME. |
| 104 |
- Will be C(none) if no such timestamp is present. |
| 105 |
returned: success |
| 106 |
type: str |
| 107 |
sample: '20190413202428Z' |
| 108 |
digest: |
| 109 |
description: The signature algorithm used to sign the CRL. |
| 110 |
returned: success |
| 111 |
type: str |
| 112 |
sample: sha256WithRSAEncryption |
| 113 |
revoked_certificates: |
| 114 |
description: List of certificates to be revoked. |
| 115 |
returned: success if O(list_revoked_certificates=true) |
| 116 |
type: list |
| 117 |
elements: dict |
| 118 |
contains: |
| 119 |
serial_number: |
| 120 |
description: |
| 121 |
- Serial number of the certificate. |
| 122 |
- 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), |
| 123 |
you need to convert it to that form with P(community.crypto.to_serial#filter). |
| 124 |
type: int |
| 125 |
sample: 1234 |
| 126 |
revocation_date: |
| 127 |
description: The point in time the certificate was revoked as ASN.1 TIME. |
| 128 |
type: str |
| 129 |
sample: '20190413202428Z' |
| 130 |
issuer: |
| 131 |
description: |
| 132 |
- The certificate's issuer. |
| 133 |
- See O(name_encoding) for how IDNs are handled. |
| 134 |
type: list |
| 135 |
elements: str |
| 136 |
sample: ["DNS:ca.example.org"] |
| 137 |
issuer_critical: |
| 138 |
description: Whether the certificate issuer extension is critical. |
| 139 |
type: bool |
| 140 |
sample: false |
| 141 |
reason: |
| 142 |
description: |
| 143 |
- The value for the revocation reason extension. |
| 144 |
type: str |
| 145 |
sample: key_compromise |
| 146 |
choices: |
| 147 |
- unspecified |
| 148 |
- key_compromise |
| 149 |
- ca_compromise |
| 150 |
- affiliation_changed |
| 151 |
- superseded |
| 152 |
- cessation_of_operation |
| 153 |
- certificate_hold |
| 154 |
- privilege_withdrawn |
| 155 |
- aa_compromise |
| 156 |
- remove_from_crl |
| 157 |
reason_critical: |
| 158 |
description: Whether the revocation reason extension is critical. |
| 159 |
type: bool |
| 160 |
sample: false |
| 161 |
invalidity_date: |
| 162 |
description: |- |
| 163 |
The point in time it was known/suspected that the private key was compromised |
| 164 |
or that the certificate otherwise became invalid as ASN.1 TIME. |
| 165 |
type: str |
| 166 |
sample: '20190413202428Z' |
| 167 |
invalidity_date_critical: |
| 168 |
description: Whether the invalidity date extension is critical. |
| 169 |
type: bool |
| 170 |
sample: false |
| 171 |
""" |
| 172 |
|
| 173 |
|
| 174 |
import base64 |
| 175 |
import binascii |
| 176 |
import typing as t |
| 177 |
|
| 178 |
from ansible.module_utils.basic import AnsibleModule |
| 179 |
|
| 180 |
from ansible_collections.community.crypto.plugins.module_utils._crypto.basic import ( |
| 181 |
OpenSSLObjectError, |
| 182 |
) |
| 183 |
from ansible_collections.community.crypto.plugins.module_utils._crypto.module_backends.crl_info import ( |
| 184 |
get_crl_info, |
| 185 |
) |
| 186 |
from ansible_collections.community.crypto.plugins.module_utils._crypto.pem import ( |
| 187 |
identify_pem_format, |
| 188 |
) |
| 189 |
|
| 190 |
|
| 191 |
def main() -> t.NoReturn: |
| 192 |
module = AnsibleModule( |
| 193 |
argument_spec={ |
| 194 |
"path": {"type": "path"}, |
| 195 |
"content": {"type": "str"}, |
| 196 |
"list_revoked_certificates": {"type": "bool", "default": True}, |
| 197 |
"name_encoding": { |
| 198 |
"type": "str", |
| 199 |
"default": "ignore", |
| 200 |
"choices": ["ignore", "idna", "unicode"], |
| 201 |
}, |
| 202 |
}, |
| 203 |
required_one_of=(["path", "content"],), |
| 204 |
mutually_exclusive=(["path", "content"],), |
| 205 |
supports_check_mode=True, |
| 206 |
) |
| 207 |
|
| 208 |
content: str | None = module.params["content"] |
| 209 |
path: str | None = module.params["path"] |
| 210 |
if content is None: |
| 211 |
if path is None: |
| 212 |
module.fail_json(msg="One of content and path must be provided") |
| 213 |
try: |
| 214 |
with open(path, "rb") as f: |
| 215 |
data = f.read() |
| 216 |
except (IOError, OSError) as e: |
| 217 |
module.fail_json(msg=f"Error while reading CRL file from disk: {e}") |
| 218 |
else: |
| 219 |
data = content.encode("utf-8") |
| 220 |
if not identify_pem_format(data): |
| 221 |
try: |
| 222 |
data = base64.b64decode(content) |
| 223 |
except (binascii.Error, TypeError) as e: |
| 224 |
module.fail_json(msg=f"Error while Base64 decoding content: {e}") |
| 225 |
|
| 226 |
list_revoked_certificates: bool = module.params["list_revoked_certificates"] |
| 227 |
try: |
| 228 |
result = get_crl_info( |
| 229 |
module=module, |
| 230 |
content=data, |
| 231 |
list_revoked_certificates=list_revoked_certificates, |
| 232 |
) |
| 233 |
module.exit_json(**result) |
| 234 |
except OpenSSLObjectError as e: |
| 235 |
module.fail_json(msg=str(e)) |
| 236 |
|
| 237 |
|
| 238 |
if __name__ == "__main__": |
| 239 |
main() |