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
try:
url = f"{proto}://{self.ip_address}:{port}{s_suffix}"
async with self.sys_websession_ssl.get(
url, timeout=WATCHDOG_TIMEOUT
async with self.sys_websession.get(
url, timeout=WATCHDOG_TIMEOUT, ssl=False
) as req:
if req.status < 300:
return True

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -1,5 +1,4 @@
"""Common test functions."""
import asyncio
from pathlib import Path
import re
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
await asyncio.gather(
coresys_obj.websession.close(), coresys_obj.websession_ssl.close()
)
await coresys_obj.websession.close()
@pytest.fixture