This repository was archived by the owner on Jun 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathvalidators.py
More file actions
265 lines (217 loc) · 8.46 KB
/
Copy pathvalidators.py
File metadata and controls
265 lines (217 loc) · 8.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
"""Validators module."""
import binascii
import six
import uuid
import json
import jsonschema
from cryptography.exceptions import InvalidSignature
from cryptography.hazmat import backends
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.asymmetric import padding
from cryptography.hazmat.primitives.serialization import load_ssh_public_key
from refstack.api import exceptions as api_exc
ext_format_checker = jsonschema.FormatChecker()
def is_uuid(inst):
"""Check that inst is a uuid_hex string."""
try:
uuid.UUID(hex=inst)
except (TypeError, ValueError):
return False
return True
@jsonschema.FormatChecker.checks(ext_format_checker,
format='uuid_hex',
raises=(TypeError, ValueError))
def checker_uuid(inst):
"""Checker 'uuid_hex' format for jsonschema validator."""
return is_uuid(inst)
class BaseValidator(object):
"""Base class for validators."""
schema = {}
def __init__(self):
"""Init."""
jsonschema.Draft4Validator.check_schema(self.schema)
self.validator = jsonschema.Draft4Validator(
self.schema,
format_checker=ext_format_checker
)
def validate(self, request):
"""Validate request."""
try:
body = json.loads(request.body.decode('utf-8'))
except (ValueError, TypeError) as e:
raise api_exc.ValidationError('Malformed request', e)
try:
jsonschema.validate(body, self.schema)
except jsonschema.ValidationError as e:
raise api_exc.ValidationError(
'Request doesn''t correspond to schema', e)
def check_emptyness(self, body, keys):
"""Check that all values are not empty."""
for key in keys:
value = body[key]
if isinstance(value, six.string_types):
value = value.strip()
if not value:
raise api_exc.ValidationError(key + ' should not be empty')
elif value is None:
raise api_exc.ValidationError(key + ' must be present')
class TestResultValidator(BaseValidator):
"""Validator for incoming test results."""
schema = {
'type': 'object',
'properties': {
'cpid': {
'type': 'string'
},
'duration_seconds': {'type': 'integer'},
'results': {
'type': 'array',
'items': [{
'type': 'object',
'properties': {
'name': {'type': 'string'},
'uuid': {
'type': 'string',
'format': 'uuid_hex'
}
}
}]
}
},
'required': ['cpid', 'duration_seconds', 'results'],
'additionalProperties': False
}
def validate(self, request):
"""Validate uploaded test results."""
super(TestResultValidator, self).validate(request)
if request.headers.get('X-Signature') or \
request.headers.get('X-Public-Key'):
try:
sign = binascii.a2b_hex(request.headers.get('X-Signature', ''))
except (binascii.Error, TypeError) as e:
raise api_exc.ValidationError('Malformed signature', e)
try:
key = load_ssh_public_key(
request.headers.get('X-Public-Key', ''),
backend=backends.default_backend()
)
except (binascii.Error, ValueError) as e:
raise api_exc.ValidationError('Malformed public key', e)
verifier = key.verifier(sign, padding.PKCS1v15(), hashes.SHA256())
verifier.update(request.body)
try:
verifier.verify()
except InvalidSignature:
raise api_exc.ValidationError('Signature verification failed')
if self._is_empty_result(request):
raise api_exc.ValidationError('Uploaded results must contain at '
'least one passing test.')
def _is_empty_result(self, request):
"""Check if the test results list is empty."""
body = json.loads(request.body.decode('utf-8'))
if len(body['results']) != 0:
return False
return True
@staticmethod
def assert_id(_id):
"""Check that _id is a valid uuid_hex string."""
return is_uuid(_id)
class PubkeyValidator(BaseValidator):
"""Validator for uploaded public pubkeys."""
schema = {
'type': 'object',
'properties': {
'raw_key': {'type': 'string'},
'self_signature': {'type': 'string'}
},
'required': ['raw_key', 'self_signature'],
'additionalProperties': False
}
def validate(self, request):
"""Validate uploaded test results."""
super(PubkeyValidator, self).validate(request)
body = json.loads(request.body.decode('utf-8'))
key_format = body['raw_key'].strip().split()[0]
if key_format not in ('ssh-dss', 'ssh-rsa',
'pgp-sign-rsa', 'pgp-sign-dss'):
raise api_exc.ValidationError('Public key has unsupported format')
try:
sign = binascii.a2b_hex(body['self_signature'])
except (binascii.Error, TypeError) as e:
raise api_exc.ValidationError('Malformed signature', e)
try:
key = load_ssh_public_key(body['raw_key'].encode('utf-8'),
backend=backends.default_backend())
except (binascii.Error, ValueError) as e:
raise api_exc.ValidationError('Malformed public key', e)
verifier = key.verifier(sign, padding.PKCS1v15(), hashes.SHA256())
verifier.update('signature'.encode('utf-8'))
try:
verifier.verify()
except InvalidSignature:
raise api_exc.ValidationError('Signature verification failed')
class VendorValidator(BaseValidator):
"""Validator for adding new vendor."""
schema = {
'type': 'object',
'properties': {
'name': {'type': 'string'},
'description': {'type': 'string'},
},
'required': ['name'],
'additionalProperties': False
}
def validate(self, request):
"""Validate uploaded vendor data."""
super(VendorValidator, self).validate(request)
body = json.loads(request.body.decode('utf-8'))
self.check_emptyness(body, ['name'])
class ProductValidator(BaseValidator):
"""Validate uploaded product data."""
schema = {
'type': 'object',
'properties': {
'name': {'type': 'string'},
'description': {'type': 'string'},
'product_type': {'type': 'integer'},
'organization_id': {'type': 'string', 'format': 'uuid_hex'},
'version': {'type': 'string'}
},
'required': ['name', 'product_type'],
'additionalProperties': False
}
def validate(self, request):
"""Validate uploaded test results."""
super(ProductValidator, self).validate(request)
body = json.loads(request.body.decode('utf-8'))
self.check_emptyness(body, ['name', 'product_type'])
class ProductVersionValidator(BaseValidator):
"""Validate adding product versions."""
schema = {
'type': 'object',
'properties': {
'version': {'type': 'string'},
'cpid': {'type': 'string'}
},
'required': ['version'],
'additionalProperties': False
}
def validate(self, request):
"""Validate product version data."""
super(ProductVersionValidator, self).validate(request)
body = json.loads(request.body.decode('utf-8'))
self.check_emptyness(body, ['version'])