Skip to content

Commit a29cb0a

Browse files
author
Tarcisio Nunes
committed
bugfix, initializing SentinelPool with password to work with sentinel authentication available starting with Redis 5.0.1
1 parent 759edf7 commit a29cb0a

File tree

6 files changed

+29
-22
lines changed

6 files changed

+29
-22
lines changed

pyredis/client.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -572,13 +572,14 @@ class SentinelClient(object):
572572
Accepts a list of sentinels in this form: [('sentinel1', 26379), ('sentinel2', 26379), ('sentinel3', 26379)]
573573
:type sentinels: list
574574
"""
575-
def __init__(self, sentinels):
575+
def __init__(self, sentinels, password=None):
576576
self._conn = None
577577
self._sentinels = deque(sentinels)
578+
self._password = password
578579

579580
def _sentinel_connect(self, sentinel):
580581
host, port = sentinel
581-
self._conn = Connection(host=host, port=port, conn_timeout=0.1, sentinel=True)
582+
self._conn = Connection(host=host, port=port, conn_timeout=0.1, sentinel=True, password=self._password)
582583
try:
583584
self.execute('PING')
584585
return True

pyredis/connection.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,6 @@ def __init__(
8888
self.database = database
8989

9090
def _authenticate(self):
91-
if self._sentinel:
92-
return
9391
if self.password:
9492
self.write('AUTH', self.password)
9593
try:
@@ -110,8 +108,8 @@ def _connect(self):
110108
self._reader = Reader(encoding=self._encoding)
111109
else:
112110
self._reader = Reader()
111+
self._authenticate()
113112
if not self._sentinel:
114-
self._authenticate()
115113
self._setdb()
116114
self._set_read_only()
117115
self._sock.settimeout(self._read_timeout)

pyredis/pool.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -452,9 +452,9 @@ class SentinelHashPool(
452452
In case a sentinel delivers stale data, how many other sentinels should be tried.
453453
:type retries: int
454454
"""
455-
def __init__(self, sentinels, buckets, slave_ok=False, retries=3, **kwargs):
455+
def __init__(self, sentinels, buckets, slave_ok=False, retries=3, sentinel_password=None, **kwargs):
456456
super().__init__(**kwargs)
457-
self._sentinel = SentinelClient(sentinels=sentinels)
457+
self._sentinel = SentinelClient(sentinels=sentinels, password=sentinel_password)
458458
self._buckets = buckets
459459
self._slave_ok = slave_ok
460460
self._retries = retries
@@ -613,9 +613,9 @@ class SentinelPool(
613613
In case a sentinel delivers stale data, how many other sentinels should be tried.
614614
:type retries: int
615615
"""
616-
def __init__(self, sentinels, name, slave_ok=False, retries=3, **kwargs):
616+
def __init__(self, sentinels, name, slave_ok=False, retries=3, sentinel_password=None, **kwargs):
617617
super().__init__(**kwargs)
618-
self._sentinel = SentinelClient(sentinels=sentinels)
618+
self._sentinel = SentinelClient(sentinels=sentinels, password=sentinel_password)
619619
self._name = name
620620
self._slave_ok = slave_ok
621621
self._retries = retries

tests/unit/test_client.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -470,12 +470,12 @@ def test__sentinel_connect(self):
470470
conn_mock = Mock()
471471
self.connection_mock.return_value = conn_mock
472472
sentinels = [('host1', 12345), ('host2', 12345), ('host3', 12345)]
473-
client = pyredis.client.SentinelClient(sentinels=sentinels)
473+
client = pyredis.client.SentinelClient(sentinels=sentinels, password='blubber')
474474
client.execute = Mock()
475475

476476
self.assertTrue(client._sentinel_connect(sentinel=('host1', 12345)))
477477
self.connection_mock.assert_called_with(
478-
host='host1', port=12345, conn_timeout=0.1, sentinel=True)
478+
host='host1', port=12345, conn_timeout=0.1, sentinel=True, password='blubber')
479479
client.execute.assert_called_with('PING')
480480
self.assertEqual(client._conn, conn_mock)
481481

@@ -490,7 +490,7 @@ def test__sentinel_connect_conn_exception(self):
490490

491491
self.assertFalse(client._sentinel_connect(sentinel=('host1', 12345)))
492492
self.connection_mock.assert_called_with(
493-
host='host1', port=12345, conn_timeout=0.1, sentinel=True)
493+
host='host1', port=12345, conn_timeout=0.1, sentinel=True, password=None)
494494
client.execute.assert_called_with('PING')
495495
client.close.assert_called_with()
496496

tests/unit/test_connection.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -517,11 +517,18 @@ def test_read_exception_result_raise(self):
517517
connection._reader = reader_mock
518518
self.assertRaises(ReplyError, connection.read)
519519

520-
def test_read_exception_result_no_raise(self):
521-
connection = pyredis.connection.Connection(host='127.0.0.1', encoding='utf-8')
522-
connection._sock = True
520+
def test_authenticate_sentinel(self):
521+
connection = pyredis.connection.Connection(host='127.0.0.1', encoding='utf-8', sentinel=True)
522+
authenticate_mock = Mock()
523+
setdb_mock = Mock()
524+
set_read_only_mock = Mock()
523525

524-
reader_mock = Mock()
525-
reader_mock.gets.return_value = ReplyError('blub')
526-
connection._reader = reader_mock
527-
connection.read(raise_on_result_err=False)
526+
connection._authenticate = authenticate_mock
527+
connection._setdb = setdb_mock
528+
connection._set_read_only = set_read_only_mock
529+
530+
connection._connect()
531+
532+
authenticate_mock.assert_called()
533+
setdb_mock.assert_not_called()
534+
set_read_only_mock.assert_not_called()

tests/unit/test_pool.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ def setUp(self):
315315
def test___init__default_args(self):
316316
pool = pyredis.pool.SentinelPool(sentinels=[('host1', 12345)], name='mymaster')
317317
pool._sentinel.sentinels = [('host1', 12345)]
318-
self.sentinelclient_mock.assert_called_with(sentinels=[('host1', 12345)])
318+
self.sentinelclient_mock.assert_called_with(sentinels=[('host1', 12345)], password=None)
319319
self.assertEqual(pool.name, 'mymaster')
320320
self.assertEqual(pool.retries, 3)
321321
self.assertFalse(pool.slave_ok)
@@ -327,10 +327,11 @@ def test___init__default_cust_args(self):
327327
sentinels=[('host1', 12345)],
328328
name='mymaster',
329329
slave_ok=True,
330-
retries=5
330+
retries=5,
331+
sentinel_password='blubber'
331332
)
332333
pool._sentinel.sentinels = [('host1', 12345)]
333-
self.sentinelclient_mock.assert_called_with(sentinels=[('host1', 12345)])
334+
self.sentinelclient_mock.assert_called_with(sentinels=[('host1', 12345)], password='blubber')
334335
self.assertEqual(pool.name, 'mymaster')
335336
self.assertEqual(pool.retries, 5)
336337
self.assertTrue(pool.slave_ok)

0 commit comments

Comments
 (0)