From a9a942a35f82e00fa46ade19e78e4e782dab136f Mon Sep 17 00:00:00 2001 From: William Brown Date: Jun 23 2017 01:15:14 +0000 Subject: Ticket 67 - get attr by type Bug Description: In python 3 the change to utf8 has caused us some difficulty. As a result, to help ease this, we need to be able to retrieve attributes by types in DS Fix Description: In mapped object, add the ability to request types for searches when we perform them. https://pagure.io/lib389/issue/67 Author: wibrown Review by: ilias95, spichugi (Thanks!) --- diff --git a/lib389/_mapped_object.py b/lib389/_mapped_object.py index 361b479..c9b7eb9 100644 --- a/lib389/_mapped_object.py +++ b/lib389/_mapped_object.py @@ -335,6 +335,24 @@ class DSLdapObject(DSLogging): entry = self._instance.search_s(self._dn, ldap.SCOPE_BASE, attrlist=[key])[0] return entry.getValue(key) + def get_attr_val_bytes(self, key): + return ensure_bytes(self.get_attr_val(key)) + + def get_attr_vals_bytes(self, key): + return ensure_list_bytes(self.get_attrs_val(key)) + + def get_attr_val_utf8(self, key): + return ensure_str(self.get_attr_val(key)) + + def get_attr_vals_utf8(self, key): + return ensure_list_str(self.get_attrs_val(key)) + + def get_attr_val_int(self, key): + return int(self.get_attr_val(key)) + + def get_attr_vals_int(self, key): + return [int(v) for v in self.get_attrs_val(key)] + # Duplicate, but with many values. IE a dict api. # This def add_values(self, values): diff --git a/lib389/backend.py b/lib389/backend.py index 1affe13..3e208bf 100644 --- a/lib389/backend.py +++ b/lib389/backend.py @@ -463,7 +463,7 @@ class Backend(DSLdapObject): raise ldap.UNWILLING_TO_PERFORM("This is a protected backend!") # First check if the mapping tree has our suffix still. # suffix = self.get_attr_val('nsslapd-suffix') - bename = ensure_str(self.get_attr_val('cn')) + bename = self.get_attr_val_utf8('cn') try: mt = self._mts.get(selector=bename) # Assert the type is "backend" @@ -492,11 +492,11 @@ class Backend(DSLdapObject): * missing indcies if we are local and have log access? """ # Check for the missing mapping tree. - suffix = ensure_str(self.get_attr_val('nsslapd-suffix')) - bename = self.get_attr_val('cn') + suffix = self.get_attr_val_utf8('nsslapd-suffix') + bename = self.get_attr_val_bytes('cn') try: mt = self._mts.get(suffix) - if mt.get_attr_val('nsslapd-backend') != ensure_bytes(bename) and mt.get_attr_val('nsslapd-state') != ensure_bytes('backend') : + if mt.get_attr_val_bytes('nsslapd-backend') != bename and mt.get_attr_val('nsslapd-state') != ensure_bytes('backend') : raise ldap.NO_SUCH_OBJECT("We have a matching suffix, but not a backend or correct database name.") except ldap.NO_SUCH_OBJECT: result = DSBLE0001 diff --git a/lib389/config.py b/lib389/config.py index 0723214..0a7539b 100644 --- a/lib389/config.py +++ b/lib389/config.py @@ -191,10 +191,8 @@ class Config(DSLdapObject): def _lint_passwordscheme(self): allowed_schemes = ['SSHA512', 'PBKDF2_SHA256'] - password_scheme = self.get_attr_val('passwordStorageScheme') - root_scheme = self.get_attr_val('nsslapd-rootpwstoragescheme') - u_password_scheme = ensure_str(password_scheme) - u_root_scheme = ensure_str(root_scheme) + u_password_scheme = self.get_attr_val_utf8('passwordStorageScheme') + u_root_scheme = self.get_attr_val_utf8('nsslapd-rootpwstoragescheme') if u_root_scheme not in allowed_schemes or u_password_scheme not in allowed_schemes: return DSCLE0002 return None