open-vault/logical/plugin/mock/path_kv.go

105 lines
2.7 KiB
Go
Raw Normal View History

2017-08-04 15:43:36 +00:00
package mock
import (
"context"
2017-08-04 15:43:36 +00:00
"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
2017-08-04 15:43:36 +00:00
// 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,
},
2017-08-04 15:43:36 +00:00
},
&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,
},
2017-08-04 15:43:36 +00:00
},
}
}
func (b *backend) pathExistenceCheck(ctx context.Context, req *logical.Request, data *framework.FieldData) (bool, error) {
2017-08-04 15:43:36 +00:00
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(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
2017-08-04 15:43:36 +00:00
entry, err := req.Storage.Get(req.Path)
if err != nil {
return nil, err
}
if entry == nil {
return nil, nil
}
value := string(entry.Value)
b.Logger().Info("reading value", "key", req.Path, "value", value)
2017-08-04 15:43:36 +00:00
// Return the secret
return &logical.Response{
Data: map[string]interface{}{
"value": value,
},
}, nil
}
func (b *backend) pathKVCreateUpdate(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
2017-08-04 15:43:36 +00:00
value := data.Get("value").(string)
b.Logger().Info("storing value", "key", req.Path, "value", value)
2017-08-04 15:43:36 +00:00
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(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
2017-08-04 15:43:36 +00:00
if err := req.Storage.Delete(req.Path); err != nil {
return nil, err
}
return nil, nil
}
func (b *backend) pathKVList(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
2017-08-04 15:43:36 +00:00
vals, err := req.Storage.List("kv/")
if err != nil {
return nil, err
}
return logical.ListResponse(vals), nil
}