forked from openbao/openbao
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbarrier_view.go
More file actions
198 lines (163 loc) · 4.83 KB
/
barrier_view.go
File metadata and controls
198 lines (163 loc) · 4.83 KB
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package vault
import (
"context"
"errors"
"sync"
"github.com/openbao/openbao/sdk/v2/logical"
)
// BarrierCore defines the core operations unique to BarrierView.
type BarrierCore interface {
Prefix() string
SubView(string) BarrierView
SetReadOnlyErr(error)
GetReadOnlyErr() error
}
// 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 interface {
logical.Storage
BarrierCore
}
type barrierView struct {
storage logical.StorageView
readOnlyErr error
readOnlyErrLock sync.RWMutex
}
var (
_ BarrierView = &barrierView{}
_ logical.ClearableView = &barrierView{}
)
// TransactionalBarrierView is like BarrierView but transactional.
type TransactionalBarrierView interface {
logical.TransactionalStorage
BarrierCore
}
type transactionalBarrierView struct {
barrierView
}
var (
_ BarrierView = &transactionalBarrierView{}
_ TransactionalBarrierView = &transactionalBarrierView{}
)
// BarrierViewTransaction is the result of beginning a transaction on a
// BarrierView.
type BarrierViewTransaction interface {
logical.Transaction
BarrierCore
}
type barrierViewTransaction struct {
barrierView
wasReadOnly bool
}
var (
_ BarrierView = &barrierViewTransaction{}
_ logical.Transaction = &barrierViewTransaction{}
_ BarrierViewTransaction = &barrierViewTransaction{}
_ logical.ClearableView = &barrierViewTransaction{}
)
// NewBarrierView takes an underlying security barrier and returns
// a view of it that can only operate with the given prefix.
func NewBarrierView(barrier logical.Storage, prefix string) BarrierView {
return newBarrierView(logical.NewStorageView(barrier, prefix))
}
func newBarrierView(s logical.StorageView) BarrierView {
if _, ok := s.(logical.TransactionalStorageView); ok {
return &transactionalBarrierView{
barrierView: barrierView{
storage: s,
},
}
}
return &barrierView{
storage: s,
}
}
func (v *barrierView) SetReadOnlyErr(readOnlyErr error) {
v.readOnlyErrLock.Lock()
defer v.readOnlyErrLock.Unlock()
v.readOnlyErr = readOnlyErr
}
func (v *barrierView) GetReadOnlyErr() error {
v.readOnlyErrLock.RLock()
defer v.readOnlyErrLock.RUnlock()
return v.readOnlyErr
}
func (v *barrierView) Prefix() string {
return v.storage.Prefix()
}
func (v *barrierView) List(ctx context.Context, prefix string) ([]string, error) {
return v.storage.List(ctx, prefix)
}
func (v *barrierView) ListPage(ctx context.Context, prefix string, after string, limit int) ([]string, error) {
return v.storage.ListPage(ctx, prefix, after, limit)
}
func (v *barrierView) Get(ctx context.Context, key string) (*logical.StorageEntry, error) {
return v.storage.Get(ctx, key)
}
// Put differs from List/Get because it checks read-only errors
func (v *barrierView) Put(ctx context.Context, entry *logical.StorageEntry) error {
if entry == nil {
return errors.New("cannot write nil entry")
}
roErr := v.GetReadOnlyErr()
if roErr != nil {
return roErr
}
return v.storage.Put(ctx, entry)
}
func (v *barrierView) Delete(ctx context.Context, key string) error {
roErr := v.GetReadOnlyErr()
if roErr != nil {
return roErr
}
return v.storage.Delete(ctx, key)
}
// SubView constructs a nested sub-view using the given prefix
func (v *barrierView) SubView(prefix string) BarrierView {
bv := newBarrierView(v.storage.SubView(prefix))
bv.SetReadOnlyErr(v.GetReadOnlyErr())
return bv
}
func (v *transactionalBarrierView) BeginReadOnlyTx(ctx context.Context) (logical.Transaction, error) {
txn, err := v.barrierView.storage.(logical.TransactionalStorage).BeginReadOnlyTx(ctx)
if err != nil {
return nil, err
}
return &barrierViewTransaction{
barrierView{
storage: txn.(logical.StorageView),
readOnlyErr: v.GetReadOnlyErr(),
},
true,
}, nil
}
func (v *transactionalBarrierView) BeginTx(ctx context.Context) (logical.Transaction, error) {
txn, err := v.barrierView.storage.(logical.TransactionalStorage).BeginTx(ctx)
if err != nil {
return nil, err
}
return &barrierViewTransaction{
barrierView{
storage: txn.(logical.StorageView),
readOnlyErr: v.GetReadOnlyErr(),
},
false,
}, nil
}
func (v *barrierViewTransaction) Commit(ctx context.Context) error {
roErr := v.GetReadOnlyErr()
if roErr != nil && !v.wasReadOnly {
return roErr
}
return v.barrierView.storage.(logical.Transaction).Commit(ctx)
}
func (v *barrierViewTransaction) Rollback(ctx context.Context) error {
return v.barrierView.storage.(logical.Transaction).Rollback(ctx)
}