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

Support a servername parameter on HTTPSConnections #1397

Merged

Conversation

JackOfMostTrades
Copy link
Contributor

The servername parameter allows callers to override the hostname used for SNI. If specified this hostname will also be used as the value for hostname verification instead of the actual hostname (unless assert_hostname is set).

This is the same option and semantics that node.js provides via the "servername" attribute, or that golang provides via the "ServerName" option.

@@ -242,14 +242,15 @@ class HTTPSConnection(HTTPConnection):

def __init__(self, host, port=None, key_file=None, cert_file=None,
strict=None, timeout=socket._GLOBAL_DEFAULT_TIMEOUT,
ssl_context=None, **kw):
ssl_context=None, servername=None, **kw):
Copy link
Member

Choose a reason for hiding this comment

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

Can we use the same argument name as ssl_wrap_socket?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep, can do. Changed it to server_hostname everywhere.

@codecov-io
Copy link

codecov-io commented Jun 15, 2018

Codecov Report

Merging #1397 into master will not change coverage.
The diff coverage is 100%.

Impacted file tree graph

@@          Coverage Diff           @@
##           master   #1397   +/-   ##
======================================
  Coverage     100%    100%           
======================================
  Files          21      21           
  Lines        1788    1794    +6     
======================================
+ Hits         1788    1794    +6
Impacted Files Coverage Δ
urllib3/connection.py 100% <100%> (ø) ⬆️
urllib3/util/wait.py 100% <0%> (ø) ⬆️

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 6ef3c06...1957cf7. Read the comment docs.

Copy link
Member

@theacodes theacodes left a comment

Choose a reason for hiding this comment

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

This looks pretty good, can you add an entry in the changelog for this?

@theacodes
Copy link
Member

Also we'll need to make sure coverage stays at 100%.

@JackOfMostTrades
Copy link
Contributor Author

Sure thing; I've updated the PR with a changelog update and added a test which asserts that server_hostname gets passed through to the wrapped socket and gets the code coverage back up to 100%.

@JackOfMostTrades
Copy link
Contributor Author

And by the way, the docs in this project for helping a new developer get tests running are fantastic. That's always been such a struggle with other projects and it was incredibly easy this time around. I appreciate the time spent writing that and keeping it up to date! :)

@theacodes
Copy link
Member

And by the way, the docs in this project for helping a new developer get tests running are fantastic. That's always been such a struggle with other projects and it was incredibly easy this time around. I appreciate the time spent writing that and keeping it up to date! :)

That's great to hear!

@sethmlarson
Copy link
Member

Thanks for submitting this patch. Can you help me understand the use-case for this parameter? I'm unsure of a situation where you'd want to override the hostname sent via SNI compared to the one you're connecting to.

@JackOfMostTrades
Copy link
Contributor Author

Can you help me understand the use-case for this parameter?

Sure. This doesn't really apply on the public internet, but it's something I've run into quite a bit in internal infrastructure. The most common case where I've run into this where you're connecting by IP instead of hostname (but you still want a particular virtual host served back). There's a few reasons you might be connecting by IP:

  • You're not using DNS for host resolution; e.g. Netflix uses Eureka which has built-in load-balancing and failover logic.
  • You're on an internal network and the DNS name would resolve to a public IP address which doesn't route correctly from the internal network.
  • You're running integration tests, e.g. against localhost. The service might not actually get added to the DNS hostname until after the integration tests have run and verified that the virtualhost configuration is working as expected.

@sethmlarson
Copy link
Member

Thanks for explaining it! Sounds good to me. 👍

@sethmlarson
Copy link
Member

Looks like a merge conflict that needs resolving for urllib3/connection.py. Could you address this and resubmit? :)

@pquentin
Copy link
Member

urllib3/connection.py moved to src/urllib3/connection.py, so that should not be too hard to fix. :) The good news is that there is a small chance that a rebase will also fix the test_client_no_intermediate error on Windows.

@JackOfMostTrades
Copy link
Contributor Author

Rebased. The windows test cleared up, although now a random macOS run of test_client_no_intermediate failed.

@sethmlarson
Copy link
Member

Uh oh, @pquentin?

@pquentin
Copy link
Member

@SethMichaelLarson Yes, this confirms that the serial number length issue is only a macOS 10.13+ issue or at least was not the only problem to fix. That at least makes sense, but unfortunately that also means that the macOS 10.12 flakiness on CI is not solved yet. At least now on every failing build we can see 1/ the actual exception 2/ where it got thrown.

@pquentin
Copy link
Member

(However, the problem is unrelated to this pull request.)

@sethmlarson sethmlarson merged commit 3f2267a into urllib3:master Jul 12, 2018
@sethmlarson
Copy link
Member

Merged! Thanks @JackOfMostTrades! 🎉

