Changeset - 0666876afad7
[Not reviewed]
default
0 1 0
Mads Kiilerich - 6 years ago 2019-10-03 23:04:58
mads@kiilerich.com
Grafted from: 11549162718a
bin: let ldap_sync use explicit .close() instead of relying on unbinding in .__del__()

It is unknown how important timely unbinding really is. Especially in this
short-lived (unused?) standalone program. But let's avoid any doubt.
1 file changed with 6 insertions and 1 deletions:
0 comments (0 inline, 0 general)
kallithea/bin/ldap_sync.py
Show inline comments
 
@@ -150,13 +150,13 @@ class LdapClient(object):
 
    def __init__(self, uri, user, key, base_dn):
 
        self.client = ldap.initialize(uri, trace_level=0)
 
        self.client.set_option(ldap.OPT_REFERRALS, 0)
 
        self.client.simple_bind(user, key)
 
        self.base_dn = base_dn
 

	
 
    def __del__(self):
 
    def close(self):
 
        self.client.unbind()
 

	
 
    def get_groups(self):
 
        """Get all the groups in form of dict {group_name: group_info,...}."""
 
        searchFilter = "objectClass=groupOfUniqueNames"
 
        result = self.client.search_s(self.base_dn, ldap.SCOPE_SUBTREE,
 
@@ -238,18 +238,23 @@ class LdapSync(object):
 
            try:
 
                self.kallithea_api.add_membership(group, member)
 
            except UserAlreadyInGroupError:
 
                # TODO: handle somehow maybe..
 
                pass
 

	
 
    def close(self):
 
        self.ldap_client.close()
 

	
 

	
 
if __name__ == '__main__':
 
    sync = LdapSync()
 
    print(sync.update_groups_from_ldap())
 

	
 
    for gr in sync.ldap_client.get_groups():
 
        # TODO: exception when user does not exist during add membership...
 
        # How should we handle this.. Either sync users as well at this step,
 
        # or just ignore those who don't exist. If we want the second case,
 
        # we need to find a way to recognize the right exception (we always get
 
        # ResponseError with no error code so maybe by return msg (?)
 
        sync.update_memberships_from_ldap(gr)
 

	
 
    sync.close()
0 comments (0 inline, 0 general)