forked from hashicorp/vault
-
Notifications
You must be signed in to change notification settings - Fork 2
/
barrier_view.go
162 lines (142 loc) · 3.84 KB
/
barrier_view.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package vault
import (
"fmt"
"strings"
"github.com/hashicorp/vault/logical"
)
// BarrierView wraps a SecurityBarrier and ensures all access is automatically
// prefixed. This is used to prevent anyone with access to the view to access
// any data in the durable storage outside of their prefix. Conceptually this
// is like a "chroot" into the barrier.
//
// BarrierView implements logical.Storage so it can be passed in as the
// durable storage mechanism for logical views.
type BarrierView struct {
barrier BarrierStorage
prefix string
}
// NewBarrierView takes an underlying security barrier and returns
// a view of it that can only operate with the given prefix.
func NewBarrierView(barrier BarrierStorage, prefix string) *BarrierView {
return &BarrierView{
barrier: barrier,
prefix: prefix,
}
}
// sanityCheck is used to perform a sanity check on a key
func (v *BarrierView) sanityCheck(key string) error {
if strings.Contains(key, "..") {
return fmt.Errorf("key cannot be relative path")
}
return nil
}
// logical.Storage impl.
func (v *BarrierView) List(prefix string) ([]string, error) {
if err := v.sanityCheck(prefix); err != nil {
return nil, err
}
return v.barrier.List(v.expandKey(prefix))
}
// logical.Storage impl.
func (v *BarrierView) Get(key string) (*logical.StorageEntry, error) {
if err := v.sanityCheck(key); err != nil {
return nil, err
}
entry, err := v.barrier.Get(v.expandKey(key))
if err != nil {
return nil, err
}
if entry == nil {
return nil, nil
}
if entry != nil {
entry.Key = v.truncateKey(entry.Key)
}
return &logical.StorageEntry{
Key: entry.Key,
Value: entry.Value,
}, nil
}
// logical.Storage impl.
func (v *BarrierView) Put(entry *logical.StorageEntry) error {
if err := v.sanityCheck(entry.Key); err != nil {
return err
}
nested := &Entry{
Key: v.expandKey(entry.Key),
Value: entry.Value,
}
return v.barrier.Put(nested)
}
// logical.Storage impl.
func (v *BarrierView) Delete(key string) error {
if err := v.sanityCheck(key); err != nil {
return err
}
return v.barrier.Delete(v.expandKey(key))
}
// SubView constructs a nested sub-view using the given prefix
func (v *BarrierView) SubView(prefix string) *BarrierView {
sub := v.expandKey(prefix)
return &BarrierView{barrier: v.barrier, prefix: sub}
}
// expandKey is used to expand to the full key path with the prefix
func (v *BarrierView) expandKey(suffix string) string {
return v.prefix + suffix
}
// truncateKey is used to remove the prefix of the key
func (v *BarrierView) truncateKey(full string) string {
return strings.TrimPrefix(full, v.prefix)
}
// ScanView is used to scan all the keys in a view iteratively
func ScanView(view *BarrierView, cb func(path string)) error {
frontier := []string{""}
for len(frontier) > 0 {
n := len(frontier)
current := frontier[n-1]
frontier = frontier[:n-1]
// List the contents
contents, err := view.List(current)
if err != nil {
return fmt.Errorf("list failed at path '%s': %v", current, err)
}
// Handle the contents in the directory
for _, c := range contents {
fullPath := current + c
if strings.HasSuffix(c, "/") {
frontier = append(frontier, fullPath)
} else {
cb(fullPath)
}
}
}
return nil
}
// CollectKeys is used to collect all the keys in a view
func CollectKeys(view *BarrierView) ([]string, error) {
// Accumulate the keys
var existing []string
cb := func(path string) {
existing = append(existing, path)
}
// Scan for all the keys
if err := ScanView(view, cb); err != nil {
return nil, err
}
return existing, nil
}
// ClearView is used to delete all the keys in a view
func ClearView(view *BarrierView) error {
// Collect all the keys
keys, err := CollectKeys(view)
if err != nil {
return err
}
// Delete all the keys
for _, key := range keys {
if err := view.Delete(key); err != nil {
return err
}
}
return nil
}