2024-03-27 18:48:48 +01:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2024-07-03 11:33:02 +02:00
|
|
|
"context"
|
2024-03-27 18:48:48 +01:00
|
|
|
"errors"
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
log "github.com/sirupsen/logrus"
|
2024-03-27 18:48:48 +01:00
|
|
|
|
|
|
|
"github.com/netbirdio/netbird/management/server/account"
|
2024-12-11 12:58:45 +01:00
|
|
|
"github.com/netbirdio/netbird/management/server/store"
|
|
|
|
"github.com/netbirdio/netbird/management/server/types"
|
2024-03-27 18:48:48 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// UpdateIntegratedValidatorGroups updates the integrated validator groups for a specified account.
|
|
|
|
// It retrieves the account associated with the provided userID, then updates the integrated validator groups
|
|
|
|
// with the provided list of group ids. The updated account is then saved.
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// - accountID: The ID of the account for which integrated validator groups are to be updated.
|
|
|
|
// - userID: The ID of the user whose account is being updated.
|
|
|
|
// - groups: A slice of strings representing the ids of integrated validator groups to be updated.
|
|
|
|
//
|
|
|
|
// Returns:
|
|
|
|
// - error: An error if any occurred during the process, otherwise returns nil
|
2024-07-03 11:33:02 +02:00
|
|
|
func (am *DefaultAccountManager) UpdateIntegratedValidatorGroups(ctx context.Context, accountID string, userID string, groups []string) error {
|
|
|
|
ok, err := am.GroupValidation(ctx, accountID, groups)
|
2024-03-27 18:48:48 +01:00
|
|
|
if err != nil {
|
2024-07-03 11:33:02 +02:00
|
|
|
log.WithContext(ctx).Debugf("error validating groups: %s", err.Error())
|
2024-03-27 18:48:48 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !ok {
|
2024-07-03 11:33:02 +02:00
|
|
|
log.WithContext(ctx).Debugf("invalid groups")
|
2024-03-27 18:48:48 +01:00
|
|
|
return errors.New("invalid groups")
|
|
|
|
}
|
|
|
|
|
2024-07-31 14:53:32 +02:00
|
|
|
unlock := am.Store.AcquireWriteLockByUID(ctx, accountID)
|
2024-03-27 18:48:48 +01:00
|
|
|
defer unlock()
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
a, err := am.Store.GetAccountByUser(ctx, userID)
|
2024-03-27 18:48:48 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var extra *account.ExtraSettings
|
|
|
|
|
|
|
|
if a.Settings.Extra != nil {
|
|
|
|
extra = a.Settings.Extra
|
|
|
|
} else {
|
|
|
|
extra = &account.ExtraSettings{}
|
|
|
|
a.Settings.Extra = extra
|
|
|
|
}
|
|
|
|
extra.IntegratedValidatorGroups = groups
|
2024-07-03 11:33:02 +02:00
|
|
|
return am.Store.SaveAccount(ctx, a)
|
2024-03-27 18:48:48 +01:00
|
|
|
}
|
|
|
|
|
2024-11-15 18:09:32 +01:00
|
|
|
func (am *DefaultAccountManager) GroupValidation(ctx context.Context, accountID string, groupIDs []string) (bool, error) {
|
|
|
|
if len(groupIDs) == 0 {
|
2024-03-27 18:48:48 +01:00
|
|
|
return true, nil
|
|
|
|
}
|
2024-11-15 18:09:32 +01:00
|
|
|
|
2024-12-11 12:58:45 +01:00
|
|
|
err := am.Store.ExecuteInTransaction(ctx, func(transaction store.Store) error {
|
2024-11-15 18:09:32 +01:00
|
|
|
for _, groupID := range groupIDs {
|
2024-12-11 12:58:45 +01:00
|
|
|
_, err := transaction.GetGroupByID(context.Background(), store.LockingStrengthShare, accountID, groupID)
|
2024-11-15 18:09:32 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2024-03-27 18:48:48 +01:00
|
|
|
}
|
|
|
|
}
|
2024-11-15 18:09:32 +01:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
2024-03-27 18:48:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2024-12-11 12:58:45 +01:00
|
|
|
func (am *DefaultAccountManager) GetValidatedPeers(account *types.Account) (map[string]struct{}, error) {
|
2024-03-27 18:48:48 +01:00
|
|
|
return am.integratedPeerValidator.GetValidatedPeers(account.Id, account.Groups, account.Peers, account.Settings.Extra)
|
|
|
|
}
|