validation.py 19 KB

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