open-vault/logical/plugin/mock/path_kv.go
Calvin Leung Huang 86ea7e945d Add plugin auto-reload capability (#3171)
* Add automatic plugin reload

* Refactor builtin/backend

* Remove plugin reload at the core level

* Refactor plugin tests

* Add auto-reload test case

* Change backend to use sync.RWMutex, fix dangling test plugin processes

* Add a canary to plugin backends to avoid reloading many times (#3174)

* Call setupPluginCatalog before mount-related operations in postUnseal

* Don't create multiple system backends since core only holds a reference (#3176)

to one.
2017-08-15 22:10:32 -04:00

104 lines
2.5 KiB
Go

package mock
import (
"fmt"
"github.com/hashicorp/vault/logical"
"github.com/hashicorp/vault/logical/framework"
)
// kvPaths is used to test CRUD and List operations. It is a simplified
// version of the passthrough backend that only accepts string values.
func kvPaths(b *backend) []*framework.Path {
return []*framework.Path{
&framework.Path{
Pattern: "kv/?",
Callbacks: map[logical.Operation]framework.OperationFunc{
logical.ListOperation: b.pathKVList,
},
},
&framework.Path{
Pattern: "kv/" + framework.GenericNameRegex("key"),
Fields: map[string]*framework.FieldSchema{
"key": &framework.FieldSchema{Type: framework.TypeString},
"value": &framework.FieldSchema{Type: framework.TypeString},
},
ExistenceCheck: b.pathExistenceCheck,
Callbacks: map[logical.Operation]framework.OperationFunc{
logical.ReadOperation: b.pathKVRead,
logical.CreateOperation: b.pathKVCreateUpdate,
logical.UpdateOperation: b.pathKVCreateUpdate,
logical.DeleteOperation: b.pathKVDelete,
},
},
}
}
func (b *backend) pathExistenceCheck(req *logical.Request, data *framework.FieldData) (bool, error) {
out, err := req.Storage.Get(req.Path)
if err != nil {
return false, fmt.Errorf("existence check failed: %v", err)
}
return out != nil, nil
}
func (b *backend) pathKVRead(
req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
entry, err := req.Storage.Get(req.Path)
if err != nil {
return nil, err
}
if entry == nil {
return nil, nil
}
value := string(entry.Value)
// Return the secret
return &logical.Response{
Data: map[string]interface{}{
"value": value,
},
}, nil
}
func (b *backend) pathKVCreateUpdate(
req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
value := data.Get("value").(string)
entry := &logical.StorageEntry{
Key: req.Path,
Value: []byte(value),
}
s := req.Storage
err := s.Put(entry)
if err != nil {
return nil, err
}
return &logical.Response{
Data: map[string]interface{}{
"value": value,
},
}, nil
}
func (b *backend) pathKVDelete(req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
if err := req.Storage.Delete(req.Path); err != nil {
return nil, err
}
return nil, nil
}
func (b *backend) pathKVList(req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
vals, err := req.Storage.List("kv/")
if err != nil {
return nil, err
}
return logical.ListResponse(vals), nil
}