我目前正在尝试模拟我们在公司中使用的LDAP服务器。为了在我们的Web应用程序中连接到它,我们使用python ldap3,因此我决定使用ldap3的模拟功能(在此处记录:https://ldap3.readthedocs.io/mocking.html)。但是,由于某些原因,它似乎无法正常工作,有关此主题的在线资源也很稀缺。
这是我当前的代码:
//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)
因此,总结一下:(1)与Real Server的连接,(2)获取其架构和信息,(3)生成其实体,(4)创建一个模拟服务器和与假用户的假连接,(5)添加伪造的用户,(6)测试它是否有效(我无法从中得到结果,这使我认为某个地方存在错误,即使我密切关注代码也是如此。)。
谢谢。