From 48490cdc9b5cb534c98daf553fde0e1412b3dcc2 Mon Sep 17 00:00:00 2001 From: Kelvin Wang Date: Fri, 21 Feb 2020 10:06:07 -0500 Subject: [PATCH] fix(cmd/influx): ui change for secret --- CHANGELOG.md | 1 + cmd/influx/secret.go | 13 ++++++-- cmd/influx/secret_test.go | 12 ++++++++ cmd/influx/setup.go | 65 ++++++++++++++++++++++----------------- 4 files changed, 59 insertions(+), 32 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b58517ca5d4..c01743947b8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ 1. [16992](https://github.com/influxdata/influxdb/pull/16992): Query Builder now groups on column values, not tag values 1. [17013](https://github.com/influxdata/influxdb/pull/17013): Scatterplots can once again render the tooltip correctly 1. [17027](https://github.com/influxdata/influxdb/pull/17027): Drop pkger gauge chart requirement for color threshold type +1. [16961](https://github.com/influxdata/influxdb/pull/16961): Remove cli confirmation of secret, add an optional parameter of secret value ## v2.0.0-beta.4 [2020-02-14] diff --git a/cmd/influx/secret.go b/cmd/influx/secret.go index cacf479e316..4b8508f75fa 100644 --- a/cmd/influx/secret.go +++ b/cmd/influx/secret.go @@ -24,8 +24,9 @@ type cmdSecretBuilder struct { svcFn secretSVCsFn - key string - org organization + key string + value string + org organization } func newCmdSecretBuilder(svcsFn secretSVCsFn, opt genericCLIOpts) *cmdSecretBuilder { @@ -51,6 +52,7 @@ func (b *cmdSecretBuilder) cmdUpdate() *cobra.Command { cmd := b.newCmd("update", b.cmdUpdateRunEFn) cmd.Short = "Update secret" cmd.Flags().StringVarP(&b.key, "key", "k", "", "The secret key (required)") + cmd.Flags().StringVarP(&b.value, "value", "v", "", "Optional secret value for scripting convenience, using this might exposed the secret to your local history") cmd.MarkFlagRequired("key") b.org.register(cmd, false) @@ -84,7 +86,12 @@ func (b *cmdSecretBuilder) cmdUpdateRunEFn(cmd *cobra.Command, args []string) er Writer: b.genericCLIOpts.w, Reader: b.genericCLIOpts.in, } - secret := getSecretFn(ui) + var secret string + if b.value != "" { + secret = b.value + } else { + secret = getSecretFn(ui) + } if err := scrSVC.PatchSecrets(ctx, orgID, map[string]string{b.key: secret}); err != nil { return fmt.Errorf("failed to update secret with key %q: %v", b.key, err) diff --git a/cmd/influx/secret_test.go b/cmd/influx/secret_test.go index 8fbfd07a02e..425f5b855b6 100644 --- a/cmd/influx/secret_test.go +++ b/cmd/influx/secret_test.go @@ -159,11 +159,23 @@ func TestCmdSecret(t *testing.T) { "--org=org name", "--key=key1", }, }, + { + name: "with key and value", + expectedKey: "key1", + flags: []string{ + "--org=org name", "--key=key1", "--value=v1", + }, + }, { name: "shorts", expectedKey: "key1", flags: []string{"-o=" + orgID.String(), "-k=key1"}, }, + { + name: "shorts with value", + expectedKey: "key1", + flags: []string{"-o=" + orgID.String(), "-k=key1", "-v=v1"}, + }, } cmdFn := func(expectedKey string) func(*globalFlags, genericCLIOpts) *cobra.Command { diff --git a/cmd/influx/setup.go b/cmd/influx/setup.go index 4cd2d6e04ab..2bada329d62 100644 --- a/cmd/influx/setup.go +++ b/cmd/influx/setup.go @@ -7,7 +7,6 @@ import ( "strconv" "strings" - "github.com/influxdata/influxdb" platform "github.com/influxdata/influxdb" "github.com/influxdata/influxdb/cmd/influx/internal" "github.com/influxdata/influxdb/http" @@ -234,42 +233,50 @@ You have entered: } } -func errSecretIsNotMatch(title string) error { - return fmt.Errorf(title + "s do not match") -} +var errPasswordNotMatch = fmt.Errorf("passwords do not match") -func errSecretIsTooShort(title string) error { - return &influxdb.Error{ - Code: influxdb.EUnprocessableEntity, - Msg: title + " is too short", - } -} +var errPasswordIsTooShort error = fmt.Errorf("password is too short") func getSecret(ui *input.UI) (secret string) { - return getSecretInput(ui, false, "secret") + var err error + query := string(promptWithColor("Please type your secret", colorCyan)) + for { + secret, err = ui.Ask(query, &input.Options{ + Required: true, + HideOrder: true, + Hide: true, + Mask: false, + }) + switch err { + case input.ErrInterrupted: + os.Exit(1) + default: + if secret = strings.TrimSpace(secret); secret == "" { + continue + } + } + break + } + return secret } func getPassword(ui *input.UI, showNew bool) (password string) { - return getSecretInput(ui, showNew, "password") -} - -func getSecretInput(ui *input.UI, showNew bool, title string) (secret string) { newStr := "" if showNew { newStr = " new" } var err error -enterSecret: - query := string(promptWithColor("Please type your"+newStr+" "+title, colorCyan)) +enterPassword: + query := string(promptWithColor("Please type your"+newStr+" password", colorCyan)) for { - secret, err = ui.Ask(query, &input.Options{ + password, err = ui.Ask(query, &input.Options{ Required: true, HideOrder: true, Hide: true, Mask: false, ValidateFunc: func(s string) error { if len(s) < 8 { - return errSecretIsTooShort(title) + return errPasswordIsTooShort } return nil }, @@ -277,25 +284,25 @@ enterSecret: switch err { case input.ErrInterrupted: os.Exit(1) + case errPasswordIsTooShort: + ui.Writer.Write(promptWithColor("Password too short - minimum length is 8 characters!\n\r", colorRed)) + continue default: - if influxdb.ErrorCode(err) == influxdb.EUnprocessableEntity { - ui.Writer.Write(promptWithColor(strings.ToTitle(title)+" too short - minimum length is 8 characters!\n\r", colorRed)) - goto enterSecret - } else if secret = strings.TrimSpace(secret); secret == "" { + if password = strings.TrimSpace(password); password == "" { continue } } break } - query = string(promptWithColor("Please type your"+newStr+" "+title+" again", colorCyan)) + query = string(promptWithColor("Please type your"+newStr+" password again", colorCyan)) for { _, err = ui.Ask(query, &input.Options{ Required: true, HideOrder: true, Hide: true, ValidateFunc: func(s string) error { - if s != secret { - return errSecretIsNotMatch(title) + if s != password { + return errPasswordNotMatch } return nil }, @@ -306,12 +313,12 @@ enterSecret: case nil: // Nothing. default: - ui.Writer.Write(promptWithColor(strings.ToTitle(title)+"s do not match!\n", colorRed)) - goto enterSecret + ui.Writer.Write(promptWithColor("Passwords do not match!\n", colorRed)) + goto enterPassword } break } - return secret + return password } func getInput(ui *input.UI, prompt, defaultValue string) string {