From ff4966ad3f048e5ff97f966f882b1e7a12ab55ff Mon Sep 17 00:00:00 2001 From: "patchback[bot]" <45432694+patchback[bot]@users.noreply.github.com> Date: Wed, 17 Nov 2021 21:46:13 +0100 Subject: [PATCH] Fix compatibility to fetch_url change in ansible-core devel (#339) (#340) * Fix compatibility to fetch_url change in ansible-core devel. * Adjust tests. (cherry picked from commit 5de50b9f914c106c48eff7c3ed48fc80b3f7fd0e) Co-authored-by: Felix Fontein --- changelogs/fragments/fetch_url-devel.yml | 2 ++ plugins/module_utils/acme/acme.py | 16 +++++++++++++--- plugins/module_utils/acme/errors.py | 8 ++++++-- .../plugins/module_utils/acme/test_errors.py | 2 ++ 4 files changed, 23 insertions(+), 5 deletions(-) create mode 100644 changelogs/fragments/fetch_url-devel.yml diff --git a/changelogs/fragments/fetch_url-devel.yml b/changelogs/fragments/fetch_url-devel.yml new file mode 100644 index 000000000..344859ba1 --- /dev/null +++ b/changelogs/fragments/fetch_url-devel.yml @@ -0,0 +1,2 @@ +minor_changes: + - "acme_* modules - fix usage of ``fetch_url`` with changes in latest ansible-core ``devel`` branch (https://github.com/ansible-collections/community.crypto/pull/339)." diff --git a/plugins/module_utils/acme/acme.py b/plugins/module_utils/acme/acme.py index cf453447c..c8ea47a87 100644 --- a/plugins/module_utils/acme/acme.py +++ b/plugins/module_utils/acme/acme.py @@ -14,8 +14,9 @@ import locale from ansible.module_utils.basic import missing_required_lib -from ansible.module_utils.urls import fetch_url from ansible.module_utils.common.text.converters import to_bytes +from ansible.module_utils.urls import fetch_url +from ansible.module_utils.six import PY3 from ansible_collections.community.crypto.plugins.module_utils.acme.backend_openssl_cli import ( OpenSSLCLIBackend, @@ -228,9 +229,14 @@ def send_signed_request(self, url, payload, key_data=None, jws_header=None, pars resp, info = fetch_url(self.module, url, data=data, headers=headers, method='POST') _assert_fetch_url_success(self.module, resp, info) result = {} + try: + # In Python 2, reading from a closed response yields a TypeError. + # In Python 3, read() simply returns '' + if PY3 and resp.closed: + raise TypeError content = resp.read() - except AttributeError: + except (AttributeError, TypeError): content = info.pop('body', None) if content or not parse_json_result: @@ -284,8 +290,12 @@ def get_request(self, uri, parse_json_result=True, headers=None, get_only=False, _assert_fetch_url_success(self.module, resp, info) try: + # In Python 2, reading from a closed response yields a TypeError. + # In Python 3, read() simply returns '' + if PY3 and resp.closed: + raise TypeError content = resp.read() - except AttributeError: + except (AttributeError, TypeError): content = info.pop('body', None) # Process result diff --git a/plugins/module_utils/acme/errors.py b/plugins/module_utils/acme/errors.py index 9a768477c..0fc40f6e6 100644 --- a/plugins/module_utils/acme/errors.py +++ b/plugins/module_utils/acme/errors.py @@ -7,8 +7,8 @@ from __future__ import absolute_import, division, print_function __metaclass__ = type -from ansible.module_utils.six import binary_type from ansible.module_utils.common.text.converters import to_text +from ansible.module_utils.six import binary_type, PY3 def format_error_problem(problem, subproblem_prefix=''): @@ -52,8 +52,12 @@ def __init__(self, module, msg=None, info=None, response=None, content=None, con # Try to get hold of content, if response is given and content is not provided if content is None and content_json is None and response is not None: try: + # In Python 2, reading from a closed response yields a TypeError. + # In Python 3, read() simply returns '' + if PY3 and response.closed: + raise TypeError content = response.read() - except AttributeError: + except (AttributeError, TypeError): content = info.pop('body', None) # Make sure that content_json is None or a dictionary diff --git a/tests/unit/plugins/module_utils/acme/test_errors.py b/tests/unit/plugins/module_utils/acme/test_errors.py index 317a9701f..fc48f43e4 100644 --- a/tests/unit/plugins/module_utils/acme/test_errors.py +++ b/tests/unit/plugins/module_utils/acme/test_errors.py @@ -115,12 +115,14 @@ def test_format_error_problem(problem, subproblem_prefix, result): def create_regular_response(response_text): response = MagicMock() response.read = MagicMock(return_value=response_text.encode('utf-8')) + response.closed = False return response def create_error_response(): response = MagicMock() response.read = MagicMock(side_effect=AttributeError('read')) + response.closed = True return response