From 4548593f9a650f53b5aaa0df435382f628f79860 Mon Sep 17 00:00:00 2001 From: Calvin Leung Huang Date: Thu, 3 Aug 2017 17:37:39 -0400 Subject: [PATCH] Update mock-plugin --- logical/plugin/backend_test.go | 18 ++--- logical/plugin/mock/backend.go | 2 +- logical/plugin/mock/backend_test.go | 2 +- logical/plugin/mock/mock-plugin/main.go | 5 +- logical/plugin/mock/path_internal.go | 8 ++- logical/plugin/mock/path_kv.go | 96 +++++++++++++++++++++++++ logical/plugin/mock/path_testing.go | 56 --------------- 7 files changed, 117 insertions(+), 70 deletions(-) create mode 100644 logical/plugin/mock/path_kv.go delete mode 100644 logical/plugin/mock/path_testing.go diff --git a/logical/plugin/backend_test.go b/logical/plugin/backend_test.go index 910c1c6938d7..deb5b635b863 100644 --- a/logical/plugin/backend_test.go +++ b/logical/plugin/backend_test.go @@ -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'") } } diff --git a/logical/plugin/mock/backend.go b/logical/plugin/mock/backend.go index 50fe9de1d6b2..fc49f2078eb3 100644 --- a/logical/plugin/mock/backend.go +++ b/logical/plugin/mock/backend.go @@ -39,7 +39,7 @@ func Backend() *backend { b.Backend = &framework.Backend{ Help: "", Paths: []*framework.Path{ - pathTesting(&b), + pathKV(&b), pathInternal(&b), }, PathsSpecial: &logical.Paths{ diff --git a/logical/plugin/mock/backend_test.go b/logical/plugin/mock/backend_test.go index 81cf7dba35cb..075911ccd093 100644 --- a/logical/plugin/mock/backend_test.go +++ b/logical/plugin/mock/backend_test.go @@ -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) } diff --git a/logical/plugin/mock/mock-plugin/main.go b/logical/plugin/mock/mock-plugin/main.go index d8af7db615d3..1cb47bfcfdde 100644 --- a/logical/plugin/mock/mock-plugin/main.go +++ b/logical/plugin/mock/mock-plugin/main.go @@ -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 { diff --git a/logical/plugin/mock/path_internal.go b/logical/plugin/mock/path_internal.go index b59b104941ad..723ecb653c22 100644 --- a/logical/plugin/mock/path_internal.go +++ b/logical/plugin/mock/path_internal.go @@ -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{ diff --git a/logical/plugin/mock/path_kv.go b/logical/plugin/mock/path_kv.go new file mode 100644 index 000000000000..47883d8ebbcc --- /dev/null +++ b/logical/plugin/mock/path_kv.go @@ -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 +} diff --git a/logical/plugin/mock/path_testing.go b/logical/plugin/mock/path_testing.go deleted file mode 100644 index b5374606a006..000000000000 --- a/logical/plugin/mock/path_testing.go +++ /dev/null @@ -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 -}