2021-04-19 22:30:27 +00:00
|
|
|
package ldap
|
|
|
|
|
|
|
|
import (
|
2021-04-26 09:53:06 +00:00
|
|
|
"errors"
|
2021-04-19 22:30:27 +00:00
|
|
|
"net"
|
2021-12-09 19:55:51 +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-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-11-05 09:37:30 +00:00
|
|
|
"goauthentik.io/internal/outpost/ldap/search"
|
2021-04-19 22:30:27 +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-11-05 09:37:30 +00:00
|
|
|
req, span := search.NewRequest(bindDN, searchReq, conn)
|
2022-05-29 19:45:25 +00:00
|
|
|
selectedApp := ""
|
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",
|
2022-05-29 19:45:25 +00:00
|
|
|
"app": selectedApp,
|
2021-09-09 13:52:24 +00:00
|
|
|
}).Observe(float64(span.EndTime.Sub(span.StartTime)))
|
2021-11-05 09:37:30 +00:00
|
|
|
req.Log().WithField("took-ms", span.EndTime.Sub(span.StartTime).Milliseconds()).Info("Search request")
|
2021-07-23 18:00:23 +00:00
|
|
|
}()
|
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
|
|
|
|
}
|
2021-12-09 19:55:51 +00:00
|
|
|
bd, err := goldap.ParseDN(strings.ToLower(searchReq.BaseDN))
|
2021-04-19 22:30:27 +00:00
|
|
|
if err != nil {
|
2021-11-05 09:37:30 +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 {
|
2021-12-09 19:55:51 +00:00
|
|
|
providerBase, _ := goldap.ParseDN(strings.ToLower(provider.BaseDN))
|
2021-09-26 12:01:22 +00:00
|
|
|
if providerBase.AncestorOf(bd) || providerBase.Equal(bd) {
|
2022-05-29 19:45:25 +00:00
|
|
|
selectedApp = provider.GetAppSlug()
|
2021-11-05 09:37:30 +00:00
|
|
|
return provider.searcher.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
|
|
|
}
|