Skip to content

Commit

Permalink
Merge pull request #564 from nk540347/add-ldap-multiple-hosts-feature
Browse files Browse the repository at this point in the history
Add support for connecting to multiple LDAP hosts
  • Loading branch information
adubovikov authored Aug 30, 2024
2 parents 9e5c7b8 + f2ee4a5 commit 60b96a6
Showing 1 changed file with 42 additions and 25 deletions.
67 changes: 42 additions & 25 deletions utils/ldap/ldap.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,39 +82,56 @@ type LDAPClient struct {
// Connect connects to the ldap backend.
func (lc *LDAPClient) Connect() error {
if lc.Conn == nil {
// Split the space-separated host string into a slice of hosts
hosts := strings.Split(lc.Host, " ")

var l *ldap.Conn
var err error
address := fmt.Sprintf("%s:%d", lc.Host, lc.Port)
if !lc.UseSSL {
l, err = ldap.Dial("tcp", address)
if err != nil {
return err
}

// Reconnect with TLS
if !lc.SkipTLS {
err = l.StartTLS(&tls.Config{InsecureSkipVerify: true})


// Iterate over each host and attempt to connect
for _, host := range hosts {
address := fmt.Sprintf("%s:%d", host, lc.Port)
if !lc.UseSSL {
l, err = ldap.Dial("tcp", address)
if err != nil {
return err
continue // Try the next host
}

// Reconnect with TLS
if !lc.SkipTLS {
err = l.StartTLS(&tls.Config{InsecureSkipVerify: true})
if err != nil {
l.Close() // Close the connection before trying the next host
continue // Try the next host
}
}
} else {
config := &tls.Config{
InsecureSkipVerify: lc.InsecureSkipVerify,
ServerName: lc.ServerName,
}
if lc.ClientCertificates != nil && len(lc.ClientCertificates) > 0 {
config.Certificates = lc.ClientCertificates
}
l, err = ldap.DialTLS("tcp", address, config)
if err != nil {
continue // Try the next host
}
}
} else {
config := &tls.Config{
InsecureSkipVerify: lc.InsecureSkipVerify,
ServerName: lc.ServerName,
}
if lc.ClientCertificates != nil && len(lc.ClientCertificates) > 0 {
config.Certificates = lc.ClientCertificates
}
l, err = ldap.DialTLS("tcp", address, config)
if err != nil {
return err
}

lc.Conn = l
return nil // Successfully connected to a host
}

lc.Conn = l
// If no connection was successful, return the last error encountered
if err != nil {
return err
}
return errors.New("failed to connect to any LDAP server")
}
return nil

return nil // Already connected
}

// Close closes the ldap backend connection.
Expand Down

0 comments on commit 60b96a6

Please sign in to comment.