| 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"} |
| 34 | |
| 35 | image_schema = { |
| 36 | "title": "image input validation", |
| 37 | "$schema": "http://json-schema.org/draft-04/schema#", |
| 38 | "type": "object", |
| 39 | # TODO |
| 40 | } |
| 41 | |
| 42 | flavor_schema = { |
| 43 | "title": "image input validation", |
| 44 | "$schema": "http://json-schema.org/draft-04/schema#", |
| 45 | "type": "object", |
| 46 | # TODO |
| 47 | } |
| 48 | |
| 49 | ns_schema = { |
| 50 | "title": "image input validation", |
| 51 | "$schema": "http://json-schema.org/draft-04/schema#", |
| 52 | "type": "object", |
| 53 | # TODO |
| 54 | } |
| 55 | |
| 56 | deploy_schema = { |
| 57 | "title": "deploy input validation", |
| 58 | "$schema": "http://json-schema.org/draft-04/schema#", |
| 59 | "type": "object", |
| 60 | "properties": { |
| 61 | "action_id": string_schema, |
| 62 | "name": name_schema, |
| 63 | "action": {"enum" ["inject_ssh_key"]}, |
| 64 | "key": ssh_key_schema, |
| 65 | "user": name_schema, |
| 66 | "password": string_schema, |
| 67 | "vnf": { |
| 68 | "type": "object", |
| 69 | "properties": { |
| 70 | "_id": id_schema, |
| 71 | # TODO |
| 72 | }, |
| 73 | "required": ["_id"], |
| 74 | "additionalProperties": True, |
| 75 | }, |
| 76 | "image": { |
| 77 | "type": "array", |
| 78 | "minItems": 1, |
| 79 | "items": image_schema |
| 80 | }, |
| 81 | "flavor": { |
| 82 | "type": "array", |
| 83 | "minItems": 1, |
| 84 | "items": flavor_schema |
| 85 | }, |
| 86 | "ns": ns_schema, |
| 87 | }, |
| 88 | |
| 89 | "required": ["name"], |
| 90 | "additionalProperties": False |
| 91 | } |
| 92 | |
| 93 | |
| 94 | class ValidationError(Exception): |
| 95 | def __init__(self, message, http_code=HTTPStatus.UNPROCESSABLE_ENTITY): |
| 96 | self.http_code = http_code |
| 97 | Exception.__init__(self, message) |
| 98 | |
| 99 | |
| 100 | def validate_input(indata, schema_to_use): |
| 101 | """ |
| 102 | Validates input data against json schema |
| 103 | :param indata: user input data. Should be a dictionary |
| 104 | :param schema_to_use: jsonschema to test |
| 105 | :return: None if ok, raises ValidationError exception on error |
| 106 | """ |
| 107 | try: |
| 108 | if schema_to_use: |
| 109 | js_v(indata, schema_to_use) |
| 110 | return None |
| 111 | except js_e.ValidationError as e: |
| 112 | if e.path: |
| 113 | error_pos = "at '" + ":".join(map(str, e.path)) + "'" |
| 114 | else: |
| 115 | error_pos = "" |
| 116 | raise ValidationError("Format error {} '{}' ".format(error_pos, e.message)) |
| 117 | except js_e.SchemaError: |
| 118 | raise ValidationError("Bad json schema {}".format(schema_to_use), http_code=HTTPStatus.INTERNAL_SERVER_ERROR) |