[Freeipa-devel] [PATCH] 0210 frontend: fix output validation for multiple type choices

Alexander Bokovoy abokovoy at redhat.com
Sat Jul 16 10:46:55 UTC 2016


Hi,

I had some time and was blocked by these bugs to do my tickets so I
actually fixed these three problems that are assigned to Martin
Babinsky. Hopefully, Martin wouldn't be offended by that. :)

------

Output entry elements may have multiple types allowed. We need to check
all of them to properly validate the output. Right now, thin client
receives type specifications for elements as tuples of types, so
what is seen as 'None' on the server side becomes (type(None),) tuple
on the thin client side.

Change validation to account this by processing each separate type
of the element and account for both None and type(None). Raise type
error only if all of the type checks failed.

https://fedorahosted.org/freeipa/ticket/6061

-- 
/ Alexander Bokovoy
-------------- next part --------------
From e5483de1a84b1bf777e98fa5ac2258168ef10686 Mon Sep 17 00:00:00 2001
From: Alexander Bokovoy <abokovoy at redhat.com>
Date: Sat, 16 Jul 2016 12:52:40 +0300
Subject: [PATCH 1/3] frontend: fix output validation for multiple type choices

Output entry elements may have multiple types allowed. We need to check
all of them to properly validate the output. Right now, thin client
receives type specifications for elements as tuples of types, so
what is seen as 'None' on the server side becomes (type(None),) tuple
on the thin client side.

Change validation to account this by processing each separate type
of the element and account for both None and type(None). Raise type
error only if all of the type checks failed.

https://fedorahosted.org/freeipa/ticket/6061
---
 ipalib/frontend.py | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)

diff --git a/ipalib/frontend.py b/ipalib/frontend.py
index cb00841..2aeaa93 100644
--- a/ipalib/frontend.py
+++ b/ipalib/frontend.py
@@ -986,9 +986,21 @@ class Command(HasParam):
                 raise ValueError('%s: unexpected keys %r in %r' % (
                     nice, sorted(extra), output)
                 )
+        # Validate each field of the output
+        # Multiple types can be allowed for the field,
+        # thus, we have to verify against each possible one.
+        # Also, there might be None and type(None) as a type,
+        # thus they need to be checked separately.
+        # Raise exception only if all possible type checks failed
         for o in self.output():
             value = output[o.name]
-            if not (o.type is None or isinstance(value, o.type)):
+            types = o.type if isinstance(o.type, tuple) else (o.type,)
+            r = False
+            for t in types:
+                r = t is None or t is type(None) or isinstance(value, t)
+                if r:
+                    break
+            if not r:
                 raise TypeError('%s:\n  output[%r]: need %r; got %r: %r' % (
                     nice, o.name, o.type, type(value), value)
                 )
-- 
2.7.4



More information about the Freeipa-devel mailing list