diff options
author | Bogdano Arendartchuk <bogdano@mandriva.org> | 2007-05-03 20:47:35 +0000 |
---|---|---|
committer | Bogdano Arendartchuk <bogdano@mandriva.org> | 2007-05-03 20:47:35 +0000 |
commit | 2779c2048793ba84e291bffc6554f5cc5cbe39e2 (patch) | |
tree | 246d2afab687f5d01f4a5cb523634d71f06f176a | |
parent | c6d5c4798d57d88d13292810639e985e6a08aff3 (diff) | |
download | mgarepo-2779c2048793ba84e291bffc6554f5cc5cbe39e2.tar mgarepo-2779c2048793ba84e291bffc6554f5cc5cbe39e2.tar.gz mgarepo-2779c2048793ba84e291bffc6554f5cc5cbe39e2.tar.bz2 mgarepo-2779c2048793ba84e291bffc6554f5cc5cbe39e2.tar.xz mgarepo-2779c2048793ba84e291bffc6554f5cc5cbe39e2.zip |
As requested by Andreas, handle LDAP exceptions and provide better error
messages. Also improved format error messages a bit.
-rw-r--r-- | RepSys/plugins/ldapusers.py | 43 |
1 files changed, 30 insertions, 13 deletions
diff --git a/RepSys/plugins/ldapusers.py b/RepSys/plugins/ldapusers.py index 7f08f56..3b83ae2 100644 --- a/RepSys/plugins/ldapusers.py +++ b/RepSys/plugins/ldapusers.py @@ -55,11 +55,28 @@ from RepSys import Error, config users_cache = {} +class LDAPError(Error): + def __init__(self, ldaperr): + self.ldaperr = ldaperr + name = ldaperr.__class__.__name__ + desc = ldaperr.message["desc"] + self.message = "LDAP error %s: %s" % (name, desc) + self.args = self.message, + def strip_entry(entry): "Leave only the first value in all keys in the entry" new = dict((key, value[0]) for key, value in entry.iteritems()) return new +def interpolate(optname, format, data): + try: + return format % data + except KeyError, e: + raise Error, "the key %s was not found in LDAP search, " \ + "check your %s configuration" % (e, optname) + except (TypeError, ValueError), e: + raise Error, "LDAP response formatting error: %s. Check " \ + "your %s configuration" % (e, optname) def make_handler(): server = config.get("global", "ldap-server") @@ -93,22 +110,22 @@ def make_handler(): if value is not None: return value - l = ldap.open(server) - if binddn: - l.bind(binddn, bindpw) - filter = filterformat % option - found = l.search_s(basedn, ldap.SCOPE_SUBTREE, filter) + try: + l = ldap.open(server) + if binddn: + l.bind(binddn, bindpw) + except ldap.LDAPError, e: + raise LDAPError(e) + + filter = interpolate("ldap-filterformat", filterformat, option) + try: + found = l.search_s(basedn, ldap.SCOPE_SUBTREE, filter) + except ldap.LDAPError, e: + raise LDAPError(e) if found: dn, entry = found[0] entry = strip_entry(entry) - try: - value = format % entry - except KeyError, e: - raise Error, "the key %s was not found in LDAP search, " \ - "check your ldap-format configuration" % e - except (TypeError, ValueError), e: - raise Error, "LDAP response formatting error: %s. Check " \ - "your ldap-format configuration" % e + value = interpolate("ldap-format", format, entry) else: # issue a warning? value = config.get(section, option, default, wrap=False) |