validation.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  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 socket
  8. import sys
  9. import six
  10. from docker.utils.ports import split_port
  11. from jsonschema import Draft4Validator
  12. from jsonschema import FormatChecker
  13. from jsonschema import RefResolver
  14. from jsonschema import ValidationError
  15. from ..const import COMPOSEFILE_V1 as V1
  16. from ..const import NANOCPUS_SCALE
  17. from .errors import ConfigurationError
  18. from .errors import VERSION_EXPLANATION
  19. from .sort_services import get_service_name_from_network_mode
  20. log = logging.getLogger(__name__)
  21. DOCKER_CONFIG_HINTS = {
  22. 'cpu_share': 'cpu_shares',
  23. 'add_host': 'extra_hosts',
  24. 'hosts': 'extra_hosts',
  25. 'extra_host': 'extra_hosts',
  26. 'device': 'devices',
  27. 'link': 'links',
  28. 'memory_swap': 'memswap_limit',
  29. 'port': 'ports',
  30. 'privilege': 'privileged',
  31. 'priviliged': 'privileged',
  32. 'privilige': 'privileged',
  33. 'volume': 'volumes',
  34. 'workdir': 'working_dir',
  35. }
  36. VALID_NAME_CHARS = '[a-zA-Z0-9\._\-]'
  37. VALID_EXPOSE_FORMAT = r'^\d+(\-\d+)?(\/[a-zA-Z]+)?$'
  38. VALID_IPV4_FORMAT = r'^(\d{1,3}.){3}\d{1,3}$'
  39. VALID_IPV4_CIDR_FORMAT = r'^(\d|[1-2]\d|3[0-2])$'
  40. VALID_IPV6_CIDR_FORMAT = r'^(\d|[1-9]\d|1[0-1]\d|12[0-8])$'
  41. @FormatChecker.cls_checks(format="ports", raises=ValidationError)
  42. def format_ports(instance):
  43. try:
  44. split_port(instance)
  45. except ValueError as e:
  46. raise ValidationError(six.text_type(e))
  47. return True
  48. @FormatChecker.cls_checks(format="expose", raises=ValidationError)
  49. def format_expose(instance):
  50. if isinstance(instance, six.string_types):
  51. if not re.match(VALID_EXPOSE_FORMAT, instance):
  52. raise ValidationError(
  53. "should be of the format 'PORT[/PROTOCOL]'")
  54. return True
  55. @FormatChecker.cls_checks("subnet_ip_address", raises=ValidationError)
  56. def format_subnet_ip_address(instance):
  57. if isinstance(instance, six.string_types):
  58. if '/' not in instance:
  59. raise ValidationError("'{0}' 75 should be of the format 'IP_ADDRESS/CIDR'".format(instance))
  60. ip_address, cidr = instance.split('/')
  61. if re.match(VALID_IPV4_FORMAT, ip_address):
  62. if not (re.match(VALID_IPV4_CIDR_FORMAT, cidr) and
  63. all(0 <= int(component) <= 255 for component in ip_address.split("."))):
  64. raise ValidationError(
  65. "'{0}' 83 should be of the format 'IP_ADDRESS/CIDR'".format(instance))
  66. elif re.match(VALID_IPV6_CIDR_FORMAT, cidr) and hasattr(socket, "inet_pton"):
  67. try:
  68. if not (socket.inet_pton(socket.AF_INET6, ip_address)):
  69. raise ValidationError(
  70. "'{0}' 88 should be of the format 'IP_ADDRESS/CIDR'".format(instance))
  71. except socket.error as e:
  72. raise ValidationError(six.text_type(e))
  73. else:
  74. raise ValidationError("'{0}' 92 should be of the format 'IP_ADDRESS/CIDR'".format(instance))
  75. return True
  76. def match_named_volumes(service_dict, project_volumes):
  77. service_volumes = service_dict.get('volumes', [])
  78. for volume_spec in service_volumes:
  79. if volume_spec.is_named_volume and volume_spec.external not in project_volumes:
  80. raise ConfigurationError(
  81. 'Named volume "{0}" is used in service "{1}" but no'
  82. ' declaration was found in the volumes section.'.format(
  83. volume_spec.repr(), service_dict.get('name')
  84. )
  85. )
  86. def python_type_to_yaml_type(type_):
  87. type_name = type(type_).__name__
  88. return {
  89. 'dict': 'mapping',
  90. 'list': 'array',
  91. 'int': 'number',
  92. 'float': 'number',
  93. 'bool': 'boolean',
  94. 'unicode': 'string',
  95. 'str': 'string',
  96. 'bytes': 'string',
  97. }.get(type_name, type_name)
  98. def validate_config_section(filename, config, section):
  99. """Validate the structure of a configuration section. This must be done
  100. before interpolation so it's separate from schema validation.
  101. """
  102. if not isinstance(config, dict):
  103. raise ConfigurationError(
  104. "In file '{filename}', {section} must be a mapping, not "
  105. "{type}.".format(
  106. filename=filename,
  107. section=section,
  108. type=anglicize_json_type(python_type_to_yaml_type(config))))
  109. for key, value in config.items():
  110. if not isinstance(key, six.string_types):
  111. raise ConfigurationError(
  112. "In file '{filename}', the {section} name {name} must be a "
  113. "quoted string, i.e. '{name}'.".format(
  114. filename=filename,
  115. section=section,
  116. name=key))
  117. if not isinstance(value, (dict, type(None))):
  118. raise ConfigurationError(
  119. "In file '{filename}', {section} '{name}' must be a mapping not "
  120. "{type}.".format(
  121. filename=filename,
  122. section=section,
  123. name=key,
  124. type=anglicize_json_type(python_type_to_yaml_type(value))))
  125. def validate_top_level_object(config_file):
  126. if not isinstance(config_file.config, dict):
  127. raise ConfigurationError(
  128. "Top level object in '{}' needs to be an object not '{}'.".format(
  129. config_file.filename,
  130. type(config_file.config)))
  131. def validate_ulimits(service_config):
  132. ulimit_config = service_config.config.get('ulimits', {})
  133. for limit_name, soft_hard_values in six.iteritems(ulimit_config):
  134. if isinstance(soft_hard_values, dict):
  135. if not soft_hard_values['soft'] <= soft_hard_values['hard']:
  136. raise ConfigurationError(
  137. "Service '{s.name}' has invalid ulimit '{ulimit}'. "
  138. "'soft' value can not be greater than 'hard' value ".format(
  139. s=service_config,
  140. ulimit=ulimit_config))
  141. def validate_extends_file_path(service_name, extends_options, filename):
  142. """
  143. The service to be extended must either be defined in the config key 'file',
  144. or within 'filename'.
  145. """
  146. error_prefix = "Invalid 'extends' configuration for %s:" % service_name
  147. if 'file' not in extends_options and filename is None:
  148. raise ConfigurationError(
  149. "%s you need to specify a 'file', e.g. 'file: something.yml'" % error_prefix
  150. )
  151. def validate_network_mode(service_config, service_names):
  152. network_mode = service_config.config.get('network_mode')
  153. if not network_mode:
  154. return
  155. if 'networks' in service_config.config:
  156. raise ConfigurationError("'network_mode' and 'networks' cannot be combined")
  157. dependency = get_service_name_from_network_mode(network_mode)
  158. if not dependency:
  159. return
  160. if dependency not in service_names:
  161. raise ConfigurationError(
  162. "Service '{s.name}' uses the network stack of service '{dep}' which "
  163. "is undefined.".format(s=service_config, dep=dependency))
  164. def validate_pid_mode(service_config, service_names):
  165. pid_mode = service_config.config.get('pid')
  166. if not pid_mode:
  167. return
  168. dependency = get_service_name_from_network_mode(pid_mode)
  169. if not dependency:
  170. return
  171. if dependency not in service_names:
  172. raise ConfigurationError(
  173. "Service '{s.name}' uses the PID namespace of service '{dep}' which "
  174. "is undefined.".format(s=service_config, dep=dependency)
  175. )
  176. def validate_links(service_config, service_names):
  177. for link in service_config.config.get('links', []):
  178. if link.split(':')[0] not in service_names:
  179. raise ConfigurationError(
  180. "Service '{s.name}' has a link to service '{link}' which is "
  181. "undefined.".format(s=service_config, link=link))
  182. def validate_depends_on(service_config, service_names):
  183. deps = service_config.config.get('depends_on', {})
  184. for dependency in deps.keys():
  185. if dependency not in service_names:
  186. raise ConfigurationError(
  187. "Service '{s.name}' depends on service '{dep}' which is "
  188. "undefined.".format(s=service_config, dep=dependency)
  189. )
  190. def get_unsupported_config_msg(path, error_key):
  191. msg = "Unsupported config option for {}: '{}'".format(path_string(path), error_key)
  192. if error_key in DOCKER_CONFIG_HINTS:
  193. msg += " (did you mean '{}'?)".format(DOCKER_CONFIG_HINTS[error_key])
  194. return msg
  195. def anglicize_json_type(json_type):
  196. if json_type.startswith(('a', 'e', 'i', 'o', 'u')):
  197. return 'an ' + json_type
  198. return 'a ' + json_type
  199. def is_service_dict_schema(schema_id):
  200. return schema_id in ('config_schema_v1.json', '#/properties/services')
  201. def handle_error_for_schema_with_id(error, path):
  202. schema_id = error.schema['id']
  203. if is_service_dict_schema(schema_id) and error.validator == 'additionalProperties':
  204. return "Invalid service name '{}' - only {} characters are allowed".format(
  205. # The service_name is one of the keys in the json object
  206. [i for i in list(error.instance) if not i or any(filter(
  207. lambda c: not re.match(VALID_NAME_CHARS, c), i
  208. ))][0],
  209. VALID_NAME_CHARS
  210. )
  211. if error.validator == 'additionalProperties':
  212. if schema_id == '#/definitions/service':
  213. invalid_config_key = parse_key_from_error_msg(error)
  214. return get_unsupported_config_msg(path, invalid_config_key)
  215. if schema_id.startswith('config_schema_v'):
  216. invalid_config_key = parse_key_from_error_msg(error)
  217. return ('Invalid top-level property "{key}". Valid top-level '
  218. 'sections for this Compose file are: {properties}, and '
  219. 'extensions starting with "x-".\n\n{explanation}').format(
  220. key=invalid_config_key,
  221. properties=', '.join(error.schema['properties'].keys()),
  222. explanation=VERSION_EXPLANATION
  223. )
  224. if not error.path:
  225. return '{}\n\n{}'.format(error.message, VERSION_EXPLANATION)
  226. def handle_generic_error(error, path):
  227. msg_format = None
  228. error_msg = error.message
  229. if error.validator == 'oneOf':
  230. msg_format = "{path} {msg}"
  231. config_key, error_msg = _parse_oneof_validator(error)
  232. if config_key:
  233. path.append(config_key)
  234. elif error.validator == 'type':
  235. msg_format = "{path} contains an invalid type, it should be {msg}"
  236. error_msg = _parse_valid_types_from_validator(error.validator_value)
  237. elif error.validator == 'required':
  238. error_msg = ", ".join(error.validator_value)
  239. msg_format = "{path} is invalid, {msg} is required."
  240. elif error.validator == 'dependencies':
  241. config_key = list(error.validator_value.keys())[0]
  242. required_keys = ",".join(error.validator_value[config_key])
  243. msg_format = "{path} is invalid: {msg}"
  244. path.append(config_key)
  245. error_msg = "when defining '{}' you must set '{}' as well".format(
  246. config_key,
  247. required_keys)
  248. elif error.cause:
  249. error_msg = six.text_type(error.cause)
  250. msg_format = "{path} is invalid: {msg}"
  251. elif error.path:
  252. msg_format = "{path} value {msg}"
  253. if msg_format:
  254. return msg_format.format(path=path_string(path), msg=error_msg)
  255. return error.message
  256. def parse_key_from_error_msg(error):
  257. return error.message.split("'")[1]
  258. def path_string(path):
  259. return ".".join(c for c in path if isinstance(c, six.string_types))
  260. def _parse_valid_types_from_validator(validator):
  261. """A validator value can be either an array of valid types or a string of
  262. a valid type. Parse the valid types and prefix with the correct article.
  263. """
  264. if not isinstance(validator, list):
  265. return anglicize_json_type(validator)
  266. if len(validator) == 1:
  267. return anglicize_json_type(validator[0])
  268. return "{}, or {}".format(
  269. ", ".join([anglicize_json_type(validator[0])] + validator[1:-1]),
  270. anglicize_json_type(validator[-1]))
  271. def _parse_oneof_validator(error):
  272. """oneOf has multiple schemas, so we need to reason about which schema, sub
  273. schema or constraint the validation is failing on.
  274. Inspecting the context value of a ValidationError gives us information about
  275. which sub schema failed and which kind of error it is.
  276. """
  277. types = []
  278. for context in error.context:
  279. if context.validator == 'oneOf':
  280. _, error_msg = _parse_oneof_validator(context)
  281. return path_string(context.path), error_msg
  282. if context.validator == 'required':
  283. return (None, context.message)
  284. if context.validator == 'additionalProperties':
  285. invalid_config_key = parse_key_from_error_msg(context)
  286. return (None, "contains unsupported option: '{}'".format(invalid_config_key))
  287. if context.validator == 'uniqueItems':
  288. return (
  289. path_string(context.path) if context.path else None,
  290. "contains non-unique items, please remove duplicates from {}".format(
  291. context.instance),
  292. )
  293. if context.path:
  294. return (
  295. path_string(context.path),
  296. "contains {}, which is an invalid type, it should be {}".format(
  297. json.dumps(context.instance),
  298. _parse_valid_types_from_validator(context.validator_value)),
  299. )
  300. if context.validator == 'type':
  301. types.append(context.validator_value)
  302. valid_types = _parse_valid_types_from_validator(types)
  303. return (None, "contains an invalid type, it should be {}".format(valid_types))
  304. def process_service_constraint_errors(error, service_name, version):
  305. if version == V1:
  306. if 'image' in error.instance and 'build' in error.instance:
  307. return (
  308. "Service {} has both an image and build path specified. "
  309. "A service can either be built to image or use an existing "
  310. "image, not both.".format(service_name))
  311. if 'image' in error.instance and 'dockerfile' in error.instance:
  312. return (
  313. "Service {} has both an image and alternate Dockerfile. "
  314. "A service can either be built to image or use an existing "
  315. "image, not both.".format(service_name))
  316. if 'image' not in error.instance and 'build' not in error.instance:
  317. return (
  318. "Service {} has neither an image nor a build context specified. "
  319. "At least one must be provided.".format(service_name))
  320. def process_config_schema_errors(error):
  321. path = list(error.path)
  322. if 'id' in error.schema:
  323. error_msg = handle_error_for_schema_with_id(error, path)
  324. if error_msg:
  325. return error_msg
  326. return handle_generic_error(error, path)
  327. def validate_against_config_schema(config_file):
  328. schema = load_jsonschema(config_file)
  329. format_checker = FormatChecker(["ports", "expose", "subnet_ip_address"])
  330. validator = Draft4Validator(
  331. schema,
  332. resolver=RefResolver(get_resolver_path(), schema),
  333. format_checker=format_checker)
  334. handle_errors(
  335. validator.iter_errors(config_file.config),
  336. process_config_schema_errors,
  337. config_file.filename)
  338. def validate_service_constraints(config, service_name, config_file):
  339. def handler(errors):
  340. return process_service_constraint_errors(
  341. errors, service_name, config_file.version)
  342. schema = load_jsonschema(config_file)
  343. validator = Draft4Validator(schema['definitions']['constraints']['service'])
  344. handle_errors(validator.iter_errors(config), handler, None)
  345. def validate_cpu(service_config):
  346. cpus = service_config.config.get('cpus')
  347. if not cpus:
  348. return
  349. nano_cpus = cpus * NANOCPUS_SCALE
  350. if isinstance(nano_cpus, float) and not nano_cpus.is_integer():
  351. raise ConfigurationError(
  352. "cpus must have nine or less digits after decimal point")
  353. def get_schema_path():
  354. return os.path.dirname(os.path.abspath(__file__))
  355. def load_jsonschema(config_file):
  356. filename = os.path.join(
  357. get_schema_path(),
  358. "config_schema_v{0}.json".format(config_file.version))
  359. if not os.path.exists(filename):
  360. raise ConfigurationError(
  361. 'Version in "{}" is unsupported. {}'
  362. .format(config_file.filename, VERSION_EXPLANATION))
  363. with open(filename, "r") as fh:
  364. return json.load(fh)
  365. def get_resolver_path():
  366. schema_path = get_schema_path()
  367. if sys.platform == "win32":
  368. scheme = "///"
  369. # TODO: why is this necessary?
  370. schema_path = schema_path.replace('\\', '/')
  371. else:
  372. scheme = "//"
  373. return "file:{}{}/".format(scheme, schema_path)
  374. def handle_errors(errors, format_error_func, filename):
  375. """jsonschema returns an error tree full of information to explain what has
  376. gone wrong. Process each error and pull out relevant information and re-write
  377. helpful error messages that are relevant.
  378. """
  379. errors = list(sorted(errors, key=str))
  380. if not errors:
  381. return
  382. error_msg = '\n'.join(format_error_func(error) for error in errors)
  383. raise ConfigurationError(
  384. "The Compose file{file_msg} is invalid because:\n{error_msg}".format(
  385. file_msg=" '{}'".format(filename) if filename else "",
  386. error_msg=error_msg))
  387. def validate_healthcheck(service_config):
  388. healthcheck = service_config.config.get('healthcheck', {})
  389. if 'test' in healthcheck and isinstance(healthcheck['test'], list):
  390. if len(healthcheck['test']) == 0:
  391. raise ConfigurationError(
  392. 'Service "{}" defines an invalid healthcheck: '
  393. '"test" is an empty list'
  394. .format(service_config.name))
  395. # when disable is true config.py::process_healthcheck adds "test: ['NONE']" to service_config
  396. elif healthcheck['test'][0] == 'NONE' and len(healthcheck) > 1:
  397. raise ConfigurationError(
  398. 'Service "{}" defines an invalid healthcheck: '
  399. '"disable: true" cannot be combined with other options'
  400. .format(service_config.name))
  401. elif healthcheck['test'][0] not in ('NONE', 'CMD', 'CMD-SHELL'):
  402. raise ConfigurationError(
  403. 'Service "{}" defines an invalid healthcheck: '
  404. 'when "test" is a list the first item must be either NONE, CMD or CMD-SHELL'
  405. .format(service_config.name))