| 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 | |
| 16 | from jsonschema import validate as js_v, exceptions as js_e |
| 17 | from http import HTTPStatus |
| 18 | |
| 19 | __author__ = "Alfonso Tierno <alfonso.tiernosepulveda@telefonica.com>" |
| 20 | __version__ = "0.1" |
| 21 | version_date = "Jun 2020" |
| 22 | |
| 23 | """ |
| 24 | Validator of input data using JSON schemas |
| 25 | """ |
| 26 | |
| 27 | # Basis schemas |
| 28 | name_schema = {"type": "string", "minLength": 1, "maxLength": 255, "pattern": "^[^,;()'\"]+$"} |
| 29 | string_schema = {"type": "string", "minLength": 1, "maxLength": 255} |
| 30 | ssh_key_schema = {"type": "string", "minLength": 1} |
| 31 | id_schema = {"type": "string", "pattern": "^[a-fA-F0-9]{8}(-[a-fA-F0-9]{4}){3}-[a-fA-F0-9]{12}$"} |
| 32 | bool_schema = {"type": "boolean"} |
| 33 | null_schema = {"type": "null"} |
| tierno | 70eeb18 | 2020-10-19 16:38:00 +0000 | [diff] [blame] | 34 | object_schema = {"type": "object"} |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 35 | |
| tierno | 70eeb18 | 2020-10-19 16:38:00 +0000 | [diff] [blame] | 36 | deploy_item_schema = { |
| 37 | "title": "deploy item validation. Each vld, vdu, flavor, image, ...", |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 38 | "$schema": "http://json-schema.org/draft-04/schema#", |
| 39 | "type": "object", |
| tierno | 70eeb18 | 2020-10-19 16:38:00 +0000 | [diff] [blame] | 40 | "properties": { |
| 41 | "id": string_schema, |
| 42 | "vim_info": object_schema, |
| 43 | "common_id": string_schema, |
| 44 | }, |
| 45 | "additionalProperties": True |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 46 | } |
| 47 | |
| tierno | 70eeb18 | 2020-10-19 16:38:00 +0000 | [diff] [blame] | 48 | deploy_item_list = { |
| 49 | "type": "array", |
| 50 | "items": deploy_item_schema, |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 51 | } |
| 52 | |
| tierno | 70eeb18 | 2020-10-19 16:38:00 +0000 | [diff] [blame] | 53 | deploy_vnf_schema = { |
| 54 | "title": "deploy.vnf.item validation", |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 55 | "$schema": "http://json-schema.org/draft-04/schema#", |
| 56 | "type": "object", |
| tierno | 70eeb18 | 2020-10-19 16:38:00 +0000 | [diff] [blame] | 57 | "properties": { |
| 58 | "_id": id_schema, |
| 59 | "vdur": deploy_item_list, |
| 60 | "vld": deploy_item_list, |
| 61 | }, |
| 62 | "additionalProperties": True, |
| 63 | "required": ["_id"], |
| 64 | } |
| 65 | |
| 66 | deploy_action_schema = { |
| 67 | "title": "deploy.action validation", |
| 68 | "$schema": "http://json-schema.org/draft-04/schema#", |
| 69 | "type": "object", |
| 70 | "properties": { |
| 71 | "action": {"enum": ["inject_ssh_key"]}, |
| 72 | "key": ssh_key_schema, |
| 73 | "user": string_schema, |
| 74 | "password": string_schema, |
| 75 | }, |
| 76 | "additionalProperties": False, |
| 77 | "required": ["action"], |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | deploy_schema = { |
| 81 | "title": "deploy input validation", |
| 82 | "$schema": "http://json-schema.org/draft-04/schema#", |
| 83 | "type": "object", |
| 84 | "properties": { |
| 85 | "action_id": string_schema, |
| tierno | 70eeb18 | 2020-10-19 16:38:00 +0000 | [diff] [blame] | 86 | "cloud_init_content": object_schema, |
| 87 | "name": string_schema, |
| 88 | "action": deploy_action_schema, |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 89 | "vnf": { |
| tierno | 70eeb18 | 2020-10-19 16:38:00 +0000 | [diff] [blame] | 90 | "type": "array", |
| 91 | "items": deploy_vnf_schema, |
| 92 | }, |
| 93 | "image": deploy_item_list, |
| 94 | "flavor": deploy_item_list, |
| 95 | "ns": { |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 96 | "type": "object", |
| 97 | "properties": { |
| tierno | 70eeb18 | 2020-10-19 16:38:00 +0000 | [diff] [blame] | 98 | "vld": deploy_item_list, |
| 99 | } |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 100 | }, |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 101 | }, |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 102 | "additionalProperties": False |
| 103 | } |
| 104 | |
| 105 | |
| 106 | class ValidationError(Exception): |
| 107 | def __init__(self, message, http_code=HTTPStatus.UNPROCESSABLE_ENTITY): |
| 108 | self.http_code = http_code |
| 109 | Exception.__init__(self, message) |
| 110 | |
| 111 | |
| 112 | def validate_input(indata, schema_to_use): |
| 113 | """ |
| 114 | Validates input data against json schema |
| 115 | :param indata: user input data. Should be a dictionary |
| 116 | :param schema_to_use: jsonschema to test |
| 117 | :return: None if ok, raises ValidationError exception on error |
| 118 | """ |
| 119 | try: |
| 120 | if schema_to_use: |
| 121 | js_v(indata, schema_to_use) |
| 122 | return None |
| 123 | except js_e.ValidationError as e: |
| 124 | if e.path: |
| 125 | error_pos = "at '" + ":".join(map(str, e.path)) + "'" |
| 126 | else: |
| 127 | error_pos = "" |
| 128 | raise ValidationError("Format error {} '{}' ".format(error_pos, e.message)) |
| 129 | except js_e.SchemaError: |
| 130 | raise ValidationError("Bad json schema {}".format(schema_to_use), http_code=HTTPStatus.INTERNAL_SERVER_ERROR) |