Skip to content
Snippets Groups Projects
Commit 1ab0b17a authored by Radoslav Bodó's avatar Radoslav Bodó
Browse files

rwm: improve config file handling (handle missing file, check and warn on permissions)

parent 3d592f89
No related branches found
No related tags found
No related merge requests found
Pipeline #7712 passed
...@@ -713,8 +713,14 @@ def load_config(path): ...@@ -713,8 +713,14 @@ def load_config(path):
"""load config dict from file""" """load config dict from file"""
config = {} config = {}
if path: try:
config = yaml.safe_load(Path(path).read_text(encoding='utf-8')) config_path = Path(path)
config_perms = config_path.stat().st_mode & 0o777
if config_perms != 0o600:
logger.warning(f"config file permissions ({config_perms:o}) are too-open")
config = yaml.safe_load(config_path.read_text(encoding='utf-8'))
except (OSError, ValueError) as exc:
logger.error(f"cannot load config file, {exc}")
logger.debug("config, %s", config) logger.debug("config, %s", config)
return config return config
...@@ -725,7 +731,9 @@ def main(argv=None): # pylint: disable=too-many-branches ...@@ -725,7 +731,9 @@ def main(argv=None): # pylint: disable=too-many-branches
args = parse_arguments(argv) args = parse_arguments(argv)
configure_logging(args.debug) configure_logging(args.debug)
rwmi = RWM(load_config(args.config)) if not (config_dict := load_config(args.config)):
return 1
rwmi = RWM(config_dict)
ret = -1 ret = -1
if args.command == "version": if args.command == "version":
......
...@@ -70,3 +70,6 @@ def test_main(): ...@@ -70,3 +70,6 @@ def test_main():
with patch.object(rwm.RWM, "storage_restore_state", mock_ok): with patch.object(rwm.RWM, "storage_restore_state", mock_ok):
assert _rwm_minconfig(["storage-restore-state", "bucket", "bucket", "state"]) == 0 assert _rwm_minconfig(["storage-restore-state", "bucket", "bucket", "state"]) == 0
# error handling
assert rwm_main(["--config", "notexist", "version"]) == 1
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment