diff --git a/logical/framework/path_map.go b/logical/framework/path_map.go index 6508a40e3..a3efdddeb 100644 --- a/logical/framework/path_map.go +++ b/logical/framework/path_map.go @@ -2,6 +2,7 @@ package framework import ( "fmt" + "strings" "sync" "github.com/hashicorp/vault/logical" @@ -56,6 +57,21 @@ func (p *PathMap) Put(s logical.Storage, k string, v map[string]interface{}) err return p.pathStruct(k).Put(s, v) } +// List reads the keys under a given path +func (p *PathMap) List(s logical.Storage, prefix string) ([]string, error) { + stripPrefix := fmt.Sprintf("struct/map/%s/", p.Name) + fullPrefix := fmt.Sprintf("%s%s", stripPrefix, prefix) + out, err := s.List(fullPrefix) + if err != nil { + return nil, err + } + stripped := make([]string, len(out)) + for idx, k := range out { + stripped[idx] = strings.TrimPrefix(k, stripPrefix) + } + return stripped, nil +} + // Paths are the paths to append to the Backend paths. func (p *PathMap) Paths() []*Path { p.once.Do(p.init) diff --git a/logical/framework/path_map_test.go b/logical/framework/path_map_test.go index d4c302d13..8aa317846 100644 --- a/logical/framework/path_map_test.go +++ b/logical/framework/path_map_test.go @@ -45,6 +45,15 @@ func TestPathMap(t *testing.T) { if v["value"] != "bar" { t.Fatalf("bad: %#v", v) } + + // Verify List + keys, err := p.List(storage, "") + if err != nil { + t.Fatalf("bad: %#v", err) + } + if len(keys) != 1 || keys[0] != "a" { + t.Fatalf("bad: %#v", keys) + } } func TestPathMap_getInvalid(t *testing.T) {