2022-05-03 16:02:51 +02:00
|
|
|
package server
|
|
|
|
|
2023-01-02 15:11:32 +01:00
|
|
|
import (
|
2024-07-03 11:33:02 +02:00
|
|
|
"context"
|
2024-08-08 17:01:38 +02:00
|
|
|
"errors"
|
2023-07-14 20:45:40 +02:00
|
|
|
"fmt"
|
2024-08-08 17:01:38 +02:00
|
|
|
"slices"
|
2023-07-14 20:45:40 +02:00
|
|
|
|
2024-08-08 17:01:38 +02:00
|
|
|
nbdns "github.com/netbirdio/netbird/dns"
|
|
|
|
"github.com/netbirdio/netbird/route"
|
2024-03-17 11:13:39 +01:00
|
|
|
"github.com/rs/xid"
|
2023-07-14 20:45:40 +02:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
2023-01-02 15:11:32 +01:00
|
|
|
"github.com/netbirdio/netbird/management/server/activity"
|
2024-03-27 18:48:48 +01:00
|
|
|
nbgroup "github.com/netbirdio/netbird/management/server/group"
|
2023-01-02 15:11:32 +01:00
|
|
|
"github.com/netbirdio/netbird/management/server/status"
|
|
|
|
)
|
2022-05-03 16:02:51 +02:00
|
|
|
|
2023-07-14 20:45:40 +02:00
|
|
|
type GroupLinkError struct {
|
|
|
|
Resource string
|
|
|
|
Name string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *GroupLinkError) Error() string {
|
|
|
|
return fmt.Sprintf("group has been linked to %s: %s", e.Resource, e.Name)
|
|
|
|
}
|
|
|
|
|
2024-09-27 16:10:50 +02:00
|
|
|
// CheckGroupPermissions validates if a user has the necessary permissions to view groups
|
|
|
|
func (am *DefaultAccountManager) CheckGroupPermissions(ctx context.Context, accountID, userID string) error {
|
|
|
|
settings, err := am.Store.GetAccountSettings(ctx, LockingStrengthShare, accountID)
|
2022-05-03 16:02:51 +02:00
|
|
|
if err != nil {
|
2024-09-27 16:10:50 +02:00
|
|
|
return err
|
2022-05-03 16:02:51 +02:00
|
|
|
}
|
|
|
|
|
2024-09-27 16:10:50 +02:00
|
|
|
user, err := am.Store.GetUserByUserID(ctx, LockingStrengthShare, userID)
|
2024-03-27 16:11:45 +01:00
|
|
|
if err != nil {
|
2024-09-27 16:10:50 +02:00
|
|
|
return err
|
2024-03-27 16:11:45 +01:00
|
|
|
}
|
|
|
|
|
2024-11-08 16:39:36 +01:00
|
|
|
if user.AccountID != accountID {
|
|
|
|
return status.NewUserNotPartOfAccountError()
|
|
|
|
}
|
|
|
|
|
|
|
|
if user.IsRegularUser() && settings.RegularUsersViewBlocked {
|
|
|
|
return status.NewAdminPermissionError()
|
2022-05-03 16:02:51 +02:00
|
|
|
}
|
|
|
|
|
2024-09-27 16:10:50 +02:00
|
|
|
return nil
|
2022-05-03 16:02:51 +02:00
|
|
|
}
|
|
|
|
|
2024-09-27 16:10:50 +02:00
|
|
|
// GetGroup returns a specific group by groupID in an account
|
|
|
|
func (am *DefaultAccountManager) GetGroup(ctx context.Context, accountID, groupID, userID string) (*nbgroup.Group, error) {
|
|
|
|
if err := am.CheckGroupPermissions(ctx, accountID, userID); err != nil {
|
2024-03-27 16:11:45 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
2024-11-08 16:39:36 +01:00
|
|
|
return am.Store.GetGroupByID(ctx, LockingStrengthShare, accountID, groupID)
|
2024-09-27 16:10:50 +02:00
|
|
|
}
|
2024-03-27 16:11:45 +01:00
|
|
|
|
2024-09-27 16:10:50 +02:00
|
|
|
// GetAllGroups returns all groups in an account
|
|
|
|
func (am *DefaultAccountManager) GetAllGroups(ctx context.Context, accountID, userID string) ([]*nbgroup.Group, error) {
|
|
|
|
if err := am.CheckGroupPermissions(ctx, accountID, userID); err != nil {
|
|
|
|
return nil, err
|
2024-03-27 16:11:45 +01:00
|
|
|
}
|
2024-11-07 22:32:14 +01:00
|
|
|
return am.Store.GetAccountGroups(ctx, LockingStrengthShare, accountID)
|
2024-03-27 16:11:45 +01:00
|
|
|
}
|
|
|
|
|
2024-01-19 15:41:27 +01:00
|
|
|
// GetGroupByName filters all groups in an account by name and returns the one with the most peers
|
2024-07-03 11:33:02 +02:00
|
|
|
func (am *DefaultAccountManager) GetGroupByName(ctx context.Context, groupName, accountID string) (*nbgroup.Group, error) {
|
2024-11-08 16:39:36 +01:00
|
|
|
return am.Store.GetGroupByName(ctx, LockingStrengthShare, accountID, groupName)
|
2024-01-19 15:41:27 +01:00
|
|
|
}
|
|
|
|
|
2022-05-03 16:02:51 +02:00
|
|
|
// SaveGroup object of the peers
|
2024-07-03 11:33:02 +02:00
|
|
|
func (am *DefaultAccountManager) SaveGroup(ctx context.Context, accountID, userID string, newGroup *nbgroup.Group) error {
|
2024-07-31 14:53:32 +02:00
|
|
|
unlock := am.Store.AcquireWriteLockByUID(ctx, accountID)
|
2022-11-07 17:52:23 +01:00
|
|
|
defer unlock()
|
2024-07-15 16:04:06 +02:00
|
|
|
return am.SaveGroups(ctx, accountID, userID, []*nbgroup.Group{newGroup})
|
|
|
|
}
|
2022-05-03 16:02:51 +02:00
|
|
|
|
2024-07-15 16:04:06 +02:00
|
|
|
// SaveGroups adds new groups to the account.
|
|
|
|
// Note: This function does not acquire the global lock.
|
|
|
|
// It is the caller's responsibility to ensure proper locking is in place before invoking this method.
|
2024-11-08 23:17:01 +01:00
|
|
|
func (am *DefaultAccountManager) SaveGroups(ctx context.Context, accountID, userID string, groups []*nbgroup.Group) error {
|
2024-11-08 16:39:36 +01:00
|
|
|
user, err := am.Store.GetUserByUserID(ctx, LockingStrengthShare, userID)
|
2022-05-03 16:02:51 +02:00
|
|
|
if err != nil {
|
2022-11-11 20:36:45 +01:00
|
|
|
return err
|
2022-05-03 16:02:51 +02:00
|
|
|
}
|
2024-02-13 10:59:31 +01:00
|
|
|
|
2024-11-08 16:39:36 +01:00
|
|
|
if user.AccountID != accountID {
|
|
|
|
return status.NewUserNotPartOfAccountError()
|
|
|
|
}
|
|
|
|
|
2024-11-11 20:38:24 +01:00
|
|
|
if user.IsRegularUser() {
|
|
|
|
return status.NewAdminPermissionError()
|
|
|
|
}
|
|
|
|
|
2024-11-08 23:17:01 +01:00
|
|
|
var eventsToStore []func()
|
|
|
|
var groupsToSave []*nbgroup.Group
|
|
|
|
var updateAccountPeers bool
|
2024-07-15 16:04:06 +02:00
|
|
|
|
2024-11-08 23:17:01 +01:00
|
|
|
err = am.Store.ExecuteInTransaction(ctx, func(transaction Store) error {
|
|
|
|
groupIDs := make([]string, 0, len(groups))
|
|
|
|
for _, newGroup := range groups {
|
|
|
|
if err = validateNewGroup(ctx, transaction, accountID, newGroup); err != nil {
|
|
|
|
return err
|
2024-07-15 16:04:06 +02:00
|
|
|
}
|
2024-03-17 11:13:39 +01:00
|
|
|
|
2024-11-08 23:17:01 +01:00
|
|
|
newGroup.AccountID = accountID
|
|
|
|
groupsToSave = append(groupsToSave, newGroup)
|
|
|
|
groupIDs = append(groupIDs, newGroup.ID)
|
2024-07-15 16:04:06 +02:00
|
|
|
|
2024-11-08 23:17:01 +01:00
|
|
|
events := am.prepareGroupEvents(ctx, transaction, accountID, userID, newGroup)
|
|
|
|
eventsToStore = append(eventsToStore, events...)
|
2024-03-17 11:13:39 +01:00
|
|
|
}
|
|
|
|
|
2024-11-08 23:17:01 +01:00
|
|
|
updateAccountPeers, err = areGroupChangesAffectPeers(ctx, transaction, accountID, groupIDs)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2024-03-17 11:13:39 +01:00
|
|
|
}
|
|
|
|
|
2024-11-08 16:39:36 +01:00
|
|
|
if err = transaction.IncrementNetworkSerial(ctx, LockingStrengthUpdate, accountID); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-11-08 23:17:01 +01:00
|
|
|
return transaction.SaveGroups(ctx, LockingStrengthUpdate, groupsToSave)
|
2024-11-08 16:39:36 +01:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2024-10-23 12:05:02 +02:00
|
|
|
}
|
2023-02-03 10:33:28 +01:00
|
|
|
|
2024-07-15 16:04:06 +02:00
|
|
|
for _, storeEvent := range eventsToStore {
|
|
|
|
storeEvent()
|
|
|
|
}
|
|
|
|
|
2024-11-08 16:39:36 +01:00
|
|
|
if updateAccountPeers {
|
|
|
|
am.updateAccountPeers(ctx, accountID)
|
|
|
|
}
|
|
|
|
|
2024-07-15 16:04:06 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// prepareGroupEvents prepares a list of event functions to be stored.
|
2024-11-08 23:17:01 +01:00
|
|
|
func (am *DefaultAccountManager) prepareGroupEvents(ctx context.Context, transaction Store, accountID, userID string, newGroup *nbgroup.Group) []func() {
|
2024-07-15 16:04:06 +02:00
|
|
|
var eventsToStore []func()
|
|
|
|
|
2023-01-02 15:11:32 +01:00
|
|
|
addedPeers := make([]string, 0)
|
|
|
|
removedPeers := make([]string, 0)
|
2024-07-15 16:04:06 +02:00
|
|
|
|
2024-11-08 23:17:01 +01:00
|
|
|
oldGroup, err := transaction.GetGroupByID(ctx, LockingStrengthShare, accountID, newGroup.ID)
|
2024-11-08 16:39:36 +01:00
|
|
|
if err == nil && oldGroup != nil {
|
2023-01-02 15:11:32 +01:00
|
|
|
addedPeers = difference(newGroup.Peers, oldGroup.Peers)
|
|
|
|
removedPeers = difference(oldGroup.Peers, newGroup.Peers)
|
2023-01-24 10:17:24 +01:00
|
|
|
} else {
|
|
|
|
addedPeers = append(addedPeers, newGroup.Peers...)
|
2024-07-15 16:04:06 +02:00
|
|
|
eventsToStore = append(eventsToStore, func() {
|
|
|
|
am.StoreEvent(ctx, userID, newGroup.ID, accountID, activity.GroupCreated, newGroup.EventMeta())
|
|
|
|
})
|
2023-01-02 15:11:32 +01:00
|
|
|
}
|
|
|
|
|
2024-11-11 20:58:22 +01:00
|
|
|
modifiedPeers := slices.Concat(addedPeers, removedPeers)
|
|
|
|
peers, err := transaction.GetPeersByIDs(ctx, LockingStrengthShare, accountID, modifiedPeers)
|
|
|
|
if err != nil {
|
|
|
|
log.WithContext(ctx).Debugf("failed to get peers for group events: %v", err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-11-08 16:39:36 +01:00
|
|
|
for _, peerID := range addedPeers {
|
2024-11-11 20:58:22 +01:00
|
|
|
peer, ok := peers[peerID]
|
|
|
|
if !ok {
|
|
|
|
log.WithContext(ctx).Debugf("skipped adding peer: %s GroupAddedToPeer activity: peer not found in store", peerID)
|
2023-01-02 15:11:32 +01:00
|
|
|
continue
|
|
|
|
}
|
2024-11-08 16:39:36 +01:00
|
|
|
|
2024-07-15 16:04:06 +02:00
|
|
|
eventsToStore = append(eventsToStore, func() {
|
2024-11-11 20:58:22 +01:00
|
|
|
meta := map[string]any{
|
|
|
|
"group": newGroup.Name, "group_id": newGroup.ID,
|
|
|
|
"peer_ip": peer.IP.String(), "peer_fqdn": peer.FQDN(am.GetDNSDomain()),
|
|
|
|
}
|
2024-11-08 23:17:01 +01:00
|
|
|
am.StoreEvent(ctx, userID, peer.ID, accountID, activity.GroupAddedToPeer, meta)
|
2024-07-15 16:04:06 +02:00
|
|
|
})
|
2023-01-02 15:11:32 +01:00
|
|
|
}
|
|
|
|
|
2024-11-08 16:39:36 +01:00
|
|
|
for _, peerID := range removedPeers {
|
2024-11-11 20:58:22 +01:00
|
|
|
peer, ok := peers[peerID]
|
|
|
|
if !ok {
|
|
|
|
log.WithContext(ctx).Debugf("skipped adding peer: %s GroupRemovedFromPeer activity: peer not found in store", peerID)
|
2023-01-02 15:11:32 +01:00
|
|
|
continue
|
|
|
|
}
|
2024-11-08 16:39:36 +01:00
|
|
|
|
2024-07-15 16:04:06 +02:00
|
|
|
eventsToStore = append(eventsToStore, func() {
|
2024-11-11 20:58:22 +01:00
|
|
|
meta := map[string]any{
|
|
|
|
"group": newGroup.Name, "group_id": newGroup.ID,
|
|
|
|
"peer_ip": peer.IP.String(), "peer_fqdn": peer.FQDN(am.GetDNSDomain()),
|
|
|
|
}
|
2024-11-08 23:17:01 +01:00
|
|
|
am.StoreEvent(ctx, userID, peer.ID, accountID, activity.GroupRemovedFromPeer, meta)
|
2024-07-15 16:04:06 +02:00
|
|
|
})
|
2023-01-02 15:11:32 +01:00
|
|
|
}
|
|
|
|
|
2024-07-15 16:04:06 +02:00
|
|
|
return eventsToStore
|
2022-05-03 16:02:51 +02:00
|
|
|
}
|
|
|
|
|
2023-01-02 15:11:32 +01:00
|
|
|
// difference returns the elements in `a` that aren't in `b`.
|
|
|
|
func difference(a, b []string) []string {
|
|
|
|
mb := make(map[string]struct{}, len(b))
|
|
|
|
for _, x := range b {
|
|
|
|
mb[x] = struct{}{}
|
|
|
|
}
|
|
|
|
var diff []string
|
|
|
|
for _, x := range a {
|
|
|
|
if _, found := mb[x]; !found {
|
|
|
|
diff = append(diff, x)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return diff
|
|
|
|
}
|
|
|
|
|
2024-08-08 17:01:38 +02:00
|
|
|
// DeleteGroup object of the peers.
|
2024-11-08 16:39:36 +01:00
|
|
|
func (am *DefaultAccountManager) DeleteGroup(ctx context.Context, accountID, userID, groupID string) error {
|
|
|
|
user, err := am.Store.GetUserByUserID(ctx, LockingStrengthShare, userID)
|
2022-05-03 16:02:51 +02:00
|
|
|
if err != nil {
|
2022-11-11 20:36:45 +01:00
|
|
|
return err
|
2022-05-03 16:02:51 +02:00
|
|
|
}
|
|
|
|
|
2024-11-08 16:39:36 +01:00
|
|
|
if user.AccountID != accountID {
|
|
|
|
return status.NewUserNotPartOfAccountError()
|
2023-07-14 20:45:40 +02:00
|
|
|
}
|
|
|
|
|
2024-11-11 20:38:24 +01:00
|
|
|
if user.IsRegularUser() {
|
|
|
|
return status.NewAdminPermissionError()
|
|
|
|
}
|
|
|
|
|
2024-11-08 23:17:01 +01:00
|
|
|
var group *nbgroup.Group
|
2024-09-27 16:10:50 +02:00
|
|
|
|
2024-11-08 23:17:01 +01:00
|
|
|
err = am.Store.ExecuteInTransaction(ctx, func(transaction Store) error {
|
|
|
|
group, err = transaction.GetGroupByID(ctx, LockingStrengthShare, accountID, groupID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-09-27 16:10:50 +02:00
|
|
|
|
2024-11-08 23:17:01 +01:00
|
|
|
if group.IsGroupAll() {
|
|
|
|
return status.Errorf(status.InvalidArgument, "deleting group ALL is not allowed")
|
|
|
|
}
|
2023-11-01 11:04:17 +01:00
|
|
|
|
2024-11-08 23:17:01 +01:00
|
|
|
if err = validateDeleteGroup(ctx, transaction, group, userID); err != nil {
|
2024-11-08 16:39:36 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-11-08 23:17:01 +01:00
|
|
|
if err = transaction.IncrementNetworkSerial(ctx, LockingStrengthUpdate, accountID); err != nil {
|
|
|
|
return err
|
2024-11-08 16:39:36 +01:00
|
|
|
}
|
2024-11-08 23:17:01 +01:00
|
|
|
|
|
|
|
return transaction.DeleteGroup(ctx, LockingStrengthUpdate, accountID, groupID)
|
2024-11-08 16:39:36 +01:00
|
|
|
})
|
|
|
|
if err != nil {
|
2024-08-08 17:01:38 +02:00
|
|
|
return err
|
2023-07-14 20:45:40 +02:00
|
|
|
}
|
|
|
|
|
2024-11-08 16:39:36 +01:00
|
|
|
am.StoreEvent(ctx, userID, groupID, accountID, activity.GroupDeleted, group.EventMeta())
|
2023-07-14 20:45:40 +02:00
|
|
|
|
2024-08-08 17:01:38 +02:00
|
|
|
return nil
|
|
|
|
}
|
2023-07-14 20:45:40 +02:00
|
|
|
|
2024-08-08 17:01:38 +02:00
|
|
|
// DeleteGroups deletes groups from an account.
|
2024-11-08 23:17:01 +01:00
|
|
|
// Note: This function does not acquire the global lock.
|
|
|
|
// It is the caller's responsibility to ensure proper locking is in place before invoking this method.
|
|
|
|
//
|
|
|
|
// If an error occurs while deleting a group, the function skips it and continues deleting other groups.
|
|
|
|
// Errors are collected and returned at the end.
|
2024-11-08 16:39:36 +01:00
|
|
|
func (am *DefaultAccountManager) DeleteGroups(ctx context.Context, accountID, userID string, groupIDs []string) error {
|
|
|
|
user, err := am.Store.GetUserByUserID(ctx, LockingStrengthShare, userID)
|
2024-08-08 17:01:38 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2023-07-14 20:45:40 +02:00
|
|
|
}
|
|
|
|
|
2024-11-08 16:39:36 +01:00
|
|
|
if user.AccountID != accountID {
|
|
|
|
return status.NewUserNotPartOfAccountError()
|
|
|
|
}
|
|
|
|
|
2024-11-11 20:38:24 +01:00
|
|
|
if user.IsRegularUser() {
|
|
|
|
return status.NewAdminPermissionError()
|
|
|
|
}
|
|
|
|
|
2024-11-08 23:17:01 +01:00
|
|
|
var allErrors error
|
|
|
|
var groupIDsToDelete []string
|
|
|
|
var deletedGroups []*nbgroup.Group
|
2023-07-14 20:45:40 +02:00
|
|
|
|
2024-11-08 23:17:01 +01:00
|
|
|
err = am.Store.ExecuteInTransaction(ctx, func(transaction Store) error {
|
|
|
|
for _, groupID := range groupIDs {
|
|
|
|
group, err := transaction.GetGroupByID(ctx, LockingStrengthShare, accountID, groupID)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
2023-07-14 20:45:40 +02:00
|
|
|
|
2024-11-08 23:17:01 +01:00
|
|
|
if err := validateDeleteGroup(ctx, transaction, group, userID); err != nil {
|
|
|
|
allErrors = errors.Join(allErrors, fmt.Errorf("failed to delete group %s: %w", groupID, err))
|
|
|
|
continue
|
|
|
|
}
|
2024-03-27 18:48:48 +01:00
|
|
|
|
2024-11-08 23:17:01 +01:00
|
|
|
groupIDsToDelete = append(groupIDsToDelete, groupID)
|
|
|
|
deletedGroups = append(deletedGroups, group)
|
|
|
|
}
|
2022-05-03 16:02:51 +02:00
|
|
|
|
2024-11-08 16:39:36 +01:00
|
|
|
if err = transaction.IncrementNetworkSerial(ctx, LockingStrengthUpdate, accountID); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-05-03 16:02:51 +02:00
|
|
|
|
2024-11-08 23:17:01 +01:00
|
|
|
return transaction.DeleteGroups(ctx, LockingStrengthUpdate, accountID, groupIDsToDelete)
|
2024-11-08 16:39:36 +01:00
|
|
|
})
|
2022-05-03 16:02:51 +02:00
|
|
|
if err != nil {
|
2024-11-08 16:39:36 +01:00
|
|
|
return err
|
2022-05-03 16:02:51 +02:00
|
|
|
}
|
|
|
|
|
2024-11-08 16:39:36 +01:00
|
|
|
for _, group := range deletedGroups {
|
|
|
|
am.StoreEvent(ctx, userID, group.ID, accountID, activity.GroupDeleted, group.EventMeta())
|
2022-05-03 16:02:51 +02:00
|
|
|
}
|
|
|
|
|
2024-11-08 16:39:36 +01:00
|
|
|
return allErrors
|
2022-05-03 16:02:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// GroupAddPeer appends peer to the group
|
2024-07-03 11:33:02 +02:00
|
|
|
func (am *DefaultAccountManager) GroupAddPeer(ctx context.Context, accountID, groupID, peerID string) error {
|
2024-11-08 23:17:01 +01:00
|
|
|
var group *nbgroup.Group
|
|
|
|
var updateAccountPeers bool
|
|
|
|
var err error
|
2022-05-03 16:02:51 +02:00
|
|
|
|
2024-11-08 23:17:01 +01:00
|
|
|
err = am.Store.ExecuteInTransaction(ctx, func(transaction Store) error {
|
|
|
|
group, err = transaction.GetGroupByID(context.Background(), LockingStrengthShare, accountID, groupID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2022-05-03 16:02:51 +02:00
|
|
|
}
|
|
|
|
|
2024-11-08 23:17:01 +01:00
|
|
|
if updated := group.AddPeer(peerID); !updated {
|
|
|
|
return nil
|
|
|
|
}
|
2024-11-08 16:39:36 +01:00
|
|
|
|
2024-11-08 23:17:01 +01:00
|
|
|
updateAccountPeers, err = areGroupChangesAffectPeers(ctx, transaction, accountID, []string{groupID})
|
|
|
|
if err != nil {
|
2024-11-08 16:39:36 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-11-08 23:17:01 +01:00
|
|
|
if err = transaction.IncrementNetworkSerial(ctx, LockingStrengthUpdate, accountID); err != nil {
|
|
|
|
return err
|
2024-11-08 16:39:36 +01:00
|
|
|
}
|
2024-11-08 23:17:01 +01:00
|
|
|
|
|
|
|
return transaction.SaveGroup(ctx, LockingStrengthUpdate, group)
|
2024-11-08 16:39:36 +01:00
|
|
|
})
|
|
|
|
if err != nil {
|
2022-06-04 22:02:22 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-11-08 16:39:36 +01:00
|
|
|
if updateAccountPeers {
|
|
|
|
am.updateAccountPeers(ctx, accountID)
|
2024-10-23 12:05:02 +02:00
|
|
|
}
|
2023-10-04 15:08:50 +02:00
|
|
|
|
|
|
|
return nil
|
2022-05-03 16:02:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// GroupDeletePeer removes peer from the group
|
2024-07-03 11:33:02 +02:00
|
|
|
func (am *DefaultAccountManager) GroupDeletePeer(ctx context.Context, accountID, groupID, peerID string) error {
|
2024-11-08 23:17:01 +01:00
|
|
|
var group *nbgroup.Group
|
|
|
|
var updateAccountPeers bool
|
|
|
|
var err error
|
2022-05-03 16:02:51 +02:00
|
|
|
|
2024-11-08 23:17:01 +01:00
|
|
|
err = am.Store.ExecuteInTransaction(ctx, func(transaction Store) error {
|
|
|
|
group, err = transaction.GetGroupByID(context.Background(), LockingStrengthShare, accountID, groupID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2024-11-08 16:39:36 +01:00
|
|
|
}
|
|
|
|
|
2024-11-08 23:17:01 +01:00
|
|
|
if updated := group.RemovePeer(peerID); !updated {
|
|
|
|
return nil
|
|
|
|
}
|
2024-11-08 16:39:36 +01:00
|
|
|
|
2024-11-08 23:17:01 +01:00
|
|
|
updateAccountPeers, err = areGroupChangesAffectPeers(ctx, transaction, accountID, []string{groupID})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-11-08 16:39:36 +01:00
|
|
|
|
|
|
|
if err = transaction.IncrementNetworkSerial(ctx, LockingStrengthUpdate, accountID); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-11-08 23:17:01 +01:00
|
|
|
return transaction.SaveGroup(ctx, LockingStrengthUpdate, group)
|
2024-11-08 16:39:36 +01:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2022-05-03 16:02:51 +02:00
|
|
|
}
|
|
|
|
|
2024-11-08 16:39:36 +01:00
|
|
|
if updateAccountPeers {
|
|
|
|
am.updateAccountPeers(ctx, accountID)
|
2024-10-23 12:05:02 +02:00
|
|
|
}
|
2023-10-04 15:08:50 +02:00
|
|
|
|
|
|
|
return nil
|
2022-05-03 16:02:51 +02:00
|
|
|
}
|
2024-08-08 17:01:38 +02:00
|
|
|
|
2024-11-08 23:17:01 +01:00
|
|
|
// validateNewGroup validates the new group for existence and required fields.
|
|
|
|
func validateNewGroup(ctx context.Context, transaction Store, accountID string, newGroup *nbgroup.Group) error {
|
|
|
|
if newGroup.ID == "" && newGroup.Issued != nbgroup.GroupIssuedAPI {
|
|
|
|
return status.Errorf(status.InvalidArgument, "%s group without ID set", newGroup.Issued)
|
|
|
|
}
|
|
|
|
|
|
|
|
if newGroup.ID == "" && newGroup.Issued == nbgroup.GroupIssuedAPI {
|
|
|
|
existingGroup, err := transaction.GetGroupByName(ctx, LockingStrengthShare, accountID, newGroup.Name)
|
|
|
|
if err != nil {
|
|
|
|
if s, ok := status.FromError(err); !ok || s.Type() != status.NotFound {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prevent duplicate groups for API-issued groups.
|
|
|
|
// Integration or JWT groups can be duplicated as they are coming from the IdP that we don't have control of.
|
|
|
|
if existingGroup != nil {
|
|
|
|
return status.Errorf(status.AlreadyExists, "group with name %s already exists", newGroup.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
newGroup.ID = xid.New().String()
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, peerID := range newGroup.Peers {
|
|
|
|
_, err := transaction.GetPeerByID(ctx, LockingStrengthShare, accountID, peerID)
|
|
|
|
if err != nil {
|
|
|
|
return status.Errorf(status.InvalidArgument, "peer with ID \"%s\" not found", peerID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func validateDeleteGroup(ctx context.Context, transaction Store, group *nbgroup.Group, userID string) error {
|
2024-08-08 17:01:38 +02:00
|
|
|
// disable a deleting integration group if the initiator is not an admin service user
|
|
|
|
if group.Issued == nbgroup.GroupIssuedIntegration {
|
2024-11-08 23:17:01 +01:00
|
|
|
executingUser, err := transaction.GetUserByUserID(ctx, LockingStrengthShare, userID)
|
2024-11-08 16:39:36 +01:00
|
|
|
if err != nil {
|
2024-08-08 17:01:38 +02:00
|
|
|
return status.Errorf(status.NotFound, "user not found")
|
|
|
|
}
|
|
|
|
if executingUser.Role != UserRoleAdmin || !executingUser.IsServiceUser {
|
|
|
|
return status.Errorf(status.PermissionDenied, "only service users with admin power can delete integration group")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-08 23:17:01 +01:00
|
|
|
if isLinked, linkedRoute := isGroupLinkedToRoute(ctx, transaction, group.AccountID, group.ID); isLinked {
|
2024-08-08 17:01:38 +02:00
|
|
|
return &GroupLinkError{"route", string(linkedRoute.NetID)}
|
|
|
|
}
|
|
|
|
|
2024-11-08 23:17:01 +01:00
|
|
|
if isLinked, linkedDns := isGroupLinkedToDns(ctx, transaction, group.AccountID, group.ID); isLinked {
|
2024-08-08 17:01:38 +02:00
|
|
|
return &GroupLinkError{"name server groups", linkedDns.Name}
|
|
|
|
}
|
|
|
|
|
2024-11-08 23:17:01 +01:00
|
|
|
if isLinked, linkedPolicy := isGroupLinkedToPolicy(ctx, transaction, group.AccountID, group.ID); isLinked {
|
2024-08-08 17:01:38 +02:00
|
|
|
return &GroupLinkError{"policy", linkedPolicy.Name}
|
|
|
|
}
|
|
|
|
|
2024-11-08 23:17:01 +01:00
|
|
|
if isLinked, linkedSetupKey := isGroupLinkedToSetupKey(ctx, transaction, group.AccountID, group.ID); isLinked {
|
2024-08-08 17:01:38 +02:00
|
|
|
return &GroupLinkError{"setup key", linkedSetupKey.Name}
|
|
|
|
}
|
|
|
|
|
2024-11-08 23:17:01 +01:00
|
|
|
if isLinked, linkedUser := isGroupLinkedToUser(ctx, transaction, group.AccountID, group.ID); isLinked {
|
2024-08-08 17:01:38 +02:00
|
|
|
return &GroupLinkError{"user", linkedUser.Id}
|
|
|
|
}
|
|
|
|
|
2024-11-11 20:38:24 +01:00
|
|
|
return checkGroupLinkedToSettings(ctx, transaction, group)
|
|
|
|
}
|
|
|
|
|
|
|
|
// checkGroupLinkedToSettings verifies if a group is linked to any settings in the account.
|
|
|
|
func checkGroupLinkedToSettings(ctx context.Context, transaction Store, group *nbgroup.Group) error {
|
2024-11-08 23:17:01 +01:00
|
|
|
dnsSettings, err := transaction.GetAccountDNSSettings(ctx, LockingStrengthShare, group.AccountID)
|
2024-11-08 16:39:36 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if slices.Contains(dnsSettings.DisabledManagementGroups, group.ID) {
|
2024-08-08 17:01:38 +02:00
|
|
|
return &GroupLinkError{"disabled DNS management groups", group.Name}
|
|
|
|
}
|
|
|
|
|
2024-11-08 23:17:01 +01:00
|
|
|
settings, err := transaction.GetAccountSettings(ctx, LockingStrengthShare, group.AccountID)
|
2024-11-08 16:39:36 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-11-11 20:38:24 +01:00
|
|
|
if settings.Extra != nil && slices.Contains(settings.Extra.IntegratedValidatorGroups, group.ID) {
|
|
|
|
return &GroupLinkError{"integrated validator", group.Name}
|
2024-08-08 17:01:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// isGroupLinkedToRoute checks if a group is linked to any route in the account.
|
2024-11-08 23:17:01 +01:00
|
|
|
func isGroupLinkedToRoute(ctx context.Context, transaction Store, accountID string, groupID string) (bool, *route.Route) {
|
|
|
|
routes, err := transaction.GetAccountRoutes(ctx, LockingStrengthShare, accountID)
|
2024-11-08 16:39:36 +01:00
|
|
|
if err != nil {
|
|
|
|
log.WithContext(ctx).Errorf("error retrieving routes while checking group linkage: %v", err)
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2024-08-08 17:01:38 +02:00
|
|
|
for _, r := range routes {
|
|
|
|
if slices.Contains(r.Groups, groupID) || slices.Contains(r.PeerGroups, groupID) {
|
|
|
|
return true, r
|
|
|
|
}
|
|
|
|
}
|
2024-11-08 16:39:36 +01:00
|
|
|
|
2024-08-08 17:01:38 +02:00
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// isGroupLinkedToPolicy checks if a group is linked to any policy in the account.
|
2024-11-08 23:17:01 +01:00
|
|
|
func isGroupLinkedToPolicy(ctx context.Context, transaction Store, accountID string, groupID string) (bool, *Policy) {
|
|
|
|
policies, err := transaction.GetAccountPolicies(ctx, LockingStrengthShare, accountID)
|
2024-11-08 16:39:36 +01:00
|
|
|
if err != nil {
|
|
|
|
log.WithContext(ctx).Errorf("error retrieving policies while checking group linkage: %v", err)
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2024-08-08 17:01:38 +02:00
|
|
|
for _, policy := range policies {
|
|
|
|
for _, rule := range policy.Rules {
|
|
|
|
if slices.Contains(rule.Sources, groupID) || slices.Contains(rule.Destinations, groupID) {
|
|
|
|
return true, policy
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// isGroupLinkedToDns checks if a group is linked to any nameserver group in the account.
|
2024-11-08 23:17:01 +01:00
|
|
|
func isGroupLinkedToDns(ctx context.Context, transaction Store, accountID string, groupID string) (bool, *nbdns.NameServerGroup) {
|
|
|
|
nameServerGroups, err := transaction.GetAccountNameServerGroups(ctx, LockingStrengthShare, accountID)
|
2024-11-08 16:39:36 +01:00
|
|
|
if err != nil {
|
|
|
|
log.WithContext(ctx).Errorf("error retrieving name server groups while checking group linkage: %v", err)
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2024-08-08 17:01:38 +02:00
|
|
|
for _, dns := range nameServerGroups {
|
|
|
|
for _, g := range dns.Groups {
|
|
|
|
if g == groupID {
|
|
|
|
return true, dns
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-11-08 16:39:36 +01:00
|
|
|
|
2024-08-08 17:01:38 +02:00
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// isGroupLinkedToSetupKey checks if a group is linked to any setup key in the account.
|
2024-11-08 23:17:01 +01:00
|
|
|
func isGroupLinkedToSetupKey(ctx context.Context, transaction Store, accountID string, groupID string) (bool, *SetupKey) {
|
|
|
|
setupKeys, err := transaction.GetAccountSetupKeys(ctx, LockingStrengthShare, accountID)
|
2024-11-08 16:39:36 +01:00
|
|
|
if err != nil {
|
|
|
|
log.WithContext(ctx).Errorf("error retrieving setup keys while checking group linkage: %v", err)
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2024-08-08 17:01:38 +02:00
|
|
|
for _, setupKey := range setupKeys {
|
|
|
|
if slices.Contains(setupKey.AutoGroups, groupID) {
|
|
|
|
return true, setupKey
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// isGroupLinkedToUser checks if a group is linked to any user in the account.
|
2024-11-08 23:17:01 +01:00
|
|
|
func isGroupLinkedToUser(ctx context.Context, transaction Store, accountID string, groupID string) (bool, *User) {
|
|
|
|
users, err := transaction.GetAccountUsers(ctx, LockingStrengthShare, accountID)
|
2024-11-08 16:39:36 +01:00
|
|
|
if err != nil {
|
|
|
|
log.WithContext(ctx).Errorf("error retrieving users while checking group linkage: %v", err)
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2024-08-08 17:01:38 +02:00
|
|
|
for _, user := range users {
|
|
|
|
if slices.Contains(user.AutoGroups, groupID) {
|
|
|
|
return true, user
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false, nil
|
|
|
|
}
|
2024-10-23 12:05:02 +02:00
|
|
|
|
2024-11-08 16:39:36 +01:00
|
|
|
// areGroupChangesAffectPeers checks if any changes to the specified groups will affect peers.
|
2024-11-08 23:17:01 +01:00
|
|
|
func areGroupChangesAffectPeers(ctx context.Context, transaction Store, accountID string, groupIDs []string) (bool, error) {
|
2024-11-08 16:39:36 +01:00
|
|
|
if len(groupIDs) == 0 {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2024-11-08 23:17:01 +01:00
|
|
|
dnsSettings, err := transaction.GetAccountDNSSettings(ctx, LockingStrengthShare, accountID)
|
2024-11-08 16:39:36 +01:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, groupID := range groupIDs {
|
|
|
|
if slices.Contains(dnsSettings.DisabledManagementGroups, groupID) {
|
|
|
|
return true, nil
|
|
|
|
}
|
2024-11-08 23:17:01 +01:00
|
|
|
if linked, _ := isGroupLinkedToDns(ctx, transaction, accountID, groupID); linked {
|
2024-11-08 16:39:36 +01:00
|
|
|
return true, nil
|
|
|
|
}
|
2024-11-08 23:17:01 +01:00
|
|
|
if linked, _ := isGroupLinkedToPolicy(ctx, transaction, accountID, groupID); linked {
|
2024-11-08 16:39:36 +01:00
|
|
|
return true, nil
|
|
|
|
}
|
2024-11-08 23:17:01 +01:00
|
|
|
if linked, _ := isGroupLinkedToRoute(ctx, transaction, accountID, groupID); linked {
|
2024-11-08 16:39:36 +01:00
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2024-11-11 10:38:34 +01:00
|
|
|
func (am *DefaultAccountManager) anyGroupHasPeers(account *Account, groupIDs []string) bool {
|
2024-10-23 12:05:02 +02:00
|
|
|
for _, groupID := range groupIDs {
|
|
|
|
if group, exists := account.Groups[groupID]; exists && group.HasPeers() {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2024-11-11 10:38:34 +01:00
|
|
|
|
|
|
|
// anyGroupHasPeers checks if any of the given groups in the account have peers.
|
|
|
|
func anyGroupHasPeers(ctx context.Context, transaction Store, accountID string, groupIDs []string) (bool, error) {
|
|
|
|
for _, groupID := range groupIDs {
|
|
|
|
group, err := transaction.GetGroupByID(ctx, LockingStrengthShare, accountID, groupID)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if group.HasPeers() {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, nil
|
|
|
|
}
|