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)
|
|
|
|
}
|
|
|
|
|
2022-05-03 16:02:51 +02:00
|
|
|
// GetGroup object of the peers
|
2024-07-03 11:33:02 +02:00
|
|
|
func (am *DefaultAccountManager) GetGroup(ctx context.Context, accountID, groupID, userID string) (*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()
|
2022-05-03 16:02:51 +02:00
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
account, err := am.Store.GetAccount(ctx, accountID)
|
2022-05-03 16:02:51 +02:00
|
|
|
if err != nil {
|
2022-11-11 20:36:45 +01:00
|
|
|
return nil, err
|
2022-05-03 16:02:51 +02:00
|
|
|
}
|
|
|
|
|
2024-03-27 16:11:45 +01:00
|
|
|
user, err := account.FindUser(userID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !user.HasAdminPower() && !user.IsServiceUser && account.Settings.RegularUsersViewBlocked {
|
|
|
|
return nil, status.Errorf(status.PermissionDenied, "groups are blocked for users")
|
|
|
|
}
|
|
|
|
|
2022-05-03 16:02:51 +02:00
|
|
|
group, ok := account.Groups[groupID]
|
|
|
|
if ok {
|
|
|
|
return group, nil
|
|
|
|
}
|
|
|
|
|
2022-11-11 20:36:45 +01:00
|
|
|
return nil, status.Errorf(status.NotFound, "group with ID %s not found", groupID)
|
2022-05-03 16:02:51 +02:00
|
|
|
}
|
|
|
|
|
2024-03-27 16:11:45 +01:00
|
|
|
// GetAllGroups returns all groups in an account
|
2024-07-03 11:33:02 +02:00
|
|
|
func (am *DefaultAccountManager) GetAllGroups(ctx context.Context, accountID string, userID string) ([]*nbgroup.Group, error) {
|
2024-07-31 14:53:32 +02:00
|
|
|
unlock := am.Store.AcquireWriteLockByUID(ctx, accountID)
|
2024-03-27 16:11:45 +01:00
|
|
|
defer unlock()
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
account, err := am.Store.GetAccount(ctx, accountID)
|
2024-03-27 16:11:45 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
user, err := account.FindUser(userID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !user.HasAdminPower() && !user.IsServiceUser && account.Settings.RegularUsersViewBlocked {
|
|
|
|
return nil, status.Errorf(status.PermissionDenied, "groups are blocked for users")
|
|
|
|
}
|
|
|
|
|
2024-03-27 18:48:48 +01:00
|
|
|
groups := make([]*nbgroup.Group, 0, len(account.Groups))
|
2024-03-27 16:11:45 +01:00
|
|
|
for _, item := range account.Groups {
|
|
|
|
groups = append(groups, item)
|
|
|
|
}
|
|
|
|
|
|
|
|
return groups, nil
|
|
|
|
}
|
|
|
|
|
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-07-31 14:53:32 +02:00
|
|
|
unlock := am.Store.AcquireWriteLockByUID(ctx, accountID)
|
2024-01-19 15:41:27 +01:00
|
|
|
defer unlock()
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
account, err := am.Store.GetAccount(ctx, accountID)
|
2024-01-19 15:41:27 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-03-27 18:48:48 +01:00
|
|
|
matchingGroups := make([]*nbgroup.Group, 0)
|
2024-01-19 15:41:27 +01:00
|
|
|
for _, group := range account.Groups {
|
|
|
|
if group.Name == groupName {
|
|
|
|
matchingGroups = append(matchingGroups, group)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(matchingGroups) == 0 {
|
|
|
|
return nil, status.Errorf(status.NotFound, "group with name %s not found", groupName)
|
|
|
|
}
|
|
|
|
|
|
|
|
maxPeers := -1
|
2024-03-27 18:48:48 +01:00
|
|
|
var groupWithMostPeers *nbgroup.Group
|
2024-01-19 15:41:27 +01:00
|
|
|
for i, group := range matchingGroups {
|
|
|
|
if len(group.Peers) > maxPeers {
|
|
|
|
maxPeers = len(group.Peers)
|
|
|
|
groupWithMostPeers = matchingGroups[i]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return groupWithMostPeers, nil
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
func (am *DefaultAccountManager) SaveGroups(ctx context.Context, accountID, userID string, newGroups []*nbgroup.Group) error {
|
2024-07-03 11:33:02 +02:00
|
|
|
account, err := am.Store.GetAccount(ctx, accountID)
|
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-07-15 16:04:06 +02:00
|
|
|
var eventsToStore []func()
|
2024-03-17 11:13:39 +01:00
|
|
|
|
2024-07-15 16:04:06 +02:00
|
|
|
for _, newGroup := range newGroups {
|
|
|
|
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 := account.FindGroupByName(newGroup.Name)
|
|
|
|
if err != nil {
|
|
|
|
s, ok := status.FromError(err)
|
|
|
|
if !ok || s.ErrorType != status.NotFound {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2024-03-17 11:13:39 +01:00
|
|
|
|
2024-07-15 16:04:06 +02:00
|
|
|
// Avoid duplicate groups only for the 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)
|
2024-03-17 11:13:39 +01:00
|
|
|
}
|
2024-07-15 16:04:06 +02:00
|
|
|
|
|
|
|
newGroup.ID = xid.New().String()
|
2024-03-17 11:13:39 +01:00
|
|
|
}
|
|
|
|
|
2024-07-15 16:04:06 +02:00
|
|
|
for _, peerID := range newGroup.Peers {
|
|
|
|
if account.Peers[peerID] == nil {
|
|
|
|
return status.Errorf(status.InvalidArgument, "peer with ID \"%s\" not found", peerID)
|
|
|
|
}
|
2024-03-17 11:13:39 +01:00
|
|
|
}
|
|
|
|
|
2024-07-15 16:04:06 +02:00
|
|
|
oldGroup := account.Groups[newGroup.ID]
|
|
|
|
account.Groups[newGroup.ID] = newGroup
|
2024-03-17 11:13:39 +01:00
|
|
|
|
2024-07-15 16:04:06 +02:00
|
|
|
events := am.prepareGroupEvents(ctx, userID, accountID, newGroup, oldGroup, account)
|
|
|
|
eventsToStore = append(eventsToStore, events...)
|
2024-02-13 10:59:31 +01:00
|
|
|
}
|
|
|
|
|
2022-06-04 22:02:22 +02:00
|
|
|
account.Network.IncSerial()
|
2024-07-31 18:48:12 +02:00
|
|
|
if err = am.Store.SaveAccount(ctx, account); err != nil {
|
2022-06-04 22:02:22 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
am.updateAccountPeers(ctx, account)
|
2023-02-03 10:33:28 +01:00
|
|
|
|
2024-07-15 16:04:06 +02:00
|
|
|
for _, storeEvent := range eventsToStore {
|
|
|
|
storeEvent()
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// prepareGroupEvents prepares a list of event functions to be stored.
|
|
|
|
func (am *DefaultAccountManager) prepareGroupEvents(ctx context.Context, userID string, accountID string, newGroup, oldGroup *nbgroup.Group, account *Account) []func() {
|
|
|
|
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
|
|
|
|
|
|
|
if 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
|
|
|
}
|
|
|
|
|
|
|
|
for _, p := range addedPeers {
|
|
|
|
peer := account.Peers[p]
|
|
|
|
if peer == nil {
|
2024-07-03 11:33:02 +02:00
|
|
|
log.WithContext(ctx).Errorf("peer %s not found under account %s while saving group", p, accountID)
|
2023-01-02 15:11:32 +01:00
|
|
|
continue
|
|
|
|
}
|
2024-07-15 16:04:06 +02:00
|
|
|
peerCopy := peer // copy to avoid closure issues
|
|
|
|
eventsToStore = append(eventsToStore, func() {
|
|
|
|
am.StoreEvent(ctx, userID, peerCopy.ID, accountID, activity.GroupAddedToPeer,
|
|
|
|
map[string]any{
|
|
|
|
"group": newGroup.Name, "group_id": newGroup.ID, "peer_ip": peerCopy.IP.String(),
|
|
|
|
"peer_fqdn": peerCopy.FQDN(am.GetDNSDomain()),
|
|
|
|
})
|
|
|
|
})
|
2023-01-02 15:11:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, p := range removedPeers {
|
|
|
|
peer := account.Peers[p]
|
|
|
|
if peer == nil {
|
2024-07-03 11:33:02 +02:00
|
|
|
log.WithContext(ctx).Errorf("peer %s not found under account %s while saving group", p, accountID)
|
2023-01-02 15:11:32 +01:00
|
|
|
continue
|
|
|
|
}
|
2024-07-15 16:04:06 +02:00
|
|
|
peerCopy := peer // copy to avoid closure issues
|
|
|
|
eventsToStore = append(eventsToStore, func() {
|
|
|
|
am.StoreEvent(ctx, userID, peerCopy.ID, accountID, activity.GroupRemovedFromPeer,
|
|
|
|
map[string]any{
|
|
|
|
"group": newGroup.Name, "group_id": newGroup.ID, "peer_ip": peerCopy.IP.String(),
|
|
|
|
"peer_fqdn": peerCopy.FQDN(am.GetDNSDomain()),
|
|
|
|
})
|
|
|
|
})
|
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-07-03 11:33:02 +02:00
|
|
|
func (am *DefaultAccountManager) DeleteGroup(ctx context.Context, accountId, userId, groupID string) 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()
|
2022-05-03 16:02:51 +02:00
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
account, err := am.Store.GetAccount(ctx, accountId)
|
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-08-08 17:01:38 +02:00
|
|
|
group, ok := account.Groups[groupID]
|
2023-07-14 20:45:40 +02:00
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-08-08 17:01:38 +02:00
|
|
|
if err = validateDeleteGroup(account, group, userId); err != nil {
|
|
|
|
return err
|
2023-11-01 11:04:17 +01:00
|
|
|
}
|
2024-08-08 17:01:38 +02:00
|
|
|
delete(account.Groups, groupID)
|
2023-11-01 11:04:17 +01:00
|
|
|
|
2024-08-08 17:01:38 +02:00
|
|
|
account.Network.IncSerial()
|
|
|
|
if err = am.Store.SaveAccount(ctx, account); err != nil {
|
|
|
|
return err
|
2023-07-14 20:45:40 +02:00
|
|
|
}
|
|
|
|
|
2024-08-08 17:01:38 +02: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
|
|
|
am.updateAccountPeers(ctx, account)
|
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.
|
|
|
|
// 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.
|
|
|
|
func (am *DefaultAccountManager) DeleteGroups(ctx context.Context, accountId, userId string, groupIDs []string) error {
|
|
|
|
account, err := am.Store.GetAccount(ctx, accountId)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2023-07-14 20:45:40 +02:00
|
|
|
}
|
|
|
|
|
2024-08-08 17:01:38 +02:00
|
|
|
var allErrors error
|
2023-07-14 20:45:40 +02:00
|
|
|
|
2024-08-08 17:01:38 +02:00
|
|
|
deletedGroups := make([]*nbgroup.Group, 0, len(groupIDs))
|
|
|
|
for _, groupID := range groupIDs {
|
|
|
|
group, ok := account.Groups[groupID]
|
|
|
|
if !ok {
|
|
|
|
continue
|
2023-07-14 20:45:40 +02:00
|
|
|
}
|
|
|
|
|
2024-08-08 17:01:38 +02:00
|
|
|
if err := validateDeleteGroup(account, 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-08-08 17:01:38 +02:00
|
|
|
delete(account.Groups, groupID)
|
|
|
|
deletedGroups = append(deletedGroups, group)
|
|
|
|
}
|
2022-05-03 16:02:51 +02:00
|
|
|
|
2022-06-04 22:02:22 +02:00
|
|
|
account.Network.IncSerial()
|
2024-07-03 11:33:02 +02:00
|
|
|
if err = am.Store.SaveAccount(ctx, account); err != nil {
|
2022-06-04 22:02:22 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-08-08 17:01:38 +02:00
|
|
|
for _, g := range deletedGroups {
|
|
|
|
am.StoreEvent(ctx, userId, g.ID, accountId, activity.GroupDeleted, g.EventMeta())
|
|
|
|
}
|
2023-07-14 20:45:40 +02:00
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
am.updateAccountPeers(ctx, account)
|
2023-10-04 15:08:50 +02:00
|
|
|
|
2024-08-08 17:01:38 +02:00
|
|
|
return allErrors
|
2022-05-03 16:02:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// ListGroups objects of the peers
|
2024-07-03 11:33:02 +02:00
|
|
|
func (am *DefaultAccountManager) ListGroups(ctx context.Context, accountID string) ([]*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()
|
2022-05-03 16:02:51 +02:00
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
account, err := am.Store.GetAccount(ctx, accountID)
|
2022-05-03 16:02:51 +02:00
|
|
|
if err != nil {
|
2022-11-11 20:36:45 +01:00
|
|
|
return nil, err
|
2022-05-03 16:02:51 +02:00
|
|
|
}
|
|
|
|
|
2024-03-27 18:48:48 +01:00
|
|
|
groups := make([]*nbgroup.Group, 0, len(account.Groups))
|
2022-05-03 16:02:51 +02:00
|
|
|
for _, item := range account.Groups {
|
|
|
|
groups = append(groups, item)
|
|
|
|
}
|
|
|
|
|
|
|
|
return groups, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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-07-31 14:53:32 +02:00
|
|
|
unlock := am.Store.AcquireWriteLockByUID(ctx, accountID)
|
2022-11-07 17:52:23 +01:00
|
|
|
defer unlock()
|
2022-05-03 16:02:51 +02:00
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
account, err := am.Store.GetAccount(ctx, accountID)
|
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
|
|
|
}
|
|
|
|
|
|
|
|
group, ok := account.Groups[groupID]
|
|
|
|
if !ok {
|
2022-11-11 20:36:45 +01:00
|
|
|
return status.Errorf(status.NotFound, "group with ID %s not found", groupID)
|
2022-05-03 16:02:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
add := true
|
|
|
|
for _, itemID := range group.Peers {
|
2023-02-03 10:33:28 +01:00
|
|
|
if itemID == peerID {
|
2022-05-03 16:02:51 +02:00
|
|
|
add = false
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if add {
|
2023-02-03 10:33:28 +01:00
|
|
|
group.Peers = append(group.Peers, peerID)
|
2022-05-03 16:02:51 +02:00
|
|
|
}
|
|
|
|
|
2022-06-04 22:02:22 +02:00
|
|
|
account.Network.IncSerial()
|
2024-07-03 11:33:02 +02:00
|
|
|
if err = am.Store.SaveAccount(ctx, account); err != nil {
|
2022-06-04 22:02:22 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
am.updateAccountPeers(ctx, account)
|
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-07-31 14:53:32 +02:00
|
|
|
unlock := am.Store.AcquireWriteLockByUID(ctx, accountID)
|
2022-11-07 17:52:23 +01:00
|
|
|
defer unlock()
|
2022-05-03 16:02:51 +02:00
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
account, err := am.Store.GetAccount(ctx, accountID)
|
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
|
|
|
}
|
|
|
|
|
|
|
|
group, ok := account.Groups[groupID]
|
|
|
|
if !ok {
|
2022-11-11 20:36:45 +01:00
|
|
|
return status.Errorf(status.NotFound, "group with ID %s not found", groupID)
|
2022-05-03 16:02:51 +02:00
|
|
|
}
|
|
|
|
|
2022-06-04 22:02:22 +02:00
|
|
|
account.Network.IncSerial()
|
2022-05-03 16:02:51 +02:00
|
|
|
for i, itemID := range group.Peers {
|
2023-09-28 14:32:36 +02:00
|
|
|
if itemID == peerID {
|
2022-05-03 16:02:51 +02:00
|
|
|
group.Peers = append(group.Peers[:i], group.Peers[i+1:]...)
|
2024-07-03 11:33:02 +02:00
|
|
|
if err := am.Store.SaveAccount(ctx, account); err != nil {
|
2022-11-11 20:36:45 +01:00
|
|
|
return err
|
2022-06-04 22:02:22 +02:00
|
|
|
}
|
2022-05-03 16:02:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
am.updateAccountPeers(ctx, account)
|
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
|
|
|
|
|
|
|
func validateDeleteGroup(account *Account, group *nbgroup.Group, userID string) error {
|
|
|
|
// disable a deleting integration group if the initiator is not an admin service user
|
|
|
|
if group.Issued == nbgroup.GroupIssuedIntegration {
|
|
|
|
executingUser := account.Users[userID]
|
|
|
|
if executingUser == nil {
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if isLinked, linkedRoute := isGroupLinkedToRoute(account.Routes, group.ID); isLinked {
|
|
|
|
return &GroupLinkError{"route", string(linkedRoute.NetID)}
|
|
|
|
}
|
|
|
|
|
|
|
|
if isLinked, linkedDns := isGroupLinkedToDns(account.NameServerGroups, group.ID); isLinked {
|
|
|
|
return &GroupLinkError{"name server groups", linkedDns.Name}
|
|
|
|
}
|
|
|
|
|
|
|
|
if isLinked, linkedPolicy := isGroupLinkedToPolicy(account.Policies, group.ID); isLinked {
|
|
|
|
return &GroupLinkError{"policy", linkedPolicy.Name}
|
|
|
|
}
|
|
|
|
|
|
|
|
if isLinked, linkedSetupKey := isGroupLinkedToSetupKey(account.SetupKeys, group.ID); isLinked {
|
|
|
|
return &GroupLinkError{"setup key", linkedSetupKey.Name}
|
|
|
|
}
|
|
|
|
|
|
|
|
if isLinked, linkedUser := isGroupLinkedToUser(account.Users, group.ID); isLinked {
|
|
|
|
return &GroupLinkError{"user", linkedUser.Id}
|
|
|
|
}
|
|
|
|
|
|
|
|
if slices.Contains(account.DNSSettings.DisabledManagementGroups, group.ID) {
|
|
|
|
return &GroupLinkError{"disabled DNS management groups", group.Name}
|
|
|
|
}
|
|
|
|
|
|
|
|
if account.Settings.Extra != nil {
|
|
|
|
if slices.Contains(account.Settings.Extra.IntegratedValidatorGroups, group.ID) {
|
|
|
|
return &GroupLinkError{"integrated validator", group.Name}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// isGroupLinkedToRoute checks if a group is linked to any route in the account.
|
|
|
|
func isGroupLinkedToRoute(routes map[route.ID]*route.Route, groupID string) (bool, *route.Route) {
|
|
|
|
for _, r := range routes {
|
|
|
|
if slices.Contains(r.Groups, groupID) || slices.Contains(r.PeerGroups, groupID) {
|
|
|
|
return true, r
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// isGroupLinkedToPolicy checks if a group is linked to any policy in the account.
|
|
|
|
func isGroupLinkedToPolicy(policies []*Policy, groupID string) (bool, *Policy) {
|
|
|
|
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.
|
|
|
|
func isGroupLinkedToDns(nameServerGroups map[string]*nbdns.NameServerGroup, groupID string) (bool, *nbdns.NameServerGroup) {
|
|
|
|
for _, dns := range nameServerGroups {
|
|
|
|
for _, g := range dns.Groups {
|
|
|
|
if g == groupID {
|
|
|
|
return true, dns
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// isGroupLinkedToSetupKey checks if a group is linked to any setup key in the account.
|
|
|
|
func isGroupLinkedToSetupKey(setupKeys map[string]*SetupKey, groupID string) (bool, *SetupKey) {
|
|
|
|
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.
|
|
|
|
func isGroupLinkedToUser(users map[string]*User, groupID string) (bool, *User) {
|
|
|
|
for _, user := range users {
|
|
|
|
if slices.Contains(user.AutoGroups, groupID) {
|
|
|
|
return true, user
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false, nil
|
|
|
|
}
|