Skip to content
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

add a ssl_context option to fingerprint - to allow the developer to override the automatically created ssl_context #2735

Closed
Closed
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion aiohttp/client_reqrep.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class Fingerprint:
32: sha256,
}

def __init__(self, fingerprint):
def __init__(self, fingerprint, ssl_context=None):
digestlen = len(fingerprint)
hashfunc = self.HASHFUNC_BY_DIGESTLEN.get(digestlen)
if not hashfunc:
Expand All @@ -78,11 +78,16 @@ def __init__(self, fingerprint):
'not supported. Use sha256.')
self._hashfunc = hashfunc
self._fingerprint = fingerprint
self._ssl_context = ssl_context

@property
def fingerprint(self):
return self._fingerprint

@property
def ssl_context(self):
return self._ssl_context

def check(self, transport):
if not transport.get_extra_info('sslcontext'):
return
Expand Down
33 changes: 19 additions & 14 deletions aiohttp/connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -764,13 +764,23 @@ def _make_ssl_context(verified):
sslcontext.set_default_verify_paths()
return sslcontext

def _check_ssl(self, ssl_ctx):
if isinstance(ssl_ctx, ssl.SSLContext):
return ssl_ctx
if (isinstance(ssl_ctx, Fingerprint) and
isinstance(ssl_ctx.ssl_context, ssl.SSLContext)):
return ssl_ctx.ssl_context
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a test for covering the line execution.


# not verified or fingerprinted
return self._make_ssl_context(False)

def _get_ssl_context(self, req):
"""Logic to get the correct SSL context

0. if req.ssl is false, return None
0. if req.is_ssl() is false, return None

1. if ssl_context is specified in req, use it
2. if _ssl_context is specified in self, use it
2. if _ssl_context is specified in self, use and check it
3. otherwise:
1. if verify_ssl is not specified in req, use self.ssl_context
(will generate a default context according to self.verify_ssl)
Expand All @@ -781,18 +791,13 @@ def _get_ssl_context(self, req):
if req.is_ssl():
if ssl is None: # pragma: no cover
raise RuntimeError('SSL is not supported.')
sslcontext = req.ssl
if isinstance(sslcontext, ssl.SSLContext):
return sslcontext
if sslcontext is not None:
# not verified or fingerprinted
return self._make_ssl_context(False)
sslcontext = self._ssl
if isinstance(sslcontext, ssl.SSLContext):
return sslcontext
if sslcontext is not None:
# not verified or fingerprinted
return self._make_ssl_context(False)

if req.ssl is not None:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

smth like this would look nicer that duplicate if-clauses:

_ssl = req.ssl or self._ssl
if _ssl is not None:
    return self._check_ssl(_ssl)

return self._check_ssl(req.ssl)

if self._ssl is not None:
return self._check_ssl(self._ssl)

return self._make_ssl_context(True)
else:
return None
Expand Down