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
|
import zmq.green as zmq
|
||||||
from eddn import __version__ as EDDN_VERSION
|
from eddn import __version__ as EDDN_VERSION
|
||||||
from eddn.conf import Settings
|
from eddn.conf import Settings
|
||||||
|
from eddn.Validator import validate, ValidationSeverity
|
||||||
|
|
||||||
from gevent import monkey
|
from gevent import monkey
|
||||||
monkey.patch_all()
|
monkey.patch_all()
|
||||||
@ -109,20 +110,26 @@ def parse_and_error_handle(data):
|
|||||||
logger.error("Error to %s: %s" % (get_remote_address(), exc.message))
|
logger.error("Error to %s: %s" % (get_remote_address(), exc.message))
|
||||||
return exc.message
|
return exc.message
|
||||||
|
|
||||||
ip_hash_salt = Settings.GATEWAY_IP_KEY_SALT
|
validationResults = validate(parsed_message)
|
||||||
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
|
if validationResults.severity <= ValidationSeverity.WARN:
|
||||||
# as compressed JSON.
|
|
||||||
gevent.spawn(push_message, simplejson.dumps(parsed_message))
|
ip_hash_salt = Settings.GATEWAY_IP_KEY_SALT
|
||||||
logger.info("Accepted %s upload from %s" % (
|
if ip_hash_salt:
|
||||||
parsed_message, get_remote_address()
|
# If an IP hash is set, salt+hash the uploader's IP address and set
|
||||||
))
|
# it as the EMDR upload key value.
|
||||||
return 'OK'
|
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/')
|
@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