2015-03-05 22:34:05 +00:00
|
|
|
package vault
|
|
|
|
|
2015-03-15 20:52:43 +00:00
|
|
|
import (
|
2018-01-19 06:44:44 +00:00
|
|
|
"context"
|
2017-08-15 20:44:16 +00:00
|
|
|
"errors"
|
2015-03-15 20:52:43 +00:00
|
|
|
"strings"
|
2015-03-05 22:34:05 +00:00
|
|
|
|
2015-03-15 20:52:43 +00:00
|
|
|
"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.
|
2015-03-05 22:34:05 +00:00
|
|
|
type BarrierView struct {
|
2017-01-07 23:18:22 +00:00
|
|
|
barrier BarrierStorage
|
|
|
|
prefix string
|
|
|
|
readonly bool
|
2015-03-05 22:34:05 +00:00
|
|
|
}
|
|
|
|
|
2017-08-15 20:44:16 +00:00
|
|
|
var (
|
|
|
|
ErrRelativePath = errors.New("relative paths not supported")
|
|
|
|
)
|
|
|
|
|
2015-03-05 22:34:05 +00:00
|
|
|
// NewBarrierView takes an underlying security barrier and returns
|
|
|
|
// a view of it that can only operate with the given prefix.
|
2015-04-10 23:43:35 +00:00
|
|
|
func NewBarrierView(barrier BarrierStorage, prefix string) *BarrierView {
|
2015-03-05 22:34:05 +00:00
|
|
|
return &BarrierView{
|
|
|
|
barrier: barrier,
|
|
|
|
prefix: prefix,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-31 20:32:24 +00:00
|
|
|
// sanityCheck is used to perform a sanity check on a key
|
|
|
|
func (v *BarrierView) sanityCheck(key string) error {
|
|
|
|
if strings.Contains(key, "..") {
|
2017-08-15 20:44:16 +00:00
|
|
|
return ErrRelativePath
|
2015-03-31 20:32:24 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-03-15 20:52:43 +00:00
|
|
|
// logical.Storage impl.
|
2018-01-19 06:44:44 +00:00
|
|
|
func (v *BarrierView) List(ctx context.Context, prefix string) ([]string, error) {
|
2015-03-31 20:32:24 +00:00
|
|
|
if err := v.sanityCheck(prefix); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-01-19 06:44:44 +00:00
|
|
|
return v.barrier.List(ctx, v.expandKey(prefix))
|
2015-03-05 22:34:05 +00:00
|
|
|
}
|
|
|
|
|
2015-03-15 20:52:43 +00:00
|
|
|
// logical.Storage impl.
|
2018-01-19 06:44:44 +00:00
|
|
|
func (v *BarrierView) Get(ctx context.Context, key string) (*logical.StorageEntry, error) {
|
2015-03-31 20:32:24 +00:00
|
|
|
if err := v.sanityCheck(key); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-01-19 06:44:44 +00:00
|
|
|
entry, err := v.barrier.Get(ctx, v.expandKey(key))
|
2015-03-05 22:34:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-03-15 20:52:43 +00:00
|
|
|
if entry == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2015-03-05 22:34:05 +00:00
|
|
|
if entry != nil {
|
|
|
|
entry.Key = v.truncateKey(entry.Key)
|
|
|
|
}
|
2015-03-15 20:52:43 +00:00
|
|
|
|
|
|
|
return &logical.StorageEntry{
|
2017-10-23 20:42:56 +00:00
|
|
|
Key: entry.Key,
|
|
|
|
Value: entry.Value,
|
|
|
|
SealWrap: entry.SealWrap,
|
2015-03-15 20:52:43 +00:00
|
|
|
}, nil
|
2015-03-05 22:34:05 +00:00
|
|
|
}
|
|
|
|
|
2015-03-15 20:52:43 +00:00
|
|
|
// logical.Storage impl.
|
2018-01-19 06:44:44 +00:00
|
|
|
func (v *BarrierView) Put(ctx context.Context, entry *logical.StorageEntry) error {
|
2015-03-31 20:32:24 +00:00
|
|
|
if err := v.sanityCheck(entry.Key); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-02-17 04:09:39 +00:00
|
|
|
|
|
|
|
expandedKey := v.expandKey(entry.Key)
|
|
|
|
|
|
|
|
if v.readonly {
|
|
|
|
return logical.ErrReadOnly
|
|
|
|
}
|
|
|
|
|
2015-03-15 20:52:43 +00:00
|
|
|
nested := &Entry{
|
2017-10-23 20:42:56 +00:00
|
|
|
Key: expandedKey,
|
|
|
|
Value: entry.Value,
|
|
|
|
SealWrap: entry.SealWrap,
|
2015-03-15 20:52:43 +00:00
|
|
|
}
|
2018-01-19 06:44:44 +00:00
|
|
|
return v.barrier.Put(ctx, nested)
|
2015-03-15 20:52:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// logical.Storage impl.
|
2018-01-19 06:44:44 +00:00
|
|
|
func (v *BarrierView) Delete(ctx context.Context, key string) error {
|
2015-03-31 20:32:24 +00:00
|
|
|
if err := v.sanityCheck(key); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-02-17 04:09:39 +00:00
|
|
|
|
|
|
|
expandedKey := v.expandKey(key)
|
|
|
|
|
|
|
|
if v.readonly {
|
|
|
|
return logical.ErrReadOnly
|
|
|
|
}
|
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
return v.barrier.Delete(ctx, expandedKey)
|
2015-03-05 22:34:05 +00:00
|
|
|
}
|
|
|
|
|
2015-03-15 20:52:43 +00:00
|
|
|
// SubView constructs a nested sub-view using the given prefix
|
|
|
|
func (v *BarrierView) SubView(prefix string) *BarrierView {
|
|
|
|
sub := v.expandKey(prefix)
|
2017-01-07 23:18:22 +00:00
|
|
|
return &BarrierView{barrier: v.barrier, prefix: sub, readonly: v.readonly}
|
2015-03-05 22:34:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|