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 <ludeeus@ludeeus.dev>
pull/73800/head
Pascal Vizeli 2022-06-21 17:11:20 +02:00 committed by GitHub
parent 27209574d2
commit eac7c5f177
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 24 additions and 21 deletions

View File

@ -25,8 +25,7 @@ ATTR_METHOD = "method"
ATTR_RESULT = "result" ATTR_RESULT = "result"
ATTR_TIMEOUT = "timeout" ATTR_TIMEOUT = "timeout"
X_AUTH_TOKEN = "X-Supervisor-Token"
X_HASSIO = "X-Hassio-Key"
X_INGRESS_PATH = "X-Ingress-Path" X_INGRESS_PATH = "X-Ingress-Path"
X_HASS_USER_ID = "X-Hass-User-ID" X_HASS_USER_ID = "X-Hass-User-ID"
X_HASS_IS_ADMIN = "X-Hass-Is-Admin" X_HASS_IS_ADMIN = "X-Hass-Is-Admin"

View File

@ -13,8 +13,6 @@ from homeassistant.components.http import (
) )
from homeassistant.const import SERVER_PORT from homeassistant.const import SERVER_PORT
from .const import X_HASSIO
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -246,7 +244,9 @@ class HassIO:
method, method,
f"http://{self._ip}{command}", f"http://{self._ip}{command}",
json=payload, 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), timeout=aiohttp.ClientTimeout(total=timeout),
) )

View File

