Skip to content

Fix SSL support in connection pool. #120

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions asyncpg/pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ async def connect(self):
**self._connect_kwargs)
self._pool._working_addr = con._addr
self._pool._working_opts = con._opts
self._pool._working_ssl_context = con._ssl_context

else:
# We've connected before and have a resolved address
Expand All @@ -126,9 +127,10 @@ async def connect(self):
else:
host, port = self._pool._working_addr

con = await self._pool._connect(host=host, port=port,
loop=self._pool._loop,
**self._pool._working_opts)
con = await self._pool._connect(
host=host, port=port, loop=self._pool._loop,
ssl=self._pool._working_ssl_context,
**self._pool._working_opts)

if self._init is not None:
await self._init(con)
Expand Down Expand Up @@ -248,7 +250,7 @@ class Pool:
"""

__slots__ = ('_queue', '_loop', '_minsize', '_maxsize',
'_working_addr', '_working_opts',
'_working_addr', '_working_opts', '_working_ssl_context',
'_holders', '_initialized', '_closed')

def __init__(self, *connect_args,
Expand Down Expand Up @@ -292,6 +294,7 @@ def __init__(self, *connect_args,

self._working_addr = None
self._working_opts = None
self._working_ssl_context = None

self._closed = False

Expand Down
25 changes: 25 additions & 0 deletions tests/test_connect.py
Original file line number Diff line number Diff line change
Expand Up @@ -579,3 +579,28 @@ async def test_ssl_connection_default_context(self):
database='postgres',
loop=self.loop,
ssl=True)

async def test_ssl_connection_pool(self):
ssl_context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
ssl_context.load_verify_locations(SSL_CA_CERT_FILE)

pool = await self.create_pool(
host='localhost',
user='ssl_user',
database='postgres',
min_size=5,
max_size=10,
ssl=ssl_context)

async def worker():
async with pool.acquire() as con:
self.assertEqual(await con.fetchval('SELECT 42'), 42)

with self.assertRaises(asyncio.TimeoutError):
await con.execute('SELECT pg_sleep(5)', timeout=0.5)

self.assertEqual(await con.fetchval('SELECT 43'), 43)

tasks = [worker() for _ in range(100)]
await asyncio.gather(*tasks, loop=self.loop)
await pool.close()