Skip to content

Commit

Permalink
Merge pull request #1404 from tobias-urdin/bp-redis
Browse files Browse the repository at this point in the history
Support authentication and SSL for redis sentinel (backport #1376)
  • Loading branch information
tobias-urdin authored Aug 20, 2024
2 parents 0873cdf + 42c6045 commit 2bb3917
Showing 1 changed file with 24 additions and 8 deletions.
32 changes: 24 additions & 8 deletions gnocchi/common/redis.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@
'ssl_ca_certs',
'sentinel',
'sentinel_fallback',
'sentinel_username',
'sentinel_password',
'sentinel_ssl'
])
"""
"""
Expand All @@ -59,6 +62,7 @@
'retry_on_timeout',
'socket_keepalive',
'ssl',
'sentinel_ssl'
])

#: Client arguments that are expected to be int convertible.
Expand Down Expand Up @@ -167,19 +171,31 @@ def get_client(conf, scripts=None):
if 'sentinel' in kwargs:
sentinel_hosts = [
_parse_sentinel(fallback)
for fallback in kwargs.get('sentinel_fallback', [])
for fallback in kwargs.pop('sentinel_fallback', [])
]
sentinel_hosts.insert(0, (kwargs['host'], kwargs['port']))
sentinel_hosts.insert(0, (kwargs.pop('host'), kwargs.pop('port')))
sentinel_name = kwargs.pop('sentinel')
sentinel_kwargs = {}
# NOTE(tkajinam): Copy socket_* options, according to the logic
# in redis-py
for key in kwargs:
if key.startswith('socket_'):
sentinel_kwargs[key] = kwargs[key]
if kwargs.pop('sentinel_ssl', False):
sentinel_kwargs['ssl'] = True
for key in ('ssl_certfile', 'ssl_keyfile', 'ssl_cafile'):
if key in kwargs:
sentinel_kwargs[key] = kwargs[key]
for key in ('username', 'password'):
if 'sentinel_' + key in kwargs:
sentinel_kwargs[key] = kwargs.pop('sentinel_' + key)
sentinel_server = sentinel.Sentinel(
sentinel_hosts,
socket_timeout=kwargs.get('socket_timeout'))
sentinel_name = kwargs['sentinel']
del kwargs['sentinel']
if 'sentinel_fallback' in kwargs:
del kwargs['sentinel_fallback']
sentinel_kwargs=sentinel_kwargs,
**kwargs)
# The client is a redis.StrictRedis using a
# Sentinel managed connection pool.
client = sentinel_server.master_for(sentinel_name, **kwargs)
client = sentinel_server.master_for(sentinel_name)
else:
client = redis.StrictRedis(**kwargs)

Expand Down

0 comments on commit 2bb3917

Please sign in to comment.