2021-04-19 22:30:27 +00:00
|
|
|
package ldap
|
|
|
|
|
|
|
|
import (
|
2021-07-22 17:17:34 +00:00
|
|
|
"context"
|
2021-04-26 09:53:06 +00:00
|
|
|
"errors"
|
2021-04-19 22:30:27 +00:00
|
|
|
"net"
|
2021-07-19 11:41:29 +00:00
|
|
|
"strings"
|
2021-04-19 22:30:27 +00:00
|
|
|
|
2021-07-22 17:17:34 +00:00
|
|
|
"github.com/getsentry/sentry-go"
|
2021-04-26 09:53:06 +00:00
|
|
|
goldap "github.com/go-ldap/ldap/v3"
|
2021-07-19 11:41:29 +00:00
|
|
|
"github.com/google/uuid"
|
2021-05-04 19:49:15 +00:00
|
|
|
"github.com/nmcclain/ldap"
|
2021-09-09 13:52:24 +00:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2021-07-19 11:41:29 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2021-09-09 13:52:24 +00:00
|
|
|
"goauthentik.io/internal/outpost/ldap/metrics"
|
2021-09-05 17:47:30 +00:00
|
|
|
"goauthentik.io/internal/utils"
|
2021-04-19 22:30:27 +00:00
|
|
|
)
|
|
|
|
|
2021-07-19 11:41:29 +00:00
|
|
|
type SearchRequest struct {
|
|
|
|
ldap.SearchRequest
|
|
|
|
BindDN string
|
|
|
|
id string
|
|
|
|
conn net.Conn
|
|
|
|
log *log.Entry
|
2021-07-22 17:17:34 +00:00
|
|
|
ctx context.Context
|
2021-07-19 11:41:29 +00:00
|
|
|
}
|
|
|
|
|
2021-05-12 16:49:15 +00:00
|
|
|
func (ls *LDAPServer) Search(bindDN string, searchReq ldap.SearchRequest, conn net.Conn) (ldap.ServerSearchResult, error) {
|
2021-07-22 17:17:34 +00:00
|
|
|
span := sentry.StartSpan(context.TODO(), "authentik.providers.ldap.search", sentry.TransactionName("authentik.providers.ldap.search"))
|
2021-08-25 20:36:08 +00:00
|
|
|
rid := uuid.New().String()
|
|
|
|
span.SetTag("request_uid", rid)
|
2021-07-22 18:38:30 +00:00
|
|
|
span.SetTag("user.username", bindDN)
|
|
|
|
span.SetTag("ak_filter", searchReq.Filter)
|
|
|
|
span.SetTag("ak_base_dn", searchReq.BaseDN)
|
2021-07-22 17:17:34 +00:00
|
|
|
|
2021-07-19 11:41:29 +00:00
|
|
|
bindDN = strings.ToLower(bindDN)
|
|
|
|
req := SearchRequest{
|
|
|
|
SearchRequest: searchReq,
|
|
|
|
BindDN: bindDN,
|
|
|
|
conn: conn,
|
2021-09-26 12:01:22 +00:00
|
|
|
log: ls.log.WithField("bindDN", bindDN).WithField("requestId", rid).WithField("scope", ldap.ScopeMap[searchReq.Scope]).WithField("client", utils.GetIP(conn.RemoteAddr())).WithField("filter", searchReq.Filter).WithField("baseDN", searchReq.BaseDN),
|
2021-07-19 11:41:29 +00:00
|
|
|
id: rid,
|
2021-07-22 17:17:34 +00:00
|
|
|
ctx: span.Context(),
|
2021-07-19 11:41:29 +00:00
|
|
|
}
|
2021-07-23 18:00:23 +00:00
|
|
|
|
|
|
|
defer func() {
|
|
|
|
span.Finish()
|
2021-09-09 13:52:24 +00:00
|
|
|
metrics.Requests.With(prometheus.Labels{
|
2021-09-16 08:03:31 +00:00
|
|
|
"outpost_name": ls.ac.Outpost.Name,
|
|
|
|
"type": "search",
|
|
|
|
"filter": req.Filter,
|
|
|
|
"dn": req.BindDN,
|
|
|
|
"client": utils.GetIP(req.conn.RemoteAddr()),
|
2021-09-09 13:52:24 +00:00
|
|
|
}).Observe(float64(span.EndTime.Sub(span.StartTime)))
|
2021-07-23 18:00:23 +00:00
|
|
|
req.log.WithField("took-ms", span.EndTime.Sub(span.StartTime).Milliseconds()).Info("Search request")
|
|
|
|
}()
|
2021-07-19 11:41:29 +00:00
|
|
|
|
2021-07-22 17:17:34 +00:00
|
|
|
defer func() {
|
|
|
|
err := recover()
|
|
|
|
if err == nil {
|
|
|
|
return
|
|
|
|
}
|
2021-09-22 11:14:28 +00:00
|
|
|
log.WithError(err.(error)).Error("recover in search request")
|
2021-07-22 17:17:34 +00:00
|
|
|
sentry.CaptureException(err.(error))
|
|
|
|
}()
|
|
|
|
|
2021-04-26 21:25:31 +00:00
|
|
|
if searchReq.BaseDN == "" {
|
|
|
|
return ldap.ServerSearchResult{ResultCode: ldap.LDAPResultSuccess}, nil
|
|
|
|
}
|
|
|
|
bd, err := goldap.ParseDN(searchReq.BaseDN)
|
2021-04-19 22:30:27 +00:00
|
|
|
if err != nil {
|
2021-07-19 11:41:29 +00:00
|
|
|
req.log.WithError(err).Info("failed to parse basedn")
|
2021-04-26 09:53:06 +00:00
|
|
|
return ldap.ServerSearchResult{ResultCode: ldap.LDAPResultOperationsError}, errors.New("invalid DN")
|
2021-04-19 22:30:27 +00:00
|
|
|
}
|
2021-04-26 09:53:06 +00:00
|
|
|
for _, provider := range ls.providers {
|
|
|
|
providerBase, _ := goldap.ParseDN(provider.BaseDN)
|
2021-09-26 12:01:22 +00:00
|
|
|
if providerBase.AncestorOf(bd) || providerBase.Equal(bd) {
|
2021-07-19 11:41:29 +00:00
|
|
|
return provider.Search(req)
|
2021-04-19 22:30:27 +00:00
|
|
|
}
|
|
|
|
}
|
2021-04-26 21:25:31 +00:00
|
|
|
return ldap.ServerSearchResult{ResultCode: ldap.LDAPResultOperationsError}, errors.New("no provider could handle request")
|
2021-04-19 22:30:27 +00:00
|
|
|
}
|