From 887ed45c9a1fb6fd9701c850ec097c6dcf4b3cf1 Mon Sep 17 00:00:00 2001
From: Erik Reid <erik.reid@geant.org>
Date: Mon, 5 Nov 2018 20:33:42 +0100
Subject: [PATCH] add config input validation

---
 inventory_provider/router_interfaces.py | 41 ++++++++++++++++++++-----
 requirements.txt                        |  2 ++
 setup.py                                |  4 ++-
 3 files changed, 38 insertions(+), 9 deletions(-)

diff --git a/inventory_provider/router_interfaces.py b/inventory_provider/router_interfaces.py
index 0384fc7c..21d6eb11 100644
--- a/inventory_provider/router_interfaces.py
+++ b/inventory_provider/router_interfaces.py
@@ -3,10 +3,33 @@ import json
 import re
 
 import click
+import jsonschema
 import mysql.connector
 from pysnmp.hlapi import nextCmd, SnmpEngine, CommunityData, \
         UdpTransportTarget, ContextData, ObjectType, ObjectIdentity
 
+CONFIG_SCHEMA = {
+    "$schema": "http://json-schema.org/draft-07/schema#",
+    "type": "object",
+    "properties": {
+        "alarms-db": {
+            "type": "object",
+            "properties": {
+                "hostname": {"type": "string"},
+                "dbname": {"type": "string"},
+                "username": {"type": "string"},
+                "password": {"type": "string"}
+            },
+            "required": ["hostname", "dbname", "username", "password"],
+            "additionalProperties": False
+        },
+        "oid_list.conf": {"type": "string"},
+        "routers_community.conf": {"type": "string"}
+    },
+    "required": ["alarms-db", "oid_list.conf", "routers_community.conf"],
+    "additionalProperties": False
+}
+
 
 def walk(agent_hostname, community, base_oid):
     """
@@ -33,7 +56,7 @@ def walk(agent_hostname, community, base_oid):
     # Pre-load MIB modules we expect to work with
     mibBuilder.loadModules('SNMPv2-MIB', 'SNMP-COMMUNITY-MIB', 'RFC1213-MIB')
 
-    print("walking %s: %s" % (agent_hostname, base_oid))
+    # print("walking %s: %s" % (agent_hostname, base_oid))
     for (engineErrorIndication,
          pduErrorIndication,
          errorIndex,
@@ -68,7 +91,9 @@ def _validate_config(ctx, param, value):
     :param value:
     :return:
     """
-    return json.loads(value.read())
+    config = json.loads(value.read())
+    jsonschema.validate(config, CONFIG_SCHEMA)
+    return config
 
 
 def load_oids(config_file):
@@ -109,7 +134,7 @@ def connection(alarmsdb):
             user=alarmsdb["username"],
             passwd=alarmsdb["password"],
             db=alarmsdb["dbname"])
-        print(cx)
+        # print(cx)
         yield cx
     finally:
         if cx:
@@ -129,7 +154,7 @@ def cursor(cnx):
 
 def _db_test(db, router):
     with cursor(db) as crs:
-        print(router)
+        # print(router)
         query = "SELECT absid FROM routers WHERE hostname = %s"
         crs.execute(query, (router['hostname'],))
         for (absid,) in crs:
@@ -150,9 +175,9 @@ def get_router_interfaces(router):
     details = {}
     for name, oid in oid_map.items():
         details[name] = walk(router["hostname"], router["community"], oid)
-        print(name)
+        # print(name)
         details[name] = list(details[name])
-        print(details[name])
+        # print(details[name])
 
     v4IfcNames = {}
     for v4IfcName in details["v4InterfaceName"]:
@@ -207,9 +232,9 @@ def cli(config):
             with connection(config["alarms-db"]) as c:
                 _db_test(c, r)
 
+            print(r["hostname"])
             for i in get_router_interfaces(r):
-                print(i)
-            break
+                print("\t%r" % i)
 
 
 if __name__ == "__main__":
diff --git a/requirements.txt b/requirements.txt
index 0a3a0947..e38b2fd1 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -1,3 +1,5 @@
 click
 mysql-connector
 pysnmp
+jsonschema
+pika
diff --git a/setup.py b/setup.py
index d5038a02..1bdb77dd 100644
--- a/setup.py
+++ b/setup.py
@@ -12,6 +12,8 @@ setup(
     install_requires=[
         'click',
         'mysql-connector',
-        'pysnmp'
+        'pysnmp',
+        'jsonschema',
+        'pika'
     ]
 )
-- 
GitLab