Skip to content

Commit

Permalink
Add support for setting redis username (celery#1351)
Browse files Browse the repository at this point in the history
* feat: add support for setting redis username

* tests: add redis connparams credentials tests

(cherry picked from commit 2d036f5)
  • Loading branch information
gabor-boros authored and pomegranited committed Aug 28, 2021
1 parent 90a12a1 commit 6150dae
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
4 changes: 3 additions & 1 deletion kombu/transport/redis.py
Original file line number Diff line number Diff line change
Expand Up @@ -899,6 +899,7 @@ def _connparams(self, asynchronous=False):
'host': conninfo.hostname or '127.0.0.1',
'port': conninfo.port or self.connection.default_port,
'virtual_host': conninfo.virtual_host,
'username': conninfo.userid,
'password': conninfo.password,
'max_connections': self.max_connections,
'socket_timeout': self.socket_timeout,
Expand Down Expand Up @@ -929,7 +930,7 @@ def _connparams(self, asynchronous=False):
pass
host = connparams['host']
if '://' in host:
scheme, _, _, _, password, path, query = _parse_url(host)
scheme, _, _, username, password, path, query = _parse_url(host)
if scheme == 'socket':
connparams = self._filter_tcp_connparams(**connparams)
connparams.update({
Expand All @@ -939,6 +940,7 @@ def _connparams(self, asynchronous=False):
connparams.pop('socket_connect_timeout', None)
connparams.pop('socket_keepalive', None)
connparams.pop('socket_keepalive_options', None)
connparams['username'] = username
connparams['password'] = password

connparams.pop('host', None)
Expand Down
16 changes: 14 additions & 2 deletions t/unit/transport/test_redis.py
Original file line number Diff line number Diff line change
Expand Up @@ -694,6 +694,18 @@ def test_connparams_regular_hostname(self):
self.channel.connection.client.hostname = 'george.vandelay.com'
assert self.channel._connparams()['host'] == 'george.vandelay.com'

def test_connparams_username(self):
self.channel.connection.client.userid = 'kombu'
assert self.channel._connparams()['username'] == 'kombu'

def test_connparams_client_credentials(self):
self.channel.connection.client.hostname = \
'redis://foo:[email protected]:6379/0'
connection_parameters = self.channel._connparams()

assert connection_parameters['username'] == 'foo'
assert connection_parameters['password'] == 'bar'

def test_connparams_password_for_unix_socket(self):
self.channel.connection.client.hostname = \
'socket://:foo@/var/run/redis.sock'
Expand Down Expand Up @@ -1434,7 +1446,7 @@ def test_getting_master_from_sentinel(self):
min_other_sentinels=0, password=None, sentinel_kwargs=None,
socket_connect_timeout=None, socket_keepalive=None,
socket_keepalive_options=None, socket_timeout=None,
retry_on_timeout=None)
username=None, retry_on_timeout=None)

master_for = patched.return_value.master_for
master_for.assert_called()
Expand All @@ -1457,7 +1469,7 @@ def test_getting_master_from_sentinel_single_node(self):
min_other_sentinels=0, password=None, sentinel_kwargs=None,
socket_connect_timeout=None, socket_keepalive=None,
socket_keepalive_options=None, socket_timeout=None,
retry_on_timeout=None)
username=None, retry_on_timeout=None)

master_for = patched.return_value.master_for
master_for.assert_called()
Expand Down

0 comments on commit 6150dae

Please sign in to comment.