perf(sessions): Use session repository with permissions

This builds on previous performance improvements to the list behavior by
reducing the number of queries against the database and the amount of
data transfered in order to retrieve a list of sessions. Previously, a
query was performed to retrieve a tuple of `(session_id, scope_id,
user_id)` for the requested scope, including all child scopes if the
request was recursive. These tuples were evaluated against the
requesting user's grants to identify the sessions that they were
authorized to list.  In the case of clusters with a large number of
sessions, this would result in a large amount of data needing to be
transfered for sessions that would not be used.  Then a second query
would be used to retrieve the full session information for the session
ids that were authorized.

This changes the list behavior to evaluate a set of permissions from
the requesting user's grants, use these permissions when constructing a
session repository, and retrieve the list of sessions the user is
authorized to list in a signle query.

Ref:
    d741034829
    32070678dc
This commit is contained in:
Timothy Messier committed 2022-09-07 17:53:39 +00:00
1 parent 2fbdcf6ce0
commit 68568828e6
3 files changed
+36 -57

No files matched your search

@@ -84,9 +84,6 @@ func BenchmarkSessionList(b *testing.B) {
iamRepo, err := iam.NewRepository(rw, rw, kmsThing)
require.NoError(b, err)
sessRepo, err := session.NewRepository(rw, rw, kmsThing)
require.NoError(b, err)
authTokenRepo, err := authtoken.NewRepository(rw, rw, kmsThing)
require.NoError(b, err)
@@ -99,8 +96,8 @@ func BenchmarkSessionList(b *testing.B) {
iamRepoFn := func() (*iam.Repository, error) {
return iamRepo, nil
}
sessRepoFn := func() (*session.Repository, error) {
return sessRepo, nil
sessRepoFn := func(opt ...session.Option) (*session.Repository, error) {
return session.NewRepository(rw, rw, kmsThing, opt...)
}
authTokenRepoFn := func() (*authtoken.Repository, error) {
return authTokenRepo, nil
@@ -8,7 +8,6 @@ import (
"github.com/hashicorp/boundary/globals"
"github.com/hashicorp/boundary/internal/daemon/controller/auth"
"github.com/hashicorp/boundary/internal/daemon/controller/common"
"github.com/hashicorp/boundary/internal/daemon/controller/common/scopeids"
"github.com/hashicorp/boundary/internal/daemon/controller/handlers"
"github.com/hashicorp/boundary/internal/errors"
pbs "github.com/hashicorp/boundary/internal/gen/controller/api/services"
@@ -19,6 +18,7 @@ import (
"github.com/hashicorp/boundary/internal/types/action"
"github.com/hashicorp/boundary/internal/types/resource"
"github.com/hashicorp/boundary/internal/types/scope"
"github.com/hashicorp/boundary/sdk/pbs/controller/api/resources/scopes"
pb "github.com/hashicorp/boundary/sdk/pbs/controller/api/resources/sessions"
"google.golang.org/grpc/codes"
)
@@ -139,34 +139,26 @@ func (s Service) ListSessions(ctx context.Context, req *pbs.ListSessionsRequest)
}
}
repo, err := s.repoFn()
var scopeIds map[string]*scopes.ScopeInfo
var err error
if !req.GetRecursive() {
scopeIds = map[string]*scopes.ScopeInfo{authResults.Scope.Id: authResults.Scope}
} else {
scopeIds, err = authResults.ScopesAuthorizedForList(ctx, req.GetScopeId(), resource.Session)
}
listPerms := authResults.ACL().ListPermissions(scopeIds, resource.Session, IdActions)
repo, err := s.repoFn(session.WithPermissions(&perms.UserPermissions{
UserId: authResults.UserId,
Permissions: listPerms,
}))
if err != nil {
return nil, errors.Wrap(ctx, err, op)
}
scopeResourceInfo, err := scopeids.GetListingResourceInformation(
ctx,
scopeids.GetListingResourceInformationInput{
IamRepoFn: s.iamRepoFn,
AuthResults: authResults,
RootScopeId: req.GetScopeId(),
Type: resource.Session,
Recursive: req.GetRecursive(),
AuthzProtectedEntityProvider: session.ListForAuthzCheck(repo, session.WithTerminated(req.IncludeTerminated)),
ActionSet: IdActions,
},
)
if err != nil {
return nil, err
}
// If no scopes match or we match scopes but there are no resources in them
// that we are authorized to see, return an empty response
if len(scopeResourceInfo.ScopeIds) == 0 ||
len(scopeResourceInfo.ResourceIds) == 0 {
return &pbs.ListSessionsResponse{}, nil
}
sesList, err := s.listFromRepo(ctx, scopeResourceInfo.ResourceIds)
sesList, err := repo.ListSessions(ctx, session.WithTerminated(req.GetIncludeTerminated()))
if err != nil {
return nil, err
}
@@ -183,14 +175,21 @@ func (s Service) ListSessions(ctx context.Context, req *pbs.ListSessionsRequest)
Type: resource.Session,
}
for _, item := range sesList {
res.Id = item.GetPublicId()
res.ScopeId = item.GetProjectId()
authorizedActions := authResults.FetchActionSetForId(ctx, item.GetPublicId(), IdActions, auth.WithResource(&res)).Strings()
if len(authorizedActions) == 0 {
continue
}
outputFields := authResults.FetchOutputFields(res, action.List).SelfOrDefaults(authResults.UserId)
outputOpts := make([]handlers.Option, 0, 3)
outputOpts = append(outputOpts, handlers.WithOutputFields(&outputFields))
if outputFields.Has(globals.ScopeField) {
outputOpts = append(outputOpts, handlers.WithScope(scopeResourceInfo.ScopeResourceMap[item.ProjectId].ScopeInfo))
outputOpts = append(outputOpts, handlers.WithScope(scopeIds[item.ProjectId]))
}
if outputFields.Has(globals.AuthorizedActionsField) {
outputOpts = append(outputOpts, handlers.WithAuthorizedActions(scopeResourceInfo.ScopeResourceMap[item.ProjectId].Resources[item.PublicId].AuthorizedActions.Strings()))
outputOpts = append(outputOpts, handlers.WithAuthorizedActions(authorizedActions))
}
item, err := toProto(ctx, item, outputOpts...)
@@ -295,18 +294,6 @@ func (s Service) getFromRepo(ctx context.Context, id string) (*session.Session,
return sess, nil
}
func (s Service) listFromRepo(ctx context.Context, sessionIds []string) ([]*session.Session, error) {
repo, err := s.repoFn()
if err != nil {
return nil, err
}
sesList, err := repo.ListSessions(ctx, session.WithSessionIds(sessionIds...))
if err != nil {
return nil, err
}
return sesList, nil
}
func (s Service) cancelInRepo(ctx context.Context, id string, version uint32) (*session.Session, error) {
const op = "sessions.(Service).cancelInRepo"
repo, err := s.repoFn()
@@ -44,14 +44,11 @@ func TestGetSession(t *testing.T) {
rw := db.New(conn)
sessRepo, err := session.NewRepository(rw, rw, kms)
require.NoError(t, err)
iamRepoFn := func() (*iam.Repository, error) {
return iamRepo, nil
}
sessRepoFn := func() (*session.Repository, error) {
return sessRepo, nil
sessRepoFn := func(opt ...session.Option) (*session.Repository, error) {
return session.NewRepository(rw, rw, kms, opt...)
}
tokenRepoFn := func() (*authtoken.Repository, error) {
return authtoken.NewRepository(rw, rw, kms)
@@ -172,8 +169,8 @@ func TestList_Self(t *testing.T) {
iamRepoFn := func() (*iam.Repository, error) {
return iamRepo, nil
}
sessRepoFn := func() (*session.Repository, error) {
return session.NewRepository(rw, rw, kms)
sessRepoFn := func(opt ...session.Option) (*session.Repository, error) {
return session.NewRepository(rw, rw, kms, opt...)
}
tokenRepoFn := func() (*authtoken.Repository, error) {
return authtoken.NewRepository(rw, rw, kms)
@@ -259,8 +256,8 @@ func TestList(t *testing.T) {
iamRepoFn := func() (*iam.Repository, error) {
return iamRepo, nil
}
sessRepoFn := func() (*session.Repository, error) {
return sessRepo, nil
sessRepoFn := func(opt ...session.Option) (*session.Repository, error) {
return session.NewRepository(rw, rw, kms, opt...)
}
tokenRepoFn := func() (*authtoken.Repository, error) {
return authtoken.NewRepository(rw, rw, kms)
@@ -552,14 +549,12 @@ func TestCancel(t *testing.T) {
iamRepo := iam.TestRepo(t, conn, wrap)
rw := db.New(conn)
sessRepo, err := session.NewRepository(rw, rw, kms)
require.NoError(t, err)
iamRepoFn := func() (*iam.Repository, error) {
return iamRepo, nil
}
sessRepoFn := func() (*session.Repository, error) {
return sessRepo, nil
sessRepoFn := func(opt ...session.Option) (*session.Repository, error) {
return session.NewRepository(rw, rw, kms, opt...)
}
tokenRepoFn := func() (*authtoken.Repository, error) {
return authtoken.NewRepository(rw, rw, kms)