| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
| 12 | # implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 16 | from http import HTTPStatus |
| 17 | |
| sousaedu | 049cbb1 | 2022-01-05 11:39:35 +0000 | [diff] [blame] | 18 | from jsonschema import exceptions as js_e, validate as js_v |
| 19 | |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 20 | __author__ = "Alfonso Tierno <alfonso.tiernosepulveda@telefonica.com>" |
| 21 | __version__ = "0.1" |
| 22 | version_date = "Jun 2020" |
| 23 | |
| 24 | """ |
| 25 | Validator of input data using JSON schemas |
| 26 | """ |
| 27 | |
| 28 | # Basis schemas |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 29 | name_schema = { |
| 30 | "type": "string", |
| 31 | "minLength": 1, |
| 32 | "maxLength": 255, |
| 33 | "pattern": "^[^,;()'\"]+$", |
| 34 | } |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 35 | string_schema = {"type": "string", "minLength": 1, "maxLength": 255} |
| 36 | ssh_key_schema = {"type": "string", "minLength": 1} |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 37 | id_schema = { |
| 38 | "type": "string", |
| 39 | "pattern": "^[a-fA-F0-9]{8}(-[a-fA-F0-9]{4}){3}-[a-fA-F0-9]{12}$", |
| 40 | } |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 41 | bool_schema = {"type": "boolean"} |
| 42 | null_schema = {"type": "null"} |
| tierno | 70eeb18 | 2020-10-19 16:38:00 +0000 | [diff] [blame] | 43 | object_schema = {"type": "object"} |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 44 | |
| tierno | 70eeb18 | 2020-10-19 16:38:00 +0000 | [diff] [blame] | 45 | deploy_item_schema = { |
| 46 | "title": "deploy item validation. Each vld, vdu, flavor, image, ...", |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 47 | "$schema": "http://json-schema.org/draft-04/schema#", |
| 48 | "type": "object", |
| tierno | 70eeb18 | 2020-10-19 16:38:00 +0000 | [diff] [blame] | 49 | "properties": { |
| 50 | "id": string_schema, |
| 51 | "vim_info": object_schema, |
| 52 | "common_id": string_schema, |
| 53 | }, |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 54 | "additionalProperties": True, |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 55 | } |
| 56 | |
| tierno | 70eeb18 | 2020-10-19 16:38:00 +0000 | [diff] [blame] | 57 | deploy_item_list = { |
| 58 | "type": "array", |
| 59 | "items": deploy_item_schema, |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 60 | } |
| 61 | |
| tierno | 70eeb18 | 2020-10-19 16:38:00 +0000 | [diff] [blame] | 62 | deploy_vnf_schema = { |
| 63 | "title": "deploy.vnf.item validation", |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 64 | "$schema": "http://json-schema.org/draft-04/schema#", |
| 65 | "type": "object", |
| tierno | 70eeb18 | 2020-10-19 16:38:00 +0000 | [diff] [blame] | 66 | "properties": { |
| 67 | "_id": id_schema, |
| 68 | "vdur": deploy_item_list, |
| 69 | "vld": deploy_item_list, |
| 70 | }, |
| 71 | "additionalProperties": True, |
| 72 | "required": ["_id"], |
| 73 | } |
| 74 | |
| 75 | deploy_action_schema = { |
| 76 | "title": "deploy.action validation", |
| 77 | "$schema": "http://json-schema.org/draft-04/schema#", |
| 78 | "type": "object", |
| 79 | "properties": { |
| 80 | "action": {"enum": ["inject_ssh_key"]}, |
| 81 | "key": ssh_key_schema, |
| 82 | "user": string_schema, |
| 83 | "password": string_schema, |
| 84 | }, |
| 85 | "additionalProperties": False, |
| 86 | "required": ["action"], |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | deploy_schema = { |
| 90 | "title": "deploy input validation", |
| 91 | "$schema": "http://json-schema.org/draft-04/schema#", |
| 92 | "type": "object", |
| 93 | "properties": { |
| 94 | "action_id": string_schema, |
| tierno | 70eeb18 | 2020-10-19 16:38:00 +0000 | [diff] [blame] | 95 | "cloud_init_content": object_schema, |
| 96 | "name": string_schema, |
| 97 | "action": deploy_action_schema, |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 98 | "vnf": { |
| tierno | 70eeb18 | 2020-10-19 16:38:00 +0000 | [diff] [blame] | 99 | "type": "array", |
| 100 | "items": deploy_vnf_schema, |
| 101 | }, |
| 102 | "image": deploy_item_list, |
| 103 | "flavor": deploy_item_list, |
| vegall | 364627c | 2023-03-17 15:09:50 +0000 | [diff] [blame] | 104 | "shared-volumes": deploy_item_list, |
| tierno | 70eeb18 | 2020-10-19 16:38:00 +0000 | [diff] [blame] | 105 | "ns": { |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 106 | "type": "object", |
| 107 | "properties": { |
| tierno | 70eeb18 | 2020-10-19 16:38:00 +0000 | [diff] [blame] | 108 | "vld": deploy_item_list, |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 109 | }, |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 110 | }, |
| Alexis Romero | b70f4ed | 2022-03-11 18:00:49 +0100 | [diff] [blame] | 111 | "affinity-or-anti-affinity-group": deploy_item_list, |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 112 | }, |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 113 | "additionalProperties": False, |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 114 | } |
| 115 | |
| k4.rahul | 78f474e | 2022-05-02 15:47:57 +0000 | [diff] [blame] | 116 | rebuild_schema = { |
| 117 | "$schema": "http://json-schema.org/draft-04/schema#", |
| 118 | "vm_rebuild": { |
| 119 | "type": "array", |
| 120 | "items": { |
| 121 | "type": "object", |
| 122 | "properties": { |
| 123 | "vdu-id": id_schema, |
| 124 | "vim_name": name_schema, |
| 125 | "member-vnf-index": name_schema, |
| 126 | }, |
| 127 | }, |
| 128 | "additionalProperties": True, |
| 129 | }, |
| 130 | } |
| 131 | |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 132 | |
| 133 | class ValidationError(Exception): |
| 134 | def __init__(self, message, http_code=HTTPStatus.UNPROCESSABLE_ENTITY): |
| 135 | self.http_code = http_code |
| 136 | Exception.__init__(self, message) |
| 137 | |
| 138 | |
| 139 | def validate_input(indata, schema_to_use): |
| 140 | """ |
| 141 | Validates input data against json schema |
| 142 | :param indata: user input data. Should be a dictionary |
| 143 | :param schema_to_use: jsonschema to test |
| 144 | :return: None if ok, raises ValidationError exception on error |
| 145 | """ |
| 146 | try: |
| 147 | if schema_to_use: |
| 148 | js_v(indata, schema_to_use) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 149 | |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 150 | return None |
| 151 | except js_e.ValidationError as e: |
| 152 | if e.path: |
| 153 | error_pos = "at '" + ":".join(map(str, e.path)) + "'" |
| 154 | else: |
| 155 | error_pos = "" |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 156 | |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 157 | raise ValidationError("Format error {} '{}' ".format(error_pos, e.message)) |
| 158 | except js_e.SchemaError: |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 159 | raise ValidationError( |
| 160 | "Bad json schema {}".format(schema_to_use), |
| 161 | http_code=HTTPStatus.INTERNAL_SERVER_ERROR, |
| 162 | ) |