2021-04-19 22:30:27 +00:00
|
|
|
package ldap
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
2023-07-27 16:51:08 +00:00
|
|
|
"time"
|
2021-04-19 22:30:27 +00:00
|
|
|
|
2023-06-06 19:40:19 +00:00
|
|
|
"beryju.io/ldap"
|
2021-07-22 17:17:34 +00:00
|
|
|
"github.com/getsentry/sentry-go"
|
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"
|
2023-06-08 13:16:40 +00:00
|
|
|
"goauthentik.io/internal/outpost/ldap/constants"
|
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,
|
2023-07-27 16:51:08 +00:00
|
|
|
}).Observe(float64(span.EndTime.Sub(span.StartTime)) / float64(time.Second))
|
|
|
|
metrics.RequestsLegacy.With(prometheus.Labels{
|
|
|
|
"outpost_name": ls.ac.Outpost.Name,
|
|
|
|
"type": "search",
|
|
|
|
"app": selectedApp,
|
2021-09-09 13:52:24 +00:00
|
|
|
}).Observe(float64(span.EndTime.Sub(span.StartTime)))
|
2023-07-08 18:51:05 +00:00
|
|
|
req.Log().WithField("attributes", searchReq.Attributes).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))
|
|
|
|
}()
|
|
|
|
|
2023-06-08 13:16:40 +00:00
|
|
|
selectedProvider := ls.providerForRequest(req)
|
|
|
|
if selectedProvider == nil {
|
|
|
|
return ls.fallbackRootDSE(req)
|
2021-04-26 21:25:31 +00:00
|
|
|
}
|
2023-06-08 13:16:40 +00:00
|
|
|
selectedApp = selectedProvider.GetAppSlug()
|
|
|
|
result, err := ls.searchRoute(req, selectedProvider)
|
2023-07-08 18:51:05 +00:00
|
|
|
return result, err
|
2023-06-08 13:16:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ls *LDAPServer) fallbackRootDSE(req *search.Request) (ldap.ServerSearchResult, error) {
|
|
|
|
req.Log().Trace("returning fallback Root DSE")
|
2023-06-06 19:40:19 +00:00
|
|
|
return ldap.ServerSearchResult{
|
2023-02-22 14:26:41 +00:00
|
|
|
Entries: []*ldap.Entry{
|
|
|
|
{
|
|
|
|
DN: "",
|
|
|
|
Attributes: []*ldap.EntryAttribute{
|
|
|
|
{
|
|
|
|
Name: "objectClass",
|
2023-06-08 13:16:40 +00:00
|
|
|
Values: []string{constants.OCTop},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "entryDN",
|
|
|
|
Values: []string{""},
|
2023-02-22 14:26:41 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "subschemaSubentry",
|
|
|
|
Values: []string{"cn=subschema"},
|
|
|
|
},
|
2023-06-08 13:16:40 +00:00
|
|
|
{
|
|
|
|
Name: "namingContexts",
|
|
|
|
Values: []string{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "description",
|
|
|
|
Values: []string{
|
|
|
|
"This LDAP server requires an authenticated session.",
|
|
|
|
},
|
|
|
|
},
|
2023-02-22 14:26:41 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Referrals: []string{}, Controls: []ldap.Control{}, ResultCode: ldap.LDAPResultSuccess,
|
|
|
|
}, nil
|
2023-06-08 13:16:40 +00:00
|
|
|
|
2021-04-19 22:30:27 +00:00
|
|
|
}
|