Skip to content

Commit

Permalink
Merge pull request #16537 from influxdata/cli_update_password
Browse files Browse the repository at this point in the history
feat(cmd/influx): update password in cli
  • Loading branch information
kelwang authored Jan 15, 2020
2 parents 6c7a61e + cf5daee commit 540e785
Show file tree
Hide file tree
Showing 7 changed files with 621 additions and 140 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
1. [16509](https://github.com/influxdata/influxdb/pull/16509): Add support for applying an influx package via a public facing URL
1. [16511](https://github.com/influxdata/influxdb/pull/16511): Add jsonnet support for influx packages
1. [14782](https://github.com/influxdata/influxdb/pull/16336): Add view page for Check
1. [16537](https://github.com/influxdata/influxdb/pull/16537): Add update password for CLI

### Bug Fixes

Expand All @@ -68,6 +69,7 @@
1. [16491](https://github.com/influxdata/influxdb/pull/16491): Add missing env vals to influx cli usage and fixes precedence of flag/env var priority

### UI Improvements

1. [16444](https://github.com/influxdata/influxdb/pull/16444): Add honeybadger reporting to create checks

## v2.0.0-alpha.21 [2019-12-13]
Expand Down
9 changes: 5 additions & 4 deletions cmd/influx/bucket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"
"reflect"
"testing"
"io/ioutil"
"time"

"github.com/influxdata/influxdb"
Expand Down Expand Up @@ -102,7 +103,7 @@ func TestCmdBucket(t *testing.T) {
return nil
}

builder := newCmdBucketBuilder(fakeSVCFn(svc), out(new(bytes.Buffer)))
builder := newCmdBucketBuilder(fakeSVCFn(svc), out(ioutil.Discard))
cmd := builder.cmdCreate()
cmd.RunE = builder.cmdCreateRunEFn
return cmd
Expand Down Expand Up @@ -151,7 +152,7 @@ func TestCmdBucket(t *testing.T) {
return nil
}

builder := newCmdBucketBuilder(fakeSVCFn(svc), out(new(bytes.Buffer)))
builder := newCmdBucketBuilder(fakeSVCFn(svc), out(ioutil.Discard))
cmd := builder.cmdDelete()
cmd.RunE = builder.cmdDeleteRunEFn
return cmd
Expand Down Expand Up @@ -258,7 +259,7 @@ func TestCmdBucket(t *testing.T) {
return nil, 0, nil
}

builder := newCmdBucketBuilder(fakeSVCFn(svc), in(new(bytes.Buffer)), out(new(bytes.Buffer)))
builder := newCmdBucketBuilder(fakeSVCFn(svc), in(new(bytes.Buffer)), out(ioutil.Discard))
cmd := builder.cmdFind()
cmd.RunE = builder.cmdFindRunEFn
return cmd, calls
Expand Down Expand Up @@ -352,7 +353,7 @@ func TestCmdBucket(t *testing.T) {
return &influxdb.Bucket{}, nil
}

builder := newCmdBucketBuilder(fakeSVCFn(svc), out(new(bytes.Buffer)))
builder := newCmdBucketBuilder(fakeSVCFn(svc), out(ioutil.Discard))
cmd := builder.cmdUpdate()
cmd.RunE = builder.cmdUpdateRunEFn
return cmd
Expand Down
2 changes: 1 addition & 1 deletion cmd/influx/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func influxCmd(opts ...genericCLIOptFn) *cobra.Command {
cmdREPL(),
cmdSetup(),
cmdTask(),
cmdUser(),
cmdUser(runEWrapper),
cmdWrite(),
)

Expand Down
15 changes: 8 additions & 7 deletions cmd/influx/organization_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"fmt"
"io/ioutil"
"reflect"
"testing"

Expand Down Expand Up @@ -63,7 +64,7 @@ func TestCmdOrg(t *testing.T) {
return nil
}

builder := newCmdOrgBuilder(fakeOrgSVCFn(svc), out(new(bytes.Buffer)))
builder := newCmdOrgBuilder(fakeOrgSVCFn(svc), out(ioutil.Discard))
cmd := builder.cmdCreate()
return cmd
}
Expand Down Expand Up @@ -109,7 +110,7 @@ func TestCmdOrg(t *testing.T) {
return nil
}

builder := newCmdOrgBuilder(fakeOrgSVCFn(svc), out(new(bytes.Buffer)))
builder := newCmdOrgBuilder(fakeOrgSVCFn(svc), out(ioutil.Discard))
cmd := builder.cmdDelete()
return cmd
}
Expand Down Expand Up @@ -181,7 +182,7 @@ func TestCmdOrg(t *testing.T) {
return nil, 0, nil
}

builder := newCmdOrgBuilder(fakeOrgSVCFn(svc), in(new(bytes.Buffer)), out(new(bytes.Buffer)))
builder := newCmdOrgBuilder(fakeOrgSVCFn(svc), in(new(bytes.Buffer)), out(ioutil.Discard))
cmd := builder.cmdFind()
return cmd, calls
}
Expand Down Expand Up @@ -268,7 +269,7 @@ func TestCmdOrg(t *testing.T) {
return &influxdb.Organization{}, nil
}

builder := newCmdOrgBuilder(fakeOrgSVCFn(svc), out(new(bytes.Buffer)))
builder := newCmdOrgBuilder(fakeOrgSVCFn(svc), out(ioutil.Discard))
cmd := builder.cmdUpdate()
return cmd
}
Expand Down Expand Up @@ -366,7 +367,7 @@ func TestCmdOrg(t *testing.T) {
return &influxdb.Organization{ID: 1}, nil
}

builder := newCmdOrgBuilder(fakeOrgSVCFn(svc), in(new(bytes.Buffer)), out(new(bytes.Buffer)))
builder := newCmdOrgBuilder(fakeOrgSVCFn(svc), in(new(bytes.Buffer)), out(ioutil.Discard))
cmd := builder.cmdMemberList()
return cmd, calls
}
Expand Down Expand Up @@ -394,7 +395,7 @@ func TestCmdOrg(t *testing.T) {
return nil
}

builder := newCmdOrgBuilder(fakeOrgUrmSVCsFn(svc, urmSVC), in(new(bytes.Buffer)), out(new(bytes.Buffer)))
builder := newCmdOrgBuilder(fakeOrgUrmSVCsFn(svc, urmSVC), in(new(bytes.Buffer)), out(ioutil.Discard))
cmd := builder.cmdMemberAdd()
return cmd, calls
}
Expand Down Expand Up @@ -457,7 +458,7 @@ func TestCmdOrg(t *testing.T) {
return nil
}

