2021-04-19 22:30:27 +00:00
|
|
|
package ldap
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
|
2022-05-08 14:48:53 +00:00
|
|
|
"github.com/getsentry/sentry-go"
|
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"
|
2022-05-08 14:48:53 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2021-11-05 09:37:30 +00:00
|
|
|
"goauthentik.io/internal/outpost/ldap/bind"
|
2021-09-09 13:52:24 +00:00
|
|
|
"goauthentik.io/internal/outpost/ldap/metrics"
|
2021-04-19 22:30:27 +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-11-05 09:37:30 +00:00
|
|
|
req, span := bind.NewRequest(bindDN, bindPW, conn)
|
2022-05-29 19:45:25 +00:00
|
|
|
selectedApp := ""
|
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",
|
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("Bind request")
|
2021-09-09 13:52:24 +00:00
|
|
|
}()
|
2022-05-08 14:48:53 +00:00
|
|
|
|
|
|
|
defer func() {
|
|
|
|
err := recover()
|
|
|
|
if err == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
log.WithError(err.(error)).Error("recover in bind request")
|
|
|
|
sentry.CaptureException(err.(error))
|
|
|
|
}()
|
|
|
|
|
2021-04-26 09:53:06 +00:00
|
|
|
for _, instance := range ls.providers {
|
2021-11-05 09:37:30 +00:00
|
|
|
username, err := instance.binder.GetUsername(bindDN)
|
2021-04-26 09:53:06 +00:00
|
|
|
if err == nil {
|
2022-05-29 19:45:25 +00:00
|
|
|
selectedApp = instance.GetAppSlug()
|
2021-11-05 09:37:30 +00:00
|
|
|
return instance.binder.Bind(username, req)
|
2021-05-07 09:46:26 +00:00
|
|
|
} else {
|
2021-11-05 09:37:30 +00:00
|
|
|
req.Log().WithError(err).Debug("Username not for instance")
|
2021-04-25 20:07:12 +00:00
|
|
|
}
|
|
|
|
}
|
2021-11-05 09:37:30 +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",
|
2022-05-29 19:45:25 +00:00
|
|
|
"app": "",
|
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
|
|
|
}
|