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

Conversation

stephan48
Copy link

@stephan48 stephan48 commented Feb 13, 2018

What do these changes do?

I faced the problem that with the aiohttp 3.0 ssl api changes i could no longer use fingerprint verification and client cert auth. As you could only specify one or the other. After a short talk with the developer we came to the conclusion that a way to solve the problem would be to add a ssl_context kwargs to the Fingerprint. This PR does exactly that.

Documentation is still missing.

Are there changes in behavior for the user?

As with the previous API aiohttp <=2 you can now use both methods again.

Related issue number

Nope

Checklist

  • will work on this tomorrow - its already late -

  • I think the code is well written

  • Unit tests for the changes exist

  • Documentation reflects the changes

  • If you provide code modification, please add yourself to CONTRIBUTORS.txt

    • The format is <Name> <Surname>.
    • Please keep alphabetical order, the file is sorted by names.
  • Add a new news fragment into the CHANGES folder

    • name it <issue_id>.<type> for example (588.bugfix)
    • if you don't have an issue_id change it to the pr id after creating the pr
    • ensure type is one of the following:
      • .feature: Signifying a new feature.
      • .bugfix: Signifying a bug fix.
      • .doc: Signifying a documentation improvement.
      • .removal: Signifying a deprecation or removal of public API.
      • .misc: A ticket has been closed, but it is not of interest to users.
    • Make sure to use full sentences with correct case and punctuation, for example: "Fix issue with non-ascii contents in doctest text files."

…verride the automatically created ssl_context
@@ -771,12 +778,18 @@ def _get_ssl_context(self, req):
sslcontext = req.ssl
if isinstance(sslcontext, ssl.SSLContext):
return sslcontext
if not (not isinstance(sslcontext, Fingerprint) or not (sslcontext.ssl_context is not None) or not isinstance(
Copy link
Member

Choose a reason for hiding this comment

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

There's too many negations, which is hard to understand instantly and easy to misunderstand. I think, this can (and should) be simplified.

Copy link
Member

Choose a reason for hiding this comment

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

It seems to me that you want this check:

isinstance(sslcontext, Fingerprint) and isinstance(sslcontext.ssl_context, ssl.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 not (not isinstance(sslcontext, Fingerprint) or not (sslcontext.ssl_context is not None) or not isinstance(
Copy link
Member

Choose a reason for hiding this comment

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

This is a duplicate check. Consider code reuse, so that it won't result in diverse copy-paste in future.

@stephan48
Copy link
Author

Hi, thank you for your comments I used some function in PyStorm to break the line - this apparently lead to it rewriting the expression without me checking. I reworked it - i would like to keep the "is not None" check as a precondition. I fixed the negation mess throu.

But I see that this check is not done in the other branches - is isinstance "None"-safe?

@webknjaz
Copy link
Member

@stephan48 this is why I use Vim :)

isinstance is "None-safe": None is instance of the NoneType

@stephan48
Copy link
Author

stephan48 commented Feb 13, 2018

yea :) for work stuff i tend to use windows & a IDE. for everything else vim on a ssh connection.

thanks for that info.

how about this:

            if ssl is None:  # pragma: no cover
                raise RuntimeError('SSL is not supported.')

            def check_ssl(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
                if ssl_ctx is not None:
                    # not verified or fingerprinted
                    return self._make_ssl_context(False)

            if req.ssl is not None:
                return check_ssl(req.ssl)

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

            return self._make_ssl_context(True)

@webknjaz
Copy link
Member

Your last if/return in check_ssl() is always true (I mean, if not needed there at all). But if looks unreliable to me in current form.

@webknjaz
Copy link
Member

Oh, and please update code in branch, cause it's fairly complicated to comment on such snippet + it's ripped out of context, which makes it hard to imagine how it would play with other parts of code around it. TIA.

@asvetlov
Copy link
Member

ping

@stephan48
Copy link
Author

Hi,

sadly I got overwhelmed by private and work stuff and i totally forgot to check back in here :(

Would the changes/the version in the last commit(pre merge) be adequate?

-- stephan

@codecov-io
Copy link

codecov-io commented Apr 30, 2018

Codecov Report

Merging #2735 into master will decrease coverage by 0.02%.
The diff coverage is 85.71%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #2735      +/-   ##
==========================================
- Coverage   97.98%   97.95%   -0.03%     
==========================================
  Files          40       40              
  Lines        7592     7595       +3     
  Branches     1323     1323              
==========================================
+ Hits         7439     7440       +1     
- Misses         51       52       +1     
- Partials      102      103       +1
Impacted Files Coverage Δ
aiohttp/client_reqrep.py 97.36% <100%> (+0.01%) ⬆️
aiohttp/connector.py 96.5% <80%> (-0.37%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update c4cbc61...abfd287. Read the comment docs.

@stephan48
Copy link
Author

Do I assume correctly, that the code coverage complains about the new check_ssl function being undocumented?

Copy link
Member

@asvetlov asvetlov left a comment

Choose a reason for hiding this comment

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

The design looks good, please fix a couple notes

def check_ssl(ssl_ctx):
if isinstance(ssl_ctx, ssl.SSLContext):
return ssl_ctx
if isinstance(ssl_ctx, Fingerprint) and \
Copy link
Member

Choose a reason for hiding this comment

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

Please use brackets:

    if (isinstance(ssl_ctx, Fingerprint) and
            isinstance(ssl_ctx.ssl_context, ssl.SSLContext)):
        return ssl_ctx.ssl_context

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.

The line is not covered by tests

return sslcontext
if sslcontext is not None:

def check_ssl(ssl_ctx):
Copy link
Member

Choose a reason for hiding this comment

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

The function has no closures, please rename it into _check_ssl and move to the module's top-level namespace

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)

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)

@CLAassistant

This comment has been minimized.

@asvetlov
Copy link
Member

I like the idea but sorry, I should close the PR.
It has no tests and documentation updates.
Please feel free to open a new PR that has all the required fixes.

@asvetlov asvetlov closed this Nov 21, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants