2022-01-10 18:43:13 +01:00
|
|
|
package internal
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2022-02-08 18:03:27 +01:00
|
|
|
"net"
|
2022-09-05 09:06:35 +02:00
|
|
|
"net/netip"
|
2022-02-08 18:03:27 +01:00
|
|
|
"os"
|
|
|
|
"runtime"
|
2022-06-04 19:41:01 +02:00
|
|
|
"strings"
|
2022-02-08 18:03:27 +01:00
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2024-08-27 19:21:14 +02:00
|
|
|
"github.com/google/uuid"
|
2023-12-20 23:02:42 +01:00
|
|
|
"github.com/pion/transport/v3/stdnet"
|
2023-03-03 19:49:18 +01:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
2024-06-13 01:20:46 +02:00
|
|
|
"go.opentelemetry.io/otel"
|
2023-03-03 19:49:18 +01:00
|
|
|
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
|
|
|
|
"google.golang.org/grpc"
|
|
|
|
"google.golang.org/grpc/keepalive"
|
|
|
|
|
2024-03-27 18:48:48 +01:00
|
|
|
"github.com/netbirdio/management-integrations/integrations"
|
2024-04-23 14:42:53 +02:00
|
|
|
|
2024-10-02 18:24:22 +02:00
|
|
|
"github.com/netbirdio/netbird/client/iface"
|
|
|
|
"github.com/netbirdio/netbird/client/iface/bind"
|
|
|
|
"github.com/netbirdio/netbird/client/iface/device"
|
2023-03-03 19:49:18 +01:00
|
|
|
"github.com/netbirdio/netbird/client/internal/dns"
|
|
|
|
"github.com/netbirdio/netbird/client/internal/peer"
|
2024-10-24 11:43:14 +02:00
|
|
|
"github.com/netbirdio/netbird/client/internal/peer/guard"
|
|
|
|
icemaker "github.com/netbirdio/netbird/client/internal/peer/ice"
|
2023-03-03 19:49:18 +01:00
|
|
|
"github.com/netbirdio/netbird/client/internal/routemanager"
|
|
|
|
"github.com/netbirdio/netbird/client/ssh"
|
2022-03-26 12:08:54 +01:00
|
|
|
"github.com/netbirdio/netbird/client/system"
|
2023-03-03 19:49:18 +01:00
|
|
|
nbdns "github.com/netbirdio/netbird/dns"
|
2022-03-26 12:08:54 +01:00
|
|
|
mgmt "github.com/netbirdio/netbird/management/client"
|
|
|
|
mgmtProto "github.com/netbirdio/netbird/management/proto"
|
|
|
|
"github.com/netbirdio/netbird/management/server"
|
2023-03-03 19:49:18 +01:00
|
|
|
"github.com/netbirdio/netbird/management/server/activity"
|
2024-08-07 10:52:31 +02:00
|
|
|
"github.com/netbirdio/netbird/management/server/telemetry"
|
2024-09-08 12:06:14 +02:00
|
|
|
relayClient "github.com/netbirdio/netbird/relay/client"
|
2023-03-03 19:49:18 +01:00
|
|
|
"github.com/netbirdio/netbird/route"
|
2022-03-26 12:08:54 +01:00
|
|
|
signal "github.com/netbirdio/netbird/signal/client"
|
|
|
|
"github.com/netbirdio/netbird/signal/proto"
|
|
|
|
signalServer "github.com/netbirdio/netbird/signal/server"
|
|
|
|
"github.com/netbirdio/netbird/util"
|
2022-01-10 18:43:13 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
kaep = keepalive.EnforcementPolicy{
|
|
|
|
MinTime: 15 * time.Second,
|
|
|
|
PermitWithoutStream: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
kasp = keepalive.ServerParameters{
|
|
|
|
MaxConnectionIdle: 15 * time.Second,
|
|
|
|
MaxConnectionAgeGrace: 5 * time.Second,
|
|
|
|
Time: 5 * time.Second,
|
|
|
|
Timeout: 2 * time.Second,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2024-09-08 12:06:14 +02:00
|
|
|
func TestMain(m *testing.M) {
|
|
|
|
_ = util.InitLog("debug", "console")
|
|
|
|
code := m.Run()
|
|
|
|
os.Exit(code)
|
|
|
|
}
|
|
|
|
|
2022-06-23 17:04:53 +02:00
|
|
|
func TestEngine_SSH(t *testing.T) {
|
2024-06-14 11:17:55 +02:00
|
|
|
// todo resolve test execution on freebsd
|
|
|
|
if runtime.GOOS == "windows" || runtime.GOOS == "freebsd" {
|
|
|
|
t.Skip("skipping TestEngine_SSH")
|
2022-06-23 17:04:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
key, err := wgtypes.GeneratePrivateKey()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
2024-09-08 12:06:14 +02:00
|
|
|
relayMgr := relayClient.NewManager(ctx, nil, key.PublicKey().String())
|
|
|
|
engine := NewEngine(
|
|
|
|
ctx, cancel,
|
|
|
|
&signal.MockClient{},
|
|
|
|
&mgmt.MockClient{},
|
|
|
|
relayMgr,
|
|
|
|
&EngineConfig{
|
|
|
|
WgIfaceName: "utun101",
|
|
|
|
WgAddr: "100.64.0.1/24",
|
|
|
|
WgPrivateKey: key,
|
|
|
|
WgPort: 33100,
|
|
|
|
ServerSSHAllowed: true,
|
|
|
|
},
|
|
|
|
MobileDependency{},
|
|
|
|
peer.NewRecorder("https://mgm"),
|
|
|
|
nil,
|
|
|
|
)
|
2022-06-23 17:04:53 +02:00
|
|
|
|
2022-11-29 11:49:18 +01:00
|
|
|
engine.dnsServer = &dns.MockServer{
|
|
|
|
UpdateDNSServerFunc: func(serial uint64, update nbdns.Config) error { return nil },
|
|
|
|
}
|
|
|
|
|
2022-06-23 17:04:53 +02:00
|
|
|
var sshKeysAdded []string
|
|
|
|
var sshPeersRemoved []string
|
|
|
|
|
|
|
|
sshCtx, cancel := context.WithCancel(context.Background())
|
|
|
|
|
|
|
|
engine.sshServerFunc = func(hostKeyPEM []byte, addr string) (ssh.Server, error) {
|
|
|
|
return &ssh.MockServer{
|
|
|
|
Ctx: sshCtx,
|
|
|
|
StopFunc: func() error {
|
|
|
|
cancel()
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
StartFunc: func() error {
|
|
|
|
<-ctx.Done()
|
|
|
|
return ctx.Err()
|
|
|
|
},
|
|
|
|
AddAuthorizedKeyFunc: func(peer, newKey string) error {
|
|
|
|
sshKeysAdded = append(sshKeysAdded, newKey)
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
RemoveAuthorizedKeyFunc: func(peer string) {
|
|
|
|
sshPeersRemoved = append(sshPeersRemoved, peer)
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
err = engine.Start()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
err := engine.Stop()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
peerWithSSH := &mgmtProto.RemotePeerConfig{
|
|
|
|
WgPubKey: "MNHf3Ma6z6mdLbriAJbqhX7+nM/B71lgw2+91q3LfhU=",
|
|
|
|
AllowedIps: []string{"100.64.0.21/24"},
|
|
|
|
SshConfig: &mgmtProto.SSHConfig{
|
|
|
|
SshPubKey: []byte("ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIFATYCqaQw/9id1Qkq3n16JYhDhXraI6Pc1fgB8ynEfQ"),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// SSH server is not enabled so SSH config of a remote peer should be ignored
|
|
|
|
networkMap := &mgmtProto.NetworkMap{
|
|
|
|
Serial: 6,
|
|
|
|
PeerConfig: nil,
|
|
|
|
RemotePeers: []*mgmtProto.RemotePeerConfig{peerWithSSH},
|
|
|
|
RemotePeersIsEmpty: false,
|
|
|
|
}
|
|
|
|
|
|
|
|
err = engine.updateNetworkMap(networkMap)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Nil(t, engine.sshServer)
|
|
|
|
|
|
|
|
// SSH server is enabled, therefore SSH config should be applied
|
|
|
|
networkMap = &mgmtProto.NetworkMap{
|
|
|
|
Serial: 7,
|
|
|
|
PeerConfig: &mgmtProto.PeerConfig{Address: "100.64.0.1/24",
|
|
|
|
SshConfig: &mgmtProto.SSHConfig{SshEnabled: true}},
|
|
|
|
RemotePeers: []*mgmtProto.RemotePeerConfig{peerWithSSH},
|
|
|
|
RemotePeersIsEmpty: false,
|
|
|
|
}
|
|
|
|
|
|
|
|
err = engine.updateNetworkMap(networkMap)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
time.Sleep(250 * time.Millisecond)
|
|
|
|
assert.NotNil(t, engine.sshServer)
|
|
|
|
assert.Contains(t, sshKeysAdded, "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIFATYCqaQw/9id1Qkq3n16JYhDhXraI6Pc1fgB8ynEfQ")
|
|
|
|
|
|
|
|
// now remove peer
|
|
|
|
networkMap = &mgmtProto.NetworkMap{
|
|
|
|
Serial: 8,
|
|
|
|
RemotePeers: []*mgmtProto.RemotePeerConfig{},
|
|
|
|
RemotePeersIsEmpty: false,
|
|
|
|
}
|
|
|
|
|
|
|
|
err = engine.updateNetworkMap(networkMap)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
// time.Sleep(250 * time.Millisecond)
|
2022-06-23 17:04:53 +02:00
|
|
|
assert.NotNil(t, engine.sshServer)
|
|
|
|
assert.Contains(t, sshPeersRemoved, "MNHf3Ma6z6mdLbriAJbqhX7+nM/B71lgw2+91q3LfhU=")
|
|
|
|
|
|
|
|
// now disable SSH server
|
|
|
|
networkMap = &mgmtProto.NetworkMap{
|
|
|
|
Serial: 9,
|
|
|
|
PeerConfig: &mgmtProto.PeerConfig{Address: "100.64.0.1/24",
|
|
|
|
SshConfig: &mgmtProto.SSHConfig{SshEnabled: false}},
|
|
|
|
RemotePeers: []*mgmtProto.RemotePeerConfig{peerWithSSH},
|
|
|
|
RemotePeersIsEmpty: false,
|
|
|
|
}
|
|
|
|
|
|
|
|
err = engine.updateNetworkMap(networkMap)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Nil(t, engine.sshServer)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-01-18 16:44:58 +01:00
|
|
|
func TestEngine_UpdateNetworkMap(t *testing.T) {
|
|
|
|
// test setup
|
|
|
|
key, err := wgtypes.GeneratePrivateKey()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
2024-09-08 12:06:14 +02:00
|
|
|
relayMgr := relayClient.NewManager(ctx, nil, key.PublicKey().String())
|
|
|
|
engine := NewEngine(
|
|
|
|
ctx, cancel,
|
|
|
|
&signal.MockClient{},
|
|
|
|
&mgmt.MockClient{},
|
|
|
|
relayMgr,
|
|
|
|
&EngineConfig{
|
|
|
|
WgIfaceName: "utun102",
|
|
|
|
WgAddr: "100.64.0.1/24",
|
|
|
|
WgPrivateKey: key,
|
|
|
|
WgPort: 33100,
|
|
|
|
},
|
|
|
|
MobileDependency{},
|
|
|
|
peer.NewRecorder("https://mgm"),
|
|
|
|
nil)
|
2024-08-29 21:31:19 +02:00
|
|
|
|
|
|
|
wgIface := &iface.MockWGIface{
|
|
|
|
RemovePeerFunc: func(peerKey string) error {
|
|
|
|
return nil
|
|
|
|
},
|
2023-04-13 17:00:01 +02:00
|
|
|
}
|
2024-08-29 21:31:19 +02:00
|
|
|
engine.wgInterface = wgIface
|
2024-09-08 12:06:14 +02:00
|
|
|
engine.routeManager = routemanager.NewManager(ctx, key.PublicKey().String(), time.Minute, engine.wgInterface, engine.statusRecorder, relayMgr, nil)
|
2022-11-23 13:39:42 +01:00
|
|
|
engine.dnsServer = &dns.MockServer{
|
|
|
|
UpdateDNSServerFunc: func(serial uint64, update nbdns.Config) error { return nil },
|
|
|
|
}
|
2023-04-13 17:00:01 +02:00
|
|
|
conn, err := net.ListenUDP("udp4", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
engine.udpMux = bind.NewUniversalUDPMuxDefault(bind.UniversalUDPMuxParams{UDPConn: conn})
|
2024-05-22 11:15:29 +02:00
|
|
|
engine.ctx = ctx
|
2024-10-24 11:43:14 +02:00
|
|
|
engine.srWatcher = guard.NewSRWatcher(nil, nil, nil, icemaker.Config{})
|
2022-01-18 16:44:58 +01:00
|
|
|
|
|
|
|
type testCase struct {
|
2022-01-18 17:52:55 +01:00
|
|
|
name string
|
|
|
|
networkMap *mgmtProto.NetworkMap
|
|
|
|
|
2022-01-18 16:44:58 +01:00
|
|
|
expectedLen int
|
2022-06-04 19:41:01 +02:00
|
|
|
expectedPeers []*mgmtProto.RemotePeerConfig
|
2022-01-18 16:44:58 +01:00
|
|
|
expectedSerial uint64
|
|
|
|
}
|
|
|
|
|
|
|
|
peer1 := &mgmtProto.RemotePeerConfig{
|
|
|
|
WgPubKey: "RRHf3Ma6z6mdLbriAJbqhX7+nM/B71lgw2+91q3LfhU=",
|
|
|
|
AllowedIps: []string{"100.64.0.10/24"},
|
|
|
|
}
|
|
|
|
|
|
|
|
peer2 := &mgmtProto.RemotePeerConfig{
|
|
|
|
WgPubKey: "LLHf3Ma6z6mdLbriAJbqhX7+nM/B71lgw2+91q3LfhU=",
|
|
|
|
AllowedIps: []string{"100.64.0.11/24"},
|
|
|
|
}
|
|
|
|
|
|
|
|
peer3 := &mgmtProto.RemotePeerConfig{
|
|
|
|
WgPubKey: "GGHf3Ma6z6mdLbriAJbqhX7+nM/B71lgw2+91q3LfhU=",
|
|
|
|
AllowedIps: []string{"100.64.0.12/24"},
|
|
|
|
}
|
|
|
|
|
2022-06-04 19:41:01 +02:00
|
|
|
modifiedPeer3 := &mgmtProto.RemotePeerConfig{
|
|
|
|
WgPubKey: "GGHf3Ma6z6mdLbriAJbqhX7+nM/B71lgw2+91q3LfhU=",
|
|
|
|
AllowedIps: []string{"100.64.0.20/24"},
|
|
|
|
}
|
|
|
|
|
2022-01-18 16:44:58 +01:00
|
|
|
case1 := testCase{
|
2022-01-18 17:52:55 +01:00
|
|
|
name: "input with a new peer to add",
|
2022-01-18 16:44:58 +01:00
|
|
|
networkMap: &mgmtProto.NetworkMap{
|
|
|
|
Serial: 1,
|
|
|
|
PeerConfig: nil,
|
|
|
|
RemotePeers: []*mgmtProto.RemotePeerConfig{
|
|
|
|
peer1,
|
|
|
|
},
|
|
|
|
RemotePeersIsEmpty: false,
|
|
|
|
},
|
|
|
|
expectedLen: 1,
|
2022-06-04 19:41:01 +02:00
|
|
|
expectedPeers: []*mgmtProto.RemotePeerConfig{peer1},
|
2022-01-18 16:44:58 +01:00
|
|
|
expectedSerial: 1,
|
|
|
|
}
|
|
|
|
|
2022-03-10 18:18:38 +01:00
|
|
|
// 2nd case - one extra peer added and network map has CurrentSerial grater than local => apply the update
|
2022-01-18 16:44:58 +01:00
|
|
|
case2 := testCase{
|
2022-01-18 17:52:55 +01:00
|
|
|
name: "input with an old peer and a new peer to add",
|
2022-01-18 16:44:58 +01:00
|
|
|
networkMap: &mgmtProto.NetworkMap{
|
|
|
|
Serial: 2,
|
|
|
|
PeerConfig: nil,
|
|
|
|
RemotePeers: []*mgmtProto.RemotePeerConfig{
|
|
|
|
peer1, peer2,
|
|
|
|
},
|
|
|
|
RemotePeersIsEmpty: false,
|
|
|
|
},
|
|
|
|
expectedLen: 2,
|
2022-06-04 19:41:01 +02:00
|
|
|
expectedPeers: []*mgmtProto.RemotePeerConfig{peer1, peer2},
|
2022-01-18 16:44:58 +01:00
|
|
|
expectedSerial: 2,
|
|
|
|
}
|
|
|
|
|
|
|
|
case3 := testCase{
|
2022-01-18 17:52:55 +01:00
|
|
|
name: "input with outdated (old) update to ignore",
|
2022-01-18 16:44:58 +01:00
|
|
|
networkMap: &mgmtProto.NetworkMap{
|
|
|
|
Serial: 0,
|
|
|
|
PeerConfig: nil,
|
|
|
|
RemotePeers: []*mgmtProto.RemotePeerConfig{
|
|
|
|
peer1, peer2, peer3,
|
|
|
|
},
|
|
|
|
RemotePeersIsEmpty: false,
|
|
|
|
},
|
|
|
|
expectedLen: 2,
|
2022-06-04 19:41:01 +02:00
|
|
|
expectedPeers: []*mgmtProto.RemotePeerConfig{peer1, peer2},
|
2022-01-18 16:44:58 +01:00
|
|
|
expectedSerial: 2,
|
|
|
|
}
|
|
|
|
|
|
|
|
case4 := testCase{
|
2022-01-18 17:52:55 +01:00
|
|
|
name: "input with one peer to remove and one new to add",
|
2022-01-18 16:44:58 +01:00
|
|
|
networkMap: &mgmtProto.NetworkMap{
|
|
|
|
Serial: 4,
|
|
|
|
PeerConfig: nil,
|
|
|
|
RemotePeers: []*mgmtProto.RemotePeerConfig{
|
|
|
|
peer2, peer3,
|
|
|
|
},
|
|
|
|
RemotePeersIsEmpty: false,
|
|
|
|
},
|
|
|
|
expectedLen: 2,
|
2022-06-04 19:41:01 +02:00
|
|
|
expectedPeers: []*mgmtProto.RemotePeerConfig{peer2, peer3},
|
2022-01-18 16:44:58 +01:00
|
|
|
expectedSerial: 4,
|
|
|
|
}
|
|
|
|
|
|
|
|
case5 := testCase{
|
2022-06-04 19:41:01 +02:00
|
|
|
name: "input with one peer to modify",
|
|
|
|
networkMap: &mgmtProto.NetworkMap{
|
|
|
|
Serial: 4,
|
|
|
|
PeerConfig: nil,
|
|
|
|
RemotePeers: []*mgmtProto.RemotePeerConfig{
|
|
|
|
modifiedPeer3, peer2,
|
|
|
|
},
|
|
|
|
RemotePeersIsEmpty: false,
|
|
|
|
},
|
|
|
|
expectedLen: 2,
|
|
|
|
expectedPeers: []*mgmtProto.RemotePeerConfig{peer2, modifiedPeer3},
|
|
|
|
expectedSerial: 4,
|
|
|
|
}
|
|
|
|
|
|
|
|
case6 := testCase{
|
2022-01-18 17:52:55 +01:00
|
|
|
name: "input with all peers to remove",
|
2022-01-18 16:44:58 +01:00
|
|
|
networkMap: &mgmtProto.NetworkMap{
|
|
|
|
Serial: 5,
|
|
|
|
PeerConfig: nil,
|
|
|
|
RemotePeers: []*mgmtProto.RemotePeerConfig{},
|
|
|
|
RemotePeersIsEmpty: true,
|
|
|
|
},
|
|
|
|
expectedLen: 0,
|
|
|
|
expectedPeers: nil,
|
|
|
|
expectedSerial: 5,
|
|
|
|
}
|
|
|
|
|
2022-06-04 19:41:01 +02:00
|
|
|
for _, c := range []testCase{case1, case2, case3, case4, case5, case6} {
|
2022-01-18 17:52:55 +01:00
|
|
|
t.Run(c.name, func(t *testing.T) {
|
|
|
|
err = engine.updateNetworkMap(c.networkMap)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
return
|
2022-01-18 16:44:58 +01:00
|
|
|
}
|
2022-01-18 17:52:55 +01:00
|
|
|
|
|
|
|
if len(engine.peerConns) != c.expectedLen {
|
|
|
|
t.Errorf("expecting Engine.peerConns to be of size %d, got %d", c.expectedLen, len(engine.peerConns))
|
|
|
|
}
|
|
|
|
|
|
|
|
if engine.networkSerial != c.expectedSerial {
|
|
|
|
t.Errorf("expecting Engine.networkSerial to be equal to %d, actual %d", c.expectedSerial, engine.networkSerial)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, p := range c.expectedPeers {
|
2022-06-04 19:41:01 +02:00
|
|
|
conn, ok := engine.peerConns[p.GetWgPubKey()]
|
|
|
|
if !ok {
|
2022-01-18 17:52:55 +01:00
|
|
|
t.Errorf("expecting Engine.peerConns to contain peer %s", p)
|
|
|
|
}
|
2022-06-04 19:41:01 +02:00
|
|
|
expectedAllowedIPs := strings.Join(p.AllowedIps, ",")
|
2023-07-26 14:00:47 +02:00
|
|
|
if conn.WgConfig().AllowedIps != expectedAllowedIPs {
|
2022-06-04 19:41:01 +02:00
|
|
|
t.Errorf("expecting peer %s to have AllowedIPs= %s, got %s", p.GetWgPubKey(),
|
2023-07-26 14:00:47 +02:00
|
|
|
expectedAllowedIPs, conn.WgConfig().AllowedIps)
|
2022-06-04 19:41:01 +02:00
|
|
|
}
|
2022-01-18 17:52:55 +01:00
|
|
|
}
|
|
|
|
})
|
2022-01-18 16:44:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEngine_Sync(t *testing.T) {
|
|
|
|
key, err := wgtypes.GeneratePrivateKey()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
// feed updates to Engine via mocked Management client
|
|
|
|
updates := make(chan *mgmtProto.SyncResponse)
|
|
|
|
defer close(updates)
|
2024-06-13 13:24:24 +02:00
|
|
|
syncFunc := func(ctx context.Context, info *system.Info, msgHandler func(msg *mgmtProto.SyncResponse) error) error {
|
2022-01-18 16:44:58 +01:00
|
|
|
for msg := range updates {
|
|
|
|
err := msgHandler(msg)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2024-09-08 12:06:14 +02:00
|
|
|
relayMgr := relayClient.NewManager(ctx, nil, key.PublicKey().String())
|
|
|
|
engine := NewEngine(ctx, cancel, &signal.MockClient{}, &mgmt.MockClient{SyncFunc: syncFunc}, relayMgr, &EngineConfig{
|
2022-06-23 17:04:53 +02:00
|
|
|
WgIfaceName: "utun103",
|
2022-01-18 16:44:58 +01:00
|
|
|
WgAddr: "100.64.0.1/24",
|
|
|
|
WgPrivateKey: key,
|
|
|
|
WgPort: 33100,
|
2024-06-13 13:24:24 +02:00
|
|
|
}, MobileDependency{}, peer.NewRecorder("https://mgm"), nil)
|
2024-05-22 11:15:29 +02:00
|
|
|
engine.ctx = ctx
|
2022-01-18 16:44:58 +01:00
|
|
|
|
2022-11-29 11:49:18 +01:00
|
|
|
engine.dnsServer = &dns.MockServer{
|
|
|
|
UpdateDNSServerFunc: func(serial uint64, update nbdns.Config) error { return nil },
|
|
|
|
}
|
|
|
|
|
2022-01-18 16:44:58 +01:00
|
|
|
defer func() {
|
|
|
|
err := engine.Stop()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
err = engine.Start()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
peer1 := &mgmtProto.RemotePeerConfig{
|
|
|
|
WgPubKey: "RRHf3Ma6z6mdLbriAJbqhX7+nM/B71lgw2+91q3LfhU=",
|
|
|
|
AllowedIps: []string{"100.64.0.10/24"},
|
|
|
|
}
|
|
|
|
peer2 := &mgmtProto.RemotePeerConfig{
|
|
|
|
WgPubKey: "LLHf3Ma6z6mdLbriAJbqhX9+nM/B71lgw2+91q3LlhU=",
|
|
|
|
AllowedIps: []string{"100.64.0.11/24"},
|
|
|
|
}
|
|
|
|
peer3 := &mgmtProto.RemotePeerConfig{
|
|
|
|
WgPubKey: "GGHf3Ma6z6mdLbriAJbqhX9+nM/B71lgw2+91q3LlhU=",
|
|
|
|
AllowedIps: []string{"100.64.0.12/24"},
|
|
|
|
}
|
|
|
|
// 1st update with just 1 peer and serial larger than the current serial of the engine => apply update
|
|
|
|
updates <- &mgmtProto.SyncResponse{
|
|
|
|
NetworkMap: &mgmtProto.NetworkMap{
|
|
|
|
Serial: 10,
|
|
|
|
PeerConfig: nil,
|
|
|
|
RemotePeers: []*mgmtProto.RemotePeerConfig{peer1, peer2, peer3},
|
|
|
|
RemotePeersIsEmpty: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
timeout := time.After(time.Second * 2)
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-timeout:
|
|
|
|
t.Fatalf("timeout while waiting for test to finish")
|
2022-02-16 20:00:21 +01:00
|
|
|
return
|
2022-01-18 16:44:58 +01:00
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
2023-03-03 19:49:18 +01:00
|
|
|
if getPeers(engine) == 3 && engine.networkSerial == 10 {
|
2022-01-18 16:44:58 +01:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-05 09:06:35 +02:00
|
|
|
func TestEngine_UpdateNetworkMapWithRoutes(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
inputErr error
|
|
|
|
networkMap *mgmtProto.NetworkMap
|
|
|
|
expectedLen int
|
|
|
|
expectedRoutes []*route.Route
|
|
|
|
expectedSerial uint64
|
|
|
|
}{
|
|
|
|
{
|
2022-11-07 15:38:21 +01:00
|
|
|
name: "Routes Config Should Be Passed To Manager",
|
2022-09-05 09:06:35 +02:00
|
|
|
networkMap: &mgmtProto.NetworkMap{
|
|
|
|
Serial: 1,
|
|
|
|
PeerConfig: nil,
|
|
|
|
RemotePeersIsEmpty: false,
|
|
|
|
Routes: []*mgmtProto.Route{
|
|
|
|
{
|
|
|
|
ID: "a",
|
|
|
|
Network: "192.168.0.0/24",
|
|
|
|
NetID: "n1",
|
|
|
|
Peer: "p1",
|
|
|
|
NetworkType: 1,
|
|
|
|
Masquerade: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ID: "b",
|
|
|
|
Network: "192.168.1.0/24",
|
|
|
|
NetID: "n2",
|
|
|
|
Peer: "p1",
|
|
|
|
NetworkType: 1,
|
|
|
|
Masquerade: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedLen: 2,
|
|
|
|
expectedRoutes: []*route.Route{
|
|
|
|
{
|
|
|
|
ID: "a",
|
|
|
|
Network: netip.MustParsePrefix("192.168.0.0/24"),
|
|
|
|
NetID: "n1",
|
|
|
|
Peer: "p1",
|
|
|
|
NetworkType: 1,
|
|
|
|
Masquerade: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ID: "b",
|
|
|
|
Network: netip.MustParsePrefix("192.168.1.0/24"),
|
|
|
|
NetID: "n2",
|
|
|
|
Peer: "p1",
|
|
|
|
NetworkType: 1,
|
|
|
|
Masquerade: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedSerial: 1,
|
|
|
|
},
|
|
|
|
{
|
2022-11-07 15:38:21 +01:00
|
|
|
name: "Empty Routes Config Should Be Passed",
|
2022-09-05 09:06:35 +02:00
|
|
|
networkMap: &mgmtProto.NetworkMap{
|
|
|
|
Serial: 1,
|
|
|
|
PeerConfig: nil,
|
|
|
|
RemotePeersIsEmpty: false,
|
|
|
|
Routes: nil,
|
|
|
|
},
|
|
|
|
expectedLen: 0,
|
|
|
|
expectedRoutes: []*route.Route{},
|
|
|
|
expectedSerial: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Error Shouldn't Break Engine",
|
|
|
|
inputErr: fmt.Errorf("mocking error"),
|
|
|
|
networkMap: &mgmtProto.NetworkMap{
|
|
|
|
Serial: 1,
|
|
|
|
PeerConfig: nil,
|
|
|
|
RemotePeersIsEmpty: false,
|
|
|
|
Routes: nil,
|
|
|
|
},
|
|
|
|
expectedLen: 0,
|
|
|
|
expectedRoutes: []*route.Route{},
|
|
|
|
expectedSerial: 1,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for n, testCase := range testCases {
|
|
|
|
t.Run(testCase.name, func(t *testing.T) {
|
|
|
|
// test setup
|
|
|
|
key, err := wgtypes.GeneratePrivateKey()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
wgIfaceName := fmt.Sprintf("utun%d", 104+n)
|
|
|
|
wgAddr := fmt.Sprintf("100.66.%d.1/24", n)
|
|
|
|
|
2024-09-08 12:06:14 +02:00
|
|
|
relayMgr := relayClient.NewManager(ctx, nil, key.PublicKey().String())
|
|
|
|
engine := NewEngine(ctx, cancel, &signal.MockClient{}, &mgmt.MockClient{}, relayMgr, &EngineConfig{
|
2022-09-05 09:06:35 +02:00
|
|
|
WgIfaceName: wgIfaceName,
|
|
|
|
WgAddr: wgAddr,
|
|
|
|
WgPrivateKey: key,
|
|
|
|
WgPort: 33100,
|
2024-06-13 13:24:24 +02:00
|
|
|
}, MobileDependency{}, peer.NewRecorder("https://mgm"), nil)
|
2024-05-22 11:15:29 +02:00
|
|
|
engine.ctx = ctx
|
2023-04-13 17:00:01 +02:00
|
|
|
newNet, err := stdnet.NewNet()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2024-10-22 20:53:14 +02:00
|
|
|
|
|
|
|
opts := iface.WGIFaceOpts{
|
|
|
|
IFaceName: wgIfaceName,
|
|
|
|
Address: wgAddr,
|
|
|
|
WGPort: engine.config.WgPort,
|
|
|
|
WGPrivKey: key.String(),
|
|
|
|
MTU: iface.DefaultMTU,
|
|
|
|
TransportNet: newNet,
|
|
|
|
}
|
|
|
|
engine.wgInterface, err = iface.NewWGIFace(opts)
|
2022-09-05 09:06:35 +02:00
|
|
|
assert.NoError(t, err, "shouldn't return error")
|
|
|
|
input := struct {
|
|
|
|
inputSerial uint64
|
|
|
|
inputRoutes []*route.Route
|
|
|
|
}{}
|
|
|
|
|
|
|
|
mockRouteManager := &routemanager.MockManager{
|
2024-05-06 14:47:49 +02:00
|
|
|
UpdateRoutesFunc: func(updateSerial uint64, newRoutes []*route.Route) (map[route.ID]*route.Route, route.HAMap, error) {
|
2022-09-05 09:06:35 +02:00
|
|
|
input.inputSerial = updateSerial
|
|
|
|
input.inputRoutes = newRoutes
|
2024-04-23 14:42:53 +02:00
|
|
|
return nil, nil, testCase.inputErr
|
2022-09-05 09:06:35 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
engine.routeManager = mockRouteManager
|
2022-11-23 13:39:42 +01:00
|
|
|
engine.dnsServer = &dns.MockServer{}
|
2022-09-05 09:06:35 +02:00
|
|
|
|
|
|
|
defer func() {
|
|
|
|
exitErr := engine.Stop()
|
|
|
|
if exitErr != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
err = engine.updateNetworkMap(testCase.networkMap)
|
|
|
|
assert.NoError(t, err, "shouldn't return error")
|
|
|
|
assert.Equal(t, testCase.expectedSerial, input.inputSerial, "serial should match")
|
2024-04-23 14:42:53 +02:00
|
|
|
assert.Len(t, input.inputRoutes, testCase.expectedLen, "clientRoutes len should match")
|
|
|
|
assert.Equal(t, testCase.expectedRoutes, input.inputRoutes, "clientRoutes should match")
|
2022-09-05 09:06:35 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-07 15:38:21 +01:00
|
|
|
func TestEngine_UpdateNetworkMapWithDNSUpdate(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
inputErr error
|
|
|
|
networkMap *mgmtProto.NetworkMap
|
|
|
|
expectedZonesLen int
|
|
|
|
expectedZones []nbdns.CustomZone
|
|
|
|
expectedNSGroupsLen int
|
|
|
|
expectedNSGroups []*nbdns.NameServerGroup
|
|
|
|
expectedSerial uint64
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "DNS Config Should Be Passed To DNS Server",
|
|
|
|
networkMap: &mgmtProto.NetworkMap{
|
|
|
|
Serial: 1,
|
|
|
|
PeerConfig: nil,
|
|
|
|
RemotePeersIsEmpty: false,
|
|
|
|
Routes: nil,
|
|
|
|
DNSConfig: &mgmtProto.DNSConfig{
|
|
|
|
ServiceEnable: true,
|
|
|
|
CustomZones: []*mgmtProto.CustomZone{
|
|
|
|
{
|
|
|
|
Domain: "netbird.cloud.",
|
|
|
|
Records: []*mgmtProto.SimpleRecord{
|
|
|
|
{
|
|
|
|
Name: "peer-a.netbird.cloud.",
|
|
|
|
Type: 1,
|
|
|
|
Class: nbdns.DefaultClass,
|
|
|
|
TTL: 300,
|
|
|
|
RData: "100.64.0.1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
NameServerGroups: []*mgmtProto.NameServerGroup{
|
|
|
|
{
|
|
|
|
Primary: true,
|
|
|
|
NameServers: []*mgmtProto.NameServer{
|
|
|
|
{
|
|
|
|
IP: "8.8.8.8",
|
|
|
|
NSType: 1,
|
|
|
|
Port: 53,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedZonesLen: 1,
|
|
|
|
expectedZones: []nbdns.CustomZone{
|
|
|
|
{
|
|
|
|
Domain: "netbird.cloud.",
|
|
|
|
Records: []nbdns.SimpleRecord{
|
|
|
|
{
|
|
|
|
Name: "peer-a.netbird.cloud.",
|
|
|
|
Type: 1,
|
|
|
|
Class: nbdns.DefaultClass,
|
|
|
|
TTL: 300,
|
|
|
|
RData: "100.64.0.1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedNSGroupsLen: 1,
|
|
|
|
expectedNSGroups: []*nbdns.NameServerGroup{
|
|
|
|
{
|
|
|
|
Primary: true,
|
|
|
|
NameServers: []nbdns.NameServer{
|
|
|
|
{
|
|
|
|
IP: netip.MustParseAddr("8.8.8.8"),
|
|
|
|
NSType: 1,
|
|
|
|
Port: 53,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedSerial: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Empty DNS Config Should Be OK",
|
|
|
|
networkMap: &mgmtProto.NetworkMap{
|
|
|
|
Serial: 1,
|
|
|
|
PeerConfig: nil,
|
|
|
|
RemotePeersIsEmpty: false,
|
|
|
|
Routes: nil,
|
|
|
|
DNSConfig: nil,
|
|
|
|
},
|
|
|
|
expectedZonesLen: 0,
|
|
|
|
expectedZones: []nbdns.CustomZone{},
|
|
|
|
expectedNSGroupsLen: 0,
|
|
|
|
expectedNSGroups: []*nbdns.NameServerGroup{},
|
|
|
|
expectedSerial: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Error Shouldn't Break Engine",
|
|
|
|
inputErr: fmt.Errorf("mocking error"),
|
|
|
|
networkMap: &mgmtProto.NetworkMap{
|
|
|
|
Serial: 1,
|
|
|
|
PeerConfig: nil,
|
|
|
|
RemotePeersIsEmpty: false,
|
|
|
|
Routes: nil,
|
|
|
|
},
|
|
|
|
expectedZonesLen: 0,
|
|
|
|
expectedZones: []nbdns.CustomZone{},
|
|
|
|
expectedNSGroupsLen: 0,
|
|
|
|
expectedNSGroups: []*nbdns.NameServerGroup{},
|
|
|
|
expectedSerial: 1,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for n, testCase := range testCases {
|
|
|
|
t.Run(testCase.name, func(t *testing.T) {
|
|
|
|
// test setup
|
|
|
|
key, err := wgtypes.GeneratePrivateKey()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
wgIfaceName := fmt.Sprintf("utun%d", 104+n)
|
|
|
|
wgAddr := fmt.Sprintf("100.66.%d.1/24", n)
|
|
|
|
|
2024-09-08 12:06:14 +02:00
|
|
|
relayMgr := relayClient.NewManager(ctx, nil, key.PublicKey().String())
|
|
|
|
engine := NewEngine(ctx, cancel, &signal.MockClient{}, &mgmt.MockClient{}, relayMgr, &EngineConfig{
|
2022-11-07 15:38:21 +01:00
|
|
|
WgIfaceName: wgIfaceName,
|
|
|
|
WgAddr: wgAddr,
|
|
|
|
WgPrivateKey: key,
|
|
|
|
WgPort: 33100,
|
2024-06-13 13:24:24 +02:00
|
|
|
}, MobileDependency{}, peer.NewRecorder("https://mgm"), nil)
|
2024-05-22 11:15:29 +02:00
|
|
|
engine.ctx = ctx
|
|
|
|
|
2023-04-13 17:00:01 +02:00
|
|
|
newNet, err := stdnet.NewNet()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2024-10-22 20:53:14 +02:00
|
|
|
opts := iface.WGIFaceOpts{
|
|
|
|
IFaceName: wgIfaceName,
|
|
|
|
Address: wgAddr,
|
|
|
|
WGPort: 33100,
|
|
|
|
WGPrivKey: key.String(),
|
|
|
|
MTU: iface.DefaultMTU,
|
|
|
|
TransportNet: newNet,
|
|
|
|
}
|
|
|
|
engine.wgInterface, err = iface.NewWGIFace(opts)
|
2022-11-07 15:38:21 +01:00
|
|
|
assert.NoError(t, err, "shouldn't return error")
|
|
|
|
|
|
|
|
mockRouteManager := &routemanager.MockManager{
|
2024-05-06 14:47:49 +02:00
|
|
|
UpdateRoutesFunc: func(updateSerial uint64, newRoutes []*route.Route) (map[route.ID]*route.Route, route.HAMap, error) {
|
2024-04-23 14:42:53 +02:00
|
|
|
return nil, nil, nil
|
2022-11-07 15:38:21 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
engine.routeManager = mockRouteManager
|
|
|
|
|
|
|
|
input := struct {
|
|
|
|
inputSerial uint64
|
|
|
|
inputNSGroups []*nbdns.NameServerGroup
|
|
|
|
inputZones []nbdns.CustomZone
|
|
|
|
}{}
|
|
|
|
|
|
|
|
mockDNSServer := &dns.MockServer{
|
|
|
|
UpdateDNSServerFunc: func(serial uint64, update nbdns.Config) error {
|
|
|
|
input.inputSerial = serial
|
|
|
|
input.inputZones = update.CustomZones
|
|
|
|
input.inputNSGroups = update.NameServerGroups
|
|
|
|
return testCase.inputErr
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
engine.dnsServer = mockDNSServer
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
exitErr := engine.Stop()
|
|
|
|
if exitErr != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
err = engine.updateNetworkMap(testCase.networkMap)
|
|
|
|
assert.NoError(t, err, "shouldn't return error")
|
|
|
|
assert.Equal(t, testCase.expectedSerial, input.inputSerial, "serial should match")
|
|
|
|
assert.Len(t, input.inputNSGroups, testCase.expectedZonesLen, "zones len should match")
|
|
|
|
assert.Equal(t, testCase.expectedZones, input.inputZones, "custom zones should match")
|
|
|
|
assert.Len(t, input.inputNSGroups, testCase.expectedNSGroupsLen, "ns groups len should match")
|
|
|
|
assert.Equal(t, testCase.expectedNSGroups, input.inputNSGroups, "ns groups should match")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-10 18:43:13 +01:00
|
|
|
func TestEngine_MultiplePeers(t *testing.T) {
|
2022-03-08 14:47:55 +01:00
|
|
|
// log.SetLevel(log.DebugLevel)
|
2022-01-10 18:43:13 +01:00
|
|
|
|
2022-03-08 14:47:55 +01:00
|
|
|
ctx, cancel := context.WithCancel(CtxInitState(context.Background()))
|
2022-01-10 18:43:13 +01:00
|
|
|
defer cancel()
|
2022-03-08 14:47:55 +01:00
|
|
|
|
2024-06-13 01:20:46 +02:00
|
|
|
sigServer, signalAddr, err := startSignal(t)
|
2022-01-10 18:43:13 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
return
|
|
|
|
}
|
2022-01-18 16:44:58 +01:00
|
|
|
defer sigServer.Stop()
|
2024-10-10 12:35:03 +02:00
|
|
|
mgmtServer, mgmtAddr, err := startManagement(t, t.TempDir(), "../testdata/store.sql")
|
2022-01-10 18:43:13 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
return
|
|
|
|
}
|
2022-01-25 09:40:28 +01:00
|
|
|
defer mgmtServer.GracefulStop()
|
2022-01-10 18:43:13 +01:00
|
|
|
|
|
|
|
setupKey := "A2C8E62B-38F5-4553-B31E-DD66C696CEBB"
|
|
|
|
|
|
|
|
mu := sync.Mutex{}
|
|
|
|
engines := []*Engine{}
|
|
|
|
numPeers := 10
|
|
|
|
wg := sync.WaitGroup{}
|
|
|
|
wg.Add(numPeers)
|
|
|
|
// create and start peers
|
|
|
|
for i := 0; i < numPeers; i++ {
|
|
|
|
j := i
|
|
|
|
go func() {
|
2023-01-17 19:16:50 +01:00
|
|
|
engine, err := createEngine(ctx, cancel, setupKey, j, mgmtAddr, signalAddr)
|
2022-01-10 18:43:13 +01:00
|
|
|
if err != nil {
|
2022-03-16 11:02:06 +01:00
|
|
|
wg.Done()
|
|
|
|
t.Errorf("unable to create the engine for peer %d with error %v", j, err)
|
2022-01-10 18:43:13 +01:00
|
|
|
return
|
|
|
|
}
|
2022-11-23 13:39:42 +01:00
|
|
|
engine.dnsServer = &dns.MockServer{}
|
2022-01-10 18:43:13 +01:00
|
|
|
mu.Lock()
|
|
|
|
defer mu.Unlock()
|
2024-08-27 19:21:14 +02:00
|
|
|
guid := fmt.Sprintf("{%s}", uuid.New().String())
|
2024-10-02 18:24:22 +02:00
|
|
|
device.CustomWindowsGUIDString = strings.ToLower(guid)
|
2022-03-16 11:02:06 +01:00
|
|
|
err = engine.Start()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to start engine for peer %d with error %v", j, err)
|
|
|
|
wg.Done()
|
|
|
|
return
|
|
|
|
}
|
2022-01-10 18:43:13 +01:00
|
|
|
engines = append(engines, engine)
|
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
// wait until all have been created and started
|
|
|
|
wg.Wait()
|
2022-03-16 11:02:06 +01:00
|
|
|
if len(engines) != numPeers {
|
|
|
|
t.Fatal("not all peers was started")
|
|
|
|
}
|
2022-01-10 18:43:13 +01:00
|
|
|
// check whether all the peer have expected peers connected
|
2022-01-17 14:01:58 +01:00
|
|
|
|
2022-01-10 18:43:13 +01:00
|
|
|
expectedConnected := numPeers * (numPeers - 1)
|
2022-03-16 11:02:06 +01:00
|
|
|
|
2022-01-17 14:01:58 +01:00
|
|
|
// adjust according to timeouts
|
|
|
|
timeout := 50 * time.Second
|
|
|
|
timeoutChan := time.After(timeout)
|
2022-03-16 11:02:06 +01:00
|
|
|
ticker := time.NewTicker(time.Second)
|
|
|
|
defer ticker.Stop()
|
|
|
|
loop:
|
2022-01-10 18:43:13 +01:00
|
|
|
for {
|
2022-01-17 14:01:58 +01:00
|
|
|
select {
|
|
|
|
case <-timeoutChan:
|
|
|
|
t.Fatalf("waiting for expected connections timeout after %s", timeout.String())
|
2022-03-16 11:02:06 +01:00
|
|
|
break loop
|
|
|
|
case <-ticker.C:
|
|
|
|
totalConnected := 0
|
|
|
|
for _, engine := range engines {
|
2023-11-27 16:40:02 +01:00
|
|
|
totalConnected += getConnectedPeers(engine)
|
2022-03-16 11:02:06 +01:00
|
|
|
}
|
|
|
|
if totalConnected == expectedConnected {
|
|
|
|
log.Infof("total connected=%d", totalConnected)
|
|
|
|
break loop
|
|
|
|
}
|
|
|
|
log.Infof("total connected=%d", totalConnected)
|
2022-01-10 18:43:13 +01:00
|
|
|
}
|
|
|
|
}
|
2022-01-17 14:01:58 +01:00
|
|
|
// cleanup test
|
2022-03-16 11:02:06 +01:00
|
|
|
for n, peerEngine := range engines {
|
2023-02-13 18:34:56 +01:00
|
|
|
t.Logf("stopping peer with interface %s from multipeer test, loopIndex %d", peerEngine.wgInterface.Name(), n)
|
2022-01-25 11:18:01 +01:00
|
|
|
errStop := peerEngine.mgmClient.Close()
|
|
|
|
if errStop != nil {
|
|
|
|
log.Infoln("got error trying to close management clients from engine: ", errStop)
|
|
|
|
}
|
|
|
|
errStop = peerEngine.Stop()
|
2022-01-17 14:01:58 +01:00
|
|
|
if errStop != nil {
|
|
|
|
log.Infoln("got error trying to close testing peers engine: ", errStop)
|
|
|
|
}
|
|
|
|
}
|
2022-01-10 18:43:13 +01:00
|
|
|
}
|
|
|
|
|
2023-01-17 19:16:50 +01:00
|
|
|
func Test_ParseNATExternalIPMappings(t *testing.T) {
|
|
|
|
ifaceList, err := net.Interfaces()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("could get the interface list, got error: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var testingIP string
|
|
|
|
var testingInterface string
|
|
|
|
|
|
|
|
for _, iface := range ifaceList {
|
|
|
|
addrList, err := iface.Addrs()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("could get the addr list, got error: %s", err)
|
|
|
|
}
|
|
|
|
for _, addr := range addrList {
|
|
|
|
prefix := netip.MustParsePrefix(addr.String())
|
|
|
|
if prefix.Addr().Is4() && !prefix.Addr().IsLoopback() {
|
|
|
|
testingIP = prefix.Addr().String()
|
|
|
|
testingInterface = iface.Name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
inputMapList []string
|
|
|
|
inputBlacklistInterface []string
|
|
|
|
expectedOutput []string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Parse Valid List Should Be OK",
|
|
|
|
inputBlacklistInterface: defaultInterfaceBlacklist,
|
|
|
|
inputMapList: []string{"1.1.1.1", "8.8.8.8/" + testingInterface},
|
|
|
|
expectedOutput: []string{"1.1.1.1", "8.8.8.8/" + testingIP},
|
|
|
|
},
|
|
|
|
{
|
2023-02-13 18:34:56 +01:00
|
|
|
name: "Only Interface name Should Return Nil",
|
2023-01-17 19:16:50 +01:00
|
|
|
inputBlacklistInterface: defaultInterfaceBlacklist,
|
|
|
|
inputMapList: []string{testingInterface},
|
|
|
|
expectedOutput: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Invalid IP Return Nil",
|
|
|
|
inputBlacklistInterface: defaultInterfaceBlacklist,
|
|
|
|
inputMapList: []string{"1.1.1.1000"},
|
|
|
|
expectedOutput: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Invalid Mapping Element Should return Nil",
|
|
|
|
inputBlacklistInterface: defaultInterfaceBlacklist,
|
|
|
|
inputMapList: []string{"1.1.1.1/10.10.10.1/eth0"},
|
|
|
|
expectedOutput: nil,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, testCase := range testCases {
|
|
|
|
t.Run(testCase.name, func(t *testing.T) {
|
|
|
|
engine := &Engine{
|
|
|
|
config: &EngineConfig{
|
|
|
|
IFaceBlackList: testCase.inputBlacklistInterface,
|
|
|
|
NATExternalIPs: testCase.inputMapList,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
parsedList := engine.parseNATExternalIPMappings()
|
|
|
|
require.ElementsMatchf(t, testCase.expectedOutput, parsedList, "elements of parsed list should match expected list")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func createEngine(ctx context.Context, cancel context.CancelFunc, setupKey string, i int, mgmtAddr string, signalAddr string) (*Engine, error) {
|
2022-01-17 14:01:58 +01:00
|
|
|
key, err := wgtypes.GeneratePrivateKey()
|
2022-01-10 18:43:13 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-01-17 19:16:50 +01:00
|
|
|
mgmtClient, err := mgmt.NewClient(ctx, mgmtAddr, key, false)
|
2022-01-10 18:43:13 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-01-17 19:16:50 +01:00
|
|
|
signalClient, err := signal.NewClient(ctx, signalAddr, key, false)
|
2022-01-10 18:43:13 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
publicKey, err := mgmtClient.GetServerPublicKey()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-05-25 23:25:02 +02:00
|
|
|
info := system.GetInfo(ctx)
|
2022-06-23 17:04:53 +02:00
|
|
|
resp, err := mgmtClient.Register(*publicKey, setupKey, "", info, nil)
|
2022-01-10 18:43:13 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var ifaceName string
|
|
|
|
if runtime.GOOS == "darwin" {
|
|
|
|
ifaceName = fmt.Sprintf("utun1%d", i)
|
|
|
|
} else {
|
|
|
|
ifaceName = fmt.Sprintf("wt%d", i)
|
|
|
|
}
|
|
|
|
|
2022-02-16 20:00:21 +01:00
|
|
|
wgPort := 33100 + i
|
2022-01-10 18:43:13 +01:00
|
|
|
conf := &EngineConfig{
|
2022-01-17 14:01:58 +01:00
|
|
|
WgIfaceName: ifaceName,
|
2022-01-10 18:43:13 +01:00
|
|
|
WgAddr: resp.PeerConfig.Address,
|
|
|
|
WgPrivateKey: key,
|
2022-02-16 20:00:21 +01:00
|
|
|
WgPort: wgPort,
|
2022-01-10 18:43:13 +01:00
|
|
|
}
|
|
|
|
|
2024-09-08 12:06:14 +02:00
|
|
|
relayMgr := relayClient.NewManager(ctx, nil, key.PublicKey().String())
|
|
|
|
e, err := NewEngine(ctx, cancel, signalClient, mgmtClient, relayMgr, conf, MobileDependency{}, peer.NewRecorder("https://mgm"), nil), nil
|
2024-05-22 11:15:29 +02:00
|
|
|
e.ctx = ctx
|
|
|
|
return e, err
|
2022-01-10 18:43:13 +01:00
|
|
|
}
|
|
|
|
|
2024-06-13 01:20:46 +02:00
|
|
|
func startSignal(t *testing.T) (*grpc.Server, string, error) {
|
|
|
|
t.Helper()
|
|
|
|
|
2022-01-10 18:43:13 +01:00
|
|
|
s := grpc.NewServer(grpc.KeepaliveEnforcementPolicy(kaep), grpc.KeepaliveParams(kasp))
|
|
|
|
|
2023-01-17 19:16:50 +01:00
|
|
|
lis, err := net.Listen("tcp", "localhost:0")
|
2022-01-10 18:43:13 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("failed to listen: %v", err)
|
|
|
|
}
|
|
|
|
|
2024-09-29 00:22:47 +02:00
|
|
|
srv, err := signalServer.NewServer(context.Background(), otel.Meter(""))
|
2024-06-13 01:20:46 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
proto.RegisterSignalExchangeServer(s, srv)
|
2022-01-10 18:43:13 +01:00
|
|
|
|
|
|
|
go func() {
|
|
|
|
if err = s.Serve(lis); err != nil {
|
|
|
|
log.Fatalf("failed to serve: %v", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2023-01-17 19:16:50 +01:00
|
|
|
return s, lis.Addr().String(), nil
|
2022-01-10 18:43:13 +01:00
|
|
|
}
|
|
|
|
|
2024-10-03 15:50:35 +02:00
|
|
|
func startManagement(t *testing.T, dataDir, testFile string) (*grpc.Server, string, error) {
|
2024-06-13 12:39:19 +02:00
|
|
|
t.Helper()
|
|
|
|
|
2022-01-18 16:44:58 +01:00
|
|
|
config := &server.Config{
|
|
|
|
Stuns: []*server.Host{},
|
|
|
|
TURNConfig: &server.TURNConfig{},
|
2024-09-08 12:06:14 +02:00
|
|
|
Relay: &server.Relay{
|
|
|
|
Addresses: []string{"127.0.0.1:1234"},
|
|
|
|
CredentialsTTL: util.Duration{Duration: time.Hour},
|
|
|
|
Secret: "222222222222222222",
|
|
|
|
},
|
2022-01-18 16:44:58 +01:00
|
|
|
Signal: &server.Host{
|
|
|
|
Proto: "http",
|
|
|
|
URI: "localhost:10000",
|
|
|
|
},
|
|
|
|
Datadir: dataDir,
|
|
|
|
HttpConfig: nil,
|
|
|
|
}
|
2022-01-10 18:43:13 +01:00
|
|
|
|
2023-01-17 19:16:50 +01:00
|
|
|
lis, err := net.Listen("tcp", "localhost:0")
|
2022-01-10 18:43:13 +01:00
|
|
|
if err != nil {
|
2023-01-17 19:16:50 +01:00
|
|
|
return nil, "", err
|
2022-01-10 18:43:13 +01:00
|
|
|
}
|
|
|
|
s := grpc.NewServer(grpc.KeepaliveEnforcementPolicy(kaep), grpc.KeepaliveParams(kasp))
|
2024-06-13 12:39:19 +02:00
|
|
|
|
2024-10-10 12:35:03 +02:00
|
|
|
store, cleanUp, err := server.NewTestStoreFromSQL(context.Background(), testFile, config.Datadir)
|
2022-01-10 18:43:13 +01:00
|
|
|
if err != nil {
|
2023-10-12 15:42:36 +02:00
|
|
|
return nil, "", err
|
2022-01-10 18:43:13 +01:00
|
|
|
}
|
2024-06-13 12:39:19 +02:00
|
|
|
t.Cleanup(cleanUp)
|
2023-10-12 15:42:36 +02:00
|
|
|
|
2023-11-16 18:21:52 +01:00
|
|
|
peersUpdateManager := server.NewPeersUpdateManager(nil)
|
2023-01-02 15:11:32 +01:00
|
|
|
eventStore := &activity.InMemoryEventStore{}
|
|
|
|
if err != nil {
|
2023-09-04 17:03:44 +02:00
|
|
|
return nil, "", err
|
2023-01-02 15:11:32 +01:00
|
|
|
}
|
2024-07-03 11:33:02 +02:00
|
|
|
ia, _ := integrations.NewIntegratedValidator(context.Background(), eventStore)
|
2024-08-07 10:52:31 +02:00
|
|
|
|
|
|
|
metrics, err := telemetry.NewDefaultAppMetrics(context.Background())
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
accountManager, err := server.BuildManager(context.Background(), store, peersUpdateManager, nil, "", "netbird.selfhosted", eventStore, nil, false, ia, metrics)
|
2022-05-21 15:21:39 +02:00
|
|
|
if err != nil {
|
2023-01-17 19:16:50 +01:00
|
|
|
return nil, "", err
|
2022-05-21 15:21:39 +02:00
|
|
|
}
|
2024-09-08 12:06:14 +02:00
|
|
|
|
|
|
|
secretsManager := server.NewTimeBasedAuthSecretsManager(peersUpdateManager, config.TURNConfig, config.Relay)
|
|
|
|
mgmtServer, err := server.NewServer(context.Background(), config, accountManager, peersUpdateManager, secretsManager, nil, nil)
|
2022-01-10 18:43:13 +01:00
|
|
|
if err != nil {
|
2023-01-17 19:16:50 +01:00
|
|
|
return nil, "", err
|
2022-01-10 18:43:13 +01:00
|
|
|
}
|
|
|
|
mgmtProto.RegisterManagementServiceServer(s, mgmtServer)
|
|
|
|
go func() {
|
|
|
|
if err = s.Serve(lis); err != nil {
|
|
|
|
log.Fatalf("failed to serve: %v", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2023-01-17 19:16:50 +01:00
|
|
|
return s, lis.Addr().String(), nil
|
2022-01-10 18:43:13 +01:00
|
|
|
}
|
2023-03-03 19:49:18 +01:00
|
|
|
|
|
|
|
// getConnectedPeers returns a connection Status or nil if peer connection wasn't found
|
|
|
|
func getConnectedPeers(e *Engine) int {
|
|
|
|
e.syncMsgMux.Lock()
|
|
|
|
defer e.syncMsgMux.Unlock()
|
|
|
|
i := 0
|
|
|
|
for _, conn := range e.peerConns {
|
|
|
|
if conn.Status() == peer.StatusConnected {
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
|
|
|
|
func getPeers(e *Engine) int {
|
|
|
|
e.syncMsgMux.Lock()
|
|
|
|
defer e.syncMsgMux.Unlock()
|
|
|
|
|
|
|
|
return len(e.peerConns)
|
|
|
|
}
|