Hi,

the attached patch fixes:
<https://fedorahosted.org/freeipa/ticket/5386>
<https://fedorahosted.org/freeipa/ticket/5391>
<https://fedorahosted.org/freeipa/ticket/5392>

Honza

--
Jan Cholasta
From 804d9f99096fdc0e560d1853ac0ca4ffb93b364f Mon Sep 17 00:00:00 2001
From: Jan Cholasta <jchol...@redhat.com>
Date: Mon, 2 Nov 2015 15:32:35 +0100
Subject: [PATCH] install: fix command line option validation

The code which calls the validators was accidentally removed, re-add it.

https://fedorahosted.org/freeipa/ticket/5386
https://fedorahosted.org/freeipa/ticket/5391
https://fedorahosted.org/freeipa/ticket/5392
---
 ipapython/install/cli.py  |  7 +++++--
 ipapython/install/core.py | 13 ++++++++++++-
 2 files changed, 17 insertions(+), 3 deletions(-)

diff --git a/ipapython/install/cli.py b/ipapython/install/cli.py
index 0047eec..d2250e5 100644
--- a/ipapython/install/cli.py
+++ b/ipapython/install/cli.py
@@ -282,7 +282,8 @@ class ConfigureTool(admintool.AdminTool):
         kwargs = {}
 
         transformed_cls = self._transform(self.configurable_class)
-        for owner_cls, name in transformed_cls.knobs():
+        knob_classes = {n: getattr(c, n) for c, n in transformed_cls.knobs()}
+        for name in knob_classes:
             value = getattr(self.options, name, None)
             if value is not None:
                 kwargs[name] = value
@@ -294,8 +295,10 @@ class ConfigureTool(admintool.AdminTool):
         try:
             cfgr = transformed_cls(**kwargs)
         except core.KnobValueError as e:
-            knob_cls = getattr(transformed_cls, e.name)
+            knob_cls = knob_classes[e.name]
             try:
+                if self.positional_arguments is None:
+                    raise IndexError
                 index = self.positional_arguments.index(e.name)
             except IndexError:
                 cli_name = knob_cls.cli_name or e.name.replace('_', '-')
diff --git a/ipapython/install/core.py b/ipapython/install/core.py
index 479149b..8e3ba58 100644
--- a/ipapython/install/core.py
+++ b/ipapython/install/core.py
@@ -118,6 +118,16 @@ class KnobBase(PropertyBase):
     def __init__(self, outer):
         self.outer = outer
 
+    def __set__(self, obj, value):
+        try:
+            self.validate(value)
+        except KnobValueError:
+            raise
+        except ValueError as e:
+            raise KnobValueError(self.__outer_name__, str(e))
+
+        super(KnobBase, self).__set__(obj, value)
+
     def validate(self, value):
         pass
 
@@ -243,7 +253,8 @@ class Configurable(six.with_metaclass(abc.ABCMeta, object)):
             except KeyError:
                 pass
             else:
-                setattr(self, name, value)
+                prop = prop_cls(self)
+                prop.__set__(self, value)
 
         if kwargs:
             extra = sorted(kwargs)
-- 
2.6.2

From a97c68f55a0d45d48ed21fb394fef13490102b80 Mon Sep 17 00:00:00 2001
From: Jan Cholasta <jchol...@redhat.com>
Date: Mon, 2 Nov 2015 15:32:35 +0100
Subject: [PATCH] install: fix command line option validation

The code which calls the validators was accidentally removed, re-add it.

https://fedorahosted.org/freeipa/ticket/5386
https://fedorahosted.org/freeipa/ticket/5391
https://fedorahosted.org/freeipa/ticket/5392
---
 ipapython/install/cli.py  |  7 +++++--
 ipapython/install/core.py | 13 ++++++++++++-
 2 files changed, 17 insertions(+), 3 deletions(-)

diff --git a/ipapython/install/cli.py b/ipapython/install/cli.py
index 63a2ee6..9331174 100644
--- a/ipapython/install/cli.py
+++ b/ipapython/install/cli.py
@@ -274,7 +274,8 @@ class ConfigureTool(admintool.AdminTool):
         kwargs = {}
 
         transformed_cls = self._transform(self.configurable_class)
-        for owner_cls, name in transformed_cls.knobs():
+        knob_classes = {n: getattr(c, n) for c, n in transformed_cls.knobs()}
+        for name in knob_classes:
             value = getattr(self.options, name, None)
             if value is not None:
                 kwargs[name] = value
@@ -286,8 +287,10 @@ class ConfigureTool(admintool.AdminTool):
         try:
             cfgr = transformed_cls(**kwargs)
         except core.KnobValueError as e:
-            knob_cls = getattr(transformed_cls, e.name)
+            knob_cls = knob_classes[e.name]
             try:
+                if self.positional_arguments is None:
+                    raise IndexError
                 index = self.positional_arguments.index(e.name)
             except IndexError:
                 cli_name = knob_cls.cli_name or e.name.replace('_', '-')
diff --git a/ipapython/install/core.py b/ipapython/install/core.py
index 3abb66e..6b2da05 100644
--- a/ipapython/install/core.py
+++ b/ipapython/install/core.py
@@ -118,6 +118,16 @@ class KnobBase(PropertyBase):
     def __init__(self, outer):
         self.outer = outer
 
+    def __set__(self, obj, value):
+        try:
+            self.validate(value)
+        except KnobValueError:
+            raise
+        except ValueError as e:
+            raise KnobValueError(self.__outer_name__, str(e))
+
+        super(KnobBase, self).__set__(obj, value)
+
     def validate(self, value):
         pass
 
@@ -245,7 +255,8 @@ class Configurable(object):
             except KeyError:
                 pass
             else:
-                setattr(self, name, value)
+                prop = prop_cls(self)
+                prop.__set__(self, value)
 
         if kwargs:
             extra = sorted(kwargs.keys())
-- 
2.6.2

-- 
Manage your subscription for the Freeipa-devel mailing list:
https://www.redhat.com/mailman/listinfo/freeipa-devel
Contribute to FreeIPA: http://www.freeipa.org/page/Contribute/Code

Reply via email to