2015-03-02 18:48:53 +00:00
|
|
|
package physical
|
|
|
|
|
2015-03-05 21:47:10 +00:00
|
|
|
import "fmt"
|
|
|
|
|
2015-03-02 18:48:53 +00:00
|
|
|
// Backend is the interface required for a physical
|
|
|
|
// backend. A physical backend is used to durably store
|
2015-04-22 21:59:16 +00:00
|
|
|
// data outside of Vault. As such, it is completely untrusted,
|
2015-03-02 18:48:53 +00:00
|
|
|
// and is only accessed via a security barrier. The backends
|
|
|
|
// must represent keys in a hierarchical manner. All methods
|
|
|
|
// are expected to be thread safe.
|
|
|
|
type Backend interface {
|
|
|
|
// Put is used to insert or update an entry
|
|
|
|
Put(entry *Entry) error
|
|
|
|
|
|
|
|
// Get is used to fetch an entry
|
|
|
|
Get(key string) (*Entry, error)
|
|
|
|
|
|
|
|
// Delete is used to permanently delete an entry
|
|
|
|
Delete(key string) error
|
|
|
|
|
|
|
|
// List is used ot list all the keys under a given
|
|
|
|
// prefix, up to the next prefix.
|
|
|
|
List(prefix string) ([]string, error)
|
|
|
|
}
|
|
|
|
|
2015-04-14 18:49:46 +00:00
|
|
|
// HABackend is an extentions to the standard physical
|
|
|
|
// backend to support high-availability. Vault only expects to
|
|
|
|
// use mutual exclusion to allow multiple instances to act as a
|
|
|
|
// hot standby for a leader that services all requests.
|
|
|
|
type HABackend interface {
|
|
|
|
// LockWith is used for mutual exclusion based on the given key.
|
2015-04-14 23:36:53 +00:00
|
|
|
LockWith(key, value string) (Lock, error)
|
2015-04-14 18:49:46 +00:00
|
|
|
}
|
|
|
|
|
2015-05-02 22:34:29 +00:00
|
|
|
// AdvertiseDetect is an optional interface that an HABackend
|
|
|
|
// can implement. If they do, an advertise address can be automatically
|
|
|
|
// detected.
|
|
|
|
type AdvertiseDetect interface {
|
|
|
|
// DetectHostAddr is used to detect the host address
|
|
|
|
DetectHostAddr() (string, error)
|
|
|
|
}
|
|
|
|
|
2015-04-14 18:49:46 +00:00
|
|
|
type Lock interface {
|
|
|
|
// Lock is used to acquire the given lock
|
|
|
|
// The stopCh is optional and if closed should interrupt the lock
|
|
|
|
// acquisition attempt. The return struct should be closed when
|
|
|
|
// leadership is lost.
|
|
|
|
Lock(stopCh <-chan struct{}) (<-chan struct{}, error)
|
|
|
|
|
|
|
|
// Unlock is used to release the lock
|
|
|
|
Unlock() error
|
2015-04-14 23:36:53 +00:00
|
|
|
|
|
|
|
// Returns the value of the lock and if it is held
|
|
|
|
Value() (bool, string, error)
|
2015-04-14 18:49:46 +00:00
|
|
|
}
|
|
|
|
|
2015-03-02 18:48:53 +00:00
|
|
|
// Entry is used to represent data stored by the physical backend
|
|
|
|
type Entry struct {
|
|
|
|
Key string
|
|
|
|
Value []byte
|
|
|
|
}
|
2015-03-05 21:47:10 +00:00
|
|
|
|
|
|
|
// 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) {
|
2015-03-05 21:57:30 +00:00
|
|
|
return NewInmem(), nil
|
2015-03-05 21:47:10 +00:00
|
|
|
},
|
2015-05-05 20:08:42 +00:00
|
|
|
"consul": newConsulBackend,
|
|
|
|
"zookeeper": newZookeeperBackend,
|
|
|
|
"file": newFileBackend,
|
2015-05-20 14:54:26 +00:00
|
|
|
"s3": newS3Backend,
|
2015-05-24 16:38:49 +00:00
|
|
|
"etcd": newEtcdBackend,
|
2015-03-05 21:47:10 +00:00
|
|
|
}
|