From 044a3565207d55cdc7afc95c20fe42697732b8ae Mon Sep 17 00:00:00 2001 From: Takashi Kajinami Date: Sun, 4 Feb 2024 01:51:16 +0900 Subject: [PATCH] redis: Fix parsing of sentinel fallbacks containing IPv6 address The sentinel fallbacks option may include IPv6 addresses like ['[::1]:26379'], and bare split can't handle this case properly. This ensures the parse logic distinguishes the separator of host and port and the characters in IPv6 addresses. Closes-Bug: #2052353 Change-Id: Ibb4d26022864ece4abf91fd2d2fd665998d80bd8 --- tooz/drivers/redis.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/tooz/drivers/redis.py b/tooz/drivers/redis.py index 9b2ef031..03bd4240 100644 --- a/tooz/drivers/redis.py +++ b/tooz/drivers/redis.py @@ -417,6 +417,18 @@ return 1 _dumps = staticmethod(utils.dumps) _loads = staticmethod(utils.loads) + @classmethod + def _parse_sentinel(cls, sentinel): + # IPv6 (eg. [::1]:6379 ) + match = re.search(r'\[(\S+)\]:(\d+)', sentinel) + if match: + return (match[1], int(match[2])) + # IPv4 or hostname (eg. 127.0.0.1:6379 or localhost:6379) + match = re.search(r'(\S+):(\d+)', sentinel) + if match: + return (match[1], int(match[2])) + raise ValueError('Malformed sentinel server format') + @classmethod def _make_client(cls, parsed_url, options, default_socket_timeout): kwargs = {} @@ -451,7 +463,7 @@ return 1 # sentinel arg. if 'sentinel' in kwargs: sentinel_hosts = [ - tuple(fallback.split(':')) + cls._parse_sentinel(fallback) for fallback in kwargs.get('sentinel_fallback', []) ] sentinel_hosts.insert(0, (kwargs['host'], kwargs['port']))