helper/backend: use logical package

This commit is contained in:
Mitchell Hashimoto 2015-03-15 14:57:19 -07:00
parent 69f62d8872
commit 0496a0837a
3 changed files with 48 additions and 48 deletions

View file

@ -8,15 +8,15 @@ import (
"sync"
"text/template"
"github.com/hashicorp/vault/vault"
"github.com/hashicorp/vault/logical"
"github.com/mitchellh/go-wordwrap"
)
// Backend is an implementation of vault.LogicalBackend that allows
// Backend is an implementation of logical.Backend that allows
// the implementer to code a backend using a much more programmer-friendly
// framework that handles a lot of the routing and validation for you.
//
// This is recommended over implementing vault.LogicalBackend directly.
// This is recommended over implementing logical.Backend directly.
type Backend struct {
// Paths are the various routes that the backend responds to.
// This cannot be modified after construction (i.e. dynamically changing
@ -54,13 +54,13 @@ type Path struct {
// Callbacks are the set of callbacks that are called for a given
// operation. If a callback for a specific operation is not present,
// then vault.ErrUnsupportedOperation is automatically generated.
// then logical.ErrUnsupportedOperation is automatically generated.
//
// The help operation is the only operation that the Path will
// automatically handle if the Help field is set. If both the Help
// field is set and there is a callback registered here, then the
// callback will be called.
Callbacks map[vault.Operation]OperationFunc
Callbacks map[logical.Operation]OperationFunc
// Help is text describing how to use this path. This will be used
// to auto-generate the help operation. The Path will automatically
@ -78,14 +78,14 @@ type Path struct {
}
// OperationFunc is the callback called for an operation on a path.
type OperationFunc func(*vault.Request, *FieldData) (*vault.Response, error)
type OperationFunc func(*logical.Request, *FieldData) (*logical.Response, error)
// vault.LogicalBackend impl.
func (b *Backend) HandleRequest(req *vault.Request) (*vault.Response, error) {
// logical.Backend impl.
func (b *Backend) HandleRequest(req *logical.Request) (*logical.Response, error) {
// Find the matching route
path, captures := b.route(req.Path)
if path == nil {
return nil, vault.ErrUnsupportedPath
return nil, logical.ErrUnsupportedPath
}
// Build up the data for the route, with the URL taking priority
@ -105,13 +105,13 @@ func (b *Backend) HandleRequest(req *vault.Request) (*vault.Response, error) {
callback, ok = path.Callbacks[req.Operation]
}
if !ok {
if req.Operation == vault.HelpOperation && path.HelpSynopsis != "" {
if req.Operation == logical.HelpOperation && path.HelpSynopsis != "" {
callback = path.helpCallback
ok = true
}
}
if !ok {
return nil, vault.ErrUnsupportedOperation
return nil, logical.ErrUnsupportedOperation
}
// Call the callback with the request and the data
@ -121,7 +121,7 @@ func (b *Backend) HandleRequest(req *vault.Request) (*vault.Response, error) {
})
}
// vault.LogicalBackend impl.
// logical.Backend impl.
func (b *Backend) RootPaths() []string {
// TODO
return nil
@ -168,7 +168,7 @@ func (b *Backend) route(path string) (*Path, map[string]string) {
return nil, nil
}
func (p *Path) helpCallback(req *vault.Request, data *FieldData) (*vault.Response, error) {
func (p *Path) helpCallback(req *logical.Request, data *FieldData) (*logical.Response, error) {
var tplData pathTemplateData
tplData.Request = req.Path
tplData.RoutePattern = p.Pattern
@ -210,7 +210,7 @@ func (p *Path) helpCallback(req *vault.Request, data *FieldData) (*vault.Respons
return nil, fmt.Errorf("error executing template: %s", err)
}
return vault.HelpResponse(buf.String(), nil), nil
return logical.HelpResponse(buf.String(), nil), nil
}
// FieldSchema is a basic schema to describe the format of a path field.

View file

@ -4,7 +4,7 @@ import (
"reflect"
"testing"
"github.com/hashicorp/vault/vault"
"github.com/hashicorp/vault/logical"
)
func BenchmarkBackendRoute(b *testing.B) {
@ -36,12 +36,12 @@ func BenchmarkBackendRoute(b *testing.B) {
}
func TestBackend_impl(t *testing.T) {
var _ vault.LogicalBackend = new(Backend)
var _ logical.Backend = new(Backend)
}
func TestBackendHandleRequest(t *testing.T) {
callback := func(req *vault.Request, data *FieldData) (*vault.Response, error) {
return &vault.Response{
callback := func(req *logical.Request, data *FieldData) (*logical.Response, error) {
return &logical.Response{
Data: map[string]interface{}{
"value": data.Get("value"),
},
@ -55,15 +55,15 @@ func TestBackendHandleRequest(t *testing.T) {
Fields: map[string]*FieldSchema{
"value": &FieldSchema{Type: TypeInt},
},
Callbacks: map[vault.Operation]OperationFunc{
vault.ReadOperation: callback,
Callbacks: map[logical.Operation]OperationFunc{
logical.ReadOperation: callback,
},
},
},
}
resp, err := b.HandleRequest(&vault.Request{
Operation: vault.ReadOperation,
resp, err := b.HandleRequest(&logical.Request{
Operation: logical.ReadOperation,
Path: "foo/bar",
Data: map[string]interface{}{"value": "42"},
})
@ -76,8 +76,8 @@ func TestBackendHandleRequest(t *testing.T) {
}
func TestBackendHandleRequest_404(t *testing.T) {
callback := func(req *vault.Request, data *FieldData) (*vault.Response, error) {
return &vault.Response{
callback := func(req *logical.Request, data *FieldData) (*logical.Response, error) {
return &logical.Response{
Data: map[string]interface{}{
"value": data.Get("value"),
},
@ -91,19 +91,19 @@ func TestBackendHandleRequest_404(t *testing.T) {
Fields: map[string]*FieldSchema{
"value": &FieldSchema{Type: TypeInt},
},
Callbacks: map[vault.Operation]OperationFunc{
vault.ReadOperation: callback,
Callbacks: map[logical.Operation]OperationFunc{
logical.ReadOperation: callback,
},
},
},
}
_, err := b.HandleRequest(&vault.Request{
Operation: vault.ReadOperation,
_, err := b.HandleRequest(&logical.Request{
Operation: logical.ReadOperation,
Path: "foo/baz",
Data: map[string]interface{}{"value": "84"},
})
if err != vault.ErrUnsupportedPath {
if err != logical.ErrUnsupportedPath {
t.Fatalf("err: %s", err)
}
}
@ -122,8 +122,8 @@ func TestBackendHandleRequest_help(t *testing.T) {
},
}
resp, err := b.HandleRequest(&vault.Request{
Operation: vault.HelpOperation,
resp, err := b.HandleRequest(&logical.Request{
Operation: logical.HelpOperation,
Path: "foo/bar",
Data: map[string]interface{}{"value": "42"},
})
@ -136,8 +136,8 @@ func TestBackendHandleRequest_help(t *testing.T) {
}
func TestBackendHandleRequest_unsupportedOperation(t *testing.T) {
callback := func(req *vault.Request, data *FieldData) (*vault.Response, error) {
return &vault.Response{
callback := func(req *logical.Request, data *FieldData) (*logical.Response, error) {
return &logical.Response{
Data: map[string]interface{}{
"value": data.Get("value"),
},
@ -151,26 +151,26 @@ func TestBackendHandleRequest_unsupportedOperation(t *testing.T) {
Fields: map[string]*FieldSchema{
"value": &FieldSchema{Type: TypeInt},
},
Callbacks: map[vault.Operation]OperationFunc{
vault.ReadOperation: callback,
Callbacks: map[logical.Operation]OperationFunc{
logical.ReadOperation: callback,
},
},
},
}
_, err := b.HandleRequest(&vault.Request{
Operation: vault.WriteOperation,
_, err := b.HandleRequest(&logical.Request{
Operation: logical.WriteOperation,
Path: "foo/bar",
Data: map[string]interface{}{"value": "84"},
})
if err != vault.ErrUnsupportedOperation {
if err != logical.ErrUnsupportedOperation {
t.Fatalf("err: %s", err)
}
}
func TestBackendHandleRequest_urlPriority(t *testing.T) {
callback := func(req *vault.Request, data *FieldData) (*vault.Response, error) {
return &vault.Response{
callback := func(req *logical.Request, data *FieldData) (*logical.Response, error) {
return &logical.Response{
Data: map[string]interface{}{
"value": data.Get("value"),
},
@ -184,15 +184,15 @@ func TestBackendHandleRequest_urlPriority(t *testing.T) {
Fields: map[string]*FieldSchema{
"value": &FieldSchema{Type: TypeInt},
},
Callbacks: map[vault.Operation]OperationFunc{
vault.ReadOperation: callback,
Callbacks: map[logical.Operation]OperationFunc{
logical.ReadOperation: callback,
},
},
},
}
resp, err := b.HandleRequest(&vault.Request{
Operation: vault.ReadOperation,
resp, err := b.HandleRequest(&logical.Request{
Operation: logical.ReadOperation,
Path: "foo/42",
Data: map[string]interface{}{"value": "84"},
})

View file

@ -5,7 +5,7 @@ import (
"os"
"testing"
"github.com/hashicorp/vault/vault"
"github.com/hashicorp/vault/logical"
)
// TestEnvVar must be set to a non-empty value for acceptance tests to run.
@ -21,7 +21,7 @@ type TestCase struct {
PreCheck func()
// Backend is the backend that will be mounted.
Backend vault.LogicalBackend
Backend logical.Backend
// Steps are the set of operations that are run for this test case.
Steps []TestStep
@ -36,7 +36,7 @@ type TestCase struct {
// TestStep is a single step within a TestCase.
type TestStep struct {
// Operation is the operation to execute
Operation vault.Operation
Operation logical.Operation
// Path is the request path. The mount prefix will be automatically added.
Path string
@ -51,7 +51,7 @@ type TestStep struct {
}
// TestCheckFunc is the callback used for Check in TestStep.
type TestCheckFunc func(*vault.Response) error
type TestCheckFunc func(*logical.Response) error
// TestTeardownFunc is the callback used for Teardown in TestCase.
type TestTeardownFunc func() error