builder := newCmdOrgBuilder(fakeOrgUrmSVCsFn(svc, urmSVC), in(new(bytes.Buffer)), out(new(bytes.Buffer)))
builder := newCmdOrgBuilder(fakeOrgUrmSVCsFn(svc, urmSVC), in(new(bytes.Buffer)), out(ioutil.Discard))
cmd := builder.cmdMemberRemove()
return cmd, calls
}
Expand Down
32 changes: 19 additions & 13 deletions cmd/influx/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func setupF(cmd *cobra.Command, args []string) error {
return fmt.Errorf("failed to write token to path %q: %v", dPath, err)
}

fmt.Println(promptWithColor("Your token has been stored in "+dPath+".", colorCyan))
fmt.Println(string(promptWithColor("Your token has been stored in "+dPath+".", colorCyan)))

w := internal.NewTabWriter(os.Stdout)
w.WriteHeaders(
Expand Down Expand Up @@ -144,7 +144,7 @@ func interactive() (req *platform.OnboardingRequest, err error) {
Reader: os.Stdin,
}
req = new(platform.OnboardingRequest)
fmt.Println(promptWithColor(`Welcome to InfluxDB 2.0!`, colorYellow))
fmt.Println(string(promptWithColor(`Welcome to InfluxDB 2.0!`, colorYellow)))
if setupFlags.username != "" {
req.User = setupFlags.username
} else {
Expand All @@ -153,7 +153,7 @@ func interactive() (req *platform.OnboardingRequest, err error) {
if setupFlags.password != "" {
req.Password = setupFlags.password
} else {
req.Password = getPassword(ui)
req.Password = getPassword(ui, false)
}
if setupFlags.token != "" {
req.Token = setupFlags.token
Expand Down Expand Up @@ -200,8 +200,9 @@ var (
keyReset = []byte{keyEscape, '[', '0', 'm'}
)

func promptWithColor(s string, color []byte) string {
return string(color) + s + string(keyReset)
func promptWithColor(s string, color []byte) []byte {
bb := append(color, []byte(s)...)
return append(bb, keyReset...)
}

func getConfirm(ui *input.UI, or *platform.OnboardingRequest) bool {
Expand All @@ -211,14 +212,14 @@ func getConfirm(ui *input.UI, or *platform.OnboardingRequest) bool {
if or.RetentionPeriod > 0 {
rp = fmt.Sprintf("%d hrs", or.RetentionPeriod)
}
fmt.Print(promptWithColor(fmt.Sprintf(`
ui.Writer.Write(promptWithColor(fmt.Sprintf(`
You have entered:
Username: %s
Organization: %s
Bucket: %s
Retention Period: %s
`, or.User, or.Org, or.Bucket, rp), colorCyan))
result, err := ui.Ask(prompt, &input.Options{
result, err := ui.Ask(string(prompt), &input.Options{
HideOrder: true,
})
if err != nil {
Expand All @@ -240,15 +241,20 @@ var (
errPasswordIsTooShort = fmt.Errorf("passwords is too short")
)

func getPassword(ui *input.UI) (password string) {
func getPassword(ui *input.UI, showNew bool) (password string) {
newStr := ""
if showNew {
newStr = " new"
}
var err error
enterPasswd:
query := promptWithColor("Please type your password", colorCyan)
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 errPasswordIsTooShort
Expand All @@ -260,7 +266,7 @@ enterPasswd:
case input.ErrInterrupted:
os.Exit(1)
case errPasswordIsTooShort:
fmt.Println(promptWithColor("Password too short - minimum length is 8 characters!", colorRed))
ui.Writer.Write(promptWithColor("Password too short - minimum length is 8 characters!", colorRed))
goto enterPasswd
default:
if password = strings.TrimSpace(password); password == "" {
Expand All @@ -269,7 +275,7 @@ enterPasswd:
}
break
}
query = promptWithColor("Please type your password again", colorCyan)
query = string(promptWithColor("Please type your"+newStr+" password again", colorCyan))
for {
_, err = ui.Ask(query, &input.Options{
Required: true,
Expand All @@ -288,7 +294,7 @@ enterPasswd:
case nil:
// Nothing.
default:
fmt.Println(promptWithColor("Passwords do not match!", colorRed))
ui.Writer.Write(promptWithColor("Passwords do not match!\n", colorRed))
goto enterPasswd
}
break
Expand All @@ -305,7 +311,7 @@ func getInput(ui *input.UI, prompt, defaultValue string) string {
option.Default = defaultValue
option.HideDefault = true
}
prompt = promptWithColor(prompt, colorCyan)
prompt = string(promptWithColor(prompt, colorCyan))
for {
line, err := ui.Ask(prompt, option)
switch err {
Expand Down
Loading

0 comments on commit 540e785

Please sign in to comment.