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,20 +110,26 @@ def parse_and_error_handle(data):
logger.error("Error to %s: %s" % (get_remote_address(), exc.message))
return exc.message
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
# it as the EMDR upload key value.
ip_hash = hashlib.sha1(ip_hash_salt + get_remote_address()).hexdigest()
parsed_message.upload_keys.append({'name': 'EMDR', 'key': ip_hash})
validationResults = validate(parsed_message)
# Sends the parsed MarketOrderList or MarketHistoryList to the Announcers
# as compressed JSON.
gevent.spawn(push_message, simplejson.dumps(parsed_message))
logger.info("Accepted %s upload from %s" % (
parsed_message, get_remote_address()
))
return 'OK'
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
# it as the EMDR upload key value.
ip_hash = hashlib.sha1(ip_hash_salt + get_remote_address()).hexdigest()
parsed_message.upload_keys.append({'name': 'EMDR', 'key': ip_hash})
# Sends the parsed MarketOrderList or MarketHistoryList to the Announcers
# as compressed JSON.
gevent.spawn(push_message, simplejson.dumps(parsed_message))
logger.info("Accepted %s upload from %s" % (
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