Add API/CLI for managed groups (#1265)

This commit is contained in:
Jeff Mitchell authored and GitHub committed 2021-06-02 14:05:10 -04:00
1 parent 179a49657e
commit 306d4fb4d3
42 files changed
+1481 -40

No files matched your search

@@ -145,7 +145,7 @@ func (s Service) GetManagedGroup(ctx context.Context, req *pbs.GetManagedGroupRe
if authResults.Error != nil {
return nil, authResults.Error
}
mg, err := s.getFromRepo(ctx, req.GetId())
mg, memberIds, err := s.getFromRepo(ctx, req.GetId())
if err != nil {
return nil, err
}
@@ -163,6 +163,9 @@ func (s Service) GetManagedGroup(ctx context.Context, req *pbs.GetManagedGroupRe
if outputFields.Has(globals.AuthorizedActionsField) {
outputOpts = append(outputOpts, handlers.WithAuthorizedActions(authResults.FetchActionSetForId(ctx, mg.GetPublicId(), IdActions[auth.SubtypeFromId(mg.GetPublicId())]).Strings()))
}
if outputFields.Has(globals.MemberIdsField) {
outputOpts = append(outputOpts, handlers.WithMemberIds(memberIds))
}
item, err := toProto(ctx, mg, outputOpts...)
if err != nil {
@@ -266,26 +269,37 @@ func (s Service) DeleteManagedGroup(ctx context.Context, req *pbs.DeleteManagedG
return nil, nil
}
func (s Service) getFromRepo(ctx context.Context, id string) (auth.ManagedGroup, error) {
func (s Service) getFromRepo(ctx context.Context, id string) (auth.ManagedGroup, []string, error) {
var out auth.ManagedGroup
var memberIds []string
switch auth.SubtypeFromId(id) {
case auth.OidcSubtype:
repo, err := s.oidcRepoFn()
if err != nil {
return nil, err
return nil, nil, err
}
mg, err := repo.LookupManagedGroup(ctx, id)
if err != nil {
if errors.IsNotFoundError(err) {
return nil, handlers.NotFoundErrorf("ManagedGroup %q doesn't exist.", id)
return nil, nil, handlers.NotFoundErrorf("ManagedGroup %q doesn't exist.", id)
}
return nil, nil, err
}
ids, err := repo.ListManagedGroupMembershipsByGroup(ctx, mg.GetPublicId())
if err != nil {
return nil, nil, err
}
if len(ids) > 0 {
memberIds = make([]string, len(ids))
for i, v := range ids {
memberIds[i] = v.MemberId
}
return nil, err
}
out = mg
default:
return nil, handlers.NotFoundErrorf("Unrecognized id.")
return nil, nil, handlers.NotFoundErrorf("Unrecognized id.")
}
return out, nil
return out, memberIds, nil
}
func (s Service) createOidcInRepo(ctx context.Context, am auth.AuthMethod, item *pb.ManagedGroup) (*oidc.ManagedGroup, error) {
@@ -529,6 +543,9 @@ func toProto(ctx context.Context, in auth.ManagedGroup, opt ...handlers.Option)
if outputFields.Has(globals.AuthorizedActionsField) {
out.AuthorizedActions = opts.WithAuthorizedActions
}
if outputFields.Has(globals.MemberIdsField) {
out.MemberIds = opts.WithMemberIds
}
switch i := in.(type) {
case *oidc.ManagedGroup:
if outputFields.Has(globals.TypeField) {
@@ -106,18 +106,33 @@ func TestGet(t *testing.T) {
oidc.WithSigningAlgs(oidc.RS256),
oidc.WithApiUrl(oidc.TestConvertToUrls(t, "https://www.alice.com/callback")[0]),
)
oidcA := oidc.TestAccount(t, conn, oidcAm, "test-subject")
omg := oidc.TestManagedGroup(t, conn, oidcAm, testFakeManagedGroupFilter)
// Set up managed group before getting. First get the current
// managed group to make sure we have the right version, then ensure
// the account is a member so we can test that return value.
oidcRepo, err := oidcRepoFn()
require.NoError(t, err)
currMg, err := oidcRepo.LookupManagedGroup(ctx, omg.GetPublicId())
require.NoError(t, err)
_, _, err = oidcRepo.SetManagedGroupMemberships(ctx, oidcAm, oidcA, []*oidc.ManagedGroup{currMg})
require.NoError(t, err)
// Fetch the group once more to get the updated time
currMg, err = oidcRepo.LookupManagedGroup(ctx, omg.GetPublicId())
require.NoError(t, err)
oidcWireManagedGroup := pb.ManagedGroup{
Id: omg.GetPublicId(),
AuthMethodId: omg.GetAuthMethodId(),
CreatedTime: omg.GetCreateTime().GetTimestamp(),
UpdatedTime: omg.GetUpdateTime().GetTimestamp(),
UpdatedTime: currMg.GetUpdateTime().GetTimestamp(),
Scope: &scopepb.ScopeInfo{Id: org.GetPublicId(), Type: scope.Org.String(), ParentScopeId: scope.Global.String()},
Version: 1,
Version: currMg.Version,
Type: "oidc",
Attributes: &structpb.Struct{Fields: map[string]*structpb.Value{"filter": structpb.NewStringValue(omg.GetFilter())}},
AuthorizedActions: oidcAuthorizedActions,
MemberIds: []string{oidcA.GetPublicId()},
}
cases := []struct {
@@ -72,4 +72,16 @@ func Test_GetOpts(t *testing.T) {
opts = GetOpts(WithManagedGroupIds(out))
require.Equal(out, opts.WithManagedGroupIds)
})
t.Run("WithMemberIds", func(t *testing.T) {
assert := assert.New(t)
require := require.New(t)
opts := GetOpts()
assert.Nil(opts.WithMemberIds)
out := []string{"foobar"}
opts = GetOpts(WithMemberIds(out))
require.Equal(out, opts.WithMemberIds)
})
}
@@ -31,6 +31,7 @@ type options struct {
WithAuthorizedActions []string
WithAuthorizedCollectionActions map[string]*structpb.ListValue
WithManagedGroupIds []string
WithMemberIds []string
}
func getDefaultOptions() options {
@@ -100,3 +101,11 @@ func WithManagedGroupIds(ids []string) Option {
o.WithManagedGroupIds = ids
}
}
// WithMemberIds provides an option when creating responses to include the given
// member IDs if allowed
func WithMemberIds(ids []string) Option {
return func(o *options) {
o.WithMemberIds = ids
}
}