2021-08-16 23:30:51 +02:00
|
|
|
package server
|
2021-07-17 14:38:59 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-07-30 17:46:38 +02:00
|
|
|
"fmt"
|
2024-02-20 09:59:56 +01:00
|
|
|
"net"
|
2024-02-20 11:53:11 +01:00
|
|
|
"net/netip"
|
2022-05-08 11:04:57 +02:00
|
|
|
"strings"
|
2021-07-24 16:14:29 +02:00
|
|
|
"time"
|
|
|
|
|
2023-03-30 10:54:09 +02:00
|
|
|
pb "github.com/golang/protobuf/proto" // nolint
|
2021-07-22 10:28:00 +02:00
|
|
|
"github.com/golang/protobuf/ptypes/timestamp"
|
2024-01-31 16:02:24 +01:00
|
|
|
"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/realip"
|
2021-07-22 10:28:00 +02:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
|
|
|
|
"google.golang.org/grpc/codes"
|
2021-07-17 14:38:59 +02:00
|
|
|
"google.golang.org/grpc/status"
|
2023-03-30 10:54:09 +02:00
|
|
|
|
|
|
|
"github.com/netbirdio/netbird/encryption"
|
|
|
|
"github.com/netbirdio/netbird/management/proto"
|
2024-09-08 12:06:14 +02:00
|
|
|
nbContext "github.com/netbirdio/netbird/management/server/context"
|
2023-09-04 11:37:39 +02:00
|
|
|
"github.com/netbirdio/netbird/management/server/jwtclaims"
|
2023-11-28 13:45:26 +01:00
|
|
|
nbpeer "github.com/netbirdio/netbird/management/server/peer"
|
2024-09-08 12:06:14 +02:00
|
|
|
"github.com/netbirdio/netbird/management/server/posture"
|
2023-03-30 10:54:09 +02:00
|
|
|
internalStatus "github.com/netbirdio/netbird/management/server/status"
|
2023-09-04 11:37:39 +02:00
|
|
|
"github.com/netbirdio/netbird/management/server/telemetry"
|
2021-07-17 14:38:59 +02:00
|
|
|
)
|
|
|
|
|
2022-07-29 20:37:09 +02:00
|
|
|
// GRPCServer an instance of a Management gRPC API server
|
|
|
|
type GRPCServer struct {
|
2022-02-22 11:28:19 +01:00
|
|
|
accountManager AccountManager
|
2021-07-30 17:46:38 +02:00
|
|
|
wgKey wgtypes.Key
|
2021-07-20 18:09:26 +02:00
|
|
|
proto.UnimplementedManagementServiceServer
|
2024-09-08 12:06:14 +02:00
|
|
|
peersUpdateManager *PeersUpdateManager
|
|
|
|
config *Config
|
|
|
|
secretsManager SecretsManager
|
|
|
|
jwtValidator *jwtclaims.JWTValidator
|
|
|
|
jwtClaimsExtractor *jwtclaims.ClaimsExtractor
|
|
|
|
appMetrics telemetry.AppMetrics
|
|
|
|
ephemeralManager *EphemeralManager
|
2021-07-22 10:28:00 +02:00
|
|
|
}
|
|
|
|
|
2021-07-17 14:38:59 +02:00
|
|
|
// NewServer creates a new Management server
|
2024-09-08 12:06:14 +02:00
|
|
|
func NewServer(
|
|
|
|
ctx context.Context,
|
|
|
|
config *Config,
|
|
|
|
accountManager AccountManager,
|
|
|
|
peersUpdateManager *PeersUpdateManager,
|
|
|
|
secretsManager SecretsManager,
|
|
|
|
appMetrics telemetry.AppMetrics,
|
|
|
|
ephemeralManager *EphemeralManager,
|
|
|
|
) (*GRPCServer, error) {
|
2021-07-22 10:28:00 +02:00
|
|
|
key, err := wgtypes.GeneratePrivateKey()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-08-29 17:48:31 +02:00
|
|
|
|
2023-03-30 16:14:51 +02:00
|
|
|
var jwtValidator *jwtclaims.JWTValidator
|
2022-05-05 20:02:15 +02:00
|
|
|
|
|
|
|
if config.HttpConfig != nil && config.HttpConfig.AuthIssuer != "" && config.HttpConfig.AuthAudience != "" && validateURL(config.HttpConfig.AuthKeysLocation) {
|
2023-03-30 16:14:51 +02:00
|
|
|
jwtValidator, err = jwtclaims.NewJWTValidator(
|
2024-07-03 11:33:02 +02:00
|
|
|
ctx,
|
2022-05-05 20:02:15 +02:00
|
|
|
config.HttpConfig.AuthIssuer,
|
2023-04-04 16:40:56 +02:00
|
|
|
config.GetAuthAudiences(),
|
2023-04-14 11:17:28 +02:00
|
|
|
config.HttpConfig.AuthKeysLocation,
|
2023-04-15 02:44:42 +02:00
|
|
|
config.HttpConfig.IdpSignKeyRefreshEnabled,
|
2023-04-14 11:17:28 +02:00
|
|
|
)
|
2022-05-05 20:02:15 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, status.Errorf(codes.Internal, "unable to create new jwt middleware, err: %v", err)
|
|
|
|
}
|
2022-05-08 11:04:57 +02:00
|
|
|
} else {
|
2024-07-03 11:33:02 +02:00
|
|
|
log.WithContext(ctx).Debug("unable to use http config to create new jwt middleware")
|
2022-05-05 20:02:15 +02:00
|
|
|
}
|
|
|
|
|
2022-10-22 15:06:54 +02:00
|
|
|
if appMetrics != nil {
|
|
|
|
// update gauge based on number of connected peers which is equal to open gRPC streams
|
|
|
|
err = appMetrics.GRPCMetrics().RegisterConnectedStreams(func() int64 {
|
|
|
|
return int64(len(peersUpdateManager.peerChannels))
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-03 21:47:20 +01:00
|
|
|
var audience, userIDClaim string
|
|
|
|
if config.HttpConfig != nil {
|
|
|
|
audience = config.HttpConfig.AuthAudience
|
|
|
|
userIDClaim = config.HttpConfig.AuthUserIDClaim
|
|
|
|
}
|
|
|
|
jwtClaimsExtractor := jwtclaims.NewClaimsExtractor(
|
|
|
|
jwtclaims.WithAudience(audience),
|
|
|
|
jwtclaims.WithUserIDClaim(userIDClaim),
|
|
|
|
)
|
|
|
|
|
2022-07-29 20:37:09 +02:00
|
|
|
return &GRPCServer{
|
2021-07-22 10:28:00 +02:00
|
|
|
wgKey: key,
|
|
|
|
// peerKey -> event channel
|
2024-09-08 12:06:14 +02:00
|
|
|
peersUpdateManager: peersUpdateManager,
|
|
|
|
accountManager: accountManager,
|
|
|
|
config: config,
|
|
|
|
secretsManager: secretsManager,
|
|
|
|
jwtValidator: jwtValidator,
|
|
|
|
jwtClaimsExtractor: jwtClaimsExtractor,
|
|
|
|
appMetrics: appMetrics,
|
|
|
|
ephemeralManager: ephemeralManager,
|
2021-07-17 14:38:59 +02:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2022-07-29 20:37:09 +02:00
|
|
|
func (s *GRPCServer) GetServerKey(ctx context.Context, req *proto.Empty) (*proto.ServerKeyResponse, error) {
|
2021-07-22 10:28:00 +02:00
|
|
|
// todo introduce something more meaningful with the key expiration/rotation
|
2022-10-22 15:06:54 +02:00
|
|
|
if s.appMetrics != nil {
|
|
|
|
s.appMetrics.GRPCMetrics().CountGetKeyRequest()
|
|
|
|
}
|
2021-07-22 10:28:00 +02:00
|
|
|
now := time.Now().Add(24 * time.Hour)
|
|
|
|
secs := int64(now.Second())
|
|
|
|
nanos := int32(now.Nanosecond())
|
|
|
|
expiresAt := ×tamp.Timestamp{Seconds: secs, Nanos: nanos}
|
|
|
|
|
|
|
|
return &proto.ServerKeyResponse{
|
|
|
|
Key: s.wgKey.PublicKey().String(),
|
|
|
|
ExpiresAt: expiresAt,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2024-02-20 09:59:56 +01:00
|
|
|
func getRealIP(ctx context.Context) net.IP {
|
|
|
|
if addr, ok := realip.FromContext(ctx); ok {
|
|
|
|
return net.IP(addr.AsSlice())
|
2024-01-31 16:02:24 +01:00
|
|
|
}
|
2024-02-20 09:59:56 +01:00
|
|
|
return nil
|
2024-01-31 16:02:24 +01:00
|
|
|
}
|
|
|
|
|
2022-05-21 15:21:39 +02:00
|
|
|
// Sync validates the existence of a connecting peer, sends an initial state (all available for the connecting peers) and
|
2021-07-22 10:28:00 +02:00
|
|
|
// notifies the connected peer of any updates (e.g. new peers under the same account)
|
2022-07-29 20:37:09 +02:00
|
|
|
func (s *GRPCServer) Sync(req *proto.EncryptedMessage, srv proto.ManagementService_SyncServer) error {
|
2023-05-19 11:42:25 +02:00
|
|
|
reqStart := time.Now()
|
2022-10-22 15:06:54 +02:00
|
|
|
if s.appMetrics != nil {
|
|
|
|
s.appMetrics.GRPCMetrics().CountSyncRequest()
|
|
|
|
}
|
2024-07-03 11:33:02 +02:00
|
|
|
|
|
|
|
ctx := srv.Context()
|
|
|
|
|
2023-03-03 18:35:38 +01:00
|
|
|
syncReq := &proto.SyncRequest{}
|
2024-07-03 11:33:02 +02:00
|
|
|
peerKey, err := s.parseRequest(ctx, req, syncReq)
|
2023-02-13 12:21:02 +01:00
|
|
|
if err != nil {
|
2023-03-03 18:35:38 +01:00
|
|
|
return err
|
2023-02-13 12:21:02 +01:00
|
|
|
}
|
|
|
|
|
2024-08-20 19:13:40 +02:00
|
|
|
// nolint:staticcheck
|
2024-07-03 11:33:02 +02:00
|
|
|
ctx = context.WithValue(ctx, nbContext.PeerIDKey, peerKey.String())
|
2024-08-20 19:13:40 +02:00
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
accountID, err := s.accountManager.GetAccountIDForPeerKey(ctx, peerKey.String())
|
|
|
|
if err != nil {
|
2024-08-20 19:13:40 +02:00
|
|
|
// nolint:staticcheck
|
|
|
|
ctx = context.WithValue(ctx, nbContext.AccountIDKey, "UNKNOWN")
|
|
|
|
log.WithContext(ctx).Tracef("peer %s is not registered", peerKey.String())
|
|
|
|
if errStatus, ok := internalStatus.FromError(err); ok && errStatus.Type() == internalStatus.NotFound {
|
|
|
|
return status.Errorf(codes.PermissionDenied, "peer is not registered")
|
|
|
|
}
|
|
|
|
return err
|
2024-07-03 11:33:02 +02:00
|
|
|
}
|
2024-08-20 19:13:40 +02:00
|
|
|
|
|
|
|
// nolint:staticcheck
|
2024-07-03 11:33:02 +02:00
|
|
|
ctx = context.WithValue(ctx, nbContext.AccountIDKey, accountID)
|
|
|
|
|
2024-08-20 19:13:40 +02:00
|
|
|
realIP := getRealIP(ctx)
|
2024-07-03 11:33:02 +02:00
|
|
|
log.WithContext(ctx).Debugf("Sync request from peer [%s] [%s]", req.WgPubKey, realIP.String())
|
|
|
|
|
2024-06-13 13:24:24 +02:00
|
|
|
if syncReq.GetMeta() == nil {
|
2024-07-03 11:33:02 +02:00
|
|
|
log.WithContext(ctx).Tracef("peer system meta has to be provided on sync. Peer %s, remote addr %s", peerKey.String(), realIP)
|
2024-06-13 13:24:24 +02:00
|
|
|
}
|
|
|
|
|
2024-08-01 16:21:43 +02:00
|
|
|
peer, netMap, postureChecks, err := s.accountManager.SyncAndMarkPeer(ctx, accountID, peerKey.String(), extractPeerMeta(ctx, syncReq.GetMeta()), realIP)
|
2021-07-22 10:28:00 +02:00
|
|
|
if err != nil {
|
2024-07-03 11:33:02 +02:00
|
|
|
return mapError(ctx, err)
|
2021-07-22 10:28:00 +02:00
|
|
|
}
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
err = s.sendInitialSync(ctx, peerKey, peer, netMap, postureChecks, srv)
|
2021-07-22 10:28:00 +02:00
|
|
|
if err != nil {
|
2024-07-03 11:33:02 +02:00
|
|
|
log.WithContext(ctx).Debugf("error while sending initial sync for %s: %v", peerKey.String(), err)
|
2021-07-22 10:28:00 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
updates := s.peersUpdateManager.CreateChannel(ctx, peer.ID)
|
2023-09-04 11:37:39 +02:00
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
s.ephemeralManager.OnPeerConnected(ctx, peer)
|
2023-09-04 11:37:39 +02:00
|
|
|
|
2024-09-08 12:06:14 +02:00
|
|
|
s.secretsManager.SetupRefresh(ctx, peer.ID)
|
2023-05-19 11:42:25 +02:00
|
|
|
|
|
|
|
if s.appMetrics != nil {
|
|
|
|
s.appMetrics.GRPCMetrics().CountSyncRequestDuration(time.Since(reqStart))
|
|
|
|
}
|
|
|
|
|
2024-08-01 16:21:43 +02:00
|
|
|
return s.handleUpdates(ctx, accountID, peerKey, peer, updates, srv)
|
2024-06-13 13:24:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// handleUpdates sends updates to the connected peer until the updates channel is closed.
|
2024-08-01 16:21:43 +02:00
|
|
|
func (s *GRPCServer) handleUpdates(ctx context.Context, accountID string, peerKey wgtypes.Key, peer *nbpeer.Peer, updates chan *UpdateMessage, srv proto.ManagementService_SyncServer) error {
|
2021-07-22 10:28:00 +02:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
// condition when there are some updates
|
|
|
|
case update, open := <-updates:
|
2023-09-25 10:23:43 +02:00
|
|
|
if s.appMetrics != nil {
|
|
|
|
s.appMetrics.GRPCMetrics().UpdateChannelQueueLength(len(updates) + 1)
|
|
|
|
}
|
|
|
|
|
2021-07-22 10:28:00 +02:00
|
|
|
if !open {
|
2024-07-03 11:33:02 +02:00
|
|
|
log.WithContext(ctx).Debugf("updates channel for peer %s was closed", peerKey.String())
|
2024-08-01 16:21:43 +02:00
|
|
|
s.cancelPeerRoutines(ctx, accountID, peer)
|
2021-07-22 10:28:00 +02:00
|
|
|
return nil
|
|
|
|
}
|
2024-07-03 11:33:02 +02:00
|
|
|
log.WithContext(ctx).Debugf("received an update for peer %s", peerKey.String())
|
2021-07-22 10:28:00 +02:00
|
|
|
|
2024-08-01 16:21:43 +02:00
|
|
|
if err := s.sendUpdate(ctx, accountID, peerKey, peer, update, srv); err != nil {
|
2024-06-13 13:24:24 +02:00
|
|
|
return err
|
2021-07-22 10:28:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// condition when client <-> server connection has been terminated
|
|
|
|
case <-srv.Context().Done():
|
|
|
|
// happens when connection drops, e.g. client disconnects
|
2024-07-03 11:33:02 +02:00
|
|
|
log.WithContext(ctx).Debugf("stream of peer %s has been closed", peerKey.String())
|
2024-08-01 16:21:43 +02:00
|
|
|
s.cancelPeerRoutines(ctx, accountID, peer)
|
2021-07-22 10:28:00 +02:00
|
|
|
return srv.Context().Err()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-13 13:24:24 +02:00
|
|
|
// sendUpdate encrypts the update message using the peer key and the server's wireguard key,
|
|
|
|
// then sends the encrypted message to the connected peer via the sync server.
|
2024-08-01 16:21:43 +02:00
|
|
|
func (s *GRPCServer) sendUpdate(ctx context.Context, accountID string, peerKey wgtypes.Key, peer *nbpeer.Peer, update *UpdateMessage, srv proto.ManagementService_SyncServer) error {
|
2024-06-13 13:24:24 +02:00
|
|
|
encryptedResp, err := encryption.EncryptMessage(peerKey, s.wgKey, update.Update)
|
|
|
|
if err != nil {
|
2024-08-01 16:21:43 +02:00
|
|
|
s.cancelPeerRoutines(ctx, accountID, peer)
|
2024-06-13 13:24:24 +02:00
|
|
|
return status.Errorf(codes.Internal, "failed processing update message")
|
|
|
|
}
|
|
|
|
err = srv.SendMsg(&proto.EncryptedMessage{
|
|
|
|
WgPubKey: s.wgKey.PublicKey().String(),
|
|
|
|
Body: encryptedResp,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2024-08-01 16:21:43 +02:00
|
|
|
s.cancelPeerRoutines(ctx, accountID, peer)
|
2024-06-13 13:24:24 +02:00
|
|
|
return status.Errorf(codes.Internal, "failed sending update message")
|
|
|
|
}
|
2024-07-03 11:33:02 +02:00
|
|
|
log.WithContext(ctx).Debugf("sent an update to peer %s", peerKey.String())
|
2024-06-13 13:24:24 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-08-01 16:21:43 +02:00
|
|
|
func (s *GRPCServer) cancelPeerRoutines(ctx context.Context, accountID string, peer *nbpeer.Peer) {
|
2024-07-03 11:33:02 +02:00
|
|
|
s.peersUpdateManager.CloseChannel(ctx, peer.ID)
|
2024-09-08 12:06:14 +02:00
|
|
|
s.secretsManager.CancelRefresh(peer.ID)
|
2024-08-01 16:21:43 +02:00
|
|
|
_ = s.accountManager.OnPeerDisconnected(ctx, accountID, peer.Key)
|
2024-07-03 11:33:02 +02:00
|
|
|
s.ephemeralManager.OnPeerDisconnected(ctx, peer)
|
2023-02-28 20:02:30 +01:00
|
|
|
}
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
func (s *GRPCServer) validateToken(ctx context.Context, jwtToken string) (string, error) {
|
2023-03-30 16:14:51 +02:00
|
|
|
if s.jwtValidator == nil {
|
|
|
|
return "", status.Error(codes.Internal, "no jwt validator set")
|
2023-02-13 12:21:02 +01:00
|
|
|
}
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
token, err := s.jwtValidator.ValidateAndParse(ctx, jwtToken)
|
2023-02-13 12:21:02 +01:00
|
|
|
if err != nil {
|
2023-03-03 18:35:38 +01:00
|
|
|
return "", status.Errorf(codes.InvalidArgument, "invalid jwt token, err: %v", err)
|
2023-02-13 12:21:02 +01:00
|
|
|
}
|
|
|
|
claims := s.jwtClaimsExtractor.FromToken(token)
|
|
|
|
// we need to call this method because if user is new, we will automatically add it to existing or create a new account
|
2024-09-27 16:10:50 +02:00
|
|
|
_, _, err = s.accountManager.GetAccountIDFromToken(ctx, claims)
|
2023-02-13 12:21:02 +01:00
|
|
|
if err != nil {
|
|
|
|
return "", status.Errorf(codes.Internal, "unable to fetch account with claims, err: %v", err)
|
|
|
|
}
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
if err := s.accountManager.CheckUserAccessByJWTGroups(ctx, claims); err != nil {
|
2024-08-14 12:30:10 +02:00
|
|
|
return "", status.Error(codes.PermissionDenied, err.Error())
|
2023-12-13 11:18:35 +01:00
|
|
|
}
|
|
|
|
|
2023-02-13 12:21:02 +01:00
|
|
|
return claims.UserId, nil
|
|
|
|
}
|
|
|
|
|
2023-03-03 18:35:38 +01:00
|
|
|
// maps internal internalStatus.Error to gRPC status.Error
|
2024-07-03 11:33:02 +02:00
|
|
|
func mapError(ctx context.Context, err error) error {
|
2023-03-03 18:35:38 +01:00
|
|
|
if e, ok := internalStatus.FromError(err); ok {
|
|
|
|
switch e.Type() {
|
|
|
|
case internalStatus.PermissionDenied:
|
2024-08-14 12:30:10 +02:00
|
|
|
return status.Error(codes.PermissionDenied, e.Message)
|
2023-03-03 18:35:38 +01:00
|
|
|
case internalStatus.Unauthorized:
|
2024-08-14 12:30:10 +02:00
|
|
|
return status.Error(codes.PermissionDenied, e.Message)
|
2023-03-03 18:35:38 +01:00
|
|
|
case internalStatus.Unauthenticated:
|
2024-08-14 12:30:10 +02:00
|
|
|
return status.Error(codes.PermissionDenied, e.Message)
|
2023-03-03 18:35:38 +01:00
|
|
|
case internalStatus.PreconditionFailed:
|
2024-08-14 12:30:10 +02:00
|
|
|
return status.Error(codes.FailedPrecondition, e.Message)
|
2023-03-03 18:35:38 +01:00
|
|
|
case internalStatus.NotFound:
|
2024-08-14 12:30:10 +02:00
|
|
|
return status.Error(codes.NotFound, e.Message)
|
2023-03-03 18:35:38 +01:00
|
|
|
default:
|
2022-11-22 12:37:36 +01:00
|
|
|
}
|
2022-05-05 20:02:15 +02:00
|
|
|
}
|
2024-07-03 11:33:02 +02:00
|
|
|
log.WithContext(ctx).Errorf("got an unhandled error: %s", err)
|
2023-03-03 18:35:38 +01:00
|
|
|
return status.Errorf(codes.Internal, "failed handling request")
|
|
|
|
}
|
2022-05-05 20:02:15 +02:00
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
func extractPeerMeta(ctx context.Context, meta *proto.PeerSystemMeta) nbpeer.PeerSystemMeta {
|
2024-06-13 13:24:24 +02:00
|
|
|
if meta == nil {
|
|
|
|
return nbpeer.PeerSystemMeta{}
|
|
|
|
}
|
|
|
|
|
|
|
|
osVersion := meta.GetOSVersion()
|
2024-02-20 09:59:56 +01:00
|
|
|
if osVersion == "" {
|
2024-06-13 13:24:24 +02:00
|
|
|
osVersion = meta.GetCore()
|
2024-02-20 09:59:56 +01:00
|
|
|
}
|
|
|
|
|
2024-06-13 13:24:24 +02:00
|
|
|
networkAddresses := make([]nbpeer.NetworkAddress, 0, len(meta.GetNetworkAddresses()))
|
|
|
|
for _, addr := range meta.GetNetworkAddresses() {
|
2024-02-20 11:53:11 +01:00
|
|
|
netAddr, err := netip.ParsePrefix(addr.GetNetIP())
|
|
|
|
if err != nil {
|
2024-07-03 11:33:02 +02:00
|
|
|
log.WithContext(ctx).Warnf("failed to parse netip address, %s: %v", addr.GetNetIP(), err)
|
2024-02-20 11:53:11 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
networkAddresses = append(networkAddresses, nbpeer.NetworkAddress{
|
|
|
|
NetIP: netAddr,
|
|
|
|
Mac: addr.GetMac(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-06-13 13:24:24 +02:00
|
|
|
files := make([]nbpeer.File, 0, len(meta.GetFiles()))
|
|
|
|
for _, file := range meta.GetFiles() {
|
|
|
|
files = append(files, nbpeer.File{
|
|
|
|
Path: file.GetPath(),
|
|
|
|
Exist: file.GetExist(),
|
|
|
|
ProcessIsRunning: file.GetProcessIsRunning(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-11-28 13:45:26 +01:00
|
|
|
return nbpeer.PeerSystemMeta{
|
2024-06-13 13:24:24 +02:00
|
|
|
Hostname: meta.GetHostname(),
|
|
|
|
GoOS: meta.GetGoOS(),
|
|
|
|
Kernel: meta.GetKernel(),
|
|
|
|
Platform: meta.GetPlatform(),
|
|
|
|
OS: meta.GetOS(),
|
2024-02-20 11:53:11 +01:00
|
|
|
OSVersion: osVersion,
|
2024-06-13 13:24:24 +02:00
|
|
|
WtVersion: meta.GetWiretrusteeVersion(),
|
|
|
|
UIVersion: meta.GetUiVersion(),
|
|
|
|
KernelVersion: meta.GetKernelVersion(),
|
2024-02-20 11:53:11 +01:00
|
|
|
NetworkAddresses: networkAddresses,
|
2024-06-13 13:24:24 +02:00
|
|
|
SystemSerialNumber: meta.GetSysSerialNumber(),
|
|
|
|
SystemProductName: meta.GetSysProductName(),
|
|
|
|
SystemManufacturer: meta.GetSysManufacturer(),
|
2024-03-01 15:15:56 +01:00
|
|
|
Environment: nbpeer.Environment{
|
2024-06-13 13:24:24 +02:00
|
|
|
Cloud: meta.GetEnvironment().GetCloud(),
|
|
|
|
Platform: meta.GetEnvironment().GetPlatform(),
|
2024-03-01 15:15:56 +01:00
|
|
|
},
|
2024-06-13 13:24:24 +02:00
|
|
|
Files: files,
|
2021-08-24 11:50:19 +02:00
|
|
|
}
|
2023-03-03 18:35:38 +01:00
|
|
|
}
|
2022-05-25 23:25:02 +02:00
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
func (s *GRPCServer) parseRequest(ctx context.Context, req *proto.EncryptedMessage, parsed pb.Message) (wgtypes.Key, error) {
|
2023-03-03 18:35:38 +01:00
|
|
|
peerKey, err := wgtypes.ParseKey(req.GetWgPubKey())
|
2021-07-17 14:38:59 +02:00
|
|
|
if err != nil {
|
2024-07-03 11:33:02 +02:00
|
|
|
log.WithContext(ctx).Warnf("error while parsing peer's WireGuard public key %s.", req.WgPubKey)
|
2023-03-03 18:35:38 +01:00
|
|
|
return wgtypes.Key{}, status.Errorf(codes.InvalidArgument, "provided wgPubKey %s is invalid", req.WgPubKey)
|
2021-07-22 10:28:00 +02:00
|
|
|
}
|
|
|
|
|
2023-03-03 18:35:38 +01:00
|
|
|
err = encryption.DecryptMessage(peerKey, s.wgKey, req.Body, parsed)
|
2021-07-22 10:28:00 +02:00
|
|
|
if err != nil {
|
2023-03-03 18:35:38 +01:00
|
|
|
return wgtypes.Key{}, status.Errorf(codes.InvalidArgument, "invalid request message")
|
2021-07-17 14:38:59 +02:00
|
|
|
}
|
|
|
|
|
2023-03-03 18:35:38 +01:00
|
|
|
return peerKey, nil
|
2021-08-15 16:56:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Login endpoint first checks whether peer is registered under any account
|
|
|
|
// In case it is, the login is successful
|
|
|
|
// In case it isn't, the endpoint checks whether setup key is provided within the request and tries to register a peer.
|
|
|
|
// In case of the successful registration login is also successful
|
2022-07-29 20:37:09 +02:00
|
|
|
func (s *GRPCServer) Login(ctx context.Context, req *proto.EncryptedMessage) (*proto.EncryptedMessage, error) {
|
2023-05-19 11:42:25 +02:00
|
|
|
reqStart := time.Now()
|
|
|
|
defer func() {
|
|
|
|
if s.appMetrics != nil {
|
|
|
|
s.appMetrics.GRPCMetrics().CountLoginRequestDuration(time.Since(reqStart))
|
|
|
|
}
|
|
|
|
}()
|
2022-10-22 15:06:54 +02:00
|
|
|
if s.appMetrics != nil {
|
|
|
|
s.appMetrics.GRPCMetrics().CountLoginRequest()
|
|
|
|
}
|
2024-01-31 16:02:24 +01:00
|
|
|
realIP := getRealIP(ctx)
|
2024-07-03 11:33:02 +02:00
|
|
|
log.WithContext(ctx).Debugf("Login request from peer [%s] [%s]", req.WgPubKey, realIP.String())
|
2021-08-15 16:56:26 +02:00
|
|
|
|
2022-05-23 13:03:57 +02:00
|
|
|
loginReq := &proto.LoginRequest{}
|
2024-07-03 11:33:02 +02:00
|
|
|
peerKey, err := s.parseRequest(ctx, req, loginReq)
|
2022-05-23 13:03:57 +02:00
|
|
|
if err != nil {
|
2023-03-03 18:35:38 +01:00
|
|
|
return nil, err
|
2021-08-15 16:56:26 +02:00
|
|
|
}
|
2022-06-23 17:04:53 +02:00
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
//nolint
|
|
|
|
ctx = context.WithValue(ctx, nbContext.PeerIDKey, peerKey.String())
|
|
|
|
accountID, err := s.accountManager.GetAccountIDForPeerKey(ctx, peerKey.String())
|
|
|
|
if err != nil {
|
|
|
|
// this case should not happen and already indicates an issue but we don't want the system to fail due to being unable to log in detail
|
|
|
|
accountID = "UNKNOWN"
|
|
|
|
}
|
|
|
|
//nolint
|
|
|
|
ctx = context.WithValue(ctx, nbContext.AccountIDKey, accountID)
|
|
|
|
|
2023-03-03 18:35:38 +01:00
|
|
|
if loginReq.GetMeta() == nil {
|
|
|
|
msg := status.Errorf(codes.FailedPrecondition,
|
2024-01-31 16:02:24 +01:00
|
|
|
"peer system meta has to be provided to log in. Peer %s, remote addr %s", peerKey.String(), realIP)
|
2024-07-03 11:33:02 +02:00
|
|
|
log.WithContext(ctx).Warn(msg)
|
2023-03-03 18:35:38 +01:00
|
|
|
return nil, msg
|
2023-02-13 12:21:02 +01:00
|
|
|
}
|
2023-02-27 16:44:26 +01:00
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
userID, err := s.processJwtToken(ctx, loginReq, peerKey)
|
2024-06-13 13:24:24 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2023-02-13 12:21:02 +01:00
|
|
|
}
|
2024-06-13 13:24:24 +02:00
|
|
|
|
2022-06-23 17:04:53 +02:00
|
|
|
var sshKey []byte
|
|
|
|
if loginReq.GetPeerKeys() != nil {
|
|
|
|
sshKey = loginReq.GetPeerKeys().GetSshPubKey()
|
|
|
|
}
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
peer, netMap, postureChecks, err := s.accountManager.LoginPeer(ctx, PeerLogin{
|
2023-03-03 18:35:38 +01:00
|
|
|
WireGuardPubKey: peerKey.String(),
|
|
|
|
SSHKey: string(sshKey),
|
2024-07-03 11:33:02 +02:00
|
|
|
Meta: extractPeerMeta(ctx, loginReq.GetMeta()),
|
2023-03-03 18:35:38 +01:00
|
|
|
UserID: userID,
|
|
|
|
SetupKey: loginReq.GetSetupKey(),
|
2024-03-27 18:48:48 +01:00
|
|
|
ConnectionIP: realIP,
|
2023-03-03 18:35:38 +01:00
|
|
|
})
|
2022-06-24 21:30:51 +02:00
|
|
|
if err != nil {
|
2024-07-03 11:33:02 +02:00
|
|
|
log.WithContext(ctx).Warnf("failed logging in peer %s: %s", peerKey, err)
|
|
|
|
return nil, mapError(ctx, err)
|
2022-06-24 21:30:51 +02:00
|
|
|
}
|
|
|
|
|
2023-09-04 11:37:39 +02:00
|
|
|
// if the login request contains setup key then it is a registration request
|
|
|
|
if loginReq.GetSetupKey() != "" {
|
2024-07-03 11:33:02 +02:00
|
|
|
s.ephemeralManager.OnPeerDisconnected(ctx, peer)
|
2023-09-04 11:37:39 +02:00
|
|
|
}
|
|
|
|
|
2024-09-08 12:06:14 +02:00
|
|
|
var relayToken *Token
|
|
|
|
if s.config.Relay != nil && len(s.config.Relay.Addresses) > 0 {
|
|
|
|
relayToken, err = s.secretsManager.GenerateRelayToken()
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("failed generating Relay token: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-15 16:56:26 +02:00
|
|
|
// if peer has reached this point then it has logged in
|
|
|
|
loginResp := &proto.LoginResponse{
|
2024-09-08 12:06:14 +02:00
|
|
|
WiretrusteeConfig: toWiretrusteeConfig(s.config, nil, relayToken),
|
2023-03-03 18:35:38 +01:00
|
|
|
PeerConfig: toPeerConfig(peer, netMap.Network, s.accountManager.GetDNSDomain()),
|
2024-07-03 11:33:02 +02:00
|
|
|
Checks: toProtocolChecks(ctx, postureChecks),
|
2021-08-15 16:56:26 +02:00
|
|
|
}
|
|
|
|
encryptedResp, err := encryption.EncryptMessage(peerKey, s.wgKey, loginResp)
|
|
|
|
if err != nil {
|
2024-07-03 11:33:02 +02:00
|
|
|
log.WithContext(ctx).Warnf("failed encrypting peer %s message", peer.ID)
|
2021-08-15 16:56:26 +02:00
|
|
|
return nil, status.Errorf(codes.Internal, "failed logging in peer")
|
|
|
|
}
|
|
|
|
|
|
|
|
return &proto.EncryptedMessage{
|
|
|
|
WgPubKey: s.wgKey.PublicKey().String(),
|
|
|
|
Body: encryptedResp,
|
|
|
|
}, nil
|
2021-07-17 14:38:59 +02:00
|
|
|
}
|
|
|
|
|
2024-06-13 13:24:24 +02:00
|
|
|
// processJwtToken validates the existence of a JWT token in the login request, and returns the corresponding user ID if
|
|
|
|
// the token is valid.
|
|
|
|
//
|
|
|
|
// The user ID can be empty if the token is not provided, which is acceptable if the peer is already
|
|
|
|
// registered or if it uses a setup key to register.
|
2024-07-03 11:33:02 +02:00
|
|
|
func (s *GRPCServer) processJwtToken(ctx context.Context, loginReq *proto.LoginRequest, peerKey wgtypes.Key) (string, error) {
|
2024-06-13 13:24:24 +02:00
|
|
|
userID := ""
|
|
|
|
if loginReq.GetJwtToken() != "" {
|
|
|
|
var err error
|
|
|
|
for i := 0; i < 3; i++ {
|
2024-07-03 11:33:02 +02:00
|
|
|
userID, err = s.validateToken(ctx, loginReq.GetJwtToken())
|
2024-06-13 13:24:24 +02:00
|
|
|
if err == nil {
|
|
|
|
break
|
|
|
|
}
|
2024-07-03 11:33:02 +02:00
|
|
|
log.WithContext(ctx).Warnf("failed validating JWT token sent from peer %s with error %v. "+
|
2024-06-13 13:24:24 +02:00
|
|
|
"Trying again as it may be due to the IdP cache issue", peerKey.String(), err)
|
|
|
|
time.Sleep(200 * time.Millisecond)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return userID, nil
|
|
|
|
}
|
|
|
|
|
2021-09-02 14:41:54 +02:00
|
|
|
func ToResponseProto(configProto Protocol) proto.HostConfig_Protocol {
|
2021-07-30 17:46:38 +02:00
|
|
|
switch configProto {
|
2021-08-16 23:30:51 +02:00
|
|
|
case UDP:
|
2021-07-30 17:46:38 +02:00
|
|
|
return proto.HostConfig_UDP
|
2021-08-16 23:30:51 +02:00
|
|
|
case DTLS:
|
2021-07-30 17:46:38 +02:00
|
|
|
return proto.HostConfig_DTLS
|
2021-08-16 23:30:51 +02:00
|
|
|
case HTTP:
|
2021-07-30 17:46:38 +02:00
|
|
|
return proto.HostConfig_HTTP
|
2021-08-16 23:30:51 +02:00
|
|
|
case HTTPS:
|
2021-07-30 17:46:38 +02:00
|
|
|
return proto.HostConfig_HTTPS
|
2021-08-16 23:30:51 +02:00
|
|
|
case TCP:
|
2021-07-30 17:46:38 +02:00
|
|
|
return proto.HostConfig_TCP
|
|
|
|
default:
|
|
|
|
panic(fmt.Errorf("unexpected config protocol type %v", configProto))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-08 12:06:14 +02:00
|
|
|
func toWiretrusteeConfig(config *Config, turnCredentials *Token, relayToken *Token) *proto.WiretrusteeConfig {
|
2022-11-08 11:38:40 +01:00
|
|
|
if config == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2024-09-08 12:06:14 +02:00
|
|
|
|
2021-07-30 17:46:38 +02:00
|
|
|
var stuns []*proto.HostConfig
|
|
|
|
for _, stun := range config.Stuns {
|
|
|
|
stuns = append(stuns, &proto.HostConfig{
|
|
|
|
Uri: stun.URI,
|
2021-09-02 14:41:54 +02:00
|
|
|
Protocol: ToResponseProto(stun.Proto),
|
2021-07-30 17:46:38 +02:00
|
|
|
})
|
|
|
|
}
|
2024-09-08 12:06:14 +02:00
|
|
|
|
2021-07-30 17:46:38 +02:00
|
|
|
var turns []*proto.ProtectedHostConfig
|
2024-09-08 12:06:14 +02:00
|
|
|
if config.TURNConfig != nil {
|
|
|
|
for _, turn := range config.TURNConfig.Turns {
|
|
|
|
var username string
|
|
|
|
var password string
|
|
|
|
if turnCredentials != nil {
|
|
|
|
username = turnCredentials.Payload
|
|
|
|
password = turnCredentials.Signature
|
|
|
|
} else {
|
|
|
|
username = turn.Username
|
|
|
|
password = turn.Password
|
|
|
|
}
|
|
|
|
turns = append(turns, &proto.ProtectedHostConfig{
|
|
|
|
HostConfig: &proto.HostConfig{
|
|
|
|
Uri: turn.URI,
|
|
|
|
Protocol: ToResponseProto(turn.Proto),
|
|
|
|
},
|
|
|
|
User: username,
|
|
|
|
Password: password,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var relayCfg *proto.RelayConfig
|
|
|
|
if config.Relay != nil && len(config.Relay.Addresses) > 0 {
|
|
|
|
relayCfg = &proto.RelayConfig{
|
|
|
|
Urls: config.Relay.Addresses,
|
|
|
|
}
|
|
|
|
|
|
|
|
if relayToken != nil {
|
|
|
|
relayCfg.TokenPayload = relayToken.Payload
|
|
|
|
relayCfg.TokenSignature = relayToken.Signature
|
2021-09-02 14:41:54 +02:00
|
|
|
}
|
2021-07-30 17:46:38 +02:00
|
|
|
}
|
|
|
|
|
2021-08-15 16:56:26 +02:00
|
|
|
return &proto.WiretrusteeConfig{
|
2021-07-30 17:46:38 +02:00
|
|
|
Stuns: stuns,
|
|
|
|
Turns: turns,
|
|
|
|
Signal: &proto.HostConfig{
|
|
|
|
Uri: config.Signal.URI,
|
2021-09-02 14:41:54 +02:00
|
|
|
Protocol: ToResponseProto(config.Signal.Proto),
|
2021-07-30 17:46:38 +02:00
|
|
|
},
|
2024-09-08 12:06:14 +02:00
|
|
|
Relay: relayCfg,
|
2021-07-30 17:46:38 +02:00
|
|
|
}
|
2021-08-15 16:56:26 +02:00
|
|
|
}
|
2021-07-30 17:46:38 +02:00
|
|
|
|
2023-11-28 13:45:26 +01:00
|
|
|
func toPeerConfig(peer *nbpeer.Peer, network *Network, dnsName string) *proto.PeerConfig {
|
2022-06-24 21:30:51 +02:00
|
|
|
netmask, _ := network.Net.Mask.Size()
|
2023-01-02 15:11:32 +01:00
|
|
|
fqdn := peer.FQDN(dnsName)
|
2021-08-15 16:56:26 +02:00
|
|
|
return &proto.PeerConfig{
|
2022-06-24 21:30:51 +02:00
|
|
|
Address: fmt.Sprintf("%s/%d", peer.IP.String(), netmask), // take it from the network
|
2022-06-23 17:04:53 +02:00
|
|
|
SshConfig: &proto.SSHConfig{SshEnabled: peer.SSHEnabled},
|
2022-11-26 13:29:50 +01:00
|
|
|
Fqdn: fqdn,
|
2021-07-30 17:46:38 +02:00
|
|
|
}
|
2021-08-15 16:56:26 +02:00
|
|
|
}
|
|
|
|
|
2024-09-08 12:06:14 +02:00
|
|
|
func toSyncResponse(ctx context.Context, config *Config, peer *nbpeer.Peer, turnCredentials *Token, relayCredentials *Token, networkMap *NetworkMap, dnsName string, checks []*posture.Checks, dnsCache *DNSConfigCache) *proto.SyncResponse {
|
2024-08-07 10:52:31 +02:00
|
|
|
response := &proto.SyncResponse{
|
2024-09-08 12:06:14 +02:00
|
|
|
WiretrusteeConfig: toWiretrusteeConfig(config, turnCredentials, relayCredentials),
|
2024-08-07 10:52:31 +02:00
|
|
|
PeerConfig: toPeerConfig(peer, networkMap.Network, dnsName),
|
|
|
|
NetworkMap: &proto.NetworkMap{
|
|
|
|
Serial: networkMap.Network.CurrentSerial(),
|
|
|
|
Routes: toProtocolRoutes(networkMap.Routes),
|
|
|
|
DNSConfig: toProtocolDNSConfig(networkMap.DNSConfig, dnsCache),
|
|
|
|
},
|
|
|
|
Checks: toProtocolChecks(ctx, checks),
|
2021-07-30 17:46:38 +02:00
|
|
|
}
|
2022-11-07 15:38:21 +01:00
|
|
|
|
2024-08-07 10:52:31 +02:00
|
|
|
response.NetworkMap.PeerConfig = response.PeerConfig
|
2021-09-07 18:36:46 +02:00
|
|
|
|
2024-08-07 10:52:31 +02:00
|
|
|
allPeers := make([]*proto.RemotePeerConfig, 0, len(networkMap.Peers)+len(networkMap.OfflinePeers))
|
|
|
|
allPeers = appendRemotePeerConfig(allPeers, networkMap.Peers, dnsName)
|
|
|
|
response.RemotePeers = allPeers
|
|
|
|
response.NetworkMap.RemotePeers = allPeers
|
|
|
|
response.RemotePeersIsEmpty = len(allPeers) == 0
|
|
|
|
response.NetworkMap.RemotePeersIsEmpty = response.RemotePeersIsEmpty
|
2021-09-07 18:36:46 +02:00
|
|
|
|
2024-08-07 10:52:31 +02:00
|
|
|
response.NetworkMap.OfflinePeers = appendRemotePeerConfig(nil, networkMap.OfflinePeers, dnsName)
|
2023-03-07 10:17:25 +01:00
|
|
|
|
2023-05-29 16:00:18 +02:00
|
|
|
firewallRules := toProtocolFirewallRules(networkMap.FirewallRules)
|
2024-08-07 10:52:31 +02:00
|
|
|
response.NetworkMap.FirewallRules = firewallRules
|
|
|
|
response.NetworkMap.FirewallRulesIsEmpty = len(firewallRules) == 0
|
2023-05-29 16:00:18 +02:00
|
|
|
|
2024-08-07 10:52:31 +02:00
|
|
|
return response
|
|
|
|
}
|
|
|
|
|
|
|
|
func appendRemotePeerConfig(dst []*proto.RemotePeerConfig, peers []*nbpeer.Peer, dnsName string) []*proto.RemotePeerConfig {
|
|
|
|
for _, rPeer := range peers {
|
|
|
|
dst = append(dst, &proto.RemotePeerConfig{
|
|
|
|
WgPubKey: rPeer.Key,
|
|
|
|
AllowedIps: []string{rPeer.IP.String() + "/32"},
|
|
|
|
SshConfig: &proto.SSHConfig{SshPubKey: []byte(rPeer.SSHKey)},
|
|
|
|
Fqdn: rPeer.FQDN(dnsName),
|
|
|
|
})
|
2021-07-30 17:46:38 +02:00
|
|
|
}
|
2024-08-07 10:52:31 +02:00
|
|
|
return dst
|
2021-07-30 17:46:38 +02:00
|
|
|
}
|
|
|
|
|
2021-07-17 14:38:59 +02:00
|
|
|
// IsHealthy indicates whether the service is healthy
|
2022-07-29 20:37:09 +02:00
|
|
|
func (s *GRPCServer) IsHealthy(ctx context.Context, req *proto.Empty) (*proto.Empty, error) {
|
2021-07-17 14:38:59 +02:00
|
|
|
return &proto.Empty{}, nil
|
|
|
|
}
|
2021-07-22 10:28:00 +02:00
|
|
|
|
|
|
|
// sendInitialSync sends initial proto.SyncResponse to the peer requesting synchronization
|
2024-07-03 11:33:02 +02:00
|
|
|
func (s *GRPCServer) sendInitialSync(ctx context.Context, peerKey wgtypes.Key, peer *nbpeer.Peer, networkMap *NetworkMap, postureChecks []*posture.Checks, srv proto.ManagementService_SyncServer) error {
|
2024-09-08 12:06:14 +02:00
|
|
|
var err error
|
|
|
|
|
|
|
|
var turnToken *Token
|
|
|
|
if s.config.TURNConfig != nil && s.config.TURNConfig.TimeBasedCredentials {
|
|
|
|
turnToken, err = s.secretsManager.GenerateTurnToken()
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("failed generating TURN token: %v", err)
|
|
|
|
}
|
2021-09-02 14:41:54 +02:00
|
|
|
}
|
2024-09-08 12:06:14 +02:00
|
|
|
|
|
|
|
var relayToken *Token
|
|
|
|
if s.config.Relay != nil && len(s.config.Relay.Addresses) > 0 {
|
|
|
|
relayToken, err = s.secretsManager.GenerateRelayToken()
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("failed generating Relay token: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
plainResp := toSyncResponse(ctx, s.config, peer, turnToken, relayToken, networkMap, s.accountManager.GetDNSDomain(), postureChecks, nil)
|
2021-07-22 10:28:00 +02:00
|
|
|
|
2021-07-22 15:23:24 +02:00
|
|
|
encryptedResp, err := encryption.EncryptMessage(peerKey, s.wgKey, plainResp)
|
2021-07-22 10:28:00 +02:00
|
|
|
if err != nil {
|
|
|
|
return status.Errorf(codes.Internal, "error handling request")
|
|
|
|
}
|
2021-09-02 14:41:54 +02:00
|
|
|
|
2021-07-22 15:23:24 +02:00
|
|
|
err = srv.Send(&proto.EncryptedMessage{
|
|
|
|
WgPubKey: s.wgKey.PublicKey().String(),
|
|
|
|
Body: encryptedResp,
|
|
|
|
})
|
2021-07-22 10:28:00 +02:00
|
|
|
|
|
|
|
if err != nil {
|
2024-07-03 11:33:02 +02:00
|
|
|
log.WithContext(ctx).Errorf("failed sending SyncResponse %v", err)
|
2021-07-22 10:28:00 +02:00
|
|
|
return status.Errorf(codes.Internal, "error handling request")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2022-05-08 11:04:57 +02:00
|
|
|
|
|
|
|
// GetDeviceAuthorizationFlow returns a device authorization flow information
|
|
|
|
// This is used for initiating an Oauth 2 device authorization grant flow
|
|
|
|
// which will be used by our clients to Login
|
2022-07-29 20:37:09 +02:00
|
|
|
func (s *GRPCServer) GetDeviceAuthorizationFlow(ctx context.Context, req *proto.EncryptedMessage) (*proto.EncryptedMessage, error) {
|
2022-05-08 11:04:57 +02:00
|
|
|
peerKey, err := wgtypes.ParseKey(req.GetWgPubKey())
|
|
|
|
if err != nil {
|
|
|
|
errMSG := fmt.Sprintf("error while parsing peer's Wireguard public key %s on GetDeviceAuthorizationFlow request.", req.WgPubKey)
|
2024-07-03 11:33:02 +02:00
|
|
|
log.WithContext(ctx).Warn(errMSG)
|
2022-05-08 11:04:57 +02:00
|
|
|
return nil, status.Error(codes.InvalidArgument, errMSG)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = encryption.DecryptMessage(peerKey, s.wgKey, req.Body, &proto.DeviceAuthorizationFlowRequest{})
|
|
|
|
if err != nil {
|
|
|
|
errMSG := fmt.Sprintf("error while decrypting peer's message with Wireguard public key %s.", req.WgPubKey)
|
2024-07-03 11:33:02 +02:00
|
|
|
log.WithContext(ctx).Warn(errMSG)
|
2022-05-08 11:04:57 +02:00
|
|
|
return nil, status.Error(codes.InvalidArgument, errMSG)
|
|
|
|
}
|
|
|
|
|
2022-08-24 14:37:18 +02:00
|
|
|
if s.config.DeviceAuthorizationFlow == nil || s.config.DeviceAuthorizationFlow.Provider == string(NONE) {
|
2022-05-08 11:04:57 +02:00
|
|
|
return nil, status.Error(codes.NotFound, "no device authorization flow information available")
|
|
|
|
}
|
|
|
|
|
|
|
|
provider, ok := proto.DeviceAuthorizationFlowProvider_value[strings.ToUpper(s.config.DeviceAuthorizationFlow.Provider)]
|
|
|
|
if !ok {
|
|
|
|
return nil, status.Errorf(codes.InvalidArgument, "no provider found in the protocol for %s", s.config.DeviceAuthorizationFlow.Provider)
|
|
|
|
}
|
|
|
|
|
|
|
|
flowInfoResp := &proto.DeviceAuthorizationFlow{
|
|
|
|
Provider: proto.DeviceAuthorizationFlowProvider(provider),
|
|
|
|
ProviderConfig: &proto.ProviderConfig{
|
2022-08-23 15:46:12 +02:00
|
|
|
ClientID: s.config.DeviceAuthorizationFlow.ProviderConfig.ClientID,
|
|
|
|
ClientSecret: s.config.DeviceAuthorizationFlow.ProviderConfig.ClientSecret,
|
|
|
|
Domain: s.config.DeviceAuthorizationFlow.ProviderConfig.Domain,
|
|
|
|
Audience: s.config.DeviceAuthorizationFlow.ProviderConfig.Audience,
|
|
|
|
DeviceAuthEndpoint: s.config.DeviceAuthorizationFlow.ProviderConfig.DeviceAuthEndpoint,
|
|
|
|
TokenEndpoint: s.config.DeviceAuthorizationFlow.ProviderConfig.TokenEndpoint,
|
2023-04-05 17:46:34 +02:00
|
|
|
Scope: s.config.DeviceAuthorizationFlow.ProviderConfig.Scope,
|
|
|
|
UseIDToken: s.config.DeviceAuthorizationFlow.ProviderConfig.UseIDToken,
|
2022-05-08 11:04:57 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
encryptedResp, err := encryption.EncryptMessage(peerKey, s.wgKey, flowInfoResp)
|
|
|
|
if err != nil {
|
|
|
|
return nil, status.Error(codes.Internal, "failed to encrypt no device authorization flow information")
|
|
|
|
}
|
|
|
|
|
|
|
|
return &proto.EncryptedMessage{
|
|
|
|
WgPubKey: s.wgKey.PublicKey().String(),
|
|
|
|
Body: encryptedResp,
|
|
|
|
}, nil
|
|
|
|
}
|
2023-07-27 11:31:07 +02:00
|
|
|
|
|
|
|
// GetPKCEAuthorizationFlow returns a pkce authorization flow information
|
|
|
|
// This is used for initiating an Oauth 2 pkce authorization grant flow
|
|
|
|
// which will be used by our clients to Login
|
2024-07-03 11:33:02 +02:00
|
|
|
func (s *GRPCServer) GetPKCEAuthorizationFlow(ctx context.Context, req *proto.EncryptedMessage) (*proto.EncryptedMessage, error) {
|
2023-07-27 11:31:07 +02:00
|
|
|
peerKey, err := wgtypes.ParseKey(req.GetWgPubKey())
|
|
|
|
if err != nil {
|
|
|
|
errMSG := fmt.Sprintf("error while parsing peer's Wireguard public key %s on GetPKCEAuthorizationFlow request.", req.WgPubKey)
|
2024-07-03 11:33:02 +02:00
|
|
|
log.WithContext(ctx).Warn(errMSG)
|
2023-07-27 11:31:07 +02:00
|
|
|
return nil, status.Error(codes.InvalidArgument, errMSG)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = encryption.DecryptMessage(peerKey, s.wgKey, req.Body, &proto.PKCEAuthorizationFlowRequest{})
|
|
|
|
if err != nil {
|
|
|
|
errMSG := fmt.Sprintf("error while decrypting peer's message with Wireguard public key %s.", req.WgPubKey)
|
2024-07-03 11:33:02 +02:00
|
|
|
log.WithContext(ctx).Warn(errMSG)
|
2023-07-27 11:31:07 +02:00
|
|
|
return nil, status.Error(codes.InvalidArgument, errMSG)
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.config.PKCEAuthorizationFlow == nil {
|
|
|
|
return nil, status.Error(codes.NotFound, "no pkce authorization flow information available")
|
|
|
|
}
|
|
|
|
|
|
|
|
flowInfoResp := &proto.PKCEAuthorizationFlow{
|
|
|
|
ProviderConfig: &proto.ProviderConfig{
|
|
|
|
Audience: s.config.PKCEAuthorizationFlow.ProviderConfig.Audience,
|
|
|
|
ClientID: s.config.PKCEAuthorizationFlow.ProviderConfig.ClientID,
|
|
|
|
ClientSecret: s.config.PKCEAuthorizationFlow.ProviderConfig.ClientSecret,
|
|
|
|
TokenEndpoint: s.config.PKCEAuthorizationFlow.ProviderConfig.TokenEndpoint,
|
|
|
|
AuthorizationEndpoint: s.config.PKCEAuthorizationFlow.ProviderConfig.AuthorizationEndpoint,
|
|
|
|
Scope: s.config.PKCEAuthorizationFlow.ProviderConfig.Scope,
|
|
|
|
RedirectURLs: s.config.PKCEAuthorizationFlow.ProviderConfig.RedirectURLs,
|
|
|
|
UseIDToken: s.config.PKCEAuthorizationFlow.ProviderConfig.UseIDToken,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
encryptedResp, err := encryption.EncryptMessage(peerKey, s.wgKey, flowInfoResp)
|
|
|
|
if err != nil {
|
|
|
|
return nil, status.Error(codes.Internal, "failed to encrypt no pkce authorization flow information")
|
|
|
|
}
|
|
|
|
|
|
|
|
return &proto.EncryptedMessage{
|
|
|
|
WgPubKey: s.wgKey.PublicKey().String(),
|
|
|
|
Body: encryptedResp,
|
|
|
|
}, nil
|
|
|
|
}
|
2024-06-13 13:24:24 +02:00
|
|
|
|
|
|
|
// SyncMeta endpoint is used to synchronize peer's system metadata and notifies the connected,
|
|
|
|
// peer's under the same account of any updates.
|
|
|
|
func (s *GRPCServer) SyncMeta(ctx context.Context, req *proto.EncryptedMessage) (*proto.Empty, error) {
|
|
|
|
realIP := getRealIP(ctx)
|
2024-07-03 11:33:02 +02:00
|
|
|
log.WithContext(ctx).Debugf("Sync meta request from peer [%s] [%s]", req.WgPubKey, realIP.String())
|
2024-06-13 13:24:24 +02:00
|
|
|
|
|
|
|
syncMetaReq := &proto.SyncMetaRequest{}
|
2024-07-03 11:33:02 +02:00
|
|
|
peerKey, err := s.parseRequest(ctx, req, syncMetaReq)
|
2024-06-13 13:24:24 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if syncMetaReq.GetMeta() == nil {
|
|
|
|
msg := status.Errorf(codes.FailedPrecondition,
|
|
|
|
"peer system meta has to be provided on sync. Peer %s, remote addr %s", peerKey.String(), realIP)
|
2024-07-03 11:33:02 +02:00
|
|
|
log.WithContext(ctx).Warn(msg)
|
2024-06-13 13:24:24 +02:00
|
|
|
return nil, msg
|
|
|
|
}
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
err = s.accountManager.SyncPeerMeta(ctx, peerKey.String(), extractPeerMeta(ctx, syncMetaReq.GetMeta()))
|
2024-06-13 13:24:24 +02:00
|
|
|
if err != nil {
|
2024-07-03 11:33:02 +02:00
|
|
|
return nil, mapError(ctx, err)
|
2024-06-13 13:24:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return &proto.Empty{}, nil
|
|
|
|
}
|
|
|
|
|
2024-06-22 16:41:16 +02:00
|
|
|
// toProtocolChecks converts posture checks to protocol checks.
|
2024-07-03 11:33:02 +02:00
|
|
|
func toProtocolChecks(ctx context.Context, postureChecks []*posture.Checks) []*proto.Checks {
|
2024-06-22 16:41:16 +02:00
|
|
|
protoChecks := make([]*proto.Checks, 0, len(postureChecks))
|
2024-06-13 13:24:24 +02:00
|
|
|
for _, postureCheck := range postureChecks {
|
|
|
|
protoChecks = append(protoChecks, toProtocolCheck(postureCheck))
|
|
|
|
}
|
|
|
|
|
|
|
|
return protoChecks
|
|
|
|
}
|
|
|
|
|
|
|
|
// toProtocolCheck converts a posture.Checks to a proto.Checks.
|
2024-06-22 16:41:16 +02:00
|
|
|
func toProtocolCheck(postureCheck *posture.Checks) *proto.Checks {
|
2024-06-13 13:24:24 +02:00
|
|
|
protoCheck := &proto.Checks{}
|
|
|
|
|
|
|
|
if check := postureCheck.Checks.ProcessCheck; check != nil {
|
|
|
|
for _, process := range check.Processes {
|
|
|
|
if process.LinuxPath != "" {
|
|
|
|
protoCheck.Files = append(protoCheck.Files, process.LinuxPath)
|
|
|
|
}
|
|
|
|
if process.MacPath != "" {
|
|
|
|
protoCheck.Files = append(protoCheck.Files, process.MacPath)
|
|
|
|
}
|
|
|
|
if process.WindowsPath != "" {
|
|
|
|
protoCheck.Files = append(protoCheck.Files, process.WindowsPath)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return protoCheck
|
|
|
|
}
|