2023-03-28 18:39:22 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2014-03-31 23:10:49 +00:00
|
|
|
package consul
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"testing"
|
2014-05-19 19:50:29 +00:00
|
|
|
"time"
|
2014-08-15 02:25:12 +00:00
|
|
|
|
2021-08-06 22:00:58 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2022-04-05 21:10:06 +00:00
|
|
|
msgpackrpc "github.com/hashicorp/consul-net-rpc/net-rpc-msgpackrpc"
|
|
|
|
|
2017-08-23 14:52:48 +00:00
|
|
|
"github.com/hashicorp/consul/acl"
|
2017-07-06 10:34:00 +00:00
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
2017-04-19 23:00:11 +00:00
|
|
|
"github.com/hashicorp/consul/api"
|
|
|
|
"github.com/hashicorp/consul/testrpc"
|
2014-03-31 23:10:49 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestKVS_Apply(t *testing.T) {
|
2020-12-07 18:42:55 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("too slow for testing.Short")
|
|
|
|
}
|
|
|
|
|
2017-06-27 13:22:18 +00:00
|
|
|
t.Parallel()
|
2014-03-31 23:10:49 +00:00
|
|
|
dir1, s1 := testServer(t)
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
2015-10-13 23:43:52 +00:00
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
defer codec.Close()
|
2014-03-31 23:10:49 +00:00
|
|
|
|
2019-11-25 17:57:35 +00:00
|
|
|
testrpc.WaitForTestAgent(t, s1.RPC, "dc1")
|
2014-03-31 23:10:49 +00:00
|
|
|
|
|
|
|
arg := structs.KVSRequest{
|
|
|
|
Datacenter: "dc1",
|
2017-04-19 23:00:11 +00:00
|
|
|
Op: api.KVSet,
|
2014-03-31 23:10:49 +00:00
|
|
|
DirEnt: structs.DirEntry{
|
|
|
|
Key: "test",
|
|
|
|
Flags: 42,
|
|
|
|
Value: []byte("test"),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
var out bool
|
2015-10-13 23:43:52 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "KVS.Apply", &arg, &out); err != nil {
|
2014-03-31 23:10:49 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify
|
2015-10-13 05:21:39 +00:00
|
|
|
state := s1.fsm.State()
|
2019-11-25 17:57:35 +00:00
|
|
|
_, d, err := state.KVSGet(nil, "test", &arg.DirEnt.EnterpriseMeta)
|
2014-03-31 23:10:49 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if d == nil {
|
|
|
|
t.Fatalf("should not be nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do a check and set
|
2017-04-19 23:00:11 +00:00
|
|
|
arg.Op = api.KVCAS
|
2014-03-31 23:10:49 +00:00
|
|
|
arg.DirEnt.ModifyIndex = d.ModifyIndex
|
|
|
|
arg.DirEnt.Flags = 43
|
2015-10-13 23:43:52 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "KVS.Apply", &arg, &out); err != nil {
|
2014-03-31 23:10:49 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check this was applied
|
|
|
|
if out != true {
|
|
|
|
t.Fatalf("bad: %v", out)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify
|
2019-11-25 17:57:35 +00:00
|
|
|
_, d, err = state.KVSGet(nil, "test", &arg.DirEnt.EnterpriseMeta)
|
2014-03-31 23:10:49 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if d.Flags != 43 {
|
|
|
|
t.Fatalf("bad: %v", d)
|
|
|
|
}
|
|
|
|
}
|
2014-03-31 23:18:44 +00:00
|
|
|
|
2014-08-15 02:25:12 +00:00
|
|
|
func TestKVS_Apply_ACLDeny(t *testing.T) {
|
2020-12-07 18:42:55 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("too slow for testing.Short")
|
|
|
|
}
|
|
|
|
|
2017-06-27 13:22:18 +00:00
|
|
|
t.Parallel()
|
2014-08-15 02:25:12 +00:00
|
|
|
dir1, s1 := testServerWithConfig(t, func(c *Config) {
|
2021-08-06 22:00:58 +00:00
|
|
|
c.PrimaryDatacenter = "dc1"
|
2018-10-19 16:04:07 +00:00
|
|
|
c.ACLsEnabled = true
|
2021-12-07 12:39:28 +00:00
|
|
|
c.ACLInitialManagementToken = "root"
|
2021-08-06 22:39:39 +00:00
|
|
|
c.ACLResolverSettings.ACLDefaultPolicy = "deny"
|
2014-08-15 02:25:12 +00:00
|
|
|
})
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
2015-10-13 23:43:52 +00:00
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
defer codec.Close()
|
2014-08-15 02:25:12 +00:00
|
|
|
|
2020-05-29 21:16:03 +00:00
|
|
|
testrpc.WaitForTestAgent(t, s1.RPC, "dc1", testrpc.WithToken("root"))
|
2014-08-15 02:25:12 +00:00
|
|
|
|
2021-09-03 20:13:11 +00:00
|
|
|
id := createToken(t, codec, testListRules)
|
2014-08-15 02:25:12 +00:00
|
|
|
|
|
|
|
// Try a write
|
|
|
|
argR := structs.KVSRequest{
|
|
|
|
Datacenter: "dc1",
|
2017-04-19 23:00:11 +00:00
|
|
|
Op: api.KVSet,
|
2014-08-15 02:25:12 +00:00
|
|
|
DirEnt: structs.DirEntry{
|
|
|
|
Key: "foo/bar",
|
|
|
|
Flags: 42,
|
|
|
|
Value: []byte("test"),
|
|
|
|
},
|
|
|
|
WriteRequest: structs.WriteRequest{Token: id},
|
|
|
|
}
|
|
|
|
var outR bool
|
2015-10-13 23:43:52 +00:00
|
|
|
err := msgpackrpc.CallWithCodec(codec, "KVS.Apply", &argR, &outR)
|
2017-08-23 14:52:48 +00:00
|
|
|
if !acl.IsErrPermissionDenied(err) {
|
2014-08-15 02:25:12 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try a recursive delete
|
|
|
|
argR = structs.KVSRequest{
|
|
|
|
Datacenter: "dc1",
|
2017-04-19 23:00:11 +00:00
|
|
|
Op: api.KVDeleteTree,
|
2014-08-15 02:25:12 +00:00
|
|
|
DirEnt: structs.DirEntry{
|
|
|
|
Key: "test",
|
|
|
|
},
|
|
|
|
WriteRequest: structs.WriteRequest{Token: id},
|
|
|
|
}
|
2015-10-13 23:43:52 +00:00
|
|
|
err = msgpackrpc.CallWithCodec(codec, "KVS.Apply", &argR, &outR)
|
2017-08-23 14:52:48 +00:00
|
|
|
if !acl.IsErrPermissionDenied(err) {
|
2014-08-15 02:25:12 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-31 23:18:44 +00:00
|
|
|
func TestKVS_Get(t *testing.T) {
|
2020-12-07 18:42:55 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("too slow for testing.Short")
|
|
|
|
}
|
|
|
|
|
2017-06-27 13:22:18 +00:00
|
|
|
t.Parallel()
|
2014-03-31 23:18:44 +00:00
|
|
|
dir1, s1 := testServer(t)
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
2015-10-13 23:43:52 +00:00
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
defer codec.Close()
|
2014-03-31 23:18:44 +00:00
|
|
|
|
2019-11-25 17:57:35 +00:00
|
|
|
testrpc.WaitForTestAgent(t, s1.RPC, "dc1")
|
2014-03-31 23:18:44 +00:00
|
|
|
|
|
|
|
arg := structs.KVSRequest{
|
|
|
|
Datacenter: "dc1",
|
2017-04-19 23:00:11 +00:00
|
|
|
Op: api.KVSet,
|
2014-03-31 23:18:44 +00:00
|
|
|
DirEnt: structs.DirEntry{
|
|
|
|
Key: "test",
|
|
|
|
Flags: 42,
|
|
|
|
Value: []byte("test"),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
var out bool
|
2015-10-13 23:43:52 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "KVS.Apply", &arg, &out); err != nil {
|
2014-03-31 23:18:44 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
getR := structs.KeyRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Key: "test",
|
|
|
|
}
|
|
|
|
var dirent structs.IndexedDirEntries
|
2015-10-13 23:43:52 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "KVS.Get", &getR, &dirent); err != nil {
|
2014-03-31 23:18:44 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if dirent.Index == 0 {
|
|
|
|
t.Fatalf("Bad: %v", dirent)
|
|
|
|
}
|
|
|
|
if len(dirent.Entries) != 1 {
|
|
|
|
t.Fatalf("Bad: %v", dirent)
|
|
|
|
}
|
|
|
|
d := dirent.Entries[0]
|
|
|
|
if d.Flags != 42 {
|
|
|
|
t.Fatalf("bad: %v", d)
|
|
|
|
}
|
|
|
|
if string(d.Value) != "test" {
|
|
|
|
t.Fatalf("bad: %v", d)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-13 19:09:39 +00:00
|
|
|
func TestKVS_Get_ACLDeny(t *testing.T) {
|
2020-12-07 18:42:55 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("too slow for testing.Short")
|
|
|
|
}
|
|
|
|
|
2017-06-27 13:22:18 +00:00
|
|
|
t.Parallel()
|
2014-08-13 19:09:39 +00:00
|
|
|
dir1, s1 := testServerWithConfig(t, func(c *Config) {
|
2021-08-06 22:00:58 +00:00
|
|
|
c.PrimaryDatacenter = "dc1"
|
2018-10-19 16:04:07 +00:00
|
|
|
c.ACLsEnabled = true
|
2021-12-07 12:39:28 +00:00
|
|
|
c.ACLInitialManagementToken = "root"
|
2021-08-06 22:39:39 +00:00
|
|
|
c.ACLResolverSettings.ACLDefaultPolicy = "deny"
|
2014-08-13 19:09:39 +00:00
|
|
|
})
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
2015-10-13 23:43:52 +00:00
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
defer codec.Close()
|
2014-08-13 19:09:39 +00:00
|
|
|
|
2020-05-29 21:16:03 +00:00
|
|
|
testrpc.WaitForTestAgent(t, s1.RPC, "dc1", testrpc.WithToken("root"))
|
2014-08-13 19:09:39 +00:00
|
|
|
|
|
|
|
arg := structs.KVSRequest{
|
|
|
|
Datacenter: "dc1",
|
2017-04-19 23:00:11 +00:00
|
|
|
Op: api.KVSet,
|
2014-08-13 19:09:39 +00:00
|
|
|
DirEnt: structs.DirEntry{
|
2014-08-15 02:25:12 +00:00
|
|
|
Key: "zip",
|
2014-08-13 19:09:39 +00:00
|
|
|
Flags: 42,
|
|
|
|
Value: []byte("test"),
|
|
|
|
},
|
|
|
|
WriteRequest: structs.WriteRequest{Token: "root"},
|
|
|
|
}
|
|
|
|
var out bool
|
2015-10-13 23:43:52 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "KVS.Apply", &arg, &out); err != nil {
|
2017-08-09 22:06:57 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
2014-08-13 19:09:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
getR := structs.KeyRequest{
|
|
|
|
Datacenter: "dc1",
|
2014-08-15 02:25:12 +00:00
|
|
|
Key: "zip",
|
2014-08-13 19:09:39 +00:00
|
|
|
}
|
|
|
|
var dirent structs.IndexedDirEntries
|
2017-10-02 22:10:21 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "KVS.Get", &getR, &dirent); !acl.IsErrPermissionDenied(err) {
|
|
|
|
t.Fatalf("Expected %v, got err: %v", acl.ErrPermissionDenied, err)
|
2014-08-13 19:09:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-03-31 23:18:44 +00:00
|
|
|
func TestKVSEndpoint_List(t *testing.T) {
|
2020-12-07 18:42:55 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("too slow for testing.Short")
|
|
|
|
}
|
|
|
|
|
2017-06-27 13:22:18 +00:00
|
|
|
t.Parallel()
|
2014-03-31 23:18:44 +00:00
|
|
|
dir1, s1 := testServer(t)
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
2015-10-13 23:43:52 +00:00
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
defer codec.Close()
|
2014-03-31 23:18:44 +00:00
|
|
|
|
2019-11-25 17:57:35 +00:00
|
|
|
testrpc.WaitForTestAgent(t, s1.RPC, "dc1")
|
2014-03-31 23:18:44 +00:00
|
|
|
|
|
|
|
keys := []string{
|
|
|
|
"/test/key1",
|
|
|
|
"/test/key2",
|
|
|
|
"/test/sub/key3",
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, key := range keys {
|
|
|
|
arg := structs.KVSRequest{
|
|
|
|
Datacenter: "dc1",
|
2017-04-19 23:00:11 +00:00
|
|
|
Op: api.KVSet,
|
2014-03-31 23:18:44 +00:00
|
|
|
DirEnt: structs.DirEntry{
|
|
|
|
Key: key,
|
|
|
|
Flags: 1,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
var out bool
|
2015-10-13 23:43:52 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "KVS.Apply", &arg, &out); err != nil {
|
2014-03-31 23:18:44 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
getR := structs.KeyRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Key: "/test",
|
|
|
|
}
|
|
|
|
var dirent structs.IndexedDirEntries
|
2015-10-13 23:43:52 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "KVS.List", &getR, &dirent); err != nil {
|
2014-03-31 23:18:44 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if dirent.Index == 0 {
|
|
|
|
t.Fatalf("Bad: %v", dirent)
|
|
|
|
}
|
|
|
|
if len(dirent.Entries) != 3 {
|
|
|
|
t.Fatalf("Bad: %v", dirent.Entries)
|
|
|
|
}
|
|
|
|
for i := 0; i < len(dirent.Entries); i++ {
|
|
|
|
d := dirent.Entries[i]
|
|
|
|
if d.Key != keys[i] {
|
|
|
|
t.Fatalf("bad: %v", d)
|
|
|
|
}
|
|
|
|
if d.Flags != 1 {
|
|
|
|
t.Fatalf("bad: %v", d)
|
|
|
|
}
|
|
|
|
if d.Value != nil {
|
|
|
|
t.Fatalf("bad: %v", d)
|
|
|
|
}
|
|
|
|
}
|
2021-12-03 17:31:48 +00:00
|
|
|
if dirent.QueryMeta.ResultsFilteredByACLs {
|
|
|
|
t.Fatal("ResultsFilteredByACLs should not be true")
|
|
|
|
}
|
2015-10-13 18:48:35 +00:00
|
|
|
|
|
|
|
// Try listing a nonexistent prefix
|
|
|
|
getR.Key = "/nope"
|
2015-10-15 22:09:13 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "KVS.List", &getR, &dirent); err != nil {
|
2015-10-13 18:48:35 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if dirent.Index == 0 {
|
|
|
|
t.Fatalf("Bad: %v", dirent)
|
|
|
|
}
|
|
|
|
if len(dirent.Entries) != 0 {
|
|
|
|
t.Fatalf("Bad: %v", dirent.Entries)
|
|
|
|
}
|
2014-03-31 23:18:44 +00:00
|
|
|
}
|
2014-04-28 23:33:54 +00:00
|
|
|
|
2015-01-06 00:57:48 +00:00
|
|
|
func TestKVSEndpoint_List_Blocking(t *testing.T) {
|
2020-12-07 18:42:55 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("too slow for testing.Short")
|
|
|
|
}
|
|
|
|
|
2017-06-27 13:22:18 +00:00
|
|
|
t.Parallel()
|
2015-01-06 00:57:48 +00:00
|
|
|
dir1, s1 := testServer(t)
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
2015-10-13 23:43:52 +00:00
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
defer codec.Close()
|
2015-01-06 00:57:48 +00:00
|
|
|
|
2019-11-25 17:57:35 +00:00
|
|
|
testrpc.WaitForTestAgent(t, s1.RPC, "dc1")
|
2015-01-06 00:57:48 +00:00
|
|
|
|
|
|
|
keys := []string{
|
|
|
|
"/test/key1",
|
|
|
|
"/test/key2",
|
|
|
|
"/test/sub/key3",
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, key := range keys {
|
|
|
|
arg := structs.KVSRequest{
|
|
|
|
Datacenter: "dc1",
|
2017-04-19 23:00:11 +00:00
|
|
|
Op: api.KVSet,
|
2015-01-06 00:57:48 +00:00
|
|
|
DirEnt: structs.DirEntry{
|
|
|
|
Key: key,
|
|
|
|
Flags: 1,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
var out bool
|
2015-10-13 23:43:52 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "KVS.Apply", &arg, &out); err != nil {
|
2015-01-06 00:57:48 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
getR := structs.KeyRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Key: "/test",
|
|
|
|
}
|
|
|
|
var dirent structs.IndexedDirEntries
|
2015-10-13 23:43:52 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "KVS.List", &getR, &dirent); err != nil {
|
2015-01-06 00:57:48 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Setup a blocking query
|
|
|
|
getR.MinQueryIndex = dirent.Index
|
|
|
|
getR.MaxQueryTime = time.Second
|
|
|
|
|
|
|
|
// Async cause a change
|
|
|
|
start := time.Now()
|
|
|
|
go func() {
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
2015-10-13 23:43:52 +00:00
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
defer codec.Close()
|
2015-01-06 00:57:48 +00:00
|
|
|
arg := structs.KVSRequest{
|
|
|
|
Datacenter: "dc1",
|
2017-04-19 23:00:11 +00:00
|
|
|
Op: api.KVDelete,
|
2015-01-06 00:57:48 +00:00
|
|
|
DirEnt: structs.DirEntry{
|
|
|
|
Key: "/test/sub/key3",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
var out bool
|
2015-10-13 23:43:52 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "KVS.Apply", &arg, &out); err != nil {
|
2020-06-05 20:52:31 +00:00
|
|
|
t.Errorf("RPC call failed: %v", err)
|
2015-01-06 00:57:48 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Re-run the query
|
|
|
|
dirent = structs.IndexedDirEntries{}
|
2015-10-13 23:43:52 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "KVS.List", &getR, &dirent); err != nil {
|
2015-01-06 00:57:48 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Should block at least 100ms
|
2017-10-17 18:38:24 +00:00
|
|
|
if time.Since(start) < 100*time.Millisecond {
|
2015-01-06 00:57:48 +00:00
|
|
|
t.Fatalf("too fast")
|
|
|
|
}
|
|
|
|
|
|
|
|
if dirent.Index == 0 {
|
|
|
|
t.Fatalf("Bad: %v", dirent)
|
|
|
|
}
|
|
|
|
if len(dirent.Entries) != 2 {
|
|
|
|
for _, ent := range dirent.Entries {
|
|
|
|
t.Errorf("Bad: %#v", *ent)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for i := 0; i < len(dirent.Entries); i++ {
|
|
|
|
d := dirent.Entries[i]
|
|
|
|
if d.Key != keys[i] {
|
|
|
|
t.Fatalf("bad: %v", d)
|
|
|
|
}
|
|
|
|
if d.Flags != 1 {
|
|
|
|
t.Fatalf("bad: %v", d)
|
|
|
|
}
|
|
|
|
if d.Value != nil {
|
|
|
|
t.Fatalf("bad: %v", d)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-13 19:09:39 +00:00
|
|
|
func TestKVSEndpoint_List_ACLDeny(t *testing.T) {
|
2020-12-07 18:42:55 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("too slow for testing.Short")
|
|
|
|
}
|
|
|
|
|
2017-06-27 13:22:18 +00:00
|
|
|
t.Parallel()
|
2014-08-13 19:09:39 +00:00
|
|
|
dir1, s1 := testServerWithConfig(t, func(c *Config) {
|
2021-08-06 22:00:58 +00:00
|
|
|
c.PrimaryDatacenter = "dc1"
|
2018-10-19 16:04:07 +00:00
|
|
|
c.ACLsEnabled = true
|
2021-12-07 12:39:28 +00:00
|
|
|
c.ACLInitialManagementToken = "root"
|
2021-08-06 22:39:39 +00:00
|
|
|
c.ACLResolverSettings.ACLDefaultPolicy = "deny"
|
2014-08-13 19:09:39 +00:00
|
|
|
})
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
2015-10-13 23:43:52 +00:00
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
defer codec.Close()
|
2014-08-13 19:09:39 +00:00
|
|
|
|
2020-05-29 21:16:03 +00:00
|
|
|
testrpc.WaitForTestAgent(t, s1.RPC, "dc1", testrpc.WithToken("root"))
|
2014-08-13 19:09:39 +00:00
|
|
|
|
|
|
|
keys := []string{
|
|
|
|
"abe",
|
|
|
|
"bar",
|
|
|
|
"foo",
|
|
|
|
"test",
|
|
|
|
"zip",
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, key := range keys {
|
|
|
|
arg := structs.KVSRequest{
|
|
|
|
Datacenter: "dc1",
|
2017-04-19 23:00:11 +00:00
|
|
|
Op: api.KVSet,
|
2014-08-13 19:09:39 +00:00
|
|
|
DirEnt: structs.DirEntry{
|
|
|
|
Key: key,
|
|
|
|
Flags: 1,
|
|
|
|
},
|
|
|
|
WriteRequest: structs.WriteRequest{Token: "root"},
|
|
|
|
}
|
|
|
|
var out bool
|
2015-10-13 23:43:52 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "KVS.Apply", &arg, &out); err != nil {
|
2014-08-13 19:09:39 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-03 20:13:11 +00:00
|
|
|
id := createToken(t, codec, testListRules)
|
2014-08-13 19:09:39 +00:00
|
|
|
|
|
|
|
getR := structs.KeyRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Key: "",
|
|
|
|
QueryOptions: structs.QueryOptions{Token: id},
|
|
|
|
}
|
|
|
|
var dirent structs.IndexedDirEntries
|
2015-10-13 23:43:52 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "KVS.List", &getR, &dirent); err != nil {
|
2014-08-13 19:09:39 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if dirent.Index == 0 {
|
|
|
|
t.Fatalf("Bad: %v", dirent)
|
|
|
|
}
|
|
|
|
if len(dirent.Entries) != 2 {
|
|
|
|
t.Fatalf("Bad: %v", dirent.Entries)
|
|
|
|
}
|
|
|
|
for i := 0; i < len(dirent.Entries); i++ {
|
|
|
|
d := dirent.Entries[i]
|
|
|
|
switch i {
|
|
|
|
case 0:
|
|
|
|
if d.Key != "foo" {
|
|
|
|
t.Fatalf("bad key")
|
|
|
|
}
|
|
|
|
case 1:
|
|
|
|
if d.Key != "test" {
|
|
|
|
t.Fatalf("bad key")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-12-03 17:31:48 +00:00
|
|
|
if !dirent.QueryMeta.ResultsFilteredByACLs {
|
|
|
|
t.Fatal("ResultsFilteredByACLs should be true")
|
|
|
|
}
|
2014-08-13 19:09:39 +00:00
|
|
|
}
|
|
|
|
|
2017-10-02 22:10:21 +00:00
|
|
|
func TestKVSEndpoint_List_ACLEnableKeyListPolicy(t *testing.T) {
|
2020-12-07 18:42:55 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("too slow for testing.Short")
|
|
|
|
}
|
|
|
|
|
2017-10-02 22:10:21 +00:00
|
|
|
t.Parallel()
|
|
|
|
dir1, s1 := testServerWithConfig(t, func(c *Config) {
|
2021-08-06 22:00:58 +00:00
|
|
|
c.PrimaryDatacenter = "dc1"
|
2018-10-19 16:04:07 +00:00
|
|
|
c.ACLsEnabled = true
|
2021-12-07 12:39:28 +00:00
|
|
|
c.ACLInitialManagementToken = "root"
|
2021-08-06 22:39:39 +00:00
|
|
|
c.ACLResolverSettings.ACLDefaultPolicy = "deny"
|
2017-10-02 22:10:21 +00:00
|
|
|
c.ACLEnableKeyListPolicy = true
|
|
|
|
})
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
defer codec.Close()
|
|
|
|
|
2020-05-29 21:16:03 +00:00
|
|
|
testrpc.WaitForTestAgent(t, s1.RPC, "dc1", testrpc.WithToken("root"))
|
2017-10-02 22:10:21 +00:00
|
|
|
|
|
|
|
keys := []string{
|
|
|
|
"abe",
|
|
|
|
"bar/bar1",
|
|
|
|
"bar/bar2",
|
|
|
|
"zip",
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, key := range keys {
|
|
|
|
arg := structs.KVSRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Op: api.KVSet,
|
|
|
|
DirEnt: structs.DirEntry{
|
|
|
|
Key: key,
|
|
|
|
Flags: 1,
|
|
|
|
},
|
|
|
|
WriteRequest: structs.WriteRequest{Token: "root"},
|
|
|
|
}
|
|
|
|
var out bool
|
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "KVS.Apply", &arg, &out); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//write acl policy that denies recursive reads on ""
|
|
|
|
var testListRules1 = `
|
2021-09-03 20:13:11 +00:00
|
|
|
key_prefix "" {
|
2017-10-02 22:10:21 +00:00
|
|
|
policy = "deny"
|
|
|
|
}
|
2021-09-03 20:13:11 +00:00
|
|
|
key_prefix "bar" {
|
2017-10-02 22:10:21 +00:00
|
|
|
policy = "list"
|
|
|
|
}
|
2021-09-03 20:13:11 +00:00
|
|
|
key_prefix "zip" {
|
2017-10-02 22:10:21 +00:00
|
|
|
policy = "read"
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
2021-09-03 20:13:11 +00:00
|
|
|
id := createToken(t, codec, testListRules1)
|
2017-10-02 22:10:21 +00:00
|
|
|
|
|
|
|
//recursive read on empty prefix should fail
|
2017-10-03 20:15:56 +00:00
|
|
|
getReq := structs.KeyRequest{
|
2017-10-02 22:10:21 +00:00
|
|
|
Datacenter: "dc1",
|
|
|
|
Key: "",
|
|
|
|
QueryOptions: structs.QueryOptions{Token: id},
|
|
|
|
}
|
|
|
|
var dirent structs.IndexedDirEntries
|
2017-10-03 20:15:56 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "KVS.List", &getReq, &dirent); !acl.IsErrPermissionDenied(err) {
|
|
|
|
t.Fatalf("expected %v but got err: %v", acl.ErrPermissionDenied, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
//listing keys on empty prefix should fail
|
|
|
|
getKeysReq := structs.KeyListRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Prefix: "",
|
|
|
|
Seperator: "/",
|
|
|
|
QueryOptions: structs.QueryOptions{Token: id},
|
|
|
|
}
|
|
|
|
var keyList structs.IndexedKeyList
|
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "KVS.ListKeys", &getKeysReq, &keyList); !acl.IsErrPermissionDenied(err) {
|
2017-10-02 22:10:21 +00:00
|
|
|
t.Fatalf("expected %v but got err: %v", acl.ErrPermissionDenied, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// recursive read with a prefix that has list permissions should succeed
|
2017-10-03 20:15:56 +00:00
|
|
|
getReq2 := structs.KeyRequest{
|
2017-10-02 22:10:21 +00:00
|
|
|
Datacenter: "dc1",
|
|
|
|
Key: "bar",
|
|
|
|
QueryOptions: structs.QueryOptions{Token: id},
|
|
|
|
}
|
2017-10-03 20:15:56 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "KVS.List", &getReq2, &dirent); err != nil {
|
2017-10-02 22:10:21 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-10-03 20:15:56 +00:00
|
|
|
expectedKeys := []string{"bar/bar1", "bar/bar2"}
|
|
|
|
var actualKeys []string
|
|
|
|
for _, entry := range dirent.Entries {
|
|
|
|
actualKeys = append(actualKeys, entry.Key)
|
2017-10-02 22:10:21 +00:00
|
|
|
}
|
2017-10-03 20:15:56 +00:00
|
|
|
|
2020-06-02 16:41:25 +00:00
|
|
|
require.Equal(t, expectedKeys, actualKeys)
|
2017-10-03 20:15:56 +00:00
|
|
|
|
|
|
|
// list keys with a prefix that has list permissions should succeed
|
|
|
|
getKeysReq2 := structs.KeyListRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Prefix: "bar",
|
|
|
|
QueryOptions: structs.QueryOptions{Token: id},
|
2017-10-02 22:10:21 +00:00
|
|
|
}
|
2017-10-03 20:15:56 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "KVS.ListKeys", &getKeysReq2, &keyList); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2019-07-20 13:37:19 +00:00
|
|
|
actualKeys = keyList.Keys
|
2017-10-02 22:10:21 +00:00
|
|
|
|
2020-06-02 16:41:25 +00:00
|
|
|
require.Equal(t, expectedKeys, actualKeys)
|
2017-10-03 20:15:56 +00:00
|
|
|
|
2017-10-02 22:10:21 +00:00
|
|
|
}
|
|
|
|
|
2014-04-28 23:33:54 +00:00
|
|
|
func TestKVSEndpoint_ListKeys(t *testing.T) {
|
2020-12-07 18:42:55 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("too slow for testing.Short")
|
|
|
|
}
|
|
|
|
|
2017-06-27 13:22:18 +00:00
|
|
|
t.Parallel()
|
2014-04-28 23:33:54 +00:00
|
|
|
dir1, s1 := testServer(t)
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
2015-10-13 23:43:52 +00:00
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
defer codec.Close()
|
2014-04-28 23:33:54 +00:00
|
|
|
|
2019-11-25 17:57:35 +00:00
|
|
|
testrpc.WaitForTestAgent(t, s1.RPC, "dc1")
|
2014-04-28 23:33:54 +00:00
|
|
|
|
|
|
|
keys := []string{
|
|
|
|
"/test/key1",
|
|
|
|
"/test/key2",
|
|
|
|
"/test/sub/key3",
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, key := range keys {
|
|
|
|
arg := structs.KVSRequest{
|
|
|
|
Datacenter: "dc1",
|
2017-04-19 23:00:11 +00:00
|
|
|
Op: api.KVSet,
|
2014-04-28 23:33:54 +00:00
|
|
|
DirEnt: structs.DirEntry{
|
|
|
|
Key: key,
|
|
|
|
Flags: 1,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
var out bool
|
2015-10-13 23:43:52 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "KVS.Apply", &arg, &out); err != nil {
|
2014-04-28 23:33:54 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
getR := structs.KeyListRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Prefix: "/test/",
|
|
|
|
Seperator: "/",
|
|
|
|
}
|
|
|
|
var dirent structs.IndexedKeyList
|
2015-10-13 23:43:52 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "KVS.ListKeys", &getR, &dirent); err != nil {
|
2014-04-28 23:33:54 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if dirent.Index == 0 {
|
|
|
|
t.Fatalf("Bad: %v", dirent)
|
|
|
|
}
|
|
|
|
if len(dirent.Keys) != 3 {
|
|
|
|
t.Fatalf("Bad: %v", dirent.Keys)
|
|
|
|
}
|
|
|
|
if dirent.Keys[0] != "/test/key1" {
|
|
|
|
t.Fatalf("Bad: %v", dirent.Keys)
|
|
|
|
}
|
|
|
|
if dirent.Keys[1] != "/test/key2" {
|
|
|
|
t.Fatalf("Bad: %v", dirent.Keys)
|
|
|
|
}
|
|
|
|
if dirent.Keys[2] != "/test/sub/" {
|
|
|
|
t.Fatalf("Bad: %v", dirent.Keys)
|
|
|
|
}
|
2021-12-03 17:31:48 +00:00
|
|
|
if dirent.QueryMeta.ResultsFilteredByACLs {
|
|
|
|
t.Fatal("ResultsFilteredByACLs should not be true")
|
|
|
|
}
|
2015-10-13 18:48:35 +00:00
|
|
|
|
|
|
|
// Try listing a nonexistent prefix
|
|
|
|
getR.Prefix = "/nope"
|
2015-10-15 22:09:13 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "KVS.ListKeys", &getR, &dirent); err != nil {
|
2015-10-13 18:48:35 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if dirent.Index == 0 {
|
|
|
|
t.Fatalf("Bad: %v", dirent)
|
|
|
|
}
|
|
|
|
if len(dirent.Keys) != 0 {
|
|
|
|
t.Fatalf("Bad: %v", dirent.Keys)
|
|
|
|
}
|
2014-05-19 19:50:29 +00:00
|
|
|
}
|
|
|
|
|
2014-08-13 19:09:39 +00:00
|
|
|
func TestKVSEndpoint_ListKeys_ACLDeny(t *testing.T) {
|
2020-12-07 18:42:55 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("too slow for testing.Short")
|
|
|
|
}
|
|
|
|
|
2017-06-27 13:22:18 +00:00
|
|
|
t.Parallel()
|
2014-08-13 19:09:39 +00:00
|
|
|
dir1, s1 := testServerWithConfig(t, func(c *Config) {
|
2021-08-06 22:00:58 +00:00
|
|
|
c.PrimaryDatacenter = "dc1"
|
2018-10-19 16:04:07 +00:00
|
|
|
c.ACLsEnabled = true
|
2021-12-07 12:39:28 +00:00
|
|
|
c.ACLInitialManagementToken = "root"
|
2021-08-06 22:39:39 +00:00
|
|
|
c.ACLResolverSettings.ACLDefaultPolicy = "deny"
|
2014-08-13 19:09:39 +00:00
|
|
|
})
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
2015-10-13 23:43:52 +00:00
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
defer codec.Close()
|
2014-08-13 19:09:39 +00:00
|
|
|
|
2020-05-29 21:16:03 +00:00
|
|
|
testrpc.WaitForTestAgent(t, s1.RPC, "dc1", testrpc.WithToken("root"))
|
2014-08-13 19:09:39 +00:00
|
|
|
|
|
|
|
keys := []string{
|
|
|
|
"abe",
|
|
|
|
"bar",
|
|
|
|
"foo",
|
|
|
|
"test",
|
|
|
|
"zip",
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, key := range keys {
|
|
|
|
arg := structs.KVSRequest{
|
|
|
|
Datacenter: "dc1",
|
2017-04-19 23:00:11 +00:00
|
|
|
Op: api.KVSet,
|
2014-08-13 19:09:39 +00:00
|
|
|
DirEnt: structs.DirEntry{
|
|
|
|
Key: key,
|
|
|
|
Flags: 1,
|
|
|
|
},
|
|
|
|
WriteRequest: structs.WriteRequest{Token: "root"},
|
|
|
|
}
|
|
|
|
var out bool
|
2015-10-13 23:43:52 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "KVS.Apply", &arg, &out); err != nil {
|
2014-08-13 19:09:39 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-03 20:13:11 +00:00
|
|
|
id := createToken(t, codec, testListRules)
|
2014-08-13 19:09:39 +00:00
|
|
|
|
|
|
|
getR := structs.KeyListRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Prefix: "",
|
|
|
|
Seperator: "/",
|
|
|
|
QueryOptions: structs.QueryOptions{Token: id},
|
|
|
|
}
|
|
|
|
var dirent structs.IndexedKeyList
|
2015-10-13 23:43:52 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "KVS.ListKeys", &getR, &dirent); err != nil {
|
2014-08-13 19:09:39 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if dirent.Index == 0 {
|
|
|
|
t.Fatalf("Bad: %v", dirent)
|
|
|
|
}
|
|
|
|
if len(dirent.Keys) != 2 {
|
|
|
|
t.Fatalf("Bad: %v", dirent.Keys)
|
|
|
|
}
|
|
|
|
if dirent.Keys[0] != "foo" {
|
|
|
|
t.Fatalf("Bad: %v", dirent.Keys)
|
|
|
|
}
|
|
|
|
if dirent.Keys[1] != "test" {
|
|
|
|
t.Fatalf("Bad: %v", dirent.Keys)
|
|
|
|
}
|
2021-12-03 17:31:48 +00:00
|
|
|
if !dirent.QueryMeta.ResultsFilteredByACLs {
|
|
|
|
t.Fatal("ResultsFilteredByACLs should be true")
|
|
|
|
}
|
2014-08-13 19:09:39 +00:00
|
|
|
}
|
|
|
|
|
2014-05-19 19:50:29 +00:00
|
|
|
func TestKVS_Apply_LockDelay(t *testing.T) {
|
2020-12-07 18:42:55 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("too slow for testing.Short")
|
|
|
|
}
|
|
|
|
|
2017-06-27 13:22:18 +00:00
|
|
|
t.Parallel()
|
2014-05-19 19:50:29 +00:00
|
|
|
dir1, s1 := testServer(t)
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
2015-10-13 23:43:52 +00:00
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
defer codec.Close()
|
2014-05-19 19:50:29 +00:00
|
|
|
|
2019-11-25 17:57:35 +00:00
|
|
|
testrpc.WaitForTestAgent(t, s1.RPC, "dc1")
|
2014-05-19 19:50:29 +00:00
|
|
|
|
2016-05-10 18:23:47 +00:00
|
|
|
// Create and invalidate a session with a lock.
|
2015-10-13 05:21:39 +00:00
|
|
|
state := s1.fsm.State()
|
2019-11-25 17:07:04 +00:00
|
|
|
|
2015-10-09 19:00:15 +00:00
|
|
|
if err := state.EnsureNode(1, &structs.Node{Node: "foo", Address: "127.0.0.1"}); err != nil {
|
2014-11-01 21:56:48 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
2014-05-19 19:50:29 +00:00
|
|
|
}
|
|
|
|
session := &structs.Session{
|
2014-10-09 18:54:47 +00:00
|
|
|
ID: generateUUID(),
|
2014-05-19 19:50:29 +00:00
|
|
|
Node: "foo",
|
|
|
|
LockDelay: 50 * time.Millisecond,
|
|
|
|
}
|
|
|
|
if err := state.SessionCreate(2, session); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
id := session.ID
|
|
|
|
d := &structs.DirEntry{
|
|
|
|
Key: "test",
|
|
|
|
Session: id,
|
|
|
|
}
|
|
|
|
if ok, err := state.KVSLock(3, d); err != nil || !ok {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2019-11-25 17:07:04 +00:00
|
|
|
|
|
|
|
if err := state.SessionDestroy(4, id, nil); err != nil {
|
2014-05-19 19:50:29 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2014-04-28 23:33:54 +00:00
|
|
|
|
2016-05-10 18:23:47 +00:00
|
|
|
// Make a new session that is valid.
|
2014-05-19 19:50:29 +00:00
|
|
|
if err := state.SessionCreate(5, session); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2017-04-21 00:02:42 +00:00
|
|
|
validID := session.ID
|
2014-05-19 19:50:29 +00:00
|
|
|
|
2016-05-10 18:23:47 +00:00
|
|
|
// Make a lock request.
|
2014-05-19 19:50:29 +00:00
|
|
|
arg := structs.KVSRequest{
|
|
|
|
Datacenter: "dc1",
|
2017-04-19 23:00:11 +00:00
|
|
|
Op: api.KVLock,
|
2014-05-19 19:50:29 +00:00
|
|
|
DirEnt: structs.DirEntry{
|
|
|
|
Key: "test",
|
2017-04-21 00:02:42 +00:00
|
|
|
Session: validID,
|
2014-05-19 19:50:29 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
var out bool
|
2015-10-13 23:43:52 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "KVS.Apply", &arg, &out); err != nil {
|
2014-05-19 19:50:29 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if out != false {
|
|
|
|
t.Fatalf("should not acquire")
|
|
|
|
}
|
|
|
|
|
2016-05-10 18:23:47 +00:00
|
|
|
// Wait for lock-delay.
|
2014-05-19 19:50:29 +00:00
|
|
|
time.Sleep(50 * time.Millisecond)
|
|
|
|
|
2016-05-10 18:23:47 +00:00
|
|
|
// Should acquire.
|
2015-10-13 23:43:52 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "KVS.Apply", &arg, &out); err != nil {
|
2014-05-19 19:50:29 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if out != true {
|
|
|
|
t.Fatalf("should acquire")
|
|
|
|
}
|
2014-04-28 23:33:54 +00:00
|
|
|
}
|
2014-08-13 19:09:39 +00:00
|
|
|
|
2016-01-20 05:46:22 +00:00
|
|
|
func TestKVS_Issue_1626(t *testing.T) {
|
2020-12-07 18:42:55 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("too slow for testing.Short")
|
|
|
|
}
|
|
|
|
|
2017-06-27 13:22:18 +00:00
|
|
|
t.Parallel()
|
2016-01-20 05:46:22 +00:00
|
|
|
dir1, s1 := testServer(t)
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
defer codec.Close()
|
|
|
|
|
2019-11-25 17:57:35 +00:00
|
|
|
testrpc.WaitForTestAgent(t, s1.RPC, "dc1")
|
2016-01-20 05:46:22 +00:00
|
|
|
|
|
|
|
// Set up the first key.
|
|
|
|
{
|
|
|
|
arg := structs.KVSRequest{
|
|
|
|
Datacenter: "dc1",
|
2017-04-19 23:00:11 +00:00
|
|
|
Op: api.KVSet,
|
2016-01-20 05:46:22 +00:00
|
|
|
DirEnt: structs.DirEntry{
|
|
|
|
Key: "foo/test",
|
|
|
|
Value: []byte("test"),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
var out bool
|
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "KVS.Apply", &arg, &out); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Retrieve the base key and snag the index.
|
|
|
|
var index uint64
|
|
|
|
{
|
|
|
|
getR := structs.KeyRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Key: "foo/test",
|
|
|
|
}
|
|
|
|
var dirent structs.IndexedDirEntries
|
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "KVS.Get", &getR, &dirent); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if dirent.Index == 0 {
|
|
|
|
t.Fatalf("Bad: %v", dirent)
|
|
|
|
}
|
|
|
|
if len(dirent.Entries) != 1 {
|
|
|
|
t.Fatalf("Bad: %v", dirent)
|
|
|
|
}
|
|
|
|
d := dirent.Entries[0]
|
|
|
|
if string(d.Value) != "test" {
|
|
|
|
t.Fatalf("bad: %v", d)
|
|
|
|
}
|
|
|
|
|
|
|
|
index = dirent.Index
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set up a blocking query on the base key.
|
|
|
|
doneCh := make(chan *structs.IndexedDirEntries, 1)
|
|
|
|
go func() {
|
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
defer codec.Close()
|
|
|
|
|
|
|
|
getR := structs.KeyRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Key: "foo/test",
|
|
|
|
QueryOptions: structs.QueryOptions{
|
|
|
|
MinQueryIndex: index,
|
|
|
|
MaxQueryTime: 3 * time.Second,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
var dirent structs.IndexedDirEntries
|
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "KVS.Get", &getR, &dirent); err != nil {
|
2020-06-05 20:52:31 +00:00
|
|
|
t.Errorf("RPC call failed: %v", err)
|
|
|
|
return
|
2016-01-20 05:46:22 +00:00
|
|
|
}
|
|
|
|
doneCh <- &dirent
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Now update a second key with a prefix that has the first key name
|
|
|
|
// as part of it.
|
|
|
|
{
|
|
|
|
arg := structs.KVSRequest{
|
|
|
|
Datacenter: "dc1",
|
2017-04-19 23:00:11 +00:00
|
|
|
Op: api.KVSet,
|
2016-01-20 05:46:22 +00:00
|
|
|
DirEnt: structs.DirEntry{
|
|
|
|
Key: "foo/test2",
|
|
|
|
Value: []byte("test"),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
var out bool
|
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "KVS.Apply", &arg, &out); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure the blocking query didn't wake up for this update.
|
|
|
|
select {
|
|
|
|
case <-doneCh:
|
|
|
|
t.Fatalf("Blocking query should not have completed")
|
|
|
|
case <-time.After(1 * time.Second):
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now update the first key's payload.
|
|
|
|
{
|
|
|
|
arg := structs.KVSRequest{
|
|
|
|
Datacenter: "dc1",
|
2017-04-19 23:00:11 +00:00
|
|
|
Op: api.KVSet,
|
2016-01-20 05:46:22 +00:00
|
|
|
DirEnt: structs.DirEntry{
|
|
|
|
Key: "foo/test",
|
|
|
|
Value: []byte("updated"),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
var out bool
|
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "KVS.Apply", &arg, &out); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure the blocking query wakes up for the final update.
|
|
|
|
select {
|
|
|
|
case dirent := <-doneCh:
|
|
|
|
if dirent.Index <= index {
|
|
|
|
t.Fatalf("Bad: %v", dirent)
|
|
|
|
}
|
|
|
|
if len(dirent.Entries) != 1 {
|
|
|
|
t.Fatalf("Bad: %v", dirent)
|
|
|
|
}
|
|
|
|
d := dirent.Entries[0]
|
|
|
|
if string(d.Value) != "updated" {
|
|
|
|
t.Fatalf("bad: %v", d)
|
|
|
|
}
|
|
|
|
case <-time.After(1 * time.Second):
|
|
|
|
t.Fatalf("Blocking query should have completed")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-13 19:09:39 +00:00
|
|
|
var testListRules = `
|
|
|
|
key "" {
|
|
|
|
policy = "deny"
|
|
|
|
}
|
|
|
|
key "foo" {
|
|
|
|
policy = "read"
|
|
|
|
}
|
|
|
|
key "test" {
|
|
|
|
policy = "write"
|
|
|
|
}
|
2014-08-15 02:25:12 +00:00
|
|
|
key "test/priv" {
|
|
|
|
policy = "read"
|
|
|
|
}
|
2014-08-13 19:09:39 +00:00
|
|
|
`
|