2014-08-06 00:24:48 +00:00
|
|
|
package consul
|
|
|
|
|
|
|
|
import (
|
2018-10-19 16:04:07 +00:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/rpc"
|
2014-08-06 00:24:48 +00:00
|
|
|
"os"
|
2018-10-19 16:04:07 +00:00
|
|
|
"path/filepath"
|
|
|
|
"reflect"
|
2014-08-12 22:32:44 +00:00
|
|
|
"strings"
|
2014-08-06 00:24:48 +00:00
|
|
|
"testing"
|
2014-08-08 22:32:43 +00:00
|
|
|
"time"
|
|
|
|
|
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"
|
2016-01-29 19:42:34 +00:00
|
|
|
"github.com/hashicorp/consul/lib"
|
2017-04-19 23:00:11 +00:00
|
|
|
"github.com/hashicorp/consul/testrpc"
|
2017-05-04 22:35:33 +00:00
|
|
|
"github.com/hashicorp/consul/testutil/retry"
|
2018-10-19 16:04:07 +00:00
|
|
|
uuid "github.com/hashicorp/go-uuid"
|
2015-10-13 23:43:52 +00:00
|
|
|
"github.com/hashicorp/net-rpc-msgpackrpc"
|
2018-10-19 16:04:07 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
2014-08-06 00:24:48 +00:00
|
|
|
)
|
|
|
|
|
2017-08-03 00:05:18 +00:00
|
|
|
func TestACLEndpoint_Bootstrap(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
dir1, s1 := testServerWithConfig(t, func(c *Config) {
|
|
|
|
c.Build = "0.8.0" // Too low for auto init of bootstrap.
|
|
|
|
c.ACLDatacenter = "dc1"
|
2018-10-19 16:04:07 +00:00
|
|
|
c.ACLsEnabled = true
|
2017-08-03 00:05:18 +00:00
|
|
|
})
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
defer codec.Close()
|
|
|
|
|
|
|
|
testrpc.WaitForLeader(t, s1.RPC, "dc1")
|
|
|
|
|
|
|
|
// Expect an error initially since ACL bootstrap is not initialized.
|
|
|
|
arg := structs.DCSpecificRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
}
|
|
|
|
var out structs.ACL
|
2018-10-19 16:04:07 +00:00
|
|
|
// We can only do some high
|
2017-08-03 00:05:18 +00:00
|
|
|
// level checks on the ACL since we don't have control over the UUID or
|
|
|
|
// Raft indexes at this level.
|
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "ACL.Bootstrap", &arg, &out); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if len(out.ID) != len("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx") ||
|
2018-10-19 16:04:07 +00:00
|
|
|
!strings.HasPrefix(out.Name, "Bootstrap Token") ||
|
|
|
|
out.Type != structs.ACLTokenTypeManagement ||
|
2017-08-03 00:05:18 +00:00
|
|
|
out.CreateIndex == 0 || out.ModifyIndex == 0 {
|
|
|
|
t.Fatalf("bad: %#v", out)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finally, make sure that another attempt is rejected.
|
2018-10-19 16:04:07 +00:00
|
|
|
err := msgpackrpc.CallWithCodec(codec, "ACL.Bootstrap", &arg, &out)
|
2017-08-03 00:05:18 +00:00
|
|
|
if err.Error() != structs.ACLBootstrapNotAllowedErr.Error() {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
func TestACLEndpoint_BootstrapTokens(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
dir1, s1 := testServerWithConfig(t, func(c *Config) {
|
|
|
|
c.ACLDatacenter = "dc1"
|
|
|
|
c.ACLsEnabled = true
|
|
|
|
c.ACLsEnabled = true
|
|
|
|
})
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
defer codec.Close()
|
|
|
|
|
|
|
|
testrpc.WaitForLeader(t, s1.RPC, "dc1")
|
|
|
|
|
|
|
|
// Expect an error initially since ACL bootstrap is not initialized.
|
|
|
|
arg := structs.DCSpecificRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
}
|
|
|
|
var out structs.ACLToken
|
|
|
|
// We can only do some high
|
|
|
|
// level checks on the ACL since we don't have control over the UUID or
|
|
|
|
// Raft indexes at this level.
|
|
|
|
require.NoError(t, msgpackrpc.CallWithCodec(codec, "ACL.BootstrapTokens", &arg, &out))
|
|
|
|
require.Equal(t, 36, len(out.AccessorID))
|
|
|
|
require.True(t, strings.HasPrefix(out.Description, "Bootstrap Token"))
|
|
|
|
require.Equal(t, out.Type, structs.ACLTokenTypeManagement)
|
|
|
|
require.True(t, out.CreateIndex > 0)
|
|
|
|
require.Equal(t, out.CreateIndex, out.ModifyIndex)
|
|
|
|
|
|
|
|
// Finally, make sure that another attempt is rejected.
|
|
|
|
err := msgpackrpc.CallWithCodec(codec, "ACL.BootstrapTokens", &arg, &out)
|
|
|
|
require.Error(t, err)
|
|
|
|
require.True(t, strings.HasPrefix(err.Error(), structs.ACLBootstrapNotAllowedErr.Error()))
|
|
|
|
|
|
|
|
_, resetIdx, err := s1.fsm.State().CanBootstrapACLToken()
|
|
|
|
|
|
|
|
resetPath := filepath.Join(dir1, "acl-bootstrap-reset")
|
|
|
|
require.NoError(t, ioutil.WriteFile(resetPath, []byte(fmt.Sprintf("%d", resetIdx)), 0600))
|
|
|
|
|
|
|
|
oldID := out.AccessorID
|
|
|
|
// Finally, make sure that another attempt is rejected.
|
|
|
|
require.NoError(t, msgpackrpc.CallWithCodec(codec, "ACL.BootstrapTokens", &arg, &out))
|
|
|
|
require.Equal(t, 36, len(out.AccessorID))
|
|
|
|
require.NotEqual(t, oldID, out.AccessorID)
|
|
|
|
require.True(t, strings.HasPrefix(out.Description, "Bootstrap Token"))
|
|
|
|
require.Equal(t, out.Type, structs.ACLTokenTypeManagement)
|
|
|
|
require.True(t, out.CreateIndex > 0)
|
|
|
|
require.Equal(t, out.CreateIndex, out.ModifyIndex)
|
|
|
|
}
|
|
|
|
|
2014-08-06 00:24:48 +00:00
|
|
|
func TestACLEndpoint_Apply(t *testing.T) {
|
2017-06-27 13:22:18 +00:00
|
|
|
t.Parallel()
|
2014-08-12 22:32:44 +00:00
|
|
|
dir1, s1 := testServerWithConfig(t, func(c *Config) {
|
|
|
|
c.ACLDatacenter = "dc1"
|
2018-10-19 16:04:07 +00:00
|
|
|
c.ACLsEnabled = true
|
2014-08-12 22:32:44 +00:00
|
|
|
c.ACLMasterToken = "root"
|
|
|
|
})
|
2014-08-06 00:24:48 +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-06 00:24:48 +00:00
|
|
|
|
2017-04-19 23:00:11 +00:00
|
|
|
testrpc.WaitForLeader(t, s1.RPC, "dc1")
|
2014-08-06 00:24:48 +00:00
|
|
|
|
|
|
|
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,
|
2014-08-06 00:24:48 +00:00
|
|
|
},
|
2014-08-12 22:32:44 +00:00
|
|
|
WriteRequest: structs.WriteRequest{Token: "root"},
|
2014-08-06 00:24:48 +00:00
|
|
|
}
|
|
|
|
var out string
|
2015-10-13 23:43:52 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "ACL.Apply", &arg, &out); err != nil {
|
2014-08-06 00:24:48 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
id := out
|
|
|
|
|
|
|
|
// Verify
|
|
|
|
state := s1.fsm.State()
|
2018-10-19 16:04:07 +00:00
|
|
|
_, s, err := state.ACLTokenGetBySecret(nil, out)
|
2014-08-06 00:24:48 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if s == nil {
|
|
|
|
t.Fatalf("should not be nil")
|
|
|
|
}
|
2018-10-19 16:04:07 +00:00
|
|
|
if s.SecretID != out {
|
2014-08-06 00:24:48 +00:00
|
|
|
t.Fatalf("bad: %v", s)
|
|
|
|
}
|
2018-10-19 16:04:07 +00:00
|
|
|
if s.Description != "User token" {
|
2014-08-06 00:24:48 +00:00
|
|
|
t.Fatalf("bad: %v", s)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do a delete
|
|
|
|
arg.Op = structs.ACLDelete
|
|
|
|
arg.ACL.ID = out
|
2015-10-13 23:43:52 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "ACL.Apply", &arg, &out); err != nil {
|
2014-08-06 00:24:48 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify
|
2018-10-19 16:04:07 +00:00
|
|
|
_, s, err = state.ACLTokenGetBySecret(nil, id)
|
2014-08-06 00:24:48 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if s != nil {
|
|
|
|
t.Fatalf("bad: %v", s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-18 22:23:02 +00:00
|
|
|
func TestACLEndpoint_Update_PurgeCache(t *testing.T) {
|
2017-06-27 13:22:18 +00:00
|
|
|
t.Parallel()
|
2014-08-18 22:23:02 +00:00
|
|
|
dir1, s1 := testServerWithConfig(t, func(c *Config) {
|
|
|
|
c.ACLDatacenter = "dc1"
|
2018-10-19 16:04:07 +00:00
|
|
|
c.ACLsEnabled = true
|
2014-08-18 22:23:02 +00:00
|
|
|
c.ACLMasterToken = "root"
|
|
|
|
})
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
2015-10-13 23:43:52 +00:00
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
defer codec.Close()
|
2014-08-18 22:23:02 +00:00
|
|
|
|
2017-04-19 23:00:11 +00:00
|
|
|
testrpc.WaitForLeader(t, s1.RPC, "dc1")
|
2014-08-18 22:23:02 +00:00
|
|
|
|
|
|
|
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,
|
2014-08-18 22:23:02 +00:00
|
|
|
},
|
|
|
|
WriteRequest: structs.WriteRequest{Token: "root"},
|
|
|
|
}
|
|
|
|
var out string
|
2015-10-13 23:43:52 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "ACL.Apply", &arg, &out); err != nil {
|
2014-08-18 22:23:02 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
id := out
|
|
|
|
|
|
|
|
// Resolve
|
2018-10-19 16:04:07 +00:00
|
|
|
acl1, err := s1.ResolveToken(id)
|
2014-08-18 22:23:02 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if acl1 == nil {
|
|
|
|
t.Fatalf("should not be nil")
|
|
|
|
}
|
|
|
|
if !acl1.KeyRead("foo") {
|
|
|
|
t.Fatalf("should be allowed")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do an update
|
|
|
|
arg.ACL.ID = out
|
|
|
|
arg.ACL.Rules = `{"key": {"": {"policy": "deny"}}}`
|
2015-10-13 23:43:52 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "ACL.Apply", &arg, &out); err != nil {
|
2014-08-18 22:23:02 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Resolve again
|
2018-10-19 16:04:07 +00:00
|
|
|
acl2, err := s1.ResolveToken(id)
|
2014-08-18 22:23:02 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if acl2 == nil {
|
|
|
|
t.Fatalf("should not be nil")
|
|
|
|
}
|
|
|
|
if acl2 == acl1 {
|
|
|
|
t.Fatalf("should not be cached")
|
|
|
|
}
|
|
|
|
if acl2.KeyRead("foo") {
|
|
|
|
t.Fatalf("should not be allowed")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do a delete
|
|
|
|
arg.Op = structs.ACLDelete
|
|
|
|
arg.ACL.Rules = ""
|
2015-10-13 23:43:52 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "ACL.Apply", &arg, &out); err != nil {
|
2014-08-18 22:23:02 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Resolve again
|
2018-10-19 16:04:07 +00:00
|
|
|
acl3, err := s1.ResolveToken(id)
|
2017-08-23 14:52:48 +00:00
|
|
|
if !acl.IsErrNotFound(err) {
|
2014-08-18 22:23:02 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if acl3 != nil {
|
|
|
|
t.Fatalf("should be nil")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-06 02:19:45 +00:00
|
|
|
func TestACLEndpoint_Apply_CustomID(t *testing.T) {
|
2017-06-27 13:22:18 +00:00
|
|
|
t.Parallel()
|
2015-05-06 02:19:45 +00:00
|
|
|
dir1, s1 := testServerWithConfig(t, func(c *Config) {
|
|
|
|
c.ACLDatacenter = "dc1"
|
2018-10-19 16:04:07 +00:00
|
|
|
c.ACLsEnabled = true
|
2015-05-06 02:19:45 +00:00
|
|
|
c.ACLMasterToken = "root"
|
|
|
|
})
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
2015-10-13 23:43:52 +00:00
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
defer codec.Close()
|
2015-05-06 02:19:45 +00:00
|
|
|
|
2017-04-19 23:00:11 +00:00
|
|
|
testrpc.WaitForLeader(t, s1.RPC, "dc1")
|
2015-05-06 02:19:45 +00:00
|
|
|
|
|
|
|
arg := structs.ACLRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Op: structs.ACLSet,
|
|
|
|
ACL: structs.ACL{
|
|
|
|
ID: "foobarbaz", // Specify custom ID, does not exist
|
|
|
|
Name: "User token",
|
2018-10-19 16:04:07 +00:00
|
|
|
Type: structs.ACLTokenTypeClient,
|
2015-05-06 02:19:45 +00:00
|
|
|
},
|
|
|
|
WriteRequest: structs.WriteRequest{Token: "root"},
|
|
|
|
}
|
|
|
|
var out string
|
2015-10-13 23:43:52 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "ACL.Apply", &arg, &out); err != nil {
|
2015-05-06 02:19:45 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if out != "foobarbaz" {
|
|
|
|
t.Fatalf("bad token ID: %s", out)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify
|
|
|
|
state := s1.fsm.State()
|
2018-10-19 16:04:07 +00:00
|
|
|
_, s, err := state.ACLTokenGetBySecret(nil, out)
|
2015-05-06 02:19:45 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if s == nil {
|
|
|
|
t.Fatalf("should not be nil")
|
|
|
|
}
|
2018-10-19 16:04:07 +00:00
|
|
|
if s.SecretID != out {
|
2015-05-06 02:19:45 +00:00
|
|
|
t.Fatalf("bad: %v", s)
|
|
|
|
}
|
2018-10-19 16:04:07 +00:00
|
|
|
if s.Description != "User token" {
|
2015-05-06 02:19:45 +00:00
|
|
|
t.Fatalf("bad: %v", s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-12 22:32:44 +00:00
|
|
|
func TestACLEndpoint_Apply_Denied(t *testing.T) {
|
2017-06-27 13:22:18 +00:00
|
|
|
t.Parallel()
|
2014-08-12 22:32:44 +00:00
|
|
|
dir1, s1 := testServerWithConfig(t, func(c *Config) {
|
|
|
|
c.ACLDatacenter = "dc1"
|
2018-10-19 16:04:07 +00:00
|
|
|
c.ACLsEnabled = true
|
2014-08-12 22:32:44 +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-12 22:32:44 +00:00
|
|
|
|
2017-04-19 23:00:11 +00:00
|
|
|
testrpc.WaitForLeader(t, s1.RPC, "dc1")
|
2014-08-12 22:32:44 +00:00
|
|
|
|
|
|
|
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,
|
2014-08-12 22:32:44 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
var out string
|
2015-10-13 23:43:52 +00:00
|
|
|
err := msgpackrpc.CallWithCodec(codec, "ACL.Apply", &arg, &out)
|
2017-08-23 14:52:48 +00:00
|
|
|
if !acl.IsErrPermissionDenied(err) {
|
2014-08-12 22:32:44 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-22 21:55:09 +00:00
|
|
|
func TestACLEndpoint_Apply_DeleteAnon(t *testing.T) {
|
2017-06-27 13:22:18 +00:00
|
|
|
t.Parallel()
|
2014-08-22 21:55:09 +00:00
|
|
|
dir1, s1 := testServerWithConfig(t, func(c *Config) {
|
|
|
|
c.ACLDatacenter = "dc1"
|
2018-10-19 16:04:07 +00:00
|
|
|
c.ACLsEnabled = true
|
2014-08-22 21:55:09 +00:00
|
|
|
c.ACLMasterToken = "root"
|
|
|
|
})
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
2015-10-13 23:43:52 +00:00
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
defer codec.Close()
|
2014-08-22 21:55:09 +00:00
|
|
|
|
2017-04-19 23:00:11 +00:00
|
|
|
testrpc.WaitForLeader(t, s1.RPC, "dc1")
|
2014-08-22 21:55:09 +00:00
|
|
|
|
|
|
|
arg := structs.ACLRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Op: structs.ACLDelete,
|
|
|
|
ACL: structs.ACL{
|
|
|
|
ID: anonymousToken,
|
|
|
|
Name: "User token",
|
2018-10-19 16:04:07 +00:00
|
|
|
Type: structs.ACLTokenTypeClient,
|
2014-08-22 21:55:09 +00:00
|
|
|
},
|
|
|
|
WriteRequest: structs.WriteRequest{Token: "root"},
|
|
|
|
}
|
|
|
|
var out string
|
2015-10-13 23:43:52 +00:00
|
|
|
err := msgpackrpc.CallWithCodec(codec, "ACL.Apply", &arg, &out)
|
2014-08-22 21:55:09 +00:00
|
|
|
if err == nil || !strings.Contains(err.Error(), "delete anonymous") {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestACLEndpoint_Apply_RootChange(t *testing.T) {
|
2017-06-27 13:22:18 +00:00
|
|
|
t.Parallel()
|
2014-08-22 21:55:09 +00:00
|
|
|
dir1, s1 := testServerWithConfig(t, func(c *Config) {
|
|
|
|
c.ACLDatacenter = "dc1"
|
2018-10-19 16:04:07 +00:00
|
|
|
c.ACLsEnabled = true
|
2014-08-22 21:55:09 +00:00
|
|
|
c.ACLMasterToken = "root"
|
|
|
|
})
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
2015-10-13 23:43:52 +00:00
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
defer codec.Close()
|
2014-08-22 21:55:09 +00:00
|
|
|
|
2017-04-19 23:00:11 +00:00
|
|
|
testrpc.WaitForLeader(t, s1.RPC, "dc1")
|
2014-08-22 21:55:09 +00:00
|
|
|
|
|
|
|
arg := structs.ACLRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Op: structs.ACLSet,
|
|
|
|
ACL: structs.ACL{
|
|
|
|
ID: "manage",
|
|
|
|
Name: "User token",
|
2018-10-19 16:04:07 +00:00
|
|
|
Type: structs.ACLTokenTypeClient,
|
2014-08-22 21:55:09 +00:00
|
|
|
},
|
|
|
|
WriteRequest: structs.WriteRequest{Token: "root"},
|
|
|
|
}
|
|
|
|
var out string
|
2015-10-13 23:43:52 +00:00
|
|
|
err := msgpackrpc.CallWithCodec(codec, "ACL.Apply", &arg, &out)
|
2014-08-22 21:55:09 +00:00
|
|
|
if err == nil || !strings.Contains(err.Error(), "root ACL") {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-06 00:24:48 +00:00
|
|
|
func TestACLEndpoint_Get(t *testing.T) {
|
2017-06-27 13:22:18 +00:00
|
|
|
t.Parallel()
|
2014-08-12 22:32:44 +00:00
|
|
|
dir1, s1 := testServerWithConfig(t, func(c *Config) {
|
|
|
|
c.ACLDatacenter = "dc1"
|
2018-10-19 16:04:07 +00:00
|
|
|
c.ACLsEnabled = true
|
2014-08-12 22:32:44 +00:00
|
|
|
c.ACLMasterToken = "root"
|
|
|
|
})
|
2014-08-06 00:24:48 +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-06 00:24:48 +00:00
|
|
|
|
2017-04-19 23:00:11 +00:00
|
|
|
testrpc.WaitForLeader(t, s1.RPC, "dc1")
|
2014-08-06 00:24:48 +00:00
|
|
|
|
|
|
|
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,
|
2014-08-06 00:24:48 +00:00
|
|
|
},
|
2014-08-12 22:32:44 +00:00
|
|
|
WriteRequest: structs.WriteRequest{Token: "root"},
|
2014-08-06 00:24:48 +00:00
|
|
|
}
|
|
|
|
var out string
|
2015-10-13 23:43:52 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "ACL.Apply", &arg, &out); err != nil {
|
2014-08-06 00:24:48 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
getR := structs.ACLSpecificRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
ACL: out,
|
|
|
|
}
|
|
|
|
var acls structs.IndexedACLs
|
2015-10-13 23:43:52 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "ACL.Get", &getR, &acls); err != nil {
|
2014-08-06 00:24:48 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if acls.Index == 0 {
|
|
|
|
t.Fatalf("Bad: %v", acls)
|
|
|
|
}
|
|
|
|
if len(acls.ACLs) != 1 {
|
|
|
|
t.Fatalf("Bad: %v", acls)
|
|
|
|
}
|
|
|
|
s := acls.ACLs[0]
|
|
|
|
if s.ID != out {
|
|
|
|
t.Fatalf("bad: %v", s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-08 22:32:43 +00:00
|
|
|
func TestACLEndpoint_GetPolicy(t *testing.T) {
|
2017-06-27 13:22:18 +00:00
|
|
|
t.Parallel()
|
2014-08-12 22:32:44 +00:00
|
|
|
dir1, s1 := testServerWithConfig(t, func(c *Config) {
|
|
|
|
c.ACLDatacenter = "dc1"
|
2018-10-19 16:04:07 +00:00
|
|
|
c.ACLsEnabled = true
|
2014-08-12 22:32:44 +00:00
|
|
|
c.ACLMasterToken = "root"
|
|
|
|
})
|
2014-08-08 22:32:43 +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-08 22:32:43 +00:00
|
|
|
|
2017-04-19 23:00:11 +00:00
|
|
|
testrpc.WaitForLeader(t, s1.RPC, "dc1")
|
2014-08-08 22:32:43 +00:00
|
|
|
|
|
|
|
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,
|
2014-08-08 22:32:43 +00:00
|
|
|
},
|
2014-08-12 22:32:44 +00:00
|
|
|
WriteRequest: structs.WriteRequest{Token: "root"},
|
2014-08-08 22:32:43 +00:00
|
|
|
}
|
|
|
|
var out string
|
2015-10-13 23:43:52 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "ACL.Apply", &arg, &out); err != nil {
|
2014-08-08 22:32:43 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
getR := structs.ACLPolicyResolveLegacyRequest{
|
2014-08-08 22:32:43 +00:00
|
|
|
Datacenter: "dc1",
|
|
|
|
ACL: out,
|
|
|
|
}
|
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
var acls structs.ACLPolicyResolveLegacyResponse
|
|
|
|
retry.Run(t, func(r *retry.R) {
|
|
|
|
|
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "ACL.GetPolicy", &getR, &acls); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if acls.Policy == nil {
|
|
|
|
t.Fatalf("Bad: %v", acls)
|
|
|
|
}
|
|
|
|
if acls.TTL != 30*time.Second {
|
|
|
|
t.Fatalf("bad: %v", acls)
|
|
|
|
}
|
|
|
|
})
|
2014-08-08 23:55:47 +00:00
|
|
|
|
|
|
|
// Do a conditional lookup with etag
|
|
|
|
getR.ETag = acls.ETag
|
2018-10-19 16:04:07 +00:00
|
|
|
var out2 structs.ACLPolicyResolveLegacyResponse
|
2015-10-13 23:43:52 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "ACL.GetPolicy", &getR, &out2); err != nil {
|
2014-08-08 23:55:47 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if out2.Policy != nil {
|
|
|
|
t.Fatalf("Bad: %v", out2)
|
|
|
|
}
|
|
|
|
if out2.TTL != 30*time.Second {
|
|
|
|
t.Fatalf("bad: %v", out2)
|
|
|
|
}
|
2014-08-08 22:32:43 +00:00
|
|
|
}
|
|
|
|
|
2014-08-06 00:24:48 +00:00
|
|
|
func TestACLEndpoint_List(t *testing.T) {
|
2017-06-27 13:22:18 +00:00
|
|
|
t.Parallel()
|
2014-08-12 22:32:44 +00:00
|
|
|
dir1, s1 := testServerWithConfig(t, func(c *Config) {
|
|
|
|
c.ACLDatacenter = "dc1"
|
2018-10-19 16:04:07 +00:00
|
|
|
c.ACLsEnabled = true
|
2014-08-12 22:32:44 +00:00
|
|
|
c.ACLMasterToken = "root"
|
|
|
|
})
|
2014-08-06 00:24:48 +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-06 00:24:48 +00:00
|
|
|
|
2017-04-19 23:00:11 +00:00
|
|
|
testrpc.WaitForLeader(t, s1.RPC, "dc1")
|
2014-08-06 00:24:48 +00:00
|
|
|
|
|
|
|
ids := []string{}
|
|
|
|
for i := 0; i < 5; i++ {
|
|
|
|
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,
|
2014-08-06 00:24:48 +00:00
|
|
|
},
|
2014-08-12 22:32:44 +00:00
|
|
|
WriteRequest: structs.WriteRequest{Token: "root"},
|
2014-08-06 00:24:48 +00:00
|
|
|
}
|
|
|
|
var out string
|
2015-10-13 23:43:52 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "ACL.Apply", &arg, &out); err != nil {
|
2014-08-06 00:24:48 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
ids = append(ids, out)
|
|
|
|
}
|
|
|
|
|
|
|
|
getR := structs.DCSpecificRequest{
|
2014-08-12 22:32:44 +00:00
|
|
|
Datacenter: "dc1",
|
|
|
|
QueryOptions: structs.QueryOptions{Token: "root"},
|
2014-08-06 00:24:48 +00:00
|
|
|
}
|
|
|
|
var acls structs.IndexedACLs
|
2015-10-13 23:43:52 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "ACL.List", &getR, &acls); err != nil {
|
2014-08-06 00:24:48 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if acls.Index == 0 {
|
|
|
|
t.Fatalf("Bad: %v", acls)
|
|
|
|
}
|
2014-08-12 22:32:44 +00:00
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
// 5 + master
|
|
|
|
if len(acls.ACLs) != 6 {
|
2014-08-06 00:24:48 +00:00
|
|
|
t.Fatalf("Bad: %v", acls.ACLs)
|
|
|
|
}
|
|
|
|
for i := 0; i < len(acls.ACLs); i++ {
|
|
|
|
s := acls.ACLs[i]
|
2014-08-12 22:32:44 +00:00
|
|
|
if s.ID == anonymousToken || s.ID == "root" {
|
|
|
|
continue
|
|
|
|
}
|
2016-01-29 19:42:34 +00:00
|
|
|
if !lib.StrContains(ids, s.ID) {
|
2014-08-06 00:24:48 +00:00
|
|
|
t.Fatalf("bad: %v", s)
|
|
|
|
}
|
|
|
|
if s.Name != "User token" {
|
|
|
|
t.Fatalf("bad: %v", s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-08-12 22:32:44 +00:00
|
|
|
|
|
|
|
func TestACLEndpoint_List_Denied(t *testing.T) {
|
2017-06-27 13:22:18 +00:00
|
|
|
t.Parallel()
|
2014-08-12 22:32:44 +00:00
|
|
|
dir1, s1 := testServerWithConfig(t, func(c *Config) {
|
|
|
|
c.ACLDatacenter = "dc1"
|
2018-10-19 16:04:07 +00:00
|
|
|
c.ACLsEnabled = true
|
2014-08-12 22:32:44 +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-12 22:32:44 +00:00
|
|
|
|
2017-04-19 23:00:11 +00:00
|
|
|
testrpc.WaitForLeader(t, s1.RPC, "dc1")
|
2014-08-12 22:32:44 +00:00
|
|
|
|
|
|
|
getR := structs.DCSpecificRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
}
|
|
|
|
var acls structs.IndexedACLs
|
2015-10-13 23:43:52 +00:00
|
|
|
err := msgpackrpc.CallWithCodec(codec, "ACL.List", &getR, &acls)
|
2017-08-23 14:52:48 +00:00
|
|
|
if !acl.IsErrPermissionDenied(err) {
|
2014-08-12 22:32:44 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
2016-08-05 04:32:36 +00:00
|
|
|
|
|
|
|
func TestACLEndpoint_ReplicationStatus(t *testing.T) {
|
2017-06-27 13:22:18 +00:00
|
|
|
t.Parallel()
|
2016-08-05 04:32:36 +00:00
|
|
|
dir1, s1 := testServerWithConfig(t, func(c *Config) {
|
|
|
|
c.ACLDatacenter = "dc2"
|
2018-10-19 16:04:07 +00:00
|
|
|
c.ACLsEnabled = true
|
|
|
|
c.ACLTokenReplication = true
|
|
|
|
c.ACLReplicationRate = 100
|
|
|
|
c.ACLReplicationBurst = 100
|
2016-08-05 04:32:36 +00:00
|
|
|
})
|
2017-08-03 22:39:31 +00:00
|
|
|
s1.tokens.UpdateACLReplicationToken("secret")
|
2016-08-05 04:32:36 +00:00
|
|
|
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-08-05 04:32:36 +00:00
|
|
|
|
|
|
|
getR := structs.DCSpecificRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
}
|
2017-05-04 22:35:33 +00:00
|
|
|
|
2017-05-04 22:52:53 +00:00
|
|
|
retry.Run(t, func(r *retry.R) {
|
2017-05-04 22:35:33 +00:00
|
|
|
var status structs.ACLReplicationStatus
|
|
|
|
err := msgpackrpc.CallWithCodec(codec, "ACL.ReplicationStatus", &getR, &status)
|
|
|
|
if err != nil {
|
|
|
|
r.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if !status.Enabled || !status.Running || status.SourceDatacenter != "dc2" {
|
|
|
|
r.Fatalf("bad: %#v", status)
|
|
|
|
}
|
|
|
|
})
|
2016-08-05 04:32:36 +00:00
|
|
|
}
|
2018-10-19 16:04:07 +00:00
|
|
|
|
|
|
|
func TestACLEndpoint_TokenRead(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
dir1, s1 := testServerWithConfig(t, func(c *Config) {
|
|
|
|
c.ACLDatacenter = "dc1"
|
|
|
|
c.ACLsEnabled = true
|
|
|
|
c.ACLMasterToken = "root"
|
|
|
|
})
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
defer codec.Close()
|
|
|
|
|
|
|
|
testrpc.WaitForLeader(t, s1.RPC, "dc1")
|
|
|
|
|
|
|
|
token, err := upsertTestToken(codec, "root", "dc1")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
acl := ACL{srv: s1}
|
|
|
|
|
|
|
|
// exists and matches what we created
|
|
|
|
{
|
2018-10-31 20:00:46 +00:00
|
|
|
req := structs.ACLTokenGetRequest{
|
2018-10-19 16:04:07 +00:00
|
|
|
Datacenter: "dc1",
|
|
|
|
TokenID: token.AccessorID,
|
|
|
|
TokenIDType: structs.ACLTokenAccessor,
|
|
|
|
QueryOptions: structs.QueryOptions{Token: "root"},
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := structs.ACLTokenResponse{}
|
|
|
|
|
|
|
|
err := acl.TokenRead(&req, &resp)
|
|
|
|
assert.NoError(err)
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(resp.Token, token) {
|
|
|
|
t.Fatalf("tokens are not equal: %v != %v", resp.Token, token)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// nil when token does not exist
|
|
|
|
{
|
|
|
|
fakeID, err := uuid.GenerateUUID()
|
|
|
|
assert.NoError(err)
|
|
|
|
|
2018-10-31 20:00:46 +00:00
|
|
|
req := structs.ACLTokenGetRequest{
|
2018-10-19 16:04:07 +00:00
|
|
|
Datacenter: "dc1",
|
|
|
|
TokenID: fakeID,
|
|
|
|
TokenIDType: structs.ACLTokenAccessor,
|
|
|
|
QueryOptions: structs.QueryOptions{Token: "root"},
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := structs.ACLTokenResponse{}
|
|
|
|
|
|
|
|
err = acl.TokenRead(&req, &resp)
|
|
|
|
assert.Nil(resp.Token)
|
|
|
|
assert.NoError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// validates ID format
|
|
|
|
{
|
2018-10-31 20:00:46 +00:00
|
|
|
req := structs.ACLTokenGetRequest{
|
2018-10-19 16:04:07 +00:00
|
|
|
Datacenter: "dc1",
|
|
|
|
TokenID: "definitely-really-certainly-not-a-uuid",
|
|
|
|
TokenIDType: structs.ACLTokenAccessor,
|
|
|
|
QueryOptions: structs.QueryOptions{Token: "root"},
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := structs.ACLTokenResponse{}
|
|
|
|
|
|
|
|
err := acl.TokenRead(&req, &resp)
|
|
|
|
assert.Nil(resp.Token)
|
|
|
|
assert.EqualError(err, "failed acl token lookup: failed acl token lookup: index error: UUID must be 36 characters")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestACLEndpoint_TokenClone(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
dir1, s1 := testServerWithConfig(t, func(c *Config) {
|
|
|
|
c.ACLDatacenter = "dc1"
|
|
|
|
c.ACLsEnabled = true
|
|
|
|
c.ACLMasterToken = "root"
|
|
|
|
})
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
defer codec.Close()
|
|
|
|
|
|
|
|
testrpc.WaitForLeader(t, s1.RPC, "dc1")
|
|
|
|
|
|
|
|
t1, err := upsertTestToken(codec, "root", "dc1")
|
|
|
|
assert.NoError(err)
|
|
|
|
|
|
|
|
acl := ACL{srv: s1}
|
|
|
|
|
2018-10-31 20:00:46 +00:00
|
|
|
req := structs.ACLTokenSetRequest{
|
2018-10-19 16:04:07 +00:00
|
|
|
Datacenter: "dc1",
|
|
|
|
ACLToken: structs.ACLToken{AccessorID: t1.AccessorID},
|
|
|
|
WriteRequest: structs.WriteRequest{Token: "root"},
|
|
|
|
}
|
|
|
|
|
|
|
|
t2 := structs.ACLToken{}
|
|
|
|
|
|
|
|
err = acl.TokenClone(&req, &t2)
|
|
|
|
assert.NoError(err)
|
|
|
|
|
|
|
|
assert.Equal(t1.Description, t2.Description)
|
|
|
|
assert.Equal(t1.Policies, t2.Policies)
|
|
|
|
assert.Equal(t1.Rules, t2.Rules)
|
|
|
|
assert.Equal(t1.Local, t2.Local)
|
|
|
|
assert.NotEqual(t1.AccessorID, t2.AccessorID)
|
|
|
|
assert.NotEqual(t1.SecretID, t2.SecretID)
|
|
|
|
}
|
|
|
|
|
2018-10-31 20:00:46 +00:00
|
|
|
func TestACLEndpoint_TokenSet(t *testing.T) {
|
2018-10-19 16:04:07 +00:00
|
|
|
t.Parallel()
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
dir1, s1 := testServerWithConfig(t, func(c *Config) {
|
|
|
|
c.ACLDatacenter = "dc1"
|
|
|
|
c.ACLsEnabled = true
|
|
|
|
c.ACLMasterToken = "root"
|
|
|
|
})
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
defer codec.Close()
|
|
|
|
|
|
|
|
testrpc.WaitForLeader(t, s1.RPC, "dc1")
|
|
|
|
|
|
|
|
acl := ACL{srv: s1}
|
|
|
|
var tokenID string
|
|
|
|
|
|
|
|
// Create it
|
|
|
|
{
|
2018-10-31 20:00:46 +00:00
|
|
|
req := structs.ACLTokenSetRequest{
|
2018-10-19 16:04:07 +00:00
|
|
|
Datacenter: "dc1",
|
|
|
|
ACLToken: structs.ACLToken{
|
|
|
|
Description: "foobar",
|
|
|
|
Policies: nil,
|
|
|
|
Local: false,
|
|
|
|
},
|
|
|
|
WriteRequest: structs.WriteRequest{Token: "root"},
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := structs.ACLToken{}
|
|
|
|
|
2018-10-31 20:00:46 +00:00
|
|
|
err := acl.TokenSet(&req, &resp)
|
2018-10-19 16:04:07 +00:00
|
|
|
assert.NoError(err)
|
|
|
|
|
|
|
|
// Get the token directly to validate that it exists
|
|
|
|
tokenResp, err := retrieveTestToken(codec, "root", "dc1", resp.AccessorID)
|
|
|
|
assert.NoError(err)
|
|
|
|
token := tokenResp.Token
|
|
|
|
|
|
|
|
assert.NotNil(token.AccessorID)
|
|
|
|
assert.Equal(token.Description, "foobar")
|
|
|
|
assert.Equal(token.AccessorID, resp.AccessorID)
|
|
|
|
|
|
|
|
tokenID = token.AccessorID
|
|
|
|
}
|
|
|
|
// Update it
|
|
|
|
{
|
2018-10-31 20:00:46 +00:00
|
|
|
req := structs.ACLTokenSetRequest{
|
2018-10-19 16:04:07 +00:00
|
|
|
Datacenter: "dc1",
|
|
|
|
ACLToken: structs.ACLToken{
|
|
|
|
Description: "new-description",
|
|
|
|
AccessorID: tokenID,
|
|
|
|
},
|
|
|
|
WriteRequest: structs.WriteRequest{Token: "root"},
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := structs.ACLToken{}
|
|
|
|
|
2018-10-31 20:00:46 +00:00
|
|
|
err := acl.TokenSet(&req, &resp)
|
2018-10-19 16:04:07 +00:00
|
|
|
assert.NoError(err)
|
|
|
|
|
|
|
|
// Get the token directly to validate that it exists
|
|
|
|
tokenResp, err := retrieveTestToken(codec, "root", "dc1", resp.AccessorID)
|
|
|
|
assert.NoError(err)
|
|
|
|
token := tokenResp.Token
|
|
|
|
|
|
|
|
assert.NotNil(token.AccessorID)
|
|
|
|
assert.Equal(token.Description, "new-description")
|
|
|
|
assert.Equal(token.AccessorID, resp.AccessorID)
|
|
|
|
}
|
|
|
|
}
|
2018-10-31 20:00:46 +00:00
|
|
|
func TestACLEndpoint_TokenSet_anon(t *testing.T) {
|
2018-10-19 16:04:07 +00:00
|
|
|
t.Parallel()
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
dir1, s1 := testServerWithConfig(t, func(c *Config) {
|
|
|
|
c.ACLDatacenter = "dc1"
|
|
|
|
c.ACLsEnabled = true
|
|
|
|
c.ACLMasterToken = "root"
|
|
|
|
})
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
defer codec.Close()
|
|
|
|
|
|
|
|
testrpc.WaitForLeader(t, s1.RPC, "dc1")
|
|
|
|
policy, err := upsertTestPolicy(codec, "root", "dc1")
|
|
|
|
assert.NoError(err)
|
|
|
|
|
|
|
|
acl := ACL{srv: s1}
|
|
|
|
|
|
|
|
// Assign the policies to a token
|
2018-10-31 20:00:46 +00:00
|
|
|
tokenUpsertReq := structs.ACLTokenSetRequest{
|
2018-10-19 16:04:07 +00:00
|
|
|
Datacenter: "dc1",
|
|
|
|
ACLToken: structs.ACLToken{
|
|
|
|
AccessorID: structs.ACLTokenAnonymousID,
|
|
|
|
Policies: []structs.ACLTokenPolicyLink{
|
|
|
|
structs.ACLTokenPolicyLink{
|
|
|
|
ID: policy.ID,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
WriteRequest: structs.WriteRequest{Token: "root"},
|
|
|
|
}
|
|
|
|
token := structs.ACLToken{}
|
2018-10-31 20:00:46 +00:00
|
|
|
err = acl.TokenSet(&tokenUpsertReq, &token)
|
2018-10-19 16:04:07 +00:00
|
|
|
assert.NoError(err)
|
|
|
|
assert.NotEmpty(token.SecretID)
|
|
|
|
|
|
|
|
tokenResp, err := retrieveTestToken(codec, "root", "dc1", structs.ACLTokenAnonymousID)
|
|
|
|
assert.Equal(len(tokenResp.Token.Policies), 1)
|
|
|
|
assert.Equal(tokenResp.Token.Policies[0].ID, policy.ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestACLEndpoint_TokenDelete(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
dir1, s1 := testServerWithConfig(t, func(c *Config) {
|
|
|
|
c.ACLDatacenter = "dc1"
|
|
|
|
c.ACLsEnabled = true
|
|
|
|
c.ACLMasterToken = "root"
|
|
|
|
})
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
defer codec.Close()
|
|
|
|
|
|
|
|
testrpc.WaitForLeader(t, s1.RPC, "dc1")
|
|
|
|
|
|
|
|
existingToken, err := upsertTestToken(codec, "root", "dc1")
|
|
|
|
assert.NoError(err)
|
|
|
|
|
|
|
|
acl := ACL{srv: s1}
|
|
|
|
|
|
|
|
// deletes a token
|
|
|
|
{
|
|
|
|
req := structs.ACLTokenDeleteRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
TokenID: existingToken.AccessorID,
|
|
|
|
WriteRequest: structs.WriteRequest{Token: "root"},
|
|
|
|
}
|
|
|
|
|
|
|
|
var resp string
|
|
|
|
|
|
|
|
err = acl.TokenDelete(&req, &resp)
|
|
|
|
assert.NoError(err)
|
|
|
|
|
|
|
|
// Make sure the token is gone
|
|
|
|
tokenResp, err := retrieveTestToken(codec, "root", "dc1", existingToken.AccessorID)
|
|
|
|
assert.Nil(tokenResp.Token)
|
|
|
|
assert.NoError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// errors when token doesn't exist
|
|
|
|
{
|
|
|
|
fakeID, err := uuid.GenerateUUID()
|
|
|
|
assert.NoError(err)
|
|
|
|
|
|
|
|
req := structs.ACLTokenDeleteRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
TokenID: fakeID,
|
|
|
|
WriteRequest: structs.WriteRequest{Token: "root"},
|
|
|
|
}
|
|
|
|
|
|
|
|
var resp string
|
|
|
|
|
|
|
|
err = acl.TokenDelete(&req, &resp)
|
|
|
|
assert.NoError(err)
|
|
|
|
|
|
|
|
// token should be nil
|
|
|
|
tokenResp, err := retrieveTestToken(codec, "root", "dc1", existingToken.AccessorID)
|
|
|
|
assert.Nil(tokenResp.Token)
|
|
|
|
assert.NoError(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
func TestACLEndpoint_TokenDelete_anon(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
dir1, s1 := testServerWithConfig(t, func(c *Config) {
|
|
|
|
c.ACLDatacenter = "dc1"
|
|
|
|
c.ACLsEnabled = true
|
|
|
|
c.ACLMasterToken = "root"
|
|
|
|
})
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
defer codec.Close()
|
|
|
|
|
|
|
|
testrpc.WaitForLeader(t, s1.RPC, "dc1")
|
|
|
|
|
|
|
|
acl := ACL{srv: s1}
|
|
|
|
|
|
|
|
req := structs.ACLTokenDeleteRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
TokenID: structs.ACLTokenAnonymousID,
|
|
|
|
WriteRequest: structs.WriteRequest{Token: "root"},
|
|
|
|
}
|
|
|
|
|
|
|
|
var resp string
|
|
|
|
|
|
|
|
err := acl.TokenDelete(&req, &resp)
|
|
|
|
assert.EqualError(err, "Delete operation not permitted on the anonymous token")
|
|
|
|
|
|
|
|
// Make sure the token is still there
|
|
|
|
tokenResp, err := retrieveTestToken(codec, "root", "dc1", structs.ACLTokenAnonymousID)
|
|
|
|
assert.NotNil(tokenResp.Token)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestACLEndpoint_TokenList(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
dir1, s1 := testServerWithConfig(t, func(c *Config) {
|
|
|
|
c.ACLDatacenter = "dc1"
|
|
|
|
c.ACLsEnabled = true
|
|
|
|
c.ACLMasterToken = "root"
|
|
|
|
})
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
defer codec.Close()
|
|
|
|
|
|
|
|
testrpc.WaitForLeader(t, s1.RPC, "dc1")
|
|
|
|
|
|
|
|
t1, err := upsertTestToken(codec, "root", "dc1")
|
|
|
|
assert.NoError(err)
|
|
|
|
|
|
|
|
t2, err := upsertTestToken(codec, "root", "dc1")
|
|
|
|
assert.NoError(err)
|
|
|
|
|
|
|
|
acl := ACL{srv: s1}
|
|
|
|
|
|
|
|
req := structs.ACLTokenListRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
QueryOptions: structs.QueryOptions{Token: "root"},
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := structs.ACLTokenListResponse{}
|
|
|
|
|
|
|
|
err = acl.TokenList(&req, &resp)
|
|
|
|
assert.NoError(err)
|
|
|
|
|
|
|
|
tokens := []string{t1.AccessorID, t2.AccessorID}
|
|
|
|
var retrievedTokens []string
|
|
|
|
|
|
|
|
for _, v := range resp.Tokens {
|
|
|
|
retrievedTokens = append(retrievedTokens, v.AccessorID)
|
|
|
|
}
|
|
|
|
assert.Subset(retrievedTokens, tokens)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestACLEndpoint_TokenBatchRead(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
dir1, s1 := testServerWithConfig(t, func(c *Config) {
|
|
|
|
c.ACLDatacenter = "dc1"
|
|
|
|
c.ACLsEnabled = true
|
|
|
|
c.ACLMasterToken = "root"
|
|
|
|
})
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
defer codec.Close()
|
|
|
|
|
|
|
|
testrpc.WaitForLeader(t, s1.RPC, "dc1")
|
|
|
|
|
|
|
|
t1, err := upsertTestToken(codec, "root", "dc1")
|
|
|
|
assert.NoError(err)
|
|
|
|
|
|
|
|
t2, err := upsertTestToken(codec, "root", "dc1")
|
|
|
|
assert.NoError(err)
|
|
|
|
|
|
|
|
acl := ACL{srv: s1}
|
|
|
|
tokens := []string{t1.AccessorID, t2.AccessorID}
|
|
|
|
|
2018-10-31 20:00:46 +00:00
|
|
|
req := structs.ACLTokenBatchGetRequest{
|
2018-10-19 16:04:07 +00:00
|
|
|
Datacenter: "dc1",
|
|
|
|
AccessorIDs: tokens,
|
|
|
|
QueryOptions: structs.QueryOptions{Token: "root"},
|
|
|
|
}
|
|
|
|
|
2018-10-31 20:00:46 +00:00
|
|
|
resp := structs.ACLTokenBatchResponse{}
|
2018-10-19 16:04:07 +00:00
|
|
|
|
|
|
|
err = acl.TokenBatchRead(&req, &resp)
|
|
|
|
assert.NoError(err)
|
|
|
|
|
|
|
|
var retrievedTokens []string
|
|
|
|
|
|
|
|
for _, v := range resp.Tokens {
|
|
|
|
retrievedTokens = append(retrievedTokens, v.AccessorID)
|
|
|
|
}
|
|
|
|
assert.EqualValues(retrievedTokens, tokens)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestACLEndpoint_PolicyRead(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
dir1, s1 := testServerWithConfig(t, func(c *Config) {
|
|
|
|
c.ACLDatacenter = "dc1"
|
|
|
|
c.ACLsEnabled = true
|
|
|
|
c.ACLMasterToken = "root"
|
|
|
|
})
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
defer codec.Close()
|
|
|
|
|
|
|
|
testrpc.WaitForLeader(t, s1.RPC, "dc1")
|
|
|
|
|
|
|
|
policy, err := upsertTestPolicy(codec, "root", "dc1")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
acl := ACL{srv: s1}
|
|
|
|
|
2018-10-31 20:00:46 +00:00
|
|
|
req := structs.ACLPolicyGetRequest{
|
2018-10-19 16:04:07 +00:00
|
|
|
Datacenter: "dc1",
|
|
|
|
PolicyID: policy.ID,
|
|
|
|
QueryOptions: structs.QueryOptions{Token: "root"},
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := structs.ACLPolicyResponse{}
|
|
|
|
|
|
|
|
err = acl.PolicyRead(&req, &resp)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(resp.Policy, policy) {
|
|
|
|
t.Fatalf("tokens are not equal: %v != %v", resp.Policy, policy)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestACLEndpoint_PolicyBatchRead(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
dir1, s1 := testServerWithConfig(t, func(c *Config) {
|
|
|
|
c.ACLDatacenter = "dc1"
|
|
|
|
c.ACLsEnabled = true
|
|
|
|
c.ACLMasterToken = "root"
|
|
|
|
})
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
defer codec.Close()
|
|
|
|
|
|
|
|
testrpc.WaitForLeader(t, s1.RPC, "dc1")
|
|
|
|
|
|
|
|
t1, err := upsertTestToken(codec, "root", "dc1")
|
|
|
|
assert.NoError(err)
|
|
|
|
|
|
|
|
t2, err := upsertTestToken(codec, "root", "dc1")
|
|
|
|
assert.NoError(err)
|
|
|
|
|
|
|
|
acl := ACL{srv: s1}
|
|
|
|
tokens := []string{t1.AccessorID, t2.AccessorID}
|
|
|
|
|
2018-10-31 20:00:46 +00:00
|
|
|
req := structs.ACLTokenBatchGetRequest{
|
2018-10-19 16:04:07 +00:00
|
|
|
Datacenter: "dc1",
|
|
|
|
AccessorIDs: tokens,
|
|
|
|
QueryOptions: structs.QueryOptions{Token: "root"},
|
|
|
|
}
|
|
|
|
|
2018-10-31 20:00:46 +00:00
|
|
|
resp := structs.ACLTokenBatchResponse{}
|
2018-10-19 16:04:07 +00:00
|
|
|
|
|
|
|
err = acl.TokenBatchRead(&req, &resp)
|
|
|
|
assert.NoError(err)
|
|
|
|
|
|
|
|
var retrievedTokens []string
|
|
|
|
|
|
|
|
for _, v := range resp.Tokens {
|
|
|
|
retrievedTokens = append(retrievedTokens, v.AccessorID)
|
|
|
|
}
|
|
|
|
assert.EqualValues(retrievedTokens, tokens)
|
|
|
|
}
|
|
|
|
|
2018-10-31 20:00:46 +00:00
|
|
|
func TestACLEndpoint_PolicySet(t *testing.T) {
|
2018-10-19 16:04:07 +00:00
|
|
|
t.Parallel()
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
dir1, s1 := testServerWithConfig(t, func(c *Config) {
|
|
|
|
c.ACLDatacenter = "dc1"
|
|
|
|
c.ACLsEnabled = true
|
|
|
|
c.ACLMasterToken = "root"
|
|
|
|
})
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
defer codec.Close()
|
|
|
|
|
|
|
|
testrpc.WaitForLeader(t, s1.RPC, "dc1")
|
|
|
|
|
|
|
|
acl := ACL{srv: s1}
|
|
|
|
var policyID string
|
|
|
|
|
|
|
|
// Create it
|
|
|
|
{
|
2018-10-31 20:00:46 +00:00
|
|
|
req := structs.ACLPolicySetRequest{
|
2018-10-19 16:04:07 +00:00
|
|
|
Datacenter: "dc1",
|
|
|
|
Policy: structs.ACLPolicy{
|
|
|
|
Description: "foobar",
|
|
|
|
Name: "baz",
|
|
|
|
Rules: "service \"\" { policy = \"read\" }",
|
|
|
|
},
|
|
|
|
WriteRequest: structs.WriteRequest{Token: "root"},
|
|
|
|
}
|
|
|
|
resp := structs.ACLPolicy{}
|
|
|
|
|
2018-10-31 20:00:46 +00:00
|
|
|
err := acl.PolicySet(&req, &resp)
|
2018-10-19 16:04:07 +00:00
|
|
|
assert.NoError(err)
|
|
|
|
assert.NotNil(resp.ID)
|
|
|
|
|
|
|
|
// Get the policy directly to validate that it exists
|
|
|
|
policyResp, err := retrieveTestPolicy(codec, "root", "dc1", resp.ID)
|
|
|
|
assert.NoError(err)
|
|
|
|
policy := policyResp.Policy
|
|
|
|
|
|
|
|
assert.NotNil(policy.ID)
|
|
|
|
assert.Equal(policy.Description, "foobar")
|
|
|
|
assert.Equal(policy.Name, "baz")
|
|
|
|
assert.Equal(policy.Rules, "service \"\" { policy = \"read\" }")
|
|
|
|
|
|
|
|
policyID = policy.ID
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update it
|
|
|
|
{
|
2018-10-31 20:00:46 +00:00
|
|
|
req := structs.ACLPolicySetRequest{
|
2018-10-19 16:04:07 +00:00
|
|
|
Datacenter: "dc1",
|
|
|
|
Policy: structs.ACLPolicy{
|
|
|
|
ID: policyID,
|
|
|
|
Description: "bat",
|
|
|
|
Name: "bar",
|
|
|
|
Rules: "service \"\" { policy = \"write\" }",
|
|
|
|
},
|
|
|
|
WriteRequest: structs.WriteRequest{Token: "root"},
|
|
|
|
}
|
|
|
|
resp := structs.ACLPolicy{}
|
|
|
|
|
2018-10-31 20:00:46 +00:00
|
|
|
err := acl.PolicySet(&req, &resp)
|
2018-10-19 16:04:07 +00:00
|
|
|
assert.NoError(err)
|
|
|
|
assert.NotNil(resp.ID)
|
|
|
|
|
|
|
|
// Get the policy directly to validate that it exists
|
|
|
|
policyResp, err := retrieveTestPolicy(codec, "root", "dc1", resp.ID)
|
|
|
|
assert.NoError(err)
|
|
|
|
policy := policyResp.Policy
|
|
|
|
|
|
|
|
assert.NotNil(policy.ID)
|
|
|
|
assert.Equal(policy.Description, "bat")
|
|
|
|
assert.Equal(policy.Name, "bar")
|
|
|
|
assert.Equal(policy.Rules, "service \"\" { policy = \"write\" }")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-31 20:00:46 +00:00
|
|
|
func TestACLEndpoint_PolicySet_globalManagement(t *testing.T) {
|
2018-10-19 16:04:07 +00:00
|
|
|
t.Parallel()
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
dir1, s1 := testServerWithConfig(t, func(c *Config) {
|
|
|
|
c.ACLDatacenter = "dc1"
|
|
|
|
c.ACLsEnabled = true
|
|
|
|
c.ACLMasterToken = "root"
|
|
|
|
})
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
defer codec.Close()
|
|
|
|
|
|
|
|
testrpc.WaitForLeader(t, s1.RPC, "dc1")
|
|
|
|
|
|
|
|
acl := ACL{srv: s1}
|
|
|
|
|
|
|
|
// Can't change the rules
|
|
|
|
{
|
|
|
|
|
2018-10-31 20:00:46 +00:00
|
|
|
req := structs.ACLPolicySetRequest{
|
2018-10-19 16:04:07 +00:00
|
|
|
Datacenter: "dc1",
|
|
|
|
Policy: structs.ACLPolicy{
|
|
|
|
ID: structs.ACLPolicyGlobalManagementID,
|
|
|
|
Name: "foobar", // This is required to get past validation
|
|
|
|
Rules: "service \"\" { policy = \"write\" }",
|
|
|
|
},
|
|
|
|
WriteRequest: structs.WriteRequest{Token: "root"},
|
|
|
|
}
|
|
|
|
resp := structs.ACLPolicy{}
|
|
|
|
|
2018-10-31 20:00:46 +00:00
|
|
|
err := acl.PolicySet(&req, &resp)
|
2018-10-19 16:04:07 +00:00
|
|
|
assert.EqualError(err, "Changing the Rules for the builtin global-management policy is not permitted")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Can rename it
|
|
|
|
{
|
2018-10-31 20:00:46 +00:00
|
|
|
req := structs.ACLPolicySetRequest{
|
2018-10-19 16:04:07 +00:00
|
|
|
Datacenter: "dc1",
|
|
|
|
Policy: structs.ACLPolicy{
|
|
|
|
ID: structs.ACLPolicyGlobalManagementID,
|
|
|
|
Name: "foobar",
|
|
|
|
Rules: structs.ACLPolicyGlobalManagement,
|
|
|
|
},
|
|
|
|
WriteRequest: structs.WriteRequest{Token: "root"},
|
|
|
|
}
|
|
|
|
resp := structs.ACLPolicy{}
|
|
|
|
|
2018-10-31 20:00:46 +00:00
|
|
|
err := acl.PolicySet(&req, &resp)
|
2018-10-19 16:04:07 +00:00
|
|
|
assert.NoError(err)
|
|
|
|
|
|
|
|
// Get the policy again
|
|
|
|
policyResp, err := retrieveTestPolicy(codec, "root", "dc1", structs.ACLPolicyGlobalManagementID)
|
|
|
|
assert.NoError(err)
|
|
|
|
policy := policyResp.Policy
|
|
|
|
|
|
|
|
assert.Equal(policy.ID, structs.ACLPolicyGlobalManagementID)
|
|
|
|
assert.Equal(policy.Name, "foobar")
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestACLEndpoint_PolicyDelete(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
dir1, s1 := testServerWithConfig(t, func(c *Config) {
|
|
|
|
c.ACLDatacenter = "dc1"
|
|
|
|
c.ACLsEnabled = true
|
|
|
|
c.ACLMasterToken = "root"
|
|
|
|
})
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
defer codec.Close()
|
|
|
|
|
|
|
|
testrpc.WaitForLeader(t, s1.RPC, "dc1")
|
|
|
|
|
|
|
|
existingPolicy, err := upsertTestPolicy(codec, "root", "dc1")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
acl := ACL{srv: s1}
|
|
|
|
|
|
|
|
req := structs.ACLPolicyDeleteRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
PolicyID: existingPolicy.ID,
|
|
|
|
WriteRequest: structs.WriteRequest{Token: "root"},
|
|
|
|
}
|
|
|
|
|
|
|
|
var resp string
|
|
|
|
|
|
|
|
err = acl.PolicyDelete(&req, &resp)
|
|
|
|
assert.NoError(err)
|
|
|
|
|
|
|
|
// Make sure the policy is gone
|
|
|
|
tokenResp, err := retrieveTestPolicy(codec, "root", "dc1", existingPolicy.ID)
|
|
|
|
assert.Nil(tokenResp.Policy)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestACLEndpoint_PolicyDelete_globalManagement(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
dir1, s1 := testServerWithConfig(t, func(c *Config) {
|
|
|
|
c.ACLDatacenter = "dc1"
|
|
|
|
c.ACLsEnabled = true
|
|
|
|
c.ACLMasterToken = "root"
|
|
|
|
})
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
defer codec.Close()
|
|
|
|
|
|
|
|
testrpc.WaitForLeader(t, s1.RPC, "dc1")
|
|
|
|
|
|
|
|
acl := ACL{srv: s1}
|
|
|
|
|
|
|
|
req := structs.ACLPolicyDeleteRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
PolicyID: structs.ACLPolicyGlobalManagementID,
|
|
|
|
WriteRequest: structs.WriteRequest{Token: "root"},
|
|
|
|
}
|
|
|
|
var resp string
|
|
|
|
|
|
|
|
err := acl.PolicyDelete(&req, &resp)
|
|
|
|
|
|
|
|
assert.EqualError(err, "Delete operation not permitted on the builtin global-management policy")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestACLEndpoint_PolicyList(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
dir1, s1 := testServerWithConfig(t, func(c *Config) {
|
|
|
|
c.ACLDatacenter = "dc1"
|
|
|
|
c.ACLsEnabled = true
|
|
|
|
c.ACLMasterToken = "root"
|
|
|
|
})
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
defer codec.Close()
|
|
|
|
|
|
|
|
testrpc.WaitForLeader(t, s1.RPC, "dc1")
|
|
|
|
|
|
|
|
p1, err := upsertTestPolicy(codec, "root", "dc1")
|
|
|
|
assert.NoError(err)
|
|
|
|
|
|
|
|
p2, err := upsertTestPolicy(codec, "root", "dc1")
|
|
|
|
assert.NoError(err)
|
|
|
|
|
|
|
|
acl := ACL{srv: s1}
|
|
|
|
|
|
|
|
req := structs.ACLPolicyListRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
QueryOptions: structs.QueryOptions{Token: "root"},
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := structs.ACLPolicyListResponse{}
|
|
|
|
|
|
|
|
err = acl.PolicyList(&req, &resp)
|
|
|
|
assert.NoError(err)
|
|
|
|
|
|
|
|
policies := []string{p1.ID, p2.ID}
|
|
|
|
var retrievedPolicies []string
|
|
|
|
|
|
|
|
for _, v := range resp.Policies {
|
|
|
|
retrievedPolicies = append(retrievedPolicies, v.ID)
|
|
|
|
}
|
|
|
|
assert.Subset(retrievedPolicies, policies)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestACLEndpoint_PolicyResolve(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
dir1, s1 := testServerWithConfig(t, func(c *Config) {
|
|
|
|
c.ACLDatacenter = "dc1"
|
|
|
|
c.ACLsEnabled = true
|
|
|
|
c.ACLMasterToken = "root"
|
|
|
|
})
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
defer codec.Close()
|
|
|
|
|
|
|
|
testrpc.WaitForLeader(t, s1.RPC, "dc1")
|
|
|
|
|
|
|
|
p1, err := upsertTestPolicy(codec, "root", "dc1")
|
|
|
|
assert.NoError(err)
|
|
|
|
|
|
|
|
p2, err := upsertTestPolicy(codec, "root", "dc1")
|
|
|
|
assert.NoError(err)
|
|
|
|
|
|
|
|
acl := ACL{srv: s1}
|
|
|
|
|
|
|
|
policies := []string{p1.ID, p2.ID}
|
|
|
|
|
|
|
|
// Assign the policies to a token
|
2018-10-31 20:00:46 +00:00
|
|
|
tokenUpsertReq := structs.ACLTokenSetRequest{
|
2018-10-19 16:04:07 +00:00
|
|
|
Datacenter: "dc1",
|
|
|
|
ACLToken: structs.ACLToken{
|
|
|
|
Policies: []structs.ACLTokenPolicyLink{
|
|
|
|
structs.ACLTokenPolicyLink{
|
|
|
|
ID: p1.ID,
|
|
|
|
},
|
|
|
|
structs.ACLTokenPolicyLink{
|
|
|
|
ID: p2.ID,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
WriteRequest: structs.WriteRequest{Token: "root"},
|
|
|
|
}
|
|
|
|
token := structs.ACLToken{}
|
2018-10-31 20:00:46 +00:00
|
|
|
err = acl.TokenSet(&tokenUpsertReq, &token)
|
2018-10-19 16:04:07 +00:00
|
|
|
assert.NoError(err)
|
|
|
|
assert.NotEmpty(token.SecretID)
|
|
|
|
|
2018-10-31 20:00:46 +00:00
|
|
|
resp := structs.ACLPolicyBatchResponse{}
|
|
|
|
req := structs.ACLPolicyBatchGetRequest{
|
2018-10-19 16:04:07 +00:00
|
|
|
Datacenter: "dc1",
|
|
|
|
PolicyIDs: []string{p1.ID, p2.ID},
|
|
|
|
QueryOptions: structs.QueryOptions{Token: token.SecretID},
|
|
|
|
}
|
|
|
|
err = acl.PolicyResolve(&req, &resp)
|
|
|
|
assert.NoError(err)
|
|
|
|
|
|
|
|
var retrievedPolicies []string
|
|
|
|
|
|
|
|
for _, v := range resp.Policies {
|
|
|
|
retrievedPolicies = append(retrievedPolicies, v.ID)
|
|
|
|
}
|
|
|
|
assert.EqualValues(retrievedPolicies, policies)
|
|
|
|
}
|
|
|
|
|
|
|
|
// upsertTestToken creates a token for testing purposes
|
|
|
|
func upsertTestToken(codec rpc.ClientCodec, masterToken string, datacenter string) (*structs.ACLToken, error) {
|
2018-10-31 20:00:46 +00:00
|
|
|
arg := structs.ACLTokenSetRequest{
|
2018-10-19 16:04:07 +00:00
|
|
|
Datacenter: datacenter,
|
|
|
|
ACLToken: structs.ACLToken{
|
|
|
|
Description: "User token",
|
|
|
|
Local: false,
|
|
|
|
Policies: nil,
|
|
|
|
},
|
|
|
|
WriteRequest: structs.WriteRequest{Token: masterToken},
|
|
|
|
}
|
|
|
|
|
|
|
|
var out structs.ACLToken
|
|
|
|
|
2018-10-31 20:00:46 +00:00
|
|
|
err := msgpackrpc.CallWithCodec(codec, "ACL.TokenSet", &arg, &out)
|
2018-10-19 16:04:07 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if out.AccessorID == "" {
|
|
|
|
return nil, fmt.Errorf("AccessorID is nil: %v", out)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &out, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// retrieveTestToken returns a policy for testing purposes
|
|
|
|
func retrieveTestToken(codec rpc.ClientCodec, masterToken string, datacenter string, id string) (*structs.ACLTokenResponse, error) {
|
2018-10-31 20:00:46 +00:00
|
|
|
arg := structs.ACLTokenGetRequest{
|
2018-10-19 16:04:07 +00:00
|
|
|
Datacenter: datacenter,
|
|
|
|
TokenID: id,
|
|
|
|
TokenIDType: structs.ACLTokenAccessor,
|
|
|
|
QueryOptions: structs.QueryOptions{Token: masterToken},
|
|
|
|
}
|
|
|
|
|
|
|
|
var out structs.ACLTokenResponse
|
|
|
|
|
|
|
|
err := msgpackrpc.CallWithCodec(codec, "ACL.TokenRead", &arg, &out)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &out, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// upsertTestPolicy creates a policy for testing purposes
|
|
|
|
func upsertTestPolicy(codec rpc.ClientCodec, masterToken string, datacenter string) (*structs.ACLPolicy, error) {
|
|
|
|
// Make sure test policies can't collide
|
|
|
|
policyUnq, err := uuid.GenerateUUID()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-10-31 20:00:46 +00:00
|
|
|
arg := structs.ACLPolicySetRequest{
|
2018-10-19 16:04:07 +00:00
|
|
|
Datacenter: datacenter,
|
|
|
|
Policy: structs.ACLPolicy{
|
|
|
|
Name: fmt.Sprintf("test-policy-%s", policyUnq),
|
|
|
|
},
|
|
|
|
WriteRequest: structs.WriteRequest{Token: masterToken},
|
|
|
|
}
|
|
|
|
|
|
|
|
var out structs.ACLPolicy
|
|
|
|
|
2018-10-31 20:00:46 +00:00
|
|
|
err = msgpackrpc.CallWithCodec(codec, "ACL.PolicySet", &arg, &out)
|
2018-10-19 16:04:07 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if out.ID == "" {
|
|
|
|
return nil, fmt.Errorf("ID is nil: %v", out)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &out, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// retrieveTestPolicy returns a policy for testing purposes
|
|
|
|
func retrieveTestPolicy(codec rpc.ClientCodec, masterToken string, datacenter string, id string) (*structs.ACLPolicyResponse, error) {
|
2018-10-31 20:00:46 +00:00
|
|
|
arg := structs.ACLPolicyGetRequest{
|
2018-10-19 16:04:07 +00:00
|
|
|
Datacenter: datacenter,
|
|
|
|
PolicyID: id,
|
|
|
|
QueryOptions: structs.QueryOptions{Token: masterToken},
|
|
|
|
}
|
|
|
|
|
|
|
|
var out structs.ACLPolicyResponse
|
|
|
|
|
|
|
|
err := msgpackrpc.CallWithCodec(codec, "ACL.PolicyRead", &arg, &out)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &out, nil
|
|
|
|
}
|