| 1 |
#!/usr/bin/python |
| 2 |
# Copyright (c) 2018 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: acme_certificate_renewal_info |
| 10 |
author: "Felix Fontein (@felixfontein)" |
| 11 |
version_added: 2.20.0 |
| 12 |
short_description: Determine whether a certificate should be renewed or not |
| 13 |
description: |
| 14 |
- Uses various information to determine whether a certificate should be renewed or not. |
| 15 |
- If available, the ARI extension (ACME Renewal Information, L(RFC 9773, https://www.rfc-editor.org/rfc/rfc9773.html)) is |
| 16 |
used. |
| 17 |
extends_documentation_fragment: |
| 18 |
- community.crypto._acme.basic |
| 19 |
- community.crypto._acme.no_account |
| 20 |
- community.crypto._attributes |
| 21 |
- community.crypto._attributes.info_module |
| 22 |
- community.crypto._attributes.idempotent_not_modify_state |
| 23 |
attributes: |
| 24 |
idempotent: |
| 25 |
support: partial |
| 26 |
details: |
| 27 |
- The module is not idempotent if O(now) is a relative timestamp, or is not specified. |
| 28 |
- If O(use_ari=true), the module is not idempotent if O(ari_algorithm=standard). |
| 29 |
options: |
| 30 |
certificate_path: |
| 31 |
description: |
| 32 |
- A path to the X.509 certificate to determine renewal of. |
| 33 |
- In case the certificate does not exist, the module will always return RV(should_renew=true). |
| 34 |
- O(certificate_path) and O(certificate_content) are mutually exclusive. |
| 35 |
type: path |
| 36 |
certificate_content: |
| 37 |
description: |
| 38 |
- The content of the X.509 certificate to determine renewal of. |
| 39 |
- O(certificate_path) and O(certificate_content) are mutually exclusive. |
| 40 |
type: str |
| 41 |
use_ari: |
| 42 |
description: |
| 43 |
- Whether to use ARI information, if available. |
| 44 |
- Set this to V(false) if the ACME server implements ARI in a way that is incompatible with this module. |
| 45 |
type: bool |
| 46 |
default: true |
| 47 |
ari_algorithm: |
| 48 |
description: |
| 49 |
- If ARI information is used, selects which algorithm is used to determine whether to renew now. |
| 50 |
- V(standard) selects the L(algorithm provided in the ARI specification, |
| 51 |
https://www.rfc-editor.org/rfc/rfc9773.html#section-4.2). |
| 52 |
- V(start) returns RV(should_renew=true) once the start of the renewal interval has been reached. |
| 53 |
type: str |
| 54 |
choices: |
| 55 |
- standard |
| 56 |
- start |
| 57 |
default: standard |
| 58 |
remaining_days: |
| 59 |
description: |
| 60 |
- The number of days the certificate must have left being valid. |
| 61 |
- For example, if O(remaining_days=20), this check causes RV(should_renew=true) if the certificate is valid for less |
| 62 |
than 20 days. |
| 63 |
type: int |
| 64 |
remaining_percentage: |
| 65 |
description: |
| 66 |
- The percentage of the certificate's validity period that should be left. |
| 67 |
- For example, if O(remaining_percentage=0.1), and the certificate's validity period is 90 days, this check causes RV(should_renew=true) |
| 68 |
if the certificate is valid for less than 9 days. |
| 69 |
- Must be a value between 0 and 1. |
| 70 |
type: float |
| 71 |
now: |
| 72 |
description: |
| 73 |
- Use this timestamp instead of the current timestamp to determine whether a certificate should be renewed. |
| 74 |
- Time can be specified either as relative time or as absolute timestamp. |
| 75 |
- Time will always be interpreted as UTC. |
| 76 |
- Valid format is C([+-]timespec | ASN.1 TIME) where timespec can be an integer + C([w | d | h | m | s]) (for example |
| 77 |
V(+32w1d2h)). |
| 78 |
type: str |
| 79 |
treat_parsing_error_as_non_existing: |
| 80 |
description: |
| 81 |
- Determines the behavior when the certificate file exists or its contents are provided, but the certificate cannot be parsed. |
| 82 |
- If V(true), will exit successfully with RV(exists=true), RV(parsable=false), and RV(should_renew=true). |
| 83 |
- If V(false), the module will fail. |
| 84 |
- If the file exists, but cannot be loaded due to I/O errors or permission errors, the module always fails. |
| 85 |
type: bool |
| 86 |
default: false |
| 87 |
version_added: 2.24.0 |
| 88 |
seealso: |
| 89 |
- module: community.crypto.acme_certificate |
| 90 |
description: Allows to obtain a certificate using the ACME protocol. |
| 91 |
- module: community.crypto.acme_ari_info |
| 92 |
description: Obtain renewal information for a certificate. |
| 93 |
""" |
| 94 |
|
| 95 |
EXAMPLES = r""" |
| 96 |
--- |
| 97 |
- name: Retrieve renewal information for a certificate |
| 98 |
community.crypto.acme_certificate_renewal_info: |
| 99 |
certificate_path: /etc/httpd/ssl/sample.com.crt |
| 100 |
register: cert_data |
| 101 |
|
| 102 |
- name: Should the certificate be renewed? |
| 103 |
ansible.builtin.debug: |
| 104 |
var: cert_data.should_renew |
| 105 |
""" |
| 106 |
|
| 107 |
RETURN = r""" |
| 108 |
should_renew: |
| 109 |
description: |
| 110 |
- Whether the certificate should be renewed. |
| 111 |
- If no certificate is provided, or the certificate is expired, will always be V(true). |
| 112 |
returned: success |
| 113 |
type: bool |
| 114 |
sample: true |
| 115 |
|
| 116 |
exists: |
| 117 |
description: |
| 118 |
- Whether the certificate file exists, or O(certificate_content) was provided. |
| 119 |
returned: success |
| 120 |
type: bool |
| 121 |
sample: true |
| 122 |
version_added: 2.24.0 |
| 123 |
|
| 124 |
parsable: |
| 125 |
description: |
| 126 |
- Whether the certificate file exists, or O(certificate_content) was provided, and the certificate can be parsed. |
| 127 |
- Can only differ from RV(exists) if O(treat_parsing_error_as_non_existing=true). |
| 128 |
returned: success |
| 129 |
type: bool |
| 130 |
sample: true |
| 131 |
version_added: 2.24.0 |
| 132 |
|
| 133 |
msg: |
| 134 |
description: |
| 135 |
- Information on the reason for renewal. |
| 136 |
- Should be shown to the user, as in case of ARI triggered renewal it can contain important information, for example on |
| 137 |
forced revocations for misissued certificates. |
| 138 |
type: str |
| 139 |
returned: success |
| 140 |
sample: The certificate does not exist. |
| 141 |
|
| 142 |
supports_ari: |
| 143 |
description: |
| 144 |
- Whether ARI information was used to determine renewal. This can be used to determine whether to specify |
| 145 |
O(community.crypto.acme_certificate#module:include_renewal_cert_id=when_ari_supported) |
| 146 |
for the M(community.crypto.acme_certificate) module. |
| 147 |
- If O(use_ari=false), this will always be V(false). |
| 148 |
returned: success |
| 149 |
type: bool |
| 150 |
sample: true |
| 151 |
|
| 152 |
cert_id: |
| 153 |
description: |
| 154 |
- The certificate ID according to L(Section 4.1 in RFC 9773, https://www.rfc-editor.org/rfc/rfc9773.html#section-4.1). |
| 155 |
returned: success, the certificate exists, and has an Authority Key Identifier X.509 extension |
| 156 |
type: str |
| 157 |
sample: aYhba4dGQEHhs3uEe6CuLN4ByNQ.AIdlQyE |
| 158 |
""" |
| 159 |
|
| 160 |
import os |
| 161 |
import random |
| 162 |
import typing as t |
| 163 |
|
| 164 |
from ansible_collections.community.crypto.plugins.module_utils._acme.acme import ( |
| 165 |
ACMEClient, |
| 166 |
create_backend, |
| 167 |
create_default_argspec, |
| 168 |
) |
| 169 |
from ansible_collections.community.crypto.plugins.module_utils._acme.errors import ( |
| 170 |
ModuleFailException, |
| 171 |
) |
| 172 |
from ansible_collections.community.crypto.plugins.module_utils._acme.io import read_file |
| 173 |
from ansible_collections.community.crypto.plugins.module_utils._acme.utils import ( |
| 174 |
compute_cert_id, |
| 175 |
) |
| 176 |
|
| 177 |
|
| 178 |
def main() -> t.NoReturn: |
| 179 |
argument_spec = create_default_argspec(with_account=False) |
| 180 |
argument_spec.update_argspec( |
| 181 |
certificate_path={"type": "path"}, |
| 182 |
certificate_content={"type": "str"}, |
| 183 |
use_ari={"type": "bool", "default": True}, |
| 184 |
ari_algorithm={ |
| 185 |
"type": "str", |
| 186 |
"choices": ["standard", "start"], |
| 187 |
"default": "standard", |
| 188 |
}, |
| 189 |
remaining_days={"type": "int"}, |
| 190 |
remaining_percentage={"type": "float"}, |
| 191 |
now={"type": "str"}, |
| 192 |
treat_parsing_error_as_non_existing={"type": "bool", "default": False}, |
| 193 |
) |
| 194 |
argument_spec.update( |
| 195 |
mutually_exclusive=[("certificate_path", "certificate_content")], |
| 196 |
) |
| 197 |
module = argument_spec.create_ansible_module(supports_check_mode=True) |
| 198 |
backend = create_backend(module, needs_acme_v2=True) |
| 199 |
|
| 200 |
result = { |
| 201 |
"changed": False, |
| 202 |
"msg": "The certificate is still valid and no condition was reached", |
| 203 |
"exists": False, |
| 204 |
"parsable": False, |
| 205 |
"supports_ari": False, |
| 206 |
} |
| 207 |
|
| 208 |
def complete(should_renew: bool, **kwargs: t.Any) -> t.NoReturn: |
| 209 |
result["should_renew"] = should_renew |
| 210 |
result.update(kwargs) |
| 211 |
module.exit_json(**result) |
| 212 |
|
| 213 |
if ( |
| 214 |
not module.params["certificate_path"] |
| 215 |
and not module.params["certificate_content"] |
| 216 |
): |
| 217 |
complete(True, msg="No certificate was specified") |
| 218 |
|
| 219 |
if module.params["certificate_path"] is not None: |
| 220 |
if not os.path.exists(module.params["certificate_path"]): |
| 221 |
complete(True, msg="The certificate file does not exist") |
| 222 |
if module.params["treat_parsing_error_as_non_existing"]: |
| 223 |
try: |
| 224 |
read_file(module.params["certificate_path"]) |
| 225 |
except ModuleFailException as e: |
| 226 |
e.do_fail(module=module) |
| 227 |
|
| 228 |
result["exists"] = True |
| 229 |
try: |
| 230 |
cert_info = backend.get_cert_information( |
| 231 |
cert_filename=module.params["certificate_path"], |
| 232 |
cert_content=module.params["certificate_content"], |
| 233 |
) |
| 234 |
except ModuleFailException as e: |
| 235 |
if module.params["treat_parsing_error_as_non_existing"]: |
| 236 |
complete(True, msg=f"Certificate cannot be parsed: {e.msg}") |
| 237 |
e.do_fail(module=module) |
| 238 |
|
| 239 |
result["parsable"] = True |
| 240 |
try: |
| 241 |
cert_id = compute_cert_id( |
| 242 |
backend=backend, |
| 243 |
cert_info=cert_info, |
| 244 |
none_if_required_information_is_missing=True, |
| 245 |
) |
| 246 |
if cert_id is not None: |
| 247 |
result["cert_id"] = cert_id |
| 248 |
|
| 249 |
if module.params["now"]: |
| 250 |
now = backend.parse_module_parameter(value=module.params["now"], name="now") |
| 251 |
else: |
| 252 |
now = backend.get_now() |
| 253 |
|
| 254 |
if now >= cert_info.not_valid_after: |
| 255 |
complete(True, msg="The certificate has already expired") |
| 256 |
|
| 257 |
client = ACMEClient(module=module, backend=backend) |
| 258 |
if ( |
| 259 |
cert_id is not None |
| 260 |
and module.params["use_ari"] |
| 261 |
and client.directory.has_renewal_info_endpoint() |
| 262 |
): |
| 263 |
renewal_info = client.get_renewal_info(cert_id=cert_id) |
| 264 |
window_start = backend.parse_acme_timestamp( |
| 265 |
renewal_info["suggestedWindow"]["start"] |
| 266 |
) |
| 267 |
window_end = backend.parse_acme_timestamp( |
| 268 |
renewal_info["suggestedWindow"]["end"] |
| 269 |
) |
| 270 |
msg_append = "" |
| 271 |
if "explanationURL" in renewal_info: |
| 272 |
msg_append = f". Information on renewal interval: {renewal_info['explanationURL']}" |
| 273 |
result["supports_ari"] = True |
| 274 |
if now > window_end: |
| 275 |
complete( |
| 276 |
True, |
| 277 |
msg=f"The suggested renewal interval provided by ARI is in the past{msg_append}", |
| 278 |
) |
| 279 |
if module.params["ari_algorithm"] == "start": |
| 280 |
if now > window_start: |
| 281 |
complete( |
| 282 |
True, |
| 283 |
msg=f"The suggested renewal interval provided by ARI has begun{msg_append}", |
| 284 |
) |
| 285 |
else: |
| 286 |
random_time = backend.interpolate_timestamp( |
| 287 |
window_start, window_end, percentage=random.random() |
| 288 |
) |
| 289 |
if now > random_time: |
| 290 |
complete( |
| 291 |
True, |
| 292 |
msg=f"The picked random renewal time {random_time} in suggested renewal interval provided by ARI is in the past{msg_append}", |
| 293 |
) |
| 294 |
|
| 295 |
if module.params["remaining_days"] is not None: |
| 296 |
remaining_days = (cert_info.not_valid_after - now).days |
| 297 |
if remaining_days < module.params["remaining_days"]: |
| 298 |
complete( |
| 299 |
True, |
| 300 |
msg=f"The certificate expires in {remaining_days} days", |
| 301 |
) |
| 302 |
|
| 303 |
if module.params["remaining_percentage"] is not None: |
| 304 |
timestamp = backend.interpolate_timestamp( |
| 305 |
cert_info.not_valid_before, |
| 306 |
cert_info.not_valid_after, |
| 307 |
percentage=1 - module.params["remaining_percentage"], |
| 308 |
) |
| 309 |
if timestamp < now: |
| 310 |
complete( |
| 311 |
True, |
| 312 |
msg=f"The remaining percentage {module.params['remaining_percentage'] * 100}% of the certificate's lifespan was reached on {timestamp}", |
| 313 |
) |
| 314 |
|
| 315 |
complete(False) |
| 316 |
except ModuleFailException as e: |
| 317 |
e.do_fail(module=module) |
| 318 |
|
| 319 |
|
| 320 |
if __name__ == "__main__": |
| 321 |
main() |