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
This commit is contained in:
Takashi Kajinami 2024-02-04 01:51:16 +09:00
parent 22361cb48d
commit a070cfda89
1 changed files with 14 additions and 1 deletions

View File

@ -17,6 +17,7 @@
from distutils import version
import functools
import logging
import re
import string
import threading
@ -417,6 +418,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 +464,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']))