2021-04-19 22:30:27 +00:00
|
|
|
package ldap
|
|
|
|
|
|
|
|
import (
|
2021-07-22 17:17:34 +00:00
|
|
|
"context"
|
2021-04-19 22:30:27 +00:00
|
|
|
"net"
|
2021-05-12 16:49:15 +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-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 BindRequest struct {
|
|
|
|
BindDN string
|
|
|
|
BindPW 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-04 19:49:15 +00:00
|
|
|
func (ls *LDAPServer) Bind(bindDN string, bindPW string, conn net.Conn) (ldap.LDAPResultCode, error) {
|
2021-07-22 17:17:34 +00:00
|
|
|
span := sentry.StartSpan(context.TODO(), "authentik.providers.ldap.bind",
|
|
|
|
sentry.TransactionName("authentik.providers.ldap.bind"))
|
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)
|
2021-07-22 17:17:34 +00:00
|
|
|
|
2021-05-12 16:49:15 +00:00
|
|
|
bindDN = strings.ToLower(bindDN)
|
2021-07-19 11:41:29 +00:00
|
|
|
req := BindRequest{
|
|
|
|
BindDN: bindDN,
|
|
|
|
BindPW: bindPW,
|
|
|
|
conn: conn,
|
2021-09-05 17:47:30 +00:00
|
|
|
log: ls.log.WithField("bindDN", bindDN).WithField("requestId", rid).WithField("client", utils.GetIP(conn.RemoteAddr())),
|
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-09-09 13:52:24 +00:00
|
|
|
defer func() {
|
|
|
|
span.Finish()
|
|
|
|
metrics.Requests.With(prometheus.Labels{
|
2021-09-16 08:03:31 +00:00
|
|
|
"outpost_name": ls.ac.Outpost.Name,
|
|
|
|
"type": "bind",
|
|
|
|
"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)))
|
|
|
|
req.log.WithField("took-ms", span.EndTime.Sub(span.StartTime).Milliseconds()).Info("Bind request")
|
|
|
|
}()
|
2021-04-26 09:53:06 +00:00
|
|
|
for _, instance := range ls.providers {
|
|
|
|
username, err := instance.getUsername(bindDN)
|
|
|
|
if err == nil {
|
2021-07-19 11:41:29 +00:00
|
|
|
return instance.Bind(username, req)
|
2021-05-07 09:46:26 +00:00
|
|
|
} else {
|
|
|
|
ls.log.WithError(err).Debug("Username not for instance")
|
2021-04-25 20:07:12 +00:00
|
|
|
}
|
|
|
|
}
|
2021-07-19 11:41:29 +00:00
|
|
|
req.log.WithField("request", "bind").Warning("No provider found for request")
|
2021-09-09 13:52:24 +00:00
|
|
|
metrics.RequestsRejected.With(prometheus.Labels{
|
2021-09-16 08:03:31 +00:00
|
|
|
"outpost_name": ls.ac.Outpost.Name,
|
|
|
|
"type": "bind",
|
|
|
|
"reason": "no_provider",
|
|
|
|
"dn": bindDN,
|
|
|
|
"client": utils.GetIP(conn.RemoteAddr()),
|
2021-09-09 13:52:24 +00:00
|
|
|
}).Inc()
|
2021-04-26 09:53:06 +00:00
|
|
|
return ldap.LDAPResultOperationsError, nil
|
2021-04-19 22:30:27 +00:00
|
|
|
}
|
2021-08-21 14:17:30 +00:00
|
|
|
|
|
|
|
func (ls *LDAPServer) TimerFlowCacheExpiry() {
|
|
|
|
for _, p := range ls.providers {
|
|
|
|
ls.log.WithField("flow", p.flowSlug).Debug("Pre-heating flow cache")
|
|
|
|
p.TimerFlowCacheExpiry()
|
|
|
|
}
|
|
|
|
}
|