2022-08-18 18:22:15 +02:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2024-07-03 11:33:02 +02:00
|
|
|
"context"
|
2024-10-02 13:41:00 +02:00
|
|
|
"fmt"
|
|
|
|
"net"
|
2023-03-13 15:14:18 +01:00
|
|
|
"net/netip"
|
|
|
|
"testing"
|
2024-10-23 12:05:02 +02:00
|
|
|
"time"
|
2023-03-13 15:14:18 +01:00
|
|
|
|
2022-08-18 18:22:15 +02:00
|
|
|
"github.com/rs/xid"
|
2023-10-09 14:39:41 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
2022-08-18 18:22:15 +02:00
|
|
|
"github.com/stretchr/testify/require"
|
2023-05-19 11:42:25 +02:00
|
|
|
|
2024-06-13 13:24:24 +02:00
|
|
|
"github.com/netbirdio/netbird/management/domain"
|
2023-05-19 11:42:25 +02: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-11-28 13:45:26 +01:00
|
|
|
nbpeer "github.com/netbirdio/netbird/management/server/peer"
|
2024-08-07 10:52:31 +02:00
|
|
|
"github.com/netbirdio/netbird/management/server/telemetry"
|
2023-05-19 11:42:25 +02:00
|
|
|
"github.com/netbirdio/netbird/route"
|
2022-08-18 18:22:15 +02:00
|
|
|
)
|
|
|
|
|
2022-12-06 10:11:57 +01:00
|
|
|
const (
|
|
|
|
peer1Key = "BhRPtynAAYRDy08+q4HTMsos8fs4plTP4NOSh7C1ry8="
|
|
|
|
peer2Key = "/yF0+vCfv+mRR5k0dca0TrGdO/oiNeAI58gToZm5NyI="
|
2023-09-28 14:32:36 +02:00
|
|
|
peer3Key = "ayF0+vCfv+mRR5k0dca0TrGdO/oiNeAI58gToZm5NaF="
|
|
|
|
peer4Key = "ayF0+vCfv+mRR5k0dca0TrGdO/oiNeAI58gToZm5acc="
|
|
|
|
peer5Key = "ayF0+vCfv+mRR5k0dca0TrGdO/oiNeAI58gToZm5a55="
|
2023-02-03 10:33:28 +01:00
|
|
|
peer1ID = "peer-1-id"
|
|
|
|
peer2ID = "peer-2-id"
|
2023-09-28 14:32:36 +02:00
|
|
|
peer3ID = "peer-3-id"
|
|
|
|
peer4ID = "peer-4-id"
|
|
|
|
peer5ID = "peer-5-id"
|
2022-12-06 10:11:57 +01:00
|
|
|
routeGroup1 = "routeGroup1"
|
|
|
|
routeGroup2 = "routeGroup2"
|
2023-09-28 14:32:36 +02:00
|
|
|
routeGroup3 = "routeGroup3" // for existing route
|
|
|
|
routeGroup4 = "routeGroup4" // for existing route
|
|
|
|
routeGroupHA1 = "routeGroupHA1"
|
|
|
|
routeGroupHA2 = "routeGroupHA2"
|
2022-12-06 10:11:57 +01:00
|
|
|
routeInvalidGroup1 = "routeInvalidGroup1"
|
2023-01-02 15:11:32 +01:00
|
|
|
userID = "testingUser"
|
2023-09-28 14:32:36 +02:00
|
|
|
existingRouteID = "random-id"
|
2022-12-06 10:11:57 +01:00
|
|
|
)
|
2022-08-18 18:22:15 +02:00
|
|
|
|
2024-06-13 13:24:24 +02:00
|
|
|
var existingNetwork = netip.MustParsePrefix("10.10.10.0/24")
|
|
|
|
var existingDomains = domain.List{"example.com"}
|
|
|
|
|
2022-08-18 18:22:15 +02:00
|
|
|
func TestCreateRoute(t *testing.T) {
|
|
|
|
type input struct {
|
2024-10-02 13:41:00 +02:00
|
|
|
network netip.Prefix
|
|
|
|
domains domain.List
|
|
|
|
keepRoute bool
|
|
|
|
networkType route.NetworkType
|
|
|
|
netID route.NetID
|
|
|
|
peerKey string
|
|
|
|
peerGroupIDs []string
|
|
|
|
description string
|
|
|
|
masquerade bool
|
|
|
|
metric int
|
|
|
|
enabled bool
|
|
|
|
groups []string
|
|
|
|
accessControlGroups []string
|
2022-08-18 18:22:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
testCases := []struct {
|
2023-10-09 14:39:41 +02:00
|
|
|
name string
|
|
|
|
inputArgs input
|
|
|
|
createInitRoute bool
|
|
|
|
shouldCreate bool
|
|
|
|
errFunc require.ErrorAssertionFunc
|
|
|
|
expectedRoute *route.Route
|
2022-08-18 18:22:15 +02:00
|
|
|
}{
|
|
|
|
{
|
2024-06-13 13:24:24 +02:00
|
|
|
name: "Happy Path Network",
|
2022-08-18 18:22:15 +02:00
|
|
|
inputArgs: input{
|
2024-10-02 13:41:00 +02:00
|
|
|
network: netip.MustParsePrefix("192.168.0.0/16"),
|
|
|
|
networkType: route.IPv4Network,
|
|
|
|
netID: "happy",
|
|
|
|
peerKey: peer1ID,
|
|
|
|
description: "super",
|
|
|
|
masquerade: false,
|
|
|
|
metric: 9999,
|
|
|
|
enabled: true,
|
|
|
|
groups: []string{routeGroup1},
|
|
|
|
accessControlGroups: []string{routeGroup1},
|
2022-08-18 18:22:15 +02:00
|
|
|
},
|
|
|
|
errFunc: require.NoError,
|
|
|
|
shouldCreate: true,
|
|
|
|
expectedRoute: &route.Route{
|
2024-10-02 13:41:00 +02:00
|
|
|
Network: netip.MustParsePrefix("192.168.0.0/16"),
|
|
|
|
NetworkType: route.IPv4Network,
|
|
|
|
NetID: "happy",
|
|
|
|
Peer: peer1ID,
|
|
|
|
Description: "super",
|
|
|
|
Masquerade: false,
|
|
|
|
Metric: 9999,
|
|
|
|
Enabled: true,
|
|
|
|
Groups: []string{routeGroup1},
|
|
|
|
AccessControlGroups: []string{routeGroup1},
|
2022-08-18 18:22:15 +02:00
|
|
|
},
|
|
|
|
},
|
2024-06-13 13:24:24 +02:00
|
|
|
{
|
|
|
|
name: "Happy Path Domains",
|
|
|
|
inputArgs: input{
|
2024-10-02 13:41:00 +02:00
|
|
|
domains: domain.List{"domain1", "domain2"},
|
|
|
|
keepRoute: true,
|
|
|
|
networkType: route.DomainNetwork,
|
|
|
|
netID: "happy",
|
|
|
|
peerKey: peer1ID,
|
|
|
|
description: "super",
|
|
|
|
masquerade: false,
|
|
|
|
metric: 9999,
|
|
|
|
enabled: true,
|
|
|
|
groups: []string{routeGroup1},
|
|
|
|
accessControlGroups: []string{routeGroup1},
|
2024-06-13 13:24:24 +02:00
|
|
|
},
|
|
|
|
errFunc: require.NoError,
|
|
|
|
shouldCreate: true,
|
|
|
|
expectedRoute: &route.Route{
|
2024-10-02 13:41:00 +02:00
|
|
|
Network: netip.MustParsePrefix("192.0.2.0/32"),
|
|
|
|
Domains: domain.List{"domain1", "domain2"},
|
|
|
|
NetworkType: route.DomainNetwork,
|
|
|
|
NetID: "happy",
|
|
|
|
Peer: peer1ID,
|
|
|
|
Description: "super",
|
|
|
|
Masquerade: false,
|
|
|
|
Metric: 9999,
|
|
|
|
Enabled: true,
|
|
|
|
Groups: []string{routeGroup1},
|
|
|
|
KeepRoute: true,
|
|
|
|
AccessControlGroups: []string{routeGroup1},
|
2024-06-13 13:24:24 +02:00
|
|
|
},
|
|
|
|
},
|
2023-09-28 14:32:36 +02:00
|
|
|
{
|
|
|
|
name: "Happy Path Peer Groups",
|
|
|
|
inputArgs: input{
|
2024-10-02 13:41:00 +02:00
|
|
|
network: netip.MustParsePrefix("192.168.0.0/16"),
|
|
|
|
networkType: route.IPv4Network,
|
|
|
|
netID: "happy",
|
|
|
|
peerGroupIDs: []string{routeGroupHA1, routeGroupHA2},
|
|
|
|
description: "super",
|
|
|
|
masquerade: false,
|
|
|
|
metric: 9999,
|
|
|
|
enabled: true,
|
|
|
|
groups: []string{routeGroup1, routeGroup2},
|
|
|
|
accessControlGroups: []string{routeGroup1, routeGroup2},
|
2023-09-28 14:32:36 +02:00
|
|
|
},
|
|
|
|
errFunc: require.NoError,
|
|
|
|
shouldCreate: true,
|
|
|
|
expectedRoute: &route.Route{
|
2024-10-02 13:41:00 +02:00
|
|
|
Network: netip.MustParsePrefix("192.168.0.0/16"),
|
|
|
|
NetworkType: route.IPv4Network,
|
|
|
|
NetID: "happy",
|
|
|
|
PeerGroups: []string{routeGroupHA1, routeGroupHA2},
|
|
|
|
Description: "super",
|
|
|
|
Masquerade: false,
|
|
|
|
Metric: 9999,
|
|
|
|
Enabled: true,
|
|
|
|
Groups: []string{routeGroup1, routeGroup2},
|
|
|
|
AccessControlGroups: []string{routeGroup1, routeGroup2},
|
2023-09-28 14:32:36 +02:00
|
|
|
},
|
|
|
|
},
|
2024-06-13 13:24:24 +02:00
|
|
|
{
|
|
|
|
name: "Both network and domains provided should fail",
|
|
|
|
inputArgs: input{
|
2024-10-02 13:41:00 +02:00
|
|
|
network: netip.MustParsePrefix("192.168.0.0/16"),
|
|
|
|
domains: domain.List{"domain1", "domain2"},
|
|
|
|
netID: "happy",
|
|
|
|
peerKey: peer1ID,
|
|
|
|
peerGroupIDs: []string{routeGroupHA1},
|
|
|
|
description: "super",
|
|
|
|
masquerade: false,
|
|
|
|
metric: 9999,
|
|
|
|
enabled: true,
|
|
|
|
groups: []string{routeGroup1},
|
|
|
|
accessControlGroups: []string{routeGroup2},
|
2024-06-13 13:24:24 +02:00
|
|
|
},
|
|
|
|
errFunc: require.Error,
|
|
|
|
shouldCreate: false,
|
|
|
|
},
|
2023-09-28 14:32:36 +02:00
|
|
|
{
|
|
|
|
name: "Both peer and peer_groups Provided Should Fail",
|
|
|
|
inputArgs: input{
|
2024-10-02 13:41:00 +02:00
|
|
|
network: netip.MustParsePrefix("192.168.0.0/16"),
|
|
|
|
networkType: route.IPv4Network,
|
|
|
|
netID: "happy",
|
|
|
|
peerKey: peer1ID,
|
|
|
|
peerGroupIDs: []string{routeGroupHA1},
|
|
|
|
description: "super",
|
|
|
|
masquerade: false,
|
|
|
|
metric: 9999,
|
|
|
|
enabled: true,
|
|
|
|
groups: []string{routeGroup1},
|
|
|
|
accessControlGroups: []string{routeGroup2},
|
2023-09-28 14:32:36 +02:00
|
|
|
},
|
|
|
|
errFunc: require.Error,
|
|
|
|
shouldCreate: false,
|
|
|
|
},
|
2022-08-18 18:22:15 +02:00
|
|
|
{
|
2024-06-13 13:24:24 +02:00
|
|
|
name: "Bad Peer Should Fail",
|
2022-08-18 18:22:15 +02:00
|
|
|
inputArgs: input{
|
2024-06-13 13:24:24 +02:00
|
|
|
network: netip.MustParsePrefix("192.168.0.0/16"),
|
|
|
|
networkType: route.IPv4Network,
|
2022-08-22 14:10:24 +02:00
|
|
|
netID: "happy",
|
2024-06-13 13:24:24 +02:00
|
|
|
peerKey: "notExistingPeer",
|
2022-08-18 18:22:15 +02:00
|
|
|
description: "super",
|
|
|
|
masquerade: false,
|
|
|
|
metric: 9999,
|
|
|
|
enabled: true,
|
2022-12-06 10:11:57 +01:00
|
|
|
groups: []string{routeGroup1},
|
2022-08-18 18:22:15 +02:00
|
|
|
},
|
|
|
|
errFunc: require.Error,
|
|
|
|
shouldCreate: false,
|
|
|
|
},
|
|
|
|
{
|
2024-06-13 13:24:24 +02:00
|
|
|
name: "Bad Peer already has this network route",
|
2022-08-18 18:22:15 +02:00
|
|
|
inputArgs: input{
|
2024-06-13 13:24:24 +02:00
|
|
|
network: existingNetwork,
|
|
|
|
networkType: route.IPv4Network,
|
|
|
|
netID: "bad",
|
|
|
|
peerKey: peer5ID,
|
2022-08-18 18:22:15 +02:00
|
|
|
description: "super",
|
|
|
|
masquerade: false,
|
|
|
|
metric: 9999,
|
|
|
|
enabled: true,
|
2022-12-06 10:11:57 +01:00
|
|
|
groups: []string{routeGroup1},
|
2022-08-18 18:22:15 +02:00
|
|
|
},
|
2024-06-13 13:24:24 +02:00
|
|
|
createInitRoute: true,
|
|
|
|
errFunc: require.Error,
|
|
|
|
shouldCreate: false,
|
2022-08-18 18:22:15 +02:00
|
|
|
},
|
2023-09-28 14:32:36 +02:00
|
|
|
{
|
2024-06-13 13:24:24 +02:00
|
|
|
name: "Bad Peer already has this domains route",
|
2023-09-28 14:32:36 +02:00
|
|
|
inputArgs: input{
|
2024-06-13 13:24:24 +02:00
|
|
|
domains: existingDomains,
|
|
|
|
networkType: route.DomainNetwork,
|
2023-09-28 14:32:36 +02:00
|
|
|
netID: "bad",
|
|
|
|
peerKey: peer5ID,
|
|
|
|
description: "super",
|
|
|
|
masquerade: false,
|
|
|
|
metric: 9999,
|
|
|
|
enabled: true,
|
|
|
|
groups: []string{routeGroup1},
|
|
|
|
},
|
2023-10-09 14:39:41 +02:00
|
|
|
createInitRoute: true,
|
|
|
|
errFunc: require.Error,
|
|
|
|
shouldCreate: false,
|
2023-09-28 14:32:36 +02:00
|
|
|
},
|
|
|
|
{
|
2024-06-13 13:24:24 +02:00
|
|
|
name: "Bad Peers Group already has this network route",
|
2023-09-28 14:32:36 +02:00
|
|
|
inputArgs: input{
|
|
|
|
network: existingNetwork,
|
2024-06-13 13:24:24 +02:00
|
|
|
networkType: route.IPv4Network,
|
|
|
|
netID: "bad",
|
|
|
|
peerGroupIDs: []string{routeGroup1, routeGroup3},
|
|
|
|
description: "super",
|
|
|
|
masquerade: false,
|
|
|
|
metric: 9999,
|
|
|
|
enabled: true,
|
|
|
|
groups: []string{routeGroup1},
|
|
|
|
},
|
|
|
|
createInitRoute: true,
|
|
|
|
errFunc: require.Error,
|
|
|
|
shouldCreate: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Bad Peers Group already has this domains route",
|
|
|
|
inputArgs: input{
|
|
|
|
domains: existingDomains,
|
|
|
|
networkType: route.DomainNetwork,
|
2023-09-28 14:32:36 +02:00
|
|
|
netID: "bad",
|
|
|
|
peerGroupIDs: []string{routeGroup1, routeGroup3},
|
|
|
|
description: "super",
|
|
|
|
masquerade: false,
|
|
|
|
metric: 9999,
|
|
|
|
enabled: true,
|
|
|
|
groups: []string{routeGroup1},
|
|
|
|
},
|
2023-10-09 14:39:41 +02:00
|
|
|
createInitRoute: true,
|
|
|
|
errFunc: require.Error,
|
|
|
|
shouldCreate: false,
|
2023-09-28 14:32:36 +02:00
|
|
|
},
|
2022-08-18 18:22:15 +02:00
|
|
|
{
|
2022-12-06 10:11:57 +01:00
|
|
|
name: "Empty Peer Should Create",
|
2022-08-18 18:22:15 +02:00
|
|
|
inputArgs: input{
|
2024-06-13 13:24:24 +02:00
|
|
|
network: netip.MustParsePrefix("192.168.0.0/16"),
|
|
|
|
networkType: route.IPv4Network,
|
2022-08-22 14:10:24 +02:00
|
|
|
netID: "happy",
|
2023-02-03 10:33:28 +01:00
|
|
|
peerKey: "",
|
2022-08-18 18:22:15 +02:00
|
|
|
description: "super",
|
|
|
|
masquerade: false,
|
|
|
|
metric: 9999,
|
|
|
|
enabled: false,
|
2022-12-06 10:11:57 +01:00
|
|
|
groups: []string{routeGroup1},
|
2022-08-18 18:22:15 +02:00
|
|
|
},
|
|
|
|
errFunc: require.NoError,
|
|
|
|
shouldCreate: true,
|
|
|
|
expectedRoute: &route.Route{
|
2022-08-22 14:10:24 +02:00
|
|
|
Network: netip.MustParsePrefix("192.168.0.0/16"),
|
|
|
|
NetworkType: route.IPv4Network,
|
|
|
|
NetID: "happy",
|
2022-08-18 18:22:15 +02:00
|
|
|
Peer: "",
|
|
|
|
Description: "super",
|
|
|
|
Masquerade: false,
|
|
|
|
Metric: 9999,
|
|
|
|
Enabled: false,
|
2022-12-06 10:11:57 +01:00
|
|
|
Groups: []string{routeGroup1},
|
2022-08-18 18:22:15 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2022-12-06 10:11:57 +01:00
|
|
|
name: "Large Metric Should Fail",
|
2022-08-18 18:22:15 +02:00
|
|
|
inputArgs: input{
|
2024-06-13 13:24:24 +02:00
|
|
|
network: netip.MustParsePrefix("192.168.0.0/16"),
|
|
|
|
networkType: route.IPv4Network,
|
2023-02-03 10:33:28 +01:00
|
|
|
peerKey: peer1ID,
|
2022-08-22 14:10:24 +02:00
|
|
|
netID: "happy",
|
2022-08-18 18:22:15 +02:00
|
|
|
description: "super",
|
|
|
|
masquerade: false,
|
|
|
|
metric: 99999,
|
|
|
|
enabled: true,
|
2022-12-06 10:11:57 +01:00
|
|
|
groups: []string{routeGroup1},
|
2022-08-18 18:22:15 +02:00
|
|
|
},
|
|
|
|
errFunc: require.Error,
|
|
|
|
shouldCreate: false,
|
|
|
|
},
|
|
|
|
{
|
2022-12-06 10:11:57 +01:00
|
|
|
name: "Small Metric Should Fail",
|
2022-08-18 18:22:15 +02:00
|
|
|
inputArgs: input{
|
2024-06-13 13:24:24 +02:00
|
|
|
network: netip.MustParsePrefix("192.168.0.0/16"),
|
|
|
|
networkType: route.IPv4Network,
|
2022-08-22 14:10:24 +02:00
|
|
|
netID: "happy",
|
2023-02-03 10:33:28 +01:00
|
|
|
peerKey: peer1ID,
|
2022-08-18 18:22:15 +02:00
|
|
|
description: "super",
|
|
|
|
masquerade: false,
|
|
|
|
metric: 0,
|
|
|
|
enabled: true,
|
2022-12-06 10:11:57 +01:00
|
|
|
groups: []string{routeGroup1},
|
2022-08-18 18:22:15 +02:00
|
|
|
},
|
|
|
|
errFunc: require.Error,
|
|
|
|
shouldCreate: false,
|
|
|
|
},
|
2022-08-22 14:10:24 +02:00
|
|
|
{
|
2022-12-06 10:11:57 +01:00
|
|
|
name: "Large NetID Should Fail",
|
2022-08-22 14:10:24 +02:00
|
|
|
inputArgs: input{
|
2024-06-13 13:24:24 +02:00
|
|
|
network: netip.MustParsePrefix("192.168.0.0/16"),
|
|
|
|
networkType: route.IPv4Network,
|
2023-02-03 10:33:28 +01:00
|
|
|
peerKey: peer1ID,
|
2022-08-22 14:10:24 +02:00
|
|
|
netID: "12345678901234567890qwertyuiopqwertyuiop1",
|
|
|
|
description: "super",
|
|
|
|
masquerade: false,
|
|
|
|
metric: 9999,
|
|
|
|
enabled: true,
|
2022-12-06 10:11:57 +01:00
|
|
|
groups: []string{routeGroup1},
|
2022-08-22 14:10:24 +02:00
|
|
|
},
|
|
|
|
errFunc: require.Error,
|
|
|
|
shouldCreate: false,
|
|
|
|
},
|
|
|
|
{
|
2022-12-06 10:11:57 +01:00
|
|
|
name: "Small NetID Should Fail",
|
2022-08-22 14:10:24 +02:00
|
|
|
inputArgs: input{
|
2024-06-13 13:24:24 +02:00
|
|
|
network: netip.MustParsePrefix("192.168.0.0/16"),
|
|
|
|
networkType: route.IPv4Network,
|
2022-08-22 14:10:24 +02:00
|
|
|
netID: "",
|
2023-02-03 10:33:28 +01:00
|
|
|
peerKey: peer1ID,
|
2022-08-22 14:10:24 +02:00
|
|
|
description: "",
|
|
|
|
masquerade: false,
|
|
|
|
metric: 9999,
|
|
|
|
enabled: true,
|
2022-12-06 10:11:57 +01:00
|
|
|
groups: []string{routeGroup1},
|
|
|
|
},
|
|
|
|
errFunc: require.Error,
|
|
|
|
shouldCreate: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Empty Group List Should Fail",
|
|
|
|
inputArgs: input{
|
2024-06-13 13:24:24 +02:00
|
|
|
network: netip.MustParsePrefix("192.168.0.0/16"),
|
|
|
|
networkType: route.IPv4Network,
|
2022-12-06 10:11:57 +01:00
|
|
|
netID: "NewId",
|
2023-02-03 10:33:28 +01:00
|
|
|
peerKey: peer1ID,
|
2022-12-06 10:11:57 +01:00
|
|
|
description: "",
|
|
|
|
masquerade: false,
|
|
|
|
metric: 9999,
|
|
|
|
enabled: true,
|
|
|
|
groups: []string{},
|
|
|
|
},
|
|
|
|
errFunc: require.Error,
|
|
|
|
shouldCreate: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Empty Group ID string Should Fail",
|
|
|
|
inputArgs: input{
|
2024-06-13 13:24:24 +02:00
|
|
|
network: netip.MustParsePrefix("192.168.0.0/16"),
|
|
|
|
networkType: route.IPv4Network,
|
2022-12-06 10:11:57 +01:00
|
|
|
netID: "NewId",
|
2023-02-03 10:33:28 +01:00
|
|
|
peerKey: peer1ID,
|
2022-12-06 10:11:57 +01:00
|
|
|
description: "",
|
|
|
|
masquerade: false,
|
|
|
|
metric: 9999,
|
|
|
|
enabled: true,
|
|
|
|
groups: []string{""},
|
|
|
|
},
|
|
|
|
errFunc: require.Error,
|
|
|
|
shouldCreate: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Invalid Group Should Fail",
|
|
|
|
inputArgs: input{
|
2024-06-13 13:24:24 +02:00
|
|
|
network: netip.MustParsePrefix("192.168.0.0/16"),
|
|
|
|
networkType: route.IPv4Network,
|
2022-12-06 10:11:57 +01:00
|
|
|
netID: "NewId",
|
2023-02-03 10:33:28 +01:00
|
|
|
peerKey: peer1ID,
|
2022-12-06 10:11:57 +01:00
|
|
|
description: "",
|
|
|
|
masquerade: false,
|
|
|
|
metric: 9999,
|
|
|
|
enabled: true,
|
|
|
|
groups: []string{routeInvalidGroup1},
|
2022-08-22 14:10:24 +02:00
|
|
|
},
|
|
|
|
errFunc: require.Error,
|
|
|
|
shouldCreate: false,
|
|
|
|
},
|
2022-08-18 18:22:15 +02:00
|
|
|
}
|
|
|
|
for _, testCase := range testCases {
|
|
|
|
t.Run(testCase.name, func(t *testing.T) {
|
|
|
|
am, err := createRouterManager(t)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("failed to create account manager")
|
|
|
|
}
|
|
|
|
|
|
|
|
account, err := initTestRouteAccount(t, am)
|
|
|
|
if err != nil {
|
2023-09-28 14:32:36 +02:00
|
|
|
t.Errorf("failed to init testing account: %s", err)
|
2022-08-18 18:22:15 +02:00
|
|
|
}
|
|
|
|
|
2023-10-09 14:39:41 +02:00
|
|
|
if testCase.createInitRoute {
|
|
|
|
groupAll, errInit := account.GetGroupAll()
|
2024-06-13 13:24:24 +02:00
|
|
|
require.NoError(t, errInit)
|
2024-10-02 13:41:00 +02:00
|
|
|
_, errInit = am.CreateRoute(context.Background(), account.Id, existingNetwork, 1, nil, "", []string{routeGroup3, routeGroup4}, "", existingRouteID, false, 1000, []string{groupAll.ID}, []string{}, true, userID, false)
|
2024-06-13 13:24:24 +02:00
|
|
|
require.NoError(t, errInit)
|
2024-10-02 13:41:00 +02:00
|
|
|
_, errInit = am.CreateRoute(context.Background(), account.Id, netip.Prefix{}, 3, existingDomains, "", []string{routeGroup3, routeGroup4}, "", existingRouteID, false, 1000, []string{groupAll.ID}, []string{groupAll.ID}, true, userID, false)
|
2024-06-13 13:24:24 +02:00
|
|
|
require.NoError(t, errInit)
|
2023-10-09 14:39:41 +02:00
|
|
|
}
|
|
|
|
|
2024-10-02 13:41:00 +02:00
|
|
|
outRoute, err := am.CreateRoute(context.Background(), account.Id, testCase.inputArgs.network, testCase.inputArgs.networkType, testCase.inputArgs.domains, testCase.inputArgs.peerKey, testCase.inputArgs.peerGroupIDs, testCase.inputArgs.description, testCase.inputArgs.netID, testCase.inputArgs.masquerade, testCase.inputArgs.metric, testCase.inputArgs.groups, testCase.inputArgs.accessControlGroups, testCase.inputArgs.enabled, userID, testCase.inputArgs.keepRoute)
|
2022-08-18 18:22:15 +02:00
|
|
|
|
|
|
|
testCase.errFunc(t, err)
|
|
|
|
|
|
|
|
if !testCase.shouldCreate {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// assign generated ID
|
|
|
|
testCase.expectedRoute.ID = outRoute.ID
|
|
|
|
|
|
|
|
if !testCase.expectedRoute.IsEqual(outRoute) {
|
|
|
|
t.Errorf("new route didn't match expected route:\nGot %#v\nExpected:%#v\n", outRoute, testCase.expectedRoute)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSaveRoute(t *testing.T) {
|
2023-02-03 10:33:28 +01:00
|
|
|
validPeer := peer2ID
|
2023-09-28 14:32:36 +02:00
|
|
|
validUsedPeer := peer5ID
|
2022-08-18 18:22:15 +02:00
|
|
|
invalidPeer := "nonExisting"
|
|
|
|
validPrefix := netip.MustParsePrefix("192.168.0.0/24")
|
2024-06-13 13:24:24 +02:00
|
|
|
placeholderPrefix := netip.MustParsePrefix("192.0.2.0/32")
|
2022-08-18 18:22:15 +02:00
|
|
|
invalidPrefix, _ := netip.ParsePrefix("192.168.0.0/34")
|
|
|
|
validMetric := 1000
|
2024-06-13 13:24:24 +02:00
|
|
|
trueKeepRoute := true
|
|
|
|
falseKeepRoute := false
|
|
|
|
ipv4networkType := route.IPv4Network
|
|
|
|
domainNetworkType := route.DomainNetwork
|
2022-08-18 18:22:15 +02:00
|
|
|
invalidMetric := 99999
|
2024-05-06 14:47:49 +02:00
|
|
|
validNetID := route.NetID("12345678901234567890qw")
|
|
|
|
invalidNetID := route.NetID("12345678901234567890qwertyuiopqwertyuiop1")
|
2023-09-28 14:32:36 +02:00
|
|
|
validGroupHA1 := routeGroupHA1
|
|
|
|
validGroupHA2 := routeGroupHA2
|
2022-08-18 18:22:15 +02:00
|
|
|
|
|
|
|
testCases := []struct {
|
2023-10-09 14:39:41 +02:00
|
|
|
name string
|
|
|
|
existingRoute *route.Route
|
|
|
|
createInitRoute bool
|
|
|
|
newPeer *string
|
|
|
|
newPeerGroups []string
|
|
|
|
newMetric *int
|
|
|
|
newPrefix *netip.Prefix
|
2024-06-13 13:24:24 +02:00
|
|
|
newDomains domain.List
|
|
|
|
newNetworkType *route.NetworkType
|
|
|
|
newKeepRoute *bool
|
2023-10-09 14:39:41 +02:00
|
|
|
newGroups []string
|
|
|
|
skipCopying bool
|
|
|
|
shouldCreate bool
|
|
|
|
errFunc require.ErrorAssertionFunc
|
|
|
|
expectedRoute *route.Route
|
2022-08-18 18:22:15 +02:00
|
|
|
}{
|
|
|
|
{
|
2024-06-13 13:24:24 +02:00
|
|
|
name: "Happy Path Network",
|
2022-08-18 18:22:15 +02:00
|
|
|
existingRoute: &route.Route{
|
|
|
|
ID: "testingRoute",
|
2022-08-22 14:10:24 +02:00
|
|
|
Network: netip.MustParsePrefix("192.168.0.0/16"),
|
|
|
|
NetID: validNetID,
|
|
|
|
NetworkType: route.IPv4Network,
|
2023-02-03 10:33:28 +01:00
|
|
|
Peer: peer1ID,
|
2022-08-18 18:22:15 +02:00
|
|
|
Description: "super",
|
|
|
|
Masquerade: false,
|
|
|
|
Metric: 9999,
|
|
|
|
Enabled: true,
|
2022-12-06 10:11:57 +01:00
|
|
|
Groups: []string{routeGroup1},
|
2022-08-18 18:22:15 +02:00
|
|
|
},
|
|
|
|
newPeer: &validPeer,
|
|
|
|
newMetric: &validMetric,
|
|
|
|
newPrefix: &validPrefix,
|
2022-12-06 10:11:57 +01:00
|
|
|
newGroups: []string{routeGroup2},
|
2022-08-18 18:22:15 +02:00
|
|
|
errFunc: require.NoError,
|
|
|
|
shouldCreate: true,
|
|
|
|
expectedRoute: &route.Route{
|
|
|
|
ID: "testingRoute",
|
2022-08-22 14:10:24 +02:00
|
|
|
Network: validPrefix,
|
|
|
|
NetID: validNetID,
|
|
|
|
NetworkType: route.IPv4Network,
|
2022-08-18 18:22:15 +02:00
|
|
|
Peer: validPeer,
|
|
|
|
Description: "super",
|
|
|
|
Masquerade: false,
|
|
|
|
Metric: validMetric,
|
|
|
|
Enabled: true,
|
2022-12-06 10:11:57 +01:00
|
|
|
Groups: []string{routeGroup2},
|
2022-08-18 18:22:15 +02:00
|
|
|
},
|
|
|
|
},
|
2024-06-13 13:24:24 +02:00
|
|
|
{
|
|
|
|
name: "Happy Path Domains",
|
|
|
|
existingRoute: &route.Route{
|
|
|
|
ID: "testingRoute",
|
|
|
|
Network: netip.Prefix{},
|
|
|
|
Domains: domain.List{"example.com"},
|
|
|
|
KeepRoute: false,
|
|
|
|
NetID: validNetID,
|
|
|
|
NetworkType: route.DomainNetwork,
|
|
|
|
Peer: peer1ID,
|
|
|
|
Description: "super",
|
|
|
|
Masquerade: false,
|
|
|
|
Metric: 9999,
|
|
|
|
Enabled: true,
|
|
|
|
Groups: []string{routeGroup1},
|
|
|
|
},
|
|
|
|
newPeer: &validPeer,
|
|
|
|
newMetric: &validMetric,
|
|
|
|
newPrefix: &netip.Prefix{},
|
|
|
|
newDomains: domain.List{"example.com", "example2.com"},
|
|
|
|
newKeepRoute: &trueKeepRoute,
|
|
|
|
newGroups: []string{routeGroup1},
|
|
|
|
errFunc: require.NoError,
|
|
|
|
shouldCreate: true,
|
|
|
|
expectedRoute: &route.Route{
|
|
|
|
ID: "testingRoute",
|
|
|
|
Network: placeholderPrefix,
|
|
|
|
Domains: domain.List{"example.com", "example2.com"},
|
|
|
|
KeepRoute: true,
|
|
|
|
NetID: validNetID,
|
|
|
|
NetworkType: route.DomainNetwork,
|
|
|
|
Peer: validPeer,
|
|
|
|
Description: "super",
|
|
|
|
Masquerade: false,
|
|
|
|
Metric: validMetric,
|
|
|
|
Enabled: true,
|
|
|
|
Groups: []string{routeGroup1},
|
|
|
|
},
|
|
|
|
},
|
2023-09-28 14:32:36 +02:00
|
|
|
{
|
|
|
|
name: "Happy Path Peer Groups",
|
|
|
|
existingRoute: &route.Route{
|
|
|
|
ID: "testingRoute",
|
|
|
|
Network: netip.MustParsePrefix("192.168.0.0/16"),
|
|
|
|
NetID: validNetID,
|
|
|
|
NetworkType: route.IPv4Network,
|
|
|
|
Description: "super",
|
|
|
|
Masquerade: false,
|
|
|
|
Metric: 9999,
|
|
|
|
Enabled: true,
|
|
|
|
Groups: []string{routeGroup1},
|
|
|
|
},
|
|
|
|
newPeerGroups: []string{validGroupHA1, validGroupHA2},
|
|
|
|
newMetric: &validMetric,
|
|
|
|
newPrefix: &validPrefix,
|
|
|
|
newGroups: []string{routeGroup2},
|
|
|
|
errFunc: require.NoError,
|
|
|
|
shouldCreate: true,
|
|
|
|
expectedRoute: &route.Route{
|
|
|
|
ID: "testingRoute",
|
|
|
|
Network: validPrefix,
|
|
|
|
NetID: validNetID,
|
|
|
|
NetworkType: route.IPv4Network,
|
|
|
|
PeerGroups: []string{validGroupHA1, validGroupHA2},
|
|
|
|
Description: "super",
|
|
|
|
Masquerade: false,
|
|
|
|
Metric: validMetric,
|
|
|
|
Enabled: true,
|
|
|
|
Groups: []string{routeGroup2},
|
|
|
|
},
|
|
|
|
},
|
2024-06-13 13:24:24 +02:00
|
|
|
{
|
|
|
|
name: "Both network and domains provided should fail",
|
|
|
|
existingRoute: &route.Route{
|
|
|
|
ID: "testingRoute",
|
|
|
|
Network: netip.MustParsePrefix("192.168.0.0/16"),
|
|
|
|
NetID: validNetID,
|
|
|
|
NetworkType: route.IPv4Network,
|
|
|
|
Description: "super",
|
|
|
|
Masquerade: false,
|
|
|
|
Metric: 9999,
|
|
|
|
Enabled: true,
|
|
|
|
Groups: []string{routeGroup1},
|
|
|
|
},
|
|
|
|
newPrefix: &validPrefix,
|
|
|
|
newDomains: domain.List{"example.com"},
|
|
|
|
errFunc: require.Error,
|
|
|
|
},
|
2023-09-28 14:32:36 +02:00
|
|
|
{
|
|
|
|
name: "Both peer and peers_roup Provided Should Fail",
|
|
|
|
existingRoute: &route.Route{
|
|
|
|
ID: "testingRoute",
|
|
|
|
Network: netip.MustParsePrefix("192.168.0.0/16"),
|
|
|
|
NetID: validNetID,
|
|
|
|
NetworkType: route.IPv4Network,
|
|
|
|
Description: "super",
|
|
|
|
Masquerade: false,
|
|
|
|
Metric: 9999,
|
|
|
|
Enabled: true,
|
|
|
|
Groups: []string{routeGroup1},
|
|
|
|
},
|
|
|
|
newPeer: &validPeer,
|
|
|
|
newPeerGroups: []string{validGroupHA1},
|
|
|
|
errFunc: require.Error,
|
|
|
|
},
|
2022-08-18 18:22:15 +02:00
|
|
|
{
|
2022-12-06 10:11:57 +01:00
|
|
|
name: "Bad Prefix Should Fail",
|
2022-08-18 18:22:15 +02:00
|
|
|
existingRoute: &route.Route{
|
|
|
|
ID: "testingRoute",
|
2022-08-22 14:10:24 +02:00
|
|
|
Network: netip.MustParsePrefix("192.168.0.0/16"),
|
|
|
|
NetID: validNetID,
|
|
|
|
NetworkType: route.IPv4Network,
|
2023-02-03 10:33:28 +01:00
|
|
|
Peer: peer1ID,
|
2022-08-18 18:22:15 +02:00
|
|
|
Description: "super",
|
|
|
|
Masquerade: false,
|
|
|
|
Metric: 9999,
|
|
|
|
Enabled: true,
|
2022-12-06 10:11:57 +01:00
|
|
|
Groups: []string{routeGroup1},
|
2022-08-18 18:22:15 +02:00
|
|
|
},
|
|
|
|
newPrefix: &invalidPrefix,
|
|
|
|
errFunc: require.Error,
|
|
|
|
},
|
|
|
|
{
|
2022-12-06 10:11:57 +01:00
|
|
|
name: "Bad Peer Should Fail",
|
2022-08-18 18:22:15 +02:00
|
|
|
existingRoute: &route.Route{
|
|
|
|
ID: "testingRoute",
|
2022-08-22 14:10:24 +02:00
|
|
|
Network: netip.MustParsePrefix("192.168.0.0/16"),
|
|
|
|
NetID: validNetID,
|
|
|
|
NetworkType: route.IPv4Network,
|
2023-02-03 10:33:28 +01:00
|
|
|
Peer: peer1ID,
|
2022-08-18 18:22:15 +02:00
|
|
|
Description: "super",
|
|
|
|
Masquerade: false,
|
|
|
|
Metric: 9999,
|
|
|
|
Enabled: true,
|
2022-12-06 10:11:57 +01:00
|
|
|
Groups: []string{routeGroup1},
|
2022-08-18 18:22:15 +02:00
|
|
|
},
|
|
|
|
newPeer: &invalidPeer,
|
|
|
|
errFunc: require.Error,
|
|
|
|
},
|
|
|
|
{
|
2022-12-06 10:11:57 +01:00
|
|
|
name: "Invalid Metric Should Fail",
|
2022-08-18 18:22:15 +02:00
|
|
|
existingRoute: &route.Route{
|
|
|
|
ID: "testingRoute",
|
2022-08-22 14:10:24 +02:00
|
|
|
Network: netip.MustParsePrefix("192.168.0.0/16"),
|
|
|
|
NetID: validNetID,
|
|
|
|
NetworkType: route.IPv4Network,
|
2023-02-03 10:33:28 +01:00
|
|
|
Peer: peer1ID,
|
2022-08-22 14:10:24 +02:00
|
|
|
Description: "super",
|
|
|
|
Masquerade: false,
|
|
|
|
Metric: 9999,
|
|
|
|
Enabled: true,
|
2022-12-06 10:11:57 +01:00
|
|
|
Groups: []string{routeGroup1},
|
2022-08-22 14:10:24 +02:00
|
|
|
},
|
|
|
|
newMetric: &invalidMetric,
|
|
|
|
errFunc: require.Error,
|
|
|
|
},
|
|
|
|
{
|
2022-12-06 10:11:57 +01:00
|
|
|
name: "Invalid NetID Should Fail",
|
2022-08-22 14:10:24 +02:00
|
|
|
existingRoute: &route.Route{
|
|
|
|
ID: "testingRoute",
|
|
|
|
Network: netip.MustParsePrefix("192.168.0.0/16"),
|
|
|
|
NetID: invalidNetID,
|
|
|
|
NetworkType: route.IPv4Network,
|
2023-02-03 10:33:28 +01:00
|
|
|
Peer: peer1ID,
|
2022-08-18 18:22:15 +02:00
|
|
|
Description: "super",
|
|
|
|
Masquerade: false,
|
|
|
|
Metric: 9999,
|
|
|
|
Enabled: true,
|
2022-12-06 10:11:57 +01:00
|
|
|
Groups: []string{routeGroup1},
|
2022-08-18 18:22:15 +02:00
|
|
|
},
|
|
|
|
newMetric: &invalidMetric,
|
|
|
|
errFunc: require.Error,
|
|
|
|
},
|
|
|
|
{
|
2022-12-06 10:11:57 +01:00
|
|
|
name: "Nil Route Should Fail",
|
2022-08-18 18:22:15 +02:00
|
|
|
existingRoute: &route.Route{
|
|
|
|
ID: "testingRoute",
|
2022-08-22 14:10:24 +02:00
|
|
|
Network: netip.MustParsePrefix("192.168.0.0/16"),
|
|
|
|
NetID: validNetID,
|
|
|
|
NetworkType: route.IPv4Network,
|
2023-02-03 10:33:28 +01:00
|
|
|
Peer: peer1ID,
|
2022-08-18 18:22:15 +02:00
|
|
|
Description: "super",
|
|
|
|
Masquerade: false,
|
|
|
|
Metric: 9999,
|
|
|
|
Enabled: true,
|
2022-12-06 10:11:57 +01:00
|
|
|
Groups: []string{routeGroup1},
|
2022-08-18 18:22:15 +02:00
|
|
|
},
|
|
|
|
skipCopying: true,
|
|
|
|
errFunc: require.Error,
|
|
|
|
},
|
2022-12-06 10:11:57 +01:00
|
|
|
{
|
|
|
|
name: "Empty Group List Should Fail",
|
|
|
|
existingRoute: &route.Route{
|
|
|
|
ID: "testingRoute",
|
|
|
|
Network: netip.MustParsePrefix("192.168.0.0/16"),
|
|
|
|
NetID: validNetID,
|
|
|
|
NetworkType: route.IPv4Network,
|
2023-02-03 10:33:28 +01:00
|
|
|
Peer: peer1ID,
|
2022-12-06 10:11:57 +01:00
|
|
|
Description: "super",
|
|
|
|
Masquerade: false,
|
|
|
|
Metric: 9999,
|
|
|
|
Enabled: true,
|
|
|
|
Groups: []string{routeGroup1},
|
|
|
|
},
|
|
|
|
newGroups: []string{},
|
|
|
|
errFunc: require.Error,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Empty Group ID String Should Fail",
|
|
|
|
existingRoute: &route.Route{
|
|
|
|
ID: "testingRoute",
|
|
|
|
Network: netip.MustParsePrefix("192.168.0.0/16"),
|
|
|
|
NetID: validNetID,
|
|
|
|
NetworkType: route.IPv4Network,
|
2023-02-03 10:33:28 +01:00
|
|
|
Peer: peer1ID,
|
2022-12-06 10:11:57 +01:00
|
|
|
Description: "super",
|
|
|
|
Masquerade: false,
|
|
|
|
Metric: 9999,
|
|
|
|
Enabled: true,
|
|
|
|
Groups: []string{routeGroup1},
|
|
|
|
},
|
|
|
|
newGroups: []string{""},
|
|
|
|
errFunc: require.Error,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Invalid Group Should Fail",
|
|
|
|
existingRoute: &route.Route{
|
|
|
|
ID: "testingRoute",
|
|
|
|
Network: netip.MustParsePrefix("192.168.0.0/16"),
|
|
|
|
NetID: validNetID,
|
|
|
|
NetworkType: route.IPv4Network,
|
2023-02-03 10:33:28 +01:00
|
|
|
Peer: peer1ID,
|
2022-12-06 10:11:57 +01:00
|
|
|
Description: "super",
|
|
|
|
Masquerade: false,
|
|
|
|
Metric: 9999,
|
|
|
|
Enabled: true,
|
|
|
|
Groups: []string{routeGroup1},
|
|
|
|
},
|
|
|
|
newGroups: []string{routeInvalidGroup1},
|
|
|
|
errFunc: require.Error,
|
|
|
|
},
|
2023-09-28 14:32:36 +02:00
|
|
|
{
|
|
|
|
name: "Allow to modify existing route with new peer",
|
|
|
|
existingRoute: &route.Route{
|
|
|
|
ID: "testingRoute",
|
2024-06-13 13:24:24 +02:00
|
|
|
Network: existingNetwork,
|
2023-09-28 14:32:36 +02:00
|
|
|
NetID: validNetID,
|
|
|
|
NetworkType: route.IPv4Network,
|
|
|
|
Peer: peer1ID,
|
|
|
|
Description: "super",
|
|
|
|
Masquerade: false,
|
|
|
|
Metric: 9999,
|
|
|
|
Enabled: true,
|
|
|
|
Groups: []string{routeGroup1},
|
|
|
|
},
|
|
|
|
newPeer: &validPeer,
|
|
|
|
errFunc: require.NoError,
|
|
|
|
shouldCreate: true,
|
|
|
|
expectedRoute: &route.Route{
|
|
|
|
ID: "testingRoute",
|
2024-06-13 13:24:24 +02:00
|
|
|
Network: existingNetwork,
|
2023-09-28 14:32:36 +02:00
|
|
|
NetID: validNetID,
|
|
|
|
NetworkType: route.IPv4Network,
|
|
|
|
Peer: validPeer,
|
|
|
|
PeerGroups: []string{},
|
|
|
|
Description: "super",
|
|
|
|
Masquerade: false,
|
|
|
|
Metric: 9999,
|
|
|
|
Enabled: true,
|
|
|
|
Groups: []string{routeGroup1},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Do not allow to modify existing route with a peer from another route",
|
|
|
|
existingRoute: &route.Route{
|
|
|
|
ID: "testingRoute",
|
2024-06-13 13:24:24 +02:00
|
|
|
Network: existingNetwork,
|
2023-09-28 14:32:36 +02:00
|
|
|
NetID: validNetID,
|
|
|
|
NetworkType: route.IPv4Network,
|
|
|
|
Peer: peer1ID,
|
|
|
|
Description: "super",
|
|
|
|
Masquerade: false,
|
|
|
|
Metric: 9999,
|
|
|
|
Enabled: true,
|
|
|
|
Groups: []string{routeGroup1},
|
|
|
|
},
|
2023-10-09 14:39:41 +02:00
|
|
|
createInitRoute: true,
|
|
|
|
newPeer: &validUsedPeer,
|
|
|
|
errFunc: require.Error,
|
2023-09-28 14:32:36 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Do not allow to modify existing route with a peers group from another route",
|
|
|
|
existingRoute: &route.Route{
|
|
|
|
ID: "testingRoute",
|
2024-06-13 13:24:24 +02:00
|
|
|
Network: existingNetwork,
|
2023-09-28 14:32:36 +02:00
|
|
|
NetID: validNetID,
|
|
|
|
NetworkType: route.IPv4Network,
|
|
|
|
PeerGroups: []string{routeGroup3},
|
|
|
|
Description: "super",
|
|
|
|
Masquerade: false,
|
|
|
|
Metric: 9999,
|
|
|
|
Enabled: true,
|
|
|
|
Groups: []string{routeGroup1},
|
|
|
|
},
|
2023-10-09 14:39:41 +02:00
|
|
|
createInitRoute: true,
|
|
|
|
newPeerGroups: []string{routeGroup4},
|
|
|
|
errFunc: require.Error,
|
2023-09-28 14:32:36 +02:00
|
|
|
},
|
2024-06-13 13:24:24 +02:00
|
|
|
{
|
|
|
|
name: "Allow switching from network route to domains route",
|
|
|
|
existingRoute: &route.Route{
|
|
|
|
ID: "testingRoute",
|
|
|
|
Network: validPrefix,
|
|
|
|
Domains: nil,
|
|
|
|
KeepRoute: false,
|
|
|
|
NetID: validNetID,
|
|
|
|
NetworkType: route.IPv4Network,
|
|
|
|
Peer: peer1ID,
|
|
|
|
Description: "super",
|
|
|
|
Masquerade: false,
|
|
|
|
Metric: 9999,
|
|
|
|
Enabled: true,
|
|
|
|
Groups: []string{routeGroup1},
|
|
|
|
},
|
|
|
|
newPrefix: &netip.Prefix{},
|
|
|
|
newDomains: domain.List{"example.com"},
|
|
|
|
newNetworkType: &domainNetworkType,
|
|
|
|
newKeepRoute: &trueKeepRoute,
|
|
|
|
errFunc: require.NoError,
|
|
|
|
shouldCreate: true,
|
|
|
|
expectedRoute: &route.Route{
|
|
|
|
ID: "testingRoute",
|
|
|
|
Network: placeholderPrefix,
|
|
|
|
NetworkType: route.DomainNetwork,
|
|
|
|
Domains: domain.List{"example.com"},
|
|
|
|
KeepRoute: true,
|
|
|
|
NetID: validNetID,
|
|
|
|
Peer: peer1ID,
|
|
|
|
Description: "super",
|
|
|
|
Masquerade: false,
|
|
|
|
Metric: 9999,
|
|
|
|
Enabled: true,
|
|
|
|
Groups: []string{routeGroup1},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Allow switching from domains route to network route",
|
|
|
|
existingRoute: &route.Route{
|
|
|
|
ID: "testingRoute",
|
|
|
|
Network: placeholderPrefix,
|
|
|
|
Domains: domain.List{"example.com"},
|
|
|
|
KeepRoute: true,
|
|
|
|
NetID: validNetID,
|
|
|
|
NetworkType: route.DomainNetwork,
|
|
|
|
Peer: peer1ID,
|
|
|
|
Description: "super",
|
|
|
|
Masquerade: false,
|
|
|
|
Metric: 9999,
|
|
|
|
Enabled: true,
|
|
|
|
Groups: []string{routeGroup1},
|
|
|
|
},
|
|
|
|
newPrefix: &validPrefix,
|
|
|
|
newDomains: nil,
|
|
|
|
newKeepRoute: &falseKeepRoute,
|
|
|
|
newNetworkType: &ipv4networkType,
|
|
|
|
errFunc: require.NoError,
|
|
|
|
shouldCreate: true,
|
|
|
|
expectedRoute: &route.Route{
|
|
|
|
ID: "testingRoute",
|
|
|
|
Network: validPrefix,
|
|
|
|
NetworkType: route.IPv4Network,
|
|
|
|
KeepRoute: false,
|
|
|
|
Domains: nil,
|
|
|
|
NetID: validNetID,
|
|
|
|
Peer: peer1ID,
|
|
|
|
Description: "super",
|
|
|
|
Masquerade: false,
|
|
|
|
Metric: 9999,
|
|
|
|
Enabled: true,
|
|
|
|
Groups: []string{routeGroup1},
|
|
|
|
},
|
|
|
|
},
|
2022-08-18 18:22:15 +02:00
|
|
|
}
|
|
|
|
for _, testCase := range testCases {
|
|
|
|
t.Run(testCase.name, func(t *testing.T) {
|
|
|
|
am, err := createRouterManager(t)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("failed to create account manager")
|
|
|
|
}
|
|
|
|
|
|
|
|
account, err := initTestRouteAccount(t, am)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("failed to init testing account")
|
|
|
|
}
|
|
|
|
|
2023-10-09 14:39:41 +02:00
|
|
|
if testCase.createInitRoute {
|
|
|
|
account.Routes["initRoute"] = &route.Route{
|
|
|
|
ID: "initRoute",
|
2024-06-13 13:24:24 +02:00
|
|
|
Network: existingNetwork,
|
2023-10-09 14:39:41 +02:00
|
|
|
NetID: existingRouteID,
|
|
|
|
NetworkType: route.IPv4Network,
|
|
|
|
PeerGroups: []string{routeGroup4},
|
|
|
|
Description: "super",
|
|
|
|
Masquerade: false,
|
|
|
|
Metric: 9999,
|
|
|
|
Enabled: true,
|
|
|
|
Groups: []string{routeGroup1},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-18 18:22:15 +02:00
|
|
|
account.Routes[testCase.existingRoute.ID] = testCase.existingRoute
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
err = am.Store.SaveAccount(context.Background(), account)
|
2022-08-18 18:22:15 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Error("account should be saved")
|
|
|
|
}
|
|
|
|
|
|
|
|
var routeToSave *route.Route
|
|
|
|
|
|
|
|
if !testCase.skipCopying {
|
|
|
|
routeToSave = testCase.existingRoute.Copy()
|
|
|
|
if testCase.newPeer != nil {
|
|
|
|
routeToSave.Peer = *testCase.newPeer
|
|
|
|
}
|
2023-09-28 14:32:36 +02:00
|
|
|
if len(testCase.newPeerGroups) != 0 {
|
|
|
|
routeToSave.PeerGroups = testCase.newPeerGroups
|
|
|
|
}
|
2022-08-18 18:22:15 +02:00
|
|
|
if testCase.newMetric != nil {
|
|
|
|
routeToSave.Metric = *testCase.newMetric
|
|
|
|
}
|
|
|
|
|
|
|
|
if testCase.newPrefix != nil {
|
2022-08-22 14:10:24 +02:00
|
|
|
routeToSave.Network = *testCase.newPrefix
|
2022-08-18 18:22:15 +02:00
|
|
|
}
|
2022-12-06 10:11:57 +01:00
|
|
|
|
2024-06-13 13:24:24 +02:00
|
|
|
routeToSave.Domains = testCase.newDomains
|
|
|
|
|
|
|
|
if testCase.newNetworkType != nil {
|
|
|
|
routeToSave.NetworkType = *testCase.newNetworkType
|
|
|
|
}
|
|
|
|
|
|
|
|
if testCase.newKeepRoute != nil {
|
|
|
|
routeToSave.KeepRoute = *testCase.newKeepRoute
|
|
|
|
}
|
|
|
|
|
2022-12-06 10:11:57 +01:00
|
|
|
if testCase.newGroups != nil {
|
|
|
|
routeToSave.Groups = testCase.newGroups
|
|
|
|
}
|
2022-08-18 18:22:15 +02:00
|
|
|
}
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
err = am.SaveRoute(context.Background(), account.Id, userID, routeToSave)
|
2022-08-18 18:22:15 +02:00
|
|
|
|
|
|
|
testCase.errFunc(t, err)
|
|
|
|
|
|
|
|
if !testCase.shouldCreate {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
account, err = am.Store.GetAccount(context.Background(), account.Id)
|
2022-11-07 12:10:56 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2022-08-18 18:22:15 +02:00
|
|
|
savedRoute, saved := account.Routes[testCase.expectedRoute.ID]
|
|
|
|
require.True(t, saved)
|
|
|
|
|
|
|
|
if !testCase.expectedRoute.IsEqual(savedRoute) {
|
|
|
|
t.Errorf("new route didn't match expected route:\nGot %#v\nExpected:%#v\n", savedRoute, testCase.expectedRoute)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDeleteRoute(t *testing.T) {
|
|
|
|
testingRoute := &route.Route{
|
|
|
|
ID: "testingRoute",
|
2022-08-22 14:10:24 +02:00
|
|
|
Network: netip.MustParsePrefix("192.168.0.0/16"),
|
2024-06-13 13:24:24 +02:00
|
|
|
Domains: domain.List{"domain1", "domain2"},
|
|
|
|
KeepRoute: true,
|
2022-08-22 14:10:24 +02:00
|
|
|
NetworkType: route.IPv4Network,
|
2022-08-18 18:22:15 +02:00
|
|
|
Peer: peer1Key,
|
|
|
|
Description: "super",
|
|
|
|
Masquerade: false,
|
|
|
|
Metric: 9999,
|
|
|
|
Enabled: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
am, err := createRouterManager(t)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("failed to create account manager")
|
|
|
|
}
|
|
|
|
|
|
|
|
account, err := initTestRouteAccount(t, am)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("failed to init testing account")
|
|
|
|
}
|
|
|
|
|
|
|
|
account.Routes[testingRoute.ID] = testingRoute
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
err = am.Store.SaveAccount(context.Background(), account)
|
2022-08-18 18:22:15 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Error("failed to save account")
|
|
|
|
}
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
err = am.DeleteRoute(context.Background(), account.Id, testingRoute.ID, userID)
|
2022-08-18 18:22:15 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Error("deleting route failed with error: ", err)
|
|
|
|
}
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
savedAccount, err := am.Store.GetAccount(context.Background(), account.Id)
|
2022-08-18 18:22:15 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Error("failed to retrieve saved account with error: ", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, found := savedAccount.Routes[testingRoute.ID]
|
|
|
|
if found {
|
|
|
|
t.Error("route shouldn't be found after delete")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-28 14:32:36 +02:00
|
|
|
func TestGetNetworkMap_RouteSyncPeerGroups(t *testing.T) {
|
|
|
|
baseRoute := &route.Route{
|
2024-10-02 13:41:00 +02:00
|
|
|
Network: netip.MustParsePrefix("192.168.0.0/16"),
|
|
|
|
NetID: "superNet",
|
|
|
|
NetworkType: route.IPv4Network,
|
|
|
|
PeerGroups: []string{routeGroupHA1, routeGroupHA2},
|
|
|
|
Description: "ha route",
|
|
|
|
Masquerade: false,
|
|
|
|
Metric: 9999,
|
|
|
|
Enabled: true,
|
|
|
|
Groups: []string{routeGroup1, routeGroup2},
|
|
|
|
AccessControlGroups: []string{routeGroup1},
|
2023-09-28 14:32:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
am, err := createRouterManager(t)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("failed to create account manager")
|
|
|
|
}
|
|
|
|
|
|
|
|
account, err := initTestRouteAccount(t, am)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("failed to init testing account")
|
|
|
|
}
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
newAccountRoutes, err := am.GetNetworkMap(context.Background(), peer1ID)
|
2023-09-28 14:32:36 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, newAccountRoutes.Routes, 0, "new accounts should have no routes")
|
|
|
|
|
2024-10-02 13:41:00 +02:00
|
|
|
newRoute, err := am.CreateRoute(context.Background(), account.Id, baseRoute.Network, baseRoute.NetworkType, baseRoute.Domains, baseRoute.Peer, baseRoute.PeerGroups, baseRoute.Description, baseRoute.NetID, baseRoute.Masquerade, baseRoute.Metric, baseRoute.Groups, baseRoute.AccessControlGroups, baseRoute.Enabled, userID, baseRoute.KeepRoute)
|
2023-09-28 14:32:36 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, newRoute.Enabled, true)
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
peer1Routes, err := am.GetNetworkMap(context.Background(), peer1ID)
|
2023-09-28 14:32:36 +02:00
|
|
|
require.NoError(t, err)
|
2023-10-09 14:39:41 +02:00
|
|
|
assert.Len(t, peer1Routes.Routes, 1, "HA route should have 1 server route")
|
2023-09-28 14:32:36 +02:00
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
peer2Routes, err := am.GetNetworkMap(context.Background(), peer2ID)
|
2023-09-28 14:32:36 +02:00
|
|
|
require.NoError(t, err)
|
2023-10-09 14:39:41 +02:00
|
|
|
assert.Len(t, peer2Routes.Routes, 1, "HA route should have 1 server route")
|
2023-09-28 14:32:36 +02:00
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
peer4Routes, err := am.GetNetworkMap(context.Background(), peer4ID)
|
2023-09-28 14:32:36 +02:00
|
|
|
require.NoError(t, err)
|
2023-10-09 14:39:41 +02:00
|
|
|
assert.Len(t, peer4Routes.Routes, 1, "HA route should have 1 server route")
|
2023-09-28 14:32:36 +02:00
|
|
|
|
2024-11-08 16:58:19 +01:00
|
|
|
groups, err := am.Store.GetAccountGroups(context.Background(), LockingStrengthShare, account.Id)
|
2023-09-28 14:32:36 +02:00
|
|
|
require.NoError(t, err)
|
2024-03-27 18:48:48 +01:00
|
|
|
var groupHA1, groupHA2 *nbgroup.Group
|
2023-09-28 14:32:36 +02:00
|
|
|
for _, group := range groups {
|
|
|
|
switch group.Name {
|
|
|
|
case routeGroupHA1:
|
|
|
|
groupHA1 = group
|
|
|
|
case routeGroupHA2:
|
|
|
|
groupHA2 = group
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
err = am.GroupDeletePeer(context.Background(), account.Id, groupHA1.ID, peer2ID)
|
2023-09-28 14:32:36 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
peer2RoutesAfterDelete, err := am.GetNetworkMap(context.Background(), peer2ID)
|
2023-09-28 14:32:36 +02:00
|
|
|
require.NoError(t, err)
|
2023-10-09 14:39:41 +02:00
|
|
|
assert.Len(t, peer2RoutesAfterDelete.Routes, 2, "after peer deletion group should have 2 client routes")
|
2023-09-28 14:32:36 +02:00
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
err = am.GroupDeletePeer(context.Background(), account.Id, groupHA2.ID, peer4ID)
|
2023-09-28 14:32:36 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
peer2RoutesAfterDelete, err = am.GetNetworkMap(context.Background(), peer2ID)
|
2023-09-28 14:32:36 +02:00
|
|
|
require.NoError(t, err)
|
2023-10-09 14:39:41 +02:00
|
|
|
assert.Len(t, peer2RoutesAfterDelete.Routes, 1, "after peer deletion group should have only 1 route")
|
2023-09-28 14:32:36 +02:00
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
err = am.GroupAddPeer(context.Background(), account.Id, groupHA2.ID, peer4ID)
|
2023-09-28 14:32:36 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
peer1RoutesAfterAdd, err := am.GetNetworkMap(context.Background(), peer1ID)
|
2023-09-28 14:32:36 +02:00
|
|
|
require.NoError(t, err)
|
2023-10-09 14:39:41 +02:00
|
|
|
assert.Len(t, peer1RoutesAfterAdd.Routes, 1, "HA route should have more than 1 route")
|
2023-09-28 14:32:36 +02:00
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
peer2RoutesAfterAdd, err := am.GetNetworkMap(context.Background(), peer2ID)
|
2023-09-28 14:32:36 +02:00
|
|
|
require.NoError(t, err)
|
2023-10-09 14:39:41 +02:00
|
|
|
assert.Len(t, peer2RoutesAfterAdd.Routes, 2, "HA route should have 2 client routes")
|
2023-09-28 14:32:36 +02:00
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
err = am.DeleteRoute(context.Background(), account.Id, newRoute.ID, userID)
|
2023-09-28 14:32:36 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
peer1DeletedRoute, err := am.GetNetworkMap(context.Background(), peer1ID)
|
2023-09-28 14:32:36 +02:00
|
|
|
require.NoError(t, err)
|
2023-10-09 14:39:41 +02:00
|
|
|
assert.Len(t, peer1DeletedRoute.Routes, 0, "we should receive one route for peer1")
|
2023-09-28 14:32:36 +02:00
|
|
|
}
|
|
|
|
|
2022-08-18 18:22:15 +02:00
|
|
|
func TestGetNetworkMap_RouteSync(t *testing.T) {
|
|
|
|
// no routes for peer in different groups
|
|
|
|
// no routes when route is deleted
|
|
|
|
baseRoute := &route.Route{
|
2024-10-02 13:41:00 +02:00
|
|
|
ID: "testingRoute",
|
|
|
|
Network: netip.MustParsePrefix("192.168.0.0/16"),
|
|
|
|
NetID: "superNet",
|
|
|
|
NetworkType: route.IPv4Network,
|
|
|
|
Peer: peer1ID,
|
|
|
|
Description: "super",
|
|
|
|
Masquerade: false,
|
|
|
|
Metric: 9999,
|
|
|
|
Enabled: true,
|
|
|
|
Groups: []string{routeGroup1},
|
|
|
|
AccessControlGroups: []string{routeGroup1},
|
2022-08-18 18:22:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
am, err := createRouterManager(t)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("failed to create account manager")
|
|
|
|
}
|
|
|
|
|
|
|
|
account, err := initTestRouteAccount(t, am)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("failed to init testing account")
|
|
|
|
}
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
newAccountRoutes, err := am.GetNetworkMap(context.Background(), peer1ID)
|
2022-08-18 18:22:15 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, newAccountRoutes.Routes, 0, "new accounts should have no routes")
|
|
|
|
|
2024-10-02 13:41:00 +02:00
|
|
|
createdRoute, err := am.CreateRoute(context.Background(), account.Id, baseRoute.Network, baseRoute.NetworkType, baseRoute.Domains, peer1ID, []string{}, baseRoute.Description, baseRoute.NetID, baseRoute.Masquerade, baseRoute.Metric, baseRoute.Groups, baseRoute.AccessControlGroups, false, userID, baseRoute.KeepRoute)
|
2022-08-18 18:22:15 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
noDisabledRoutes, err := am.GetNetworkMap(context.Background(), peer1ID)
|
2022-08-18 18:22:15 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, noDisabledRoutes.Routes, 0, "no routes for disabled routes")
|
|
|
|
|
|
|
|
enabledRoute := createdRoute.Copy()
|
|
|
|
enabledRoute.Enabled = true
|
|
|
|
|
2023-02-03 10:33:28 +01:00
|
|
|
// network map contains route.Route objects that have Route.Peer field filled with Peer.Key instead of Peer.ID
|
|
|
|
expectedRoute := enabledRoute.Copy()
|
|
|
|
expectedRoute.Peer = peer1Key
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
err = am.SaveRoute(context.Background(), account.Id, userID, enabledRoute)
|
2022-08-18 18:22:15 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
peer1Routes, err := am.GetNetworkMap(context.Background(), peer1ID)
|
2022-08-18 18:22:15 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, peer1Routes.Routes, 1, "we should receive one route for peer1")
|
2023-02-03 10:33:28 +01:00
|
|
|
require.True(t, expectedRoute.IsEqual(peer1Routes.Routes[0]), "received route should be equal")
|
2022-08-18 18:22:15 +02:00
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
peer2Routes, err := am.GetNetworkMap(context.Background(), peer2ID)
|
2022-08-18 18:22:15 +02:00
|
|
|
require.NoError(t, err)
|
2022-12-06 10:11:57 +01:00
|
|
|
require.Len(t, peer2Routes.Routes, 0, "no routes for peers not in the distribution group")
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
err = am.GroupAddPeer(context.Background(), account.Id, routeGroup1, peer2ID)
|
2022-12-06 10:11:57 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
peer2Routes, err = am.GetNetworkMap(context.Background(), peer2ID)
|
2022-12-06 10:11:57 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, peer2Routes.Routes, 1, "we should receive one route")
|
2022-08-18 18:22:15 +02:00
|
|
|
require.True(t, peer1Routes.Routes[0].IsEqual(peer2Routes.Routes[0]), "routes should be the same for peers in the same group")
|
|
|
|
|
2024-03-27 18:48:48 +01:00
|
|
|
newGroup := &nbgroup.Group{
|
2022-08-18 18:22:15 +02:00
|
|
|
ID: xid.New().String(),
|
|
|
|
Name: "peer1 group",
|
2023-02-03 10:33:28 +01:00
|
|
|
Peers: []string{peer1ID},
|
2022-08-18 18:22:15 +02:00
|
|
|
}
|
2024-07-03 11:33:02 +02:00
|
|
|
err = am.SaveGroup(context.Background(), account.Id, userID, newGroup)
|
2022-08-18 18:22:15 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
rules, err := am.ListPolicies(context.Background(), account.Id, "testingUser")
|
2022-08-18 18:22:15 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
defaultRule := rules[0]
|
2023-03-13 15:14:18 +01:00
|
|
|
newPolicy := defaultRule.Copy()
|
|
|
|
newPolicy.Name = "peer1 only"
|
|
|
|
newPolicy.Rules[0].Sources = []string{newGroup.ID}
|
|
|
|
newPolicy.Rules[0].Destinations = []string{newGroup.ID}
|
2022-08-18 18:22:15 +02:00
|
|
|
|
2024-11-12 09:18:16 +01:00
|
|
|
_, err = am.SavePolicy(context.Background(), account.Id, userID, newPolicy)
|
2022-08-18 18:22:15 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
err = am.DeletePolicy(context.Background(), account.Id, defaultRule.ID, userID)
|
2022-08-18 18:22:15 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
peer1GroupRoutes, err := am.GetNetworkMap(context.Background(), peer1ID)
|
2022-08-18 18:22:15 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, peer1GroupRoutes.Routes, 1, "we should receive one route for peer1")
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
peer2GroupRoutes, err := am.GetNetworkMap(context.Background(), peer2ID)
|
2022-08-18 18:22:15 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, peer2GroupRoutes.Routes, 0, "we should not receive routes for peer2")
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
err = am.DeleteRoute(context.Background(), account.Id, enabledRoute.ID, userID)
|
2022-08-18 18:22:15 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
peer1DeletedRoute, err := am.GetNetworkMap(context.Background(), peer1ID)
|
2022-08-18 18:22:15 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, peer1DeletedRoute.Routes, 0, "we should receive one route for peer1")
|
|
|
|
}
|
|
|
|
|
|
|
|
func createRouterManager(t *testing.T) (*DefaultAccountManager, error) {
|
2023-11-10 16:33:13 +01:00
|
|
|
t.Helper()
|
2022-08-18 18:22:15 +02:00
|
|
|
store, err := createRouterStore(t)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-01-02 15:11:32 +01:00
|
|
|
eventStore := &activity.InMemoryEventStore{}
|
2024-08-07 10:52:31 +02:00
|
|
|
|
|
|
|
metrics, err := telemetry.NewDefaultAppMetrics(context.Background())
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
return BuildManager(context.Background(), store, NewPeersUpdateManager(nil), nil, "", "netbird.selfhosted", eventStore, nil, false, MocIntegratedValidator{}, metrics)
|
2022-08-18 18:22:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func createRouterStore(t *testing.T) (Store, error) {
|
2023-11-10 16:33:13 +01:00
|
|
|
t.Helper()
|
2022-08-18 18:22:15 +02:00
|
|
|
dataDir := t.TempDir()
|
2024-10-10 12:35:03 +02:00
|
|
|
store, cleanUp, err := NewTestStoreFromSQL(context.Background(), "", dataDir)
|
2022-08-18 18:22:15 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-05-16 18:28:37 +02:00
|
|
|
t.Cleanup(cleanUp)
|
2022-08-18 18:22:15 +02:00
|
|
|
|
|
|
|
return store, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func initTestRouteAccount(t *testing.T, am *DefaultAccountManager) (*Account, error) {
|
2023-09-28 14:32:36 +02:00
|
|
|
t.Helper()
|
|
|
|
|
2023-02-03 10:33:28 +01:00
|
|
|
accountID := "testingAcc"
|
|
|
|
domain := "example.com"
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
account := newAccountWithId(context.Background(), accountID, userID, domain)
|
|
|
|
err := am.Store.SaveAccount(context.Background(), account)
|
2023-02-03 10:33:28 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ips := account.getTakenIPs()
|
|
|
|
peer1IP, err := AllocatePeerIP(account.Network.Net, ips)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-11-28 13:45:26 +01:00
|
|
|
peer1 := &nbpeer.Peer{
|
2024-09-06 16:28:19 +02:00
|
|
|
IP: peer1IP,
|
|
|
|
ID: peer1ID,
|
|
|
|
Key: peer1Key,
|
|
|
|
Name: "test-host1@netbird.io",
|
|
|
|
DNSLabel: "test-host1",
|
|
|
|
UserID: userID,
|
2023-11-28 13:45:26 +01:00
|
|
|
Meta: nbpeer.PeerSystemMeta{
|
2022-08-18 18:22:15 +02:00
|
|
|
Hostname: "test-host1@netbird.io",
|
|
|
|
GoOS: "linux",
|
|
|
|
Kernel: "Linux",
|
|
|
|
Core: "21.04",
|
|
|
|
Platform: "x86_64",
|
|
|
|
OS: "Ubuntu",
|
|
|
|
WtVersion: "development",
|
|
|
|
UIVersion: "development",
|
|
|
|
},
|
2023-12-06 15:02:10 +01:00
|
|
|
Status: &nbpeer.PeerStatus{},
|
2022-08-18 18:22:15 +02:00
|
|
|
}
|
2023-02-03 10:33:28 +01:00
|
|
|
account.Peers[peer1.ID] = peer1
|
|
|
|
|
|
|
|
ips = account.getTakenIPs()
|
|
|
|
peer2IP, err := AllocatePeerIP(account.Network.Net, ips)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-11-28 13:45:26 +01:00
|
|
|
peer2 := &nbpeer.Peer{
|
2024-09-06 16:28:19 +02:00
|
|
|
IP: peer2IP,
|
|
|
|
ID: peer2ID,
|
|
|
|
Key: peer2Key,
|
|
|
|
Name: "test-host2@netbird.io",
|
|
|
|
DNSLabel: "test-host2",
|
|
|
|
UserID: userID,
|
2023-11-28 13:45:26 +01:00
|
|
|
Meta: nbpeer.PeerSystemMeta{
|
2022-08-18 18:22:15 +02:00
|
|
|
Hostname: "test-host2@netbird.io",
|
|
|
|
GoOS: "linux",
|
|
|
|
Kernel: "Linux",
|
|
|
|
Core: "21.04",
|
|
|
|
Platform: "x86_64",
|
|
|
|
OS: "Ubuntu",
|
|
|
|
WtVersion: "development",
|
|
|
|
UIVersion: "development",
|
|
|
|
},
|
2023-12-06 15:02:10 +01:00
|
|
|
Status: &nbpeer.PeerStatus{},
|
2022-08-18 18:22:15 +02:00
|
|
|
}
|
2023-02-03 10:33:28 +01:00
|
|
|
account.Peers[peer2.ID] = peer2
|
2022-08-18 18:22:15 +02:00
|
|
|
|
2023-09-28 14:32:36 +02:00
|
|
|
ips = account.getTakenIPs()
|
|
|
|
peer3IP, err := AllocatePeerIP(account.Network.Net, ips)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-11-28 13:45:26 +01:00
|
|
|
peer3 := &nbpeer.Peer{
|
2024-09-06 16:28:19 +02:00
|
|
|
IP: peer3IP,
|
|
|
|
ID: peer3ID,
|
|
|
|
Key: peer3Key,
|
|
|
|
Name: "test-host3@netbird.io",
|
|
|
|
DNSLabel: "test-host3",
|
|
|
|
UserID: userID,
|
2023-11-28 13:45:26 +01:00
|
|
|
Meta: nbpeer.PeerSystemMeta{
|
2023-09-28 14:32:36 +02:00
|
|
|
Hostname: "test-host3@netbird.io",
|
|
|
|
GoOS: "darwin",
|
|
|
|
Kernel: "Darwin",
|
|
|
|
Core: "13.4.1",
|
|
|
|
Platform: "arm64",
|
|
|
|
OS: "darwin",
|
|
|
|
WtVersion: "development",
|
|
|
|
UIVersion: "development",
|
|
|
|
},
|
2023-12-06 15:02:10 +01:00
|
|
|
Status: &nbpeer.PeerStatus{},
|
2023-09-28 14:32:36 +02:00
|
|
|
}
|
|
|
|
account.Peers[peer3.ID] = peer3
|
|
|
|
|
|
|
|
ips = account.getTakenIPs()
|
|
|
|
peer4IP, err := AllocatePeerIP(account.Network.Net, ips)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-11-28 13:45:26 +01:00
|
|
|
peer4 := &nbpeer.Peer{
|
2024-09-06 16:28:19 +02:00
|
|
|
IP: peer4IP,
|
|
|
|
ID: peer4ID,
|
|
|
|
Key: peer4Key,
|
|
|
|
Name: "test-host4@netbird.io",
|
|
|
|
DNSLabel: "test-host4",
|
|
|
|
UserID: userID,
|
2023-11-28 13:45:26 +01:00
|
|
|
Meta: nbpeer.PeerSystemMeta{
|
2023-09-28 14:32:36 +02:00
|
|
|
Hostname: "test-host4@netbird.io",
|
|
|
|
GoOS: "linux",
|
|
|
|
Kernel: "Linux",
|
|
|
|
Core: "21.04",
|
|
|
|
Platform: "x86_64",
|
|
|
|
OS: "Ubuntu",
|
|
|
|
WtVersion: "development",
|
|
|
|
UIVersion: "development",
|
|
|
|
},
|
2023-12-06 15:02:10 +01:00
|
|
|
Status: &nbpeer.PeerStatus{},
|
2023-09-28 14:32:36 +02:00
|
|
|
}
|
|
|
|
account.Peers[peer4.ID] = peer4
|
|
|
|
|
|
|
|
ips = account.getTakenIPs()
|
|
|
|
peer5IP, err := AllocatePeerIP(account.Network.Net, ips)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-11-28 13:45:26 +01:00
|
|
|
peer5 := &nbpeer.Peer{
|
2024-09-06 16:28:19 +02:00
|
|
|
IP: peer5IP,
|
|
|
|
ID: peer5ID,
|
|
|
|
Key: peer5Key,
|
|
|
|
Name: "test-host5@netbird.io",
|
|
|
|
DNSLabel: "test-host5",
|
|
|
|
UserID: userID,
|
2023-11-28 13:45:26 +01:00
|
|
|
Meta: nbpeer.PeerSystemMeta{
|
2024-09-06 16:28:19 +02:00
|
|
|
Hostname: "test-host5@netbird.io",
|
2023-09-28 14:32:36 +02:00
|
|
|
GoOS: "linux",
|
|
|
|
Kernel: "Linux",
|
|
|
|
Core: "21.04",
|
|
|
|
Platform: "x86_64",
|
|
|
|
OS: "Ubuntu",
|
|
|
|
WtVersion: "development",
|
|
|
|
UIVersion: "development",
|
|
|
|
},
|
2023-12-06 15:02:10 +01:00
|
|
|
Status: &nbpeer.PeerStatus{},
|
2023-09-28 14:32:36 +02:00
|
|
|
}
|
|
|
|
account.Peers[peer5.ID] = peer5
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
err = am.Store.SaveAccount(context.Background(), account)
|
2022-08-18 18:22:15 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-02-03 10:33:28 +01:00
|
|
|
groupAll, err := account.GetGroupAll()
|
2022-08-18 18:22:15 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-07-03 11:33:02 +02:00
|
|
|
err = am.GroupAddPeer(context.Background(), accountID, groupAll.ID, peer1ID)
|
2022-08-18 18:22:15 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-07-03 11:33:02 +02:00
|
|
|
err = am.GroupAddPeer(context.Background(), accountID, groupAll.ID, peer2ID)
|
2023-02-03 10:33:28 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-07-03 11:33:02 +02:00
|
|
|
err = am.GroupAddPeer(context.Background(), accountID, groupAll.ID, peer3ID)
|
2023-09-28 14:32:36 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2022-12-06 10:11:57 +01:00
|
|
|
}
|
2024-07-03 11:33:02 +02:00
|
|
|
err = am.GroupAddPeer(context.Background(), accountID, groupAll.ID, peer4ID)
|
2022-12-06 10:11:57 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-03-27 18:48:48 +01:00
|
|
|
newGroup := []*nbgroup.Group{
|
2023-09-28 14:32:36 +02:00
|
|
|
{
|
|
|
|
ID: routeGroup1,
|
|
|
|
Name: routeGroup1,
|
|
|
|
Peers: []string{peer1.ID},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ID: routeGroup2,
|
|
|
|
Name: routeGroup2,
|
|
|
|
Peers: []string{peer2.ID},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ID: routeGroup3,
|
|
|
|
Name: routeGroup3,
|
|
|
|
Peers: []string{peer5.ID},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ID: routeGroup4,
|
|
|
|
Name: routeGroup4,
|
|
|
|
Peers: []string{peer5.ID},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ID: routeGroupHA1,
|
|
|
|
Name: routeGroupHA1,
|
|
|
|
Peers: []string{peer1.ID, peer2.ID, peer3.ID}, // we have one non Linux peer, see peer3
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ID: routeGroupHA2,
|
|
|
|
Name: routeGroupHA2,
|
|
|
|
Peers: []string{peer1.ID, peer4.ID},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, group := range newGroup {
|
2024-07-03 11:33:02 +02:00
|
|
|
err = am.SaveGroup(context.Background(), accountID, userID, group)
|
2023-09-28 14:32:36 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-12-06 10:11:57 +01:00
|
|
|
}
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
return am.Store.GetAccount(context.Background(), account.Id)
|
2022-08-18 18:22:15 +02:00
|
|
|
}
|
2024-10-02 13:41:00 +02:00
|
|
|
|
|
|
|
func TestAccount_getPeersRoutesFirewall(t *testing.T) {
|
|
|
|
var (
|
|
|
|
peerBIp = "100.65.80.39"
|
|
|
|
peerCIp = "100.65.254.139"
|
|
|
|
peerHIp = "100.65.29.55"
|
|
|
|
)
|
|
|
|
|
|
|
|
account := &Account{
|
|
|
|
Peers: map[string]*nbpeer.Peer{
|
|
|
|
"peerA": {
|
|
|
|
ID: "peerA",
|
|
|
|
IP: net.ParseIP("100.65.14.88"),
|
|
|
|
Status: &nbpeer.PeerStatus{},
|
|
|
|
Meta: nbpeer.PeerSystemMeta{
|
|
|
|
GoOS: "linux",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"peerB": {
|
|
|
|
ID: "peerB",
|
|
|
|
IP: net.ParseIP(peerBIp),
|
|
|
|
Status: &nbpeer.PeerStatus{},
|
|
|
|
Meta: nbpeer.PeerSystemMeta{},
|
|
|
|
},
|
|
|
|
"peerC": {
|
|
|
|
ID: "peerC",
|
|
|
|
IP: net.ParseIP(peerCIp),
|
|
|
|
Status: &nbpeer.PeerStatus{},
|
|
|
|
},
|
|
|
|
"peerD": {
|
|
|
|
ID: "peerD",
|
|
|
|
IP: net.ParseIP("100.65.62.5"),
|
|
|
|
Status: &nbpeer.PeerStatus{},
|
|
|
|
Meta: nbpeer.PeerSystemMeta{
|
|
|
|
GoOS: "linux",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"peerE": {
|
|
|
|
ID: "peerE",
|
|
|
|
IP: net.ParseIP("100.65.32.206"),
|
|
|
|
Key: peer1Key,
|
|
|
|
Status: &nbpeer.PeerStatus{},
|
|
|
|
Meta: nbpeer.PeerSystemMeta{
|
|
|
|
GoOS: "linux",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"peerF": {
|
|
|
|
ID: "peerF",
|
|
|
|
IP: net.ParseIP("100.65.250.202"),
|
|
|
|
Status: &nbpeer.PeerStatus{},
|
|
|
|
},
|
|
|
|
"peerG": {
|
|
|
|
ID: "peerG",
|
|
|
|
IP: net.ParseIP("100.65.13.186"),
|
|
|
|
Status: &nbpeer.PeerStatus{},
|
|
|
|
},
|
|
|
|
"peerH": {
|
|
|
|
ID: "peerH",
|
|
|
|
IP: net.ParseIP(peerHIp),
|
|
|
|
Status: &nbpeer.PeerStatus{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Groups: map[string]*nbgroup.Group{
|
|
|
|
"routingPeer1": {
|
|
|
|
ID: "routingPeer1",
|
|
|
|
Name: "RoutingPeer1",
|
|
|
|
Peers: []string{
|
|
|
|
"peerA",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"routingPeer2": {
|
|
|
|
ID: "routingPeer2",
|
|
|
|
Name: "RoutingPeer2",
|
|
|
|
Peers: []string{
|
|
|
|
"peerD",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"route1": {
|
|
|
|
ID: "route1",
|
|
|
|
Name: "Route1",
|
|
|
|
Peers: []string{},
|
|
|
|
},
|
|
|
|
"route2": {
|
|
|
|
ID: "route2",
|
|
|
|
Name: "Route2",
|
|
|
|
Peers: []string{},
|
|
|
|
},
|
|
|
|
"finance": {
|
|
|
|
ID: "finance",
|
|
|
|
Name: "Finance",
|
|
|
|
Peers: []string{
|
|
|
|
"peerF",
|
|
|
|
"peerG",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"dev": {
|
|
|
|
ID: "dev",
|
|
|
|
Name: "Dev",
|
|
|
|
Peers: []string{
|
|
|
|
"peerC",
|
|
|
|
"peerH",
|
|
|
|
"peerB",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"contractors": {
|
|
|
|
ID: "contractors",
|
|
|
|
Name: "Contractors",
|
|
|
|
Peers: []string{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Routes: map[route.ID]*route.Route{
|
|
|
|
"route1": {
|
|
|
|
ID: "route1",
|
|
|
|
Network: netip.MustParsePrefix("192.168.0.0/16"),
|
|
|
|
NetID: "route1",
|
|
|
|
NetworkType: route.IPv4Network,
|
|
|
|
PeerGroups: []string{"routingPeer1", "routingPeer2"},
|
|
|
|
Description: "Route1 ha route",
|
|
|
|
Masquerade: false,
|
|
|
|
Metric: 9999,
|
|
|
|
Enabled: true,
|
|
|
|
Groups: []string{"dev"},
|
|
|
|
AccessControlGroups: []string{"route1"},
|
|
|
|
},
|
|
|
|
"route2": {
|
|
|
|
ID: "route2",
|
|
|
|
Network: existingNetwork,
|
|
|
|
NetID: "route2",
|
|
|
|
NetworkType: route.IPv4Network,
|
|
|
|
Peer: "peerE",
|
|
|
|
Description: "Allow",
|
|
|
|
Masquerade: false,
|
|
|
|
Metric: 9999,
|
|
|
|
Enabled: true,
|
|
|
|
Groups: []string{"finance"},
|
|
|
|
AccessControlGroups: []string{"route2"},
|
|
|
|
},
|
|
|
|
"route3": {
|
|
|
|
ID: "route3",
|
|
|
|
Network: netip.MustParsePrefix("192.0.2.0/32"),
|
|
|
|
Domains: domain.List{"example.com"},
|
|
|
|
NetID: "route3",
|
|
|
|
NetworkType: route.DomainNetwork,
|
|
|
|
Peer: "peerE",
|
|
|
|
Description: "Allow all traffic to routed DNS network",
|
|
|
|
Masquerade: false,
|
|
|
|
Metric: 9999,
|
|
|
|
Enabled: true,
|
|
|
|
Groups: []string{"contractors"},
|
|
|
|
AccessControlGroups: []string{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Policies: []*Policy{
|
|
|
|
{
|
|
|
|
ID: "RuleRoute1",
|
|
|
|
Name: "Route1",
|
|
|
|
Enabled: true,
|
|
|
|
Rules: []*PolicyRule{
|
|
|
|
{
|
|
|
|
ID: "RuleRoute1",
|
|
|
|
Name: "ruleRoute1",
|
|
|
|
Bidirectional: true,
|
|
|
|
Enabled: true,
|
|
|
|
Protocol: PolicyRuleProtocolALL,
|
|
|
|
Action: PolicyTrafficActionAccept,
|
|
|
|
Ports: []string{"80", "320"},
|
|
|
|
Sources: []string{
|
|
|
|
"dev",
|
|
|
|
},
|
|
|
|
Destinations: []string{
|
|
|
|
"route1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ID: "RuleRoute2",
|
|
|
|
Name: "Route2",
|
|
|
|
Enabled: true,
|
|
|
|
Rules: []*PolicyRule{
|
|
|
|
{
|
|
|
|
ID: "RuleRoute2",
|
|
|
|
Name: "ruleRoute2",
|
|
|
|
Bidirectional: true,
|
|
|
|
Enabled: true,
|
|
|
|
Protocol: PolicyRuleProtocolTCP,
|
|
|
|
Action: PolicyTrafficActionAccept,
|
|
|
|
PortRanges: []RulePortRange{
|
|
|
|
{
|
|
|
|
Start: 80,
|
|
|
|
End: 350,
|
|
|
|
}, {
|
|
|
|
Start: 80,
|
|
|
|
End: 350,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Sources: []string{
|
|
|
|
"finance",
|
|
|
|
},
|
|
|
|
Destinations: []string{
|
|
|
|
"route2",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
validatedPeers := make(map[string]struct{})
|
|
|
|
for p := range account.Peers {
|
|
|
|
validatedPeers[p] = struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("check applied policies for the route", func(t *testing.T) {
|
|
|
|
route1 := account.Routes["route1"]
|
|
|
|
policies := getAllRoutePoliciesFromGroups(account, route1.AccessControlGroups)
|
|
|
|
assert.Len(t, policies, 1)
|
|
|
|
|
|
|
|
route2 := account.Routes["route2"]
|
|
|
|
policies = getAllRoutePoliciesFromGroups(account, route2.AccessControlGroups)
|
|
|
|
assert.Len(t, policies, 1)
|
|
|
|
|
|
|
|
route3 := account.Routes["route3"]
|
|
|
|
policies = getAllRoutePoliciesFromGroups(account, route3.AccessControlGroups)
|
|
|
|
assert.Len(t, policies, 0)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("check peer routes firewall rules", func(t *testing.T) {
|
|
|
|
routesFirewallRules := account.getPeerRoutesFirewallRules(context.Background(), "peerA", validatedPeers)
|
|
|
|
assert.Len(t, routesFirewallRules, 2)
|
|
|
|
|
|
|
|
expectedRoutesFirewallRules := []*RouteFirewallRule{
|
|
|
|
{
|
|
|
|
SourceRanges: []string{
|
|
|
|
fmt.Sprintf(AllowedIPsFormat, peerCIp),
|
|
|
|
fmt.Sprintf(AllowedIPsFormat, peerHIp),
|
|
|
|
fmt.Sprintf(AllowedIPsFormat, peerBIp),
|
|
|
|
},
|
|
|
|
Action: "accept",
|
|
|
|
Destination: "192.168.0.0/16",
|
|
|
|
Protocol: "all",
|
|
|
|
Port: 80,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
SourceRanges: []string{
|
|
|
|
fmt.Sprintf(AllowedIPsFormat, peerCIp),
|
|
|
|
fmt.Sprintf(AllowedIPsFormat, peerHIp),
|
|
|
|
fmt.Sprintf(AllowedIPsFormat, peerBIp),
|
|
|
|
},
|
|
|
|
Action: "accept",
|
|
|
|
Destination: "192.168.0.0/16",
|
|
|
|
Protocol: "all",
|
|
|
|
Port: 320,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
assert.ElementsMatch(t, routesFirewallRules, expectedRoutesFirewallRules)
|
|
|
|
|
2024-10-03 15:50:35 +02:00
|
|
|
// peerD is also the routing peer for route1, should contain same routes firewall rules as peerA
|
2024-10-02 13:41:00 +02:00
|
|
|
routesFirewallRules = account.getPeerRoutesFirewallRules(context.Background(), "peerD", validatedPeers)
|
|
|
|
assert.Len(t, routesFirewallRules, 2)
|
|
|
|
assert.ElementsMatch(t, routesFirewallRules, expectedRoutesFirewallRules)
|
|
|
|
|
|
|
|
// peerE is a single routing peer for route 2 and route 3
|
|
|
|
routesFirewallRules = account.getPeerRoutesFirewallRules(context.Background(), "peerE", validatedPeers)
|
|
|
|
assert.Len(t, routesFirewallRules, 3)
|
|
|
|
|
|
|
|
expectedRoutesFirewallRules = []*RouteFirewallRule{
|
|
|
|
{
|
|
|
|
SourceRanges: []string{"100.65.250.202/32", "100.65.13.186/32"},
|
|
|
|
Action: "accept",
|
|
|
|
Destination: existingNetwork.String(),
|
|
|
|
Protocol: "tcp",
|
|
|
|
PortRange: RulePortRange{Start: 80, End: 350},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
SourceRanges: []string{"0.0.0.0/0"},
|
|
|
|
Action: "accept",
|
|
|
|
Destination: "192.0.2.0/32",
|
|
|
|
Protocol: "all",
|
|
|
|
IsDynamic: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
SourceRanges: []string{"::/0"},
|
|
|
|
Action: "accept",
|
|
|
|
Destination: "192.0.2.0/32",
|
|
|
|
Protocol: "all",
|
|
|
|
IsDynamic: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
assert.ElementsMatch(t, routesFirewallRules, expectedRoutesFirewallRules)
|
|
|
|
|
|
|
|
// peerC is part of route1 distribution groups but should not receive the routes firewall rules
|
|
|
|
routesFirewallRules = account.getPeerRoutesFirewallRules(context.Background(), "peerC", validatedPeers)
|
|
|
|
assert.Len(t, routesFirewallRules, 0)
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
2024-10-23 12:05:02 +02:00
|
|
|
|
|
|
|
func TestRouteAccountPeersUpdate(t *testing.T) {
|
|
|
|
manager, err := createRouterManager(t)
|
|
|
|
require.NoError(t, err, "failed to create account manager")
|
|
|
|
|
|
|
|
account, err := initTestRouteAccount(t, manager)
|
|
|
|
require.NoError(t, err, "failed to init testing account")
|
|
|
|
|
|
|
|
err = manager.SaveGroups(context.Background(), account.Id, userID, []*nbgroup.Group{
|
|
|
|
{
|
|
|
|
ID: "groupA",
|
|
|
|
Name: "GroupA",
|
|
|
|
Peers: []string{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ID: "groupB",
|
|
|
|
Name: "GroupB",
|
|
|
|
Peers: []string{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ID: "groupC",
|
|
|
|
Name: "GroupC",
|
|
|
|
Peers: []string{},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
updMsg := manager.peersUpdateManager.CreateChannel(context.Background(), peer1ID)
|
|
|
|
t.Cleanup(func() {
|
|
|
|
manager.peersUpdateManager.CloseChannel(context.Background(), peer1ID)
|
|
|
|
})
|
|
|
|
|
|
|
|
// Creating a route with no routing peer and no peers in PeerGroups or Groups should not update account peers and not send peer update
|
|
|
|
t.Run("creating route no routing peer and no peers in groups", func(t *testing.T) {
|
|
|
|
route := route.Route{
|
|
|
|
ID: "testingRoute1",
|
|
|
|
Network: netip.MustParsePrefix("100.65.250.202/32"),
|
|
|
|
NetID: "superNet",
|
|
|
|
NetworkType: route.IPv4Network,
|
|
|
|
PeerGroups: []string{"groupA"},
|
|
|
|
Description: "super",
|
|
|
|
Masquerade: false,
|
|
|
|
Metric: 9999,
|
|
|
|
Enabled: true,
|
|
|
|
Groups: []string{"groupA"},
|
|
|
|
}
|
|
|
|
|
|
|
|
done := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
peerShouldNotReceiveUpdate(t, updMsg)
|
|
|
|
close(done)
|
|
|
|
}()
|
|
|
|
|
|
|
|
_, err := manager.CreateRoute(
|
|
|
|
context.Background(), account.Id, route.Network, route.NetworkType, route.Domains, route.Peer,
|
|
|
|
route.PeerGroups, route.Description, route.NetID, route.Masquerade, route.Metric,
|
|
|
|
route.Groups, []string{}, true, userID, route.KeepRoute,
|
|
|
|
)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-done:
|
|
|
|
case <-time.After(time.Second):
|
|
|
|
t.Error("timeout waiting for peerShouldNotReceiveUpdate")
|
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
// Creating a route with no routing peer and having peers in groups should update account peers and send peer update
|
|
|
|
t.Run("creating a route with peers in PeerGroups and Groups", func(t *testing.T) {
|
|
|
|
route := route.Route{
|
|
|
|
ID: "testingRoute2",
|
|
|
|
Network: netip.MustParsePrefix("192.0.2.0/32"),
|
|
|
|
NetID: "superNet",
|
|
|
|
NetworkType: route.IPv4Network,
|
|
|
|
PeerGroups: []string{routeGroup3},
|
|
|
|
Description: "super",
|
|
|
|
Masquerade: false,
|
|
|
|
Metric: 9999,
|
|
|
|
Enabled: true,
|
|
|
|
Groups: []string{routeGroup3},
|
|
|
|
}
|
|
|
|
|
|
|
|
done := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
peerShouldReceiveUpdate(t, updMsg)
|
|
|
|
close(done)
|
|
|
|
}()
|
|
|
|
|
|
|
|
_, err := manager.CreateRoute(
|
|
|
|
context.Background(), account.Id, route.Network, route.NetworkType, route.Domains, route.Peer,
|
|
|
|
route.PeerGroups, route.Description, route.NetID, route.Masquerade, route.Metric,
|
|
|
|
route.Groups, []string{}, true, userID, route.KeepRoute,
|
|
|
|
)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-done:
|
|
|
|
case <-time.After(time.Second):
|
|
|
|
t.Error("timeout waiting for peerShouldReceiveUpdate")
|
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
baseRoute := route.Route{
|
|
|
|
ID: "testingRoute3",
|
|
|
|
Network: netip.MustParsePrefix("192.168.0.0/16"),
|
|
|
|
NetID: "superNet",
|
|
|
|
NetworkType: route.IPv4Network,
|
|
|
|
Peer: peer1ID,
|
|
|
|
Description: "super",
|
|
|
|
Masquerade: false,
|
|
|
|
Metric: 9999,
|
|
|
|
Enabled: true,
|
|
|
|
Groups: []string{routeGroup1},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Creating route should update account peers and send peer update
|
|
|
|
t.Run("creating route with a routing peer", func(t *testing.T) {
|
|
|
|
done := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
peerShouldReceiveUpdate(t, updMsg)
|
|
|
|
close(done)
|
|
|
|
}()
|
|
|
|
|
|
|
|
newRoute, err := manager.CreateRoute(
|
|
|
|
context.Background(), account.Id, baseRoute.Network, baseRoute.NetworkType, baseRoute.Domains, baseRoute.Peer,
|
|
|
|
baseRoute.PeerGroups, baseRoute.Description, baseRoute.NetID, baseRoute.Masquerade, baseRoute.Metric,
|
|
|
|
baseRoute.Groups, []string{}, true, userID, baseRoute.KeepRoute,
|
|
|
|
)
|
|
|
|
require.NoError(t, err)
|
|
|
|
baseRoute = *newRoute
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-done:
|
|
|
|
case <-time.After(time.Second):
|
|
|
|
t.Error("timeout waiting for peerShouldReceiveUpdate")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
// Updating the route should update account peers and send peer update when there is peers in group
|
|
|
|
t.Run("updating route", func(t *testing.T) {
|
|
|
|
baseRoute.Groups = []string{routeGroup1, routeGroup2}
|
|
|
|
|
|
|
|
done := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
peerShouldReceiveUpdate(t, updMsg)
|
|
|
|
close(done)
|
|
|
|
}()
|
|
|
|
|
|
|
|
err := manager.SaveRoute(context.Background(), account.Id, userID, &baseRoute)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-done:
|
|
|
|
case <-time.After(time.Second):
|
|
|
|
t.Error("timeout waiting for peerShouldReceiveUpdate")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
// Deleting the route should update account peers and send peer update
|
|
|
|
t.Run("deleting route", func(t *testing.T) {
|
|
|
|
done := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
peerShouldReceiveUpdate(t, updMsg)
|
|
|
|
close(done)
|
|
|
|
}()
|
|
|
|
|
|
|
|
err := manager.DeleteRoute(context.Background(), account.Id, baseRoute.ID, userID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-done:
|
|
|
|
case <-time.After(time.Second):
|
|
|
|
t.Error("timeout waiting for peerShouldReceiveUpdate")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
// Adding peer to route peer groups that do not have any peers should update account peers and send peer update
|
|
|
|
t.Run("adding peer to route peer groups that do not have any peers", func(t *testing.T) {
|
|
|
|
newRoute := route.Route{
|
|
|
|
Network: netip.MustParsePrefix("192.168.12.0/16"),
|
|
|
|
NetID: "superNet",
|
|
|
|
NetworkType: route.IPv4Network,
|
|
|
|
PeerGroups: []string{"groupB"},
|
|
|
|
Description: "super",
|
|
|
|
Masquerade: false,
|
|
|
|
Metric: 9999,
|
|
|
|
Enabled: true,
|
|
|
|
Groups: []string{routeGroup1},
|
|
|
|
}
|
|
|
|
_, err := manager.CreateRoute(
|
|
|
|
context.Background(), account.Id, newRoute.Network, newRoute.NetworkType, newRoute.Domains, newRoute.Peer,
|
|
|
|
newRoute.PeerGroups, newRoute.Description, newRoute.NetID, newRoute.Masquerade, newRoute.Metric,
|
|
|
|
newRoute.Groups, []string{}, true, userID, newRoute.KeepRoute,
|
|
|
|
)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
done := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
peerShouldReceiveUpdate(t, updMsg)
|
|
|
|
close(done)
|
|
|
|
}()
|
|
|
|
|
|
|
|
err = manager.SaveGroup(context.Background(), account.Id, userID, &nbgroup.Group{
|
|
|
|
ID: "groupB",
|
|
|
|
Name: "GroupB",
|
|
|
|
Peers: []string{peer1ID},
|
|
|
|
})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-done:
|
|
|
|
case <-time.After(time.Second):
|
|
|
|
t.Error("timeout waiting for peerShouldReceiveUpdate")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
// Adding peer to route groups that do not have any peers should update account peers and send peer update
|
|
|
|
t.Run("adding peer to route groups that do not have any peers", func(t *testing.T) {
|
|
|
|
newRoute := route.Route{
|
|
|
|
Network: netip.MustParsePrefix("192.168.13.0/16"),
|
|
|
|
NetID: "superNet",
|
|
|
|
NetworkType: route.IPv4Network,
|
|
|
|
PeerGroups: []string{"groupB"},
|
|
|
|
Description: "super",
|
|
|
|
Masquerade: false,
|
|
|
|
Metric: 9999,
|
|
|
|
Enabled: true,
|
|
|
|
Groups: []string{"groupC"},
|
|
|
|
}
|
|
|
|
_, err := manager.CreateRoute(
|
|
|
|
context.Background(), account.Id, newRoute.Network, newRoute.NetworkType, newRoute.Domains, newRoute.Peer,
|
|
|
|
newRoute.PeerGroups, newRoute.Description, newRoute.NetID, newRoute.Masquerade, newRoute.Metric,
|
|
|
|
newRoute.Groups, []string{}, true, userID, newRoute.KeepRoute,
|
|
|
|
)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
done := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
peerShouldReceiveUpdate(t, updMsg)
|
|
|
|
close(done)
|
|
|
|
}()
|
|
|
|
|
|
|
|
err = manager.SaveGroup(context.Background(), account.Id, userID, &nbgroup.Group{
|
|
|
|
ID: "groupC",
|
|
|
|
Name: "GroupC",
|
|
|
|
Peers: []string{peer1ID},
|
|
|
|
})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-done:
|
|
|
|
case <-time.After(time.Second):
|
|
|
|
t.Error("timeout waiting for peerShouldReceiveUpdate")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|