@ -11,6 +11,7 @@ import aiohttp
from aiohttp import web from aiohttp import web
from aiohttp.client import ClientTimeout from aiohttp.client import ClientTimeout
from aiohttp.hdrs import ( from aiohttp.hdrs import (
AUTHORIZATION,
CACHE_CONTROL, CACHE_CONTROL,
CONTENT_ENCODING, CONTENT_ENCODING,
CONTENT_LENGTH, CONTENT_LENGTH,
@ -18,11 +19,12 @@ from aiohttp.hdrs import (
TRANSFER_ENCODING, TRANSFER_ENCODING,
) )
from aiohttp.web_exceptions import HTTPBadGateway from aiohttp.web_exceptions import HTTPBadGateway
from multidict import istr
from homeassistant.components.http import KEY_AUTHENTICATED, HomeAssistantView from homeassistant.components.http import KEY_AUTHENTICATED, HomeAssistantView
from homeassistant.components.onboarding import async_is_onboarded 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__) _LOGGER = logging.getLogger(__name__)
@ -89,7 +91,7 @@ class HassIOView(HomeAssistantView):
if path == "backups/new/upload": if path == "backups/new/upload":
# We need to reuse the full content type that includes the boundary # We need to reuse the full content type that includes the boundary
headers[ headers[
"Content-Type" CONTENT_TYPE
] = request._stored_content_type # pylint: disable=protected-access ] = request._stored_content_type # pylint: disable=protected-access
try: try:
@ -123,17 +125,17 @@ class HassIOView(HomeAssistantView):
raise HTTPBadGateway() raise HTTPBadGateway()
def _init_header(request: web.Request) -> dict[str, str]: def _init_header(request: web.Request) -> dict[istr, str]:
"""Create initial header.""" """Create initial header."""
headers = { headers = {
X_HASSIO: os.environ.get("SUPERVISOR_TOKEN", ""), AUTHORIZATION: f"Bearer {os.environ.get('SUPERVISOR_TOKEN', '')}",
CONTENT_TYPE: request.content_type, CONTENT_TYPE: request.content_type,
} }
# Add user data # Add user data
if request.get("hass_user") is not None: if request.get("hass_user") is not None:
headers[X_HASS_USER_ID] = request["hass_user"].id headers[istr(X_HASS_USER_ID)] = request["hass_user"].id
headers[X_HASS_IS_ADMIN] = str(int(request["hass_user"].is_admin)) headers[istr(X_HASS_IS_ADMIN)] = str(int(request["hass_user"].is_admin))
return headers return headers

View File

@ -15,7 +15,7 @@ from homeassistant.components.http import HomeAssistantView
from homeassistant.core import HomeAssistant, callback from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.aiohttp_client import async_get_clientsession 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__) _LOGGER = logging.getLogger(__name__)
@ -183,7 +183,7 @@ def _init_header(request: web.Request, token: str) -> CIMultiDict | dict[str, st
headers[name] = value headers[name] = value
# Inject token / cleanup later on Supervisor # 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 # Ingress information
headers[X_INGRESS_PATH] = f"/api/hassio_ingress/{token}" headers[X_INGRESS_PATH] = f"/api/hassio_ingress/{token}"

View File

@ -5,6 +5,8 @@ from unittest.mock import MagicMock, patch
from aiohttp.hdrs import X_FORWARDED_FOR, X_FORWARDED_HOST, X_FORWARDED_PROTO from aiohttp.hdrs import X_FORWARDED_FOR, X_FORWARDED_HOST, X_FORWARDED_PROTO
import pytest import pytest
from homeassistant.components.hassio.const import X_AUTH_TOKEN
@pytest.mark.parametrize( @pytest.mark.parametrize(
"build_type", "build_type",
@ -35,7 +37,7 @@ async def test_ingress_request_get(hassio_client, build_type, aioclient_mock):
# Check we forwarded command # Check we forwarded command
assert len(aioclient_mock.mock_calls) == 1 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 ( assert (
aioclient_mock.mock_calls[-1][3]["X-Ingress-Path"] aioclient_mock.mock_calls[-1][3]["X-Ingress-Path"]
== f"/api/hassio_ingress/{build_type[0]}" == 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 # Check we forwarded command
assert len(aioclient_mock.mock_calls) == 1 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 ( assert (
aioclient_mock.mock_calls[-1][3]["X-Ingress-Path"] aioclient_mock.mock_calls[-1][3]["X-Ingress-Path"]
== f"/api/hassio_ingress/{build_type[0]}" == 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 # Check we forwarded command
assert len(aioclient_mock.mock_calls) == 1 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 ( assert (
aioclient_mock.mock_calls[-1][3]["X-Ingress-Path"] aioclient_mock.mock_calls[-1][3]["X-Ingress-Path"]
== f"/api/hassio_ingress/{build_type[0]}" == 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 # Check we forwarded command
assert len(aioclient_mock.mock_calls) == 1 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 ( assert (
aioclient_mock.mock_calls[-1][3]["X-Ingress-Path"] aioclient_mock.mock_calls[-1][3]["X-Ingress-Path"]
== f"/api/hassio_ingress/{build_type[0]}" == 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 # Check we forwarded command
assert len(aioclient_mock.mock_calls) == 1 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 ( assert (
aioclient_mock.mock_calls[-1][3]["X-Ingress-Path"] aioclient_mock.mock_calls[-1][3]["X-Ingress-Path"]
== f"/api/hassio_ingress/{build_type[0]}" == 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 # Check we forwarded command
assert len(aioclient_mock.mock_calls) == 1 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 ( assert (
aioclient_mock.mock_calls[-1][3]["X-Ingress-Path"] aioclient_mock.mock_calls[-1][3]["X-Ingress-Path"]
== f"/api/hassio_ingress/{build_type[0]}" == 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 # Check we forwarded command
assert len(aioclient_mock.mock_calls) == 1 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 ( assert (
aioclient_mock.mock_calls[-1][3]["X-Ingress-Path"] aioclient_mock.mock_calls[-1][3]["X-Ingress-Path"]
== f"/api/hassio_ingress/{build_type[0]}" == f"/api/hassio_ingress/{build_type[0]}"

View File

@ -348,7 +348,7 @@ async def test_setup_hassio_no_additional_data(hass, aioclient_mock):
assert result assert result
assert aioclient_mock.call_count == 15 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): async def test_fail_setup_without_environ_var(hass):