Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mock - search doesn't work where is my error, plz ? #1094

Open
Nuhuruine opened this issue Jun 16, 2023 · 3 comments
Open

Mock - search doesn't work where is my error, plz ? #1094

Nuhuruine opened this issue Jun 16, 2023 · 3 comments

Comments

@Nuhuruine
Copy link

Hi,

With the following code:

from ldap3 import Server, Connection, ALL, ALL_ATTRIBUTES, MOCK_SYNC

srv = Server('fake_server')
conn = Connection(srv,
                          user="cn=bind,dc=example,dc=com",
                          password='password',
                          client_strategy=MOCK_SYNC)
user = "cn=bind,dc=example,dc=com"
user_attributes = {'userPassword': 'password',
                           'sn': 'bind_sn',
                           'revision': 0}
conn.strategy.add_entry(user, user_attributes)
conn.bind()
conn.search('dc=example,dc=com', '(&(objectclass=*)(cn=*))', attributes=ALL_ATTRIBUTES)
print(conn.strategy.entries)
print(conn.response)

I got :

{'cn=schema': {'cn': [b'schema'], 'entryDN': [b'cn=schema']}, 'cn=bind,dc=example,dc=com': {'userPassword': [b'password'], 'sn': [b'bind_sn'], 'revision': [b'0'], 'cn': [b'bind'], 'entryDN': [b'cn=bind,dc=example,dc=com']}}
[]

For information :

pip show ldap3
Name: ldap3
Version: 2.9.1
Summary: A strictly RFC 4510 conforming LDAP V3 pure Python client library
Home-page: https://github.com/cannatag/ldap3
Author: Giovanni Cannata
Author-email: cannatag@gmail.com
License: LGPL v3
Location: ....
Requires: pyasn1
Required-by: 

Could you help me to understand where is my error please ?

@sqqqrly
Copy link

sqqqrly commented Jul 28, 2023

Are you sure bind() was successful?

from ldap3 import Server, Connection, MOCK_SYNC, ALL

# Create a mock server
server = Server('mock_server', get_info=ALL)

# Bind using the mock server (no actual connection is made)
conn = Connection(server, user='cn=admin,dc=example,dc=com', password='password', client_strategy=MOCK_SYNC)
print(f"result (Connection): {conn.result}")

if not conn.bind():
    print(f"Error in bind: {conn.result}")
    sys.exit(3)

Output:

result (Connection): None
Error in bind: {'result': 49, 'description': 'invalidCredentials', 'dn': '', 'message': 'missing object', 'referrals': [], 'saslCreds': None, 'type': 'bindResponse'}

I am not sure where I am going wrong either.

╰─➤  pip list | grep ldap3
ldap3                        2.9.1

@Nuhuruine
Copy link
Author

Hi,

Thanks for your response. So I added the control on bind() but I got the same result ...

from ldap3 import Server, Connection, ALL, ALL_ATTRIBUTES, MOCK_SYNC

srv = Server('fake_server')
conn = Connection(srv,
                          user="cn=bind,dc=example,dc=com",
                          password='password',
                          client_strategy=MOCK_SYNC)
user = "cn=bind,dc=example,dc=com"
user_attributes = {'userPassword': 'password',
                           'sn': 'bind_sn',
                           'revision': 0}
conn.strategy.add_entry(user, user_attributes)
if conn.bind():
    search_filter = '(&(objectclass=*)(cn=*))'
    conn.search('dc=example,dc=com', search_filter, attributes=ALL_ATTRIBUTES)
    print(conn.strategy.entries)
    print(conn.response)
else:
    print('error in bind', conn.result)

The result :

python3 test_mock_ldap3.py 
{'cn=schema': {'cn': [b'schema'], 'entryDN': [b'cn=schema']}, 'cn=bind,dc=example,dc=com': {'userPassword': [b'password'], 'sn': [b'bind_sn'], 'revision': [b'0'], 'cn': [b'bind'], 'entryDN': [b'cn=bind,dc=example,dc=com']}}
[]

But, if I change : search_filter = '(cn=*)', I got :

{'cn=schema': {'cn': [b'schema'], 'entryDN': [b'cn=schema']}, 'cn=bind,dc=example,dc=com': {'userPassword': [b'password'], 'sn': [b'bind_sn'], 'revision': [b'0'], 'cn': [b'bind'], 'entryDN': [b'cn=bind,dc=example,dc=com']}}
[{'raw_dn': b'cn=bind,dc=example,dc=com', 'dn': 'cn=bind,dc=example,dc=com', 'raw_attributes': {'userPassword': [b'password'], 'sn': [b'bind_sn'], 'revision': [b'0'], 'cn': [b'bind']}, 'attributes': {'userPassword': ['password'], 'sn': ['bind_sn'], 'revision': ['0'], 'cn': ['bind']}, 'type': 'searchResEntry'}]

But with search_filter = '(objectclass=*)', I got nothing :/

Do you know why the search on objectclass return nothing, please ?

@sqqqrly
Copy link

sqqqrly commented Aug 1, 2023

If I create a Connection to my fake_server using:

    srv_name = 'fake_server'
    srv = Server(srv_name)
    conn = Connection(srv,
                      user="cn=bind,dc=example,dc=com",
                      password='password',
                      client_strategy=MOCK_SYNC)

Then I seem to need to add a bind user before calling bind():

    # Add working user
    user = "cn=bind,dc=example,dc=com"
    user_attributes = {'userPassword': 'password', 'sn': 'bind_sn', 'revision': 0}
    conn.strategy.add_entry(user, user_attributes)
    print(f"result adding bind: {conn.result}")

    assert conn.bind(), f"bind failed: {conn.result}"
    print(f"Successfully bound to {srv_name}")

If there is no bind user added, the bind() result will be:

AssertionError: bind failed: {'result': 49, 'description': 'invalidCredentials', 'dn': '', 'message': 'missing object', 'referrals': [], 'saslCreds': None, 'type': 'bindResponse'}

This was not obvious to me and caused some seemingly inconsistent results.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants