mirror of
https://github.com/home-assistant/core.git
synced 2026-12-04 12:50:45 -04:00
Remove orphaned system user of removed almond integration (#186943)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
388fc5632f
commit
7ee20ff7ac
@@ -2,6 +2,7 @@
|
||||
|
||||
import asyncio
|
||||
from collections.abc import Callable, Coroutine
|
||||
import logging
|
||||
import struct
|
||||
from typing import Any
|
||||
|
||||
@@ -10,6 +11,8 @@ from homeassistant.core import Event, HomeAssistant, callback
|
||||
from homeassistant.helpers import issue_registry as ir
|
||||
from homeassistant.helpers.issue_registry import IssueSeverity
|
||||
from homeassistant.helpers.signal import KEY_HA_STOP
|
||||
from homeassistant.helpers.start import async_at_started
|
||||
from homeassistant.helpers.storage import Store
|
||||
from homeassistant.helpers.system_info import async_get_system_info
|
||||
from homeassistant.helpers.typing import ConfigType
|
||||
|
||||
@@ -43,6 +46,12 @@ DEPRECATION_URL = (
|
||||
"deprecating-core-and-supervised-installation-methods-and-32-bit-systems/"
|
||||
)
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
# Storage of the removed almond integration, which created an admin system user
|
||||
LEGACY_ALMOND_STORAGE_KEY = "almond"
|
||||
LEGACY_ALMOND_STORAGE_VERSION = 1
|
||||
|
||||
__all__ = [
|
||||
"DOMAIN",
|
||||
"SERVICE_HOMEASSISTANT_RESTART",
|
||||
@@ -127,9 +136,28 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
||||
# Delay deprecation check to make sure installation method is determined correctly
|
||||
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STARTED, _async_check_deprecation)
|
||||
|
||||
async_at_started(hass, _async_remove_legacy_almond_user_and_data)
|
||||
|
||||
return True
|
||||
|
||||
|
||||
async def _async_remove_legacy_almond_user_and_data(hass: HomeAssistant) -> None:
|
||||
"""Remove the system user left behind by the removed almond integration."""
|
||||
store = Store[dict[str, Any]](
|
||||
hass, LEGACY_ALMOND_STORAGE_VERSION, LEGACY_ALMOND_STORAGE_KEY
|
||||
)
|
||||
if (data := await store.async_load()) is None:
|
||||
return
|
||||
|
||||
if (user_id := data.get("almond_user")) and (
|
||||
user := await hass.auth.async_get_user(user_id)
|
||||
):
|
||||
_LOGGER.debug("Removing system user %s of legacy almond integration", user.id)
|
||||
await hass.auth.async_remove_user(user)
|
||||
|
||||
await store.async_remove()
|
||||
|
||||
|
||||
async def _async_stop(hass: HomeAssistant, restart: bool) -> None:
|
||||
"""Stop home assistant."""
|
||||
exit_code = RESTART_EXIT_CODE if restart else 0
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
"""The tests for Core components."""
|
||||
|
||||
from typing import Any
|
||||
from unittest.mock import Mock, patch
|
||||
|
||||
import probatio
|
||||
@@ -807,3 +808,56 @@ async def test_deprecated_installation_issue_container_32bit(
|
||||
assert issue.domain == DOMAIN
|
||||
assert issue.severity == ir.IssueSeverity.WARNING
|
||||
assert issue.translation_placeholders == {"arch": arch}
|
||||
|
||||
|
||||
async def test_remove_legacy_almond_user(
|
||||
hass: HomeAssistant, hass_storage: dict[str, Any]
|
||||
) -> None:
|
||||
"""Test the system user of the removed almond integration is removed."""
|
||||
almond_user = await hass.auth.async_create_system_user("Almond")
|
||||
other_user = await hass.auth.async_create_system_user("Other")
|
||||
hass_storage["almond"] = {
|
||||
"version": 1,
|
||||
"key": "almond",
|
||||
"data": {"almond_user": almond_user.id},
|
||||
}
|
||||
|
||||
assert await async_setup_component(hass, DOMAIN, {})
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert await hass.auth.async_get_user(almond_user.id) is None
|
||||
assert await hass.auth.async_get_user(other_user.id) is not None
|
||||
assert "almond" not in hass_storage
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"data",
|
||||
[
|
||||
pytest.param({"almond_user": "unknown"}, id="unknown_user"),
|
||||
pytest.param({}, id="no_user"),
|
||||
],
|
||||
)
|
||||
async def test_remove_legacy_almond_store_without_user(
|
||||
hass: HomeAssistant, hass_storage: dict[str, Any], data: dict[str, Any]
|
||||
) -> None:
|
||||
"""Test the almond store is removed if it does not reference an existing user."""
|
||||
other_user = await hass.auth.async_create_system_user("Other")
|
||||
hass_storage["almond"] = {"version": 1, "key": "almond", "data": data}
|
||||
|
||||
assert await async_setup_component(hass, DOMAIN, {})
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert await hass.auth.async_get_user(other_user.id) is not None
|
||||
assert "almond" not in hass_storage
|
||||
|
||||
|
||||
async def test_no_legacy_almond_store(
|
||||
hass: HomeAssistant, hass_storage: dict[str, Any]
|
||||
) -> None:
|
||||
"""Test no user is removed without an almond store."""
|
||||
other_user = await hass.auth.async_create_system_user("Other")
|
||||
|
||||
assert await async_setup_component(hass, DOMAIN, {})
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert await hass.auth.async_get_user(other_user.id) is not None
|
||||
|
||||
Reference in New Issue
Block a user