validate.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. import os
  2. import jsonschema
  3. import pkg_resources
  4. import ruamel.yaml
  5. from borgmatic.config import load, normalize, override
  6. def schema_filename():
  7. '''
  8. Path to the installed YAML configuration schema file, used to validate and parse the
  9. configuration.
  10. '''
  11. return pkg_resources.resource_filename('borgmatic', 'config/schema.yaml')
  12. def format_error_path_element(path_element):
  13. '''
  14. Given a path element into a JSON data structure, format it for display as a string.
  15. '''
  16. if isinstance(path_element, int):
  17. return str('[{}]'.format(path_element))
  18. return str('.{}'.format(path_element))
  19. def format_error(error):
  20. '''
  21. Given an instance of jsonschema.exceptions.ValidationError, format it for display as a string.
  22. '''
  23. if not error.path:
  24. return 'At the top level: {}'.format(error.message)
  25. formatted_path = ''.join(format_error_path_element(element) for element in error.path)
  26. return "At '{}': {}".format(formatted_path.lstrip('.'), error.message)
  27. class Validation_error(ValueError):
  28. '''
  29. A collection of error messages generated when attempting to validate a particular
  30. configuration file.
  31. '''
  32. def __init__(self, config_filename, errors):
  33. '''
  34. Given a configuration filename path and a sequence of
  35. jsonschema.exceptions.ValidationError instances, create a Validation_error.
  36. '''
  37. self.config_filename = config_filename
  38. self.errors = errors
  39. def __str__(self):
  40. '''
  41. Render a validation error as a user-facing string.
  42. '''
  43. return 'An error occurred while parsing a configuration file at {}:\n'.format(
  44. self.config_filename
  45. ) + '\n'.join(format_error(error) for error in self.errors)
  46. def apply_logical_validation(config_filename, parsed_configuration):
  47. '''
  48. Given a parsed and schematically valid configuration as a data structure of nested dicts (see
  49. below), run through any additional logical validation checks. If there are any such validation
  50. problems, raise a Validation_error.
  51. '''
  52. archive_name_format = parsed_configuration.get('storage', {}).get('archive_name_format')
  53. prefix = parsed_configuration.get('retention', {}).get('prefix')
  54. if archive_name_format and not prefix:
  55. raise Validation_error(
  56. config_filename,
  57. ('If you provide an archive_name_format, you must also specify a retention prefix.',),
  58. )
  59. location_repositories = parsed_configuration.get('location', {}).get('repositories')
  60. check_repositories = parsed_configuration.get('consistency', {}).get('check_repositories', [])
  61. for repository in check_repositories:
  62. if repository not in location_repositories:
  63. raise Validation_error(
  64. config_filename,
  65. (
  66. 'Unknown repository in the consistency section\'s check_repositories: {}'.format(
  67. repository
  68. ),
  69. ),
  70. )
  71. def parse_configuration(config_filename, schema_filename, overrides=None):
  72. '''
  73. Given the path to a config filename in YAML format, the path to a schema filename in a YAML
  74. rendition of JSON Schema format, a sequence of configuration file override strings in the form
  75. of "section.option=value", return the parsed configuration as a data structure of nested dicts
  76. and lists corresponding to the schema. Example return value:
  77. {'location': {'source_directories': ['/home', '/etc'], 'repository': 'hostname.borg'},
  78. 'retention': {'keep_daily': 7}, 'consistency': {'checks': ['repository', 'archives']}}
  79. Raise FileNotFoundError if the file does not exist, PermissionError if the user does not
  80. have permissions to read the file, or Validation_error if the config does not match the schema.
  81. '''
  82. try:
  83. config = load.load_configuration(config_filename)
  84. schema = load.load_configuration(schema_filename)
  85. except (ruamel.yaml.error.YAMLError, RecursionError) as error:
  86. raise Validation_error(config_filename, (str(error),))
  87. override.apply_overrides(config, overrides)
  88. normalize.normalize(config)
  89. try:
  90. validator = jsonschema.Draft7Validator(schema)
  91. except AttributeError:
  92. validator = jsonschema.Draft4Validator(schema)
  93. validation_errors = tuple(validator.iter_errors(config))
  94. if validation_errors:
  95. raise Validation_error(config_filename, validation_errors)
  96. apply_logical_validation(config_filename, config)
  97. return config
  98. def normalize_repository_path(repository):
  99. '''
  100. Given a repository path, return the absolute path of it (for local repositories).
  101. '''
  102. # A colon in the repository indicates it's a remote repository. Bail.
  103. if ':' in repository:
  104. return repository
  105. return os.path.abspath(repository)
  106. def repositories_match(first, second):
  107. '''
  108. Given two repository paths (relative and/or absolute), return whether they match.
  109. '''
  110. return normalize_repository_path(first) == normalize_repository_path(second)
  111. def guard_configuration_contains_repository(repository, configurations):
  112. '''
  113. Given a repository path and a dict mapping from config filename to corresponding parsed config
  114. dict, ensure that the repository is declared exactly once in all of the configurations.
  115. If no repository is given, then error if there are multiple configured repositories.
  116. Raise ValueError if the repository is not found in a configuration, or is declared multiple
  117. times.
  118. '''
  119. if not repository:
  120. count = len(
  121. tuple(
  122. config_repository
  123. for config in configurations.values()
  124. for config_repository in config['location']['repositories']
  125. )
  126. )
  127. if count > 1:
  128. raise ValueError(
  129. 'Can\'t determine which repository to use. Use --repository option to disambiguate'
  130. )
  131. return
  132. count = len(
  133. tuple(
  134. config_repository
  135. for config in configurations.values()
  136. for config_repository in config['location']['repositories']
  137. if repositories_match(repository, config_repository)
  138. )
  139. )
  140. if count == 0:
  141. raise ValueError('Repository {} not found in configuration files'.format(repository))
  142. if count > 1:
  143. raise ValueError('Repository {} found in multiple configuration files'.format(repository))