From 71f960457ed2e2fe53c4c8ea5c37b50180d89a6a Mon Sep 17 00:00:00 2001 From: Jan Cholasta Date: Thu, 19 May 2016 14:19:07 +0200 Subject: ipalib: make optional positional command arguments actually optional Fix several plugins not to assume optional positional arguments have a value of None when not specified. https://fedorahosted.org/freeipa/ticket/4739 Reviewed-By: David Kupka --- ipalib/plugins/aci.py | 2 +- ipalib/plugins/baseldap.py | 11 ++++++++--- ipalib/plugins/batch.py | 4 ++-- ipalib/plugins/delegation.py | 2 +- ipalib/plugins/dns.py | 9 +++++---- ipalib/plugins/group.py | 5 +++-- ipalib/plugins/internal.py | 2 +- ipalib/plugins/krbtpolicy.py | 16 +++++++++++----- ipalib/plugins/misc.py | 2 +- ipalib/plugins/otptoken.py | 3 +++ ipalib/plugins/pwpolicy.py | 6 ++++++ ipalib/plugins/selfservice.py | 2 +- ipalib/plugins/servicedelegation.py | 3 +-- 13 files changed, 44 insertions(+), 23 deletions(-) (limited to 'ipalib') diff --git a/ipalib/plugins/aci.py b/ipalib/plugins/aci.py index 0c46359f9..17d0e343d 100644 --- a/ipalib/plugins/aci.py +++ b/ipalib/plugins/aci.py @@ -700,7 +700,7 @@ class aci_find(crud.Search): takes_options = (_prefix_option.clone_rename("aciprefix?", required=False), gen_pkey_only_option("name"),) - def execute(self, term, **kw): + def execute(self, term=None, **kw): ldap = self.api.Backend.ldap2 entry = ldap.get_entry(self.api.env.basedn, ['aci']) diff --git a/ipalib/plugins/baseldap.py b/ipalib/plugins/baseldap.py index 6fd1c394b..752919c6b 100644 --- a/ipalib/plugins/baseldap.py +++ b/ipalib/plugins/baseldap.py @@ -2017,9 +2017,14 @@ class LDAPSearch(BaseLDAPCommand, crud.Search): def execute(self, *args, **options): ldap = self.obj.backend - term = args[-1] + index = tuple(self.args).index('criteria') + keys = args[:index] + try: + term = args[index] + except IndexError: + term = None if self.obj.parent_object: - base_dn = self.api.Object[self.obj.parent_object].get_dn(*args[:-1]) + base_dn = self.api.Object[self.obj.parent_object].get_dn(*keys) else: base_dn = DN(self.obj.container_dn, api.env.basedn) assert isinstance(base_dn, DN) @@ -2083,7 +2088,7 @@ class LDAPSearch(BaseLDAPCommand, crud.Search): except errors.EmptyResult: (entries, truncated) = ([], False) except errors.NotFound: - self.api.Object[self.obj.parent_object].handle_not_found(*args[:-1]) + self.api.Object[self.obj.parent_object].handle_not_found(*keys) for callback in self.get_callbacks('post'): truncated = callback(self, ldap, entries, truncated, *args, **options) diff --git a/ipalib/plugins/batch.py b/ipalib/plugins/batch.py index 517f4b92b..84a650575 100644 --- a/ipalib/plugins/batch.py +++ b/ipalib/plugins/batch.py @@ -87,9 +87,9 @@ class batch(Command): Output('results', (list, tuple), doc='') ) - def execute(self, *args, **options): + def execute(self, methods=None, **options): results = [] - for arg in (args[0] or []): + for arg in (methods or []): params = dict() name = None try: diff --git a/ipalib/plugins/delegation.py b/ipalib/plugins/delegation.py index ca30890f7..c502c38fa 100644 --- a/ipalib/plugins/delegation.py +++ b/ipalib/plugins/delegation.py @@ -197,7 +197,7 @@ class delegation_find(crud.Search): takes_options = (gen_pkey_only_option("name"),) has_output_params = output_params - def execute(self, term, **kw): + def execute(self, term=None, **kw): kw['aciprefix'] = ACI_PREFIX results = api.Command['aci_find'](term, **kw)['result'] diff --git a/ipalib/plugins/dns.py b/ipalib/plugins/dns.py index 8a31dcfeb..d30f50b29 100644 --- a/ipalib/plugins/dns.py +++ b/ipalib/plugins/dns.py @@ -1654,8 +1654,8 @@ def _convert_to_idna(value): pass return None -def _create_idn_filter(cmd, ldap, *args, **options): - term = args[-1] + +def _create_idn_filter(cmd, ldap, term=None, **options): if term: #include idna values to search term_idna = _convert_to_idna(term) @@ -4191,11 +4191,12 @@ class dnsrecord_find(LDAPSearch): continue yield option - def pre_callback(self, ldap, filter, attrs_list, base_dn, scope, *args, **options): + def pre_callback(self, ldap, filter, attrs_list, base_dn, scope, + dnszoneidnsname, *args, **options): assert isinstance(base_dn, DN) # validate if zone is master zone - self.obj.check_zone(args[-2], **options) + self.obj.check_zone(dnszoneidnsname, **options) filter = _create_idn_filter(self, ldap, *args, **options) return (filter, base_dn, ldap.SCOPE_SUBTREE) diff --git a/ipalib/plugins/group.py b/ipalib/plugins/group.py index b9d38592a..9b2847cf3 100644 --- a/ipalib/plugins/group.py +++ b/ipalib/plugins/group.py @@ -455,7 +455,8 @@ class group_find(LDAPSearch): ), ) - def pre_callback(self, ldap, filter, attrs_list, base_dn, scope, *args, **options): + def pre_callback(self, ldap, filter, attrs_list, base_dn, scope, + criteria=None, **options): assert isinstance(base_dn, DN) # filter groups by pseudo type @@ -485,7 +486,7 @@ class group_find(LDAPSearch): if len(attrs) == 1 and isinstance(attrs[0], six.string_types): search_attrs = attrs[0].split(',') for a in search_attrs: - search_kw[a] = args[-1] + search_kw[a] = criteria cflt = ldap.make_filter(search_kw, exact=False) filter = ldap.combine_filters((oflt, cflt), rules=ldap.MATCH_ALL) diff --git a/ipalib/plugins/internal.py b/ipalib/plugins/internal.py index 54871f76d..c28693a46 100644 --- a/ipalib/plugins/internal.py +++ b/ipalib/plugins/internal.py @@ -70,7 +70,7 @@ class json_metadata(Command): Output('commands', dict, doc=_('Dict of JSON encoded IPA Commands')), ) - def execute(self, objname, methodname, **options): + def execute(self, objname=None, methodname=None, **options): objects = dict() methods = dict() commands = dict() diff --git a/ipalib/plugins/krbtpolicy.py b/ipalib/plugins/krbtpolicy.py index fad03beab..3f0d4c857 100644 --- a/ipalib/plugins/krbtpolicy.py +++ b/ipalib/plugins/krbtpolicy.py @@ -149,6 +149,9 @@ class krbtpolicy(baseldap.LDAPObject): class krbtpolicy_mod(baseldap.LDAPUpdate): __doc__ = _('Modify Kerberos ticket policy.') + def execute(self, uid=None, **options): + return super(krbtpolicy_mod, self).execute(uid, **options) + def pre_callback(self, ldap, dn, entry_attrs, attrs_list, *keys, **options): assert isinstance(dn, DN) # disable all flag @@ -162,6 +165,9 @@ class krbtpolicy_mod(baseldap.LDAPUpdate): class krbtpolicy_show(baseldap.LDAPRetrieve): __doc__ = _('Display the current Kerberos ticket policy.') + def execute(self, uid=None, **options): + return super(krbtpolicy_show, self).execute(uid, **options) + def pre_callback(self, ldap, dn, attrs_list, *keys, **options): assert isinstance(dn, DN) # disable all flag @@ -206,14 +212,14 @@ class krbtpolicy_reset(baseldap.LDAPQuery): has_output = output.standard_entry - def execute(self, *keys, **options): + def execute(self, uid=None, **options): ldap = self.obj.backend - dn = self.obj.get_dn(*keys, **options) + dn = self.obj.get_dn(uid, **options) def_values = {} # if reseting policy for a user - just his values - if keys[-1] is not None: + if uid is not None: for a in self.obj.default_attributes: def_values[a] = None # if reseting global policy - set values to default @@ -227,11 +233,11 @@ class krbtpolicy_reset(baseldap.LDAPQuery): except errors.EmptyModlist: pass - if keys[-1] is not None: + if uid is not None: # policy for user was deleted, retrieve global policy dn = self.obj.get_dn(None) entry_attrs = ldap.get_entry(dn, self.obj.default_attributes) entry_attrs = entry_to_dict(entry_attrs, **options) - return dict(result=entry_attrs, value=pkey_to_value(keys[-1], options)) + return dict(result=entry_attrs, value=pkey_to_value(uid, options)) diff --git a/ipalib/plugins/misc.py b/ipalib/plugins/misc.py index 4284f0c6c..23773c28f 100644 --- a/ipalib/plugins/misc.py +++ b/ipalib/plugins/misc.py @@ -85,7 +85,7 @@ class env(LocalOrRemote): keys.add(query) return keys - def execute(self, variables, **options): + def execute(self, variables=None, **options): if variables is None: keys = self.env else: diff --git a/ipalib/plugins/otptoken.py b/ipalib/plugins/otptoken.py index 9ea2c3726..0303356bc 100644 --- a/ipalib/plugins/otptoken.py +++ b/ipalib/plugins/otptoken.py @@ -291,6 +291,9 @@ class otptoken_add(LDAPCreate): Str('uri?', label=_('URI')), ) + def execute(self, ipatokenuniqueid=None, **options): + return super(otptoken_add, self).execute(ipatokenuniqueid, **options) + def pre_callback(self, ldap, dn, entry_attrs, attrs_list, *keys, **options): # Fill in a default UUID when not specified. if entry_attrs.get('ipatokenuniqueid', None) is None: diff --git a/ipalib/plugins/pwpolicy.py b/ipalib/plugins/pwpolicy.py index 86c559b7d..ec1bf1cc7 100644 --- a/ipalib/plugins/pwpolicy.py +++ b/ipalib/plugins/pwpolicy.py @@ -488,6 +488,9 @@ class pwpolicy_del(LDAPDelete): class pwpolicy_mod(LDAPUpdate): __doc__ = _('Modify a group password policy.') + def execute(self, cn=None, **options): + return super(pwpolicy_mod, self).execute(cn, **options) + def pre_callback(self, ldap, dn, entry_attrs, attrs_list, *keys, **options): assert isinstance(dn, DN) self.obj.convert_time_on_input(entry_attrs) @@ -538,6 +541,9 @@ class pwpolicy_show(LDAPRetrieve): ), ) + def execute(self, cn=None, **options): + return super(pwpolicy_show, self).execute(cn, **options) + def pre_callback(self, ldap, dn, attrs_list, *keys, **options): assert isinstance(dn, DN) if options.get('user') is not None: diff --git a/ipalib/plugins/selfservice.py b/ipalib/plugins/selfservice.py index f733768bc..47a1f2713 100644 --- a/ipalib/plugins/selfservice.py +++ b/ipalib/plugins/selfservice.py @@ -194,7 +194,7 @@ class selfservice_find(crud.Search): takes_options = (gen_pkey_only_option("name"),) has_output_params = output_params - def execute(self, term, **kw): + def execute(self, term=None, **kw): kw['selfaci'] = True kw['aciprefix'] = ACI_PREFIX result = api.Command['aci_find'](term, **kw)['result'] diff --git a/ipalib/plugins/servicedelegation.py b/ipalib/plugins/servicedelegation.py index 3637b9180..db538c0d9 100644 --- a/ipalib/plugins/servicedelegation.py +++ b/ipalib/plugins/servicedelegation.py @@ -500,7 +500,7 @@ class servicedelegationtarget_find(LDAPSearch): ) def pre_callback(self, ldap, filters, attrs_list, base_dn, scope, - *args, **options): + term=None, **options): """ Exclude rules from the search output. A target contains a subset of a rule objectclass. @@ -515,7 +515,6 @@ class servicedelegationtarget_find(LDAPSearch): ) search_kw = {} - term = args[-1] for a in self.obj.default_attributes: search_kw[a] = term -- cgit