Update mock-plugin (#3107)
This commit is contained in:
parent
1e3c74862e
commit
0871e7eadb
|
@ -21,14 +21,16 @@ func TestBackendPlugin_HandleRequest(t *testing.T) {
|
|||
defer cleanup()
|
||||
|
||||
resp, err := b.HandleRequest(&logical.Request{
|
||||
Operation: logical.ReadOperation,
|
||||
Path: "test/ing",
|
||||
Data: map[string]interface{}{"value": "foo"},
|
||||
Operation: logical.CreateOperation,
|
||||
Path: "kv/foo",
|
||||
Data: map[string]interface{}{
|
||||
"value": "bar",
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if resp.Data["value"] != "foo" {
|
||||
if resp.Data["value"] != "bar" {
|
||||
t.Fatalf("bad: %#v", resp)
|
||||
}
|
||||
}
|
||||
|
@ -76,17 +78,17 @@ func TestBackendPlugin_HandleExistenceCheck(t *testing.T) {
|
|||
|
||||
checkFound, exists, err := b.HandleExistenceCheck(&logical.Request{
|
||||
Operation: logical.CreateOperation,
|
||||
Path: "test/ing",
|
||||
Data: map[string]interface{}{"value": "foo"},
|
||||
Path: "kv/foo",
|
||||
Data: map[string]interface{}{"value": "bar"},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !checkFound {
|
||||
t.Fatal("existence check not found for path 'test/ing'")
|
||||
t.Fatal("existence check not found for path 'kv/foo")
|
||||
}
|
||||
if exists {
|
||||
t.Fatal("existence check should have returned 'false' for 'testing/read'")
|
||||
t.Fatal("existence check should have returned 'false' for 'kv/foo'")
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -39,7 +39,7 @@ func Backend() *backend {
|
|||
b.Backend = &framework.Backend{
|
||||
Help: "",
|
||||
Paths: []*framework.Path{
|
||||
pathTesting(&b),
|
||||
pathKV(&b),
|
||||
pathInternal(&b),
|
||||
},
|
||||
PathsSpecial: &logical.Paths{
|
||||
|
|
|
@ -6,6 +6,6 @@ import (
|
|||
"github.com/hashicorp/vault/logical"
|
||||
)
|
||||
|
||||
func TestMockBackend_impl(t *testing.T) {
|
||||
func TestBackend_impl(t *testing.T) {
|
||||
var _ logical.Backend = new(backend)
|
||||
}
|
||||
|
|
|
@ -5,6 +5,7 @@ import (
|
|||
"os"
|
||||
|
||||
"github.com/hashicorp/vault/helper/pluginutil"
|
||||
"github.com/hashicorp/vault/logical"
|
||||
"github.com/hashicorp/vault/logical/plugin"
|
||||
"github.com/hashicorp/vault/logical/plugin/mock"
|
||||
)
|
||||
|
@ -17,8 +18,10 @@ func main() {
|
|||
tlsConfig := apiClientMeta.GetTLSConfig()
|
||||
tlsProviderFunc := pluginutil.VaultPluginTLSProvider(tlsConfig)
|
||||
|
||||
factoryFunc := mock.FactoryType(logical.TypeLogical)
|
||||
|
||||
err := plugin.Serve(&plugin.ServeOpts{
|
||||
BackendFactoryFunc: mock.Factory,
|
||||
BackendFactoryFunc: factoryFunc,
|
||||
TLSProviderFunc: tlsProviderFunc,
|
||||
})
|
||||
if err != nil {
|
||||
|
|
|
@ -5,18 +5,20 @@ import (
|
|||
"github.com/hashicorp/vault/logical/framework"
|
||||
)
|
||||
|
||||
// pathInternal is used to test viewing internal backend values. In this case,
|
||||
// it is used to test the invalidate func.
|
||||
func pathInternal(b *backend) *framework.Path {
|
||||
return &framework.Path{
|
||||
Pattern: "internal",
|
||||
Fields: map[string]*framework.FieldSchema{},
|
||||
ExistenceCheck: b.pathTestingExistenceCheck,
|
||||
ExistenceCheck: b.pathExistenceCheck,
|
||||
Callbacks: map[logical.Operation]framework.OperationFunc{
|
||||
logical.ReadOperation: b.pathTestingReadInternal,
|
||||
logical.ReadOperation: b.pathInternalRead,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (b *backend) pathTestingReadInternal(
|
||||
func (b *backend) pathInternalRead(
|
||||
req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
|
||||
// Return the secret
|
||||
return &logical.Response{
|
||||
|
|
|
@ -0,0 +1,96 @@
|
|||
package mock
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/hashicorp/vault/logical"
|
||||
"github.com/hashicorp/vault/logical/framework"
|
||||
)
|
||||
|
||||
// pathKV is used to test CRUD and List operations. It is a simplified
|
||||
// version of the passthrough backend that only accepts string values.
|
||||
func pathKV(b *backend) *framework.Path {
|
||||
return &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,
|
||||
logical.ListOperation: b.pathKVList,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
}
|
|
@ -1,56 +0,0 @@
|
|||
package mock
|
||||
|
||||
import (
|
||||
"github.com/hashicorp/vault/logical"
|
||||
"github.com/hashicorp/vault/logical/framework"
|
||||
)
|
||||
|
||||
func pathTesting(b *backend) *framework.Path {
|
||||
return &framework.Path{
|
||||
Pattern: "test/ing",
|
||||
Fields: map[string]*framework.FieldSchema{
|
||||
"value": &framework.FieldSchema{Type: framework.TypeString},
|
||||
},
|
||||
ExistenceCheck: b.pathTestingExistenceCheck,
|
||||
Callbacks: map[logical.Operation]framework.OperationFunc{
|
||||
logical.ReadOperation: b.pathTestingRead,
|
||||
logical.CreateOperation: b.pathTestingCreate,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (b *backend) pathTestingRead(
|
||||
req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
|
||||
// Return the secret
|
||||
return &logical.Response{
|
||||
Data: map[string]interface{}{
|
||||
"value": data.Get("value"),
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (b *backend) pathTestingCreate(
|
||||
req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
|
||||
val := data.Get("value").(string)
|
||||
|
||||
entry := &logical.StorageEntry{
|
||||
Key: "test/ing",
|
||||
Value: []byte(val),
|
||||
}
|
||||
|
||||
s := req.Storage
|
||||
err := s.Put(entry)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &logical.Response{
|
||||
Data: map[string]interface{}{
|
||||
"value": data.Get("value"),
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (b *backend) pathTestingExistenceCheck(req *logical.Request, data *framework.FieldData) (bool, error) {
|
||||
return false, nil
|
||||
}
|
Loading…
Reference in New Issue