Merge pull request #16961 from influxdata/cli_secret_ui

fix(cmd/influx): ui change for secret
pull/17039/head
kelwang 2020-02-27 15:08:52 -07:00 committed by GitHub
commit 6387ecc9f4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 65 additions and 38 deletions

View File

@ -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]

View File

@ -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)

View File

@ -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 {

View File

@ -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")
}
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))
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) {
newStr := ""
if showNew {
newStr = " new"
}
var err error
enterPassword:
query := string(promptWithColor("Please type your"+newStr+" password", colorCyan))
for {
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 {