From 56be8168a4c96eb3903888f0b04a158c76e3ed64 Mon Sep 17 00:00:00 2001 From: hghaf099 <83242695+hghaf099@users.noreply.github.com> Date: Thu, 21 Oct 2021 22:35:13 -0400 Subject: [PATCH] CLI request when namespace is in argument and part of the path (#12720) * CLI makes request to incorrect URL when namespace is both provided as argument and part of the path fixes #12675 * adding change log * removing a switch and addressing a possibility of out of bound index --- changelog/12720.txt | 3 +++ command/kv_helpers.go | 26 +++++++++++++++++++++----- 2 files changed, 24 insertions(+), 5 deletions(-) create mode 100644 changelog/12720.txt diff --git a/changelog/12720.txt b/changelog/12720.txt new file mode 100644 index 0000000000000..2c213120562ed --- /dev/null +++ b/changelog/12720.txt @@ -0,0 +1,3 @@ +```release-note:bug +cli: fixes CLI requests when namespace is both provided as argument and part of the path +``` diff --git a/command/kv_helpers.go b/command/kv_helpers.go index a1b331fc61ed4..b058ba2d54947 100644 --- a/command/kv_helpers.go +++ b/command/kv_helpers.go @@ -106,13 +106,29 @@ func isKVv2(path string, client *api.Client) (string, bool, error) { } func addPrefixToVKVPath(p, mountPath, apiPrefix string) string { - switch { - case p == mountPath, p == strings.TrimSuffix(mountPath, "/"): + + if p == mountPath || p == strings.TrimSuffix(mountPath, "/") { return path.Join(mountPath, apiPrefix) - default: - p = strings.TrimPrefix(p, mountPath) - return path.Join(mountPath, apiPrefix, p) } + + tp := strings.TrimPrefix(p, mountPath) + for { + // If the entire mountPath is included in the path, we are done + if tp != p { + break + } + // Trim the parts of the mountPath that are not included in the + // path, for example, in cases where the mountPath contains + // namespaces which are not included in the path. + partialMountPath := strings.SplitN(mountPath, "/", 2) + if len(partialMountPath) <= 1 || partialMountPath[1] == ""{ + break + } + mountPath = partialMountPath[1] + tp = strings.TrimPrefix(p, mountPath) + } + + return path.Join(mountPath, apiPrefix, tp) } func getHeaderForMap(header string, data map[string]interface{}) string {