Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
10 changes: 6 additions & 4 deletions pkg/onepassword/items.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package onepassword

import (
"fmt"
"strings"
"regexp"

"github.com/1Password/connect-sdk-go/connect"
"github.com/1Password/connect-sdk-go/onepassword"
Expand Down Expand Up @@ -42,9 +42,11 @@ func GetOnePasswordItemByPath(opConnectClient connect.Client, path string) (*one
}

func ParseVaultAndItemFromPath(path string) (string, string, error) {
splitPath := strings.Split(path, "/")
if len(splitPath) == 4 && splitPath[0] == "vaults" && splitPath[2] == "items" {
return splitPath[1], splitPath[3], nil
r := regexp.MustCompile("vaults/(.*)/items/(.*)")
splitPath := r.FindAllStringSubmatch(path, -1)
Copy link
Contributor

@AndyTitu AndyTitu Jul 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A little bit shorter but does the same thing:

Suggested change
splitPath := r.FindAllStringSubmatch(path, -1)
splitPath := r.FindStringSubmatch(path)

since we expect a single match anyway. Otherwise, injecting another vaults/.../items/... in the path could lead to an unexpected result.

We could also add another unit test case for the vaults/foo1/items/bar1/vaults/foo2/items/bar2 example


if len(splitPath) == 1 && len(splitPath[0]) == 3 {
return splitPath[0][1], splitPath[0][2], nil
}
return "", "", fmt.Errorf("%q is not an acceptable path for One Password item. Must be of the format: `vaults/{vault_id}/items/{item_id}`", path)
}
Expand Down
57 changes: 57 additions & 0 deletions pkg/onepassword/items_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package onepassword

import (
"fmt"
"testing"
)

func TestParseVaultAndItemFromPath(t *testing.T) {
cases := []struct {
Path string
Vault string
Item string
Error error
}{
{
"vaults/foo/items/bar",
"foo",
"bar",
nil,
},
{
"vaults/foo/items/bar/baz",
"foo",
"bar/baz",
nil,
},
{
"vaults/foo/bar/items/baz",
"foo/bar",
"baz",
nil,
},
{
"foo/bar",
"",
"",
fmt.Errorf("\"foo/bar\" is not an acceptable path for One Password item. Must be of the format: `vaults/{vault_id}/items/{item_id}`"),
},
}

for _, c := range cases {
vault, item, err := ParseVaultAndItemFromPath(c.Path)

if err != c.Error && err.Error() != c.Error.Error() {
t.Errorf("unexpected error %v: %v", err, c.Error)
}

if vault != c.Vault {
t.Errorf("couldn't extract vault out of path %s: %s", c.Path, vault)
}

if item != c.Item {
t.Errorf("couldn't extract item out of path %s: %s != %s", c.Path, item, c.Item)
}

}
}