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 | |
Mike Frysinger | 17844a0 | 2019-08-24 18:21:02 -0400 | [diff] [blame] | 22 | # TODO(vapier): Re-enable check once we upgrade to pylint-1.8+. |
| 23 | # pylint: disable=no-name-in-module |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 24 | from google.protobuf import message as protobuf_message |
Mike Frysinger | 17844a0 | 2019-08-24 18:21:02 -0400 | [diff] [blame] | 25 | # pylint: enable=no-name-in-module |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 26 | |
| 27 | |
| 28 | def _value(field, message): |
| 29 | """Helper function to fetch the value of the field. |
| 30 | |
| 31 | Args: |
| 32 | field (str): The field name. Can be nested via . separation. |
| 33 | message (Message): The protobuf message it is being fetched from. |
| 34 | |
| 35 | Returns: |
| 36 | str|None|int|list|Message|bool - The value of the field. |
| 37 | """ |
| 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 | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 53 | def exists(*fields): |
| 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 | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 80 | def is_in(field, values): |
| 81 | """Validate |field| does not contain |value|. |
| 82 | |
| 83 | Args: |
| 84 | field (str): The field being checked. May be . separated nested fields. |
| 85 | values (list): The possible values field may take. |
| 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 | |
Mike Frysinger | 88e02c1 | 2019-10-01 15:05:36 -0400 | [diff] [blame] | 107 | # pylint: disable=docstring-misnamed-args |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 108 | def require(*fields): |
| 109 | """Verify |fields| have all been set. |
| 110 | |
| 111 | Args: |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 112 | fields (str): The fields being checked. May be . separated nested fields. |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 113 | """ |
| 114 | assert fields |
| 115 | |
| 116 | def decorator(func): |
Alex Klein | 4de25e8 | 2019-08-05 15:58:39 -0600 | [diff] [blame] | 117 | @functools.wraps(func) |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 118 | def _require(input_proto, output_proto, config, *args, **kwargs): |
| 119 | if config.do_validation: |
| 120 | for field in fields: |
| 121 | logging.debug('Validating %s is set.', field) |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 122 | |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 123 | value = _value(field, input_proto) |
| 124 | if not value: |
| 125 | cros_build_lib.Die('%s is required.', field) |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 126 | |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 127 | return func(input_proto, output_proto, config, *args, **kwargs) |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 128 | |
| 129 | return _require |
| 130 | |
| 131 | return decorator |
Alex Klein | 69339cc | 2019-07-22 14:08:35 -0600 | [diff] [blame] | 132 | |
| 133 | |
| 134 | def validation_complete(func): |
| 135 | """Automatically skip the endpoint when called after all other validators. |
| 136 | |
| 137 | This decorator MUST be applied after all other validate decorators. |
| 138 | The config can be checked manually if there is non-decorator validation, but |
| 139 | this is much cleaner if it is all done in decorators. |
| 140 | """ |
Alex Klein | 4de25e8 | 2019-08-05 15:58:39 -0600 | [diff] [blame] | 141 | |
| 142 | @functools.wraps(func) |
Alex Klein | 69339cc | 2019-07-22 14:08:35 -0600 | [diff] [blame] | 143 | def _validate_only(request, response, configs, *args, **kwargs): |
| 144 | if configs.validate_only: |
| 145 | # Avoid calling the endpoint. |
| 146 | return 0 |
| 147 | else: |
| 148 | return func(request, response, configs, *args, **kwargs) |
| 149 | |
| 150 | return _validate_only |