From 64364b20ff34b7492910838b44e16b3191f27f33 Mon Sep 17 00:00:00 2001 From: Dan Helfman Date: Sun, 22 Jul 2018 12:08:49 -0700 Subject: [PATCH] Skip non-"*.yaml" config filenames in /etc/borgmatic.d/ so as not to parse backup files, editor swap files, etc. (#77) --- NEWS | 2 ++ borgmatic/config/collect.py | 5 +++-- borgmatic/config/validate.py | 1 + borgmatic/tests/unit/config/test_collect.py | 15 +++++++++++++++ 4 files changed, 21 insertions(+), 2 deletions(-) diff --git a/NEWS b/NEWS index 7b07fe5..6ee552c 100644 --- a/NEWS +++ b/NEWS @@ -1,6 +1,8 @@ 1.2.1.dev0 * Skip before/after backup hooks when only doing --prune, --check, --list, and/or --info. * #38, #76: Upgrade ruamel.yaml compatibility version range and fix support for Python 3.7. + * #77: Skip non-"*.yaml" config filenames in /etc/borgmatic.d/ so as not to parse backup files, + editor swap files, etc. 1.2.0 * #61: Support for Borg --list option via borgmatic command-line to list all archives. diff --git a/borgmatic/config/collect.py b/borgmatic/config/collect.py index 02ccc9d..47f0ef4 100644 --- a/borgmatic/config/collect.py +++ b/borgmatic/config/collect.py @@ -12,7 +12,8 @@ def collect_config_filenames(config_paths): ''' Given a sequence of config paths, both filenames and directories, resolve that to just an iterable of files. Accomplish this by listing any given directories looking for contained config - files. This is non-recursive, so any directories within the given directories are ignored. + files (ending with the ".yaml" extension). This is non-recursive, so any directories within the + given directories are ignored. Return paths even if they don't exist on disk, so the user can find out about missing configuration paths. However, skip a default config path if it's missing, so the user doesn't @@ -32,5 +33,5 @@ def collect_config_filenames(config_paths): for filename in os.listdir(path): full_filename = os.path.join(path, filename) - if not os.path.isdir(full_filename): + if full_filename.endswith('.yaml') and not os.path.isdir(full_filename): yield full_filename diff --git a/borgmatic/config/validate.py b/borgmatic/config/validate.py index 795a07d..bdbbbf7 100644 --- a/borgmatic/config/validate.py +++ b/borgmatic/config/validate.py @@ -10,6 +10,7 @@ from ruamel import yaml logger = logging.getLogger(__name__) + def schema_filename(): ''' Path to the installed YAML configuration schema file, used to validate and parse the diff --git a/borgmatic/tests/unit/config/test_collect.py b/borgmatic/tests/unit/config/test_collect.py index 65acbf8..d573ae0 100644 --- a/borgmatic/tests/unit/config/test_collect.py +++ b/borgmatic/tests/unit/config/test_collect.py @@ -32,6 +32,21 @@ def test_collect_config_filenames_collects_files_from_given_directories_and_igno ) +def test_collect_config_filenames_collects_files_from_given_directories_and_ignores_non_yaml_filenames(): + config_paths = ('/etc/borgmatic.d',) + mock_path = flexmock(module.os.path) + mock_path.should_receive('exists').and_return(True) + mock_path.should_receive('isdir').with_args('/etc/borgmatic.d').and_return(True) + mock_path.should_receive('isdir').with_args('/etc/borgmatic.d/foo.yaml').and_return(False) + mock_path.should_receive('isdir').with_args('/etc/borgmatic.d/bar.yaml~').and_return(False) + mock_path.should_receive('isdir').with_args('/etc/borgmatic.d/baz.txt').and_return(False) + flexmock(module.os).should_receive('listdir').and_return(['foo.yaml', 'bar.yaml~', 'baz.txt']) + + config_filenames = tuple(module.collect_config_filenames(config_paths)) + + assert config_filenames == ('/etc/borgmatic.d/foo.yaml',) + + def test_collect_config_filenames_skips_etc_borgmatic_config_dot_yaml_if_it_does_not_exist(): config_paths = ('config.yaml', '/etc/borgmatic/config.yaml') mock_path = flexmock(module.os.path)