| 1 |
#!/usr/bin/python |
| 2 |
# Copyright (c) 2018 Felix Fontein (@felixfontein) |
| 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_inspect |
| 10 |
author: "Felix Fontein (@felixfontein)" |
| 11 |
short_description: Send direct requests to an ACME server |
| 12 |
description: |
| 13 |
- Allows to send direct requests to an ACME server with the L(ACME protocol,https://tools.ietf.org/html/rfc8555), which |
| 14 |
is supported by CAs such as L(Let's Encrypt,https://letsencrypt.org/). |
| 15 |
- This module can be used to debug failed certificate request attempts, for example when M(community.crypto.acme_certificate) |
| 16 |
fails or encounters a problem which you wish to investigate. |
| 17 |
- The module can also be used to directly access features of an ACME servers which are not yet supported by the Ansible |
| 18 |
ACME modules. |
| 19 |
notes: |
| 20 |
- The O(account_uri) option must be specified for properly authenticated ACME v2 requests (except a C(new-account) request). |
| 21 |
- "Using the C(ansible) tool, M(community.crypto.acme_inspect) can be used to directly execute ACME requests without the |
| 22 |
need of writing a playbook. For example, the following command retrieves the ACME account with ID 1 from Let's Encrypt |
| 23 |
(assuming C(/path/to/key) is the correct private account key): C(ansible localhost -m acme_inspect -a \"account_key_src=/path/to/key |
| 24 |
acme_directory=https://acme-v02.api.letsencrypt.org/directory account_uri=https://acme-v02.api.letsencrypt.org/acme/acct/1 |
| 25 |
method=get url=https://acme-v02.api.letsencrypt.org/acme/acct/1\")." |
| 26 |
seealso: |
| 27 |
- name: Automatic Certificate Management Environment (ACME) |
| 28 |
description: The specification of the ACME protocol (RFC 8555). |
| 29 |
link: https://tools.ietf.org/html/rfc8555 |
| 30 |
- name: ACME TLS ALPN Challenge Extension |
| 31 |
description: The specification of the C(tls-alpn-01) challenge (RFC 8737). |
| 32 |
link: https://www.rfc-editor.org/rfc/rfc8737.html |
| 33 |
extends_documentation_fragment: |
| 34 |
- community.crypto._acme.basic |
| 35 |
- community.crypto._acme.account |
| 36 |
- community.crypto._attributes |
| 37 |
- community.crypto._attributes.actiongroup_acme |
| 38 |
attributes: |
| 39 |
check_mode: |
| 40 |
support: none |
| 41 |
diff_mode: |
| 42 |
support: none |
| 43 |
idempotent: |
| 44 |
support: none |
| 45 |
options: |
| 46 |
url: |
| 47 |
description: |
| 48 |
- The URL to send the request to. |
| 49 |
- Must be specified if O(method) is not V(directory-only). |
| 50 |
type: str |
| 51 |
method: |
| 52 |
description: |
| 53 |
- The method to use to access the given URL on the ACME server. |
| 54 |
- The value V(post) executes an authenticated POST request. The content must be specified in the O(content) option. |
| 55 |
- The value V(get) executes an authenticated POST-as-GET request for ACME v2, and a regular GET request for ACME v1. |
| 56 |
- The value V(directory-only) only retrieves the directory, without doing a request. |
| 57 |
type: str |
| 58 |
default: get |
| 59 |
choices: |
| 60 |
- get |
| 61 |
- post |
| 62 |
- directory-only |
| 63 |
content: |
| 64 |
description: |
| 65 |
- An encoded JSON object which will be sent as the content if O(method) is V(post). |
| 66 |
- Required when O(method) is V(post), and not allowed otherwise. |
| 67 |
type: str |
| 68 |
fail_on_acme_error: |
| 69 |
description: |
| 70 |
- If O(method) is V(post) or V(get), make the module fail in case an ACME error is returned. |
| 71 |
type: bool |
| 72 |
default: true |
| 73 |
""" |
| 74 |
|
| 75 |
EXAMPLES = r""" |
| 76 |
--- |
| 77 |
- name: Get directory |
| 78 |
community.crypto.acme_inspect: |
| 79 |
acme_directory: https://acme-staging-v02.api.letsencrypt.org/directory |
| 80 |
method: directory-only |
| 81 |
register: directory |
| 82 |
|
| 83 |
- name: Create an account |
| 84 |
community.crypto.acme_inspect: |
| 85 |
acme_directory: https://acme-staging-v02.api.letsencrypt.org/directory |
| 86 |
account_key_src: /etc/pki/cert/private/account.key |
| 87 |
url: "{{ directory.newAccount}}" |
| 88 |
method: post |
| 89 |
content: '{"termsOfServiceAgreed":true}' |
| 90 |
register: account_creation |
| 91 |
# account_creation.headers.location contains the account URI |
| 92 |
# if creation was successful |
| 93 |
|
| 94 |
- name: Get account information |
| 95 |
community.crypto.acme_inspect: |
| 96 |
acme_directory: https://acme-staging-v02.api.letsencrypt.org/directory |
| 97 |
account_key_src: /etc/pki/cert/private/account.key |
| 98 |
account_uri: "{{ account_creation.headers.location }}" |
| 99 |
url: "{{ account_creation.headers.location }}" |
| 100 |
method: get |
| 101 |
|
| 102 |
- name: Update account contacts |
| 103 |
community.crypto.acme_inspect: |
| 104 |
acme_directory: https://acme-staging-v02.api.letsencrypt.org/directory |
| 105 |
account_key_src: /etc/pki/cert/private/account.key |
| 106 |
account_uri: "{{ account_creation.headers.location }}" |
| 107 |
url: "{{ account_creation.headers.location }}" |
| 108 |
method: post |
| 109 |
content: '{{ account_info | to_json }}' |
| 110 |
vars: |
| 111 |
account_info: |
| 112 |
# For valid values, see |
| 113 |
# https://tools.ietf.org/html/rfc8555#section-7.3 |
| 114 |
contact: |
| 115 |
- mailto:[email protected] |
| 116 |
|
| 117 |
- name: Create certificate order |
| 118 |
community.crypto.acme_certificate: |
| 119 |
acme_directory: https://acme-staging-v02.api.letsencrypt.org/directory |
| 120 |
account_key_src: /etc/pki/cert/private/account.key |
| 121 |
account_uri: "{{ account_creation.headers.location }}" |
| 122 |
csr: /etc/pki/cert/csr/sample.com.csr |
| 123 |
fullchain_dest: /etc/httpd/ssl/sample.com-fullchain.crt |
| 124 |
challenge: http-01 |
| 125 |
modify_account: false |
| 126 |
register: certificate_request |
| 127 |
|
| 128 |
# Assume something went wrong. certificate_request.order_uri contains |
| 129 |
# the order URI. |
| 130 |
|
| 131 |
- name: Get order information |
| 132 |
community.crypto.acme_inspect: |
| 133 |
acme_directory: https://acme-staging-v02.api.letsencrypt.org/directory |
| 134 |
account_key_src: /etc/pki/cert/private/account.key |
| 135 |
account_uri: "{{ account_creation.headers.location }}" |
| 136 |
url: "{{ certificate_request.order_uri }}" |
| 137 |
method: get |
| 138 |
register: order |
| 139 |
|
| 140 |
- name: Get first authz for order |
| 141 |
community.crypto.acme_inspect: |
| 142 |
acme_directory: https://acme-staging-v02.api.letsencrypt.org/directory |
| 143 |
account_key_src: /etc/pki/cert/private/account.key |
| 144 |
account_uri: "{{ account_creation.headers.location }}" |
| 145 |
url: "{{ order.output_json.authorizations[0] }}" |
| 146 |
method: get |
| 147 |
register: authz |
| 148 |
|
| 149 |
- name: Get HTTP-01 challenge for authz |
| 150 |
community.crypto.acme_inspect: |
| 151 |
acme_directory: https://acme-staging-v02.api.letsencrypt.org/directory |
| 152 |
account_key_src: /etc/pki/cert/private/account.key |
| 153 |
account_uri: "{{ account_creation.headers.location }}" |
| 154 |
url: "{{ authz.output_json.challenges | selectattr('type', 'equalto', 'http-01') }}" |
| 155 |
method: get |
| 156 |
register: http01challenge |
| 157 |
|
| 158 |
- name: Activate HTTP-01 challenge manually |
| 159 |
community.crypto.acme_inspect: |
| 160 |
acme_directory: https://acme-staging-v02.api.letsencrypt.org/directory |
| 161 |
account_key_src: /etc/pki/cert/private/account.key |
| 162 |
account_uri: "{{ account_creation.headers.location }}" |
| 163 |
url: "{{ http01challenge.url }}" |
| 164 |
method: post |
| 165 |
content: '{}' |
| 166 |
""" |
| 167 |
|
| 168 |
RETURN = r""" |
| 169 |
directory: |
| 170 |
description: The ACME directory's content. |
| 171 |
returned: always |
| 172 |
type: dict |
| 173 |
sample: { |
| 174 |
"a85k3x9f91A4": "https://community.letsencrypt.org/t/adding-random-entries-to-the-directory/33417", |
| 175 |
"keyChange": "https://acme-v02.api.letsencrypt.org/acme/key-change", |
| 176 |
"meta": { |
| 177 |
"caaIdentities": ["letsencrypt.org"], |
| 178 |
"termsOfService": "https://letsencrypt.org/documents/LE-SA-v1.2-November-15-2017.pdf", |
| 179 |
"website": "https://letsencrypt.org", |
| 180 |
}, |
| 181 |
"newAccount": "https://acme-v02.api.letsencrypt.org/acme/new-acct", |
| 182 |
"newNonce": "https://acme-v02.api.letsencrypt.org/acme/new-nonce", |
| 183 |
"newOrder": "https://acme-v02.api.letsencrypt.org/acme/new-order", |
| 184 |
"revokeCert": "https://acme-v02.api.letsencrypt.org/acme/revoke-cert" |
| 185 |
} |
| 186 |
headers: |
| 187 |
description: The request's HTTP headers (with lowercase keys). |
| 188 |
returned: always |
| 189 |
type: dict |
| 190 |
sample: { |
| 191 |
"boulder-requester": "12345", |
| 192 |
"cache-control": "max-age=0, no-cache, no-store", |
| 193 |
"connection": "close", |
| 194 |
"content-length": "904", |
| 195 |
"content-type": "application/json", |
| 196 |
"cookies": {}, |
| 197 |
"cookies_string": "", |
| 198 |
"date": "Wed, 07 Nov 2018 12:34:56 GMT", |
| 199 |
"expires": "Wed, 07 Nov 2018 12:44:56 GMT", |
| 200 |
"link": '<https://letsencrypt.org/documents/LE-SA-v1.2-November-15-2017.pdf>;rel="terms-of-service"', |
| 201 |
"msg": "OK (904 bytes)", |
| 202 |
"pragma": "no-cache", |
| 203 |
"replay-nonce": "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGH", |
| 204 |
"server": "nginx", |
| 205 |
"status": 200, |
| 206 |
"strict-transport-security": "max-age=604800", |
| 207 |
"url": "https://acme-v02.api.letsencrypt.org/acme/acct/46161", |
| 208 |
"x-frame-options": "DENY", |
| 209 |
} |
| 210 |
output_text: |
| 211 |
description: The raw text output. |
| 212 |
returned: always |
| 213 |
type: str |
| 214 |
sample: "{\\n \\\"id\\\": 12345,\\n \\\"key\\\": {\\n \\\"kty\\\": \\\"RSA\\\",\\n ..." |
| 215 |
output_json: |
| 216 |
description: The output parsed as JSON. |
| 217 |
returned: if output can be parsed as JSON |
| 218 |
type: dict |
| 219 |
sample: |
| 220 |
- id: 12345 |
| 221 |
- key: |
| 222 |
- kty: RSA |
| 223 |
- '...' |
| 224 |
""" |
| 225 |
|
| 226 |
import typing as t |
| 227 |
|
| 228 |
from ansible.module_utils.common.text.converters import to_bytes, to_text |
| 229 |
|
| 230 |
from ansible_collections.community.crypto.plugins.module_utils._acme.acme import ( |
| 231 |
ACMEClient, |
| 232 |
create_backend, |
| 233 |
create_default_argspec, |
| 234 |
) |
| 235 |
from ansible_collections.community.crypto.plugins.module_utils._acme.errors import ( |
| 236 |
ACMEProtocolException, |
| 237 |
ModuleFailException, |
| 238 |
) |
| 239 |
|
| 240 |
|
| 241 |
def main() -> t.NoReturn: |
| 242 |
argument_spec = create_default_argspec(require_account_key=False) |
| 243 |
argument_spec.update_argspec( |
| 244 |
url={"type": "str"}, |
| 245 |
method={ |
| 246 |
"type": "str", |
| 247 |
"choices": ["get", "post", "directory-only"], |
| 248 |
"default": "get", |
| 249 |
}, |
| 250 |
content={"type": "str"}, |
| 251 |
fail_on_acme_error={"type": "bool", "default": True}, |
| 252 |
) |
| 253 |
argument_spec.update( |
| 254 |
required_if=[ |
| 255 |
("method", "get", ["url"]), |
| 256 |
("method", "post", ["url", "content"]), |
| 257 |
("method", "get", ["account_key_src", "account_key_content"], True), |
| 258 |
("method", "post", ["account_key_src", "account_key_content"], True), |
| 259 |
], |
| 260 |
) |
| 261 |
module = argument_spec.create_ansible_module() |
| 262 |
backend = create_backend(module, needs_acme_v2=False) |
| 263 |
|
| 264 |
result: dict[str, t.Any] = {} |
| 265 |
changed = False |
| 266 |
try: |
| 267 |
# Get hold of ACMEClient and ACMEAccount objects (includes directory) |
| 268 |
client = ACMEClient(module=module, backend=backend) |
| 269 |
method: t.Literal["get", "post", "directory-only"] = module.params["method"] |
| 270 |
result["directory"] = client.directory.directory |
| 271 |
# Do we have to do more requests? |
| 272 |
if method != "directory-only": |
| 273 |
url = module.params["url"] |
| 274 |
fail_on_acme_error = module.params["fail_on_acme_error"] |
| 275 |
# Do request |
| 276 |
if method == "get": |
| 277 |
data, info = client.get_request( |
| 278 |
url, parse_json_result=False, fail_on_error=False |
| 279 |
) |
| 280 |
elif method == "post": |
| 281 |
changed = True # only POSTs can change |
| 282 |
data, info = client.send_signed_request( |
| 283 |
url, |
| 284 |
to_bytes(module.params["content"]), |
| 285 |
parse_json_result=False, |
| 286 |
encode_payload=False, |
| 287 |
fail_on_error=False, |
| 288 |
) |
| 289 |
else: |
| 290 |
raise AssertionError("Can never be reached") # pragma: no cover |
| 291 |
# Update results |
| 292 |
result.update( |
| 293 |
{ |
| 294 |
"headers": info, |
| 295 |
"output_text": to_text(data), |
| 296 |
} |
| 297 |
) |
| 298 |
# See if we can parse the result as JSON |
| 299 |
try: |
| 300 |
result["output_json"] = module.from_json(to_text(data)) |
| 301 |
except Exception: |
| 302 |
pass |
| 303 |
# Fail if error was returned |
| 304 |
if fail_on_acme_error and info["status"] >= 400: |
| 305 |
raise ACMEProtocolException(module=module, info=info, content=data) |
| 306 |
# Done! |
| 307 |
module.exit_json(changed=changed, **result) |
| 308 |
except ModuleFailException as e: |
| 309 |
e.do_fail(module=module, **result) |
| 310 |
|
| 311 |
|
| 312 |
if __name__ == "__main__": |
| 313 |
main() |