From 78d4b7fbc4a4fe170a6cd2275865c88b02b0da02 Mon Sep 17 00:00:00 2001 From: Jordan Bradford <36420801+jrdnbradford@users.noreply.github.com> Date: Fri, 2 Feb 2024 22:52:16 -0500 Subject: [PATCH] Test config setup --- tljh/config-schema.json | 143 +++++++++++++++++++++++++++++++++- tljh/config.py | 25 +++--- tljh/requirements-hub-env.txt | 2 - 3 files changed, 158 insertions(+), 12 deletions(-) diff --git a/tljh/config-schema.json b/tljh/config-schema.json index 15591da..4563d19 100644 --- a/tljh/config-schema.json +++ b/tljh/config-schema.json @@ -1 +1,142 @@ -file:///home/bradfojb/Desktop/Personal-Repos/the-littlest-jupyterhub/schema.json \ No newline at end of file +{ + "$schema": "http://json-schema.org/draft-07/schema#", + "title": "Littlest JupyterHub YAML config file", + "definitions": { + "Users": { + "type": "object", + "properties": { + "extra_user_groups": { + "type": "object", + "items": { + "type": "string" + } + }, + "allowed": { + "type": "array", + "items": { + "type": "string" + } + }, + "banned": { + "type": "array", + "items": { + "type": "string" + } + }, + "admin": { + "type": "array", + "items": { + "type": "string" + } + } + } + }, + "HTTP": { + "type": "object", + "additionalProperties": false, + "properties": { + "address": { + "type": "string", + "format": "ipv4" + }, + "port": { + "type": "integer" + } + } + }, + "HTTPS": { + "type": "object", + "additionalProperties": false, + "properties": { + "enabled": { + "type": "boolean" + }, + "address": { + "type": "string", + "format": "ipv4" + }, + "port": { + "type": "integer" + }, + "tls": { + "$ref": "#/definitions/TLS" + }, + "letsencrypt": { + "$ref": "#/definitions/LetsEncrypt" + } + } + }, + "LetsEncrypt": { + "type": "object", + "additionalProperties": false, + "properties": { + "email": { + "type": "string", + "format": "email" + }, + "domains": { + "type": "array", + "items": { + "type": "string", + "format": "hostname" + } + } + } + }, + "TLS": { + "type": "object", + "additionalProperties": false, + "properties": { + "key": { + "type": "string" + }, + "cert": { + "type": "string" + } + }, + "required": ["key", "cert"] + }, + "Limits": { + "description": "User CPU and memory limits.", + "type": "object", + "additionalProperties": false, + "properties": { + "memory": { + "type": "string" + }, + "cpu": { + "type": "integer" + } + } + }, + "UserEnvironment": { + "type": "object", + "additionalProperties": false, + "properties": { + "default_app": { + "type": "string", + "enum": ["jupyterlab", "classic"], + "default": "jupyterlab" + } + } + } + }, + "properties": { + "additionalProperties": false, + "user_environment": { + "$ref": "#/definitions/UserEnvironment" + }, + "users": { + "$ref": "#/definitions/Users" + }, + "limits": { + "$ref": "#/definitions/Limits" + }, + "https": { + "$ref": "#/definitions/HTTPS" + }, + "http": { + "$ref": "#/definitions/HTTP" + } + } +} diff --git a/tljh/config.py b/tljh/config.py index 09ecef0..8f0767a 100644 --- a/tljh/config.py +++ b/tljh/config.py @@ -155,10 +155,18 @@ def remove_item_from_config(config, property_path, value): def validate_config(config): - import json - import jsonschema - config_schema = json.load("config-schema.json") - jsonschema.validate(instance=config, schema=config_schema) + import json, jsonschema + + cwd = os.getcwd() + config_schema_file = os.path.join(cwd, "config-schema.json") + with open(config_schema_file) as f: + config_schema = json.load(f) + + try: + jsonschema.validate(instance=config, schema=config_schema) + except jsonschema.exceptions.ValidationError as e: + print(e) + exit() def show_config(config_path): @@ -179,14 +187,13 @@ def set_config_value(config_path, key_path, value): Set key at key_path in config_path to value """ # FIXME: Have a file lock here - # FIXME: Validate schema here try: with open(config_path) as f: config = yaml.load(f) except FileNotFoundError: config = {} - config = set_item_in_config(config, key_path, value) + validate_config(config) with open(config_path, "w") as f: @@ -198,7 +205,6 @@ def unset_config_value(config_path, key_path): Unset key at key_path in config_path """ # FIXME: Have a file lock here - # FIXME: Validate schema here try: with open(config_path) as f: config = yaml.load(f) @@ -206,6 +212,7 @@ def unset_config_value(config_path, key_path): config = {} config = unset_item_from_config(config, key_path) + validate_config(config) with open(config_path, "w") as f: yaml.dump(config, f) @@ -216,7 +223,6 @@ def add_config_value(config_path, key_path, value): Add value to list at key_path """ # FIXME: Have a file lock here - # FIXME: Validate schema here try: with open(config_path) as f: config = yaml.load(f) @@ -224,6 +230,7 @@ def add_config_value(config_path, key_path, value): config = {} config = add_item_to_config(config, key_path, value) + validate_config(config) with open(config_path, "w") as f: yaml.dump(config, f) @@ -234,7 +241,6 @@ def remove_config_value(config_path, key_path, value): Remove value from list at key_path """ # FIXME: Have a file lock here - # FIXME: Validate schema here try: with open(config_path) as f: config = yaml.load(f) @@ -242,6 +248,7 @@ def remove_config_value(config_path, key_path, value): config = {} config = remove_item_from_config(config, key_path, value) + validate_config(config) with open(config_path, "w") as f: yaml.dump(config, f) diff --git a/tljh/requirements-hub-env.txt b/tljh/requirements-hub-env.txt index cde66e4..62f39f4 100644 --- a/tljh/requirements-hub-env.txt +++ b/tljh/requirements-hub-env.txt @@ -17,8 +17,6 @@ jupyterhub-tmpauthenticator>=1.0.0,<2 oauthenticator[azuread]>=16.0.4,<17 jupyterhub-idle-culler>=1.2.1,<2 -jsonschema - # pycurl is installed to improve reliability and performance for when JupyterHub # makes web requests. JupyterHub will use tornado's CurlAsyncHTTPClient when # making requests over tornado's SimpleHTTPClient automatically if pycurl is