| 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_finalize |
| 10 |
author: Felix Fontein (@felixfontein) |
| 11 |
version_added: 2.24.0 |
| 12 |
short_description: Finalize an ACME v2 order |
| 13 |
description: |
| 14 |
- Finalizes an ACME v2 order and obtains the certificate and certificate chains. |
| 15 |
This is the final 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_validate) modules. |
| 22 |
seealso: |
| 23 |
- module: community.crypto.acme_certificate_order_create |
| 24 |
description: Create an ACME order. |
| 25 |
- module: community.crypto.acme_certificate_order_validate |
| 26 |
description: Validate pending authorizations of an ACME order. |
| 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 |
- module: community.crypto.certificate_complete_chain |
| 37 |
description: Allows to find the root certificate for the returned fullchain. |
| 38 |
- module: community.crypto.acme_certificate_revoke |
| 39 |
description: Allows to revoke certificates. |
| 40 |
- module: community.crypto.acme_inspect |
| 41 |
description: Allows to debug problems. |
| 42 |
- module: community.crypto.acme_certificate_deactivate_authz |
| 43 |
description: Allows to deactivate (invalidate) ACME v2 orders. |
| 44 |
extends_documentation_fragment: |
| 45 |
- community.crypto._acme.basic |
| 46 |
- community.crypto._acme.account |
| 47 |
- community.crypto._acme.certificate |
| 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 |
order_uri: |
| 62 |
description: |
| 63 |
- The order URI provided by RV(community.crypto.acme_certificate_order_create#module:order_uri). |
| 64 |
type: str |
| 65 |
required: true |
| 66 |
cert_dest: |
| 67 |
description: |
| 68 |
- "The destination file for the certificate." |
| 69 |
type: path |
| 70 |
fullchain_dest: |
| 71 |
description: |
| 72 |
- "The destination file for the full chain (that is, a certificate followed |
| 73 |
by chain of intermediate certificates)." |
| 74 |
type: path |
| 75 |
chain_dest: |
| 76 |
description: |
| 77 |
- If specified, the intermediate certificate will be written to this file. |
| 78 |
type: path |
| 79 |
deactivate_authzs: |
| 80 |
description: |
| 81 |
- "Deactivate authentication objects (authz) after issuing a certificate, |
| 82 |
or when issuing the certificate failed." |
| 83 |
- V(never) never deactivates them. |
| 84 |
- V(always) always deactivates them in cases of errors or when the certificate was issued. |
| 85 |
- V(on_error) only deactivates them in case of errors. |
| 86 |
- V(on_success) only deactivates them in case the certificate was successfully issued. |
| 87 |
- "Authentication objects are bound to an account key and remain valid |
| 88 |
for a certain amount of time, and can be used to issue certificates |
| 89 |
without having to re-authenticate the domain. This can be a security |
| 90 |
concern." |
| 91 |
type: str |
| 92 |
choices: |
| 93 |
- never |
| 94 |
- on_error |
| 95 |
- on_success |
| 96 |
- always |
| 97 |
default: always |
| 98 |
retrieve_all_alternates: |
| 99 |
description: |
| 100 |
- "When set to V(true), will retrieve all alternate trust chains offered by the ACME CA. |
| 101 |
These will not be written to disk, but will be returned together with the main |
| 102 |
chain as RV(all_chains). See the documentation for the RV(all_chains) return |
| 103 |
value for details." |
| 104 |
type: bool |
| 105 |
default: false |
| 106 |
select_chain: |
| 107 |
description: |
| 108 |
- "Allows to specify criteria by which an (alternate) trust chain can be selected." |
| 109 |
- "The list of criteria will be processed one by one until a chain is found |
| 110 |
matching a criterium. If such a chain is found, it will be used by the |
| 111 |
module instead of the default chain." |
| 112 |
- "If a criterium matches multiple chains, the first one matching will be |
| 113 |
returned. The order is determined by the ordering of the C(Link) headers |
| 114 |
returned by the ACME server and might not be deterministic." |
| 115 |
- "Every criterium can consist of multiple different conditions, like O(select_chain[].issuer) |
| 116 |
and O(select_chain[].subject). For the criterium to match a chain, all conditions must apply |
| 117 |
to the same certificate in the chain." |
| 118 |
- "This option can only be used with the C(cryptography) backend." |
| 119 |
type: list |
| 120 |
elements: dict |
| 121 |
suboptions: |
| 122 |
test_certificates: |
| 123 |
description: |
| 124 |
- "Determines which certificates in the chain will be tested." |
| 125 |
- "V(all) tests all certificates in the chain (excluding the leaf, which is |
| 126 |
identical in all chains)." |
| 127 |
- "V(first) only tests the first certificate in the chain, that is the one which |
| 128 |
signed the leaf." |
| 129 |
- "V(last) only tests the last certificate in the chain, that is the one furthest |
| 130 |
away from the leaf. Its issuer is the root certificate of this chain." |
| 131 |
type: str |
| 132 |
default: all |
| 133 |
choices: [first, last, all] |
| 134 |
issuer: |
| 135 |
description: |
| 136 |
- "Allows to specify parts of the issuer of a certificate in the chain must |
| 137 |
have to be selected." |
| 138 |
- "If O(select_chain[].issuer) is empty, any certificate will match." |
| 139 |
- 'An example value would be V({"commonName": "My Preferred CA Root"}).' |
| 140 |
type: dict |
| 141 |
subject: |
| 142 |
description: |
| 143 |
- "Allows to specify parts of the subject of a certificate in the chain must |
| 144 |
have to be selected." |
| 145 |
- "If O(select_chain[].subject) is empty, any certificate will match." |
| 146 |
- 'An example value would be V({"CN": "My Preferred CA Intermediate"})' |
| 147 |
type: dict |
| 148 |
subject_key_identifier: |
| 149 |
description: |
| 150 |
- "Checks for the SubjectKeyIdentifier extension. This is an identifier based |
| 151 |
on the private key of the intermediate certificate." |
| 152 |
- "The identifier must be of the form |
| 153 |
V(A8:4A:6A:63:04:7D:DD:BA:E6:D1:39:B7:A6:45:65:EF:F3:A8:EC:A1)." |
| 154 |
type: str |
| 155 |
authority_key_identifier: |
| 156 |
description: |
| 157 |
- "Checks for the AuthorityKeyIdentifier extension. This is an identifier based |
| 158 |
on the private key of the issuer of the intermediate certificate." |
| 159 |
- "The identifier must be of the form |
| 160 |
V(C4:A7:B1:A4:7B:2C:71:FA:DB:E1:4B:90:75:FF:C4:15:60:85:89:10)." |
| 161 |
type: str |
| 162 |
""" |
| 163 |
|
| 164 |
EXAMPLES = r""" |
| 165 |
--- |
| 166 |
### Example with HTTP-01 challenge ### |
| 167 |
|
| 168 |
- name: Create a challenge for sample.com using a account key from a variable |
| 169 |
community.crypto.acme_certificate_order_create: |
| 170 |
account_key_content: "{{ account_private_key }}" |
| 171 |
csr: /etc/pki/cert/csr/sample.com.csr |
| 172 |
register: sample_com_challenge |
| 173 |
|
| 174 |
# Alternative first step: |
| 175 |
- name: Create a challenge for sample.com using a account key from Hashi Vault |
| 176 |
community.crypto.acme_certificate_order_create: |
| 177 |
account_key_content: >- |
| 178 |
{{ lookup('community.hashi_vault.hashi_vault', 'secret=secret/account_private_key:value') }} |
| 179 |
csr: /etc/pki/cert/csr/sample.com.csr |
| 180 |
register: sample_com_challenge |
| 181 |
|
| 182 |
# Alternative first step: |
| 183 |
- name: Create a challenge for sample.com using a account key file |
| 184 |
community.crypto.acme_certificate_order_create: |
| 185 |
account_key_src: /etc/pki/cert/private/account.key |
| 186 |
csr_content: "{{ lookup('file', '/etc/pki/cert/csr/sample.com.csr') }}" |
| 187 |
register: sample_com_challenge |
| 188 |
|
| 189 |
# Perform the necessary steps to fulfill the challenge. For example: |
| 190 |
# |
| 191 |
# - name: Copy http-01 challenges |
| 192 |
# ansible.builtin.copy: |
| 193 |
# dest: /var/www/{{ item.identifier }}/{{ item.challenges['http-01'].resource }} |
| 194 |
# content: "{{ item.challenges['http-01'].resource_value }}" |
| 195 |
# loop: "{{ sample_com_challenge.challenge_data }}" |
| 196 |
# when: "'http-01' in item.challenges" |
| 197 |
|
| 198 |
- name: Let the challenge be validated |
| 199 |
community.crypto.acme_certificate_order_validate: |
| 200 |
account_key_src: /etc/pki/cert/private/account.key |
| 201 |
order_uri: "{{ sample_com_challenge.order_uri }}" |
| 202 |
challenge: http-01 |
| 203 |
|
| 204 |
- name: Retrieve the cert and intermediate certificate |
| 205 |
community.crypto.acme_certificate_order_finalize: |
| 206 |
account_key_src: /etc/pki/cert/private/account.key |
| 207 |
csr: /etc/pki/cert/csr/sample.com.csr |
| 208 |
order_uri: "{{ sample_com_challenge.order_uri }}" |
| 209 |
cert_dest: /etc/httpd/ssl/sample.com.crt |
| 210 |
fullchain_dest: /etc/httpd/ssl/sample.com-fullchain.crt |
| 211 |
chain_dest: /etc/httpd/ssl/sample.com-intermediate.crt |
| 212 |
|
| 213 |
--- |
| 214 |
### Example with DNS challenge against production ACME server ### |
| 215 |
|
| 216 |
- name: Create a challenge for sample.com using a account key file. |
| 217 |
community.crypto.acme_certificate_order_create: |
| 218 |
acme_directory: https://acme-v01.api.letsencrypt.org/directory |
| 219 |
account_key_src: /etc/pki/cert/private/account.key |
| 220 |
csr: /etc/pki/cert/csr/sample.com.csr |
| 221 |
register: sample_com_challenge |
| 222 |
|
| 223 |
# Perform the necessary steps to fulfill the challenge. For example: |
| 224 |
# |
| 225 |
# - name: Create DNS records for dns-01 challenges |
| 226 |
# community.aws.route53: |
| 227 |
# zone: sample.com |
| 228 |
# record: "{{ item.key }}" |
| 229 |
# type: TXT |
| 230 |
# ttl: 60 |
| 231 |
# state: present |
| 232 |
# wait: true |
| 233 |
# # Note: item.value is a list of TXT entries, and route53 |
| 234 |
# # requires every entry to be enclosed in quotes |
| 235 |
# value: "{{ item.value | map('community.dns.quote_txt', always_quote=true) | list }}" |
| 236 |
# loop: "{{ sample_com_challenge.challenge_data_dns | dict2items }}" |
| 237 |
|
| 238 |
- name: Let the challenge be validated |
| 239 |
community.crypto.acme_certificate_order_validate: |
| 240 |
acme_directory: https://acme-v01.api.letsencrypt.org/directory |
| 241 |
account_key_src: /etc/pki/cert/private/account.key |
| 242 |
order_uri: "{{ sample_com_challenge.order_uri }}" |
| 243 |
challenge: dns-01 |
| 244 |
|
| 245 |
- name: Retrieve the cert and intermediate certificate |
| 246 |
community.crypto.acme_certificate_order_finalize: |
| 247 |
acme_directory: https://acme-v01.api.letsencrypt.org/directory |
| 248 |
account_key_src: /etc/pki/cert/private/account.key |
| 249 |
csr: /etc/pki/cert/csr/sample.com.csr |
| 250 |
order_uri: "{{ sample_com_challenge.order_uri }}" |
| 251 |
cert_dest: /etc/httpd/ssl/sample.com.crt |
| 252 |
fullchain_dest: /etc/httpd/ssl/sample.com-fullchain.crt |
| 253 |
chain_dest: /etc/httpd/ssl/sample.com-intermediate.crt |
| 254 |
""" |
| 255 |
|
| 256 |
RETURN = """ |
| 257 |
account_uri: |
| 258 |
description: ACME account URI. |
| 259 |
returned: success |
| 260 |
type: str |
| 261 |
all_chains: |
| 262 |
description: |
| 263 |
- When O(retrieve_all_alternates=true), the module will query the ACME server for |
| 264 |
alternate chains. This return value will contain a list of all chains returned, |
| 265 |
the first entry being the main chain returned by the server. |
| 266 |
- See L(Section 7.4.2 of RFC8555,https://tools.ietf.org/html/rfc8555#section-7.4.2) |
| 267 |
for details. |
| 268 |
returned: success and O(retrieve_all_alternates=true) |
| 269 |
type: list |
| 270 |
elements: dict |
| 271 |
contains: |
| 272 |
cert: |
| 273 |
description: |
| 274 |
- The leaf certificate itself, in PEM format. |
| 275 |
type: str |
| 276 |
returned: always |
| 277 |
chain: |
| 278 |
description: |
| 279 |
- The certificate chain, excluding the root, as concatenated PEM certificates. |
| 280 |
type: str |
| 281 |
returned: always |
| 282 |
full_chain: |
| 283 |
description: |
| 284 |
- The certificate chain, excluding the root, but including the leaf certificate, |
| 285 |
as concatenated PEM certificates. |
| 286 |
type: str |
| 287 |
returned: always |
| 288 |
selected_chain: |
| 289 |
description: |
| 290 |
- The selected certificate chain. |
| 291 |
- If O(select_chain) is not specified, this will be the main chain returned by the |
| 292 |
ACME server. |
| 293 |
returned: success |
| 294 |
type: dict |
| 295 |
contains: |
| 296 |
cert: |
| 297 |
description: |
| 298 |
- The leaf certificate itself, in PEM format. |
| 299 |
type: str |
| 300 |
returned: always |
| 301 |
chain: |
| 302 |
description: |
| 303 |
- The certificate chain, excluding the root, as concatenated PEM certificates. |
| 304 |
type: str |
| 305 |
returned: always |
| 306 |
full_chain: |
| 307 |
description: |
| 308 |
- The certificate chain, excluding the root, but including the leaf certificate, |
| 309 |
as concatenated PEM certificates. |
| 310 |
type: str |
| 311 |
returned: always |
| 312 |
""" |
| 313 |
|
| 314 |
import typing as t |
| 315 |
|
| 316 |
from ansible_collections.community.crypto.plugins.module_utils._acme.acme import ( |
| 317 |
create_backend, |
| 318 |
create_default_argspec, |
| 319 |
) |
| 320 |
from ansible_collections.community.crypto.plugins.module_utils._acme.certificate import ( |
| 321 |
ACMECertificateClient, |
| 322 |
) |
| 323 |
from ansible_collections.community.crypto.plugins.module_utils._acme.errors import ( |
| 324 |
ModuleFailException, |
| 325 |
) |
| 326 |
|
| 327 |
if t.TYPE_CHECKING: |
| 328 |
from ansible_collections.community.crypto.plugins.module_utils._acme.certificates import ( # pragma: no cover |
| 329 |
CertificateChain, |
| 330 |
) |
| 331 |
|
| 332 |
|
| 333 |
def main() -> t.NoReturn: |
| 334 |
argument_spec = create_default_argspec(with_certificate=True) |
| 335 |
argument_spec.update_argspec( |
| 336 |
order_uri={"type": "str", "required": True}, |
| 337 |
cert_dest={"type": "path"}, |
| 338 |
fullchain_dest={"type": "path"}, |
| 339 |
chain_dest={"type": "path"}, |
| 340 |
deactivate_authzs={ |
| 341 |
"type": "str", |
| 342 |
"default": "always", |
| 343 |
"choices": ["never", "always", "on_error", "on_success"], |
| 344 |
}, |
| 345 |
retrieve_all_alternates={"type": "bool", "default": False}, |
| 346 |
select_chain={ |
| 347 |
"type": "list", |
| 348 |
"elements": "dict", |
| 349 |
"options": { |
| 350 |
"test_certificates": { |
| 351 |
"type": "str", |
| 352 |
"default": "all", |
| 353 |
"choices": ["first", "last", "all"], |
| 354 |
}, |
| 355 |
"issuer": {"type": "dict"}, |
| 356 |
"subject": {"type": "dict"}, |
| 357 |
"subject_key_identifier": {"type": "str"}, |
| 358 |
"authority_key_identifier": {"type": "str"}, |
| 359 |
}, |
| 360 |
}, |
| 361 |
) |
| 362 |
module = argument_spec.create_ansible_module() |
| 363 |
|
| 364 |
backend = create_backend(module, needs_acme_v2=False) |
| 365 |
|
| 366 |
try: |
| 367 |
client = ACMECertificateClient(module=module, backend=backend) |
| 368 |
select_chain_matcher = client.parse_select_chain(module.params["select_chain"]) |
| 369 |
other = {} |
| 370 |
done = False |
| 371 |
order = None |
| 372 |
try: |
| 373 |
# Step 1: load order |
| 374 |
order = client.load_order() |
| 375 |
|
| 376 |
download_all_chains = ( |
| 377 |
len(select_chain_matcher) > 0 |
| 378 |
or module.params["retrieve_all_alternates"] |
| 379 |
) |
| 380 |
changed = False |
| 381 |
alternate_chains: list[CertificateChain] | None |
| 382 |
if order.status == "valid": |
| 383 |
# Step 2 and 3: download certificate(s) and chain(s) |
| 384 |
cert, alternate_chains = client.download_certificate( |
| 385 |
order, |
| 386 |
download_all_chains=download_all_chains, |
| 387 |
) |
| 388 |
else: |
| 389 |
client.check_that_authorizations_can_be_used(order) |
| 390 |
|
| 391 |
# Step 2: wait for authorizations to validate |
| 392 |
pending_authzs = client.collect_pending_authzs(order) |
| 393 |
client.wait_for_validation(pending_authzs) |
| 394 |
|
| 395 |
# Step 3: finalize order, wait, then download certificate(s) and chain(s) |
| 396 |
cert, alternate_chains = client.get_certificate( |
| 397 |
order, |
| 398 |
download_all_chains=download_all_chains, |
| 399 |
) |
| 400 |
changed = True |
| 401 |
|
| 402 |
# Step 4: pick chain, write certificates, and provide return values |
| 403 |
if alternate_chains is not None: |
| 404 |
# Prepare return value for all alternate chains |
| 405 |
if module.params["retrieve_all_alternates"]: |
| 406 |
all_chains = [cert.to_json()] |
| 407 |
for alt_chain in alternate_chains: |
| 408 |
all_chains.append(alt_chain.to_json()) |
| 409 |
other["all_chains"] = all_chains |
| 410 |
|
| 411 |
# Try to select alternate chain depending on criteria |
| 412 |
if select_chain_matcher: |
| 413 |
matching_chain = client.find_matching_chain( |
| 414 |
chains=[cert] + alternate_chains, |
| 415 |
select_chain_matcher=select_chain_matcher, |
| 416 |
) |
| 417 |
if matching_chain: |
| 418 |
cert = matching_chain |
| 419 |
else: |
| 420 |
module.debug("Found no matching alternative chain") |
| 421 |
|
| 422 |
if client.write_cert_chain( |
| 423 |
cert=cert, |
| 424 |
cert_dest=module.params["cert_dest"], |
| 425 |
fullchain_dest=module.params["fullchain_dest"], |
| 426 |
chain_dest=module.params["chain_dest"], |
| 427 |
): |
| 428 |
changed = True |
| 429 |
|
| 430 |
done = True |
| 431 |
finally: |
| 432 |
if ( |
| 433 |
module.params["deactivate_authzs"] == "always" |
| 434 |
or (module.params["deactivate_authzs"] == "on_success" and done) |
| 435 |
or (module.params["deactivate_authzs"] == "on_error" and not done) |
| 436 |
) and order: |
| 437 |
client.deactivate_authzs(order) |
| 438 |
module.exit_json( |
| 439 |
changed=changed, |
| 440 |
account_uri=client.client.account_uri, |
| 441 |
selected_chain=cert.to_json(), |
| 442 |
**other, |
| 443 |
) |
| 444 |
except ModuleFailException as e: |
| 445 |
e.do_fail(module=module) |
| 446 |
|
| 447 |
|
| 448 |
if __name__ == "__main__": |
| 449 |
main() |