From 85cb68eb66e192e9cbc8a7dd416f73193b14ec04 Mon Sep 17 00:00:00 2001 From: Yang Luo Date: Sat, 2 Dec 2023 17:51:25 +0800 Subject: [PATCH] feat: unbind LDAP clients if not used any more --- controllers/ldap.go | 1 + object/check.go | 8 +++++++- object/ldap_autosync.go | 3 +++ object/ldap_conn.go | 11 +++++++++++ 4 files changed, 22 insertions(+), 1 deletion(-) diff --git a/controllers/ldap.go b/controllers/ldap.go index b2ae5a743b2a..cd15eb6887e9 100644 --- a/controllers/ldap.go +++ b/controllers/ldap.go @@ -59,6 +59,7 @@ func (c *ApiController) GetLdapUsers() { c.ResponseError(err.Error()) return } + defer conn.Close() //groupsMap, err := conn.GetLdapGroups(ldapServer.BaseDn) //if err != nil { diff --git a/object/check.go b/object/check.go index a6cc2638a3a4..6474e10c33e2 100644 --- a/object/check.go +++ b/object/check.go @@ -237,22 +237,28 @@ func checkLdapUserPassword(user *User, password string, lang string) error { searchResult, err := conn.Conn.Search(searchReq) if err != nil { + conn.Close() return err } if len(searchResult.Entries) == 0 { + conn.Close() continue } if len(searchResult.Entries) > 1 { + conn.Close() return fmt.Errorf(i18n.Translate(lang, "check:Multiple accounts with same uid, please check your ldap server")) } hit = true dn := searchResult.Entries[0].DN - if err := conn.Conn.Bind(dn, password); err == nil { + if err = conn.Conn.Bind(dn, password); err == nil { ldapLoginSuccess = true + conn.Close() break } + + conn.Close() } if !ldapLoginSuccess { diff --git a/object/ldap_autosync.go b/object/ldap_autosync.go index ceac2c18a228..8c80a8dc1dbb 100644 --- a/object/ldap_autosync.go +++ b/object/ldap_autosync.go @@ -100,6 +100,7 @@ func (l *LdapAutoSynchronizer) syncRoutine(ldap *Ldap, stopChan chan struct{}) e users, err := conn.GetLdapUsers(ldap) if err != nil { + conn.Close() logs.Warning(fmt.Sprintf("autoSync failed for %s, error %s", ldap.Id, err)) continue } @@ -111,6 +112,8 @@ func (l *LdapAutoSynchronizer) syncRoutine(ldap *Ldap, stopChan chan struct{}) e } else { logs.Info(fmt.Sprintf("ldap autosync success, %d new users, %d existing users", len(users)-len(existed), len(existed))) } + + conn.Close() } } diff --git a/object/ldap_conn.go b/object/ldap_conn.go index bc9f716d4efb..71d93d74163d 100644 --- a/object/ldap_conn.go +++ b/object/ldap_conn.go @@ -81,6 +81,17 @@ func (ldap *Ldap) GetLdapConn() (c *LdapConn, err error) { return &LdapConn{Conn: conn, IsAD: isAD}, nil } +func (l *LdapConn) Close() { + if l.Conn == nil { + return + } + + err := l.Conn.Unbind() + if err != nil { + panic(err) + } +} + func isMicrosoftAD(Conn *goldap.Conn) (bool, error) { SearchFilter := "(objectClass=*)" SearchAttributes := []string{"vendorname", "vendorversion", "isGlobalCatalogReady", "forestFunctionality"}