Skip to content

Commit c0f8fde

Browse files
committed
Merge remote-tracking branch 'origin/master'
2 parents dca2e68 + 8cc2f2c commit c0f8fde

File tree

8 files changed

+63
-36
lines changed

8 files changed

+63
-36
lines changed

pyredis/client.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -571,14 +571,20 @@ class SentinelClient(object):
571571
:param sentinels:
572572
Accepts a list of sentinels in this form: [('sentinel1', 26379), ('sentinel2', 26379), ('sentinel3', 26379)]
573573
:type sentinels: list
574+
575+
:param password:
576+
Password used for authentication of Sentinel instance itself. If None, no authentication is done.
577+
Only available starting with Redis 5.0.1.
578+
:type password: str
574579
"""
575-
def __init__(self, sentinels):
580+
def __init__(self, sentinels, password=None):
576581
self._conn = None
577582
self._sentinels = deque(sentinels)
583+
self._password = password
578584

579585
def _sentinel_connect(self, sentinel):
580586
host, port = sentinel
581-
self._conn = Connection(host=host, port=port, conn_timeout=0.1, sentinel=True)
587+
self._conn = Connection(host=host, port=port, conn_timeout=0.1, sentinel=True, password=self._password)
582588
try:
583589
self.execute('PING')
584590
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/helper.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,12 @@ def slot_from_key(key):
3838

3939

4040
class ClusterMap(object):
41-
def __init__(self, seeds, lock=Lock()):
41+
def __init__(self, seeds, password=None, lock=Lock()):
4242
self._id = uuid4()
4343
self._lock = lock
4444
self._map = {}
4545
self._seeds = deque(seeds)
46+
self._password = password
4647

4748
@property
4849
def id(self):
@@ -54,7 +55,7 @@ def _make_str(endpoint):
5455

5556
def _fetch_map(self):
5657
for seed in self._seeds:
57-
conn = Connection(host=seed[0], port=seed[1], encoding='utf-8')
58+
conn = Connection(host=seed[0], port=seed[1], encoding='utf-8', password=self._password)
5859
try:
5960
conn.write(b'CLUSTER', b'SLOTS')
6061
return conn.read()

pyredis/pool.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -213,9 +213,9 @@ class ClusterPool(
213213
:type retries: int
214214
"""
215215

216-
def __init__(self, seeds, slave_ok=False, **kwargs):
217-
super().__init__(**kwargs)
218-
self._map = ClusterMap(seeds=seeds)
216+
def __init__(self, seeds, slave_ok=False, password=None, **kwargs):
217+
super().__init__(password=password, **kwargs)
218+
self._map = ClusterMap(seeds=seeds, password=password)
219219
self._slave_ok = slave_ok
220220
self._cluster = True
221221

@@ -451,10 +451,15 @@ class SentinelHashPool(
451451
:param retries:
452452
In case a sentinel delivers stale data, how many other sentinels should be tried.
453453
:type retries: int
454+
455+
:param sentinel_password:
456+
Password used for authentication of Sentinel instance itself. If None, no authentication is done.
457+
Only available starting with Redis 5.0.1.
458+
:type sentinel_password: str
454459
"""
455-
def __init__(self, sentinels, buckets, slave_ok=False, retries=3, **kwargs):
460+
def __init__(self, sentinels, buckets, slave_ok=False, retries=3, sentinel_password=None, **kwargs):
456461
super().__init__(**kwargs)
457-
self._sentinel = SentinelClient(sentinels=sentinels)
462+
self._sentinel = SentinelClient(sentinels=sentinels, password=sentinel_password)
458463
self._buckets = buckets
459464
self._slave_ok = slave_ok
460465
self._retries = retries
@@ -612,10 +617,15 @@ class SentinelPool(
612617
:param retries:
613618
In case a sentinel delivers stale data, how many other sentinels should be tried.
614619
:type retries: int
620+
621+
:param sentinel_password:
622+
Password used for authentication of Sentinel instance itself. If None, no authentication is done.
623+
Only available starting with Redis 5.0.1.
624+
:type sentinel_password: str
615625
"""
616-
def __init__(self, sentinels, name, slave_ok=False, retries=3, **kwargs):
626+
def __init__(self, sentinels, name, slave_ok=False, retries=3, sentinel_password=None, **kwargs):
617627
super().__init__(**kwargs)
618-
self._sentinel = SentinelClient(sentinels=sentinels)
628+
self._sentinel = SentinelClient(sentinels=sentinels, password=sentinel_password)
619629
self._name = name
620630
self._slave_ok = slave_ok
621631
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_helper.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,10 @@ def setUp(self):
104104
]
105105

106106
def test___init__(self):
107-
clustermap = ClusterMap(self.seeds)
107+
clustermap = ClusterMap(self.seeds, password='blubber')
108108
self.assertEqual(clustermap._map, {})
109109
self.assertEqual(clustermap._seeds, deque(self.seeds))
110+
self.assertEqual(clustermap._password, 'blubber')
110111

