I am currently trying to mock the LDAP server we are using in the company. For connections to it in our web app we use python ldap3, so I decided to use the mocking feature of ldap3 (which is documented here: https://ldap3.readthedocs.io/mocking.html). However, it can't seem to work for some reason and the resources online on this topic are scarce.
This is my current code:
//imports and environment variables for the LDAP servers
...
_LDAP_SERVER = Server(host=LDAP.host, port=int(LDAP.port), get_info='ALL')
server = _LDAP_SERVER
#connection to real server
_CONNECTION = Connection(
server,
LDAP.manager_dn, LDAP.manager_password,
auto_bind=True, client_strategy=RESTARTABLE
)
#extracting the json files
server.info.to_file('my_real_server_info.json')
server.schema.to_file('my_real_server_schema.json')
#getting the real server entries - everything works to this point
if _CONNECTION.search(LDAP.root_dn, _ALL_USERS_SEARCH_FILTER, attributes=_SEARCH_ATTRIBUTES):
_CONNECTION.response_to_file('my_real_server_entries.json', raw=True)
_CONNECTION.unbind()
#creating the mock server per guidelines
mock_server = Server.from_definition('Mock Server', 'my_real_server_info.json', 'my_real_server_schema.json')
#making a new fake connection
fake_connection = Connection(mock_server, user='CN=testuser, CN=users, DC=company, DC=com', password='fakepassword',
client_strategy=MOCK_SYNC)
fake_connection.strategy.add_entry('CN=selen001,CN=users, DC=company,DC=com', {
"cn": "selen001", #username
"displayName": "Admin, selenium",
"mail": "selenium@COMPANY.COM",
}
)
fake_connection.strategy.add_entry('CN=selen002,CN=users,DC=company,DC=int', {
"cn": "selen002", #username
"displayName": "User, selenium",
"mail": "selenium2@COMPANY.COM",
}
)
fake_connection.bind()
#I want to test if it works, but I can't get any results
if fake_connection.search('DC=company,DC=com', _ALL_USERS_SEARCH_FILTER, attributes=_SEARCH_ATTRIBUTES):
fake_connection.response_to_file('my_real_server_entries1337.json', raw=True)
So to summarise: (1) Connection to Real Server, (2) get its schema and info, (3) generate its entities, (4) create a mock server and a fake connection with fake user, (5) add fake users, (6) test if it works (I can't get a result out of this, which leads me to think that there is an error somewhere, even though I followed the code closely..).
Thank you.