2017-08-04 15:43:36 +00:00
|
|
|
package mock
|
|
|
|
|
|
|
|
import (
|
2018-01-08 18:31:38 +00:00
|
|
|
"context"
|
2017-08-04 15:43:36 +00:00
|
|
|
|
2018-04-05 15:49:21 +00:00
|
|
|
"github.com/hashicorp/errwrap"
|
2019-04-12 21:54:35 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/framework"
|
2019-04-13 07:44:06 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/logical"
|
2017-08-04 15:43:36 +00:00
|
|
|
)
|
|
|
|
|
2017-08-16 02:10:32 +00:00
|
|
|
// 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.
|
2017-08-16 02:10:32 +00:00
|
|
|
func kvPaths(b *backend) []*framework.Path {
|
|
|
|
return []*framework.Path{
|
2021-04-08 16:43:39 +00:00
|
|
|
{
|
2017-08-16 02:10:32 +00:00
|
|
|
Pattern: "kv/?",
|
|
|
|
Callbacks: map[logical.Operation]framework.OperationFunc{
|
|
|
|
logical.ListOperation: b.pathKVList,
|
|
|
|
},
|
2017-08-04 15:43:36 +00:00
|
|
|
},
|
2021-04-08 16:43:39 +00:00
|
|
|
{
|
2017-08-16 02:10:32 +00:00
|
|
|
Pattern: "kv/" + framework.GenericNameRegex("key"),
|
|
|
|
Fields: map[string]*framework.FieldSchema{
|
2021-04-08 16:43:39 +00:00
|
|
|
"key": {Type: framework.TypeString},
|
|
|
|
"value": {Type: framework.TypeString},
|
|
|
|
"version": {Type: framework.TypeInt},
|
2017-08-16 02:10:32 +00:00
|
|
|
},
|
|
|
|
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
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-08 18:31:38 +00:00
|
|
|
func (b *backend) pathExistenceCheck(ctx context.Context, req *logical.Request, data *framework.FieldData) (bool, error) {
|
2018-01-19 06:44:44 +00:00
|
|
|
out, err := req.Storage.Get(ctx, req.Path)
|
2017-08-04 15:43:36 +00:00
|
|
|
if err != nil {
|
2018-04-05 15:49:21 +00:00
|
|
|
return false, errwrap.Wrapf("existence check failed: {{err}}", err)
|
2017-08-04 15:43:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return out != nil, nil
|
|
|
|
}
|
|
|
|
|
2018-01-08 18:31:38 +00:00
|
|
|
func (b *backend) pathKVRead(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
|
2018-02-21 22:36:53 +00:00
|
|
|
version := data.Get("version").(int)
|
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
entry, err := req.Storage.Get(ctx, req.Path)
|
2017-08-04 15:43:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if entry == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
value := string(entry.Value)
|
|
|
|
|
2018-01-18 21:49:20 +00:00
|
|
|
b.Logger().Info("reading value", "key", req.Path, "value", value)
|
2017-08-04 15:43:36 +00:00
|
|
|
// Return the secret
|
2018-02-21 22:36:53 +00:00
|
|
|
resp := &logical.Response{
|
2017-08-04 15:43:36 +00:00
|
|
|
Data: map[string]interface{}{
|
2018-02-21 22:36:53 +00:00
|
|
|
"value": value,
|
|
|
|
"version": version,
|
2017-08-04 15:43:36 +00:00
|
|
|
},
|
2018-02-21 22:36:53 +00:00
|
|
|
}
|
|
|
|
if version != 0 {
|
|
|
|
resp.Data["version"] = version
|
|
|
|
}
|
|
|
|
return resp, nil
|
2017-08-04 15:43:36 +00:00
|
|
|
}
|
|
|
|
|
2018-01-08 18:31:38 +00:00
|
|
|
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)
|
|
|
|
|
2018-01-18 21:49:20 +00:00
|
|
|
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
|
2018-01-19 06:44:44 +00:00
|
|
|
err := s.Put(ctx, entry)
|
2017-08-04 15:43:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &logical.Response{
|
|
|
|
Data: map[string]interface{}{
|
|
|
|
"value": value,
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2018-01-08 18:31:38 +00:00
|
|
|
func (b *backend) pathKVDelete(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
|
2018-01-19 06:44:44 +00:00
|
|
|
if err := req.Storage.Delete(ctx, req.Path); err != nil {
|
2017-08-04 15:43:36 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2018-01-08 18:31:38 +00:00
|
|
|
func (b *backend) pathKVList(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
|
2018-01-19 06:44:44 +00:00
|
|
|
vals, err := req.Storage.List(ctx, "kv/")
|
2017-08-04 15:43:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return logical.ListResponse(vals), nil
|
|
|
|
}
|