2021-11-05 09:37:30 +00:00
|
|
|
package direct
|
2021-04-26 09:53:06 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"strings"
|
|
|
|
|
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-06-29 14:21:00 +00:00
|
|
|
"goauthentik.io/api"
|
2021-07-17 19:24:11 +00:00
|
|
|
"goauthentik.io/internal/outpost"
|
2021-11-05 09:37:30 +00:00
|
|
|
"goauthentik.io/internal/outpost/ldap/bind"
|
|
|
|
"goauthentik.io/internal/outpost/ldap/flags"
|
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/server"
|
2021-04-26 09:53:06 +00:00
|
|
|
)
|
|
|
|
|
2021-04-29 18:26:14 +00:00
|
|
|
const ContextUserKey = "ak_user"
|
|
|
|
|
2021-11-05 09:37:30 +00:00
|
|
|
type DirectBinder struct {
|
|
|
|
si server.LDAPServerInstance
|
|
|
|
log *log.Entry
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewDirectBinder(si server.LDAPServerInstance) *DirectBinder {
|
|
|
|
db := &DirectBinder{
|
|
|
|
si: si,
|
|
|
|
log: log.WithField("logger", "authentik.outpost.ldap.binder.direct"),
|
|
|
|
}
|
|
|
|
db.log.Info("initialised direct binder")
|
|
|
|
return db
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *DirectBinder) GetUsername(dn string) (string, error) {
|
|
|
|
if !strings.HasSuffix(strings.ToLower(dn), strings.ToLower(db.si.GetBaseDN())) {
|
2021-04-26 09:53:06 +00:00
|
|
|
return "", errors.New("invalid base DN")
|
|
|
|
}
|
|
|
|
dns, err := goldap.ParseDN(dn)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
for _, part := range dns.RDNs {
|
|
|
|
for _, attribute := range part.Attributes {
|
2021-05-07 09:46:26 +00:00
|
|
|
if strings.ToLower(attribute.Type) == "cn" {
|
2021-04-26 09:53:06 +00:00
|
|
|
return attribute.Value, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-05-07 09:46:26 +00:00
|
|
|
return "", errors.New("failed to find cn")
|
2021-04-26 09:53:06 +00:00
|
|
|
}
|
|
|
|
|
2021-11-05 09:37:30 +00:00
|
|
|
func (db *DirectBinder) Bind(username string, req *bind.Request) (ldap.LDAPResultCode, error) {
|
|
|
|
fe := outpost.NewFlowExecutor(req.Context(), db.si.GetFlowSlug(), db.si.GetAPIClient().GetConfig(), log.Fields{
|
2021-07-19 11:41:29 +00:00
|
|
|
"bindDN": req.BindDN,
|
2021-11-05 09:37:30 +00:00
|
|
|
"client": req.RemoteAddr(),
|
|
|
|
"requestId": req.ID(),
|
2021-07-19 11:41:29 +00:00
|
|
|
})
|
2021-11-05 09:37:30 +00:00
|
|
|
fe.DelegateClientIP(req.RemoteAddr())
|
2021-07-17 19:24:11 +00:00
|
|
|
fe.Params.Add("goauthentik.io/outpost/ldap", "true")
|
2021-05-16 19:07:01 +00:00
|
|
|
|
2021-07-17 19:24:11 +00:00
|
|
|
fe.Answers[outpost.StageIdentification] = username
|
2021-07-19 11:41:29 +00:00
|
|
|
fe.Answers[outpost.StagePassword] = req.BindPW
|
2021-07-17 19:24:11 +00:00
|
|
|
|
|
|
|
passed, err := fe.Execute()
|
2021-07-18 20:22:35 +00:00
|
|
|
if !passed {
|
2021-09-09 13:52:24 +00:00
|
|
|
metrics.RequestsRejected.With(prometheus.Labels{
|
2021-11-05 09:37:30 +00:00
|
|
|
"outpost_name": db.si.GetOutpostName(),
|
2021-09-16 08:03:31 +00:00
|
|
|
"type": "bind",
|
|
|
|
"reason": "invalid_credentials",
|
|
|
|
"dn": req.BindDN,
|
2021-11-05 09:37:30 +00:00
|
|
|
"client": req.RemoteAddr(),
|
2021-09-09 13:52:24 +00:00
|
|
|
}).Inc()
|
2021-07-18 20:22:35 +00:00
|
|
|
return ldap.LDAPResultInvalidCredentials, nil
|
|
|
|
}
|
2021-07-17 19:24:11 +00:00
|
|
|
if err != nil {
|
2021-09-09 13:52:24 +00:00
|
|
|
metrics.RequestsRejected.With(prometheus.Labels{
|
2021-11-05 09:37:30 +00:00
|
|
|
"outpost_name": db.si.GetOutpostName(),
|
2021-09-16 08:03:31 +00:00
|
|
|
"type": "bind",
|
|
|
|
"reason": "flow_error",
|
|
|
|
"dn": req.BindDN,
|
2021-11-05 09:37:30 +00:00
|
|
|
"client": req.RemoteAddr(),
|
2021-09-09 13:52:24 +00:00
|
|
|
}).Inc()
|
2021-11-05 09:37:30 +00:00
|
|
|
req.Log().WithError(err).Warning("failed to execute flow")
|
2021-07-17 19:24:11 +00:00
|
|
|
return ldap.LDAPResultOperationsError, nil
|
2021-04-26 09:53:06 +00:00
|
|
|
}
|
2021-07-22 17:17:34 +00:00
|
|
|
|
2021-11-05 09:37:30 +00:00
|
|
|
access, err := fe.CheckApplicationAccess(db.si.GetAppSlug())
|
2021-07-17 19:24:11 +00:00
|
|
|
if !access {
|
2021-11-05 09:37:30 +00:00
|
|
|
req.Log().Info("Access denied for user")
|
2021-09-09 13:52:24 +00:00
|
|
|
metrics.RequestsRejected.With(prometheus.Labels{
|
2021-11-05 09:37:30 +00:00
|
|
|
"outpost_name": db.si.GetOutpostName(),
|
2021-09-16 08:03:31 +00:00
|
|
|
"type": "bind",
|
|
|
|
"reason": "access_denied",
|
|
|
|
"dn": req.BindDN,
|
2021-11-05 09:37:30 +00:00
|
|
|
"client": req.RemoteAddr(),
|
2021-09-09 13:52:24 +00:00
|
|
|
}).Inc()
|
2021-05-16 19:07:01 +00:00
|
|
|
return ldap.LDAPResultInsufficientAccessRights, nil
|
|
|
|
}
|
2021-04-26 09:53:06 +00:00
|
|
|
if err != nil {
|
2021-09-09 13:52:24 +00:00
|
|
|
metrics.RequestsRejected.With(prometheus.Labels{
|
2021-11-05 09:37:30 +00:00
|
|
|
"outpost_name": db.si.GetOutpostName(),
|
2021-09-16 08:03:31 +00:00
|
|
|
"type": "bind",
|
|
|
|
"reason": "access_check_fail",
|
|
|
|
"dn": req.BindDN,
|
2021-11-05 09:37:30 +00:00
|
|
|
"client": req.RemoteAddr(),
|
2021-09-09 13:52:24 +00:00
|
|
|
}).Inc()
|
2021-11-05 09:37:30 +00:00
|
|
|
req.Log().WithError(err).Warning("failed to check access")
|
2021-04-26 09:53:06 +00:00
|
|
|
return ldap.LDAPResultOperationsError, nil
|
|
|
|
}
|
2021-11-05 09:37:30 +00:00
|
|
|
req.Log().Info("User has access")
|
|
|
|
uisp := sentry.StartSpan(req.Context(), "authentik.providers.ldap.bind.user_info")
|
2021-04-29 18:26:14 +00:00
|
|
|
// Get user info to store in context
|
2021-07-17 19:24:11 +00:00
|
|
|
userInfo, _, err := fe.ApiClient().CoreApi.CoreUsersMeRetrieve(context.Background()).Execute()
|
2021-04-29 18:26:14 +00:00
|
|
|
if err != nil {
|
2021-09-09 13:52:24 +00:00
|
|
|
metrics.RequestsRejected.With(prometheus.Labels{
|
2021-11-05 09:37:30 +00:00
|
|
|
"outpost_name": db.si.GetOutpostName(),
|
2021-09-16 08:03:31 +00:00
|
|
|
"type": "bind",
|
|
|
|
"reason": "user_info_fail",
|
|
|
|
"dn": req.BindDN,
|
2021-11-05 09:37:30 +00:00
|
|
|
"client": req.RemoteAddr(),
|
2021-09-09 13:52:24 +00:00
|
|
|
}).Inc()
|
2021-11-05 09:37:30 +00:00
|
|
|
req.Log().WithError(err).Warning("failed to get user info")
|
2021-04-29 18:26:14 +00:00
|
|
|
return ldap.LDAPResultOperationsError, nil
|
|
|
|
}
|
2021-11-05 09:37:30 +00:00
|
|
|
cs := db.SearchAccessCheck(userInfo.User)
|
|
|
|
flags := flags.UserFlags{
|
2021-08-05 16:16:06 +00:00
|
|
|
UserPk: userInfo.User.Pk,
|
2021-07-19 11:41:29 +00:00
|
|
|
CanSearch: cs != nil,
|
2021-05-04 19:49:15 +00:00
|
|
|
}
|
2021-11-05 09:37:30 +00:00
|
|
|
db.si.SetFlags(req.BindDN, flags)
|
|
|
|
if flags.CanSearch {
|
|
|
|
req.Log().WithField("group", cs).Info("Allowed access to search")
|
2021-07-19 11:41:29 +00:00
|
|
|
}
|
2021-07-22 17:17:34 +00:00
|
|
|
uisp.Finish()
|
2021-04-26 09:53:06 +00:00
|
|
|
return ldap.LDAPResultSuccess, nil
|
|
|
|
}
|
|
|
|
|
2021-05-04 22:03:19 +00:00
|
|
|
// SearchAccessCheck Check if the current user is allowed to search
|
2021-11-05 09:37:30 +00:00
|
|
|
func (db *DirectBinder) SearchAccessCheck(user api.UserSelf) *string {
|
2021-05-04 22:03:19 +00:00
|
|
|
for _, group := range user.Groups {
|
2021-11-05 09:37:30 +00:00
|
|
|
for _, allowedGroup := range db.si.GetSearchAllowedGroups() {
|
|
|
|
db.log.WithField("userGroup", group.Pk).WithField("allowedGroup", allowedGroup).Trace("Checking search access")
|
2021-05-16 19:07:01 +00:00
|
|
|
if group.Pk == allowedGroup.String() {
|
2021-07-19 11:41:29 +00:00
|
|
|
return &group.Name
|
2021-05-04 22:03:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-07-19 11:41:29 +00:00
|
|
|
return nil
|
2021-05-04 22:03:19 +00:00
|
|
|
}
|
2021-07-17 19:24:11 +00:00
|
|
|
|
2021-11-05 09:37:30 +00:00
|
|
|
func (db *DirectBinder) TimerFlowCacheExpiry() {
|
|
|
|
fe := outpost.NewFlowExecutor(context.Background(), db.si.GetFlowSlug(), db.si.GetAPIClient().GetConfig(), log.Fields{})
|
2021-08-21 14:17:30 +00:00
|
|
|
fe.Params.Add("goauthentik.io/outpost/ldap", "true")
|
|
|
|
fe.Params.Add("goauthentik.io/outpost/ldap-warmup", "true")
|
|
|
|
|
|
|
|
err := fe.WarmUp()
|
|
|
|
if err != nil {
|
2021-11-05 09:37:30 +00:00
|
|
|
db.log.WithError(err).Warning("failed to warm up flow cache")
|
2021-08-21 14:17:30 +00:00
|
|
|
}
|
|
|
|
}
|