mirror of
https://github.com/optim-enterprises-bv/vault.git
synced 2025-11-03 12:07:54 +00:00
Update mock-plugin (#3107)
This commit is contained in:
committed by
GitHub
parent
bc0954923c
commit
663185c8b3
@@ -21,14 +21,16 @@ func TestBackendPlugin_HandleRequest(t *testing.T) {
|
|||||||
defer cleanup()
|
defer cleanup()
|
||||||
|
|
||||||
resp, err := b.HandleRequest(&logical.Request{
|
resp, err := b.HandleRequest(&logical.Request{
|
||||||
Operation: logical.ReadOperation,
|
Operation: logical.CreateOperation,
|
||||||
Path: "test/ing",
|
Path: "kv/foo",
|
||||||
Data: map[string]interface{}{"value": "foo"},
|
Data: map[string]interface{}{
|
||||||
|
"value": "bar",
|
||||||
|
},
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
if resp.Data["value"] != "foo" {
|
if resp.Data["value"] != "bar" {
|
||||||
t.Fatalf("bad: %#v", resp)
|
t.Fatalf("bad: %#v", resp)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -76,17 +78,17 @@ func TestBackendPlugin_HandleExistenceCheck(t *testing.T) {
|
|||||||
|
|
||||||
checkFound, exists, err := b.HandleExistenceCheck(&logical.Request{
|
checkFound, exists, err := b.HandleExistenceCheck(&logical.Request{
|
||||||
Operation: logical.CreateOperation,
|
Operation: logical.CreateOperation,
|
||||||
Path: "test/ing",
|
Path: "kv/foo",
|
||||||
Data: map[string]interface{}{"value": "foo"},
|
Data: map[string]interface{}{"value": "bar"},
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
if !checkFound {
|
if !checkFound {
|
||||||
t.Fatal("existence check not found for path 'test/ing'")
|
t.Fatal("existence check not found for path 'kv/foo")
|
||||||
}
|
}
|
||||||
if exists {
|
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{
|
b.Backend = &framework.Backend{
|
||||||
Help: "",
|
Help: "",
|
||||||
Paths: []*framework.Path{
|
Paths: []*framework.Path{
|
||||||
pathTesting(&b),
|
pathKV(&b),
|
||||||
pathInternal(&b),
|
pathInternal(&b),
|
||||||
},
|
},
|
||||||
PathsSpecial: &logical.Paths{
|
PathsSpecial: &logical.Paths{
|
||||||
|
|||||||
@@ -6,6 +6,6 @@ import (
|
|||||||
"github.com/hashicorp/vault/logical"
|
"github.com/hashicorp/vault/logical"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestMockBackend_impl(t *testing.T) {
|
func TestBackend_impl(t *testing.T) {
|
||||||
var _ logical.Backend = new(backend)
|
var _ logical.Backend = new(backend)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
|
|
||||||
"github.com/hashicorp/vault/helper/pluginutil"
|
"github.com/hashicorp/vault/helper/pluginutil"
|
||||||
|
"github.com/hashicorp/vault/logical"
|
||||||
"github.com/hashicorp/vault/logical/plugin"
|
"github.com/hashicorp/vault/logical/plugin"
|
||||||
"github.com/hashicorp/vault/logical/plugin/mock"
|
"github.com/hashicorp/vault/logical/plugin/mock"
|
||||||
)
|
)
|
||||||
@@ -17,8 +18,10 @@ func main() {
|
|||||||
tlsConfig := apiClientMeta.GetTLSConfig()
|
tlsConfig := apiClientMeta.GetTLSConfig()
|
||||||
tlsProviderFunc := pluginutil.VaultPluginTLSProvider(tlsConfig)
|
tlsProviderFunc := pluginutil.VaultPluginTLSProvider(tlsConfig)
|
||||||
|
|
||||||
|
factoryFunc := mock.FactoryType(logical.TypeLogical)
|
||||||
|
|
||||||
err := plugin.Serve(&plugin.ServeOpts{
|
err := plugin.Serve(&plugin.ServeOpts{
|
||||||
BackendFactoryFunc: mock.Factory,
|
BackendFactoryFunc: factoryFunc,
|
||||||
TLSProviderFunc: tlsProviderFunc,
|
TLSProviderFunc: tlsProviderFunc,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -5,18 +5,20 @@ import (
|
|||||||
"github.com/hashicorp/vault/logical/framework"
|
"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 {
|
func pathInternal(b *backend) *framework.Path {
|
||||||
return &framework.Path{
|
return &framework.Path{
|
||||||
Pattern: "internal",
|
Pattern: "internal",
|
||||||
Fields: map[string]*framework.FieldSchema{},
|
Fields: map[string]*framework.FieldSchema{},
|
||||||
ExistenceCheck: b.pathTestingExistenceCheck,
|
ExistenceCheck: b.pathExistenceCheck,
|
||||||
Callbacks: map[logical.Operation]framework.OperationFunc{
|
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) {
|
req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
|
||||||
// Return the secret
|
// Return the secret
|
||||||
return &logical.Response{
|
return &logical.Response{
|
||||||
|
|||||||
96
logical/plugin/mock/path_kv.go
Normal file
96
logical/plugin/mock/path_kv.go
Normal file
@@ -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
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user