Fix KV list command with whitespaces (#8017)

* Fix KV list command with whitespaces

* Fix kv list whitespace

* Fix list whitespace

* Fix failing test

Co-authored-by: swayne275 <swayne@hashicorp.com>
This commit is contained in:
Michel Vocks
2020-11-23 20:38:14 +01:00
committed by GitHub
parent c101a91b9c
commit 60f44132a1
4 changed files with 16 additions and 4 deletions

View File

@@ -36,7 +36,7 @@ func extractListData(secret *api.Secret) ([]interface{}, bool) {
// sanitizePath removes any leading or trailing things from a "path". // sanitizePath removes any leading or trailing things from a "path".
func sanitizePath(s string) string { func sanitizePath(s string) string {
return ensureNoTrailingSlash(ensureNoLeadingSlash(strings.TrimSpace(s))) return ensureNoTrailingSlash(ensureNoLeadingSlash(s))
} }
// ensureTrailingSlash ensures the given string has a trailing slash. // ensureTrailingSlash ensures the given string has a trailing slash.

View File

@@ -73,7 +73,14 @@ func (c *KVListCommand) Run(args []string) int {
return 2 return 2
} }
path := ensureTrailingSlash(sanitizePath(args[0])) // Append trailing slash
path := args[0]
if !strings.HasSuffix(path , "/") {
path += "/"
}
// Sanitize path
path = sanitizePath(path)
mountPath, v2, err := isKVv2(path, client) mountPath, v2, err := isKVv2(path, client)
if err != nil { if err != nil {
c.UI.Error(err.Error()) c.UI.Error(err.Error())

View File

@@ -75,8 +75,13 @@ func (c *ListCommand) Run(args []string) int {
return 2 return 2
} }
path := ensureTrailingSlash(sanitizePath(args[0])) // Append trailing slash
path := args[0]
if !strings.HasSuffix(path , "/") {
path += "/"
}
path = sanitizePath(path)
secret, err := client.Logical().List(path) secret, err := client.Logical().List(path)
if err != nil { if err != nil {
c.UI.Error(fmt.Sprintf("Error listing %s: %s", path, err)) c.UI.Error(fmt.Sprintf("Error listing %s: %s", path, err))

View File

@@ -116,7 +116,7 @@ func TestListCommand_Run(t *testing.T) {
t.Errorf("expected %d to be %d", code, exp) t.Errorf("expected %d to be %d", code, exp)
} }
expected := "Error listing secret/list/: " expected := "Error listing secret/list: "
combined := ui.OutputWriter.String() + ui.ErrorWriter.String() combined := ui.OutputWriter.String() + ui.ErrorWriter.String()
if !strings.Contains(combined, expected) { if !strings.Contains(combined, expected) {
t.Errorf("expected %q to contain %q", combined, expected) t.Errorf("expected %q to contain %q", combined, expected)