forked from hashicorp/vault
-
Notifications
You must be signed in to change notification settings - Fork 0
/
physical_view.go
98 lines (84 loc) · 2.15 KB
/
physical_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
package physical
import (
"context"
"errors"
"strings"
)
var (
ErrRelativePath = errors.New("relative paths not supported")
)
// View represents a prefixed view of a physical backend
type View struct {
backend Backend
prefix string
}
// Verify View satisfies the correct interfaces
var _ Backend = (*View)(nil)
// NewView takes an underlying physical backend and returns
// a view of it that can only operate with the given prefix.
func NewView(backend Backend, prefix string) *View {
return &View{
backend: backend,
prefix: prefix,
}
}
// List the contents of the prefixed view
func (v *View) List(ctx context.Context, prefix string) ([]string, error) {
if err := v.sanityCheck(prefix); err != nil {
return nil, err
}
return v.backend.List(ctx, v.expandKey(prefix))
}
// Get the key of the prefixed view
func (v *View) Get(ctx context.Context, key string) (*Entry, error) {
if err := v.sanityCheck(key); err != nil {
return nil, err
}
entry, err := v.backend.Get(ctx, 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 &Entry{
Key: entry.Key,
Value: entry.Value,
}, nil
}
// Put the entry into the prefix view
func (v *View) Put(ctx context.Context, entry *Entry) error {
if err := v.sanityCheck(entry.Key); err != nil {
return err
}
nested := &Entry{
Key: v.expandKey(entry.Key),
Value: entry.Value,
}
return v.backend.Put(ctx, nested)
}
// Delete the entry from the prefix view
func (v *View) Delete(ctx context.Context, key string) error {
if err := v.sanityCheck(key); err != nil {
return err
}
return v.backend.Delete(ctx, v.expandKey(key))
}
// sanityCheck is used to perform a sanity check on a key
func (v *View) sanityCheck(key string) error {
if strings.Contains(key, "..") {
return ErrRelativePath
}
return nil
}
// expandKey is used to expand to the full key path with the prefix
func (v *View) expandKey(suffix string) string {
return v.prefix + suffix
}
// truncateKey is used to remove the prefix of the key
func (v *View) truncateKey(full string) string {
return strings.TrimPrefix(full, v.prefix)
}