Skip to content

Commit

Permalink
get only required groups for auto-group validation
Browse files Browse the repository at this point in the history
Signed-off-by: bcmmbaga <bethuelmbaga12@gmail.com>
  • Loading branch information
bcmmbaga committed Nov 7, 2024
1 parent 1a5f3c6 commit 7190ea1
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 28 deletions.
5 changes: 5 additions & 0 deletions management/server/group/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,8 @@ func (g *Group) Copy() *Group {
func (g *Group) HasPeers() bool {
return len(g.Peers) > 0
}

// IsGroupAll checks if the group is a default "All" group.
func (g *Group) IsGroupAll() bool {
return g.ID == "All"
}
46 changes: 18 additions & 28 deletions management/server/setupkey.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,20 +233,16 @@ func (am *DefaultAccountManager) CreateSetupKey(ctx context.Context, accountID s
return nil, status.NewUserNotPartOfAccountError()
}

var accountGroups []*nbgroup.Group
var groups []*nbgroup.Group
var setupKey *SetupKey
var plainKey string

err = am.Store.ExecuteInTransaction(ctx, func(transaction Store) error {
accountGroups, err = transaction.GetAccountGroups(ctx, LockingStrengthShare, accountID)
groups, err = validateSetupKeyAutoGroups(ctx, transaction, accountID, autoGroups)
if err != nil {
return err
}

if err = validateSetupKeyAutoGroups(accountGroups, autoGroups); err != nil {
return err
}

setupKey, plainKey = GenerateSetupKey(keyName, keyType, expiresIn, autoGroups, usageLimit, ephemeral)
setupKey.AccountID = accountID

Expand All @@ -257,8 +253,8 @@ func (am *DefaultAccountManager) CreateSetupKey(ctx context.Context, accountID s
}

am.StoreEvent(ctx, userID, setupKey.Id, accountID, activity.SetupKeyCreated, setupKey.EventMeta())
groupMap := make(map[string]*nbgroup.Group, len(accountGroups))
for _, g := range accountGroups {
groupMap := make(map[string]*nbgroup.Group, len(groups))
for _, g := range groups {
groupMap[g.ID] = g
}

Expand Down Expand Up @@ -294,20 +290,16 @@ func (am *DefaultAccountManager) SaveSetupKey(ctx context.Context, accountID str
return nil, status.NewUserNotPartOfAccountError()
}

var accountGroups []*nbgroup.Group
var groups []*nbgroup.Group
var oldKey *SetupKey
var newKey *SetupKey

err = am.Store.ExecuteInTransaction(ctx, func(transaction Store) error {
accountGroups, err = transaction.GetAccountGroups(ctx, LockingStrengthShare, accountID)
groups, err = validateSetupKeyAutoGroups(ctx, transaction, accountID, keyToSave.AutoGroups)
if err != nil {
return err
}

if err = validateSetupKeyAutoGroups(accountGroups, keyToSave.AutoGroups); err != nil {
return err
}

oldKey, err = transaction.GetSetupKeyByID(ctx, LockingStrengthShare, accountID, keyToSave.Id)
if err != nil {
return err
Expand All @@ -334,8 +326,8 @@ func (am *DefaultAccountManager) SaveSetupKey(ctx context.Context, accountID str
addedGroups := difference(newKey.AutoGroups, oldKey.AutoGroups)
removedGroups := difference(oldKey.AutoGroups, newKey.AutoGroups)

groupMap := make(map[string]*nbgroup.Group, len(accountGroups))
for _, g := range accountGroups {
groupMap := make(map[string]*nbgroup.Group, len(groups))
for _, g := range groups {
groupMap[g.ID] = g
}

Expand Down Expand Up @@ -439,22 +431,20 @@ func (am *DefaultAccountManager) DeleteSetupKey(ctx context.Context, accountID,
return nil
}

func validateSetupKeyAutoGroups(groups []*nbgroup.Group, autoGroups []string) error {
groupMap := make(map[string]*nbgroup.Group, len(groups))
for _, g := range groups {
groupMap[g.ID] = g
}
func validateSetupKeyAutoGroups(ctx context.Context, transaction Store, accountID string, autoGroupIDs []string) ([]*nbgroup.Group, error) {
autoGroups := make([]*nbgroup.Group, 0, len(autoGroupIDs))

for _, groupID := range autoGroups {
g, exists := groupMap[groupID]
if !exists {
return status.Errorf(status.NotFound, "group %s doesn't exist", groupID)
for _, groupID := range autoGroupIDs {
group, err := transaction.GetGroupByID(ctx, LockingStrengthShare, groupID, accountID)
if err != nil {
return nil, err
}

if g.Name == "All" {
return status.Errorf(status.InvalidArgument, "can't add 'All' group to the setup key")
if group.IsGroupAll() {
return nil, status.Errorf(status.InvalidArgument, "can't add 'All' group to the setup key")
}
autoGroups = append(autoGroups, group)
}

return nil
return autoGroups, nil
}

0 comments on commit 7190ea1

Please sign in to comment.