2929PROJECT_OPENLDAP_CACERT_FILE = import_from_settings ("PROJECT_OPENLDAP_CACERT_FILE" )
3030PROJECT_OPENLDAP_ARCHIVE_OU = import_from_settings ("PROJECT_OPENLDAP_ARCHIVE_OU" )
3131
32- PROJECT_OPENLDAP_DESCRIPTION_TITLE_LENGTH = import_from_settings (
33- "PROJECT_OPENLDAP_DESCRIPTION_TITLE_LENGTH"
34- )
32+ PROJECT_OPENLDAP_DESCRIPTION_TITLE_LENGTH = import_from_settings ("PROJECT_OPENLDAP_DESCRIPTION_TITLE_LENGTH" )
3533
3634# provide a sensible default locally to stop the openldap description being too long
3735MAX_OPENLDAP_DESCRIPTION_LENGTH = 250
@@ -64,9 +62,7 @@ def add_members_to_openldap_posixgroup(dn, list_memberuids, write=True):
6462 Should not raise any exceptions
6563 returns True if not skipped and successful, False otherwise
6664 """
67- return _ldap_write_wrapper (
68- "modify" , dn , {"memberUid" : [(MODIFY_ADD , list_memberuids )]}, write = write
69- )
65+ return _ldap_write_wrapper ("modify" , dn , {"memberUid" : [(MODIFY_ADD , list_memberuids )]}, write = write )
7066
7167
7268def remove_members_from_openldap_posixgroup (dn , list_memberuids , write = True ):
@@ -75,9 +71,7 @@ def remove_members_from_openldap_posixgroup(dn, list_memberuids, write=True):
7571 Should not raise any exceptions
7672 returns True if not skipped and successful, False otherwise
7773 """
78- return _ldap_write_wrapper (
79- "modify" , dn , {"memberUid" : [(MODIFY_DELETE , list_memberuids )]}, write = write
80- )
74+ return _ldap_write_wrapper ("modify" , dn , {"memberUid" : [(MODIFY_DELETE , list_memberuids )]}, write = write )
8175
8276
8377def add_per_project_ou_to_openldap (project_obj , dn , openldap_ou_description , write = True ):
@@ -127,9 +121,7 @@ def update_posixgroup_description_in_openldap(dn, openldap_description, write=Tr
127121 Should not raise any exceptions
128122 returns True if not skipped and successful, False otherwise
129123 """
130- return _ldap_write_wrapper (
131- "modify" , dn , {"description" : [(MODIFY_REPLACE , [openldap_description ])]}, write = write
132- )
124+ return _ldap_write_wrapper ("modify" , dn , {"description" : [(MODIFY_REPLACE , [openldap_description ])]}, write = write )
133125
134126
135127# MOVE the project to an archive OU - defined as env var
@@ -139,9 +131,7 @@ def move_dn_in_openldap(current_dn, relative_dn, destination_ou, write=True):
139131 Should not raise any exceptions
140132 returns True if not skipped and successful, False otherwise
141133 """
142- return _ldap_write_wrapper (
143- "modify_dn" , current_dn , relative_dn , new_superior = destination_ou , write = write
144- )
134+ return _ldap_write_wrapper ("modify_dn" , current_dn , relative_dn , new_superior = destination_ou , write = write )
145135
146136
147137def ldapsearch_check_project_dn (dn ):
@@ -182,9 +172,7 @@ def ldapsearch_get_description(dn):
182172 raises LDAPException
183173 raises KeyError if search has no results
184174 """
185- conn , _ = _ldap_read_wrapper (
186- "search" , dn , "(objectclass=posixGroup)" , attributes = ["description" ]
187- )
175+ conn , _ = _ldap_read_wrapper ("search" , dn , "(objectclass=posixGroup)" , attributes = ["description" ])
188176 if len (conn .entries ) == 0 :
189177 raise KeyError (dn )
190178 return conn .entries
@@ -230,9 +218,7 @@ def construct_dn_str(project_obj):
230218
231219def construct_dn_archived_str (project_obj ):
232220 """Create a distinguished name (dn) for a project posixgroup in a per project ou, in the archive ou"""
233- return (
234- f"cn={ project_obj .project_code } ,ou={ project_obj .project_code } ,{ PROJECT_OPENLDAP_ARCHIVE_OU } "
235- )
221+ return f"cn={ project_obj .project_code } ,ou={ project_obj .project_code } ,{ PROJECT_OPENLDAP_ARCHIVE_OU } "
236222
237223
238224def construct_per_project_ou_relative_dn_str (project_obj ):
@@ -276,9 +262,7 @@ def construct_project_posixgroup_description(project_obj):
276262
277263 # also deal with the combined description field, if it gets too long
278264 if len (description ) > MAX_OPENLDAP_DESCRIPTION_LENGTH :
279- truncated_description = textwrap .shorten (
280- description , MAX_OPENLDAP_DESCRIPTION_LENGTH , placeholder = "..."
281- )
265+ truncated_description = textwrap .shorten (description , MAX_OPENLDAP_DESCRIPTION_LENGTH , placeholder = "..." )
282266 description = truncated_description
283267
284268 return description
@@ -301,19 +285,15 @@ def _ldap_write_wrapper(funcname, *args, **kwargs) -> bool:
301285 if not kwargs .pop ("write" , True ):
302286 logger .info ("write is falsey, skipping..." , stack_info = True , extra = logger_extra_data )
303287 return False
304- with _connection (
305- server , PROJECT_OPENLDAP_BIND_USER , PROJECT_OPENLDAP_BIND_PASSWORD , auto_bind = True
306- ) as conn :
288+ with _connection (server , PROJECT_OPENLDAP_BIND_USER , PROJECT_OPENLDAP_BIND_PASSWORD , auto_bind = True ) as conn :
307289 if isinstance (conn , LDAPException ):
308290 logger .error ("Failed to open LDAP connection" , exc_info = conn , extra = logger_extra_data )
309291 return False
310292 func = getattr (conn , funcname )
311293 try :
312294 func (* args , ** kwargs )
313295 except Exception :
314- logger .exception (
315- "An unexpected exception occurred!" , exc_info = True , extra = logger_extra_data
316- )
296+ logger .exception ("An unexpected exception occurred!" , exc_info = True , extra = logger_extra_data )
317297 return False
318298 if conn .result ["result" ] != 0 :
319299 logger .error ("LDAP operation failed!" , stack_info = True , extra = logger_extra_data )
0 commit comments