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"
|
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-09-27 16:10:50 +02:00
|
|
|
user, err := am.Store.GetUserByUserID(ctx, LockingStrengthShare, userID)
|
2024-01-25 09:50:27 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-09-27 16:10:50 +02:00
|
|
|
if !user.IsAdminOrServiceUser() || user.AccountID != accountID {
|
|
|
|
return nil, status.Errorf(status.PermissionDenied, "only users with admin power can view name server groups")
|
2022-09-30 13:47:11 +02:00
|
|
|
}
|
|
|
|
|
2024-09-27 16:10:50 +02:00
|
|
|
return am.Store.GetNameServerGroupByID(ctx, LockingStrengthShare, nsGroupID, accountID)
|
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) {
|
2022-11-07 17:52:23 +01:00
|
|
|
|
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-07-03 11:33:02 +02:00
|
|
|
account, err := am.Store.GetAccount(ctx, accountID)
|
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
|
|
|
}
|
|
|
|
|
|
|
|
newNSGroup := &nbdns.NameServerGroup{
|
2023-10-19 19:32:42 +02:00
|
|
|
ID: xid.New().String(),
|
|
|
|
Name: name,
|
|
|
|
Description: description,
|
|
|
|
NameServers: nameServerList,
|
|
|
|
Groups: groups,
|
|
|
|
Enabled: enabled,
|
|
|
|
Primary: primary,
|
|
|
|
Domains: domains,
|
|
|
|
SearchDomainsEnabled: searchDomainEnabled,
|
2022-09-30 13:47:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
err = validateNameServerGroup(false, newNSGroup, account)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if account.NameServerGroups == nil {
|
|
|
|
account.NameServerGroups = make(map[string]*nbdns.NameServerGroup)
|
|
|
|
}
|
|
|
|
|
|
|
|
account.NameServerGroups[newNSGroup.ID] = newNSGroup
|
|
|
|
|
|
|
|
account.Network.IncSerial()
|
2024-07-03 11:33:02 +02:00
|
|
|
err = am.Store.SaveAccount(ctx, account)
|
2022-09-30 13:47:11 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
am.updateAccountPeers(ctx, account)
|
2022-11-07 15:38:21 +01:00
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
am.StoreEvent(ctx, userID, newNSGroup.ID, accountID, activity.NameserverGroupCreated, newNSGroup.EventMeta())
|
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 {
|
2022-11-07 17:52:23 +01:00
|
|
|
|
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-07-03 11:33:02 +02:00
|
|
|
account, err := am.Store.GetAccount(ctx, accountID)
|
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
|
|
|
}
|
|
|
|
|
|
|
|
err = validateNameServerGroup(true, nsGroupToSave, account)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
account.NameServerGroups[nsGroupToSave.ID] = nsGroupToSave
|
|
|
|
|
|
|
|
account.Network.IncSerial()
|
2024-07-03 11:33:02 +02:00
|
|
|
err = am.Store.SaveAccount(ctx, account)
|
2022-09-30 13:47:11 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
am.updateAccountPeers(ctx, account)
|
2022-11-07 15:38:21 +01:00
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
am.StoreEvent(ctx, userID, nsGroupToSave.ID, accountID, activity.NameserverGroupUpdated, nsGroupToSave.EventMeta())
|
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 {
|
2022-11-07 17:52:23 +01:00
|
|
|
|
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-07-03 11:33:02 +02:00
|
|
|
account, err := am.Store.GetAccount(ctx, accountID)
|
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
|
|
|
}
|
|
|
|
|
2023-01-25 16:29:59 +01:00
|
|
|
nsGroup := account.NameServerGroups[nsGroupID]
|
|
|
|
if nsGroup == nil {
|
|
|
|
return status.Errorf(status.NotFound, "nameserver group %s wasn't found", nsGroupID)
|
|
|
|
}
|
2022-09-30 13:47:11 +02:00
|
|
|
delete(account.NameServerGroups, nsGroupID)
|
|
|
|
|
|
|
|
account.Network.IncSerial()
|
2024-07-03 11:33:02 +02:00
|
|
|
err = am.Store.SaveAccount(ctx, account)
|
2022-09-30 13:47:11 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
am.updateAccountPeers(ctx, account)
|
2022-11-07 15:38:21 +01:00
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
am.StoreEvent(ctx, userID, nsGroup.ID, accountID, activity.NameserverGroupDeleted, nsGroup.EventMeta())
|
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-09-27 16:10:50 +02:00
|
|
|
user, err := am.Store.GetUserByUserID(ctx, 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-09-27 16:10:50 +02:00
|
|
|
if !user.IsAdminOrServiceUser() || user.AccountID != accountID {
|
2024-01-25 09:50:27 +01:00
|
|
|
return nil, status.Errorf(status.PermissionDenied, "only users with admin power can view name server groups")
|
|
|
|
}
|
|
|
|
|
2024-09-27 16:10:50 +02:00
|
|
|
return am.Store.GetAccountNameServerGroups(ctx, LockingStrengthShare, accountID)
|
2022-09-30 13:47:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func validateNameServerGroup(existingGroup bool, nameserverGroup *nbdns.NameServerGroup, account *Account) error {
|
|
|
|
nsGroupID := ""
|
|
|
|
if existingGroup {
|
|
|
|
nsGroupID = nameserverGroup.ID
|
|
|
|
_, found := account.NameServerGroups[nsGroupID]
|
|
|
|
if !found {
|
2022-11-11 20:36:45 +01:00
|
|
|
return status.Errorf(status.NotFound, "nameserver group with ID %s was not found", nsGroupID)
|
2022-09-30 13:47:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-19 19:32:42 +02:00
|
|
|
err := validateDomainInput(nameserverGroup.Primary, nameserverGroup.Domains, nameserverGroup.SearchDomainsEnabled)
|
2022-11-03 18:39:37 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = validateNSGroupName(nameserverGroup.Name, nsGroupID, account.NameServerGroups)
|
2022-09-30 13:47:11 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = validateNSList(nameserverGroup.NameServers)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = validateGroups(nameserverGroup.Groups, account.Groups)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-09-30 13:47:11 +02:00
|
|
|
func validateNSGroupName(name, nsGroupID string, nsGroupMap map[string]*nbdns.NameServerGroup) error {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
for _, nsGroup := range nsGroupMap {
|
|
|
|
if name == nsGroup.Name && nsGroup.ID != nsGroupID {
|
2022-11-11 20:36:45 +01:00
|
|
|
return status.Errorf(status.InvalidArgument, "a nameserver group with name %s already exist", name)
|
2022-09-30 13:47:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func validateNSList(list []nbdns.NameServer) error {
|
|
|
|
nsListLenght := len(list)
|
2024-02-19 14:29:20 +01:00
|
|
|
if nsListLenght == 0 || nsListLenght > 3 {
|
|
|
|
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
|
|
|
}
|
|
|
|
found := false
|
|
|
|
for groupID := range groups {
|
|
|
|
if id == groupID {
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !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
|
|
|
|
}
|