mirror of
https://github.com/home-assistant/supervisor.git
synced 2025-07-14 12:46:32 +00:00
Make secrets more robust again (#2758)
This commit is contained in:
parent
98f8e032e3
commit
2be1529cb8
@ -5,6 +5,7 @@ from pathlib import Path
|
||||
from typing import Dict, Optional, Union
|
||||
|
||||
from ..coresys import CoreSys, CoreSysAttributes
|
||||
from ..exceptions import YamlFileError
|
||||
from ..jobs.const import JobExecutionLimit
|
||||
from ..jobs.decorator import Job
|
||||
from ..utils.yaml import read_yaml_file
|
||||
@ -48,7 +49,16 @@ class HomeAssistantSecrets(CoreSysAttributes):
|
||||
return
|
||||
|
||||
# Read secrets
|
||||
secrets = await self.sys_run_in_executor(read_yaml_file, self.path_secrets)
|
||||
try:
|
||||
secrets = await self.sys_run_in_executor(read_yaml_file, self.path_secrets)
|
||||
except YamlFileError as err:
|
||||
_LOGGER.warning("Can't read Home Assistant secrets: %s", err)
|
||||
return
|
||||
|
||||
if not isinstance(secrets, dict):
|
||||
return
|
||||
|
||||
# Process secrets
|
||||
self.secrets = {
|
||||
k: v for k, v in secrets.items() if isinstance(v, (bool, float, int, str))
|
||||
}
|
||||
|
@ -39,8 +39,9 @@ def write_json_file(jsonfile: Path, data: Any) -> None:
|
||||
fp.write(json.dumps(data, indent=2, cls=JSONEncoder))
|
||||
jsonfile.chmod(0o600)
|
||||
except (OSError, ValueError, TypeError) as err:
|
||||
_LOGGER.error("Can't write %s: %s", jsonfile, err)
|
||||
raise JsonFileError() from err
|
||||
raise JsonFileError(
|
||||
f"Can't write {jsonfile!s}: {err!s}", _LOGGER.error
|
||||
) from err
|
||||
|
||||
|
||||
def read_json_file(jsonfile: Path) -> Any:
|
||||
@ -48,5 +49,6 @@ def read_json_file(jsonfile: Path) -> Any:
|
||||
try:
|
||||
return json.loads(jsonfile.read_text())
|
||||
except (OSError, ValueError, TypeError, UnicodeDecodeError) as err:
|
||||
_LOGGER.error("Can't read json from %s: %s", jsonfile, err)
|
||||
raise JsonFileError() from err
|
||||
raise JsonFileError(
|
||||
f"Can't read json from {jsonfile!s}: {err!s}", _LOGGER.error
|
||||
) from err
|
||||
|
@ -19,8 +19,9 @@ def read_yaml_file(path: Path) -> dict:
|
||||
return _YAML.load(path) or {}
|
||||
|
||||
except (YAMLError, AttributeError) as err:
|
||||
_LOGGER.error("Can't read YAML file %s - %s", path, err)
|
||||
raise YamlFileError() from err
|
||||
raise YamlFileError(
|
||||
f"Can't read YAML file {path!s} - {err!s}", _LOGGER.error
|
||||
) from err
|
||||
|
||||
|
||||
def write_yaml_file(path: Path, data: dict) -> None:
|
||||
@ -30,5 +31,4 @@ def write_yaml_file(path: Path, data: dict) -> None:
|
||||
_YAML.dump(data, fp)
|
||||
path.chmod(0o600)
|
||||
except (YAMLError, OSError, ValueError, TypeError) as err:
|
||||
_LOGGER.error("Can't write %s: %s", path, err)
|
||||
raise YamlFileError() from err
|
||||
raise YamlFileError(f"Can't write {path!s}: {err!s}", _LOGGER.error) from err
|
||||
|
Loading…
x
Reference in New Issue
Block a user