-
Notifications
You must be signed in to change notification settings - Fork 2
/
ad_corp.py
executable file
·34 lines (26 loc) · 1.41 KB
/
ad_corp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
from ldap3 import Server, Connection, AUTO_BIND_NO_TLS, SUBTREE
"""
CompanyDirectory gets instantiated from aws_ad_accounts_sync.py.
It uses an ldap3 generator to paginate all of the users from an LDAP tree.
It pulls out the samaccountnames from the users and puts that information
inside a simple hashmap so it can be looked up later.
"""
class CompanyDirectory(object):
def __init__(self, config, logger):
self.config = config
self.logger = logger
self.vp_map = {}
def get_all_ldap_users(self):
with Connection(Server(self.config.ldap_url, port=self.config.ldap_port, use_ssl=True),
auto_bind=AUTO_BIND_NO_TLS, read_only=True, check_names=True, user=self.config.ldap_binddn,
password=self.config.ldap_bindpw) as c:
total_entries = 0
ldap_paged_search_generator = c.extend.standard.paged_search(search_base=self.config.ldap_basedn,
search_filter=self.config.ldap_search_filter, search_scope=SUBTREE, paged_size=5000,
attributes=self.config.ldap_searchreq_attrlist, get_operational_attributes=True)
results = {}
for entry in ldap_paged_search_generator:
total_entries += 1
results[entry['attributes']['sAMAccountName'].lower()] = ''
self.logger.debug('Refreshed the ldap_users_vp_map, ldap_users_qty: %s' % len(results))
return results