Create a framework for JSON validation at the gateway.

This commit is contained in:
James Muscat 2014-12-17 13:30:09 +00:00
parent 43a9931e38
commit 2d406f9e25
2 changed files with 51 additions and 13 deletions

View File

@ -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
View 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