| 1 |
#!/usr/bin/python |
| 2 |
# Copyright (c) 2016 Michael Gruener <[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_deactivate_authz |
| 10 |
author: "Felix Fontein (@felixfontein)" |
| 11 |
version_added: 2.20.0 |
| 12 |
short_description: Deactivate all authz for an ACME v2 order |
| 13 |
description: |
| 14 |
- Deactivate all authentication objects (authz) for an ACME v2 order, which effectively deactivates (invalidates) the order |
| 15 |
itself. |
| 16 |
- Authentication objects are bound to an account key and remain valid for a certain amount of time, and can be used to issue |
| 17 |
certificates without having to re-authenticate the domain. This can be a security concern. |
| 18 |
- Another reason to use this module is to deactivate an order whose processing failed when using |
| 19 |
O(community.crypto.acme_certificate#module:include_renewal_cert_id). |
| 20 |
seealso: |
| 21 |
- module: community.crypto.acme_certificate |
| 22 |
extends_documentation_fragment: |
| 23 |
- community.crypto._acme.basic |
| 24 |
- community.crypto._acme.account |
| 25 |
- community.crypto._attributes |
| 26 |
- community.crypto._attributes.actiongroup_acme |
| 27 |
attributes: |
| 28 |
check_mode: |
| 29 |
support: full |
| 30 |
diff_mode: |
| 31 |
support: none |
| 32 |
idempotent: |
| 33 |
support: full |
| 34 |
options: |
| 35 |
order_uri: |
| 36 |
description: |
| 37 |
- The ACME v2 order to deactivate. |
| 38 |
- Can be obtained from RV(community.crypto.acme_certificate#module:order_uri). |
| 39 |
type: str |
| 40 |
required: true |
| 41 |
""" |
| 42 |
|
| 43 |
EXAMPLES = r""" |
| 44 |
--- |
| 45 |
- name: Deactivate all authzs for an order |
| 46 |
community.crypto.acme_certificate_deactivate_authz: |
| 47 |
account_key_content: "{{ account_private_key }}" |
| 48 |
order_uri: "{{ certificate_result.order_uri }}" |
| 49 |
""" |
| 50 |
|
| 51 |
RETURN = """#""" |
| 52 |
|
| 53 |
import typing as t |
| 54 |
|
| 55 |
from ansible_collections.community.crypto.plugins.module_utils._acme.account import ( |
| 56 |
ACMEAccount, |
| 57 |
) |
| 58 |
from ansible_collections.community.crypto.plugins.module_utils._acme.acme import ( |
| 59 |
ACMEClient, |
| 60 |
create_backend, |
| 61 |
create_default_argspec, |
| 62 |
) |
| 63 |
from ansible_collections.community.crypto.plugins.module_utils._acme.errors import ( |
| 64 |
ModuleFailException, |
| 65 |
) |
| 66 |
from ansible_collections.community.crypto.plugins.module_utils._acme.orders import Order |
| 67 |
|
| 68 |
|
| 69 |
def main() -> t.NoReturn: |
| 70 |
argument_spec = create_default_argspec() |
| 71 |
argument_spec.update_argspec( |
| 72 |
order_uri={"type": "str", "required": True}, |
| 73 |
) |
| 74 |
module = argument_spec.create_ansible_module(supports_check_mode=True) |
| 75 |
|
| 76 |
backend = create_backend(module, needs_acme_v2=False) |
| 77 |
|
| 78 |
try: |
| 79 |
client = ACMEClient(module=module, backend=backend) |
| 80 |
account = ACMEAccount(client=client) |
| 81 |
|
| 82 |
dummy, account_data = account.setup_account(allow_creation=False) |
| 83 |
if account_data is None: |
| 84 |
raise ModuleFailException(msg="Account does not exist or is deactivated.") |
| 85 |
|
| 86 |
order = Order.from_url(client=client, url=module.params["order_uri"]) |
| 87 |
order.load_authorizations(client=client) |
| 88 |
|
| 89 |
changed = False |
| 90 |
for authz in order.authorizations.values(): |
| 91 |
if not authz.can_deactivate(): |
| 92 |
continue |
| 93 |
changed = True |
| 94 |
if module.check_mode: |
| 95 |
continue |
| 96 |
try: |
| 97 |
authz.deactivate(client=client) |
| 98 |
except Exception: |
| 99 |
# ignore errors |
| 100 |
pass |
| 101 |
if authz.status != "deactivated": |
| 102 |
module.warn(warning=f"Could not deactivate authz object {authz.url}.") |
| 103 |
|
| 104 |
module.exit_json(changed=changed) |
| 105 |
except ModuleFailException as e: |
| 106 |
e.do_fail(module=module) |
| 107 |
|
| 108 |
|
| 109 |
if __name__ == "__main__": |
| 110 |
main() |