From 4779865ea30dba0b5f0594cdc4633b0cc93bf36a Mon Sep 17 00:00:00 2001 From: Petr Viktorin Date: Mon, 21 Jan 2013 08:39:09 -0500 Subject: Replace getList by a get_entries method The find_entries method is cumbersome to use: it requires keyword arguments for simple uses, and callers are tempted to ignore the 'truncated' flag it returns. Introduce a simpler method, get_entries, that returns the found list directly, and raises an errors if the list is truncated. Replace the getList method by get_entries. Part of the work for: https://fedorahosted.org/freeipa/ticket/2660 --- ipaserver/install/adtrustinstance.py | 6 +++--- ipaserver/install/ldapupdate.py | 2 +- ipaserver/install/replication.py | 32 ++++++++++++++++++-------------- ipaserver/ipaldap.py | 28 ++++++++++++++++++---------- 4 files changed, 40 insertions(+), 28 deletions(-) (limited to 'ipaserver') diff --git a/ipaserver/install/adtrustinstance.py b/ipaserver/install/adtrustinstance.py index 52534c19..fe1ef53a 100644 --- a/ipaserver/install/adtrustinstance.py +++ b/ipaserver/install/adtrustinstance.py @@ -261,9 +261,9 @@ class ADTRUSTInstance(service.Service): """ try: - res = self.admin_conn.getList(DN(api.env.container_ranges, self.suffix), - ldap.SCOPE_ONELEVEL, - "(objectclass=ipaDomainIDRange)") + res = self.admin_conn.get_entries( + DN(api.env.container_ranges, self.suffix), + ldap.SCOPE_ONELEVEL, "(objectclass=ipaDomainIDRange)") if len(res) != 1: root_logger.critical("Found more than one ID range for the " \ "local domain.") diff --git a/ipaserver/install/ldapupdate.py b/ipaserver/install/ldapupdate.py index 899e31fc..de1298d8 100644 --- a/ipaserver/install/ldapupdate.py +++ b/ipaserver/install/ldapupdate.py @@ -509,7 +509,7 @@ class LDAPUpdate: sattrs = ["*", "aci", "attributeTypes", "objectClasses"] scope = ldap.SCOPE_BASE - return self.conn.getList(dn, scope, searchfilter, sattrs) + return self.conn.get_entries(dn, scope, searchfilter, sattrs) def _apply_update_disposition(self, updates, entry): """ diff --git a/ipaserver/install/replication.py b/ipaserver/install/replication.py index fcb6ca84..32ea5220 100644 --- a/ipaserver/install/replication.py +++ b/ipaserver/install/replication.py @@ -251,8 +251,9 @@ class ReplicationManager(object): """ filt = self.get_agreement_filter() try: - ents = self.conn.getList(DN(('cn', 'mapping tree'), ('cn', 'config')), - ldap.SCOPE_SUBTREE, filt) + ents = self.conn.get_entries( + DN(('cn', 'mapping tree'), ('cn', 'config')), + ldap.SCOPE_SUBTREE, filt) except errors.NotFound: ents = [] return ents @@ -269,8 +270,9 @@ class ReplicationManager(object): filt = self.get_agreement_filter(IPA_REPLICA) try: - ents = self.conn.getList(DN(('cn', 'mapping tree'), ('cn', 'config')), - ldap.SCOPE_SUBTREE, filt) + ents = self.conn.get_entries( + DN(('cn', 'mapping tree'), ('cn', 'config')), + ldap.SCOPE_SUBTREE, filt) except errors.NotFound: return res @@ -291,8 +293,9 @@ class ReplicationManager(object): filt = self.get_agreement_filter(host=hostname) try: - entries = self.conn.getList(DN(('cn', 'mapping tree'), ('cn', 'config')), - ldap.SCOPE_SUBTREE, filt) + entries = self.conn.get_entries( + DN(('cn', 'mapping tree'), ('cn', 'config')), + ldap.SCOPE_SUBTREE, filt) except errors.NotFound: return None @@ -1031,7 +1034,7 @@ class ReplicationManager(object): newschedule = '2358-2359 0' filter = self.get_agreement_filter(host=hostname) - entries = conn.getList( + entries = conn.get_entries( DN(('cn', 'config')), ldap.SCOPE_SUBTREE, filter) if len(entries) == 0: root_logger.error("Unable to find replication agreement for %s" % @@ -1086,9 +1089,9 @@ class ReplicationManager(object): # delete master kerberos key and all its svc principals try: - filter='(krbprincipalname=*/%s@%s)' % (replica, realm) - entries = self.conn.getList(self.suffix, ldap.SCOPE_SUBTREE, - filterstr=filter) + entries = self.conn.get_entries( + self.suffix, ldap.SCOPE_SUBTREE, + filter='(krbprincipalname=*/%s@%s)' % (replica, realm)) if entries: entries.sort(key=len, reverse=True) for dn in entries: @@ -1128,8 +1131,9 @@ class ReplicationManager(object): # delete master entry with all active services try: - dn = DN(('cn', replica), ('cn', 'masters'), ('cn', 'ipa'), ('cn', 'etc'), self.suffix) - entries = self.conn.getList(dn, ldap.SCOPE_SUBTREE) + dn = DN(('cn', replica), ('cn', 'masters'), ('cn', 'ipa'), + ('cn', 'etc'), self.suffix) + entries = self.conn.get_entries(dn, ldap.SCOPE_SUBTREE) if entries: entries.sort(key=len, reverse=True) for dn in entries: @@ -1145,8 +1149,8 @@ class ReplicationManager(object): try: basedn = DN(('cn', 'etc'), self.suffix) filter = '(dnaHostname=%s)' % replica - entries = self.conn.getList(basedn, ldap.SCOPE_SUBTREE, - filterstr=filter) + entries = self.conn.get_entries( + basedn, ldap.SCOPE_SUBTREE, filter=filter) if len(entries) != 0: for e in entries: self.conn.deleteEntry(e.dn) diff --git a/ipaserver/ipaldap.py b/ipaserver/ipaldap.py index 7fceb889..78021201 100644 --- a/ipaserver/ipaldap.py +++ b/ipaserver/ipaldap.py @@ -1069,6 +1069,24 @@ class LDAPConnection(object): ) return self.combine_filters(flts, rules) + def get_entries(self, base_dn, scope=None, filter=None, attrs_list=None): + """Return a list of matching entries. + + Raises an error if the list is truncated by the server + + :param base_dn: dn of the entry at which to start the search + :param scope: search scope, see LDAP docs (default ldap2.SCOPE_SUBTREE) + :param filter: LDAP filter to apply + :param attrs_list: ist of attributes to return, all if None (default) + + Use the find_entries method for more options. + """ + entries, truncated = self.find_entries( + base_dn=base_dn, scope=scope, filter=filter, attrs_list=attrs_list) + if truncated: + raise errors.LimitsExceeded() + return entries + def find_entries(self, filter=None, attrs_list=None, base_dn=None, scope=_ldap.SCOPE_SUBTREE, time_limit=None, size_limit=None, normalize=True, search_refs=False): @@ -1629,16 +1647,6 @@ class IPAdmin(LDAPConnection): ) return result[0] - def getList(self, base, scope, filterstr='(objectClass=*)', attrlist=None): - # FIXME: for backwards compatibility only - result, truncated = self.find_entries( - filter=filterstr, - attrs_list=attrlist, - base_dn=base, - scope=scope, - ) - return result - def addEntry(self, entry): # FIXME: for backwards compatibility only self.add_entry(entry.dn, entry) -- cgit