Make secrets more robust again (#2758)

This commit is contained in:
Pascal Vizeli 2021-03-26 16:01:25 +01:00 committed by GitHub
parent 98f8e032e3
commit 2be1529cb8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 9 deletions

View File

@ -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))
}

View File

@ -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

View File

@ -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