@JackOfMostTrades
Copy link
Contributor Author

Yay! Thanks for everyone's time getting this cleaned up and merged in. I appreciate it!

Dobatymo pushed a commit to Dobatymo/urllib3 that referenced this pull request Mar 16, 2022
@multani multani mentioned this pull request Aug 19, 2023
5 tasks
multani added a commit to multani/aiohttp that referenced this pull request Aug 19, 2023
This add the missing support to set the `server_hostname` setting when
creating TCP connection, when the underlying connection is authenticated
using TLS.

See the documentation for the 2 stdlib functions:

* https://docs.python.org/3/library/asyncio-eventloop.html#asyncio.loop.create_connection
* https://docs.python.org/3/library/asyncio-eventloop.html#opening-network-connections

The implemention is similar to what was done in urllib3 in urllib3/urllib3#1397

This would be needed to support features in clients using aiohttp, such as tomplus/kubernetes_asyncio#267

Closes: aio-libs#7114
multani added a commit to multani/aiohttp that referenced this pull request Aug 19, 2023
This add the missing support to set the `server_hostname` setting when
creating TCP connection, when the underlying connection is authenticated
using TLS.

See the documentation for the 2 stdlib functions:

* https://docs.python.org/3/library/asyncio-eventloop.html#asyncio.loop.create_connection
* https://docs.python.org/3/library/asyncio-eventloop.html#opening-network-connections

The implemention is similar to what was done in urllib3 in urllib3/urllib3#1397

This would be needed to support features in clients using aiohttp, such as tomplus/kubernetes_asyncio#267

Closes: aio-libs#7114
multani added a commit to multani/aiohttp that referenced this pull request Aug 20, 2023
This add the missing support to set the `server_hostname` setting when
creating TCP connection, when the underlying connection is authenticated
using TLS.

See the documentation for the 2 stdlib functions:

* https://docs.python.org/3/library/asyncio-eventloop.html#asyncio.loop.create_connection
* https://docs.python.org/3/library/asyncio-eventloop.html#opening-network-connections

The implemention is similar to what was done in urllib3 in urllib3/urllib3#1397

This would be needed to support features in clients using aiohttp, such as tomplus/kubernetes_asyncio#267

Closes: aio-libs#7114
Dreamsorcerer added a commit to aio-libs/aiohttp that referenced this pull request Aug 20, 2023
…7541)

## What do these changes do?

This adds the missing support to set the `server_hostname` setting when
creating TCP connection, when the underlying connection is authenticated
using TLS.

See the documentation for the 2 stdlib functions:

*
https://docs.python.org/3/library/asyncio-eventloop.html#asyncio.loop.create_connection
*
https://docs.python.org/3/library/asyncio-eventloop.html#opening-network-connections


This would be needed to support features in clients using aiohttp, such
as tomplus/kubernetes_asyncio#267

## Are there changes in behavior for the user?

The default behavior should not change, but this would allow on a
per-connection basis to specify a custom server name to check the
certificate name against.

## Related issue number

Closes: #7114

(for reference, similar implementation in urllib3:
urllib3/urllib3#1397)

## Checklist

- [x] I think the code is well written
- [x] Unit tests for the changes exist
- [x] Documentation reflects the changes
- [x] If you provide code modification, please add yourself to
`CONTRIBUTORS.txt`
  * The format is &lt;Name&gt; &lt;Surname&gt;.
  * Please keep alphabetical order, the file is sorted by names.
- [x] 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."

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Sam Bull <aa6bs0@sambull.org>
multani added a commit to multani/aiohttp that referenced this pull request Aug 20, 2023
…io-libs#7541)

This adds the missing support to set the `server_hostname` setting when
creating TCP connection, when the underlying connection is authenticated
using TLS.

See the documentation for the 2 stdlib functions:

*
https://docs.python.org/3/library/asyncio-eventloop.html#asyncio.loop.create_connection
*
https://docs.python.org/3/library/asyncio-eventloop.html#opening-network-connections

This would be needed to support features in clients using aiohttp, such
as tomplus/kubernetes_asyncio#267

The default behavior should not change, but this would allow on a
per-connection basis to specify a custom server name to check the
certificate name against.

Closes: aio-libs#7114

(for reference, similar implementation in urllib3:
urllib3/urllib3#1397)

- [x] I think the code is well written
- [x] Unit tests for the changes exist
- [x] Documentation reflects the changes
- [x] If you provide code modification, please add yourself to
`CONTRIBUTORS.txt`
  * The format is &lt;Name&gt; &lt;Surname&gt;.
  * Please keep alphabetical order, the file is sorted by names.
- [x] 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."

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Sam Bull <aa6bs0@sambull.org>
(cherry picked from commit ac29dea)
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