mirror of
https://github.com/optim-enterprises-bv/vault.git
synced 2025-10-30 02:02:43 +00:00
logical/framework: Supporting list of path map
This commit is contained in:
@@ -2,6 +2,7 @@ package framework
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"github.com/hashicorp/vault/logical"
|
"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)
|
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.
|
// Paths are the paths to append to the Backend paths.
|
||||||
func (p *PathMap) Paths() []*Path {
|
func (p *PathMap) Paths() []*Path {
|
||||||
p.once.Do(p.init)
|
p.once.Do(p.init)
|
||||||
|
|||||||
@@ -45,6 +45,15 @@ func TestPathMap(t *testing.T) {
|
|||||||
if v["value"] != "bar" {
|
if v["value"] != "bar" {
|
||||||
t.Fatalf("bad: %#v", v)
|
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) {
|
func TestPathMap_getInvalid(t *testing.T) {
|
||||||
|
|||||||
Reference in New Issue
Block a user