Skip to content

Commit

Permalink
feat: unbind LDAP clients if not used any more
Browse files Browse the repository at this point in the history
  • Loading branch information
hsluoyz committed Dec 2, 2023
1 parent b25b5f0 commit 85cb68e
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 1 deletion.
1 change: 1 addition & 0 deletions controllers/ldap.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
8 changes: 7 additions & 1 deletion object/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 3 additions & 0 deletions object/ldap_autosync.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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()
}
}

Expand Down
11 changes: 11 additions & 0 deletions object/ldap_conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"}
Expand Down

0 comments on commit 85cb68e

Please sign in to comment.