2022-01-18 16:44:58 +01:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-08-14 12:30:10 +02:00
|
|
|
"errors"
|
2022-01-18 16:44:58 +01:00
|
|
|
"fmt"
|
2022-02-03 18:35:54 +01:00
|
|
|
"io"
|
2023-03-16 17:22:36 +01:00
|
|
|
"sync"
|
2022-02-03 18:35:54 +01:00
|
|
|
"time"
|
|
|
|
|
2023-03-16 17:22:36 +01:00
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
gstatus "google.golang.org/grpc/status"
|
|
|
|
|
2024-07-17 16:26:06 +02:00
|
|
|
"github.com/cenkalti/backoff/v4"
|
2022-01-18 16:44:58 +01:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
|
|
|
|
"google.golang.org/grpc"
|
|
|
|
"google.golang.org/grpc/connectivity"
|
2023-04-10 18:22:25 +02:00
|
|
|
|
2023-03-16 17:22:36 +01:00
|
|
|
"github.com/netbirdio/netbird/client/system"
|
|
|
|
"github.com/netbirdio/netbird/encryption"
|
|
|
|
"github.com/netbirdio/netbird/management/proto"
|
2024-04-08 18:56:52 +02:00
|
|
|
nbgrpc "github.com/netbirdio/netbird/util/grpc"
|
2022-01-18 16:44:58 +01:00
|
|
|
)
|
|
|
|
|
2024-03-01 15:17:35 +01:00
|
|
|
const ConnectTimeout = 10 * time.Second
|
|
|
|
|
2024-06-13 13:24:24 +02:00
|
|
|
const (
|
|
|
|
errMsgMgmtPublicKey = "failed getting Management Service public key: %s"
|
|
|
|
errMsgNoMgmtConnection = "no connection to management"
|
|
|
|
)
|
|
|
|
|
2023-03-16 17:22:36 +01:00
|
|
|
// ConnStateNotifier is a wrapper interface of the status recorders
|
|
|
|
type ConnStateNotifier interface {
|
2024-01-22 12:20:24 +01:00
|
|
|
MarkManagementDisconnected(error)
|
2023-03-16 17:22:36 +01:00
|
|
|
MarkManagementConnected()
|
|
|
|
}
|
|
|
|
|
2022-01-18 16:44:58 +01:00
|
|
|
type GrpcClient struct {
|
2023-03-16 17:22:36 +01:00
|
|
|
key wgtypes.Key
|
|
|
|
realClient proto.ManagementServiceClient
|
|
|
|
ctx context.Context
|
|
|
|
conn *grpc.ClientConn
|
|
|
|
connStateCallback ConnStateNotifier
|
|
|
|
connStateCallbackLock sync.RWMutex
|
2022-01-18 16:44:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewClient creates a new client to Management service
|
|
|
|
func NewClient(ctx context.Context, addr string, ourPrivateKey wgtypes.Key, tlsEnabled bool) (*GrpcClient, error) {
|
2024-07-17 16:26:06 +02:00
|
|
|
var conn *grpc.ClientConn
|
2022-01-18 16:44:58 +01:00
|
|
|
|
2024-07-17 16:26:06 +02:00
|
|
|
operation := func() error {
|
|
|
|
var err error
|
|
|
|
conn, err = nbgrpc.CreateConnection(addr, tlsEnabled)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("createConnection error: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2022-01-18 16:44:58 +01:00
|
|
|
}
|
|
|
|
|
2024-07-17 16:26:06 +02:00
|
|
|
err := backoff.Retry(operation, nbgrpc.Backoff(ctx))
|
2022-01-18 16:44:58 +01:00
|
|
|
if err != nil {
|
2024-07-17 16:26:06 +02:00
|
|
|
log.Errorf("failed creating connection to Management Service: %v", err)
|
2022-01-18 16:44:58 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
realClient := proto.NewManagementServiceClient(conn)
|
|
|
|
|
|
|
|
return &GrpcClient{
|
2023-03-16 17:22:36 +01:00
|
|
|
key: ourPrivateKey,
|
|
|
|
realClient: realClient,
|
|
|
|
ctx: ctx,
|
|
|
|
conn: conn,
|
|
|
|
connStateCallbackLock: sync.RWMutex{},
|
2022-01-18 16:44:58 +01:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close closes connection to the Management Service
|
|
|
|
func (c *GrpcClient) Close() error {
|
|
|
|
return c.conn.Close()
|
|
|
|
}
|
|
|
|
|
2023-03-16 17:22:36 +01:00
|
|
|
// SetConnStateListener set the ConnStateNotifier
|
|
|
|
func (c *GrpcClient) SetConnStateListener(notifier ConnStateNotifier) {
|
|
|
|
c.connStateCallbackLock.Lock()
|
|
|
|
defer c.connStateCallbackLock.Unlock()
|
|
|
|
c.connStateCallback = notifier
|
|
|
|
}
|
|
|
|
|
2022-03-08 14:47:55 +01:00
|
|
|
// defaultBackoff is a basic backoff mechanism for general issues
|
2022-01-18 16:44:58 +01:00
|
|
|
func defaultBackoff(ctx context.Context) backoff.BackOff {
|
|
|
|
return backoff.WithContext(&backoff.ExponentialBackOff{
|
|
|
|
InitialInterval: 800 * time.Millisecond,
|
2022-07-02 20:38:16 +02:00
|
|
|
RandomizationFactor: 1,
|
|
|
|
Multiplier: 1.7,
|
2022-01-18 16:44:58 +01:00
|
|
|
MaxInterval: 10 * time.Second,
|
2022-07-02 20:38:16 +02:00
|
|
|
MaxElapsedTime: 3 * 30 * 24 * time.Hour, // 3 months
|
2022-01-18 16:44:58 +01:00
|
|
|
Stop: backoff.Stop,
|
|
|
|
Clock: backoff.SystemClock,
|
|
|
|
}, ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ready indicates whether the client is okay and ready to be used
|
|
|
|
// for now it just checks whether gRPC connection to the service is ready
|
|
|
|
func (c *GrpcClient) ready() bool {
|
|
|
|
return c.conn.GetState() == connectivity.Ready || c.conn.GetState() == connectivity.Idle
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sync wraps the real client's Sync endpoint call and takes care of retries and encryption/decryption of messages
|
|
|
|
// Blocking request. The result will be sent via msgHandler callback function
|
2024-06-13 13:24:24 +02:00
|
|
|
func (c *GrpcClient) Sync(ctx context.Context, sysInfo *system.Info, msgHandler func(msg *proto.SyncResponse) error) error {
|
2022-01-18 16:44:58 +01:00
|
|
|
operation := func() error {
|
|
|
|
log.Debugf("management connection state %v", c.conn.GetState())
|
2022-07-02 20:38:16 +02:00
|
|
|
connState := c.conn.GetState()
|
2024-06-13 13:24:24 +02:00
|
|
|
|
2022-07-02 20:38:16 +02:00
|
|
|
if connState == connectivity.Shutdown {
|
|
|
|
return backoff.Permanent(fmt.Errorf("connection to management has been shut down"))
|
|
|
|
} else if !(connState == connectivity.Ready || connState == connectivity.Idle) {
|
2024-05-07 18:50:34 +02:00
|
|
|
c.conn.WaitForStateChange(ctx, connState)
|
2022-07-02 20:38:16 +02:00
|
|
|
return fmt.Errorf("connection to management is not ready and in %s state", connState)
|
2022-01-18 16:44:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
serverPubKey, err := c.GetServerPublicKey()
|
|
|
|
if err != nil {
|
2024-06-13 13:24:24 +02:00
|
|
|
log.Debugf(errMsgMgmtPublicKey, err)
|
2022-01-18 16:44:58 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-06-13 13:24:24 +02:00
|
|
|
return c.handleStream(ctx, *serverPubKey, sysInfo, msgHandler)
|
2022-01-18 16:44:58 +01:00
|
|
|
}
|
|
|
|
|
2024-06-13 13:24:24 +02:00
|
|
|
err := backoff.Retry(operation, defaultBackoff(ctx))
|
2022-01-18 16:44:58 +01:00
|
|
|
if err != nil {
|
2022-07-02 20:38:16 +02:00
|
|
|
log.Warnf("exiting the Management service connection retry loop due to the unrecoverable error: %s", err)
|
2024-06-13 13:24:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *GrpcClient) handleStream(ctx context.Context, serverPubKey wgtypes.Key, sysInfo *system.Info,
|
|
|
|
msgHandler func(msg *proto.SyncResponse) error) error {
|
|
|
|
ctx, cancelStream := context.WithCancel(ctx)
|
|
|
|
defer cancelStream()
|
|
|
|
|
|
|
|
stream, err := c.connectToStream(ctx, serverPubKey, sysInfo)
|
|
|
|
if err != nil {
|
|
|
|
log.Debugf("failed to open Management Service stream: %s", err)
|
|
|
|
if s, ok := gstatus.FromError(err); ok && s.Code() == codes.PermissionDenied {
|
|
|
|
return backoff.Permanent(err) // unrecoverable error, propagate to the upper layer
|
|
|
|
}
|
2022-01-18 16:44:58 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-06-13 13:24:24 +02:00
|
|
|
log.Infof("connected to the Management Service stream")
|
|
|
|
c.notifyConnected()
|
|
|
|
|
|
|
|
// blocking until error
|
|
|
|
err = c.receiveEvents(stream, serverPubKey, msgHandler)
|
|
|
|
if err != nil {
|
|
|
|
s, _ := gstatus.FromError(err)
|
|
|
|
switch s.Code() {
|
|
|
|
case codes.PermissionDenied:
|
|
|
|
return backoff.Permanent(err) // unrecoverable error, propagate to the upper layer
|
|
|
|
case codes.Canceled:
|
|
|
|
log.Debugf("management connection context has been canceled, this usually indicates shutdown")
|
|
|
|
return nil
|
|
|
|
default:
|
|
|
|
c.notifyDisconnected(err)
|
|
|
|
log.Warnf("disconnected from the Management service but will retry silently. Reason: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-18 16:44:58 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-06-12 14:43:55 +02:00
|
|
|
// GetNetworkMap return with the network map
|
2024-06-13 13:24:24 +02:00
|
|
|
func (c *GrpcClient) GetNetworkMap(sysInfo *system.Info) (*proto.NetworkMap, error) {
|
2023-04-17 11:15:37 +02:00
|
|
|
serverPubKey, err := c.GetServerPublicKey()
|
|
|
|
if err != nil {
|
|
|
|
log.Debugf("failed getting Management Service public key: %s", err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx, cancelStream := context.WithCancel(c.ctx)
|
|
|
|
defer cancelStream()
|
2024-06-13 13:24:24 +02:00
|
|
|
stream, err := c.connectToStream(ctx, *serverPubKey, sysInfo)
|
2023-04-17 11:15:37 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Debugf("failed to open Management Service stream: %s", err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
_ = stream.CloseSend()
|
|
|
|
}()
|
|
|
|
|
|
|
|
update, err := stream.Recv()
|
|
|
|
if err == io.EOF {
|
|
|
|
log.Debugf("Management stream has been closed by server: %s", err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
log.Debugf("disconnected from Management Service sync stream: %v", err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
decryptedResp := &proto.SyncResponse{}
|
|
|
|
err = encryption.DecryptMessage(*serverPubKey, c.key, update.Body, decryptedResp)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("failed decrypting update message from Management Service: %s", err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if decryptedResp.GetNetworkMap() == nil {
|
|
|
|
return nil, fmt.Errorf("invalid msg, required network map")
|
|
|
|
}
|
|
|
|
|
2023-06-12 14:43:55 +02:00
|
|
|
return decryptedResp.GetNetworkMap(), nil
|
2023-04-17 11:15:37 +02:00
|
|
|
}
|
|
|
|
|
2024-06-13 13:24:24 +02:00
|
|
|
func (c *GrpcClient) connectToStream(ctx context.Context, serverPubKey wgtypes.Key, sysInfo *system.Info) (proto.ManagementService_SyncClient, error) {
|
|
|
|
req := &proto.SyncRequest{Meta: infoToMetaData(sysInfo)}
|
2022-01-18 16:44:58 +01:00
|
|
|
|
|
|
|
myPrivateKey := c.key
|
|
|
|
myPublicKey := myPrivateKey.PublicKey()
|
|
|
|
|
|
|
|
encryptedReq, err := encryption.EncryptMessage(serverPubKey, myPrivateKey, req)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("failed encrypting message: %s", err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
syncReq := &proto.EncryptedMessage{WgPubKey: myPublicKey.String(), Body: encryptedReq}
|
2022-09-26 18:02:20 +02:00
|
|
|
sync, err := c.realClient.Sync(ctx, syncReq)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return sync, nil
|
2022-01-18 16:44:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *GrpcClient) receiveEvents(stream proto.ManagementService_SyncClient, serverPubKey wgtypes.Key, msgHandler func(msg *proto.SyncResponse) error) error {
|
|
|
|
for {
|
|
|
|
update, err := stream.Recv()
|
|
|
|
if err == io.EOF {
|
2022-07-02 20:38:16 +02:00
|
|
|
log.Debugf("Management stream has been closed by server: %s", err)
|
2022-01-18 16:44:58 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err != nil {
|
2022-07-02 20:38:16 +02:00
|
|
|
log.Debugf("disconnected from Management Service sync stream: %v", err)
|
2022-01-18 16:44:58 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Debugf("got an update message from Management Service")
|
|
|
|
decryptedResp := &proto.SyncResponse{}
|
|
|
|
err = encryption.DecryptMessage(serverPubKey, c.key, update.Body, decryptedResp)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("failed decrypting update message from Management Service: %s", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = msgHandler(decryptedResp)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("failed handling an update message received from Management Service: %v", err.Error())
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-02 20:38:16 +02:00
|
|
|
// GetServerPublicKey returns server's WireGuard public key (used later for encrypting messages sent to the server)
|
2022-01-18 16:44:58 +01:00
|
|
|
func (c *GrpcClient) GetServerPublicKey() (*wgtypes.Key, error) {
|
|
|
|
if !c.ready() {
|
2024-08-14 12:30:10 +02:00
|
|
|
return nil, errors.New(errMsgNoMgmtConnection)
|
2022-01-18 16:44:58 +01:00
|
|
|
}
|
|
|
|
|
2022-07-02 20:38:16 +02:00
|
|
|
mgmCtx, cancel := context.WithTimeout(c.ctx, 5*time.Second)
|
2022-01-18 16:44:58 +01:00
|
|
|
defer cancel()
|
|
|
|
resp, err := c.realClient.GetServerKey(mgmCtx, &proto.Empty{})
|
|
|
|
if err != nil {
|
2024-05-06 18:04:32 +02:00
|
|
|
log.Errorf("failed while getting Management Service public key: %v", err)
|
|
|
|
return nil, fmt.Errorf("failed while getting Management Service public key")
|
2022-01-18 16:44:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
serverKey, err := wgtypes.ParseKey(resp.Key)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &serverKey, nil
|
|
|
|
}
|
|
|
|
|
2024-01-22 12:20:24 +01:00
|
|
|
// IsHealthy probes the gRPC connection and returns false on errors
|
|
|
|
func (c *GrpcClient) IsHealthy() bool {
|
|
|
|
switch c.conn.GetState() {
|
|
|
|
case connectivity.TransientFailure:
|
|
|
|
return false
|
|
|
|
case connectivity.Connecting:
|
|
|
|
return true
|
|
|
|
case connectivity.Shutdown:
|
|
|
|
return true
|
|
|
|
case connectivity.Idle:
|
|
|
|
case connectivity.Ready:
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx, cancel := context.WithTimeout(c.ctx, 1*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
_, err := c.realClient.GetServerKey(ctx, &proto.Empty{})
|
|
|
|
if err != nil {
|
|
|
|
c.notifyDisconnected(err)
|
|
|
|
log.Warnf("health check returned: %s", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
c.notifyConnected()
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2022-01-18 16:44:58 +01:00
|
|
|
func (c *GrpcClient) login(serverKey wgtypes.Key, req *proto.LoginRequest) (*proto.LoginResponse, error) {
|
|
|
|
if !c.ready() {
|
2024-08-14 12:30:10 +02:00
|
|
|
return nil, errors.New(errMsgNoMgmtConnection)
|
2022-01-18 16:44:58 +01:00
|
|
|
}
|
2024-07-17 16:26:06 +02:00
|
|
|
|
2022-01-18 16:44:58 +01:00
|
|
|
loginReq, err := encryption.EncryptMessage(serverKey, c.key, req)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("failed to encrypt message: %s", err)
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-07-17 16:26:06 +02:00
|
|
|
|
|
|
|
var resp *proto.EncryptedMessage
|
|
|
|
operation := func() error {
|
|
|
|
mgmCtx, cancel := context.WithTimeout(context.Background(), ConnectTimeout)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
var err error
|
|
|
|
resp, err = c.realClient.Login(mgmCtx, &proto.EncryptedMessage{
|
|
|
|
WgPubKey: c.key.PublicKey().String(),
|
|
|
|
Body: loginReq,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2024-07-22 15:49:25 +02:00
|
|
|
// retry only on context canceled
|
|
|
|
if s, ok := gstatus.FromError(err); ok && s.Code() == codes.Canceled {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return backoff.Permanent(err)
|
2024-07-17 16:26:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
err = backoff.Retry(operation, nbgrpc.Backoff(c.ctx))
|
2022-01-18 16:44:58 +01:00
|
|
|
if err != nil {
|
2024-07-17 16:26:06 +02:00
|
|
|
log.Errorf("failed to login to Management Service: %v", err)
|
2022-01-18 16:44:58 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
loginResp := &proto.LoginResponse{}
|
|
|
|
err = encryption.DecryptMessage(serverKey, c.key, resp.Body, loginResp)
|
|
|
|
if err != nil {
|
2024-07-17 16:26:06 +02:00
|
|
|
log.Errorf("failed to decrypt login response: %s", err)
|
2022-01-18 16:44:58 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return loginResp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
2022-06-23 17:04:53 +02:00
|
|
|
func (c *GrpcClient) Register(serverKey wgtypes.Key, setupKey string, jwtToken string, sysInfo *system.Info, pubSSHKey []byte) (*proto.LoginResponse, error) {
|
|
|
|
keys := &proto.PeerKeys{
|
|
|
|
SshPubKey: pubSSHKey,
|
|
|
|
WgPubKey: []byte(c.key.PublicKey().String()),
|
|
|
|
}
|
|
|
|
return c.login(serverKey, &proto.LoginRequest{SetupKey: setupKey, Meta: infoToMetaData(sysInfo), JwtToken: jwtToken, PeerKeys: keys})
|
2022-01-18 16:44:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Login attempts login to Management Server. Takes care of encrypting and decrypting messages.
|
2022-06-23 17:04:53 +02:00
|
|
|
func (c *GrpcClient) Login(serverKey wgtypes.Key, sysInfo *system.Info, pubSSHKey []byte) (*proto.LoginResponse, error) {
|
|
|
|
keys := &proto.PeerKeys{
|
|
|
|
SshPubKey: pubSSHKey,
|
|
|
|
WgPubKey: []byte(c.key.PublicKey().String()),
|
|
|
|
}
|
|
|
|
return c.login(serverKey, &proto.LoginRequest{Meta: infoToMetaData(sysInfo), PeerKeys: keys})
|
2022-01-18 16:44:58 +01:00
|
|
|
}
|
2022-05-08 11:04:57 +02:00
|
|
|
|
|
|
|
// GetDeviceAuthorizationFlow returns a device authorization flow information.
|
|
|
|
// It also takes care of encrypting and decrypting messages.
|
|
|
|
func (c *GrpcClient) GetDeviceAuthorizationFlow(serverKey wgtypes.Key) (*proto.DeviceAuthorizationFlow, error) {
|
|
|
|
if !c.ready() {
|
|
|
|
return nil, fmt.Errorf("no connection to management in order to get device authorization flow")
|
|
|
|
}
|
|
|
|
mgmCtx, cancel := context.WithTimeout(c.ctx, time.Second*2)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
message := &proto.DeviceAuthorizationFlowRequest{}
|
|
|
|
encryptedMSG, err := encryption.EncryptMessage(serverKey, c.key, message)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := c.realClient.GetDeviceAuthorizationFlow(mgmCtx, &proto.EncryptedMessage{
|
|
|
|
WgPubKey: c.key.PublicKey().String(),
|
|
|
|
Body: encryptedMSG},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
flowInfoResp := &proto.DeviceAuthorizationFlow{}
|
|
|
|
err = encryption.DecryptMessage(serverKey, c.key, resp.Body, flowInfoResp)
|
|
|
|
if err != nil {
|
|
|
|
errWithMSG := fmt.Errorf("failed to decrypt device authorization flow message: %s", err)
|
|
|
|
log.Error(errWithMSG)
|
|
|
|
return nil, errWithMSG
|
|
|
|
}
|
|
|
|
|
|
|
|
return flowInfoResp, nil
|
|
|
|
}
|
2022-05-23 13:03:57 +02:00
|
|
|
|
2023-07-27 11:31:07 +02:00
|
|
|
// GetPKCEAuthorizationFlow returns a pkce authorization flow information.
|
|
|
|
// It also takes care of encrypting and decrypting messages.
|
|
|
|
func (c *GrpcClient) GetPKCEAuthorizationFlow(serverKey wgtypes.Key) (*proto.PKCEAuthorizationFlow, error) {
|
|
|
|
if !c.ready() {
|
|
|
|
return nil, fmt.Errorf("no connection to management in order to get pkce authorization flow")
|
|
|
|
}
|
|
|
|
mgmCtx, cancel := context.WithTimeout(c.ctx, time.Second*2)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
message := &proto.PKCEAuthorizationFlowRequest{}
|
|
|
|
encryptedMSG, err := encryption.EncryptMessage(serverKey, c.key, message)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := c.realClient.GetPKCEAuthorizationFlow(mgmCtx, &proto.EncryptedMessage{
|
|
|
|
WgPubKey: c.key.PublicKey().String(),
|
|
|
|
Body: encryptedMSG,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
flowInfoResp := &proto.PKCEAuthorizationFlow{}
|
|
|
|
err = encryption.DecryptMessage(serverKey, c.key, resp.Body, flowInfoResp)
|
|
|
|
if err != nil {
|
|
|
|
errWithMSG := fmt.Errorf("failed to decrypt pkce authorization flow message: %s", err)
|
|
|
|
log.Error(errWithMSG)
|
|
|
|
return nil, errWithMSG
|
|
|
|
}
|
|
|
|
|
|
|
|
return flowInfoResp, nil
|
|
|
|
}
|
|
|
|
|
2024-06-13 13:24:24 +02:00
|
|
|
// SyncMeta sends updated system metadata to the Management Service.
|
|
|
|
// It should be used if there is changes on peer posture check after initial sync.
|
|
|
|
func (c *GrpcClient) SyncMeta(sysInfo *system.Info) error {
|
|
|
|
if !c.ready() {
|
2024-08-14 12:30:10 +02:00
|
|
|
return errors.New(errMsgNoMgmtConnection)
|
2024-06-13 13:24:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
serverPubKey, err := c.GetServerPublicKey()
|
|
|
|
if err != nil {
|
|
|
|
log.Debugf(errMsgMgmtPublicKey, err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
syncMetaReq, err := encryption.EncryptMessage(*serverPubKey, c.key, &proto.SyncMetaRequest{Meta: infoToMetaData(sysInfo)})
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("failed to encrypt message: %s", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
mgmCtx, cancel := context.WithTimeout(c.ctx, ConnectTimeout)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
_, err = c.realClient.SyncMeta(mgmCtx, &proto.EncryptedMessage{
|
|
|
|
WgPubKey: c.key.PublicKey().String(),
|
|
|
|
Body: syncMetaReq,
|
|
|
|
})
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-01-22 12:20:24 +01:00
|
|
|
func (c *GrpcClient) notifyDisconnected(err error) {
|
2023-03-16 17:22:36 +01:00
|
|
|
c.connStateCallbackLock.RLock()
|
|
|
|
defer c.connStateCallbackLock.RUnlock()
|
|
|
|
|
|
|
|
if c.connStateCallback == nil {
|
|
|
|
return
|
|
|
|
}
|
2024-01-22 12:20:24 +01:00
|
|
|
c.connStateCallback.MarkManagementDisconnected(err)
|
2023-03-16 17:22:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *GrpcClient) notifyConnected() {
|
|
|
|
c.connStateCallbackLock.RLock()
|
|
|
|
defer c.connStateCallbackLock.RUnlock()
|
|
|
|
|
|
|
|
if c.connStateCallback == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c.connStateCallback.MarkManagementConnected()
|
|
|
|
}
|
|
|
|
|
2022-05-23 13:03:57 +02:00
|
|
|
func infoToMetaData(info *system.Info) *proto.PeerSystemMeta {
|
|
|
|
if info == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2024-02-20 11:53:11 +01:00
|
|
|
|
|
|
|
addresses := make([]*proto.NetworkAddress, 0, len(info.NetworkAddresses))
|
|
|
|
for _, addr := range info.NetworkAddresses {
|
|
|
|
addresses = append(addresses, &proto.NetworkAddress{
|
|
|
|
NetIP: addr.NetIP.String(),
|
|
|
|
Mac: addr.Mac,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-06-13 13:24:24 +02:00
|
|
|
files := make([]*proto.File, 0, len(info.Files))
|
|
|
|
for _, file := range info.Files {
|
|
|
|
files = append(files, &proto.File{
|
|
|
|
Path: file.Path,
|
|
|
|
Exist: file.Exist,
|
|
|
|
ProcessIsRunning: file.ProcessIsRunning,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-05-23 13:03:57 +02:00
|
|
|
return &proto.PeerSystemMeta{
|
|
|
|
Hostname: info.Hostname,
|
|
|
|
GoOS: info.GoOS,
|
|
|
|
OS: info.OS,
|
|
|
|
Core: info.OSVersion,
|
2024-02-20 09:59:56 +01:00
|
|
|
OSVersion: info.OSVersion,
|
2022-05-23 13:03:57 +02:00
|
|
|
Platform: info.Platform,
|
|
|
|
Kernel: info.Kernel,
|
|
|
|
WiretrusteeVersion: info.WiretrusteeVersion,
|
2022-05-25 23:25:02 +02:00
|
|
|
UiVersion: info.UIVersion,
|
2024-02-20 09:59:56 +01:00
|
|
|
KernelVersion: info.KernelVersion,
|
2024-02-20 11:53:11 +01:00
|
|
|
NetworkAddresses: addresses,
|
|
|
|
SysSerialNumber: info.SystemSerialNumber,
|
|
|
|
SysManufacturer: info.SystemManufacturer,
|
|
|
|
SysProductName: info.SystemProductName,
|
2024-03-01 15:15:56 +01:00
|
|
|
Environment: &proto.Environment{
|
|
|
|
Cloud: info.Environment.Cloud,
|
|
|
|
Platform: info.Environment.Platform,
|
|
|
|
},
|
2024-06-13 13:24:24 +02:00
|
|
|
Files: files,
|
2022-05-23 13:03:57 +02:00
|
|
|
}
|
|
|
|
}
|