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
This commit is contained in:
Maycon Santos 2023-03-23 16:35:06 +01:00 committed by GitHub
parent 8f66dea11c
commit 628b497e81
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 42 additions and 23 deletions

View File

@ -3,14 +3,18 @@ package android
import ( import (
"context" "context"
"fmt" "fmt"
"github.com/cenkalti/backoff/v4"
"github.com/netbirdio/netbird/client/cmd"
"time" "time"
"github.com/netbirdio/netbird/client/internal" "github.com/cenkalti/backoff/v4"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"google.golang.org/grpc/codes" "google.golang.org/grpc/codes"
gstatus "google.golang.org/grpc/status" 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 // 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. // SaveConfigIfSSOSupported test the connectivity with the management server by retrieving the server device flow info.
// If the SSO is supported than save the configuration. Return with the SSO login is supported or not. // If it returns a flow info than save the configuration and return true. If it gets a codes.NotFound, it means that SSO
func (a *Auth) LoginAndSaveConfigIfSSOSupported() (bool, error) { // is not supported and returns false without saving the configuration. For other errors return false.
var needsLogin bool func (a *Auth) SaveConfigIfSSOSupported() (bool, error) {
supportsSSO := true
err := a.withBackOff(a.ctx, func() (err error) { err := a.withBackOff(a.ctx, func() (err error) {
needsLogin, err = internal.IsLoginRequired(a.ctx, a.config.PrivateKey, a.config.ManagementURL, a.config.SSHKey) _, err = internal.GetDeviceAuthorizationFlowInfo(a.ctx, a.config.PrivateKey, a.config.ManagementURL)
return 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 { if err != nil {
return false, fmt.Errorf("backoff cycle failed: %v", err) return false, fmt.Errorf("backoff cycle failed: %v", err)
} }
if !needsLogin {
return false, nil
}
err = internal.WriteOutConfig(a.cfgPath, a.config) 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. // 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 := a.withBackOff(a.ctx, func() error {
err := internal.Login(a.ctx, a.config, setupKey, "") backoffErr := internal.Login(ctxWithValues, a.config, setupKey, "")
if s, ok := gstatus.FromError(err); ok && (s.Code() == codes.InvalidArgument || s.Code() == codes.PermissionDenied) { if s, ok := gstatus.FromError(backoffErr); ok && (s.Code() == codes.PermissionDenied) {
return nil // we got an answer from management, exit backoff earlier
return backoff.Permanent(backoffErr)
} }
return err return backoffErr
}) })
if err != nil { if err != nil {
return fmt.Errorf("backoff cycle failed: %v", err) return fmt.Errorf("backoff cycle failed: %v", err)

View File

@ -34,7 +34,7 @@ func GetInfo(ctx context.Context) *Info {
func extractDeviceName(ctx context.Context) string { func extractDeviceName(ctx context.Context) string {
v, ok := ctx.Value(DeviceNameCtxKey).(string) v, ok := ctx.Value(DeviceNameCtxKey).(string)
if !ok { if !ok {
return "" return "android"
} }
return v return v
} }

View File

@ -3,24 +3,26 @@ package server
import ( import (
"context" "context"
"fmt" "fmt"
pb "github.com/golang/protobuf/proto" //nolint
"strings" "strings"
"time" "time"
pb "github.com/golang/protobuf/proto" //nolint
"github.com/netbirdio/netbird/management/server/telemetry" "github.com/netbirdio/netbird/management/server/telemetry"
"github.com/netbirdio/netbird/management/server/http/middleware" "github.com/netbirdio/netbird/management/server/http/middleware"
"github.com/netbirdio/netbird/management/server/jwtclaims" "github.com/netbirdio/netbird/management/server/jwtclaims"
"github.com/golang/protobuf/ptypes/timestamp" "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" log "github.com/sirupsen/logrus"
"golang.zx2c4.com/wireguard/wgctrl/wgtypes" "golang.zx2c4.com/wireguard/wgctrl/wgtypes"
"google.golang.org/grpc/codes" "google.golang.org/grpc/codes"
gRPCPeer "google.golang.org/grpc/peer" gRPCPeer "google.golang.org/grpc/peer"
"google.golang.org/grpc/status" "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 // GRPCServer an instance of a Management gRPC API server
@ -222,6 +224,7 @@ func mapError(err error) error {
default: default:
} }
} }
log.Errorf("got an unhandled error: %s", err)
return status.Errorf(codes.Internal, "failed handling request") return status.Errorf(codes.Internal, "failed handling request")
} }