blob: 91ea613714e093323fba998463361627ba865810 [file] [log] [blame]
tierno1d213f42020-04-24 14:02:51 +00001# -*- 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
tierno1d213f42020-04-24 14:02:51 +000016from http import HTTPStatus
17
sousaedu049cbb12022-01-05 11:39:35 +000018from jsonschema import exceptions as js_e, validate as js_v
19
tierno1d213f42020-04-24 14:02:51 +000020__author__ = "Alfonso Tierno <alfonso.tiernosepulveda@telefonica.com>"
21__version__ = "0.1"
22version_date = "Jun 2020"
23
24"""
25Validator of input data using JSON schemas
26"""
27
28# Basis schemas
sousaedu80135b92021-02-17 15:05:18 +010029name_schema = {
30 "type": "string",
31 "minLength": 1,
32 "maxLength": 255,
33 "pattern": "^[^,;()'\"]+$",
34}
tierno1d213f42020-04-24 14:02:51 +000035string_schema = {"type": "string", "minLength": 1, "maxLength": 255}
36ssh_key_schema = {"type": "string", "minLength": 1}
sousaedu80135b92021-02-17 15:05:18 +010037id_schema = {
38 "type": "string",
39 "pattern": "^[a-fA-F0-9]{8}(-[a-fA-F0-9]{4}){3}-[a-fA-F0-9]{12}$",
40}
tierno1d213f42020-04-24 14:02:51 +000041bool_schema = {"type": "boolean"}
42null_schema = {"type": "null"}
tierno70eeb182020-10-19 16:38:00 +000043object_schema = {"type": "object"}
tierno1d213f42020-04-24 14:02:51 +000044
tierno70eeb182020-10-19 16:38:00 +000045deploy_item_schema = {
46 "title": "deploy item validation. Each vld, vdu, flavor, image, ...",
tierno1d213f42020-04-24 14:02:51 +000047 "$schema": "http://json-schema.org/draft-04/schema#",
48 "type": "object",
tierno70eeb182020-10-19 16:38:00 +000049 "properties": {
50 "id": string_schema,
51 "vim_info": object_schema,
52 "common_id": string_schema,
53 },
sousaedu80135b92021-02-17 15:05:18 +010054 "additionalProperties": True,
tierno1d213f42020-04-24 14:02:51 +000055}
56
tierno70eeb182020-10-19 16:38:00 +000057deploy_item_list = {
58 "type": "array",
59 "items": deploy_item_schema,
tierno1d213f42020-04-24 14:02:51 +000060}
61
tierno70eeb182020-10-19 16:38:00 +000062deploy_vnf_schema = {
63 "title": "deploy.vnf.item validation",
tierno1d213f42020-04-24 14:02:51 +000064 "$schema": "http://json-schema.org/draft-04/schema#",
65 "type": "object",
tierno70eeb182020-10-19 16:38:00 +000066 "properties": {
67 "_id": id_schema,
68 "vdur": deploy_item_list,
69 "vld": deploy_item_list,
70 },
71 "additionalProperties": True,
72 "required": ["_id"],
73}
74
75deploy_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"],
tierno1d213f42020-04-24 14:02:51 +000087}
88
89deploy_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,
tierno70eeb182020-10-19 16:38:00 +000095 "cloud_init_content": object_schema,
96 "name": string_schema,
97 "action": deploy_action_schema,
tierno1d213f42020-04-24 14:02:51 +000098 "vnf": {
tierno70eeb182020-10-19 16:38:00 +000099 "type": "array",
100 "items": deploy_vnf_schema,
101 },
102 "image": deploy_item_list,
103 "flavor": deploy_item_list,
104 "ns": {
tierno1d213f42020-04-24 14:02:51 +0000105 "type": "object",
106 "properties": {
tierno70eeb182020-10-19 16:38:00 +0000107 "vld": deploy_item_list,
sousaedu80135b92021-02-17 15:05:18 +0100108 },
tierno1d213f42020-04-24 14:02:51 +0000109 },
tierno1d213f42020-04-24 14:02:51 +0000110 },
sousaedu80135b92021-02-17 15:05:18 +0100111 "additionalProperties": False,
tierno1d213f42020-04-24 14:02:51 +0000112}
113
114
115class ValidationError(Exception):
116 def __init__(self, message, http_code=HTTPStatus.UNPROCESSABLE_ENTITY):
117 self.http_code = http_code
118 Exception.__init__(self, message)
119
120
121def validate_input(indata, schema_to_use):
122 """
123 Validates input data against json schema
124 :param indata: user input data. Should be a dictionary
125 :param schema_to_use: jsonschema to test
126 :return: None if ok, raises ValidationError exception on error
127 """
128 try:
129 if schema_to_use:
130 js_v(indata, schema_to_use)
sousaedu80135b92021-02-17 15:05:18 +0100131
tierno1d213f42020-04-24 14:02:51 +0000132 return None
133 except js_e.ValidationError as e:
134 if e.path:
135 error_pos = "at '" + ":".join(map(str, e.path)) + "'"
136 else:
137 error_pos = ""
sousaedu80135b92021-02-17 15:05:18 +0100138
tierno1d213f42020-04-24 14:02:51 +0000139 raise ValidationError("Format error {} '{}' ".format(error_pos, e.message))
140 except js_e.SchemaError:
sousaedu80135b92021-02-17 15:05:18 +0100141 raise ValidationError(
142 "Bad json schema {}".format(schema_to_use),
143 http_code=HTTPStatus.INTERNAL_SERVER_ERROR,
144 )