validation.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. import json
  2. import logging
  3. import os
  4. from functools import wraps
  5. from docker.utils.ports import split_port
  6. from jsonschema import Draft4Validator
  7. from jsonschema import FormatChecker
  8. from jsonschema import RefResolver
  9. from jsonschema import ValidationError
  10. from .errors import ConfigurationError
  11. log = logging.getLogger(__name__)
  12. DOCKER_CONFIG_HINTS = {
  13. 'cpu_share': 'cpu_shares',
  14. 'add_host': 'extra_hosts',
  15. 'hosts': 'extra_hosts',
  16. 'extra_host': 'extra_hosts',
  17. 'device': 'devices',
  18. 'link': 'links',
  19. 'memory_swap': 'memswap_limit',
  20. 'port': 'ports',
  21. 'privilege': 'privileged',
  22. 'priviliged': 'privileged',
  23. 'privilige': 'privileged',
  24. 'volume': 'volumes',
  25. 'workdir': 'working_dir',
  26. }
  27. VALID_NAME_CHARS = '[a-zA-Z0-9\._\-]'
  28. @FormatChecker.cls_checks(
  29. format="ports",
  30. raises=ValidationError(
  31. "Invalid port formatting, it should be "
  32. "'[[remote_ip:]remote_port:]port[/protocol]'"))
  33. def format_ports(instance):
  34. try:
  35. split_port(instance)
  36. except ValueError:
  37. return False
  38. return True
  39. @FormatChecker.cls_checks(format="environment")
  40. def format_boolean_in_environment(instance):
  41. """
  42. Check if there is a boolean in the environment and display a warning.
  43. Always return True here so the validation won't raise an error.
  44. """
  45. if isinstance(instance, bool):
  46. log.warn(
  47. "Warning: There is a boolean value, {0} in the 'environment' key.\n"
  48. "Environment variables can only be strings.\nPlease add quotes to any boolean values to make them string "
  49. "(eg, '{0}').\nThis warning will become an error in a future release. \r\n".format(instance)
  50. )
  51. return True
  52. def validate_service_names(func):
  53. @wraps(func)
  54. def func_wrapper(config):
  55. for service_name in config.keys():
  56. if type(service_name) is int:
  57. raise ConfigurationError(
  58. "Service name: {} needs to be a string, eg '{}'".format(service_name, service_name)
  59. )
  60. return func(config)
  61. return func_wrapper
  62. def validate_top_level_object(func):
  63. @wraps(func)
  64. def func_wrapper(config):
  65. if not isinstance(config, dict):
  66. raise ConfigurationError(
  67. "Top level object needs to be a dictionary. Check your .yml file that you have defined a service at the top level."
  68. )
  69. return func(config)
  70. return func_wrapper
  71. def validate_extends_file_path(service_name, extends_options, filename):
  72. """
  73. The service to be extended must either be defined in the config key 'file',
  74. or within 'filename'.
  75. """
  76. error_prefix = "Invalid 'extends' configuration for %s:" % service_name
  77. if 'file' not in extends_options and filename is None:
  78. raise ConfigurationError(
  79. "%s you need to specify a 'file', e.g. 'file: something.yml'" % error_prefix
  80. )
  81. def validate_extended_service_exists(extended_service_name, full_extended_config, extended_config_path):
  82. if extended_service_name not in full_extended_config:
  83. msg = (
  84. "Cannot extend service '%s' in %s: Service not found"
  85. ) % (extended_service_name, extended_config_path)
  86. raise ConfigurationError(msg)
  87. def get_unsupported_config_msg(service_name, error_key):
  88. msg = "Unsupported config option for '{}' service: '{}'".format(service_name, error_key)
  89. if error_key in DOCKER_CONFIG_HINTS:
  90. msg += " (did you mean '{}'?)".format(DOCKER_CONFIG_HINTS[error_key])
  91. return msg
  92. def anglicize_validator(validator):
  93. if validator in ["array", "object"]:
  94. return 'an ' + validator
  95. return 'a ' + validator
  96. def process_errors(errors, service_name=None):
  97. """
  98. jsonschema gives us an error tree full of information to explain what has
  99. gone wrong. Process each error and pull out relevant information and re-write
  100. helpful error messages that are relevant.
  101. """
  102. def _parse_key_from_error_msg(error):
  103. return error.message.split("'")[1]
  104. def _clean_error_message(message):
  105. return message.replace("u'", "'")
  106. def _parse_valid_types_from_validator(validator):
  107. """
  108. A validator value can be either an array of valid types or a string of
  109. a valid type. Parse the valid types and prefix with the correct article.
  110. """
  111. if isinstance(validator, list):
  112. if len(validator) >= 2:
  113. first_type = anglicize_validator(validator[0])
  114. last_type = anglicize_validator(validator[-1])
  115. types_from_validator = "{}{}".format(first_type, ", ".join(validator[1:-1]))
  116. msg = "{} or {}".format(
  117. types_from_validator,
  118. last_type
  119. )
  120. else:
  121. msg = "{}".format(anglicize_validator(validator[0]))
  122. else:
  123. msg = "{}".format(anglicize_validator(validator))
  124. return msg
  125. def _parse_oneof_validator(error):
  126. """
  127. oneOf has multiple schemas, so we need to reason about which schema, sub
  128. schema or constraint the validation is failing on.
  129. Inspecting the context value of a ValidationError gives us information about
  130. which sub schema failed and which kind of error it is.
  131. """
  132. constraint = [context for context in error.context if len(context.path) > 0]
  133. if constraint:
  134. valid_types = _parse_valid_types_from_validator(constraint[0].validator_value)
  135. msg = "contains {}, which is an invalid type, it should be {}".format(
  136. constraint[0].instance,
  137. valid_types
  138. )
  139. return msg
  140. uniqueness = [context for context in error.context if context.validator == 'uniqueItems']
  141. if uniqueness:
  142. msg = "contains non unique items, please remove duplicates from {}".format(
  143. uniqueness[0].instance
  144. )
  145. return msg
  146. types = [context.validator_value for context in error.context if context.validator == 'type']
  147. valid_types = _parse_valid_types_from_validator(types)
  148. msg = "contains an invalid type, it should be {}".format(valid_types)
  149. return msg
  150. root_msgs = []
  151. invalid_keys = []
  152. required = []
  153. type_errors = []
  154. other_errors = []
  155. for error in errors:
  156. # handle root level errors
  157. if len(error.path) == 0 and not error.instance.get('name'):
  158. if error.validator == 'type':
  159. msg = "Top level object needs to be a dictionary. Check your .yml file that you have defined a service at the top level."
  160. root_msgs.append(msg)
  161. elif error.validator == 'additionalProperties':
  162. invalid_service_name = _parse_key_from_error_msg(error)
  163. msg = "Invalid service name '{}' - only {} characters are allowed".format(invalid_service_name, VALID_NAME_CHARS)
  164. root_msgs.append(msg)
  165. else:
  166. root_msgs.append(_clean_error_message(error.message))
  167. else:
  168. if not service_name:
  169. # field_schema errors will have service name on the path
  170. service_name = error.path[0]
  171. error.path.popleft()
  172. else:
  173. # service_schema errors have the service name passed in, as that
  174. # is not available on error.path or necessarily error.instance
  175. service_name = service_name
  176. if error.validator == 'additionalProperties':
  177. invalid_config_key = _parse_key_from_error_msg(error)
  178. invalid_keys.append(get_unsupported_config_msg(service_name, invalid_config_key))
  179. elif error.validator == 'anyOf':
  180. if 'image' in error.instance and 'build' in error.instance:
  181. required.append(
  182. "Service '{}' has both an image and build path specified. "
  183. "A service can either be built to image or use an existing "
  184. "image, not both.".format(service_name))
  185. elif 'image' not in error.instance and 'build' not in error.instance:
  186. required.append(
  187. "Service '{}' has neither an image nor a build path "
  188. "specified. Exactly one must be provided.".format(service_name))
  189. elif 'image' in error.instance and 'dockerfile' in error.instance:
  190. required.append(
  191. "Service '{}' has both an image and alternate Dockerfile. "
  192. "A service can either be built to image or use an existing "
  193. "image, not both.".format(service_name))
  194. else:
  195. required.append(_clean_error_message(error.message))
  196. elif error.validator == 'oneOf':
  197. config_key = error.path[0]
  198. msg = _parse_oneof_validator(error)
  199. type_errors.append("Service '{}' configuration key '{}' {}".format(
  200. service_name, config_key, msg)
  201. )
  202. elif error.validator == 'type':
  203. msg = _parse_valid_types_from_validator(error.validator_value)
  204. if len(error.path) > 0:
  205. config_key = " ".join(["'%s'" % k for k in error.path])
  206. type_errors.append(
  207. "Service '{}' configuration key {} contains an invalid "
  208. "type, it should be {}".format(
  209. service_name,
  210. config_key,
  211. msg))
  212. else:
  213. root_msgs.append(
  214. "Service '{}' doesn\'t have any configuration options. "
  215. "All top level keys in your docker-compose.yml must map "
  216. "to a dictionary of configuration options.'".format(service_name))
  217. elif error.validator == 'required':
  218. config_key = error.path[0]
  219. required.append(
  220. "Service '{}' option '{}' is invalid, {}".format(
  221. service_name,
  222. config_key,
  223. _clean_error_message(error.message)))
  224. elif error.validator == 'dependencies':
  225. dependency_key = list(error.validator_value.keys())[0]
  226. required_keys = ",".join(error.validator_value[dependency_key])
  227. required.append("Invalid '{}' configuration for '{}' service: when defining '{}' you must set '{}' as well".format(
  228. dependency_key, service_name, dependency_key, required_keys))
  229. else:
  230. config_key = " ".join(["'%s'" % k for k in error.path])
  231. err_msg = "Service '{}' configuration key {} value {}".format(service_name, config_key, error.message)
  232. other_errors.append(err_msg)
  233. return "\n".join(root_msgs + invalid_keys + required + type_errors + other_errors)
  234. def validate_against_fields_schema(config):
  235. schema_filename = "fields_schema.json"
  236. format_checkers = ["ports", "environment"]
  237. return _validate_against_schema(config, schema_filename, format_checkers)
  238. def validate_against_service_schema(config, service_name):
  239. schema_filename = "service_schema.json"
  240. format_checkers = ["ports"]
  241. return _validate_against_schema(config, schema_filename, format_checkers, service_name)
  242. def _validate_against_schema(config, schema_filename, format_checker=[], service_name=None):
  243. config_source_dir = os.path.dirname(os.path.abspath(__file__))
  244. schema_file = os.path.join(config_source_dir, schema_filename)
  245. with open(schema_file, "r") as schema_fh:
  246. schema = json.load(schema_fh)
  247. resolver = RefResolver('file://' + config_source_dir + '/', schema)
  248. validation_output = Draft4Validator(schema, resolver=resolver, format_checker=FormatChecker(format_checker))
  249. errors = [error for error in sorted(validation_output.iter_errors(config), key=str)]
  250. if errors:
  251. error_msg = process_errors(errors, service_name)
  252. raise ConfigurationError("Validation failed, reason(s):\n{}".format(error_msg))