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
parent
27209574d2
commit
eac7c5f177
|
@ -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"
|
||||
|
|
|
@ -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),
|
||||
)
|
||||
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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}"
|
||||
|
|
|
@ -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]}"
|
||||
|
|
|
@ -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):
|
||||
|
|
Loading…
Reference in New Issue