2015-05-05 20:08:42 +00:00
|
|
|
package physical
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2015-07-13 09:33:23 +00:00
|
|
|
"path/filepath"
|
2015-05-05 20:08:42 +00:00
|
|
|
"sort"
|
|
|
|
"strings"
|
2015-05-26 02:14:00 +00:00
|
|
|
"sync"
|
2015-05-05 20:08:42 +00:00
|
|
|
"time"
|
|
|
|
|
2016-08-19 20:45:17 +00:00
|
|
|
log "github.com/mgutz/logxi/v1"
|
|
|
|
|
2015-05-05 20:08:42 +00:00
|
|
|
"github.com/armon/go-metrics"
|
|
|
|
"github.com/samuel/go-zookeeper/zk"
|
|
|
|
)
|
|
|
|
|
2015-07-13 09:33:23 +00:00
|
|
|
const (
|
|
|
|
// ZKNodeFilePrefix is prefixed to any "files" in ZooKeeper,
|
|
|
|
// so that they do not collide with directory entries. Otherwise,
|
|
|
|
// we cannot delete a file if the path is a full-prefix of another
|
|
|
|
// key.
|
|
|
|
ZKNodeFilePrefix = "_"
|
|
|
|
)
|
|
|
|
|
2015-05-05 20:08:42 +00:00
|
|
|
// ZookeeperBackend is a physical backend that stores data at specific
|
|
|
|
// prefix within Zookeeper. It is used in production situations as
|
|
|
|
// it allows Vault to run on multiple machines in a highly-available manner.
|
|
|
|
type ZookeeperBackend struct {
|
2015-05-21 03:15:31 +00:00
|
|
|
path string
|
2015-05-05 20:08:42 +00:00
|
|
|
client *zk.Conn
|
2016-02-19 12:19:01 +00:00
|
|
|
acl []zk.ACL
|
2016-08-19 20:45:17 +00:00
|
|
|
logger log.Logger
|
2015-05-05 20:08:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// newZookeeperBackend constructs a Zookeeper backend using the given API client
|
|
|
|
// and the prefix in the KV store.
|
2016-08-19 20:45:17 +00:00
|
|
|
func newZookeeperBackend(conf map[string]string, logger log.Logger) (Backend, error) {
|
2015-05-05 20:08:42 +00:00
|
|
|
// Get the path in Zookeeper
|
2015-05-21 03:15:31 +00:00
|
|
|
path, ok := conf["path"]
|
2015-05-05 20:08:42 +00:00
|
|
|
if !ok {
|
2015-05-21 03:15:31 +00:00
|
|
|
path = "vault/"
|
2015-05-05 20:08:42 +00:00
|
|
|
}
|
|
|
|
|
2015-05-05 21:56:49 +00:00
|
|
|
// Ensure path is suffixed and prefixed (zk requires prefix /)
|
2015-05-21 03:15:31 +00:00
|
|
|
if !strings.HasSuffix(path, "/") {
|
|
|
|
path += "/"
|
2015-05-05 20:08:42 +00:00
|
|
|
}
|
2015-05-21 03:15:31 +00:00
|
|
|
if !strings.HasPrefix(path, "/") {
|
|
|
|
path = "/" + path
|
2015-05-05 20:08:42 +00:00
|
|
|
}
|
|
|
|
|
2015-05-06 18:08:08 +00:00
|
|
|
// Configure the client, default to localhost instance
|
2015-05-05 22:20:38 +00:00
|
|
|
var machines string
|
|
|
|
machines, ok = conf["address"]
|
|
|
|
if !ok {
|
2015-05-06 13:57:24 +00:00
|
|
|
machines = "localhost:2181"
|
2015-05-05 22:20:38 +00:00
|
|
|
}
|
2015-05-05 20:08:42 +00:00
|
|
|
|
2016-02-19 12:19:01 +00:00
|
|
|
// zNode owner and schema.
|
|
|
|
var owner string
|
|
|
|
var schema string
|
|
|
|
var schemaAndOwner string
|
|
|
|
schemaAndOwner, ok = conf["znode_owner"]
|
|
|
|
if !ok {
|
|
|
|
owner = "anyone"
|
|
|
|
schema = "world"
|
|
|
|
} else {
|
|
|
|
parsedSchemaAndOwner := strings.SplitN(schemaAndOwner, ":", 2)
|
|
|
|
if len(parsedSchemaAndOwner) != 2 {
|
|
|
|
return nil, fmt.Errorf("znode_owner expected format is 'schema:owner'")
|
|
|
|
} else {
|
|
|
|
schema = parsedSchemaAndOwner[0]
|
|
|
|
owner = parsedSchemaAndOwner[1]
|
2016-02-19 13:24:57 +00:00
|
|
|
|
|
|
|
// znode_owner is in config and structured correctly - but does it make any sense?
|
|
|
|
// Either 'owner' or 'schema' was set but not both - this seems like a failed attempt
|
2016-02-19 13:28:02 +00:00
|
|
|
// (e.g. ':MyUser' which omit the schema, or ':' omitting both)
|
2016-02-19 13:24:57 +00:00
|
|
|
if owner == "" || schema == "" {
|
|
|
|
return nil, fmt.Errorf("znode_owner expected format is 'schema:auth'")
|
|
|
|
}
|
2016-02-19 12:19:01 +00:00
|
|
|
}
|
|
|
|
}
|
2016-02-15 15:03:12 +00:00
|
|
|
|
2016-02-19 12:19:01 +00:00
|
|
|
acl := []zk.ACL{{zk.PermAll, schema, owner}}
|
2016-02-15 15:03:12 +00:00
|
|
|
|
2016-02-19 12:19:01 +00:00
|
|
|
// Authnetication info
|
|
|
|
var schemaAndUser string
|
2016-02-19 13:24:57 +00:00
|
|
|
var useAddAuth bool
|
|
|
|
schemaAndUser, useAddAuth = conf["auth_info"]
|
|
|
|
if useAddAuth {
|
2016-02-19 12:19:01 +00:00
|
|
|
parsedSchemaAndUser := strings.SplitN(schemaAndUser, ":", 2)
|
|
|
|
if len(parsedSchemaAndUser) != 2 {
|
|
|
|
return nil, fmt.Errorf("auth_info expected format is 'schema:auth'")
|
|
|
|
} else {
|
|
|
|
schema = parsedSchemaAndUser[0]
|
|
|
|
owner = parsedSchemaAndUser[1]
|
2016-02-19 13:24:57 +00:00
|
|
|
|
|
|
|
// auth_info is in config and structured correctly - but does it make any sense?
|
|
|
|
// Either 'owner' or 'schema' was set but not both - this seems like a failed attempt
|
2016-02-19 13:28:02 +00:00
|
|
|
// (e.g. ':MyUser' which omit the schema, or ':' omitting both)
|
2016-02-19 13:24:57 +00:00
|
|
|
if owner == "" || schema == "" {
|
|
|
|
return nil, fmt.Errorf("auth_info expected format is 'schema:auth'")
|
|
|
|
}
|
2016-02-19 12:19:01 +00:00
|
|
|
}
|
|
|
|
}
|
2016-02-15 15:03:12 +00:00
|
|
|
|
2016-02-19 13:24:57 +00:00
|
|
|
// We have all of the configuration in hand - let's try and connect to ZK
|
2015-05-05 22:20:38 +00:00
|
|
|
client, _, err := zk.Connect(strings.Split(machines, ","), time.Second)
|
2015-05-05 20:08:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("client setup failed: %v", err)
|
|
|
|
}
|
|
|
|
|
2016-02-19 13:24:57 +00:00
|
|
|
// ZK AddAuth API if the user asked for it
|
|
|
|
if useAddAuth {
|
2016-02-19 12:19:01 +00:00
|
|
|
err = client.AddAuth(schema, []byte(owner))
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Zookeeper rejected authentication information provided at auth_info: %v", err)
|
|
|
|
}
|
|
|
|
}
|
2016-02-15 15:03:12 +00:00
|
|
|
|
2015-05-05 20:08:42 +00:00
|
|
|
// Setup the backend
|
|
|
|
c := &ZookeeperBackend{
|
2015-05-21 03:15:31 +00:00
|
|
|
path: path,
|
2015-05-05 20:08:42 +00:00
|
|
|
client: client,
|
2016-02-19 12:19:01 +00:00
|
|
|
acl: acl,
|
2016-04-26 03:10:32 +00:00
|
|
|
logger: logger,
|
2015-05-05 20:08:42 +00:00
|
|
|
}
|
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
2015-05-06 18:08:08 +00:00
|
|
|
// ensurePath is used to create each node in the path hierarchy.
|
|
|
|
// We avoid calling this optimistically, and invoke it when we get
|
|
|
|
// an error during an operation
|
|
|
|
func (c *ZookeeperBackend) ensurePath(path string, value []byte) error {
|
2015-05-05 20:08:42 +00:00
|
|
|
nodes := strings.Split(path, "/")
|
|
|
|
fullPath := ""
|
2015-05-06 02:23:24 +00:00
|
|
|
for index, node := range nodes {
|
2015-05-05 20:08:42 +00:00
|
|
|
if strings.TrimSpace(node) != "" {
|
|
|
|
fullPath += "/" + node
|
2015-05-06 02:23:24 +00:00
|
|
|
isLastNode := index+1 == len(nodes)
|
|
|
|
|
|
|
|
// set parent nodes to nil, leaf to value
|
|
|
|
// this block reduces round trips by being smart on the leaf create/set
|
|
|
|
if exists, _, _ := c.client.Exists(fullPath); !isLastNode && !exists {
|
2016-02-15 15:03:12 +00:00
|
|
|
if _, err := c.client.Create(fullPath, nil, int32(0), c.acl); err != nil {
|
2015-05-06 18:08:08 +00:00
|
|
|
return err
|
|
|
|
}
|
2015-05-06 02:23:24 +00:00
|
|
|
} else if isLastNode && !exists {
|
2016-02-15 15:03:12 +00:00
|
|
|
if _, err := c.client.Create(fullPath, value, int32(0), c.acl); err != nil {
|
2015-05-06 18:08:08 +00:00
|
|
|
return err
|
|
|
|
}
|
2015-05-06 02:23:24 +00:00
|
|
|
} else if isLastNode && exists {
|
2015-05-12 14:12:00 +00:00
|
|
|
if _, err := c.client.Set(fullPath, value, int32(-1)); err != nil {
|
2015-05-06 18:08:08 +00:00
|
|
|
return err
|
|
|
|
}
|
2015-05-05 20:08:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-05-06 18:08:08 +00:00
|
|
|
return nil
|
2015-05-05 20:08:42 +00:00
|
|
|
}
|
|
|
|
|
2015-07-13 09:33:23 +00:00
|
|
|
// nodePath returns an etcd filepath based on the given key.
|
|
|
|
func (c *ZookeeperBackend) nodePath(key string) string {
|
|
|
|
return filepath.Join(c.path, filepath.Dir(key), ZKNodeFilePrefix+filepath.Base(key))
|
|
|
|
}
|
|
|
|
|
2015-05-05 20:08:42 +00:00
|
|
|
// Put is used to insert or update an entry
|
|
|
|
func (c *ZookeeperBackend) Put(entry *Entry) error {
|
|
|
|
defer metrics.MeasureSince([]string{"zookeeper", "put"}, time.Now())
|
|
|
|
|
2015-05-06 18:08:08 +00:00
|
|
|
// Attempt to set the full path
|
2015-07-13 09:33:23 +00:00
|
|
|
fullPath := c.nodePath(entry.Key)
|
2015-05-12 14:12:00 +00:00
|
|
|
_, err := c.client.Set(fullPath, entry.Value, -1)
|
2015-05-05 20:08:42 +00:00
|
|
|
|
2015-05-06 18:08:08 +00:00
|
|
|
// If we get ErrNoNode, we need to construct the path hierarchy
|
2015-05-06 02:23:24 +00:00
|
|
|
if err == zk.ErrNoNode {
|
2015-05-06 18:08:08 +00:00
|
|
|
return c.ensurePath(fullPath, entry.Value)
|
2015-05-06 02:23:24 +00:00
|
|
|
}
|
2015-05-06 18:08:08 +00:00
|
|
|
return err
|
2015-05-05 20:08:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get is used to fetch an entry
|
|
|
|
func (c *ZookeeperBackend) Get(key string) (*Entry, error) {
|
|
|
|
defer metrics.MeasureSince([]string{"zookeeper", "get"}, time.Now())
|
|
|
|
|
2015-05-06 18:08:08 +00:00
|
|
|
// Attempt to read the full path
|
2015-07-13 09:33:23 +00:00
|
|
|
fullPath := c.nodePath(key)
|
2015-05-05 20:08:42 +00:00
|
|
|
value, _, err := c.client.Get(fullPath)
|
|
|
|
|
2015-05-06 18:08:08 +00:00
|
|
|
// Ignore if the node does not exist
|
|
|
|
if err == zk.ErrNoNode {
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
if err != nil {
|
2015-05-05 20:08:42 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2015-05-06 18:08:08 +00:00
|
|
|
|
|
|
|
// Handle a non-existing value
|
2015-05-05 20:08:42 +00:00
|
|
|
if value == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
ent := &Entry{
|
|
|
|
Key: key,
|
|
|
|
Value: value,
|
|
|
|
}
|
|
|
|
return ent, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete is used to permanently delete an entry
|
|
|
|
func (c *ZookeeperBackend) Delete(key string) error {
|
|
|
|
defer metrics.MeasureSince([]string{"zookeeper", "delete"}, time.Now())
|
|
|
|
|
2015-05-06 18:08:08 +00:00
|
|
|
// Delete the full path
|
2015-07-13 09:33:23 +00:00
|
|
|
fullPath := c.nodePath(key)
|
2015-07-13 09:05:17 +00:00
|
|
|
err := c.client.Delete(fullPath, -1)
|
2015-05-05 20:08:42 +00:00
|
|
|
|
2015-05-06 18:08:08 +00:00
|
|
|
// Mask if the node does not exist
|
2015-05-05 22:14:41 +00:00
|
|
|
if err == zk.ErrNoNode {
|
2015-05-06 18:08:08 +00:00
|
|
|
err = nil
|
2015-05-05 20:08:42 +00:00
|
|
|
}
|
2015-05-06 18:08:08 +00:00
|
|
|
return err
|
2015-05-05 20:08:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// List is used ot list all the keys under a given
|
|
|
|
// prefix, up to the next prefix.
|
|
|
|
func (c *ZookeeperBackend) List(prefix string) ([]string, error) {
|
|
|
|
defer metrics.MeasureSince([]string{"zookeeper", "list"}, time.Now())
|
|
|
|
|
2015-05-06 18:08:08 +00:00
|
|
|
// Query the children at the full path
|
2015-05-21 03:15:31 +00:00
|
|
|
fullPath := strings.TrimSuffix(c.path+prefix, "/")
|
2015-05-06 02:23:24 +00:00
|
|
|
result, _, err := c.client.Children(fullPath)
|
2015-05-05 20:08:42 +00:00
|
|
|
|
2015-05-06 18:08:08 +00:00
|
|
|
// If the path nodes are missing, no children!
|
2015-05-06 02:23:24 +00:00
|
|
|
if err == zk.ErrNoNode {
|
|
|
|
return []string{}, nil
|
|
|
|
}
|
2015-05-05 20:08:42 +00:00
|
|
|
|
|
|
|
children := []string{}
|
|
|
|
for _, key := range result {
|
2015-05-06 18:08:08 +00:00
|
|
|
// Check if this entry has any child entries,
|
|
|
|
// and append the slash which is what Vault depends on
|
|
|
|
// for iteration
|
2015-05-05 20:08:42 +00:00
|
|
|
nodeChildren, _, _ := c.client.Children(fullPath + "/" + key)
|
|
|
|
if nodeChildren != nil && len(nodeChildren) > 0 {
|
|
|
|
children = append(children, key+"/")
|
2015-07-13 09:33:23 +00:00
|
|
|
} else {
|
|
|
|
children = append(children, key[1:])
|
2015-05-05 20:08:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
sort.Strings(children)
|
|
|
|
return children, nil
|
|
|
|
}
|
2015-05-12 04:37:08 +00:00
|
|
|
|
|
|
|
// LockWith is used for mutual exclusion based on the given key.
|
|
|
|
func (c *ZookeeperBackend) LockWith(key, value string) (Lock, error) {
|
|
|
|
l := &ZookeeperHALock{
|
|
|
|
in: c,
|
|
|
|
key: key,
|
|
|
|
value: value,
|
|
|
|
}
|
|
|
|
return l, nil
|
|
|
|
}
|
|
|
|
|
2016-07-18 17:19:58 +00:00
|
|
|
// HAEnabled indicates whether the HA functionality should be exposed.
|
|
|
|
// Currently always returns true.
|
|
|
|
func (c *ZookeeperBackend) HAEnabled() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2015-05-12 04:37:08 +00:00
|
|
|
// ZookeeperHALock is a Zookeeper Lock implementation for the HABackend
|
|
|
|
type ZookeeperHALock struct {
|
|
|
|
in *ZookeeperBackend
|
|
|
|
key string
|
|
|
|
value string
|
|
|
|
|
2015-05-26 02:14:00 +00:00
|
|
|
held bool
|
|
|
|
localLock sync.Mutex
|
|
|
|
leaderCh chan struct{}
|
|
|
|
zkLock *zk.Lock
|
2015-05-12 04:37:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (i *ZookeeperHALock) Lock(stopCh <-chan struct{}) (<-chan struct{}, error) {
|
2015-05-26 02:14:00 +00:00
|
|
|
i.localLock.Lock()
|
|
|
|
defer i.localLock.Unlock()
|
2015-05-12 04:37:08 +00:00
|
|
|
if i.held {
|
|
|
|
return nil, fmt.Errorf("lock already held")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Attempt an async acquisition
|
|
|
|
didLock := make(chan struct{})
|
|
|
|
failLock := make(chan error, 1)
|
|
|
|
releaseCh := make(chan bool, 1)
|
2015-07-13 09:33:23 +00:00
|
|
|
lockpath := i.in.nodePath(i.key)
|
2015-05-26 02:14:00 +00:00
|
|
|
go i.attemptLock(lockpath, didLock, failLock, releaseCh)
|
2015-05-12 04:37:08 +00:00
|
|
|
|
|
|
|
// Wait for lock acquisition, failure, or shutdown
|
|
|
|
select {
|
|
|
|
case <-didLock:
|
|
|
|
releaseCh <- false
|
|
|
|
case err := <-failLock:
|
|
|
|
return nil, err
|
|
|
|
case <-stopCh:
|
|
|
|
releaseCh <- true
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the leader channel
|
|
|
|
i.held = true
|
|
|
|
i.leaderCh = make(chan struct{})
|
|
|
|
|
2015-05-21 02:54:35 +00:00
|
|
|
// Watch for Events which could result in loss of our zkLock and close(i.leaderCh)
|
2015-05-28 04:39:12 +00:00
|
|
|
currentVal, _, lockeventCh, err := i.in.client.GetW(lockpath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("unable to watch HA lock: %v", err)
|
2015-05-21 02:54:35 +00:00
|
|
|
}
|
|
|
|
if i.value != string(currentVal) {
|
|
|
|
return nil, fmt.Errorf("lost HA lock immediately before watch")
|
|
|
|
}
|
2015-05-28 04:39:12 +00:00
|
|
|
go i.monitorLock(lockeventCh, i.leaderCh)
|
2015-05-12 04:37:08 +00:00
|
|
|
|
|
|
|
return i.leaderCh, nil
|
|
|
|
}
|
|
|
|
|
2015-05-26 02:14:00 +00:00
|
|
|
func (i *ZookeeperHALock) attemptLock(lockpath string, didLock chan struct{}, failLock chan error, releaseCh chan bool) {
|
|
|
|
// Wait to acquire the lock in ZK
|
2016-02-15 15:29:08 +00:00
|
|
|
lock := zk.NewLock(i.in.client, lockpath, i.in.acl)
|
2015-05-26 02:14:00 +00:00
|
|
|
err := lock.Lock()
|
|
|
|
if err != nil {
|
|
|
|
failLock <- err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// Set node value
|
2015-05-26 04:12:16 +00:00
|
|
|
data := []byte(i.value)
|
2015-05-28 04:39:12 +00:00
|
|
|
err = i.in.ensurePath(lockpath, data)
|
|
|
|
if err != nil {
|
|
|
|
failLock <- err
|
2015-05-26 02:14:00 +00:00
|
|
|
lock.Unlock()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
i.zkLock = lock
|
|
|
|
|
|
|
|
// Signal that lock is held
|
|
|
|
close(didLock)
|
|
|
|
|
|
|
|
// Handle an early abort
|
|
|
|
release := <-releaseCh
|
|
|
|
if release {
|
|
|
|
lock.Unlock()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-28 04:39:12 +00:00
|
|
|
func (i *ZookeeperHALock) monitorLock(lockeventCh <-chan zk.Event, leaderCh chan struct{}) {
|
2015-05-26 02:14:00 +00:00
|
|
|
for {
|
|
|
|
select {
|
2015-07-04 23:03:02 +00:00
|
|
|
case event := <-lockeventCh:
|
2015-05-26 02:14:00 +00:00
|
|
|
// Lost connection?
|
2015-05-28 04:39:12 +00:00
|
|
|
switch event.State {
|
|
|
|
case zk.StateConnected:
|
|
|
|
case zk.StateHasSession:
|
|
|
|
default:
|
|
|
|
close(leaderCh)
|
2015-05-26 04:12:16 +00:00
|
|
|
return
|
2015-05-26 02:14:00 +00:00
|
|
|
}
|
2015-05-28 04:39:12 +00:00
|
|
|
|
|
|
|
// Lost lock?
|
|
|
|
switch event.Type {
|
|
|
|
case zk.EventNodeChildrenChanged:
|
|
|
|
case zk.EventSession:
|
|
|
|
default:
|
|
|
|
close(leaderCh)
|
2015-05-26 04:12:16 +00:00
|
|
|
return
|
2015-05-26 02:14:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-12 04:37:08 +00:00
|
|
|
func (i *ZookeeperHALock) Unlock() error {
|
2015-05-26 02:14:00 +00:00
|
|
|
i.localLock.Lock()
|
|
|
|
defer i.localLock.Unlock()
|
2015-05-12 04:37:08 +00:00
|
|
|
if !i.held {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
i.held = false
|
|
|
|
i.zkLock.Unlock()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *ZookeeperHALock) Value() (bool, string, error) {
|
2015-07-13 09:33:23 +00:00
|
|
|
lockpath := i.in.nodePath(i.key)
|
2015-05-12 04:37:08 +00:00
|
|
|
value, _, err := i.in.client.Get(lockpath)
|
2015-05-26 02:14:00 +00:00
|
|
|
return (value != nil), string(value), err
|
2015-05-12 04:37:08 +00:00
|
|
|
}
|