2021-08-16 23:30:51 +02:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-01-02 15:11:32 +01:00
|
|
|
"github.com/netbirdio/netbird/management/server/activity"
|
2022-02-08 18:03:27 +01:00
|
|
|
"net"
|
|
|
|
"path/filepath"
|
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2022-03-26 12:08:54 +01:00
|
|
|
"github.com/netbirdio/netbird/client/system"
|
2022-02-08 18:03:27 +01:00
|
|
|
|
2022-03-26 12:08:54 +01:00
|
|
|
"github.com/netbirdio/netbird/encryption"
|
|
|
|
"github.com/netbirdio/netbird/management/proto"
|
|
|
|
mgmtProto "github.com/netbirdio/netbird/management/proto"
|
|
|
|
mgmt "github.com/netbirdio/netbird/management/server"
|
|
|
|
"github.com/netbirdio/netbird/management/server/mock_server"
|
2021-08-16 23:30:51 +02:00
|
|
|
log "github.com/sirupsen/logrus"
|
2022-02-08 18:03:27 +01:00
|
|
|
"github.com/stretchr/testify/assert"
|
2022-02-22 18:18:05 +01:00
|
|
|
|
2022-03-26 12:08:54 +01:00
|
|
|
"github.com/netbirdio/netbird/util"
|
2021-08-16 23:30:51 +02:00
|
|
|
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
|
|
|
|
"google.golang.org/grpc"
|
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
"google.golang.org/grpc/status"
|
|
|
|
)
|
|
|
|
|
2021-08-20 22:37:58 +02:00
|
|
|
const ValidKey = "A2C8E62B-38F5-4553-B31E-DD66C696CEBB"
|
|
|
|
|
2022-05-08 11:04:57 +02:00
|
|
|
func startManagement(t *testing.T) (*grpc.Server, net.Listener) {
|
2021-08-16 23:30:51 +02:00
|
|
|
level, _ := log.ParseLevel("debug")
|
|
|
|
log.SetLevel(level)
|
|
|
|
|
|
|
|
testDir := t.TempDir()
|
2022-05-08 11:04:57 +02:00
|
|
|
|
2021-08-16 23:30:51 +02:00
|
|
|
config := &mgmt.Config{}
|
2022-05-08 11:04:57 +02:00
|
|
|
_, err := util.ReadJson("../server/testdata/management.json", config)
|
2021-08-16 23:30:51 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
config.Datadir = testDir
|
|
|
|
err = util.CopyFileContents("../server/testdata/store.json", filepath.Join(testDir, "store.json"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
lis, err := net.Listen("tcp", ":0")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
s := grpc.NewServer()
|
2022-11-08 10:46:12 +01:00
|
|
|
store, err := mgmt.NewFileStore(config.Datadir)
|
2021-08-16 23:30:51 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2021-08-29 17:48:31 +02:00
|
|
|
peersUpdateManager := mgmt.NewPeersUpdateManager()
|
2023-01-02 15:11:32 +01:00
|
|
|
eventStore := &activity.InMemoryEventStore{}
|
|
|
|
accountManager, err := mgmt.BuildManager(store, peersUpdateManager, nil, "", "",
|
|
|
|
eventStore)
|
2022-05-21 15:21:39 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2021-09-02 14:41:54 +02:00
|
|
|
turnManager := mgmt.NewTimeBasedAuthSecretsManager(peersUpdateManager, config.TURNConfig)
|
2022-10-22 15:06:54 +02:00
|
|
|
mgmtServer, err := mgmt.NewServer(config, accountManager, peersUpdateManager, turnManager, nil)
|
2021-08-16 23:30:51 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
mgmtProto.RegisterManagementServiceServer(s, mgmtServer)
|
|
|
|
go func() {
|
|
|
|
if err := s.Serve(lis); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return s, lis
|
|
|
|
}
|
|
|
|
|
2022-05-08 11:04:57 +02:00
|
|
|
func startMockManagement(t *testing.T) (*grpc.Server, net.Listener, *mock_server.ManagementServiceServerMock, wgtypes.Key) {
|
2022-02-08 18:03:27 +01:00
|
|
|
lis, err := net.Listen("tcp", ":0")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
s := grpc.NewServer()
|
|
|
|
|
2022-05-08 11:04:57 +02:00
|
|
|
serverKey, err := wgtypes.GenerateKey()
|
2022-02-08 18:03:27 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2022-05-08 11:04:57 +02:00
|
|
|
mgmtMockServer := &mock_server.ManagementServiceServerMock{
|
2022-02-08 18:03:27 +01:00
|
|
|
GetServerKeyFunc: func(context.Context, *proto.Empty) (*proto.ServerKeyResponse, error) {
|
|
|
|
response := &proto.ServerKeyResponse{
|
|
|
|
Key: serverKey.PublicKey().String(),
|
|
|
|
}
|
|
|
|
return response, nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
mgmtProto.RegisterManagementServiceServer(s, mgmtMockServer)
|
|
|
|
go func() {
|
|
|
|
if err := s.Serve(lis); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2022-05-08 11:04:57 +02:00
|
|
|
return s, lis, mgmtMockServer, serverKey
|
|
|
|
}
|
|
|
|
|
|
|
|
func closeManagementSilently(s *grpc.Server, listener net.Listener) {
|
|
|
|
s.GracefulStop()
|
|
|
|
err := listener.Close()
|
|
|
|
if err != nil {
|
|
|
|
log.Warnf("error while closing management listener %v", err)
|
|
|
|
return
|
|
|
|
}
|
2022-02-08 18:03:27 +01:00
|
|
|
}
|
|
|
|
|
2021-08-16 23:30:51 +02:00
|
|
|
func TestClient_GetServerPublicKey(t *testing.T) {
|
2022-05-08 11:04:57 +02:00
|
|
|
testKey, err := wgtypes.GenerateKey()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
ctx := context.Background()
|
|
|
|
s, listener := startManagement(t)
|
|
|
|
defer closeManagementSilently(s, listener)
|
2021-08-16 23:30:51 +02:00
|
|
|
|
2022-05-08 11:04:57 +02:00
|
|
|
client, err := NewClient(ctx, listener.Addr().String(), testKey, false)
|
2021-08-16 23:30:51 +02:00
|
|
|
if err != nil {
|
2022-05-08 11:04:57 +02:00
|
|
|
t.Fatal(err)
|
2021-08-16 23:30:51 +02:00
|
|
|
}
|
|
|
|
|
2022-05-08 11:04:57 +02:00
|
|
|
key, err := client.GetServerPublicKey()
|
|
|
|
if err != nil {
|
|
|
|
t.Error("couldn't retrieve management public key")
|
|
|
|
}
|
2021-08-16 23:30:51 +02:00
|
|
|
if key == nil {
|
2022-05-08 11:04:57 +02:00
|
|
|
t.Error("got an empty management public key")
|
2021-08-16 23:30:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestClient_LoginUnregistered_ShouldThrow_401(t *testing.T) {
|
2022-05-08 11:04:57 +02:00
|
|
|
testKey, err := wgtypes.GenerateKey()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
ctx := context.Background()
|
|
|
|
s, listener := startManagement(t)
|
|
|
|
defer closeManagementSilently(s, listener)
|
|
|
|
|
|
|
|
client, err := NewClient(ctx, listener.Addr().String(), testKey, false)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
key, err := client.GetServerPublicKey()
|
2021-08-16 23:30:51 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2022-05-25 23:25:02 +02:00
|
|
|
sysInfo := system.GetInfo(context.TODO())
|
2022-06-23 17:04:53 +02:00
|
|
|
_, err = client.Login(*key, sysInfo, nil)
|
2021-08-16 23:30:51 +02:00
|
|
|
if err == nil {
|
|
|
|
t.Error("expecting err on unregistered login, got nil")
|
|
|
|
}
|
|
|
|
if s, ok := status.FromError(err); !ok || s.Code() != codes.PermissionDenied {
|
|
|
|
t.Errorf("expecting err code %d denied on on unregistered login got %d", codes.PermissionDenied, s.Code())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestClient_LoginRegistered(t *testing.T) {
|
2022-05-08 11:04:57 +02:00
|
|
|
testKey, err := wgtypes.GenerateKey()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
ctx := context.Background()
|
|
|
|
s, listener := startManagement(t)
|
|
|
|
defer closeManagementSilently(s, listener)
|
|
|
|
|
|
|
|
client, err := NewClient(ctx, listener.Addr().String(), testKey, false)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
key, err := client.GetServerPublicKey()
|
2021-08-16 23:30:51 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2022-05-25 23:25:02 +02:00
|
|
|
info := system.GetInfo(context.TODO())
|
2022-06-23 17:04:53 +02:00
|
|
|
resp, err := client.Register(*key, ValidKey, "", info, nil)
|
2021-08-16 23:30:51 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp == nil {
|
|
|
|
t.Error("expecting non nil response, got nil")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestClient_Sync(t *testing.T) {
|
2022-05-08 11:04:57 +02:00
|
|
|
testKey, err := wgtypes.GenerateKey()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
ctx := context.Background()
|
|
|
|
s, listener := startManagement(t)
|
|
|
|
defer closeManagementSilently(s, listener)
|
|
|
|
|
|
|
|
client, err := NewClient(ctx, listener.Addr().String(), testKey, false)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
serverKey, err := client.GetServerPublicKey()
|
2021-08-16 23:30:51 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
2022-05-25 23:25:02 +02:00
|
|
|
info := system.GetInfo(context.TODO())
|
2022-06-23 17:04:53 +02:00
|
|
|
_, err = client.Register(*serverKey, ValidKey, "", info, nil)
|
2021-08-16 23:30:51 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// create and register second peer (we should receive on Sync request)
|
|
|
|
remoteKey, err := wgtypes.GenerateKey()
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2022-05-08 11:04:57 +02:00
|
|
|
remoteClient, err := NewClient(context.TODO(), listener.Addr().String(), remoteKey, false)
|
2021-08-16 23:30:51 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2022-02-08 18:03:27 +01:00
|
|
|
|
2022-05-25 23:25:02 +02:00
|
|
|
info = system.GetInfo(context.TODO())
|
2022-06-23 17:04:53 +02:00
|
|
|
_, err = remoteClient.Register(*serverKey, ValidKey, "", info, nil)
|
2021-08-16 23:30:51 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ch := make(chan *mgmtProto.SyncResponse, 1)
|
|
|
|
|
2021-09-07 18:36:46 +02:00
|
|
|
go func() {
|
2022-05-08 11:04:57 +02:00
|
|
|
err = client.Sync(func(msg *mgmtProto.SyncResponse) error {
|
2021-09-07 18:36:46 +02:00
|
|
|
ch <- msg
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}()
|
2021-08-16 23:30:51 +02:00
|
|
|
|
|
|
|
select {
|
|
|
|
case resp := <-ch:
|
|
|
|
if resp.GetPeerConfig() == nil {
|
|
|
|
t.Error("expecting non nil PeerConfig got nil")
|
|
|
|
}
|
|
|
|
if resp.GetWiretrusteeConfig() == nil {
|
|
|
|
t.Error("expecting non nil WiretrusteeConfig got nil")
|
|
|
|
}
|
|
|
|
if len(resp.GetRemotePeers()) != 1 {
|
|
|
|
t.Errorf("expecting RemotePeers size %d got %d", 1, len(resp.GetRemotePeers()))
|
2022-05-21 15:21:39 +02:00
|
|
|
return
|
2021-08-16 23:30:51 +02:00
|
|
|
}
|
2021-09-07 18:36:46 +02:00
|
|
|
if resp.GetRemotePeersIsEmpty() == true {
|
|
|
|
t.Error("expecting RemotePeers property to be false, got true")
|
|
|
|
}
|
2021-08-16 23:30:51 +02:00
|
|
|
if resp.GetRemotePeers()[0].GetWgPubKey() != remoteKey.PublicKey().String() {
|
|
|
|
t.Errorf("expecting RemotePeer public key %s got %s", remoteKey.PublicKey().String(), resp.GetRemotePeers()[0].GetWgPubKey())
|
|
|
|
}
|
|
|
|
case <-time.After(3 * time.Second):
|
|
|
|
t.Error("timeout waiting for test to finish")
|
|
|
|
}
|
|
|
|
}
|
2022-02-08 18:03:27 +01:00
|
|
|
|
|
|
|
func Test_SystemMetaDataFromClient(t *testing.T) {
|
2022-05-08 11:04:57 +02:00
|
|
|
s, lis, mgmtMockServer, serverKey := startMockManagement(t)
|
|
|
|
defer s.GracefulStop()
|
2022-02-08 18:03:27 +01:00
|
|
|
|
|
|
|
testKey, err := wgtypes.GenerateKey()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
serverAddr := lis.Addr().String()
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
testClient, err := NewClient(ctx, serverAddr, testKey, false)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("error while creating testClient: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
key, err := testClient.GetServerPublicKey()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("error while getting server public key from testclient, %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var actualMeta *proto.PeerSystemMeta
|
|
|
|
var actualValidKey string
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(1)
|
|
|
|
|
2022-05-21 15:21:39 +02:00
|
|
|
mgmtMockServer.LoginFunc = func(ctx context.Context, msg *proto.EncryptedMessage) (*proto.EncryptedMessage, error) {
|
|
|
|
peerKey, err := wgtypes.ParseKey(msg.GetWgPubKey())
|
|
|
|
if err != nil {
|
|
|
|
log.Warnf("error while parsing peer's Wireguard public key %s on Sync request.", msg.WgPubKey)
|
|
|
|
return nil, status.Errorf(codes.InvalidArgument, "provided wgPubKey %s is invalid", msg.WgPubKey)
|
|
|
|
}
|
2022-02-08 18:03:27 +01:00
|
|
|
|
2022-05-21 15:21:39 +02:00
|
|
|
loginReq := &proto.LoginRequest{}
|
|
|
|
err = encryption.DecryptMessage(peerKey, serverKey, msg.Body, loginReq)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2022-02-08 18:03:27 +01:00
|
|
|
|
2022-05-21 15:21:39 +02:00
|
|
|
actualMeta = loginReq.GetMeta()
|
|
|
|
actualValidKey = loginReq.GetSetupKey()
|
|
|
|
wg.Done()
|
2022-02-08 18:03:27 +01:00
|
|
|
|
2022-05-21 15:21:39 +02:00
|
|
|
loginResp := &proto.LoginResponse{}
|
|
|
|
encryptedResp, err := encryption.EncryptMessage(peerKey, serverKey, loginResp)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2022-02-08 18:03:27 +01:00
|
|
|
}
|
|
|
|
|
2022-05-21 15:21:39 +02:00
|
|
|
return &mgmtProto.EncryptedMessage{
|
|
|
|
WgPubKey: serverKey.PublicKey().String(),
|
|
|
|
Body: encryptedResp,
|
|
|
|
Version: 0,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2022-05-25 23:25:02 +02:00
|
|
|
info := system.GetInfo(context.TODO())
|
2022-06-23 17:04:53 +02:00
|
|
|
_, err = testClient.Register(*key, ValidKey, "", info, nil)
|
2022-02-08 18:03:27 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error while trying to register client: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
expectedMeta := &proto.PeerSystemMeta{
|
|
|
|
Hostname: info.Hostname,
|
|
|
|
GoOS: info.GoOS,
|
|
|
|
Kernel: info.Kernel,
|
|
|
|
Core: info.OSVersion,
|
|
|
|
Platform: info.Platform,
|
|
|
|
OS: info.OS,
|
|
|
|
WiretrusteeVersion: info.WiretrusteeVersion,
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, ValidKey, actualValidKey)
|
|
|
|
assert.Equal(t, expectedMeta, actualMeta)
|
|
|
|
}
|
2022-05-08 11:04:57 +02:00
|
|
|
|
|
|
|
func Test_GetDeviceAuthorizationFlow(t *testing.T) {
|
|
|
|
s, lis, mgmtMockServer, serverKey := startMockManagement(t)
|
|
|
|
defer s.GracefulStop()
|
|
|
|
|
|
|
|
testKey, err := wgtypes.GenerateKey()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
serverAddr := lis.Addr().String()
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
client, err := NewClient(ctx, serverAddr, testKey, false)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("error while creating testClient: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedFlowInfo := &proto.DeviceAuthorizationFlow{
|
|
|
|
Provider: 0,
|
|
|
|
ProviderConfig: &proto.ProviderConfig{ClientID: "client"},
|
|
|
|
}
|
|
|
|
|
2022-05-21 15:21:39 +02:00
|
|
|
mgmtMockServer.GetDeviceAuthorizationFlowFunc = func(ctx context.Context, req *mgmtProto.EncryptedMessage) (*proto.EncryptedMessage, error) {
|
|
|
|
encryptedResp, err := encryption.EncryptMessage(serverKey, client.key, expectedFlowInfo)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2022-05-08 11:04:57 +02:00
|
|
|
}
|
|
|
|
|
2022-05-21 15:21:39 +02:00
|
|
|
return &mgmtProto.EncryptedMessage{
|
|
|
|
WgPubKey: serverKey.PublicKey().String(),
|
|
|
|
Body: encryptedResp,
|
|
|
|
Version: 0,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2022-05-08 11:04:57 +02:00
|
|
|
flowInfo, err := client.GetDeviceAuthorizationFlow(serverKey)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("error while retrieving device auth flow information")
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, expectedFlowInfo.Provider, flowInfo.Provider, "provider should match")
|
|
|
|
assert.Equal(t, expectedFlowInfo.ProviderConfig.ClientID, flowInfo.ProviderConfig.ClientID, "provider configured client ID should match")
|
|
|
|
}
|