Change behavior of -token and -password to support env/file only (#2327)

* Revert "Remove `-token` flag (#2303)"

This reverts commit 65cff995d7.

* Change behavior of -token

* Add use of MustParsePath

* Update changelog

* Use errors.Is
This commit is contained in:
Jeff Mitchell authored and GitHub committed 2022-08-05 16:08:48 -04:00
1 parent ddb7ec7b49
commit fe2ee7ef4a
9 files changed
+101 -24

No files matched your search

+22 -6
View File
@@ -1,6 +1,7 @@
package accountscmd
import (
"errors"
"fmt"
"os"
"strings"
@@ -141,8 +142,13 @@ func extraFlagsHandlingFuncImpl(c *Command, _ *base.FlagSets, opts *[]accounts.O
c.flagPassword = strings.TrimSpace(value)
default:
password, err := parseutil.ParsePath(c.flagPassword)
if err != nil && err.Error() != parseutil.ErrNotAUrl.Error() {
password, err := parseutil.MustParsePath(c.flagPassword)
switch {
case err == nil:
case errors.Is(err, parseutil.ErrNotParsed):
c.UI.Error("Password flag must be used with env:// or file:// syntax or left empty for an interactive prompt")
return false
default:
c.UI.Error(fmt.Sprintf("Error parsing password flag: %v", err))
return false
}
@@ -164,8 +170,13 @@ func extraFlagsHandlingFuncImpl(c *Command, _ *base.FlagSets, opts *[]accounts.O
c.flagCurrentPassword = strings.TrimSpace(value)
default:
password, err := parseutil.ParsePath(c.flagCurrentPassword)
if err != nil && err.Error() != parseutil.ErrNotAUrl.Error() {
password, err := parseutil.MustParsePath(c.flagCurrentPassword)
switch {
case err == nil:
case errors.Is(err, parseutil.ErrNotParsed):
c.UI.Error("Current password flag must be used with env:// or file:// syntax or left empty for an interactive prompt")
return false
default:
c.UI.Error(fmt.Sprintf("Error parsing current password flag: %v", err))
return false
}
@@ -196,8 +207,13 @@ func extraFlagsHandlingFuncImpl(c *Command, _ *base.FlagSets, opts *[]accounts.O
c.flagNewPassword = strings.TrimSpace(value)
default:
password, err := parseutil.ParsePath(c.flagNewPassword)
if err != nil && err.Error() != parseutil.ErrNotAUrl.Error() {
password, err := parseutil.MustParsePath(c.flagNewPassword)
switch {
case err == nil:
case errors.Is(err, parseutil.ErrNotParsed):
c.UI.Error("New password flag must be used with env:// or file:// syntax or left empty for an interactive prompt")
return false
default:
c.UI.Error(fmt.Sprintf("Error parsing new password flag: %v", err))
return false
}
@@ -1,6 +1,7 @@
package accountscmd
import (
"errors"
"fmt"
"os"
"strings"
@@ -73,7 +74,7 @@ func extraPasswordFlagsFuncImpl(c *PasswordCommand, set *base.FlagSets, f *base.
f.StringVar(&base.StringVar{
Name: "password",
Target: &c.flagPassword,
Usage: "The password for the account. If blank, the command will prompt for the password to be entered interactively in a non-echoing way. Otherwise, this can refer to a file on disk (file://) from which a password will be read; an env var (env://) from which the password will be read; or a string containing the password.",
Usage: "The password for the account. If blank, the command will prompt for the password to be entered interactively in a non-echoing way. Otherwise, this can refer to a file on disk (file://) from which a password will be read or an env var (env://) from which the password will be read.",
})
}
}
@@ -117,8 +118,13 @@ func extraPasswordFlagsHandlingFuncImpl(c *PasswordCommand, _ *base.FlagSets, op
*opts = append(*opts, accounts.WithPasswordAccountPassword(strings.TrimSpace(value)))
default:
password, err := parseutil.ParsePath(c.flagPassword)
if err != nil && err.Error() != parseutil.ErrNotAUrl.Error() {
password, err := parseutil.MustParsePath(c.flagPassword)
switch {
case err == nil:
case errors.Is(err, parseutil.ErrNotParsed):
c.UI.Error("Password flag must be used with env:// or file:// syntax or left empty for an interactive prompt")
return false
default:
c.UI.Error(fmt.Sprintf("Error parsing password flag: %v", err))
return false
}
@@ -64,7 +64,7 @@ func (c *PasswordCommand) Flags() *base.FlagSets {
Name: "password",
Target: &c.flagPassword,
EnvVar: envPassword,
Usage: "The password associated with the login name. If blank, the command will prompt for the password to be entered interactively in a non-echoing way. Otherwise, this can refer to a file on disk (file://) from which a password will be read; an env var (env://) from which the password will be read; or a string containing the password.",
Usage: "The password associated with the login name. If blank, the command will prompt for the password to be entered interactively in a non-echoing way. Otherwise, this can refer to a file on disk (file://) from which a password will be read or an env var (env://) from which the password will be read.",
})
f.StringVar(&base.StringVar{
@@ -114,8 +114,13 @@ func (c *PasswordCommand) Run(args []string) int {
c.flagPassword = strings.TrimSpace(value)
default:
password, err := parseutil.ParsePath(c.flagPassword)
if err != nil && err.Error() != parseutil.ErrNotAUrl.Error() {
password, err := parseutil.MustParsePath(c.flagPassword)
switch {
case err == nil:
case errors.Is(err, parseutil.ErrNotParsed):
c.UI.Error("Password flag must be used with env:// or file:// syntax or left empty for an interactive prompt")
return base.CommandUserError
default:
c.UI.Error(fmt.Sprintf("Error parsing password flag: %v", err))
return base.CommandUserError
}
@@ -1,6 +1,9 @@
package credentialscmd
import (
"errors"
"fmt"
"github.com/hashicorp/boundary/api/credentials"
"github.com/hashicorp/boundary/internal/cmd/base"
"github.com/hashicorp/go-secure-stdlib/parseutil"
@@ -58,9 +61,14 @@ func extraSshPrivateKeyFlagHandlingFuncImpl(c *SshPrivateKeyCommand, _ *base.Fla
switch c.flagPrivateKey {
case "":
default:
privateKey, err := parseutil.ParsePath(c.flagPrivateKey)
if err != nil && err.Error() != parseutil.ErrNotAUrl.Error() {
c.UI.Error("Error parsing private key flag: " + err.Error())
privateKey, err := parseutil.MustParsePath(c.flagPrivateKey)
switch {
case err == nil:
case errors.Is(err, parseutil.ErrNotParsed):
c.UI.Error("Private key flag must be used with env:// or file:// syntax")
return false
default:
c.UI.Error(fmt.Sprintf("Error parsing private key flag: %v", err))
return false
}
*opts = append(*opts, credentials.WithSshPrivateKeyCredentialPrivateKey(privateKey))
@@ -1,6 +1,9 @@
package credentialscmd
import (
"errors"
"fmt"
"github.com/hashicorp/boundary/api/credentials"
"github.com/hashicorp/boundary/internal/cmd/base"
"github.com/hashicorp/go-secure-stdlib/parseutil"
@@ -43,7 +46,7 @@ func extraUsernamePasswordFlagsFuncImpl(c *UsernamePasswordCommand, set *base.Fl
f.StringVar(&base.StringVar{
Name: passwordFlagName,
Target: &c.flagPassword,
Usage: "The password associated with the credential. This can be the value itself, refer to a file on disk (file://) from which the value will be read, or an env var (env://) from which the value will be read.",
Usage: "The password associated with the credential. This can be a file on disk (file://) from which the value will be read, or an env var (env://) from which the value will be read.",
})
}
}
@@ -58,9 +61,14 @@ func extraUsernamePasswordFlagHandlingFuncImpl(c *UsernamePasswordCommand, _ *ba
switch c.flagPassword {
case "":
default:
password, err := parseutil.ParsePath(c.flagPassword)
if err != nil && err.Error() != parseutil.ErrNotAUrl.Error() {
c.UI.Error("Error parsing password flag: " + err.Error())
password, err := parseutil.MustParsePath(c.flagPassword)
switch {
case err == nil:
case errors.Is(err, parseutil.ErrNotParsed):
c.UI.Error("Password flag must be used with env:// or file:// syntax")
return false
default:
c.UI.Error(fmt.Sprintf("Error parsing password flag: %v", err))
return false
}
*opts = append(*opts, credentials.WithUsernamePasswordCredentialPassword(password))