Skip to content

Commit

Permalink
fix(cmd/influx): ui change for secret
Browse files Browse the repository at this point in the history
  • Loading branch information
kelwang committed Feb 21, 2020
1 parent b8130bf commit 0a15ff9
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 33 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,26 @@
### Features

### Bug Fixes

1. [16919](https://github.com/influxdata/influxdb/pull/16919): Sort dashboards on homepage alphabetically
1. [16934](https://github.com/influxdata/influxdb/pull/16934): Tokens page now sorts by status
1. [16931](https://github.com/influxdata/influxdb/pull/16931): Set the defualt value of tags in a Check
1. [16935](https://github.com/influxdata/influxdb/pull/16935): Fix sort by variable 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]

### Features

1. [16855](https://github.com/influxdata/influxdb/pull/16855): Added labels to buckets in UI
1. [16842](https://github.com/influxdata/influxdb/pull/16842): Connect monaco editor to Flux LSP server
1. [16856](https://github.com/influxdata/influxdb/pull/16856): Update Flux to v0.59.6

### Bug Fixes

1. [16852](https://github.com/influxdata/influxdb/pull/16852): Revert for bad indexing of UserResourceMappings and Authorizations
1. [15911](https://github.com/influxdata/influxdb/pull/15911): Gauge no longer allowed to become too small
1. [16878](https://github.com/influxdata/influxdb/pull/16878): Fix issue with INFLUX_TOKEN env vars being overridden by default token
1. [16878](https://github.com/influxdata/influxdb/pull/16878): Fix issue with INFLUX_TOKEN env vars being overridden by default token

## v2.0.0-beta.3 [2020-02-11]

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")
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))
goto enterPassword
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

0 comments on commit 0a15ff9

Please sign in to comment.