2022-09-30 13:47:11 +02:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2024-07-03 11:33:02 +02:00
|
|
|
"context"
|
2023-08-16 11:25:38 +02:00
|
|
|
"errors"
|
|
|
|
"regexp"
|
|
|
|
"unicode/utf8"
|
|
|
|
|
2022-11-03 18:39:37 +01:00
|
|
|
"github.com/miekg/dns"
|
2023-08-16 11:25:38 +02:00
|
|
|
"github.com/rs/xid"
|
|
|
|
|
2022-09-30 13:47:11 +02:00
|
|
|
nbdns "github.com/netbirdio/netbird/dns"
|
2023-01-25 16:29:59 +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"
|
2022-11-11 20:36:45 +01:00
|
|
|
"github.com/netbirdio/netbird/management/server/status"
|
2024-12-11 12:58:45 +01:00
|
|
|
"github.com/netbirdio/netbird/management/server/store"
|
2022-09-30 13:47:11 +02:00
|
|
|
)
|
|
|
|
|
2023-09-18 12:25:12 +02:00
|
|
|
const domainPattern = `^(?i)[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,}$`
|
2022-09-30 13:47:11 +02:00
|
|
|
|
|
|
|
// GetNameServerGroup gets a nameserver group object from account and nameserver group IDs
|
2024-07-03 11:33:02 +02:00
|
|
|
func (am *DefaultAccountManager) GetNameServerGroup(ctx context.Context, accountID, userID, nsGroupID string) (*nbdns.NameServerGroup, error) {
|
2024-12-11 12:58:45 +01:00
|
|
|
user, err := am.Store.GetUserByUserID(ctx, store.LockingStrengthShare, userID)
|
2024-01-25 09:50:27 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-11-26 17:39:04 +01:00
|
|
|
if user.AccountID != accountID {
|
|
|
|
return nil, status.NewUserNotPartOfAccountError()
|
2022-09-30 13:47:11 +02:00
|
|
|
}
|
|
|
|
|
2024-11-26 17:39:04 +01:00
|
|
|
if user.IsRegularUser() {
|
|
|
|
return nil, status.NewAdminPermissionError()
|
|
|
|
}
|
|
|
|
|
2024-12-11 12:58:45 +01:00
|
|
|
return am.Store.GetNameServerGroupByID(ctx, store.LockingStrengthShare, accountID, nsGroupID)
|
2022-09-30 13:47:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// CreateNameServerGroup creates and saves a new nameserver group
|
2024-07-03 11:33:02 +02:00
|
|
|
func (am *DefaultAccountManager) CreateNameServerGroup(ctx context.Context, accountID string, name, description string, nameServerList []nbdns.NameServer, groups []string, primary bool, domains []string, enabled bool, userID string, searchDomainEnabled bool) (*nbdns.NameServerGroup, 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-09-30 13:47:11 +02:00
|
|
|
|
2024-12-11 12:58:45 +01:00
|
|
|
user, err := am.Store.GetUserByUserID(ctx, store.LockingStrengthShare, userID)
|
2022-09-30 13:47:11 +02:00
|
|
|
if err != nil {
|
2022-11-11 20:36:45 +01:00
|
|
|
return nil, err
|
2022-09-30 13:47:11 +02:00
|
|
|
}
|
|
|
|
|
2024-11-26 17:39:04 +01:00
|
|
|
if user.AccountID != accountID {
|
|
|
|
return nil, status.NewUserNotPartOfAccountError()
|
|
|
|
}
|
|
|
|
|
2022-09-30 13:47:11 +02:00
|
|
|
newNSGroup := &nbdns.NameServerGroup{
|
2023-10-19 19:32:42 +02:00
|
|
|
ID: xid.New().String(),
|
2024-11-26 17:39:04 +01:00
|
|
|
AccountID: accountID,
|
2023-10-19 19:32:42 +02:00
|
|
|
Name: name,
|
|
|
|
Description: description,
|
|
|
|
NameServers: nameServerList,
|
|
|
|
Groups: groups,
|
|
|
|
Enabled: enabled,
|
|
|
|
Primary: primary,
|
|
|
|
Domains: domains,
|
|
|
|
SearchDomainsEnabled: searchDomainEnabled,
|
2022-09-30 13:47:11 +02:00
|
|
|
}
|
|
|
|
|
2024-11-26 17:39:04 +01:00
|
|
|
var updateAccountPeers bool
|
2022-09-30 13:47:11 +02:00
|
|
|
|
2024-12-11 12:58:45 +01:00
|
|
|
err = am.Store.ExecuteInTransaction(ctx, func(transaction store.Store) error {
|
2024-11-26 17:39:04 +01:00
|
|
|
if err = validateNameServerGroup(ctx, transaction, accountID, newNSGroup); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-09-30 13:47:11 +02:00
|
|
|
|
2024-11-26 17:39:04 +01:00
|
|
|
updateAccountPeers, err = anyGroupHasPeers(ctx, transaction, accountID, newNSGroup.Groups)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-09-30 13:47:11 +02:00
|
|
|
|
2024-12-11 12:58:45 +01:00
|
|
|
if err = transaction.IncrementNetworkSerial(ctx, store.LockingStrengthUpdate, accountID); err != nil {
|
2024-11-26 17:39:04 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-12-11 12:58:45 +01:00
|
|
|
return transaction.SaveNameServerGroup(ctx, store.LockingStrengthUpdate, newNSGroup)
|
2024-11-26 17:39:04 +01:00
|
|
|
})
|
|
|
|
if err != nil {
|
2022-09-30 13:47:11 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-11-26 17:39:04 +01:00
|
|
|
am.StoreEvent(ctx, userID, newNSGroup.ID, accountID, activity.NameserverGroupCreated, newNSGroup.EventMeta())
|
|
|
|
|
|
|
|
if updateAccountPeers {
|
2024-11-15 18:09:32 +01:00
|
|
|
am.updateAccountPeers(ctx, accountID)
|
2024-10-23 12:05:02 +02:00
|
|
|
}
|
2023-01-25 16:29:59 +01:00
|
|
|
|
2022-09-30 13:47:11 +02:00
|
|
|
return newNSGroup.Copy(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SaveNameServerGroup saves nameserver group
|
2024-07-03 11:33:02 +02:00
|
|
|
func (am *DefaultAccountManager) SaveNameServerGroup(ctx context.Context, accountID, userID string, nsGroupToSave *nbdns.NameServerGroup) 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-09-30 13:47:11 +02:00
|
|
|
|
|
|
|
if nsGroupToSave == nil {
|
2022-11-11 20:36:45 +01:00
|
|
|
return status.Errorf(status.InvalidArgument, "nameserver group provided is nil")
|
2022-09-30 13:47:11 +02:00
|
|
|
}
|
|
|
|
|
2024-12-11 12:58:45 +01:00
|
|
|
user, err := am.Store.GetUserByUserID(ctx, store.LockingStrengthShare, userID)
|
2022-09-30 13:47:11 +02:00
|
|
|
if err != nil {
|
2022-11-11 20:36:45 +01:00
|
|
|
return err
|
2022-09-30 13:47:11 +02:00
|
|
|
}
|
|
|
|
|
2024-11-26 17:39:04 +01:00
|
|
|
if user.AccountID != accountID {
|
|
|
|
return status.NewUserNotPartOfAccountError()
|
2022-09-30 13:47:11 +02:00
|
|
|
}
|
|
|
|
|
2024-11-26 17:39:04 +01:00
|
|
|
var updateAccountPeers bool
|
|
|
|
|
2024-12-11 12:58:45 +01:00
|
|
|
err = am.Store.ExecuteInTransaction(ctx, func(transaction store.Store) error {
|
|
|
|
oldNSGroup, err := transaction.GetNameServerGroupByID(ctx, store.LockingStrengthShare, accountID, nsGroupToSave.ID)
|
2024-11-26 17:39:04 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
nsGroupToSave.AccountID = accountID
|
|
|
|
|
|
|
|
if err = validateNameServerGroup(ctx, transaction, accountID, nsGroupToSave); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
updateAccountPeers, err = areNameServerGroupChangesAffectPeers(ctx, transaction, nsGroupToSave, oldNSGroup)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-12-11 12:58:45 +01:00
|
|
|
if err = transaction.IncrementNetworkSerial(ctx, store.LockingStrengthUpdate, accountID); err != nil {
|
2024-11-26 17:39:04 +01:00
|
|
|
return err
|
|
|
|
}
|
2022-09-30 13:47:11 +02:00
|
|
|
|
2024-12-11 12:58:45 +01:00
|
|
|
return transaction.SaveNameServerGroup(ctx, store.LockingStrengthUpdate, nsGroupToSave)
|
2024-11-26 17:39:04 +01:00
|
|
|
})
|
|
|
|
if err != nil {
|
2022-09-30 13:47:11 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-11-26 17:39:04 +01:00
|
|
|
am.StoreEvent(ctx, userID, nsGroupToSave.ID, accountID, activity.NameserverGroupUpdated, nsGroupToSave.EventMeta())
|
|
|
|
|
|
|
|
if updateAccountPeers {
|
2024-11-15 18:09:32 +01:00
|
|
|
am.updateAccountPeers(ctx, accountID)
|
2024-10-23 12:05:02 +02:00
|
|
|
}
|
2023-01-25 16:29:59 +01:00
|
|
|
|
2022-09-30 13:47:11 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteNameServerGroup deletes nameserver group with nsGroupID
|
2024-07-03 11:33:02 +02:00
|
|
|
func (am *DefaultAccountManager) DeleteNameServerGroup(ctx context.Context, accountID, nsGroupID, userID 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-09-30 13:47:11 +02:00
|
|
|
|
2024-12-11 12:58:45 +01:00
|
|
|
user, err := am.Store.GetUserByUserID(ctx, store.LockingStrengthShare, userID)
|
2022-09-30 13:47:11 +02:00
|
|
|
if err != nil {
|
2022-11-11 20:36:45 +01:00
|
|
|
return err
|
2022-09-30 13:47:11 +02:00
|
|
|
}
|
|
|
|
|
2024-11-26 17:39:04 +01:00
|
|
|
if user.AccountID != accountID {
|
|
|
|
return status.NewUserNotPartOfAccountError()
|
2023-01-25 16:29:59 +01:00
|
|
|
}
|
2022-09-30 13:47:11 +02:00
|
|
|
|
2024-11-26 17:39:04 +01:00
|
|
|
var nsGroup *nbdns.NameServerGroup
|
|
|
|
var updateAccountPeers bool
|
|
|
|
|
2024-12-11 12:58:45 +01:00
|
|
|
err = am.Store.ExecuteInTransaction(ctx, func(transaction store.Store) error {
|
|
|
|
nsGroup, err = transaction.GetNameServerGroupByID(ctx, store.LockingStrengthUpdate, accountID, nsGroupID)
|
2024-11-26 17:39:04 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
updateAccountPeers, err = anyGroupHasPeers(ctx, transaction, accountID, nsGroup.Groups)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-12-11 12:58:45 +01:00
|
|
|
if err = transaction.IncrementNetworkSerial(ctx, store.LockingStrengthUpdate, accountID); err != nil {
|
2024-11-26 17:39:04 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-12-11 12:58:45 +01:00
|
|
|
return transaction.DeleteNameServerGroup(ctx, store.LockingStrengthUpdate, accountID, nsGroupID)
|
2024-11-26 17:39:04 +01:00
|
|
|
})
|
|
|
|
if err != nil {
|
2022-09-30 13:47:11 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-11-26 17:39:04 +01:00
|
|
|
am.StoreEvent(ctx, userID, nsGroup.ID, accountID, activity.NameserverGroupDeleted, nsGroup.EventMeta())
|
|
|
|
|
|
|
|
if updateAccountPeers {
|
2024-11-15 18:09:32 +01:00
|
|
|
am.updateAccountPeers(ctx, accountID)
|
2024-10-23 12:05:02 +02:00
|
|
|
}
|
2023-01-25 16:29:59 +01:00
|
|
|
|
2022-09-30 13:47:11 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListNameServerGroups returns a list of nameserver groups from account
|
2024-07-03 11:33:02 +02:00
|
|
|
func (am *DefaultAccountManager) ListNameServerGroups(ctx context.Context, accountID string, userID string) ([]*nbdns.NameServerGroup, error) {
|
2024-12-11 12:58:45 +01:00
|
|
|
user, err := am.Store.GetUserByUserID(ctx, store.LockingStrengthShare, userID)
|
2022-09-30 13:47:11 +02:00
|
|
|
if err != nil {
|
2022-11-11 20:36:45 +01:00
|
|
|
return nil, err
|
2022-09-30 13:47:11 +02:00
|
|
|
}
|
|
|
|
|
2024-11-26 17:39:04 +01:00
|
|
|
if user.AccountID != accountID {
|
|
|
|
return nil, status.NewUserNotPartOfAccountError()
|
|
|
|
}
|
|
|
|
|
|
|
|
if user.IsRegularUser() {
|
|
|
|
return nil, status.NewAdminPermissionError()
|
2024-01-25 09:50:27 +01:00
|
|
|
}
|
|
|
|
|
2024-12-11 12:58:45 +01:00
|
|
|
return am.Store.GetAccountNameServerGroups(ctx, store.LockingStrengthShare, accountID)
|
2022-09-30 13:47:11 +02:00
|
|
|
}
|
|
|
|
|
2024-12-11 12:58:45 +01:00
|
|
|
func validateNameServerGroup(ctx context.Context, transaction store.Store, accountID string, nameserverGroup *nbdns.NameServerGroup) error {
|
2024-11-26 17:39:04 +01:00
|
|
|
err := validateDomainInput(nameserverGroup.Primary, nameserverGroup.Domains, nameserverGroup.SearchDomainsEnabled)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2022-09-30 13:47:11 +02:00
|
|
|
}
|
|
|
|
|
2024-11-26 17:39:04 +01:00
|
|
|
err = validateNSList(nameserverGroup.NameServers)
|
2022-11-03 18:39:37 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-12-11 12:58:45 +01:00
|
|
|
nsServerGroups, err := transaction.GetAccountNameServerGroups(ctx, store.LockingStrengthShare, accountID)
|
2022-09-30 13:47:11 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-11-26 17:39:04 +01:00
|
|
|
err = validateNSGroupName(nameserverGroup.Name, nameserverGroup.ID, nsServerGroups)
|
2022-09-30 13:47:11 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-12-11 12:58:45 +01:00
|
|
|
groups, err := transaction.GetGroupsByIDs(ctx, store.LockingStrengthShare, accountID, nameserverGroup.Groups)
|
2022-09-30 13:47:11 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-11-26 17:39:04 +01:00
|
|
|
return validateGroups(nameserverGroup.Groups, groups)
|
|
|
|
}
|
|
|
|
|
|
|
|
// areNameServerGroupChangesAffectPeers checks if the changes in the nameserver group affect the peers.
|
2024-12-11 12:58:45 +01:00
|
|
|
func areNameServerGroupChangesAffectPeers(ctx context.Context, transaction store.Store, newNSGroup, oldNSGroup *nbdns.NameServerGroup) (bool, error) {
|
2024-11-26 17:39:04 +01:00
|
|
|
if !newNSGroup.Enabled && !oldNSGroup.Enabled {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
hasPeers, err := anyGroupHasPeers(ctx, transaction, newNSGroup.AccountID, newNSGroup.Groups)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if hasPeers {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return anyGroupHasPeers(ctx, transaction, oldNSGroup.AccountID, oldNSGroup.Groups)
|
2022-09-30 13:47:11 +02:00
|
|
|
}
|
|
|
|
|
2023-10-19 19:32:42 +02:00
|
|
|
func validateDomainInput(primary bool, domains []string, searchDomainsEnabled bool) error {
|
2022-11-03 18:39:37 +01:00
|
|
|
if !primary && len(domains) == 0 {
|
2022-11-11 20:36:45 +01:00
|
|
|
return status.Errorf(status.InvalidArgument, "nameserver group primary status is false and domains are empty,"+
|
2022-11-03 18:39:37 +01:00
|
|
|
" it should be primary or have at least one domain")
|
|
|
|
}
|
|
|
|
if primary && len(domains) != 0 {
|
2022-11-11 20:36:45 +01:00
|
|
|
return status.Errorf(status.InvalidArgument, "nameserver group primary status is true and domains are not empty,"+
|
2022-11-03 18:39:37 +01:00
|
|
|
" you should set either primary or domain")
|
|
|
|
}
|
2023-10-19 19:32:42 +02:00
|
|
|
|
|
|
|
if primary && searchDomainsEnabled {
|
|
|
|
return status.Errorf(status.InvalidArgument, "nameserver group primary status is true and search domains is enabled,"+
|
|
|
|
" you should not set search domains for primary nameservers")
|
|
|
|
}
|
|
|
|
|
2022-11-03 18:39:37 +01:00
|
|
|
for _, domain := range domains {
|
2023-08-16 11:25:38 +02:00
|
|
|
if err := validateDomain(domain); err != nil {
|
|
|
|
return status.Errorf(status.InvalidArgument, "nameserver group got an invalid domain: %s %q", domain, err)
|
2022-11-03 18:39:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-11-26 17:39:04 +01:00
|
|
|
func validateNSGroupName(name, nsGroupID string, groups []*nbdns.NameServerGroup) error {
|
2022-09-30 13:47:11 +02:00
|
|
|
if utf8.RuneCountInString(name) > nbdns.MaxGroupNameChar || name == "" {
|
2022-11-11 20:36:45 +01:00
|
|
|
return status.Errorf(status.InvalidArgument, "nameserver group name should be between 1 and %d", nbdns.MaxGroupNameChar)
|
2022-09-30 13:47:11 +02:00
|
|
|
}
|
|
|
|
|
2024-11-26 17:39:04 +01:00
|
|
|
for _, nsGroup := range groups {
|
2022-09-30 13:47:11 +02:00
|
|
|
if name == nsGroup.Name && nsGroup.ID != nsGroupID {
|
2024-11-26 17:39:04 +01:00
|
|
|
return status.Errorf(status.InvalidArgument, "nameserver group with name %s already exist", name)
|
2022-09-30 13:47:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func validateNSList(list []nbdns.NameServer) error {
|
2024-11-26 17:39:04 +01:00
|
|
|
nsListLength := len(list)
|
|
|
|
if nsListLength == 0 || nsListLength > 3 {
|
2024-02-19 14:29:20 +01:00
|
|
|
return status.Errorf(status.InvalidArgument, "the list of nameservers should be 1 or 3, got %d", len(list))
|
2022-09-30 13:47:11 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-03-27 18:48:48 +01:00
|
|
|
func validateGroups(list []string, groups map[string]*nbgroup.Group) error {
|
2022-09-30 13:47:11 +02:00
|
|
|
if len(list) == 0 {
|
2022-11-11 20:36:45 +01:00
|
|
|
return status.Errorf(status.InvalidArgument, "the list of group IDs should not be empty")
|
2022-09-30 13:47:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, id := range list {
|
|
|
|
if id == "" {
|
2022-11-11 20:36:45 +01:00
|
|
|
return status.Errorf(status.InvalidArgument, "group ID should not be empty string")
|
2022-09-30 13:47:11 +02:00
|
|
|
}
|
2024-11-26 17:39:04 +01:00
|
|
|
if _, found := groups[id]; !found {
|
2022-11-11 20:36:45 +01:00
|
|
|
return status.Errorf(status.InvalidArgument, "group id %s not found", id)
|
2022-09-30 13:47:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2023-08-16 11:25:38 +02:00
|
|
|
|
2023-11-27 13:01:00 +01:00
|
|
|
var domainMatcher = regexp.MustCompile(domainPattern)
|
|
|
|
|
2023-08-16 11:25:38 +02:00
|
|
|
func validateDomain(domain string) error {
|
|
|
|
if !domainMatcher.MatchString(domain) {
|
|
|
|
return errors.New("domain should consists of only letters, numbers, and hyphens with no leading, trailing hyphens, or spaces")
|
|
|
|
}
|
|
|
|
|
|
|
|
labels, valid := dns.IsDomainName(domain)
|
|
|
|
if !valid {
|
|
|
|
return errors.New("invalid domain name")
|
|
|
|
}
|
|
|
|
|
|
|
|
if labels < 2 {
|
|
|
|
return errors.New("domain should consists of a minimum of two labels")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|