對於client鏈接ldap server的策略,ldap3提供了4種選擇,能夠經過client_strategy設置Connection object應用哪一種策略:數據庫
l SYNCjson
l ASYNC安全
l RESTARTABLEdom
l REUSABLE異步
同步策略(SYNC, RESTARTABLE),全部的ldap操做返回True/Falsesocket
異步策略(ASYNC, REUSABLE)返回一個msgid(一個數值),異步策略發送請求後不用等待響應,須要響應的時候直接使用get_response(message_id)獲取結果。等待響應的超時時間能夠經過get_response的timeout參數指定,默認10s。若是使用get_request=True in the get_response(),同時會返回發送的請求字典。ide
創建鏈接:ui
創建Server對象時使用get_info=ldap3.ALL參數,創建Connection鏈接以後能夠獲取到server信息(匿名獲取),從中能夠獲取到域名信息,域控計算機名,ldap server支持的Extension和Controlspa
創建Server時指定 active=True,創建鏈接前會先檢查ldap server的可用性;active=5指定拋出 LDAPServerPoolExhaustedError異常以前重試的次數orm
exhaust=True : 若是ldap server不時active,server將會從pool中移除。exhaust=10:設置爲數值,表示認爲server 10s不可達,則認爲它爲offline,
When all servers in a pool are not available the strategy will wait for the number of seconds specified in ldap.
POOLING_LOOP_TIMEOUT before starting a new cycle. This defaults to 10 seconds.
The pool can have different HA strategies:
• FIRST: gets the first server in the pool, if ‘active’ is set to True gets the first available server
• ROUND_ROBIN: each time the connection is open the subsequent server in the pool is used. If active is set to
True unavailable servers will be discarded
• RANDOM: each time the connection is open a random server is chosen in the pool. If active is set to True
unavailable servers will be discarded
A server pool can be defined in different ways:
server1 = Server('server1')
server2 = Server('server2')
server3 = Server('server1', port=636, use_ssl=True)
• explicitly with Server objects in the init:
server_pool = ServerPool([server1, server2, server3], POOLING_STRATEGY_ROUND_
˓→ ROBIN, active=True, exhaust=True)
• explicitly with an add operation in the pool object:
server_pool = ServerPool(None, POOLING_STRATEGY_ROUND_ROBIN_ACTIVE)
server_pool.add(server1)
server_pool.add(server2)
server_pool.add(server3)
44 Chapter 1. Contents
ldap3 Documentation, Release 2.5
• implicitly directly in the Connection object init (passing a list of servers):
conn = Connection([server1, server2, server3]) # the ServerPool object is
˓→ defined with the default pooling strategy
Pools can be dynamically changed. You can add and remove Server objects from pools even if they are already used
in Connection:
server4 = Server('server2', port=636, use_ssl=True)
server_pool.remove(server2)
server_pool.add(server4)
Connections are notified of the change and can reopen the socket to the new server at next open() operation.
You can also save the schema and info in a json string:
json_info = server.info.to_json()
json_schema = server.schema.to_json()
or can have them saved on file:
server.info.to_file('server-info.json')
server.schema.to_file('server-schema.json')
to build a new server object with the saved json files you can retrieve them with:
from ldap3 import DsaInfo, SchemaInfo
dsa_info = DsaInfo.from_file('server-info.json')
schema_info = SchemaInfo.from_file('server-schema.json')
server = Server('hostname', dsa_info, schema_info)
ldap server的Schema數據庫中存儲了ldap server中的對象的已知類型信息,能夠經過server.schema獲取到(微軟AD須要鑑權,匿名用戶沒法獲取),裏面存儲了ldap server理解那些數據類型,同時也指定,哪些屬性被ldap server中的對象支持
使用鑑權用戶鏈接ldap server後能夠查看server.shema等高級別操做。查看當前鑑權用戶信息。如下鏈接使用的不安全的鏈接,密碼信息明文傳輸,能夠被抓取。使用authentication=ldap3.NTLM的鑑權方式沒法顯示的看到鑑權信息。
可使用如下方式創建安全鏈接,2種方式都是創建TLS鏈接:
l LDAP over TLS
l the StartTLS extended operation ##微軟AD不支持
ldap查詢
ldap查詢基於search_base和search_filter,filter是個表達式:
l 查詢全部顯示名叫John而且email以‘@example.org’結尾的用戶:(&(givenName=John)(mail=*@example.org))
l 查詢顯示名爲Jhon或者Fred而且郵箱以@example.org結尾的用戶
(&
(|
(GivenName=Jhon)
(givenName=Fred)
)
( mail=*@example.org)
)
搜索search_base下的全部用戶,默認search_scope='SUBTREE',沒有指定請求任何attribute,只返回entries的distinguished Name,請求成功(同步strategy)返回True,conn.entries獲取查詢到的結果:
conn.search(base_search,'(objectclass=person)')
conn.entries
可使用訪問字典或者訪問對象屬性的方式訪問從server上獲取到的attribute值,有些屬性不區分大小寫,raw_values獲取到的是從server返回的原始的值:
返回的entry能夠格式化爲json字符串
若是查詢的屬性的值爲空,返回的entries中將不包含此屬性,除非在Connection中指定return_empty_attributes=False,微軟AD中貌似不起做用。
對ldap server進行search操做以後,Connection有如下屬性能夠訪問:
在AD上增長entry,第一個參數爲增長的對象dn,第二個參數爲object_class,指定建立的object的類型,第三個參數爲object提供的個性化attribute:
域控支持的objectclass能夠經過server.schema獲取到,建立不一樣類型的objectclass支持哪些attribute能夠經過server.schema.object_classes['user']方式獲取到,大多數attribute在建立object的時候都是可選的,必選參數會單獨列出:
重命名一個dn,利用modify_dn提供的參數new_superior=new_dn,還能夠將dn從一個ou移動到另外一個ou:
檢查object的屬性是否和給定值同樣。