outposts: implement general paginator for list API requests (#10619)

* outposts: implement general paginator

Signed-off-by: Jens Langhammer <jens@goauthentik.io>

* migrate LDAP

Signed-off-by: Jens Langhammer <jens@goauthentik.io>

* change main outpost refresh logic to use paginator everywhere

Signed-off-by: Jens Langhammer <jens@goauthentik.io>

* add comments to understand anything

Signed-off-by: Jens Langhammer <jens@goauthentik.io>

* actually use paginator everywhere

Signed-off-by: Jens Langhammer <jens@goauthentik.io>

---------

Signed-off-by: Jens Langhammer <jens@goauthentik.io>
This commit is contained in:
Jens L.
2024-07-29 22:14:18 +02:00
committed by GitHub
parent d79ac0e5bc
commit 1b285f85c0
8 changed files with 127 additions and 91 deletions

View File

@ -12,13 +12,13 @@ import (
"github.com/getsentry/sentry-go"
"github.com/prometheus/client_golang/prometheus"
"goauthentik.io/api/v3"
"goauthentik.io/internal/outpost/ak"
"goauthentik.io/internal/outpost/ldap/constants"
"goauthentik.io/internal/outpost/ldap/group"
"goauthentik.io/internal/outpost/ldap/metrics"
"goauthentik.io/internal/outpost/ldap/search"
"goauthentik.io/internal/outpost/ldap/server"
"goauthentik.io/internal/outpost/ldap/utils"
"goauthentik.io/internal/outpost/ldap/utils/paginator"
)
type DirectSearcher struct {
@ -120,9 +120,14 @@ func (ds *DirectSearcher) Search(req *search.Request) (ldap.ServerSearchResult,
return nil
}
u := paginator.FetchUsers(searchReq)
u, err := ak.Paginator(searchReq, ak.PaginatorOptions{
PageSize: 100,
Logger: ds.log,
})
uapisp.Finish()
if err != nil {
return err
}
users = &u
} else {
if flags.UserInfo == nil {
@ -161,8 +166,14 @@ func (ds *DirectSearcher) Search(req *search.Request) (ldap.ServerSearchResult,
searchReq = searchReq.MembersByPk([]int32{flags.UserPk})
}
g := paginator.FetchGroups(searchReq)
g, err := ak.Paginator(searchReq, ak.PaginatorOptions{
PageSize: 100,
Logger: ds.log,
})
gapisp.Finish()
if err != nil {
return err
}
req.Log().WithField("count", len(g)).Trace("Got results from API")
if !flags.CanSearch {
@ -177,7 +188,6 @@ func (ds *DirectSearcher) Search(req *search.Request) (ldap.ServerSearchResult,
}
}
}
groups = &g
return nil
})

View File

@ -11,6 +11,7 @@ import (
"github.com/prometheus/client_golang/prometheus"
log "github.com/sirupsen/logrus"
"goauthentik.io/api/v3"
"goauthentik.io/internal/outpost/ak"
"goauthentik.io/internal/outpost/ldap/constants"
"goauthentik.io/internal/outpost/ldap/flags"
"goauthentik.io/internal/outpost/ldap/group"
@ -19,7 +20,6 @@ import (
"goauthentik.io/internal/outpost/ldap/search/direct"
"goauthentik.io/internal/outpost/ldap/server"
"goauthentik.io/internal/outpost/ldap/utils"
"goauthentik.io/internal/outpost/ldap/utils/paginator"
)
type MemorySearcher struct {
@ -38,8 +38,17 @@ func NewMemorySearcher(si server.LDAPServerInstance) *MemorySearcher {
ds: direct.NewDirectSearcher(si),
}
ms.log.Debug("initialised memory searcher")
ms.users = paginator.FetchUsers(ms.si.GetAPIClient().CoreApi.CoreUsersList(context.TODO()).IncludeGroups(true))
ms.groups = paginator.FetchGroups(ms.si.GetAPIClient().CoreApi.CoreGroupsList(context.TODO()).IncludeUsers(true))
// Error is not handled here, we get an empty/truncated list and the error is logged
users, _ := ak.Paginator(ms.si.GetAPIClient().CoreApi.CoreUsersList(context.TODO()).IncludeGroups(true), ak.PaginatorOptions{
PageSize: 100,
Logger: ms.log,
})
ms.users = users
groups, _ := ak.Paginator(ms.si.GetAPIClient().CoreApi.CoreGroupsList(context.TODO()).IncludeUsers(true), ak.PaginatorOptions{
PageSize: 100,
Logger: ms.log,
})
ms.groups = groups
return ms
}