2021-11-05 09:37:30 +00:00
|
|
|
package search
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-12-16 10:00:19 +00:00
|
|
|
"fmt"
|
2021-11-05 09:37:30 +00:00
|
|
|
"net"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/getsentry/sentry-go"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"github.com/nmcclain/ldap"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"goauthentik.io/internal/utils"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Request struct {
|
|
|
|
ldap.SearchRequest
|
|
|
|
BindDN string
|
|
|
|
log *log.Entry
|
|
|
|
|
|
|
|
id string
|
|
|
|
conn net.Conn
|
|
|
|
ctx context.Context
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewRequest(bindDN string, searchReq ldap.SearchRequest, conn net.Conn) (*Request, *sentry.Span) {
|
|
|
|
rid := uuid.New().String()
|
|
|
|
bindDN = strings.ToLower(bindDN)
|
2023-05-09 09:22:57 +00:00
|
|
|
span := sentry.StartSpan(context.TODO(), "authentik.providers.ldap.search", sentry.WithTransactionName("authentik.providers.ldap.search"))
|
2021-12-16 10:00:19 +00:00
|
|
|
span.Description = fmt.Sprintf("%s (%s)", searchReq.BaseDN, ldap.ScopeMap[searchReq.Scope])
|
2021-11-05 09:37:30 +00:00
|
|
|
span.SetTag("request_uid", rid)
|
2021-12-16 10:19:57 +00:00
|
|
|
hub := sentry.GetHubFromContext(span.Context())
|
|
|
|
if hub == nil {
|
|
|
|
hub = sentry.CurrentHub()
|
|
|
|
}
|
|
|
|
hub.Scope().SetUser(sentry.User{
|
2021-12-16 10:00:19 +00:00
|
|
|
Username: bindDN,
|
|
|
|
ID: bindDN,
|
|
|
|
IPAddress: utils.GetIP(conn.RemoteAddr()),
|
|
|
|
})
|
|
|
|
span.SetTag("ldap_filter", searchReq.Filter)
|
|
|
|
span.SetTag("ldap_base_dn", searchReq.BaseDN)
|
2021-11-05 09:37:30 +00:00
|
|
|
return &Request{
|
|
|
|
SearchRequest: searchReq,
|
|
|
|
BindDN: bindDN,
|
|
|
|
conn: conn,
|
|
|
|
log: 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),
|
|
|
|
id: rid,
|
|
|
|
ctx: span.Context(),
|
|
|
|
}, span
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Request) Context() context.Context {
|
|
|
|
return r.ctx
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Request) Log() *log.Entry {
|
|
|
|
return r.log
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Request) RemoteAddr() string {
|
|
|
|
return utils.GetIP(r.conn.RemoteAddr())
|
|
|
|
}
|