2021-04-19 23:30:27 +01:00
|
|
|
package ldap
|
|
|
|
|
|
|
|
import (
|
2021-05-16 20:07:01 +01:00
|
|
|
"context"
|
2021-04-26 10:53:06 +01:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
2021-05-08 19:59:31 +01:00
|
|
|
"sync"
|
2021-04-26 10:53:06 +01:00
|
|
|
|
2021-05-04 23:03:19 +01:00
|
|
|
"github.com/go-openapi/strfmt"
|
2021-04-19 23:30:27 +01:00
|
|
|
log "github.com/sirupsen/logrus"
|
2021-11-05 09:37:30 +00:00
|
|
|
"goauthentik.io/api"
|
|
|
|
directbind "goauthentik.io/internal/outpost/ldap/bind/direct"
|
|
|
|
"goauthentik.io/internal/outpost/ldap/constants"
|
|
|
|
"goauthentik.io/internal/outpost/ldap/flags"
|
|
|
|
directsearch "goauthentik.io/internal/outpost/ldap/search/direct"
|
|
|
|
memorysearch "goauthentik.io/internal/outpost/ldap/search/memory"
|
2021-08-21 16:53:09 +01:00
|
|
|
)
|
|
|
|
|
2021-11-12 23:26:01 +00:00
|
|
|
func (ls *LDAPServer) getCurrentProvider(pk int32) *ProviderInstance {
|
|
|
|
for _, p := range ls.providers {
|
|
|
|
if p.outpostPk == pk {
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-04-19 23:30:27 +01:00
|
|
|
func (ls *LDAPServer) Refresh() error {
|
2021-05-16 20:35:23 +01:00
|
|
|
outposts, _, err := ls.ac.Client.OutpostsApi.OutpostsLdapList(context.Background()).Execute()
|
2021-04-26 10:53:06 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-05-16 20:07:01 +01:00
|
|
|
if len(outposts.Results) < 1 {
|
2021-04-26 10:53:06 +01:00
|
|
|
return errors.New("no ldap provider defined")
|
|
|
|
}
|
2021-05-16 20:07:01 +01:00
|
|
|
providers := make([]*ProviderInstance, len(outposts.Results))
|
|
|
|
for idx, provider := range outposts.Results {
|
2021-11-05 09:37:30 +00:00
|
|
|
userDN := strings.ToLower(fmt.Sprintf("ou=%s,%s", constants.OUUsers, *provider.BaseDn))
|
|
|
|
groupDN := strings.ToLower(fmt.Sprintf("ou=%s,%s", constants.OUGroups, *provider.BaseDn))
|
|
|
|
virtualGroupDN := strings.ToLower(fmt.Sprintf("ou=%s,%s", constants.OUVirtualGroups, *provider.BaseDn))
|
2021-07-13 17:24:18 +01:00
|
|
|
logger := log.WithField("logger", "authentik.outpost.ldap").WithField("provider", provider.Name)
|
2021-11-12 23:26:01 +00:00
|
|
|
|
|
|
|
// Get existing instance so we can transfer boundUsers
|
|
|
|
existing := ls.getCurrentProvider(provider.Pk)
|
|
|
|
users := make(map[string]flags.UserFlags)
|
|
|
|
if existing != nil {
|
2021-11-18 18:36:27 +00:00
|
|
|
existing.boundUsersMutex.RLock()
|
2021-11-12 23:26:01 +00:00
|
|
|
users = existing.boundUsers
|
2021-11-18 18:36:27 +00:00
|
|
|
existing.boundUsersMutex.RUnlock()
|
2021-11-12 23:26:01 +00:00
|
|
|
}
|
|
|
|
|
2021-04-26 10:53:06 +01:00
|
|
|
providers[idx] = &ProviderInstance{
|
2021-05-16 20:07:01 +01:00
|
|
|
BaseDN: *provider.BaseDn,
|
2021-08-21 16:53:09 +01:00
|
|
|
VirtualGroupDN: virtualGroupDN,
|
2021-05-04 23:03:19 +01:00
|
|
|
GroupDN: groupDN,
|
|
|
|
UserDN: userDN,
|
2021-05-16 20:07:01 +01:00
|
|
|
appSlug: provider.ApplicationSlug,
|
|
|
|
flowSlug: provider.BindFlowSlug,
|
|
|
|
searchAllowedGroups: []*strfmt.UUID{(*strfmt.UUID)(provider.SearchGroup.Get())},
|
2021-05-08 19:59:31 +01:00
|
|
|
boundUsersMutex: sync.RWMutex{},
|
2021-11-12 23:26:01 +00:00
|
|
|
boundUsers: users,
|
2021-05-04 23:03:19 +01:00
|
|
|
s: ls,
|
2021-07-17 20:24:11 +01:00
|
|
|
log: logger,
|
2021-07-13 17:24:18 +01:00
|
|
|
tlsServerName: provider.TlsServerName,
|
2021-07-14 19:37:27 +01:00
|
|
|
uidStartNumber: *provider.UidStartNumber,
|
|
|
|
gidStartNumber: *provider.GidStartNumber,
|
2021-09-16 09:03:31 +01:00
|
|
|
outpostName: ls.ac.Outpost.Name,
|
2021-11-12 23:26:01 +00:00
|
|
|
outpostPk: provider.Pk,
|
2021-07-13 17:24:18 +01:00
|
|
|
}
|
|
|
|
if provider.Certificate.Get() != nil {
|
2021-07-21 22:53:43 +01:00
|
|
|
kp := provider.Certificate.Get()
|
|
|
|
err := ls.cs.AddKeypair(*kp)
|
2021-07-13 17:24:18 +01:00
|
|
|
if err != nil {
|
2021-07-21 22:53:43 +01:00
|
|
|
ls.log.WithError(err).Warning("Failed to initially fetch certificate")
|
2021-07-13 17:24:18 +01:00
|
|
|
}
|
2021-07-21 22:53:43 +01:00
|
|
|
providers[idx].cert = ls.cs.Get(*kp)
|
2021-04-26 10:53:06 +01:00
|
|
|
}
|
2021-11-05 09:37:30 +00:00
|
|
|
if *provider.SearchMode.Ptr() == api.SEARCHMODEENUM_CACHED {
|
|
|
|
providers[idx].searcher = memorysearch.NewMemorySearcher(providers[idx])
|
|
|
|
} else if *provider.SearchMode.Ptr() == api.SEARCHMODEENUM_DIRECT {
|
|
|
|
providers[idx].searcher = directsearch.NewDirectSearcher(providers[idx])
|
|
|
|
}
|
|
|
|
providers[idx].binder = directbind.NewDirectBinder(providers[idx])
|
2021-04-26 10:53:06 +01:00
|
|
|
}
|
|
|
|
ls.providers = providers
|
|
|
|
ls.log.Info("Update providers")
|
2021-04-19 23:30:27 +01:00
|
|
|
return nil
|
|
|
|
}
|