Skip to content

Commit b5ed948

Browse files
authored
[management, client] Add logout feature (#4268)
1 parent 552dc60 commit b5ed948

File tree

18 files changed

+707
-122
lines changed

18 files changed

+707
-122
lines changed

client/cmd/logout.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package cmd
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"os/user"
7+
"time"
8+
9+
"github.com/spf13/cobra"
10+
11+
"github.com/netbirdio/netbird/client/proto"
12+
)
13+
14+
var logoutCmd = &cobra.Command{
15+
Use: "logout",
16+
Short: "logout from the Netbird Management Service and delete peer",
17+
RunE: func(cmd *cobra.Command, args []string) error {
18+
SetFlagsFromEnvVars(rootCmd)
19+
20+
cmd.SetOut(cmd.OutOrStdout())
21+
22+
ctx, cancel := context.WithTimeout(context.Background(), time.Second*7)
23+
defer cancel()
24+
25+
conn, err := DialClientGRPCServer(ctx, daemonAddr)
26+
if err != nil {
27+
return fmt.Errorf("connect to daemon: %v", err)
28+
}
29+
defer conn.Close()
30+
31+
daemonClient := proto.NewDaemonServiceClient(conn)
32+
33+
req := &proto.LogoutRequest{}
34+
35+
if profileName != "" {
36+
req.ProfileName = &profileName
37+
38+
currUser, err := user.Current()
39+
if err != nil {
40+
return fmt.Errorf("get current user: %v", err)
41+
}
42+
username := currUser.Username
43+
req.Username = &username
44+
}
45+
46+
if _, err := daemonClient.Logout(ctx, req); err != nil {
47+
return fmt.Errorf("logout: %v", err)
48+
}
49+
50+
cmd.Println("Logged out successfully")
51+
return nil
52+
},
53+
}
54+
55+
func init() {
56+
logoutCmd.PersistentFlags().StringVar(&profileName, profileNameFlag, "", profileNameDesc)
57+
}

client/cmd/profile.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@ package cmd
33
import (
44
"context"
55
"fmt"
6-
"time"
7-
86
"os/user"
7+
"time"
98

109
"github.com/spf13/cobra"
1110

@@ -22,10 +21,11 @@ var profileCmd = &cobra.Command{
2221
}
2322

2423
var profileListCmd = &cobra.Command{
25-
Use: "list",
26-
Short: "list all profiles",
27-
Long: `List all available profiles in the Netbird client.`,
28-
RunE: listProfilesFunc,
24+
Use: "list",
25+
Short: "list all profiles",
26+
Long: `List all available profiles in the Netbird client.`,
27+
Aliases: []string{"ls"},
28+
RunE: listProfilesFunc,
2929
}
3030

3131
var profileAddCmd = &cobra.Command{

client/cmd/root.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ func init() {
133133
rootCmd.AddCommand(downCmd)
134134
rootCmd.AddCommand(statusCmd)
135135
rootCmd.AddCommand(loginCmd)
136+
rootCmd.AddCommand(logoutCmd)
136137
rootCmd.AddCommand(versionCmd)
137138
rootCmd.AddCommand(sshCmd)
138139
rootCmd.AddCommand(networksCMD)

client/internal/profilemanager/profilemanager.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ import (
1313
)
1414

1515
const (
16-
defaultProfileName = "default"
16+
DefaultProfileName = "default"
17+
defaultProfileName = DefaultProfileName // Keep for backward compatibility
1718
activeProfileStateFilename = "active_profile.txt"
1819
)
1920

client/proto/daemon.pb.go

Lines changed: 149 additions & 49 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

client/proto/daemon.proto

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ service DaemonService {
7979
rpc ListProfiles(ListProfilesRequest) returns (ListProfilesResponse) {}
8080

8181
rpc GetActiveProfile(GetActiveProfileRequest) returns (GetActiveProfileResponse) {}
82+
83+
// Logout disconnects from the network and deletes the peer from the management server
84+
rpc Logout(LogoutRequest) returns (LogoutResponse) {}
8285
}
8386

8487

@@ -614,4 +617,11 @@ message GetActiveProfileRequest {}
614617
message GetActiveProfileResponse {
615618
string profileName = 1;
616619
string username = 2;
617-
}
620+
}
621+
622+
message LogoutRequest {
623+
optional string profileName = 1;
624+
optional string username = 2;
625+
}
626+
627+
message LogoutResponse {}

client/proto/daemon_grpc.pb.go

Lines changed: 38 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)