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
22 changes: 19 additions & 3 deletions sdk/logical/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,13 @@ type ClearableView interface {

// ScanView is used to scan all the keys in a view iteratively
func ScanView(ctx context.Context, view ClearableView, cb func(path string)) error {
frontier := []string{""}
return scanView(ctx, view, "", cb)
}

// scanView is ScanView starting at a directory prefix of the view
// ("" for the view root). Every key handed to cb is prefixed with start.
func scanView(ctx context.Context, view ClearableView, start string, cb func(path string)) error {
frontier := []string{start}
for len(frontier) > 0 {
n := len(frontier)
current := frontier[n-1]
Expand Down Expand Up @@ -146,8 +152,18 @@ func CollectKeysWithPrefix(ctx context.Context, view ClearableView, prefix strin
}
}

// Scan for all the keys
if err := ScanView(ctx, view, cb); err != nil {
// Scan for all the keys, starting at the directory containing the
// prefix instead of the view root. Previously the scan walked the
// entire view and filtered afterwards, so listing a small prefix
// (e.g. "password_policy/" on the sys/ view) also walked every
// other directory in the view - including the expiration manager's
// lease tree under sys/expire/id/ - and failed once that walk grew
// too large for the request deadline.
start := ""
if i := strings.LastIndex(prefix, "/"); i >= 0 {
start = prefix[:i+1]
}
if err := scanView(ctx, view, start, cb); err != nil {
return nil, err
}
return keys, nil
Expand Down
57 changes: 57 additions & 0 deletions sdk/logical/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ package logical

import (
"context"
"fmt"
"strings"
"testing"

"github.com/go-test/deep"
Expand Down Expand Up @@ -102,3 +104,58 @@ func prepKeyStorage(t *testing.T) Storage {

return s
}

// recordingView wraps a Storage, recording every List prefix it is asked
// for, and failing List calls outside allowedPrefix. It proves that
// CollectKeysWithPrefix never descends outside the requested prefix.
type recordingView struct {
Storage
lists []string
allowedPrefix string
}

func (v *recordingView) List(ctx context.Context, prefix string) ([]string, error) {
v.lists = append(v.lists, prefix)
if !strings.HasPrefix(prefix, v.allowedPrefix) {
return nil, fmt.Errorf("list outside prefix %q: %q", v.allowedPrefix, prefix)
}
return v.Storage.List(ctx, prefix)
}

func TestCollectKeysWithPrefix_DoesNotScanOutsidePrefix(t *testing.T) {
s := &InmemStorage{}
for _, key := range []string{
"password_policy/default",
"password_policy/strong",
"password_policy/nested/deep",
"expire/id/auth/approle/login/abc",
"expire/id/auth/approle/login/def",
"other/x",
} {
if err := s.Put(context.Background(), &StorageEntry{Key: key}); err != nil {
t.Fatal(err)
}
}

v := &recordingView{Storage: s, allowedPrefix: "password_policy/"}
keys, err := CollectKeysWithPrefix(context.Background(), v, "password_policy/")
if err != nil {
t.Fatal(err)
}

// Depth-first order: "default" and "strong" are visited while listing
// "password_policy/", "nested/deep" only after descending into "nested/".
exp := []string{
"password_policy/default",
"password_policy/strong",
"password_policy/nested/deep",
}
if diff := deep.Equal(keys, exp); diff != nil {
t.Fatal(diff)
}
for _, prefix := range v.lists {
if !strings.HasPrefix(prefix, "password_policy/") {
t.Fatalf("listed outside the prefix: %q (all lists: %v)", prefix, v.lists)
}
}
}