mirror of
https://github.com/optim-enterprises-bv/vault.git
synced 2025-10-30 18:17:55 +00:00
physical: Factory constructor style for backends
This commit is contained in:
@@ -1,17 +1,14 @@
|
||||
package physical
|
||||
|
||||
import "github.com/hashicorp/consul/api"
|
||||
|
||||
// ConsulBackend is a physical backend that stores data at specific
|
||||
// prefix within Consul. It is used for most production situations as
|
||||
// it allows Vault to run on multiple machines in a highly-available manner.
|
||||
type ConsulBackend struct {
|
||||
}
|
||||
|
||||
// NewConsulBackend constructs a Consul backend using the given API client
|
||||
// newConsulBackend constructs a Consul backend using the given API client
|
||||
// and the prefix in the KV store.
|
||||
func NewConsulBackend(client *api.Client, prefix string) (*ConsulBackend, error) {
|
||||
func newConsulBackend(conf map[string]string) (Backend, error) {
|
||||
// TODO
|
||||
c := &ConsulBackend{}
|
||||
return c, nil
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
@@ -6,9 +6,8 @@ package physical
|
||||
type FileBackend struct {
|
||||
}
|
||||
|
||||
// NewFileBackend constructs a Filebackend using the given directory
|
||||
func NewFileBackend(dir string) (*FileBackend, error) {
|
||||
// newFileBackend constructs a Filebackend using the given directory
|
||||
func newFileBackend(conf map[string]string) (Backend, error) {
|
||||
// TODO:
|
||||
f := &FileBackend{}
|
||||
return f, nil
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
@@ -16,7 +16,7 @@ type InmemBackend struct {
|
||||
}
|
||||
|
||||
// NewInmem constructs a new in-memory backend
|
||||
func NewInmem() *InmemBackend {
|
||||
func newInmem() *InmemBackend {
|
||||
in := &InmemBackend{
|
||||
root: radix.New(),
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@ package physical
|
||||
import "testing"
|
||||
|
||||
func TestInmem(t *testing.T) {
|
||||
inm := NewInmem()
|
||||
inm := newInmem()
|
||||
testBackend(t, inm)
|
||||
testBackend_ListPrefix(t, inm)
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package physical
|
||||
|
||||
import "fmt"
|
||||
|
||||
// Backend is the interface required for a physical
|
||||
// backend. A physical backend is used to durably store
|
||||
// datd outside of Vault. As such, it is completely untrusted,
|
||||
@@ -26,3 +28,26 @@ type Entry struct {
|
||||
Key string
|
||||
Value []byte
|
||||
}
|
||||
|
||||
// Factory is the factory function to create a physical backend.
|
||||
type Factory func(map[string]string) (Backend, error)
|
||||
|
||||
// NewBackend returns a new Bckend with the given type and configuration.
|
||||
// The backend is looked up in the BuiltinBackends variable.
|
||||
func NewBackend(t string, conf map[string]string) (Backend, error) {
|
||||
f, ok := BuiltinBackends[t]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("unknown physical backend type: %s", t)
|
||||
}
|
||||
return f(conf)
|
||||
}
|
||||
|
||||
// BuiltinBackends is the list of built-in physical backends that can
|
||||
// be used with NewBackend.
|
||||
var BuiltinBackends = map[string]Factory{
|
||||
"inmem": func(map[string]string) (Backend, error) {
|
||||
return newInmem(), nil
|
||||
},
|
||||
"consul": newConsulBackend,
|
||||
"file": newFileBackend,
|
||||
}
|
||||
|
||||
@@ -5,6 +5,22 @@ import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func testNewBackend(t *testing.T) {
|
||||
_, err := NewBackend("foobar", nil)
|
||||
if err == nil {
|
||||
t.Fatalf("expected error")
|
||||
}
|
||||
|
||||
b, err := NewBackend("inmem", nil)
|
||||
if err != nil {
|
||||
t.Fatalf("err: %v", err)
|
||||
}
|
||||
|
||||
if b == nil {
|
||||
t.Fatalf("expected backend")
|
||||
}
|
||||
}
|
||||
|
||||
func testBackend(t *testing.T, b Backend) {
|
||||
// Should be empty
|
||||
keys, err := b.List("")
|
||||
|
||||
Reference in New Issue
Block a user