changeset e07943bc461d in trytond:4.6
details: https://hg.tryton.org/trytond?cmd=changeset;node=e07943bc461d
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)
        (grafted from 5e0629dbc137a8e0051e9fa13cd8d8b276cdbde4)
diffstat:

 CHANGELOG                              |   2 ++
 trytond/backend/postgresql/database.py |  30 ++++++++++++++++++------------
 2 files changed, 20 insertions(+), 12 deletions(-)

diffs (61 lines):

diff -r 4e78693bbc19 -r e07943bc461d CHANGELOG
--- a/CHANGELOG Tue Mar 26 16:03:20 2019 +0100
+++ b/CHANGELOG Mon Apr 15 15:56:03 2019 +0200
@@ -1,3 +1,5 @@
+* Replace dsn by params to connect to postgresql
+
 Version 4.6.14 - 2019-04-02
 * Bug fixes (see mercurial logs for details)
 * Check read access on field in search order (issue8189)
diff -r 4e78693bbc19 -r e07943bc461d trytond/backend/postgresql/database.py
--- a/trytond/backend/postgresql/database.py    Tue Mar 26 16:03:20 2019 +0100
+++ b/trytond/backend/postgresql/database.py    Mon Apr 15 15:56:03 2019 +0200
@@ -92,22 +92,27 @@
             minconn = config.getint('database', 'minconn', default=1)
             maxconn = config.getint('database', 'maxconn', default=64)
             inst._connpool = ThreadedConnectionPool(
-                minconn, maxconn, cls.dsn(name),
-                cursor_factory=LoggingCursor)
-
+                minconn, maxconn,
+                cursor_factory=LoggingCursor,
+                **cls._connection_params(name))
             cls._databases[name] = inst
             return inst
 
     @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.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.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
@@ -178,7 +183,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):
                             res.append(db_name)
                 except Exception:

Reply via email to