|
|
|
@ -1,8 +1,7 @@ |
|
|
|
|
#!/usr/bin/env python3 |
|
|
|
|
""" |
|
|
|
|
This is a script which converts each user record in CSC LDAP from the |
|
|
|
|
'account' to the 'inetOrgPerson' objectClass. It pulls first/last name |
|
|
|
|
information from UWLDAP. |
|
|
|
|
This is a script which adds 'sn' and 'givenName' attributes to each user record. |
|
|
|
|
It pulls first/last name information from UWLDAP. |
|
|
|
|
|
|
|
|
|
GSSAPI is used for LDAP authentication, so make sure to run `kinit` first. |
|
|
|
|
""" |
|
|
|
@ -21,8 +20,10 @@ csc_conn = ldap3.Connection( |
|
|
|
|
LDAP_URI, authentication=ldap3.SASL, sasl_mechanism=ldap3.KERBEROS, |
|
|
|
|
auto_bind=True, raise_exceptions=True) |
|
|
|
|
uw_conn = ldap3.Connection(UWLDAP_URI, auto_bind=True, raise_exceptions=True) |
|
|
|
|
csc_conn.search(LDAP_MEMBERS_BASE, '(&(objectClass=member)(objectClass=account))', |
|
|
|
|
attributes=ldap3.ALL_ATTRIBUTES) |
|
|
|
|
csc_conn.search( |
|
|
|
|
LDAP_MEMBERS_BASE, |
|
|
|
|
'(&(objectClass=member)(!(|(sn=*)(givenName=*))))', |
|
|
|
|
attributes=['uid', 'cn']) |
|
|
|
|
total_records_updated = 0 |
|
|
|
|
for csc_entry in csc_conn.entries: |
|
|
|
|
uid = csc_entry.uid.value |
|
|
|
@ -32,7 +33,7 @@ for csc_entry in csc_conn.entries: |
|
|
|
|
try: |
|
|
|
|
uw_conn.search( |
|
|
|
|
f'uid={uid},{UWLDAP_MEMBERS_BASE}', '(objectClass=*)', |
|
|
|
|
attributes=ldap3.ALL_ATTRIBUTES, search_scope=ldap3.BASE) |
|
|
|
|
attributes=['sn', 'givenName'], search_scope=ldap3.BASE) |
|
|
|
|
uw_entry = uw_conn.entries[0] |
|
|
|
|
sn = uw_entry.sn.value |
|
|
|
|
given_name = uw_entry.givenName.value |
|
|
|
@ -42,24 +43,11 @@ for csc_entry in csc_conn.entries: |
|
|
|
|
print(f'WARNING: could not retrieve first and last names for {uid}; inferring from whitespace instead') |
|
|
|
|
words = cn.split() |
|
|
|
|
given_name, sn = words[0], words[-1] |
|
|
|
|
old_object_classes = csc_entry.objectClass.values.copy() |
|
|
|
|
old_object_classes.remove('account') |
|
|
|
|
new_object_classes = old_object_classes + [ |
|
|
|
|
'person', 'organizationalPerson', 'inetOrgPerson', |
|
|
|
|
] |
|
|
|
|
attrs = csc_entry.entry_attributes_as_dict.copy() |
|
|
|
|
attrs['objectClass'] = new_object_classes |
|
|
|
|
attrs['givenName'] = [given_name] |
|
|
|
|
attrs['sn'] = [sn] |
|
|
|
|
csc_conn.delete(csc_entry.entry_dn) |
|
|
|
|
try: |
|
|
|
|
csc_conn.add(csc_entry.entry_dn, attributes=attrs) |
|
|
|
|
except Exception: |
|
|
|
|
print(traceback.format_exc()) |
|
|
|
|
print(f"!!! ERROR !!! We weren't able to create a new record for {uid}.") |
|
|
|
|
print('You need to add the old record back in. Here it is:') |
|
|
|
|
print(csc_entry) |
|
|
|
|
sys.exit(1) |
|
|
|
|
print(f'Created new record for {uid}') |
|
|
|
|
changes = { |
|
|
|
|
'givenName': [(ldap3.MODIFY_ADD, [given_name])], |
|
|
|
|
'sn': [(ldap3.MODIFY_ADD, [sn])], |
|
|
|
|
} |
|
|
|
|
csc_conn.modify(csc_entry.entry_dn, changes) |
|
|
|
|
print(f'Updated record for {uid}') |
|
|
|
|
total_records_updated += 1 |
|
|
|
|
print(f'Total records updated: {total_records_updated}') |