Skip to content

Commit

Permalink
Make sure only supported parameters are written to connection string (p…
Browse files Browse the repository at this point in the history
  • Loading branch information
CyberDem0n authored Nov 12, 2024
1 parent e1faa38 commit efba02f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
5 changes: 4 additions & 1 deletion patroni/postgresql/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -617,7 +617,10 @@ def format_dsn(self, params: Dict[str, Any]) -> str:
def escape(value: Any) -> str:
return re.sub(r'([\'\\ ])', r'\\\1', str(value))

return ' '.join('{0}={1}'.format(kw, escape(params[kw])) for kw in keywords if params.get(kw) is not None)
key_ver = {'target_session_attrs': 100000, 'gssencmode': 120000, 'channel_binding': 130000,
'sslpassword': 130000, 'sslcrldir': 140000, 'sslnegotiation': 170000}
return ' '.join('{0}={1}'.format(kw, escape(params[kw])) for kw in keywords
if params.get(kw) is not None and self._postgresql.major_version >= key_ver.get(kw, 0))

def _write_recovery_params(self, fd: ConfigWriter, recovery_params: CaseInsensitiveDict) -> None:
if self._postgresql.major_version >= 90500:
Expand Down
23 changes: 23 additions & 0 deletions tests/test_postgresql.py
Original file line number Diff line number Diff line change
Expand Up @@ -1182,3 +1182,26 @@ def test_load_current_server_parameters(self):
self.assertEqual(dict(self.p.config._recovery_params),
{'primary_conninfo': {'host': 'a', 'port': '5433', 'passfile': '/blabla', 'sslmode': 'prefer',
'gssencmode': 'prefer', 'channel_binding': 'prefer', 'sslnegotiation': 'postgres'}})

def test_format_dsn(self):
params = {'host': '1', 'port': 2, 'target_session_attrs': 'read-write', 'gssencmode': 'prefer',
'channel_binding': 'prefer', 'sslpassword': 'pwd', 'sslcrldir': '/', 'sslnegotiation': 'postgres'}
self.p._major_version = 90600
self.assertEqual(self.p.config.format_dsn(params), 'host=1 port=2')
self.p._major_version = 100000
self.assertEqual(self.p.config.format_dsn(params), 'host=1 port=2 target_session_attrs=read-write')
self.p._major_version = 120000
self.assertEqual(self.p.config.format_dsn(params),
'host=1 port=2 gssencmode=prefer target_session_attrs=read-write')
self.p._major_version = 130000
self.assertEqual(self.p.config.format_dsn(params),
'host=1 port=2 sslpassword=pwd gssencmode=prefer '
'channel_binding=prefer target_session_attrs=read-write')
self.p._major_version = 140000
self.assertEqual(self.p.config.format_dsn(params),
'host=1 port=2 sslpassword=pwd sslcrldir=/ gssencmode=prefer '
'channel_binding=prefer target_session_attrs=read-write')
self.p._major_version = 170000
self.assertEqual(self.p.config.format_dsn(params),
'host=1 port=2 sslpassword=pwd sslcrldir=/ gssencmode=prefer channel_binding=prefer '
'target_session_attrs=read-write sslnegotiation=postgres')

0 comments on commit efba02f

Please sign in to comment.