From ceaf3f289170ec9ec6810856d965ce1495caeeb1 Mon Sep 17 00:00:00 2001 From: Dev Singh Date: Mon, 10 Jan 2022 09:21:46 -0600 Subject: [PATCH] add validation schema for persistent keys --- src/cli/config.py | 40 ++++++++++++++-------------------- src/cli/validation-schema.json | 26 ++++++++++++++++++++++ src/requirements.txt | 4 +++- 3 files changed, 45 insertions(+), 25 deletions(-) create mode 100644 src/cli/validation-schema.json diff --git a/src/cli/config.py b/src/cli/config.py index f837133..3160c4b 100644 --- a/src/cli/config.py +++ b/src/cli/config.py @@ -2,6 +2,7 @@ import math import json from multiprocessing import Pool import os +from cerberus import Validator from data import set_database_config, get_database_config from interface import stderr, stdout, INF, ERR @@ -140,32 +141,16 @@ class ConfigurationError (Exception): self.code = code def parse_config_persistent(send, config): + v = Validator(load_validation_schema(), allow_unknown = True) + isValidated = v.validate(config) - try: - apikey = config["persistent"]["key"]["database"] - except: - raise ConfigurationError("persistent/key/database field is invalid or missing", 111) - try: - tbakey = config["persistent"]["key"]["tba"] - except: - raise ConfigurationError("persistent/key/tba field is invalid or missing", 112) - try: - preference = config["persistent"]["config-preference"] - except: - raise ConfigurationError("persistent/config-preference field is invalid or missing", 113) - try: - sync = config["persistent"]["synchronize-config"] - except: - raise ConfigurationError("persistent/synchronize-config field is invalid or missing", 114) + if not isValidated: + raise ConfigurationError(v.errors, 101) - if apikey == None or apikey == "": - raise ConfigurationError("persistent/key/database field is empty", 115) - if tbakey == None or tbakey == "": - raise ConfigurationError("persistent/key/tba field is empty", 116) - if preference == None or preference == "": - raise ConfigurationError("persistent/config-preference field is empty", 117) - if sync != True and sync != False: - raise ConfigurationError("persistent/synchronize-config field is empty", 118) + apikey = config["persistent"]["key"]["database"] + tbakey = config["persistent"]["key"]["tba"] + preference = config["persistent"]["config-preference"] + sync = config["persistent"]["synchronize-config"] return apikey, tbakey, preference, sync @@ -249,6 +234,13 @@ def load_config(path, config_vector): f.close() return 1 +def load_validation_schema(): + try: + with open("validation-schema.json", "r") as f: + return json.load(f) + except: + raise FileNotFoundError("Validation schema not found at validation-schema.json") + def save_config(path, config_vector): f = open(path, "w+") json.dump(config_vector, f, ensure_ascii=False, indent=4) diff --git a/src/cli/validation-schema.json b/src/cli/validation-schema.json new file mode 100644 index 0000000..c0e02b8 --- /dev/null +++ b/src/cli/validation-schema.json @@ -0,0 +1,26 @@ +{ + "persistent": { + "type": "dict", + "require_all": true, + "schema": { + "key": { + "type": "dict", + "require_all":true, + "schema": { + "database": {"type":"string"}, + "tba": {"type": "string"}, + "tra": { + "type": "dict", + "require_all": true, + "schema": { + "CLIENT_ID": {"type": "string"}, + "CLIENT_SECRET": {"type": "string"} + } + } + } + }, + "config-preference": {"type": "string", "required": true}, + "synchronize-config": {"type": "boolean", "required": true} + } + } +} diff --git a/src/requirements.txt b/src/requirements.txt index 52854df..c89383a 100644 --- a/src/requirements.txt +++ b/src/requirements.txt @@ -14,4 +14,6 @@ pyparsing kivy==2.0.0rc2 pyzmq -python-daemon \ No newline at end of file +python-daemon + +cerebrus \ No newline at end of file