2023-03-15 19:00:52 +03:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
2024-01-02 21:36:20 +03:00
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
2023-03-15 19:00:52 +03:00
|
|
|
|
2015-03-06 01:34:05 +03:00
|
|
|
package vault
|
|
|
|
|
2015-03-15 23:52:43 +03:00
|
|
|
import (
|
2018-01-19 09:44:44 +03:00
|
|
|
"context"
|
2017-08-15 23:44:16 +03:00
|
|
|
"errors"
|
2018-04-04 04:39:11 +03:00
|
|
|
"sync"
|
2015-03-06 01:34:05 +03:00
|
|
|
|
2019-04-13 00:54:35 +03:00
|
|
|
"github.com/hashicorp/vault/sdk/logical"
|
2015-03-15 23:52:43 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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.
|
2015-03-06 01:34:05 +03:00
|
|
|
type BarrierView struct {
|
2019-01-31 17:25:18 +03:00
|
|
|
storage *logical.StorageView
|
2018-04-04 04:39:11 +03:00
|
|
|
readOnlyErr error
|
|
|
|
readOnlyErrLock sync.RWMutex
|
2018-09-18 06:03:00 +03:00
|
|
|
iCheck interface{}
|
2015-03-06 01:34:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewBarrierView takes an underlying security barrier and returns
|
|
|
|
// a view of it that can only operate with the given prefix.
|
2019-01-31 17:25:18 +03:00
|
|
|
func NewBarrierView(barrier logical.Storage, prefix string) *BarrierView {
|
2015-03-06 01:34:05 +03:00
|
|
|
return &BarrierView{
|
2019-01-31 17:25:18 +03:00
|
|
|
storage: logical.NewStorageView(barrier, prefix),
|
2015-03-06 01:34:05 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-18 06:03:00 +03:00
|
|
|
func (v *BarrierView) setICheck(iCheck interface{}) {
|
|
|
|
v.iCheck = iCheck
|
|
|
|
}
|
|
|
|
|
2018-02-09 22:04:25 +03:00
|
|
|
func (v *BarrierView) setReadOnlyErr(readOnlyErr error) {
|
2018-04-04 04:39:11 +03:00
|
|
|
v.readOnlyErrLock.Lock()
|
|
|
|
defer v.readOnlyErrLock.Unlock()
|
2018-02-09 22:04:25 +03:00
|
|
|
v.readOnlyErr = readOnlyErr
|
|
|
|
}
|
|
|
|
|
2018-04-04 04:39:11 +03:00
|
|
|
func (v *BarrierView) getReadOnlyErr() error {
|
|
|
|
v.readOnlyErrLock.RLock()
|
|
|
|
defer v.readOnlyErrLock.RUnlock()
|
|
|
|
return v.readOnlyErr
|
|
|
|
}
|
|
|
|
|
2019-01-31 17:25:18 +03:00
|
|
|
func (v *BarrierView) Prefix() string {
|
|
|
|
return v.storage.Prefix()
|
2015-03-31 23:32:24 +03:00
|
|
|
}
|
|
|
|
|
2018-01-19 09:44:44 +03:00
|
|
|
func (v *BarrierView) List(ctx context.Context, prefix string) ([]string, error) {
|
2019-01-31 17:25:18 +03:00
|
|
|
return v.storage.List(ctx, prefix)
|
2015-03-06 01:34:05 +03:00
|
|
|
}
|
|
|
|
|
2018-01-19 09:44:44 +03:00
|
|
|
func (v *BarrierView) Get(ctx context.Context, key string) (*logical.StorageEntry, error) {
|
2019-01-31 17:25:18 +03:00
|
|
|
return v.storage.Get(ctx, key)
|
2015-03-06 01:34:05 +03:00
|
|
|
}
|
|
|
|
|
2019-01-31 17:25:18 +03:00
|
|
|
// Put differs from List/Get because it checks read-only errors
|
2018-01-19 09:44:44 +03:00
|
|
|
func (v *BarrierView) Put(ctx context.Context, entry *logical.StorageEntry) error {
|
2018-06-20 16:32:06 +03:00
|
|
|
if entry == nil {
|
|
|
|
return errors.New("cannot write nil entry")
|
|
|
|
}
|
|
|
|
|
2019-01-31 17:25:18 +03:00
|
|
|
expandedKey := v.storage.ExpandKey(entry.Key)
|
2017-02-17 07:09:39 +03:00
|
|
|
|
2018-04-04 04:39:11 +03:00
|
|
|
roErr := v.getReadOnlyErr()
|
|
|
|
if roErr != nil {
|
2018-09-18 06:03:00 +03:00
|
|
|
if runICheck(v, expandedKey, roErr) {
|
|
|
|
return roErr
|
|
|
|
}
|
2017-02-17 07:09:39 +03:00
|
|
|
}
|
|
|
|
|
2019-01-31 17:25:18 +03:00
|
|
|
return v.storage.Put(ctx, entry)
|
2015-03-15 23:52:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// logical.Storage impl.
|
2018-01-19 09:44:44 +03:00
|
|
|
func (v *BarrierView) Delete(ctx context.Context, key string) error {
|
2019-01-31 17:25:18 +03:00
|
|
|
expandedKey := v.storage.ExpandKey(key)
|
2017-02-17 07:09:39 +03:00
|
|
|
|
2018-04-04 04:39:11 +03:00
|
|
|
roErr := v.getReadOnlyErr()
|
|
|
|
if roErr != nil {
|
2018-09-18 06:03:00 +03:00
|
|
|
if runICheck(v, expandedKey, roErr) {
|
|
|
|
return roErr
|
|
|
|
}
|
2017-02-17 07:09:39 +03:00
|
|
|
}
|
|
|
|
|
2019-01-31 17:25:18 +03:00
|
|
|
return v.storage.Delete(ctx, key)
|
2015-03-06 01:34:05 +03:00
|
|
|
}
|
|
|
|
|
2015-03-15 23:52:43 +03:00
|
|
|
// SubView constructs a nested sub-view using the given prefix
|
|
|
|
func (v *BarrierView) SubView(prefix string) *BarrierView {
|
2019-01-31 17:25:18 +03:00
|
|
|
return &BarrierView{
|
|
|
|
storage: v.storage.SubView(prefix),
|
|
|
|
readOnlyErr: v.getReadOnlyErr(),
|
|
|
|
iCheck: v.iCheck,
|
|
|
|
}
|
2015-03-06 01:34:05 +03:00
|
|
|
}
|