Skip to content

Commit 1e81615

Browse files
Selection of existing connection works with concealed password
1 parent 59a9719 commit 1e81615

File tree

3 files changed

+21
-11
lines changed

3 files changed

+21
-11
lines changed

NEWS.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,3 +149,4 @@ Deleted Plugin import left behind in 0.2.2
149149
* added README example (thanks tanhuil)
150150
* bugfix in executing column_local_vars (thanks tebeka)
151151
* pgspecial installation optional (thanks jstoebel and arjoe)
152+
* conceal passwords in connection strings (thanks jstoebel)

run_tests.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
#!/bin/bash
2-
python -c "import pytest; pytest.main(['.', '-x', '--pdb'])"
2+
ipython -c "import pytest; pytest.main(['.', '-x', '--pdb'])"
33
# Insert breakpoints with `import pytest; pytest.set_trace()`

src/sql/connection.py

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,26 @@
11
import sqlalchemy
22
import os
3+
import re
34

45
class ConnectionError(Exception):
56
pass
67

78

9+
def rough_dict_get(dct, sought, default=None):
10+
'''
11+
Like dct.get(sought), but any key containing sought will do.
12+
13+
If there is a `@` in sought, seek each piece separately.
14+
This lets `me@server` match `me:***@myserver/db`
15+
'''
16+
17+
sought = sought.split('@')
18+
for (key, val) in dct.items():
19+
if not any(s.lower() not in key.lower() for s in sought):
20+
return val
21+
return default
22+
23+
824
class Connection(object):
925
current = None
1026
connections = {}
@@ -25,8 +41,7 @@ def __init__(self, connect_str=None):
2541
self.metadata = sqlalchemy.MetaData(bind=engine)
2642
self.name = self.assign_name(engine)
2743
self.session = engine.connect()
28-
self.connections[self.name] = self
29-
self.connections[str(self.metadata.bind.url)] = self
44+
self.connections[repr(self.metadata.bind.url)] = self
3045
Connection.current = self
3146

3247
@classmethod
@@ -37,8 +52,7 @@ def set(cls, descriptor):
3752
if isinstance(descriptor, Connection):
3853
cls.current = descriptor
3954
else:
40-
existing = cls.connections.get(descriptor) or \
41-
cls.connections.get(descriptor.lower())
55+
existing = rough_dict_get(cls.connections, descriptor)
4256
cls.current = existing or Connection(descriptor)
4357
else:
4458
if cls.connections:
@@ -52,12 +66,7 @@ def set(cls, descriptor):
5266

5367
@classmethod
5468
def assign_name(cls, engine):
55-
core_name = '%s@%s' % (engine.url.username or '', engine.url.database)
56-
incrementer = 1
57-
name = core_name
58-
while name in cls.connections:
59-
name = '%s_%d' % (core_name, incrementer)
60-
incrementer += 1
69+
name = '%s@%s' % (engine.url.username or '', engine.url.database)
6170
return name
6271

6372
@classmethod

0 commit comments

Comments
 (0)