d6e1262939e5 — John Mulligan tip 4 years ago
sambacc: add initial support for checking JSON schema of config
2 files changed, 136 insertions(+), 1 deletions(-)

A => sambacc/config.json.schema
M sambacc/config.py
A => sambacc/config.json.schema +120 -0
@@ 0,0 1,120 @@ 
+{
+  "$schema": "http://json-schema.org/draft-07/schema#",
+  "$id": "mailto:phlogistonjohn+sambacc-0@asynchrono.us",
+  "title": "sambacc configuration",
+  "$comment": "ingore anything starting with an underscore",
+  "definitions": {
+    "options": {
+      "description": "JSON key-value mapping of smb.conf options",
+      "$id": "#options",
+      "type": "object",
+      "additionalProperties": {
+        "type": "string"
+      }
+    }
+  },
+  "type": "object",
+  "properties": {
+    "samba-container-config": {
+      "type": "string",
+      "description": "version string"
+    },
+    "configs": {
+      "type": "object",
+      "additionalProperties": {
+        "type": "object",
+        "additionalProperties": false,
+        "properties": {
+          "shares": {
+            "type": "array",
+            "items": {
+              "type": "string",
+              "minLength": 1
+            },
+            "minItems": 1,
+            "uniqeItems": true
+          },
+          "globals": {
+            "type": "array",
+            "items": {
+              "type": "string",
+              "minLength": 1
+            },
+            "minItems": 1,
+            "uniqeItems": true
+          },
+          "instance_name": {
+            "type": "string",
+            "minLength": 1
+          }
+        }
+      }
+    },
+    "shares": {
+      "type": "object",
+      "additionalProperties": {
+        "type": "object",
+        "additionalProperties": false,
+        "properties": {
+          "options": {
+            "description": "JSON key-value mapping of smb.conf options",
+            "type": "object",
+            "additionalProperties": {
+              "type": "string"
+            }
+          }
+        }
+      }
+    },
+    "globals": {
+      "type": "object",
+      "additionalProperties": {
+        "type": "object",
+        "additionalProperties": false,
+        "properties": {
+          "options": {
+            "description": "JSON key-value mapping of smb.conf options",
+            "type": "object",
+            "additionalProperties": {
+              "type": "string"
+            }
+          }
+        }
+      }
+    },
+    "users": {
+      "type": "object",
+      "additionalProperties": {
+        "type": "array",
+        "items": {
+          "type": "object",
+          "additionalProperties": false,
+          "properties": {
+            "name": {
+              "type": "string"
+            },
+            "uid": {
+              "type": "number"
+            },
+            "gid": {
+              "type": "number"
+            },
+            "password": {
+              "type": "string"
+            },
+            "nt_hash": {
+              "type": "string"
+            }
+          }
+        }
+      }
+    }
+  },
+  "patternProperties": {
+    "^_": true
+  },
+  "additionalProperties": false,
+  "required": [
+    "samba-container-config"
+  ]
+}

          
M sambacc/config.py +16 -1
@@ 19,8 19,21 @@ 
 import binascii
 import errno
 import json
+import pkg_resources
+
+import jsonschema
+
 
 _VALID_VERSIONS = ["v0"]
+_JSON_SCHEMA = None
+
+
+def _schema():
+    global _JSON_SCHEMA
+    if _JSON_SCHEMA is None:
+        sstream = pkg_resources.resource_stream(__name__, "config.json.schema")
+        _JSON_SCHEMA = json.load(sstream)
+    return _JSON_SCHEMA
 
 # alias open to _open to support test assertions when running
 # as UID 0

          
@@ 55,7 68,7 @@ def read_config_files(fnames):
     return gconfig
 
 
-def check_config_data(data):
+def check_config_data(data, validate=True):
     """Return the config data or raise a ValueError if the config
     is invalid or incomplete.
     """

          
@@ 65,6 78,8 @@ def check_config_data(data):
         raise ValueError("Invalid config: no samba-container-config key")
     elif version not in _VALID_VERSIONS:
         raise ValueError(f"Invalid config: unknown version {version}")
+    if validate:
+        jsonschema.validate(instance=data, schema=_schema())
     return data