2019-12-11 15:16:37 -05:00
|
|
|
// Package ldap provides a simple ldap client to authenticate,
|
|
|
|
// retrieve basic information and groups for a user.
|
2019-08-15 11:34:54 -05:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2019-12-11 15:16:37 -05:00
|
|
|
"time"
|
|
|
|
|
2019-08-15 11:34:54 -05:00
|
|
|
"crypto/tls"
|
|
|
|
"crypto/x509"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/anuvu/zot/errors"
|
2019-12-11 15:16:37 -05:00
|
|
|
goldap "github.com/go-ldap/ldap/v3"
|
|
|
|
|
2019-11-25 17:33:58 -05:00
|
|
|
"github.com/anuvu/zot/pkg/log"
|
2019-12-11 15:16:37 -05:00
|
|
|
"github.com/go-ldap/ldap/v3"
|
2019-08-15 11:34:54 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type LDAPClient struct {
|
2019-12-11 15:16:37 -05:00
|
|
|
InsecureSkipVerify bool
|
|
|
|
UseSSL bool
|
|
|
|
SkipTLS bool
|
|
|
|
SubtreeSearch bool
|
|
|
|
Port int
|
|
|
|
Attributes []string
|
|
|
|
Base string
|
|
|
|
BindDN string
|
|
|
|
BindPassword string
|
|
|
|
GroupFilter string // e.g. "(memberUid=%s)"
|
|
|
|
Host string
|
|
|
|
ServerName string
|
|
|
|
UserFilter string // e.g. "(uid=%s)"
|
|
|
|
Conn *ldap.Conn
|
|
|
|
ClientCertificates []tls.Certificate // Adding client certificates
|
|
|
|
ClientCAs *x509.CertPool
|
|
|
|
Log log.Logger
|
2019-08-15 11:34:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Connect connects to the ldap backend.
|
|
|
|
func (lc *LDAPClient) Connect() error {
|
|
|
|
if lc.Conn == nil {
|
|
|
|
var l *goldap.Conn
|
2019-12-13 00:53:18 -05:00
|
|
|
|
2019-08-15 11:34:54 -05:00
|
|
|
var err error
|
2019-12-13 00:53:18 -05:00
|
|
|
|
2019-08-15 11:34:54 -05:00
|
|
|
address := fmt.Sprintf("%s:%d", lc.Host, lc.Port)
|
2019-12-13 00:53:18 -05:00
|
|
|
|
2019-08-15 11:34:54 -05:00
|
|
|
if !lc.UseSSL {
|
|
|
|
l, err = goldap.Dial("tcp", address)
|
|
|
|
if err != nil {
|
2019-12-11 15:16:37 -05:00
|
|
|
lc.Log.Error().Err(err).Str("address", address).Msg("non-TLS connection failed")
|
2019-08-15 11:34:54 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reconnect with TLS
|
|
|
|
if !lc.SkipTLS {
|
|
|
|
config := &tls.Config{
|
2020-05-11 17:13:24 -05:00
|
|
|
InsecureSkipVerify: lc.InsecureSkipVerify, // nolint: gosec // InsecureSkipVerify is not true by default
|
2019-12-11 15:16:37 -05:00
|
|
|
RootCAs: lc.ClientCAs,
|
2019-08-15 11:34:54 -05:00
|
|
|
}
|
|
|
|
if lc.ClientCertificates != nil && len(lc.ClientCertificates) > 0 {
|
|
|
|
config.Certificates = lc.ClientCertificates
|
2020-05-11 17:13:24 -05:00
|
|
|
config.BuildNameToCertificate() // nolint: staticcheck
|
2019-08-15 11:34:54 -05:00
|
|
|
}
|
2019-12-13 00:53:18 -05:00
|
|
|
|
2019-08-15 11:34:54 -05:00
|
|
|
err = l.StartTLS(config)
|
2019-12-13 00:53:18 -05:00
|
|
|
|
2019-08-15 11:34:54 -05:00
|
|
|
if err != nil {
|
2019-12-11 15:16:37 -05:00
|
|
|
lc.Log.Error().Err(err).Str("address", address).Msg("TLS connection failed")
|
2019-08-15 11:34:54 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
config := &tls.Config{
|
2020-05-11 17:13:24 -05:00
|
|
|
InsecureSkipVerify: lc.InsecureSkipVerify, // nolint: gosec // InsecureSkipVerify is not true by default
|
2019-08-15 11:34:54 -05:00
|
|
|
ServerName: lc.ServerName,
|
2019-12-11 15:16:37 -05:00
|
|
|
RootCAs: lc.ClientCAs,
|
2019-08-15 11:34:54 -05:00
|
|
|
}
|
|
|
|
if lc.ClientCertificates != nil && len(lc.ClientCertificates) > 0 {
|
|
|
|
config.Certificates = lc.ClientCertificates
|
2020-05-11 17:13:24 -05:00
|
|
|
config.BuildNameToCertificate() // nolint: staticcheck
|
2019-08-15 11:34:54 -05:00
|
|
|
}
|
|
|
|
l, err = goldap.DialTLS("tcp", address, config)
|
|
|
|
if err != nil {
|
2019-12-11 15:16:37 -05:00
|
|
|
lc.Log.Error().Err(err).Str("address", address).Msg("TLS connection failed")
|
2019-08-15 11:34:54 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
lc.Conn = l
|
|
|
|
}
|
2019-12-13 00:53:18 -05:00
|
|
|
|
2019-08-15 11:34:54 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-12-11 15:16:37 -05:00
|
|
|
// Close closes the ldap backend connection.
|
|
|
|
func (lc *LDAPClient) Close() {
|
|
|
|
if lc.Conn != nil {
|
|
|
|
lc.Conn.Close()
|
|
|
|
lc.Conn = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const maxRetries = 8
|
|
|
|
|
2019-11-18 14:28:52 -05:00
|
|
|
func sleepAndRetry(retries, maxRetries int) bool {
|
|
|
|
if retries > maxRetries {
|
|
|
|
return false
|
|
|
|
}
|
2019-12-13 00:53:18 -05:00
|
|
|
|
2019-11-18 14:28:52 -05:00
|
|
|
if retries < maxRetries {
|
|
|
|
time.Sleep(time.Duration(retries) * time.Second) // gradually backoff
|
|
|
|
return true
|
|
|
|
}
|
2019-12-13 00:53:18 -05:00
|
|
|
|
2019-11-18 14:28:52 -05:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-08-15 11:34:54 -05:00
|
|
|
// Authenticate authenticates the user against the ldap backend.
|
|
|
|
func (lc *LDAPClient) Authenticate(username, password string) (bool, map[string]string, error) {
|
2019-08-15 11:34:54 -05:00
|
|
|
if password == "" {
|
|
|
|
// RFC 4513 section 5.1.2
|
|
|
|
return false, nil, errors.ErrLDAPEmptyPassphrase
|
|
|
|
}
|
|
|
|
|
2019-11-18 14:28:52 -05:00
|
|
|
connected := false
|
|
|
|
for retries := 0; !connected && sleepAndRetry(retries, maxRetries); retries++ {
|
|
|
|
err := lc.Connect()
|
2019-08-15 11:34:54 -05:00
|
|
|
if err != nil {
|
2019-11-18 14:28:52 -05:00
|
|
|
continue
|
2019-08-15 11:34:54 -05:00
|
|
|
}
|
2019-11-18 14:28:52 -05:00
|
|
|
|
|
|
|
// First bind with a read only user
|
|
|
|
if lc.BindDN != "" && lc.BindPassword != "" {
|
|
|
|
err := lc.Conn.Bind(lc.BindDN, lc.BindPassword)
|
|
|
|
if err != nil {
|
2019-12-11 15:16:37 -05:00
|
|
|
lc.Log.Error().Err(err).Str("bindDN", lc.BindDN).Msg("bind failed")
|
2019-11-18 14:28:52 -05:00
|
|
|
// clean up the cached conn, so we can retry
|
|
|
|
lc.Conn.Close()
|
|
|
|
lc.Conn = nil
|
2019-12-13 00:53:18 -05:00
|
|
|
|
2019-11-18 14:28:52 -05:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
2019-12-13 00:53:18 -05:00
|
|
|
|
2019-11-18 14:28:52 -05:00
|
|
|
connected = true
|
|
|
|
}
|
|
|
|
|
|
|
|
// exhausted all retries?
|
|
|
|
if !connected {
|
2019-12-11 15:16:37 -05:00
|
|
|
lc.Log.Error().Err(errors.ErrLDAPBadConn).Msg("exhausted all retries")
|
2019-11-18 14:28:52 -05:00
|
|
|
return false, nil, errors.ErrLDAPBadConn
|
2019-08-15 11:34:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
attributes := append(lc.Attributes, "dn")
|
|
|
|
searchScope := goldap.ScopeSingleLevel
|
2019-12-13 00:53:18 -05:00
|
|
|
|
2019-12-11 15:16:37 -05:00
|
|
|
if lc.SubtreeSearch {
|
2019-08-15 11:34:54 -05:00
|
|
|
searchScope = goldap.ScopeWholeSubtree
|
|
|
|
}
|
|
|
|
// Search for the given username
|
|
|
|
searchRequest := goldap.NewSearchRequest(
|
|
|
|
lc.Base,
|
|
|
|
searchScope, goldap.NeverDerefAliases, 0, 0, false,
|
|
|
|
fmt.Sprintf(lc.UserFilter, username),
|
|
|
|
attributes,
|
|
|
|
nil,
|
|
|
|
)
|
|
|
|
|
|
|
|
sr, err := lc.Conn.Search(searchRequest)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("%v\n", err)
|
2019-12-11 15:16:37 -05:00
|
|
|
lc.Log.Error().Err(err).Str("bindDN", lc.BindDN).Str("username", username).
|
2019-08-15 11:34:54 -05:00
|
|
|
Str("baseDN", lc.Base).Msg("search failed")
|
2019-12-13 00:53:18 -05:00
|
|
|
|
2019-08-15 11:34:54 -05:00
|
|
|
return false, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(sr.Entries) < 1 {
|
|
|
|
err := errors.ErrBadUser
|
2019-12-11 15:16:37 -05:00
|
|
|
lc.Log.Error().Err(err).Str("bindDN", lc.BindDN).Str("username", username).
|
2019-08-15 11:34:54 -05:00
|
|
|
Str("baseDN", lc.Base).Msg("entries not found")
|
2019-12-13 00:53:18 -05:00
|
|
|
|
2019-08-15 11:34:54 -05:00
|
|
|
return false, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(sr.Entries) > 1 {
|
|
|
|
err := errors.ErrEntriesExceeded
|
2019-12-11 15:16:37 -05:00
|
|
|
lc.Log.Error().Err(err).Str("bindDN", lc.BindDN).Str("username", username).
|
2019-08-15 11:34:54 -05:00
|
|
|
Str("baseDN", lc.Base).Msg("too many entries")
|
2019-12-13 00:53:18 -05:00
|
|
|
|
2019-08-15 11:34:54 -05:00
|
|
|
return false, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
userDN := sr.Entries[0].DN
|
|
|
|
user := map[string]string{}
|
2019-12-13 00:53:18 -05:00
|
|
|
|
2019-08-15 11:34:54 -05:00
|
|
|
for _, attr := range lc.Attributes {
|
|
|
|
user[attr] = sr.Entries[0].GetAttributeValue(attr)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bind as the user to verify their password
|
|
|
|
err = lc.Conn.Bind(userDN, password)
|
|
|
|
if err != nil {
|
2019-12-11 15:16:37 -05:00
|
|
|
lc.Log.Error().Err(err).Str("bindDN", userDN).Msg("user bind failed")
|
2019-08-15 11:34:54 -05:00
|
|
|
return false, user, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Rebind as the read only user for any further queries
|
|
|
|
if lc.BindDN != "" && lc.BindPassword != "" {
|
|
|
|
err = lc.Conn.Bind(lc.BindDN, lc.BindPassword)
|
|
|
|
if err != nil {
|
|
|
|
return true, user, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, user, nil
|
|
|
|
}
|