mirror of
https://github.com/home-assistant/supervisor.git
synced 2025-07-25 18:16:32 +00:00
Fix secrets containing unsupported types (#1345)
* Fix secrets containing unsupported types * Black * Cleanup
This commit is contained in:
parent
7b8ad0782d
commit
05d7eff09a
@ -2,18 +2,15 @@
|
|||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
import logging
|
import logging
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import Dict
|
from typing import Dict, Union
|
||||||
|
|
||||||
from ruamel.yaml import YAML, YAMLError
|
from ruamel.yaml import YAML, YAMLError
|
||||||
import voluptuous as vol
|
|
||||||
|
|
||||||
from .coresys import CoreSys, CoreSysAttributes
|
from .coresys import CoreSys, CoreSysAttributes
|
||||||
from .utils import AsyncThrottle
|
from .utils import AsyncThrottle
|
||||||
|
|
||||||
_LOGGER: logging.Logger = logging.getLogger(__name__)
|
_LOGGER: logging.Logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
SECRETS_SCHEMA = vol.Schema({str: vol.Any(str, int, None, float)})
|
|
||||||
|
|
||||||
|
|
||||||
class SecretsManager(CoreSysAttributes):
|
class SecretsManager(CoreSysAttributes):
|
||||||
"""Manage Home Assistant secrets."""
|
"""Manage Home Assistant secrets."""
|
||||||
@ -21,14 +18,14 @@ class SecretsManager(CoreSysAttributes):
|
|||||||
def __init__(self, coresys: CoreSys):
|
def __init__(self, coresys: CoreSys):
|
||||||
"""Initialize secret manager."""
|
"""Initialize secret manager."""
|
||||||
self.coresys: CoreSys = coresys
|
self.coresys: CoreSys = coresys
|
||||||
self.secrets: Dict[str, str] = {}
|
self.secrets: Dict[str, Union[bool, float, int, str]] = {}
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def path_secrets(self) -> Path:
|
def path_secrets(self) -> Path:
|
||||||
"""Return path to secret file."""
|
"""Return path to secret file."""
|
||||||
return Path(self.sys_config.path_homeassistant, "secrets.yaml")
|
return Path(self.sys_config.path_homeassistant, "secrets.yaml")
|
||||||
|
|
||||||
def get(self, secret: str) -> str:
|
def get(self, secret: str) -> Union[bool, float, int, str]:
|
||||||
"""Get secret from store."""
|
"""Get secret from store."""
|
||||||
_LOGGER.info("Request secret %s", secret)
|
_LOGGER.info("Request secret %s", secret)
|
||||||
return self.secrets.get(secret)
|
return self.secrets.get(secret)
|
||||||
@ -55,10 +52,12 @@ class SecretsManager(CoreSysAttributes):
|
|||||||
yaml = YAML()
|
yaml = YAML()
|
||||||
data = await self.sys_run_in_executor(yaml.load, self.path_secrets) or {}
|
data = await self.sys_run_in_executor(yaml.load, self.path_secrets) or {}
|
||||||
|
|
||||||
self.secrets = SECRETS_SCHEMA(data)
|
# Filter to only get supported values
|
||||||
|
self.secrets = {
|
||||||
|
k: v for k, v in data.items() if isinstance(v, (bool, float, int, str))
|
||||||
|
}
|
||||||
|
|
||||||
except YAMLError as err:
|
except YAMLError as err:
|
||||||
_LOGGER.error("Can't process Home Assistant secrets: %s", err)
|
_LOGGER.error("Can't process Home Assistant secrets: %s", err)
|
||||||
except vol.Invalid:
|
|
||||||
_LOGGER.warning("Home Assistant secrets have a invalid format")
|
|
||||||
else:
|
else:
|
||||||
_LOGGER.debug("Reload Home Assistant secrets: %s", len(self.secrets))
|
_LOGGER.debug("Reload Home Assistant secrets: %s", len(self.secrets))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user