Skip to content

Commit 748195e

Browse files
committed
Refactor SSL EOF handling
Signed-off-by: Justin Cinkelj <justin.cinkelj@xlab.si>
1 parent fe99b53 commit 748195e

File tree

3 files changed

+16
-22
lines changed

3 files changed

+16
-22
lines changed

plugins/module_utils/client.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
__metaclass__ = type
1010

1111
import json
12+
import ssl
1213
from typing import Any, Optional, Union
1314

1415
from ansible.module_utils.urls import Request, basic_auth_header
@@ -141,6 +142,13 @@ def _request(
141142
and type(e.args[0]) == ConnectionResetError
142143
):
143144
raise ConnectionResetError(e)
145+
elif (
146+
e.args
147+
and isinstance(e.args, tuple)
148+
and type(e.args[0])
149+
in [ssl.SSLEOFError, ssl.SSLZeroReturnError, ssl.SSLSyscallError]
150+
):
151+
raise type(e.args[0])(e)
144152
raise ScaleComputingError(e.reason)
145153
return Response(raw_resp.status, raw_resp.read(), raw_resp.headers)
146154

plugins/modules/certificate.py

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -116,28 +116,14 @@ def ensure_present(
116116
)
117117
sleep(2)
118118
continue
119-
except errors.ScaleComputingError as ex:
119+
except (ssl.SSLEOFError, ssl.SSLZeroReturnError, ssl.SSLSyscallError) as ex:
120120
# Ignore "EOF occurred in violation of protocol (_ssl.c:997)"
121-
# Also other have same problem - https://github.com/psf/requests/issues/3006#issuecomment-183394849.
122-
# We do not use requests library, but problem is the same.
123-
# ex.args - this is what Exception.__init__() stores into object.
124-
ex_reason = ex.args[0]
125-
# ex_reason is instance of SSLEOFError or ssl.SSLEOFError
126-
strerror = ex_reason.strerror
127-
if (
128-
"EOF occurred in violation of protocol" in strerror
129-
and "ssl.c" in strerror
130-
):
131-
module.warn(
132-
f"retry {ii}/{max_retries}, SSLEOFError - ignore and continue"
133-
)
134-
sleep(2)
135-
continue
136-
else:
137-
module.warn(
138-
f"retry {ii}/{max_retries}, re-raise unexpected exception" + str(ex)
139-
)
140-
raise
121+
# Alternative message "TLS/SSL connection has been closed (EOF) (_ssl.c:1129)".
122+
module.warn(
123+
f"retry {ii}/{max_retries}, SSL error {ex.__class__.__name__} - ignore and continue"
124+
)
125+
sleep(2)
126+
continue
141127
after: TypedCertificateToAnsible = dict(certificate=get_certificate(module))
142128
return True, after, dict(before=before, after=after)
143129

tests/integration/targets/certificate/tasks/02_ssl_eof_error.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868
# - certificate_info.record.certificate != default_cert_info.cert
6969
- "{{ certificate_info.record.certificate | replace('\n','') == lookup('file', 'certificate_example.crt') | replace('\n','') }}"
7070
- certificate_info.warnings | length >= 1
71-
- certificate_info.warnings[0] == "retry 0/10, SSLEOFError - ignore and continue"
71+
- certificate_info.warnings[0] == "retry 0/10, SSL error SSLZeroReturnError - ignore and continue"
7272

7373
- name: Get new / uploaded cert from cluster
7474
community.crypto.get_certificate:

0 commit comments

Comments
 (0)