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

fix: ssl deprecation in home assistant #803

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from

Conversation

YogevBokobza
Copy link
Collaborator

@YogevBokobza YogevBokobza commented Sep 12, 2024

Description

Using previous code made this deprecation message in Home Assistant when calling this function:
aioswitcher/device/tools.py:202: DeprecationWarning: ssl.PROTOCOL_TLS is deprecated

Consulting with ChatGPT I got this answer:
The key change here is replacing:
ssl.SSLContext()
with:
ssl.create_default_context()

This change does the following:

  • It uses ssl.create_default_context(), which creates a secure default SSL context with modern settings.
  • It avoids the deprecated ssl.PROTOCOL_TLS by using the recommended default settings.
  • It provides better security by using up-to-date SSL/TLS protocols and cipher suites.

Related issue (if any): fixes #issue_number_goes_here

Checklist

  • I have followed this repository's contributing guidelines.
  • I will adhere to the project's code of conduct.

Additional information

Anything else?

@pull-request-size pull-request-size bot added the size: xs Pull request has less then 10 lines label Sep 12, 2024
@auto-me-bot auto-me-bot bot added the status: needs review Pull request needs a review label Sep 12, 2024
@TomerFi
Copy link
Owner

TomerFi commented Sep 13, 2024

Chat-GPT aside, the docs say:

Instances of SSLSocket must be created using the SSLContext.wrap_socket() method.
The helper function create_default_context() returns a new context with secure default settings.

So, create_default_context, as evident by its name and as explained in its docs, returns the default SSLContext, which, as explained in its docs: "If not specified, the default is PROTOCOL_TLS".

The error specified in this PR body is: "DeprecationWarning: ssl.PROTOCOL_TLS is deprecated".

So, I'm just wondering if we are sure this solves our issue.

@thecode
Copy link
Collaborator

thecode commented Sep 13, 2024

Can you show a log with this warning? I have multiple integrations using default SSL context and didn't see this warning yet. Anyhow lets wait to see how others are fixing it (keep the PR open for now).

@YogevBokobza
Copy link
Collaborator Author

Can you show a log with this warning? I have multiple integrations using default SSL context and didn't see this warning yet. Anyhow lets wait to see how others are fixing it (keep the PR open for now).

Sure here is the log:

tests/components/switcher_kis/test_config_flow.py::test_reauth_invalid_auth
/home/vscode/.local/ha-venv/lib/python3.12/site-packages/aioswitcher/device/tools.py:202: DeprecationWarning: ssl.SSLContext() without protocol argument is deprecated.
ssl_context = ssl.SSLContext()

tests/components/switcher_kis/test_config_flow.py::test_user_setup_found_token_device[mock_bridge0]
tests/components/switcher_kis/test_config_flow.py::test_user_setup_found_token_device[mock_bridge0]
tests/components/switcher_kis/test_config_flow.py::test_reauth_successful[user_input0]
tests/components/switcher_kis/test_config_flow.py::test_reauth_invalid_auth
/home/vscode/.local/ha-venv/lib/python3.12/site-packages/aioswitcher/device/tools.py:202: DeprecationWarning: ssl.PROTOCOL_TLS is deprecated
ssl_context = ssl.SSLContext()

-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html

@YogevBokobza
Copy link
Collaborator Author

So maybe it should be:
SSLContext(PROTOCOL_TLS_CLIENT) ?

@TomerFi
Copy link
Owner

TomerFi commented Sep 13, 2024

So maybe it should be: SSLContext(PROTOCOL_TLS_CLIENT) ?

This is what's recommended by the docs:

Deprecated since version 3.10: TLS clients and servers require different default settings for secure communication.
The generic TLS protocol constant is deprecated in favor of PROTOCOL_TLS_CLIENT and PROTOCOL_TLS_SERVER.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
size: xs Pull request has less then 10 lines status: needs review Pull request needs a review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants