mirror of
https://github.com/netbirdio/netbird.git
synced 2024-11-26 01:53:42 +01:00
get only required groups for auto-group validation
Signed-off-by: bcmmbaga <bethuelmbaga12@gmail.com>
This commit is contained in:
parent
1a5f3c653c
commit
931521d505
@ -49,3 +49,8 @@ func (g *Group) Copy() *Group {
|
|||||||
func (g *Group) HasPeers() bool {
|
func (g *Group) HasPeers() bool {
|
||||||
return len(g.Peers) > 0
|
return len(g.Peers) > 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// IsGroupAll checks if the group is a default "All" group.
|
||||||
|
func (g *Group) IsGroupAll() bool {
|
||||||
|
return g.Name == "All"
|
||||||
|
}
|
||||||
|
@ -233,20 +233,16 @@ func (am *DefaultAccountManager) CreateSetupKey(ctx context.Context, accountID s
|
|||||||
return nil, status.NewUserNotPartOfAccountError()
|
return nil, status.NewUserNotPartOfAccountError()
|
||||||
}
|
}
|
||||||
|
|
||||||
var accountGroups []*nbgroup.Group
|
var groups []*nbgroup.Group
|
||||||
var setupKey *SetupKey
|
var setupKey *SetupKey
|
||||||
var plainKey string
|
var plainKey string
|
||||||
|
|
||||||
err = am.Store.ExecuteInTransaction(ctx, func(transaction Store) error {
|
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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = validateSetupKeyAutoGroups(accountGroups, autoGroups); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
setupKey, plainKey = GenerateSetupKey(keyName, keyType, expiresIn, autoGroups, usageLimit, ephemeral)
|
setupKey, plainKey = GenerateSetupKey(keyName, keyType, expiresIn, autoGroups, usageLimit, ephemeral)
|
||||||
setupKey.AccountID = accountID
|
setupKey.AccountID = accountID
|
||||||
|
|
||||||
@ -257,8 +253,8 @@ func (am *DefaultAccountManager) CreateSetupKey(ctx context.Context, accountID s
|
|||||||
}
|
}
|
||||||
|
|
||||||
am.StoreEvent(ctx, userID, setupKey.Id, accountID, activity.SetupKeyCreated, setupKey.EventMeta())
|
am.StoreEvent(ctx, userID, setupKey.Id, accountID, activity.SetupKeyCreated, setupKey.EventMeta())
|
||||||
groupMap := make(map[string]*nbgroup.Group, len(accountGroups))
|
groupMap := make(map[string]*nbgroup.Group, len(groups))
|
||||||
for _, g := range accountGroups {
|
for _, g := range groups {
|
||||||
groupMap[g.ID] = g
|
groupMap[g.ID] = g
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -294,20 +290,16 @@ func (am *DefaultAccountManager) SaveSetupKey(ctx context.Context, accountID str
|
|||||||
return nil, status.NewUserNotPartOfAccountError()
|
return nil, status.NewUserNotPartOfAccountError()
|
||||||
}
|
}
|
||||||
|
|
||||||
var accountGroups []*nbgroup.Group
|
var groups []*nbgroup.Group
|
||||||
var oldKey *SetupKey
|
var oldKey *SetupKey
|
||||||
var newKey *SetupKey
|
var newKey *SetupKey
|
||||||
|
|
||||||
err = am.Store.ExecuteInTransaction(ctx, func(transaction Store) error {
|
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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = validateSetupKeyAutoGroups(accountGroups, keyToSave.AutoGroups); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
oldKey, err = transaction.GetSetupKeyByID(ctx, LockingStrengthShare, accountID, keyToSave.Id)
|
oldKey, err = transaction.GetSetupKeyByID(ctx, LockingStrengthShare, accountID, keyToSave.Id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@ -334,8 +326,8 @@ func (am *DefaultAccountManager) SaveSetupKey(ctx context.Context, accountID str
|
|||||||
addedGroups := difference(newKey.AutoGroups, oldKey.AutoGroups)
|
addedGroups := difference(newKey.AutoGroups, oldKey.AutoGroups)
|
||||||
removedGroups := difference(oldKey.AutoGroups, newKey.AutoGroups)
|
removedGroups := difference(oldKey.AutoGroups, newKey.AutoGroups)
|
||||||
|
|
||||||
groupMap := make(map[string]*nbgroup.Group, len(accountGroups))
|
groupMap := make(map[string]*nbgroup.Group, len(groups))
|
||||||
for _, g := range accountGroups {
|
for _, g := range groups {
|
||||||
groupMap[g.ID] = g
|
groupMap[g.ID] = g
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -439,22 +431,20 @@ func (am *DefaultAccountManager) DeleteSetupKey(ctx context.Context, accountID,
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func validateSetupKeyAutoGroups(groups []*nbgroup.Group, autoGroups []string) error {
|
func validateSetupKeyAutoGroups(ctx context.Context, transaction Store, accountID string, autoGroupIDs []string) ([]*nbgroup.Group, error) {
|
||||||
groupMap := make(map[string]*nbgroup.Group, len(groups))
|
autoGroups := make([]*nbgroup.Group, 0, len(autoGroupIDs))
|
||||||
for _, g := range groups {
|
|
||||||
groupMap[g.ID] = g
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, groupID := range autoGroups {
|
for _, groupID := range autoGroupIDs {
|
||||||
g, exists := groupMap[groupID]
|
group, err := transaction.GetGroupByID(ctx, LockingStrengthShare, groupID, accountID)
|
||||||
if !exists {
|
if err != nil {
|
||||||
return status.Errorf(status.NotFound, "group %s doesn't exist", groupID)
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if g.Name == "All" {
|
if group.IsGroupAll() {
|
||||||
return status.Errorf(status.InvalidArgument, "can't add 'All' group to the setup key")
|
return nil, status.Errorf(status.InvalidArgument, "can't add 'All' group to the setup key")
|
||||||
}
|
}
|
||||||
|
autoGroups = append(autoGroups, group)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return autoGroups, nil
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user