On 09/13/2012 09:19 PM, Rob Crittenden wrote:
> Martin Kosek wrote:
>> When ADD command is being executed and a single-value object attribute
>> is being set with both option and addattr IPA ends up in an internal
>> error.
>>
>> Make better value sanitizing job in this case and let IPA throw
>> a user-friendly error. Unit test exercising this situation is added.
>>
>> https://fedorahosted.org/freeipa/ticket/2429
> 
> +                if not isinstance(val, (list, tuple)):
> +                    val = [val]
> +            val.extend(adddict[attr])
> 
> I val is a tuple the extend is still going to fail. Can val ever be a tuple? 
> If
> so we'd need to convert it to a list.
> 
> rob

I don't think it could be a tuple, I am about 99% certain. So for this 1% I
added a special clause for tuple. Patch attached.

We will be able to be even more certain when Honza finishes his strict encoding
patch he was working on in the summer. With his patch, the attributes should
always be a list.

Martin
From 915f8ae9540b71f009c43b308bb21ebe6a2e2849 Mon Sep 17 00:00:00 2001
From: Martin Kosek <mko...@redhat.com>
Date: Thu, 13 Sep 2012 15:51:51 +0200
Subject: [PATCH] Fix addattr internal error

When ADD command is being executed and a single-value object attribute
is being set with both option and addattr IPA ends up in an internal
error.

Make better value sanitizing job in this case and let IPA throw
a user-friendly error. Unit test exercising this situation is added.

https://fedorahosted.org/freeipa/ticket/2429
---
 ipalib/plugins/baseldap.py     | 12 +++++++++++-
 tests/test_xmlrpc/test_attr.py | 10 ++++++++++
 2 files changed, 21 insertions(+), 1 deletion(-)

diff --git a/ipalib/plugins/baseldap.py b/ipalib/plugins/baseldap.py
index 6a054ffd801b159769bc2ce2871cb03afeba5c3d..14a46f2d0344c4276ec98091314b15e6e552ed77 100644
--- a/ipalib/plugins/baseldap.py
+++ b/ipalib/plugins/baseldap.py
@@ -882,7 +882,17 @@ last, after all sets and adds."""),
             entry_attrs[attr] = val
 
         for attr in direct_add:
-            entry_attrs.setdefault(attr, []).extend(adddict[attr])
+            try:
+                val = entry_attrs[attr]
+            except KeyError:
+                val = []
+            else:
+                if not isinstance(val, (list, tuple)):
+                    val = [val]
+                elif isinstance(val, tuple):
+                    val = list(val)
+            val.extend(adddict[attr])
+            entry_attrs[attr] = val
 
         for attr in direct_del:
             for delval in deldict[attr]:
diff --git a/tests/test_xmlrpc/test_attr.py b/tests/test_xmlrpc/test_attr.py
index f5353e1b217fec96e18353923a11b509224a9083..39320875bd5edd4fd6022ed66ce1a8b87ccc8e92 100644
--- a/tests/test_xmlrpc/test_attr.py
+++ b/tests/test_xmlrpc/test_attr.py
@@ -37,6 +37,16 @@ class test_attr(Declarative):
     tests = [
 
         dict(
+            desc='Try to add user %r with single-value attribute set via '
+                 'option and --addattr' % user1,
+            command=(
+                'user_add', [user1], dict(givenname=u'Test', sn=u'User1',
+                    addattr=u'sn=User2')
+            ),
+            expected=errors.OnlyOneValueAllowed(attr='sn'),
+        ),
+
+        dict(
             desc='Create %r' % user1,
             command=(
                 'user_add', [user1], dict(givenname=u'Test', sn=u'User1',
-- 
1.7.11.4

_______________________________________________
Freeipa-devel mailing list
Freeipa-devel@redhat.com
https://www.redhat.com/mailman/listinfo/freeipa-devel

Reply via email to