Base64-decode unicode values in Bytes parameters.

Fix wrong handling of strings in --setattr/--addattr/--delattr.

These changes make it possible to use Bytes in --setattr/--addattr/
--delattr without errors.

Fixes managing SSH keys on command-line

https://fedorahosted.org/freeipa/ticket/754
This commit is contained in:
Jan Cholasta 2012-01-23 09:50:41 -05:00 committed by Rob Crittenden
parent c00bf9e38a
commit ca3f304110
3 changed files with 19 additions and 9 deletions

View File

@ -1054,11 +1054,7 @@ class cli(backend.Executioner):
Decode param values if appropriate.
"""
for (key, value) in kw.iteritems():
param = cmd.params[key]
if isinstance(param, Bytes):
yield (key, value)
else:
yield (key, self.Backend.textui.decode(value))
yield (key, self.Backend.textui.decode(value))
def build_parser(self, cmd):
parser = CLIOptionParser(

View File

@ -101,6 +101,10 @@ a more detailed description for clarity.
import re
import decimal
import base64
import csv
from xmlrpclib import MAXINT, MININT
from types import NoneType
from util import make_repr
from text import _ as ugettext
@ -109,8 +113,6 @@ from errors import ConversionError, RequirementError, ValidationError
from errors import PasswordMismatch
from constants import NULLS, TYPE_ERROR, CALLABLE_ERROR
from text import Gettext, FixMe
import csv
from xmlrpclib import MAXINT, MININT
class DefaultFrom(ReadOnly):
@ -1440,6 +1442,14 @@ class Bytes(Data):
length=self.length,
)
def _convert_scalar(self, value, index=None):
if isinstance(value, unicode):
try:
value = base64.b64decode(value)
except TypeError:
raise ConversionError(name=self.name, index=index, error=self.type_error)
return super(Bytes, self)._convert_scalar(value, index)
class Str(Data):
"""

View File

@ -24,6 +24,7 @@ import re
import json
import time
from copy import deepcopy
import base64
from ipalib import api, crud, errors
from ipalib import Method, Object, Command
@ -862,6 +863,9 @@ last, after all sets and adds."""),
try:
entry_attrs[attr].remove(delval)
except ValueError:
if isinstance(delval, str):
# This is a Binary value, base64 encode it
delval = unicode(base64.b64encode(delval))
raise errors.AttrValueNotFound(attr=attr, value=delval)
# normalize all values
@ -871,8 +875,8 @@ last, after all sets and adds."""),
entry_attrs[attr] = list(set([val for val in entry_attrs[attr] if val]))
if not entry_attrs[attr]:
entry_attrs[attr] = None
elif len(entry_attrs[attr]) == 1:
entry_attrs[attr] = entry_attrs[attr][0]
elif isinstance(entry_attrs[attr], (tuple, list)) and len(entry_attrs[attr]) == 1:
entry_attrs[attr] = entry_attrs[attr][0]
class LDAPCreate(BaseLDAPCommand, crud.Create):
"""