validation.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. from __future__ import absolute_import
  2. from __future__ import unicode_literals
  3. import json
  4. import logging
  5. import os
  6. import re
  7. import sys
  8. import six
  9. from docker.utils.ports import split_port
  10. from jsonschema import Draft4Validator
  11. from jsonschema import FormatChecker
  12. from jsonschema import RefResolver
  13. from jsonschema import ValidationError
  14. from ..const import COMPOSEFILE_V1 as V1
  15. from .errors import ConfigurationError
  16. from .errors import VERSION_EXPLANATION
  17. from .sort_services import get_service_name_from_network_mode
  18. log = logging.getLogger(__name__)
  19. DOCKER_CONFIG_HINTS = {
  20. 'cpu_share': 'cpu_shares',
  21. 'add_host': 'extra_hosts',
  22. 'hosts': 'extra_hosts',
  23. 'extra_host': 'extra_hosts',
  24. 'device': 'devices',
  25. 'link': 'links',
  26. 'memory_swap': 'memswap_limit',
  27. 'port': 'ports',
  28. 'privilege': 'privileged',
  29. 'priviliged': 'privileged',
  30. 'privilige': 'privileged',
  31. 'volume': 'volumes',
  32. 'workdir': 'working_dir',
  33. }
  34. VALID_NAME_CHARS = '[a-zA-Z0-9\._\-]'
  35. VALID_EXPOSE_FORMAT = r'^\d+(\-\d+)?(\/[a-zA-Z]+)?$'
  36. @FormatChecker.cls_checks(format="ports", raises=ValidationError)
  37. def format_ports(instance):
  38. try:
  39. split_port(instance)
  40. except ValueError as e:
  41. raise ValidationError(six.text_type(e))
  42. return True
  43. @FormatChecker.cls_checks(format="expose", raises=ValidationError)
  44. def format_expose(instance):
  45. if isinstance(instance, six.string_types):
  46. if not re.match(VALID_EXPOSE_FORMAT, instance):
  47. raise ValidationError(
  48. "should be of the format 'PORT[/PROTOCOL]'")
  49. return True
  50. @FormatChecker.cls_checks(format="bool-value-in-mapping")
  51. def format_boolean_in_environment(instance):
  52. """Check if there is a boolean in the mapping sections and display a warning.
  53. Always return True here so the validation won't raise an error.
  54. """
  55. if isinstance(instance, bool):
  56. log.warn(
  57. "There is a boolean value in the 'environment', 'labels', or "
  58. "'extra_hosts' field of a service.\n"
  59. "These sections only support string values.\n"
  60. "Please add quotes to any boolean values to make them strings "
  61. "(eg, 'True', 'false', 'yes', 'N', 'on', 'Off').\n"
  62. "This warning will become an error in a future release. \r\n"
  63. )
  64. return True
  65. def match_named_volumes(service_dict, project_volumes):
  66. service_volumes = service_dict.get('volumes', [])
  67. for volume_spec in service_volumes:
  68. if volume_spec.is_named_volume and volume_spec.external not in project_volumes:
  69. raise ConfigurationError(
  70. 'Named volume "{0}" is used in service "{1}" but no'
  71. ' declaration was found in the volumes section.'.format(
  72. volume_spec.repr(), service_dict.get('name')
  73. )
  74. )
  75. def python_type_to_yaml_type(type_):
  76. type_name = type(type_).__name__
  77. return {
  78. 'dict': 'mapping',
  79. 'list': 'array',
  80. 'int': 'number',
  81. 'float': 'number',
  82. 'bool': 'boolean',
  83. 'unicode': 'string',
  84. 'str': 'string',
  85. 'bytes': 'string',
  86. }.get(type_name, type_name)
  87. def validate_config_section(filename, config, section):
  88. """Validate the structure of a configuration section. This must be done
  89. before interpolation so it's separate from schema validation.
  90. """
  91. if not isinstance(config, dict):
  92. raise ConfigurationError(
  93. "In file '{filename}', {section} must be a mapping, not "
  94. "{type}.".format(
  95. filename=filename,
  96. section=section,
  97. type=anglicize_json_type(python_type_to_yaml_type(config))))
  98. for key, value in config.items():
  99. if not isinstance(key, six.string_types):
  100. raise ConfigurationError(
  101. "In file '{filename}', the {section} name {name} must be a "
  102. "quoted string, i.e. '{name}'.".format(
  103. filename=filename,
  104. section=section,
  105. name=key))
  106. if not isinstance(value, (dict, type(None))):
  107. raise ConfigurationError(
  108. "In file '{filename}', {section} '{name}' must be a mapping not "
  109. "{type}.".format(
  110. filename=filename,
  111. section=section,
  112. name=key,
  113. type=anglicize_json_type(python_type_to_yaml_type(value))))
  114. def validate_top_level_object(config_file):
  115. if not isinstance(config_file.config, dict):
  116. raise ConfigurationError(
  117. "Top level object in '{}' needs to be an object not '{}'.".format(
  118. config_file.filename,
  119. type(config_file.config)))
  120. def validate_ulimits(service_config):
  121. ulimit_config = service_config.config.get('ulimits', {})
  122. for limit_name, soft_hard_values in six.iteritems(ulimit_config):
  123. if isinstance(soft_hard_values, dict):
  124. if not soft_hard_values['soft'] <= soft_hard_values['hard']:
  125. raise ConfigurationError(
  126. "Service '{s.name}' has invalid ulimit '{ulimit}'. "
  127. "'soft' value can not be greater than 'hard' value ".format(
  128. s=service_config,
  129. ulimit=ulimit_config))
  130. def validate_extends_file_path(service_name, extends_options, filename):
  131. """
  132. The service to be extended must either be defined in the config key 'file',
  133. or within 'filename'.
  134. """
  135. error_prefix = "Invalid 'extends' configuration for %s:" % service_name
  136. if 'file' not in extends_options and filename is None:
  137. raise ConfigurationError(
  138. "%s you need to specify a 'file', e.g. 'file: something.yml'" % error_prefix
  139. )
  140. def validate_network_mode(service_config, service_names):
  141. network_mode = service_config.config.get('network_mode')
  142. if not network_mode:
  143. return
  144. if 'networks' in service_config.config:
  145. raise ConfigurationError("'network_mode' and 'networks' cannot be combined")
  146. dependency = get_service_name_from_network_mode(network_mode)
  147. if not dependency:
  148. return
  149. if dependency not in service_names:
  150. raise ConfigurationError(
  151. "Service '{s.name}' uses the network stack of service '{dep}' which "
  152. "is undefined.".format(s=service_config, dep=dependency))
  153. def validate_depends_on(service_config, service_names):
  154. for dependency in service_config.config.get('depends_on', []):
  155. if dependency not in service_names:
  156. raise ConfigurationError(
  157. "Service '{s.name}' depends on service '{dep}' which is "
  158. "undefined.".format(s=service_config, dep=dependency))
  159. def get_unsupported_config_msg(path, error_key):
  160. msg = "Unsupported config option for {}: '{}'".format(path_string(path), error_key)
  161. if error_key in DOCKER_CONFIG_HINTS:
  162. msg += " (did you mean '{}'?)".format(DOCKER_CONFIG_HINTS[error_key])
  163. return msg
  164. def anglicize_json_type(json_type):
  165. if json_type.startswith(('a', 'e', 'i', 'o', 'u')):
  166. return 'an ' + json_type
  167. return 'a ' + json_type
  168. def is_service_dict_schema(schema_id):
  169. return schema_id in ('config_schema_v1.json', '#/properties/services')
  170. def handle_error_for_schema_with_id(error, path):
  171. schema_id = error.schema['id']
  172. if is_service_dict_schema(schema_id) and error.validator == 'additionalProperties':
  173. return "Invalid service name '{}' - only {} characters are allowed".format(
  174. # The service_name is the key to the json object
  175. list(error.instance)[0],
  176. VALID_NAME_CHARS)
  177. if error.validator == 'additionalProperties':
  178. if schema_id == '#/definitions/service':
  179. invalid_config_key = parse_key_from_error_msg(error)
  180. return get_unsupported_config_msg(path, invalid_config_key)
  181. if not error.path:
  182. return '{}\n{}'.format(error.message, VERSION_EXPLANATION)
  183. def handle_generic_error(error, path):
  184. msg_format = None
  185. error_msg = error.message
  186. if error.validator == 'oneOf':
  187. msg_format = "{path} {msg}"
  188. config_key, error_msg = _parse_oneof_validator(error)
  189. if config_key:
  190. path.append(config_key)
  191. elif error.validator == 'type':
  192. msg_format = "{path} contains an invalid type, it should be {msg}"
  193. error_msg = _parse_valid_types_from_validator(error.validator_value)
  194. elif error.validator == 'required':
  195. error_msg = ", ".join(error.validator_value)
  196. msg_format = "{path} is invalid, {msg} is required."
  197. elif error.validator == 'dependencies':
  198. config_key = list(error.validator_value.keys())[0]
  199. required_keys = ",".join(error.validator_value[config_key])
  200. msg_format = "{path} is invalid: {msg}"
  201. path.append(config_key)
  202. error_msg = "when defining '{}' you must set '{}' as well".format(
  203. config_key,
  204. required_keys)
  205. elif error.cause:
  206. error_msg = six.text_type(error.cause)
  207. msg_format = "{path} is invalid: {msg}"
  208. elif error.path:
  209. msg_format = "{path} value {msg}"
  210. if msg_format:
  211. return msg_format.format(path=path_string(path), msg=error_msg)
  212. return error.message
  213. def parse_key_from_error_msg(error):
  214. return error.message.split("'")[1]
  215. def path_string(path):
  216. return ".".join(c for c in path if isinstance(c, six.string_types))
  217. def _parse_valid_types_from_validator(validator):
  218. """A validator value can be either an array of valid types or a string of
  219. a valid type. Parse the valid types and prefix with the correct article.
  220. """
  221. if not isinstance(validator, list):
  222. return anglicize_json_type(validator)
  223. if len(validator) == 1:
  224. return anglicize_json_type(validator[0])
  225. return "{}, or {}".format(
  226. ", ".join([anglicize_json_type(validator[0])] + validator[1:-1]),
  227. anglicize_json_type(validator[-1]))
  228. def _parse_oneof_validator(error):
  229. """oneOf has multiple schemas, so we need to reason about which schema, sub
  230. schema or constraint the validation is failing on.
  231. Inspecting the context value of a ValidationError gives us information about
  232. which sub schema failed and which kind of error it is.
  233. """
  234. types = []
  235. for context in error.context:
  236. if context.validator == 'oneOf':
  237. _, error_msg = _parse_oneof_validator(context)
  238. return path_string(context.path), error_msg
  239. if context.validator == 'required':
  240. return (None, context.message)
  241. if context.validator == 'additionalProperties':
  242. invalid_config_key = parse_key_from_error_msg(context)
  243. return (None, "contains unsupported option: '{}'".format(invalid_config_key))
  244. if context.path:
  245. return (
  246. path_string(context.path),
  247. "contains {}, which is an invalid type, it should be {}".format(
  248. json.dumps(context.instance),
  249. _parse_valid_types_from_validator(context.validator_value)),
  250. )
  251. if context.validator == 'uniqueItems':
  252. return (
  253. None,
  254. "contains non unique items, please remove duplicates from {}".format(
  255. context.instance),
  256. )
  257. if context.validator == 'type':
  258. types.append(context.validator_value)
  259. valid_types = _parse_valid_types_from_validator(types)
  260. return (None, "contains an invalid type, it should be {}".format(valid_types))
  261. def process_service_constraint_errors(error, service_name, version):
  262. if version == V1:
  263. if 'image' in error.instance and 'build' in error.instance:
  264. return (
  265. "Service {} has both an image and build path specified. "
  266. "A service can either be built to image or use an existing "
  267. "image, not both.".format(service_name))
  268. if 'image' in error.instance and 'dockerfile' in error.instance:
  269. return (
  270. "Service {} has both an image and alternate Dockerfile. "
  271. "A service can either be built to image or use an existing "
  272. "image, not both.".format(service_name))
  273. if 'image' not in error.instance and 'build' not in error.instance:
  274. return (
  275. "Service {} has neither an image nor a build context specified. "
  276. "At least one must be provided.".format(service_name))
  277. def process_config_schema_errors(error):
  278. path = list(error.path)
  279. if 'id' in error.schema:
  280. error_msg = handle_error_for_schema_with_id(error, path)
  281. if error_msg:
  282. return error_msg
  283. return handle_generic_error(error, path)
  284. def validate_against_config_schema(config_file):
  285. schema = load_jsonschema(config_file.version)
  286. format_checker = FormatChecker(["ports", "expose", "bool-value-in-mapping"])
  287. validator = Draft4Validator(
  288. schema,
  289. resolver=RefResolver(get_resolver_path(), schema),
  290. format_checker=format_checker)
  291. handle_errors(
  292. validator.iter_errors(config_file.config),
  293. process_config_schema_errors,
  294. config_file.filename)
  295. def validate_service_constraints(config, service_name, version):
  296. def handler(errors):
  297. return process_service_constraint_errors(errors, service_name, version)
  298. schema = load_jsonschema(version)
  299. validator = Draft4Validator(schema['definitions']['constraints']['service'])
  300. handle_errors(validator.iter_errors(config), handler, None)
  301. def get_schema_path():
  302. return os.path.dirname(os.path.abspath(__file__))
  303. def load_jsonschema(version):
  304. filename = os.path.join(
  305. get_schema_path(),
  306. "config_schema_v{0}.json".format(version))
  307. with open(filename, "r") as fh:
  308. return json.load(fh)
  309. def get_resolver_path():
  310. schema_path = get_schema_path()
  311. if sys.platform == "win32":
  312. scheme = "///"
  313. # TODO: why is this necessary?
  314. schema_path = schema_path.replace('\\', '/')
  315. else:
  316. scheme = "//"
  317. return "file:{}{}/".format(scheme, schema_path)
  318. def handle_errors(errors, format_error_func, filename):
  319. """jsonschema returns an error tree full of information to explain what has
  320. gone wrong. Process each error and pull out relevant information and re-write
  321. helpful error messages that are relevant.
  322. """
  323. errors = list(sorted(errors, key=str))
  324. if not errors:
  325. return
  326. error_msg = '\n'.join(format_error_func(error) for error in errors)
  327. raise ConfigurationError(
  328. "Validation failed{file_msg}, reason(s):\n{error_msg}".format(
  329. file_msg=" in file '{}'".format(filename) if filename else "",
  330. error_msg=error_msg))