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

Pass default SSLContext instances to Octoprint custom HTTP sessions #105351

Merged
merged 1 commit into from
Jan 2, 2024

Conversation

vexofp
Copy link
Contributor

@vexofp vexofp commented Dec 9, 2023

Proposed change

This PR restores the use of the default Home Assistant SSLContext instances in the Octoprint integration. In #90001, a switch was made to use custom HTTP sessions. These sessions are currently creating their own SSL contexts. Using different contexts can lead to inconsistent request failures, due to different SSL configurations being used for different requests (which is how I discovered this).

Currently, we pass ssl=None/False when creating the custom aiohttp.TCPConnector, which causes it to create its own context with or without verification, respectively. Instead, this PR passes in the appropriately configured default Home Assistant SSLContext instance from get_default_context() or get_default_no_verify_context(), respectively.

Type of change

  • Dependency upgrade
  • Bugfix (non-breaking change which fixes an issue)
  • New integration (thank you!)
  • New feature (which adds functionality to an existing integration)
  • Deprecation (breaking change to happen in the future)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Additional information

Checklist

  • The code change is tested and works locally.
  • Local tests pass. Your PR cannot be merged unless tests pass
  • There is no commented out code in this PR.
  • I have followed the development checklist
  • I have followed the perfect PR recommendations
  • The code has been formatted using Ruff (ruff format homeassistant tests)
  • Tests have been added to verify that the new code works.

If user exposed functionality or configuration variables are added/changed:

If the code communicates with devices, web services, or third-party tools:

  • The manifest file has all fields filled out correctly.
    Updated and included derived files by running: python3 -m script.hassfest.
  • New or updated dependencies have been added to requirements_all.txt.
    Updated by running python3 -m script.gen_requirements_all.
  • For the updated dependencies - a link to the changelog, or at minimum a diff between library versions is added to the PR description.
  • Untested files have been added to .coveragerc.

To help with the load of incoming pull requests:

@home-assistant
Copy link

home-assistant bot commented Dec 9, 2023

Hey there @rfleming71, mind taking a look at this pull request as it has been labeled with an integration (octoprint) you are listed as a code owner for? Thanks!

Code owner commands

Code owners of octoprint can trigger bot actions by commenting:

  • @home-assistant close Closes the pull request.
  • @home-assistant rename Awesome new title Renames the pull request.
  • @home-assistant reopen Reopen the pull request.
  • @home-assistant unassign octoprint Removes the current integration label and assignees on the pull request, add the integration domain after the command.
  • @home-assistant add-label needs-more-information Add a label (needs-more-information, problem in dependency, problem in custom component) to the pull request.
  • @home-assistant remove-label needs-more-information Remove a label (needs-more-information, problem in dependency, problem in custom component) on the pull request.

@vexofp
Copy link
Contributor Author

vexofp commented Dec 9, 2023

No test case currently, as I could not think of a clean way to write one for this. Happy to add if anyone has a good idea how to go about it.

@edenhaus
Copy link
Contributor

Using different contexts can lead to inconsistent request failures, due to different SSL configurations being used for different requests (which is how I discovered this).

Can you please explain this further? When are different requests using different SSL configurations?

@vexofp
Copy link
Contributor Author

vexofp commented Dec 31, 2023

Can you please explain this further? When are different requests using different SSL configurations?

Sure.

Since #90001, the Octoprint integration is creating its own aiohttp session. When doing so, it is passing ssl=False or None, which causes aiohttp to create a new SSLContext for that session, and configure it per its own defaults.

Most other parts of Home Assistant, such as the REST integration, use the shared "default" HTTP sessions (aiohttp_client.async_get_clientsession() or httpx_client.get_async_client()) which in turn used the shared SSLContext (get_default_context() and get_default_no_verify_context()).

An SSLContext controls the various TLS configuration options, such as allowed protocol options, cipher suites, trusted certificate authorities, and client authentication certificates. So any time you have two different contexts, with potentially two different configurations, it is possible that those differences could cause a connection using one to succeed while the other fails. When this happens inside a single application, it can be very confusing why some requests are succeeding and others are failing.

Concretely, the way that Home Assistant configures its contexts is similar to what aiohttp currently does, but not identical. (And this could diverge further at any point in time, since we have no control over their default configuration.) In particular, when setting which certificate authorities to trust, Home Assistant honors the REQUESTS_CA_BUNDLE environment variable, and falls back to using certifi. aiohttp, on the other hand, just uses the OS-default authorities.

Thus, the Octoprint integration may fail to make a TLS connection while the REST integration succeeds for the same server, or vice versa. This happens when the user has set REQUESTS_CA_BUNDLE with a bundle that trusts the server, but the OS-default does not. Or when the server's certificate is trusted by the OS-default list, but not certifi. Etc.

The best way to ensure consistent behavior is to try to use the same contexts everywhere.

Re-using contexts also has performance benefits. The context is the container for various caches such as TLS session identifiers/tickets, which enable faster and more efficient reconnections. And of course, slightly less memory usage.

Copy link
Member

@MartinHjelmare MartinHjelmare left a comment

Choose a reason for hiding this comment

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

Looks good!

Copy link
Contributor

@edenhaus edenhaus left a comment

Choose a reason for hiding this comment

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

Thanks @vexofp 👍

@edenhaus edenhaus merged commit ef26184 into home-assistant:dev Jan 2, 2024
23 checks passed
@github-actions github-actions bot locked and limited conversation to collaborators Jan 3, 2024
@bdraco bdraco added this to the 2024.1.6 milestone Jan 28, 2024
@bdraco bdraco mentioned this pull request Jan 28, 2024
20 tasks
@bdraco
Copy link
Member

bdraco commented Jan 28, 2024

tagged for 2024.1.6 because this fixes the mypy failure seen in #109028

@home-assistant home-assistant unlocked this conversation Jan 28, 2024
@github-actions github-actions bot locked and limited conversation to collaborators Jan 30, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Octoprint integration does not use default SSLContext instances
5 participants