Skip to content

Commit

Permalink
feat(cmd/influx): allow for setting v1 passwords via CLI args (#20123)
Browse files Browse the repository at this point in the history
  • Loading branch information
danxmoran authored Nov 20, 2020
1 parent f8f789c commit cfca0cc
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

### Features

1. [20123](https://github.com/influxdata/influxdb/pull/20123): Allow password to be specified as a CLI option in `influx v1 auth create`.
1. [20123](https://github.com/influxdata/influxdb/pull/20123): Allow password to be specified as a CLI option in `influx v1 auth set-password`.

### Bug Fixes

## v2.0.2 [2020-11-19]
Expand Down
28 changes: 19 additions & 9 deletions cmd/influx/v1_authorization.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func cmdV1Auth(f *globalFlags, opt genericCLIOpts) *cobra.Command {
var v1AuthCreateFlags struct {
username string
description string
password string
noPassword bool
hideHeaders bool
json bool
Expand All @@ -68,6 +69,7 @@ func v1AuthCreateCmd(f *globalFlags, opt genericCLIOpts) *cobra.Command {
cmd.Flags().StringVar(&v1AuthCreateFlags.username, "username", "", "The username to identify this token")
_ = cmd.MarkFlagRequired("username")
cmd.Flags().StringVarP(&v1AuthCreateFlags.description, "description", "d", "", "Token description")
cmd.Flags().StringVar(&v1AuthCreateFlags.password, "password", "", "The password to set on this token")
cmd.Flags().BoolVar(&v1AuthCreateFlags.noPassword, "no-password", false, "Don't prompt for a password. You must use v1 auth set-password command before using the token.")
registerPrintOptions(opt.viper, cmd, &v1AuthCreateFlags.hideHeaders, &v1AuthCreateFlags.json)

Expand All @@ -83,6 +85,10 @@ func makeV1AuthorizationCreateE(opt genericCLIOpts) func(*cobra.Command, []strin
return err
}

if v1AuthCreateFlags.password != "" && v1AuthCreateFlags.noPassword {
return errors.New("only one of --password and --no-password may be specified")
}

userSvc, err := newUserService()
if err != nil {
return err
Expand Down Expand Up @@ -115,8 +121,8 @@ func makeV1AuthorizationCreateE(opt genericCLIOpts) func(*cobra.Command, []strin
return fmt.Errorf("authorization with username %q exists", v1AuthCreateFlags.username)
}

var password string
if !v1AuthCreateFlags.noPassword {
password := v1AuthCreateFlags.password
if password == "" && !v1AuthCreateFlags.noPassword {
ui := &input.UI{
Writer: opt.w,
Reader: opt.in,
Expand Down Expand Up @@ -161,7 +167,7 @@ func makeV1AuthorizationCreateE(opt genericCLIOpts) func(*cobra.Command, []strin
return err
}

if !v1AuthCreateFlags.noPassword {
if password != "" {
if err := s.SetPassword(context.Background(), auth.ID, password); err != nil {
_ = s.DeleteAuthorization(context.Background(), auth.ID)
return fmt.Errorf("error setting password: %w", err)
Expand Down Expand Up @@ -591,7 +597,8 @@ func (f *v1AuthLookupFlags) makeFilter() influxdb.AuthorizationFilter {
}

var v1AuthSetPasswordFlags struct {
lookup v1AuthLookupFlags
lookup v1AuthLookupFlags
password string
}

func v1AuthSetPasswordCmd(f *globalFlags, opt genericCLIOpts) *cobra.Command {
Expand All @@ -603,6 +610,7 @@ func v1AuthSetPasswordCmd(f *globalFlags, opt genericCLIOpts) *cobra.Command {

f.registerFlags(opt.viper, cmd)
v1AuthSetPasswordFlags.lookup.register(opt.viper, cmd, false)
cmd.Flags().StringVar(&v1AuthSetPasswordFlags.password, "password", "", "Password to set on the authorization")

return cmd
}
Expand All @@ -623,13 +631,15 @@ func makeV1AuthorizationSetPasswordF(opt genericCLIOpts) func(*cobra.Command, []
return err
}

ui := &input.UI{
Writer: opt.w,
Reader: opt.in,
password := v1AuthSetPasswordFlags.password
if password == "" {
ui := &input.UI{
Writer: opt.w,
Reader: opt.in,
}
password = cinternal.GetPassword(ui, false)
}

password := cinternal.GetPassword(ui, false)

if err := s.SetPassword(context.Background(), auth.ID, password); err != nil {
return fmt.Errorf("error setting password: %w", err)
}
Expand Down

0 comments on commit cfca0cc

Please sign in to comment.