changeset 410a48a732a3 in trytond:5.0
details: https://hg.tryton.org/trytond?cmd=changeset;node=410a48a732a3
description:
        Replace dsn by params to connect to postgresql

        It is safer to use explicit params than construct DSN string.

        issue8270
        review269321002
        (grafted from f1ee858677a72976aa2c74bb477c28990f03e3b1)
diffstat:

 CHANGELOG                              |   2 ++
 trytond/backend/postgresql/database.py |  26 ++++++++++++++++----------
 2 files changed, 18 insertions(+), 10 deletions(-)

diffs (60 lines):

diff -r adc38a451a47 -r 410a48a732a3 CHANGELOG
--- a/CHANGELOG Thu Apr 04 19:49:05 2019 +0200
+++ b/CHANGELOG Mon Apr 15 15:56:03 2019 +0200
@@ -1,3 +1,5 @@
+* Replace dsn by params to connect to postgresql
+
 Version 5.0.6 - 2019-04-02
 * Bug fixes (see mercurial logs for details)
 * Check read access on field in search order (issue8189)
diff -r adc38a451a47 -r 410a48a732a3 trytond/backend/postgresql/database.py
--- a/trytond/backend/postgresql/database.py    Thu Apr 04 19:49:05 2019 +0200
+++ b/trytond/backend/postgresql/database.py    Mon Apr 15 15:56:03 2019 +0200
@@ -118,7 +118,7 @@
                 inst = DatabaseInterface.__new__(cls, name=name)
                 logger.info('connect to "%s"', name)
                 inst._connpool = ThreadedConnectionPool(
-                    minconn, _maxconn, cls.dsn(name),
+                    minconn, _maxconn, **cls._connection_params(name),
                     cursor_factory=LoggingCursor)
                 cls._databases[name] = inst
             inst._last_use = datetime.now()
@@ -128,15 +128,20 @@
         super(Database, self).__init__(name)
 
     @classmethod
-    def dsn(cls, name):
+    def _connection_params(cls, name):
         uri = parse_uri(config.get('database', 'uri'))
-        host = uri.hostname and "host=%s" % uri.hostname or ''
-        port = uri.port and "port=%s" % uri.port or ''
-        name = "dbname=%s" % name
-        user = uri.username and "user=%s" % uri.username or ''
-        password = ("password=%s" % urllib.parse.unquote_plus(uri.password)
-            if uri.password else '')
-        return '%s %s %s %s %s' % (host, port, name, user, password)
+        params = {
+            'dbname': name,
+            }
+        if uri.username:
+            params['user'] = uri.username
+        if uri.password:
+            params['password'] = urllib.parse.unquote_plus(uri.password)
+        if uri.hostname:
+            params['host'] = uri.hostname
+        if uri.port:
+            params['port'] = uri.port
+        return params
 
     def connect(self):
         return self
@@ -205,7 +210,8 @@
             res = []
             for db_name, in cursor:
                 try:
-                    with connect(self.dsn(db_name)) as conn:
+                    with connect(**self._connection_params(db_name)
+                            ) as conn:
                         if self._test(conn, hostname=hostname):
                             res.append(db_name)
                 except Exception:

Reply via email to