2021-08-16 23:30:51 +02:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-02-08 18:03:27 +01:00
|
|
|
"net"
|
|
|
|
"path/filepath"
|
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2023-05-19 11:42:25 +02:00
|
|
|
"github.com/netbirdio/netbird/management/server/activity"
|
|
|
|
|
2022-03-26 12:08:54 +01:00
|
|
|
"github.com/netbirdio/netbird/client/system"
|
2022-02-08 18:03:27 +01:00
|
|
|
|
2023-05-19 11:42:25 +02:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
2022-03-26 12:08:54 +01:00
|
|
|
"github.com/netbirdio/netbird/encryption"
|
|
|
|
mgmtProto "github.com/netbirdio/netbird/management/proto"
|
|
|
|
mgmt "github.com/netbirdio/netbird/management/server"
|
|
|
|
"github.com/netbirdio/netbird/management/server/mock_server"
|
2022-02-22 18:18:05 +01:00
|
|
|
|
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"
|
2023-05-19 11:42:25 +02:00
|
|
|
|
|
|
|
"github.com/netbirdio/netbird/util"
|
2021-08-16 23:30:51 +02:00
|
|
|
)
|
|
|
|
|
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) {
|
2023-11-10 16:33:13 +01:00
|
|
|
t.Helper()
|
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()
|
2023-10-12 15:42:36 +02:00
|
|
|
store, err := mgmt.NewStoreFromJson(config.Datadir, nil)
|
2021-08-16 23:30:51 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2023-11-16 18:21:52 +01:00
|
|
|
peersUpdateManager := mgmt.NewPeersUpdateManager(nil)
|
2023-01-02 15:11:32 +01:00
|
|
|
eventStore := &activity.InMemoryEventStore{}
|
2024-02-20 09:59:56 +01:00
|
|
|
accountManager, err := mgmt.BuildManager(store, peersUpdateManager, nil, "", "", eventStore, nil, false)
|
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)
|
2023-09-04 11:37:39 +02:00
|
|
|
mgmtServer, err := mgmt.NewServer(config, accountManager, peersUpdateManager, turnManager, nil, 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) {
|
2023-11-10 16:33:13 +01:00
|
|
|
t.Helper()
|
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{
|
2023-10-16 17:00:05 +02:00
|
|
|
GetServerKeyFunc: func(context.Context, *mgmtProto.Empty) (*mgmtProto.ServerKeyResponse, error) {
|
|
|
|
response := &mgmtProto.ServerKeyResponse{
|
2022-02-08 18:03:27 +01:00
|
|
|
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 {
|
2023-11-10 16:33:13 +01:00
|
|
|
t.Errorf("expecting err code %d denied on unregistered login got %d", codes.PermissionDenied, s.Code())
|
2021-08-16 23:30:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2023-11-27 16:40:02 +01:00
|
|
|
t.Fatal(err)
|
2022-02-08 18:03:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
serverAddr := lis.Addr().String()
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
testClient, err := NewClient(ctx, serverAddr, testKey, false)
|
|
|
|
if err != nil {
|
2023-11-27 16:40:02 +01:00
|
|
|
t.Fatalf("error while creating testClient: %v", err)
|
2022-02-08 18:03:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
key, err := testClient.GetServerPublicKey()
|
|
|
|
if err != nil {
|
2023-11-27 16:40:02 +01:00
|
|
|
t.Fatalf("error while getting server public key from testclient, %v", err)
|
2022-02-08 18:03:27 +01:00
|
|
|
}
|
|
|
|
|
2023-10-16 17:00:05 +02:00
|
|
|
var actualMeta *mgmtProto.PeerSystemMeta
|
2022-02-08 18:03:27 +01:00
|
|
|
var actualValidKey string
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(1)
|
|
|
|
|
2023-10-16 17:00:05 +02:00
|
|
|
mgmtMockServer.LoginFunc = func(ctx context.Context, msg *mgmtProto.EncryptedMessage) (*mgmtProto.EncryptedMessage, error) {
|
2022-05-21 15:21:39 +02:00
|
|
|
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
|
|
|
|
2023-10-16 17:00:05 +02:00
|
|
|
loginReq := &mgmtProto.LoginRequest{}
|
2022-05-21 15:21:39 +02:00
|
|
|
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
|
|
|
|
2023-10-16 17:00:05 +02:00
|
|
|
loginResp := &mgmtProto.LoginResponse{}
|
2022-05-21 15:21:39 +02:00
|
|
|
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()
|
|
|
|
|
2024-02-20 11:53:11 +01:00
|
|
|
protoNetAddr := make([]*mgmtProto.NetworkAddress, 0, len(info.NetworkAddresses))
|
|
|
|
for _, addr := range info.NetworkAddresses {
|
|
|
|
protoNetAddr = append(protoNetAddr, &mgmtProto.NetworkAddress{
|
|
|
|
NetIP: addr.NetIP.String(),
|
|
|
|
Mac: addr.Mac,
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-10-16 17:00:05 +02:00
|
|
|
expectedMeta := &mgmtProto.PeerSystemMeta{
|
2022-02-08 18:03:27 +01:00
|
|
|
Hostname: info.Hostname,
|
|
|
|
GoOS: info.GoOS,
|
|
|
|
Kernel: info.Kernel,
|
|
|
|
Platform: info.Platform,
|
|
|
|
OS: info.OS,
|
2024-02-20 09:59:56 +01:00
|
|
|
Core: info.OSVersion,
|
|
|
|
OSVersion: info.OSVersion,
|
2022-02-08 18:03:27 +01:00
|
|
|
WiretrusteeVersion: info.WiretrusteeVersion,
|
2024-02-20 09:59:56 +01:00
|
|
|
KernelVersion: info.KernelVersion,
|
2024-02-20 11:53:11 +01:00
|
|
|
|
|
|
|
NetworkAddresses: protoNetAddr,
|
|
|
|
SysSerialNumber: info.SystemSerialNumber,
|
|
|
|
SysProductName: info.SystemProductName,
|
|
|
|
SysManufacturer: info.SystemManufacturer,
|
2022-02-08 18:03:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, ValidKey, actualValidKey)
|
2024-02-20 11:53:11 +01:00
|
|
|
if !isEqual(expectedMeta, actualMeta) {
|
|
|
|
t.Errorf("expected and actual meta are not equal")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func isEqual(a, b *mgmtProto.PeerSystemMeta) bool {
|
|
|
|
if len(a.NetworkAddresses) != len(b.NetworkAddresses) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, addr := range a.GetNetworkAddresses() {
|
|
|
|
var found bool
|
|
|
|
for _, oAddr := range b.GetNetworkAddresses() {
|
|
|
|
if addr.GetMac() == oAddr.GetMac() && addr.GetNetIP() == oAddr.GetNetIP() {
|
|
|
|
found = true
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Infof("------")
|
|
|
|
|
|
|
|
return a.GetHostname() == b.GetHostname() &&
|
|
|
|
a.GetGoOS() == b.GetGoOS() &&
|
|
|
|
a.GetKernel() == b.GetKernel() &&
|
|
|
|
a.GetKernelVersion() == b.GetKernelVersion() &&
|
|
|
|
a.GetCore() == b.GetCore() &&
|
|
|
|
a.GetPlatform() == b.GetPlatform() &&
|
|
|
|
a.GetOS() == b.GetOS() &&
|
|
|
|
a.GetOSVersion() == b.GetOSVersion() &&
|
|
|
|
a.GetWiretrusteeVersion() == b.GetWiretrusteeVersion() &&
|
|
|
|
a.GetUiVersion() == b.GetUiVersion() &&
|
|
|
|
a.GetSysSerialNumber() == b.GetSysSerialNumber() &&
|
|
|
|
a.GetSysProductName() == b.GetSysProductName() &&
|
|
|
|
a.GetSysManufacturer() == b.GetSysManufacturer()
|
2022-02-08 18:03:27 +01:00
|
|
|
}
|
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 {
|
2023-11-27 16:40:02 +01:00
|
|
|
t.Fatal(err)
|
2022-05-08 11:04:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
serverAddr := lis.Addr().String()
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
client, err := NewClient(ctx, serverAddr, testKey, false)
|
|
|
|
if err != nil {
|
2023-11-27 16:40:02 +01:00
|
|
|
t.Fatalf("error while creating testClient: %v", err)
|
2022-05-08 11:04:57 +02:00
|
|
|
}
|
|
|
|
|
2023-10-16 17:00:05 +02:00
|
|
|
expectedFlowInfo := &mgmtProto.DeviceAuthorizationFlow{
|
2022-05-08 11:04:57 +02:00
|
|
|
Provider: 0,
|
2023-10-16 17:00:05 +02:00
|
|
|
ProviderConfig: &mgmtProto.ProviderConfig{ClientID: "client"},
|
2022-05-08 11:04:57 +02:00
|
|
|
}
|
|
|
|
|
2023-10-16 17:00:05 +02:00
|
|
|
mgmtMockServer.GetDeviceAuthorizationFlowFunc = func(ctx context.Context, req *mgmtProto.EncryptedMessage) (*mgmtProto.EncryptedMessage, error) {
|
2022-05-21 15:21:39 +02:00
|
|
|
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")
|
|
|
|
}
|
2023-07-27 11:31:07 +02:00
|
|
|
|
|
|
|
func Test_GetPKCEAuthorizationFlow(t *testing.T) {
|
|
|
|
s, lis, mgmtMockServer, serverKey := startMockManagement(t)
|
|
|
|
defer s.GracefulStop()
|
|
|
|
|
|
|
|
testKey, err := wgtypes.GenerateKey()
|
|
|
|
if err != nil {
|
2023-11-27 16:40:02 +01:00
|
|
|
t.Fatal(err)
|
2023-07-27 11:31:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
serverAddr := lis.Addr().String()
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
client, err := NewClient(ctx, serverAddr, testKey, false)
|
|
|
|
if err != nil {
|
2023-11-27 16:40:02 +01:00
|
|
|
t.Fatalf("error while creating testClient: %v", err)
|
2023-07-27 11:31:07 +02:00
|
|
|
}
|
|
|
|
|
2023-10-16 17:00:05 +02:00
|
|
|
expectedFlowInfo := &mgmtProto.PKCEAuthorizationFlow{
|
|
|
|
ProviderConfig: &mgmtProto.ProviderConfig{
|
2023-07-27 11:31:07 +02:00
|
|
|
ClientID: "client",
|
|
|
|
ClientSecret: "secret",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-10-16 17:00:05 +02:00
|
|
|
mgmtMockServer.GetPKCEAuthorizationFlowFunc = func(ctx context.Context, req *mgmtProto.EncryptedMessage) (*mgmtProto.EncryptedMessage, error) {
|
2023-07-27 11:31:07 +02:00
|
|
|
encryptedResp, err := encryption.EncryptMessage(serverKey, client.key, expectedFlowInfo)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &mgmtProto.EncryptedMessage{
|
|
|
|
WgPubKey: serverKey.PublicKey().String(),
|
|
|
|
Body: encryptedResp,
|
|
|
|
Version: 0,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
flowInfo, err := client.GetPKCEAuthorizationFlow(serverKey)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("error while retrieving pkce auth flow information")
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, expectedFlowInfo.ProviderConfig.ClientID, flowInfo.ProviderConfig.ClientID, "provider configured client ID should match")
|
|
|
|
assert.Equal(t, expectedFlowInfo.ProviderConfig.ClientSecret, flowInfo.ProviderConfig.ClientSecret, "provider configured client secret should match")
|
|
|
|
}
|