diff --git a/src/eddn/Gateway.py b/src/eddn/Gateway.py index 2a099ca..9a961c6 100644 --- a/src/eddn/Gateway.py +++ b/src/eddn/Gateway.py @@ -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/') diff --git a/src/eddn/Validator.py b/src/eddn/Validator.py new file mode 100644 index 0000000..b9608b6 --- /dev/null +++ b/src/eddn/Validator.py @@ -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