mirror of
https://github.com/EDCD/EDDN.git
synced 2025-06-06 02:13:24 +03:00
Create a framework for JSON validation at the gateway.
This commit is contained in:
parent
43a9931e38
commit
2d406f9e25
@ -11,6 +11,7 @@ import zlib
|
||||
import zmq.green as zmq
|
||||
from eddn import __version__ as EDDN_VERSION
|
||||
from eddn.conf import Settings
|
||||
from eddn.Validator import validate, ValidationSeverity
|
||||
|
||||
from gevent import monkey
|
||||
monkey.patch_all()
|
||||
@ -109,6 +110,10 @@ def parse_and_error_handle(data):
|
||||
logger.error("Error to %s: %s" % (get_remote_address(), exc.message))
|
||||
return exc.message
|
||||
|
||||
validationResults = validate(parsed_message)
|
||||
|
||||
if validationResults.severity <= ValidationSeverity.WARN:
|
||||
|
||||
ip_hash_salt = Settings.GATEWAY_IP_KEY_SALT
|
||||
if ip_hash_salt:
|
||||
# If an IP hash is set, salt+hash the uploader's IP address and set
|
||||
@ -123,6 +128,8 @@ def parse_and_error_handle(data):
|
||||
parsed_message, get_remote_address()
|
||||
))
|
||||
return 'OK'
|
||||
else:
|
||||
return "FAIL: " + str(parsed_message) + str(validationResults.messages)
|
||||
|
||||
|
||||
@post('/upload/')
|
||||
|
31
src/eddn/Validator.py
Normal file
31
src/eddn/Validator.py
Normal file
@ -0,0 +1,31 @@
|
||||
from enum import IntEnum
|
||||
|
||||
|
||||
def validate(json_object):
|
||||
results = ValidationResults()
|
||||
|
||||
if "$schemaRef" not in json_object:
|
||||
results.add(ValidationSeverity.FATAL, JsonValidationException("No $schemaRef found, unable to validate."))
|
||||
|
||||
return results
|
||||
|
||||
|
||||
class ValidationSeverity(IntEnum):
|
||||
OK = 0,
|
||||
WARN = 1,
|
||||
ERROR = 2,
|
||||
FATAL = 3
|
||||
|
||||
|
||||
class ValidationResults(object):
|
||||
|
||||
severity = ValidationSeverity.OK
|
||||
messages = []
|
||||
|
||||
def add(self, severity, exception):
|
||||
self.severity = max(severity, self.severity)
|
||||
self.messages.append(exception)
|
||||
|
||||
|
||||
class JsonValidationException(Exception):
|
||||
pass
|
Loading…
x
Reference in New Issue
Block a user