From a68106ee885d16269ec50fa9d304126fc7ae19c3 Mon Sep 17 00:00:00 2001 From: Andrew Lee <32912555+candrewlee14@users.noreply.github.com> Date: Fri, 17 Jun 2022 14:59:11 -0600 Subject: [PATCH] Replace token flags with = to prevent bad parsing of leading dash in token (#399) --- cmd/influx/main.go | 29 ++++++- cmd/influx/main_test.go | 165 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 193 insertions(+), 1 deletion(-) diff --git a/cmd/influx/main.go b/cmd/influx/main.go index 5743110..dd97834 100644 --- a/cmd/influx/main.go +++ b/cmd/influx/main.go @@ -3,8 +3,10 @@ package main import ( "fmt" "os" + "strings" "time" + "github.com/fatih/color" "github.com/influxdata/influx-cli/v2/pkg/cli/middleware" "github.com/urfave/cli" ) @@ -91,9 +93,34 @@ func newApp() cli.App { } } +// This creates a new slice and replaces `-t "-FOO-TOKEN"` with `-t=-FOO-TOKEN` +// This is necessary to do because the command line arg: +// `-t "-FOO-TOKEN"`` will be parsed as two separate flags instead of a flag and token value. +func ReplaceTokenArg(args []string) []string { + if len(args) == 0 { + return []string{} + } + newArgs := make([]string, len(args)) + copy(newArgs, args) + for i, arg := range newArgs[:len(newArgs)-1] { + switch arg { + case "--token", "-t": + if strings.HasPrefix(newArgs[i+1], "-") { + color.HiYellow("warning: %[1]s %[2]s interpreted as %[1]s=%[2]s, consider using %[1]s=%[2]s syntax when tokens start with a hyphen", + newArgs[i], newArgs[i+1], + ) + } + newArgs[i] = strings.Join(newArgs[i:i+2], "=") + newArgs = append(newArgs[:i+1], newArgs[i+2:]...) + } + } + return newArgs +} + func main() { app := newApp() - if err := app.Run(os.Args); err != nil { + args := ReplaceTokenArg(os.Args) + if err := app.Run(args); err != nil { // Errors will normally be handled by cli.HandleExitCoder via ExitErrHandler set on app. Any error not implementing // the cli.ExitCoder interface can be handled here. _, _ = fmt.Fprintf(os.Stderr, "Error: %v\n", err) diff --git a/cmd/influx/main_test.go b/cmd/influx/main_test.go index 705b093..f049c8c 100644 --- a/cmd/influx/main_test.go +++ b/cmd/influx/main_test.go @@ -148,6 +148,171 @@ func TestApp_HostSpecificErrors(t *testing.T) { } } +func TestReplaceTokenArg(t *testing.T) { + tests := []struct { + name string + argsBefore []string + argsAfter []string + }{ + { + name: "short token flag second to last arg", + argsBefore: []string{ + "config", + "create", + "-n", + "targetflux", + "-u", + "http://localhost:8087", + "-t", + "-therestofmytoken", + "-o", + "organisation", + }, + argsAfter: []string{ + "config", + "create", + "-n", + "targetflux", + "-u", + "http://localhost:8087", + "-t=-therestofmytoken", + "-o", + "organisation", + }, + }, + { + name: "long token flag second to last arg", + argsBefore: []string{ + "config", + "create", + "-n", + "targetflux", + "-u", + "http://localhost:8087", + "--token", + "-foo_token", + "-o", + "organisation", + }, + argsAfter: []string{ + "config", + "create", + "-n", + "targetflux", + "-u", + "http://localhost:8087", + "--token=-foo_token", + "-o", + "organisation", + }, + }, + { + name: "short token flag last arg", + argsBefore: []string{ + "config", + "create", + "-n", + "targetflux", + "-u", + "http://localhost:8087", + "-o", + "organisation", + "-t", + "-therestofmytoken", + }, + argsAfter: []string{ + "config", + "create", + "-n", + "targetflux", + "-u", + "http://localhost:8087", + "-o", + "organisation", + "-t=-therestofmytoken", + }, + }, + { + name: "long token flag last arg", + argsBefore: []string{ + "config", + "create", + "-n", + "targetflux", + "-u", + "http://localhost:8087", + "-o", + "organisation", + "--token", + "-foo_token", + }, + argsAfter: []string{ + "config", + "create", + "-n", + "targetflux", + "-u", + "http://localhost:8087", + "-o", + "organisation", + "--token=-foo_token", + }, + }, + { + name: "no token flag", + argsBefore: []string{ + "v1", + "shell", + }, + argsAfter: []string{ + "v1", + "shell", + }, + }, + { + name: "unprovided token value for flag", + argsBefore: []string{ + "v1", + "shell", + "--token", + }, + argsAfter: []string{ + "v1", + "shell", + "--token", + }, + }, + { + name: "no token flag, other flags", + argsBefore: []string{ + "v1", + "shell", + "-u", + "http://localhost:8087", + "-o", + "organisation", + }, + argsAfter: []string{ + "v1", + "shell", + "-u", + "http://localhost:8087", + "-o", + "organisation", + }, + }, + { + name: "no args", + argsBefore: []string{}, + argsAfter: []string{}, + }, + } + for _, tt := range tests { + newArgs := ReplaceTokenArg(tt.argsBefore) + require.Equal(t, tt.argsAfter, newArgs) + } +} + type testWriter struct { b *bytes.Buffer written bool