@@ -63,6 +63,7 @@ def __init__(self, uri):
63
63
try :
64
64
self .connection = psycopg2 .connect (expandedConnInfo )
65
65
except self .connection_error_types () as e :
66
+ # get credentials if cached or assking to the user no more than 3 times
66
67
err = unicode (e )
67
68
uri = self .uri ()
68
69
conninfo = uri .connectionInfo (False )
@@ -85,44 +86,13 @@ def __init__(self, uri):
85
86
except self .connection_error_types () as e :
86
87
if i == 2 :
87
88
raise ConnectionError (e )
88
-
89
89
err = unicode (e )
90
90
finally :
91
- # remove certs (if any) of the expanded connectionInfo
92
- expandedUri = QgsDataSourceURI (newExpandedConnInfo )
93
-
94
- sslCertFile = expandedUri .param ("sslcert" )
95
- if sslCertFile :
96
- sslCertFile = sslCertFile .replace ("'" , "" )
97
- os .remove (sslCertFile )
98
-
99
- sslKeyFile = expandedUri .param ("sslkey" )
100
- if sslKeyFile :
101
- sslKeyFile = sslKeyFile .replace ("'" , "" )
102
- os .remove (sslKeyFile )
103
-
104
- sslCAFile = expandedUri .param ("sslrootcert" )
105
- if sslCAFile :
106
- sslCAFile = sslCAFile .replace ("'" , "" )
107
- os .remove (sslCAFile )
91
+ # clear certs for each time trying to connect
92
+ self ._clearSslTempCertsIfAny (newExpandedConnInfo )
108
93
finally :
109
- # remove certs (if any) of the expanded connectionInfo
110
- expandedUri = QgsDataSourceURI (expandedConnInfo )
111
-
112
- sslCertFile = expandedUri .param ("sslcert" )
113
- if sslCertFile :
114
- sslCertFile = sslCertFile .replace ("'" , "" )
115
- os .remove (sslCertFile )
116
-
117
- sslKeyFile = expandedUri .param ("sslkey" )
118
- if sslKeyFile :
119
- sslKeyFile = sslKeyFile .replace ("'" , "" )
120
- os .remove (sslKeyFile )
121
-
122
- sslCAFile = expandedUri .param ("sslrootcert" )
123
- if sslCAFile :
124
- sslCAFile = sslCAFile .replace ("'" , "" )
125
- os .remove (sslCAFile )
94
+ # clear certs of the first connection try
95
+ self ._clearSslTempCertsIfAny (expandedConnInfo )
126
96
127
97
self .connection .set_isolation_level (psycopg2 .extensions .ISOLATION_LEVEL_AUTOCOMMIT )
128
98
@@ -138,6 +108,33 @@ def __init__(self, uri):
138
108
def _connectionInfo (self ):
139
109
return str (self .uri ().connectionInfo (True ))
140
110
111
+ def _clearSslTempCertsIfAny (self , connectionInfo ):
112
+ # remove certs (if any) of the connectionInfo
113
+ expandedUri = QgsDataSourceURI (connectionInfo )
114
+
115
+ def removeCert (certFile ):
116
+ certFile = certFile .replace ("'" , "" )
117
+ file = QFile (certFile )
118
+ # set permission to allow removing on Win.
119
+ # On linux and Mac if file is set with QFile::>ReadUser
120
+ # does not create problem removin certs
121
+ if not file .setPermissions (QFile .WriteOwner ):
122
+ raise Exception ('Cannot change permissions on {}: error code: {}' .format (file .fileName (), file .error ()))
123
+ if not file .remove ():
124
+ raise Exception ('Cannot remove {}: error code: {}' .format (file .fileName (), file .error ()))
125
+
126
+ sslCertFile = expandedUri .param ("sslcert" )
127
+ if sslCertFile :
128
+ removeCert (sslCertFile )
129
+
130
+ sslKeyFile = expandedUri .param ("sslkey" )
131
+ if sslKeyFile :
132
+ removeCert (sslKeyFile )
133
+
134
+ sslCAFile = expandedUri .param ("sslrootcert" )
135
+ if sslCAFile :
136
+ removeCert (sslCAFile )
137
+
141
138
def _checkSpatial (self ):
142
139
""" check whether postgis_version is present in catalog """
143
140
c = self ._execute (None , u"SELECT COUNT(*) FROM pg_proc WHERE proname = 'postgis_version'" )
0 commit comments