| 1 |
#!/usr/bin/python |
| 2 |
# Copyright (c) 2024 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 = """ |
| 9 |
module: acme_certificate_order_validate |
| 10 |
author: Felix Fontein (@felixfontein) |
| 11 |
version_added: 2.24.0 |
| 12 |
short_description: Validate authorizations of an ACME v2 order |
| 13 |
description: |
| 14 |
- Validates pending authorizations of an ACME v2 order. |
| 15 |
This is the second to last step of obtaining a new certificate with the |
| 16 |
L(ACME protocol,https://tools.ietf.org/html/rfc8555) from a Certificate |
| 17 |
Authority such as L(Let's Encrypt,https://letsencrypt.org/). |
| 18 |
This module does not support ACME v1, the original version of the ACME protocol before standardization. |
| 19 |
- This module needs to be used in conjunction with the |
| 20 |
M(community.crypto.acme_certificate_order_create) and |
| 21 |
M(community.crypto.acme_certificate_order_finalize) modules. |
| 22 |
seealso: |
| 23 |
- module: community.crypto.acme_certificate_order_create |
| 24 |
description: Create an ACME order. |
| 25 |
- module: community.crypto.acme_certificate_order_finalize |
| 26 |
description: Finalize an ACME order after satisfying the challenges. |
| 27 |
- module: community.crypto.acme_certificate_order_info |
| 28 |
description: Obtain information for an ACME order. |
| 29 |
- name: The Let's Encrypt documentation |
| 30 |
description: Documentation for the Let's Encrypt Certification Authority. |
| 31 |
Provides useful information for example on rate limits. |
| 32 |
link: https://letsencrypt.org/docs/ |
| 33 |
- name: Automatic Certificate Management Environment (ACME) |
| 34 |
description: The specification of the ACME protocol (RFC 8555). |
| 35 |
link: https://tools.ietf.org/html/rfc8555 |
| 36 |
- name: ACME TLS ALPN Challenge Extension |
| 37 |
description: The specification of the V(tls-alpn-01) challenge (RFC 8737). |
| 38 |
link: https://www.rfc-editor.org/rfc/rfc8737.html |
| 39 |
- module: community.crypto.acme_challenge_cert_helper |
| 40 |
description: Helps preparing V(tls-alpn-01) challenges. |
| 41 |
- module: community.crypto.acme_inspect |
| 42 |
description: Allows to debug problems. |
| 43 |
- module: community.crypto.acme_certificate_deactivate_authz |
| 44 |
description: Allows to deactivate (invalidate) ACME v2 orders. |
| 45 |
extends_documentation_fragment: |
| 46 |
- community.crypto._acme.basic |
| 47 |
- community.crypto._acme.account |
| 48 |
- community.crypto._attributes |
| 49 |
- community.crypto._attributes.actiongroup_acme |
| 50 |
- community.crypto._attributes.files |
| 51 |
attributes: |
| 52 |
check_mode: |
| 53 |
support: none |
| 54 |
diff_mode: |
| 55 |
support: none |
| 56 |
safe_file_operations: |
| 57 |
support: full |
| 58 |
idempotent: |
| 59 |
support: full |
| 60 |
options: |
| 61 |
challenge: |
| 62 |
description: |
| 63 |
- The challenge to be performed for every pending authorization. |
| 64 |
- Must be provided if there is at least one pending authorization. |
| 65 |
- In case of authorization reuse, or in case of CAs which use External Account Binding |
| 66 |
and other means of validating certificate assurance, it might not be necessary |
| 67 |
to provide this option. |
| 68 |
- Support for V(dns-account-01) and V(dns-persist-01) has been added in community.crypto 3.2.0. |
| 69 |
type: str |
| 70 |
choices: |
| 71 |
- http-01 |
| 72 |
- dns-01 |
| 73 |
- dns-account-01 |
| 74 |
- dns-persist-01 |
| 75 |
- tls-alpn-01 |
| 76 |
order_uri: |
| 77 |
description: |
| 78 |
- The order URI provided by RV(community.crypto.acme_certificate_order_create#module:order_uri). |
| 79 |
type: str |
| 80 |
required: true |
| 81 |
deactivate_authzs: |
| 82 |
description: |
| 83 |
- "Deactivate authentication objects (authz) in case an error happens." |
| 84 |
- "Authentication objects are bound to an account key and remain valid |
| 85 |
for a certain amount of time, and can be used to issue certificates |
| 86 |
without having to re-authenticate the domain. This can be a security |
| 87 |
concern." |
| 88 |
type: bool |
| 89 |
default: true |
| 90 |
""" |
| 91 |
|
| 92 |
EXAMPLES = r""" |
| 93 |
--- |
| 94 |
### Example with HTTP-01 challenge ### |
| 95 |
|
| 96 |
- name: Create a challenge for sample.com using a account key from a variable |
| 97 |
community.crypto.acme_certificate_order_create: |
| 98 |
account_key_content: "{{ account_private_key }}" |
| 99 |
csr: /etc/pki/cert/csr/sample.com.csr |
| 100 |
register: sample_com_challenge |
| 101 |
|
| 102 |
# Alternative first step: |
| 103 |
- name: Create a challenge for sample.com using a account key from Hashi Vault |
| 104 |
community.crypto.acme_certificate_order_create: |
| 105 |
account_key_content: >- |
| 106 |
{{ lookup('community.hashi_vault.hashi_vault', 'secret=secret/account_private_key:value') }} |
| 107 |
csr: /etc/pki/cert/csr/sample.com.csr |
| 108 |
register: sample_com_challenge |
| 109 |
|
| 110 |
# Alternative first step: |
| 111 |
- name: Create a challenge for sample.com using a account key file |
| 112 |
community.crypto.acme_certificate_order_create: |
| 113 |
account_key_src: /etc/pki/cert/private/account.key |
| 114 |
csr_content: "{{ lookup('file', '/etc/pki/cert/csr/sample.com.csr') }}" |
| 115 |
register: sample_com_challenge |
| 116 |
|
| 117 |
# Perform the necessary steps to fulfill the challenge. For example: |
| 118 |
# |
| 119 |
# - name: Copy http-01 challenges |
| 120 |
# ansible.builtin.copy: |
| 121 |
# dest: /var/www/{{ item.identifier }}/{{ item.challenges['http-01'].resource }} |
| 122 |
# content: "{{ item.challenges['http-01'].resource_value }}" |
| 123 |
# loop: "{{ sample_com_challenge.challenge_data }}" |
| 124 |
# when: "'http-01' in item.challenges" |
| 125 |
|
| 126 |
- name: Let the challenge be validated |
| 127 |
community.crypto.acme_certificate_order_validate: |
| 128 |
account_key_src: /etc/pki/cert/private/account.key |
| 129 |
order_uri: "{{ sample_com_challenge.order_uri }}" |
| 130 |
challenge: http-01 |
| 131 |
|
| 132 |
- name: Retrieve the cert and intermediate certificate |
| 133 |
community.crypto.acme_certificate_order_finalize: |
| 134 |
account_key_src: /etc/pki/cert/private/account.key |
| 135 |
csr: /etc/pki/cert/csr/sample.com.csr |
| 136 |
order_uri: "{{ sample_com_challenge.order_uri }}" |
| 137 |
cert_dest: /etc/httpd/ssl/sample.com.crt |
| 138 |
fullchain_dest: /etc/httpd/ssl/sample.com-fullchain.crt |
| 139 |
chain_dest: /etc/httpd/ssl/sample.com-intermediate.crt |
| 140 |
|
| 141 |
--- |
| 142 |
### Example with DNS challenge against production ACME server ### |
| 143 |
|
| 144 |
- name: Create a challenge for sample.com using a account key file. |
| 145 |
community.crypto.acme_certificate_order_create: |
| 146 |
acme_directory: https://acme-v01.api.letsencrypt.org/directory |
| 147 |
account_key_src: /etc/pki/cert/private/account.key |
| 148 |
csr: /etc/pki/cert/csr/sample.com.csr |
| 149 |
register: sample_com_challenge |
| 150 |
|
| 151 |
# Perform the necessary steps to fulfill the challenge. For example: |
| 152 |
# |
| 153 |
# - name: Create DNS records for dns-01 challenges |
| 154 |
# community.aws.route53: |
| 155 |
# zone: sample.com |
| 156 |
# record: "{{ item.key }}" |
| 157 |
# type: TXT |
| 158 |
# ttl: 60 |
| 159 |
# state: present |
| 160 |
# wait: true |
| 161 |
# # Note: item.value is a list of TXT entries, and route53 |
| 162 |
# # requires every entry to be enclosed in quotes |
| 163 |
# value: "{{ item.value | map('community.dns.quote_txt', always_quote=true) | list }}" |
| 164 |
# loop: "{{ sample_com_challenge.challenge_data_dns | dict2items }}" |
| 165 |
|
| 166 |
- name: Let the challenge be validated |
| 167 |
community.crypto.acme_certificate_order_validate: |
| 168 |
acme_directory: https://acme-v01.api.letsencrypt.org/directory |
| 169 |
account_key_src: /etc/pki/cert/private/account.key |
| 170 |
order_uri: "{{ sample_com_challenge.order_uri }}" |
| 171 |
challenge: dns-01 |
| 172 |
|
| 173 |
- name: Retrieve the cert and intermediate certificate |
| 174 |
community.crypto.acme_certificate_order_finalize: |
| 175 |
acme_directory: https://acme-v01.api.letsencrypt.org/directory |
| 176 |
account_key_src: /etc/pki/cert/private/account.key |
| 177 |
csr: /etc/pki/cert/csr/sample.com.csr |
| 178 |
order_uri: "{{ sample_com_challenge.order_uri }}" |
| 179 |
cert_dest: /etc/httpd/ssl/sample.com.crt |
| 180 |
fullchain_dest: /etc/httpd/ssl/sample.com-fullchain.crt |
| 181 |
chain_dest: /etc/httpd/ssl/sample.com-intermediate.crt |
| 182 |
""" |
| 183 |
|
| 184 |
RETURN = """ |
| 185 |
account_uri: |
| 186 |
description: ACME account URI. |
| 187 |
returned: success |
| 188 |
type: str |
| 189 |
validating_challenges: |
| 190 |
description: List of challenges whose validation was triggered. |
| 191 |
returned: success |
| 192 |
type: list |
| 193 |
elements: dict |
| 194 |
contains: |
| 195 |
identifier: |
| 196 |
description: |
| 197 |
- The identifier the challenge is for. |
| 198 |
type: str |
| 199 |
returned: always |
| 200 |
identifier_type: |
| 201 |
description: |
| 202 |
- The identifier's type for the challenge. |
| 203 |
type: str |
| 204 |
returned: always |
| 205 |
choices: |
| 206 |
- dns |
| 207 |
- ip |
| 208 |
authz_url: |
| 209 |
description: |
| 210 |
- The URL of the authorization object for this challenge. |
| 211 |
type: str |
| 212 |
returned: always |
| 213 |
challenge_type: |
| 214 |
description: |
| 215 |
- The challenge's type. |
| 216 |
type: str |
| 217 |
returned: always |
| 218 |
choices: |
| 219 |
- http-01 |
| 220 |
- dns-01 |
| 221 |
- tls-alpn-01 |
| 222 |
challenge_url: |
| 223 |
description: |
| 224 |
- The URL of the challenge object. |
| 225 |
type: str |
| 226 |
returned: always |
| 227 |
""" |
| 228 |
|
| 229 |
import typing as t |
| 230 |
|
| 231 |
from ansible_collections.community.crypto.plugins.module_utils._acme.acme import ( |
| 232 |
create_backend, |
| 233 |
create_default_argspec, |
| 234 |
) |
| 235 |
from ansible_collections.community.crypto.plugins.module_utils._acme.certificate import ( |
| 236 |
ACMECertificateClient, |
| 237 |
) |
| 238 |
from ansible_collections.community.crypto.plugins.module_utils._acme.errors import ( |
| 239 |
ModuleFailException, |
| 240 |
) |
| 241 |
|
| 242 |
if t.TYPE_CHECKING: |
| 243 |
from ansible_collections.community.crypto.plugins.module_utils._acme.challenges import ( # pragma: no cover |
| 244 |
Authorization, |
| 245 |
) |
| 246 |
|
| 247 |
|
| 248 |
def main() -> t.NoReturn: |
| 249 |
argument_spec = create_default_argspec(with_certificate=False) |
| 250 |
argument_spec.update_argspec( |
| 251 |
order_uri={"type": "str", "required": True}, |
| 252 |
challenge={ |
| 253 |
"type": "str", |
| 254 |
"choices": [ |
| 255 |
"http-01", |
| 256 |
"dns-01", |
| 257 |
"dns-account-01", |
| 258 |
"dns-persist-01", |
| 259 |
"tls-alpn-01", |
| 260 |
], |
| 261 |
}, |
| 262 |
deactivate_authzs={"type": "bool", "default": True}, |
| 263 |
) |
| 264 |
module = argument_spec.create_ansible_module() |
| 265 |
|
| 266 |
backend = create_backend(module, needs_acme_v2=False) |
| 267 |
|
| 268 |
try: |
| 269 |
client = ACMECertificateClient(module=module, backend=backend) |
| 270 |
done = False |
| 271 |
order = None |
| 272 |
try: |
| 273 |
# Step 1: load order |
| 274 |
order = client.load_order() |
| 275 |
client.check_that_authorizations_can_be_used(order) |
| 276 |
|
| 277 |
# Step 2: find all pending authorizations |
| 278 |
pending_authzs = client.collect_pending_authzs(order) |
| 279 |
|
| 280 |
# Step 3: figure out challenges to use |
| 281 |
challenges = {} |
| 282 |
for authz in pending_authzs: |
| 283 |
challenges[authz.combined_identifier] = module.params["challenge"] |
| 284 |
|
| 285 |
missing_challenge_authzs = [k for k, v in challenges.items() if v is None] |
| 286 |
if missing_challenge_authzs: |
| 287 |
missing_challenge_authzs_str = ", ".join( |
| 288 |
sorted(missing_challenge_authzs) |
| 289 |
) |
| 290 |
raise ModuleFailException( |
| 291 |
"The challenge parameter must be supplied if there are pending authorizations." |
| 292 |
f" The following authorizations are pending: {missing_challenge_authzs_str}" |
| 293 |
) |
| 294 |
|
| 295 |
bad_challenge_authzs = [ |
| 296 |
authz.combined_identifier |
| 297 |
for authz in pending_authzs |
| 298 |
if authz.find_challenge( |
| 299 |
challenge_type=challenges[authz.combined_identifier] |
| 300 |
) |
| 301 |
is None |
| 302 |
] |
| 303 |
if bad_challenge_authzs: |
| 304 |
authz_challenges_pairs = ", ".join( |
| 305 |
sorted( |
| 306 |
f"{authz} with {challenges[authz]}" |
| 307 |
for authz in bad_challenge_authzs |
| 308 |
) |
| 309 |
) |
| 310 |
raise ModuleFailException( |
| 311 |
f"The following authorizations do not support the selected challenges: {authz_challenges_pairs}" |
| 312 |
) |
| 313 |
|
| 314 |
def is_pending(authz: Authorization) -> bool: |
| 315 |
challenge_name = challenges[authz.combined_identifier] |
| 316 |
challenge_obj = authz.find_challenge(challenge_type=challenge_name) |
| 317 |
return challenge_obj is not None and challenge_obj.status == "pending" |
| 318 |
|
| 319 |
really_pending_authzs = [ |
| 320 |
authz for authz in pending_authzs if is_pending(authz) |
| 321 |
] |
| 322 |
|
| 323 |
# Step 4: validate pending authorizations |
| 324 |
authzs_with_challenges_to_wait_for = client.call_validate( |
| 325 |
really_pending_authzs, |
| 326 |
get_challenge=lambda authz: challenges[authz.combined_identifier], |
| 327 |
wait=False, |
| 328 |
) |
| 329 |
|
| 330 |
done = True |
| 331 |
finally: |
| 332 |
if order and module.params["deactivate_authzs"] and not done: |
| 333 |
client.deactivate_authzs(order) |
| 334 |
module.exit_json( |
| 335 |
changed=len(authzs_with_challenges_to_wait_for) > 0, |
| 336 |
account_uri=client.client.account_uri, |
| 337 |
validating_challenges=[ |
| 338 |
{ |
| 339 |
"identifier": authz.identifier, |
| 340 |
"identifier_type": authz.identifier_type, |
| 341 |
"authz_url": authz.url, |
| 342 |
"challenge_type": challenge_type, |
| 343 |
"challenge_url": challenge.url if challenge else None, |
| 344 |
} |
| 345 |
for authz, challenge_type, challenge in authzs_with_challenges_to_wait_for |
| 346 |
], |
| 347 |
) |
| 348 |
except ModuleFailException as e: |
| 349 |
e.do_fail(module=module) |
| 350 |
|
| 351 |
|
| 352 |
if __name__ == "__main__": |
| 353 |
main() |