111112
def test__make_str(self):
112113
result = ClusterMap._make_str(['127.0.0.1', 7002])
@@ -118,13 +119,14 @@ def test__fetch_map_first_try_ok(self):
118119

119120
self.connection_mock.return_value = conn1
120121

121-
clustermap = ClusterMap(self.seeds)
122+
clustermap = ClusterMap(self.seeds, password="blubber")
122123

123124
result = clustermap._fetch_map()
124125

125126
self.assertEqual(result, self.map)
126127
conn1.write.assert_called_with(b'CLUSTER', b'SLOTS')
127128
self.assertTrue(conn1.close.called)
129+
self.connection_mock.assert_called_with(host='host1', port=12345, encoding='utf-8', password='blubber')
128130

129131
def test__fetch_map_second_try_ok(self):
130132
conn1 = Mock()
@@ -160,9 +162,9 @@ def test__fetch_map_exception(self):
160162
self.assertRaises(PyRedisError, clustermap._fetch_map)
161163

162164
self.connection_mock.assert_has_calls([
163-
call(host='host1', port=12345, encoding='utf-8'),
164-
call(host='host2', port=12345, encoding='utf-8'),
165-
call(host='host3', port=12345, encoding='utf-8')
165+
call(host='host1', port=12345, encoding='utf-8', password=None),
166+
call(host='host2', port=12345, encoding='utf-8', password=None),
167+
call(host='host3', port=12345, encoding='utf-8', password=None)
166168
])
167169

168170
conn1.write.assert_called_with(b'CLUSTER', b'SLOTS')

tests/unit/test_pool.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -191,24 +191,26 @@ def setUp(self):
191191

192192
self.addCleanup(patch.stopall)
193193

194-
self.pool = pyredis.pool.ClusterPool(seeds=[('seed1', 12345), ('seed2', 12345), ('seed3', 12345)])
194+
self.pool = pyredis.pool.ClusterPool(seeds=[('seed1', 12345), ('seed2', 12345), ('seed3', 12345)],
195+
password='blubber')
195196

196197
def test___init__(self):
197-
self.map_mock.assert_called_with(seeds=[('seed1', 12345), ('seed2', 12345), ('seed3', 12345)])
198+
self.map_mock.assert_called_with(seeds=[('seed1', 12345), ('seed2', 12345), ('seed3', 12345)],
199+
password='blubber')
198200
self.assertEqual(self.pool._map, self.map_mock_inst)
199201
self.assertFalse(self.pool.slave_ok)
200202

201203
def test___init__slave_ok_true(self):
202204
self.pool = pyredis.pool.ClusterPool(seeds=[('seed1', 12345), ('seed2', 12345), ('seed3', 12345)], slave_ok=True)
203-
self.map_mock.assert_called_with(seeds=[('seed1', 12345), ('seed2', 12345), ('seed3', 12345)])
205+
self.map_mock.assert_called_with(seeds=[('seed1', 12345), ('seed2', 12345), ('seed3', 12345)], password=None)
204206
self.assertEqual(self.pool._map, self.map_mock_inst)
205207
self.assertTrue(self.pool.slave_ok)
206208

207209
def test__connect(self):
208210
client = self.pool._connect()
209211
self.client_mock.assert_called_with(
210212
conn_timeout=2,
211-
password=None,
213+
password='blubber',
212214
read_timeout=2,
213215
cluster_map=self.pool._map,
214216
encoding=None,
@@ -313,7 +315,7 @@ def setUp(self):
313315
def test___init__default_args(self):
314316
pool = pyredis.pool.SentinelPool(sentinels=[('host1', 12345)], name='mymaster')
315317
pool._sentinel.sentinels = [('host1', 12345)]
316-
self.sentinelclient_mock.assert_called_with(sentinels=[('host1', 12345)])
318+
self.sentinelclient_mock.assert_called_with(sentinels=[('host1', 12345)], password=None)
317319
self.assertEqual(pool.name, 'mymaster')
318320
self.assertEqual(pool.retries, 3)
319321
self.assertFalse(pool.slave_ok)
@@ -325,10 +327,11 @@ def test___init__default_cust_args(self):
325327
sentinels=[('host1', 12345)],
326328
name='mymaster',
327329
slave_ok=True,
328-
retries=5
330+
retries=5,
331+
sentinel_password='blubber'
329332
)
330333
pool._sentinel.sentinels = [('host1', 12345)]
331-
self.sentinelclient_mock.assert_called_with(sentinels=[('host1', 12345)])
334+
self.sentinelclient_mock.assert_called_with(sentinels=[('host1', 12345)], password='blubber')
332335
self.assertEqual(pool.name, 'mymaster')
333336
self.assertEqual(pool.retries, 5)
334337
self.assertTrue(pool.slave_ok)

0 commit comments

Comments
 (0)