2014-05-16 22:20:41 +00:00
|
|
|
package consul
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"testing"
|
2014-12-10 15:02:23 +00:00
|
|
|
"time"
|
2014-12-12 23:43:34 +00:00
|
|
|
|
2021-08-06 22:00:58 +00:00
|
|
|
msgpackrpc "github.com/hashicorp/net-rpc-msgpackrpc"
|
2021-12-03 20:43:43 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2021-08-06 22:00:58 +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"
|
2020-05-27 16:47:32 +00:00
|
|
|
"github.com/hashicorp/consul/lib/stringslice"
|
2017-04-19 23:00:11 +00:00
|
|
|
"github.com/hashicorp/consul/testrpc"
|
2014-05-16 22:20:41 +00:00
|
|
|
)
|
|
|
|
|
2016-12-13 05:59:22 +00:00
|
|
|
func TestSession_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-05-16 22:20:41 +00:00
|
|
|
dir1, s1 := testServer(t)
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
2019-11-25 17:07:04 +00:00
|
|
|
|
2015-10-13 23:43:52 +00:00
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
defer codec.Close()
|
2014-05-16 22:20:41 +00:00
|
|
|
|
2017-04-19 23:00:11 +00:00
|
|
|
testrpc.WaitForLeader(t, s1.RPC, "dc1")
|
2014-05-16 22:20:41 +00:00
|
|
|
|
|
|
|
// Just add a node
|
2015-10-13 05:21:39 +00:00
|
|
|
s1.fsm.State().EnsureNode(1, &structs.Node{Node: "foo", Address: "127.0.0.1"})
|
2014-05-16 22:20:41 +00:00
|
|
|
|
|
|
|
arg := structs.SessionRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Op: structs.SessionCreate,
|
|
|
|
Session: structs.Session{
|
|
|
|
Node: "foo",
|
2014-06-09 18:42:28 +00:00
|
|
|
Name: "my-session",
|
2014-05-16 22:20:41 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
var out string
|
2015-10-13 23:43:52 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Session.Apply", &arg, &out); err != nil {
|
2014-05-16 22:20:41 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
id := out
|
|
|
|
|
|
|
|
// Verify
|
2015-10-13 05:21:39 +00:00
|
|
|
state := s1.fsm.State()
|
2019-11-25 17:07:04 +00:00
|
|
|
_, s, err := state.SessionGet(nil, out, nil)
|
2014-05-16 22:20:41 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if s == nil {
|
|
|
|
t.Fatalf("should not be nil")
|
|
|
|
}
|
2014-06-09 18:42:28 +00:00
|
|
|
if s.Node != "foo" {
|
|
|
|
t.Fatalf("bad: %v", s)
|
|
|
|
}
|
|
|
|
if s.Name != "my-session" {
|
|
|
|
t.Fatalf("bad: %v", s)
|
|
|
|
}
|
2014-05-16 22:20:41 +00:00
|
|
|
|
|
|
|
// Do a delete
|
|
|
|
arg.Op = structs.SessionDestroy
|
|
|
|
arg.Session.ID = out
|
2015-10-13 23:43:52 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Session.Apply", &arg, &out); err != nil {
|
2014-05-16 22:20:41 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify
|
2019-11-25 17:07:04 +00:00
|
|
|
_, s, err = state.SessionGet(nil, id, nil)
|
2014-05-16 22:20:41 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if s != nil {
|
|
|
|
t.Fatalf("bad: %v", s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-13 05:59:22 +00:00
|
|
|
func TestSession_DeleteApply(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-11-20 02:11:41 +00:00
|
|
|
dir1, s1 := testServer(t)
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
2019-11-25 17:07:04 +00:00
|
|
|
|
2015-10-13 23:43:52 +00:00
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
defer codec.Close()
|
2014-11-20 02:11:41 +00:00
|
|
|
|
2017-04-19 23:00:11 +00:00
|
|
|
testrpc.WaitForLeader(t, s1.RPC, "dc1")
|
2014-11-20 02:11:41 +00:00
|
|
|
|
|
|
|
// Just add a node
|
2015-10-13 05:21:39 +00:00
|
|
|
s1.fsm.State().EnsureNode(1, &structs.Node{Node: "foo", Address: "127.0.0.1"})
|
2014-11-20 02:11:41 +00:00
|
|
|
|
|
|
|
arg := structs.SessionRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Op: structs.SessionCreate,
|
|
|
|
Session: structs.Session{
|
|
|
|
Node: "foo",
|
|
|
|
Name: "my-session",
|
|
|
|
Behavior: structs.SessionKeysDelete,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
var out string
|
2015-10-13 23:43:52 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Session.Apply", &arg, &out); err != nil {
|
2014-11-20 02:11:41 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
id := out
|
|
|
|
|
|
|
|
// Verify
|
2015-10-13 05:21:39 +00:00
|
|
|
state := s1.fsm.State()
|
2019-11-25 17:07:04 +00:00
|
|
|
_, s, err := state.SessionGet(nil, out, nil)
|
2014-11-20 02:11:41 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if s == nil {
|
|
|
|
t.Fatalf("should not be nil")
|
|
|
|
}
|
|
|
|
if s.Node != "foo" {
|
|
|
|
t.Fatalf("bad: %v", s)
|
|
|
|
}
|
|
|
|
if s.Name != "my-session" {
|
|
|
|
t.Fatalf("bad: %v", s)
|
|
|
|
}
|
2014-11-20 19:29:18 +00:00
|
|
|
if s.Behavior != structs.SessionKeysDelete {
|
|
|
|
t.Fatalf("bad: %v", s)
|
|
|
|
}
|
2014-11-20 02:11:41 +00:00
|
|
|
|
|
|
|
// Do a delete
|
|
|
|
arg.Op = structs.SessionDestroy
|
|
|
|
arg.Session.ID = out
|
2015-10-13 23:43:52 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Session.Apply", &arg, &out); err != nil {
|
2014-11-20 02:11:41 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify
|
2019-11-25 17:07:04 +00:00
|
|
|
_, s, err = state.SessionGet(nil, id, nil)
|
2014-11-20 02:11:41 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if s != nil {
|
|
|
|
t.Fatalf("bad: %v", s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-13 05:59:22 +00:00
|
|
|
func TestSession_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()
|
2016-12-13 05:59:22 +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"
|
2016-12-13 05:59:22 +00:00
|
|
|
})
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
2019-11-25 17:07:04 +00:00
|
|
|
|
2016-12-13 05:59:22 +00:00
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
defer codec.Close()
|
|
|
|
|
2020-05-29 21:16:03 +00:00
|
|
|
testrpc.WaitForLeader(t, s1.RPC, "dc1", testrpc.WithToken("root"))
|
2016-12-13 05:59:22 +00:00
|
|
|
|
2021-09-03 20:13:11 +00:00
|
|
|
rules := `
|
2016-12-13 05:59:22 +00:00
|
|
|
session "foo" {
|
|
|
|
policy = "write"
|
|
|
|
}
|
2021-09-03 20:13:11 +00:00
|
|
|
`
|
|
|
|
token := createToken(t, codec, rules)
|
2016-12-13 05:59:22 +00:00
|
|
|
|
|
|
|
// Just add a node.
|
|
|
|
s1.fsm.State().EnsureNode(1, &structs.Node{Node: "foo", Address: "127.0.0.1"})
|
|
|
|
|
2020-05-29 21:16:03 +00:00
|
|
|
// Try to create without a token, which will be denied.
|
2016-12-13 05:59:22 +00:00
|
|
|
arg := structs.SessionRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Op: structs.SessionCreate,
|
|
|
|
Session: structs.Session{
|
|
|
|
Node: "foo",
|
|
|
|
Name: "my-session",
|
|
|
|
},
|
|
|
|
}
|
2020-05-29 21:16:03 +00:00
|
|
|
var id string
|
|
|
|
err := msgpackrpc.CallWithCodec(codec, "Session.Apply", &arg, &id)
|
2017-08-23 14:52:48 +00:00
|
|
|
if !acl.IsErrPermissionDenied(err) {
|
2016-12-13 05:59:22 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now set a token and try again. This should go through.
|
|
|
|
arg.Token = token
|
2020-05-29 21:16:03 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Session.Apply", &arg, &id); err != nil {
|
2016-12-13 05:59:22 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2020-05-29 21:16:03 +00:00
|
|
|
// Make sure the delete of the session fails without a token.
|
2016-12-13 05:59:22 +00:00
|
|
|
var out string
|
|
|
|
arg.Op = structs.SessionDestroy
|
2020-05-29 21:16:03 +00:00
|
|
|
arg.Session.ID = id
|
2016-12-13 05:59:22 +00:00
|
|
|
arg.Token = ""
|
|
|
|
err = msgpackrpc.CallWithCodec(codec, "Session.Apply", &arg, &out)
|
2017-08-23 14:52:48 +00:00
|
|
|
if !acl.IsErrPermissionDenied(err) {
|
2016-12-13 05:59:22 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now set a token and try again. This should go through.
|
|
|
|
arg.Token = token
|
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Session.Apply", &arg, &out); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSession_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-05-16 22:20:41 +00:00
|
|
|
dir1, s1 := testServer(t)
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
2019-11-25 17:07:04 +00:00
|
|
|
|
2015-10-13 23:43:52 +00:00
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
defer codec.Close()
|
2014-05-16 22:20:41 +00:00
|
|
|
|
2017-04-19 23:00:11 +00:00
|
|
|
testrpc.WaitForLeader(t, s1.RPC, "dc1")
|
2014-05-16 22:20:41 +00:00
|
|
|
|
2015-10-13 05:21:39 +00:00
|
|
|
s1.fsm.State().EnsureNode(1, &structs.Node{Node: "foo", Address: "127.0.0.1"})
|
2014-05-16 22:20:41 +00:00
|
|
|
arg := structs.SessionRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Op: structs.SessionCreate,
|
|
|
|
Session: structs.Session{
|
2014-11-20 19:29:18 +00:00
|
|
|
Node: "foo",
|
2014-05-16 22:20:41 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
var out string
|
2015-10-13 23:43:52 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Session.Apply", &arg, &out); err != nil {
|
2014-05-16 22:20:41 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-05-16 22:49:17 +00:00
|
|
|
getR := structs.SessionSpecificRequest{
|
2014-05-16 22:20:41 +00:00
|
|
|
Datacenter: "dc1",
|
2019-11-25 17:07:04 +00:00
|
|
|
SessionID: out,
|
2014-05-16 22:20:41 +00:00
|
|
|
}
|
|
|
|
var sessions structs.IndexedSessions
|
2015-10-13 23:43:52 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Session.Get", &getR, &sessions); err != nil {
|
2014-05-16 22:20:41 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if sessions.Index == 0 {
|
|
|
|
t.Fatalf("Bad: %v", sessions)
|
|
|
|
}
|
|
|
|
if len(sessions.Sessions) != 1 {
|
|
|
|
t.Fatalf("Bad: %v", sessions)
|
|
|
|
}
|
|
|
|
s := sessions.Sessions[0]
|
|
|
|
if s.ID != out {
|
|
|
|
t.Fatalf("bad: %v", s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-05 16:06:09 +00:00
|
|
|
func TestSession_Get_Compat(t *testing.T) {
|
2020-12-07 18:42:55 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("too slow for testing.Short")
|
|
|
|
}
|
|
|
|
|
2020-03-05 16:06:09 +00:00
|
|
|
t.Parallel()
|
|
|
|
dir1, s1 := testServer(t)
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
|
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
defer codec.Close()
|
|
|
|
|
|
|
|
testrpc.WaitForLeader(t, s1.RPC, "dc1")
|
|
|
|
|
|
|
|
s1.fsm.State().EnsureNode(1, &structs.Node{Node: "foo", Address: "127.0.0.1"})
|
|
|
|
arg := structs.SessionRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Op: structs.SessionCreate,
|
|
|
|
Session: structs.Session{
|
|
|
|
Node: "foo",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
var out string
|
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Session.Apply", &arg, &out); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
getR := structs.SessionSpecificRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
// this should get converted to the SessionID field internally
|
|
|
|
Session: out,
|
|
|
|
}
|
|
|
|
var sessions structs.IndexedSessions
|
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Session.Get", &getR, &sessions); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if sessions.Index == 0 {
|
|
|
|
t.Fatalf("Bad: %v", sessions)
|
|
|
|
}
|
|
|
|
if len(sessions.Sessions) != 1 {
|
|
|
|
t.Fatalf("Bad: %v", sessions)
|
|
|
|
}
|
|
|
|
s := sessions.Sessions[0]
|
|
|
|
if s.ID != out {
|
|
|
|
t.Fatalf("bad: %v", s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-13 05:59:22 +00:00
|
|
|
func TestSession_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-05-16 22:20:41 +00:00
|
|
|
dir1, s1 := testServer(t)
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
2019-11-25 17:07:04 +00:00
|
|
|
|
2015-10-13 23:43:52 +00:00
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
defer codec.Close()
|
2014-05-16 22:20:41 +00:00
|
|
|
|
2017-04-19 23:00:11 +00:00
|
|
|
testrpc.WaitForLeader(t, s1.RPC, "dc1")
|
2014-05-16 22:20:41 +00:00
|
|
|
|
2015-10-13 05:21:39 +00:00
|
|
|
s1.fsm.State().EnsureNode(1, &structs.Node{Node: "foo", Address: "127.0.0.1"})
|
2014-05-16 22:20:41 +00:00
|
|
|
ids := []string{}
|
|
|
|
for i := 0; i < 5; i++ {
|
|
|
|
arg := structs.SessionRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Op: structs.SessionCreate,
|
|
|
|
Session: structs.Session{
|
|
|
|
Node: "foo",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
var out string
|
2015-10-13 23:43:52 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Session.Apply", &arg, &out); err != nil {
|
2014-05-16 22:20:41 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
ids = append(ids, out)
|
|
|
|
}
|
|
|
|
|
|
|
|
getR := structs.DCSpecificRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
}
|
|
|
|
var sessions structs.IndexedSessions
|
2015-10-13 23:43:52 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Session.List", &getR, &sessions); err != nil {
|
2014-05-16 22:20:41 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if sessions.Index == 0 {
|
|
|
|
t.Fatalf("Bad: %v", sessions)
|
|
|
|
}
|
|
|
|
if len(sessions.Sessions) != 5 {
|
|
|
|
t.Fatalf("Bad: %v", sessions.Sessions)
|
|
|
|
}
|
|
|
|
for i := 0; i < len(sessions.Sessions); i++ {
|
|
|
|
s := sessions.Sessions[i]
|
2020-05-27 16:47:32 +00:00
|
|
|
if !stringslice.Contains(ids, s.ID) {
|
2014-05-16 22:20:41 +00:00
|
|
|
t.Fatalf("bad: %v", s)
|
|
|
|
}
|
|
|
|
if s.Node != "foo" {
|
|
|
|
t.Fatalf("bad: %v", s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-13 05:59:22 +00:00
|
|
|
func TestSession_Get_List_NodeSessions_ACLFilter(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()
|
2021-12-03 20:43:43 +00:00
|
|
|
|
2016-12-13 05:59:22 +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"
|
2016-12-13 05:59:22 +00:00
|
|
|
})
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
2019-11-25 17:07:04 +00:00
|
|
|
|
2016-12-13 05:59:22 +00:00
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
defer codec.Close()
|
|
|
|
|
2020-05-29 21:16:03 +00:00
|
|
|
testrpc.WaitForLeader(t, s1.RPC, "dc1", testrpc.WithToken("root"))
|
2016-12-13 05:59:22 +00:00
|
|
|
|
2021-12-03 20:43:43 +00:00
|
|
|
deniedToken := createTokenWithPolicyName(t, codec, "denied", `
|
|
|
|
session "foo" {
|
|
|
|
policy = "deny"
|
|
|
|
}
|
|
|
|
`, "root")
|
|
|
|
|
|
|
|
allowedToken := createTokenWithPolicyName(t, codec, "allowed", `
|
|
|
|
session "foo" {
|
|
|
|
policy = "read"
|
|
|
|
}
|
|
|
|
`, "root")
|
2016-12-13 05:59:22 +00:00
|
|
|
|
|
|
|
// Create a node and a session.
|
|
|
|
s1.fsm.State().EnsureNode(1, &structs.Node{Node: "foo", Address: "127.0.0.1"})
|
|
|
|
arg := structs.SessionRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Op: structs.SessionCreate,
|
|
|
|
Session: structs.Session{
|
|
|
|
Node: "foo",
|
|
|
|
},
|
|
|
|
WriteRequest: structs.WriteRequest{Token: "root"},
|
|
|
|
}
|
|
|
|
var out string
|
2021-12-03 20:43:43 +00:00
|
|
|
err := msgpackrpc.CallWithCodec(codec, "Session.Apply", &arg, &out)
|
|
|
|
require.NoError(t, err)
|
2016-12-13 05:59:22 +00:00
|
|
|
|
2021-12-03 20:43:43 +00:00
|
|
|
t.Run("Get", func(t *testing.T) {
|
|
|
|
require := require.New(t)
|
2020-05-29 21:16:03 +00:00
|
|
|
|
2021-12-03 20:43:43 +00:00
|
|
|
req := &structs.SessionSpecificRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
SessionID: out,
|
2016-12-13 05:59:22 +00:00
|
|
|
}
|
2021-12-03 20:43:43 +00:00
|
|
|
req.Token = deniedToken
|
2016-12-13 05:59:22 +00:00
|
|
|
|
2021-12-03 20:43:43 +00:00
|
|
|
// ACL-restricted results filtered out.
|
2016-12-13 05:59:22 +00:00
|
|
|
var sessions structs.IndexedSessions
|
2021-12-03 20:43:43 +00:00
|
|
|
|
|
|
|
err := msgpackrpc.CallWithCodec(codec, "Session.Get", req, &sessions)
|
|
|
|
require.NoError(err)
|
|
|
|
require.Empty(sessions.Sessions)
|
|
|
|
require.True(sessions.QueryMeta.ResultsFilteredByACLs, "ResultsFilteredByACLs should be true")
|
|
|
|
|
|
|
|
// ACL-restricted results included.
|
|
|
|
req.Token = allowedToken
|
|
|
|
|
|
|
|
err = msgpackrpc.CallWithCodec(codec, "Session.Get", req, &sessions)
|
|
|
|
require.NoError(err)
|
|
|
|
require.Len(sessions.Sessions, 1)
|
|
|
|
require.False(sessions.QueryMeta.ResultsFilteredByACLs, "ResultsFilteredByACLs should be false")
|
|
|
|
|
|
|
|
// Try to get a session that doesn't exist to make sure that's handled
|
|
|
|
// correctly by the filter (it will get passed a nil slice).
|
|
|
|
req.SessionID = "adf4238a-882b-9ddc-4a9d-5b6758e4159e"
|
|
|
|
|
|
|
|
err = msgpackrpc.CallWithCodec(codec, "Session.Get", req, &sessions)
|
|
|
|
require.NoError(err)
|
|
|
|
require.Empty(sessions.Sessions)
|
|
|
|
require.False(sessions.QueryMeta.ResultsFilteredByACLs, "ResultsFilteredByACLs should be false")
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("List", func(t *testing.T) {
|
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
req := &structs.DCSpecificRequest{
|
|
|
|
Datacenter: "dc1",
|
2016-12-13 05:59:22 +00:00
|
|
|
}
|
2021-12-03 20:43:43 +00:00
|
|
|
req.Token = deniedToken
|
|
|
|
|
|
|
|
// ACL-restricted results filtered out.
|
2016-12-13 05:59:22 +00:00
|
|
|
var sessions structs.IndexedSessions
|
2021-12-03 20:43:43 +00:00
|
|
|
|
|
|
|
err := msgpackrpc.CallWithCodec(codec, "Session.List", req, &sessions)
|
|
|
|
require.NoError(err)
|
|
|
|
require.Empty(sessions.Sessions)
|
|
|
|
require.True(sessions.QueryMeta.ResultsFilteredByACLs, "ResultsFilteredByACLs should be true")
|
|
|
|
|
|
|
|
// ACL-restricted results included.
|
|
|
|
req.Token = allowedToken
|
|
|
|
|
|
|
|
err = msgpackrpc.CallWithCodec(codec, "Session.List", req, &sessions)
|
|
|
|
require.NoError(err)
|
|
|
|
require.Len(sessions.Sessions, 1)
|
|
|
|
require.False(sessions.QueryMeta.ResultsFilteredByACLs, "ResultsFilteredByACLs should be false")
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("NodeSessions", func(t *testing.T) {
|
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
req := &structs.NodeSpecificRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "foo",
|
2016-12-13 05:59:22 +00:00
|
|
|
}
|
2021-12-03 20:43:43 +00:00
|
|
|
req.Token = deniedToken
|
2016-12-13 05:59:22 +00:00
|
|
|
|
2021-12-03 20:43:43 +00:00
|
|
|
// ACL-restricted results filtered out.
|
2016-12-13 05:59:22 +00:00
|
|
|
var sessions structs.IndexedSessions
|
2021-12-03 20:43:43 +00:00
|
|
|
|
|
|
|
err := msgpackrpc.CallWithCodec(codec, "Session.NodeSessions", req, &sessions)
|
|
|
|
require.NoError(err)
|
|
|
|
require.Empty(sessions.Sessions)
|
|
|
|
require.True(sessions.QueryMeta.ResultsFilteredByACLs, "ResultsFilteredByACLs should be true")
|
|
|
|
|
|
|
|
// ACL-restricted results included.
|
|
|
|
req.Token = allowedToken
|
|
|
|
|
|
|
|
err = msgpackrpc.CallWithCodec(codec, "Session.NodeSessions", req, &sessions)
|
|
|
|
require.NoError(err)
|
|
|
|
require.Len(sessions.Sessions, 1)
|
|
|
|
require.False(sessions.QueryMeta.ResultsFilteredByACLs, "ResultsFilteredByACLs should be false")
|
|
|
|
})
|
2016-12-13 05:59:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSession_ApplyTimers(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-12-13 05:54:29 +00:00
|
|
|
dir1, s1 := testServer(t)
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
2019-11-25 17:07:04 +00:00
|
|
|
|
2015-10-13 23:43:52 +00:00
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
defer codec.Close()
|
2014-12-13 05:54:29 +00:00
|
|
|
|
2019-11-25 17:07:04 +00:00
|
|
|
testrpc.WaitForTestAgent(t, s1.RPC, "dc1")
|
|
|
|
|
2015-10-13 05:21:39 +00:00
|
|
|
s1.fsm.State().EnsureNode(1, &structs.Node{Node: "foo", Address: "127.0.0.1"})
|
2014-12-13 05:54:29 +00:00
|
|
|
arg := structs.SessionRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Op: structs.SessionCreate,
|
|
|
|
Session: structs.Session{
|
|
|
|
Node: "foo",
|
|
|
|
TTL: "10s",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
var out string
|
2015-10-13 23:43:52 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Session.Apply", &arg, &out); err != nil {
|
2014-12-13 05:54:29 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the session map
|
2017-06-27 13:25:25 +00:00
|
|
|
if s1.sessionTimers.Get(out) == nil {
|
2014-12-13 05:54:29 +00:00
|
|
|
t.Fatalf("missing session timer")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Destroy the session
|
|
|
|
arg.Op = structs.SessionDestroy
|
|
|
|
arg.Session.ID = out
|
2015-10-13 23:43:52 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Session.Apply", &arg, &out); err != nil {
|
2014-12-13 05:54:29 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the session map
|
2017-06-27 13:25:25 +00:00
|
|
|
if s1.sessionTimers.Get(out) != nil {
|
2014-12-13 05:54:29 +00:00
|
|
|
t.Fatalf("session timer exists")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-13 05:59:22 +00:00
|
|
|
func TestSession_Renew(t *testing.T) {
|
2020-12-07 18:42:55 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("too slow for testing.Short")
|
|
|
|
}
|
|
|
|
|
2018-08-06 23:46:09 +00:00
|
|
|
// This method is timing sensitive, disable Parallel
|
|
|
|
//t.Parallel()
|
|
|
|
ttl := 1 * time.Second
|
2017-04-27 09:46:38 +00:00
|
|
|
TTL := ttl.String()
|
|
|
|
|
|
|
|
dir1, s1 := testServerWithConfig(t, func(c *Config) {
|
|
|
|
c.SessionTTLMin = ttl
|
|
|
|
})
|
2014-12-10 15:02:23 +00:00
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
2018-09-04 13:02:47 +00:00
|
|
|
testrpc.WaitForTestAgent(t, s1.RPC, "dc1")
|
2019-11-25 17:07:04 +00:00
|
|
|
|
2015-10-13 23:43:52 +00:00
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
defer codec.Close()
|
2014-12-10 15:02:23 +00:00
|
|
|
|
2015-10-13 05:21:39 +00:00
|
|
|
s1.fsm.State().EnsureNode(1, &structs.Node{Node: "foo", Address: "127.0.0.1"})
|
2014-12-10 15:02:23 +00:00
|
|
|
ids := []string{}
|
|
|
|
for i := 0; i < 5; i++ {
|
|
|
|
arg := structs.SessionRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Op: structs.SessionCreate,
|
|
|
|
Session: structs.Session{
|
|
|
|
Node: "foo",
|
2014-12-10 21:43:15 +00:00
|
|
|
TTL: TTL,
|
2014-12-10 15:02:23 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
var out string
|
2015-10-13 23:43:52 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Session.Apply", &arg, &out); err != nil {
|
2014-12-10 15:02:23 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
ids = append(ids, out)
|
|
|
|
}
|
|
|
|
|
2014-12-13 05:54:29 +00:00
|
|
|
// Verify the timer map is setup
|
2017-06-27 13:25:25 +00:00
|
|
|
if s1.sessionTimers.Len() != 5 {
|
2014-12-13 05:54:29 +00:00
|
|
|
t.Fatalf("missing session timers")
|
|
|
|
}
|
|
|
|
|
2014-12-10 15:02:23 +00:00
|
|
|
getR := structs.DCSpecificRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
}
|
|
|
|
|
|
|
|
var sessions structs.IndexedSessions
|
2015-10-13 23:43:52 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Session.List", &getR, &sessions); err != nil {
|
2014-12-10 15:02:23 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if sessions.Index == 0 {
|
|
|
|
t.Fatalf("Bad: %v", sessions)
|
|
|
|
}
|
|
|
|
if len(sessions.Sessions) != 5 {
|
|
|
|
t.Fatalf("Bad: %v", sessions.Sessions)
|
|
|
|
}
|
|
|
|
for i := 0; i < len(sessions.Sessions); i++ {
|
|
|
|
s := sessions.Sessions[i]
|
2020-05-27 16:47:32 +00:00
|
|
|
if !stringslice.Contains(ids, s.ID) {
|
2014-12-10 15:02:23 +00:00
|
|
|
t.Fatalf("bad: %v", s)
|
|
|
|
}
|
|
|
|
if s.Node != "foo" {
|
|
|
|
t.Fatalf("bad: %v", s)
|
|
|
|
}
|
2014-12-10 21:43:15 +00:00
|
|
|
if s.TTL != TTL {
|
|
|
|
t.Fatalf("bad session TTL: %s %v", s.TTL, s)
|
2014-12-10 15:02:23 +00:00
|
|
|
}
|
2014-12-10 21:43:15 +00:00
|
|
|
t.Logf("Created session '%s'", s.ID)
|
2014-12-10 15:02:23 +00:00
|
|
|
}
|
|
|
|
|
2014-12-11 01:49:06 +00:00
|
|
|
// Sleep for time shorter than internal destroy ttl
|
|
|
|
time.Sleep(ttl * structs.SessionTTLMultiplier / 2)
|
2014-12-10 15:02:23 +00:00
|
|
|
|
|
|
|
// renew 3 out of 5 sessions
|
|
|
|
for i := 0; i < 3; i++ {
|
|
|
|
renewR := structs.SessionSpecificRequest{
|
|
|
|
Datacenter: "dc1",
|
2019-11-25 17:07:04 +00:00
|
|
|
SessionID: ids[i],
|
2014-12-10 15:02:23 +00:00
|
|
|
}
|
|
|
|
var session structs.IndexedSessions
|
2015-10-13 23:43:52 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Session.Renew", &renewR, &session); err != nil {
|
2014-12-10 15:02:23 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if session.Index == 0 {
|
|
|
|
t.Fatalf("Bad: %v", session)
|
|
|
|
}
|
|
|
|
if len(session.Sessions) != 1 {
|
|
|
|
t.Fatalf("Bad: %v", session.Sessions)
|
|
|
|
}
|
|
|
|
|
|
|
|
s := session.Sessions[0]
|
2020-05-27 16:47:32 +00:00
|
|
|
if !stringslice.Contains(ids, s.ID) {
|
2014-12-10 15:02:23 +00:00
|
|
|
t.Fatalf("bad: %v", s)
|
|
|
|
}
|
|
|
|
if s.Node != "foo" {
|
|
|
|
t.Fatalf("bad: %v", s)
|
|
|
|
}
|
2014-12-10 21:43:15 +00:00
|
|
|
|
|
|
|
t.Logf("Renewed session '%s'", s.ID)
|
2014-12-10 15:02:23 +00:00
|
|
|
}
|
|
|
|
|
2014-12-11 01:49:06 +00:00
|
|
|
// now sleep for 2/3 the internal destroy TTL time for renewed sessions
|
|
|
|
// which is more than the internal destroy TTL time for the non-renewed sessions
|
|
|
|
time.Sleep((ttl * structs.SessionTTLMultiplier) * 2.0 / 3.0)
|
2014-12-10 15:02:23 +00:00
|
|
|
|
2014-12-11 01:49:06 +00:00
|
|
|
var sessionsL1 structs.IndexedSessions
|
2015-10-13 23:43:52 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Session.List", &getR, &sessionsL1); err != nil {
|
2014-12-10 15:02:23 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-12-11 01:49:06 +00:00
|
|
|
if sessionsL1.Index == 0 {
|
|
|
|
t.Fatalf("Bad: %v", sessionsL1)
|
2014-12-10 15:02:23 +00:00
|
|
|
}
|
2014-12-10 21:43:15 +00:00
|
|
|
|
|
|
|
t.Logf("Expect 2 sessions to be destroyed")
|
|
|
|
|
2014-12-11 01:49:06 +00:00
|
|
|
for i := 0; i < len(sessionsL1.Sessions); i++ {
|
|
|
|
s := sessionsL1.Sessions[i]
|
2020-05-27 16:47:32 +00:00
|
|
|
if !stringslice.Contains(ids, s.ID) {
|
2014-12-10 15:02:23 +00:00
|
|
|
t.Fatalf("bad: %v", s)
|
|
|
|
}
|
|
|
|
if s.Node != "foo" {
|
|
|
|
t.Fatalf("bad: %v", s)
|
|
|
|
}
|
2014-12-10 21:43:15 +00:00
|
|
|
if s.TTL != TTL {
|
2014-12-10 15:02:23 +00:00
|
|
|
t.Fatalf("bad: %v", s)
|
|
|
|
}
|
2014-12-10 21:43:15 +00:00
|
|
|
if i > 2 {
|
|
|
|
t.Errorf("session '%s' should be destroyed", s.ID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-12 23:43:34 +00:00
|
|
|
if len(sessionsL1.Sessions) != 3 {
|
2014-12-11 01:49:06 +00:00
|
|
|
t.Fatalf("Bad: %v", sessionsL1.Sessions)
|
2014-12-10 15:02:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// now sleep again for ttl*2 - no sessions should still be alive
|
2014-12-11 01:49:06 +00:00
|
|
|
time.Sleep(ttl * structs.SessionTTLMultiplier)
|
2014-12-10 15:02:23 +00:00
|
|
|
|
2014-12-11 01:49:06 +00:00
|
|
|
var sessionsL2 structs.IndexedSessions
|
2015-10-13 23:43:52 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Session.List", &getR, &sessionsL2); err != nil {
|
2014-12-10 15:02:23 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-12-11 01:49:06 +00:00
|
|
|
if sessionsL2.Index == 0 {
|
|
|
|
t.Fatalf("Bad: %v", sessionsL2)
|
2014-12-10 15:02:23 +00:00
|
|
|
}
|
2014-12-11 01:49:06 +00:00
|
|
|
if len(sessionsL2.Sessions) != 0 {
|
|
|
|
for i := 0; i < len(sessionsL2.Sessions); i++ {
|
|
|
|
s := sessionsL2.Sessions[i]
|
2020-05-27 16:47:32 +00:00
|
|
|
if !stringslice.Contains(ids, s.ID) {
|
2014-12-10 21:43:15 +00:00
|
|
|
t.Fatalf("bad: %v", s)
|
|
|
|
}
|
|
|
|
if s.Node != "foo" {
|
|
|
|
t.Fatalf("bad: %v", s)
|
|
|
|
}
|
|
|
|
if s.TTL != TTL {
|
|
|
|
t.Fatalf("bad: %v", s)
|
|
|
|
}
|
|
|
|
t.Errorf("session '%s' should be destroyed", s.ID)
|
|
|
|
}
|
2014-12-11 01:49:06 +00:00
|
|
|
|
|
|
|
t.Fatalf("Bad: %v", sessionsL2.Sessions)
|
2014-12-10 15:02:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-13 05:59:22 +00:00
|
|
|
func TestSession_Renew_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()
|
2016-12-13 05:59:22 +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"
|
2016-12-13 05:59:22 +00:00
|
|
|
})
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
2019-11-25 17:07:04 +00:00
|
|
|
|
2016-12-13 05:59:22 +00:00
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
defer codec.Close()
|
|
|
|
|
2020-05-29 21:16:03 +00:00
|
|
|
testrpc.WaitForTestAgent(t, s1.RPC, "dc1", testrpc.WithToken("root"))
|
2019-11-25 17:07:04 +00:00
|
|
|
|
2021-09-03 20:13:11 +00:00
|
|
|
rules := `
|
2016-12-13 05:59:22 +00:00
|
|
|
session "foo" {
|
|
|
|
policy = "write"
|
|
|
|
}
|
2021-09-03 20:13:11 +00:00
|
|
|
`
|
|
|
|
token := createToken(t, codec, rules)
|
2016-12-13 05:59:22 +00:00
|
|
|
|
|
|
|
// Just add a node.
|
|
|
|
s1.fsm.State().EnsureNode(1, &structs.Node{Node: "foo", Address: "127.0.0.1"})
|
|
|
|
|
2020-05-29 21:16:03 +00:00
|
|
|
// Create a session.
|
2016-12-13 05:59:22 +00:00
|
|
|
arg := structs.SessionRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Op: structs.SessionCreate,
|
|
|
|
Session: structs.Session{
|
|
|
|
Node: "foo",
|
|
|
|
Name: "my-session",
|
|
|
|
},
|
2020-05-29 21:16:03 +00:00
|
|
|
WriteRequest: structs.WriteRequest{Token: token},
|
2016-12-13 05:59:22 +00:00
|
|
|
}
|
|
|
|
var id string
|
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Session.Apply", &arg, &id); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2020-05-29 21:16:03 +00:00
|
|
|
// Renew without a token should be rejected.
|
2016-12-13 05:59:22 +00:00
|
|
|
renewR := structs.SessionSpecificRequest{
|
|
|
|
Datacenter: "dc1",
|
2019-11-25 17:07:04 +00:00
|
|
|
SessionID: id,
|
2016-12-13 05:59:22 +00:00
|
|
|
}
|
|
|
|
var session structs.IndexedSessions
|
|
|
|
err := msgpackrpc.CallWithCodec(codec, "Session.Renew", &renewR, &session)
|
2017-08-23 14:52:48 +00:00
|
|
|
if !acl.IsErrPermissionDenied(err) {
|
2016-12-13 05:59:22 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the token and it should go through.
|
|
|
|
renewR.Token = token
|
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Session.Renew", &renewR, &session); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-05 16:06:09 +00:00
|
|
|
func TestSession_Renew_Compat(t *testing.T) {
|
2020-12-07 18:42:55 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("too slow for testing.Short")
|
|
|
|
}
|
|
|
|
|
2020-03-05 16:06:09 +00:00
|
|
|
// This method is timing sensitive, disable Parallel
|
|
|
|
//t.Parallel()
|
|
|
|
ttl := 5 * time.Second
|
|
|
|
TTL := ttl.String()
|
|
|
|
|
|
|
|
dir1, s1 := testServerWithConfig(t, func(c *Config) {
|
|
|
|
c.SessionTTLMin = ttl
|
|
|
|
})
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
testrpc.WaitForTestAgent(t, s1.RPC, "dc1")
|
|
|
|
|
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
defer codec.Close()
|
|
|
|
|
|
|
|
s1.fsm.State().EnsureNode(1, &structs.Node{Node: "foo", Address: "127.0.0.1"})
|
|
|
|
var id string
|
|
|
|
arg := structs.SessionRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Op: structs.SessionCreate,
|
|
|
|
Session: structs.Session{
|
|
|
|
Node: "foo",
|
|
|
|
TTL: TTL,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Session.Apply", &arg, &id); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// renew the session
|
|
|
|
renewR := structs.SessionSpecificRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
// this will get ranslated internally to the SessionID field
|
|
|
|
Session: id,
|
|
|
|
}
|
|
|
|
var session structs.IndexedSessions
|
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Session.Renew", &renewR, &session); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if session.Index == 0 {
|
|
|
|
t.Fatalf("Bad: %v", session)
|
|
|
|
}
|
|
|
|
if len(session.Sessions) != 1 {
|
|
|
|
t.Fatalf("Bad: %v", session.Sessions)
|
|
|
|
}
|
|
|
|
|
|
|
|
s := session.Sessions[0]
|
|
|
|
if id != s.ID {
|
|
|
|
t.Fatalf("bad: %v", s)
|
|
|
|
}
|
|
|
|
if s.Node != "foo" {
|
|
|
|
t.Fatalf("bad: %v", s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-13 05:59:22 +00:00
|
|
|
func TestSession_NodeSessions(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-16 22:20:41 +00:00
|
|
|
dir1, s1 := testServer(t)
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
2019-11-25 17:07:04 +00:00
|
|
|
|
2015-10-13 23:43:52 +00:00
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
defer codec.Close()
|
2014-05-16 22:20:41 +00:00
|
|
|
|
2017-04-19 23:00:11 +00:00
|
|
|
testrpc.WaitForLeader(t, s1.RPC, "dc1")
|
2014-05-16 22:20:41 +00:00
|
|
|
|
2015-10-13 05:21:39 +00:00
|
|
|
s1.fsm.State().EnsureNode(1, &structs.Node{Node: "foo", Address: "127.0.0.1"})
|
|
|
|
s1.fsm.State().EnsureNode(1, &structs.Node{Node: "bar", Address: "127.0.0.1"})
|
2014-05-16 22:20:41 +00:00
|
|
|
ids := []string{}
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
arg := structs.SessionRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Op: structs.SessionCreate,
|
|
|
|
Session: structs.Session{
|
|
|
|
Node: "bar",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if i < 5 {
|
|
|
|
arg.Session.Node = "foo"
|
|
|
|
}
|
|
|
|
var out string
|
2015-10-13 23:43:52 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Session.Apply", &arg, &out); err != nil {
|
2014-05-16 22:20:41 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if i < 5 {
|
|
|
|
ids = append(ids, out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
getR := structs.NodeSpecificRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "foo",
|
|
|
|
}
|
|
|
|
var sessions structs.IndexedSessions
|
2015-10-13 23:43:52 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Session.NodeSessions", &getR, &sessions); err != nil {
|
2014-05-16 22:20:41 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if sessions.Index == 0 {
|
|
|
|
t.Fatalf("Bad: %v", sessions)
|
|
|
|
}
|
|
|
|
if len(sessions.Sessions) != 5 {
|
|
|
|
t.Fatalf("Bad: %v", sessions.Sessions)
|
|
|
|
}
|
|
|
|
for i := 0; i < len(sessions.Sessions); i++ {
|
|
|
|
s := sessions.Sessions[i]
|
2020-05-27 16:47:32 +00:00
|
|
|
if !stringslice.Contains(ids, s.ID) {
|
2014-05-16 22:20:41 +00:00
|
|
|
t.Fatalf("bad: %v", s)
|
|
|
|
}
|
|
|
|
if s.Node != "foo" {
|
|
|
|
t.Fatalf("bad: %v", s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-03-27 05:30:04 +00:00
|
|
|
|
2016-12-13 05:59:22 +00:00
|
|
|
func TestSession_Apply_BadTTL(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-03-27 05:30:04 +00:00
|
|
|
dir1, s1 := testServer(t)
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
2019-11-25 17:07:04 +00:00
|
|
|
|
2015-10-13 23:43:52 +00:00
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
defer codec.Close()
|
2015-03-27 05:30:04 +00:00
|
|
|
|
2017-04-19 23:00:11 +00:00
|
|
|
testrpc.WaitForLeader(t, s1.RPC, "dc1")
|
2015-03-27 05:30:04 +00:00
|
|
|
|
|
|
|
arg := structs.SessionRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Op: structs.SessionCreate,
|
|
|
|
Session: structs.Session{
|
|
|
|
Node: "foo",
|
|
|
|
Name: "my-session",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Session with illegal TTL
|
|
|
|
arg.Session.TTL = "10z"
|
|
|
|
|
|
|
|
var out string
|
2015-10-13 23:43:52 +00:00
|
|
|
err := msgpackrpc.CallWithCodec(codec, "Session.Apply", &arg, &out)
|
2015-03-27 05:30:04 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("expected error")
|
|
|
|
}
|
2020-11-13 18:02:59 +00:00
|
|
|
if err.Error() != `Session TTL '10z' invalid: time: unknown unit "z" in duration "10z"` {
|
2015-03-27 05:30:04 +00:00
|
|
|
t.Fatalf("incorrect error message: %s", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
// less than SessionTTLMin
|
|
|
|
arg.Session.TTL = "5s"
|
|
|
|
|
2015-10-13 23:43:52 +00:00
|
|
|
err = msgpackrpc.CallWithCodec(codec, "Session.Apply", &arg, &out)
|
2015-03-27 05:30:04 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("expected error")
|
|
|
|
}
|
2015-11-14 23:30:53 +00:00
|
|
|
if err.Error() != "Invalid Session TTL '5000000000', must be between [10s=24h0m0s]" {
|
2015-03-27 05:30:04 +00:00
|
|
|
t.Fatalf("incorrect error message: %s", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
// more than SessionTTLMax
|
2015-11-14 23:30:53 +00:00
|
|
|
arg.Session.TTL = "100000s"
|
2015-03-27 05:30:04 +00:00
|
|
|
|
2015-10-13 23:43:52 +00:00
|
|
|
err = msgpackrpc.CallWithCodec(codec, "Session.Apply", &arg, &out)
|
2015-03-27 05:30:04 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("expected error")
|
|
|
|
}
|
2015-11-14 23:30:53 +00:00
|
|
|
if err.Error() != "Invalid Session TTL '100000000000000', must be between [10s=24h0m0s]" {
|
2015-03-27 05:30:04 +00:00
|
|
|
t.Fatalf("incorrect error message: %s", err.Error())
|
|
|
|
}
|
|
|
|
}
|