mirror of
https://github.com/optim-enterprises-bv/vault.git
synced 2025-11-01 19:17:58 +00:00
physical: Factory constructor style for backends
This commit is contained in:
@@ -1,17 +1,14 @@
|
|||||||
package physical
|
package physical
|
||||||
|
|
||||||
import "github.com/hashicorp/consul/api"
|
|
||||||
|
|
||||||
// ConsulBackend is a physical backend that stores data at specific
|
// ConsulBackend is a physical backend that stores data at specific
|
||||||
// prefix within Consul. It is used for most production situations as
|
// prefix within Consul. It is used for most production situations as
|
||||||
// it allows Vault to run on multiple machines in a highly-available manner.
|
// it allows Vault to run on multiple machines in a highly-available manner.
|
||||||
type ConsulBackend struct {
|
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.
|
// 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
|
// TODO
|
||||||
c := &ConsulBackend{}
|
return nil, nil
|
||||||
return c, nil
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,9 +6,8 @@ package physical
|
|||||||
type FileBackend struct {
|
type FileBackend struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewFileBackend constructs a Filebackend using the given directory
|
// newFileBackend constructs a Filebackend using the given directory
|
||||||
func NewFileBackend(dir string) (*FileBackend, error) {
|
func newFileBackend(conf map[string]string) (Backend, error) {
|
||||||
// TODO:
|
// TODO:
|
||||||
f := &FileBackend{}
|
return nil, nil
|
||||||
return f, nil
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ type InmemBackend struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// NewInmem constructs a new in-memory backend
|
// NewInmem constructs a new in-memory backend
|
||||||
func NewInmem() *InmemBackend {
|
func newInmem() *InmemBackend {
|
||||||
in := &InmemBackend{
|
in := &InmemBackend{
|
||||||
root: radix.New(),
|
root: radix.New(),
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ package physical
|
|||||||
import "testing"
|
import "testing"
|
||||||
|
|
||||||
func TestInmem(t *testing.T) {
|
func TestInmem(t *testing.T) {
|
||||||
inm := NewInmem()
|
inm := newInmem()
|
||||||
testBackend(t, inm)
|
testBackend(t, inm)
|
||||||
testBackend_ListPrefix(t, inm)
|
testBackend_ListPrefix(t, inm)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
package physical
|
package physical
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
// Backend is the interface required for a physical
|
// Backend is the interface required for a physical
|
||||||
// backend. A physical backend is used to durably store
|
// backend. A physical backend is used to durably store
|
||||||
// datd outside of Vault. As such, it is completely untrusted,
|
// datd outside of Vault. As such, it is completely untrusted,
|
||||||
@@ -26,3 +28,26 @@ type Entry struct {
|
|||||||
Key string
|
Key string
|
||||||
Value []byte
|
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"
|
"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) {
|
func testBackend(t *testing.T, b Backend) {
|
||||||
// Should be empty
|
// Should be empty
|
||||||
keys, err := b.List("")
|
keys, err := b.List("")
|
||||||
|
|||||||
Reference in New Issue
Block a user