Skip to content

Commit

Permalink
Fix UP calls when state is idle (#338)
Browse files Browse the repository at this point in the history
* Fix UP calls when state is idle

When we want to login we can call server.Login
It already checks the login status of the peer

* Remove unused status

* Defer close daemon client conn

Co-authored-by: braginini <[email protected]>
  • Loading branch information
mlsmaycon and braginini authored May 27, 2022
1 parent 4088aaf commit 258cb3d
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 47 deletions.
81 changes: 44 additions & 37 deletions client/cmd/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/netbirdio/netbird/client/internal"
"github.com/netbirdio/netbird/client/proto"
"github.com/netbirdio/netbird/util"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"google.golang.org/grpc/codes"
gstatus "google.golang.org/grpc/status"
Expand Down Expand Up @@ -55,7 +56,13 @@ var upCmd = &cobra.Command{
"If the daemon is not running please run: "+
"\nnetbird service install \nnetbird service start\n", err)
}
defer conn.Close()
defer func() {
err := conn.Close()
if err != nil {
log.Warnf("failed closing dameon gRPC client connection %v", err)
return
}
}()

client := proto.NewDaemonServiceClient(conn)

Expand All @@ -64,49 +71,49 @@ var upCmd = &cobra.Command{
return fmt.Errorf("unable to get daemon status: %v", err)
}

if status.Status == string(internal.StatusNeedsLogin) || status.Status == string(internal.StatusLoginFailed) {
loginRequest := proto.LoginRequest{
SetupKey: setupKey,
PreSharedKey: preSharedKey,
ManagementUrl: managementURL,
}
if status.Status == string(internal.StatusConnected) {
cmd.Println("Already connected")
return nil
}

var loginErr error

var loginResp *proto.LoginResponse

err = WithBackOff(func() error {
var backOffErr error
loginResp, backOffErr = client.Login(ctx, &loginRequest)
if s, ok := gstatus.FromError(backOffErr); ok && (s.Code() == codes.InvalidArgument ||
s.Code() == codes.PermissionDenied ||
s.Code() == codes.NotFound ||
s.Code() == codes.Unimplemented) {
loginErr = backOffErr
return nil
}
return backOffErr
})
if err != nil {
return fmt.Errorf("login backoff cycle failed: %v", err)
}
loginRequest := proto.LoginRequest{
SetupKey: setupKey,
PreSharedKey: preSharedKey,
ManagementUrl: managementURL,
}

if loginErr != nil {
return fmt.Errorf("login failed: %v", loginErr)
var loginErr error

var loginResp *proto.LoginResponse

err = WithBackOff(func() error {
var backOffErr error
loginResp, backOffErr = client.Login(ctx, &loginRequest)
if s, ok := gstatus.FromError(backOffErr); ok && (s.Code() == codes.InvalidArgument ||
s.Code() == codes.PermissionDenied ||
s.Code() == codes.NotFound ||
s.Code() == codes.Unimplemented) {
loginErr = backOffErr
return nil
}
return backOffErr
})
if err != nil {
return fmt.Errorf("login backoff cycle failed: %v", err)
}

if loginResp.NeedsSSOLogin {
if loginErr != nil {
return fmt.Errorf("login failed: %v", loginErr)
}

openURL(cmd, loginResp.VerificationURIComplete)
if loginResp.NeedsSSOLogin {

_, err = client.WaitSSOLogin(ctx, &proto.WaitSSOLoginRequest{UserCode: loginResp.UserCode})
if err != nil {
return fmt.Errorf("waiting sso login failed with: %v", err)
}
openURL(cmd, loginResp.VerificationURIComplete)

_, err = client.WaitSSOLogin(ctx, &proto.WaitSSOLoginRequest{UserCode: loginResp.UserCode})
if err != nil {
return fmt.Errorf("waiting sso login failed with: %v", err)
}
} else if status.Status != string(internal.StatusIdle) {
cmd.Println("Already connected")
return nil
}

if _, err := client.Up(ctx, &proto.UpRequest{}); err != nil {
Expand Down
12 changes: 2 additions & 10 deletions client/ui/client_ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,21 +259,13 @@ func (s *serviceClient) menuUpClick() error {
return err
}

status, err := conn.Status(s.ctx, &proto.StatusRequest{})
err = s.login()
if err != nil {
log.Errorf("get service status: %v", err)
return err
}

if status.Status == string(internal.StatusNeedsLogin) || status.Status == string(internal.StatusLoginFailed) {
err = s.login()
if err != nil {
log.Errorf("get service status: %v", err)
return err
}
}

status, err = conn.Status(s.ctx, &proto.StatusRequest{})
status, err := conn.Status(s.ctx, &proto.StatusRequest{})
if err != nil {
log.Errorf("get service status: %v", err)
return err
Expand Down

0 comments on commit 258cb3d

Please sign in to comment.