Skip to content

Commit

Permalink
[auth][db_manager]removed certs (if any) after postgres connection
Browse files Browse the repository at this point in the history
  • Loading branch information
luipir committed Jan 13, 2016
1 parent 68478a7 commit 5fb798e
Showing 1 changed file with 41 additions and 3 deletions.
44 changes: 41 additions & 3 deletions python/plugins/db_manager/db_plugins/postgis/connector.py
Expand Up @@ -23,7 +23,7 @@
"""

from PyQt4.QtCore import QRegExp
from qgis.core import QgsCredentials
from qgis.core import QgsCredentials, QgsDataSourceURI

from ..connector import DBConnector
from ..plugin import ConnectionError, DbError, Table
Expand Down Expand Up @@ -51,8 +51,9 @@ def __init__(self, uri):
username = uri.username() or os.environ.get('PGUSER') or os.environ.get('USER')
password = uri.password() or os.environ.get('PGPASSWORD')

expandedConnInfo = self._connectionInfo()
try:
self.connection = psycopg2.connect(self._connectionInfo().encode('utf-8'))
self.connection = psycopg2.connect(expandedConnInfo.encode('utf-8'))
except self.connection_error_types() as e:
err = unicode(e)
uri = self.uri()
Expand All @@ -69,14 +70,51 @@ def __init__(self, uri):
if password:
uri.setPassword(password)

newExpandedConnInfo = uri.connectionInfo(True)
try:
self.connection = psycopg2.connect(uri.connectionInfo(True).encode('utf-8'))
self.connection = psycopg2.connect(newExpandedConnInfo.encode('utf-8'))
QgsCredentials.instance().put(conninfo, username, password)
except self.connection_error_types() as e:
if i == 2:
raise ConnectionError(e)

err = unicode(e)
finally:
# remove certs (if any) of the expanded connectionInfo
expandedUri = QgsDataSourceURI(newExpandedConnInfo)

sslCertFile = expandedUri.param("sslcert")
if sslCertFile:
sslCertFile = sslCertFile.replace("'", "")
os.remove(sslCertFile)

sslKeyFile = expandedUri.param("sslkey")
if sslKeyFile:
sslKeyFile = sslKeyFile.replace("'", "")
os.remove(sslKeyFile)

sslCAFile = expandedUri.param("sslrootcert")
if sslCAFile:
sslCAFile = sslCAFile.replace("'", "")
os.remove(sslCAFile)
finally:
# remove certs (if any) of the expanded connectionInfo
expandedUri = QgsDataSourceURI(expandedConnInfo)

sslCertFile = expandedUri.param("sslcert")
if sslCertFile:
sslCertFile = sslCertFile.replace("'", "")
os.remove(sslCertFile)

sslKeyFile = expandedUri.param("sslkey")
if sslKeyFile:
sslKeyFile = sslKeyFile.replace("'", "")
os.remove(sslKeyFile)

sslCAFile = expandedUri.param("sslrootcert")
if sslCAFile:
sslCAFile = sslCAFile.replace("'", "")
os.remove(sslCAFile)

self.connection.set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT)

Expand Down

0 comments on commit 5fb798e

Please sign in to comment.