From 1be8527906b53f95475b51dd1e5cef55d9467ce3 Mon Sep 17 00:00:00 2001 From: shatoboar Date: Fri, 4 Feb 2022 15:53:02 +0100 Subject: [PATCH 1/8] test: WIP mocking the grpc server for testing the sending of the client information --- management/client/client_test.go | 88 ++++++++++++++++++++- management/server/management_server_mock.go | 46 +++++++++++ 2 files changed, 130 insertions(+), 4 deletions(-) create mode 100644 management/server/management_server_mock.go diff --git a/management/client/client_test.go b/management/client/client_test.go index c80e4fd1014..f0c83d31f25 100644 --- a/management/client/client_test.go +++ b/management/client/client_test.go @@ -2,7 +2,14 @@ package client import ( "context" + "net" + "path/filepath" + "testing" + "time" + log "github.com/sirupsen/logrus" + "github.com/wiretrustee/wiretrustee/encryption" + "github.com/wiretrustee/wiretrustee/management/proto" mgmtProto "github.com/wiretrustee/wiretrustee/management/proto" mgmt "github.com/wiretrustee/wiretrustee/management/server" "github.com/wiretrustee/wiretrustee/util" @@ -10,10 +17,6 @@ import ( "google.golang.org/grpc" "google.golang.org/grpc/codes" "google.golang.org/grpc/status" - "net" - "path/filepath" - "testing" - "time" ) var tested *GrpcClient @@ -78,6 +81,54 @@ func startManagement(config *mgmt.Config, t *testing.T) (*grpc.Server, net.Liste return s, lis } +func startMockManagement(t *testing.T) (*grpc.Server, net.Listener) { + lis, err := net.Listen("tcp", ":0") + if err != nil { + t.Fatal(err) + } + + s := grpc.NewServer() + if err != nil { + t.Fatal(err) + } + + mgmtServer := &mgmt.ManagementServiceServerMock{ + //TODO: here needs to be the logic to test the equality of the metadata + 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) + } + + loginReq := &proto.LoginRequest{} + // GetServerPublicKey() + // how can we get the data access without the private key + err = encryption.DecryptMessage(peerKey, wgtypes.Key{}, msg.Body, loginReq) // need to init the privKey? + if err != nil { + log.Fatal(err) + } + + return nil, nil + }, + } + + 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 +} + func TestClient_GetServerPublicKey(t *testing.T) { key, err := tested.GetServerPublicKey() @@ -177,3 +228,32 @@ func TestClient_Sync(t *testing.T) { t.Error("timeout waiting for test to finish") } } + +func Test_SystemMetaDataFromClient(t *testing.T) { + _, lis := startMockManagement(t) + + 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.Fatal(err) + } + + key, err := testClient.GetServerPublicKey() + if err != nil { + log.Fatal(err) + } + + _, err = testClient.Register(*key, ValidKey) + if err != nil { + t.Error(err) + } + + // assert.Equalf() +} diff --git a/management/server/management_server_mock.go b/management/server/management_server_mock.go new file mode 100644 index 00000000000..8ec07789406 --- /dev/null +++ b/management/server/management_server_mock.go @@ -0,0 +1,46 @@ +package server + +import ( + "context" + + "github.com/wiretrustee/wiretrustee/management/proto" + "google.golang.org/grpc/codes" + "google.golang.org/grpc/status" +) + +type ManagementServiceServerMock struct { + proto.UnimplementedManagementServiceServer + + LoginFunc func(context.Context, *proto.EncryptedMessage) (*proto.EncryptedMessage, error) + SyncFunc func(*proto.EncryptedMessage, proto.ManagementService_SyncServer) + GetServerKeyFunc func(context.Context, *proto.Empty) (*proto.ServerKeyResponse, error) + IsHealthyFunc func(context.Context, *proto.Empty) (*proto.Empty, error) +} + +func (m ManagementServiceServerMock) Login(ctx context.Context, req *proto.EncryptedMessage) (*proto.EncryptedMessage, error) { + if m.LoginFunc != nil { + return m.LoginFunc(ctx, req) + } + return nil, status.Errorf(codes.Unimplemented, "method Login not implemented") +} + +func (m ManagementServiceServerMock) Sync(msg *proto.EncryptedMessage, sync proto.ManagementService_SyncServer) error { + if m.SyncFunc != nil { + return m.Sync(msg, sync) + } + return status.Errorf(codes.Unimplemented, "method Sync not implemented") +} + +func (m ManagementServiceServerMock) GetServerKey(ctx context.Context, empty *proto.Empty) (*proto.ServerKeyResponse, error) { + if m.GetServerKeyFunc != nil { + return m.GetServerKeyFunc(ctx, empty) + } + return nil, status.Errorf(codes.Unimplemented, "method GetServerKey not implemented") +} + +func (m ManagementServiceServerMock) IsHealthy(ctx context.Context, empty *proto.Empty) (*proto.Empty, error) { + if m.IsHealthyFunc != nil { + return m.IsHealthyFunc(ctx, empty) + } + return nil, status.Errorf(codes.Unimplemented, "method IsHealthy not implemented") +} From 42430cfe6510f0c3134f3b6452515ea73d4be42b Mon Sep 17 00:00:00 2001 From: shatoboar Date: Tue, 8 Feb 2022 14:00:51 +0100 Subject: [PATCH 2/8] WIP: Test_SystemMetaDataFromClient with mocks, todo: --- management/client/client_test.go | 67 +++++++++++++++++++++----------- 1 file changed, 45 insertions(+), 22 deletions(-) diff --git a/management/client/client_test.go b/management/client/client_test.go index f0c83d31f25..a0b371a9a78 100644 --- a/management/client/client_test.go +++ b/management/client/client_test.go @@ -4,10 +4,12 @@ import ( "context" "net" "path/filepath" + "sync" "testing" "time" log "github.com/sirupsen/logrus" + "github.com/stretchr/testify/assert" "github.com/wiretrustee/wiretrustee/encryption" "github.com/wiretrustee/wiretrustee/management/proto" mgmtProto "github.com/wiretrustee/wiretrustee/management/proto" @@ -21,6 +23,8 @@ import ( var tested *GrpcClient var serverAddr string +var mgmtMockServer *mgmt.ManagementServiceServerMock +var serverKey wgtypes.Key const ValidKey = "A2C8E62B-38F5-4553-B31E-DD66C696CEBB" @@ -92,25 +96,17 @@ func startMockManagement(t *testing.T) (*grpc.Server, net.Listener) { t.Fatal(err) } - mgmtServer := &mgmt.ManagementServiceServerMock{ - //TODO: here needs to be the logic to test the equality of the metadata - LoginFunc: func(ctx context.Context, msg *proto.EncryptedMessage) (*proto.EncryptedMessage, error) { + serverKey, err = wgtypes.GenerateKey() + if err != nil { + t.Fatal(err) + } - 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) + mgmtMockServer = &mgmt.ManagementServiceServerMock{ + GetServerKeyFunc: func(context.Context, *proto.Empty) (*proto.ServerKeyResponse, error) { + response := &proto.ServerKeyResponse{ + Key: serverKey.PublicKey().String(), } - - loginReq := &proto.LoginRequest{} - // GetServerPublicKey() - // how can we get the data access without the private key - err = encryption.DecryptMessage(peerKey, wgtypes.Key{}, msg.Body, loginReq) // need to init the privKey? - if err != nil { - log.Fatal(err) - } - - return nil, nil + return response, nil }, } @@ -118,7 +114,7 @@ func startMockManagement(t *testing.T) (*grpc.Server, net.Listener) { t.Fatal(err) } - mgmtProto.RegisterManagementServiceServer(s, mgmtServer) + mgmtProto.RegisterManagementServiceServer(s, mgmtMockServer) go func() { if err := s.Serve(lis); err != nil { t.Error(err) @@ -242,18 +238,45 @@ func Test_SystemMetaDataFromClient(t *testing.T) { testClient, err := NewClient(ctx, serverAddr, testKey, false) if err != nil { - log.Fatal(err) + log.Fatalf("error while creating testClient: %v", err) } key, err := testClient.GetServerPublicKey() if err != nil { - log.Fatal(err) + log.Fatalf("error while getting server public key from testclient, %v", err) } _, err = testClient.Register(*key, ValidKey) if err != nil { - t.Error(err) + t.Errorf("error while trying to register client: %v", err) } - // assert.Equalf() + var actualMeta *proto.PeerSystemMeta + var actualValidKey string + var wg sync.WaitGroup + wg.Add(1) + + 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) + } + + loginReq := &proto.LoginRequest{} + err = encryption.DecryptMessage(peerKey, serverKey, msg.Body, loginReq) + if err != nil { + log.Fatal(err) + } + + actualMeta = loginReq.GetMeta() + actualValidKey = loginReq.GetSetupKey() + wg.Done() + return nil, nil + } + + wg.Wait() + assert.Equal(t, ValidKey, actualValidKey) + assert.Equal(t, "development", actualMeta.WiretrusteeVersion) } From 3f10b0afc5f7fdd55dfd8e19a40d74242b8ef874 Mon Sep 17 00:00:00 2001 From: braginini Date: Tue, 8 Feb 2022 14:45:59 +0100 Subject: [PATCH 3/8] fix: failing meta data test --- go.mod | 4 ++-- management/client/client_test.go | 30 +++++++++++++++++------------- management/proto/management.proto | 2 ++ 3 files changed, 21 insertions(+), 15 deletions(-) diff --git a/go.mod b/go.mod index d7afe81439f..2b8e04d3e33 100644 --- a/go.mod +++ b/go.mod @@ -30,6 +30,8 @@ require ( require ( github.com/cakturk/go-netstat v0.0.0-20200220111822-e5b49efee7a5 github.com/magiconair/properties v1.8.5 + github.com/pion/logging v0.2.2 + github.com/pion/turn/v2 v2.0.5 github.com/rs/xid v1.3.0 github.com/stretchr/testify v1.7.0 ) @@ -46,12 +48,10 @@ require ( github.com/mdlayher/socket v0.0.0-20211102153432-57e3fa563ecb // indirect github.com/nxadm/tail v1.4.8 // indirect github.com/pion/dtls/v2 v2.0.12 // indirect - github.com/pion/logging v0.2.2 // indirect github.com/pion/mdns v0.0.5 // indirect github.com/pion/randutil v0.1.0 // indirect github.com/pion/stun v0.3.5 // indirect github.com/pion/transport v0.12.3 // indirect - github.com/pion/turn/v2 v2.0.5 // indirect github.com/pion/udp v0.1.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect github.com/vishvananda/netns v0.0.0-20191106174202-0a2b9b5464df // indirect diff --git a/management/client/client_test.go b/management/client/client_test.go index a0b371a9a78..9a351135346 100644 --- a/management/client/client_test.go +++ b/management/client/client_test.go @@ -92,9 +92,6 @@ func startMockManagement(t *testing.T) (*grpc.Server, net.Listener) { } s := grpc.NewServer() - if err != nil { - t.Fatal(err) - } serverKey, err = wgtypes.GenerateKey() if err != nil { @@ -110,10 +107,6 @@ func startMockManagement(t *testing.T) (*grpc.Server, net.Listener) { }, } - if err != nil { - t.Fatal(err) - } - mgmtProto.RegisterManagementServiceServer(s, mgmtMockServer) go func() { if err := s.Serve(lis); err != nil { @@ -246,11 +239,6 @@ func Test_SystemMetaDataFromClient(t *testing.T) { log.Fatalf("error while getting server public key from testclient, %v", err) } - _, err = testClient.Register(*key, ValidKey) - if err != nil { - t.Errorf("error while trying to register client: %v", err) - } - var actualMeta *proto.PeerSystemMeta var actualValidKey string var wg sync.WaitGroup @@ -273,9 +261,25 @@ func Test_SystemMetaDataFromClient(t *testing.T) { actualMeta = loginReq.GetMeta() actualValidKey = loginReq.GetSetupKey() wg.Done() - return nil, nil + + loginResp := &proto.LoginResponse{} + encryptedResp, err := encryption.EncryptMessage(peerKey, serverKey, loginResp) + if err != nil { + return nil, err + } + + return &mgmtProto.EncryptedMessage{ + WgPubKey: serverKey.PublicKey().String(), + Body: encryptedResp, + Version: 0, + }, nil } + _, err = testClient.Register(*key, ValidKey) + if err != nil { + t.Errorf("error while trying to register client: %v", err) + } + wg.Wait() assert.Equal(t, ValidKey, actualValidKey) assert.Equal(t, "development", actualMeta.WiretrusteeVersion) diff --git a/management/proto/management.proto b/management/proto/management.proto index bf3454199b6..67dee0bcb5a 100644 --- a/management/proto/management.proto +++ b/management/proto/management.proto @@ -9,11 +9,13 @@ package management; service ManagementService { // Login logs in peer. In case server returns codes.PermissionDenied this endpoint can be used to register Peer providing LoginRequest.setupKey + // Returns encrypted LoginResponse in EncryptedMessage.Body rpc Login(EncryptedMessage) returns (EncryptedMessage) {} // Sync enables peer synchronization. Each peer that is connected to this stream will receive updates from the server. // For example, if a new peer has been added to an account all other connected peers will receive this peer's Wireguard public key as an update // The initial SyncResponse contains all of the available peers so the local state can be refreshed + // Returns encrypted SyncResponse in EncryptedMessage.Body rpc Sync(EncryptedMessage) returns (stream EncryptedMessage) {} // Exposes a Wireguard public key of the Management service. From 573a9c2f7f79a12164859aa39e13521e07ea2a82 Mon Sep 17 00:00:00 2001 From: braginini Date: Tue, 8 Feb 2022 14:59:22 +0100 Subject: [PATCH 4/8] test: add system meta expectation in management client test --- management/client/client.go | 3 +++ management/client/client_test.go | 17 +++++++++++++++-- management/client/grpc.go | 16 ++++++++++++++++ management/client/mock.go | 9 +++++++++ 4 files changed, 43 insertions(+), 2 deletions(-) diff --git a/management/client/client.go b/management/client/client.go index e881fb40d79..e32aa5b3dd4 100644 --- a/management/client/client.go +++ b/management/client/client.go @@ -1,6 +1,7 @@ package client import ( + "github.com/wiretrustee/wiretrustee/client/system" "github.com/wiretrustee/wiretrustee/management/proto" "golang.zx2c4.com/wireguard/wgctrl/wgtypes" "io" @@ -10,6 +11,8 @@ type Client interface { io.Closer Sync(msgHandler func(msg *proto.SyncResponse) error) error GetServerPublicKey() (*wgtypes.Key, error) + // Register Deprecated Register(serverKey wgtypes.Key, setupKey string) (*proto.LoginResponse, error) + RegisterV2(serverKey wgtypes.Key, setupKey string, info *system.Info) (*proto.LoginResponse, error) Login(serverKey wgtypes.Key) (*proto.LoginResponse, error) } diff --git a/management/client/client_test.go b/management/client/client_test.go index 9a351135346..f583f0a1eef 100644 --- a/management/client/client_test.go +++ b/management/client/client_test.go @@ -2,6 +2,7 @@ package client import ( "context" + "github.com/wiretrustee/wiretrustee/client/system" "net" "path/filepath" "sync" @@ -275,12 +276,24 @@ func Test_SystemMetaDataFromClient(t *testing.T) { }, nil } - _, err = testClient.Register(*key, ValidKey) + info := system.GetInfo() + _, err = testClient.RegisterV2(*key, ValidKey, info) 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, "development", actualMeta.WiretrusteeVersion) + assert.Equal(t, expectedMeta, actualMeta) } diff --git a/management/client/grpc.go b/management/client/grpc.go index 369f0dde3a5..ac9393298f9 100644 --- a/management/client/grpc.go +++ b/management/client/grpc.go @@ -233,6 +233,7 @@ func (c *GrpcClient) login(serverKey wgtypes.Key, req *proto.LoginRequest) (*pro // Register registers peer on Management Server. It actually calls a Login endpoint with a provided setup key // Takes care of encrypting and decrypting messages. // This method will also collect system info and send it with the request (e.g. hostname, os, etc) +//Deprecated func (c *GrpcClient) Register(serverKey wgtypes.Key, setupKey string) (*proto.LoginResponse, error) { gi := system.GetInfo() meta := &proto.PeerSystemMeta{ @@ -248,6 +249,21 @@ func (c *GrpcClient) Register(serverKey wgtypes.Key, setupKey string) (*proto.Lo return c.login(serverKey, &proto.LoginRequest{SetupKey: setupKey, Meta: meta}) } +// Register registers peer on Management Server. It actually calls a Login endpoint with a provided setup key and system information +// Takes care of encrypting and decrypting messages. +func (c *GrpcClient) RegisterV2(serverKey wgtypes.Key, setupKey string, info *system.Info) (*proto.LoginResponse, error) { + meta := &proto.PeerSystemMeta{ + Hostname: info.Hostname, + GoOS: info.GoOS, + OS: info.OS, + Core: info.OSVersion, + Platform: info.Platform, + Kernel: info.Kernel, + WiretrusteeVersion: info.WiretrusteeVersion, + } + return c.login(serverKey, &proto.LoginRequest{SetupKey: setupKey, Meta: meta}) +} + // Login attempts login to Management Server. Takes care of encrypting and decrypting messages. func (c *GrpcClient) Login(serverKey wgtypes.Key) (*proto.LoginResponse, error) { return c.login(serverKey, &proto.LoginRequest{}) diff --git a/management/client/mock.go b/management/client/mock.go index af99ba8632f..3195edfe35c 100644 --- a/management/client/mock.go +++ b/management/client/mock.go @@ -1,6 +1,7 @@ package client import ( + "github.com/wiretrustee/wiretrustee/client/system" "github.com/wiretrustee/wiretrustee/management/proto" "golang.zx2c4.com/wireguard/wgctrl/wgtypes" ) @@ -10,6 +11,7 @@ type MockClient struct { SyncFunc func(msgHandler func(msg *proto.SyncResponse) error) error GetServerPublicKeyFunc func() (*wgtypes.Key, error) RegisterFunc func(serverKey wgtypes.Key, setupKey string) (*proto.LoginResponse, error) + RegisterV2Func func(serverKey wgtypes.Key, setupKey string, info *system.Info) (*proto.LoginResponse, error) LoginFunc func(serverKey wgtypes.Key) (*proto.LoginResponse, error) } @@ -41,6 +43,13 @@ func (m *MockClient) Register(serverKey wgtypes.Key, setupKey string) (*proto.Lo return m.RegisterFunc(serverKey, setupKey) } +func (m *MockClient) RegisterV2(serverKey wgtypes.Key, setupKey string, info *system.Info) (*proto.LoginResponse, error) { + if m.RegisterV2Func == nil { + return nil, nil + } + return m.RegisterV2Func(serverKey, setupKey, info) +} + func (m *MockClient) Login(serverKey wgtypes.Key) (*proto.LoginResponse, error) { if m.LoginFunc == nil { return nil, nil From 504ba80546571005cd4744cb24219f0fa11cc77c Mon Sep 17 00:00:00 2001 From: shatoboar Date: Tue, 8 Feb 2022 15:42:37 +0100 Subject: [PATCH 5/8] fix: removing deprecated register function, replacing with new one --- client/cmd/login.go | 7 +++++-- client/internal/engine_test.go | 20 ++++++++++++-------- management/client/client_test.go | 16 +++++++++++----- management/client/grpc.go | 20 +------------------- 4 files changed, 29 insertions(+), 34 deletions(-) diff --git a/client/cmd/login.go b/client/cmd/login.go index e439b8e8f58..5c3a25f4d19 100644 --- a/client/cmd/login.go +++ b/client/cmd/login.go @@ -4,17 +4,19 @@ import ( "bufio" "context" "fmt" + "os" + "github.com/google/uuid" log "github.com/sirupsen/logrus" "github.com/spf13/cobra" "github.com/wiretrustee/wiretrustee/client/internal" + "github.com/wiretrustee/wiretrustee/client/system" mgm "github.com/wiretrustee/wiretrustee/management/client" mgmProto "github.com/wiretrustee/wiretrustee/management/proto" "github.com/wiretrustee/wiretrustee/util" "golang.zx2c4.com/wireguard/wgctrl/wgtypes" "google.golang.org/grpc/codes" "google.golang.org/grpc/status" - "os" ) var ( @@ -118,7 +120,8 @@ func registerPeer(serverPublicKey wgtypes.Key, client *mgm.GrpcClient, setupKey } log.Debugf("sending peer registration request to Management Service") - loginResp, err := client.Register(serverPublicKey, validSetupKey.String()) + info := system.GetInfo() + loginResp, err := client.Register(serverPublicKey, validSetupKey.String(), info) if err != nil { log.Errorf("failed registering peer %v", err) return nil, err diff --git a/client/internal/engine_test.go b/client/internal/engine_test.go index bda009a6019..8ab23cd23d2 100644 --- a/client/internal/engine_test.go +++ b/client/internal/engine_test.go @@ -3,7 +3,16 @@ package internal import ( "context" "fmt" + "net" + "os" + "path/filepath" + "runtime" + "sync" + "testing" + "time" + log "github.com/sirupsen/logrus" + "github.com/wiretrustee/wiretrustee/client/system" mgmt "github.com/wiretrustee/wiretrustee/management/client" mgmtProto "github.com/wiretrustee/wiretrustee/management/proto" "github.com/wiretrustee/wiretrustee/management/server" @@ -14,13 +23,6 @@ import ( "golang.zx2c4.com/wireguard/wgctrl/wgtypes" "google.golang.org/grpc" "google.golang.org/grpc/keepalive" - "net" - "os" - "path/filepath" - "runtime" - "sync" - "testing" - "time" ) var ( @@ -382,7 +384,9 @@ func createEngine(ctx context.Context, cancel context.CancelFunc, setupKey strin return nil, err } - resp, err := mgmtClient.Register(*publicKey, setupKey) + info := system.GetInfo() + resp, err := mgmtClient.Register(*publicKey, setupKey, info) + // resp, err := mgmtClient.Register(*publicKey, setupKey) if err != nil { return nil, err } diff --git a/management/client/client_test.go b/management/client/client_test.go index f583f0a1eef..b9c745485f4 100644 --- a/management/client/client_test.go +++ b/management/client/client_test.go @@ -2,13 +2,14 @@ package client import ( "context" - "github.com/wiretrustee/wiretrustee/client/system" "net" "path/filepath" "sync" "testing" "time" + "github.com/wiretrustee/wiretrustee/client/system" + log "github.com/sirupsen/logrus" "github.com/stretchr/testify/assert" "github.com/wiretrustee/wiretrustee/encryption" @@ -150,7 +151,9 @@ func TestClient_LoginRegistered(t *testing.T) { if err != nil { t.Error(err) } - resp, err := tested.Register(*key, ValidKey) + info := system.GetInfo() + resp, err := tested.Register(*key, ValidKey, info) + // resp, err := tested.Register(*key, ValidKey) if err != nil { t.Error(err) } @@ -166,7 +169,8 @@ func TestClient_Sync(t *testing.T) { t.Error(err) } - _, err = tested.Register(*serverKey, ValidKey) + info := system.GetInfo() + _, err = tested.Register(*serverKey, ValidKey, info) if err != nil { t.Error(err) } @@ -180,7 +184,9 @@ func TestClient_Sync(t *testing.T) { if err != nil { t.Fatal(err) } - _, err = remoteClient.Register(*serverKey, ValidKey) + + info = system.GetInfo() + _, err = remoteClient.Register(*serverKey, ValidKey, info) if err != nil { t.Fatal(err) } @@ -277,7 +283,7 @@ func Test_SystemMetaDataFromClient(t *testing.T) { } info := system.GetInfo() - _, err = testClient.RegisterV2(*key, ValidKey, info) + _, err = testClient.Register(*key, ValidKey, info) if err != nil { t.Errorf("error while trying to register client: %v", err) } diff --git a/management/client/grpc.go b/management/client/grpc.go index ac9393298f9..28d90c8263e 100644 --- a/management/client/grpc.go +++ b/management/client/grpc.go @@ -233,25 +233,7 @@ func (c *GrpcClient) login(serverKey wgtypes.Key, req *proto.LoginRequest) (*pro // Register registers peer on Management Server. It actually calls a Login endpoint with a provided setup key // Takes care of encrypting and decrypting messages. // This method will also collect system info and send it with the request (e.g. hostname, os, etc) -//Deprecated -func (c *GrpcClient) Register(serverKey wgtypes.Key, setupKey string) (*proto.LoginResponse, error) { - gi := system.GetInfo() - meta := &proto.PeerSystemMeta{ - Hostname: gi.Hostname, - GoOS: gi.GoOS, - OS: gi.OS, - Core: gi.OSVersion, - Platform: gi.Platform, - Kernel: gi.Kernel, - WiretrusteeVersion: gi.WiretrusteeVersion, - } - log.Debugf("detected system %v", meta) - return c.login(serverKey, &proto.LoginRequest{SetupKey: setupKey, Meta: meta}) -} - -// Register registers peer on Management Server. It actually calls a Login endpoint with a provided setup key and system information -// Takes care of encrypting and decrypting messages. -func (c *GrpcClient) RegisterV2(serverKey wgtypes.Key, setupKey string, info *system.Info) (*proto.LoginResponse, error) { +func (c *GrpcClient) Register(serverKey wgtypes.Key, setupKey string, info *system.Info) (*proto.LoginResponse, error) { meta := &proto.PeerSystemMeta{ Hostname: info.Hostname, GoOS: info.GoOS, From 432f28403647231adf2eea1d31c779f041a9216b Mon Sep 17 00:00:00 2001 From: shatoboar Date: Tue, 8 Feb 2022 15:57:49 +0100 Subject: [PATCH 6/8] fix: removing deprecated register function from mockclient interface impl --- management/client/client.go | 6 +++--- management/client/mock.go | 14 +++----------- 2 files changed, 6 insertions(+), 14 deletions(-) diff --git a/management/client/client.go b/management/client/client.go index e32aa5b3dd4..8561d8ab13f 100644 --- a/management/client/client.go +++ b/management/client/client.go @@ -1,10 +1,10 @@ package client import ( - "github.com/wiretrustee/wiretrustee/client/system" + "io" + "github.com/wiretrustee/wiretrustee/management/proto" "golang.zx2c4.com/wireguard/wgctrl/wgtypes" - "io" ) type Client interface { @@ -13,6 +13,6 @@ type Client interface { GetServerPublicKey() (*wgtypes.Key, error) // Register Deprecated Register(serverKey wgtypes.Key, setupKey string) (*proto.LoginResponse, error) - RegisterV2(serverKey wgtypes.Key, setupKey string, info *system.Info) (*proto.LoginResponse, error) + // RegisterV2(serverKey wgtypes.Key, setupKey string, info *system.Info) (*proto.LoginResponse, error) Login(serverKey wgtypes.Key) (*proto.LoginResponse, error) } diff --git a/management/client/mock.go b/management/client/mock.go index 3195edfe35c..ac4c64c07e2 100644 --- a/management/client/mock.go +++ b/management/client/mock.go @@ -10,8 +10,7 @@ type MockClient struct { CloseFunc func() error SyncFunc func(msgHandler func(msg *proto.SyncResponse) error) error GetServerPublicKeyFunc func() (*wgtypes.Key, error) - RegisterFunc func(serverKey wgtypes.Key, setupKey string) (*proto.LoginResponse, error) - RegisterV2Func func(serverKey wgtypes.Key, setupKey string, info *system.Info) (*proto.LoginResponse, error) + RegisterFunc func(serverKey wgtypes.Key, setupKey string, info *system.Info) (*proto.LoginResponse, error) LoginFunc func(serverKey wgtypes.Key) (*proto.LoginResponse, error) } @@ -36,18 +35,11 @@ func (m *MockClient) GetServerPublicKey() (*wgtypes.Key, error) { return m.GetServerPublicKeyFunc() } -func (m *MockClient) Register(serverKey wgtypes.Key, setupKey string) (*proto.LoginResponse, error) { +func (m *MockClient) Register(serverKey wgtypes.Key, setupKey string, info *system.Info) (*proto.LoginResponse, error) { if m.RegisterFunc == nil { return nil, nil } - return m.RegisterFunc(serverKey, setupKey) -} - -func (m *MockClient) RegisterV2(serverKey wgtypes.Key, setupKey string, info *system.Info) (*proto.LoginResponse, error) { - if m.RegisterV2Func == nil { - return nil, nil - } - return m.RegisterV2Func(serverKey, setupKey, info) + return m.RegisterFunc(serverKey, setupKey, info) } func (m *MockClient) Login(serverKey wgtypes.Key) (*proto.LoginResponse, error) { From a789083f66cad37f094230cc0660f33a6a6acca7 Mon Sep 17 00:00:00 2001 From: shatoboar Date: Tue, 8 Feb 2022 16:08:34 +0100 Subject: [PATCH 7/8] fix: fixing interface declaration --- client/cmd/up.go | 4 ++-- client/internal/engine.go | 9 +++++---- management/client/client.go | 4 ++-- 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/client/cmd/up.go b/client/cmd/up.go index 4b333da21a5..e03b7e4dc70 100644 --- a/client/cmd/up.go +++ b/client/cmd/up.go @@ -2,6 +2,8 @@ package cmd import ( "context" + "time" + "github.com/cenkalti/backoff/v4" "github.com/kardianos/service" log "github.com/sirupsen/logrus" @@ -13,7 +15,6 @@ import ( "golang.zx2c4.com/wireguard/wgctrl/wgtypes" "google.golang.org/grpc/codes" "google.golang.org/grpc/status" - "time" ) var ( @@ -186,7 +187,6 @@ func runClient() error { return err } - // create start the Wiretrustee Engine that will connect to the Signal and Management streams and manage connections to remote peers. engine := internal.NewEngine(signalClient, mgmClient, engineConfig, cancel, ctx) err = engine.Start() if err != nil { diff --git a/client/internal/engine.go b/client/internal/engine.go index 8b4e3dfec22..9b92df5ad79 100644 --- a/client/internal/engine.go +++ b/client/internal/engine.go @@ -3,6 +3,11 @@ package internal import ( "context" "fmt" + "math/rand" + "strings" + "sync" + "time" + "github.com/pion/ice/v2" log "github.com/sirupsen/logrus" "github.com/wiretrustee/wiretrustee/client/internal/peer" @@ -14,10 +19,6 @@ import ( sProto "github.com/wiretrustee/wiretrustee/signal/proto" "github.com/wiretrustee/wiretrustee/util" "golang.zx2c4.com/wireguard/wgctrl/wgtypes" - "math/rand" - "strings" - "sync" - "time" ) // PeerConnectionTimeoutMax is a timeout of an initial connection attempt to a remote peer. diff --git a/management/client/client.go b/management/client/client.go index 8561d8ab13f..98a4ce1b96b 100644 --- a/management/client/client.go +++ b/management/client/client.go @@ -3,6 +3,7 @@ package client import ( "io" + "github.com/wiretrustee/wiretrustee/client/system" "github.com/wiretrustee/wiretrustee/management/proto" "golang.zx2c4.com/wireguard/wgctrl/wgtypes" ) @@ -11,8 +12,7 @@ type Client interface { io.Closer Sync(msgHandler func(msg *proto.SyncResponse) error) error GetServerPublicKey() (*wgtypes.Key, error) - // Register Deprecated - Register(serverKey wgtypes.Key, setupKey string) (*proto.LoginResponse, error) + Register(serverKey wgtypes.Key, setupKey string, info *system.Info) (*proto.LoginResponse, error) // RegisterV2(serverKey wgtypes.Key, setupKey string, info *system.Info) (*proto.LoginResponse, error) Login(serverKey wgtypes.Key) (*proto.LoginResponse, error) } From 1ae813107c49d76b208ff9e2e64a8693b76610b9 Mon Sep 17 00:00:00 2001 From: braginini Date: Tue, 8 Feb 2022 17:40:57 +0100 Subject: [PATCH 8/8] chore: remove unused commented code --- client/internal/engine_test.go | 1 - management/client/client.go | 1 - management/client/client_test.go | 1 - 3 files changed, 3 deletions(-) diff --git a/client/internal/engine_test.go b/client/internal/engine_test.go index 8ab23cd23d2..8a9d154b121 100644 --- a/client/internal/engine_test.go +++ b/client/internal/engine_test.go @@ -386,7 +386,6 @@ func createEngine(ctx context.Context, cancel context.CancelFunc, setupKey strin info := system.GetInfo() resp, err := mgmtClient.Register(*publicKey, setupKey, info) - // resp, err := mgmtClient.Register(*publicKey, setupKey) if err != nil { return nil, err } diff --git a/management/client/client.go b/management/client/client.go index 98a4ce1b96b..76dc3ad0350 100644 --- a/management/client/client.go +++ b/management/client/client.go @@ -13,6 +13,5 @@ type Client interface { Sync(msgHandler func(msg *proto.SyncResponse) error) error GetServerPublicKey() (*wgtypes.Key, error) Register(serverKey wgtypes.Key, setupKey string, info *system.Info) (*proto.LoginResponse, error) - // RegisterV2(serverKey wgtypes.Key, setupKey string, info *system.Info) (*proto.LoginResponse, error) Login(serverKey wgtypes.Key) (*proto.LoginResponse, error) } diff --git a/management/client/client_test.go b/management/client/client_test.go index b9c745485f4..a93c253bced 100644 --- a/management/client/client_test.go +++ b/management/client/client_test.go @@ -153,7 +153,6 @@ func TestClient_LoginRegistered(t *testing.T) { } info := system.GetInfo() resp, err := tested.Register(*key, ValidKey, info) - // resp, err := tested.Register(*key, ValidKey) if err != nil { t.Error(err) }