Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 1 | # Copyright 2019 The Chromium OS Authors. All rights reserved. |
| 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 5 | """Validation helpers for simple input validation in the API. |
| 6 | |
| 7 | Note: Every validator MUST respect config.do_validation. This is an internally |
| 8 | set config option that allows the mock call decorators to be placed before or |
| 9 | after the validation decorators, rather than forcing an ordering that could then |
| 10 | produce incorrect outputs if missed. |
| 11 | """ |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 12 | |
Alex Klein | 4de25e8 | 2019-08-05 15:58:39 -0600 | [diff] [blame] | 13 | import functools |
Chris McDonald | 1672ddb | 2021-07-21 11:48:23 -0600 | [diff] [blame] | 14 | import logging |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 15 | import os |
Alex Klein | bebccd5 | 2021-01-22 13:37:35 -0700 | [diff] [blame] | 16 | from typing import Callable, Iterable, List, Optional, Union |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 17 | |
Mike Frysinger | 2c02406 | 2021-05-22 15:43:22 -0400 | [diff] [blame] | 18 | from chromite.third_party.google.protobuf import message as protobuf_message |
Mike Frysinger | 849d640 | 2019-10-17 00:14:16 -0400 | [diff] [blame] | 19 | |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 20 | from chromite.lib import cros_build_lib |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 21 | |
Mike Frysinger | ef94e4c | 2020-02-10 23:59:54 -0500 | [diff] [blame] | 22 | |
Alex Klein | bebccd5 | 2021-01-22 13:37:35 -0700 | [diff] [blame] | 23 | def _value( |
| 24 | field: str, message: protobuf_message.Message |
| 25 | ) -> Union[bool, int, str, None, List, protobuf_message.Message]: |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 26 | """Helper function to fetch the value of the field. |
| 27 | |
| 28 | Args: |
Alex Klein | bebccd5 | 2021-01-22 13:37:35 -0700 | [diff] [blame] | 29 | field: The field name. Can be nested via . separation. |
| 30 | message: The protobuf message it is being fetched from. |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 31 | |
| 32 | Returns: |
Alex Klein | bebccd5 | 2021-01-22 13:37:35 -0700 | [diff] [blame] | 33 | The value of the field. |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 34 | """ |
Alex Klein | bdace30 | 2020-12-03 14:40:23 -0700 | [diff] [blame] | 35 | if not field: |
| 36 | return message |
| 37 | |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 38 | value = message |
| 39 | for part in field.split('.'): |
| 40 | if not isinstance(value, protobuf_message.Message): |
| 41 | value = None |
| 42 | break |
| 43 | |
| 44 | try: |
| 45 | value = getattr(value, part) |
| 46 | except AttributeError as e: |
Mike Frysinger | 6b5c3cd | 2019-08-27 16:51:00 -0400 | [diff] [blame] | 47 | cros_build_lib.Die('Invalid field: %s', e) |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 48 | |
| 49 | return value |
| 50 | |
Alex Klein | 69339cc | 2019-07-22 14:08:35 -0600 | [diff] [blame] | 51 | |
Mike Frysinger | 88e02c1 | 2019-10-01 15:05:36 -0400 | [diff] [blame] | 52 | # pylint: disable=docstring-misnamed-args |
Alex Klein | bebccd5 | 2021-01-22 13:37:35 -0700 | [diff] [blame] | 53 | def exists(*fields: str): |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 54 | """Validate that the paths in |fields| exist. |
| 55 | |
| 56 | Args: |
| 57 | fields (str): The fields being checked. Can be . separated nested |
| 58 | fields. |
| 59 | """ |
| 60 | assert fields |
| 61 | |
| 62 | def decorator(func): |
Alex Klein | 4de25e8 | 2019-08-05 15:58:39 -0600 | [diff] [blame] | 63 | @functools.wraps(func) |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 64 | def _exists(input_proto, output_proto, config, *args, **kwargs): |
| 65 | if config.do_validation: |
| 66 | for field in fields: |
| 67 | logging.debug('Validating %s exists.', field) |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 68 | |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 69 | value = _value(field, input_proto) |
| 70 | if not value or not os.path.exists(value): |
| 71 | cros_build_lib.Die('%s path does not exist: %s' % (field, value)) |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 72 | |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 73 | return func(input_proto, output_proto, config, *args, **kwargs) |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 74 | |
| 75 | return _exists |
| 76 | |
| 77 | return decorator |
| 78 | |
| 79 | |
Alex Klein | bebccd5 | 2021-01-22 13:37:35 -0700 | [diff] [blame] | 80 | def is_in(field: str, values: Iterable): |
Alex Klein | bdace30 | 2020-12-03 14:40:23 -0700 | [diff] [blame] | 81 | """Validate |field| is an element of |values|. |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 82 | |
| 83 | Args: |
Alex Klein | bebccd5 | 2021-01-22 13:37:35 -0700 | [diff] [blame] | 84 | field: The field being checked. May be . separated nested fields. |
| 85 | values: The possible values field may take. |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 86 | """ |
| 87 | assert field |
| 88 | assert values |
| 89 | |
| 90 | def decorator(func): |
Alex Klein | 4de25e8 | 2019-08-05 15:58:39 -0600 | [diff] [blame] | 91 | @functools.wraps(func) |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 92 | def _is_in(input_proto, output_proto, config, *args, **kwargs): |
| 93 | if config.do_validation: |
| 94 | logging.debug('Validating %s is in %r', field, values) |
| 95 | value = _value(field, input_proto) |
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 | if value not in values: |
| 98 | 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] | 99 | |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 100 | return func(input_proto, output_proto, config, *args, **kwargs) |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 101 | |
| 102 | return _is_in |
| 103 | |
| 104 | return decorator |
| 105 | |
| 106 | |
Alex Klein | bebccd5 | 2021-01-22 13:37:35 -0700 | [diff] [blame] | 107 | def each_in(field: str, |
| 108 | subfield: Optional[str], |
| 109 | values: Iterable, |
| 110 | optional: bool = False): |
Alex Klein | bdace30 | 2020-12-03 14:40:23 -0700 | [diff] [blame] | 111 | """Validate each |subfield| of the repeated |field| is in |values|. |
| 112 | |
| 113 | Args: |
Alex Klein | bebccd5 | 2021-01-22 13:37:35 -0700 | [diff] [blame] | 114 | field: The field being checked. May be . separated nested fields. |
| 115 | subfield: The field in the repeated |field| to validate, or None |
Alex Klein | bdace30 | 2020-12-03 14:40:23 -0700 | [diff] [blame] | 116 | when |field| is not a repeated message, e.g. enum, scalars. |
Alex Klein | bebccd5 | 2021-01-22 13:37:35 -0700 | [diff] [blame] | 117 | values: The possible values field may take. |
| 118 | optional: Also allow the field to be empty when True. |
Alex Klein | bdace30 | 2020-12-03 14:40:23 -0700 | [diff] [blame] | 119 | """ |
| 120 | assert field |
| 121 | assert values |
| 122 | |
| 123 | def decorator(func): |
| 124 | @functools.wraps(func) |
| 125 | def _is_in(input_proto, output_proto, config, *args, **kwargs): |
| 126 | if config.do_validation: |
| 127 | members = _value(field, input_proto) or [] |
| 128 | if not optional and not members: |
| 129 | cros_build_lib.Die('The %s field is empty.', field) |
| 130 | for member in members: |
| 131 | logging.debug('Validating %s.[each].%s is in %r.', field, subfield, |
| 132 | values) |
| 133 | value = _value(subfield, member) |
| 134 | if value not in values: |
| 135 | cros_build_lib.Die('%s.[each].%s (%r) must be in %r is required.', |
| 136 | field, subfield, value, values) |
| 137 | |
| 138 | return func(input_proto, output_proto, config, *args, **kwargs) |
| 139 | |
| 140 | return _is_in |
| 141 | |
| 142 | return decorator |
| 143 | |
| 144 | |
Sean McAllister | 17eed8d | 2021-09-21 10:41:16 -0600 | [diff] [blame] | 145 | def constraint(description): |
| 146 | """Define a function to be used as a constraint check. |
| 147 | |
| 148 | A constraint is a function that checks the value of a field and either |
| 149 | does nothing (returns None) or returns a string indicating why the value |
| 150 | isn't valid. |
| 151 | |
| 152 | We bind a human readable description to the constraint for error reporting |
| 153 | and logging. |
| 154 | |
| 155 | Args: |
| 156 | description: Human readable description of the constraint |
| 157 | """ |
| 158 | |
| 159 | def decorator(func): |
| 160 | @functools.wraps(func) |
| 161 | def _func(*args, **kwargs): |
| 162 | func(*args, **kwargs) |
| 163 | |
| 164 | setattr(_func, '__constraint_description__', description) |
| 165 | return _func |
| 166 | |
| 167 | return decorator |
| 168 | |
| 169 | |
| 170 | def check_constraint(field: str, checkfunc: Callable): |
| 171 | """Validate all values of |field| pass a constraint. |
| 172 | |
| 173 | Args: |
| 174 | field: The field being checked. May be . separated nested fields. |
| 175 | checkfunc: A constraint function to check on each value |
| 176 | """ |
| 177 | assert field |
| 178 | assert constraint |
| 179 | |
| 180 | # Get description for the constraint if it's set |
| 181 | constraint_description = getattr( |
| 182 | checkfunc, |
| 183 | '__constraint_description__', |
| 184 | checkfunc.__name__, |
| 185 | ) |
| 186 | |
| 187 | def decorator(func): |
| 188 | @functools.wraps(func) |
| 189 | def _check_constraint(input_proto, output_proto, config, *args, **kwargs): |
| 190 | if config.do_validation: |
| 191 | values = _value(field, input_proto) or [] |
| 192 | |
| 193 | failed = [] |
| 194 | for val in values: |
| 195 | msg = checkfunc(val) |
| 196 | if msg is not None: |
| 197 | failed.append((val, msg)) |
| 198 | |
| 199 | if failed: |
Lizzy Presland | b2d6064 | 2021-11-12 01:41:58 +0000 | [diff] [blame] | 200 | msg = f'{field}.[all] one or more values failed check ' \ |
| 201 | f'"{constraint_description}"\n' |
Sean McAllister | 17eed8d | 2021-09-21 10:41:16 -0600 | [diff] [blame] | 202 | |
| 203 | for value, msg in failed: |
Lizzy Presland | b2d6064 | 2021-11-12 01:41:58 +0000 | [diff] [blame] | 204 | msg += ' %s: %s\n' % (value, msg) |
Sean McAllister | 17eed8d | 2021-09-21 10:41:16 -0600 | [diff] [blame] | 205 | cros_build_lib.Die(msg) |
| 206 | |
| 207 | return func(input_proto, output_proto, config, *args, **kwargs) |
| 208 | |
| 209 | return _check_constraint |
| 210 | |
| 211 | return decorator |
| 212 | |
| 213 | |
Mike Frysinger | 88e02c1 | 2019-10-01 15:05:36 -0400 | [diff] [blame] | 214 | # pylint: disable=docstring-misnamed-args |
Alex Klein | bebccd5 | 2021-01-22 13:37:35 -0700 | [diff] [blame] | 215 | def require(*fields: str): |
Greg Edelston | dc94107 | 2021-08-11 12:32:30 -0600 | [diff] [blame] | 216 | """Verify |fields| have all been set to truthy values. |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 217 | |
| 218 | Args: |
Alex Klein | bebccd5 | 2021-01-22 13:37:35 -0700 | [diff] [blame] | 219 | fields: The fields being checked. May be . separated nested fields. |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 220 | """ |
| 221 | assert fields |
| 222 | |
| 223 | def decorator(func): |
Alex Klein | 4de25e8 | 2019-08-05 15:58:39 -0600 | [diff] [blame] | 224 | @functools.wraps(func) |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 225 | def _require(input_proto, output_proto, config, *args, **kwargs): |
| 226 | if config.do_validation: |
| 227 | for field in fields: |
| 228 | logging.debug('Validating %s is set.', field) |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 229 | |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 230 | value = _value(field, input_proto) |
| 231 | if not value: |
| 232 | cros_build_lib.Die('%s is required.', field) |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 233 | |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 234 | return func(input_proto, output_proto, config, *args, **kwargs) |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 235 | |
| 236 | return _require |
| 237 | |
| 238 | return decorator |
Alex Klein | 69339cc | 2019-07-22 14:08:35 -0600 | [diff] [blame] | 239 | |
| 240 | |
Alex Klein | 60c8052 | 2020-10-13 18:05:38 -0600 | [diff] [blame] | 241 | # pylint: disable=docstring-misnamed-args |
Alex Klein | bebccd5 | 2021-01-22 13:37:35 -0700 | [diff] [blame] | 242 | def require_any(*fields: str): |
Alex Klein | 60c8052 | 2020-10-13 18:05:38 -0600 | [diff] [blame] | 243 | """Verify at least one of |fields| have been set. |
| 244 | |
| 245 | Args: |
Alex Klein | bebccd5 | 2021-01-22 13:37:35 -0700 | [diff] [blame] | 246 | fields: The fields being checked. May be . separated nested fields. |
Alex Klein | 60c8052 | 2020-10-13 18:05:38 -0600 | [diff] [blame] | 247 | """ |
| 248 | assert fields |
| 249 | |
| 250 | def decorator(func): |
| 251 | @functools.wraps(func) |
| 252 | def _require(input_proto, output_proto, config, *args, **kwargs): |
| 253 | if config.do_validation: |
| 254 | for field in fields: |
| 255 | logging.debug('Validating %s is set.', field) |
| 256 | value = _value(field, input_proto) |
| 257 | if value: |
| 258 | break |
| 259 | else: |
| 260 | cros_build_lib.Die('At least one of the following must be set: %s', |
| 261 | ', '.join(fields)) |
| 262 | |
| 263 | return func(input_proto, output_proto, config, *args, **kwargs) |
| 264 | |
| 265 | return _require |
| 266 | |
| 267 | return decorator |
| 268 | |
| 269 | |
Alex Klein | bebccd5 | 2021-01-22 13:37:35 -0700 | [diff] [blame] | 270 | def require_each(field: str, |
| 271 | subfields: Iterable[str], |
| 272 | allow_empty: bool = True): |
Alex Klein | 86242bf | 2020-09-22 15:23:23 -0600 | [diff] [blame] | 273 | """Verify |field| each have all of the |subfields| set. |
| 274 | |
| 275 | When |allow_empty| is True, |field| may be empty, and |subfields| are only |
| 276 | validated when it is not empty. When |allow_empty| is False, |field| must |
| 277 | also have at least one entry. |
| 278 | |
| 279 | Args: |
Alex Klein | bebccd5 | 2021-01-22 13:37:35 -0700 | [diff] [blame] | 280 | field: The repeated field being checked. May be . separated nested |
Alex Klein | 86242bf | 2020-09-22 15:23:23 -0600 | [diff] [blame] | 281 | fields. |
Alex Klein | bebccd5 | 2021-01-22 13:37:35 -0700 | [diff] [blame] | 282 | subfields: The fields of the repeated message to validate. |
| 283 | allow_empty: Also require at least one entry in the repeated field. |
Alex Klein | 86242bf | 2020-09-22 15:23:23 -0600 | [diff] [blame] | 284 | """ |
| 285 | assert field |
| 286 | assert subfields |
| 287 | assert not isinstance(subfields, str) |
| 288 | |
| 289 | def decorator(func): |
| 290 | @functools.wraps(func) |
| 291 | def _require_each(input_proto, output_proto, config, *args, **kwargs): |
| 292 | if config.do_validation: |
| 293 | members = _value(field, input_proto) or [] |
| 294 | if not allow_empty and not members: |
| 295 | cros_build_lib.Die('The %s field is empty.', field) |
| 296 | for member in members: |
| 297 | for subfield in subfields: |
| 298 | logging.debug('Validating %s.[each].%s is set.', field, subfield) |
| 299 | value = _value(subfield, member) |
| 300 | if not value: |
| 301 | cros_build_lib.Die('%s is required.', field) |
| 302 | |
| 303 | return func(input_proto, output_proto, config, *args, **kwargs) |
| 304 | |
| 305 | return _require_each |
| 306 | |
| 307 | return decorator |
| 308 | |
| 309 | |
Alex Klein | bebccd5 | 2021-01-22 13:37:35 -0700 | [diff] [blame] | 310 | def validation_complete(func: Callable): |
Alex Klein | 69339cc | 2019-07-22 14:08:35 -0600 | [diff] [blame] | 311 | """Automatically skip the endpoint when called after all other validators. |
| 312 | |
| 313 | This decorator MUST be applied after all other validate decorators. |
| 314 | The config can be checked manually if there is non-decorator validation, but |
| 315 | this is much cleaner if it is all done in decorators. |
| 316 | """ |
Alex Klein | 4de25e8 | 2019-08-05 15:58:39 -0600 | [diff] [blame] | 317 | |
| 318 | @functools.wraps(func) |
Alex Klein | 69339cc | 2019-07-22 14:08:35 -0600 | [diff] [blame] | 319 | def _validate_only(request, response, configs, *args, **kwargs): |
| 320 | if configs.validate_only: |
| 321 | # Avoid calling the endpoint. |
| 322 | return 0 |
| 323 | else: |
| 324 | return func(request, response, configs, *args, **kwargs) |
| 325 | |
| 326 | return _validate_only |