fix(ldap): exiting by peer exception occurred during the TLS connection(#5977)

This commit is contained in:
睿音 2024-02-01 10:43:08 +08:00 committed by GitHub
parent c82866975e
commit 9bd3c87bcc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -50,31 +50,13 @@ func loginLdap(c *gin.Context, req *LoginReq) {
ldapUserSearchBase := setting.GetStr(conf.LdapUserSearchBase)
ldapUserSearchFilter := setting.GetStr(conf.LdapUserSearchFilter) // (uid=%s)
var tlsEnabled bool = false
if strings.HasPrefix(ldapServer, "ldaps://") {
tlsEnabled = true
ldapServer = strings.TrimPrefix(ldapServer, "ldaps://")
} else if strings.HasPrefix(ldapServer, "ldap://") {
ldapServer = strings.TrimPrefix(ldapServer, "ldap://")
}
l, err := ldap.Dial("tcp", ldapServer)
// Connect to LdapServer
l, err := dial(ldapServer)
if err != nil {
utils.Log.Errorf("failed to connect to LDAP: %v", err)
common.ErrorResp(c, err, 500)
return
}
defer l.Close()
if tlsEnabled {
// Reconnect with TLS
err = l.StartTLS(&tls.Config{InsecureSkipVerify: true})
if err != nil {
utils.Log.Errorf("failed to start tls: %v", err)
common.ErrorResp(c, err, 500)
return
}
}
// First bind with a read only user
if ldapManagerDN != "" && ldapManagerPassword != "" {
@ -157,3 +139,19 @@ func ladpRegister(username string) (*model.User, error) {
}
return user, nil
}
func dial(ldapServer string) (*ldap.Conn, error) {
var tlsEnabled bool = false
if strings.HasPrefix(ldapServer, "ldaps://") {
tlsEnabled = true
ldapServer = strings.TrimPrefix(ldapServer, "ldaps://")
} else if strings.HasPrefix(ldapServer, "ldap://") {
ldapServer = strings.TrimPrefix(ldapServer, "ldap://")
}
if tlsEnabled {
return ldap.DialTLS("tcp", ldapServer, &tls.Config{InsecureSkipVerify: true})
} else {
return ldap.Dial("tcp", ldapServer)
}
}