| 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_info |
| 10 |
author: Felix Fontein (@felixfontein) |
| 11 |
version_added: 2.24.0 |
| 12 |
short_description: Obtain information for an ACME v2 order |
| 13 |
description: |
| 14 |
- Obtain information for an ACME v2 order. |
| 15 |
This can be used during the process 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), |
| 21 |
M(community.crypto.acme_certificate_order_validate), and |
| 22 |
M(community.crypto.acme_certificate_order_finalize) modules. |
| 23 |
- The module supports the V(dns-account-01) challenge type according to |
| 24 |
L(acme-dns-account-label draft 02, https://datatracker.ietf.org/doc/html/draft-ietf-acme-dns-account-label-02). |
| 25 |
Note that the supported draft version can change at any time, |
| 26 |
and changes will only be considered breaking once the draft reached RFC status. |
| 27 |
- The module supports the V(dns-persist-01) challenge type according to |
| 28 |
L(acme-dns-persist draft 01, https://www.ietf.org/archive/id/draft-ietf-acme-dns-persist-01.html). |
| 29 |
Note that the supported draft version can change at any time, |
| 30 |
and changes will only be considered breaking once the draft reached RFC status. |
| 31 |
seealso: |
| 32 |
- module: community.crypto.acme_certificate_order_create |
| 33 |
description: Create an ACME order. |
| 34 |
- module: community.crypto.acme_certificate_order_validate |
| 35 |
description: Validate pending authorizations of an ACME order. |
| 36 |
- module: community.crypto.acme_certificate_order_finalize |
| 37 |
description: Finalize an ACME order after satisfying the challenges. |
| 38 |
- name: Automatic Certificate Management Environment (ACME) |
| 39 |
description: The specification of the ACME protocol (RFC 8555). |
| 40 |
link: https://tools.ietf.org/html/rfc8555 |
| 41 |
- name: ACME TLS ALPN Challenge Extension |
| 42 |
description: The specification of the V(tls-alpn-01) challenge (RFC 8737). |
| 43 |
link: https://www.rfc-editor.org/rfc/rfc8737.html |
| 44 |
- module: community.crypto.acme_inspect |
| 45 |
description: Allows to debug problems. |
| 46 |
- module: community.crypto.acme_certificate_deactivate_authz |
| 47 |
description: Allows to deactivate (invalidate) ACME v2 orders. |
| 48 |
extends_documentation_fragment: |
| 49 |
- community.crypto._acme.basic |
| 50 |
- community.crypto._acme.account |
| 51 |
- community.crypto._attributes |
| 52 |
- community.crypto._attributes.actiongroup_acme |
| 53 |
- community.crypto._attributes.idempotent_not_modify_state |
| 54 |
- community.crypto._attributes.info_module |
| 55 |
options: |
| 56 |
order_uri: |
| 57 |
description: |
| 58 |
- The order URI provided by RV(community.crypto.acme_certificate_order_create#module:order_uri). |
| 59 |
type: str |
| 60 |
required: true |
| 61 |
""" |
| 62 |
|
| 63 |
EXAMPLES = r""" |
| 64 |
--- |
| 65 |
- name: Create a challenge for sample.com using a account key from a variable |
| 66 |
community.crypto.acme_certificate_order_create: |
| 67 |
account_key_content: "{{ account_private_key }}" |
| 68 |
csr: /etc/pki/cert/csr/sample.com.csr |
| 69 |
register: order |
| 70 |
|
| 71 |
- name: Obtain information on the order |
| 72 |
community.crypto.acme_certificate_order_info: |
| 73 |
account_key_src: /etc/pki/cert/private/account.key |
| 74 |
order_uri: "{{ order.order_uri }}" |
| 75 |
register: order_info |
| 76 |
|
| 77 |
- name: Show information |
| 78 |
ansible.builtin.debug: |
| 79 |
var: order_info |
| 80 |
""" |
| 81 |
|
| 82 |
RETURN = """ |
| 83 |
account_uri: |
| 84 |
description: ACME account URI. |
| 85 |
returned: success |
| 86 |
type: str |
| 87 |
order_uri: |
| 88 |
description: ACME order URI. |
| 89 |
returned: success |
| 90 |
type: str |
| 91 |
order: |
| 92 |
description: |
| 93 |
- The order object. |
| 94 |
- See U(https://www.rfc-editor.org/rfc/rfc8555#section-7.1.3) for its specification. |
| 95 |
returned: success |
| 96 |
type: dict |
| 97 |
contains: |
| 98 |
status: |
| 99 |
description: |
| 100 |
- The status of this order. |
| 101 |
- See U(https://www.rfc-editor.org/rfc/rfc8555#section-7.1.6) for state changes. |
| 102 |
type: str |
| 103 |
returned: always |
| 104 |
choices: |
| 105 |
- pending |
| 106 |
- ready |
| 107 |
- processing |
| 108 |
- valid |
| 109 |
- invalid |
| 110 |
expires: |
| 111 |
description: |
| 112 |
- The timestamp after which the server will consider this order invalid. |
| 113 |
- Encoded in the format specified in L(RFC 3339, https://www.rfc-editor.org/rfc/rfc3339). |
| 114 |
type: str |
| 115 |
returned: if RV(order.status) is V(pending) or V(valid), and sometimes in other situations |
| 116 |
identifiers: |
| 117 |
description: |
| 118 |
- An array of identifier objects that the order pertains to. |
| 119 |
returned: always |
| 120 |
type: list |
| 121 |
elements: dict |
| 122 |
contains: |
| 123 |
type: |
| 124 |
description: |
| 125 |
- The type of identifier. |
| 126 |
- So far V(dns) and V(ip) are defined values. |
| 127 |
type: str |
| 128 |
returned: always |
| 129 |
sample: dns |
| 130 |
choices: |
| 131 |
- dns |
| 132 |
- ip |
| 133 |
value: |
| 134 |
description: |
| 135 |
- The identifier itself. |
| 136 |
type: str |
| 137 |
returned: always |
| 138 |
sample: example.com |
| 139 |
notBefore: |
| 140 |
description: |
| 141 |
- The requested value of the C(notBefore) field in the certificate. |
| 142 |
- Encoded in the date format defined in L(RFC 3339, https://www.rfc-editor.org/rfc/rfc3339). |
| 143 |
type: str |
| 144 |
returned: depending on order |
| 145 |
notAfter: |
| 146 |
description: |
| 147 |
- The requested value of the C(notAfter) field in the certificate. |
| 148 |
- Encoded in the date format defined in L(RFC 3339, https://www.rfc-editor.org/rfc/rfc3339). |
| 149 |
type: str |
| 150 |
returned: depending on order |
| 151 |
error: |
| 152 |
description: |
| 153 |
- The error that occurred while processing the order, if any. |
| 154 |
- This field is structured as a L(problem document according to RFC 7807, https://www.rfc-editor.org/rfc/rfc7807). |
| 155 |
type: dict |
| 156 |
returned: sometimes |
| 157 |
authorizations: |
| 158 |
description: |
| 159 |
- For pending orders, the authorizations that the client needs to complete before the |
| 160 |
requested certificate can be issued, including unexpired authorizations that the client |
| 161 |
has completed in the past for identifiers specified in the order. |
| 162 |
- The authorizations required are dictated by server policy; there may not be a 1:1 |
| 163 |
relationship between the order identifiers and the authorizations required. |
| 164 |
- For final orders (in the V(valid) or V(invalid) state), the authorizations that were |
| 165 |
completed. Each entry is a URL from which an authorization can be fetched with a POST-as-GET request. |
| 166 |
- The authorizations themselves are returned as RV(authorizations_by_identifier). |
| 167 |
type: list |
| 168 |
elements: str |
| 169 |
returned: always |
| 170 |
finalize: |
| 171 |
description: |
| 172 |
- A URL that a CSR must be POSTed to once all of the order's authorizations are satisfied to finalize the |
| 173 |
order. The result of a successful finalization will be the population of the certificate URL for the order. |
| 174 |
type: str |
| 175 |
returned: always |
| 176 |
certificate: |
| 177 |
description: |
| 178 |
- A URL for the certificate that has been issued in response to this order. |
| 179 |
type: str |
| 180 |
returned: when the certificate has been issued |
| 181 |
replaces: |
| 182 |
description: |
| 183 |
- If the order was created to replace an existing certificate using the C(replaces) mechanism from |
| 184 |
L(RFC 9773, https://www.rfc-editor.org/rfc/rfc9773.html), this provides the |
| 185 |
certificate ID of the certificate that will be replaced by this order. |
| 186 |
type: str |
| 187 |
returned: when the certificate order is replacing a certificate through RFC 9773 |
| 188 |
profile: |
| 189 |
description: |
| 190 |
- If the ACME CA supports profiles through the L(draft-aaron-acme-profiles, |
| 191 |
https://datatracker.ietf.org/doc/draft-aaron-acme-profiles/) mechanism and informs about the profile |
| 192 |
selected for this order, this field will contain the name of the profile used. |
| 193 |
type: str |
| 194 |
returned: depending on the ACME CA |
| 195 |
authorizations_by_identifier: |
| 196 |
description: |
| 197 |
- A dictionary mapping identifiers to their authorization objects. |
| 198 |
returned: success |
| 199 |
type: dict |
| 200 |
contains: |
| 201 |
identifier: |
| 202 |
description: |
| 203 |
- The keys in this dictionary are the identifiers. C(identifier) is a placeholder used in the documentation. |
| 204 |
- See U(https://www.rfc-editor.org/rfc/rfc8555#section-7.1.4) for how authorization objects look like. |
| 205 |
type: dict |
| 206 |
contains: |
| 207 |
identifier: |
| 208 |
description: |
| 209 |
- The identifier that the account is authorized to represent. |
| 210 |
type: dict |
| 211 |
returned: always |
| 212 |
contains: |
| 213 |
type: |
| 214 |
description: |
| 215 |
- The type of identifier. |
| 216 |
- So far V(dns) and V(ip) are defined values. |
| 217 |
type: str |
| 218 |
returned: always |
| 219 |
sample: dns |
| 220 |
choices: |
| 221 |
- dns |
| 222 |
- ip |
| 223 |
value: |
| 224 |
description: |
| 225 |
- The identifier itself. |
| 226 |
type: str |
| 227 |
returned: always |
| 228 |
sample: example.com |
| 229 |
status: |
| 230 |
description: |
| 231 |
- The status of this authorization. |
| 232 |
- See U(https://www.rfc-editor.org/rfc/rfc8555#section-7.1.6) for state changes. |
| 233 |
type: str |
| 234 |
choices: |
| 235 |
- pending |
| 236 |
- valid |
| 237 |
- invalid |
| 238 |
- deactivated |
| 239 |
- expired |
| 240 |
- revoked |
| 241 |
returned: always |
| 242 |
expires: |
| 243 |
description: |
| 244 |
- The timestamp after which the server will consider this authorization invalid. |
| 245 |
- Encoded in the format specified in L(RFC 3339, https://www.rfc-editor.org/rfc/rfc3339). |
| 246 |
type: str |
| 247 |
returned: if RV(authorizations_by_identifier.identifier.status=valid), and sometimes in other situations |
| 248 |
challenges: |
| 249 |
description: |
| 250 |
- For pending authorizations, the challenges that the client can fulfill in order to prove |
| 251 |
possession of the identifier. |
| 252 |
- For valid authorizations, the challenge that was validated. |
| 253 |
- For invalid authorizations, the challenge that was attempted and failed. |
| 254 |
- Each array entry is an object with parameters required to validate the challenge. |
| 255 |
A client should attempt to fulfill one of these challenges, and a server should consider |
| 256 |
any one of the challenges sufficient to make the authorization valid. |
| 257 |
- See U(https://www.rfc-editor.org/rfc/rfc8555#section-8) for the general structure. The structure |
| 258 |
of every entry depends on the challenge's type. For C(tls-alpn-01) challenges, the structure is |
| 259 |
defined in U(https://www.rfc-editor.org/rfc/rfc8737.html#section-3). |
| 260 |
type: list |
| 261 |
elements: dict |
| 262 |
returned: always |
| 263 |
contains: |
| 264 |
type: |
| 265 |
description: |
| 266 |
- The type of challenge encoded in the object. |
| 267 |
- Support for V(dns-account-01) and V(dns-persist-01) has been added in community.crypto 3.2.0. |
| 268 |
type: str |
| 269 |
returned: always |
| 270 |
choices: |
| 271 |
- http-01 |
| 272 |
- dns-01 |
| 273 |
- dns-account-01 |
| 274 |
- dns-persist-01 |
| 275 |
- tls-alpn-01 |
| 276 |
url: |
| 277 |
description: |
| 278 |
- The URL to which a response can be posted. |
| 279 |
type: str |
| 280 |
returned: always |
| 281 |
status: |
| 282 |
description: |
| 283 |
- The status of this challenge. |
| 284 |
- See U(https://www.rfc-editor.org/rfc/rfc8555#section-7.1.6) for state changes. |
| 285 |
type: str |
| 286 |
choices: |
| 287 |
- pending |
| 288 |
- processing |
| 289 |
- valid |
| 290 |
- invalid |
| 291 |
returned: always |
| 292 |
validated: |
| 293 |
description: |
| 294 |
- The time at which the server validated this challenge. |
| 295 |
- Encoded in the format specified in L(RFC 3339, https://www.rfc-editor.org/rfc/rfc3339). |
| 296 |
type: str |
| 297 |
returned: always if RV(authorizations_by_identifier.identifier.challenges[].type=valid), otherwise in some situations |
| 298 |
error: |
| 299 |
description: |
| 300 |
- Error that occurred while the server was validating the challenge, if any. |
| 301 |
- This field is structured as a L(problem document according to RFC 7807, https://www.rfc-editor.org/rfc/rfc7807). |
| 302 |
type: dict |
| 303 |
returned: always if RV(authorizations_by_identifier.identifier.challenges[].type=invalid), otherwise in some situations |
| 304 |
wildcard: |
| 305 |
description: |
| 306 |
- This field B(must) be present and true for authorizations created as a result of a |
| 307 |
C(newOrder) request containing a DNS identifier with a value that was a wildcard |
| 308 |
domain name. For other authorizations, it B(must) be absent. |
| 309 |
- Wildcard domain names are described in U(https://www.rfc-editor.org/rfc/rfc8555#section-7.1.3) |
| 310 |
of the ACME specification. |
| 311 |
type: bool |
| 312 |
returned: sometimes |
| 313 |
authorizations_by_status: |
| 314 |
description: |
| 315 |
- For every status, a list of identifiers whose authorizations have this status. |
| 316 |
returned: success |
| 317 |
type: dict |
| 318 |
contains: |
| 319 |
pending: |
| 320 |
description: |
| 321 |
- A list of all identifiers whose authorizations are in the C(pending) state. |
| 322 |
- See U(https://www.rfc-editor.org/rfc/rfc8555#section-7.1.6) for state changes |
| 323 |
of authorizations. |
| 324 |
type: list |
| 325 |
elements: str |
| 326 |
returned: always |
| 327 |
invalid: |
| 328 |
description: |
| 329 |
- A list of all identifiers whose authorizations are in the C(invalid) state. |
| 330 |
- See U(https://www.rfc-editor.org/rfc/rfc8555#section-7.1.6) for state changes |
| 331 |
of authorizations. |
| 332 |
type: list |
| 333 |
elements: str |
| 334 |
returned: always |
| 335 |
valid: |
| 336 |
description: |
| 337 |
- A list of all identifiers whose authorizations are in the C(valid) state. |
| 338 |
- See U(https://www.rfc-editor.org/rfc/rfc8555#section-7.1.6) for state changes |
| 339 |
of authorizations. |
| 340 |
type: list |
| 341 |
elements: str |
| 342 |
returned: always |
| 343 |
revoked: |
| 344 |
description: |
| 345 |
- A list of all identifiers whose authorizations are in the C(revoked) state. |
| 346 |
- See U(https://www.rfc-editor.org/rfc/rfc8555#section-7.1.6) for state changes |
| 347 |
of authorizations. |
| 348 |
type: list |
| 349 |
elements: str |
| 350 |
returned: always |
| 351 |
deactivated: |
| 352 |
description: |
| 353 |
- A list of all identifiers whose authorizations are in the C(deactivated) state. |
| 354 |
- See U(https://www.rfc-editor.org/rfc/rfc8555#section-7.1.6) for state changes |
| 355 |
of authorizations. |
| 356 |
type: list |
| 357 |
elements: str |
| 358 |
returned: always |
| 359 |
expired: |
| 360 |
description: |
| 361 |
- A list of all identifiers whose authorizations are in the C(expired) state. |
| 362 |
- See U(https://www.rfc-editor.org/rfc/rfc8555#section-7.1.6) for state changes |
| 363 |
of authorizations. |
| 364 |
type: list |
| 365 |
elements: str |
| 366 |
returned: always |
| 367 |
""" |
| 368 |
|
| 369 |
import typing as t |
| 370 |
|
| 371 |
from ansible_collections.community.crypto.plugins.module_utils._acme.acme import ( |
| 372 |
create_backend, |
| 373 |
create_default_argspec, |
| 374 |
) |
| 375 |
from ansible_collections.community.crypto.plugins.module_utils._acme.certificate import ( |
| 376 |
ACMECertificateClient, |
| 377 |
) |
| 378 |
from ansible_collections.community.crypto.plugins.module_utils._acme.errors import ( |
| 379 |
ModuleFailException, |
| 380 |
) |
| 381 |
|
| 382 |
|
| 383 |
def main() -> t.NoReturn: |
| 384 |
argument_spec = create_default_argspec(with_certificate=False) |
| 385 |
argument_spec.update_argspec( |
| 386 |
order_uri={"type": "str", "required": True}, |
| 387 |
) |
| 388 |
module = argument_spec.create_ansible_module(supports_check_mode=True) |
| 389 |
|
| 390 |
backend = create_backend(module, needs_acme_v2=False) |
| 391 |
|
| 392 |
try: |
| 393 |
client = ACMECertificateClient(module=module, backend=backend) |
| 394 |
order = client.load_order() |
| 395 |
authorizations_by_identifier: dict[str, dict[str, t.Any]] = {} |
| 396 |
authorizations_by_status: dict[str, list[str]] = { |
| 397 |
"pending": [], |
| 398 |
"invalid": [], |
| 399 |
"valid": [], |
| 400 |
"revoked": [], |
| 401 |
"deactivated": [], |
| 402 |
"expired": [], |
| 403 |
} |
| 404 |
for identifier, authz in order.authorizations.items(): |
| 405 |
authorizations_by_identifier[identifier] = authz.to_json() |
| 406 |
if authz.status is not None: |
| 407 |
authorizations_by_status[authz.status].append(identifier) |
| 408 |
module.exit_json( |
| 409 |
changed=False, |
| 410 |
account_uri=client.client.account_uri, |
| 411 |
order_uri=order.url, |
| 412 |
order=order.data, |
| 413 |
authorizations_by_identifier=authorizations_by_identifier, |
| 414 |
authorizations_by_status=authorizations_by_status, |
| 415 |
) |
| 416 |
except ModuleFailException as e: |
| 417 |
e.do_fail(module=module) |
| 418 |
|
| 419 |
|
| 420 |
if __name__ == "__main__": |
| 421 |
main() |