Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cmd/influx): ui change for secret #16961

Merged
merged 1 commit into from
Feb 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down
13 changes: 10 additions & 3 deletions cmd/influx/secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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")
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo: might expose

cmd.MarkFlagRequired("key")
b.org.register(cmd, false)

Expand Down Expand Up @@ -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)
Expand Down
12 changes: 12 additions & 0 deletions cmd/influx/secret_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
65 changes: 36 additions & 29 deletions cmd/influx/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -234,68 +233,76 @@ 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
},
})
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
},
Expand All @@ -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 {
Expand Down