Python ldap.SCOPE_ONELEVEL Examples

The following are 13 code examples of ldap.SCOPE_ONELEVEL(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module ldap , or try the search function .
Example #1
Source File: test_ldap_backend.py    From st2-auth-backend-ldap with Apache License 2.0 6 votes vote down vote up
def test_search_valid_username(self):
        username = 'sarah_connor'
        password = 'Reece4ever'
        user_dn = 'uid={},ou=users,dc=example,dc=com'.format(username)

        mock_res = (user_dn, LDAPAuthenticationBackendTestCase.directory[user_dn])

        user = {"base_dn": "ou=users,dc=example,dc=com", "search_filter": "(uid={username})", "scope": "onelevel"}

        self.ldapobj.search_s.seed(user["base_dn"], ldap.SCOPE_ONELEVEL, user["search_filter"].format(username=username))([mock_res])

        result = _do_simple_bind('cn=manager,dc=example,dc=com', 'ldaptest', user_search=user, group_search=None, username=username, password=password)

        expected_methods_called = (
            self.connect_methods +
            ['simple_bind_s', 'whoami_s', 'search', 'result', 'result'] +
            self.connect_methods +
            ['simple_bind_s', 'whoami_s', 'unbind', 'unbind']
        )

        self.assertEquals(self.ldapobj.methods_called(), expected_methods_called)
        self.assertTrue(result) 
Example #2
Source File: test_ldap_backend.py    From st2-auth-backend-ldap with Apache License 2.0 6 votes vote down vote up
def test_search_invalid_username(self):
        username = 'invalid_username'
        password = 'Reece4ever'
        user = {"base_dn": "ou=users,dc=example,dc=com", "search_filter": "(uid={username})", "scope": "onelevel"}

        mock_res = []

        self.ldapobj.search_s.seed(user["base_dn"], ldap.SCOPE_ONELEVEL, user["search_filter"].format(username=username))(mock_res)
        result = _do_simple_bind('cn=manager,dc=example,dc=com', 'ldaptest', user_search=user, group_search=None, username=username, password=password)

        expected_methods_called = (
            self.connect_methods +
            ['simple_bind_s', 'whoami_s', 'search', 'result', 'unbind']
        )

        self.assertEquals(self.ldapobj.methods_called(), expected_methods_called)
        self.assertFalse(result) 
Example #3
Source File: test_ldap_backend.py    From st2-auth-backend-ldap with Apache License 2.0 6 votes vote down vote up
def test_search_invalid_password(self):
        username = 'sarah_connor'
        password = 'bad_password'
        user = {"base_dn": "ou=users,dc=example,dc=com", "search_filter": "(uid={username})", "scope": "onelevel"}

        mock_res_id = 1234
        mock_res = (ldap.RES_SEARCH_RESULT, None)

        self.ldapobj._search.seed(user["base_dn"], ldap.SCOPE_ONELEVEL, user["search_filter"].format(username=username))(mock_res_id)
        self.ldapobj._result.seed(mock_res_id, all=0)(mock_res)
        result = _do_simple_bind('cn=manager,dc=example,dc=com', 'ldaptest', user_search=user, group_search=None, username=username, password=password)

        expected_methods_called = (
            self.connect_methods +
            ['simple_bind_s', 'whoami_s', 'search', 'result', 'unbind']
        )

        self.assertEquals(self.ldapobj.methods_called(), expected_methods_called)
        self.assertFalse(result) 
Example #4
Source File: freeipaserver.py    From checkipaconsistency with GNU General Public License v3.0 6 votes vote down vote up
def _count_netgroups(self):
        self._log.debug('Counting netgroups...')
        results = self._search(
            'cn=ng,cn=alt,%s' % self._base_dn,
            '(ipaUniqueID=*)',
            ['dn'],
            scope=ldap.SCOPE_ONELEVEL
        )

        if not results and type(results) is not list:
            r = 0
        else:
            r = len(results)

        self._log.debug(r)
        return r 
Example #5
Source File: freeipaserver.py    From checkipaconsistency with GNU General Public License v3.0 6 votes vote down vote up
def _count_certificates(self):
        self._log.debug('Counting certificates...')
        results = self._search(
            'ou=certificateRepository,ou=ca,o=ipaca',
            '(certStatus=*)',
            ['subjectName'],
            scope=ldap.SCOPE_ONELEVEL
        )

        if not results and type(results) is not list:
            r = 0
        else:
            r = len(results)

        self._log.debug(r)
        return r 
Example #6
Source File: ldap_backend.py    From st2-auth-backend-ldap with Apache License 2.0 5 votes vote down vote up
def _scope_to_ldap_option(self, scope):
        """
        Transform scope string into ldap module constant.
        """
        if 'base' in scope.lower():
            opt = ldap.SCOPE_BASE
        elif 'onelevel' in scope.lower():
            opt = ldap.SCOPE_ONELEVEL
        else:
            opt = ldap.SCOPE_SUBTREE
        return opt 
Example #7
Source File: test_ldap_backend.py    From st2-auth-backend-ldap with Apache License 2.0 5 votes vote down vote up
def test_search_valid_username_valid_group(self):
        username = 'john_connor'
        password = 'HastaLavista'
        user_dn = 'uid={},ou=users,dc=example,dc=com'.format(username)
        mock_user_res_id = 1234
        mock_user_res = (ldap.RES_SEARCH_RESULT, [(user_dn, LDAPAuthenticationBackendTestCase.directory[user_dn])])

        groupname = 'resistance'
        group_dn = 'cn={groupname},ou=groups,dc=example,dc=com'.format(groupname=groupname)
        mock_group_res_id = 9999
        mock_group_res = (ldap.RES_SEARCH_RESULT, [(group_dn, LDAPAuthenticationBackendTestCase.directory[group_dn])])

        user = {"base_dn": "ou=users,dc=example,dc=com", "search_filter": "(uid={username})", "scope": "onelevel"}
        group = {"base_dn": "ou=groups,dc=example,dc=com", "search_filter": "(&(cn=%s)(memberUid={username}))"%groupname, "scope": "subtree"}

        self.ldapobj._search.seed(user["base_dn"], ldap.SCOPE_ONELEVEL, user["search_filter"].format(username=username))(mock_user_res_id)
        self.ldapobj._search.seed(group["base_dn"], ldap.SCOPE_SUBTREE, group["search_filter"].format(username=username))(mock_group_res_id)
        self.ldapobj._result.seed(mock_user_res_id, all=0)(mock_user_res)
        self.ldapobj._result.seed(mock_group_res_id, all=0)(mock_group_res)

        result = _do_simple_bind('cn=manager,dc=example,dc=com', 'ldaptest', user_search=user, group_search=group, username=username, password=password)

        expected_methods_called = (
            self.connect_methods +
            ['simple_bind_s', 'whoami_s', 'search', 'result', 'result'] +
            self.connect_methods +
            ['simple_bind_s', 'whoami_s', 'unbind', 'search', 'result', 'result', 'unbind']
        )

        self.assertEquals(self.ldapobj.methods_called(), expected_methods_called)
        self.assertTrue(result) 
Example #8
Source File: region.py    From satori with Apache License 2.0 5 votes vote down vote up
def nodes_of(region):
    svr = ldap.initialize('ldap://ldap.example.com')
    svr.simple_bind_s('uid=user,cn=users,cn=accounts,dc=in,dc=example,dc=com', 'example-password')

    return [i[1]['fqdn'][0] for i in svr.search_s(
        'cn=computers,cn=accounts,dc=in,dc=example,dc=com',
        ldap.SCOPE_ONELEVEL,
        'cn=*.%s.in.example.com' % region,
        ['fqdn']
    )] 
Example #9
Source File: config.py    From ssh-ldap-pubkey with MIT License 5 votes vote down vote up
def parse_scope_opt(value):
    """Convert `scope` option to ldap's `SCOPE_*` constant."""
    return {
        'base': ldap.SCOPE_BASE,
        'one': ldap.SCOPE_ONELEVEL,
        'sub': ldap.SCOPE_SUBTREE
    }[value.lower()] if value else None 
Example #10
Source File: client_libldap.py    From code with MIT License 5 votes vote down vote up
def search(self, base, filter=None, scope=None, attrs=None):
        scope = {
            "base":         ldap.SCOPE_BASE,
            "subtree":      ldap.SCOPE_SUBTREE,
            "sub":          ldap.SCOPE_SUBTREE,
            "onelevel":     ldap.SCOPE_ONELEVEL,
            "one":          ldap.SCOPE_ONELEVEL,
            "subordinate":  ldap.SCOPE_SUBORDINATE,
            "child":        ldap.SCOPE_SUBORDINATE,
        }[scope or "subtree"]
        result = self.conn.search_ext_s(base, scope, filter, attrs)
        result = [(dn, CaseInsensitiveDict(attrs)) for (dn, attrs) in result]
        return result 
Example #11
Source File: freeipaserver.py    From checkipaconsistency with GNU General Public License v3.0 5 votes vote down vote up
def _count_hbac_rules(self):
        self._log.debug('Counting HBAC rules...')
        results = self._search(
            'cn=hbac,%s' % self._base_dn,
            '(ipaUniqueID=*)',
            scope=ldap.SCOPE_ONELEVEL
        )
        r = len(results)
        self._log.debug(r)
        return r 
Example #12
Source File: freeipaserver.py    From checkipaconsistency with GNU General Public License v3.0 5 votes vote down vote up
def _count_sudo_rules(self):
        self._log.debug('Counting SUDO rules...')
        results = self._search(
            'cn=sudorules,cn=sudo,%s' % self._base_dn,
            '(ipaUniqueID=*)',
            scope=ldap.SCOPE_ONELEVEL
        )
        r = len(results)
        self._log.debug(r)
        return r 
Example #13
Source File: freeipaserver.py    From checkipaconsistency with GNU General Public License v3.0 5 votes vote down vote up
def _count_dns_zones(self):
        self._log.debug('Counting DNS zones...')
        results = self._search(
            'cn=dns,%s' % self._base_dn,
            '(|(objectClass=idnszone)(objectClass=idnsforwardzone))',
            scope=ldap.SCOPE_ONELEVEL
        )
        if not results and type(results) is not list:
            r = 0
        else:
            r = len(results)
        self._log.debug(r)
        return r