Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UI: Stabilize KV secret tests #20491

Merged
merged 19 commits into from
May 4, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Improve addPrefixToKVPath helper (#20488)
  • Loading branch information
averche authored and hellobontempo committed May 3, 2023
commit cb5162d8d8ce751148bb0d1561fe5156fed30757
3 changes: 3 additions & 0 deletions changelog/20488.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:improvement
cli: Improve addPrefixToKVPath helper
```
6 changes: 3 additions & 3 deletions command/kv_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ func (c *KVDeleteCommand) Run(args []string) int {
var fullPath string
if v2 {
secret, err = c.deleteV2(partialPath, mountPath, client)
fullPath = addPrefixToKVPath(partialPath, mountPath, "data")
fullPath = addPrefixToKVPath(partialPath, mountPath, "data", false)
} else {
// v1
if mountFlagSyntax {
Expand Down Expand Up @@ -195,13 +195,13 @@ func (c *KVDeleteCommand) deleteV2(path, mountPath string, client *api.Client) (
var secret *api.Secret
switch {
case len(c.flagVersions) > 0:
path = addPrefixToKVPath(path, mountPath, "delete")
path = addPrefixToKVPath(path, mountPath, "delete", false)
data := map[string]interface{}{
"versions": kvParseVersionsFlags(c.flagVersions),
}
secret, err = client.Logical().Write(path, data)
default:
path = addPrefixToKVPath(path, mountPath, "data")
path = addPrefixToKVPath(path, mountPath, "data", false)
secret, err = client.Logical().Delete(path)
}

Expand Down
2 changes: 1 addition & 1 deletion command/kv_destroy.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ func (c *KVDestroyCommand) Run(args []string) int {
c.UI.Error("Destroy not supported on KV Version 1")
return 1
}
destroyPath := addPrefixToKVPath(partialPath, mountPath, "destroy")
destroyPath := addPrefixToKVPath(partialPath, mountPath, "destroy", false)
if err != nil {
c.UI.Error(err.Error())
return 2
Expand Down
2 changes: 1 addition & 1 deletion command/kv_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ func (c *KVGetCommand) Run(args []string) int {
var fullPath string
// Add /data to v2 paths only
if v2 {
fullPath = addPrefixToKVPath(partialPath, mountPath, "data")
fullPath = addPrefixToKVPath(partialPath, mountPath, "data", false)

if c.flagVersion > 0 {
versionParam = map[string]string{
Expand Down
18 changes: 12 additions & 6 deletions command/kv_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,15 +128,15 @@ func isKVv2(path string, client *api.Client) (string, bool, error) {
return mountPath, version == 2, nil
}

func addPrefixToKVPath(p, mountPath, apiPrefix string) string {
if p == mountPath || p == strings.TrimSuffix(mountPath, "/") {
func addPrefixToKVPath(path, mountPath, apiPrefix string, skipIfExists bool) string {
if path == mountPath || path == strings.TrimSuffix(mountPath, "/") {
return paths.Join(mountPath, apiPrefix)
}

tp := strings.TrimPrefix(p, mountPath)
pathSuffix := strings.TrimPrefix(path, mountPath)
for {
// If the entire mountPath is included in the path, we are done
if tp != p {
if pathSuffix != path {
break
}
// Trim the parts of the mountPath that are not included in the
Expand All @@ -147,10 +147,16 @@ func addPrefixToKVPath(p, mountPath, apiPrefix string) string {
break
}
mountPath = strings.TrimSuffix(partialMountPath[1], "/")
tp = strings.TrimPrefix(tp, mountPath)
pathSuffix = strings.TrimPrefix(pathSuffix, mountPath)
}

return paths.Join(mountPath, apiPrefix, tp)
if skipIfExists {
if strings.HasPrefix(pathSuffix, apiPrefix) || strings.HasPrefix(pathSuffix, "/"+apiPrefix) {
return paths.Join(mountPath, pathSuffix)
}
}

return paths.Join(mountPath, apiPrefix, pathSuffix)
}

func getHeaderForMap(header string, data map[string]interface{}) string {
Expand Down
78 changes: 78 additions & 0 deletions command/kv_helpers_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package command

import "testing"

// TestAddPrefixToKVPath tests the addPrefixToKVPath helper function
func TestAddPrefixToKVPath(t *testing.T) {
cases := map[string]struct {
path string
mountPath string
apiPrefix string
skipIfExists bool
expected string
}{
"simple": {
path: "kv-v2/foo",
mountPath: "kv-v2/",
apiPrefix: "data",
skipIfExists: false,
expected: "kv-v2/data/foo",
},

"multi-part": {
path: "my/kv-v2/mount/path/foo/bar/baz",
mountPath: "my/kv-v2/mount/path",
apiPrefix: "metadata",
skipIfExists: false,
expected: "my/kv-v2/mount/path/metadata/foo/bar/baz",
},

"with-namespace": {
path: "my/kv-v2/mount/path/foo/bar/baz",
mountPath: "my/ns1/my/kv-v2/mount/path",
apiPrefix: "metadata",
skipIfExists: false,
expected: "my/kv-v2/mount/path/metadata/foo/bar/baz",
},

"skip-if-exists-true": {
path: "kv-v2/data/foo",
mountPath: "kv-v2/",
apiPrefix: "data",
skipIfExists: true,
expected: "kv-v2/data/foo",
},

"skip-if-exists-false": {
path: "kv-v2/data/foo",
mountPath: "kv-v2",
apiPrefix: "data",
skipIfExists: false,
expected: "kv-v2/data/data/foo",
},

"skip-if-exists-with-namespace": {
path: "my/kv-v2/mount/path/metadata/foo/bar/baz",
mountPath: "my/ns1/my/kv-v2/mount/path",
apiPrefix: "metadata",
skipIfExists: true,
expected: "my/kv-v2/mount/path/metadata/foo/bar/baz",
},
}

for name, tc := range cases {
name, tc := name, tc
t.Run(name, func(t *testing.T) {
actual := addPrefixToKVPath(
tc.path,
tc.mountPath,
tc.apiPrefix,
tc.skipIfExists,
)

if tc.expected != actual {
t.Fatalf("unexpected output; want: %v, got: %v", tc.expected, actual)
}
})
}
}
2 changes: 1 addition & 1 deletion command/kv_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func (c *KVListCommand) Run(args []string) int {
// Add /metadata to v2 paths only
var fullPath string
if v2 {
fullPath = addPrefixToKVPath(partialPath, mountPath, "metadata")
fullPath = addPrefixToKVPath(partialPath, mountPath, "metadata", false)
} else {
// v1
if mountFlagSyntax {
Expand Down
2 changes: 1 addition & 1 deletion command/kv_metadata_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ func (c *KVMetadataDeleteCommand) Run(args []string) int {
return 1
}

fullPath := addPrefixToKVPath(partialPath, mountPath, "metadata")
fullPath := addPrefixToKVPath(partialPath, mountPath, "metadata", false)
if secret, err := client.Logical().Delete(fullPath); err != nil {
c.UI.Error(fmt.Sprintf("Error deleting %s: %s", fullPath, err))
if secret != nil {
Expand Down
2 changes: 1 addition & 1 deletion command/kv_metadata_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func (c *KVMetadataGetCommand) Run(args []string) int {
return 1
}

fullPath := addPrefixToKVPath(partialPath, mountPath, "metadata")
fullPath := addPrefixToKVPath(partialPath, mountPath, "metadata", false)
secret, err := client.Logical().Read(fullPath)
if err != nil {
c.UI.Error(fmt.Sprintf("Error reading %s: %s", fullPath, err))
Expand Down
2 changes: 1 addition & 1 deletion command/kv_metadata_patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ func (c *KVMetadataPatchCommand) Run(args []string) int {
return 1
}

fullPath := addPrefixToKVPath(partialPath, mountPath, "metadata")
fullPath := addPrefixToKVPath(partialPath, mountPath, "metadata", false)

data := make(map[string]interface{}, 0)

Expand Down
2 changes: 1 addition & 1 deletion command/kv_metadata_put.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ func (c *KVMetadataPutCommand) Run(args []string) int {
return 1
}

fullPath := addPrefixToKVPath(partialPath, mountPath, "metadata")
fullPath := addPrefixToKVPath(partialPath, mountPath, "metadata", false)
data := map[string]interface{}{}

if c.flagMaxVersions >= 0 {
Expand Down
2 changes: 1 addition & 1 deletion command/kv_patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ func (c *KVPatchCommand) Run(args []string) int {
return 2
}

fullPath := addPrefixToKVPath(partialPath, mountPath, "data")
fullPath := addPrefixToKVPath(partialPath, mountPath, "data", false)
if err != nil {
c.UI.Error(err.Error())
return 2
Expand Down
2 changes: 1 addition & 1 deletion command/kv_put.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ func (c *KVPutCommand) Run(args []string) int {
// Add /data to v2 paths only
var fullPath string
if v2 {
fullPath = addPrefixToKVPath(partialPath, mountPath, "data")
fullPath = addPrefixToKVPath(partialPath, mountPath, "data", false)
data = map[string]interface{}{
"data": data,
"options": map[string]interface{}{},
Expand Down
2 changes: 1 addition & 1 deletion command/kv_rollback.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ func (c *KVRollbackCommand) Run(args []string) int {
return 2
}

fullPath := addPrefixToKVPath(partialPath, mountPath, "data")
fullPath := addPrefixToKVPath(partialPath, mountPath, "data", false)
if err != nil {
c.UI.Error(err.Error())
return 2
Expand Down
2 changes: 1 addition & 1 deletion command/kv_undelete.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ func (c *KVUndeleteCommand) Run(args []string) int {
return 1
}

undeletePath := addPrefixToKVPath(partialPath, mountPath, "undelete")
undeletePath := addPrefixToKVPath(partialPath, mountPath, "undelete", false)
data := map[string]interface{}{
"versions": kvParseVersionsFlags(c.flagVersions),
}
Expand Down