Include symbolic HTTP status codes in error and log messages when available. (#524)

pull/525/head
Felix Fontein 2022-10-31 21:33:27 +01:00 committed by GitHub
parent fd71773668
commit 4533b3e934
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 29 additions and 17 deletions

View File

@ -0,0 +1,2 @@
minor_changes:
- "acme_* modules - include symbolic HTTP status codes in error and log messages when available (https://github.com/ansible-collections/community.crypto/pull/524)."

View File

@ -38,6 +38,7 @@ from ansible_collections.community.crypto.plugins.module_utils.acme.errors impor
NetworkException, NetworkException,
ModuleFailException, ModuleFailException,
KeyParsingError, KeyParsingError,
format_http_status,
) )
from ansible_collections.community.crypto.plugins.module_utils.acme.utils import ( from ansible_collections.community.crypto.plugins.module_utils.acme.utils import (
@ -69,7 +70,7 @@ def _decode_retry(module, response, info, retry_count):
retry_after = min(max(1, int(info.get('retry-after'))), 60) retry_after = min(max(1, int(info.get('retry-after'))), 60)
except (TypeError, ValueError) as dummy: except (TypeError, ValueError) as dummy:
retry_after = 10 retry_after = 10
module.log('Retrieved a %d HTTP status on %s, retrying in %s seconds' % (info['status'], info['url'], retry_after)) module.log('Retrieved a %s HTTP status on %s, retrying in %s seconds' % (format_http_status(info['status']), info['url'], retry_after))
time.sleep(retry_after) time.sleep(retry_after)
return True return True
@ -138,7 +139,7 @@ class ACMEDirectory(object):
retry_count += 1 retry_count += 1
continue continue
if info['status'] not in (200, 204): if info['status'] not in (200, 204):
raise NetworkException("Failed to get replay-nonce, got status {0}".format(info['status'])) raise NetworkException("Failed to get replay-nonce, got status {0}".format(format_http_status(info['status'])))
return info['replay-nonce'] return info['replay-nonce']

View File

@ -10,6 +10,14 @@ __metaclass__ = type
from ansible.module_utils.common.text.converters import to_text from ansible.module_utils.common.text.converters import to_text
from ansible.module_utils.six import binary_type, PY3 from ansible.module_utils.six import binary_type, PY3
from ansible.module_utils.six.moves.http_client import responses as http_responses
def format_http_status(status_code):
expl = http_responses.get(status_code)
if not expl:
return str(status_code)
return '%d %s' % (status_code, expl)
def format_error_problem(problem, subproblem_prefix=''): def format_error_problem(problem, subproblem_prefix=''):
@ -87,9 +95,10 @@ class ACMEProtocolException(ModuleFailException):
extras['http_status'] = code extras['http_status'] = code
if code is not None and code >= 400 and content_json is not None and 'type' in content_json: if code is not None and code >= 400 and content_json is not None and 'type' in content_json:
if 'status' in content_json and content_json['status'] != code: if 'status' in content_json and content_json['status'] != code:
code = 'status {problem_code} (HTTP status: {http_code})'.format(http_code=code, problem_code=content_json['status']) code = 'status {problem_code} (HTTP status: {http_code})'.format(
http_code=format_http_status(code), problem_code=content_json['status'])
else: else:
code = 'status {problem_code}'.format(problem_code=code) code = 'status {problem_code}'.format(problem_code=format_http_status(code))
subproblems = content_json.pop('subproblems', None) subproblems = content_json.pop('subproblems', None)
add_msg = ' {problem}.'.format(problem=format_error_problem(content_json)) add_msg = ' {problem}.'.format(problem=format_error_problem(content_json))
extras['problem'] = content_json extras['problem'] = content_json
@ -103,12 +112,12 @@ class ACMEProtocolException(ModuleFailException):
problem=format_error_problem(problem, subproblem_prefix='{0}.'.format(index)), problem=format_error_problem(problem, subproblem_prefix='{0}.'.format(index)),
) )
else: else:
code = 'HTTP status {code}'.format(code=code) code = 'HTTP status {code}'.format(code=format_http_status(code))
if content_json is not None: if content_json is not None:
add_msg = ' The JSON error result: {content}'.format(content=content_json) add_msg = ' The JSON error result: {content}'.format(content=content_json)
elif content is not None: elif content is not None:
add_msg = ' The raw error result: {content}'.format(content=to_text(content)) add_msg = ' The raw error result: {content}'.format(content=to_text(content))
msg = '{msg} for {url} with {code}'.format(msg=msg, url=url, code=code) msg = '{msg} for {url} with {code}'.format(msg=msg, url=url, code=format_http_status(code))
elif content_json is not None: elif content_json is not None:
add_msg = ' The JSON result: {content}'.format(content=content_json) add_msg = ' The JSON result: {content}'.format(content=content_json)
elif content is not None: elif content is not None:

View File

