From 628b497e8198017950fe4ca5fe3facae2952586e Mon Sep 17 00:00:00 2001 From: Maycon Santos Date: Thu, 23 Mar 2023 16:35:06 +0100 Subject: [PATCH] Adjustments for the change server flow (#756) Check SSO support by calling the internal.GetDeviceAuthorizationFlowInfo Rename LoginSaveConfigIfSSOSupported to SaveConfigIfSSOSupported Receive device name as input for setup-key login have a default android name when no context value is provided log non parsed errors from management registration calls --- client/android/login.go | 52 +++++++++++++++++++++------------ client/system/info_android.go | 2 +- management/server/grpcserver.go | 11 ++++--- 3 files changed, 42 insertions(+), 23 deletions(-) diff --git a/client/android/login.go b/client/android/login.go index e4cb5513d..4e2f1ab30 100644 --- a/client/android/login.go +++ b/client/android/login.go @@ -3,14 +3,18 @@ package android import ( "context" "fmt" - "github.com/cenkalti/backoff/v4" - "github.com/netbirdio/netbird/client/cmd" "time" - "github.com/netbirdio/netbird/client/internal" + "github.com/cenkalti/backoff/v4" + log "github.com/sirupsen/logrus" "google.golang.org/grpc/codes" gstatus "google.golang.org/grpc/status" + + "github.com/netbirdio/netbird/client/cmd" + "github.com/netbirdio/netbird/client/system" + + "github.com/netbirdio/netbird/client/internal" ) // URLOpener it is a callback interface. The Open function will be triggered if @@ -52,32 +56,44 @@ func NewAuthWithConfig(ctx context.Context, config *internal.Config) *Auth { } } -// LoginAndSaveConfigIfSSOSupported test the connectivity with the management server. -// If the SSO is supported than save the configuration. Return with the SSO login is supported or not. -func (a *Auth) LoginAndSaveConfigIfSSOSupported() (bool, error) { - var needsLogin bool +// SaveConfigIfSSOSupported test the connectivity with the management server by retrieving the server device flow info. +// If it returns a flow info than save the configuration and return true. If it gets a codes.NotFound, it means that SSO +// is not supported and returns false without saving the configuration. For other errors return false. +func (a *Auth) SaveConfigIfSSOSupported() (bool, error) { + supportsSSO := true err := a.withBackOff(a.ctx, func() (err error) { - needsLogin, err = internal.IsLoginRequired(a.ctx, a.config.PrivateKey, a.config.ManagementURL, a.config.SSHKey) - return + _, err = internal.GetDeviceAuthorizationFlowInfo(a.ctx, a.config.PrivateKey, a.config.ManagementURL) + if s, ok := gstatus.FromError(err); ok && s.Code() == codes.NotFound { + supportsSSO = false + err = nil + } + return err }) + + if !supportsSSO { + return false, nil + } + if err != nil { return false, fmt.Errorf("backoff cycle failed: %v", err) } - if !needsLogin { - return false, nil - } + err = internal.WriteOutConfig(a.cfgPath, a.config) - return needsLogin, err + return true, err } // LoginWithSetupKeyAndSaveConfig test the connectivity with the management server with the setup key. -func (a *Auth) LoginWithSetupKeyAndSaveConfig(setupKey string) error { +func (a *Auth) LoginWithSetupKeyAndSaveConfig(setupKey string, deviceName string) error { + //nolint + ctxWithValues := context.WithValue(a.ctx, system.DeviceNameCtxKey, deviceName) + err := a.withBackOff(a.ctx, func() error { - err := internal.Login(a.ctx, a.config, setupKey, "") - if s, ok := gstatus.FromError(err); ok && (s.Code() == codes.InvalidArgument || s.Code() == codes.PermissionDenied) { - return nil + backoffErr := internal.Login(ctxWithValues, a.config, setupKey, "") + if s, ok := gstatus.FromError(backoffErr); ok && (s.Code() == codes.PermissionDenied) { + // we got an answer from management, exit backoff earlier + return backoff.Permanent(backoffErr) } - return err + return backoffErr }) if err != nil { return fmt.Errorf("backoff cycle failed: %v", err) diff --git a/client/system/info_android.go b/client/system/info_android.go index 65fb409f6..9ea9c0487 100644 --- a/client/system/info_android.go +++ b/client/system/info_android.go @@ -34,7 +34,7 @@ func GetInfo(ctx context.Context) *Info { func extractDeviceName(ctx context.Context) string { v, ok := ctx.Value(DeviceNameCtxKey).(string) if !ok { - return "" + return "android" } return v } diff --git a/management/server/grpcserver.go b/management/server/grpcserver.go index fa0e49ed3..45be9815c 100644 --- a/management/server/grpcserver.go +++ b/management/server/grpcserver.go @@ -3,24 +3,26 @@ package server import ( "context" "fmt" - pb "github.com/golang/protobuf/proto" //nolint "strings" "time" + pb "github.com/golang/protobuf/proto" //nolint + "github.com/netbirdio/netbird/management/server/telemetry" "github.com/netbirdio/netbird/management/server/http/middleware" "github.com/netbirdio/netbird/management/server/jwtclaims" "github.com/golang/protobuf/ptypes/timestamp" - "github.com/netbirdio/netbird/encryption" - "github.com/netbirdio/netbird/management/proto" - internalStatus "github.com/netbirdio/netbird/management/server/status" log "github.com/sirupsen/logrus" "golang.zx2c4.com/wireguard/wgctrl/wgtypes" "google.golang.org/grpc/codes" gRPCPeer "google.golang.org/grpc/peer" "google.golang.org/grpc/status" + + "github.com/netbirdio/netbird/encryption" + "github.com/netbirdio/netbird/management/proto" + internalStatus "github.com/netbirdio/netbird/management/server/status" ) // GRPCServer an instance of a Management gRPC API server @@ -222,6 +224,7 @@ func mapError(err error) error { default: } } + log.Errorf("got an unhandled error: %s", err) return status.Errorf(codes.Internal, "failed handling request") }