Skip to content

Commit 5d95b64

Browse files
d9poucespre-commit-ci[bot]auvipy
authored
fix: interpret the ssl_check_hostname as a boolean (#2229)
* fix: interpret the ssl_check_hostname as a boolean * fix: interpret the ssl_check_hostname as a boolean, add an unittest * fix: interpret the ssl_check_hostname as a boolean, add an unittest, add a pre-commit pass. * ci: add an integration test, as required * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * ci: remove unused imported packages --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Asif Saif Uddin <[email protected]>
1 parent def208a commit 5d95b64

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

kombu/utils/url.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ def parse_url(url):
3939
if query:
4040
keys = [key for key in query.keys() if key.startswith('ssl_')]
4141
for key in keys:
42-
if key == 'ssl_cert_reqs':
42+
if key == "ssl_check_hostname":
43+
query[key] = query[key].lower() != 'false'
44+
elif key == 'ssl_cert_reqs':
4345
query[key] = parse_ssl_cert_reqs(query[key])
4446
if query[key] is None:
4547
logger.warning('Defaulting to insecure SSL behaviour.')

t/integration/test_redis.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,3 +222,24 @@ def connect_timeout(self):
222222
# note the host/port here is irrelevant because
223223
# connect will raise a socket.timeout
224224
kombu.Connection('redis://localhost:12345').connect()
225+
226+
227+
@pytest.mark.env('redis')
228+
def test_RedisConnection_check_hostname(monkeypatch):
229+
# simulate a connection timeout for a new connection
230+
def connect_check_certificate(self):
231+
if self.check_hostname:
232+
raise OSError("check_hostname=True")
233+
raise socket.timeout("check_hostname=False")
234+
monkeypatch.setattr(
235+
redis.connection.SSLConnection, "_connect", connect_check_certificate)
236+
237+
# ensure the timeout raises a TimeoutError
238+
with pytest.raises(redis.exceptions.TimeoutError):
239+
# note the host/port here is irrelevant because
240+
# connect will raise a socket.timeout, not a CertificateError
241+
kombu.Connection('rediss://localhost:12345?ssl_check_hostname=false').connect()
242+
with pytest.raises(redis.exceptions.ConnectionError):
243+
# note the host/port here is irrelevant because
244+
# connect will raise a CertificateError due to hostname mismatch
245+
kombu.Connection('rediss://localhost:12345?ssl_check_hostname=true').connect()

t/unit/utils/test_url.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,18 +50,27 @@ def test_maybe_sanitize_url(url, expected):
5050

5151
def test_ssl_parameters():
5252
url = 'rediss://user:password@host:6379/0?'
53+
querystring = urlencode({
54+
"ssl_check_hostname": "on",
55+
})
56+
kwargs = parse_url(url + querystring)
57+
assert kwargs['transport'] == 'rediss'
58+
assert kwargs['ssl']['ssl_check_hostname'] is True
59+
5360
querystring = urlencode({
5461
'ssl_cert_reqs': 'required',
5562
'ssl_ca_certs': '/var/ssl/myca.pem',
5663
'ssl_certfile': '/var/ssl/server-cert.pem',
5764
'ssl_keyfile': '/var/ssl/priv/worker-key.pem',
65+
"ssl_check_hostname": "false",
5866
})
5967
kwargs = parse_url(url + querystring)
6068
assert kwargs['transport'] == 'rediss'
6169
assert kwargs['ssl']['ssl_cert_reqs'] == ssl.CERT_REQUIRED
6270
assert kwargs['ssl']['ssl_ca_certs'] == '/var/ssl/myca.pem'
6371
assert kwargs['ssl']['ssl_certfile'] == '/var/ssl/server-cert.pem'
6472
assert kwargs['ssl']['ssl_keyfile'] == '/var/ssl/priv/worker-key.pem'
73+
assert kwargs['ssl']['ssl_check_hostname'] is False
6574

6675
kombu.utils.url.ssl_available = False
6776

0 commit comments

Comments
 (0)