Skip to content

Commit

Permalink
Try fixing windows CI
Browse files Browse the repository at this point in the history
  • Loading branch information
1st1 committed Nov 20, 2019
1 parent 4027794 commit 66c2689
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 7 deletions.
6 changes: 0 additions & 6 deletions .ci/appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,7 @@ environment:
secure: XudOvV6WtY9yRoqKahXMswFth8SF1UTnSXws4UBjeqzQUjOx2V2VRvIdpPfiqUKt

matrix:
- PYTHON: "C:\\Python35\\python.exe"
- PYTHON: "C:\\Python35-x64\\python.exe"
- PYTHON: "C:\\Python36\\python.exe"
- PYTHON: "C:\\Python36-x64\\python.exe"
- PYTHON: "C:\\Python37\\python.exe"
- PYTHON: "C:\\Python37-x64\\python.exe"
- PYTHON: "C:\\Python38\\python.exe"
- PYTHON: "C:\\Python38-x64\\python.exe"

branches:
Expand Down
71 changes: 70 additions & 1 deletion asyncpg/connect_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,75 @@ def _parse_connect_arguments(*, dsn, host, port, user, password, passfile,
return addrs, params, config


async def _create_ssl_connection2(protocol_factory, host, port, *,
loop, ssl_context, ssl_is_advisory=False):

class TLSUpgradeProto(asyncio.Protocol):
def __init__(self):
self.on_data = loop.create_future()

def data_received(self, data):
if data == b'S':
self.on_data.set_result(True)
elif (ssl_is_advisory and
ssl_context.verify_mode == ssl_module.CERT_NONE and
data == b'N'):
# ssl_is_advisory will imply that ssl.verify_mode == CERT_NONE,
# since the only way to get ssl_is_advisory is from
# sslmode=prefer (or sslmode=allow). But be extra sure to
# disallow insecure connections when the ssl context asks for
# real security.
self.on_data.set_result(False)
else:
self.on_data.set_exception(
ConnectionError(
'PostgreSQL server at "{}:{}" '
'rejected SSL upgrade'.format(host, port)))

def connection_lost(self, exc):
if not self.on_data.done():
if exc is None:
exc = ConnectionError('unexpected connection_lost() call')
self.on_data.set_exception(exc)

if hasattr(loop, 'start_tls'):
tr, pr = await loop.create_connection(TLSUpgradeProto, host, port)
tr.write(struct.pack('!ll', 8, 80877103)) # SSLRequest message.

try:
ssl_upgrade = await pr.on_data
except (Exception, asyncio.CancelledError):
tr.close()
raise

if ssl_upgrade:
if ssl_context is True:
ssl_context = ssl_module.create_default_context()

try:
new_tr = await loop.start_tls(
tr, pr, ssl_context, server_hostname=host)
except (Exception, asyncio.CancelledError):
tr.close()
raise
else:
new_tr = tr

pg_proto = protocol_factory()
pg_proto.connection_made(new_tr)
new_tr.set_protocol(pg_proto)

return new_tr, pg_proto
else:
return await _negotiate_ssl_connection(
host, port,
functools.partial(loop.create_connection, protocol_factory),
loop=loop,
ssl=ssl_context,
server_hostname=host,
ssl_is_advisory=ssl_is_advisory)


async def _connect_addr(*, addr, loop, timeout, params, config,
connection_class):
assert loop is not None
Expand All @@ -520,7 +589,7 @@ async def _connect_addr(*, addr, loop, timeout, params, config,
assert not params.ssl
connector = loop.create_unix_connection(proto_factory, addr)
elif params.ssl:
connector = _create_ssl_connection(
connector = _create_ssl_connection2(
proto_factory, *addr, loop=loop, ssl_context=params.ssl,
ssl_is_advisory=params.ssl_is_advisory)
else:
Expand Down

0 comments on commit 66c2689

Please sign in to comment.