Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | # Copyright 2019 The Chromium OS Authors. All rights reserved. |
| 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame^] | 6 | """Validation helpers for simple input validation in the API. |
| 7 | |
| 8 | Note: Every validator MUST respect config.do_validation. This is an internally |
| 9 | set config option that allows the mock call decorators to be placed before or |
| 10 | after the validation decorators, rather than forcing an ordering that could then |
| 11 | produce incorrect outputs if missed. |
| 12 | """ |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 13 | |
| 14 | from __future__ import print_function |
| 15 | |
Alex Klein | 4de25e8 | 2019-08-05 15:58:39 -0600 | [diff] [blame] | 16 | import functools |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 17 | import os |
| 18 | |
| 19 | from chromite.lib import cros_build_lib |
| 20 | from chromite.lib import cros_logging as logging |
| 21 | |
| 22 | from google.protobuf import message as protobuf_message |
| 23 | |
| 24 | |
| 25 | def _value(field, message): |
| 26 | """Helper function to fetch the value of the field. |
| 27 | |
| 28 | Args: |
| 29 | field (str): The field name. Can be nested via . separation. |
| 30 | message (Message): The protobuf message it is being fetched from. |
| 31 | |
| 32 | Returns: |
| 33 | str|None|int|list|Message|bool - The value of the field. |
| 34 | """ |
| 35 | value = message |
| 36 | for part in field.split('.'): |
| 37 | if not isinstance(value, protobuf_message.Message): |
| 38 | value = None |
| 39 | break |
| 40 | |
| 41 | try: |
| 42 | value = getattr(value, part) |
| 43 | except AttributeError as e: |
| 44 | cros_build_lib.Die('Invalid field: %s', e.message) |
| 45 | |
| 46 | return value |
| 47 | |
Alex Klein | 69339cc | 2019-07-22 14:08:35 -0600 | [diff] [blame] | 48 | |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 49 | #pylint: disable=docstring-misnamed-args |
| 50 | def exists(*fields): |
| 51 | """Validate that the paths in |fields| exist. |
| 52 | |
| 53 | Args: |
| 54 | fields (str): The fields being checked. Can be . separated nested |
| 55 | fields. |
| 56 | """ |
| 57 | assert fields |
| 58 | |
| 59 | def decorator(func): |
Alex Klein | 4de25e8 | 2019-08-05 15:58:39 -0600 | [diff] [blame] | 60 | @functools.wraps(func) |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame^] | 61 | def _exists(input_proto, output_proto, config, *args, **kwargs): |
| 62 | if config.do_validation: |
| 63 | for field in fields: |
| 64 | logging.debug('Validating %s exists.', field) |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 65 | |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame^] | 66 | value = _value(field, input_proto) |
| 67 | if not value or not os.path.exists(value): |
| 68 | cros_build_lib.Die('%s path does not exist: %s' % (field, value)) |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 69 | |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame^] | 70 | return func(input_proto, output_proto, config, *args, **kwargs) |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 71 | |
| 72 | return _exists |
| 73 | |
| 74 | return decorator |
| 75 | |
| 76 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 77 | def is_in(field, values): |
| 78 | """Validate |field| does not contain |value|. |
| 79 | |
| 80 | Args: |
| 81 | field (str): The field being checked. May be . separated nested fields. |
| 82 | values (list): The possible values field may take. |
| 83 | """ |
| 84 | assert field |
| 85 | assert values |
| 86 | |
| 87 | def decorator(func): |
Alex Klein | 4de25e8 | 2019-08-05 15:58:39 -0600 | [diff] [blame] | 88 | @functools.wraps(func) |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame^] | 89 | def _is_in(input_proto, output_proto, config, *args, **kwargs): |
| 90 | if config.do_validation: |
| 91 | logging.debug('Validating %s is in %r', field, values) |
| 92 | value = _value(field, input_proto) |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 93 | |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame^] | 94 | if value not in values: |
| 95 | cros_build_lib.Die('%s (%r) must be in %r', field, value, values) |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 96 | |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame^] | 97 | return func(input_proto, output_proto, config, *args, **kwargs) |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 98 | |
| 99 | return _is_in |
| 100 | |
| 101 | return decorator |
| 102 | |
| 103 | |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 104 | #pylint: disable=docstring-misnamed-args |
| 105 | def require(*fields): |
| 106 | """Verify |fields| have all been set. |
| 107 | |
| 108 | Args: |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 109 | fields (str): The fields being checked. May be . separated nested fields. |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 110 | """ |
| 111 | assert fields |
| 112 | |
| 113 | def decorator(func): |
Alex Klein | 4de25e8 | 2019-08-05 15:58:39 -0600 | [diff] [blame] | 114 | @functools.wraps(func) |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame^] | 115 | def _require(input_proto, output_proto, config, *args, **kwargs): |
| 116 | if config.do_validation: |
| 117 | for field in fields: |
| 118 | logging.debug('Validating %s is set.', field) |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 119 | |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame^] | 120 | value = _value(field, input_proto) |
| 121 | if not value: |
| 122 | cros_build_lib.Die('%s is required.', field) |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 123 | |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame^] | 124 | return func(input_proto, output_proto, config, *args, **kwargs) |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 125 | |
| 126 | return _require |
| 127 | |
| 128 | return decorator |
Alex Klein | 69339cc | 2019-07-22 14:08:35 -0600 | [diff] [blame] | 129 | |
| 130 | |
| 131 | def validation_complete(func): |
| 132 | """Automatically skip the endpoint when called after all other validators. |
| 133 | |
| 134 | This decorator MUST be applied after all other validate decorators. |
| 135 | The config can be checked manually if there is non-decorator validation, but |
| 136 | this is much cleaner if it is all done in decorators. |
| 137 | """ |
Alex Klein | 4de25e8 | 2019-08-05 15:58:39 -0600 | [diff] [blame] | 138 | |
| 139 | @functools.wraps(func) |
Alex Klein | 69339cc | 2019-07-22 14:08:35 -0600 | [diff] [blame] | 140 | def _validate_only(request, response, configs, *args, **kwargs): |
| 141 | if configs.validate_only: |
| 142 | # Avoid calling the endpoint. |
| 143 | return 0 |
| 144 | else: |
| 145 | return func(request, response, configs, *args, **kwargs) |
| 146 | |
| 147 | return _validate_only |