Cleanup websession with new aiohttp ssl (#2861)

This commit is contained in:
Pascal Vizeli 2021-05-14 10:07:00 +02:00 committed by GitHub
parent 1ef46424ea
commit a5ed68b641
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 10 additions and 27 deletions

View File

@ -477,8 +477,8 @@ class Addon(AddonModel):
# Make HTTP request # Make HTTP request
try: try:
url = f"{proto}://{self.ip_address}:{port}{s_suffix}" url = f"{proto}://{self.ip_address}:{port}{s_suffix}"
async with self.sys_websession_ssl.get( async with self.sys_websession.get(
url, timeout=WATCHDOG_TIMEOUT url, timeout=WATCHDOG_TIMEOUT, ssl=False
) as req: ) as req:
if req.status < 300: if req.status < 300:
return True return True

View File

@ -112,9 +112,7 @@ class APIProxy(CoreSysAttributes):
url = f"{self.sys_homeassistant.api_url}/api/websocket" url = f"{self.sys_homeassistant.api_url}/api/websocket"
try: try:
client = await self.sys_websession_ssl.ws_connect( client = await self.sys_websession.ws_connect(url, heartbeat=30, ssl=False)
url, heartbeat=30, verify_ssl=False
)
# Handle authentication # Handle authentication
data = await client.receive_json() data = await client.receive_json()

View File

@ -272,7 +272,6 @@ class Core(CoreSysAttributes):
await asyncio.wait( await asyncio.wait(
[ [
self.sys_websession.close(), self.sys_websession.close(),
self.sys_websession_ssl.close(),
self.sys_ingress.unload(), self.sys_ingress.unload(),
self.sys_hardware.unload(), self.sys_hardware.unload(),
] ]

View File

@ -56,9 +56,6 @@ class CoreSys:
# External objects # External objects
self._loop: asyncio.BaseEventLoop = asyncio.get_running_loop() self._loop: asyncio.BaseEventLoop = asyncio.get_running_loop()
self._websession: aiohttp.ClientSession = aiohttp.ClientSession() self._websession: aiohttp.ClientSession = aiohttp.ClientSession()
self._websession_ssl: aiohttp.ClientSession = aiohttp.ClientSession(
connector=aiohttp.TCPConnector(ssl=False)
)
# Global objects # Global objects
self._config: CoreConfig = CoreConfig() self._config: CoreConfig = CoreConfig()
@ -104,11 +101,6 @@ class CoreSys:
"""Return websession object.""" """Return websession object."""
return self._websession return self._websession
@property
def websession_ssl(self) -> aiohttp.ClientSession:
"""Return websession object with disabled SSL."""
return self._websession_ssl
@property @property
def config(self) -> CoreConfig: def config(self) -> CoreConfig:
"""Return CoreConfig object.""" """Return CoreConfig object."""
@ -491,11 +483,6 @@ class CoreSysAttributes:
"""Return websession object.""" """Return websession object."""
return self.coresys.websession return self.coresys.websession
@property
def sys_websession_ssl(self) -> aiohttp.ClientSession:
"""Return websession object with disabled SSL."""
return self.coresys.websession_ssl
@property @property
def sys_config(self) -> CoreConfig: def sys_config(self) -> CoreConfig:
"""Return CoreConfig object.""" """Return CoreConfig object."""

View File

@ -36,13 +36,14 @@ class HomeAssistantAPI(CoreSysAttributes):
return return
with suppress(asyncio.TimeoutError, aiohttp.ClientError): with suppress(asyncio.TimeoutError, aiohttp.ClientError):
async with self.sys_websession_ssl.post( async with self.sys_websession.post(
f"{self.sys_homeassistant.api_url}/auth/token", f"{self.sys_homeassistant.api_url}/auth/token",
timeout=30, timeout=30,
data={ data={
"grant_type": "refresh_token", "grant_type": "refresh_token",
"refresh_token": self.sys_homeassistant.refresh_token, "refresh_token": self.sys_homeassistant.refresh_token,
}, },
ssl=False,
) as resp: ) as resp:
if resp.status != 200: if resp.status != 200:
_LOGGER.error("Can't update Home Assistant access token!") _LOGGER.error("Can't update Home Assistant access token!")
@ -80,13 +81,14 @@ class HomeAssistantAPI(CoreSysAttributes):
headers[hdrs.AUTHORIZATION] = f"Bearer {self.access_token}" headers[hdrs.AUTHORIZATION] = f"Bearer {self.access_token}"
try: try:
async with getattr(self.sys_websession_ssl, method)( async with getattr(self.sys_websession, method)(
url, url,
data=data, data=data,
timeout=timeout, timeout=timeout,
json=json, json=json,
headers=headers, headers=headers,
params=params, params=params,
ssl=False,
) as resp: ) as resp:
# Access token expired # Access token expired
if resp.status == 401: if resp.status == 401:

View File

@ -60,7 +60,7 @@ class WSClient:
) -> "WSClient": ) -> "WSClient":
"""Create an authenticated websocket client.""" """Create an authenticated websocket client."""
try: try:
client = await session.ws_connect(url) client = await session.ws_connect(url, ssl=False)
except aiohttp.client_exceptions.ClientConnectorError: except aiohttp.client_exceptions.ClientConnectorError:
raise HomeAssistantWSError("Can't connect") from None raise HomeAssistantWSError("Can't connect") from None
@ -96,7 +96,7 @@ class HomeAssistantWebSocket(CoreSysAttributes):
await self.sys_homeassistant.api.ensure_access_token() await self.sys_homeassistant.api.ensure_access_token()
client = await WSClient.connect_with_auth( client = await WSClient.connect_with_auth(
self.sys_websession_ssl, self.sys_websession,
f"{self.sys_homeassistant.api_url}/api/websocket", f"{self.sys_homeassistant.api_url}/api/websocket",
self.sys_homeassistant.api.access_token, self.sys_homeassistant.api.access_token,
) )

View File

@ -1,5 +1,4 @@
"""Common test functions.""" """Common test functions."""
import asyncio
from pathlib import Path from pathlib import Path
import re import re
from unittest.mock import AsyncMock, MagicMock, PropertyMock, patch from unittest.mock import AsyncMock, MagicMock, PropertyMock, patch
@ -164,9 +163,7 @@ async def coresys(loop, docker, network_manager, aiohttp_client) -> CoreSys:
yield coresys_obj yield coresys_obj
await asyncio.gather( await coresys_obj.websession.close()
coresys_obj.websession.close(), coresys_obj.websession_ssl.close()
)
@pytest.fixture @pytest.fixture