mirror of
https://github.com/netbirdio/netbird.git
synced 2024-12-24 23:59:25 +01:00
Fix dns route retrieval condition (#2165)
* Fix route retrieval condition * Make error messages take domains into account
This commit is contained in:
parent
b075009ef7
commit
f9462eea27
@ -18,6 +18,11 @@ import (
|
|||||||
|
|
||||||
"github.com/eko/gocache/v3/cache"
|
"github.com/eko/gocache/v3/cache"
|
||||||
cacheStore "github.com/eko/gocache/v3/store"
|
cacheStore "github.com/eko/gocache/v3/store"
|
||||||
|
gocache "github.com/patrickmn/go-cache"
|
||||||
|
"github.com/rs/xid"
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
|
"golang.org/x/exp/maps"
|
||||||
|
|
||||||
"github.com/netbirdio/netbird/base62"
|
"github.com/netbirdio/netbird/base62"
|
||||||
nbdns "github.com/netbirdio/netbird/dns"
|
nbdns "github.com/netbirdio/netbird/dns"
|
||||||
"github.com/netbirdio/netbird/management/domain"
|
"github.com/netbirdio/netbird/management/domain"
|
||||||
@ -33,10 +38,6 @@ import (
|
|||||||
"github.com/netbirdio/netbird/management/server/posture"
|
"github.com/netbirdio/netbird/management/server/posture"
|
||||||
"github.com/netbirdio/netbird/management/server/status"
|
"github.com/netbirdio/netbird/management/server/status"
|
||||||
"github.com/netbirdio/netbird/route"
|
"github.com/netbirdio/netbird/route"
|
||||||
gocache "github.com/patrickmn/go-cache"
|
|
||||||
"github.com/rs/xid"
|
|
||||||
log "github.com/sirupsen/logrus"
|
|
||||||
"golang.org/x/exp/maps"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -383,9 +384,9 @@ func (a *Account) getRoutingPeerRoutes(peerID string) (enabledRoutes []*route.Ro
|
|||||||
func (a *Account) GetRoutesByPrefixOrDomains(prefix netip.Prefix, domains domain.List) []*route.Route {
|
func (a *Account) GetRoutesByPrefixOrDomains(prefix netip.Prefix, domains domain.List) []*route.Route {
|
||||||
var routes []*route.Route
|
var routes []*route.Route
|
||||||
for _, r := range a.Routes {
|
for _, r := range a.Routes {
|
||||||
if r.IsDynamic() && r.Domains.PunycodeString() == domains.PunycodeString() {
|
dynamic := r.IsDynamic()
|
||||||
routes = append(routes, r)
|
if dynamic && r.Domains.PunycodeString() == domains.PunycodeString() ||
|
||||||
} else if r.Network.String() == prefix.String() {
|
!dynamic && r.Network.String() == prefix.String() {
|
||||||
routes = append(routes, r)
|
routes = append(routes, r)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package server
|
package server
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"net/netip"
|
"net/netip"
|
||||||
"unicode/utf8"
|
"unicode/utf8"
|
||||||
|
|
||||||
@ -51,7 +52,7 @@ func (am *DefaultAccountManager) checkRoutePrefixOrDomainsExistForPeers(account
|
|||||||
|
|
||||||
for _, prefixRoute := range routesWithPrefix {
|
for _, prefixRoute := range routesWithPrefix {
|
||||||
// we skip route(s) with the same network ID as we want to allow updating of the existing route
|
// we skip route(s) with the same network ID as we want to allow updating of the existing route
|
||||||
// when create a new route routeID is newly generated so nothing will be skipped
|
// when creating a new route routeID is newly generated so nothing will be skipped
|
||||||
if routeID == prefixRoute.ID {
|
if routeID == prefixRoute.ID {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@ -65,8 +66,9 @@ func (am *DefaultAccountManager) checkRoutePrefixOrDomainsExistForPeers(account
|
|||||||
group := account.GetGroup(groupID)
|
group := account.GetGroup(groupID)
|
||||||
if group == nil {
|
if group == nil {
|
||||||
return status.Errorf(
|
return status.Errorf(
|
||||||
status.InvalidArgument, "failed to add route with prefix %s - peer group %s doesn't exist",
|
status.InvalidArgument, "failed to add route with %s - peer group %s doesn't exist",
|
||||||
prefix.String(), groupID)
|
getRouteDescriptor(prefix, domains), groupID,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, pID := range group.Peers {
|
for _, pID := range group.Peers {
|
||||||
@ -83,18 +85,18 @@ func (am *DefaultAccountManager) checkRoutePrefixOrDomainsExistForPeers(account
|
|||||||
}
|
}
|
||||||
if _, ok := seenPeers[peerID]; ok {
|
if _, ok := seenPeers[peerID]; ok {
|
||||||
return status.Errorf(status.AlreadyExists,
|
return status.Errorf(status.AlreadyExists,
|
||||||
"failed to add route with prefix %s - peer %s already has this route", prefix.String(), peerID)
|
"failed to add route with %s - peer %s already has this route", getRouteDescriptor(prefix, domains), peerID)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// check that peerGroupIDs are not in any route peerGroups list
|
// check that peerGroupIDs are not in any route peerGroups list
|
||||||
for _, groupID := range peerGroupIDs {
|
for _, groupID := range peerGroupIDs {
|
||||||
group := account.GetGroup(groupID) // we validated the group existent before entering this function, o need to check again.
|
group := account.GetGroup(groupID) // we validated the group existence before entering this function, no need to check again.
|
||||||
|
|
||||||
if _, ok := seenPeerGroups[groupID]; ok {
|
if _, ok := seenPeerGroups[groupID]; ok {
|
||||||
return status.Errorf(
|
return status.Errorf(
|
||||||
status.AlreadyExists, "failed to add route with prefix %s - peer group %s already has this route",
|
status.AlreadyExists, "failed to add route with %s - peer group %s already has this route",
|
||||||
prefix.String(), group.Name)
|
getRouteDescriptor(prefix, domains), group.Name)
|
||||||
}
|
}
|
||||||
|
|
||||||
// check that the peers from peerGroupIDs groups are not the same peers we saw in routesWithPrefix
|
// check that the peers from peerGroupIDs groups are not the same peers we saw in routesWithPrefix
|
||||||
@ -105,8 +107,8 @@ func (am *DefaultAccountManager) checkRoutePrefixOrDomainsExistForPeers(account
|
|||||||
return status.Errorf(status.InvalidArgument, "peer with ID %s not found", peerID)
|
return status.Errorf(status.InvalidArgument, "peer with ID %s not found", peerID)
|
||||||
}
|
}
|
||||||
return status.Errorf(status.AlreadyExists,
|
return status.Errorf(status.AlreadyExists,
|
||||||
"failed to add route with prefix %s - peer %s from the group %s already has this route",
|
"failed to add route with %s - peer %s from the group %s already has this route",
|
||||||
prefix.String(), peer.Name, group.Name)
|
getRouteDescriptor(prefix, domains), peer.Name, group.Name)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -114,6 +116,13 @@ func (am *DefaultAccountManager) checkRoutePrefixOrDomainsExistForPeers(account
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getRouteDescriptor(prefix netip.Prefix, domains domain.List) string {
|
||||||
|
if len(domains) > 0 {
|
||||||
|
return fmt.Sprintf("domains [%s]", domains.SafeString())
|
||||||
|
}
|
||||||
|
return fmt.Sprintf("prefix %s", prefix.String())
|
||||||
|
}
|
||||||
|
|
||||||
// CreateRoute creates and saves a new route
|
// CreateRoute creates and saves a new route
|
||||||
func (am *DefaultAccountManager) CreateRoute(accountID string, prefix netip.Prefix, networkType route.NetworkType, domains domain.List, peerID string, peerGroupIDs []string, description string, netID route.NetID, masquerade bool, metric int, groups []string, enabled bool, userID string, keepRoute bool) (*route.Route, error) {
|
func (am *DefaultAccountManager) CreateRoute(accountID string, prefix netip.Prefix, networkType route.NetworkType, domains domain.List, peerID string, peerGroupIDs []string, description string, netID route.NetID, masquerade bool, metric int, groups []string, enabled bool, userID string, keepRoute bool) (*route.Route, error) {
|
||||||
unlock := am.Store.AcquireAccountWriteLock(accountID)
|
unlock := am.Store.AcquireAccountWriteLock(accountID)
|
||||||
|
Loading…
Reference in New Issue
Block a user