From 29a32ce59d7986ee811ffa431fd3df557b1a2785 Mon Sep 17 00:00:00 2001
From: Michael Hanselmann <hansmi@google.com>
Date: Wed, 28 Nov 2012 13:11:08 +0100
Subject: [PATCH] ssconf: Add function to verify keys

The new utility for configuring the node daemon will have to check
whether it received valid ssconf names.

Signed-off-by: Michael Hanselmann <hansmi@google.com>
Reviewed-by: Guido Trotter <ultrotter@google.com>
---
 lib/ssconf.py                  | 14 ++++++++++++++
 test/ganeti.ssconf_unittest.py | 15 +++++++++++++++
 2 files changed, 29 insertions(+)

diff --git a/lib/ssconf.py b/lib/ssconf.py
index 7e34b5d3b..7823e6624 100644
--- a/lib/ssconf.py
+++ b/lib/ssconf.py
@@ -387,3 +387,17 @@ def VerifyClusterName(name, _cfg_location=None):
   else:
     if name != local_name:
       raise errors.GenericError("Current cluster name is '%s'" % local_name)
+
+
+def VerifyKeys(keys):
+  """Raises an exception if unknown ssconf keys are given.
+
+  @type keys: sequence
+  @param keys: Key names to verify
+  @raise errors.GenericError: When invalid keys were found
+
+  """
+  invalid = frozenset(keys) - _VALID_KEYS
+  if invalid:
+    raise errors.GenericError("Invalid ssconf keys: %s" %
+                              utils.CommaJoin(sorted(invalid)))
diff --git a/test/ganeti.ssconf_unittest.py b/test/ganeti.ssconf_unittest.py
index 1db88e889..958859b35 100755
--- a/test/ganeti.ssconf_unittest.py
+++ b/test/ganeti.ssconf_unittest.py
@@ -169,5 +169,20 @@ class TestVerifyClusterName(unittest.TestCase):
                         "cluster.example.com", _cfg_location=self.tmpdir)
 
 
+class TestVerifyKeys(unittest.TestCase):
+  def testNoKeys(self):
+    ssconf.VerifyKeys({})
+
+  def testValidKeys(self):
+    ssconf.VerifyKeys(ssconf._VALID_KEYS)
+
+    for key in ssconf._VALID_KEYS:
+      ssconf.VerifyKeys([key])
+
+  def testInvalidKeys(self):
+    for key in ["", ".", " ", "foo", "bar", "HelloWorld"]:
+      self.assertRaises(errors.GenericError, ssconf.VerifyKeys, [key])
+
+
 if __name__ == "__main__":
   testutils.GanetiTestProgram()
-- 
GitLab