Skip to content

Commit 1b4865a

Browse files
authored
feat(cli): reauthenticate user in case of invalid token (#3643)
1 parent c40b26e commit 1b4865a

15 files changed

+165
-46
lines changed

cli/cmd/configure_cmd.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ var configureCmd = &cobra.Command{
2121
Short: "Configure your tracetest CLI",
2222
Long: "Configure your tracetest CLI",
2323
PreRun: setupLogger,
24-
Run: WithResultHandler(WithParamsHandler(configParams)(func(cmd *cobra.Command, _ []string) (string, error) {
25-
ctx := context.Background()
24+
Run: WithResultHandler(WithParamsHandler(configParams)(func(ctx context.Context, cmd *cobra.Command, _ []string) (string, error) {
2625
flags := agentConfig.Flags{
2726
CI: configParams.CI,
2827
}

cli/cmd/dashboard_cmd.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package cmd
22

33
import (
4+
"context"
45
"fmt"
56

67
"github.com/kubeshop/tracetest/cli/ui"
@@ -13,7 +14,7 @@ var dashboardCmd = &cobra.Command{
1314
Short: "Opens the Tracetest Dashboard URL",
1415
Long: "Opens the Tracetest Dashboard URL",
1516
PreRun: setupCommand(),
16-
Run: WithResultHandler(func(_ *cobra.Command, _ []string) (string, error) {
17+
Run: WithResultHandler(func(_ context.Context, _ *cobra.Command, _ []string) (string, error) {
1718
if cliConfig.IsEmpty() {
1819
return "", fmt.Errorf("missing Tracetest endpoint configuration")
1920
}

cli/cmd/middleware.go

Lines changed: 57 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,55 @@
11
package cmd
22

33
import (
4+
"context"
45
"errors"
56
"fmt"
67
"os"
78

9+
"github.com/kubeshop/tracetest/cli/config"
810
"github.com/kubeshop/tracetest/cli/pkg/resourcemanager"
11+
"github.com/kubeshop/tracetest/cli/ui"
912

1013
"github.com/spf13/cobra"
1114
)
1215

13-
type RunFn func(cmd *cobra.Command, args []string) (string, error)
16+
type RunFn func(ctx context.Context, cmd *cobra.Command, args []string) (string, error)
1417
type CobraRunFn func(cmd *cobra.Command, args []string)
1518
type MiddlewareWrapper func(RunFn) RunFn
1619

20+
func rootCtx(cmd *cobra.Command) context.Context {
21+
// cobra does not correctly progpagate rootcmd context to sub commands,
22+
// so we need to manually traverse the command tree to find the root context
23+
if cmd == nil {
24+
return nil
25+
}
26+
27+
var (
28+
ctx = cmd.Context()
29+
p = cmd.Parent()
30+
)
31+
if cmd.Parent() == nil {
32+
return ctx
33+
}
34+
for {
35+
ctx = p.Context()
36+
p = p.Parent()
37+
if p == nil {
38+
break
39+
}
40+
}
41+
return ctx
42+
}
43+
1744
func WithResultHandler(runFn RunFn) CobraRunFn {
1845
return func(cmd *cobra.Command, args []string) {
19-
res, err := runFn(cmd, args)
46+
// we need the root cmd context in case of an error caused rerun
47+
ctx := rootCtx(cmd)
48+
49+
res, err := runFn(ctx, cmd, args)
2050

2151
if err != nil {
22-
OnError(err)
52+
handleError(ctx, err)
2353
return
2454
}
2555

@@ -29,6 +59,28 @@ func WithResultHandler(runFn RunFn) CobraRunFn {
2959
}
3060
}
3161

62+
func handleError(ctx context.Context, err error) {
63+
reqErr := resourcemanager.RequestError{}
64+
if errors.As(err, &reqErr) && reqErr.IsAuthError {
65+
handleAuthError(ctx)
66+
} else {
67+
OnError(err)
68+
}
69+
}
70+
71+
func handleAuthError(ctx context.Context) {
72+
ui.DefaultUI.Warning("Your authentication token has expired, please log in again.")
73+
configurator.
74+
WithOnFinish(func(ctx context.Context, _ config.Config) {
75+
retryCommand(ctx)
76+
}).
77+
ExecuteUserLogin(ctx, cliConfig)
78+
}
79+
80+
func retryCommand(ctx context.Context) {
81+
handleRootExecErr(rootCmd.ExecuteContext(ctx))
82+
}
83+
3284
type errorMessageRenderer interface {
3385
Render()
3486
}
@@ -66,7 +118,7 @@ func handleErrorMessage(err error) string {
66118

67119
func WithParamsHandler(validators ...Validator) MiddlewareWrapper {
68120
return func(runFn RunFn) RunFn {
69-
return func(cmd *cobra.Command, args []string) (string, error) {
121+
return func(ctx context.Context, cmd *cobra.Command, args []string) (string, error) {
70122
errors := make([]error, 0)
71123

72124
for _, validator := range validators {
@@ -82,7 +134,7 @@ func WithParamsHandler(validators ...Validator) MiddlewareWrapper {
82134
return "", fmt.Errorf(errorText)
83135
}
84136

85-
return runFn(cmd, args)
137+
return runFn(ctx, cmd, args)
86138
}
87139
}
88140
}

cli/cmd/resource_apply_cmd.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,8 @@ func init() {
2121
Short: "Apply resources",
2222
Long: "Apply (create/update) resources to your Tracetest server",
2323
PreRun: setupCommand(),
24-
Run: WithResourceMiddleware(func(_ *cobra.Command, args []string) (string, error) {
24+
Run: WithResourceMiddleware(func(ctx context.Context, _ *cobra.Command, args []string) (string, error) {
2525
resourceType := resourceParams.ResourceName
26-
ctx := context.Background()
2726

2827
resourceClient, err := resources.Get(resourceType)
2928
if err != nil {

cli/cmd/resource_delete_cmd.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,8 @@ func init() {
2121
Short: "Delete resources",
2222
Long: "Delete resources from your Tracetest server",
2323
PreRun: setupCommand(),
24-
Run: WithResourceMiddleware(func(_ *cobra.Command, args []string) (string, error) {
24+
Run: WithResourceMiddleware(func(ctx context.Context, _ *cobra.Command, args []string) (string, error) {
2525
resourceType := resourceParams.ResourceName
26-
ctx := context.Background()
2726

2827
resourceClient, err := resources.Get(resourceType)
2928
if err != nil {

cli/cmd/resource_export_cmd.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,8 @@ func init() {
2121
Long: "Export a resource from your Tracetest server",
2222
Short: "Export resource",
2323
PreRun: setupCommand(),
24-
Run: WithResourceMiddleware(func(_ *cobra.Command, args []string) (string, error) {
24+
Run: WithResourceMiddleware(func(ctx context.Context, _ *cobra.Command, args []string) (string, error) {
2525
resourceType := resourceParams.ResourceName
26-
ctx := context.Background()
2726

2827
resourceClient, err := resources.Get(resourceType)
2928
if err != nil {

cli/cmd/resource_get_cmd.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,8 @@ func init() {
2020
Short: "Get resource",
2121
Long: "Get a resource from your Tracetest server",
2222
PreRun: setupCommand(),
23-
Run: WithResourceMiddleware(func(_ *cobra.Command, args []string) (string, error) {
23+
Run: WithResourceMiddleware(func(ctx context.Context, _ *cobra.Command, args []string) (string, error) {
2424
resourceType := resourceParams.ResourceName
25-
ctx := context.Background()
2625

2726
resourceClient, err := resources.Get(resourceType)
2827
if err != nil {

cli/cmd/resource_list_cmd.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,8 @@ func init() {
1919
Short: "List resources",
2020
Long: "List resources from your Tracetest server",
2121
PreRun: setupCommand(),
22-
Run: WithResourceMiddleware(func(_ *cobra.Command, args []string) (string, error) {
22+
Run: WithResourceMiddleware(func(ctx context.Context, _ *cobra.Command, args []string) (string, error) {
2323
resourceType := resourceParams.ResourceName
24-
ctx := context.Background()
2524

2625
resourceClient, err := resources.Get(resourceType)
2726
if err != nil {

cli/cmd/resource_run_cmd.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ func init() {
2424
Short: "run resources",
2525
Long: "run resources",
2626
PreRun: setupCommand(WithOptionalResourceName()),
27-
Run: WithResourceMiddleware(func(_ *cobra.Command, args []string) (string, error) {
28-
ctx := context.Background()
27+
Run: WithResourceMiddleware(func(ctx context.Context, _ *cobra.Command, args []string) (string, error) {
2928
resourceType, err := getResourceType(runParams, resourceParams)
3029
if err != nil {
3130
return "", err

cli/cmd/root.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,16 @@ var rootCmd = &cobra.Command{
3131
}
3232

3333
func Execute() {
34-
if err := rootCmd.Execute(); err != nil {
35-
fmt.Fprintln(os.Stderr, err)
36-
ExitCLI(1)
34+
handleRootExecErr(rootCmd.Execute())
35+
}
36+
37+
func handleRootExecErr(err error) {
38+
if err == nil {
39+
ExitCLI(0)
3740
}
41+
42+
fmt.Fprintln(os.Stderr, err)
43+
ExitCLI(1)
3844
}
3945

4046
func ExitCLI(errorCode int) {

0 commit comments

Comments
 (0)