From eac7c5f177bf5702fb42a4eab49de348c9e32f40 Mon Sep 17 00:00:00 2001 From: Pascal Vizeli Date: Tue, 21 Jun 2022 17:11:20 +0200 Subject: [PATCH] Remove deprecated X-Hassio-Key usage (#73783) * Remove deprecated X-Hassio-Key usage * ... * Update const.py * Update ingress.py * Update test_ingress.py Co-authored-by: Ludeeus --- homeassistant/components/hassio/const.py | 3 +-- homeassistant/components/hassio/handler.py | 6 +++--- homeassistant/components/hassio/http.py | 14 ++++++++------ homeassistant/components/hassio/ingress.py | 4 ++-- tests/components/hassio/test_ingress.py | 16 +++++++++------- tests/components/hassio/test_init.py | 2 +- 6 files changed, 24 insertions(+), 21 deletions(-) diff --git a/homeassistant/components/hassio/const.py b/homeassistant/components/hassio/const.py index 2d99b1f5605..e4991e5fc03 100644 --- a/homeassistant/components/hassio/const.py +++ b/homeassistant/components/hassio/const.py @@ -25,8 +25,7 @@ ATTR_METHOD = "method" ATTR_RESULT = "result" ATTR_TIMEOUT = "timeout" - -X_HASSIO = "X-Hassio-Key" +X_AUTH_TOKEN = "X-Supervisor-Token" X_INGRESS_PATH = "X-Ingress-Path" X_HASS_USER_ID = "X-Hass-User-ID" X_HASS_IS_ADMIN = "X-Hass-Is-Admin" diff --git a/homeassistant/components/hassio/handler.py b/homeassistant/components/hassio/handler.py index ba1b3bfaf35..7b3ed697227 100644 --- a/homeassistant/components/hassio/handler.py +++ b/homeassistant/components/hassio/handler.py @@ -13,8 +13,6 @@ from homeassistant.components.http import ( ) from homeassistant.const import SERVER_PORT -from .const import X_HASSIO - _LOGGER = logging.getLogger(__name__) @@ -246,7 +244,9 @@ class HassIO: method, f"http://{self._ip}{command}", json=payload, - headers={X_HASSIO: os.environ.get("SUPERVISOR_TOKEN", "")}, + headers={ + aiohttp.hdrs.AUTHORIZATION: f"Bearer {os.environ.get('SUPERVISOR_TOKEN', '')}" + }, timeout=aiohttp.ClientTimeout(total=timeout), ) diff --git a/homeassistant/components/hassio/http.py b/homeassistant/components/hassio/http.py index 3f492114545..7d2e79956cc 100644 --- a/homeassistant/components/hassio/http.py +++ b/homeassistant/components/hassio/http.py @@ -11,6 +11,7 @@ import aiohttp from aiohttp import web from aiohttp.client import ClientTimeout from aiohttp.hdrs import ( + AUTHORIZATION, CACHE_CONTROL, CONTENT_ENCODING, CONTENT_LENGTH, @@ -18,11 +19,12 @@ from aiohttp.hdrs import ( TRANSFER_ENCODING, ) from aiohttp.web_exceptions import HTTPBadGateway +from multidict import istr from homeassistant.components.http import KEY_AUTHENTICATED, HomeAssistantView from homeassistant.components.onboarding import async_is_onboarded -from .const import X_HASS_IS_ADMIN, X_HASS_USER_ID, X_HASSIO +from .const import X_HASS_IS_ADMIN, X_HASS_USER_ID _LOGGER = logging.getLogger(__name__) @@ -89,7 +91,7 @@ class HassIOView(HomeAssistantView): if path == "backups/new/upload": # We need to reuse the full content type that includes the boundary headers[ - "Content-Type" + CONTENT_TYPE ] = request._stored_content_type # pylint: disable=protected-access try: @@ -123,17 +125,17 @@ class HassIOView(HomeAssistantView): raise HTTPBadGateway() -def _init_header(request: web.Request) -> dict[str, str]: +def _init_header(request: web.Request) -> dict[istr, str]: """Create initial header.""" headers = { - X_HASSIO: os.environ.get("SUPERVISOR_TOKEN", ""), + AUTHORIZATION: f"Bearer {os.environ.get('SUPERVISOR_TOKEN', '')}", CONTENT_TYPE: request.content_type, } # Add user data if request.get("hass_user") is not None: - headers[X_HASS_USER_ID] = request["hass_user"].id - headers[X_HASS_IS_ADMIN] = str(int(request["hass_user"].is_admin)) + headers[istr(X_HASS_USER_ID)] = request["hass_user"].id + headers[istr(X_HASS_IS_ADMIN)] = str(int(request["hass_user"].is_admin)) return headers diff --git a/homeassistant/components/hassio/ingress.py b/homeassistant/components/hassio/ingress.py index c8b56e6f1bb..6caa97b788f 100644 --- a/homeassistant/components/hassio/ingress.py +++ b/homeassistant/components/hassio/ingress.py @@ -15,7 +15,7 @@ from homeassistant.components.http import HomeAssistantView from homeassistant.core import HomeAssistant, callback from homeassistant.helpers.aiohttp_client import async_get_clientsession -from .const import X_HASSIO, X_INGRESS_PATH +from .const import X_AUTH_TOKEN, X_INGRESS_PATH _LOGGER = logging.getLogger(__name__) @@ -183,7 +183,7 @@ def _init_header(request: web.Request, token: str) -> CIMultiDict | dict[str, st headers[name] = value # Inject token / cleanup later on Supervisor - headers[X_HASSIO] = os.environ.get("SUPERVISOR_TOKEN", "") + headers[X_AUTH_TOKEN] = os.environ.get("SUPERVISOR_TOKEN", "") # Ingress information headers[X_INGRESS_PATH] = f"/api/hassio_ingress/{token}" diff --git a/tests/components/hassio/test_ingress.py b/tests/components/hassio/test_ingress.py index 60fea96d4ea..34016fa9052 100644 --- a/tests/components/hassio/test_ingress.py +++ b/tests/components/hassio/test_ingress.py @@ -5,6 +5,8 @@ from unittest.mock import MagicMock, patch from aiohttp.hdrs import X_FORWARDED_FOR, X_FORWARDED_HOST, X_FORWARDED_PROTO import pytest +from homeassistant.components.hassio.const import X_AUTH_TOKEN + @pytest.mark.parametrize( "build_type", @@ -35,7 +37,7 @@ async def test_ingress_request_get(hassio_client, build_type, aioclient_mock): # Check we forwarded command assert len(aioclient_mock.mock_calls) == 1 - assert aioclient_mock.mock_calls[-1][3]["X-Hassio-Key"] == "123456" + assert aioclient_mock.mock_calls[-1][3][X_AUTH_TOKEN] == "123456" assert ( aioclient_mock.mock_calls[-1][3]["X-Ingress-Path"] == f"/api/hassio_ingress/{build_type[0]}" @@ -75,7 +77,7 @@ async def test_ingress_request_post(hassio_client, build_type, aioclient_mock): # Check we forwarded command assert len(aioclient_mock.mock_calls) == 1 - assert aioclient_mock.mock_calls[-1][3]["X-Hassio-Key"] == "123456" + assert aioclient_mock.mock_calls[-1][3][X_AUTH_TOKEN] == "123456" assert ( aioclient_mock.mock_calls[-1][3]["X-Ingress-Path"] == f"/api/hassio_ingress/{build_type[0]}" @@ -115,7 +117,7 @@ async def test_ingress_request_put(hassio_client, build_type, aioclient_mock): # Check we forwarded command assert len(aioclient_mock.mock_calls) == 1 - assert aioclient_mock.mock_calls[-1][3]["X-Hassio-Key"] == "123456" + assert aioclient_mock.mock_calls[-1][3][X_AUTH_TOKEN] == "123456" assert ( aioclient_mock.mock_calls[-1][3]["X-Ingress-Path"] == f"/api/hassio_ingress/{build_type[0]}" @@ -155,7 +157,7 @@ async def test_ingress_request_delete(hassio_client, build_type, aioclient_mock) # Check we forwarded command assert len(aioclient_mock.mock_calls) == 1 - assert aioclient_mock.mock_calls[-1][3]["X-Hassio-Key"] == "123456" + assert aioclient_mock.mock_calls[-1][3][X_AUTH_TOKEN] == "123456" assert ( aioclient_mock.mock_calls[-1][3]["X-Ingress-Path"] == f"/api/hassio_ingress/{build_type[0]}" @@ -195,7 +197,7 @@ async def test_ingress_request_patch(hassio_client, build_type, aioclient_mock): # Check we forwarded command assert len(aioclient_mock.mock_calls) == 1 - assert aioclient_mock.mock_calls[-1][3]["X-Hassio-Key"] == "123456" + assert aioclient_mock.mock_calls[-1][3][X_AUTH_TOKEN] == "123456" assert ( aioclient_mock.mock_calls[-1][3]["X-Ingress-Path"] == f"/api/hassio_ingress/{build_type[0]}" @@ -235,7 +237,7 @@ async def test_ingress_request_options(hassio_client, build_type, aioclient_mock # Check we forwarded command assert len(aioclient_mock.mock_calls) == 1 - assert aioclient_mock.mock_calls[-1][3]["X-Hassio-Key"] == "123456" + assert aioclient_mock.mock_calls[-1][3][X_AUTH_TOKEN] == "123456" assert ( aioclient_mock.mock_calls[-1][3]["X-Ingress-Path"] == f"/api/hassio_ingress/{build_type[0]}" @@ -268,7 +270,7 @@ async def test_ingress_websocket(hassio_client, build_type, aioclient_mock): # Check we forwarded command assert len(aioclient_mock.mock_calls) == 1 - assert aioclient_mock.mock_calls[-1][3]["X-Hassio-Key"] == "123456" + assert aioclient_mock.mock_calls[-1][3][X_AUTH_TOKEN] == "123456" assert ( aioclient_mock.mock_calls[-1][3]["X-Ingress-Path"] == f"/api/hassio_ingress/{build_type[0]}" diff --git a/tests/components/hassio/test_init.py b/tests/components/hassio/test_init.py index 6ac3debe3d8..60fec517aa9 100644 --- a/tests/components/hassio/test_init.py +++ b/tests/components/hassio/test_init.py @@ -348,7 +348,7 @@ async def test_setup_hassio_no_additional_data(hass, aioclient_mock): assert result assert aioclient_mock.call_count == 15 - assert aioclient_mock.mock_calls[-1][3]["X-Hassio-Key"] == "123456" + assert aioclient_mock.mock_calls[-1][3]["Authorization"] == "Bearer 123456" async def test_fail_setup_without_environ_var(hass):