Skip to content

Commit 740b93e

Browse files
committed
Fix pool connection failure when a password is present (#16)
Make sure that password does not get removed from initial connection settings so that subsequent pool connections can reuse it properly.
1 parent 8bc19b0 commit 740b93e

File tree

2 files changed

+48
-2
lines changed

2 files changed

+48
-2
lines changed

asyncpg/protocol/coreproto.pyx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ cdef class CoreProtocol:
1313
def __init__(self, con_args):
1414
self.buffer = ReadBuffer()
1515
self.user = con_args.get('user')
16-
self.password = con_args.pop('password', None)
16+
self.password = con_args.get('password')
1717
self.auth_msg = None
1818
self.con_args = con_args
1919
self.transport = None
@@ -497,6 +497,8 @@ cdef class CoreProtocol:
497497
buf.write_bytestring("'{}'".format(self.encoding).encode('ascii'))
498498

499499
for param in self.con_args:
500+
if param == 'password':
501+
continue
500502
buf.write_str(param, self.encoding)
501503
buf.write_str(self.con_args[param], self.encoding)
502504

tests/test_pool.py

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from asyncpg import _testbase as tb
1111

1212

13-
class TestPool(tb.ClusterTestCase):
13+
class TestPool(tb.ConnectedTestCase):
1414

1515
async def test_pool_01(self):
1616
for n in {1, 3, 5, 10, 20, 100}:
@@ -94,3 +94,47 @@ async def setup(con):
9494
con = await pool.acquire()
9595

9696
self.assertIs(con, await fut)
97+
98+
async def test_pool_auth(self):
99+
if not self.cluster.is_managed():
100+
self.skipTest('unmanaged cluster')
101+
102+
self.cluster.reset_hba()
103+
104+
self.cluster.add_hba_entry(
105+
type='local',
106+
database='postgres', user='pooluser',
107+
auth_method='md5')
108+
109+
self.cluster.add_hba_entry(
110+
type='host', address='127.0.0.0/32',
111+
database='postgres', user='pooluser',
112+
auth_method='md5')
113+
114+
self.cluster.reload()
115+
116+
try:
117+
await self.con.execute('''
118+
CREATE ROLE pooluser WITH LOGIN PASSWORD 'poolpassword'
119+
''')
120+
121+
pool = await self.create_pool(database='postgres',
122+
user='pooluser',
123+
password='poolpassword',
124+
min_size=5, max_size=10)
125+
126+
async def worker():
127+
con = await pool.acquire()
128+
self.assertEqual(await con.fetchval('SELECT 1'), 1)
129+
await pool.release(con)
130+
131+
tasks = [worker() for _ in range(5)]
132+
await asyncio.gather(*tasks, loop=self.loop)
133+
await pool.close()
134+
135+
finally:
136+
await self.con.execute('DROP ROLE pooluser')
137+
138+
# Reset cluster's pg_hba.conf since we've meddled with it
139+
self.cluster.trust_local_connections()
140+
self.cluster.reload()

0 commit comments

Comments
 (0)