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 |
Mike Frysinger | ef94e4c | 2020-02-10 23:59:54 -0500 | [diff] [blame] | 18 | import sys |
Alex Klein | bebccd5 | 2021-01-22 13:37:35 -0700 | [diff] [blame^] | 19 | from typing import Callable, Iterable, List, Optional, Union |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 20 | |
Mike Frysinger | 849d640 | 2019-10-17 00:14:16 -0400 | [diff] [blame] | 21 | from google.protobuf import message as protobuf_message |
| 22 | |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 23 | from chromite.lib import cros_build_lib |
| 24 | from chromite.lib import cros_logging as logging |
| 25 | |
Mike Frysinger | ef94e4c | 2020-02-10 23:59:54 -0500 | [diff] [blame] | 26 | assert sys.version_info >= (3, 6), 'This module requires Python 3.6+' |
| 27 | |
| 28 | |
Alex Klein | bebccd5 | 2021-01-22 13:37:35 -0700 | [diff] [blame^] | 29 | def _value( |
| 30 | field: str, message: protobuf_message.Message |
| 31 | ) -> Union[bool, int, str, None, List, protobuf_message.Message]: |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 32 | """Helper function to fetch the value of the field. |
| 33 | |
| 34 | Args: |
Alex Klein | bebccd5 | 2021-01-22 13:37:35 -0700 | [diff] [blame^] | 35 | field: The field name. Can be nested via . separation. |
| 36 | message: The protobuf message it is being fetched from. |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 37 | |
| 38 | Returns: |
Alex Klein | bebccd5 | 2021-01-22 13:37:35 -0700 | [diff] [blame^] | 39 | The value of the field. |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 40 | """ |
Alex Klein | bdace30 | 2020-12-03 14:40:23 -0700 | [diff] [blame] | 41 | if not field: |
| 42 | return message |
| 43 | |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 44 | value = message |
| 45 | for part in field.split('.'): |
| 46 | if not isinstance(value, protobuf_message.Message): |
| 47 | value = None |
| 48 | break |
| 49 | |
| 50 | try: |
| 51 | value = getattr(value, part) |
| 52 | except AttributeError as e: |
Mike Frysinger | 6b5c3cd | 2019-08-27 16:51:00 -0400 | [diff] [blame] | 53 | cros_build_lib.Die('Invalid field: %s', e) |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 54 | |
| 55 | return value |
| 56 | |
Alex Klein | 69339cc | 2019-07-22 14:08:35 -0600 | [diff] [blame] | 57 | |
Mike Frysinger | 88e02c1 | 2019-10-01 15:05:36 -0400 | [diff] [blame] | 58 | # pylint: disable=docstring-misnamed-args |
Alex Klein | bebccd5 | 2021-01-22 13:37:35 -0700 | [diff] [blame^] | 59 | def exists(*fields: str): |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 60 | """Validate that the paths in |fields| exist. |
| 61 | |
| 62 | Args: |
| 63 | fields (str): The fields being checked. Can be . separated nested |
| 64 | fields. |
| 65 | """ |
| 66 | assert fields |
| 67 | |
| 68 | def decorator(func): |
Alex Klein | 4de25e8 | 2019-08-05 15:58:39 -0600 | [diff] [blame] | 69 | @functools.wraps(func) |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 70 | def _exists(input_proto, output_proto, config, *args, **kwargs): |
| 71 | if config.do_validation: |
| 72 | for field in fields: |
| 73 | logging.debug('Validating %s exists.', field) |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 74 | |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 75 | value = _value(field, input_proto) |
| 76 | if not value or not os.path.exists(value): |
| 77 | cros_build_lib.Die('%s path does not exist: %s' % (field, value)) |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 78 | |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 79 | return func(input_proto, output_proto, config, *args, **kwargs) |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 80 | |
| 81 | return _exists |
| 82 | |
| 83 | return decorator |
| 84 | |
| 85 | |
Alex Klein | bebccd5 | 2021-01-22 13:37:35 -0700 | [diff] [blame^] | 86 | def is_in(field: str, values: Iterable): |
Alex Klein | bdace30 | 2020-12-03 14:40:23 -0700 | [diff] [blame] | 87 | """Validate |field| is an element of |values|. |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 88 | |
| 89 | Args: |
Alex Klein | bebccd5 | 2021-01-22 13:37:35 -0700 | [diff] [blame^] | 90 | field: The field being checked. May be . separated nested fields. |
| 91 | values: The possible values field may take. |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 92 | """ |
| 93 | assert field |
| 94 | assert values |
| 95 | |
| 96 | def decorator(func): |
Alex Klein | 4de25e8 | 2019-08-05 15:58:39 -0600 | [diff] [blame] | 97 | @functools.wraps(func) |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 98 | def _is_in(input_proto, output_proto, config, *args, **kwargs): |
| 99 | if config.do_validation: |
| 100 | logging.debug('Validating %s is in %r', field, values) |
| 101 | value = _value(field, input_proto) |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 102 | |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 103 | if value not in values: |
| 104 | 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] | 105 | |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 106 | return func(input_proto, output_proto, config, *args, **kwargs) |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 107 | |
| 108 | return _is_in |
| 109 | |
| 110 | return decorator |
| 111 | |
| 112 | |
Alex Klein | bebccd5 | 2021-01-22 13:37:35 -0700 | [diff] [blame^] | 113 | def each_in(field: str, |
| 114 | subfield: Optional[str], |
| 115 | values: Iterable, |
| 116 | optional: bool = False): |
Alex Klein | bdace30 | 2020-12-03 14:40:23 -0700 | [diff] [blame] | 117 | """Validate each |subfield| of the repeated |field| is in |values|. |
| 118 | |
| 119 | Args: |
Alex Klein | bebccd5 | 2021-01-22 13:37:35 -0700 | [diff] [blame^] | 120 | field: The field being checked. May be . separated nested fields. |
| 121 | subfield: The field in the repeated |field| to validate, or None |
Alex Klein | bdace30 | 2020-12-03 14:40:23 -0700 | [diff] [blame] | 122 | when |field| is not a repeated message, e.g. enum, scalars. |
Alex Klein | bebccd5 | 2021-01-22 13:37:35 -0700 | [diff] [blame^] | 123 | values: The possible values field may take. |
| 124 | optional: Also allow the field to be empty when True. |
Alex Klein | bdace30 | 2020-12-03 14:40:23 -0700 | [diff] [blame] | 125 | """ |
| 126 | assert field |
| 127 | assert values |
| 128 | |
| 129 | def decorator(func): |
| 130 | @functools.wraps(func) |
| 131 | def _is_in(input_proto, output_proto, config, *args, **kwargs): |
| 132 | if config.do_validation: |
| 133 | members = _value(field, input_proto) or [] |
| 134 | if not optional and not members: |
| 135 | cros_build_lib.Die('The %s field is empty.', field) |
| 136 | for member in members: |
| 137 | logging.debug('Validating %s.[each].%s is in %r.', field, subfield, |
| 138 | values) |
| 139 | value = _value(subfield, member) |
| 140 | if value not in values: |
| 141 | cros_build_lib.Die('%s.[each].%s (%r) must be in %r is required.', |
| 142 | field, subfield, value, values) |
| 143 | |
| 144 | return func(input_proto, output_proto, config, *args, **kwargs) |
| 145 | |
| 146 | return _is_in |
| 147 | |
| 148 | return decorator |
| 149 | |
| 150 | |
Mike Frysinger | 88e02c1 | 2019-10-01 15:05:36 -0400 | [diff] [blame] | 151 | # pylint: disable=docstring-misnamed-args |
Alex Klein | bebccd5 | 2021-01-22 13:37:35 -0700 | [diff] [blame^] | 152 | def require(*fields: str): |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 153 | """Verify |fields| have all been set. |
| 154 | |
| 155 | Args: |
Alex Klein | bebccd5 | 2021-01-22 13:37:35 -0700 | [diff] [blame^] | 156 | fields: The fields being checked. May be . separated nested fields. |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 157 | """ |
| 158 | assert fields |
| 159 | |
| 160 | def decorator(func): |
Alex Klein | 4de25e8 | 2019-08-05 15:58:39 -0600 | [diff] [blame] | 161 | @functools.wraps(func) |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 162 | def _require(input_proto, output_proto, config, *args, **kwargs): |
| 163 | if config.do_validation: |
| 164 | for field in fields: |
| 165 | logging.debug('Validating %s is set.', field) |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 166 | |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 167 | value = _value(field, input_proto) |
| 168 | if not value: |
| 169 | cros_build_lib.Die('%s is required.', field) |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 170 | |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 171 | return func(input_proto, output_proto, config, *args, **kwargs) |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 172 | |
| 173 | return _require |
| 174 | |
| 175 | return decorator |
Alex Klein | 69339cc | 2019-07-22 14:08:35 -0600 | [diff] [blame] | 176 | |
| 177 | |
Alex Klein | 60c8052 | 2020-10-13 18:05:38 -0600 | [diff] [blame] | 178 | # pylint: disable=docstring-misnamed-args |
Alex Klein | bebccd5 | 2021-01-22 13:37:35 -0700 | [diff] [blame^] | 179 | def require_any(*fields: str): |
Alex Klein | 60c8052 | 2020-10-13 18:05:38 -0600 | [diff] [blame] | 180 | """Verify at least one of |fields| have been set. |
| 181 | |
| 182 | Args: |
Alex Klein | bebccd5 | 2021-01-22 13:37:35 -0700 | [diff] [blame^] | 183 | fields: The fields being checked. May be . separated nested fields. |
Alex Klein | 60c8052 | 2020-10-13 18:05:38 -0600 | [diff] [blame] | 184 | """ |
| 185 | assert fields |
| 186 | |
| 187 | def decorator(func): |
| 188 | @functools.wraps(func) |
| 189 | def _require(input_proto, output_proto, config, *args, **kwargs): |
| 190 | if config.do_validation: |
| 191 | for field in fields: |
| 192 | logging.debug('Validating %s is set.', field) |
| 193 | value = _value(field, input_proto) |
| 194 | if value: |
| 195 | break |
| 196 | else: |
| 197 | cros_build_lib.Die('At least one of the following must be set: %s', |
| 198 | ', '.join(fields)) |
| 199 | |
| 200 | return func(input_proto, output_proto, config, *args, **kwargs) |
| 201 | |
| 202 | return _require |
| 203 | |
| 204 | return decorator |
| 205 | |
| 206 | |
Alex Klein | bebccd5 | 2021-01-22 13:37:35 -0700 | [diff] [blame^] | 207 | def require_each(field: str, |
| 208 | subfields: Iterable[str], |
| 209 | allow_empty: bool = True): |
Alex Klein | 86242bf | 2020-09-22 15:23:23 -0600 | [diff] [blame] | 210 | """Verify |field| each have all of the |subfields| set. |
| 211 | |
| 212 | When |allow_empty| is True, |field| may be empty, and |subfields| are only |
| 213 | validated when it is not empty. When |allow_empty| is False, |field| must |
| 214 | also have at least one entry. |
| 215 | |
| 216 | Args: |
Alex Klein | bebccd5 | 2021-01-22 13:37:35 -0700 | [diff] [blame^] | 217 | field: The repeated field being checked. May be . separated nested |
Alex Klein | 86242bf | 2020-09-22 15:23:23 -0600 | [diff] [blame] | 218 | fields. |
Alex Klein | bebccd5 | 2021-01-22 13:37:35 -0700 | [diff] [blame^] | 219 | subfields: The fields of the repeated message to validate. |
| 220 | allow_empty: Also require at least one entry in the repeated field. |
Alex Klein | 86242bf | 2020-09-22 15:23:23 -0600 | [diff] [blame] | 221 | """ |
| 222 | assert field |
| 223 | assert subfields |
| 224 | assert not isinstance(subfields, str) |
| 225 | |
| 226 | def decorator(func): |
| 227 | @functools.wraps(func) |
| 228 | def _require_each(input_proto, output_proto, config, *args, **kwargs): |
| 229 | if config.do_validation: |
| 230 | members = _value(field, input_proto) or [] |
| 231 | if not allow_empty and not members: |
| 232 | cros_build_lib.Die('The %s field is empty.', field) |
| 233 | for member in members: |
| 234 | for subfield in subfields: |
| 235 | logging.debug('Validating %s.[each].%s is set.', field, subfield) |
| 236 | value = _value(subfield, member) |
| 237 | if not value: |
| 238 | cros_build_lib.Die('%s is required.', field) |
| 239 | |
| 240 | return func(input_proto, output_proto, config, *args, **kwargs) |
| 241 | |
| 242 | return _require_each |
| 243 | |
| 244 | return decorator |
| 245 | |
| 246 | |
Alex Klein | bebccd5 | 2021-01-22 13:37:35 -0700 | [diff] [blame^] | 247 | def validation_complete(func: Callable): |
Alex Klein | 69339cc | 2019-07-22 14:08:35 -0600 | [diff] [blame] | 248 | """Automatically skip the endpoint when called after all other validators. |
| 249 | |
| 250 | This decorator MUST be applied after all other validate decorators. |
| 251 | The config can be checked manually if there is non-decorator validation, but |
| 252 | this is much cleaner if it is all done in decorators. |
| 253 | """ |
Alex Klein | 4de25e8 | 2019-08-05 15:58:39 -0600 | [diff] [blame] | 254 | |
| 255 | @functools.wraps(func) |
Alex Klein | 69339cc | 2019-07-22 14:08:35 -0600 | [diff] [blame] | 256 | def _validate_only(request, response, configs, *args, **kwargs): |
| 257 | if configs.validate_only: |
| 258 | # Avoid calling the endpoint. |
| 259 | return 0 |
| 260 | else: |
| 261 | return func(request, response, configs, *args, **kwargs) |
| 262 | |
| 263 | return _validate_only |