Skip to content

Commit

Permalink
Implement code review suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
PhongT16 committed Oct 10, 2023
1 parent fc821b4 commit 49c0143
Showing 1 changed file with 81 additions and 18 deletions.
99 changes: 81 additions & 18 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
import ldap3
import time
import logging
# TODO: what should I do about the memberOf? should it just be the output of cnn.entries[0].memberOf from the members loop?
# TODO: join together the search base into one path and pass it to write_to_file

file_path = "output.ldif"
def write_to_file(uid, mail, memberOf, domain):
def write_to_file(ldif_entries):

# dn, uid, mail, mailRoutingAddress, profileType

try:
with open(file_path, 'a') as file:
file.write(f"dn: {mail}\n")
file.write(f"uid: {mail}\n")
file.write(f"mail: {mail}\n")
file.write(f"mailRoutingAddress: {uid}@{domain}\n")
file.write(f"profileType: 0\n")
for group in memberOf:
file.write(f"memberOf: {group}\n")
file.write("\n")
# entry = [uid, mail, memberOf, domain]
try:
with open(file_path, 'w') as file:
for entry in ldif_entries:
file.write(f"dn: {entry[1]}\n")
file.write(f"uid: {entry[1]}\n")
file.write(f"mail: {entry[1]}\n")
file.write(f"mailRoutingAddress: {entry[0]}@{entry[3]}\n")
file.write(f"profileType: 0\n")
for group in entry[2]:
file.write(f"memberOf: {group}\n")
file.write("\n")
except FileNotFoundError:
print(f"The file '{file_path}' was not found.")
except IOError as e:
Expand All @@ -33,6 +33,8 @@ def get_email_list_from_ldap(group_name):
Returns:
A list of emails from the specified group_name using ldap server
"""

entries = []
ldap_server = "ldaps://ldap1.ncsa.illinois.edu" # Replace with your LDAP server

ldap_user = None
Expand Down Expand Up @@ -70,6 +72,7 @@ def get_email_list_from_ldap(group_name):
else:
members = [ m.split(',')[0].split('=')[1] for m in conn.entries[0].uniqueMember ]

print(f"{len(members)} user entries in get_email_list_from_ldap")
for member in members:
result = conn.search(search_base, f"(uid={member})", search_scope, attributes=attributes)
if not result:
Expand All @@ -82,19 +85,79 @@ def get_email_list_from_ldap(group_name):
mail = conn.entries[0].mail
except:
# If a primary email isn't set, then there's no point in adding the user entry to the LDIF
logger.warning(f"Primary email doesn't exist in entry with uid {member}")
continue
try:
memberOf = conn.entries[0].memberOf
except:
memberOf = []

write_to_file(uid, mail, memberOf, domain)
#write_to_file(uid, mail, memberOf, domain)
entry = [uid, mail, memberOf, domain]
entries.append(entry)
return entries

def get_user_entries_from_ldap(group_name):
entries = []
ldap_server = "ldaps://ldap1.ncsa.illinois.edu" # Replace with your LDAP server

ldap_user = None
ldap_password = None

search_base = 'dc=ncsa,dc=illinois,dc=edu'

domain = ""
subdomains = search_base.split(',')
for index, subdomain in enumerate(subdomains):
if (index == 0):
domain = subdomain.split('=')[1]
else:
domain = domain + "." + subdomain.split('=')[1]

# The subtree attributes indicatest that we want to search the entire tree starting from the search
search_scope = ldap3.SUBTREE
attributes = ldap3.ALL_ATTRIBUTES

group_list = [
group_name
]

with ldap3.Connection(ldap_server, ldap_user, ldap_password) as conn:
if not conn.bind():
raise Exception("Error: Could not bind to LDAP server")

for group_name in group_list:
ldap_filter = f'(&(objectClass=person)(memberOf={group_name}))'
search_filter = f"(cn={group_name})"
#print("search_filter: " + search_filter)
result = conn.search(search_base, ldap_filter, search_scope, attributes=attributes)
if not result:
raise KeyError(f"Error: Could not find group {group_name}")

print(f"There are {len(conn.entries)} in get_user_entries_from_ldap")
for entry in conn.entries:
#Retrieve and print user attributes
uid = entry.uid

try:
mail = entry.mail
except:
# If a primary email isn't set, then there's no point in adding the user entry to the LDIF
logger.warning(f"Primary email doesn't exist in entry with uid {uid}")
continue
try:
memberOf = entry.memberOf
except:
memberOf = []

entries.append([uid, mail, memberOf, domain])
return entries
def main():
start_time = time.time()
with open(file_path, 'w') as file:
pass # This block is empty, and the file is closed automatically
mail_list = get_email_list_from_ldap('all_ncsa_employe') # org_ici
group_name = 'all_ncsa_employe'
ldif_entries = get_user_entries_from_ldap(f"cn={group_name},ou=Groups,dc=ncsa,dc=illinois,dc=edu")
#ldif_entries = get_email_list_from_ldap('all_ncsa_employe') # org_ici
write_to_file(ldif_entries)
end_time = time.time()
elapsed_time = end_time - start_time
# Print the elapsed time in seconds
Expand Down

0 comments on commit 49c0143

Please sign in to comment.