2015-04-09 20:23:14 +00:00
|
|
|
package consul
|
|
|
|
|
|
|
|
import (
|
2015-05-14 01:22:34 +00:00
|
|
|
"fmt"
|
2017-05-26 00:37:16 +00:00
|
|
|
"math"
|
2015-04-09 20:23:14 +00:00
|
|
|
"math/rand"
|
2017-10-31 20:34:49 +00:00
|
|
|
"net/rpc"
|
2015-04-09 20:23:14 +00:00
|
|
|
"os"
|
2015-06-23 02:14:02 +00:00
|
|
|
"strings"
|
2015-04-09 20:23:14 +00:00
|
|
|
"testing"
|
2015-04-13 20:45:42 +00:00
|
|
|
"time"
|
2015-04-09 20:23:14 +00:00
|
|
|
|
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-08-14 14:36:07 +00:00
|
|
|
"github.com/hashicorp/consul/lib"
|
2019-03-27 12:54:56 +00:00
|
|
|
"github.com/hashicorp/consul/sdk/testutil/retry"
|
2019-04-25 16:26:33 +00:00
|
|
|
"github.com/hashicorp/consul/testrpc"
|
2015-10-15 23:07:16 +00:00
|
|
|
"github.com/hashicorp/net-rpc-msgpackrpc"
|
2015-04-09 20:23:14 +00:00
|
|
|
"github.com/hashicorp/serf/coordinate"
|
2017-08-14 14:36:07 +00:00
|
|
|
"github.com/pascaldekloe/goe/verify"
|
2015-04-09 20:23:14 +00:00
|
|
|
)
|
|
|
|
|
2015-06-06 03:31:33 +00:00
|
|
|
// generateRandomCoordinate creates a random coordinate. This mucks with the
|
|
|
|
// underlying structure directly, so it's not really useful for any particular
|
|
|
|
// position in the network, but it's a good payload to send through to make
|
|
|
|
// sure things come out the other side or get stored correctly.
|
|
|
|
func generateRandomCoordinate() *coordinate.Coordinate {
|
2015-04-09 20:23:14 +00:00
|
|
|
config := coordinate.DefaultConfig()
|
2015-06-06 03:31:33 +00:00
|
|
|
coord := coordinate.NewCoordinate(config)
|
|
|
|
for i := range coord.Vec {
|
|
|
|
coord.Vec[i] = rand.NormFloat64()
|
2015-04-13 20:45:42 +00:00
|
|
|
}
|
2015-06-06 03:31:33 +00:00
|
|
|
coord.Error = rand.NormFloat64()
|
|
|
|
coord.Adjustment = rand.NormFloat64()
|
|
|
|
return coord
|
2015-04-09 20:23:14 +00:00
|
|
|
}
|
|
|
|
|
2015-05-14 02:09:58 +00:00
|
|
|
func TestCoordinate_Update(t *testing.T) {
|
2017-06-27 13:22:18 +00:00
|
|
|
t.Parallel()
|
|
|
|
dir1, s1 := testServerWithConfig(t, func(c *Config) {
|
|
|
|
c.CoordinateUpdatePeriod = 500 * time.Millisecond
|
|
|
|
c.CoordinateUpdateBatchSize = 5
|
|
|
|
c.CoordinateUpdateMaxBatches = 2
|
|
|
|
})
|
2015-06-06 03:31:33 +00:00
|
|
|
defer os.RemoveAll(dir1)
|
2015-04-09 20:23:14 +00:00
|
|
|
defer s1.Shutdown()
|
2015-05-14 01:22:34 +00:00
|
|
|
|
2015-10-15 23:07:16 +00:00
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
defer codec.Close()
|
2018-09-12 13:49:27 +00:00
|
|
|
testrpc.WaitForTestAgent(t, s1.RPC, "dc1")
|
2015-04-09 20:23:14 +00:00
|
|
|
|
2015-10-23 22:19:14 +00:00
|
|
|
// Register some nodes.
|
|
|
|
nodes := []string{"node1", "node2"}
|
2017-10-31 20:34:49 +00:00
|
|
|
if err := registerNodes(nodes, codec); err != nil {
|
|
|
|
t.Fatal(err)
|
2015-10-23 22:19:14 +00:00
|
|
|
}
|
|
|
|
|
2015-06-23 02:14:02 +00:00
|
|
|
// Send an update for the first node.
|
2015-05-08 08:31:34 +00:00
|
|
|
arg1 := structs.CoordinateUpdateRequest{
|
2015-04-18 21:05:29 +00:00
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "node1",
|
2015-06-06 03:31:33 +00:00
|
|
|
Coord: generateRandomCoordinate(),
|
2015-04-09 20:23:14 +00:00
|
|
|
}
|
2015-06-23 02:14:02 +00:00
|
|
|
var out struct{}
|
2015-10-15 23:07:16 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Coordinate.Update", &arg1, &out); err != nil {
|
2015-06-23 02:14:02 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2015-04-09 20:23:14 +00:00
|
|
|
|
2015-06-23 02:14:02 +00:00
|
|
|
// Send an update for the second node.
|
2015-05-08 08:31:34 +00:00
|
|
|
arg2 := structs.CoordinateUpdateRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "node2",
|
2015-06-06 03:31:33 +00:00
|
|
|
Coord: generateRandomCoordinate(),
|
2015-05-08 08:31:34 +00:00
|
|
|
}
|
2015-10-15 23:07:16 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Coordinate.Update", &arg2, &out); err != nil {
|
2015-04-09 20:23:14 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-06-23 02:14:02 +00:00
|
|
|
// Make sure the updates did not yet apply because the update period
|
|
|
|
// hasn't expired.
|
2015-04-09 20:23:14 +00:00
|
|
|
state := s1.fsm.State()
|
2017-10-27 02:16:40 +00:00
|
|
|
_, c, err := state.Coordinate("node1", nil)
|
2015-04-09 20:23:14 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2017-08-14 14:36:07 +00:00
|
|
|
verify.Values(t, "", c, lib.CoordinateSet{})
|
|
|
|
|
2017-10-27 02:16:40 +00:00
|
|
|
_, c, err = state.Coordinate("node2", nil)
|
2015-06-23 02:14:02 +00:00
|
|
|
if err != nil {
|
2015-05-08 08:31:34 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2017-08-14 14:36:07 +00:00
|
|
|
verify.Values(t, "", c, lib.CoordinateSet{})
|
2015-06-06 03:31:33 +00:00
|
|
|
|
2015-10-23 22:19:14 +00:00
|
|
|
// Send another update for the second node. It should take precedence
|
|
|
|
// since there will be two updates in the same batch.
|
|
|
|
arg2.Coord = generateRandomCoordinate()
|
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Coordinate.Update", &arg2, &out); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-06-23 02:14:02 +00:00
|
|
|
// Wait a while and the updates should get picked up.
|
2016-03-21 23:44:35 +00:00
|
|
|
time.Sleep(3 * s1.config.CoordinateUpdatePeriod)
|
2017-10-27 02:16:40 +00:00
|
|
|
_, c, err = state.Coordinate("node1", nil)
|
2015-05-08 08:31:34 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2017-08-14 14:36:07 +00:00
|
|
|
expected := lib.CoordinateSet{
|
|
|
|
"": arg1.Coord,
|
2015-05-08 08:31:34 +00:00
|
|
|
}
|
2017-08-14 14:36:07 +00:00
|
|
|
verify.Values(t, "", c, expected)
|
|
|
|
|
2017-10-27 02:16:40 +00:00
|
|
|
_, c, err = state.Coordinate("node2", nil)
|
2015-05-08 08:31:34 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2017-08-14 14:36:07 +00:00
|
|
|
expected = lib.CoordinateSet{
|
|
|
|
"": arg2.Coord,
|
2015-05-08 08:31:34 +00:00
|
|
|
}
|
2017-08-14 14:36:07 +00:00
|
|
|
verify.Values(t, "", c, expected)
|
2015-06-06 03:31:33 +00:00
|
|
|
|
2015-10-23 22:19:14 +00:00
|
|
|
// Register a bunch of additional nodes.
|
|
|
|
spamLen := s1.config.CoordinateUpdateBatchSize*s1.config.CoordinateUpdateMaxBatches + 1
|
|
|
|
for i := 0; i < spamLen; i++ {
|
|
|
|
req := structs.RegisterRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: fmt.Sprintf("bogusnode%d", i),
|
|
|
|
Address: "127.0.0.1",
|
|
|
|
}
|
|
|
|
var reply struct{}
|
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Catalog.Register", &req, &reply); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-29 22:53:29 +00:00
|
|
|
// Now spam some coordinate updates and make sure it starts throwing
|
2015-07-29 23:33:25 +00:00
|
|
|
// them away if they exceed the batch allowance. Note we have to make
|
2015-06-29 22:53:29 +00:00
|
|
|
// unique names since these are held in map by node name.
|
|
|
|
for i := 0; i < spamLen; i++ {
|
|
|
|
arg1.Node = fmt.Sprintf("bogusnode%d", i)
|
2015-06-06 03:31:33 +00:00
|
|
|
arg1.Coord = generateRandomCoordinate()
|
2015-10-15 23:07:16 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Coordinate.Update", &arg1, &out); err != nil {
|
2015-06-06 03:31:33 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2015-04-09 20:23:14 +00:00
|
|
|
}
|
2015-06-06 03:31:33 +00:00
|
|
|
|
2015-06-23 02:14:02 +00:00
|
|
|
// Wait a little while for the batch routine to run, then make sure
|
2015-06-29 22:53:29 +00:00
|
|
|
// exactly one of the updates got dropped (we won't know which one).
|
2016-03-21 23:44:35 +00:00
|
|
|
time.Sleep(3 * s1.config.CoordinateUpdatePeriod)
|
2015-06-29 22:53:29 +00:00
|
|
|
numDropped := 0
|
|
|
|
for i := 0; i < spamLen; i++ {
|
2017-10-27 02:16:40 +00:00
|
|
|
_, c, err = state.Coordinate(fmt.Sprintf("bogusnode%d", i), nil)
|
2015-06-29 22:53:29 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2017-08-14 14:36:07 +00:00
|
|
|
if len(c) == 0 {
|
2015-06-29 22:53:29 +00:00
|
|
|
numDropped++
|
|
|
|
}
|
2015-06-06 03:31:33 +00:00
|
|
|
}
|
2015-06-29 22:53:29 +00:00
|
|
|
if numDropped != 1 {
|
|
|
|
t.Fatalf("wrong number of coordinates dropped, %d != 1", numDropped)
|
2015-06-06 03:31:33 +00:00
|
|
|
}
|
2015-06-27 20:26:41 +00:00
|
|
|
|
2017-05-26 00:37:16 +00:00
|
|
|
// Send a coordinate with a NaN to make sure that we don't absorb that
|
|
|
|
// into the database.
|
|
|
|
arg2.Coord.Vec[0] = math.NaN()
|
|
|
|
err = msgpackrpc.CallWithCodec(codec, "Coordinate.Update", &arg2, &out)
|
|
|
|
if err == nil || !strings.Contains(err.Error(), "invalid coordinate") {
|
|
|
|
t.Fatalf("should have failed with an error, got %v", err)
|
|
|
|
}
|
|
|
|
|
2015-06-27 20:26:41 +00:00
|
|
|
// Finally, send a coordinate with the wrong dimensionality to make sure
|
|
|
|
// there are no panics, and that it gets rejected.
|
|
|
|
arg2.Coord.Vec = make([]float64, 2*len(arg2.Coord.Vec))
|
2015-10-15 23:07:16 +00:00
|
|
|
err = msgpackrpc.CallWithCodec(codec, "Coordinate.Update", &arg2, &out)
|
2017-05-26 00:37:16 +00:00
|
|
|
if err == nil || !strings.Contains(err.Error(), "incompatible coordinate") {
|
2015-06-27 20:26:41 +00:00
|
|
|
t.Fatalf("should have failed with an error, got %v", err)
|
|
|
|
}
|
2015-04-29 01:47:41 +00:00
|
|
|
}
|
|
|
|
|
2016-12-12 19:58:31 +00:00
|
|
|
func TestCoordinate_Update_ACLDeny(t *testing.T) {
|
2017-06-27 13:22:18 +00:00
|
|
|
t.Parallel()
|
2016-12-12 19:58:31 +00:00
|
|
|
dir1, s1 := testServerWithConfig(t, func(c *Config) {
|
|
|
|
c.ACLDatacenter = "dc1"
|
2018-10-19 16:04:07 +00:00
|
|
|
c.ACLsEnabled = true
|
2016-12-12 19:58:31 +00:00
|
|
|
c.ACLMasterToken = "root"
|
|
|
|
c.ACLDefaultPolicy = "deny"
|
|
|
|
c.ACLEnforceVersion8 = false
|
|
|
|
})
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
defer codec.Close()
|
|
|
|
|
2017-04-19 23:00:11 +00:00
|
|
|
testrpc.WaitForLeader(t, s1.RPC, "dc1")
|
2016-12-12 19:58:31 +00:00
|
|
|
|
|
|
|
// Register some nodes.
|
|
|
|
nodes := []string{"node1", "node2"}
|
2017-10-31 20:34:49 +00:00
|
|
|
if err := registerNodes(nodes, codec); err != nil {
|
|
|
|
t.Fatal(err)
|
2016-12-12 19:58:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Send an update for the first node. This should go through since we
|
|
|
|
// don't have version 8 ACLs enforced yet.
|
|
|
|
req := structs.CoordinateUpdateRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "node1",
|
|
|
|
Coord: generateRandomCoordinate(),
|
|
|
|
}
|
|
|
|
var out struct{}
|
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Coordinate.Update", &req, &out); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now turn on version 8 enforcement and try again.
|
|
|
|
s1.config.ACLEnforceVersion8 = true
|
|
|
|
err := msgpackrpc.CallWithCodec(codec, "Coordinate.Update", &req, &out)
|
2017-08-23 14:52:48 +00:00
|
|
|
if !acl.IsErrPermissionDenied(err) {
|
2016-12-12 19:58:31 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create an ACL that can write to the node.
|
|
|
|
arg := structs.ACLRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Op: structs.ACLSet,
|
|
|
|
ACL: structs.ACL{
|
|
|
|
Name: "User token",
|
2018-10-19 16:04:07 +00:00
|
|
|
Type: structs.ACLTokenTypeClient,
|
2016-12-12 19:58:31 +00:00
|
|
|
Rules: `
|
|
|
|
node "node1" {
|
|
|
|
policy = "write"
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
},
|
|
|
|
WriteRequest: structs.WriteRequest{Token: "root"},
|
|
|
|
}
|
|
|
|
var id string
|
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "ACL.Apply", &arg, &id); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// With the token, it should now go through.
|
|
|
|
req.Token = id
|
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Coordinate.Update", &req, &out); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// But it should be blocked for the other node.
|
|
|
|
req.Node = "node2"
|
|
|
|
err = msgpackrpc.CallWithCodec(codec, "Coordinate.Update", &req, &out)
|
2017-08-23 14:52:48 +00:00
|
|
|
if !acl.IsErrPermissionDenied(err) {
|
2016-12-12 19:58:31 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-29 23:33:25 +00:00
|
|
|
func TestCoordinate_ListDatacenters(t *testing.T) {
|
2017-06-27 13:22:18 +00:00
|
|
|
t.Parallel()
|
2015-07-29 23:33:25 +00:00
|
|
|
dir1, s1 := testServer(t)
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
2015-10-15 23:07:16 +00:00
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
defer codec.Close()
|
2015-07-29 23:33:25 +00:00
|
|
|
|
2017-04-19 23:00:11 +00:00
|
|
|
testrpc.WaitForLeader(t, s1.RPC, "dc1")
|
2015-07-29 23:33:25 +00:00
|
|
|
|
|
|
|
// It's super hard to force the Serfs into a known configuration of
|
|
|
|
// coordinates, so the best we can do is make sure our own DC shows
|
|
|
|
// up in the list with the proper coordinates. The guts of the algorithm
|
|
|
|
// are extensively tested in rtt_test.go using a mock database.
|
|
|
|
var out []structs.DatacenterMap
|
2015-10-15 23:07:16 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Coordinate.ListDatacenters", struct{}{}, &out); err != nil {
|
2015-07-29 23:33:25 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if len(out) != 1 ||
|
|
|
|
out[0].Datacenter != "dc1" ||
|
|
|
|
len(out[0].Coordinates) != 1 ||
|
|
|
|
out[0].Coordinates[0].Node != s1.config.NodeName {
|
|
|
|
t.Fatalf("bad: %v", out)
|
|
|
|
}
|
|
|
|
c, err := s1.serfWAN.GetCoordinate()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("bad: %v", err)
|
|
|
|
}
|
2017-08-14 14:36:07 +00:00
|
|
|
verify.Values(t, "", c, out[0].Coordinates[0].Coord)
|
2015-07-29 23:33:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestCoordinate_ListNodes(t *testing.T) {
|
2017-06-27 13:22:18 +00:00
|
|
|
t.Parallel()
|
2015-07-29 23:33:25 +00:00
|
|
|
dir1, s1 := testServer(t)
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
|
2015-10-15 23:07:16 +00:00
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
defer codec.Close()
|
2017-04-19 23:00:11 +00:00
|
|
|
testrpc.WaitForLeader(t, s1.RPC, "dc1")
|
2015-07-29 23:33:25 +00:00
|
|
|
|
2015-10-23 22:19:14 +00:00
|
|
|
// Register some nodes.
|
|
|
|
nodes := []string{"foo", "bar", "baz"}
|
2017-10-31 20:34:49 +00:00
|
|
|
if err := registerNodes(nodes, codec); err != nil {
|
|
|
|
t.Fatal(err)
|
2015-10-23 22:19:14 +00:00
|
|
|
}
|
|
|
|
|
2016-12-12 19:58:31 +00:00
|
|
|
// Send coordinate updates for a few nodes.
|
2015-07-29 23:33:25 +00:00
|
|
|
arg1 := structs.CoordinateUpdateRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "foo",
|
|
|
|
Coord: generateRandomCoordinate(),
|
|
|
|
}
|
|
|
|
var out struct{}
|
2015-10-15 23:07:16 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Coordinate.Update", &arg1, &out); err != nil {
|
2015-07-29 23:33:25 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
arg2 := structs.CoordinateUpdateRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "bar",
|
|
|
|
Coord: generateRandomCoordinate(),
|
|
|
|
}
|
2015-10-15 23:07:16 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Coordinate.Update", &arg2, &out); err != nil {
|
2015-07-29 23:33:25 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
arg3 := structs.CoordinateUpdateRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "baz",
|
|
|
|
Coord: generateRandomCoordinate(),
|
|
|
|
}
|
2015-10-15 23:07:16 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Coordinate.Update", &arg3, &out); err != nil {
|
2015-07-29 23:33:25 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2015-07-30 18:31:35 +00:00
|
|
|
// Now query back for all the nodes.
|
2017-05-04 22:52:53 +00:00
|
|
|
retry.Run(t, func(r *retry.R) {
|
2016-12-12 19:58:31 +00:00
|
|
|
arg := structs.DCSpecificRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
}
|
|
|
|
resp := structs.IndexedCoordinates{}
|
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Coordinate.ListNodes", &arg, &resp); err != nil {
|
2017-04-29 16:34:02 +00:00
|
|
|
r.Fatalf("err: %v", err)
|
2016-12-12 19:58:31 +00:00
|
|
|
}
|
|
|
|
if len(resp.Coordinates) != 3 ||
|
|
|
|
resp.Coordinates[0].Node != "bar" ||
|
|
|
|
resp.Coordinates[1].Node != "baz" ||
|
|
|
|
resp.Coordinates[2].Node != "foo" {
|
2017-04-29 16:34:02 +00:00
|
|
|
r.Fatalf("bad: %v", resp.Coordinates)
|
2016-12-12 19:58:31 +00:00
|
|
|
}
|
2017-08-14 14:36:07 +00:00
|
|
|
verify.Values(t, "", resp.Coordinates[0].Coord, arg2.Coord) // bar
|
|
|
|
verify.Values(t, "", resp.Coordinates[1].Coord, arg3.Coord) // baz
|
|
|
|
verify.Values(t, "", resp.Coordinates[2].Coord, arg1.Coord) // foo
|
2017-04-29 16:34:02 +00:00
|
|
|
})
|
2016-12-12 19:58:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestCoordinate_ListNodes_ACLFilter(t *testing.T) {
|
2017-06-27 13:22:18 +00:00
|
|
|
t.Parallel()
|
2016-12-12 19:58:31 +00:00
|
|
|
dir1, s1 := testServerWithConfig(t, func(c *Config) {
|
|
|
|
c.ACLDatacenter = "dc1"
|
2018-10-19 16:04:07 +00:00
|
|
|
c.ACLsEnabled = true
|
2016-12-12 19:58:31 +00:00
|
|
|
c.ACLMasterToken = "root"
|
|
|
|
c.ACLDefaultPolicy = "deny"
|
|
|
|
c.ACLEnforceVersion8 = false
|
|
|
|
})
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
defer codec.Close()
|
|
|
|
|
2017-04-19 23:00:11 +00:00
|
|
|
testrpc.WaitForLeader(t, s1.RPC, "dc1")
|
2016-12-12 19:58:31 +00:00
|
|
|
|
|
|
|
// Register some nodes.
|
|
|
|
nodes := []string{"foo", "bar", "baz"}
|
|
|
|
for _, node := range nodes {
|
|
|
|
req := structs.RegisterRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: node,
|
|
|
|
Address: "127.0.0.1",
|
|
|
|
WriteRequest: structs.WriteRequest{
|
|
|
|
Token: "root",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
var reply struct{}
|
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Catalog.Register", &req, &reply); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send coordinate updates for a few nodes.
|
|
|
|
arg1 := structs.CoordinateUpdateRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "foo",
|
|
|
|
Coord: generateRandomCoordinate(),
|
|
|
|
WriteRequest: structs.WriteRequest{
|
|
|
|
Token: "root",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
var out struct{}
|
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Coordinate.Update", &arg1, &out); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
arg2 := structs.CoordinateUpdateRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "bar",
|
|
|
|
Coord: generateRandomCoordinate(),
|
|
|
|
WriteRequest: structs.WriteRequest{
|
|
|
|
Token: "root",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Coordinate.Update", &arg2, &out); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
arg3 := structs.CoordinateUpdateRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "baz",
|
|
|
|
Coord: generateRandomCoordinate(),
|
|
|
|
WriteRequest: structs.WriteRequest{
|
|
|
|
Token: "root",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Coordinate.Update", &arg3, &out); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
// Wait for all the coordinate updates to apply. Since we aren't
|
|
|
|
// enforcing version 8 ACLs, this should also allow us to read
|
|
|
|
// everything back without a token.
|
2017-05-04 22:52:53 +00:00
|
|
|
retry.Run(t, func(r *retry.R) {
|
2016-12-12 19:58:31 +00:00
|
|
|
arg := structs.DCSpecificRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
}
|
|
|
|
resp := structs.IndexedCoordinates{}
|
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Coordinate.ListNodes", &arg, &resp); err != nil {
|
2017-04-29 16:34:02 +00:00
|
|
|
r.Fatalf("err: %v", err)
|
2016-12-12 19:58:31 +00:00
|
|
|
}
|
2017-04-29 16:34:02 +00:00
|
|
|
if got, want := len(resp.Coordinates), 3; got != want {
|
|
|
|
r.Fatalf("got %d coordinates want %d", got, want)
|
2016-12-12 19:58:31 +00:00
|
|
|
}
|
2017-04-29 16:34:02 +00:00
|
|
|
})
|
2016-12-12 19:58:31 +00:00
|
|
|
|
|
|
|
// Now that we've waited for the batch processing to ingest the
|
|
|
|
// coordinates we can do the rest of the requests without the loop. We
|
|
|
|
// will start by turning on version 8 ACL support which should block
|
|
|
|
// everything.
|
|
|
|
s1.config.ACLEnforceVersion8 = true
|
2015-07-29 23:33:25 +00:00
|
|
|
arg := structs.DCSpecificRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
}
|
|
|
|
resp := structs.IndexedCoordinates{}
|
2015-10-15 23:07:16 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Coordinate.ListNodes", &arg, &resp); err != nil {
|
2015-07-29 23:33:25 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2016-12-12 19:58:31 +00:00
|
|
|
if len(resp.Coordinates) != 0 {
|
|
|
|
t.Fatalf("bad: %#v", resp.Coordinates)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create an ACL that can read one of the nodes.
|
|
|
|
var id string
|
|
|
|
{
|
|
|
|
req := structs.ACLRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Op: structs.ACLSet,
|
|
|
|
ACL: structs.ACL{
|
|
|
|
Name: "User token",
|
2018-10-19 16:04:07 +00:00
|
|
|
Type: structs.ACLTokenTypeClient,
|
2016-12-12 19:58:31 +00:00
|
|
|
Rules: `
|
|
|
|
node "foo" {
|
|
|
|
policy = "read"
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
},
|
|
|
|
WriteRequest: structs.WriteRequest{Token: "root"},
|
|
|
|
}
|
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "ACL.Apply", &req, &id); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// With the token, it should now go through.
|
|
|
|
arg.Token = id
|
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Coordinate.ListNodes", &arg, &resp); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if len(resp.Coordinates) != 1 || resp.Coordinates[0].Node != "foo" {
|
|
|
|
t.Fatalf("bad: %#v", resp.Coordinates)
|
2015-07-29 23:33:25 +00:00
|
|
|
}
|
|
|
|
}
|
2017-10-27 02:16:40 +00:00
|
|
|
|
|
|
|
func TestCoordinate_Node(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
dir1, s1 := testServer(t)
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
|
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
defer codec.Close()
|
2018-09-10 15:58:53 +00:00
|
|
|
testrpc.WaitForTestAgent(t, s1.RPC, "dc1")
|
2017-10-27 02:16:40 +00:00
|
|
|
|
|
|
|
// Register some nodes.
|
|
|
|
nodes := []string{"foo", "bar"}
|
2017-10-31 20:34:49 +00:00
|
|
|
if err := registerNodes(nodes, codec); err != nil {
|
|
|
|
t.Fatal(err)
|
2017-10-27 02:16:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Send coordinate updates for each node.
|
|
|
|
arg1 := structs.CoordinateUpdateRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "foo",
|
|
|
|
Coord: generateRandomCoordinate(),
|
|
|
|
}
|
|
|
|
var out struct{}
|
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Coordinate.Update", &arg1, &out); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
arg2 := structs.CoordinateUpdateRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "bar",
|
|
|
|
Coord: generateRandomCoordinate(),
|
|
|
|
}
|
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Coordinate.Update", &arg2, &out); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now query back for a specific node (make sure we only get coordinates for foo).
|
|
|
|
retry.Run(t, func(r *retry.R) {
|
|
|
|
arg := structs.NodeSpecificRequest{
|
|
|
|
Node: "foo",
|
|
|
|
Datacenter: "dc1",
|
|
|
|
}
|
|
|
|
resp := structs.IndexedCoordinates{}
|
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Coordinate.Node", &arg, &resp); err != nil {
|
|
|
|
r.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if len(resp.Coordinates) != 1 ||
|
|
|
|
resp.Coordinates[0].Node != "foo" {
|
|
|
|
r.Fatalf("bad: %v", resp.Coordinates)
|
|
|
|
}
|
|
|
|
verify.Values(t, "", resp.Coordinates[0].Coord, arg1.Coord) // foo
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCoordinate_Node_ACLDeny(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
dir1, s1 := testServerWithConfig(t, func(c *Config) {
|
|
|
|
c.ACLDatacenter = "dc1"
|
2018-10-19 16:04:07 +00:00
|
|
|
c.ACLsEnabled = true
|
2017-10-27 02:16:40 +00:00
|
|
|
c.ACLMasterToken = "root"
|
|
|
|
c.ACLDefaultPolicy = "deny"
|
|
|
|
c.ACLEnforceVersion8 = false
|
|
|
|
})
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
defer codec.Close()
|
|
|
|
|
|
|
|
testrpc.WaitForLeader(t, s1.RPC, "dc1")
|
|
|
|
|
|
|
|
// Register some nodes.
|
|
|
|
nodes := []string{"node1", "node2"}
|
2017-10-31 20:34:49 +00:00
|
|
|
if err := registerNodes(nodes, codec); err != nil {
|
|
|
|
t.Fatal(err)
|
2017-10-27 02:16:40 +00:00
|
|
|
}
|
|
|
|
|
2017-10-31 22:08:14 +00:00
|
|
|
coord := generateRandomCoordinate()
|
2017-10-27 02:16:40 +00:00
|
|
|
req := structs.CoordinateUpdateRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "node1",
|
2017-10-31 22:08:14 +00:00
|
|
|
Coord: coord,
|
2017-10-27 02:16:40 +00:00
|
|
|
}
|
|
|
|
var out struct{}
|
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Coordinate.Update", &req, &out); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-10-31 22:08:14 +00:00
|
|
|
// Try a read for the first node. This should go through since we
|
|
|
|
// don't have version 8 ACLs enforced yet.
|
|
|
|
arg := structs.NodeSpecificRequest{
|
|
|
|
Node: "node1",
|
|
|
|
Datacenter: "dc1",
|
|
|
|
}
|
|
|
|
resp := structs.IndexedCoordinates{}
|
|
|
|
retry.Run(t, func(r *retry.R) {
|
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Coordinate.Node", &arg, &resp); err != nil {
|
|
|
|
r.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if len(resp.Coordinates) != 1 ||
|
|
|
|
resp.Coordinates[0].Node != "node1" {
|
|
|
|
r.Fatalf("bad: %v", resp.Coordinates)
|
|
|
|
}
|
|
|
|
verify.Values(t, "", resp.Coordinates[0].Coord, coord)
|
|
|
|
})
|
|
|
|
|
2017-10-27 02:16:40 +00:00
|
|
|
// Now turn on version 8 enforcement and try again.
|
|
|
|
s1.config.ACLEnforceVersion8 = true
|
2017-10-31 22:08:14 +00:00
|
|
|
err := msgpackrpc.CallWithCodec(codec, "Coordinate.Node", &arg, &resp)
|
2017-10-27 02:16:40 +00:00
|
|
|
if !acl.IsErrPermissionDenied(err) {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-10-31 22:08:14 +00:00
|
|
|
// Create an ACL that can read from the node.
|
|
|
|
aclReq := structs.ACLRequest{
|
2017-10-27 02:16:40 +00:00
|
|
|
Datacenter: "dc1",
|
|
|
|
Op: structs.ACLSet,
|
|
|
|
ACL: structs.ACL{
|
|
|
|
Name: "User token",
|
2018-10-19 16:04:07 +00:00
|
|
|
Type: structs.ACLTokenTypeClient,
|
2017-10-27 02:16:40 +00:00
|
|
|
Rules: `
|
|
|
|
node "node1" {
|
2017-10-31 22:08:14 +00:00
|
|
|
policy = "read"
|
2017-10-27 02:16:40 +00:00
|
|
|
}
|
|
|
|
`,
|
|
|
|
},
|
|
|
|
WriteRequest: structs.WriteRequest{Token: "root"},
|
|
|
|
}
|
|
|
|
var id string
|
2017-10-31 22:08:14 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "ACL.Apply", &aclReq, &id); err != nil {
|
2017-10-27 02:16:40 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// With the token, it should now go through.
|
2017-10-31 22:08:14 +00:00
|
|
|
arg.Token = id
|
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Coordinate.Node", &arg, &resp); err != nil {
|
2017-10-27 02:16:40 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// But it should be blocked for the other node.
|
2017-10-31 22:08:14 +00:00
|
|
|
arg.Node = "node2"
|
|
|
|
err = msgpackrpc.CallWithCodec(codec, "Coordinate.Node", &arg, &resp)
|
2017-10-27 02:16:40 +00:00
|
|
|
if !acl.IsErrPermissionDenied(err) {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
2017-10-31 20:34:49 +00:00
|
|
|
|
|
|
|
func registerNodes(nodes []string, codec rpc.ClientCodec) error {
|
|
|
|
for _, node := range nodes {
|
|
|
|
req := structs.RegisterRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: node,
|
|
|
|
Address: "127.0.0.1",
|
|
|
|
}
|
|
|
|
var reply struct{}
|
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Catalog.Register", &req, &reply); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|