Write pgpass line per host if more than one is specified in connstr (#2192)

Partly addresses #2189
This commit is contained in:
Michael Banck
2022-02-10 15:40:24 +01:00
committed by GitHub
parent d3e3b4e16f
commit 48d8c13e6b
+13 -8
View File
@@ -618,19 +618,19 @@ class ConfigHandler(object):
def _check_passfile(self, passfile, wanted_primary_conninfo):
# If there is a passfile in the primary_conninfo try to figure out that
# the passfile contains the line allowing connection to the given node.
# the passfile contains the line(s) allowing connection to the given node.
# We assume that the passfile was created by Patroni and therefore doing
# the full match and not covering cases when host, port or user are set to '*'
passfile_mtime = mtime(passfile)
if passfile_mtime:
try:
with open(passfile) as f:
wanted_line = self._pgpass_line(wanted_primary_conninfo).strip()
for raw_line in f:
if raw_line.strip() == wanted_line:
self._passfile = passfile
self._passfile_mtime = passfile_mtime
return True
wanted_lines = self._pgpass_line(wanted_primary_conninfo).splitlines()
file_lines = f.read().splitlines()
if set(wanted_lines) == set(file_lines):
self._passfile = passfile
self._passfile_mtime = passfile_mtime
return True
except Exception:
logger.info('Failed to read %s', passfile)
return False
@@ -745,7 +745,12 @@ class ConfigHandler(object):
return re.sub(r'([:\\])', r'\\\1', str(value))
record = {n: escape(record.get(n) or '*') for n in ('host', 'port', 'user', 'password')}
return '{host}:{port}:*:{user}:{password}'.format(**record)
# 'host' could be several comma-separated hostnames, in this case
# we need to write on pgpass line per host
line = ''
for hostname in record.get('host').split(','):
line += hostname + ':{port}:*:{user}:{password}'.format(**record) + '\n'
return line.rstrip()
def write_pgpass(self, record):
line = self._pgpass_line(record)