@ -166,7 +166,7 @@ TEST_ACME_PROTOCOL_EXCEPTION = [
}, },
}, },
None, None,
'ACME request failed for https://ca.example.com/foo with HTTP status 201.', 'ACME request failed for https://ca.example.com/foo with HTTP status 201 Created.',
{ {
'http_url': 'https://ca.example.com/foo', 'http_url': 'https://ca.example.com/foo',
'http_status': 201, 'http_status': 201,
@ -181,7 +181,7 @@ TEST_ACME_PROTOCOL_EXCEPTION = [
'response': create_regular_response('xxx'), 'response': create_regular_response('xxx'),
}, },
None, None,
'ACME request failed for https://ca.example.com/foo with HTTP status 201. The raw error result: xxx', 'ACME request failed for https://ca.example.com/foo with HTTP status 201 Created. The raw error result: xxx',
{ {
'http_url': 'https://ca.example.com/foo', 'http_url': 'https://ca.example.com/foo',
'http_status': 201, 'http_status': 201,
@ -196,7 +196,7 @@ TEST_ACME_PROTOCOL_EXCEPTION = [
'response': create_regular_response('xxx'), 'response': create_regular_response('xxx'),
}, },
create_decode_error('yyy'), create_decode_error('yyy'),
'ACME request failed for https://ca.example.com/foo with HTTP status 201. The raw error result: xxx', 'ACME request failed for https://ca.example.com/foo with HTTP status 201 Created. The raw error result: xxx',
{ {
'http_url': 'https://ca.example.com/foo', 'http_url': 'https://ca.example.com/foo',
'http_status': 201, 'http_status': 201,
@ -211,7 +211,7 @@ TEST_ACME_PROTOCOL_EXCEPTION = [
'response': create_regular_response('xxx'), 'response': create_regular_response('xxx'),
}, },
lambda content: dict(foo='bar'), lambda content: dict(foo='bar'),
"ACME request failed for https://ca.example.com/foo with HTTP status 201. The JSON error result: {'foo': 'bar'}", "ACME request failed for https://ca.example.com/foo with HTTP status 201 Created. The JSON error result: {'foo': 'bar'}",
{ {
'http_url': 'https://ca.example.com/foo', 'http_url': 'https://ca.example.com/foo',
'http_status': 201, 'http_status': 201,
@ -226,7 +226,7 @@ TEST_ACME_PROTOCOL_EXCEPTION = [
'response': create_error_response(), 'response': create_error_response(),
}, },
None, None,
'ACME request failed for https://ca.example.com/foo with HTTP status 201.', 'ACME request failed for https://ca.example.com/foo with HTTP status 201 Created.',
{ {
'http_url': 'https://ca.example.com/foo', 'http_url': 'https://ca.example.com/foo',
'http_status': 201, 'http_status': 201,
@ -242,7 +242,7 @@ TEST_ACME_PROTOCOL_EXCEPTION = [
'response': create_error_response(), 'response': create_error_response(),
}, },
lambda content: dict(foo='bar'), lambda content: dict(foo='bar'),
"ACME request failed for https://ca.example.com/foo with HTTP status 201. The JSON error result: {'foo': 'bar'}", "ACME request failed for https://ca.example.com/foo with HTTP status 201 Created. The JSON error result: {'foo': 'bar'}",
{ {
'http_url': 'https://ca.example.com/foo', 'http_url': 'https://ca.example.com/foo',
'http_status': 201, 'http_status': 201,
@ -257,7 +257,7 @@ TEST_ACME_PROTOCOL_EXCEPTION = [
'content': 'xxx', 'content': 'xxx',
}, },
None, None,
"ACME request failed for https://ca.example.com/foo with HTTP status 201. The raw error result: xxx", "ACME request failed for https://ca.example.com/foo with HTTP status 201 Created. The raw error result: xxx",
{ {
'http_url': 'https://ca.example.com/foo', 'http_url': 'https://ca.example.com/foo',
'http_status': 201, 'http_status': 201,
@ -277,7 +277,7 @@ TEST_ACME_PROTOCOL_EXCEPTION = [
} }
}, },
None, None,
"ACME request failed for https://ca.example.com/foo with HTTP status 400. The JSON error result: {'foo': 'bar'}", "ACME request failed for https://ca.example.com/foo with HTTP status 400 Bad Request. The JSON error result: {'foo': 'bar'}",
{ {
'http_url': 'https://ca.example.com/foo', 'http_url': 'https://ca.example.com/foo',
'http_status': 400, 'http_status': 400,
@ -295,7 +295,7 @@ TEST_ACME_PROTOCOL_EXCEPTION = [
}, },
}, },
None, None,
"ACME request failed for https://ca.example.com/foo with HTTP status 201. The JSON error result: {'type': 'foo'}", "ACME request failed for https://ca.example.com/foo with HTTP status 201 Created. The JSON error result: {'type': 'foo'}",
{ {
'http_url': 'https://ca.example.com/foo', 'http_url': 'https://ca.example.com/foo',
'http_status': 201, 'http_status': 201,
@ -312,7 +312,7 @@ TEST_ACME_PROTOCOL_EXCEPTION = [
}, },
}, },
None, None,
"ACME request failed for https://ca.example.com/foo with status 400. Error foo.", "ACME request failed for https://ca.example.com/foo with status 400 Bad Request. Error foo.",
{ {
'http_url': 'https://ca.example.com/foo', 'http_url': 'https://ca.example.com/foo',
'http_status': 400, 'http_status': 400,
@ -341,7 +341,7 @@ TEST_ACME_PROTOCOL_EXCEPTION = [
}, },
}, },
None, None,
"ACME request failed for https://ca.example.com/foo with status 400. Error \"Foo Error\" (foo). Subproblems:\n" "ACME request failed for https://ca.example.com/foo with status 400 Bad Request. Error \"Foo Error\" (foo). Subproblems:\n"
"(0) Error bar: \"This is a bar error\".", "(0) Error bar: \"This is a bar error\".",
{ {
'http_url': 'https://ca.example.com/foo', 'http_url': 'https://ca.example.com/foo',