Skip to content

Commit 5c5428f

Browse files
authored
Revert "Use Unix socket for Supervisor to Core communication (#6590)" (#6735)
This reverts commit 28fa0b3.
1 parent 77e87fa commit 5c5428f

File tree

19 files changed

+254
-862
lines changed

19 files changed

+254
-862
lines changed

.github/workflows/builder.yml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -284,10 +284,9 @@ jobs:
284284
--privileged \
285285
--security-opt seccomp=unconfined \
286286
--security-opt apparmor=unconfined \
287-
-v /run/docker.sock:/run/docker.sock:rw \
288-
-v /run/dbus:/run/dbus:ro \
289-
-v /run/supervisor:/run/os:rw \
290-
-v /tmp/supervisor/data:/data:rw,slave \
287+
-v /run/docker.sock:/run/docker.sock \
288+
-v /run/dbus:/run/dbus \
289+
-v /tmp/supervisor/data:/data \
291290
-v /etc/machine-id:/etc/machine-id:ro \
292291
-e SUPERVISOR_SHARE="/tmp/supervisor/data" \
293292
-e SUPERVISOR_NAME=hassio_supervisor \

supervisor/api/proxy.py

Lines changed: 50 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import aiohttp
99
from aiohttp import WSCloseCode, WSMessageTypeError, web
10+
from aiohttp.client_exceptions import ClientConnectorError
1011
from aiohttp.client_ws import ClientWebSocketResponse
1112
from aiohttp.hdrs import AUTHORIZATION, CONTENT_TYPE
1213
from aiohttp.http_websocket import WSMsgType
@@ -178,16 +179,57 @@ async def api(self, request: web.Request):
178179

179180
async def _websocket_client(self) -> ClientWebSocketResponse:
180181
"""Initialize a WebSocket API connection."""
182+
url = f"{self.sys_homeassistant.api_url}/api/websocket"
183+
181184
try:
182-
ws_client = await self.sys_homeassistant.api.connect_websocket(
183-
max_msg_size=MAX_MESSAGE_SIZE_FROM_CORE
185+
client = await self.sys_websession.ws_connect(
186+
url, heartbeat=30, ssl=False, max_msg_size=MAX_MESSAGE_SIZE_FROM_CORE
184187
)
185-
return ws_client.client
186-
except HomeAssistantAPIError as err:
187-
raise APIError(
188-
f"Error connecting to Home Assistant WebSocket: {err}",
189-
_LOGGER.error,
190-
) from err
188+
189+
# Handle authentication
190+
data = await client.receive_json()
191+
192+
if data.get("type") == "auth_ok":
193+
return client
194+
195+
if data.get("type") != "auth_required":
196+
# Invalid protocol
197+
raise APIError(
198+
f"Got unexpected response from Home Assistant WebSocket: {data}",
199+
_LOGGER.error,
200+
)
201+
202+
# Auth session
203+
await self.sys_homeassistant.api.ensure_access_token()
204+
await client.send_json(
205+
{
206+
"type": "auth",
207+
"access_token": self.sys_homeassistant.api.access_token,
208+
},
209+
dumps=json_dumps,
210+
)
211+
212+
data = await client.receive_json()
213+
214+
if data.get("type") == "auth_ok":
215+
return client
216+
217+
# Renew the Token is invalid
218+
if (
219+
data.get("type") == "invalid_auth"
220+
and self.sys_homeassistant.refresh_token
221+
):
222+
self.sys_homeassistant.api.access_token = None
223+
return await self._websocket_client()
224+
225+
raise HomeAssistantAuthError()
226+
227+
except (RuntimeError, ValueError, TypeError, ClientConnectorError) as err:
228+
_LOGGER.error("Client error on WebSocket API %s.", err)
229+
except HomeAssistantAuthError:
230+
_LOGGER.error("Failed authentication to Home Assistant WebSocket")
231+
232+
raise APIError()
191233

192234
async def _proxy_message(
193235
self,

supervisor/const.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,9 @@
3939
FILE_SUFFIX_CONFIGURATION = [".yaml", ".yml", ".json"]
4040

4141
MACHINE_ID = Path("/etc/machine-id")
42-
RUN_SUPERVISOR_STATE = Path("/run/supervisor")
43-
SOCKET_CORE = Path("/run/os/core.sock")
4442
SOCKET_DBUS = Path("/run/dbus/system_bus_socket")
4543
SOCKET_DOCKER = Path("/run/docker.sock")
44+
RUN_SUPERVISOR_STATE = Path("/run/supervisor")
4645
SYSTEMD_JOURNAL_PERSISTENT = Path("/var/log/journal")
4746
SYSTEMD_JOURNAL_VOLATILE = Path("/run/log/journal")
4847

supervisor/core.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,6 @@ async def stop(self) -> None:
338338
self.sys_create_task(coro)
339339
for coro in (
340340
self.sys_websession.close(),
341-
self.sys_homeassistant.api.close(),
342341
self.sys_ingress.unload(),
343342
self.sys_hardware.unload(),
344343
self.sys_dbus.unload(),

supervisor/docker/const.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,6 @@ def to_dict(self) -> dict[str, str | int]:
140140
}
141141

142142

143-
ENV_CORE_API_SOCKET = "SUPERVISOR_CORE_API_SOCKET"
144143
ENV_DUPLICATE_LOG_FILE = "HA_DUPLICATE_LOG_FILE"
145144
ENV_TIME = "TZ"
146145
ENV_TOKEN = "SUPERVISOR_TOKEN"
@@ -170,12 +169,6 @@ def to_dict(self) -> dict[str, str | int]:
170169
target=MACHINE_ID.as_posix(),
171170
read_only=True,
172171
)
173-
MOUNT_CORE_RUN = DockerMount(
174-
type=MountType.BIND,
175-
source="/run/supervisor",
176-
target="/run/supervisor",
177-
read_only=False,
178-
)
179172
MOUNT_UDEV = DockerMount(
180173
type=MountType.BIND, source="/run/udev", target="/run/udev", read_only=True
181174
)

supervisor/docker/homeassistant.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,10 @@
1313
from ..jobs.const import JobConcurrency
1414
from ..jobs.decorator import Job
1515
from .const import (
16-
ENV_CORE_API_SOCKET,
1716
ENV_DUPLICATE_LOG_FILE,
1817
ENV_TIME,
1918
ENV_TOKEN,
2019
ENV_TOKEN_OLD,
21-
MOUNT_CORE_RUN,
2220
MOUNT_DBUS,
2321
MOUNT_DEV,
2422
MOUNT_MACHINE_ID,
@@ -164,9 +162,6 @@ def mounts(self) -> list[DockerMount]:
164162
if self.sys_machine_id:
165163
mounts.append(MOUNT_MACHINE_ID)
166164

167-
if self.sys_homeassistant.api.supports_unix_socket:
168-
mounts.append(MOUNT_CORE_RUN)
169-
170165
return mounts
171166

172167
@Job(
@@ -185,8 +180,6 @@ async def run(self, *, restore_job_id: str | None = None) -> None:
185180
}
186181
if restore_job_id:
187182
environment[ENV_RESTORE_JOB_ID] = restore_job_id
188-
if self.sys_homeassistant.api.supports_unix_socket:
189-
environment[ENV_CORE_API_SOCKET] = "/run/supervisor/core.sock"
190183
if self.sys_homeassistant.duplicate_log_file:
191184
environment[ENV_DUPLICATE_LOG_FILE] = "1"
192185
await self._run(

supervisor/docker/interface.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,6 @@ def timeout(self) -> int:
115115
def name(self) -> str:
116116
"""Return name of Docker container."""
117117

118-
@property
119-
def attached(self) -> bool:
120-
"""Return True if container/image metadata has been loaded."""
121-
return self._meta is not None
122-
123118
@property
124119
def meta_config(self) -> dict[str, Any]:
125120
"""Return meta data of configuration for container/image."""

0 commit comments

Comments
 (0)