2014-03-31 21:15:49 +00:00
|
|
|
package consul
|
|
|
|
|
2014-03-31 23:00:23 +00:00
|
|
|
import (
|
|
|
|
"fmt"
|
2014-08-13 19:09:39 +00:00
|
|
|
"time"
|
|
|
|
|
2014-03-31 23:00:23 +00:00
|
|
|
"github.com/armon/go-metrics"
|
2016-05-07 00:50:58 +00:00
|
|
|
"github.com/hashicorp/consul/acl"
|
2017-04-19 23:00:11 +00:00
|
|
|
"github.com/hashicorp/consul/api"
|
2017-01-25 01:23:48 +00:00
|
|
|
"github.com/hashicorp/consul/consul/state"
|
2014-03-31 23:00:23 +00:00
|
|
|
"github.com/hashicorp/consul/consul/structs"
|
2017-01-24 19:20:51 +00:00
|
|
|
"github.com/hashicorp/go-memdb"
|
2014-03-31 23:00:23 +00:00
|
|
|
)
|
2014-03-31 21:15:49 +00:00
|
|
|
|
|
|
|
// KVS endpoint is used to manipulate the Key-Value store
|
|
|
|
type KVS struct {
|
|
|
|
srv *Server
|
|
|
|
}
|
2014-03-31 23:00:23 +00:00
|
|
|
|
2016-05-07 00:50:58 +00:00
|
|
|
// preApply does all the verification of a KVS update that is performed BEFORE
|
|
|
|
// we submit as a Raft log entry. This includes enforcing the lock delay which
|
|
|
|
// must only be done on the leader.
|
2017-04-19 23:00:11 +00:00
|
|
|
func kvsPreApply(srv *Server, acl acl.ACL, op api.KVOp, dirEnt *structs.DirEntry) (bool, error) {
|
2016-05-07 00:50:58 +00:00
|
|
|
// Verify the entry.
|
2017-04-19 23:00:11 +00:00
|
|
|
if dirEnt.Key == "" && op != api.KVDeleteTree {
|
2016-05-07 00:50:58 +00:00
|
|
|
return false, fmt.Errorf("Must provide key")
|
2014-03-31 23:00:23 +00:00
|
|
|
}
|
|
|
|
|
2016-05-07 00:50:58 +00:00
|
|
|
// Apply the ACL policy if any.
|
|
|
|
if acl != nil {
|
|
|
|
switch op {
|
2017-04-19 23:00:11 +00:00
|
|
|
case api.KVDeleteTree:
|
2016-05-07 00:50:58 +00:00
|
|
|
if !acl.KeyWritePrefix(dirEnt.Key) {
|
2017-04-21 00:02:42 +00:00
|
|
|
return false, errPermissionDenied
|
2014-08-15 02:25:12 +00:00
|
|
|
}
|
2016-05-10 18:23:47 +00:00
|
|
|
|
2017-04-19 23:00:11 +00:00
|
|
|
case api.KVGet, api.KVGetTree:
|
2016-05-13 22:58:55 +00:00
|
|
|
// Filtering for GETs is done on the output side.
|
|
|
|
|
2017-04-19 23:00:11 +00:00
|
|
|
case api.KVCheckSession, api.KVCheckIndex:
|
2016-05-13 22:58:55 +00:00
|
|
|
// These could reveal information based on the outcome
|
|
|
|
// of the transaction, and they operate on individual
|
|
|
|
// keys so we check them here.
|
2016-05-10 18:23:47 +00:00
|
|
|
if !acl.KeyRead(dirEnt.Key) {
|
2017-04-21 00:02:42 +00:00
|
|
|
return false, errPermissionDenied
|
2016-05-10 18:23:47 +00:00
|
|
|
}
|
|
|
|
|
2014-08-15 02:25:12 +00:00
|
|
|
default:
|
2016-05-07 00:50:58 +00:00
|
|
|
if !acl.KeyWrite(dirEnt.Key) {
|
2017-04-21 00:02:42 +00:00
|
|
|
return false, errPermissionDenied
|
2014-08-15 02:25:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-19 19:50:29 +00:00
|
|
|
// If this is a lock, we must check for a lock-delay. Since lock-delay
|
2016-05-07 00:50:58 +00:00
|
|
|
// is based on wall-time, each peer would expire the lock-delay at a slightly
|
2014-05-19 19:50:29 +00:00
|
|
|
// different time. This means the enforcement of lock-delay cannot be done
|
|
|
|
// after the raft log is committed as it would lead to inconsistent FSMs.
|
|
|
|
// Instead, the lock-delay must be enforced before commit. This means that
|
|
|
|
// only the wall-time of the leader node is used, preventing any inconsistencies.
|
2017-04-19 23:00:11 +00:00
|
|
|
if op == api.KVLock {
|
2016-05-11 04:41:47 +00:00
|
|
|
state := srv.fsm.State()
|
2016-05-07 00:50:58 +00:00
|
|
|
expires := state.KVSLockDelay(dirEnt.Key)
|
2014-05-19 19:50:29 +00:00
|
|
|
if expires.After(time.Now()) {
|
2016-05-11 04:41:47 +00:00
|
|
|
srv.logger.Printf("[WARN] consul.kvs: Rejecting lock of %s due to lock-delay until %v",
|
2016-05-07 00:50:58 +00:00
|
|
|
dirEnt.Key, expires)
|
|
|
|
return false, nil
|
2014-05-19 19:50:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-07 00:50:58 +00:00
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Apply is used to apply a KVS update request to the data store.
|
|
|
|
func (k *KVS) Apply(args *structs.KVSRequest, reply *bool) error {
|
|
|
|
if done, err := k.srv.forward("KVS.Apply", args, args, reply); done {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer metrics.MeasureSince([]string{"consul", "kvs", "apply"}, time.Now())
|
|
|
|
|
|
|
|
// Perform the pre-apply checks.
|
|
|
|
acl, err := k.srv.resolveToken(args.Token)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-05-11 04:41:47 +00:00
|
|
|
ok, err := kvsPreApply(k.srv, acl, args.Op, &args.DirEnt)
|
2016-05-07 00:50:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !ok {
|
|
|
|
*reply = false
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Apply the update.
|
2014-03-31 23:10:49 +00:00
|
|
|
resp, err := k.srv.raftApply(structs.KVSRequestType, args)
|
2014-03-31 23:00:23 +00:00
|
|
|
if err != nil {
|
2014-03-31 23:10:49 +00:00
|
|
|
k.srv.logger.Printf("[ERR] consul.kvs: Apply failed: %v", err)
|
2014-03-31 23:00:23 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if respErr, ok := resp.(error); ok {
|
|
|
|
return respErr
|
|
|
|
}
|
|
|
|
|
2016-05-07 00:50:58 +00:00
|
|
|
// Check if the return type is a bool.
|
2014-03-31 23:00:23 +00:00
|
|
|
if respBool, ok := resp.(bool); ok {
|
|
|
|
*reply = respBool
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-05-07 00:50:58 +00:00
|
|
|
// Get is used to lookup a single key.
|
2014-03-31 23:00:23 +00:00
|
|
|
func (k *KVS) Get(args *structs.KeyRequest, reply *structs.IndexedDirEntries) error {
|
2014-04-19 00:17:12 +00:00
|
|
|
if done, err := k.srv.forward("KVS.Get", args, args, reply); done {
|
2014-03-31 23:00:23 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-08-13 19:09:39 +00:00
|
|
|
acl, err := k.srv.resolveToken(args.Token)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-01-24 19:20:51 +00:00
|
|
|
return k.srv.blockingQuery(
|
2015-10-09 19:00:15 +00:00
|
|
|
&args.QueryOptions,
|
|
|
|
&reply.QueryMeta,
|
2017-04-21 00:46:29 +00:00
|
|
|
func(ws memdb.WatchSet, state *state.Store) error {
|
2017-01-24 19:20:51 +00:00
|
|
|
index, ent, err := state.KVSGet(ws, args.Key)
|
2014-03-31 23:00:23 +00:00
|
|
|
if err != nil {
|
2014-04-21 18:18:27 +00:00
|
|
|
return err
|
2014-03-31 23:00:23 +00:00
|
|
|
}
|
2014-08-13 19:09:39 +00:00
|
|
|
if acl != nil && !acl.KeyRead(args.Key) {
|
|
|
|
ent = nil
|
|
|
|
}
|
2014-03-31 23:00:23 +00:00
|
|
|
if ent == nil {
|
2014-04-01 03:00:46 +00:00
|
|
|
// Must provide non-zero index to prevent blocking
|
|
|
|
// Index 1 is impossible anyways (due to Raft internals)
|
2015-10-13 18:48:35 +00:00
|
|
|
if index == 0 {
|
|
|
|
reply.Index = 1
|
|
|
|
} else {
|
|
|
|
reply.Index = index
|
|
|
|
}
|
2014-03-31 23:00:23 +00:00
|
|
|
reply.Entries = nil
|
|
|
|
} else {
|
|
|
|
reply.Index = ent.ModifyIndex
|
|
|
|
reply.Entries = structs.DirEntries{ent}
|
|
|
|
}
|
2014-04-21 18:18:27 +00:00
|
|
|
return nil
|
2015-10-09 19:00:15 +00:00
|
|
|
})
|
2014-03-31 23:00:23 +00:00
|
|
|
}
|
|
|
|
|
2016-05-07 00:50:58 +00:00
|
|
|
// List is used to list all keys with a given prefix.
|
2014-03-31 23:00:23 +00:00
|
|
|
func (k *KVS) List(args *structs.KeyRequest, reply *structs.IndexedDirEntries) error {
|
2014-04-19 00:17:12 +00:00
|
|
|
if done, err := k.srv.forward("KVS.List", args, args, reply); done {
|
2014-03-31 23:00:23 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-08-13 19:09:39 +00:00
|
|
|
acl, err := k.srv.resolveToken(args.Token)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-01-24 19:20:51 +00:00
|
|
|
return k.srv.blockingQuery(
|
2015-10-09 19:00:15 +00:00
|
|
|
&args.QueryOptions,
|
|
|
|
&reply.QueryMeta,
|
2017-04-21 00:46:29 +00:00
|
|
|
func(ws memdb.WatchSet, state *state.Store) error {
|
2017-01-24 19:20:51 +00:00
|
|
|
index, ent, err := state.KVSList(ws, args.Key)
|
2014-03-31 23:00:23 +00:00
|
|
|
if err != nil {
|
2014-04-21 18:18:27 +00:00
|
|
|
return err
|
2014-03-31 23:00:23 +00:00
|
|
|
}
|
2014-08-13 19:09:39 +00:00
|
|
|
if acl != nil {
|
|
|
|
ent = FilterDirEnt(acl, ent)
|
|
|
|
}
|
2014-12-19 00:27:46 +00:00
|
|
|
|
2014-12-19 00:50:15 +00:00
|
|
|
if len(ent) == 0 {
|
|
|
|
// Must provide non-zero index to prevent blocking
|
|
|
|
// Index 1 is impossible anyways (due to Raft internals)
|
|
|
|
if index == 0 {
|
|
|
|
reply.Index = 1
|
2014-12-19 00:27:46 +00:00
|
|
|
} else {
|
2014-12-19 00:50:15 +00:00
|
|
|
reply.Index = index
|
2014-03-31 23:00:23 +00:00
|
|
|
}
|
2015-01-13 19:51:24 +00:00
|
|
|
reply.Entries = nil
|
2014-12-19 00:50:15 +00:00
|
|
|
} else {
|
2015-10-09 19:00:15 +00:00
|
|
|
reply.Index = index
|
2014-03-31 23:00:23 +00:00
|
|
|
reply.Entries = ent
|
|
|
|
}
|
2014-04-21 18:18:27 +00:00
|
|
|
return nil
|
2015-10-09 19:00:15 +00:00
|
|
|
})
|
2014-03-31 23:00:23 +00:00
|
|
|
}
|
2014-04-28 23:33:54 +00:00
|
|
|
|
2016-05-07 00:50:58 +00:00
|
|
|
// ListKeys is used to list all keys with a given prefix to a separator.
|
2014-04-28 23:33:54 +00:00
|
|
|
func (k *KVS) ListKeys(args *structs.KeyListRequest, reply *structs.IndexedKeyList) error {
|
|
|
|
if done, err := k.srv.forward("KVS.ListKeys", args, args, reply); done {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-08-13 19:09:39 +00:00
|
|
|
acl, err := k.srv.resolveToken(args.Token)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-01-24 19:20:51 +00:00
|
|
|
return k.srv.blockingQuery(
|
2015-10-09 19:00:15 +00:00
|
|
|
&args.QueryOptions,
|
|
|
|
&reply.QueryMeta,
|
2017-04-21 00:46:29 +00:00
|
|
|
func(ws memdb.WatchSet, state *state.Store) error {
|
2017-01-24 19:20:51 +00:00
|
|
|
index, keys, err := state.KVSListKeys(ws, args.Prefix, args.Seperator)
|
2015-10-09 19:00:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Must provide non-zero index to prevent blocking
|
|
|
|
// Index 1 is impossible anyways (due to Raft internals)
|
|
|
|
if index == 0 {
|
|
|
|
reply.Index = 1
|
|
|
|
} else {
|
|
|
|
reply.Index = index
|
|
|
|
}
|
|
|
|
|
2014-08-13 19:09:39 +00:00
|
|
|
if acl != nil {
|
|
|
|
keys = FilterKeys(acl, keys)
|
|
|
|
}
|
|
|
|
reply.Keys = keys
|
2015-10-09 19:00:15 +00:00
|
|
|
return nil
|
|
|
|
})
|
2014-04-28 23:33:54 +00:00
|
|
|
}
|