-
-
Notifications
You must be signed in to change notification settings - Fork 37.7k
Add custom ssl_context to aiohttp helper #84573
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -77,7 +77,7 @@ def async_get_clientsession( | |
| @bind_hass | ||
| def async_create_clientsession( | ||
| hass: HomeAssistant, | ||
| verify_ssl: bool = True, | ||
| ssl_context: bool | SSLContext = True | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a breaking change. Maybe keep the old parameter while adding the new parameter next to the old?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That is a good idea, schould I log a warning when the old parameter is used that it will be deprecated?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we want to deprecate the old parameter, yes. I'm not sure if we want that. Let's hear what others think first.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree with Martin. We should keep both parameters here. See my comment above as well. |
||
| auto_cleanup: bool = True, | ||
| **kwargs: Any, | ||
| ) -> aiohttp.ClientSession: | ||
|
|
@@ -96,7 +96,7 @@ def async_create_clientsession( | |
|
|
||
| clientsession = _async_create_clientsession( | ||
| hass, | ||
| verify_ssl, | ||
| ssl_context, | ||
| auto_cleanup_method=auto_cleanup_method, | ||
| **kwargs, | ||
| ) | ||
|
|
@@ -107,14 +107,14 @@ def async_create_clientsession( | |
| @callback | ||
| def _async_create_clientsession( | ||
| hass: HomeAssistant, | ||
| verify_ssl: bool = True, | ||
| ssl_context: bool | SSLContext = True | ||
| auto_cleanup_method: Callable[[HomeAssistant, aiohttp.ClientSession], None] | ||
| | None = None, | ||
| **kwargs: Any, | ||
| ) -> aiohttp.ClientSession: | ||
| """Create a new ClientSession with kwargs, i.e. for cookies.""" | ||
| clientsession = aiohttp.ClientSession( | ||
| connector=_async_get_connector(hass, verify_ssl), | ||
| connector=_async_get_connector(hass, ssl_context), | ||
| json_serialize=json_dumps, | ||
| response_class=HassClientResponse, | ||
| **kwargs, | ||
|
|
@@ -239,24 +239,25 @@ def _async_close_websession(event: Event) -> None: | |
|
|
||
| @callback | ||
| def _async_get_connector( | ||
| hass: HomeAssistant, verify_ssl: bool = True | ||
| hass: HomeAssistant, ssl_context: bool | SSLContext = True | ||
| ) -> aiohttp.BaseConnector: | ||
| """Return the connector pool for aiohttp. | ||
|
|
||
| This method must be run in the event loop. | ||
| """ | ||
| key = DATA_CONNECTOR if verify_ssl else DATA_CONNECTOR_NOTVERIFY | ||
| key = None | ||
| if isinstance(ssl_context, bool): | ||
| key = DATA_CONNECTOR if verify_ssl else DATA_CONNECTOR_NOTVERIFY | ||
|
|
||
| if key in hass.data: | ||
| return cast(aiohttp.BaseConnector, hass.data[key]) | ||
| if key in hass.data: | ||
| return cast(aiohttp.BaseConnector, hass.data[key]) | ||
|
|
||
| if verify_ssl: | ||
| if ssl_context is True: | ||
| ssl_context: bool | SSLContext = ssl_util.client_context() | ||
| else: | ||
| ssl_context = False | ||
|
|
||
| connector = aiohttp.TCPConnector(enable_cleanup_closed=True, ssl=ssl_context) | ||
| hass.data[key] = connector | ||
| if key is not None: | ||
| hass.data[key] = connector | ||
|
|
||
| async def _async_close_connector(event: Event) -> None: | ||
| """Close connector pool.""" | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The behavior here is a bit unexpected. If ssl_context is false it seems to imply a non secure connection