Add support for recursive listing (#885)
This commit is contained in:
1 parent
1f9fcad1cd
commit
6cd97a4a6e
77 files changed
+2244
-1448
No files matched your search
@@ -19,6 +19,7 @@ import (
|
||||
"github.com/hashicorp/boundary/internal/kms"
|
||||
"github.com/hashicorp/boundary/internal/perms"
|
||||
"github.com/hashicorp/boundary/internal/servers/controller/common"
|
||||
"github.com/hashicorp/boundary/internal/servers/controller/common/scopeids"
|
||||
"github.com/hashicorp/boundary/internal/servers/controller/handlers"
|
||||
"github.com/hashicorp/boundary/internal/servers/controller/handlers/accounts"
|
||||
"github.com/hashicorp/boundary/internal/servers/controller/handlers/authtokens"
|
||||
@@ -129,17 +130,24 @@ func (s Service) ListAuthMethods(ctx context.Context, req *pbs.ListAuthMethodsRe
|
||||
if authResults.Error != nil {
|
||||
return nil, authResults.Error
|
||||
}
|
||||
ul, err := s.listFromRepo(ctx, authResults.Scope.GetId())
|
||||
|
||||
scopeIds, scopeInfoMap, err := scopeids.GetScopeIds(
|
||||
ctx, s.iamRepoFn, authResults, req.GetScopeId(), req.GetRecursive())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
ul, err := s.listFromRepo(ctx, scopeIds)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
finalItems := make([]*pb.AuthMethod, 0, len(ul))
|
||||
res := &perms.Resource{
|
||||
ScopeId: authResults.Scope.Id,
|
||||
Type: resource.AuthMethod,
|
||||
Type: resource.AuthMethod,
|
||||
}
|
||||
for _, item := range ul {
|
||||
item.Scope = authResults.Scope
|
||||
item.Scope = scopeInfoMap[item.GetScopeId()]
|
||||
res.ScopeId = item.Scope.Id
|
||||
item.AuthorizedActions = authResults.FetchActionSetForId(ctx, item.Id, IdActions, auth.WithResource(res)).Strings()
|
||||
if len(item.AuthorizedActions) > 0 {
|
||||
finalItems = append(finalItems, item)
|
||||
@@ -270,12 +278,12 @@ func (s Service) getFromRepo(ctx context.Context, id string) (*pb.AuthMethod, er
|
||||
return toAuthMethodProto(u)
|
||||
}
|
||||
|
||||
func (s Service) listFromRepo(ctx context.Context, scopeId string) ([]*pb.AuthMethod, error) {
|
||||
func (s Service) listFromRepo(ctx context.Context, scopeIds []string) ([]*pb.AuthMethod, error) {
|
||||
repo, err := s.pwRepoFn()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ul, err := repo.ListAuthMethods(ctx, scopeId)
|
||||
ul, err := repo.ListAuthMethods(ctx, scopeIds)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -194,30 +194,37 @@ func TestList(t *testing.T) {
|
||||
}
|
||||
|
||||
cases := []struct {
|
||||
name string
|
||||
scopeId string
|
||||
res *pbs.ListAuthMethodsResponse
|
||||
err error
|
||||
name string
|
||||
req *pbs.ListAuthMethodsRequest
|
||||
res *pbs.ListAuthMethodsResponse
|
||||
err error
|
||||
}{
|
||||
{
|
||||
name: "List Some Auth Methods",
|
||||
scopeId: oWithAuthMethods.GetPublicId(),
|
||||
res: &pbs.ListAuthMethodsResponse{Items: wantSomeAuthMethods},
|
||||
name: "List Some Auth Methods",
|
||||
req: &pbs.ListAuthMethodsRequest{ScopeId: oWithAuthMethods.GetPublicId()},
|
||||
res: &pbs.ListAuthMethodsResponse{Items: wantSomeAuthMethods},
|
||||
},
|
||||
{
|
||||
name: "List Other Auth Methods",
|
||||
scopeId: oWithOtherAuthMethods.GetPublicId(),
|
||||
res: &pbs.ListAuthMethodsResponse{Items: wantOtherAuthMethods},
|
||||
name: "List Other Auth Methods",
|
||||
req: &pbs.ListAuthMethodsRequest{ScopeId: oWithOtherAuthMethods.GetPublicId()},
|
||||
res: &pbs.ListAuthMethodsResponse{Items: wantOtherAuthMethods},
|
||||
},
|
||||
{
|
||||
name: "List No Auth Methods",
|
||||
scopeId: oNoAuthMethods.GetPublicId(),
|
||||
res: &pbs.ListAuthMethodsResponse{},
|
||||
name: "List No Auth Methods",
|
||||
req: &pbs.ListAuthMethodsRequest{ScopeId: oNoAuthMethods.GetPublicId()},
|
||||
res: &pbs.ListAuthMethodsResponse{},
|
||||
},
|
||||
{
|
||||
name: "Unfound Auth Method",
|
||||
scopeId: "o_DoesntExis",
|
||||
err: handlers.ApiErrorWithCode(codes.NotFound),
|
||||
name: "Unfound Auth Method",
|
||||
req: &pbs.ListAuthMethodsRequest{ScopeId: "o_DoesntExis"},
|
||||
err: handlers.ApiErrorWithCode(codes.NotFound),
|
||||
},
|
||||
{
|
||||
name: "List All Auth Methods Recursively",
|
||||
req: &pbs.ListAuthMethodsRequest{ScopeId: "global", Recursive: true},
|
||||
res: &pbs.ListAuthMethodsResponse{
|
||||
Items: append(wantSomeAuthMethods, wantOtherAuthMethods...),
|
||||
},
|
||||
},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
@@ -226,12 +233,12 @@ func TestList(t *testing.T) {
|
||||
s, err := authmethods.NewService(kms, pwRepoFn, iamRepoFn, atRepoFn)
|
||||
require.NoError(err, "Couldn't create new auth_method service.")
|
||||
|
||||
got, gErr := s.ListAuthMethods(auth.DisabledAuthTestContext(auth.WithScopeId(tc.scopeId)), &pbs.ListAuthMethodsRequest{ScopeId: tc.scopeId})
|
||||
got, gErr := s.ListAuthMethods(auth.DisabledAuthTestContext(auth.WithScopeId(tc.req.GetScopeId())), tc.req)
|
||||
if tc.err != nil {
|
||||
require.Error(gErr)
|
||||
assert.True(errors.Is(gErr, tc.err), "ListAuthMethods() for scope %q got error %v, wanted %v", tc.scopeId, gErr, tc.err)
|
||||
assert.True(errors.Is(gErr, tc.err), "ListAuthMethods() for scope %q got error %v, wanted %v", tc.req.GetScopeId(), gErr, tc.err)
|
||||
}
|
||||
assert.Empty(cmp.Diff(got, tc.res, protocmp.Transform()), "ListAuthMethods() for scope %q got response %q, wanted %q", tc.scopeId, got, tc.res)
|
||||
assert.Empty(cmp.Diff(got, tc.res, protocmp.Transform()), "ListAuthMethods() for scope %q got response %q, wanted %q", tc.req.GetScopeId(), got, tc.res)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user